cmdgroup parsing: handle parsing a single cmdlist (badly)

This commit is contained in:
Khaïs COLIN 2025-03-11 16:41:38 +01:00
parent 9707316085
commit d8dd1613c8
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
8 changed files with 157 additions and 29 deletions

View file

@ -210,16 +210,14 @@ The exit status of this construct is the exit status of LIST.
```c ```c
struct s_cmdgroup; struct s_cmdgroup;
typedef union u_cmdgroup_item_inner
{
struct s_cmdgroup cmdgroup;
struct s_cmdlist cmdlist;
} t_cmdgroup_item_inner;
typedef struct s_cmdgroup_item typedef struct s_cmdgroup_item
{ {
enum e_cmdgroup_item_type type; enum e_cmdgroup_item_type type;
union u_cmdgroup_item_inner inner; union u_cmdgroup_item_inner
{
t_cmdgroup *cmdgroup;
struct s_cmdlist *cmdlist;
} inner;
} t_cmdgroup_item; } t_cmdgroup_item;
typedef struct s_cmdgroup typedef struct s_cmdgroup

View file

@ -6,17 +6,32 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/11 15:18:02 by khais #+# #+# */ /* Created: 2025/03/11 15:18:02 by khais #+# #+# */
/* Updated: 2025/03/11 15:18:59 by khais ### ########.fr */ /* Updated: 2025/03/11 18:14:24 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cmdgroup.h" #include "cmdgroup.h"
#include <stdlib.h> #include <stdlib.h>
#include "cmdgroup_item.h"
#include "cmdgroup_item_type.h"
#include "libft.h"
t_cmdgroup *cmdgroup_from_wordlist(t_wordlist *words) t_cmdgroup *cmdgroup_from_wordlist(t_wordlist *words)
{ {
(void)words; t_cmdgroup *cmd;
if (words == NULL)
return (NULL); return (NULL);
cmd = ft_calloc(1, sizeof(t_cmdgroup));
if (cmd == NULL)
return (NULL);
cmd->item_num = 1;
cmd->items = ft_calloc(1, sizeof(t_cmdgroup_item));
if (cmd->items == NULL)
return (free(cmd), NULL);
cmd->items[0].type = TYPE_LIST;
cmd->items[0].inner.cmdlist = cmdlist_from_wordlist(words);
return (cmd);
} }
void cmdgroup_destroy(t_cmdgroup *cmd) void cmdgroup_destroy(t_cmdgroup *cmd)

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/11 15:11:57 by khais #+# #+# */ /* Created: 2025/03/11 15:11:57 by khais #+# #+# */
/* Updated: 2025/03/11 15:17:22 by khais ### ########.fr */ /* Updated: 2025/03/11 18:14:33 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,8 +15,18 @@
# include "../wordlist/wordlist.h" # include "../wordlist/wordlist.h"
struct s_cmdgroup_item;
typedef struct s_cmdgroup typedef struct s_cmdgroup
{ {
/*
** Number of items in this cmdgroup
*/
int item_num;
/*
** array of items in this cmdgroup
*/
struct s_cmdgroup_item *items;
} t_cmdgroup; } t_cmdgroup;
t_cmdgroup *cmdgroup_from_wordlist(t_wordlist *words); t_cmdgroup *cmdgroup_from_wordlist(t_wordlist *words);

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmdgroup_item.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/11 15:43:15 by khais #+# #+# */
/* Updated: 2025/03/11 18:10:24 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CMDGROUP_ITEM_H
# define CMDGROUP_ITEM_H
# include "cmdgroup_item_type.h"
# include "../command_list/command_list.h"
# include "cmdgroup.h"
typedef struct s_cmdgroup_item
{
enum e_cmdgroup_item_type type;
union u_cmdgroup_item_inner
{
t_cmdgroup *cmdgroup;
struct s_cmdlist *cmdlist;
} inner;
} t_cmdgroup_item;
#endif // CMDGROUP_ITEM_H

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmdgroup_item_type.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/11 15:45:33 by khais #+# #+# */
/* Updated: 2025/03/11 15:46:25 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CMDGROUP_ITEM_TYPE_H
# define CMDGROUP_ITEM_TYPE_H
typedef enum e_cmdgroup_item_type
{
TYPE_INVALID = 0,
TYPE_GROUP,
TYPE_LIST,
} t_cmdgroup_item_type;
#endif // CMDGROUP_ITEM_TYPE_H

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/11 15:07:23 by khais #+# #+# */ /* Created: 2025/03/11 17:30/41 by khais #+# #+# */
/* Updated: 2025/03/11 16:33:42 by khais ### ########.fr */ /* Updated: 2025/03/11 17:30:41 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
#include <assert.h> #include <assert.h>
#include "libft.h" #include "libft.h"
#include "../src/parser/cmdgroup/cmdgroup.h" #include "../src/parser/cmdgroup/cmdgroup.h"
#include "../src/parser/wordsplit/wordsplit.h" #include "../src/parser/cmdgroup/cmdgroup_item.h"
static void test_cmdgroup_parsing_empty(void) static void test_cmdgroup_parsing_empty(void)
{ {
@ -30,8 +30,27 @@ static void test_cmdgroup_parsing_empty(void)
do_leak_check(); do_leak_check();
} }
static void test_cmdgroup_parsing_single_cmdlist(void)
{
t_cmdgroup *cmd;
// arange
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
cmd = parse_cmdgroup("echo this | cat -e && echo works | wc -c");
// assert
assert(NULL != cmd);
assert(1 == cmd->item_num);
assert(TYPE_LIST == cmd->items[0].type);
assert_cmdgroup_itemlistequal("echo this | cat -e && echo works | wc -c", cmd, 0);
// cleanup
cmdgroup_destroy(cmd);
do_leak_check();
}
int main(void) int main(void)
{ {
test_cmdgroup_parsing_empty(); test_cmdgroup_parsing_empty();
test_cmdgroup_parsing_single_cmdlist();
// redirections
return (0); return (0);
} }

View file

@ -6,18 +6,20 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 15:21:09 by khais #+# #+# */ /* Created: 2025/02/13 15:21:09 by khais #+# #+# */
/* Updated: 2025/03/11 16:33:46 by khais ### ########.fr */ /* Updated: 2025/03/11 18:12:05 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
#include <assert.h> #include <assert.h>
#include "testutil.h" #include "testutil.h"
#include "parse_command_list.h"
#include "unistd.h" #include "unistd.h"
#include <assert.h> #include <assert.h>
#include "parse_pipeline.h" #include "parse_pipeline.h"
#include "../src/parser/wordlist/wordlist.h" #include "../src/parser/wordlist/wordlist.h"
#include "../src/parser/wordsplit/wordsplit.h" #include "../src/parser/wordsplit/wordsplit.h"
#include "../src/parser/cmdgroup/cmdgroup_item.h"
void assert_strequal(char *str1, char *str2) void assert_strequal(char *str1, char *str2)
{ {
@ -60,14 +62,9 @@ void assert_simple_commandequal(t_simple_cmd *expected, t_simple_cmd *got, int i
assert(got_word == NULL); assert(got_word == NULL);
} }
void assert_pipelineequal(char *expected, t_cmdlist *cmd, int idx) void assert_pipelineequal_raw(t_pipeline *expected, t_pipeline *got)
{ {
ft_printf("Expected command list %p to have at least %d pipelines, and got %d\n", cmd, idx + 1, cmd->num_pipelines); assert(expected == got || expected != NULL);
assert(cmd->num_pipelines >= idx + 1);
t_pipeline *got = cmd->pipelines[idx];
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);
int j = 0; int j = 0;
while (j < got->num_cmd) while (j < got->num_cmd)
{ {
@ -75,14 +72,24 @@ void assert_pipelineequal(char *expected, t_cmdlist *cmd, int idx)
wordlist_debug(got->cmds[j]->words); wordlist_debug(got->cmds[j]->words);
j++; j++;
} }
ft_dprintf(STDERR_FILENO, "Expected pipeline to have %d commands, got pipeline with %d\n", expected_pipeline->num_cmd, got->num_cmd); ft_dprintf(STDERR_FILENO, "Expected pipeline to have %d commands, got pipeline with %d\n", expected->num_cmd, got->num_cmd);
assert(expected_pipeline->num_cmd == got->num_cmd); assert(expected->num_cmd == got->num_cmd);
int i = 0; int i = 0;
while (i < expected_pipeline->num_cmd) while (i < expected->num_cmd)
{ {
assert_simple_commandequal(expected_pipeline->cmds[i], got->cmds[i], i); assert_simple_commandequal(expected->cmds[i], got->cmds[i], i);
i++; i++;
} }
}
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);
assert(cmd->num_pipelines >= idx + 1);
t_pipeline *got = cmd->pipelines[idx];
ft_dprintf(STDERR_FILENO, "Expected pipeline %p to equal [%s]\n", got, expected);
t_pipeline *expected_pipeline = parse_pipeline(expected);
assert_pipelineequal_raw(expected_pipeline, got);
pipeline_destroy(expected_pipeline); pipeline_destroy(expected_pipeline);
} }
@ -117,3 +124,28 @@ t_cmdgroup *parse_cmdgroup(char *input)
wordlist_destroy(words); wordlist_destroy(words);
return (cmd); return (cmd);
} }
void assert_cmdgroup_itemlistequal(char *expected_str, t_cmdgroup *got_cmd, int item_number)
{
ft_dprintf(STDERR_FILENO, "checking that item %d of cmdlist %p matches [%s]\n", item_number, got_cmd, expected_str);
ft_dprintf(STDERR_FILENO, "expecteing to have at least %d items, and got %d\n", item_number + 1, got_cmd->item_num);
assert(got_cmd->item_num > item_number);
ft_dprintf(STDERR_FILENO, "expecteing item %p to be of type list got %d\n", got_cmd->items[item_number], got_cmd->items[item_number].type);
t_cmdlist *expected = parse_command_list(expected_str);
t_cmdlist *got = got_cmd->items[item_number].inner.cmdlist;
if (expected == NULL)
{
ft_dprintf(STDERR_FILENO, "expecting the list to be null\n");
assert(expected == got);
return ;
}
ft_dprintf(STDERR_FILENO, "checking if the list matches...\n");
assert(expected->num_pipelines == got->num_pipelines);
int i = 0;
while (i < expected->num_pipelines)
{
assert_pipelineequal_raw(expected->pipelines[i], got->pipelines[i]);
assert(expected->operators[i] == got->operators[i]);
i++;
}
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 15:57:21 by khais #+# #+# */ /* Created: 2025/02/13 15:57:21 by khais #+# #+# */
/* Updated: 2025/03/11 16:34:10 by khais ### ########.fr */ /* Updated: 2025/03/11 17:26:49 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,5 +24,6 @@ void assert_pipelineequal(char *expected, t_cmdlist *cmd, int idx);
t_worddesc *create_single_word(char *str); t_worddesc *create_single_word(char *str);
void assert_pipeline_cmd_word(t_pipeline *pipeline, char *expected_word, int cmd_num, int word_num); void assert_pipeline_cmd_word(t_pipeline *pipeline, char *expected_word, int cmd_num, int word_num);
t_cmdgroup *parse_cmdgroup(char *input); t_cmdgroup *parse_cmdgroup(char *input);
void assert_cmdgroup_itemlistequal(char *expected_str, t_cmdgroup *got_cmd, int item_number);
#endif #endif