|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* parser.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2022/05/02 16:09:25 by narnaud #+# #+# */
|
|
|
|
/* Updated: 2022/05/05 14:30:11 by mea ### ########.fr */
|
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "minishell.h"
|
|
|
|
|
|
|
|
char *expend_str(t_datas *datas, char *line)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
int tmp_i;
|
|
|
|
|
|
|
|
tmp = ft_calloc(1024, sizeof *tmp);
|
|
|
|
if (!tmp)
|
|
|
|
free(tmp);
|
|
|
|
tmp_i = 0;
|
|
|
|
while (*line)
|
|
|
|
{
|
|
|
|
if (*line == '$')
|
|
|
|
tmp_i = replace_var(datas, &line, tmp, tmp_i);
|
|
|
|
tmp[tmp_i] = *line;
|
|
|
|
line++;
|
|
|
|
tmp_i++;
|
|
|
|
}
|
|
|
|
return (tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int push_heredoc(t_datas *datas, char *str)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
int pip[2];
|
|
|
|
|
|
|
|
pipe(pip);
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
line = readline(">");
|
|
|
|
if (!ft_strncmp(line, str, ft_strlen(str) + 1))
|
|
|
|
break;
|
|
|
|
ft_putstr_fd(expend_str(datas, line), pip[1]);
|
|
|
|
write(pip[1], "\n", 1);
|
|
|
|
}
|
|
|
|
close(pip[1]);
|
|
|
|
return (pip[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t count_arguments(t_token *tok)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
while (tok && tok->type != PIPE)
|
|
|
|
{
|
|
|
|
if (tok->type == WORD)
|
|
|
|
ret++;
|
|
|
|
tok = tok->next;
|
|
|
|
}
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_redir(t_datas *datas, t_command *cmd, t_token *tok)
|
|
|
|
{
|
|
|
|
if (tok->type == OUT)
|
|
|
|
{
|
|
|
|
if (cmd->fd[1])
|
|
|
|
close(cmd->fd[1]);
|
|
|
|
cmd->fd[1] = open(tok->value, O_CREAT | O_TRUNC | O_WRONLY, 0644);
|
|
|
|
}
|
|
|
|
else if (tok->type == ADD)
|
|
|
|
{
|
|
|
|
if (cmd->fd[1])
|
|
|
|
close(cmd->fd[1]);
|
|
|
|
cmd->fd[1] = open(tok->value, O_CREAT | O_APPEND | O_WRONLY, 0644);
|
|
|
|
}
|
|
|
|
else if (tok->type == IN)
|
|
|
|
{
|
|
|
|
if (cmd->fd[0])
|
|
|
|
close(cmd->fd[0]);
|
|
|
|
cmd->fd[0] = open(tok->value, O_RDONLY);
|
|
|
|
}
|
|
|
|
else if (tok->type == HD)
|
|
|
|
{
|
|
|
|
if (cmd->fd[0])
|
|
|
|
close(cmd->fd[0]);
|
|
|
|
cmd->fd[0] = push_heredoc(datas, tok->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|