From 0ae84abb14d8be98ed052f9c96b4084271bdf1d6 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Mon, 8 Nov 2021 04:02:43 +0100 Subject: [PATCH] bugfix expansions --- srcs/parsing/expansions/expand_token.c | 16 ++++++---- .../expansions/new_token_for_each_field.c | 9 ++++-- srcs/parsing/expansions/words_expansions.c | 31 ++++++++----------- srcs/parsing/parsing.c | 10 ++++-- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/srcs/parsing/expansions/expand_token.c b/srcs/parsing/expansions/expand_token.c index da78916..1b46df2 100644 --- a/srcs/parsing/expansions/expand_token.c +++ b/srcs/parsing/expansions/expand_token.c @@ -27,6 +27,7 @@ t_list *expand_token(t_token *t) { int in_quotes; int i; + int i_exp; t_list head; t_list *expand; @@ -39,7 +40,8 @@ t_list *expand_token(t_token *t) if (t->content[i] == '$') { expand->next = ret_parameter_expansion(t, &i); - if (!expand->next) + expand = expand->next; + if (!expand) {//todo wrap perror("expand_token() error"); return (ft_lstclear(&head.next, free)); @@ -48,12 +50,14 @@ t_list *expand_token(t_token *t) else { expand->next = ft_lstnew_generic(sizeof(t_list), ft_strlen(&t->content[i]) + 1); - if (!expand->next) + expand = expand->next; + i_exp = 0; + if (!expand) {//todo wrap perror("expand_token() error"); return (ft_lstclear(&head.next, free)); } - while (t->content[i] && (t->content[i] != '$' && in_quotes != IN_QUOTES)) + while (t->content[i] && (t->content[i] != '$' || in_quotes == IN_QUOTES)) { // quoting if (t->content[i] == '\'' && in_quotes != IN_DQUOTES) @@ -63,7 +67,7 @@ t_list *expand_token(t_token *t) else in_quotes = IN_QUOTES; } - i++; + ((char *)expand->content)[i_exp++] = t->content[i++]; } } } @@ -88,7 +92,7 @@ static t_list *ret_parameter_expansion(t_token *t, int *i) (*i)++; // skip '$' if (t->content[*i] == '?') { - i++; + (*i)++; expand->content = ft_itoa(g_all->last_exit_status); return (ft_retp_free(expand, tmp, free)); } @@ -100,7 +104,7 @@ static t_list *ret_parameter_expansion(t_token *t, int *i) } i_tmp = 0; while (t->content[*i] == '_' || ft_isalnum(t->content[*i])) - tmp[i_tmp++] = t->content[*i++]; + tmp[i_tmp++] = t->content[(*i)++]; expand->content = getenv(tmp); free(tmp); if (expand->content) diff --git a/srcs/parsing/expansions/new_token_for_each_field.c b/srcs/parsing/expansions/new_token_for_each_field.c index 10236bd..c3ee91c 100644 --- a/srcs/parsing/expansions/new_token_for_each_field.c +++ b/srcs/parsing/expansions/new_token_for_each_field.c @@ -28,18 +28,19 @@ int new_token_for_each_field(char **fields, t_token **t) while (fields[i]) { insert_lst->next = (t_token *)ft_lstnew_generic(sizeof(*insert_lst), 0); - if (!insert_lst->next) + insert_lst = insert_lst->next; + if (!insert_lst) {//todo wrap perror("insert_token_for_each_field() error"); ft_free_2d_arr(fields); return ((int)ft_lstclear((t_list **)&head.next, NULL)); } - insert_lst = insert_lst->next; insert_lst->content = fields[i]; insert_lst->id = T_WORD; + i++; } free(fields); - *t = insert_tokens(*t, insert_lst); + *t = insert_tokens(*t, head.next); return (1); } @@ -51,6 +52,8 @@ static t_token *insert_tokens(t_token *t, t_token *insert_lst) t->id = 0; free(t->content); t->content = NULL; + if (!insert_lst) + return (t); tmp = t->next; t->next = insert_lst; diff --git a/srcs/parsing/expansions/words_expansions.c b/srcs/parsing/expansions/words_expansions.c index b878ead..36f5288 100644 --- a/srcs/parsing/expansions/words_expansions.c +++ b/srcs/parsing/expansions/words_expansions.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */ -/* Updated: 2021/11/07 04:36:52 by lperrey ### ########.fr */ +/* Updated: 2021/11/08 03:59:02 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,23 +45,18 @@ int words_expansions(t_token *t) tmp_expand = expand_token(t); if (!tmp_expand) return (0); - if (((t_list*)tmp_expand)->next) - { - // 2 - tmp_expand = rejoin_after_expand(tmp_expand); - if (!tmp_expand) - return (0); - // 3 WIP PLACEHOLDER, MUST WRITE A ft_split_quoted() FOR NO SPLIT IN QUOTES - tmp_split = ft_split(tmp_expand, ' '); - free(tmp_expand); - if (!tmp_split) - return (0); - // 4 - if (!new_token_for_each_field(tmp_split, &t)) - return (0); - } - else - ft_lstclear((t_list **)&tmp_expand, free); + // 2 + tmp_expand = rejoin_after_expand(tmp_expand); + if (!tmp_expand) + return (0); + // 3 WIP PLACEHOLDER, MUST WRITE A ft_split_quoted() FOR NO SPLIT IN QUOTES + tmp_split = ft_split(tmp_expand, ' '); + free(tmp_expand); + if (!tmp_split) + return (0); + // 4 + if (!new_token_for_each_field(tmp_split, &t)) + return (0); } t = t->next; } diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index b96df90..d55ad04 100644 --- a/srcs/parsing/parsing.c +++ b/srcs/parsing/parsing.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */ -/* Updated: 2021/11/07 04:37:30 by lperrey ### ########.fr */ +/* Updated: 2021/11/08 01:03:29 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,13 +37,19 @@ t_cmd **parsing(t_token *token_list) return (NULL); // 2.9.1 - 2) Expansion + // TEST TOKENS PRINT + ft_putstr_fd("TOKENS LIST :\n-----------\n", 1); + ft_lstprint((t_list *)token_list, 1); + // words_expansions(token_list); + // + ft_putstr_fd("TOKENS LIST EXPANDED :\n-----------\n", 1); + ft_lstprint((t_list *)token_list, 1); // 2.9.1 - 3) Redirection // Struct CMD fill - // HUGO WIP // cmd_expansion(cmd_arr, envp); // handle_argv(token_list, cmd_arr, cmd_nbr);