From 95938511494c1fac802770abb4e0f486d722f628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 16 Apr 2025 12:58:52 +0200 Subject: [PATCH] parsing: show errors for unexpected newline $ < minishell: syntax error near unexpected token `newline' $ > minishell: syntax error near unexpected token `newline' $ << minishell: syntax error near unexpected token `newline' $ >> minishell: syntax error near unexpected token `newline' --- src/parser/cmd_parsing.c | 22 ++++++++++++++-------- src/parser/redirect/redirect_parse.c | 9 +++++---- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/parser/cmd_parsing.c b/src/parser/cmd_parsing.c index 582224d..923a3c8 100644 --- a/src/parser/cmd_parsing.c +++ b/src/parser/cmd_parsing.c @@ -18,6 +18,15 @@ #include "cmd/cmd_destroy.h" #include "cmd/cmds_parse.h" +static void parse_error_newline(t_minishell *app) +{ + t_worddesc *token; + + token = worddesc_create(ft_strdup("newline"), 0, NULL, WORD_TOKEN); + parse_error(app, token); + worddesc_destroy(token); +} + void parse_error(t_minishell *app, t_worddesc *token) { ft_dprintf(STDERR_FILENO, "minishell: syntax error near unexpected " @@ -40,15 +49,12 @@ t_cmd *minishell_parse(t_minishell *app, char *command_line) if (!tokens) return (NULL); root_cmd = minishell_cmds_parse(app, &tokens); - if (!root_cmd) + if (root_cmd == NULL && ft_errno_get() != FT_ESUCCESS) { - if (ft_errno_get() != FT_ESUCCESS) - app->last_return_value = 1; - return (wordlist_destroy(tokens), NULL); - } - if (tokens) - { - parse_error(app, tokens->word); + if (tokens == NULL) + parse_error_newline(app); + else + parse_error(app, tokens->word); wordlist_destroy(tokens); cmd_destroy(root_cmd); return (NULL); diff --git a/src/parser/redirect/redirect_parse.c b/src/parser/redirect/redirect_parse.c index 6265fc3..31aaaf6 100644 --- a/src/parser/redirect/redirect_parse.c +++ b/src/parser/redirect/redirect_parse.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:13:58 by khais #+# #+# */ -/* Updated: 2025/04/15 14:14:07 by khais ### ########.fr */ +/* Updated: 2025/04/16 12:58:04 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,14 +26,15 @@ t_redirect *minishell_redirect_parse(t_minishell *app, t_wordlist **tokens) if ((*tokens) == NULL) return (NULL); redir_list = NULL; - while (is_redir((*tokens)->word)) + while ((*tokens) != NULL && is_redir((*tokens)->word)) { - if ((*tokens)->next == NULL) + redir_operator = wordlist_pop(tokens); + if ((*tokens) == NULL) { + worddesc_destroy(redir_operator); ft_errno(FT_EERRNO); return (redirect_destroy(redir_list), NULL); } - 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);