From 0f684e618956c0898747546bb82e3755eb88b3b1 Mon Sep 17 00:00:00 2001 From: AntoHesse Date: Sat, 4 Mar 2017 02:51:52 +0100 Subject: [PATCH] clean instruction (token, parser, error) whatever succes or error, still issue lexing after syntax error --- 42sh/Makefile | 1 + 42sh/includes/minishell.h | 3 +++ 42sh/includes/parser.h | 4 ++-- 42sh/src/main/instruction_free.c | 9 +++++++++ 42sh/src/main/main.c | 10 +++++----- 42sh/src/parser/error_syntax.c | 17 ++++++++++++----- 6 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 42sh/src/main/instruction_free.c diff --git a/42sh/Makefile b/42sh/Makefile index fabb7a5a..e608d9a8 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -228,6 +228,7 @@ main/shell_exit.c\ main/shell_get_avdata.c\ main/shell_get_opts.c\ main/shell_init.c\ +main/instruction_free.c\ parser/add_case.c\ parser/add_cmd.c\ parser/add_condition.c\ diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 3b482e3c..383a14c9 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -78,6 +78,9 @@ void shell_exit(void); int data_init(void); void data_exit(void); +int instruction_free(t_list **token, t_parser *parser, + t_btree **ast); + char *ft_putast(void *node); void ft_putast2(void *node); diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 49220b89..296de442 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -89,8 +89,8 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state); int push_stack(t_sym *stack, t_sym new_sym); int pop_stack(t_sym **stack, t_sym erase_sym); */ -int error_syntax(t_list **token); -int error_EOF(void); +int error_syntax(t_list **token, t_parser *parser, t_btree **ast); +int error_EOF(t_list **token, t_parser *parser, t_btree **ast); int ft_read_stack(t_sym *stack); char *read_state(t_sym current); diff --git a/42sh/src/main/instruction_free.c b/42sh/src/main/instruction_free.c new file mode 100644 index 00000000..4864e2ce --- /dev/null +++ b/42sh/src/main/instruction_free.c @@ -0,0 +1,9 @@ +#include "parser.h" + +int instruction_free(t_list **token, t_parser *parser, t_btree **ast) +{ + ft_lstdel(token, &token_free); + ft_lstdel(&parser->stack, NULL); + btree_del(ast, &ast_free); + return (0); +} diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index fddbc046..9a53abbc 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -33,7 +33,8 @@ int handle_instruction(int fd) { if (ret == -1) return (-1); - return (parser.state == UNDEFINED ? error_EOF() : 1); + return (parser.state == UNDEFINED ? error_EOF(&token, + &parser, &ast) : 1); } ft_strappend(&lexer.str, str); if (get_lexer_stack(lexer) == BACKSLASH) @@ -56,16 +57,15 @@ int handle_instruction(int fd) if (parser.state == SUCCESS) break ; else if (parser.state == ERROR && !SH_IS_INTERACTIVE(data_singleton()->opts)) - return (error_syntax(&token)); + return (error_syntax(&token, &parser, &ast)); else if (parser.state == ERROR) - error_syntax(&token); - token = NULL; + error_syntax(&token, &parser, &ast); } DG("Before execution:"); btree_print(STDBUG, ast, &ft_putast); if (ft_exec(&ast)) return (1); - btree_del(&ast, &ast_free); + instruction_free(&token, &parser, &ast); ft_add_str_in_history(lexer.str); return (0); } diff --git a/42sh/src/parser/error_syntax.c b/42sh/src/parser/error_syntax.c index 13100924..470c3b87 100644 --- a/42sh/src/parser/error_syntax.c +++ b/42sh/src/parser/error_syntax.c @@ -50,13 +50,16 @@ t_errormatch g_errormatch[] = {0, NULL}, }; -int error_syntax(t_list **lst) +int error_syntax(t_list **lst, t_parser *parser, + t_btree **ast) { t_token *token; int i; - + int temp; + token = (*lst)->content; i = 0; + temp = 0; while (g_errormatch[i].token) { if (g_errormatch[i].token == token->type) @@ -64,16 +67,20 @@ int error_syntax(t_list **lst) ft_putstr_fd("syntax error near unexpected token `", 2); ft_putstr_fd(g_errormatch[i].error, 2); ft_putstr_fd("'\n", 2); - return (1); + temp = 1; + break ; } i++; } - ft_putstr_fd("grammar error, notify ariard", 2); + instruction_free(lst, parser, ast); + if (temp == 0) + ft_putstr_fd("grammar error, notify ariard", 2); return (1); } -int error_EOF(void) +int error_EOF(t_list **lst, t_parser *parser, t_btree **ast) { ft_putstr_fd("syntax error near unexpected EOF", 2); + instruction_free(lst, parser, ast); return (1); }