From 791b99e46966f59f1e3da9c791ecdf5fd09c7600 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Wed, 8 Mar 2017 23:18:55 +0100 Subject: [PATCH] last commit --- 42sh/Makefile | 1 + 42sh/includes/minishell.h | 3 ++- 42sh/includes/parser.h | 4 +++ 42sh/includes/types.h | 3 ++- 42sh/src/lexer/insert_newline.c | 3 +-- 42sh/src/lexer/lexer_end.c | 2 +- 42sh/src/lexer/lexer_heredoc.c | 9 +++++-- 42sh/src/lexer/lexer_word.c | 2 +- 42sh/src/main/data_init.c | 3 ++- 42sh/src/main/data_singleton.c | 2 +- 42sh/src/main/main.c | 17 ++++++++----- 42sh/src/parser/add_cmd.c | 9 +------ 42sh/src/parser/add_redir.c | 26 +++++++++++++------ 42sh/src/parser/aggregate_sym.c | 9 ++++--- 42sh/src/parser/eval_sym.c | 3 ++- 42sh/src/parser/ft_parse.c | 4 ++- 42sh/src/parser/heredoc_parser.c | 43 ++++++++++++++++++++++++++++++++ 42sh/src/parser/parser_init.c | 3 ++- 42sh/src/parser/produce_sym.c | 9 ++++--- 42sh/src/parser/read_stack.c | 4 ++- 20 files changed, 115 insertions(+), 44 deletions(-) create mode 100644 42sh/src/parser/heredoc_parser.c diff --git a/42sh/Makefile b/42sh/Makefile index 4196032e..46f6de4d 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -269,6 +269,7 @@ parser/build_tree.c\ parser/error_syntax.c\ parser/eval_sym.c\ parser/ft_parse.c\ +parser/heredoc_parser.c\ parser/parser_init.c\ parser/pop_stack.c\ parser/produce_sym.c\ diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 9b20a56e..6dcbefc4 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/03/08 12:05:37 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 20:53:04 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,6 +42,7 @@ struct s_data t_comp *comp; t_exec exec; t_jobc jobc; + t_list *heredoc_queue; char **local_var; t_list *lst_func; }; diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index a144c663..6e8a06ff 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -30,6 +30,7 @@ struct s_parser t_parstate state; t_list *stack; t_sym *new_sym; + t_list *heredoc_queue; }; struct s_aggrematch @@ -74,6 +75,8 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state); int push_stack(t_list **stack, t_sym new_sym); int pop_stack(t_list **stack, t_sym erase_sym); +int pop_heredoc(t_list **heredoc_queue, t_list **lst); + int error_syntax(t_list **token, t_parser *parser, t_btree **ast); int error_EOF(t_list **token, t_parser *parser, t_btree **ast); @@ -158,6 +161,7 @@ struct s_redir t_type type; int n; char *word; + char *heredoc_data; /* int close; */ }; diff --git a/42sh/includes/types.h b/42sh/includes/types.h index cf5b074f..15b72159 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/08 16:08:38 by ariard ### ########.fr */ +/* Updated: 2017/03/08 17:16:00 by ariard ### ########.fr */ /* Updated: 2017/03/07 18:35:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -193,6 +193,7 @@ enum e_sym REDIR, CMD, HEREDOCDATA, + HEREDOC_DELIM, ALL = 200, TERMINUS = 300, }; diff --git a/42sh/src/lexer/insert_newline.c b/42sh/src/lexer/insert_newline.c index 7a24a0ca..8cd60a2a 100644 --- a/42sh/src/lexer/insert_newline.c +++ b/42sh/src/lexer/insert_newline.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/21 21:05:23 by ariard #+# #+# */ -/* Updated: 2017/02/24 16:22:08 by ariard ### ########.fr */ +/* Updated: 2017/03/08 18:33:31 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,6 @@ int insert_newline(t_list **alst) { t_token *token; - DG("insert newline"); token = token_init(); token->type = TK_NEWLINE; ft_lsteadd(alst, ft_lstnew(token, sizeof(*token))); diff --git a/42sh/src/lexer/lexer_end.c b/42sh/src/lexer/lexer_end.c index 52a2bd3d..ec527e69 100644 --- a/42sh/src/lexer/lexer_end.c +++ b/42sh/src/lexer/lexer_end.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 16:58:24 by jhalford #+# #+# */ -/* Updated: 2017/03/08 15:52:31 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 17:52:05 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_heredoc.c b/42sh/src/lexer/lexer_heredoc.c index e655aee2..a1efef2d 100644 --- a/42sh/src/lexer/lexer_heredoc.c +++ b/42sh/src/lexer/lexer_heredoc.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 15:51:17 by jhalford #+# #+# */ -/* Updated: 2017/03/08 15:53:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 20:04:52 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,8 +18,13 @@ int lexer_heredoc(t_list **alst, t_lexer *lexer) token = (*alst)->content; token->type = HEREDOCDATA; - token_append_str(token, lexer->str, 0, 0); + //token_append_char(token, '\n', 0, 0); + while (lexer->str[lexer->pos]) + if (token_append_char(token, lexer->str[lexer->pos++], 0, 0)) + return (1); return (0); + //ft_strappend(&lexer->str, (char[]){'\n', 0}); + //lexer->pos++; /* heredoc_lst = *(t_list**)lexer->heredoc_stack->content; */ /* heredoc_tok = heredoc_lst->content; */ /* if (!(heredoc_lst->next)) */ diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index b3e8aea4..d322ee59 100644 --- a/42sh/src/lexer/lexer_word.c +++ b/42sh/src/lexer/lexer_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */ -/* Updated: 2017/03/08 12:10:37 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 19:07:20 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 843047d7..3bb1774b 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2017/03/08 14:39:07 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 21:02:29 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,7 @@ int data_init(void) data->local_var = NULL; data->lst_func = NULL; + data->heredoc_queue = NULL; if ((term_name = ft_getenv(data->env, "TERM")) == NULL) return (-1); if (tgetent(NULL, term_name) != 1) diff --git a/42sh/src/main/data_singleton.c b/42sh/src/main/data_singleton.c index e86abb8d..51fe1e00 100644 --- a/42sh/src/main/data_singleton.c +++ b/42sh/src/main/data_singleton.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 11:36:39 by jhalford #+# #+# */ -/* Updated: 2017/02/20 20:57:52 by ariard ### ########.fr */ +/* Updated: 2017/03/08 20:50:57 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index a65b3758..8079edab 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/08 16:06:44 by ariard ### ########.fr */ +/* Updated: 2017/03/08 21:58:19 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ int handle_instruction(int fd) while (1) { if ((ret = readline(fd, get_lexer_stack(lexer) || - parser.state == UNDEFINED, &str))) + parser.state == UNDEFINED || lexer.state == HEREDOC, &str))) { if (ret == -1) return (-1); @@ -52,10 +52,15 @@ int handle_instruction(int fd) token_print(token); if (insert_newline(&token)) return (1); - DG("exit newline"); if (ft_parse(&ast, &token, &parser)) continue ; - if (parser.state == SUCCESS) + DG(); + lexer.state = (data_singleton()->heredoc_queue) ? HEREDOC : 0; + if (data_singleton()->heredoc_queue) + DG("still in HEREDOC"); + if (lexer.state) + continue; + else if (parser.state == SUCCESS) break ; else if (parser.state == ERROR && SH_IS_INTERACTIVE(data_singleton()->opts)) { @@ -67,8 +72,8 @@ int handle_instruction(int fd) } DG("Before execution:"); // btree_print(STDBUG, ast, &ft_putast); - if (ft_exec(&ast)) - return (1); +// if (ft_exec(&ast)) +// return (1); instruction_free(&token, &parser, &ast); ft_add_str_in_history(lexer.str); return (0); diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 47eb3499..f7c8f491 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/07 20:10:10 by wescande ### ########.fr */ +/* Updated: 2017/03/08 16:19:23 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,13 +88,6 @@ int add_cmd(t_btree **ast, t_list **lst) else node->type = CMD; if (token->type == TK_WORD || token->type == TK_ASSIGNEMENT_WORD) -/* if ((my_tab = (char **)malloc(sizeof(char *) * 4))) - { - my_tab[0] = ft_strdup(token->data); - my_tab[1] = (char *)dup_char_esc(token->esc, token->size >> 3); - my_tab[2] = (char *)dup_char_esc(token->esc2, token->size >> 3); - my_tab[3] = NULL; - }*/ ft_ld_pushback(&node->data.cmd.token, gen_tab(token->data, token->esc, token->esc2, 1)); return (0); diff --git a/42sh/src/parser/add_redir.c b/42sh/src/parser/add_redir.c index 3040c067..b4747ba2 100644 --- a/42sh/src/parser/add_redir.c +++ b/42sh/src/parser/add_redir.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 16:39:05 by ariard #+# #+# */ -/* Updated: 2017/03/07 22:46:46 by ariard ### ########.fr */ +/* Updated: 2017/03/08 22:11:07 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,6 +60,7 @@ int add_redir_word(t_btree **ast, t_list **lst) t_astnode *node; t_token *token; t_redir *redir; + t_redir *temp; token = (*lst)->content; node = (*ast)->item; @@ -67,12 +68,13 @@ int add_redir_word(t_btree **ast, t_list **lst) { DG("add file"); redir = (ft_lstlast(node->data.cmd.redir))->content; - DG("now redir.type :%s", read_state(redir->type)); - DG("n is ? : %d", redir->n); + DG("now redir.type :%s", read_state(redir->type)); + redir->word = ft_strdup(token->data); if (redir->type == TK_DLESS) - redir->word = NULL; - else - redir->word = ft_strdup(token->data); + { + temp = ft_lstlast((data_singleton()->heredoc_queue))->content; + temp->word = redir->word; + } } return (0); } @@ -83,6 +85,7 @@ int add_redir_type(t_btree **ast, t_list **lst) t_token *token; t_redir redir; t_redir *temp; + t_redir *temp_heredoc; if (!*ast) gen_node(ast); @@ -90,16 +93,23 @@ int add_redir_type(t_btree **ast, t_list **lst) node = (*ast)->item; if (!(node->type == TK_IO_NUMBER)) { - DG("add redir"); redir.n = (token->type == TK_LESS || token->type == TK_DLESS || token->type == TK_LESSAND) ? STDIN : STDOUT; - redir.type = token->type; + redir.type = token->type; + redir.heredoc_data = NULL; ft_lsteadd(&node->data.cmd.redir, ft_lstnew(&redir, sizeof(redir))); + if (token->type == TK_DLESS) + ft_lsteadd(&data_singleton()->heredoc_queue, ft_lstnew(&redir, sizeof(redir))); } else { temp = (ft_lstlast(node->data.cmd.redir))->content; temp->type = token->type; + if (token->type == TK_DLESS) + { + temp_heredoc = data_singleton()->heredoc_queue->content; + temp_heredoc->n = temp->n; + } } node->type = REDIR; return (0); diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 65edcf13..a91f6a8c 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -103,6 +103,7 @@ t_aggrematch g_aggrematch[] = {NEWLINE_LIST, COMPLETE_COMMANDS, LINEBREAK, 0}, {NEWLINE_LIST, LINEBREAK, PROGRAM, LINEBREAK}, {NEWLINE_LIST, FOR_WORDLIST, SEQUENTIAL_SEP, 0}, + {NEWLINE_LIST, PROGRAM, PROGRAM, PROGRAM}, {SEQUENTIAL_SEP, FOR_WORDLIST, SEQUENTIAL_SEP, FOR_WORDLIST}, //to check {FILENAME, TK_LESS, IO_FILE, TK_LESS}, @@ -335,21 +336,21 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state) return (1); 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); 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)) { diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index 957c647d..5919b135 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -551,6 +551,7 @@ t_stackmatch g_stackmatch[] = {NEWLINE_LIST, TK_BANG}, {NEWLINE_LIST, TK_PAREN_OPEN}, {NEWLINE_LIST, TK_LBRACE}, + {NEWLINE_LIST, PROGRAM}, {HERE_END, TK_DLESS}, {HERE_END, TK_DLESS}, {HERE_END, TK_DLESSDASH}, @@ -1123,7 +1124,7 @@ int eval_sym(t_list **stack, t_sym new_sym) if (!*stack) return (1); 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 ee706c67..c949eb17 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -34,6 +34,8 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) { t_sym *head; + if (pop_heredoc(&parser->heredoc_queue, token)) + return (0); while (*token) { produce_sym(&parser->stack, parser->new_sym, token); @@ -53,7 +55,7 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) else parser->state = UNDEFINED; build_tree(ast, token); - btree_print(STDBUG, *ast, &ft_putast); +// btree_print(STDBUG, *ast, &ft_putast); if ((end_instruction(&parser->stack) && !(*token)->next)) insert_linebreak(token); else diff --git a/42sh/src/parser/heredoc_parser.c b/42sh/src/parser/heredoc_parser.c new file mode 100644 index 00000000..e76b7791 --- /dev/null +++ b/42sh/src/parser/heredoc_parser.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* heredoc_parser.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/08 16:21:05 by ariard #+# #+# */ +/* Updated: 2017/03/08 23:18:29 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int pop_heredoc(t_list **heredoc_queue, t_list **lst) +{ + t_token *token; + t_list *temp; + t_list *temp2; + t_redir *head; + + temp = NULL; + if (!heredoc_queue && !*heredoc_queue) + return (0); + token = (*lst)->content; + if (token->type == HEREDOCDATA) + { + head = data_singleton()->heredoc_queue->content; + temp = data_singleton()->heredoc_queue; + DG("compare %s with %s", (char *)token->data, head->word); + if (head && ft_strcmp((char *)token->data, head->word) == 0) + { + temp2 = temp->next; + free(temp); + data_singleton()->heredoc_queue = temp2; + } + else + head->heredoc_data = ft_strjoin(head->heredoc_data, token->data); + ft_lstdel(lst, &token_free); + return (1); + } + return (0); +} diff --git a/42sh/src/parser/parser_init.c b/42sh/src/parser/parser_init.c index 6b807a98..7e599c4f 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/06 17:04:33 by ariard ### ########.fr */ +/* Updated: 2017/03/08 21:56:56 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,4 +19,5 @@ void parser_init(t_parser *parser) parser->stack = NULL; push_stack(&parser->stack, TERMINUS); push_stack(&parser->stack, LINEBREAK); + parser->heredoc_queue = NULL; } diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index 6881847c..a4d17984 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/07 21:24:40 by wescande ### ########.fr */ +/* Updated: 2017/03/08 18:00:22 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -96,6 +96,7 @@ t_prodmatch g_prodmatch[] = {TK_NEWLINE, CONDITION, NEWLINE_LIST}, {TK_NEWLINE, FOR_WORDLIST, NEWLINE_LIST}, {TK_NEWLINE, SEQUENTIAL_SEP, NEWLINE_LIST}, + {TK_NEWLINE, PROGRAM, NEWLINE_LIST}, {TK_SEMI, MATH_SUP, SEPARATOR_OP}, {TK_SEMI, CMD_SUPERIOR, SEPARATOR_OP}, {TK_SEMI, LIST, SEPARATOR_OP}, @@ -118,8 +119,8 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst) return (1); 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) @@ -127,7 +128,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/src/parser/read_stack.c b/42sh/src/parser/read_stack.c index 4f28e16c..0e272f80 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 15:32:10 by ariard #+# #+# */ -/* Updated: 2017/03/08 00:23:48 by wescande ### ########.fr */ +/* Updated: 2017/03/08 17:39:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ char *read_state(t_sym current) { + if (current == HEREDOCDATA) + return ("HEREDOCDATA"); if (current == TERMINUS) return ("TERMINUS"); if (current == SEQUENCE)