parsing: refactor minishell_simple_cmd_parse into own file

This commit is contained in:
Khaïs COLIN 2025-04-15 10:36:55 +02:00
parent e033909819
commit 811ce3ef8e
5 changed files with 72 additions and 34 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */
/* Updated: 2025/04/15 10:32:24 by khais ### ########.fr */
/* Updated: 2025/04/15 10:40:22 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,7 @@
#include "worddesc/worddesc.h"
#include "wordlist/wordlist.h"
#include <unistd.h>
#include "redirect/redirect.h"
#include "simple_cmd/simple_cmd_parse.h"
#include "cmd/cmd_destroy.h"
#include "redirect/redirect_parse.h"
@ -27,35 +27,6 @@ void parse_error(t_minishell *app, t_worddesc *token)
app->last_return_value = 2;
}
t_cmd *minishell_simple_parse(t_minishell *app, t_wordlist *tokens)
{
t_cmd *simple;
t_redirect *redir;
simple = cmd_create(FT_SIMPLE);
if (simple == NULL)
return (NULL);
simple->value.simple = ft_calloc(1, sizeof(t_simple_cmd));
if (simple->value.simple == NULL)
return (ft_errno(FT_ENOMEM), free(simple), NULL);
redir = minishell_redirect_parse(app, tokens);
t_redirect_add_back(&simple->value.simple->redirections, redir);
if (ft_errno_get() != FT_ESUCCESS)
return (cmd_destroy(simple), NULL);
while (tokens && tokens->word->token_type == WORD_TOKEN)
{
simple->value.simple->words = wordlist_push(simple->value.simple->words,
wordlist_pop(&tokens));
if (!simple->value.simple->words)
return (ft_errno(FT_EERRNO), cmd_destroy(simple), NULL);
redir = minishell_redirect_parse(app, tokens);
t_redirect_add_back(&simple->value.simple->redirections, redir);
}
if (!simple->value.simple->words)
return (cmd_destroy(simple), parse_error(app, tokens->word), NULL);
return (simple);
}
t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_wordlist *tokens)
{
t_cmd *opt;
@ -108,7 +79,7 @@ t_cmd *minishell_group_or_simple_parse(t_minishell *app, t_wordlist *tokens)
return (minishell_group_parse(app, tokens));
}
else
return (minishell_simple_parse(app, tokens));
return (minishell_simple_cmd_parse(app, tokens));
}
t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_wordlist *tokens,

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 10:14:13 by khais #+# #+# */
/* Updated: 2025/04/15 10:34:31 by khais ### ########.fr */
/* Updated: 2025/04/15 10:36:45 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,6 +29,5 @@ t_cmd *minishell_group_or_simple_parse(t_minishell *app,
t_wordlist *tokens);
t_cmd *minishell_opt_pipeline_parse(t_minishell *app,
t_wordlist *tokens);
t_cmd *minishell_simple_parse(t_minishell *app, t_wordlist *tokens);
#endif

View file

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* simple_cmd_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 10:38:47 by khais #+# #+# */
/* Updated: 2025/04/15 10:40:08 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "simple_cmd_parse.h"
#include "../../ft_errno.h"
#include "../cmd/cmd.h"
#include "../redirect/redirect_parse.h"
#include "../cmd/cmd_destroy.h"
#include "../cmd_parsing.h"
t_cmd *minishell_simple_cmd_parse(t_minishell *app, t_wordlist *tokens)
{
t_cmd *simple;
t_redirect *redir;
simple = cmd_create(FT_SIMPLE);
if (simple == NULL)
return (NULL);
simple->value.simple = ft_calloc(1, sizeof(t_simple_cmd));
if (simple->value.simple == NULL)
return (ft_errno(FT_ENOMEM), free(simple), NULL);
redir = minishell_redirect_parse(app, tokens);
t_redirect_add_back(&simple->value.simple->redirections, redir);
if (ft_errno_get() != FT_ESUCCESS)
return (cmd_destroy(simple), NULL);
while (tokens && tokens->word->token_type == WORD_TOKEN)
{
simple->value.simple->words = wordlist_push(simple->value.simple->words,
wordlist_pop(&tokens));
if (!simple->value.simple->words)
return (ft_errno(FT_EERRNO), cmd_destroy(simple), NULL);
redir = minishell_redirect_parse(app, tokens);
t_redirect_add_back(&simple->value.simple->redirections, redir);
}
if (!simple->value.simple->words)
return (cmd_destroy(simple), parse_error(app, tokens->word), NULL);
return (simple);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* simple_cmd_parse.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 10:38:23 by khais #+# #+# */
/* Updated: 2025/04/15 10:38:41 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SIMPLE_CMD_PARSE_H
# define SIMPLE_CMD_PARSE_H
# include "../../minishell.h"
t_cmd *minishell_simple_cmd_parse(t_minishell *app, t_wordlist *tokens);
#endif // SIMPLE_CMD_PARSE_H