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'
This commit is contained in:
Khaïs COLIN 2025-04-16 12:58:52 +02:00
parent e1a1a43041
commit 9593851149
2 changed files with 19 additions and 12 deletions

View file

@ -18,6 +18,15 @@
#include "cmd/cmd_destroy.h" #include "cmd/cmd_destroy.h"
#include "cmd/cmds_parse.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) void parse_error(t_minishell *app, t_worddesc *token)
{ {
ft_dprintf(STDERR_FILENO, "minishell: syntax error near unexpected " 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) if (!tokens)
return (NULL); return (NULL);
root_cmd = minishell_cmds_parse(app, &tokens); 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) if (tokens == NULL)
app->last_return_value = 1; parse_error_newline(app);
return (wordlist_destroy(tokens), NULL); else
} parse_error(app, tokens->word);
if (tokens)
{
parse_error(app, tokens->word);
wordlist_destroy(tokens); wordlist_destroy(tokens);
cmd_destroy(root_cmd); cmd_destroy(root_cmd);
return (NULL); return (NULL);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 10:13:58 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) if ((*tokens) == NULL)
return (NULL); return (NULL);
redir_list = 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); ft_errno(FT_EERRNO);
return (redirect_destroy(redir_list), NULL); return (redirect_destroy(redir_list), NULL);
} }
redir_operator = wordlist_pop(tokens);
redir_specifier = wordlist_pop(tokens); redir_specifier = wordlist_pop(tokens);
new_redir = redir_from_words(redir_operator, redir_specifier, app); new_redir = redir_from_words(redir_operator, redir_specifier, app);
t_redirect_add_back(&redir_list, new_redir); t_redirect_add_back(&redir_list, new_redir);