heredoc changes

This commit is contained in:
Jack Halford 2017-03-13 16:11:46 +01:00
parent 163c93c0c2
commit 6b78971807
8 changed files with 48 additions and 41 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:28:41 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 12:56:11 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 11:49:05 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:08:51 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 15:30:25 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 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) if (parser.state == ERROR)
error_syntax(&token, &parser, &ast); error_syntax(&token, &parser, &ast);
lexer.state = data_singleton()->heredoc_queue ? HEREDOC : 0; lexer.state = data_singleton()->heredoc_queue ? HEREDOC : 0;
DG("lexer.state=%i", lexer.state);
if (lexer.state) if (lexer.state)
continue; continue;
else if (parser.state == SUCCESS) else if (parser.state == SUCCESS)

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 16:39:05 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_astnode *node;
t_token *token; t_token *token;
t_redir *redir; t_redir *redir;
t_redir *temp; /* t_redir *temp; */
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; 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 = (ft_lstlast(node->data.cmd.redir))->content;
redir->word = ft_strdup(token->data); redir->word = ft_strdup(token->data);
if (redir->type == TK_DLESS) /* 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; /* temp = ft_lstlast((data_singleton()->heredoc_queue))->content; */
} /* temp->word = redir->word; */
/* } */
} }
return (0); return (0);
} }
@ -101,31 +102,40 @@ static int add_redir_type_number(t_btree **ast, t_list **lst)
return (0); 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) int add_redir_type(t_btree **ast, t_list **lst)
{ {
t_astnode *node; t_astnode *node;
t_token *token; t_token *token;
t_redir redir; t_redir redir;
t_list *temp; t_list *redir_lst;
t_list **queue;
queue = &data_singleton()->heredoc_queue;
if (!*ast) if (!*ast)
gen_node(ast); gen_node(ast);
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; node = (*ast)->item;
if (!(node->type == TK_IO_NUMBER)) if (!(node->type == TK_IO_NUMBER))
{ {
redir.n = (token->type == TK_LESS || token->type == TK_DLESS redir_init(token->type, &redir);
|| token->type == TK_LESSAND) ? STDIN : STDOUT; redir_lst = ft_lstnew(&redir, sizeof(redir));
redir.type = token->type; ft_lsteadd(&node->data.cmd.redir, redir_lst);
redir.heredoc_data = NULL; /* DG("redir1 @ %p word=[%s]", redir_lst->content, ((t_redir*)redir_lst->content)->word); */
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);
if (token->type == TK_DLESS) 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 else
add_redir_type_number(ast, lst); add_redir_type_number(ast, lst);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 16:21:05 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) int pop_heredoc(t_list **lst)
{ {
t_token *token; t_token *token;
t_list *temp; t_list **head;
t_list *temp2; t_redir *redir;
t_redir *head;
temp = NULL;
token = (*lst)->content; token = (*lst)->content;
if (token->type == HEREDOCDATA && data_singleton()->heredoc_queue != NULL) if (token->type == HEREDOCDATA && data_singleton()->heredoc_queue != NULL)
{ {
head = data_singleton()->heredoc_queue->content; head = &data_singleton()->heredoc_queue;
temp = data_singleton()->heredoc_queue; redir = *(t_redir**)(*head)->content;
if (head && token) 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; ft_lst_delif(head, (t_redir**)(*head)->content, ft_addrcmp, ft_lst_cfree);
// free(temp); /* DG("matched heredoc EOF, new head @ %p", *head); */
data_singleton()->heredoc_queue = temp2;
DG("data is %s et adr %p", head->heredoc_data, head->heredoc_data);
DG("redir @ %p", head);
} }
else else
{ {
head->heredoc_data = ft_strjoin(head->heredoc_data, redir->heredoc_data = ft_str3join(redir->heredoc_data,
token->data); (char*)token->data, "\n");
head->heredoc_data = ft_strjoin(head->heredoc_data, "\n");
} }
} }
ft_lstdel(lst, &token_free); ft_lstdel(lst, &token_free);