From 36de7bf150b2b3f904ddce6d32ca19b9909af7f3 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Fri, 17 Dec 2021 03:17:58 +0100 Subject: [PATCH 1/2] added valid_command() rule (only "cmd_prefix") + skip exec if no cmd_name in a command --- srcs/exec/pipeline.c | 6 +++--- srcs/parsing/parsing.c | 3 ++- srcs/parsing/valid_syntax/valid_command.c | 17 ++++++++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/srcs/exec/pipeline.c b/srcs/exec/pipeline.c index e8d61d2..150a4ca 100644 --- a/srcs/exec/pipeline.c +++ b/srcs/exec/pipeline.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/12/11 05:24:07 by lperrey ### ########.fr */ +/* Updated: 2021/12/16 15:55:36 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,7 +73,7 @@ static int pipeline_find_access(t_cmd *pipeline[], char *path[]) i = 0; while (pipeline[i]) { - if (!pipeline[i]->error) + if (!pipeline[i]->error && pipeline[i]->argv[0]) { if (!cmd_find_access(pipeline[i], path)) return (0); @@ -91,7 +91,7 @@ static pid_t pipeline_exec(t_cmd *pipeline[], t_all *c) i = 0; while (pipeline[i]) { - if (!pipeline[i]->error) + if (!pipeline[i]->error && pipeline[i]->argv[0]) { ret = cmd_exec_in_subshell(pipeline[i], c); if (ret != EXIT_SUCCESS) diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index 3a9755e..eb9df80 100644 --- a/srcs/parsing/parsing.c +++ b/srcs/parsing/parsing.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */ -/* Updated: 2021/12/16 04:58:12 by lperrey ### ########.fr */ +/* Updated: 2021/12/16 16:23:09 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -93,6 +93,7 @@ command : cmd_prefix cmd_name cmd_suffix | cmd_prefix cmd_name | cmd_name cmd_suffix | cmd_name + | cmd_prefix ; cmd_name : WORD // Apply rule 7a ; diff --git a/srcs/parsing/valid_syntax/valid_command.c b/srcs/parsing/valid_syntax/valid_command.c index eafb625..cee3bc5 100644 --- a/srcs/parsing/valid_syntax/valid_command.c +++ b/srcs/parsing/valid_syntax/valid_command.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/24 18:52:05 by lperrey #+# #+# */ -/* Updated: 2021/10/24 19:21:28 by lperrey ### ########.fr */ +/* Updated: 2021/12/17 03:14:06 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ static int valid_command_rule1(t_token **token_list); static int valid_command_rule2(t_token **token_list); static int valid_command_rule3(t_token **token_list); static int valid_command_rule4(t_token **token_list); +static int valid_command_rule5(t_token **token_list); int valid_command(t_token **token_list) { @@ -35,6 +36,9 @@ int valid_command(t_token **token_list) *token_list = cmd_start; if (valid_command_rule4(token_list)) return (1); + *token_list = cmd_start; + if (valid_command_rule5(token_list)) + return (1); return (0); } @@ -94,3 +98,14 @@ static int valid_command_rule4(t_token **token_list) } return (0); } + +// cmd_prefix +static int valid_command_rule5(t_token **token_list) +{ + while (valid_io_redirect(token_list)) + { + if (valid_command_separator(*token_list)) + return (1); + } + return (0); +} From fa711891329f276a0bda4f514bbe2ce80c143442 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Fri, 17 Dec 2021 04:13:21 +0100 Subject: [PATCH 2/2] split valid_command.c in two files --- Makefile | 2 +- srcs/parsing/valid_syntax/rules_command.c | 83 +++++++++++++++++++++++ srcs/parsing/valid_syntax/valid_command.c | 82 ++-------------------- 3 files changed, 90 insertions(+), 77 deletions(-) create mode 100644 srcs/parsing/valid_syntax/rules_command.c diff --git a/Makefile b/Makefile index 0a3a5b0..6f6fdbb 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ SRCS = main.c \ signals.c error_wrappers.c last_exit_status.c \ lexing.c fill_token.c check_operators.c \ parsing.c create_pipeline.c \ - valid_syntax.c valid_pipeline.c valid_command.c valid_io_redirect.c \ + valid_syntax.c valid_pipeline.c valid_command.c rules_command.c valid_io_redirect.c \ expansions.c expand_token.c rejoin_after_expand.c new_token_for_each_field.c \ redirections.c here_doc.c \ exec_cmd_line.c pipeline.c \ diff --git a/srcs/parsing/valid_syntax/rules_command.c b/srcs/parsing/valid_syntax/rules_command.c new file mode 100644 index 0000000..b572e73 --- /dev/null +++ b/srcs/parsing/valid_syntax/rules_command.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rules_command.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/24 18:52:05 by lperrey #+# #+# */ +/* Updated: 2021/12/17 03:18:49 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int valid_io_redirect(t_token **token_list); + +// cmd_prefix cmd_name cmd_suffix +int valid_command_rule1(t_token **token_list) +{ + while (valid_io_redirect(token_list)) + { + if (valid_token(token_list, T_WORD)) + { + while (valid_token(token_list, T_WORD) + || valid_io_redirect(token_list)) + { + if (valid_command_separator(*token_list)) + return (1); + } + } + } + return (0); +} + +// cmd_prefix cmd_name +int valid_command_rule2(t_token **token_list) +{ + while (valid_io_redirect(token_list)) + { + if (valid_token(token_list, T_WORD)) + { + if (valid_command_separator(*token_list)) + return (1); + } + } + return (0); +} + +// cmd_name cmd_suffix +int valid_command_rule3(t_token **token_list) +{ + if (valid_token(token_list, T_WORD)) + { + while (valid_token(token_list, T_WORD) || valid_io_redirect(token_list)) + { + if (valid_command_separator(*token_list)) + return (1); + } + } + return (0); +} + +// cmd_name +int valid_command_rule4(t_token **token_list) +{ + if (valid_token(token_list, T_WORD)) + { + if (valid_command_separator(*token_list)) + return (1); + } + return (0); +} + +// cmd_prefix +int valid_command_rule5(t_token **token_list) +{ + while (valid_io_redirect(token_list)) + { + if (valid_command_separator(*token_list)) + return (1); + } + return (0); +} diff --git a/srcs/parsing/valid_syntax/valid_command.c b/srcs/parsing/valid_syntax/valid_command.c index cee3bc5..11e3a90 100644 --- a/srcs/parsing/valid_syntax/valid_command.c +++ b/srcs/parsing/valid_syntax/valid_command.c @@ -6,19 +6,17 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/24 18:52:05 by lperrey #+# #+# */ -/* Updated: 2021/12/17 03:14:06 by lperrey ### ########.fr */ +/* Updated: 2021/12/17 03:28:53 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int valid_io_redirect(t_token **token_list); - -static int valid_command_rule1(t_token **token_list); -static int valid_command_rule2(t_token **token_list); -static int valid_command_rule3(t_token **token_list); -static int valid_command_rule4(t_token **token_list); -static int valid_command_rule5(t_token **token_list); +int valid_command_rule1(t_token **token_list); +int valid_command_rule2(t_token **token_list); +int valid_command_rule3(t_token **token_list); +int valid_command_rule4(t_token **token_list); +int valid_command_rule5(t_token **token_list); int valid_command(t_token **token_list) { @@ -41,71 +39,3 @@ int valid_command(t_token **token_list) return (1); return (0); } - -// cmd_prefix cmd_name cmd_suffix -static int valid_command_rule1(t_token **token_list) -{ - while (valid_io_redirect(token_list)) - { - if (valid_token(token_list, T_WORD)) - { - while (valid_token(token_list, T_WORD) - || valid_io_redirect(token_list)) - { - if (valid_command_separator(*token_list)) - return (1); - } - } - } - return (0); -} - -// cmd_prefix cmd_name -static int valid_command_rule2(t_token **token_list) -{ - while (valid_io_redirect(token_list)) - { - if (valid_token(token_list, T_WORD)) - { - if (valid_command_separator(*token_list)) - return (1); - } - } - return (0); -} - -// cmd_name cmd_suffix -static int valid_command_rule3(t_token **token_list) -{ - if (valid_token(token_list, T_WORD)) - { - while (valid_token(token_list, T_WORD) || valid_io_redirect(token_list)) - { - if (valid_command_separator(*token_list)) - return (1); - } - } - return (0); -} - -// cmd_name -static int valid_command_rule4(t_token **token_list) -{ - if (valid_token(token_list, T_WORD)) - { - if (valid_command_separator(*token_list)) - return (1); - } - return (0); -} - -// cmd_prefix -static int valid_command_rule5(t_token **token_list) -{ - while (valid_io_redirect(token_list)) - { - if (valid_command_separator(*token_list)) - return (1); - } - return (0); -}