mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
command list parse: handle two commands separated by &&
This commit is contained in:
parent
3b8b2c7a4a
commit
3b36a77d34
3 changed files with 102 additions and 6 deletions
|
|
@ -6,20 +6,48 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/24 17:49:46 by khais #+# #+# */
|
||||
/* Updated: 2025/02/24 17:51:25 by khais ### ########.fr */
|
||||
/* Updated: 2025/02/24 18:58:48 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "command_list.h"
|
||||
#include "libft.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_command_list *command_list_from_wordlist(t_wordlist *words)
|
||||
{
|
||||
(void)words;
|
||||
return (NULL);
|
||||
t_command_list *output;
|
||||
t_wordlist *current_wordlist;
|
||||
t_worddesc *current_word;
|
||||
|
||||
output = NULL;
|
||||
if (words != NULL)
|
||||
{
|
||||
output = ft_calloc(1, sizeof(t_command_list));
|
||||
if (output == NULL)
|
||||
return (NULL);
|
||||
current_wordlist = NULL;
|
||||
current_word = wordlist_pop(&words);
|
||||
while (ft_strcmp("&&", current_word->word) != 0)
|
||||
{
|
||||
current_wordlist = wordlist_push(current_wordlist, current_word);
|
||||
current_word = wordlist_pop(&words);
|
||||
}
|
||||
worddesc_destroy(current_word);
|
||||
output->operator = OP_AND;
|
||||
wordlist_debug(current_wordlist);
|
||||
output->pipeline_left = pipeline_from_wordlist(current_wordlist);
|
||||
wordlist_debug(words);
|
||||
output->pipeline_right = pipeline_from_wordlist(words);
|
||||
}
|
||||
return (output);
|
||||
}
|
||||
|
||||
void command_list_destroy(t_command_list *cmd)
|
||||
{
|
||||
(void)cmd;
|
||||
if (cmd == NULL)
|
||||
return ;
|
||||
pipeline_destroy(cmd->pipeline_left);
|
||||
pipeline_destroy(cmd->pipeline_right);
|
||||
free(cmd);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/24 17:45:01 by khais #+# #+# */
|
||||
/* Updated: 2025/02/24 17:49:33 by khais ### ########.fr */
|
||||
/* Updated: 2025/02/24 18:09:24 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,6 +14,15 @@
|
|||
# define COMMAND_LIST_H
|
||||
|
||||
# include "../wordlist/wordlist.h"
|
||||
# include "../pipeline/pipeline.h"
|
||||
|
||||
typedef enum e_operator
|
||||
{
|
||||
OP_INVALID,
|
||||
OP_AND,
|
||||
OP_OR,
|
||||
} t_operator;
|
||||
|
||||
/*
|
||||
** cf. 3.2.4 Lists of Commands
|
||||
**
|
||||
|
|
@ -58,6 +67,9 @@
|
|||
*/
|
||||
typedef struct s_command_list
|
||||
{
|
||||
t_pipeline *pipeline_left;
|
||||
t_pipeline *pipeline_right;
|
||||
t_operator operator;
|
||||
} t_command_list;
|
||||
|
||||
t_command_list *command_list_from_wordlist(t_wordlist *words);
|
||||
|
|
|
|||
|
|
@ -6,12 +6,15 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/24 17:40:48 by khais #+# #+# */
|
||||
/* Updated: 2025/02/24 17:47:39 by khais ### ########.fr */
|
||||
/* Updated: 2025/02/25 12:34:07 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../src/parser/command_list/command_list.h"
|
||||
#include "../src/parser/wordsplit/wordsplit.h"
|
||||
#include "ft_printf.h"
|
||||
#include "testutil.h"
|
||||
#include "unistd.h"
|
||||
#include <assert.h>
|
||||
|
||||
static t_command_list *parse_command_list(char *input)
|
||||
|
|
@ -21,6 +24,48 @@ static t_command_list *parse_command_list(char *input)
|
|||
return (cmd);
|
||||
}
|
||||
|
||||
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_simple_commandequal(t_simple_cmd *expected, t_simple_cmd *got, int idx)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "Checking cmd idx %d\n", idx);
|
||||
int i = 0;
|
||||
t_wordlist *expected_word = expected->words;
|
||||
t_wordlist *got_word = got->words;
|
||||
while (expected_word != NULL)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "Checking word %d: ", i);
|
||||
assert_strequal(expected_word->word->word, got_word->word->word);
|
||||
ft_dprintf(STDERR_FILENO, "Checking word %d: expected flags=%d got flags=%d\n", i, expected_word->word->flags, got_word->word->flags);
|
||||
assert(expected_word->word->flags == got_word->word->flags);
|
||||
expected_word = expected_word->next;
|
||||
got_word = got_word->next;
|
||||
i++;
|
||||
}
|
||||
assert(expected_word == NULL);
|
||||
assert(got_word == NULL);
|
||||
}
|
||||
|
||||
static void assert_pipelineequal(char *expected, t_pipeline *got)
|
||||
{
|
||||
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);
|
||||
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;
|
||||
while (i < expected_pipeline->num_cmd)
|
||||
{
|
||||
assert_simple_commandequal(expected_pipeline->cmds[i], got->cmds[i], i);
|
||||
i++;
|
||||
}
|
||||
pipeline_destroy(expected_pipeline);
|
||||
}
|
||||
|
||||
static void test_parse_command_list_empty(void)
|
||||
{
|
||||
|
|
@ -29,8 +74,19 @@ static void test_parse_command_list_empty(void)
|
|||
command_list_destroy(cmd);
|
||||
}
|
||||
|
||||
static void test_parse_command_list_simple_and(void)
|
||||
{
|
||||
t_command_list *cmd = parse_command_list("echo this | cat -e && echo works | wc -c");
|
||||
assert(cmd != NULL);
|
||||
assert(cmd->operator == OP_AND);
|
||||
assert_pipelineequal("echo this | cat -e", cmd->pipeline_left);
|
||||
assert_pipelineequal("echo works | wc -c", cmd->pipeline_right);
|
||||
command_list_destroy(cmd);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_parse_command_list_empty();
|
||||
test_parse_command_list_simple_and();
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue