expand redirections

This commit is contained in:
LuckyLaszlo
2021-12-11 04:28:43 +01:00
parent d3d55386d9
commit 82f0362323

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
/* Updated: 2021/12/09 20:38:05 by lperrey ### ########.fr */
/* Updated: 2021/12/11 04:22:07 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,6 +24,8 @@ int redirections(t_token *t, t_cmd **pipeline)
i = 0;
while (t)
{
if (t->id == '|')
i++;
if (!pipeline[i]->error)
{
if (t->id == '<' || t->id == T_DLESS)
@@ -37,13 +39,65 @@ int redirections(t_token *t, t_cmd **pipeline)
return (0);
}
}
if (t->id == '|')
i++;
t = t->next;
}
return (1);
}
void test_tokens_print(t_list *lst)
{
while (lst)
{
printf("CONTENT = %s\n", lst->content);
printf("ID = %i\n", ((t_token*)lst)->id);
lst = lst->next;
}
}
// TODO : Expansion + quote removal sur le word t->next->content.
// si plus d'un champ ou aucun champ aprés expansion,
// message d'erreur comme bash "bash: $VAR: ambiguous redirect"
int EXPAND_AND_QUOTE_REMOVAL(t_token *t)
{
t_token *head;
t_token *next_token;
int ret;
ret = 1;
head = t;
t = t->next;
/* ft_putstr_fd("Token Before:\n-----------\n", STDERR_FILENO);
test_tokens_print((t_list *)t);
ft_putstr_fd("-----------\n", STDERR_FILENO); */
next_token = t->next;
t->next = NULL;
if (!token_expansions(t))
return (0);
head->next = t->next;
free(t);
/* ft_putstr_fd("Head After:\n-----------\n", STDERR_FILENO);
test_tokens_print((t_list *)head);
ft_putstr_fd("-----------\n", STDERR_FILENO); */
if (head->next)
head->next->id = T_REDIRECTION_WORD; // Eventuellement a integrer dans token_expansions()
if (ft_lstsize((t_list *)head->next) != 1)
{
ret = 0;
ft_putstr_fd("minishell: ambiguous redirect\n", STDERR_FILENO); // tmp message
}
((t_token *)ft_lstlast((t_list *)head))->next = next_token;
/* ft_putstr_fd("HEAD after adjust/rejoin:\n-----------\n", STDERR_FILENO);
test_tokens_print((t_list *)head);
ft_putstr_fd("-----------\n", STDERR_FILENO); */
return (ret);
}
static int redirect_cmd_input(t_token *t, t_cmd *cmd)
{
if (cmd->fd_in != STDIN_FILENO)
@@ -51,11 +105,11 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd)
perror("close()");
if (t->id == '<')
{
// TODO : Expansion + quote removal sur le word t->next->content.
// si plus d'un champ ou aucun champ aprés expansion,
// message d'erreur comme bash "bash: $VAR: ambiguous redirect"
// OU prise en compte seulement du premier champ.
//EXPAND_AND_QUOTE_REMOVAL_PLACEHOLDER();
if (!EXPAND_AND_QUOTE_REMOVAL(t))
{
cmd->error = EXIT_REDIRECTION;
return (0);
}
cmd->fd_in = open(t->next->content, O_RDONLY);
if (cmd->fd_in == -1)
{
@@ -87,11 +141,11 @@ static int redirect_cmd_output(t_token *t, t_cmd *cmd)
if (cmd->fd_out != STDOUT_FILENO)
if (close(cmd->fd_out) == -1)
perror("close()");
// TODO : Expansion + quote removal sur le word t->next->content.
// si plus d'un champ ou aucun champ aprés expansion,
// message d'erreur comme bash "bash: $VAR: ambiguous redirect"
// OU prise en compte seulement du premier champ.
//EXPAND_AND_QUOTE_REMOVAL_PLACEHOLDER();
if (!EXPAND_AND_QUOTE_REMOVAL(t))
{
cmd->error = EXIT_REDIRECTION;
return (0);
}
flags = O_WRONLY | O_CREAT;
if (t->id == '>')
flags = flags | O_TRUNC;