diff --git a/src/ft_errno.c b/src/ft_errno.c index 98a988a..2a74b6b 100644 --- a/src/ft_errno.c +++ b/src/ft_errno.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/21 12:40:46 by khais #+# #+# */ -/* Updated: 2025/02/21 12:40:51 by khais ### ########.fr */ +/* Updated: 2025/02/21 15:38:54 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,6 +50,7 @@ char *ft_strerror(t_errno errno) [FT_ESUCCESS] = "Success", [FT_EINVAL] = "Invalid argument", [FT_EBADID] = "Bad identifier", + [FT_EUNEXPECTED_PIPE] = "minishell: syntax error near unexpected token `|'", }; if (errno >= 0 && errno < FT_EMAXERRNO) diff --git a/src/ft_errno.h b/src/ft_errno.h index 4d8c615..67941e9 100644 --- a/src/ft_errno.h +++ b/src/ft_errno.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/21 12:40:58 by khais #+# #+# */ -/* Updated: 2025/02/21 12:41:02 by khais ### ########.fr */ +/* Updated: 2025/02/21 15:37:56 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ typedef enum e_errno FT_ESUCCESS = 0, FT_EINVAL, FT_EBADID, + FT_EUNEXPECTED_PIPE, FT_EMAXERRNO, } t_errno; diff --git a/src/parser/pipeline/pipeline.c b/src/parser/pipeline/pipeline.c index d07787a..a421b07 100644 --- a/src/parser/pipeline/pipeline.c +++ b/src/parser/pipeline/pipeline.c @@ -6,13 +6,13 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/21 13:23:50 by khais #+# #+# */ -/* Updated: 2025/02/21 15:23:12 by khais ### ########.fr */ +/* Updated: 2025/02/21 15:42:34 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipeline.h" #include "libft.h" -#include "unistd.h" +#include "../../ft_errno.h" #include "../matchers/pipe.h" static int pipeline_count_cmds(t_wordlist *words) @@ -53,9 +53,11 @@ static t_pipeline *pipeline_parse_wordlist(t_pipeline *pipeline, { pipeline->cmds[idx] = simple_cmd_from_wordlist(current_wordlist); if (pipeline->cmds[idx] == NULL) - return (pipeline_destroy(pipeline), NULL); + return (pipeline_destroy(pipeline), wordlist_destroy(words), NULL); current_wordlist = NULL; current_word = ignore_word(current_word, &words); + if (is_pipe(current_word)) + return (pipeline_destroy(pipeline), wordlist_destroy(words), worddesc_destroy(current_word), ft_errno(FT_EUNEXPECTED_PIPE), NULL); idx++; } current_wordlist = wordlist_push(current_wordlist, current_word); diff --git a/tests/parse_pipelines.c b/tests/parse_pipelines.c index 594af28..53bf02d 100644 --- a/tests/parse_pipelines.c +++ b/tests/parse_pipelines.c @@ -6,12 +6,13 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/21 13:13:58 by khais #+# #+# */ -/* Updated: 2025/02/21 15:29:48 by khais ### ########.fr */ +/* Updated: 2025/02/21 16:03:39 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "../src/parser/pipeline/pipeline.h" #include "../src/parser/wordsplit/wordsplit.h" +#include "../src/ft_errno.h" #include #include "ft_printf.h" #include "libft.h" @@ -95,6 +96,20 @@ static void test_parse_pipeline_four_cmd(void) pipeline_destroy(pipeline); } +static void test_parse_pipeline_double_pipe_rejected(void) +{ + ft_errno(FT_ESUCCESS); + assert(parse_pipeline("echo hello | | tee output.txt") == NULL); + assert(ft_errno_get() == FT_EUNEXPECTED_PIPE); +} + +static void test_parse_pipeline_triple_pipe_rejected(void) +{ + ft_errno(FT_ESUCCESS); + assert(parse_pipeline("echo hello | | | tee output.txt") == NULL); + assert(ft_errno_get() == FT_EUNEXPECTED_PIPE); +} + int main(void) { test_parse_empty_pipeline(); @@ -102,5 +117,7 @@ int main(void) test_parse_pipeline_two_cmd(); test_parse_pipeline_three_cmd(); test_parse_pipeline_four_cmd(); + test_parse_pipeline_double_pipe_rejected(); + test_parse_pipeline_triple_pipe_rejected(); return (0); }