mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
79 lines
2.4 KiB
C
79 lines
2.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* cmd_parsing.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */
|
|
/* Updated: 2025/04/25 13:17:56 by kcolin ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "cmd_parsing.h"
|
|
#include <fcntl.h>
|
|
#include "worddesc/worddesc.h"
|
|
#include "wordlist/wordlist.h"
|
|
#include <unistd.h>
|
|
#include "cmd/cmd_destroy.h"
|
|
#include "cmd/cmds_parse.h"
|
|
#include "../ft_errno.h"
|
|
|
|
static void parse_error(t_minishell *app, t_worddesc *token)
|
|
{
|
|
if (ft_errno_get() == FT_EHEREDOC_FAILED)
|
|
{
|
|
app->last_return_value = 130;
|
|
return ;
|
|
}
|
|
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)
|
|
{
|
|
t_worddesc *token;
|
|
|
|
token = worddesc_create(ft_strdup("newline"), 0, NULL, WORD_TOKEN);
|
|
parse_error(app, token);
|
|
worddesc_destroy(token);
|
|
}
|
|
|
|
static t_cmd *expecting_quote_error(t_minishell *app)
|
|
{
|
|
if (ft_errno_get() != FT_ESUCCESS)
|
|
{
|
|
app->last_return_value = 2;
|
|
ft_perror("minishell");
|
|
}
|
|
return (NULL);
|
|
}
|
|
|
|
/*
|
|
** TODO check if we need to differentiate the cause of a NULL return.
|
|
*/
|
|
t_cmd *minishell_parse(t_minishell *app, char *command_line)
|
|
{
|
|
t_cmd *root_cmd;
|
|
t_wordlist *tokens;
|
|
|
|
ft_errno(FT_ESUCCESS);
|
|
if (!command_line)
|
|
return (NULL);
|
|
tokens = minishell_wordsplit(command_line);
|
|
if (!tokens)
|
|
return (expecting_quote_error(app));
|
|
root_cmd = minishell_cmds_parse(app, &tokens);
|
|
if ((root_cmd == NULL && ft_errno_get() != FT_ESUCCESS) || tokens != NULL)
|
|
{
|
|
if (tokens == NULL)
|
|
parse_error_newline(app);
|
|
else
|
|
parse_error(app, tokens->word);
|
|
wordlist_destroy(tokens);
|
|
cmd_destroy(root_cmd);
|
|
return (NULL);
|
|
}
|
|
return (root_cmd);
|
|
}
|