feat(redirections): allow a command composed only of redirections

This commit is contained in:
Khaïs COLIN 2025-04-28 14:20:13 +02:00
parent a9055b4c66
commit 92d647e33c
8 changed files with 58 additions and 17 deletions

View file

@ -41,9 +41,9 @@ OPT_PIPELINE -> | GROUP_OR_SIMPLE OPT_PIPELINE
OPT_PIPELINE -> ε
GROUP_OR_SIMPLE -> (CMDS) REDIR
GROUP_OR_SIMPLE -> SIMPLE
SIMPLE -> REDIR word REDIR SIMPLE_LST
SIMPLE_LST -> word REDIR SIMPLE_LST
SIMPLE_LST -> ε
SIMPLE -> REDIR SIMPLE_TAIL
SIMPLE_TAIL -> word REDIR SIMPLE_TAIL
SIMPLE_TAIL -> ε
REDIR -> > word REDIR
REDIR -> >> word REDIR
REDIR -> < word REDIR

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 16:37:21 by khais #+# #+# */
/* Updated: 2025/04/28 12:46:14 by khais ### ########.fr */
/* Updated: 2025/04/28 14:40:19 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,8 @@ t_builtin_type get_builtin(t_simple_cmd *cmd)
{
char *word;
if (cmd->words == NULL)
return (BUILTIN_INVALID);
word = cmd->words->word->word;
if (ft_strcmp("pwd", word) == 0)
return (BUILTIN_PWD);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
/* Updated: 2025/04/28 12:48:38 by khais ### ########.fr */
/* Updated: 2025/04/28 14:42:13 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -78,6 +78,8 @@ static void exec_external_cmd(t_simple_cmd *cmd, t_minishell *app,
char *exe;
int pid;
if (cmd->words == NULL)
return (restore_std_fds(fds));
exe = get_cmdpath(cmd->words->word->word, app);
if (exe == NULL)
return (command_not_found(cmd, app));
@ -94,7 +96,7 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app,
{
t_std_fds fds;
if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL)
if (cmd == NULL)
return ;
ft_errno(FT_ESUCCESS);
if (post_process_command(cmd, app) == NULL)

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 10:46:28 by khais #+# #+# */
/* Updated: 2025/04/25 17:38:11 by khais ### ########.fr */
/* Updated: 2025/04/28 14:26:10 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -46,7 +46,7 @@ t_cmd *minishell_group_or_simple_parse(t_minishell *app, t_wordlist **tokens)
{
t_cmd *cmd;
if ((*tokens)->word->token_type == OPEN_PARENTH_TOKEN)
if ((*tokens) && (*tokens)->word->token_type == OPEN_PARENTH_TOKEN)
{
worddesc_destroy(wordlist_pop(tokens));
cmd = minishell_group_cmd_parse(app, tokens);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 10:38:47 by khais #+# #+# */
/* Updated: 2025/04/17 11:46:19 by khais ### ########.fr */
/* Updated: 2025/04/28 14:30:35 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -53,7 +53,7 @@ t_cmd *minishell_simple_cmd_parse(t_minishell *app, t_wordlist **tokens)
return (cmd_destroy(simple), NULL);
t_redirect_add_back(&simple->value.simple->redirections, redir);
}
if (!simple->value.simple->words)
if (!simple->value.simple->words && !simple->value.simple->redirections)
return (cmd_destroy(simple), ft_errno(FT_EERRNO), NULL);
return (simple);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 13:34:51 by khais #+# #+# */
/* Updated: 2025/04/21 11:09:04 by khais ### ########.fr */
/* Updated: 2025/04/28 14:38:29 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -50,12 +50,12 @@ t_simple_cmd *simple_cmd_expand_vars(t_simple_cmd *cmd, t_minishell *app)
{
if (cmd == NULL)
return (NULL);
cmd->words = wordlist_var_expansion(cmd->words, app);
if (cmd->words == NULL)
return (NULL);
ft_errno(FT_ESUCCESS);
cmd->redirections = redirection_var_expansion(cmd->redirections, app);
if (cmd->redirections == NULL && ft_errno_get() != FT_ESUCCESS)
return (NULL);
cmd->words = wordlist_var_expansion(cmd->words, app);
if (cmd->words == NULL && ft_errno_get() != FT_ESUCCESS)
return (NULL);
return (cmd);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 15:48:52 by khais #+# #+# */
/* Updated: 2025/04/25 14:27:56 by khais ### ########.fr */
/* Updated: 2025/04/28 14:37:59 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -100,9 +100,9 @@ t_simple_cmd *simple_cmd_fieldsplit(t_simple_cmd *cmd)
{
if (cmd == NULL)
return (NULL);
if (wordlist_fieldsplit(&cmd->words) == NULL)
return (NULL);
if (redirect_fieldsplit(cmd) == NULL)
return (NULL);
if (cmd->words != NULL && wordlist_fieldsplit(&cmd->words) == NULL)
return (NULL);
return (cmd);
}

37
test.sh
View file

@ -1404,4 +1404,41 @@ hello
hi
EOF
when_run <<EOF "output redirections with no associated cmd"
> outfile1 >> outfile2
ls
EOF
expecting <<EOF
outfile1
outfile2
EOF
when_run <<EOF "here-doc with no associated cmd"
<< eof
hello there
eof
echo \$?
EOF
expecting <<EOF
0
EOF
when_run <<EOF "input redirection with no associated cmd"
echo hello > infile
< infile
echo \$?
EOF
expecting <<EOF
0
EOF
when_run <<EOF "input redirection with no associated cmd where infile does not exist"
< infile
echo \$?
EOF
expecting <<EOF
minishell: infile: No such file or directory
1
EOF
finalize