From 7fb9aa2da5d606838392f81a78039d1c251688c4 Mon Sep 17 00:00:00 2001 From: gwojda Date: Thu, 2 Feb 2017 19:07:32 +0100 Subject: [PATCH] =?UTF-8?q?changement=20du=20squelette=20(passage=20a=20un?= =?UTF-8?q?=20tableau=20de=20pointeur=20sur=20fonction)=20-=20pas=20encore?= =?UTF-8?q?=20tout=20rest=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 42sh/Makefile | 2 + 42sh/includes/builtin.h | 5 +- 42sh/includes/ft_readline.h | 129 +++++++++++---------- 42sh/src/line-editing/check_backslash.c | 4 +- 42sh/src/line-editing/control_c_and_d.c | 49 ++++++++ 42sh/src/line-editing/copy_cut_paste.c | 94 ++++++++------- 42sh/src/line-editing/get_touch.c | 134 +++++++--------------- 42sh/src/line-editing/get_touch_toolz.c | 131 ++++++++++++++------- 42sh/src/line-editing/get_touch_toolz_2.c | 30 ++++- 42sh/src/line-editing/heredoc.c | 4 +- 42sh/src/line-editing/history.c | 87 ++++++++++++++ 42sh/src/line-editing/history_parsing.c | 8 +- 42sh/src/line-editing/list_toolz.c | 11 +- 42sh/src/line-editing/move_to_line.c | 28 ++--- 42sh/src/line-editing/print_and_del.c | 26 ++++- 42sh/src/line-editing/quotes_gest.c | 19 +-- 42sh/src/line-editing/readline.c | 11 +- 42sh/src/line-editing/surch_in_history.c | 8 +- 42sh/src/line-editing/tool_line_2.c | 8 +- 42sh/src/line-editing/toolz.c | 31 +---- 42sh/src/line-editing/toolz2.c | 2 +- 42sh/src/line-editing/toolz_parseur.c | 94 +-------------- 22 files changed, 490 insertions(+), 425 deletions(-) create mode 100644 42sh/src/line-editing/control_c_and_d.c create mode 100644 42sh/src/line-editing/history.c diff --git a/42sh/Makefile b/42sh/Makefile index 8d286750..e240d66b 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -144,12 +144,14 @@ lexer/token_init.c\ lexer/token_print.c\ line-editing/builtin_history.c\ line-editing/check_backslash.c\ +line-editing/control_c_and_d.c\ line-editing/copy_cut_paste.c\ line-editing/ft_split_whitespaces.c\ line-editing/get_touch.c\ line-editing/get_touch_toolz.c\ line-editing/get_touch_toolz_2.c\ line-editing/heredoc.c\ +line-editing/history.c\ line-editing/history_parsing.c\ line-editing/history_parsing_toolz.c\ line-editing/history_parsing_toolz_2.c\ diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index f2324223..45db3c5a 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* builtin.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */ -/* Updated: 2017/01/21 18:42:34 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 19:07:01 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,5 +27,6 @@ int builtin_unsetenv(const char *path, char *const argv[], char *const envp[]); int builtin_jobs(const char *path, char *const av[], char *const envp[]); int builtin_fg(const char *path, char *const av[], char *const envp[]); int builtin_bg(const char *path, char *const av[], char *const envp[]); +int builtin_history(const char *path, char *const av[], char *const envp[]); #endif diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index fb23daf1..c79f8815 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 13:23:12 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 18:28:44 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,6 +38,7 @@ # define TOUCHE_CTRL_C 3 # define TOUCHE_CTRL_D 4 # define TOUCHE_CTRL_R 18 +# define TOUCHE_SUPPR 127 # define TOUCHE_HOME 4741915 # define TOUCHE_END 4610843 # define TOUCHE_TAB 9 @@ -65,84 +66,82 @@ typedef struct s_list_history typedef struct s_line { char *input; + char *copy_tmp; + size_t pos; int prompt_size; int list_size; char opt; t_list_history *list_end; + t_list_history *list_cur; t_list_history *list_beg; } t_line; -void ft_add_in_history_file(char *str); -int builtin_history(const char *path, char *const av[], char *const envp[]); -void ft_check_backslash(char **str); -char *ft_strget_history(char *str); -void ft_surch_in_history(char **str, size_t *pos); -void ft_realloc_str_history_3(char **str, size_t pos, char *s); -void ft_move_right(size_t *pos, char *str); -void ft_move_left(size_t *pos, char *str); -int ft_put(int nb); -void ft_end(char *str, size_t *pos); -void ft_home(char *str, size_t *pos); -void ft_realloc_str_history_2(char **str, size_t pos, char *s); -void ft_realloc_str_history(char **str, size_t pos - , int nb_his, int len); -char *ft_strdupi_w(char const *s); -void ft_history_parsing(); -void ft_check_heredoc(char **str); -void ft_history_builtin(void); +typedef struct s_key +{ + int value; + void (*f)(void); +} t_key; + +extern t_key g_keys[]; + + +void ft_putnc(char c, int n); int ft_nbr_len(int nbr); -int ft_found_next_char(char *str, size_t i); -void ft_check_end_of_line(char *str, size_t pos); -void ft_printall(char *str, size_t *pos); +void ft_puttermcaps(char *str); void ft_get_beggin_with_curs(char *str, size_t *pos); -void ft_history(char **str, int ret, t_list_history **head, size_t *pos); -void ft_suppr(char **str, size_t *i); -void ft_del(char **str, size_t *i); +void ft_check_end_of_line(char *str, size_t pos); +int ft_size_term(void); +void ft_add_in_history_file(char *str); +void ft_get_beggin(char *str, size_t *pos); +void ft_get_next_str(char *str, size_t *pos); +void ft_putall_current_str(char *str, size_t *pos); void ft_current_str(char *str, size_t pos); int ft_strlen_next(char *str, size_t pos); -void ft_putall_current_str(char *str, size_t *pos); -void ft_get_next_str(char *str, size_t *pos); -void ft_get_beggin(char *str, size_t *pos); - -long long ft_pow(int nbr, int power); -char *ft_strndup(char const *s, int n); -char *ft_strdupi(char const *s); -void ft_puttermcaps(char *str); -void ft_putnc(char c, int n); -int ft_size_term(void); -void ft_free_tabstr(char ***env); -int ft_is_whitespaces(char *str); -int ft_get_size_prev(char *str, size_t pos); -int ft_nb_line(char *str, size_t pos); -int ft_get_ind_prev(char *str, size_t pos); -void ft_found_next_word(char *str, size_t *pos); -void ft_move_to_beggin(char *str, size_t *pos); -int ft_nb_last_line(char *str, size_t pos); -void ft_found_prev_word(char *str, size_t *pos); +void ft_push_back_history(t_list_history **head, t_list_history *new); +void ft_prompt(void); char *ft_remove_imput(char *str, size_t pos); char *ft_realloc_imput(char *str, int a, size_t pos); -int ft_readline(void); -struct termios *ft_stats_term_termcaps(void); -struct termios *ft_save_stats_term(void); -void ft_init_line(void); -void ft_read_it(int input, size_t *pos, char **str); -void ft_check_quotes(char **s, t_list_history *head); -int ft_check_quotes_num(char *s); -void ft_print(char **str, int ret, size_t *i); -void ft_move_to_line(int ret, size_t *pos, char *str); -void ft_get_head(t_list_history **head); -void ft_push_back_history(t_list_history **head, t_list_history *new); +char *ft_strdupi(char const *s); +char *ft_strndup(char const *s, int n); t_list_history *ft_create_history_list(char *str); -void ft_curse_move(char *str); -void ft_move_suppr(char *str, size_t pos); -void ft_move_dell(char *str, size_t pos); -void ft_move_to_word(int ret, size_t *pos, char *str); -void ft_move_term(int ret, size_t *pos, char *str); -void ft_home_end(char *str, int ret, size_t *pos); +char *ft_strdupi_w(char const *s); +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); -char **ft_split_whitespaces(char const *s); -void ft_cxv(int ret, size_t *pos, char **str); -char *ft_lecture(t_list_history *head); -void ft_prompt(void); + +char *ft_read_stdin(void); +void ft_end(void); +void ft_home(void); +void ft_move_right(void); +void ft_move_left(void); +void ft_up(void); +void ft_down(void); +void ft_buff_f6(void); +void ft_control_d(void); +void ft_control_c(void); +void ft_del(void); +void ft_suppr(void); +void ft_print(int ret); +void ft_surch_in_history(void); +void ft_printall(void); +void ft_history_down(void); +void ft_history_up(void); +void ft_found_next_word(void); +void ft_found_prev_word(void); +void ft_c(void); +void ft_x(void); +void ft_v(void); +void ft_history_parsing(void); +void ft_read_it(int input, size_t *pos, char **str); +int ft_readline(void); + +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 14e0fe22..1ab0256e 100644 --- a/42sh/src/line-editing/check_backslash.c +++ b/42sh/src/line-editing/check_backslash.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 13:32:52 by gwojda #+# #+# */ -/* Updated: 2017/02/02 11:11:11 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 16:01:25 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ void ft_check_backslash(char **str) tmp1 = *str; tmp2 = ft_strjoin(tmp1, "\n"); free(tmp1); - tmp1 = ft_lecture(data_singleton()->line.list_beg); + tmp1 = ft_read_stdin(); *str = ft_strjoin(tmp2, tmp1); free(tmp1); free(tmp2); diff --git a/42sh/src/line-editing/control_c_and_d.c b/42sh/src/line-editing/control_c_and_d.c new file mode 100644 index 00000000..a194e7a0 --- /dev/null +++ b/42sh/src/line-editing/control_c_and_d.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* control_c_and_d.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/02 15:17:28 by gwojda #+# #+# */ +/* Updated: 2017/02/02 16:43:15 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_buff_f6(void) +{ + int ret; + + ret = 0; + read(0, &ret, sizeof(int)); +} + +void ft_control_d(void) +{ + char **str; + size_t *pos; + + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (!(*str) || (*str)[0] == '\0') + builtin_exit(NULL, (char*[]){"exit", NULL}, NULL); + else if (*pos < ft_strlen(*str)) + ft_del(); + else + ft_puttermcaps("bl"); +} + +void ft_control_c(void) +{ + char **str; + size_t *pos; + + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + ft_putchar('\n'); + ft_prompt(); + ft_strdel(str); + *pos = 0; +} diff --git a/42sh/src/line-editing/copy_cut_paste.c b/42sh/src/line-editing/copy_cut_paste.c index 19f24b47..587469bb 100644 --- a/42sh/src/line-editing/copy_cut_paste.c +++ b/42sh/src/line-editing/copy_cut_paste.c @@ -6,37 +6,12 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 12:45:06 by gwojda #+# #+# */ -/* Updated: 2017/02/01 15:16:33 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 16:10:51 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static void ft_v(char *tmp, size_t *pos, char **str) -{ - size_t tmp_pos; - int i; - - i = 0; - tmp_pos = *pos; - if (!tmp) - return ; - while (tmp[i]) - { - *str = ft_realloc_imput(*str, tmp[i], *pos + i); - ++i; - } - if (*pos) - { - --(*pos); - ft_get_beggin_with_curs(*str, pos); - } - ft_current_str(*str, *pos); - ft_get_next_str(*str, pos); - ft_putnc('\b', *pos - tmp_pos); - (*pos) = tmp_pos; -} - static char *ft_strdupi_space(char const *s) { int i; @@ -61,10 +36,49 @@ static char *ft_strdupi_space(char const *s) return (str); } -static void ft_x(char **tmp, size_t *pos, char **str) +void ft_v(void) { - int i; + size_t tmp_pos; + int i; + char *tmp; + char **str; + size_t *pos; + tmp = data_singleton()->line.copy_tmp; + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + i = 0; + tmp_pos = *pos; + if (!*str || !tmp) + return ; + while (tmp[i]) + { + *str = ft_realloc_imput(*str, tmp[i], *pos + i); + ++i; + } + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(*str, pos); + } + ft_current_str(*str, *pos); + ft_get_next_str(*str, pos); + ft_putnc('\b', *pos - tmp_pos); + (*pos) = tmp_pos; +} + +void ft_x(void) +{ + int i; + char **tmp; + char **str; + size_t *pos; + + tmp = &data_singleton()->line.copy_tmp; + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (!*str) + return ; if (*tmp) ft_strdel(tmp); *tmp = ft_strdupi_space(&(*str)[(*pos)]); @@ -77,20 +91,16 @@ static void ft_x(char **tmp, size_t *pos, char **str) ft_puttermcaps("cd"); } -void ft_cxv(int ret, size_t *pos, char **str) +void ft_c(void) { - static char *tmp = NULL; + char *tmp; + char **str; + size_t *pos; - if (!*str) - return ; - if (ret == TOUCHE_OPT_X) - ft_x(&tmp, pos, str); - else if (ret == TOUCHE_OPT_C) - { - if (tmp) - ft_strdel(&tmp); - tmp = ft_strdupi_space((*str) + (*pos)); - } - else if (ret == TOUCHE_OPT_V) - ft_v(tmp, pos, str); + tmp = data_singleton()->line.copy_tmp; + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (tmp) + ft_strdel(&tmp); + tmp = ft_strdupi_space((*str) + (*pos)); } diff --git a/42sh/src/line-editing/get_touch.c b/42sh/src/line-editing/get_touch.c index 14d09512..2ecde44c 100644 --- a/42sh/src/line-editing/get_touch.c +++ b/42sh/src/line-editing/get_touch.c @@ -6,114 +6,64 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:28:49 by gwojda #+# #+# */ -/* Updated: 2017/02/02 11:34:22 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 18:27:07 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static int ft_lecture_3(int ret, char **str, size_t *i) +t_key g_key[] = { - if (ret == 127 && (*i) > 0) - ft_suppr(str, i); - else if (ret == TOUCHE_DELETE && (*str) && (*i) < ft_strlen((*str))) - ft_del(str, i); - else if (ret == TOUCHE_HOME) - ft_home(*str, i); - else if (ret == TOUCHE_END) - ft_end(*str, i); - else - return (0); - return (1); -} + {FLECHE_HAUT , &ft_history_up }, + {FLECHE_BAS , &ft_history_down }, + {FLECHE_GAUCHE , &ft_move_left }, + {FLECHE_DROITE , &ft_move_right }, + {TOUCHE_DELETE , &ft_del }, + {TOUCHE_CTRL_C , &ft_control_c }, + {TOUCHE_CTRL_D , &ft_control_d }, + {TOUCHE_CTRL_R , &ft_history_parsing }, + {TOUCHE_SUPPR , &ft_suppr }, + {TOUCHE_HOME , &ft_home }, + {TOUCHE_END , &ft_end }, + {TOUCHE_OPT_UP , &ft_up }, + {TOUCHE_OPT_DOWN , &ft_down }, + {TOUCHE_OPT_LEFT , &ft_found_prev_word }, + {TOUCHE_OPT_RIGHT , &ft_found_next_word }, + {TOUCHE_OPT_C , &ft_c }, + {TOUCHE_OPT_V , &ft_v }, + {TOUCHE_OPT_X , &ft_x }, + {TOUCHE_F5 , &ft_printall }, + {TOUCHE_F6 , &ft_buff_f6 }, + {0 , 0 }, +}; -static int ft_lecture_2(int ret, char **str, size_t *i) +char *ft_read_stdin(void) { - if (ret == TOUCHE_F5) - ft_printall(*str, i); - else if (ret == TOUCHE_OPT_LEFT || ret == TOUCHE_OPT_RIGHT) - ft_move_to_word(ret, i, *str); - else if (ret == TOUCHE_OPT_X || ret == TOUCHE_OPT_C - || ret == TOUCHE_OPT_V) - ft_cxv(ret, i, str); - else if (ret == FLECHE_DROITE) - ft_move_right(i, *str); - else if (ret == FLECHE_GAUCHE) - ft_move_left(i, *str); - else if (ret == TOUCHE_OPT_UP || ret == TOUCHE_OPT_DOWN) - ft_move_to_line(ret, i, *str); - else if (ft_isprint(ret)) - ft_print(str, ret, i); - else - return (0); - return (1); -} + int ret; + int j; -char *ft_lecture(t_list_history *head) -{ - char *str; - int ret; - size_t i; - - str = NULL; - if (data_singleton()->line.opt & HIST) - str = data_singleton()->line.input; - i = 0; - if (str) + if (data_singleton()->line.input) { - ft_current_str(str, i); - ft_get_next_str(str, &i); - if (str[i]) - ++i; + ft_current_str(data_singleton()->line.input, data_singleton()->line.pos); + ft_get_next_str(data_singleton()->line.input, &data_singleton()->line.pos); + if (data_singleton()->line.input[data_singleton()->line.pos]) + ++(data_singleton()->line.pos); } while (42) { ret = 0; + j = 0; read(0, &ret, sizeof(int)); - if (ret == TOUCHE_F6 && read(0, &ret, sizeof(int)) > 0) - continue ; - if (ret == TOUCHE_CTRL_R) - { - ft_surch_in_history(&str, &i); - continue ; - } -/* -** if (ret == TOUCHE_TAB) -** ret = ft_completion(&str, &i); -*/ - if (ret == TOUCHE_CTRL_D) - { - if (!str || str[0] == '\0') - exit(0); - else if (i < ft_strlen(str)) - ft_del(&str, &i); - else - ft_puttermcaps("bl"); - } - if (ret == TOUCHE_CTRL_C) - { - ft_putchar('\n'); - ft_prompt(); - ft_strdel(&str); - i = 0; - } - if (ft_lecture_2(ret, &str, &i)) - continue ; - else if (ret == FLECHE_BAS || ret == FLECHE_HAUT) - { - if (!head) - continue ; - ft_history(&str, ret, &head, &i); - if (str) - i = ft_strlen_next(str, i); - else - i = 0; - } - else if (ft_lecture_3(ret, &str, &i)) - continue ; + while (g_key[j].value && g_key[j].value != ret) + ++j; + if (g_key[j].value) + g_key[j].f(); + else if (ft_isprint(ret)) + ft_print(ret); else if (ret == 10) - return (str); + return (data_singleton()->line.input); else if (ft_isascii(ret) == 0) - ft_read_it(ret, &i, &str); + ft_read_it(ret, &data_singleton()->line.pos, + &data_singleton()->line.input); } } diff --git a/42sh/src/line-editing/get_touch_toolz.c b/42sh/src/line-editing/get_touch_toolz.c index ae54f960..e5937fac 100644 --- a/42sh/src/line-editing/get_touch_toolz.c +++ b/42sh/src/line-editing/get_touch_toolz.c @@ -6,61 +6,108 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:42:54 by gwojda #+# #+# */ -/* Updated: 2017/02/02 11:41:52 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 15:22:35 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void ft_history_2(char **str, int ret, t_list_history **head - , size_t *pos) +void ft_found_prev_word(void) { - if (*str) + int i; + char *str; + size_t *pos; + + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + i = 0; + if (!*pos) + return ; + if (str[*pos - 1] != '\n' && !(str[*pos] == '\n' || str[*pos] == ' ')) + { + ft_puttermcaps("le"); + --(*pos); + } + while ((int)*pos - i - 1 > 0 && str[*pos - i - 1] == ' ') + { + ft_puttermcaps("le"); + ++i; + } + (*pos) = (i && str[*pos - i] == '\n') ? *pos - i + 1 : *pos - i; + i = 0; + if (*pos >= 1 && str[*pos - 1] == '\n') + { + ft_puttermcaps("cd"); + if (*pos >= 2) + (*pos) -= 2; + ft_get_beggin(str, pos); + ft_current_str(str, *pos); + ft_get_next_str(str, pos); + ++(*pos); + } + else + { + if (*pos && !(str[*pos] == '\n' || str[*pos] == ' ')) + { + ft_puttermcaps("le"); + --(*pos); + } + while (*pos - i && (str[*pos - i] == '\n' || str[*pos - i] == ' ')) + { + ft_puttermcaps("le"); + ++i; + } + while (*pos - i && str[*pos - i] != '\n' && str[*pos - i] != ' ') + { + ft_puttermcaps("le"); + ++i; + } + if (str[*pos - i] == '\n' || str[*pos - i] == ' ') + { + ft_puttermcaps("nd"); + ++(*pos); + } + (*pos) -= i; + } +} + +void ft_found_next_word(void) +{ + int i; + char *str; + size_t *pos; + + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + i = 0; + while (str[i + *pos] && str[i + *pos] == ' ') + { + ft_putchar(str[i + *pos]); + ++i; + } + if (str[*pos] == '\n') { if (*pos) { --(*pos); - ft_get_beggin_with_curs(*str, pos); + ft_get_beggin_with_curs(str, pos); } ft_puttermcaps("cd"); - *pos = 0; - ft_strdel(str); + ft_get_next_str(str, pos); + (*pos) += (*pos) ? 2 : 1; + ft_current_str(str, *pos); + ft_get_next_str(str, pos); + if (!str[*pos]) + --(*pos); + ft_get_beggin_with_curs(str, pos); } - if (ret == FLECHE_BAS) + else { - if ((*head)->next) - (*head) = (*head)->next; - if (!(*head)->str) - *str = NULL; - else - *str = ft_strdup((*head)->str); - if (*str) - ft_current_str(*str, *pos); + while (str[i + *pos] && str[i + *pos] != '\n' && str[i + *pos] != ' ') + { + ft_putchar(str[i + *pos]); + ++i; + } + *pos += i; } } - -void ft_history(char **str, int ret, t_list_history **head, size_t *pos) -{ - ft_history_2(str, ret, head, pos); - if (ret == FLECHE_HAUT) - { - if ((*head)->prev) - (*head) = (*head)->prev; - if (!(*head)->str) - *str = NULL; - else - *str = ft_strdup((*head)->str); - if (*str) - ft_current_str(*str, *pos); - } -} - -void ft_move_to_word(int ret, size_t *pos, char *str) -{ - if (!str) - return ; - if (ret == TOUCHE_OPT_LEFT) - ft_found_prev_word(str, pos); - else if (ret == TOUCHE_OPT_RIGHT) - ft_found_next_word(str, pos); -} diff --git a/42sh/src/line-editing/get_touch_toolz_2.c b/42sh/src/line-editing/get_touch_toolz_2.c index d7c6b2e4..05805aa2 100644 --- a/42sh/src/line-editing/get_touch_toolz_2.c +++ b/42sh/src/line-editing/get_touch_toolz_2.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:43:58 by gwojda #+# #+# */ -/* Updated: 2017/02/02 11:34:34 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 17:54:27 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,8 +18,13 @@ int ft_put(int nb) return (1); } -void ft_end(char *str, size_t *pos) +void ft_end(void) { + char *str; + size_t *pos; + + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; if (!str) return ; if (*pos) @@ -35,8 +40,13 @@ void ft_end(char *str, size_t *pos) ft_get_next_str(str, pos); } -void ft_home(char *str, size_t *pos) +void ft_home(void) { + char *str; + size_t *pos; + + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; if (!str) return ; if (*pos) @@ -56,10 +66,14 @@ void ft_home(char *str, size_t *pos) ft_get_beggin_with_curs(str, pos); } -void ft_move_right(size_t *pos, char *str) +void ft_move_right(void) { size_t tmp; + char *str; + size_t *pos; + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; if (ft_strlen(str) <= *pos) return ; if (str[*pos] == '\n') @@ -84,8 +98,13 @@ void ft_move_right(size_t *pos, char *str) } } -void ft_move_left(size_t *pos, char *str) +void ft_move_left(void) { + char *str; + size_t *pos; + + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; if (!*pos) return ; if (str[*pos - 1] == '\n') @@ -96,7 +115,6 @@ void ft_move_left(size_t *pos, char *str) --(*pos); return ; } - printf("here\n\n"); ft_puttermcaps("cd"); (*pos) -= 2; ft_get_beggin(str, pos); diff --git a/42sh/src/line-editing/heredoc.c b/42sh/src/line-editing/heredoc.c index 649524d0..7637dcbd 100644 --- a/42sh/src/line-editing/heredoc.c +++ b/42sh/src/line-editing/heredoc.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/24 15:52:34 by gwojda #+# #+# */ -/* Updated: 2017/01/25 16:03:11 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 16:09:42 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,7 @@ static void ft_check_heredoc_2(char **str, char *end) *str = ft_strjoin(*str, "\n"); free(tmp); tmp = *str; - tmp2 = ft_lecture(data_singleton()->line.list_beg); + tmp2 = ft_read_stdin(); if (!ft_strcmp(end, tmp2)) boolean = 1; *str = ft_strjoin(tmp, tmp2); diff --git a/42sh/src/line-editing/history.c b/42sh/src/line-editing/history.c new file mode 100644 index 00000000..295af73e --- /dev/null +++ b/42sh/src/line-editing/history.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* history.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/02 15:22:19 by gwojda #+# #+# */ +/* Updated: 2017/02/02 18:04:16 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_history_down(void) +{ + t_list_history *head; + char **str; + size_t *pos; + + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + head = data_singleton()->line.list_cur; + if (!head || !*str) + return ; + if (*str) + { + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(*str, pos); + } + ft_puttermcaps("cd"); + *pos = 0; + ft_strdel(str); + } + if (head->next) + head = head->next; + if (!head->str) + *str = NULL; + else + *str = ft_strdup(head->str); + if (*str) + ft_current_str(*str, *pos); + if (*str) + *pos = ft_strlen_next(*str, *pos); + else + *pos = 0; + data_singleton()->line.list_cur = head; +} + +void ft_history_up(void) +{ + t_list_history *head; + char **str; + size_t *pos; + + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + head = data_singleton()->line.list_cur; + if (!head) + return ; + if (*str) + { + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(*str, pos); + } + ft_puttermcaps("cd"); + *pos = 0; + ft_strdel(str); + } + if (head->prev) + head = head->prev; + if (!head->str) + *str = NULL; + else + *str = ft_strdup(head->str); + if (*str) + ft_current_str(*str, *pos); + if (*str) + *pos = ft_strlen_next(*str, *pos); + else + *pos = 0; + data_singleton()->line.list_cur = head; +} diff --git a/42sh/src/line-editing/history_parsing.c b/42sh/src/line-editing/history_parsing.c index ec518952..bd3f67d4 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/01/25 20:05:40 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 16:20:26 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,12 +56,10 @@ static void ft_history_parsing_2(void) { data_singleton()->line.opt = data_singleton()->line.opt | HIST; ft_prompt(); - data_singleton()->line.input = ft_lecture( - data_singleton()->line.list_beg); + 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, - data_singleton()->line.list_beg); + ft_check_quotes(&data_singleton()->line.input); ft_check_heredoc(&data_singleton()->line.input); ft_history_parsing(); } diff --git a/42sh/src/line-editing/list_toolz.c b/42sh/src/line-editing/list_toolz.c index 86df0fe5..51bde359 100644 --- a/42sh/src/line-editing/list_toolz.c +++ b/42sh/src/line-editing/list_toolz.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/17 11:37:47 by gwojda #+# #+# */ -/* Updated: 2017/01/24 15:19:04 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 16:08:01 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,12 +49,3 @@ void ft_push_back_history(t_list_history **head, t_list_history *new) new->next = (*head); (*head)->prev = new; } - -void ft_get_head(t_list_history **head) -{ - if (head && *head) - { - while ((*head)->next && (*head)->next->str) - (*head) = (*head)->next; - } -} diff --git a/42sh/src/line-editing/move_to_line.c b/42sh/src/line-editing/move_to_line.c index 87b3a5d1..c8161cfb 100644 --- a/42sh/src/line-editing/move_to_line.c +++ b/42sh/src/line-editing/move_to_line.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:21:40 by gwojda #+# #+# */ -/* Updated: 2017/01/25 16:40:09 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 15:07:08 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,11 +38,17 @@ static void ft_up_2(size_t *pos, char *str) (*pos) -= i; } -static void ft_up(size_t *pos, char *str) +void ft_up(void) { int i; + char *str; + size_t *pos; i = 0; + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (!str) + return ; if (str[*pos - 1] == '\n') { ft_puttermcaps("cd"); @@ -73,12 +79,18 @@ static void ft_down_2(size_t *pos, char *str) ft_get_beggin_with_curs(str, pos); } -static void ft_down(size_t *pos, char *str) +void ft_down(void) { int i; int len; + char *str; + size_t *pos; i = 0; + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (!str) + return ; if (str[*pos] == '\n') ft_down_2(pos, str); else @@ -92,13 +104,3 @@ static void ft_down(size_t *pos, char *str) *pos += i; } } - -void ft_move_to_line(int ret, size_t *pos, char *str) -{ - if (!str) - return ; - if (ret == TOUCHE_OPT_UP) - ft_up(pos, str); - else if (ret == TOUCHE_OPT_DOWN) - ft_down(pos, str); -} diff --git a/42sh/src/line-editing/print_and_del.c b/42sh/src/line-editing/print_and_del.c index e4100b9b..ac352ae1 100644 --- a/42sh/src/line-editing/print_and_del.c +++ b/42sh/src/line-editing/print_and_del.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/05 16:02:43 by gwojda #+# #+# */ -/* Updated: 2017/02/02 11:12:29 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 15:10:50 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,11 +23,15 @@ int ft_found_next_char(char *str, size_t i) return (0); } -void ft_print(char **str, int ret, size_t *i) +void ft_print(int ret) { - int j; + int j; + char **str; + size_t *i; j = 0; + str = &data_singleton()->line.input; + i = &data_singleton()->line.pos; *str = ft_realloc_imput(*str, ret, *i); while (*((*str) + *i + j) && *((*str) + *i + j) != '\n') { @@ -52,12 +56,18 @@ void ft_suppr_2(char **str, size_t *i, size_t tmp) ft_strdel(str); } -void ft_suppr(char **str, size_t *i) +void ft_suppr(void) { size_t tmp; char boolean; + char **str; + size_t *i; + str = &data_singleton()->line.input; + i = &data_singleton()->line.pos; boolean = 0; + if (*i <= 0) + return ; if ((*str)[*i - 1] != '\n') boolean = 1; --(*i); @@ -75,12 +85,18 @@ void ft_suppr(char **str, size_t *i) ft_suppr_2(str, i, tmp); } -void ft_del(char **str, size_t *i) +void ft_del(void) { size_t tmp; + char **str; + size_t *i; + str = &data_singleton()->line.input; + i = &data_singleton()->line.pos; tmp = *i; *str = ft_remove_imput((*str), tmp); + if (!((*str) && (*i) < ft_strlen((*str)))) + return ; if (*i) { --(*i); diff --git a/42sh/src/line-editing/quotes_gest.c b/42sh/src/line-editing/quotes_gest.c index caf7ba9d..c4be15f9 100644 --- a/42sh/src/line-editing/quotes_gest.c +++ b/42sh/src/line-editing/quotes_gest.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/05 12:20:19 by gwojda #+# #+# */ -/* Updated: 2017/01/25 16:50:14 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 17:53:41 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,13 +51,15 @@ int ft_check_quotes_num(char *s) return (0); } -void ft_check_quotes(char **s, t_list_history *head) +void ft_check_quotes(char **s) { - int ret; - char *tmp; - char *tmp2; - int prompt_size_mem; + int ret; + char *tmp; + char *tmp2; + int prompt_size_mem; + t_list_history *head; + head = data_singleton()->line.list_beg; if (!*s) return ; while ((ret = (ft_check_quotes_num(*s)))) @@ -71,12 +73,15 @@ void ft_check_quotes(char **s, t_list_history *head) ft_strdel(&tmp); tmp = *s; prompt_size_mem = data_singleton()->line.prompt_size; - tmp2 = ft_lecture(head); + data_singleton()->line.input = NULL; + data_singleton()->line.pos = 0; + tmp2 = ft_read_stdin(); data_singleton()->line.prompt_size = prompt_size_mem; ft_putchar('\n'); if (!tmp2) continue ; *s = ft_strjoin(tmp, tmp2); + data_singleton()->line.input = *s; ft_strdel(&tmp); ft_strdel(&tmp2); } diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index 79e7f552..f48668b4 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 14:38:30 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 18:03:23 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,8 @@ void ft_init_line(void) { data_singleton()->line.input = NULL; + data_singleton()->line.copy_tmp = NULL; + data_singleton()->line.pos = 0; data_singleton()->line.prompt_size = 0; data_singleton()->line.list_size = 0; data_singleton()->line.list_end = NULL; @@ -76,11 +78,12 @@ int ft_readline(void) return (-1); if (data_singleton()->line.input) ft_strdel(&data_singleton()->line.input); + data_singleton()->line.list_cur = data_singleton()->line.list_beg; + data_singleton()->line.pos = 0; ft_prompt(); - data_singleton()->line.input = ft_lecture(data_singleton()->line.list_beg); + data_singleton()->line.input = ft_read_stdin(); ft_putchar('\n'); - ft_check_quotes(&data_singleton()->line.input, - data_singleton()->line.list_beg); + ft_check_quotes(&data_singleton()->line.input); ft_check_heredoc(&data_singleton()->line.input); ft_check_backslash(&data_singleton()->line.input); ft_history_parsing(); diff --git a/42sh/src/line-editing/surch_in_history.c b/42sh/src/line-editing/surch_in_history.c index 6630548b..424dc5d2 100644 --- a/42sh/src/line-editing/surch_in_history.c +++ b/42sh/src/line-editing/surch_in_history.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 10:43:16 by gwojda #+# #+# */ -/* Updated: 2017/01/26 14:01:53 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 15:16:42 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,12 +32,16 @@ void ft_surch_and_realloc(char **str, char **str_srch, int ret, size_t *srch_pos *str = ft_strget_history(*str_srch); } -void ft_surch_in_history(char **str, size_t *pos) +void ft_surch_in_history(void) { char *str_srch; int ret; size_t srch_pos; + char **str; + size_t *pos; + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; srch_pos = 0; str_srch = NULL; if (*str) diff --git a/42sh/src/line-editing/tool_line_2.c b/42sh/src/line-editing/tool_line_2.c index 4ef5fe9a..c5c9dd8e 100644 --- a/42sh/src/line-editing/tool_line_2.c +++ b/42sh/src/line-editing/tool_line_2.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 19:07:52 by gwojda #+# #+# */ -/* Updated: 2017/02/01 15:24:50 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 14:16:24 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,11 +24,15 @@ static void ft_clear_window() execve("/usr/bin/clear", tab_str, data_singleton()->env); } -void ft_printall(char *str, size_t *pos) +void ft_printall(void) { size_t pos_tmp; int ret; + char *str; + size_t *pos; + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; ret = 0; if (read(0, &ret, sizeof(int)) == -1 || ret != 126 || !str) return ; diff --git a/42sh/src/line-editing/toolz.c b/42sh/src/line-editing/toolz.c index c71ea256..1051442e 100644 --- a/42sh/src/line-editing/toolz.c +++ b/42sh/src/line-editing/toolz.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/16 17:06:30 by gwojda #+# #+# */ -/* Updated: 2017/01/20 18:54:46 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 16:22:47 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,35 +24,6 @@ void ft_putnc(char c, int n) } } -void ft_free_tabstr(char ***env) -{ - int i; - - i = 0; - while ((*env)[i]) - { - free((*env)[i]); - ++i; - } - free(*env); - env = NULL; -} - -int ft_is_whitespaces(char *str) -{ - int i; - - i = 0; - while (str[i]) - { - if (str[i] != '\t' || str[i] != ' ') - return (1); - ++i; - } - return (0); -} - - char *ft_strndup(char const *s, int n) { int i; diff --git a/42sh/src/line-editing/toolz2.c b/42sh/src/line-editing/toolz2.c index 95c99d85..ce1c62f6 100644 --- a/42sh/src/line-editing/toolz2.c +++ b/42sh/src/line-editing/toolz2.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/07 11:00:28 by gwojda #+# #+# */ -/* Updated: 2017/02/02 13:31:57 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 16:04:55 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/toolz_parseur.c b/42sh/src/line-editing/toolz_parseur.c index adc6b90b..a0781e12 100644 --- a/42sh/src/line-editing/toolz_parseur.c +++ b/42sh/src/line-editing/toolz_parseur.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/16 16:14:46 by gwojda #+# #+# */ -/* Updated: 2017/02/02 11:46:55 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 14:29:34 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,95 +49,3 @@ char *ft_remove_imput(char *str, size_t pos) free(new_str); return (new_str2); } - -void ft_found_prev_word(char *str, size_t *pos) -{ - int i; - - i = 0; - if (!*pos) - return ; - if (str[*pos - 1] != '\n' && !(str[*pos] == '\n' || str[*pos] == ' ')) - { - ft_puttermcaps("le"); - --(*pos); - } - while ((int)*pos - i - 1 > 0 && str[*pos - i - 1] == ' ') - { - ft_puttermcaps("le"); - ++i; - } - (*pos) = (i && str[*pos - i] == '\n') ? *pos - i + 1 : *pos - i; - i = 0; - if (*pos >= 1 && str[*pos - 1] == '\n') - { - ft_puttermcaps("cd"); - if (*pos >= 2) - (*pos) -= 2; - ft_get_beggin(str, pos); - ft_current_str(str, *pos); - ft_get_next_str(str, pos); - ++(*pos); - } - else - { - if (*pos && !(str[*pos] == '\n' || str[*pos] == ' ')) - { - ft_puttermcaps("le"); - --(*pos); - } - while (*pos - i && (str[*pos - i] == '\n' || str[*pos - i] == ' ')) - { - ft_puttermcaps("le"); - ++i; - } - while (*pos - i && str[*pos - i] != '\n' && str[*pos - i] != ' ') - { - ft_puttermcaps("le"); - ++i; - } - if (str[*pos - i] == '\n' || str[*pos - i] == ' ') - { - ft_puttermcaps("nd"); - ++(*pos); - } - (*pos) -= i; - } -} - -void ft_found_next_word(char *str, size_t *pos) -{ - int i; - - i = 0; - while (str[i + *pos] && str[i + *pos] == ' ') - { - ft_putchar(str[i + *pos]); - ++i; - } - if (str[*pos] == '\n') - { - if (*pos) - { - --(*pos); - ft_get_beggin_with_curs(str, pos); - } - ft_puttermcaps("cd"); - ft_get_next_str(str, pos); - (*pos) += (*pos) ? 2 : 1; - ft_current_str(str, *pos); - ft_get_next_str(str, pos); - if (!str[*pos]) - --(*pos); - ft_get_beggin_with_curs(str, pos); - } - else - { - while (str[i + *pos] && str[i + *pos] != '\n' && str[i + *pos] != ' ') - { - ft_putchar(str[i + *pos]); - ++i; - } - *pos += i; - } -}