From 8873f31f7935ac6310598769773bb7ed506ae6b0 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Mon, 6 Mar 2017 17:57:51 +0100 Subject: [PATCH] protection stack symbolique --- 42sh/fi | 16 ------ 42sh/file | 15 ------ 42sh/includes/lexer.h | 9 +++- 42sh/includes/types.h | 3 +- 42sh/src/lexer/get_reserved_words.c | 79 +++++++++++++++++------------ 42sh/src/main/main.c | 4 +- 42sh/src/parser/add_cmd.c | 2 +- 42sh/src/parser/aggregate_sym.c | 11 ++-- 42sh/src/parser/error_syntax.c | 2 +- 42sh/src/parser/eval_sym.c | 2 +- 42sh/src/parser/ft_parse.c | 5 +- 42sh/src/parser/parser_init.c | 5 +- 42sh/src/parser/pop_stack.c | 17 ++++--- 42sh/src/parser/produce_sym.c | 8 +-- 42sh/yolo | 0 15 files changed, 87 insertions(+), 91 deletions(-) delete mode 100644 42sh/fi delete mode 100644 42sh/file delete mode 100644 42sh/yolo diff --git a/42sh/fi b/42sh/fi deleted file mode 100644 index 7caa40de..00000000 --- a/42sh/fi +++ /dev/null @@ -1,16 +0,0 @@ -42sh -Makefile -STDBUG -TESTSHELL -donovan_segaults_06-02 -fi -file -includes -libft -objs -parser_init.c -pdf -sample -src -test_framework.sh -update_makefile.sh diff --git a/42sh/file b/42sh/file deleted file mode 100644 index 9bda8fd6..00000000 --- a/42sh/file +++ /dev/null @@ -1,15 +0,0 @@ -42sh -Makefile -STDBUG -TESTSHELL -donovan_segaults_06-02 -file -includes -libft -objs -parser_init.c -pdf -sample -src -test_framework.sh -update_makefile.sh diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 74fd80bc..9cd38208 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/03/05 17:29:09 by jhalford ### ########.fr */ +/* Updated: 2017/03/06 17:52:13 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,6 +63,13 @@ struct s_lexer t_list *heredoc_stack; }; +struct s_rvwords +{ + char *word; + int type; +}; + +extern t_rvwords g_rvwords[]; extern int (*g_lexer[])(t_list **alst, t_lexer *lexer); diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 68f377fc..ddeed664 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/03/06 15:31:28 by ariard ### ########.fr */ +/* Updated: 2017/03/06 17:51:59 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ typedef enum e_mode t_mode; typedef struct s_lexer t_lexer; typedef enum e_lexstate t_lexstate; typedef struct s_token t_token; +typedef struct s_rvwords t_rvwords; typedef struct s_ld t_ld; typedef struct s_astnode t_astnode; diff --git a/42sh/src/lexer/get_reserved_words.c b/42sh/src/lexer/get_reserved_words.c index 139d4c1f..6f29ae61 100644 --- a/42sh/src/lexer/get_reserved_words.c +++ b/42sh/src/lexer/get_reserved_words.c @@ -6,62 +6,77 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 00:07:05 by ariard #+# #+# */ -/* Updated: 2017/02/21 21:06:16 by ariard ### ########.fr */ +/* Updated: 2017/03/06 17:56:14 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "lexer.h" -int get_reserved_words(t_list **alst) + +t_rvwords g_rvwords[] = +{ + {"while", TK_WHILE}, + {"done", TK_DONE}, + {"do", TK_DO}, + {"if", TK_IF}, + {"then", TK_THEN}, + {"fi", TK_FI}, + {"elif", TK_ELIF}, + {"else", TK_ELSE}, + {"until", TK_UNTIL}, + {"case", TK_CASE}, + {"esac", TK_ESAC}, + {"for", TK_FOR}, + {"null", 0}, +}; + + +static int recognization_rvwords(t_token *pv_tk) +{ + if (!pv_tk || (pv_tk->type == TK_NEWLINE || pv_tk->type == TK_AMP + || pv_tk->type == TK_SEMI || pv_tk->type == TK_WHILE + || pv_tk->type == TK_DONE || pv_tk->type == TK_DO + || pv_tk->type == TK_IF || pv_tk->type == TK_FI || pv_tk->type == TK_THEN + || pv_tk->type == TK_ELIF || pv_tk->type == TK_ELSE)) + return (1); + return (0); +} + +int get_reserved_words(t_list **alst) { t_token *token; - t_token *previous_token; + t_token *pv_tk; t_token *ante_token; t_list *temp; + int i; temp = *alst; - previous_token = NULL; + pv_tk = NULL; ante_token = NULL; while (temp) { token = temp->content; - if (!previous_token || (previous_token->type & RW_SEP)) + //no more comp & + if (recognization_rvwords(pv_tk)) { if (token->type == TK_WORD) { - if (ft_strncmp(token->data, "while", 5) == 0) - token->type = TK_WHILE; - else if (ft_strncmp(token->data, "done", 4) == 0) - token->type = TK_DONE; - else if (ft_strncmp(token->data, "do" , 2) == 0) - token->type = TK_DO; - else if (ft_strncmp(token->data, "if", 2) == 0) - token->type = TK_IF; - else if (ft_strncmp(token->data, "then", 4) == 0) - token->type = TK_THEN; - else if(ft_strncmp(token->data, "fi", 4) == 0) - token->type = TK_FI; - else if (ft_strncmp(token->data, "elif", 4) == 0) - token->type = TK_ELIF; - else if (ft_strncmp(token->data, "else", 4) == 0) - token->type = TK_ELSE; - else if (ft_strncmp(token->data, "until", 5) == 0) - token->type = TK_UNTIL; - else if (ft_strncmp(token->data, "case", 4) == 0) - token->type = TK_CASE; - else if (ft_strncmp(token->data, "esac", 4) == 0) - token->type = TK_ESAC; - else if (ft_strncmp(token->data, "for", 3) == 0) - token->type = TK_FOR; + i = 0; + while (g_rvwords[i].type) + { + if (ft_strcmp(token->data, g_rvwords[i].word) == 0) + token->type = g_rvwords[i].type; + i++; + } } } if (ante_token && (ante_token->type == TK_CASE || ante_token->type == TK_FOR) && ft_strncmp(token->data, "in", 2) == 0) token->type = TK_IN; - if (previous_token && previous_token->type == TK_FOR && token->type == TK_WORD) + if (pv_tk && pv_tk->type == TK_FOR && token->type == TK_WORD) token->type = TK_NAME; - ante_token = previous_token; - previous_token = token; + ante_token = pv_tk; + pv_tk = token; temp = temp->next; } return (0); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 875a3cf7..cf5038e7 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/05 16:47:43 by ariard ### ########.fr */ +/* Updated: 2017/03/06 17:55:29 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,9 +47,9 @@ int handle_instruction(int fd) if (get_lexer_stack(lexer) > 1) continue ; lexer.state = DEFAULT; - token_print(token); if (get_reserved_words(&token)) return (1); + token_print(token); if (insert_newline(&token)) return (1); if (ft_parse(&ast, &token, &parser)) diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 8a1f255a..489ec97f 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 20:49:15 by ariard #+# #+# */ -/* Updated: 2017/03/06 15:57:48 by ariard ### ########.fr */ +/* Updated: 2017/03/06 16:40:03 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 93cfc4fe..4bcd5f45 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -316,21 +316,22 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state) i = 0; head = (*stack)->content; -// DG("aggregate head %s && sym %s", -// read_state(*head), read_state(*new_sym)); + DG("aggregate head %s && sym %s", + read_state(*head), read_state(*new_sym)); while (g_aggrematch[i].top) { if (*new_sym == g_aggrematch[i].top && MATCH_STACK(*head, g_aggrematch[i].under)) { -// DG("MATCH : %s", read_state(g_aggrematch[i].new_sym)); + DG("MATCH : %s", read_state(g_aggrematch[i].new_sym)); *new_sym = g_aggrematch[i].new_sym; if (g_aggrematch[i].erase_sym) { - pop_stack(stack, g_aggrematch[i].erase_sym); + if (pop_stack(stack, g_aggrematch[i].erase_sym)) + return (1); head = (*stack)->content; -// DG("stack after pop: %s", read_state(*head)); + DG("stack after pop: %s", read_state(*head)); } if (eval_sym(stack, *new_sym)) return ((*state = ERROR)); diff --git a/42sh/src/parser/error_syntax.c b/42sh/src/parser/error_syntax.c index 408d3afd..161b1ac3 100644 --- a/42sh/src/parser/error_syntax.c +++ b/42sh/src/parser/error_syntax.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:15:35 by ariard #+# #+# */ -/* Updated: 2017/03/05 18:23:57 by ariard ### ########.fr */ +/* Updated: 2017/03/06 17:50:43 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index 8f0b9597..f17ae9c4 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -1027,7 +1027,7 @@ int eval_sym(t_list **stack, t_sym new_sym) int i; head = (*stack)->content; -// DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); + DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); i = 0; while (g_stackmatch[i].top) { diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index b959f9c5..f87d0bcb 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -37,12 +37,13 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) while (*token) { produce_sym(&parser->stack, parser->new_sym, token); -// DG("new sym %s", read_state(*parser->new_sym)); + DG("new sym %s", read_state(*parser->new_sym)); if (eval_sym(&parser->stack, *parser->new_sym)) return ((parser->state = ERROR)); else { - aggregate_sym(&parser->stack, parser->new_sym, &parser->state); + if (aggregate_sym(&parser->stack, parser->new_sym, &parser->state)) + return (1); push_stack(&parser->stack, *parser->new_sym); } // ft_read_stack(parser->stack); diff --git a/42sh/src/parser/parser_init.c b/42sh/src/parser/parser_init.c index 9d1f40dc..1c884a72 100644 --- a/42sh/src/parser/parser_init.c +++ b/42sh/src/parser/parser_init.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/21 16:14:04 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:28:00 by ariard ### ########.fr */ +/* Updated: 2017/03/06 17:04:33 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,8 +16,7 @@ void parser_init(t_parser *parser) { parser->state = SUCCESS; parser->new_sym = ft_memalloc(sizeof(t_sym)); + parser->stack = NULL; push_stack(&parser->stack, TERMINUS); push_stack(&parser->stack, LINEBREAK); -// parser->stack = ft_memalloc(sizeof(t_sym) * 1000); -// push_stack(parser->stack, LINEBREAK); } diff --git a/42sh/src/parser/pop_stack.c b/42sh/src/parser/pop_stack.c index b2b8c9d7..1552953c 100644 --- a/42sh/src/parser/pop_stack.c +++ b/42sh/src/parser/pop_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 19:12:44 by ariard #+# #+# */ -/* Updated: 2017/03/05 16:28:52 by ariard ### ########.fr */ +/* Updated: 2017/03/06 17:09:00 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,16 +17,19 @@ int pop_stack(t_list **stack, t_sym erase_sym) t_sym *head; t_list *temp; - head = (*stack)->content; - while ((*stack) && *head != erase_sym) + while ((*stack) && *(head = (*stack)->content) != erase_sym) + { + temp = *stack; + (*stack) = (*stack)->next; + ft_lstdelone(&temp, NULL); + } + if (*stack) { temp = *stack; (*stack) = (*stack)->next; ft_lstdelone(&temp, NULL); - head = (*stack)->content; } - temp = *stack; - (*stack) = (*stack)->next; - ft_lstdelone(&temp, NULL); + else + return (1); return (0); } diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index c2302123..c7432e03 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ -/* Updated: 2017/03/05 15:12:59 by ariard ### ########.fr */ +/* Updated: 2017/03/06 16:43:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -107,8 +107,8 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst) token = (*lst)->content; head = (*stack)->content; -// DG("produce stack : %s && token : %s", read_state(*head), -// read_state(token->type)); + DG("produce stack : %s && token : %s", read_state(*head), + read_state(token->type)); i = 0; *new_sym = 0; while (g_prodmatch[i].new_sym) @@ -116,7 +116,7 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst) if (token->type == g_prodmatch[i].token && *head == g_prodmatch[i].stack) { -// DG("MATCH : %s", read_state(g_prodmatch[i].new_sym)); + DG("MATCH : %s", read_state(g_prodmatch[i].new_sym)); *new_sym = g_prodmatch[i].new_sym; } i++; diff --git a/42sh/yolo b/42sh/yolo deleted file mode 100644 index e69de29b..00000000