42-archive/42sh/src/parser/add_redir.c
2017-03-06 15:58:30 +01:00

106 lines
2.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* add_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 16:39:05 by ariard #+# #+# */
/* Updated: 2017/03/06 15:54:24 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
int isdir_sep(t_btree **ast, t_list **list)
{
t_token *token;
t_astnode *node;
token = (*list)->content;
if (*ast)
{
node = (*ast)->item;
if ((node->type == CMD || node->type == TK_IO_NUMBER) &&
(token->type == TK_LESS || token->type == TK_GREAT
|| token->type == TK_GREATAND || token->type == TK_LESSAND
|| token->type == TK_DLESS || token->type == TK_DGREAT))
return (1);
}
if (!*ast)
{
if (token->type == TK_LESS || token->type == TK_GREAT
|| token->type == TK_GREATAND || token->type == TK_LESSAND
|| token->type == TK_DLESS || token->type == TK_DGREAT)
return (1);
}
return (0);
}
int isdir_word(t_btree **ast, t_list **list)
{
t_astnode *node;
t_token *token;
token = (*list)->content;
if (*ast)
{
node = (*ast)->item;
if (token->type == TK_WORD && node->type == REDIR)
{
node->type = CMD;
return (1);
}
}
return (0);
}
int add_redir_word(t_btree **ast, t_list **lst)
{
t_astnode *node;
t_token *token;
t_redir *redir;
token = (*lst)->content;
node = (*ast)->item;
if (node->data.cmd.redir)
{
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);
if (redir->type == TK_DLESS)
redir->word = NULL;
else
redir->word = ft_strdup(token->data);
}
return (0);
}
int add_redir_type(t_btree **ast, t_list **lst)
{
t_astnode *node;
t_token *token;
t_redir redir;
t_redir *temp;
if (!*ast)
gen_node(ast);
token = (*lst)->content;
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;
ft_lsteadd(&node->data.cmd.redir, ft_lstnew(&redir, sizeof(redir)));
}
else
{
temp = (ft_lstlast(node->data.cmd.redir))->content;
temp->type = token->type;
}
node->type = REDIR;
return (0);
}