From 72714855f7f9f6d84db5db8af0ce872bd3194359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gu=C3=A9len?= Date: Tue, 8 Apr 2025 13:08:43 +0200 Subject: [PATCH] parse-cmd: fixes and partial norm. --- src/parser/cmd_parsing.c | 44 ++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/parser/cmd_parsing.c b/src/parser/cmd_parsing.c index 75a3547..46e0878 100644 --- a/src/parser/cmd_parsing.c +++ b/src/parser/cmd_parsing.c @@ -6,7 +6,7 @@ /* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */ -/* Updated: 2025/04/08 10:21:41 by jguelen ### ########.fr */ +/* Updated: 2025/04/08 13:01:32 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -108,19 +108,42 @@ ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); return (subtree); } +static t_cmd *cmds_parse_reorient_subtree(t_cmd **list, t_cmd **subtree, + t_cmd **opt, t_connector connec) +{ + *list = ft_calloc(1, sizeof(t_cmd)); + if (!*list) + { + ft_errno(FT_ENOMEM); + return (ft_perror("minishell_cmds_parse"), NULL); + } + (*list)->value.connection = ft_calloc(1, sizeof(t_connec_cmd)); + if (!(*list)->value.connection) + { + ft_errno(FT_ENOMEM); + return (ft_perror("minishell_cmds_parse"), NULL); + } + (*list)->type = FT_CONNECTION; + (*list)->value.connection->connector = connec; + (*list)->value.connection->first = *subtree; + (*list)->value.connection->second = *opt; + *subtree = *list; + return (*subtree); +} + /* ** Parse list of commands or pipeline. */ -// TODO refactor +// TODO remove debugs t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder) { -ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); - t_cmd *subtree; t_cmd *opt; t_cmd *list; t_connector connec; +ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); + subtree = minishell_pipeline_parse(app, builder); if (!subtree) return (NULL); //ft_errno? @@ -131,18 +154,11 @@ ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); return (subtree); while (opt) { - list = ft_calloc(1, sizeof(t_cmd)); - if (!list) + if (cmds_parse_reorient_subtree(&list, &subtree, &opt, connec) == NULL) { app->last_return_value = 1; - ft_errno(FT_ENOMEM); - return (ft_perror("minishell_cmds_parse"), NULL); + return (NULL); } - list->type = FT_CONNECTION; - list->connector = connec; - list->first = subtree; - list->second = opt; - subtree = list; opt = minishell_opt_cmds_parse(app, builder, &connec); } if (ft_errno_get() != FT_ESUCCESS) @@ -151,8 +167,6 @@ ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); } /* -** TODO Recheck error cases to see if allocation error occurred or something -** else and if this is necessary. ** TODO Include destruction of builder contents if necessary. */ t_cmd *minishell_parse(t_minishell *app, char *command_line)