From 2369795bd24faa0dd8c4701e1222a5bb05866c27 Mon Sep 17 00:00:00 2001 From: gazhonsepaskwa Date: Wed, 5 Feb 2025 13:44:43 +0100 Subject: [PATCH] subsh redir --- includes/parser/ast.h | 1 + srcs/parsing/ast/redirections.c | 61 ++++++++++++++++++++++++++------- srcs/parsing/ast/subsh.c | 22 ++++++++---- srcs/parsing/ast/top_token.c | 5 ++- 4 files changed, 69 insertions(+), 20 deletions(-) diff --git a/includes/parser/ast.h b/includes/parser/ast.h index a04ab89..a949eaf 100644 --- a/includes/parser/ast.h +++ b/includes/parser/ast.h @@ -70,6 +70,7 @@ 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); +void create_redir_subsh(t_node *head, t_ast_n *self); // cmd void create_cmd(t_ast_n *self, t_node *lst); // subsh diff --git a/srcs/parsing/ast/redirections.c b/srcs/parsing/ast/redirections.c index bb76ce8..c0fe0e6 100644 --- a/srcs/parsing/ast/redirections.c +++ b/srcs/parsing/ast/redirections.c @@ -6,7 +6,7 @@ /* By: nalebrun +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/05 07:38:26 by nalebrun #+# #+# */ -/* Updated: 2025/02/05 07:38:26 by nalebrun ### ########.fr */ +/* Updated: 2025/02/05 11:41:23 by nalebrun ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,23 +46,58 @@ static void add_redir(t_redir redir, t_redir **redir_list) *redir_list = tmp; } -void create_redir(t_node *cpy, t_ast_n *self) +void create_redir(t_node *head, t_ast_n *self) { t_redir redir; - while (cpy) + while (head) { - while (cpy && get_redir(cpy) == _NR) - cpy = cpy->next; - if (!cpy) + while (head && get_redir(head) == _NR) + head = head->next; + if (!head) break; - redir = get_redir(cpy); + redir = get_redir(head); 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; - } + add_to_tab(&self->files, head->next->val); + head = head->next; + while (head && head->next && get_redir(head) == _NR) + head = head->next; + } +} + +int in_parenthesis(t_node *head) +{ + int count; + + count = 0; + while (head) + { + if (head->pressision == SUBSH_S) + count += 1; + if (head->pressision == SUBSH_E) + count -= 1; + head = head->next; + } + if (count != 0) + return (1); + return (0); +} + +void create_redir_subsh(t_node *head, t_ast_n *self) +{ + t_redir redir; + + while (head) + { + while (head && (get_redir(head) == _NR || in_parenthesis(head))) + head = head->next; + if (!head) + break; + redir = get_redir(head); + add_redir(redir, &self->redir); + add_to_tab(&self->files, head->next->val); + head = head->next; + while (head && head->next && get_redir(head) == _NR) + head = head->next; } } diff --git a/srcs/parsing/ast/subsh.c b/srcs/parsing/ast/subsh.c index f2730fc..f4abed5 100644 --- a/srcs/parsing/ast/subsh.c +++ b/srcs/parsing/ast/subsh.c @@ -16,24 +16,34 @@ t_node *remove_parentheses(t_node *lst) { t_node *it; t_node *out; + int deepness; out = NULL; it = lst; - if (it->pressision == SUBSH_S) - it = it->next; - while (it->pressision != SUBSH_E) + it = it->next; + deepness = 1; + while (1) { + if (it->pressision == SUBSH_S) + deepness += 1; + if (it->pressision == SUBSH_E) + deepness -= 1; + if (deepness == 0) + break; add_node_back(&out, ft_strdup(it->val), it->token, it->pressision); it = it->next; } return (out); } -void create_subsh(t_ast_n *parent, t_node *lst, t_msh *msh) +void create_subsh(t_ast_n *self, t_node *lst, t_msh *msh) { - t_node *cpy; t_node *cutted; cutted = remove_parentheses(lst); - parent->left = create_ast_n(cutted, parent, msh); + self->left = create_ast_n(cutted, self, msh); + self->files = NULL; + self->redir = ft_calloc(1, sizeof(t_redir)); + self->redir[0] = _NR; + create_redir_subsh(lst, self); } diff --git a/srcs/parsing/ast/top_token.c b/srcs/parsing/ast/top_token.c index ad206df..8457d37 100644 --- a/srcs/parsing/ast/top_token.c +++ b/srcs/parsing/ast/top_token.c @@ -16,7 +16,10 @@ static int last_tok_subsh(t_node *lst) { while (lst) { - if (lst->next == NULL && !ft_strncmp(lst->val, ")", 1)) + if ((lst->next == NULL + || lst->next->pressision == D_RED_R + || lst->next->pressision == RED_R) + && !ft_strncmp(lst->val, ")", 1)) return (1); lst = lst->next; }