From 92d647e33c1bbe42beb108c68ddb68f00ec492a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 28 Apr 2025 14:20:13 +0200 Subject: [PATCH] feat(redirections): allow a command composed only of redirections --- grammar.md | 6 +-- src/executing/simple_cmd/builtins.c | 4 +- src/executing/simple_cmd/simple_cmd_execute.c | 6 ++- src/parser/group_cmd/group_cmd_parse.c | 4 +- src/parser/simple_cmd/simple_cmd_parse.c | 4 +- src/postprocess/expansion/expand_vars.c | 8 ++-- src/postprocess/fieldsplit/fieldsplit.c | 6 +-- test.sh | 37 +++++++++++++++++++ 8 files changed, 58 insertions(+), 17 deletions(-) diff --git a/grammar.md b/grammar.md index 122ae45..57f6602 100644 --- a/grammar.md +++ b/grammar.md @@ -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 diff --git a/src/executing/simple_cmd/builtins.c b/src/executing/simple_cmd/builtins.c index bcf8974..e0e7fbc 100644 --- a/src/executing/simple_cmd/builtins.c +++ b/src/executing/simple_cmd/builtins.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/src/executing/simple_cmd/simple_cmd_execute.c b/src/executing/simple_cmd/simple_cmd_execute.c index 876999f..92f6570 100644 --- a/src/executing/simple_cmd/simple_cmd_execute.c +++ b/src/executing/simple_cmd/simple_cmd_execute.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) diff --git a/src/parser/group_cmd/group_cmd_parse.c b/src/parser/group_cmd/group_cmd_parse.c index 4e0ebb2..b0171a3 100644 --- a/src/parser/group_cmd/group_cmd_parse.c +++ b/src/parser/group_cmd/group_cmd_parse.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/src/parser/simple_cmd/simple_cmd_parse.c b/src/parser/simple_cmd/simple_cmd_parse.c index 435e6b8..fc24d57 100644 --- a/src/parser/simple_cmd/simple_cmd_parse.c +++ b/src/parser/simple_cmd/simple_cmd_parse.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/src/postprocess/expansion/expand_vars.c b/src/postprocess/expansion/expand_vars.c index 43036cc..389b848 100644 --- a/src/postprocess/expansion/expand_vars.c +++ b/src/postprocess/expansion/expand_vars.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/src/postprocess/fieldsplit/fieldsplit.c b/src/postprocess/fieldsplit/fieldsplit.c index 68c4338..d76123e 100644 --- a/src/postprocess/fieldsplit/fieldsplit.c +++ b/src/postprocess/fieldsplit/fieldsplit.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/test.sh b/test.sh index 52b0e0b..d888344 100755 --- a/test.sh +++ b/test.sh @@ -1404,4 +1404,41 @@ hello hi EOF +when_run < outfile1 >> outfile2 +ls +EOF +expecting < infile +< infile +echo \$? +EOF +expecting <