mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
pipeline: handle parsing of single-command pipelines
This commit is contained in:
parent
695596fde2
commit
256a8f5f9b
5 changed files with 138 additions and 0 deletions
1
Makefile
1
Makefile
|
|
@ -32,6 +32,7 @@ srcs = \
|
|||
src/parser/matchers/operator_combo.c \
|
||||
src/parser/matchers/operator_start.c \
|
||||
src/parser/matchers/quote.c \
|
||||
src/parser/pipeline/pipeline.c \
|
||||
src/parser/simple_cmd/simple_cmd.c \
|
||||
src/parser/worddesc/worddesc.c \
|
||||
src/parser/wordlist/wordlist.c \
|
||||
|
|
|
|||
47
src/parser/pipeline/pipeline.c
Normal file
47
src/parser/pipeline/pipeline.c
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pipeline.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/21 13:23:50 by khais #+# #+# */
|
||||
/* Updated: 2025/02/21 13:45:12 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "pipeline.h"
|
||||
#include "libft.h"
|
||||
|
||||
t_pipeline *pipeline_from_wordlist(t_wordlist *words)
|
||||
{
|
||||
t_pipeline *pipeline;
|
||||
|
||||
if (words == NULL)
|
||||
return (NULL);
|
||||
pipeline = ft_calloc(1, sizeof(t_pipeline));
|
||||
if (pipeline == NULL)
|
||||
return (NULL);
|
||||
pipeline->num_cmd = 1;
|
||||
pipeline->cmds = ft_calloc(1, sizeof(t_simple_cmd *));
|
||||
if (pipeline->cmds == NULL)
|
||||
return (NULL);
|
||||
pipeline->cmds[0] = simple_cmd_from_wordlist(words);
|
||||
return (pipeline);
|
||||
}
|
||||
|
||||
void pipeline_destroy(t_pipeline *pipeline)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (pipeline == NULL)
|
||||
return ;
|
||||
i = 0;
|
||||
while (i < pipeline->num_cmd)
|
||||
{
|
||||
simple_cmd_destroy(pipeline->cmds[i]);
|
||||
i++;
|
||||
}
|
||||
free(pipeline->cmds);
|
||||
free(pipeline);
|
||||
}
|
||||
27
src/parser/pipeline/pipeline.h
Normal file
27
src/parser/pipeline/pipeline.h
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pipeline.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/21 13:19:51 by khais #+# #+# */
|
||||
/* Updated: 2025/02/21 13:27:50 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PIPELINE_H
|
||||
# define PIPELINE_H
|
||||
|
||||
# include "../simple_cmd/simple_cmd.h"
|
||||
|
||||
typedef struct s_pipeline
|
||||
{
|
||||
t_simple_cmd **cmds;
|
||||
int num_cmd;
|
||||
} t_pipeline;
|
||||
|
||||
t_pipeline *pipeline_from_wordlist(t_wordlist *words);
|
||||
void pipeline_destroy(t_pipeline *pipeline);
|
||||
|
||||
#endif
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
# file are prefixed with test_
|
||||
rawtests = \
|
||||
metacharacters \
|
||||
parse_pipelines \
|
||||
parse_simple_cmds \
|
||||
test_env_manip \
|
||||
word_splitting \
|
||||
|
|
|
|||
62
tests/parse_pipelines.c
Normal file
62
tests/parse_pipelines.c
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parse_pipelines.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/21 13:13:58 by khais #+# #+# */
|
||||
/* Updated: 2025/02/21 13:44:16 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../src/parser/pipeline/pipeline.h"
|
||||
#include "../src/parser/wordsplit/wordsplit.h"
|
||||
#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)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "expected pipeline %p to have at least %d words, but got %d\n", pipeline, cmd_num + 1, pipeline->num_cmd);
|
||||
assert(false);
|
||||
}
|
||||
char *got_word = wordlist_get(pipeline->cmds[cmd_num]->words, word_num)->word;
|
||||
ft_dprintf(STDERR_FILENO, "for pipeline %p cmd %d word %d, expected '%s', and got '%s'\n", pipeline, cmd_num, word_num, expected_word, got_word);
|
||||
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);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_parse_empty_pipeline();
|
||||
test_parse_pipeline_single_cmd();
|
||||
return (0);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue