From fb35d3ea5319bd35a082a3674f119489e6743c56 Mon Sep 17 00:00:00 2001 From: wescande Date: Thu, 23 Mar 2017 03:25:13 +0100 Subject: [PATCH] rectif issue sur glob /*/*/* + rectif sur le matching avec plusieurs bquote dans le meme word + rectif expansion sur les char esc --- 42sh/Makefile | 4 ++-- 42sh/includes/glob.h | 3 ++- 42sh/src/glob/command_getoutput.c | 18 ++++++++++++++-- 42sh/src/glob/expand_bquote.c | 36 +++++++++++-------------------- 42sh/src/glob/match_pattern.c | 6 +++--- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/42sh/Makefile b/42sh/Makefile index 4f4b54e0..e558b851 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/22 14:59:33 by gwojda ### ########.fr # +# Updated: 2017/03/23 01:59:31 by wescande ### ########.fr # # # # **************************************************************************** # NAME = 42sh CC = gcc -FLAGS = -Wall -Wextra -Werror #-fvisibility=hidden# -fsanitize=address +FLAGS = -Wall -Wextra -Werror -fsanitize=address D_FLAGS = -g DELTA = $$(echo "$$(tput cols)-47"|bc) diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h index e1b1795c..77236481 100644 --- a/42sh/includes/glob.h +++ b/42sh/includes/glob.h @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:31:18 by wescande #+# #+# */ -/* Updated: 2017/03/15 18:52:34 by jhalford ### ########.fr */ +/* Updated: 2017/03/23 03:18:41 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,6 +65,7 @@ char **glob(char *str, unsigned char *esc, void esc_print(char *str, unsigned char *esc); int word_is_assignment(char **content); void *tab_esc_copy(void *content); +char *get_output(char *command); /* ** return TRUE if path file is a directory. diff --git a/42sh/src/glob/command_getoutput.c b/42sh/src/glob/command_getoutput.c index f3d8d8ae..dd024a75 100644 --- a/42sh/src/glob/command_getoutput.c +++ b/42sh/src/glob/command_getoutput.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 19:44:25 by wescande #+# #+# */ -/* Updated: 2017/03/22 22:25:49 by wescande ### ########.fr */ +/* Updated: 2017/03/23 03:19:23 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,8 @@ static char *manage_output(int *fds) output = NULL; close(fds[PIPE_WRITE]); - while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE)) > 0) { + while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE)) > 0) + { buf[ret] = 0; ft_strappend(&output, buf); } @@ -52,3 +53,16 @@ char *command_getoutput(char *command) waitpid(pid, &ret, WUNTRACED); return (manage_output(fds)); } + +char *get_output(char *command) +{ + char *output; + int len; + + if (!(output = command_getoutput(command))) + return (NULL); + len = ft_strlen(output); + while (len && output[--len] == '\n') + output[len] = '\0'; + return (output); +} diff --git a/42sh/src/glob/expand_bquote.c b/42sh/src/glob/expand_bquote.c index 5d3ee1fb..5cdf4200 100644 --- a/42sh/src/glob/expand_bquote.c +++ b/42sh/src/glob/expand_bquote.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 17:47:53 by wescande #+# #+# */ -/* Updated: 2017/03/22 19:24:52 by jhalford ### ########.fr */ +/* Updated: 2017/03/23 03:20:10 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,27 +33,27 @@ static void expand_all_bquote(t_bquote *me, char *content, ft_strlen(me->s2)}), calc_expand_esc(me->esc2, ft_strlen(me->s1), (int[2]){ft_strlen(content), 1}, - (int[2]){ft_strlen(me->s1) + ft_strlen(me->mid), + (int[2]){ft_strlen(me->s1) + ft_strlen(me->mid) + 1, ft_strlen(me->s2)}), 0)); } -static void init_expand(t_bquote *me, char *content, int esc) +static void init_expand(t_bquote *me, char *content, int doifs) { char *ifs; char *content2; - ifs = esc ? NULL : ft_getenv(data_singleton()->local_var, "IFS"); + ifs = doifs ? ft_getenv(data_singleton()->local_var, "IFS") : NULL; content = ft_strtok(content, ifs); if (!content || !(content2 = ft_strtok(NULL, ifs))) { ft_ld_pushfront(me->wk, gen_tab(ft_strjoinf(ft_strjoin(me->s1, content), me->s2, 1), calc_expand_esc(me->esc, ft_strlen(me->s1), (int[2]){ft_strlen(content), 1}, - (int[2]){ft_strlen(me->s1) + ft_strlen(me->mid), ft_strlen(me->s2)}), + (int[2]){ft_strlen(me->s1) + ft_strlen(me->mid) + 1, ft_strlen(me->s2)}), calc_expand_esc(me->esc2, ft_strlen(me->s1), (int[2]){ft_strlen(content), 1}, - (int[2]){ft_strlen(me->s1) + ft_strlen(me->mid), + (int[2]){ft_strlen(me->s1) + ft_strlen(me->mid) + 1, ft_strlen(me->s2)}), 0)); } else @@ -67,19 +67,6 @@ static void init_expand(t_bquote *me, char *content, int esc) } } -static char *get_output(char *command) -{ - char *output; - int len; - - if (!(output = command_getoutput(command))) - return (NULL); - len = ft_strlen(output); - while (output[--len] == '\n') - output[len] = '\0'; - return (output); -} - static int search_bquote(t_bquote *me) { char *sta; @@ -98,7 +85,7 @@ static int search_bquote(t_bquote *me) me->s2 = ft_strdup(me->str + 1); if ((content = get_output(me->mid))) init_expand(me, content, - is_char_esc(me->esc, CH(*me->wk)[0], sta)); + !is_char_esc(me->esc, CH(*me->wk)[0], sta)); ft_strdel(&me->mid); ft_strdel(&me->s1); ft_strdel(&me->s2); @@ -109,12 +96,13 @@ static int search_bquote(t_bquote *me) return (0); } -void delete(t_ld **tmp, t_ld **src) +static int delete(t_ld **tmp, t_ld **src) { if (*tmp == *src) ft_ld_del(src, &ft_tabdel); else ft_ld_del(tmp, &ft_tabdel); + return (1); } void expand_bquote(t_glob *gl) @@ -129,15 +117,15 @@ void expand_bquote(t_glob *gl) while ((gl->m_pat = ft_ld_front(gl->m_pat)) && do_it) { do_it = 0; - while (gl->m_pat && !do_it) + while (gl->m_pat) { me.wk = &gl->m_pat; me.esc = UCH(gl->m_pat)[1]; me.esc2 = UCH(gl->m_pat)[2]; me.str = CH(gl->m_pat)[0] - 1; if ((tmp = gl->m_pat) && - (do_it = search_bquote(&me)) == 1) - delete(&tmp, &gl->m_pat); + (do_it = search_bquote(&me)) && delete(&tmp, &gl->m_pat)) + continue; if (!gl->m_pat || !gl->m_pat->next) break ; gl->m_pat = gl->m_pat->next; diff --git a/42sh/src/glob/match_pattern.c b/42sh/src/glob/match_pattern.c index 159ea57e..94cbed7d 100644 --- a/42sh/src/glob/match_pattern.c +++ b/42sh/src/glob/match_pattern.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/24 17:30:23 by wescande #+# #+# */ -/* Updated: 2017/03/14 23:25:44 by wescande ### ########.fr */ +/* Updated: 2017/03/23 03:16:08 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,8 +71,8 @@ static int match_star(t_glob *gl, char *str, char *full_word) char *fix; char *pat; - if (gl->pat[1] == '*' && - !is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat + 1)) + if (!is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat + 1) + && gl->pat[1] == '*') { while (gl->pat[1] == '*') ++gl->pat;