diff --git a/src/parser/command_list/command_list.c b/src/parser/command_list/command_list.c index a24c7e0..1f94cb8 100644 --- a/src/parser/command_list/command_list.c +++ b/src/parser/command_list/command_list.c @@ -6,12 +6,13 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/24 17:49:46 by khais #+# #+# */ -/* Updated: 2025/02/25 13:33:53 by khais ### ########.fr */ +/* Updated: 2025/02/25 14:57:20 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "command_list.h" #include "libft.h" +#include "unistd.h" #include /* @@ -84,23 +85,33 @@ t_command_list *command_list_from_wordlist(t_wordlist *words) t_command_list *output; t_wordlist *current_wordlist; t_worddesc *current_word; + int idx; output = allocate_command_list(words); if (output == NULL) return (NULL); current_wordlist = NULL; current_word = wordlist_pop(&words); - while (current_word != NULL && match_op(current_word->word) == OP_INVALID) + idx = 0; + while (current_word != NULL) { - current_wordlist = wordlist_push(current_wordlist, current_word); - current_word = wordlist_pop(&words); + if (match_op(current_word->word) == OP_INVALID) + { + current_wordlist = wordlist_push(current_wordlist, current_word); + current_word = wordlist_pop(&words); + } + else + { + output->pipelines[idx] = pipeline_from_wordlist(current_wordlist); + output->operator = match_op(current_word->word); + current_wordlist = NULL; + worddesc_destroy(current_word); + current_word = wordlist_pop(&words); + idx++; + } } - if (current_word != NULL) - output->operator = match_op(current_word->word); - worddesc_destroy(current_word); - output->pipelines[0] = pipeline_from_wordlist(current_wordlist); - if (words != NULL) - output->pipelines[1] = pipeline_from_wordlist(words); + if (current_wordlist != NULL) + output->pipelines[idx] = pipeline_from_wordlist(current_wordlist); return (output); } diff --git a/tests/parse_command_lists.c b/tests/parse_command_lists.c index 64eaa51..b8d6ef6 100644 --- a/tests/parse_command_lists.c +++ b/tests/parse_command_lists.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/24 17:40:48 by khais #+# #+# */ -/* Updated: 2025/02/25 13:32:27 by khais ### ########.fr */ +/* Updated: 2025/02/25 15:02:03 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,6 +60,13 @@ static void assert_pipelineequal(char *expected, t_command_list *cmd, int idx) ft_dprintf(STDERR_FILENO, "Expected pipeline %p to equal [%s]\n", got, expected); t_pipeline *expected_pipeline = parse_pipeline(expected); assert(expected_pipeline == got || expected_pipeline != NULL); + int j = 0; + while (j < got->num_cmd) + { + ft_dprintf(STDERR_FILENO, "Got pipeline cmd %d: ", j); + wordlist_debug(got->cmds[j]->words); + j++; + } ft_dprintf(STDERR_FILENO, "Expected pipeline to have %d commands, got pipeline with %d\n", expected_pipeline->num_cmd, got->num_cmd); assert(expected_pipeline->num_cmd == got->num_cmd); int i = 0; @@ -110,11 +117,24 @@ static void test_parse_command_list_simple_or(void) command_list_destroy(cmd); } +static void test_parse_command_list_triple_or(void) +{ + t_command_list *cmd = parse_command_list("echo this | cat -e || echo works | wc -c || echo as well | cut -d' ' -f1"); + assert(cmd != NULL); + assert_pipelineequal("echo this | cat -e", cmd, 0); + assert_pipelineequal("echo works | wc -c", cmd, 1); + assert_pipelineequal("echo as well | cut -d' ' -f1", cmd, 2); + assert(cmd->operator == OP_OR); + assert(cmd->num_pipelines == 3); + command_list_destroy(cmd); +} + int main(void) { test_parse_command_list_empty(); test_parse_command_list_single_pipeline(); test_parse_command_list_simple_and(); test_parse_command_list_simple_or(); + test_parse_command_list_triple_or(); return (0); }