diff --git a/includes/parser/ast.h b/includes/parser/ast.h index 5455766..a04ab89 100644 --- a/includes/parser/ast.h +++ b/includes/parser/ast.h @@ -65,7 +65,18 @@ t_ast_n *get_ast(t_msh *msh, t_node *lst); t_nodell *cutll(t_node *lst, t_node *expected, size_t limiter); t_node *get_top_token(t_node *lst, t_state *state); -// env TMP -char **init_env(char **envp); +// recurce +t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent, t_msh *msh); +// redir +t_redir get_redir(t_node *node); +void create_redir(t_node *cpy, t_ast_n *self); +// cmd +void create_cmd(t_ast_n *self, t_node *lst); +// subsh +void create_subsh(t_ast_n *parent, t_node *lst, t_msh *msh); +// pipeline +void create_pline(t_ast_n *self, t_node *lst, t_node *token, t_msh *msh); +// and_or +void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token, t_msh *msh); #endif diff --git a/srcs/parsing/ast/and_or.c b/srcs/parsing/ast/and_or.c new file mode 100644 index 0000000..b4aa5ff --- /dev/null +++ b/srcs/parsing/ast/and_or.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* and_or.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/05 07:57:33 by nalebrun #+# #+# */ +/* Updated: 2025/02/05 07:57:33 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../../includes/minishell.h" + +void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token, t_msh *msh) +{ + t_nodell *nodell; + + nodell = cutll(lst, token, 1); + parrent->left = create_ast_n(nodell->node, parrent, msh); + parrent->right = create_ast_n(nodell->next->node, parrent, msh); + // free_lltab(sublsts); +} diff --git a/srcs/parsing/ast/ast.c b/srcs/parsing/ast/ast.c index c93e503..439cf13 100644 --- a/srcs/parsing/ast/ast.c +++ b/srcs/parsing/ast/ast.c @@ -13,246 +13,6 @@ #include "../../../includes/minishell.h" #include -// =================================================================== - -t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent, t_msh *msh); - -// ==================================================================== - - -void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token, t_msh *msh) -{ - t_nodell *nodell; - - nodell = cutll(lst, token, 1); - parrent->left = create_ast_n(nodell->node, parrent, msh); - parrent->right = create_ast_n(nodell->next->node, parrent, msh); - // free_lltab(sublsts); -} - - - -// =================================================================== - - - -t_redir get_redir(t_node *node) -{ - if (!node) - return (_NR); - else if (!ft_strncmp(node->val, ">>", 2)) - return (_RED_DR); - else if (!ft_strncmp(node->val, ">", 1)) - return (_RED_R); - else if (!ft_strncmp(node->val, "<<", 2)) - return (_RED_DL); - else if (!ft_strncmp(node->val, "<", 1)) - return (_RED_L); - return (_NR); -} - -char **lltotab(t_node *lst, t_node *limiter) -{ - char **out; - int count; - t_node *cpy; - - count = 0; - cpy = lst; - while (cpy && cpy != limiter) - { - count++; - cpy = cpy->next; - } - out = ft_calloc(count + 1, sizeof(char *)); - if (!out) - return (NULL); - cpy = lst; - count = 0; - while (cpy && cpy != limiter) - { - out[count] = ft_strdup(cpy->val); - count++; - cpy = cpy->next; - } - out[count] = NULL; - return (out); -} - -void add_redir(t_redir redir, t_redir **redir_list) -{ - int i; - int j; - t_redir *tmp; - - i = 0; - while ((*redir_list)[i] != _NR) - i++; - tmp = ft_calloc(i + 2, sizeof(t_redir)); - j = -1; - while ((*redir_list)[++j] != _NR) - tmp[j] = (*redir_list)[j]; - tmp[j++] = redir; - tmp[j] = _NR; - free(*redir_list); - *redir_list = tmp; -} - -void create_redir(t_node *cpy, t_ast_n *self) -{ - t_redir redir; - - while (cpy) - { - while (cpy && get_redir(cpy) == _NR) - cpy = cpy->next; - if (!cpy) - break; - redir = get_redir(cpy); - add_redir(redir, &self->redir); - add_to_tab(&self->files, cpy->next->val); - cpy = cpy->next; - while (cpy && cpy->next && get_redir(cpy) == _NR) - { - cpy = cpy->next; - } - } -} - -t_node *get_cmd(t_node *lst) -{ - t_node *cpy; - - cpy = lst; - while (cpy) - { - if (get_redir(cpy) != _NR && cpy->next && cpy->next->next && get_redir(cpy->next->next) == _NR) - return (cpy->next->next); - cpy = cpy->next; - } - return (lst); -} - -char **get_args(t_node *cmd) -{ - t_node *cpy; - - cpy = cmd; - while (cpy && cpy->next && !get_redir(cpy->next)) - cpy = cpy->next; - // create the arg list by skipping the redir and the command but everything else is an arg (ex : ls > test.txt -la) - return (lltotab(cmd, cpy->next)); -} - -void create_cmd(t_ast_n *self, t_node *lst) -{ - t_node *cmd_node; - - self->state = _CMD; - self->files = NULL; - self->redir = ft_calloc(1, sizeof(t_redir)); - self->redir[0] = _NR; - cmd_node = get_cmd(lst); - self->cmd = ft_strdup(cmd_node->val); - self->args = get_args(cmd_node); - create_redir(lst, self); - // debug - int i = -1; - while (self->files && self->files[++i]) - ft_debug("redir : [%d]%s\n",self->redir[i], self->files[i]); - ft_debug("====\n"); - i = -1; - while (self->args && self->args[++i]) - ft_debug("args : %s\n",self->args[i], self->args[i]); -} - - -//===================================================================== - - - -void create_pline(t_ast_n *self, t_node *lst, t_node *token, t_msh *msh) -{ - t_nodell *nodell; - t_nodell *cpy; - int count; - int i; - - nodell = cutll(lst, token, -1); - cpy = nodell; - count = 0; - while (cpy) - { - count++; - cpy = cpy->next; - } - self->pline = ft_calloc(count + 1, sizeof(t_ast_n *)); - cpy = nodell; - i = 0; - while (cpy) - { - self->pline[i] = create_ast_n(cpy->node, self, msh); - cpy = cpy->next; - i++; - } - cpy = NULL; - // free_lltab(sublsts); -} - -//====================================================================== - - - -void remove_parentheses(t_node **lst) -{ - t_node *tmp; - t_node *cpy; - - if (!lst || !*lst || !(*lst)->next) - return; - - tmp = *lst; - *lst = (*lst)->next; - free(tmp->val); - free(tmp); - cpy = *lst; - while (cpy->next && cpy->next->next) - cpy = cpy->next; - if (cpy->next) - { - free(cpy->next->val); - free(cpy->next); - cpy->next = NULL; - } -} - -void create_subsh(t_ast_n *parent, t_node *lst, t_msh *msh) -{ - t_node *cpy; - - cpy = lst; - while (cpy) - { - ft_printf("%s\n", cpy->val); - cpy = cpy->next; - } - remove_parentheses(&lst); - ft_printf("\n\n"); - cpy = lst; - while (cpy) - { - ft_printf("%s\n", cpy->val); - cpy = cpy->next; - } - parent->left = create_ast_n(lst, parent, msh); -} - - - -//================================================================== - - - t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent, t_msh *msh) { t_ast_n *node; diff --git a/srcs/parsing/ast/commands.c b/srcs/parsing/ast/commands.c new file mode 100644 index 0000000..bf596a7 --- /dev/null +++ b/srcs/parsing/ast/commands.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* commands.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/05 07:48:37 by nalebrun #+# #+# */ +/* Updated: 2025/02/05 07:48:37 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +# include "../../../includes/minishell.h" + +char **lltotab(t_node *lst, t_node *limiter) +{ + char **out; + int count; + t_node *cpy; + + count = 0; + cpy = lst; + while (cpy && cpy != limiter) + { + count++; + cpy = cpy->next; + } + out = ft_calloc(count + 1, sizeof(char *)); + if (!out) + return (NULL); + cpy = lst; + count = 0; + while (cpy && cpy != limiter) + { + out[count] = ft_strdup(cpy->val); + count++; + cpy = cpy->next; + } + out[count] = NULL; + return (out); +} + +char **get_args(t_node *start) +{ + t_node *cpy; + char **out; + + cpy = NULL; + while (start) + { + while (start && get_redir(start)) + start = start->next->next; + if (start) + { + add_node_back(&cpy, start->val, start->token, start->pressision); + start = start->next; + } + } + out = lltotab(cpy, NULL); + free_linked_list(cpy); + return (out); +} + +void create_cmd(t_ast_n *self, t_node *lst) +{ + char **cmd_args; + + self->state = _CMD; + self->files = NULL; + self->redir = ft_calloc(1, sizeof(t_redir)); + self->redir[0] = _NR; + cmd_args = get_args(lst); + self->args = cmd_args; + self->cmd = cmd_args[0]; + create_redir(lst, self); + // debug + int i = -1; + while (self->files && self->files[++i]) + ft_debug("redir : [%d]%s\n",self->redir[i], self->files[i]); + ft_debug("====\n"); + i = -1; + while (self->args && self->args[++i]) + ft_debug("args : %s\n",self->args[i], self->args[i]); +} diff --git a/srcs/parsing/ast/pipeline.c b/srcs/parsing/ast/pipeline.c new file mode 100644 index 0000000..793058b --- /dev/null +++ b/srcs/parsing/ast/pipeline.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipeline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/05 07:54:22 by nalebrun #+# #+# */ +/* Updated: 2025/02/05 07:54:22 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../../includes/minishell.h" + +void create_pline(t_ast_n *self, t_node *lst, t_node *token, t_msh *msh) +{ + t_nodell *nodell; + t_nodell *cpy; + int count; + int i; + + nodell = cutll(lst, token, -1); + cpy = nodell; + count = 0; + while (cpy) + { + count++; + cpy = cpy->next; + } + self->pline = ft_calloc(count + 1, sizeof(t_ast_n *)); + cpy = nodell; + i = 0; + while (cpy) + { + self->pline[i] = create_ast_n(cpy->node, self, msh); + cpy = cpy->next; + i++; + } + cpy = NULL; + // free_lltab(sublsts); +} diff --git a/srcs/parsing/ast/redirections.c b/srcs/parsing/ast/redirections.c new file mode 100644 index 0000000..bb76ce8 --- /dev/null +++ b/srcs/parsing/ast/redirections.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirections.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/05 07:38:26 by nalebrun #+# #+# */ +/* Updated: 2025/02/05 07:38:26 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../../includes/minishell.h" + +t_redir get_redir(t_node *node) +{ + if (!node) + return (_NR); + else if (!ft_strncmp(node->val, ">>", 2)) + return (_RED_DR); + else if (!ft_strncmp(node->val, ">", 1)) + return (_RED_R); + else if (!ft_strncmp(node->val, "<<", 2)) + return (_RED_DL); + else if (!ft_strncmp(node->val, "<", 1)) + return (_RED_L); + return (_NR); +} + +static void add_redir(t_redir redir, t_redir **redir_list) +{ + int i; + int j; + t_redir *tmp; + + i = 0; + while ((*redir_list)[i] != _NR) + i++; + tmp = ft_calloc(i + 2, sizeof(t_redir)); + j = -1; + while ((*redir_list)[++j] != _NR) + tmp[j] = (*redir_list)[j]; + tmp[j++] = redir; + tmp[j] = _NR; + free(*redir_list); + *redir_list = tmp; +} + +void create_redir(t_node *cpy, t_ast_n *self) +{ + t_redir redir; + + while (cpy) + { + while (cpy && get_redir(cpy) == _NR) + cpy = cpy->next; + if (!cpy) + break; + redir = get_redir(cpy); + add_redir(redir, &self->redir); + add_to_tab(&self->files, cpy->next->val); + cpy = cpy->next; + while (cpy && cpy->next && get_redir(cpy) == _NR) + { + cpy = cpy->next; + } + } +} diff --git a/srcs/parsing/ast/subsh.c b/srcs/parsing/ast/subsh.c new file mode 100644 index 0000000..2ff4831 --- /dev/null +++ b/srcs/parsing/ast/subsh.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* subsh.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/05 07:51:27 by nalebrun #+# #+# */ +/* Updated: 2025/02/05 07:51:27 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../../includes/minishell.h" + +static void remove_parentheses(t_node **lst) +{ + t_node *tmp; + t_node *cpy; + + if (!lst || !*lst || !(*lst)->next) + return; + + tmp = *lst; + *lst = (*lst)->next; + free(tmp->val); + free(tmp); + cpy = *lst; + while (cpy->next && cpy->next->next) + cpy = cpy->next; + if (cpy->next) + { + free(cpy->next->val); + free(cpy->next); + cpy->next = NULL; + } +} + +void create_subsh(t_ast_n *parent, t_node *lst, t_msh *msh) +{ + t_node *cpy; + + cpy = lst; + while (cpy) + { + ft_printf("%s\n", cpy->val); + cpy = cpy->next; + } + remove_parentheses(&lst); + ft_printf("\n\n"); + cpy = lst; + while (cpy) + { + ft_printf("%s\n", cpy->val); + cpy = cpy->next; + } + parent->left = create_ast_n(lst, parent, msh); +}