Ajout de la gestion des heredocs (pas testé a fond, push avant de partir)
This commit is contained in:
parent
5d09281cb1
commit
691eef6800
4 changed files with 84 additions and 9 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */
|
||||
/* Updated: 2017/01/24 15:14:05 by gwojda ### ########.fr */
|
||||
/* Updated: 2017/01/24 16:42:30 by gwojda ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -62,8 +62,10 @@ typedef struct s_line
|
|||
int prompt_size;
|
||||
int list_size;
|
||||
t_list_history *list_end;
|
||||
t_list_history *list_beg;
|
||||
} t_line;
|
||||
|
||||
void ft_check_heredoc(char **str);
|
||||
void ft_history_builtin(void);
|
||||
int ft_nbr_len(int nbr);
|
||||
int ft_found_next_char(char *str, size_t i);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/24 14:54:53 by gwojda #+# #+# */
|
||||
/* Updated: 2017/01/24 15:26:28 by gwojda ### ########.fr */
|
||||
/* Updated: 2017/01/24 16:44:13 by gwojda ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -28,6 +28,6 @@ void ft_history_builtin(void)
|
|||
ft_putnc(' ', ft_nbr_len(data_singleton()->line.list_size) - ft_nbr_len(len));
|
||||
ft_printf("%zu %s\n", len, head->str);
|
||||
++len;
|
||||
head = head->nextm;
|
||||
head = head->next;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
71
42sh/src/line-editing/heredoc.c
Normal file
71
42sh/src/line-editing/heredoc.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* heredoc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/24 15:52:34 by gwojda #+# #+# */
|
||||
/* Updated: 2017/01/24 17:07:21 by gwojda ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
char *ft_get_next_word(char *str)
|
||||
{
|
||||
int j;
|
||||
int k;
|
||||
|
||||
j = 0;
|
||||
k = 0;
|
||||
while (str[j] && str[j] == ' ')
|
||||
++j;
|
||||
while (str[j + k] && str[j + k] != ' ')
|
||||
++k;
|
||||
return (ft_strndup(str + j, k));
|
||||
}
|
||||
|
||||
void ft_check_heredoc(char **str)
|
||||
{
|
||||
int i;
|
||||
char boolean;
|
||||
char *end;
|
||||
char *tmp;
|
||||
char *tmp2;
|
||||
|
||||
i = 0;
|
||||
end = NULL;
|
||||
boolean = 0;
|
||||
if (!*str)
|
||||
return ;
|
||||
while ((*str)[i])
|
||||
{
|
||||
if ((*str)[i] == '<' && !ft_strncmp(*str + i, "<<", 2))
|
||||
{
|
||||
if ((*str)[i + 1] == '<' && (*str)[i + 2])
|
||||
end = ft_get_next_word(*str + i + 2);
|
||||
else
|
||||
return ;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
if (!end)
|
||||
return ;
|
||||
while (!boolean)
|
||||
{
|
||||
ft_putstr("heredoc> ");
|
||||
data_singleton()->line.prompt_size = 9;
|
||||
tmp = *str;
|
||||
*str = ft_strjoin(*str, "\n");
|
||||
free(tmp);
|
||||
tmp = *str;
|
||||
tmp2 = ft_lecture(data_singleton()->line.list_beg);
|
||||
if (!ft_strcmp(end, tmp2))
|
||||
boolean = 1;
|
||||
*str = ft_strjoin(tmp, tmp2);
|
||||
free(tmp);
|
||||
free(tmp2);
|
||||
ft_putchar('\n');
|
||||
}
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */
|
||||
/* Updated: 2017/01/24 15:29:59 by gwojda ### ########.fr */
|
||||
/* Updated: 2017/01/24 16:58:30 by gwojda ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -18,6 +18,7 @@ void ft_init_line(void)
|
|||
data_singleton()->line.prompt_size = 0;
|
||||
data_singleton()->line.list_size = 0;
|
||||
data_singleton()->line.list_end = NULL;
|
||||
data_singleton()->line.list_beg = NULL;
|
||||
}
|
||||
|
||||
struct termios *ft_save_stats_term(void)
|
||||
|
|
@ -38,6 +39,7 @@ struct termios *ft_stats_term_termcaps(void)
|
|||
|
||||
if (!term)
|
||||
{
|
||||
ft_init_line();
|
||||
term = (struct termios *)malloc(sizeof(struct termios));
|
||||
tcgetattr(0, term);
|
||||
(*term).c_lflag &= ~(ECHO | ICANON | ISIG);
|
||||
|
|
@ -49,16 +51,16 @@ struct termios *ft_stats_term_termcaps(void)
|
|||
|
||||
int ft_readline(void)
|
||||
{
|
||||
static t_list_history *head = NULL;
|
||||
|
||||
if (tcsetattr(0, TCSANOW, ft_stats_term_termcaps()) == -1)
|
||||
return (-1);
|
||||
ft_prompt();
|
||||
data_singleton()->line.input = ft_lecture(head);
|
||||
data_singleton()->line.input = ft_lecture(data_singleton()->line.list_beg);
|
||||
ft_putstr("\n");
|
||||
ft_check_quotes(&data_singleton()->line.input, head);
|
||||
ft_check_quotes(&data_singleton()->line.input, data_singleton()->line.list_beg);
|
||||
ft_check_heredoc(&data_singleton()->line.input);
|
||||
ft_printf("\nend = %s\n\n", data_singleton()->line.input);
|
||||
if (data_singleton()->line.input)
|
||||
ft_push_back_history(&head, ft_create_history_list(data_singleton()->line.input));
|
||||
ft_push_back_history(&data_singleton()->line.list_beg, ft_create_history_list(data_singleton()->line.input));
|
||||
if (tcsetattr(0, TCSANOW, ft_save_stats_term()) == -1)
|
||||
return (-1);
|
||||
return (0);
|
||||
|
|
|
|||
Loading…
Reference in a new issue