From 8a4d1be7f40728064376d8bcba8159b5c0374990 Mon Sep 17 00:00:00 2001 From: gazhonsepaskwa Date: Tue, 4 Feb 2025 10:39:10 +0100 Subject: [PATCH] working on args --- includes/parser/ast.h | 10 +++--- lib/libft/libft.h | 1 + lib/libft/srcs/format/add_to_tab.c | 56 ++++++++++++++++++++++++++++++ srcs/parsing/ast/ast.c | 52 ++++++++++++++------------- 4 files changed, 91 insertions(+), 28 deletions(-) create mode 100644 lib/libft/srcs/format/add_to_tab.c diff --git a/includes/parser/ast.h b/includes/parser/ast.h index 92a62fa..a038600 100644 --- a/includes/parser/ast.h +++ b/includes/parser/ast.h @@ -31,7 +31,8 @@ typedef enum e_redir _NR, _RED_L, _RED_R, - _RED_DR + _RED_DR, + _RED_DL } t_redir; typedef struct s_ast_n @@ -48,9 +49,10 @@ typedef struct s_ast_n int _stdout; int _stdin; int save_std; - t_redir redir; - char *infile; - char *outfile; + t_redir *inredir; + t_redir *outredir; + char **infile; + char **outfile; bool sh; } t_ast_n; diff --git a/lib/libft/libft.h b/lib/libft/libft.h index 9712ee0..52af24d 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -68,6 +68,7 @@ char *ft_tabstr(char **tab); int is_charset(char c, char *set); void free_tab(char **tab); char **free_all(char **tab, int count); +void add_to_tab(char ***tab, char *str); void ft_put_c_fd(char c, int fd); void ft_put_s_fd(char *s, int fd); diff --git a/lib/libft/srcs/format/add_to_tab.c b/lib/libft/srcs/format/add_to_tab.c new file mode 100644 index 0000000..a1fe352 --- /dev/null +++ b/lib/libft/srcs/format/add_to_tab.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_to_tab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/04 09:59:01 by nalebrun #+# #+# */ +/* Updated: 2025/02/04 09:59:01 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +static void free_null_ptr(void *ptr) +{ + if (ptr != NULL) + { + free(ptr); + ptr = NULL; + } +} + +static char **add_space_to_tab(char **tab, int count) +{ + char **new_env; + int i; + + i = 0; + new_env = malloc(sizeof(char *) * (count + 1)); + if (!new_env) + return (NULL); + new_env[count] = NULL; + while (tab[i] && i < count) + { + new_env[i] = ft_strdup(tab[i]); + free_null_ptr(tab[i]); + i++; + } + free_null_ptr(tab); + return (new_env); +} + +void add_to_tab(char ***tab, char *str) +{ + char **tmp; + int i; + + tmp = *tab; + i = 0; + while ((*tab)[i]) + i++; + *tab = add_space_to_tab(*tab, i + 1); + free(tmp); + (*tab)[i] = ft_strdup(str); +} diff --git a/srcs/parsing/ast/ast.c b/srcs/parsing/ast/ast.c index c294ca1..a91bff1 100644 --- a/srcs/parsing/ast/ast.c +++ b/srcs/parsing/ast/ast.c @@ -36,7 +36,7 @@ void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token, t_msh *msh) -t_redir is_redir(t_node *node) +t_redir get_redir(t_node *node) { if (!node) return (_NR); @@ -76,41 +76,45 @@ char **lltotab(t_node *lst, t_node *limiter) out[count] = NULL; return (out); } +// +// void add_redir(t_redir redir, t_redir *redir_list) +// { +// } -t_node *get_redir_node(t_node *head) +void create_redir(t_node *cpy, t_ast_n *self) { - t_node *cpy; + t_redir redir; - cpy = head; - while (cpy && !is_redir(cpy)) + while (cpy) + { + redir = get_redir(cpy); + if (redir == _RED_R || redir == _RED_DR) + { + // add_redir(redir, self->outredir); + add_to_tab(&self->outfile, cpy->next->val); + } + else if (redir == _RED_L || redir == _RED_DL) + { + // add_redir(redir, self->inredir); + add_to_tab(&self->infile, cpy->next->val); + } cpy = cpy->next; - return (cpy); + while (cpy && get_redir(cpy) == _NR) + cpy = cpy->next; + } } void create_cmd(t_ast_n *self, t_node *lst) { - t_node *sec_redir_node; + t_node *cpy; self->state = _CMD; self->infile = NULL; self->outfile = NULL; - if (is_redir(lst) != _NR) - { - self->cmd = ft_strdup(lst->next->next->val); - self->args = lltotab(lst->next->next, NULL); - self->infile = ft_strdup(lst->next->val); - } - else - { - self->cmd = ft_strdup(lst->val); - self->args = lltotab(lst, get_redir_node(lst->next)); - } - sec_redir_node = get_redir_node(lst->next); - if (lst->next && sec_redir_node) - { - self->outfile = ft_strdup(sec_redir_node->next->val); - } - self->redir = is_redir(get_redir_node(lst)); + self->inredir = NULL; + self->outredir = NULL; + cpy = lst; + create_redir(cpy, self); }