command list refactor: rename t_command_list to t_cmdlist

This commit is contained in:
Khaïs COLIN 2025-02-25 15:15:23 +01:00 committed by Khaïs COLIN
parent 659c9f57ff
commit 5fceb4713d
3 changed files with 33 additions and 33 deletions

View file

@ -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 15:08:48 by khais ### ########.fr */ /* Updated: 2025/02/25 15:25:14 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -60,13 +60,13 @@ static int command_list_count_pipelines(t_wordlist *words)
** **
** Handles malloc error. ** Handles malloc error.
*/ */
static t_command_list *allocate_command_list(t_wordlist *words) static t_cmdlist *allocate_command_list(t_wordlist *words)
{ {
t_command_list *output; t_cmdlist *output;
if (words == NULL) if (words == NULL)
return (NULL); return (NULL);
output = ft_calloc(1, sizeof(t_command_list)); output = ft_calloc(1, sizeof(t_cmdlist));
if (output == NULL) if (output == NULL)
return (NULL); return (NULL);
output->num_pipelines = command_list_count_pipelines(words); output->num_pipelines = command_list_count_pipelines(words);
@ -84,12 +84,12 @@ static t_command_list *allocate_command_list(t_wordlist *words)
/* /*
** Create a new command list from the given wordlist. ** Create a new command list from the given wordlist.
*/ */
t_command_list *command_list_from_wordlist(t_wordlist *words) t_cmdlist *cmdlist_from_wordlist(t_wordlist *words)
{ {
t_command_list *output; t_cmdlist *output;
t_wordlist *current_wordlist; t_wordlist *current_wordlist;
t_worddesc *current_word; t_worddesc *current_word;
int idx; int idx;
output = allocate_command_list(words); output = allocate_command_list(words);
if (output == NULL) if (output == NULL)
@ -125,7 +125,7 @@ t_command_list *command_list_from_wordlist(t_wordlist *words)
/* /*
** destroy the given command list and all associated memory ** destroy the given command list and all associated memory
*/ */
void command_list_destroy(t_command_list *cmd) void cmdlist_destroy(t_cmdlist *cmd)
{ {
int i; int i;

View file

@ -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 15:06:23 by khais ### ########.fr */ /* Updated: 2025/02/25 15:25:14 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -78,7 +78,7 @@ typedef enum e_operator
** The return status of AND and OR lists is the exit status of the last command ** The return status of AND and OR lists is the exit status of the last command
** executed in the list. ** executed in the list.
*/ */
typedef struct s_command_list typedef struct s_cmdlist
{ {
/* /*
** List of pipelines contained in this command list. ** List of pipelines contained in this command list.
@ -100,9 +100,9 @@ typedef struct s_command_list
** In example above, operators[1] == OP_END ** In example above, operators[1] == OP_END
*/ */
t_operator *operators; t_operator *operators;
} t_command_list; } t_cmdlist;
t_command_list *command_list_from_wordlist(t_wordlist *words); t_cmdlist *cmdlist_from_wordlist(t_wordlist *words);
void command_list_destroy(t_command_list *cmd); void cmdlist_destroy(t_cmdlist *cmd);
#endif // COMMAND_LIST_H #endif // COMMAND_LIST_H

View file

@ -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:10:29 by khais ### ########.fr */ /* Updated: 2025/02/25 15:25:14 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,11 +17,11 @@
#include "unistd.h" #include "unistd.h"
#include <assert.h> #include <assert.h>
static t_command_list *parse_command_list(char *input) static t_cmdlist *parse_command_list(char *input)
{ {
ft_dprintf(STDERR_FILENO, "Now checking command list with input [%s]\n", input); ft_dprintf(STDERR_FILENO, "Now checking command list with input [%s]\n", input);
t_wordlist *words = minishell_wordsplit(input); t_wordlist *words = minishell_wordsplit(input);
t_command_list *cmd = command_list_from_wordlist(words); t_cmdlist *cmd = cmdlist_from_wordlist(words);
return (cmd); return (cmd);
} }
@ -52,7 +52,7 @@ static void assert_simple_commandequal(t_simple_cmd *expected, t_simple_cmd *got
assert(got_word == NULL); assert(got_word == NULL);
} }
static void assert_pipelineequal(char *expected, t_command_list *cmd, int idx) static void assert_pipelineequal(char *expected, t_cmdlist *cmd, int idx)
{ {
ft_printf("Expected command list %p to have at least %d pipelines, and got %d\n", cmd, idx + 1, cmd->num_pipelines); ft_printf("Expected command list %p to have at least %d pipelines, and got %d\n", cmd, idx + 1, cmd->num_pipelines);
assert(cmd->num_pipelines >= idx + 1); assert(cmd->num_pipelines >= idx + 1);
@ -80,46 +80,46 @@ static void assert_pipelineequal(char *expected, t_command_list *cmd, int idx)
static void test_parse_command_list_empty(void) static void test_parse_command_list_empty(void)
{ {
t_command_list *cmd = parse_command_list(""); t_cmdlist *cmd = parse_command_list("");
assert(cmd == NULL); assert(cmd == NULL);
command_list_destroy(cmd); cmdlist_destroy(cmd);
} }
static void test_parse_command_list_single_pipeline(void) static void test_parse_command_list_single_pipeline(void)
{ {
t_command_list *cmd = parse_command_list("echo this | cat -e"); t_cmdlist *cmd = parse_command_list("echo this | cat -e");
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_END); assert(cmd->operators[0] == OP_END);
assert(cmd->num_pipelines == 1); assert(cmd->num_pipelines == 1);
command_list_destroy(cmd); cmdlist_destroy(cmd);
} }
static void test_parse_command_list_simple_and(void) 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_cmdlist *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_AND); 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); cmdlist_destroy(cmd);
} }
static void test_parse_command_list_simple_or(void) 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_cmdlist *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(cmd->operators[0] == OP_OR);
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); cmdlist_destroy(cmd);
} }
static void test_parse_command_list_triple_or(void) 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_cmdlist *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(cmd->operators[0] == OP_OR);
@ -127,12 +127,12 @@ static void test_parse_command_list_triple_or(void)
assert(cmd->operators[1] == OP_OR); 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->num_pipelines == 3); assert(cmd->num_pipelines == 3);
command_list_destroy(cmd); cmdlist_destroy(cmd);
} }
static void test_parse_command_list_triple_both_operators(void) 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"); t_cmdlist *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(cmd->operators[0] == OP_OR);
@ -141,12 +141,12 @@ static void test_parse_command_list_triple_both_operators(void)
assert_pipelineequal("echo as well | cut -d' ' -f1", cmd, 2); assert_pipelineequal("echo as well | cut -d' ' -f1", cmd, 2);
assert(cmd->operators[2] == OP_END); assert(cmd->operators[2] == OP_END);
assert(cmd->num_pipelines == 3); assert(cmd->num_pipelines == 3);
command_list_destroy(cmd); cmdlist_destroy(cmd);
} }
static void test_parse_command_list_quad_both_operators(void) 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"); t_cmdlist *cmd = parse_command_list("echo this | cat -e || echo works | wc -c && echo as well | cut -d' ' -f1 || echo final");
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(cmd->operators[0] == OP_OR);
@ -156,7 +156,7 @@ static void test_parse_command_list_quad_both_operators(void)
assert(cmd->operators[2] == OP_OR); assert(cmd->operators[2] == OP_OR);
assert_pipelineequal("echo final", cmd, 3); assert_pipelineequal("echo final", cmd, 3);
assert(cmd->operators[3] == OP_END); assert(cmd->operators[3] == OP_END);
command_list_destroy(cmd); cmdlist_destroy(cmd);
} }
int main(void) int main(void)