diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index dd5c0698..f47ca6be 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/01/24 16:42:30 by gwojda ### ########.fr */ +/* Updated: 2017/01/25 14:37:32 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,6 +49,8 @@ # define TOUCHE_F5 892427035 # define TOUCHE_F6 925981467 +# define HIST 1 + typedef struct s_list_history { char *str; @@ -61,10 +63,16 @@ typedef struct s_line char *input; int prompt_size; int list_size; + char opt; t_list_history *list_end; t_list_history *list_beg; } t_line; +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); int ft_nbr_len(int nbr); diff --git a/42sh/src/line-editing/ft_split_whitespaces.c b/42sh/src/line-editing/ft_split_whitespaces.c index ccca5b34..86cb9aaf 100644 --- a/42sh/src/line-editing/ft_split_whitespaces.c +++ b/42sh/src/line-editing/ft_split_whitespaces.c @@ -6,13 +6,13 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/02 17:52:52 by gwojda #+# #+# */ -/* Updated: 2017/01/19 16:42:38 by gwojda ### ########.fr */ +/* Updated: 2017/01/25 14:10:37 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static char *ft_strdupi_w(char const *s) +char *ft_strdupi_w(char const *s) { int i; char *str; diff --git a/42sh/src/line-editing/get_touch.c b/42sh/src/line-editing/get_touch.c index 95fe46c5..14104d63 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/01/23 13:32:04 by gwojda ### ########.fr */ +/* Updated: 2017/01/25 14:39:59 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,7 +50,16 @@ char *ft_lecture(t_list_history *head) size_t i; str = NULL; + if (data_singleton()->line.opt & HIST) + str = data_singleton()->line.input; i = 0; + if (str) + { + ft_current_str(str, i); + ft_get_next_str(str, &i); + if (str[i]) + ++i; + } while (42) { ft_check_end_of_line(str, i); diff --git a/42sh/src/line-editing/history_parsing.c b/42sh/src/line-editing/history_parsing.c new file mode 100644 index 00000000..7e5fdecd --- /dev/null +++ b/42sh/src/line-editing/history_parsing.c @@ -0,0 +1,122 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* history_parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/25 11:39:47 by gwojda #+# #+# */ +/* Updated: 2017/01/25 15:05:04 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static char *ft_strget_history(char *str) +{ + t_list_history *list; + size_t i; + char *tmp; + + list = data_singleton()->line.list_beg; + if (!list) + return (NULL); + if (!list->str) + list = list->prev; + while (list->str) + { + tmp = list->str; + i = 0; + while (tmp[i]) + { + if (ft_strlen(tmp + i) >= ft_strlen(str) + && !ft_strncmp(tmp + i, str, ft_strlen(str))) + return (tmp); + ++i; + } + list = list->prev; + } + return (list->str); +} + +void ft_realloc_str_history_3(char **str, size_t pos, char *s) +{ + char *new_str; + char *new_str2; + char *new_str3; + + if (!*str) + return ; + new_str = ft_strndup(*str, pos); + new_str3 = ft_strget_history(s); + if (new_str3) + { + new_str2 = ft_strjoin(new_str, new_str3); + free(new_str); + new_str3 = ft_strjoin(new_str2, (*str) + pos + ft_strlen(s) + 2); + free(new_str2); + } + else + new_str3 = ft_strjoin(new_str, (*str) + pos + ft_strlen(s) + 2); + free(s); + free(*str); + *str = new_str3; +} + +void ft_history_parsing() +{ + char *str; + int i; + char boolean; + + i = 0; + boolean = 0; + str = data_singleton()->line.input; + if (!str) + return ; + while (str[i]) + { + if (str[i] == '!') + { + boolean = 1; + if (!ft_strncmp("!!", str + i, 2)) + { + ft_realloc_str_history(&(data_singleton()->line.input), i, 0, 2); + ++i; + } + else if (ft_isdigit(str[i + 1])) + { + ft_realloc_str_history(&(data_singleton()->line.input), i, + ft_atoi(str + i + 1), ft_nbr_len(ft_atoi(str + i + 1)) + 1); + i += ft_nbr_len(ft_atoi(str + i + 1)); + } + else if (str[i + 1] == '-') + { + ft_realloc_str_history(&(data_singleton()->line.input), i, + data_singleton()->line.list_size - ft_atoi(str + i + 2), + ft_nbr_len(ft_atoi(str + i + 2)) + 2); + i += ft_nbr_len(ft_atoi(str + i + 2) + 1); + } + else if (str[i + 1] == '?') + ft_realloc_str_history_3(&(data_singleton()->line.input), i, + ft_strdupi_w(str + i + 2)); + else if (str[i + 1] != ' ') + ft_realloc_str_history_2(&(data_singleton()->line.input), i, + ft_strdupi_w(str + i + 1)); + else + boolean = 0; + } + ++i; + } + if (boolean) + { + data_singleton()->line.opt = data_singleton()->line.opt | HIST; + ft_prompt(); + data_singleton()->line.input = ft_lecture(data_singleton()->line.list_beg); + 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_heredoc(&data_singleton()->line.input); + ft_history_parsing(); + } +} diff --git a/42sh/src/line-editing/history_parsing_toolz.c b/42sh/src/line-editing/history_parsing_toolz.c new file mode 100644 index 00000000..31a4b818 --- /dev/null +++ b/42sh/src/line-editing/history_parsing_toolz.c @@ -0,0 +1,94 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* history_parsing_toolz.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/25 14:09:39 by gwojda #+# #+# */ +/* Updated: 2017/01/25 14:41:29 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static char *ft_nget_histo(size_t nb_his) +{ + t_list_history *list; + size_t i; + + i = 0; + list = data_singleton()->line.list_beg; + if (!list) + return (NULL); + if (!list->str) + list = list->prev; + while (i < nb_his && list->str) + { + list = list->prev; + ++i; + } + return (list->str); +} + +void ft_realloc_str_history(char **str, size_t pos, int nb_his, int len) +{ + char *new_str; + char *new_str2; + char *new_str3; + + if (!*str) + return ; + new_str = ft_strndup(*str, pos); + ft_printf("\n\npos = %d\n\n", len); + new_str3 = ft_nget_histo(nb_his); + if (new_str3) + { + new_str2 = ft_strjoin(new_str, new_str3); + free(new_str); + new_str3 = ft_strjoin(new_str2, (*str) + pos + len); + free(new_str2); + } + else + new_str3 = ft_strjoin(new_str, (*str) + pos + len); + free(*str); + *str = new_str3; +} + +static char *ft_strget_histo(char *str) +{ + t_list_history *list; + + list = data_singleton()->line.list_beg; + if (!list) + return (NULL); + if (!list->str) + list = list->prev; + while (list->str && ft_strncmp(list->str, str, ft_strlen(str))) + list = list->prev; + return (list->str); +} + +void ft_realloc_str_history_2(char **str, size_t pos, char *s) +{ + char *new_str; + char *new_str2; + char *new_str3; + + if (!*str) + return ; + new_str = ft_strndup(*str, pos); + new_str3 = ft_strget_histo(s); + if (new_str3) + { + new_str2 = ft_strjoin(new_str, new_str3); + free(new_str); + new_str3 = ft_strjoin(new_str2, (*str) + pos + ft_strlen(s) + 1); + free(new_str2); + } + else + new_str3 = ft_strjoin(new_str, (*str) + pos + ft_strlen(s) + 1); + free(s); + free(*str); + *str = new_str3; +} diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index cbab0550..6903af81 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/01/24 17:23:11 by gwojda ### ########.fr */ +/* Updated: 2017/01/25 14:37:16 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ void ft_init_line(void) data_singleton()->line.list_size = 0; data_singleton()->line.list_end = NULL; data_singleton()->line.list_beg = NULL; + data_singleton()->line.opt = 0; } struct termios *ft_save_stats_term(void) @@ -53,11 +54,15 @@ int ft_readline(void) { if (tcsetattr(0, TCSANOW, ft_stats_term_termcaps()) == -1) return (-1); + if (data_singleton()->line.input) + data_singleton()->line.input = NULL; ft_prompt(); data_singleton()->line.input = ft_lecture(data_singleton()->line.list_beg); ft_putstr("\n"); ft_check_quotes(&data_singleton()->line.input, data_singleton()->line.list_beg); ft_check_heredoc(&data_singleton()->line.input); + ft_history_builtin(); + ft_history_parsing(); if (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) diff --git a/42sh/src/line-editing/toolz_parseur.c b/42sh/src/line-editing/toolz_parseur.c index 1f29046a..557027b3 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/01/24 14:14:31 by gwojda ### ########.fr */ +/* Updated: 2017/01/25 11:51:30 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,11 +38,9 @@ char *ft_realloc_imput(char *str, int a, size_t pos) char *ft_remove_imput(char *str, size_t pos) { - int i; char *new_str; char *new_str2; - i = 0; if (!str) return (str); new_str = ft_strndup(str, pos);