From 96e46e9130bc3a312c8dbe52f2f2f03f0755251a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 16 Apr 2025 15:43:10 +0200 Subject: [PATCH] fix(parsing/cmdgroup): report error the same way as everywhere else, prevent double report before: ``` $ () minishell: syntax error near unexpected token `)' minishell: syntax error near unexpected token `)' ``` after: ``` $ () minishell: syntax error near unexpected token `)' ``` This probably fixes a few bugs as well, but I didn't look too hard. It just seemed nicer to have a consistent way to report errors. --- src/parser/cmd_parsing.c | 17 +++++++++-------- src/parser/cmd_parsing.h | 4 +--- src/parser/group_cmd/group_cmd_parse.c | 13 ++++--------- test.sh | 9 +++++++++ 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/parser/cmd_parsing.c b/src/parser/cmd_parsing.c index 923a3c8..1b7757a 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/16 13:12:35 by khais ### ########.fr */ +/* Updated: 2025/04/16 15:39:50 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,14 @@ #include #include "cmd/cmd_destroy.h" #include "cmd/cmds_parse.h" +#include "../ft_errno.h" + +static void parse_error(t_minishell *app, t_worddesc *token) +{ + ft_dprintf(STDERR_FILENO, "minishell: syntax error near unexpected " + "token `%s'\n", token->word); + app->last_return_value = 2; +} static void parse_error_newline(t_minishell *app) { @@ -27,13 +35,6 @@ static void parse_error_newline(t_minishell *app) worddesc_destroy(token); } -void parse_error(t_minishell *app, t_worddesc *token) -{ - ft_dprintf(STDERR_FILENO, "minishell: syntax error near unexpected " - "token `%s'\n", token->word); - app->last_return_value = 2; -} - /* ** TODO check if we need to differentiate the cause of a NULL return. */ diff --git a/src/parser/cmd_parsing.h b/src/parser/cmd_parsing.h index 8025fc0..d32ad46 100644 --- a/src/parser/cmd_parsing.h +++ b/src/parser/cmd_parsing.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:14:13 by khais #+# #+# */ -/* Updated: 2025/04/16 13:11:11 by khais ### ########.fr */ +/* Updated: 2025/04/16 15:39:16 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,10 +14,8 @@ # define CMD_PARSING_H # include "../minishell.h" -# include "../ft_errno.h" # include -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); diff --git a/src/parser/group_cmd/group_cmd_parse.c b/src/parser/group_cmd/group_cmd_parse.c index f2058da..63ec22b 100644 --- a/src/parser/group_cmd/group_cmd_parse.c +++ b/src/parser/group_cmd/group_cmd_parse.c @@ -6,12 +6,12 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:46:28 by khais #+# #+# */ -/* Updated: 2025/04/16 13:12:57 by khais ### ########.fr */ +/* Updated: 2025/04/16 15:43:44 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "group_cmd_parse.h" -#include "../cmd_parsing.h" +#include "../../ft_errno.h" #include "../cmd/cmd_destroy.h" #include "../cmd/cmd.h" #include "../redirect/redirect_parse.h" @@ -24,14 +24,9 @@ t_cmd *minishell_group_cmd_parse(t_minishell *app, t_wordlist **tokens) t_cmd *group; subtree = minishell_cmds_parse(app, tokens); - if (!subtree + if (!subtree || (*tokens) == NULL || (*tokens)->word->token_type != CLOSE_PARENTH_TOKEN) - { - ft_errno(FT_EERRNO); - if ((*tokens)->word->token_type != CLOSE_PARENTH_TOKEN) - parse_error(app, (*tokens)->word); - return (cmd_destroy(subtree), NULL); - } + return (cmd_destroy(subtree), ft_errno(FT_EERRNO), NULL); worddesc_destroy(wordlist_pop(tokens)); group = cmd_create(FT_GROUP); if (!group) diff --git a/test.sh b/test.sh index c1d7d44..5bc067c 100755 --- a/test.sh +++ b/test.sh @@ -635,4 +635,13 @@ minishell: syntax error near unexpected token `|' 2 EOF +when_run <