From 4840051dbd814b930c7abe2d20bd73f672ce159c Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 13 Feb 2017 17:46:56 +0100 Subject: [PATCH] backquote expansion checks for splitting --- 42sh/Makefile | 2 +- 42sh/src/glob/lib_perso/ft_memrealloc.c | 4 +-- 42sh/src/lexer/expand_bquotes.c | 38 +++++++++++++++---------- 42sh/src/lexer/lexer_bquote.c | 5 +++- 42sh/src/lexer/lexer_delim.c | 1 - 42sh/src/main/main.c | 2 +- 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/42sh/Makefile b/42sh/Makefile index 5fa85765..860d0f28 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -246,7 +246,7 @@ $(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR) @$(eval COLOR=$(shell echo $$(($(PERCENT)%35+196)))) @$(eval TO_DO=$(shell echo $$((20-$(INDEX)*20/$(NB))))) @printf "\r\e[38;5;11m⌛ MAKE %10.10s : %2d%% \e[48;5;%dm%*s\e[0m%*s\e[48;5;255m \e[0m \e[38;5;11m %*s\e[0m\e[K" $(NAME) $(PERCENT) $(COLOR) $(DONE) "" $(TO_DO) "" $(DELTA) "$@" - @$(CC) $(FLAGS) -MMD -c $< -o $@\ + @$(CC) $(FLAGS) $(D_FLAGS) -MMD -c $< -o $@\ -I $(INC_DIR)\ -I $(LIBFT_INC) @$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) diff --git a/42sh/src/glob/lib_perso/ft_memrealloc.c b/42sh/src/glob/lib_perso/ft_memrealloc.c index 4f3c6125..2ea59d78 100644 --- a/42sh/src/glob/lib_perso/ft_memrealloc.c +++ b/42sh/src/glob/lib_perso/ft_memrealloc.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_memrealloc.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: wescande +#+ +:+ +#+ */ +/* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/05 13:44:36 by wescande #+# #+# */ -/* Updated: 2017/02/10 12:13:23 by gwojda ### ########.fr */ +/* Updated: 2017/02/09 17:01:01 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/expand_bquotes.c b/42sh/src/lexer/expand_bquotes.c index 9336761e..92d65856 100644 --- a/42sh/src/lexer/expand_bquotes.c +++ b/42sh/src/lexer/expand_bquotes.c @@ -19,13 +19,16 @@ int expand_bquotes(t_list **alst) t_list *cur_word; t_list *lst; t_token *token; - t_token token_buf; + t_token *token_buf; char *word; + char *ifs; char *output; + char *last_char; char *bq_start; char *bq_end; char *after_bq; char **str; + t_lexer lexer; t_flag tk; tk = TK_WORD; @@ -36,7 +39,6 @@ int expand_bquotes(t_list **alst) after_word = cur_word->next; token = cur_word->content; str = &token->data; - DG("found word=[%s]", *str); if (!(bq_start = ft_strchr(*str, '`'))) { cur_word = cur_word->next; @@ -49,31 +51,37 @@ int expand_bquotes(t_list **alst) } *bq_end = 0; after_bq = ft_strdup(bq_end + 1); - output = command_getoutput(bq_start + 1); - word = ft_strtok(output, " \n\t"); - DG("strtok=[%s]", word); - DG("first_tok was [%s]", *str); + word = command_getoutput(bq_start + 1); + output = word; + last_char = word + ft_strlen(word) - 1; + while (*last_char == '\n') + *last_char++ = 0; + ifs = ft_getenv(data_singleton()->env, "IFS"); + if (ifs) + word = ft_strtok(word, ifs); *bq_start = 0; ft_strappend(str, word); - DG("first_tok=[%s]", *str); - while ((word = ft_strtok(NULL, " \n\t"))) + while (ifs && (lexer.str = ft_strtok(NULL, ifs))) { - DG("strtok=[%s]", word); - token_buf.data = ft_strdup(word); - token_buf.type = TK_WORD; - new_word = ft_lstnew(&token_buf, sizeof(token_buf)); + lexer.pos = 0; + token_buf = token_init(); + token_buf->type = TK_WORD; + while (lexer.str[lexer.pos]) + { + token_append(token_buf, &lexer, 0, 0); + lexer.pos++; + } + new_word = ft_lstnew(token_buf, sizeof(*token_buf)); cur_word->next = new_word; new_word->next = after_word; cur_word = new_word; } - token = new_word->content; + token = cur_word->content; ft_strappend(&token->data, after_bq); ft_strdel(&after_bq); ft_strdel(&output); cur_word = after_word; } - token_print(*alst); - DG("check end"); return (0); } diff --git a/42sh/src/lexer/lexer_bquote.c b/42sh/src/lexer/lexer_bquote.c index 0f961942..e5e468dd 100644 --- a/42sh/src/lexer/lexer_bquote.c +++ b/42sh/src/lexer/lexer_bquote.c @@ -19,7 +19,10 @@ int lexer_bquote(t_list **alst, t_lexer *lexer) token = (*alst)->content; token->type = TK_WORD; - token_append(token, lexer, 0, 0); + if (lexer->state == DQUOTE_BQUOTE) + token_append(token, lexer, 1, 1); + else + token_append(token, lexer, 0, 0); if (lexer->str[lexer->pos] == '`') { lexer->pos++; diff --git a/42sh/src/lexer/lexer_delim.c b/42sh/src/lexer/lexer_delim.c index a30dd955..d4571deb 100644 --- a/42sh/src/lexer/lexer_delim.c +++ b/42sh/src/lexer/lexer_delim.c @@ -17,7 +17,6 @@ int lexer_delim(t_list **alst, t_lexer *lexer) t_token *token; token = (*alst)->content; - DG("DELIM"); while (ft_is_delim(lexer->str[lexer->pos])) lexer->pos++; lexer->state = DEFAULT; diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index eeaf7f45..7fa3ae46 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -67,9 +67,9 @@ int interactive_shell() return (1); DG("check main 0"); token_print(token); - DG("check main 1"); if (ft_parse(&ast, &token)) return (1); + DG("check main 1"); btree_print(STDBUG, ast, &ft_putast); if (ft_exec(&ast)) return (1);