You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

100 lines
2.3 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/06 07:52:29 by narnaud #+# #+# */
/* Updated: 2022/04/14 09:19:12 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
size_t count_arguments(t_token tok)
{
size_t ret;
ret = 0;
while (tok.value)
{
if (tok.type == 0 && tok.value[0] != '|')
ret++;
if (tok.next)
tok = *(tok.next);
else
break ;
}
return (ret);
}
void update_redir(t_command *cmd, t_token tok)
{
if (tok.type == OUT)
{
if (cmd->fd[1])
close(cmd->fd[1]);
cmd->output = init_file(tok.value, O_TRUNC | O_WRONLY);
}
else if (tok.type == ADD)
{
if (cmd->fd[1])
close(cmd->fd[1]);
cmd->fd[1] = init_file(tok.value, O_APPEND | O_WRONLY);
}
else if (tok.type == IN)
{
if (cmd->fd[0])
close(cmd->fd[0]);
cmd->fd[0] = init_file(tok.value, O_RDONLY);
}
else if (tok.type == HD)
{
if (cmd->fd[0])
close(cmd->fd[0]);
cmd->fd[0] = 0;
cmd->heredoc = tok.value;
}
}
t_command *parser(t_token tok)
{
int i;
t_command *cmd;
t_command *ret;
cmd = ft_calloc(1, sizeof(t_command));
ret = cmd;
while (tok.value)
{
i = 0;
cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *));
while(tok.value[0] != '|')
{
if (tok.type)
update_redir(cmd, tok);
else
{
cmd->argv[i] = tok.value;
i++;
}
if (tok.next)
tok = *(tok.next);
else
break ;
}
//printf("Input : %d\n", cmd->input);
//printf("Output : %d\n", cmd->output);
cmd->argv[i] = NULL;
if (tok.next)
tok = *(tok.next);
else
break ;
cmd->next = ft_calloc(1, sizeof(t_command));
cmd->next->prev = cmd;
cmd = cmd->next;
}
return (ret);
}