From dfaf7286606a3e03d544db272c46741ff66f00e3 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 19 Mar 2017 15:08:30 +0100 Subject: [PATCH 1/2] builtin_read many changes --- 42sh/includes/builtin_read.h | 6 +-- 42sh/includes/minishell.h | 1 + 42sh/libft/Makefile | 1 - 42sh/libft/includes/cliopts.h | 2 +- 42sh/libft/src/cliopts/cliopts_get.c | 5 +-- 42sh/libft/src/cliopts/cliopts_getdata.c | 32 --------------- 42sh/src/builtin/bt_read_get.c | 17 ++++---- 42sh/src/builtin/bt_read_term.c | 18 ++++---- 42sh/src/builtin/builtin_export.c | 2 +- 42sh/src/builtin/builtin_read.c | 52 ++++++++++++------------ 42sh/src/main/data_init.c | 2 +- 42sh/src/main/shell_init.c | 11 ++--- 12 files changed, 53 insertions(+), 96 deletions(-) delete mode 100644 42sh/libft/src/cliopts/cliopts_getdata.c diff --git a/42sh/includes/builtin_read.h b/42sh/includes/builtin_read.h index cf3edb5b..c40d11e4 100644 --- a/42sh/includes/builtin_read.h +++ b/42sh/includes/builtin_read.h @@ -13,10 +13,6 @@ #ifndef BUILTIN_READ_H # define BUILTIN_READ_H -/* # include "types.h" */ -/* # include "builtin.h" */ -/* # include "minishell.h" */ - # define READ_OPT_LA (1 << 0) # define READ_OPT_LD (1 << 1) # define READ_OPT_LE (1 << 2) @@ -35,12 +31,12 @@ typedef struct s_readopt t_readopt; struct s_read { t_flag opts; + char **names; char delim; int nchars; char *prompt; int timeout; int fd; - char **names; char *input; }; diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 7b012022..20db1a21 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -41,6 +41,7 @@ struct s_data { t_flag opts; + char **av_data; int fd; char **env; int argc; diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index a861e167..415c540f 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -49,7 +49,6 @@ char/ft_isprint.c\ char/ft_tolower.c\ char/ft_toupper.c\ cliopts/cliopts_get.c\ -cliopts/cliopts_getdata.c\ cliopts/cliopts_has.c\ color/ft_color_mk.c\ color/ft_color_mkif.c\ diff --git a/42sh/libft/includes/cliopts.h b/42sh/libft/includes/cliopts.h index 2de89bd8..ad21c697 100644 --- a/42sh/libft/includes/cliopts.h +++ b/42sh/libft/includes/cliopts.h @@ -32,10 +32,10 @@ struct s_cliopts struct s_data_template { t_flag flag; + char **av_data; }; int cliopts_get(char **av, t_cliopts opt_map[], void *data); -char **cliopts_getdata(char **av); int cliopts_has(char **av, char c); #endif diff --git a/42sh/libft/src/cliopts/cliopts_get.c b/42sh/libft/src/cliopts/cliopts_get.c index 9aaea51f..b9551f1d 100644 --- a/42sh/libft/src/cliopts/cliopts_get.c +++ b/42sh/libft/src/cliopts/cliopts_get.c @@ -58,7 +58,6 @@ static int cliopts_parse_short(char ***av, t_cliopts opt_map[], void *data) ++(*av); if ((map->get)(av, data)) return (ERR_SET(E_CO_MISS, *arg)); - break; } ((t_data_template*)data)->flag |= map->flag_on; ((t_data_template*)data)->flag &= ~map->flag_off; @@ -66,7 +65,6 @@ static int cliopts_parse_short(char ***av, t_cliopts opt_map[], void *data) } ++(*av); return (0); - } static int cliopts_parse_long(char ***av, t_cliopts opt_map[], void *data) @@ -109,7 +107,8 @@ int cliopts_get(char **av, t_cliopts opt_map[], void *data) return (1); } else - return (0); + break ; } + ((t_data_template*)data)->av_data = av; return (0); } diff --git a/42sh/libft/src/cliopts/cliopts_getdata.c b/42sh/libft/src/cliopts/cliopts_getdata.c deleted file mode 100644 index 1beb10cd..00000000 --- a/42sh/libft/src/cliopts/cliopts_getdata.c +++ /dev/null @@ -1,32 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* cliopts_getdata.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/14 20:35:15 by jhalford #+# #+# */ -/* Updated: 2017/03/14 21:35:19 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cliopts.h" - -char **cliopts_getdata(char **av) -{ - if (!av) - return (NULL); - av++; - while (*av) - { - if (ft_strcmp(*av, "--") == 0) - return (av + 1); - else if ((*av)[0] == '-' && (*av)[1] == '-') - av++; - else if ((*av)[0] == '-') - av++; - else - return (av); - } - return (av); -} diff --git a/42sh/src/builtin/bt_read_get.c b/42sh/src/builtin/bt_read_get.c index 2f94336b..27ff5f50 100644 --- a/42sh/src/builtin/bt_read_get.c +++ b/42sh/src/builtin/bt_read_get.c @@ -16,7 +16,8 @@ int bt_read_getdelim(char ***av, t_read *data) { if (!av || !*av) return (1); - data->delim = ***av; + if (data) + data->delim = ***av; return (0); } @@ -24,17 +25,17 @@ int bt_read_getnchars(char ***av, t_read *data) { if (!av || !*av) return (1); - data->nchars = ft_atoi(**av); + if (data) + data->nchars = ft_atoi(**av); return (0); } int bt_read_getprompt(char ***av, t_read *data) { - DG("getting prompt"); if (!av || !*av || !**av) return (1); - data->prompt = **av; - DG("got prompt [%s]", data->prompt); + if (data) + data->prompt = **av; return (0); } @@ -42,7 +43,8 @@ int bt_read_gettimeout(char ***av, t_read *data) { if (!av || !*av) return (1); - data->timeout = ft_atoi(**av); + if (data) + data->timeout = ft_atoi(**av); return (0); } @@ -50,6 +52,7 @@ int bt_read_getfd(char ***av, t_read *data) { if (!av || !*av) return (1); - data->fd = ft_atoi(**av); + if (data) + data->fd = ft_atoi(**av); return (0); } diff --git a/42sh/src/builtin/bt_read_term.c b/42sh/src/builtin/bt_read_term.c index 6438ff58..0ba68f65 100644 --- a/42sh/src/builtin/bt_read_term.c +++ b/42sh/src/builtin/bt_read_term.c @@ -27,17 +27,13 @@ int bt_read_terminit(t_read *data) (void)data; term = bt_read_term(1); - term.c_lflag &= ~(ICANON | ECHO); - if (data->opts & READ_OPT_LT) - { - term.c_cc[VTIME] = data->timeout * 10; - term.c_cc[VMIN] = 0; - } - else - { - term.c_cc[VTIME] = 0; - term.c_cc[VMIN] = 1; - } + term.c_lflag = ECHO | ECHOE | ECHOK | ICANON; + term.c_lflag &= data->timeout ? ~ICANON : ~0; + if (data->opts & READ_OPT_LS) + term.c_lflag &= ~ECHO; + term.c_cc[VTIME] = data->timeout * 10; + term.c_cc[VMIN] = !data->timeout; + term.c_cc[VEOL] = data->delim; if (tcsetattr(0, TCSANOW, &term) < 0) return (-1); return (0); diff --git a/42sh/src/builtin/builtin_export.c b/42sh/src/builtin/builtin_export.c index af4d6a5e..36772cf9 100644 --- a/42sh/src/builtin/builtin_export.c +++ b/42sh/src/builtin/builtin_export.c @@ -48,7 +48,7 @@ int builtin_export( ft_perror(); if (data.flag & BT_EXPORT_LP) return (bt_export_print()); - av = cliopts_getdata((char**)av); + av = data.av_data; while (*av) { if ((equal = ft_strchr(*av, '='))) diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index 31e923dd..4d62333e 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -38,18 +38,18 @@ int bt_read_init(t_read *data, char **av) data->fd = 0; data->timeout = 0; data->input = NULL; - data->names = NULL; if ((cliopts_get(av, g_read_opts, data))) return(ft_perror()); - if (data->names) - DG("%s,%s", data->names[0], data->names[1]); + DG("opts=%b", data->opts); bt_read_terminit(data); return (0); } + int bt_read_loop(t_read *data) { int i; int esc; + int ret; char buf[2]; esc = 0; @@ -58,16 +58,15 @@ int bt_read_loop(t_read *data) ft_printf(data->prompt); while (42) { - if (read(data->fd, buf, 1) <= 0) - return (1); - buf[1] = 0; - if (!(data->opts &READ_OPT_LS)) - ft_putchar(*buf); + if ((ret = read(data->fd, buf, 1)) <= 0) + return (ret); + buf[ret] = 0; + DG("read [%c]", *buf); if (!esc && *buf == data->delim) break ; esc = esc ? 0 : !(data->opts & READ_OPT_LR) && (*buf == '\\'); ft_strappend(&data->input, buf); - if (*buf == '\n' && !(data->opts & READ_OPT_LR)) + if (*buf == '\n' && !(data->opts & (READ_OPT_LR | READ_OPT_LS))) ft_putstr("> "); if ((data->opts & READ_OPT_LN) && ++i >= data->nchars) break ; @@ -80,25 +79,22 @@ int bt_read_assign(t_read *data) char *input; char **names; char *ifs; - char *start; + char *tok; + DG("assigning"); input = data->input; - names = data->names ? data->names : (char*[]){"REPLY", NULL}; - ifs = ft_getenv(data_singleton()->env, "IFS"); - start = input; - while (*start && *names) + names = *data->names ? data->names : (char*[]){"REPLY", NULL}; + ifs = ft_getenv(data_singleton()->local_var, "IFS"); + if (!names[1]) + ifs = NULL; + tok = ft_strtok(input, ifs); + while (*names) { - if (!(names[1]) || !ifs) - { - builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL); - break ; - } - while (*input && !ft_strchr(ifs, *input)) - input++; - while (input && ft_strchr(ifs, *input)) - *(input++) = 0; - builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL); - start = input; + DG("tok=%s", tok); + DG("name=%s", *names); + builtin_setenv("setenv", (char*[]){"setenv", *names, tok}, NULL); + ifs = names[1] ? ifs : NULL; + tok = ft_strtok(NULL, ifs); names++; } return (0); @@ -114,10 +110,12 @@ int builtin_read(const char *path, char *const av[], char *const envp[]) ret = 0; if (bt_read_init(&data, (char **)av)) ret = 2; - else if (bt_read_loop(&data)) - ret = 1; + else if ((ret = bt_read_loop(&data))) + ; else if (bt_read_assign(&data)) ret = 1; + if (ret == -1) + exit(1); if (ret != 0) bt_read_usage(); if (ret != 2) diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 7c901196..7bc9ef65 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -35,7 +35,7 @@ int data_init(int ac, char **av) ft_strdel(&shlvl); } data->comp = NULL; - data->opts = 0; + data->opts = SH_INTERACTIVE | SH_OPTS_JOBC; exec_reset(); data->lst_func = NULL; lexer_init(&data->lexer); diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 0ef8b902..ec0a87ab 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -28,19 +28,19 @@ int get_input_fd(char **av) struct stat buf; data = data_singleton(); + file = *data->av_data; if (SH_IS_INTERACTIVE(data->opts)) return (STDIN); else if (data->opts & SH_OPTS_LC) { pipe(fds); - file = *cliopts_getdata(av); write(fds[PIPE_WRITE], file, ft_strlen(file)); close(fds[PIPE_WRITE]); dup2_close(fds[PIPE_READ], (fd = 10)); fcntl(fd, F_SETFD, FD_CLOEXEC); return (fd); } - else if ((file = *cliopts_getdata(av)) && !stat(file, &buf)) + else if (file && !stat(file, &buf)) { fd = -1; if (S_ISDIR(buf.st_mode)) @@ -85,13 +85,10 @@ int shell_init(int ac, char **av) data = data_singleton(); data_init(ac, av); - if (isatty(STDIN) && !*cliopts_getdata(av)) - { - data->opts |= SH_INTERACTIVE; - data->opts |= SH_OPTS_JOBC; - } if (cliopts_get(av, g_opts, data)) return (ft_perror()); + if (!isatty(STDIN) || *data->av_data) + data->opts &= ~(SH_INTERACTIVE | SH_OPTS_JOBC); data->fd = get_input_fd(av); if (SH_IS_INTERACTIVE(data->opts)) interactive_settings(); From 5b27418eade72ccd8220126cf141e283c17aa57c Mon Sep 17 00:00:00 2001 From: gwojda Date: Sun, 19 Mar 2017 15:35:21 +0100 Subject: [PATCH 2/2] fix invalid read/leaks completion --- 42sh/Makefile | 4 ++-- 42sh/src/completion/c_find_abspath.c | 4 +++- 42sh/src/completion/c_match.c | 10 ++++++++-- 42sh/src/completion/c_match_update.c | 2 +- 42sh/src/completion/c_output.c | 16 ++++++++-------- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/42sh/Makefile b/42sh/Makefile index 8d15f1b5..d1e649ce 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,14 +6,14 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/03/18 14:17:54 by gwojda ### ########.fr # +# Updated: 2017/03/19 15:14:11 by gwojda ### ########.fr # # # # **************************************************************************** # NAME = 42sh CC = gcc -FLAGS = -Wall -Wextra -Werror -fvisibility=hidden +FLAGS = -Wall -Wextra -Werror -g D_FLAGS = -g DELTA = $$(echo "$$(tput cols)-47"|bc) diff --git a/42sh/src/completion/c_find_abspath.c b/42sh/src/completion/c_find_abspath.c index b29bcdca..8e169306 100644 --- a/42sh/src/completion/c_find_abspath.c +++ b/42sh/src/completion/c_find_abspath.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/09 16:54:59 by gwojda #+# #+# */ -/* Updated: 2017/03/18 14:04:24 by gwojda ### ########.fr */ +/* Updated: 2017/03/19 15:22:06 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,8 @@ void c_seek_abs_path(t_comp *c, char *current_word) len = ft_strrchr(c->rcmd, '/') - current_word + 1; if (len < 0) return ; + if (c->cpath) + ft_strdel(&c->cpath); c->cpath = ft_strndup(current_word, len); if (current_word[0] == '~') { diff --git a/42sh/src/completion/c_match.c b/42sh/src/completion/c_match.c index ce15d99e..06440ac5 100644 --- a/42sh/src/completion/c_match.c +++ b/42sh/src/completion/c_match.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/15 13:27:14 by alao #+# #+# */ -/* Updated: 2017/03/16 10:20:44 by gwojda ### ########.fr */ +/* Updated: 2017/03/19 15:34:38 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ static int c_chevron(t_comp *c) size_t pos; pos = c->ircmd; + if (pos >= ft_strlen(c->rcmd)) + pos = ft_strlen(c->rcmd) - (ft_strlen(data_singleton()->line.input) - pos); while (pos) { if (c->rcmd[pos] == '<' || c->rcmd[pos] == '>') @@ -38,9 +40,12 @@ static int c_chevron(t_comp *c) static char *c_current_words(t_comp *c) { - int pos; + size_t pos; pos = c->ircmd; + if (pos >= ft_strlen(c->rcmd)) + pos = ft_strlen(c->rcmd) - + (ft_strlen(data_singleton()->line.input) - pos + 1); while (pos && c->rcmd[pos] != ' ') --pos; if (c->rcmd[pos] == ' ') @@ -57,6 +62,7 @@ int c_matching(t_data *s, t_comp *c) char *current_word; current_word = c_current_words(c); + DG("current_word = %s", current_word); if (ft_strchr(c->rcmd, '/')) c_seek_abs_path(c, current_word); else if (ft_strchr(c->rcmd, '$')) diff --git a/42sh/src/completion/c_match_update.c b/42sh/src/completion/c_match_update.c index bbbee3b2..00f32313 100644 --- a/42sh/src/completion/c_match_update.c +++ b/42sh/src/completion/c_match_update.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 12:03:30 by alao #+# #+# */ -/* Updated: 2017/03/17 12:03:26 by gwojda ### ########.fr */ +/* Updated: 2017/03/19 15:28:50 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/completion/c_output.c b/42sh/src/completion/c_output.c index e3c207e2..70c11f7e 100644 --- a/42sh/src/completion/c_output.c +++ b/42sh/src/completion/c_output.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:10:38 by alao #+# #+# */ -/* Updated: 2017/03/17 16:48:49 by gwojda ### ########.fr */ +/* Updated: 2017/03/19 15:18:55 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -105,12 +105,6 @@ int c_keypress(t_comp *c, long int key) { t_clst *ptr; - if (key == 27 || key == 127 || key == 2117294875) - { - c_term_clear(c); - c_clear(data_singleton()); - return (0); - } if (key == 10 || key == 32) { ptr = c->lst; @@ -119,10 +113,16 @@ int c_keypress(t_comp *c, long int key) c_updater(c, ptr->name); return (1); } - if (key == KP_U || key == KP_D || key == KP_L || key == KP_R) + else if (key == KP_U || key == KP_D || key == KP_L || key == KP_R) { c_arrow(c, key); return (0); } + else if (!ft_isprint(key)) + { + c_term_clear(c); + c_clear(data_singleton()); + return (0); + } return ((c_rematch(c, key)) ? (0) : (1)); }