parse-cmd: fixes and partial norm.

This commit is contained in:
Jérôme Guélen 2025-04-08 13:08:43 +02:00
parent 10a3c9c411
commit 72714855f7
No known key found for this signature in database

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 10:28:28 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); 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. ** Parse list of commands or pipeline.
*/ */
// TODO refactor // TODO remove debugs
t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder) 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 *subtree;
t_cmd *opt; t_cmd *opt;
t_cmd *list; t_cmd *list;
t_connector connec; t_connector connec;
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
subtree = minishell_pipeline_parse(app, builder); subtree = minishell_pipeline_parse(app, builder);
if (!subtree) if (!subtree)
return (NULL); //ft_errno? return (NULL); //ft_errno?
@ -131,18 +154,11 @@ ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
return (subtree); return (subtree);
while (opt) while (opt)
{ {
list = ft_calloc(1, sizeof(t_cmd)); if (cmds_parse_reorient_subtree(&list, &subtree, &opt, connec) == NULL)
if (!list)
{ {
app->last_return_value = 1; app->last_return_value = 1;
ft_errno(FT_ENOMEM); return (NULL);
return (ft_perror("minishell_cmds_parse"), NULL);
} }
list->type = FT_CONNECTION;
list->connector = connec;
list->first = subtree;
list->second = opt;
subtree = list;
opt = minishell_opt_cmds_parse(app, builder, &connec); opt = minishell_opt_cmds_parse(app, builder, &connec);
} }
if (ft_errno_get() != FT_ESUCCESS) 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. ** TODO Include destruction of builder contents if necessary.
*/ */
t_cmd *minishell_parse(t_minishell *app, char *command_line) t_cmd *minishell_parse(t_minishell *app, char *command_line)