From d3d55386d95ead98fa880166ee6f6aa860db7228 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Sat, 11 Dec 2021 04:27:57 +0100 Subject: [PATCH] refactoring parsing (wip) --- headers/minishell_prototypes.h | 6 +-- .../expansions/new_token_for_each_field.c | 14 +++---- srcs/parsing/expansions/words_expansions.c | 37 +++++++++++++------ srcs/parsing/parsing.c | 27 ++++++-------- 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index 20ff829..1178276 100644 --- a/headers/minishell_prototypes.h +++ b/headers/minishell_prototypes.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */ -/* Updated: 2021/12/05 17:36:01 by lperrey ### ########.fr */ +/* Updated: 2021/12/10 18:46:48 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,8 +40,8 @@ int valid_command_separator(const t_token *token_list); size_t count_pipes(t_token *token_list); t_cmd **pipeline_alloc(size_t cmd_nbr); int pipeline_fill_argv(t_token *token_list, t_cmd **pipeline); -int words_expansions(t_token *token_list); -int token_expansions(t_token **t); +int expansions(t_token *token_list, t_cmd **pipeline); +int token_expansions(t_token *t); int redirections(t_token *token_list, t_cmd **pipeline); // Exec diff --git a/srcs/parsing/expansions/new_token_for_each_field.c b/srcs/parsing/expansions/new_token_for_each_field.c index 029106b..91d72f1 100644 --- a/srcs/parsing/expansions/new_token_for_each_field.c +++ b/srcs/parsing/expansions/new_token_for_each_field.c @@ -6,15 +6,15 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */ -/* Updated: 2021/11/30 18:51:13 by lperrey ### ########.fr */ +/* Updated: 2021/12/10 19:11:52 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static t_token *insert_tokens(t_token *t, t_token *insert_lst); +static void insert_tokens(t_token *t, t_token *insert_lst); -int new_token_for_each_field(char **fields, t_token **t) +int new_token_for_each_field(char **fields, t_token *t) { t_token head; t_token *insert_lst; @@ -38,11 +38,11 @@ int new_token_for_each_field(char **fields, t_token **t) i++; } free(fields); - *t = insert_tokens(*t, head.next); + insert_tokens(t, head.next); return (1); } -static t_token *insert_tokens(t_token *t, t_token *insert_lst) +static void insert_tokens(t_token *t, t_token *insert_lst) { t_token *tmp; t_token *insert_lst_last; @@ -50,12 +50,12 @@ static t_token *insert_tokens(t_token *t, t_token *insert_lst) t->id = 0; ft_free_null(&t->content); if (!insert_lst) - return (t); + return ; tmp = t->next; t->next = insert_lst; insert_lst_last = (t_token *)ft_lstlast((t_list *)insert_lst); insert_lst_last->next = tmp; - return (insert_lst_last); + //return (insert_lst_last); // return inutile pour reusinage } diff --git a/srcs/parsing/expansions/words_expansions.c b/srcs/parsing/expansions/words_expansions.c index 17f0a46..e217d8b 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/30 18:35:59 by lperrey ### ########.fr */ +/* Updated: 2021/12/10 19:51:33 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ t_list *expand_token(char *content); char *rejoin_after_expand(t_list *expand_lst); -int new_token_for_each_field(char **fields, t_token **t); +int new_token_for_each_field(char **fields, t_token *t); // 1 - chaque bout dans un element d'une t_list // (telle quelle si non expand, ou VARIABLE de env) @@ -25,28 +25,43 @@ int new_token_for_each_field(char **fields, t_token **t); // (ft_lstadd_front() sur le token original, puis set le token orignal à : // t->id = 0 ; free(t->content) ; t->content = NULL ; pour qu'il soit ignoré sur la suite du parsing) -int words_expansions(t_token *token_list) +int expansions(t_token *t, t_cmd **pipeline) { - while (token_list) + int i; + t_token *next_token; + + i = 0; + while (t) { - if (token_list->id == T_WORD) - token_expansions(&token_list); - token_list = token_list->next; + if (t->id == '|') + i++; + if (!pipeline[i]->error && t->id == T_WORD) + { + next_token = t->next; + if (!token_expansions(t)) + { + pipeline[i]->error = EXIT_EXPANSION; + } + while (t != next_token) + t = t->next; + } + else + t = t->next; } return (1); } -int token_expansions(t_token **t) +int token_expansions(t_token *t) { void *tmp; char **tmp_split; // 1 - tmp = expand_token((*t)->content); + tmp = (t_list*)expand_token(t->content); if (!tmp) return (0); // 2 - tmp = rejoin_after_expand(tmp); + tmp = (char*)rejoin_after_expand(tmp); if (!tmp) return (0); // 3 @@ -55,7 +70,7 @@ int token_expansions(t_token **t) if (!tmp_split) return (0); // 4 - tmp = ft_dup_2d_arr(tmp_split, (t_dup_f)ft_strdup_quotes); + tmp = (char**)ft_dup_2d_arr(tmp_split, (t_dup_f)ft_strdup_quotes); ft_free_2d_arr(tmp_split); tmp_split = tmp; if (!tmp_split) diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index bf2708f..7d1b27c 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/12/06 01:08:11 by lperrey ### ########.fr */ +/* Updated: 2021/12/10 19:58:17 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,25 +52,22 @@ t_cmd **parsing(t_token *token_list) // 2.9.1 - 1) Save Words save_redirections_words(token_list); - // 2.9.1 - 2) Expansion - // TEST TOKENS PRINT - //ft_putstr_fd("TOKENS LIST :\n-----------\n", 1); - //ft_lstprint((t_list *)token_list, 1); - // - // getenv() ne va pas fonctionner avec le changement d'environnement prévu jusqu'ici. - // TODO : Revoir le changement d'environnement (avec extern char **environ ?) - // OU Integrer un equivalent perso comme dans pipex - if (!words_expansions(token_list)) - return (NULL); - // - //ft_putstr_fd("TOKENS LIST EXPANDED :\n-----------\n", 1); - //ft_lstprint((t_list *)token_list, 1); - // Struct CMD alloc pipeline = pipeline_alloc(1 + count_pipes(token_list)); if (!pipeline) return (NULL); + // 2.9.1 - 2) Expansion + // TEST TOKENS PRINT +/* ft_putstr_fd("TOKENS LIST :\n-----------\n", STDERR_FILENO); + ft_lstprint((t_list *)token_list, STDERR_FILENO); */ + // + if (!expansions(token_list, pipeline)) + return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline)); + // +/* ft_putstr_fd("TOKENS LIST EXPANDED :\n-----------\n", STDERR_FILENO); + ft_lstprint((t_list *)token_list, STDERR_FILENO); */ + // 2.9.1 - 3) Redirection if (!redirections(token_list, pipeline)) return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));