From 07cf3a706661341ff61726a667835b7f7a898cfd Mon Sep 17 00:00:00 2001 From: gazhonsepaskwa Date: Sat, 15 Feb 2025 13:43:26 +0100 Subject: [PATCH] ast bigest fix + norm --- includes/exec/builtins.h | 2 +- includes/parser/heredoc.h | 2 ++ includes/parser/parsing.h | 7 ++-- run_vg.sh | 2 +- srcs/builtins/export.c | 2 +- srcs/builtins/export_utils.c | 7 ++-- srcs/parsing/ast/utils/top_token.c | 46 ++++++++++-------------- srcs/parsing/ast/utils/top_token_utils.c | 39 ++++++++++++++++++++ srcs/parsing/parser.c | 1 - srcs/parsing/syntax/syntax.c | 2 +- srcs/parsing/tokenizer/tokenizer.c | 14 -------- 11 files changed, 70 insertions(+), 54 deletions(-) create mode 100644 srcs/parsing/ast/utils/top_token_utils.c diff --git a/includes/exec/builtins.h b/includes/exec/builtins.h index 818fd9a..cdcfa91 100644 --- a/includes/exec/builtins.h +++ b/includes/exec/builtins.h @@ -39,7 +39,7 @@ int builtin_export(char **arg, t_ast_n *head); //UTILS int count_char(char *str); -int is_append(char *str); +int is_append(char *str); char **key_value(char *str); void set_new_export(char *str, t_ast_n *node); int count_args(char **tab); diff --git a/includes/parser/heredoc.h b/includes/parser/heredoc.h index 105f1b1..1167477 100644 --- a/includes/parser/heredoc.h +++ b/includes/parser/heredoc.h @@ -19,5 +19,7 @@ typedef struct s_msh t_msh; void read_hereinput(char *limiter, t_node *lst, t_msh *msh); void parse_heredoc(char *limiter, t_node *lst, t_msh *msh); void create_heredoc(t_node *lst, t_msh *msh); +void end_heredoc(char *buf, t_msh *msh, t_node *lst, char *limiter); +void exit_heredoc(char *limiter, t_msh *msh, t_node *lst); #endif diff --git a/includes/parser/parsing.h b/includes/parser/parsing.h index 381d567..9aa77d6 100644 --- a/includes/parser/parsing.h +++ b/includes/parser/parsing.h @@ -19,6 +19,8 @@ typedef struct s_msh t_msh; t_ast_n *parser(char *input, t_msh *msh); +void interpret_cmd(char **input, t_msh *msh); + int unexpected_token(t_node *node); int is_aop_operator(t_node *node); int is_redir(t_node *cpy); @@ -27,8 +29,7 @@ int syntax_err_mess(char *token_base, int selected); int check_unclosed(char *c, t_node *node); int check_unclosed_quote(char *c, t_node *node); -void interpret_cmd(char **input, t_msh *msh); -void end_heredoc(char *buf, t_msh *msh, t_node *lst, char *limiter); -void exit_heredoc(char *limiter, t_msh *msh, t_node *lst); +int last_tok_redir(t_node *lst); +int last_tok_subsh(t_node *lst); #endif diff --git a/run_vg.sh b/run_vg.sh index f5450c0..f37317c 100755 --- a/run_vg.sh +++ b/run_vg.sh @@ -1 +1 @@ -make && valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all --trace-children=yes --suppressions=valgrind.supp -s ./minishell +make && valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all --trace-children=yes --suppressions=/home/nalebrun/git/minishell/valgrind.supp -s ./minishell diff --git a/srcs/builtins/export.c b/srcs/builtins/export.c index b96427a..85f5091 100644 --- a/srcs/builtins/export.c +++ b/srcs/builtins/export.c @@ -32,7 +32,7 @@ int is_export_valid(char *str) while ((++str) < key_end) { if (!ft_isalnum(*str) && *str != '_') - return (0); + return (0); } return (1); } diff --git a/srcs/builtins/export_utils.c b/srcs/builtins/export_utils.c index 64d4830..1fb9404 100644 --- a/srcs/builtins/export_utils.c +++ b/srcs/builtins/export_utils.c @@ -12,12 +12,12 @@ #include "../../includes/minishell.h" -int is_append(char *str) +int is_append(char *str) { if (*(ft_strchr(str, '=') - 1) == '+') - return (1); + return (1); else - return (0); + return (0); } void set_new_export(char *str, t_ast_n *node) @@ -37,5 +37,4 @@ void set_new_export(char *str, t_ast_n *node) } else set_var_env(str, NULL, node->msh); - } diff --git a/srcs/parsing/ast/utils/top_token.c b/srcs/parsing/ast/utils/top_token.c index 49c603c..0e34c27 100644 --- a/srcs/parsing/ast/utils/top_token.c +++ b/srcs/parsing/ast/utils/top_token.c @@ -13,32 +13,6 @@ #include "../../../../includes/minishell.h" #include -static int last_tok_subsh(t_node *lst) -{ - while (lst) - { - if ((lst->next == NULL) && !ft_strncmp(lst->val, ")", 1)) - return (1); - lst = lst->next; - } - return (0); -} - -static int last_tok_redir(t_node *lst) -{ - while (lst) - { - if ((lst->next == NULL || lst->next->pressision == D_RED_R - || lst->next->pressision == RED_R - || lst->next->pressision == RED_L - || lst->next->pressision == HEREDOC) && !ft_strncmp(lst->val, - ")", 1)) - return (1); - lst = lst->next; - } - return (0); -} - static void skip_parentheses(t_node **lst) { if (!ft_strncmp((*lst)->val, "(", 1)) @@ -69,15 +43,31 @@ static t_node *find_token(char *tok, t_node *lst) return (NULL); } +static t_node *rfind_token(char *tok, t_node *lst) +{ + t_node *lst_it; + + lst_it = NULL; + while (lst) + { + skip_parentheses(&lst); + if (lst && !ft_strncmp(lst->val, tok, ft_strlen(tok))) + lst_it = lst; + if (lst) + lst = lst->next; + } + return (lst_it); +} + t_node *get_top_token(t_node *lst, t_state *state) { *state = _SUBSH; - if (find_token("&&", lst)) + if (rfind_token("&&", lst) > rfind_token("||", lst)) { *state = _AND; return (find_token("&&", lst)); } - else if (find_token("||", lst)) + else if (rfind_token("||", lst) > rfind_token("&&", lst)) { *state = _OR; return (find_token("||", lst)); diff --git a/srcs/parsing/ast/utils/top_token_utils.c b/srcs/parsing/ast/utils/top_token_utils.c new file mode 100644 index 0000000..0fec4d2 --- /dev/null +++ b/srcs/parsing/ast/utils/top_token_utils.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* top_token_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/15 13:28:47 by nalebrun #+# #+# */ +/* Updated: 2025/02/15 13:31:44 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../../../includes/minishell.h" + +int last_tok_subsh(t_node *lst) +{ + while (lst) + { + if ((lst->next == NULL) && !ft_strncmp(lst->val, ")", 1)) + return (1); + lst = lst->next; + } + return (0); +} + +int last_tok_redir(t_node *lst) +{ + while (lst) + { + if ((lst->next == NULL || lst->next->pressision == D_RED_R + || lst->next->pressision == RED_R + || lst->next->pressision == RED_L + || lst->next->pressision == HEREDOC) && !ft_strncmp(lst->val, + ")", 1)) + return (1); + lst = lst->next; + } + return (0); +} diff --git a/srcs/parsing/parser.c b/srcs/parsing/parser.c index a92accb..e5f5fb0 100644 --- a/srcs/parsing/parser.c +++ b/srcs/parsing/parser.c @@ -62,7 +62,6 @@ t_ast_n *parser(char *input, t_msh *msh) create_heredoc(lst, msh); if (DEBUG) dio = drawio_init(DIO_PATH); - // gen_dio_linked_list(lst, dio); ast = get_ast(msh, lst); debug_dio_ast(ast, dio); if (!ast) diff --git a/srcs/parsing/syntax/syntax.c b/srcs/parsing/syntax/syntax.c index 3926d9e..caddcb7 100644 --- a/srcs/parsing/syntax/syntax.c +++ b/srcs/parsing/syntax/syntax.c @@ -75,7 +75,7 @@ int syntax_error(t_node *head) return (syntax_err_mess(cpy->val, 0)); while (cpy) { - if (redir_error(cpy)) + if (redir_error(cpy)) return (1); if (parenthesis_error(cpy)) return (1); diff --git a/srcs/parsing/tokenizer/tokenizer.c b/srcs/parsing/tokenizer/tokenizer.c index 70b28dc..a79ca6f 100644 --- a/srcs/parsing/tokenizer/tokenizer.c +++ b/srcs/parsing/tokenizer/tokenizer.c @@ -116,31 +116,17 @@ t_node *tokenize(char *str) head = tokenize_base(str); if (!head) return (NULL); - // debug_token_list(head, "tokenizer base"); - if (!trim_nodes(head)) return (NULL); - // debug_token_list(head, "trim"); - if (!unstick_nodes(head)) return (NULL); - // debug_token_list(head, "unstick"); - stick_quote_node(head, 39); stick_quote_node(head, '"'); - // debug_token_list(head, "stick_quote_node"); - set_token(head); - // debug_token_list(head, "set token"); - if (!trim_nodes(head)) return (NULL); - // debug_token_list(head, "trim2"); - del_void_nodes(&head); - // debug_token_list(head, "del_void_nodes"); debug_token_list(head, "tokenizer"); - if (syntax_error(head)) return (free_linked_list(head), NULL); return (head);