diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index dabaaa87..fb23daf1 100644 --- a/42sh/includes/ft_readline.h +++ b/42sh/includes/ft_readline.h @@ -6,13 +6,14 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */ -/* Updated: 2017/02/01 15:10:44 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 13:23:12 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_READLINE_H # define FT_READLINE_H +# include # include # include # include @@ -71,6 +72,7 @@ typedef struct s_line 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); diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index b5be9b33..c0aec11e 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 10:41:26 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 13:34:47 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,24 @@ void ft_init_line(void) data_singleton()->line.opt = 0; } +void ft_init_history(void) +{ + int fd; + char *str; + + fd = open(".42sh_history", O_RDONLY); + if (fd == -1) + return ; + while (get_next_line(fd, &str) > 0) + { + ft_push_back_history(&data_singleton()->line.list_beg, + ft_create_history_list(str)); + free(str); + } + free(str); + close(fd); +} + struct termios *ft_save_stats_term(void) { static struct termios *term_save = NULL; @@ -41,6 +59,7 @@ struct termios *ft_stats_term_termcaps(void) if (!term) { ft_init_line(); + ft_init_history(); term = (struct termios *)malloc(sizeof(struct termios)); tcgetattr(0, term); (*term).c_lflag &= ~(ECHO | ICANON | ISIG); @@ -50,22 +69,8 @@ struct termios *ft_stats_term_termcaps(void) return (term); } -void ft_reset_stats_term(int signal) -{ - char *name_term; - - if (signal == SIGWINCH) - { - if ((name_term = getenv("TERM")) == NULL) - return ; - if (tgetent(NULL, name_term) == -1) - return ; - } -} - int ft_readline(void) { - signal(SIGWINCH, ft_reset_stats_term); ft_save_stats_term(); if (tcsetattr(0, TCSANOW, ft_stats_term_termcaps()) == -1) return (-1); @@ -80,8 +85,11 @@ int ft_readline(void) ft_check_backslash(&data_singleton()->line.input); 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)); + ft_add_in_history_file(data_singleton()->line.input); + } if (tcsetattr(0, TCSANOW, ft_save_stats_term()) == -1) return (-1); return (0); diff --git a/42sh/src/line-editing/toolz2.c b/42sh/src/line-editing/toolz2.c index 9be15d3e..95c99d85 100644 --- a/42sh/src/line-editing/toolz2.c +++ b/42sh/src/line-editing/toolz2.c @@ -6,12 +6,24 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/07 11:00:28 by gwojda #+# #+# */ -/* Updated: 2017/01/24 15:00:16 by gwojda ### ########.fr */ +/* Updated: 2017/02/02 13:31:57 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +void ft_add_in_history_file(char *str) +{ + int fd; + + fd = open(".42sh_history", O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR); + if (fd == -1) + return ; + write(fd, str, ft_strlen(str)); + write(fd, "\n", 1); + close(fd); +} + int ft_nbr_len(int nbr) { if (nbr % 10 != nbr) @@ -33,7 +45,10 @@ void ft_puttermcaps(char *str) int ft_size_term(void) { - return (tgetnum("co")); + struct winsize w; + + ioctl(0, TIOCGWINSZ, &w); + return (w.ws_col); } long long ft_pow(int nbr, int power)