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/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,14 +49,11 @@ 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 (ft_errno_get() != FT_ESUCCESS)
app->last_return_value = 1;
return (wordlist_destroy(tokens), NULL);
}
if (tokens)
if (root_cmd == NULL && ft_errno_get() != FT_ESUCCESS)
{
if (tokens == NULL)
parse_error_newline(app);
else
parse_error(app, tokens->word);
wordlist_destroy(tokens);
cmd_destroy(root_cmd);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);