diff --git a/src/parser/command_list/command_list.c b/src/parser/command_list/command_list.c index 399f4df..3829bf6 100644 --- a/src/parser/command_list/command_list.c +++ b/src/parser/command_list/command_list.c @@ -6,13 +6,16 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/24 17:49:46 by khais #+# #+# */ -/* Updated: 2025/02/26 13:56:45 by khais ### ########.fr */ +/* Updated: 2025/02/26 16:53:55 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "command_list.h" #include "command_list_builder.h" #include "operator.h" +#include "libft.h" +#include "../../ft_errno.h" +#include #include /* @@ -35,6 +38,22 @@ static bool cmdlist_builder_at_last_pipeline(t_cmdlist_builder *builder) return (builder->idx == builder->cmdlist->num_pipelines - 1); } +/* +** Frees all memory associated with this builder and set error to true. +*/ +static void cmdlist_builder_error(t_cmdlist_builder *builder) +{ + if (builder == NULL) + return ; + cmdlist_destroy(builder->cmdlist); + ft_dprintf(STDERR_FILENO, "current wordlist: %p\n", builder->current_wordlist); + ft_dprintf(STDERR_FILENO, "current wordlist->word: %p\n", builder->current_wordlist->word); + wordlist_destroy(builder->current_wordlist); + ft_dprintf(STDERR_FILENO, "current wordlist has been freed: %p\n", builder->current_wordlist); + worddesc_destroy(builder->current_word); + builder->error = true; +} + /* ** Delimit the current pipeline, creating a new t_pipeline object. ** @@ -46,8 +65,17 @@ static void cmdlist_builder_delimit( t_cmdlist_builder *builder, t_wordlist **words) { + ft_dprintf(STDERR_FILENO, "delimit: "); + wordlist_debug(builder->current_wordlist); builder->cmdlist->pipelines[builder->idx] = pipeline_from_wordlist(builder->current_wordlist); + if (builder->cmdlist->pipelines[builder->idx] == NULL) + { + ft_perror("invalid pipeline"); + cmdlist_builder_error(builder); + ft_dprintf(STDERR_FILENO, "destroyed builder\n"); + return ; + } if (cmdlist_builder_at_last_pipeline(builder)) builder->cmdlist->operators[builder->idx] = OP_END; else @@ -68,13 +96,16 @@ t_cmdlist *cmdlist_from_wordlist(t_wordlist *words) if (setup_cmdlist_builder(&builder, &words) == NULL) return (NULL); - while (builder.current_word != NULL) + while (builder.error == false && builder.current_word != NULL) { + ft_dprintf(STDERR_FILENO, "current word: %s\n", builder.current_word->word); if (match_op(builder.current_word->word) == OP_INVALID) cmdlist_builder_next_word(&builder, &words); else cmdlist_builder_delimit(&builder, &words); } + if (builder.error) + return (NULL); if (builder.current_wordlist != NULL) cmdlist_builder_delimit(&builder, &words); return (builder.cmdlist); diff --git a/src/parser/command_list/command_list_builder.c b/src/parser/command_list/command_list_builder.c index 748102b..716e277 100644 --- a/src/parser/command_list/command_list_builder.c +++ b/src/parser/command_list/command_list_builder.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/26 13:51:18 by khais #+# #+# */ -/* Updated: 2025/02/26 13:52:39 by khais ### ########.fr */ +/* Updated: 2025/02/26 17:02:05 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,7 +78,10 @@ t_cmdlist_builder *setup_cmdlist_builder( if (builder->cmdlist == NULL) return (NULL); builder->current_wordlist = NULL; + ft_dprintf(STDERR_FILENO, "words before pop in setup: %p\n", *words); builder->current_word = wordlist_pop(words); + ft_dprintf(STDERR_FILENO, "builder->current_word after pop in setup: %p\n", builder->current_word); + ft_dprintf(STDERR_FILENO, "words after pop in setup: %p\n", *words); builder->idx = 0; return (builder); } diff --git a/src/parser/command_list/command_list_builder.h b/src/parser/command_list/command_list_builder.h index f8824f1..a2d0882 100644 --- a/src/parser/command_list/command_list_builder.h +++ b/src/parser/command_list/command_list_builder.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/26 13:50:25 by khais #+# #+# */ -/* Updated: 2025/02/26 13:54:55 by khais ### ########.fr */ +/* Updated: 2025/02/26 14:05:36 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,11 @@ typedef struct s_cmdlist_builder ** index of the pipeline that is about to be inserted into the cmdlist */ int idx; + /* + ** set to true in an error was encountered that requires stopping the + ** processing. + */ + bool error; } t_cmdlist_builder; t_cmdlist_builder *setup_cmdlist_builder( diff --git a/tests/parse_command_lists.c b/tests/parse_command_lists.c index d18ad1b..346fc47 100644 --- a/tests/parse_command_lists.c +++ b/tests/parse_command_lists.c @@ -6,13 +6,14 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/24 17:40:48 by khais #+# #+# */ -/* Updated: 2025/02/25 15:25:14 by khais ### ########.fr */ +/* Updated: 2025/02/26 17:29:15 by khais ### ########.fr */ /* */ /* ************************************************************************** */ +#include "ft_printf.h" + #include "../src/parser/command_list/command_list.h" #include "../src/parser/wordsplit/wordsplit.h" -#include "ft_printf.h" #include "testutil.h" #include "unistd.h" #include @@ -159,8 +160,16 @@ static void test_parse_command_list_quad_both_operators(void) cmdlist_destroy(cmd); } +static void test_parse_command_list_invalid_pipeline(void) +{ + t_cmdlist *cmd = parse_command_list("echo this | | cat -e || echo does not work"); + assert(cmd == NULL); +} + int main(void) { + test_parse_command_list_invalid_pipeline(); + test_parse_command_list_empty(); test_parse_command_list_single_pipeline(); test_parse_command_list_simple_and();