mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
pipeline: parse basic pipeline commands with two commands
This commit is contained in:
parent
2b8bb859d1
commit
5f1485d1d5
4 changed files with 97 additions and 9 deletions
|
|
@ -6,12 +6,66 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/21 13:23:50 by khais #+# #+# */
|
/* Created: 2025/02/21 13:23:50 by khais #+# #+# */
|
||||||
/* Updated: 2025/02/21 13:45:12 by khais ### ########.fr */
|
/* Updated: 2025/02/21 15:23:12 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "pipeline.h"
|
#include "pipeline.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
#include "unistd.h"
|
||||||
|
#include "../matchers/pipe.h"
|
||||||
|
|
||||||
|
static int pipeline_count_cmds(t_wordlist *words)
|
||||||
|
{
|
||||||
|
int count;
|
||||||
|
|
||||||
|
if (words == NULL)
|
||||||
|
return (0);
|
||||||
|
count = 1;
|
||||||
|
while (words != NULL)
|
||||||
|
{
|
||||||
|
if (is_pipe(words->word))
|
||||||
|
count++;
|
||||||
|
words = words->next;
|
||||||
|
}
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
|
|
||||||
|
static t_worddesc *ignore_word(t_worddesc *current_word, t_wordlist **words)
|
||||||
|
{
|
||||||
|
worddesc_destroy(current_word);
|
||||||
|
return (wordlist_pop(words));
|
||||||
|
}
|
||||||
|
|
||||||
|
static t_pipeline *pipeline_parse_wordlist(t_pipeline *pipeline,
|
||||||
|
t_wordlist *words)
|
||||||
|
{
|
||||||
|
int idx;
|
||||||
|
t_wordlist *current_wordlist;
|
||||||
|
t_worddesc *current_word;
|
||||||
|
|
||||||
|
idx = 0;
|
||||||
|
current_wordlist = NULL;
|
||||||
|
current_word = wordlist_pop(&words);
|
||||||
|
while (current_word != NULL)
|
||||||
|
{
|
||||||
|
if (is_pipe(current_word))
|
||||||
|
{
|
||||||
|
pipeline->cmds[idx] = simple_cmd_from_wordlist(current_wordlist);
|
||||||
|
if (pipeline->cmds[idx] == NULL)
|
||||||
|
return (pipeline_destroy(pipeline), NULL);
|
||||||
|
current_wordlist = NULL;
|
||||||
|
current_word = ignore_word(current_word, &words);
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
current_wordlist = wordlist_push(current_wordlist, current_word);
|
||||||
|
current_word = wordlist_pop(&words);
|
||||||
|
}
|
||||||
|
pipeline->cmds[idx] = simple_cmd_from_wordlist(current_wordlist);
|
||||||
|
if (pipeline->cmds[idx] == NULL)
|
||||||
|
return (pipeline_destroy(pipeline), NULL);
|
||||||
|
return (pipeline);
|
||||||
|
}
|
||||||
|
|
||||||
t_pipeline *pipeline_from_wordlist(t_wordlist *words)
|
t_pipeline *pipeline_from_wordlist(t_wordlist *words)
|
||||||
{
|
{
|
||||||
|
|
@ -22,11 +76,11 @@ t_pipeline *pipeline_from_wordlist(t_wordlist *words)
|
||||||
pipeline = ft_calloc(1, sizeof(t_pipeline));
|
pipeline = ft_calloc(1, sizeof(t_pipeline));
|
||||||
if (pipeline == NULL)
|
if (pipeline == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
pipeline->num_cmd = 1;
|
pipeline->num_cmd = pipeline_count_cmds(words);
|
||||||
pipeline->cmds = ft_calloc(1, sizeof(t_simple_cmd *));
|
pipeline->cmds = ft_calloc(pipeline->num_cmd, sizeof(t_simple_cmd *));
|
||||||
if (pipeline->cmds == NULL)
|
if (pipeline->cmds == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
pipeline->cmds[0] = simple_cmd_from_wordlist(words);
|
pipeline = pipeline_parse_wordlist(pipeline, words);
|
||||||
return (pipeline);
|
return (pipeline);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/13 17:07:01 by khais #+# #+# */
|
/* Created: 2025/02/13 17:07:01 by khais #+# #+# */
|
||||||
/* Updated: 2025/02/14 16:47:04 by khais ### ########.fr */
|
/* Updated: 2025/02/21 14:04:50 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -91,3 +91,22 @@ t_wordlist *wordlist_push(t_wordlist *wordlist, t_worddesc *worddesc)
|
||||||
return (wordlist_destroy(start), NULL);
|
return (wordlist_destroy(start), NULL);
|
||||||
return (start);
|
return (start);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** remove and return the first element in the given wordlist
|
||||||
|
**
|
||||||
|
** If wordlist is empty, return null.
|
||||||
|
*/
|
||||||
|
t_worddesc *wordlist_pop(t_wordlist **wordlist)
|
||||||
|
{
|
||||||
|
t_wordlist *first;
|
||||||
|
t_worddesc *word;
|
||||||
|
|
||||||
|
if ((*wordlist) == NULL)
|
||||||
|
return (NULL);
|
||||||
|
first = *wordlist;
|
||||||
|
(*wordlist) = first->next;
|
||||||
|
word = first->word;
|
||||||
|
free(first);
|
||||||
|
return (word);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/13 15:46:02 by khais #+# #+# */
|
/* Created: 2025/02/13 15:46:02 by khais #+# #+# */
|
||||||
/* Updated: 2025/02/14 15:48:36 by khais ### ########.fr */
|
/* Updated: 2025/02/21 14:00:19 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -38,5 +38,6 @@ t_wordlist *wordlist_create(t_worddesc *word);
|
||||||
void wordlist_destroy(t_wordlist *wordlist);
|
void wordlist_destroy(t_wordlist *wordlist);
|
||||||
t_worddesc *wordlist_get(t_wordlist *wordlist, int idx);
|
t_worddesc *wordlist_get(t_wordlist *wordlist, int idx);
|
||||||
t_wordlist *wordlist_push(t_wordlist *wordlist, t_worddesc *worddesc);
|
t_wordlist *wordlist_push(t_wordlist *wordlist, t_worddesc *worddesc);
|
||||||
|
t_worddesc *wordlist_pop(t_wordlist **wordlist);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/21 13:13:58 by khais #+# #+# */
|
/* Created: 2025/02/21 13:13:58 by khais #+# #+# */
|
||||||
/* Updated: 2025/02/21 13:44:16 by khais ### ########.fr */
|
/* Updated: 2025/02/21 14:07:52 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -29,11 +29,13 @@ static void assert_pipeline_cmd_word(t_pipeline *pipeline, char *expected_word,
|
||||||
{
|
{
|
||||||
if (pipeline->num_cmd <= cmd_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);
|
ft_dprintf(STDERR_FILENO, "expected pipeline %p to have at least %d cmds, but got %d\n", pipeline, cmd_num + 1, pipeline->num_cmd);
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
|
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");
|
||||||
char *got_word = wordlist_get(pipeline->cmds[cmd_num]->words, word_num)->word;
|
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);
|
ft_dprintf(STDERR_FILENO, "'%s'\n", got_word);
|
||||||
assert(expected_word == got_word || ft_strncmp(expected_word, got_word, INT_MAX) == 0);
|
assert(expected_word == got_word || ft_strncmp(expected_word, got_word, INT_MAX) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,9 +56,21 @@ static void test_parse_pipeline_single_cmd(void)
|
||||||
pipeline_destroy(pipeline);
|
pipeline_destroy(pipeline);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
test_parse_empty_pipeline();
|
test_parse_empty_pipeline();
|
||||||
test_parse_pipeline_single_cmd();
|
test_parse_pipeline_single_cmd();
|
||||||
|
test_parse_pipeline_two_cmd();
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue