command list parse: handle triple pipeline separated with ||

This commit is contained in:
Khaïs COLIN 2025-02-25 13:46:11 +01:00 committed by Khaïs COLIN
parent e6dd1d6a56
commit 1c00020c86
2 changed files with 42 additions and 11 deletions

View file

@ -6,12 +6,13 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 17:49:46 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 "command_list.h"
#include "libft.h" #include "libft.h"
#include "unistd.h"
#include <stdlib.h> #include <stdlib.h>
/* /*
@ -84,23 +85,33 @@ t_command_list *command_list_from_wordlist(t_wordlist *words)
t_command_list *output; t_command_list *output;
t_wordlist *current_wordlist; t_wordlist *current_wordlist;
t_worddesc *current_word; t_worddesc *current_word;
int idx;
output = allocate_command_list(words); output = allocate_command_list(words);
if (output == NULL) if (output == NULL)
return (NULL); return (NULL);
current_wordlist = NULL; current_wordlist = NULL;
current_word = wordlist_pop(&words); 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); if (match_op(current_word->word) == OP_INVALID)
current_word = wordlist_pop(&words); {
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) if (current_wordlist != NULL)
output->operator = match_op(current_word->word); output->pipelines[idx] = pipeline_from_wordlist(current_wordlist);
worddesc_destroy(current_word);
output->pipelines[0] = pipeline_from_wordlist(current_wordlist);
if (words != NULL)
output->pipelines[1] = pipeline_from_wordlist(words);
return (output); return (output);
} }

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 17:40:48 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); ft_dprintf(STDERR_FILENO, "Expected pipeline %p to equal [%s]\n", got, expected);
t_pipeline *expected_pipeline = parse_pipeline(expected); t_pipeline *expected_pipeline = parse_pipeline(expected);
assert(expected_pipeline == got || expected_pipeline != NULL); 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); 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); assert(expected_pipeline->num_cmd == got->num_cmd);
int i = 0; int i = 0;
@ -110,11 +117,24 @@ static void test_parse_command_list_simple_or(void)
command_list_destroy(cmd); 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) int main(void)
{ {
test_parse_command_list_empty(); test_parse_command_list_empty();
test_parse_command_list_single_pipeline(); test_parse_command_list_single_pipeline();
test_parse_command_list_simple_and(); test_parse_command_list_simple_and();
test_parse_command_list_simple_or(); test_parse_command_list_simple_or();
test_parse_command_list_triple_or();
return (0); return (0);
} }