From c0918e8e6b5412507d9abeb7c46a4375d73dcf85 Mon Sep 17 00:00:00 2001 From: gwojda Date: Fri, 3 Feb 2017 15:04:54 +0100 Subject: [PATCH] quoting + backslash + backquotes + parentheses + accolades revu : normalement ca marche. On va tenter d'ajouter la completion --- 42sh/Makefile | 1 + 42sh/includes/ft_readline.h | 37 ++++++++-- 42sh/src/line-editing/check_backslash.c | 93 ++++++++++++++++++++----- 42sh/src/line-editing/completion.c | 34 +++++++++ 42sh/src/line-editing/get_touch.c | 4 +- 42sh/src/line-editing/get_touch_toolz.c | 6 +- 42sh/src/line-editing/history.c | 4 +- 42sh/src/line-editing/history_parsing.c | 3 +- 42sh/src/line-editing/reader.c | 5 +- 42sh/src/line-editing/readline.c | 5 +- 10 files changed, 156 insertions(+), 36 deletions(-) create mode 100644 42sh/src/line-editing/completion.c diff --git a/42sh/Makefile b/42sh/Makefile index 0b60f83d..9f764f0a 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -143,6 +143,7 @@ lexer/token_init.c\ lexer/token_print.c\ line-editing/builtin_history.c\ line-editing/check_backslash.c\ +line-editing/completion.c\ line-editing/control_c_and_d.c\ line-editing/copy_cut_paste.c\ line-editing/ft_split_whitespaces.c\ diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index c79f8815..ac99a2a2 100644 --- a/42sh/includes/ft_readline.h +++ b/42sh/includes/ft_readline.h @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */ -/* Updated: 2017/02/02 18:28:44 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 15:04:38 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,27 @@ # define TOUCHE_F5 892427035 # define TOUCHE_F6 925981467 +# define PROMPT_QUOTES "quote> " +# define PROMPT_DQUOTES "dquote> " +# define PROMPT_BQUOTES "bquote> " +# define PROMPT_ACCOLADE "cursh> " +# define PROMPT_BRACKET "subsh> " +# define PROMPT_BSLASH "> " + +# define SIZE_PROMPT_QUOTES 7 +# define SIZE_PROMPT_DQUOTES 8 +# define SIZE_PROMPT_BQUOTES 8 +# define SIZE_PROMPT_ACCOLADE 7 +# define SIZE_PROMPT_BRACKET 7 +# define SIZE_PROMPT_BSLASH 2 + +# define IS_QUOTES 1 << 0 +# define IS_BQUOTES 1 << 1 +# define IS_DQUOTES 1 << 2 +# define IS_ACCOLADE 1 << 3 +# define IS_BRACKET 1 << 4 +# define IS_BSLASH 1 << 5 + # define HIST 1 # define ERROR_CNTL_R 1 @@ -82,8 +103,15 @@ typedef struct s_key void (*f)(void); } t_key; -extern t_key g_keys[]; +typedef struct s_prompt_type +{ + char key; + int value; + char *new_prompt; +} t_prompt_type; +extern t_key g_keys[]; +extern t_prompt_type g_prompt_tab[]; void ft_putnc(char c, int n); int ft_nbr_len(int nbr); @@ -109,10 +137,10 @@ void ft_realloc_str_history(char **str, size_t pos, int nb_his, int len); void ft_realloc_str_history_2(char **str, size_t pos, char *s); long long ft_pow(int nbr, int power); void ft_realloc_str_history_3(char **str, size_t pos, char *s); -void ft_check_backslash(char **str); char *ft_strget_history(char *str); int ft_nb_last_line(char *str, size_t pos); int ft_put(int nb); +void ft_check_line(void); char *ft_read_stdin(void); void ft_end(void); @@ -139,9 +167,8 @@ void ft_v(void); void ft_history_parsing(void); void ft_read_it(int input, size_t *pos, char **str); int ft_readline(void); +int ft_completion(int ret); void ft_check_heredoc(char **str); -void ft_check_quotes(char **s); - #endif diff --git a/42sh/src/line-editing/check_backslash.c b/42sh/src/line-editing/check_backslash.c index 1ab0256e..247d6828 100644 --- a/42sh/src/line-editing/check_backslash.c +++ b/42sh/src/line-editing/check_backslash.c @@ -6,30 +6,85 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 13:32:52 by gwojda #+# #+# */ -/* Updated: 2017/02/02 16:01:25 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 14:09:20 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void ft_check_backslash(char **str) +t_prompt_type g_prompt_tab[] = { - char *tmp1; - char *tmp2; + {IS_QUOTES ,SIZE_PROMPT_QUOTES ,PROMPT_QUOTES}, + {IS_BQUOTES ,SIZE_PROMPT_BQUOTES ,PROMPT_BQUOTES}, + {IS_DQUOTES ,SIZE_PROMPT_DQUOTES ,PROMPT_DQUOTES}, + {IS_ACCOLADE ,SIZE_PROMPT_ACCOLADE ,PROMPT_ACCOLADE}, + {IS_BRACKET ,SIZE_PROMPT_BRACKET ,PROMPT_BRACKET}, + {IS_BSLASH ,SIZE_PROMPT_BSLASH ,PROMPT_BSLASH}, + {0 ,0 ,0}, +}; - if (!*str || !**str) - return ; - if ((*str)[ft_strlen(*str) - 1] == '\\') - { - ft_putstr("> "); - data_singleton()->line.prompt_size = 2; - tmp1 = *str; - tmp2 = ft_strjoin(tmp1, "\n"); - free(tmp1); - tmp1 = ft_read_stdin(); - *str = ft_strjoin(tmp2, tmp1); - free(tmp1); - free(tmp2); - ft_putchar('\n'); - } +void ft_read_more(short c) +{ + char *str_tmp; + char *str_tmp2; + int i; + + i = 0; + str_tmp2 = data_singleton()->line.input; + str_tmp = ft_strjoin(str_tmp2, "\n"); + free(str_tmp2); + data_singleton()->line.input = NULL; + data_singleton()->line.pos = 0; + while (g_prompt_tab[i].key && !(g_prompt_tab[i].key & c)) + ++i; + data_singleton()->line.prompt_size = g_prompt_tab[i].value; + ft_printf("\n%s", g_prompt_tab[i].new_prompt); + str_tmp2 = ft_read_stdin(); + str_tmp2 = ft_strjoin(str_tmp, data_singleton()->line.input); + free(str_tmp); + free(data_singleton()->line.input); + data_singleton()->line.input = str_tmp2; + ft_check_line(); +} + +void ft_check_this_char(char c, short *status) +{ + int i; + char stats[] = {'\'', '`', '\"', '{', '(', '\\', '\0'}; + + i = 0; + while (stats[i] && stats[i] != c) + ++i; + if (!stats[i]) + return ; + if (((1 << i) & ~(*status))) + { + if (((1 << i) > *status && + (*status == 0 && !(IS_QUOTES & *status) && (!(IS_DQUOTES & *status)))) + || (((1 << i) == IS_BQUOTES) && !(IS_QUOTES & *status))) + (*status) = (*status) | (1 << i); + } + else + (*status) = (*status) ^ (1 << i); +} + +void ft_check_line(void) +{ + int i; + char *str; + short status; + + i = 0; + status = 0; + str = data_singleton()->line.input; + while (str[i]) + { + if (IS_BSLASH & status) + status = status ^ IS_BSLASH; + else + ft_check_this_char(str[i], &status); + ++i; + } + if (status) + ft_read_more(status); } diff --git a/42sh/src/line-editing/completion.c b/42sh/src/line-editing/completion.c new file mode 100644 index 00000000..beb64572 --- /dev/null +++ b/42sh/src/line-editing/completion.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* completion.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/03 14:15:55 by gwojda #+# #+# */ +/* Updated: 2017/02/03 15:03:17 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int ft_completion(int ret) +{ + size_t tmp; + + if ((ret != TOUCHE_TAB && ret != 10) + || (ret == 10 && !(data_singleton()->comp))) + return (0); + data_singleton()->line.pos = tmp; + if (ret == 10) + ft_puttermcaps("cd"); + if (data_singleton()->comp || ret == TOUCHE_TAB) + completion(ret); + if (ret == 10) + { + ft_current_str(data_singleton()->line.input, tmp); + ft_get_next_str(data_singleton()->line.input, &tmp); + data_singleton()->line.pos = tmp; + } + return (1); +} diff --git a/42sh/src/line-editing/get_touch.c b/42sh/src/line-editing/get_touch.c index 2ecde44c..cd607266 100644 --- a/42sh/src/line-editing/get_touch.c +++ b/42sh/src/line-editing/get_touch.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:28:49 by gwojda #+# #+# */ -/* Updated: 2017/02/02 18:27:07 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 14:46:22 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,6 +54,8 @@ char *ft_read_stdin(void) ret = 0; j = 0; read(0, &ret, sizeof(int)); + if (ft_completion(ret)) + continue ; while (g_key[j].value && g_key[j].value != ret) ++j; if (g_key[j].value) diff --git a/42sh/src/line-editing/get_touch_toolz.c b/42sh/src/line-editing/get_touch_toolz.c index e5937fac..f99820dc 100644 --- a/42sh/src/line-editing/get_touch_toolz.c +++ b/42sh/src/line-editing/get_touch_toolz.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:42:54 by gwojda #+# #+# */ -/* Updated: 2017/02/02 15:22:35 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 12:08:45 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,13 +73,15 @@ void ft_found_prev_word(void) void ft_found_next_word(void) { - int i; + int i; char *str; size_t *pos; str = data_singleton()->line.input; pos = &data_singleton()->line.pos; i = 0; + if (!str) + return ; while (str[i + *pos] && str[i + *pos] == ' ') { ft_putchar(str[i + *pos]); diff --git a/42sh/src/line-editing/history.c b/42sh/src/line-editing/history.c index 295af73e..cd587a51 100644 --- a/42sh/src/line-editing/history.c +++ b/42sh/src/line-editing/history.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/02 15:22:19 by gwojda #+# #+# */ -/* Updated: 2017/02/02 18:04:16 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 11:55:36 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ void ft_history_down(void) str = &data_singleton()->line.input; pos = &data_singleton()->line.pos; head = data_singleton()->line.list_cur; - if (!head || !*str) + if (!head) return ; if (*str) { diff --git a/42sh/src/line-editing/history_parsing.c b/42sh/src/line-editing/history_parsing.c index bd3f67d4..1da1f4dd 100644 --- a/42sh/src/line-editing/history_parsing.c +++ b/42sh/src/line-editing/history_parsing.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/25 11:39:47 by gwojda #+# #+# */ -/* Updated: 2017/02/02 16:20:26 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 11:57:07 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,7 +59,6 @@ static void ft_history_parsing_2(void) data_singleton()->line.input = ft_read_stdin(); ft_putchar('\n'); data_singleton()->line.opt = data_singleton()->line.opt | ~HIST; - ft_check_quotes(&data_singleton()->line.input); ft_check_heredoc(&data_singleton()->line.input); ft_history_parsing(); } diff --git a/42sh/src/line-editing/reader.c b/42sh/src/line-editing/reader.c index 4a3e17d3..22d41fd2 100644 --- a/42sh/src/line-editing/reader.c +++ b/42sh/src/line-editing/reader.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:52:57 by gwojda #+# #+# */ -/* Updated: 2017/01/25 19:07:39 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 11:40:10 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,6 +61,7 @@ void ft_read_it(int input, size_t *pos, char **str) return ; ft_read_it_2(input, t); ft_read_it_3(str, t, pos, &j); - ft_putstr((*str) + (*pos) - j); +// ft_current_str((*str) + (*pos) - j, *pos); +// ft_get_next_str((*str) + (*pos) - j, pos); ft_putnc('\b', ft_strlen((*str)) - ((*pos))); } diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index f48668b4..d2e0a2f1 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/02/02 18:03:23 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 12:33:51 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,10 +82,9 @@ int ft_readline(void) data_singleton()->line.pos = 0; ft_prompt(); data_singleton()->line.input = ft_read_stdin(); + ft_check_line(); ft_putchar('\n'); - ft_check_quotes(&data_singleton()->line.input); ft_check_heredoc(&data_singleton()->line.input); - ft_check_backslash(&data_singleton()->line.input); ft_history_parsing(); if (data_singleton()->line.input) {