2025-02-21 13:44:51 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* parse_pipelines.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2025/02/21 13:13:58 by khais #+# #+# */
|
2025-02-21 15:46:34 +01:00
|
|
|
/* Updated: 2025/02/21 16:03:39 by khais ### ########.fr */
|
2025-02-21 13:44:51 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#include "../src/parser/pipeline/pipeline.h"
|
|
|
|
|
#include "../src/parser/wordsplit/wordsplit.h"
|
2025-02-21 15:46:34 +01:00
|
|
|
#include "../src/ft_errno.h"
|
2025-02-21 13:44:51 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include "ft_printf.h"
|
|
|
|
|
#include "libft.h"
|
|
|
|
|
#include "unistd.h"
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
|
|
static t_pipeline *parse_pipeline(char *input)
|
|
|
|
|
{
|
|
|
|
|
t_wordlist *words = minishell_wordsplit(input);
|
|
|
|
|
t_pipeline *pipeline = pipeline_from_wordlist(words);
|
|
|
|
|
return (pipeline);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void assert_pipeline_cmd_word(t_pipeline *pipeline, char *expected_word, int cmd_num, int word_num)
|
|
|
|
|
{
|
|
|
|
|
if (pipeline->num_cmd <= cmd_num)
|
|
|
|
|
{
|
2025-02-21 14:13:51 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "expected pipeline %p to have at least %d cmds, but got %d\n", pipeline, cmd_num + 1, pipeline->num_cmd);
|
2025-02-21 13:44:51 +01:00
|
|
|
assert(false);
|
|
|
|
|
}
|
2025-02-21 14:13:51 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "for pipeline %p cmd %d word %d, expected '%s', and got ", pipeline, cmd_num, word_num, expected_word);
|
|
|
|
|
assert(pipeline->cmds[cmd_num] != NULL && "null cmd at that location");
|
2025-02-21 13:44:51 +01:00
|
|
|
char *got_word = wordlist_get(pipeline->cmds[cmd_num]->words, word_num)->word;
|
2025-02-21 14:13:51 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "'%s'\n", got_word);
|
2025-02-21 13:44:51 +01:00
|
|
|
assert(expected_word == got_word || ft_strncmp(expected_word, got_word, INT_MAX) == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_parse_empty_pipeline(void)
|
|
|
|
|
{
|
|
|
|
|
t_pipeline *pipeline = parse_pipeline("");
|
|
|
|
|
assert(pipeline == NULL);
|
|
|
|
|
pipeline_destroy(pipeline);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_parse_pipeline_single_cmd(void)
|
|
|
|
|
{
|
|
|
|
|
t_pipeline *pipeline = parse_pipeline("echo hello world");
|
|
|
|
|
assert(pipeline->num_cmd == 1);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "echo", 0, 0);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "hello", 0, 1);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "world", 0, 2);
|
|
|
|
|
pipeline_destroy(pipeline);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 14:13:51 +01:00
|
|
|
static void test_parse_pipeline_two_cmd(void)
|
|
|
|
|
{
|
|
|
|
|
t_pipeline *pipeline = parse_pipeline("echo hello world | tee output.txt");
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "echo", 0, 0);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "hello", 0, 1);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "world", 0, 2);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "tee", 1, 0);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "output.txt", 1, 1);
|
|
|
|
|
pipeline_destroy(pipeline);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 15:30:07 +01:00
|
|
|
static void test_parse_pipeline_three_cmd(void)
|
|
|
|
|
{
|
|
|
|
|
t_pipeline *pipeline = parse_pipeline("echo hello world | tee output.txt | cat -e");
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "echo", 0, 0);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "hello", 0, 1);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "world", 0, 2);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "tee", 1, 0);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "output.txt", 1, 1);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "cat", 2, 0);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "-e", 2, 1);
|
|
|
|
|
pipeline_destroy(pipeline);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_parse_pipeline_four_cmd(void)
|
|
|
|
|
{
|
|
|
|
|
t_pipeline *pipeline = parse_pipeline("echo hello world | tee output.txt | cat -e | hexdump -C");
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "echo", 0, 0);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "hello", 0, 1);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "world", 0, 2);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "tee", 1, 0);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "output.txt", 1, 1);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "cat", 2, 0);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "-e", 2, 1);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "hexdump", 3, 0);
|
|
|
|
|
assert_pipeline_cmd_word(pipeline, "-C", 3, 1);
|
|
|
|
|
pipeline_destroy(pipeline);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 15:46:34 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 13:44:51 +01:00
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
test_parse_empty_pipeline();
|
|
|
|
|
test_parse_pipeline_single_cmd();
|
2025-02-21 14:13:51 +01:00
|
|
|
test_parse_pipeline_two_cmd();
|
2025-02-21 15:30:07 +01:00
|
|
|
test_parse_pipeline_three_cmd();
|
|
|
|
|
test_parse_pipeline_four_cmd();
|
2025-02-21 15:46:34 +01:00
|
|
|
test_parse_pipeline_double_pipe_rejected();
|
|
|
|
|
test_parse_pipeline_triple_pipe_rejected();
|
2025-02-21 13:44:51 +01:00
|
|
|
return (0);
|
|
|
|
|
}
|