From 6b7897180768a7a5206500cbb66179cc538e0457 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 13 Mar 2017 16:11:46 +0100 Subject: [PATCH] heredoc changes --- 42sh/src/builtin/builtin_exit.c | 2 +- 42sh/src/job-control/job_update_status.c | 2 +- 42sh/src/job-control/job_wait.c | 2 +- 42sh/src/lexer/lexer_lex.c | 2 +- 42sh/src/lexer/token_init.c | 2 +- 42sh/src/main/main.c | 3 +- 42sh/src/parser/add_redir.c | 48 ++++++++++++++---------- 42sh/src/parser/heredoc_parser.c | 28 ++++++-------- 8 files changed, 48 insertions(+), 41 deletions(-) diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index f00c59da..c4b4fd7f 100644 --- a/42sh/src/builtin/builtin_exit.c +++ b/42sh/src/builtin/builtin_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */ -/* Updated: 2017/03/13 14:47:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 14:52:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_update_status.c b/42sh/src/job-control/job_update_status.c index 329e0645..c70d1f75 100644 --- a/42sh/src/job-control/job_update_status.c +++ b/42sh/src/job-control/job_update_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:56:11 by jhalford #+# #+# */ -/* Updated: 2017/03/11 14:17:06 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 14:53:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c index 15305beb..ab75673e 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 11:49:05 by jhalford #+# #+# */ -/* Updated: 2017/03/13 13:22:22 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 15:29:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_lex.c b/42sh/src/lexer/lexer_lex.c index ec448add..14faa960 100644 --- a/42sh/src/lexer/lexer_lex.c +++ b/42sh/src/lexer/lexer_lex.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:08:51 by jhalford #+# #+# */ -/* Updated: 2017/03/09 14:29:15 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 15:41:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/token_init.c b/42sh/src/lexer/token_init.c index 3e58f078..a601f73b 100644 --- a/42sh/src/lexer/token_init.c +++ b/42sh/src/lexer/token_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 15:30:25 by jhalford #+# #+# */ -/* Updated: 2017/03/08 13:21:55 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 15:41:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index d186be71..2598ac89 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/13 14:47:13 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 16:02:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,6 +62,7 @@ int handle_instruction(int fd) if (parser.state == ERROR) error_syntax(&token, &parser, &ast); lexer.state = data_singleton()->heredoc_queue ? HEREDOC : 0; + DG("lexer.state=%i", lexer.state); if (lexer.state) continue; else if (parser.state == SUCCESS) diff --git a/42sh/src/parser/add_redir.c b/42sh/src/parser/add_redir.c index 8f97f8db..071e177d 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/13 13:06:20 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 16:11:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,7 +65,7 @@ int add_redir_word(t_btree **ast, t_list **lst) t_astnode *node; t_token *token; t_redir *redir; - t_redir *temp; + /* t_redir *temp; */ token = (*lst)->content; node = (*ast)->item; @@ -73,11 +73,12 @@ int add_redir_word(t_btree **ast, t_list **lst) { redir = (ft_lstlast(node->data.cmd.redir))->content; redir->word = ft_strdup(token->data); - if (redir->type == TK_DLESS) - { - temp = ft_lstlast((data_singleton()->heredoc_queue))->content; - temp->word = redir->word; - } + /* DG("redir1 @ %p word=[%s]", redir, redir->word); */ + /* if (redir->type == TK_DLESS) */ + /* { */ + /* temp = ft_lstlast((data_singleton()->heredoc_queue))->content; */ + /* temp->word = redir->word; */ + /* } */ } return (0); } @@ -101,31 +102,40 @@ static int add_redir_type_number(t_btree **ast, t_list **lst) return (0); } +int redir_init(t_type type, t_redir *redir) +{ + redir->n = (type == TK_LESS || type == TK_DLESS + || type == TK_LESSAND) ? STDIN : STDOUT; + redir->type = type; + redir->heredoc_data = NULL; + redir->word = NULL; + return (0); +} + int add_redir_type(t_btree **ast, t_list **lst) { t_astnode *node; t_token *token; t_redir redir; - t_list *temp; + t_list *redir_lst; + t_list **queue; + queue = &data_singleton()->heredoc_queue; if (!*ast) gen_node(ast); token = (*lst)->content; node = (*ast)->item; if (!(node->type == TK_IO_NUMBER)) { - redir.n = (token->type == TK_LESS || token->type == TK_DLESS - || token->type == TK_LESSAND) ? STDIN : STDOUT; - redir.type = token->type; - redir.heredoc_data = NULL; - redir.word = NULL; - temp = ft_lstnew(&redir, sizeof(redir)); - DG("adr is %p", temp); - ft_lsteadd(&node->data.cmd.redir, temp); - DG("adr is %p", node->data.cmd.redir); + redir_init(token->type, &redir); + redir_lst = ft_lstnew(&redir, sizeof(redir)); + ft_lsteadd(&node->data.cmd.redir, redir_lst); + /* DG("redir1 @ %p word=[%s]", redir_lst->content, ((t_redir*)redir_lst->content)->word); */ if (token->type == TK_DLESS) - ft_lsteadd(&data_singleton()->heredoc_queue, temp); - DG("adr is %p", data_singleton()->heredoc_queue); + { + ft_lsteadd(queue, ft_lstnew(&redir_lst->content, sizeof(t_redir*))); + /* DG("redir2 @ %p word=[%s]", *(t_redir**)(*queue)->content, (*(t_redir**)(*queue)->content)->word); */ + } } else add_redir_type_number(ast, lst); diff --git a/42sh/src/parser/heredoc_parser.c b/42sh/src/parser/heredoc_parser.c index 31d3fd24..27704b71 100644 --- a/42sh/src/parser/heredoc_parser.c +++ b/42sh/src/parser/heredoc_parser.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 16:21:05 by ariard #+# #+# */ -/* Updated: 2017/03/13 14:01:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 16:10:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,31 +15,27 @@ int pop_heredoc(t_list **lst) { t_token *token; - t_list *temp; - t_list *temp2; - t_redir *head; + t_list **head; + t_redir *redir; - temp = NULL; token = (*lst)->content; if (token->type == HEREDOCDATA && data_singleton()->heredoc_queue != NULL) { - head = data_singleton()->heredoc_queue->content; - temp = data_singleton()->heredoc_queue; + head = &data_singleton()->heredoc_queue; + redir = *(t_redir**)(*head)->content; if (head && token) { - if (ft_strcmp((char *)token->data, head->word) == 0) + /* DG("comparing w/ input [%s], head @ %p", token->data, *head); */ + /* DG("redir1 @ %p word=[%s]", redir, redir->word); */ + if (ft_strcmp((char *)token->data, redir->word) == 0) { - temp2 = temp->next; -// free(temp); - data_singleton()->heredoc_queue = temp2; - DG("data is %s et adr %p", head->heredoc_data, head->heredoc_data); - DG("redir @ %p", head); + ft_lst_delif(head, (t_redir**)(*head)->content, ft_addrcmp, ft_lst_cfree); + /* DG("matched heredoc EOF, new head @ %p", *head); */ } else { - head->heredoc_data = ft_strjoin(head->heredoc_data, - token->data); - head->heredoc_data = ft_strjoin(head->heredoc_data, "\n"); + redir->heredoc_data = ft_str3join(redir->heredoc_data, + (char*)token->data, "\n"); } } ft_lstdel(lst, &token_free);