diff --git a/src/parser/cmd_parsing.c b/src/parser/cmd_parsing.c index d63fad4..3a5f5b9 100644 --- a/src/parser/cmd_parsing.c +++ b/src/parser/cmd_parsing.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */ -/* Updated: 2025/04/14 12:57:46 by khais ### ########.fr */ +/* Updated: 2025/04/14 14:52:24 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -115,21 +115,21 @@ static t_redirect *redir_from_words(t_worddesc *operator, return (redir); } -t_redirect *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder) +t_redirect *minishell_redir_parse(t_minishell *app, t_wordlist *tokens) { t_redirect *redir_list; t_worddesc *redir_operator; t_worddesc *redir_specifier; t_redirect *new_redir; - ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); + ft_printf("%s() %s\n", __FUNCTION__, tokens->word->word); redir_list = NULL; - while (is_redir(builder->tokens->word)) + while (is_redir(tokens->word)) { - if (builder->tokens->next == NULL) + if (tokens->next == NULL) return (redirect_destroy(redir_list), NULL); - redir_operator = wordlist_pop(&builder->tokens); - redir_specifier = wordlist_pop(&builder->tokens); + redir_operator = wordlist_pop(&tokens); + redir_specifier = wordlist_pop(&tokens); new_redir = redir_from_words(redir_operator, redir_specifier, app); t_redirect_add_back(&redir_list, new_redir); } @@ -139,12 +139,12 @@ t_redirect *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder) /* ** TODO NORM */ -t_cmd *minishell_simple_parse(t_minishell *app, t_cmd_builder *builder) +t_cmd *minishell_simple_parse(t_minishell *app, t_wordlist *tokens) { t_cmd *simple; t_redirect *redir; - ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); + ft_printf("%s() %s\n", __FUNCTION__, tokens->word->word); simple = ft_calloc(1, sizeof(t_cmd)); if (!simple) { @@ -158,35 +158,35 @@ t_cmd *minishell_simple_parse(t_minishell *app, t_cmd_builder *builder) ft_errno(FT_ENOMEM); return (free(simple), NULL); } - redir = minishell_redir_parse(app, builder); + redir = minishell_redir_parse(app, tokens); t_redirect_add_back(&simple->value.simple->redirections, redir); if (ft_errno_get() != FT_ESUCCESS) return (t_cmd_destroy(simple), NULL); - while (builder->tokens && builder->tokens->word->token_type == WORD_TOKEN) + while (tokens && tokens->word->token_type == WORD_TOKEN) { simple->value.simple->words = wordlist_push(simple->value.simple->words, - wordlist_pop(&builder->tokens)); + wordlist_pop(&tokens)); if (!simple->value.simple->words) return (ft_errno(FT_EERRNO), t_cmd_destroy(simple), NULL); - redir = minishell_redir_parse(app, builder); + redir = minishell_redir_parse(app, tokens); t_redirect_add_back(&simple->value.simple->redirections, redir); } if (!simple->value.simple->words) - return (parse_error(app, builder->tokens->word), NULL); + return (parse_error(app, tokens->word), NULL); return (simple); } -t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_cmd_builder *builder) +t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_wordlist *tokens) { t_cmd *opt; t_worddesc *token; - token = builder->tokens->word; + token = tokens->word; if (token->token_type == PIPE_TOKEN) { - token = wordlist_pop(&builder->tokens); + token = wordlist_pop(&tokens); worddesc_destroy(token); - opt = minishell_group_or_smp_parse(app, builder); + opt = minishell_group_or_smp_parse(app, tokens); if (!opt) ft_errno(FT_EERRNO); return (opt); @@ -197,58 +197,58 @@ t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_cmd_builder *builder) /* ** TODO NORM -> function is too long */ -t_cmd *minishell_group_or_smp_parse(t_minishell *app, t_cmd_builder *builder) +t_cmd *minishell_group_or_smp_parse(t_minishell *app, t_wordlist *tokens) { t_cmd *subtree; t_cmd *group; - ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); - if (builder->tokens->word->token_type == OPEN_PARENTH_TOKEN) + ft_printf("%s() %s\n", __FUNCTION__, tokens->word->word); + if (tokens->word->token_type == OPEN_PARENTH_TOKEN) { - worddesc_destroy(wordlist_pop(&builder->tokens)); - subtree = minishell_cmds_parse(app, builder); + worddesc_destroy(wordlist_pop(&tokens)); + subtree = minishell_cmds_parse(app, tokens); if (!subtree - || builder->tokens->word->token_type != CLOSE_PARENTH_TOKEN) + || tokens->word->token_type != CLOSE_PARENTH_TOKEN) { ft_errno(FT_EERRNO); - if (builder->tokens->word->token_type != CLOSE_PARENTH_TOKEN) - parse_error(app, builder->tokens->word); + if (tokens->word->token_type != CLOSE_PARENTH_TOKEN) + parse_error(app, tokens->word); return (t_cmd_destroy(subtree), NULL); } - worddesc_destroy(wordlist_pop(&builder->tokens)); + worddesc_destroy(wordlist_pop(&tokens)); group = ft_calloc(1, sizeof(t_cmd)); if (!group) return (ft_errno(FT_ENOMEM), t_cmd_destroy(subtree), NULL); group->value.group->cmd = subtree; - group->value.group->redirects = minishell_redir_parse(app, builder); + group->value.group->redirects = minishell_redir_parse(app, tokens); if (group->value.group->redirects == NULL && ft_errno_get() != FT_ESUCCESS) return (t_cmd_destroy(group), NULL); return (group); } - subtree = minishell_simple_parse(app, builder); + subtree = minishell_simple_parse(app, tokens); if (!subtree) ft_errno(FT_EERRNO); return (subtree); } -t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_cmd_builder *builder, +t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_wordlist *tokens, t_connector *connec) { t_worddesc *token; t_cmd *opt; - ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); - token = builder->tokens->word; + ft_printf("%s() %s\n", __FUNCTION__, tokens->word->word); + token = tokens->word; if (token->token_type == OR_TOKEN || token->token_type == AND_TOKEN) { if (token->token_type == OR_TOKEN) *connec = FT_OR; else *connec = FT_AND; - token = wordlist_pop(&builder->tokens); + token = wordlist_pop(&tokens); worddesc_destroy(token); - opt = minishell_pipeline_parse(app, builder); + opt = minishell_pipeline_parse(app, tokens); if (!opt) ft_errno(FT_EERRNO); return (opt); @@ -283,17 +283,17 @@ static t_cmd *connec_reorient_subtree(t_cmd **list, t_cmd **subtree, return (*subtree); } -t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder) +t_cmd *minishell_pipeline_parse(t_minishell *app, t_wordlist *tokens) { t_cmd *subtree; t_cmd *opt; t_cmd *pipeline; - ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); - subtree = minishell_group_or_smp_parse(app, builder); + ft_printf("%s() %s\n", __FUNCTION__, tokens->word->word); + subtree = minishell_group_or_smp_parse(app, tokens); if (!subtree) return (NULL); - opt = minishell_opt_pipeline_parse(app, builder); + opt = minishell_opt_pipeline_parse(app, tokens); if (!opt && ft_errno_get() != FT_ESUCCESS) return (NULL); if (!opt) @@ -305,7 +305,7 @@ t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder) app->last_return_value = 1; return (ft_perror("minishell_pipeline_parse"), NULL); } - opt = minishell_opt_pipeline_parse(app, builder); + opt = minishell_opt_pipeline_parse(app, tokens); } if (ft_errno_get() != FT_ESUCCESS) return (t_cmd_destroy(subtree), NULL); @@ -316,18 +316,18 @@ t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder) ** Parse list of commands or pipeline. */ // TODO remove debugs -t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder) +t_cmd *minishell_cmds_parse(t_minishell *app, t_wordlist *tokens) { 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); + ft_printf("%s() %s\n", __FUNCTION__, tokens->word->word); + subtree = minishell_pipeline_parse(app, tokens); if (!subtree) return (NULL); - opt = minishell_opt_cmds_parse(app, builder, &connec); + opt = minishell_opt_cmds_parse(app, tokens, &connec); if (!opt && ft_errno_get() != FT_ESUCCESS) return (t_cmd_destroy(subtree), NULL); if (!opt) @@ -339,7 +339,7 @@ t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder) app->last_return_value = 1; return (ft_perror("minishell_cmds_parse"), NULL); } - opt = minishell_opt_cmds_parse(app, builder, &connec); + opt = minishell_opt_cmds_parse(app, tokens, &connec); } if (ft_errno_get() != FT_ESUCCESS) return (t_cmd_destroy(subtree), NULL); @@ -351,27 +351,27 @@ t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder) */ t_cmd *minishell_parse(t_minishell *app, char *command_line) { - t_cmd_builder builder; - t_cmd *root_cmd; + t_cmd *root_cmd; + t_wordlist *tokens; ft_errno(FT_ESUCCESS); app->last_return_value = 0; if (!command_line) return (NULL); - builder.tokens = minishell_wordsplit(command_line); - if (!builder.tokens) + tokens = minishell_wordsplit(command_line); + if (!tokens) return (NULL); - root_cmd = minishell_cmds_parse(app, &builder); + root_cmd = minishell_cmds_parse(app, tokens); if (!root_cmd) { if (ft_errno_get() != FT_ESUCCESS && !app->last_return_value) app->last_return_value = 1; - return (wordlist_destroy(builder.tokens), NULL); + return (wordlist_destroy(tokens), NULL); } - if (builder.tokens) + if (tokens) { - parse_error(app, builder.tokens->word); - wordlist_destroy(builder.tokens); + parse_error(app, tokens->word); + wordlist_destroy(tokens); t_cmd_destroy(root_cmd); return (NULL); } diff --git a/src/parser/cmd_parsing.h b/src/parser/cmd_parsing.h index 7ad2540..3238bee 100644 --- a/src/parser/cmd_parsing.h +++ b/src/parser/cmd_parsing.h @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/14 12:53/43 by khais #+# #+# */ -/* Updated: 2025/04/14 12:53:43 by khais ### ########.fr */ +/* Created: 2025/04/14 14:46/44 by khais #+# #+# */ +/* Updated: 2025/04/14 14:46:44 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,31 +17,19 @@ # include "../ft_errno.h" # include -typedef struct s_cmd_builder -{ - t_wordlist *tokens; - t_wordlist *current_wordlist; - t_redirect *current_redirection_list; - t_worddesc *current_word; - /* - ** last_here_doc is meant to store only one entry. - */ - t_redirectee *last_here_doc; -} t_cmd_builder; - void parse_error(t_minishell *app, t_worddesc *token); t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back); t_cmd *minishell_parse(t_minishell *app, char *command_line); -t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder); -t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder); -t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_cmd_builder *builder, +t_cmd *minishell_cmds_parse(t_minishell *app, t_wordlist *tokens); +t_cmd *minishell_pipeline_parse(t_minishell *app, t_wordlist *tokens); +t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_wordlist *tokens, t_connector *connec); t_cmd *minishell_group_or_smp_parse(t_minishell *app, - t_cmd_builder *builder); + t_wordlist *tokens); t_cmd *minishell_opt_pipeline_parse(t_minishell *app, - t_cmd_builder *builder); -t_cmd *minishell_simple_parse(t_minishell *app, t_cmd_builder *builder); -t_redirect *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder); + t_wordlist *tokens); +t_cmd *minishell_simple_parse(t_minishell *app, t_wordlist *tokens); +t_redirect *minishell_redir_parse(t_minishell *app, t_wordlist *tokens); #endif