mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
command list parse: different operators
This commit is contained in:
parent
1c00020c86
commit
659c9f57ff
3 changed files with 79 additions and 9 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* 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 14:57:20 by khais ### ########.fr */
|
/* Updated: 2025/02/25 15:08:48 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -74,6 +74,10 @@ static t_command_list *allocate_command_list(t_wordlist *words)
|
||||||
= ft_calloc(output->num_pipelines, sizeof(t_pipeline *));
|
= ft_calloc(output->num_pipelines, sizeof(t_pipeline *));
|
||||||
if (output->pipelines == NULL)
|
if (output->pipelines == NULL)
|
||||||
return (free(output), NULL);
|
return (free(output), NULL);
|
||||||
|
output->operators
|
||||||
|
= ft_calloc(output->num_pipelines, sizeof(t_operator));
|
||||||
|
if (output->operators == NULL)
|
||||||
|
return (free(output->pipelines), free(output), NULL);
|
||||||
return (output);
|
return (output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,7 +107,7 @@ t_command_list *command_list_from_wordlist(t_wordlist *words)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
output->pipelines[idx] = pipeline_from_wordlist(current_wordlist);
|
output->pipelines[idx] = pipeline_from_wordlist(current_wordlist);
|
||||||
output->operator = match_op(current_word->word);
|
output->operators[idx] = match_op(current_word->word);
|
||||||
current_wordlist = NULL;
|
current_wordlist = NULL;
|
||||||
worddesc_destroy(current_word);
|
worddesc_destroy(current_word);
|
||||||
current_word = wordlist_pop(&words);
|
current_word = wordlist_pop(&words);
|
||||||
|
|
@ -111,7 +115,10 @@ t_command_list *command_list_from_wordlist(t_wordlist *words)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (current_wordlist != NULL)
|
if (current_wordlist != NULL)
|
||||||
|
{
|
||||||
output->pipelines[idx] = pipeline_from_wordlist(current_wordlist);
|
output->pipelines[idx] = pipeline_from_wordlist(current_wordlist);
|
||||||
|
output->operators[idx] = OP_END;
|
||||||
|
}
|
||||||
return (output);
|
return (output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -131,5 +138,6 @@ void command_list_destroy(t_command_list *cmd)
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
free(cmd->pipelines);
|
free(cmd->pipelines);
|
||||||
|
free(cmd->operators);
|
||||||
free(cmd);
|
free(cmd);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/24 17:45:01 by khais #+# #+# */
|
/* Created: 2025/02/24 17:45:01 by khais #+# #+# */
|
||||||
/* Updated: 2025/02/25 12:43:52 by khais ### ########.fr */
|
/* Updated: 2025/02/25 15:06:23 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -18,9 +18,22 @@
|
||||||
|
|
||||||
typedef enum e_operator
|
typedef enum e_operator
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
** Not a valid operator
|
||||||
|
*/
|
||||||
OP_INVALID,
|
OP_INVALID,
|
||||||
|
/*
|
||||||
|
** &&
|
||||||
|
*/
|
||||||
OP_AND,
|
OP_AND,
|
||||||
|
/*
|
||||||
|
** ||
|
||||||
|
*/
|
||||||
OP_OR,
|
OP_OR,
|
||||||
|
/*
|
||||||
|
** End of operator list
|
||||||
|
*/
|
||||||
|
OP_END,
|
||||||
} t_operator;
|
} t_operator;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -67,9 +80,26 @@ typedef enum e_operator
|
||||||
*/
|
*/
|
||||||
typedef struct s_command_list
|
typedef struct s_command_list
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
** List of pipelines contained in this command list.
|
||||||
|
**
|
||||||
|
** These should be executed left to right.
|
||||||
|
*/
|
||||||
t_pipeline **pipelines;
|
t_pipeline **pipelines;
|
||||||
|
/*
|
||||||
|
** Number of pipelines in this command list.
|
||||||
|
*/
|
||||||
int num_pipelines;
|
int num_pipelines;
|
||||||
t_operator operator;
|
/*
|
||||||
|
** Operators that separate the pipelines.
|
||||||
|
**
|
||||||
|
** pipelines[0] operators[0] pipelines[1] operators[1]
|
||||||
|
**
|
||||||
|
** Terminated by OP_END
|
||||||
|
**
|
||||||
|
** In example above, operators[1] == OP_END
|
||||||
|
*/
|
||||||
|
t_operator *operators;
|
||||||
} t_command_list;
|
} t_command_list;
|
||||||
|
|
||||||
t_command_list *command_list_from_wordlist(t_wordlist *words);
|
t_command_list *command_list_from_wordlist(t_wordlist *words);
|
||||||
|
|
|
||||||
|
|
@ -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 15:02:03 by khais ### ########.fr */
|
/* Updated: 2025/02/25 15:10:29 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -89,8 +89,8 @@ static void test_parse_command_list_single_pipeline(void)
|
||||||
{
|
{
|
||||||
t_command_list *cmd = parse_command_list("echo this | cat -e");
|
t_command_list *cmd = parse_command_list("echo this | cat -e");
|
||||||
assert(cmd != NULL);
|
assert(cmd != NULL);
|
||||||
assert(cmd->operator == OP_INVALID);
|
|
||||||
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
||||||
|
assert(cmd->operators[0] == OP_END);
|
||||||
assert(cmd->num_pipelines == 1);
|
assert(cmd->num_pipelines == 1);
|
||||||
command_list_destroy(cmd);
|
command_list_destroy(cmd);
|
||||||
}
|
}
|
||||||
|
|
@ -99,8 +99,8 @@ static void test_parse_command_list_simple_and(void)
|
||||||
{
|
{
|
||||||
t_command_list *cmd = parse_command_list("echo this | cat -e && echo works | wc -c");
|
t_command_list *cmd = parse_command_list("echo this | cat -e && echo works | wc -c");
|
||||||
assert(cmd != NULL);
|
assert(cmd != NULL);
|
||||||
assert(cmd->operator == OP_AND);
|
|
||||||
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
||||||
|
assert(cmd->operators[0] == OP_AND);
|
||||||
assert_pipelineequal("echo works | wc -c", cmd, 1);
|
assert_pipelineequal("echo works | wc -c", cmd, 1);
|
||||||
assert(cmd->num_pipelines == 2);
|
assert(cmd->num_pipelines == 2);
|
||||||
command_list_destroy(cmd);
|
command_list_destroy(cmd);
|
||||||
|
|
@ -111,8 +111,8 @@ static void test_parse_command_list_simple_or(void)
|
||||||
t_command_list *cmd = parse_command_list("echo this | cat -e || echo works | wc -c");
|
t_command_list *cmd = parse_command_list("echo this | cat -e || echo works | wc -c");
|
||||||
assert(cmd != NULL);
|
assert(cmd != NULL);
|
||||||
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
||||||
|
assert(cmd->operators[0] == OP_OR);
|
||||||
assert_pipelineequal("echo works | wc -c", cmd, 1);
|
assert_pipelineequal("echo works | wc -c", cmd, 1);
|
||||||
assert(cmd->operator == OP_OR);
|
|
||||||
assert(cmd->num_pipelines == 2);
|
assert(cmd->num_pipelines == 2);
|
||||||
command_list_destroy(cmd);
|
command_list_destroy(cmd);
|
||||||
}
|
}
|
||||||
|
|
@ -122,13 +122,43 @@ 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");
|
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(cmd != NULL);
|
||||||
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
||||||
|
assert(cmd->operators[0] == OP_OR);
|
||||||
assert_pipelineequal("echo works | wc -c", cmd, 1);
|
assert_pipelineequal("echo works | wc -c", cmd, 1);
|
||||||
|
assert(cmd->operators[1] == OP_OR);
|
||||||
assert_pipelineequal("echo as well | cut -d' ' -f1", cmd, 2);
|
assert_pipelineequal("echo as well | cut -d' ' -f1", cmd, 2);
|
||||||
assert(cmd->operator == OP_OR);
|
|
||||||
assert(cmd->num_pipelines == 3);
|
assert(cmd->num_pipelines == 3);
|
||||||
command_list_destroy(cmd);
|
command_list_destroy(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_parse_command_list_triple_both_operators(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(cmd->operators[0] == OP_OR);
|
||||||
|
assert_pipelineequal("echo works | wc -c", cmd, 1);
|
||||||
|
assert(cmd->operators[1] == OP_AND);
|
||||||
|
assert_pipelineequal("echo as well | cut -d' ' -f1", cmd, 2);
|
||||||
|
assert(cmd->operators[2] == OP_END);
|
||||||
|
assert(cmd->num_pipelines == 3);
|
||||||
|
command_list_destroy(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_parse_command_list_quad_both_operators(void)
|
||||||
|
{
|
||||||
|
t_command_list *cmd = parse_command_list("echo this | cat -e || echo works | wc -c && echo as well | cut -d' ' -f1 || echo final");
|
||||||
|
assert(cmd != NULL);
|
||||||
|
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
||||||
|
assert(cmd->operators[0] == OP_OR);
|
||||||
|
assert_pipelineequal("echo works | wc -c", cmd, 1);
|
||||||
|
assert(cmd->operators[1] == OP_AND);
|
||||||
|
assert_pipelineequal("echo as well | cut -d' ' -f1", cmd, 2);
|
||||||
|
assert(cmd->operators[2] == OP_OR);
|
||||||
|
assert_pipelineequal("echo final", cmd, 3);
|
||||||
|
assert(cmd->operators[3] == OP_END);
|
||||||
|
command_list_destroy(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
test_parse_command_list_empty();
|
test_parse_command_list_empty();
|
||||||
|
|
@ -136,5 +166,7 @@ int main(void)
|
||||||
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();
|
test_parse_command_list_triple_or();
|
||||||
|
test_parse_command_list_triple_both_operators();
|
||||||
|
test_parse_command_list_quad_both_operators();
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue