From 59d37acc61ce7e46f0300f7df6ee9c67f87bde73 Mon Sep 17 00:00:00 2001 From: gwojda Date: Wed, 22 Mar 2017 21:29:11 +0100 Subject: [PATCH 1/2] fix bugs multiligne c/c/p --- 42sh/Makefile | 1 + 42sh/includes/ft_readline.h | 5 +- 42sh/src/line_editing/copy_cut_paste.c | 48 +--------------- 42sh/src/line_editing/underline_function.c | 21 ++++++- 42sh/src/line_editing/underline_reset.c | 64 ++++++++++++++++++++++ 5 files changed, 90 insertions(+), 49 deletions(-) create mode 100644 42sh/src/line_editing/underline_reset.c diff --git a/42sh/Makefile b/42sh/Makefile index 221f230c..4f4b54e0 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -268,6 +268,7 @@ line_editing/queue.c\ line_editing/readline.c\ line_editing/resize.c\ line_editing/underline_function.c\ +line_editing/underline_reset.c\ main/data_exit.c\ main/data_init.c\ main/data_singleton.c\ diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index cbef9516..9536c4ab 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/03/22 20:59:35 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 21:14:55 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -129,6 +129,9 @@ void sigwinch_resize(int sig); size_t ft_hist_len(void); void underline_right(char **str, size_t *pos, size_t pos_ref); void underline_left(char **str, size_t *pos, size_t pos_ref); +void reset_term(char **str, size_t *pos); +void reset_and_remove_term(char **str, size_t *pos, char *copy_tmp); +int reset_term_hard(void); int ft_read_stdin(char **input); diff --git a/42sh/src/line_editing/copy_cut_paste.c b/42sh/src/line_editing/copy_cut_paste.c index 5e524c15..23411367 100644 --- a/42sh/src/line_editing/copy_cut_paste.c +++ b/42sh/src/line_editing/copy_cut_paste.c @@ -6,54 +6,12 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 12:45:06 by gwojda #+# #+# */ -/* Updated: 2017/03/22 20:58:12 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 21:14:34 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static void reset_term(char **str, size_t *pos) -{ - size_t pos_ref; - - pos_ref = *pos; - if (*pos) - { - --(*pos); - ft_get_beggin_with_curs(*str, pos); - } - ft_puttermcaps("cd"); - ft_current_str(*str, *pos); - ft_get_next_str(*str, pos); - ft_putnc('\b', *pos - pos_ref); - (*pos) = pos_ref; -} - -static void reset_and_remove_term(char **str, size_t *pos, char *copy_tmp) -{ - size_t pos_ref; - - pos_ref = *pos; - if (!data_singleton()->line.pos_tmp) - pos_ref += ft_strlen(data_singleton()->line.copy_tmp); - while (*copy_tmp) - { - --pos_ref; - *str = ft_remove_imput(*str, pos_ref); - ++copy_tmp; - } - if (*pos) - { - --(*pos); - ft_get_beggin_with_curs(*str, pos); - } - ft_puttermcaps("cd"); - ft_current_str(*str, *pos); - ft_get_next_str(*str, pos); - ft_putnc('\b', *pos - pos_ref); - (*pos) = pos_ref; -} - int ft_x(char **str, size_t *pos) { int ret; @@ -70,7 +28,7 @@ int ft_x(char **str, size_t *pos) if (read(STDIN, &ret, sizeof(int)) < 0) return (-1); if ((*str)[*pos] == '\n') - return (0); + return (reset_term_hard()); if (ret == FLECHE_GAUCHE) underline_left(str, pos, pos_ref); else if (ret == FLECHE_DROITE) @@ -99,7 +57,7 @@ int ft_c(char **str, size_t *pos) if (read(STDIN, &ret, sizeof(int)) < 0) return (-1); if ((*str)[*pos] == '\n') - return (0); + return (reset_term_hard()); if (ret == FLECHE_GAUCHE) underline_left(str, pos, pos_ref); else if (ret == FLECHE_DROITE) diff --git a/42sh/src/line_editing/underline_function.c b/42sh/src/line_editing/underline_function.c index 54830597..5412e6af 100644 --- a/42sh/src/line_editing/underline_function.c +++ b/42sh/src/line_editing/underline_function.c @@ -6,13 +6,24 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/22 20:55:24 by gwojda #+# #+# */ -/* Updated: 2017/03/22 20:56:54 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 21:28:31 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void underline_right(char **str, size_t *pos, size_t pos_ref) +static void underline_check_end_of_line(char *str, size_t pos) +{ + if (!str) + return ; + if (ft_nb_last_line(str, pos) == 0) + { + ft_putchar(str[pos]); + ft_puttermcaps("le"); + } +} + +void underline_right(char **str, size_t *pos, size_t pos_ref) { if (!(*pos < ft_strlen(*str))) return ; @@ -25,6 +36,7 @@ void underline_right(char **str, size_t *pos, size_t pos_ref) ft_puttermcaps("me"); ++(data_singleton()->line.pos_tmp); ++(*pos); + underline_check_end_of_line(*str, *pos); } else { @@ -34,10 +46,11 @@ void underline_right(char **str, size_t *pos, size_t pos_ref) if (data_singleton()->line.pos_tmp) --(data_singleton()->line.pos_tmp); ++(*pos); + underline_check_end_of_line(*str, *pos); } } -void underline_left(char **str, size_t *pos, size_t pos_ref) +void underline_left(char **str, size_t *pos, size_t pos_ref) { if (!*pos) return ; @@ -46,6 +59,7 @@ void underline_left(char **str, size_t *pos, size_t pos_ref) --(*pos); ft_putchar('\b'); ft_putchar((*str)[*pos]); + underline_check_end_of_line(*str, *pos + 1); ft_putchar('\b'); data_singleton()->line.copy_tmp = ft_remove_imput(data_singleton()-> line.copy_tmp, data_singleton()->line.pos_tmp); @@ -61,6 +75,7 @@ void underline_left(char **str, size_t *pos, size_t pos_ref) data_singleton()->line.copy_tmp = ft_realloc_imput(data_singleton()-> line.copy_tmp, (*str)[*pos], data_singleton()->line.pos_tmp); ft_putchar((*str)[*pos]); + underline_check_end_of_line(*str, *pos + 1); ft_puttermcaps("me"); ft_putchar('\b'); } diff --git a/42sh/src/line_editing/underline_reset.c b/42sh/src/line_editing/underline_reset.c new file mode 100644 index 00000000..bddcf88c --- /dev/null +++ b/42sh/src/line_editing/underline_reset.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* underline_reset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/22 21:14:07 by gwojda #+# #+# */ +/* Updated: 2017/03/22 21:16:24 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void reset_term(char **str, size_t *pos) +{ + size_t pos_ref; + + pos_ref = *pos; + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(*str, pos); + } + ft_puttermcaps("cd"); + ft_current_str(*str, *pos); + ft_get_next_str(*str, pos); + ft_putnc('\b', *pos - pos_ref); + (*pos) = pos_ref; +} + +void reset_and_remove_term(char **str, size_t *pos, char *copy_tmp) +{ + size_t pos_ref; + + pos_ref = *pos; + if (!data_singleton()->line.pos_tmp) + pos_ref += ft_strlen(data_singleton()->line.copy_tmp); + while (*copy_tmp) + { + --pos_ref; + *str = ft_remove_imput(*str, pos_ref); + ++copy_tmp; + } + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(*str, pos); + } + ft_puttermcaps("cd"); + ft_current_str(*str, *pos); + ft_get_next_str(*str, pos); + ft_putnc('\b', *pos - pos_ref); + (*pos) = pos_ref; +} + +int reset_term_hard(void) +{ + ft_putnc('\b', ft_strlen(data_singleton()->line.copy_tmp)); + ft_putstr(data_singleton()->line.copy_tmp); + ft_strdel(&data_singleton()->line.copy_tmp); + data_singleton()->line.pos_tmp = 0; + return (0); +} From 1cd40a7d44c7392a69e94d9b115f3ece47895242 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Wed, 22 Mar 2017 21:49:22 +0100 Subject: [PATCH 2/2] no leaks var --- 42sh/src/exec/exec_var.c | 7 ++++--- 42sh/src/exec/ft_exec.c | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/42sh/src/exec/exec_var.c b/42sh/src/exec/exec_var.c index dbeaea7c..496a3fcc 100644 --- a/42sh/src/exec/exec_var.c +++ b/42sh/src/exec/exec_var.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 11:12:05 by ariard #+# #+# */ -/* Updated: 2017/03/20 15:43:34 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 21:34:39 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,8 +30,9 @@ int exec_var(t_btree **ast) node = (*ast)->item; av = token_to_argv(node->data.cmd.token, 1); set_var(av[0], &var, &value); - if (ft_getenv(data_singleton()->env, var)) - return (0); builtin_setenv("internal", (char*[]){"local", var, value, 0}, NULL); + ft_strdel(&var); + ft_strdel(&value); + ft_tabdel(&av); return (0); } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 38897055..5cf734ba 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2017/03/16 23:38:29 by ariard ### ########.fr */ +/* Updated: 2017/03/22 19:52:58 by ariard ### ########.fr */ /* */ /* ************************************************************************** */