diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index c132a271..207cc49c 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -111,6 +111,7 @@ int add_branch(t_btree **ast, t_list **lst); int add_redir_word(t_btree **ast, t_list **lst); int add_redir_type(t_btree **ast, t_list **lst); int add_redir_condition(t_btree **ast, t_list **lst); +int redir_init(t_type type, t_redir *redir); int add_case_cmd(t_btree **ast, t_list **lst); int add_case_sep(t_btree **ast, t_list **lst); int add_pattern(t_btree **ast, t_list **lst); diff --git a/42sh/libft b/42sh/libft index b209bb1f..9382dc10 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit b209bb1fb718a68256253d5ab5ff69a46a90d5d6 +Subproject commit 9382dc10fdb91892ab26604a5776e5301ab88b71 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/exec/redirect_dless.c b/42sh/src/exec/redirect_dless.c index 5bcd3aa3..15c16342 100644 --- a/42sh/src/exec/redirect_dless.c +++ b/42sh/src/exec/redirect_dless.c @@ -18,11 +18,9 @@ int redirect_dless(t_redir *redir) pipe(fds); str = redir->heredoc_data; - DG("[%s] && adr %p", str, str); - DG("redir @ %p", redir); write(fds[PIPE_WRITE], str, ft_strlen(str)); close(fds[PIPE_WRITE]); - dup2(fds[PIPE_READ], 0); + dup2(fds[PIPE_READ], redir->n); close(fds[PIPE_READ]); return (0); } 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 e473017d..0dbd9636 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,6 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/13 16:36:42 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,6 +61,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) @@ -76,7 +76,7 @@ int handle_instruction(int fd) // if (ft_exec(&ast)) // return (1); instruction_free(&token, &parser, &ast); - if (SH_IS_INTERACTIVE(data_singleton()->opts)) + if (SH_IS_INTERACTIVE(data_singleton()->opts) && *lexer.str) ft_add_str_in_history(lexer.str); return (0); } diff --git a/42sh/src/parser/add_number.c b/42sh/src/parser/add_number.c index 49809151..d044c123 100644 --- a/42sh/src/parser/add_number.c +++ b/42sh/src/parser/add_number.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 17:28:31 by ariard #+# #+# */ -/* Updated: 2017/03/13 15:57:10 by ariard ### ########.fr */ +/* Updated: 2017/03/13 16:58:33 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,9 +44,8 @@ int add_ionumber(t_btree **ast, t_list **lst) node = (*ast)->item; node->cache = node->type; node->type = token->type; + redir_init(token->type, &redir); redir.n = ft_atoi(token->data); - redir.word = NULL; - redir.heredoc_data = NULL; ft_lsteadd(&node->data.cmd.redir, ft_lstnew(&redir, sizeof(redir))); return (0); } diff --git a/42sh/src/parser/add_redir.c b/42sh/src/parser/add_redir.c index fec9a2a6..870ad9f9 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 15:56:25 by ariard ### ########.fr */ +/* Updated: 2017/03/13 16:57:59 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -84,14 +84,27 @@ static int add_redir_type_number(t_btree **ast, t_list **lst) t_redir *redir; t_astnode *node; t_token *token; + t_list **queue; + t_list *redir_lst; - DG(); + queue = &data_singleton()->heredoc_queue; token = (*lst)->content; node = (*ast)->item; - redir = (ft_lstlast(node->data.cmd.redir))->content; + redir_lst = ft_lstlast(node->data.cmd.redir); + redir = redir_lst->content; redir->type = token->type; if (token->type == TK_DLESS) - ft_lsteadd(&data_singleton()->heredoc_queue, ft_lstlast(node->data.cmd.redir)); + ft_lsteadd(queue, ft_lstnew(&redir_lst->content, sizeof(t_redir*))); + 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); } @@ -100,23 +113,21 @@ 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)); - ft_lsteadd(&node->data.cmd.redir, temp); + redir_init(token->type, &redir); + redir_lst = ft_lstnew(&redir, sizeof(redir)); + ft_lsteadd(&node->data.cmd.redir, redir_lst); if (token->type == TK_DLESS) - ft_lsteadd(&data_singleton()->heredoc_queue, temp); + ft_lsteadd(queue, ft_lstnew(&redir_lst->content, sizeof(t_redir*))); } else add_redir_type_number(ast, lst); diff --git a/42sh/src/parser/heredoc_parser.c b/42sh/src/parser/heredoc_parser.c index e0cf26bd..542c270b 100644 --- a/42sh/src/parser/heredoc_parser.c +++ b/42sh/src/parser/heredoc_parser.c @@ -6,6 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 16:21:05 by ariard #+# #+# */ +/* Updated: 2017/03/13 16:57:08 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,24 +15,27 @@ int pop_heredoc(t_list **lst) { t_token *token; - t_list *temp; - t_redir *head; + t_list **head; + t_redir *redir; - temp = NULL; token = (*lst)->content; if (token->type == HEREDOCDATA && data_singleton()->heredoc_queue != NULL) { - temp = data_singleton()->heredoc_queue; - head = temp->content; + head = &data_singleton()->heredoc_queue; + redir = *(t_redir**)(*head)->content; if (head && token) { - if (ft_strcmp((char *)token->data, head->word) == 0) - ft_lst_removeif(&data_singleton()->heredoc_queue, temp->content, &ft_addrcmp); + /* 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) + { + 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);