expand_redirection() : rejoin token list on error

+ words_expansions.c -> expansions.c
+ generic.c misc
This commit is contained in:
LuckyLaszlo
2021-12-11 20:19:26 +01:00
parent a7de843dc1
commit 9c660d4f92
4 changed files with 41 additions and 65 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */
/* Updated: 2021/12/05 17:35:32 by lperrey ### ########.fr */
/* Updated: 2021/12/11 20:16:36 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -202,13 +202,13 @@ void *ft_lstnew_generic(size_t lst_size, size_t content_size)
void ft_perror_io(char *err_str, char *io_file)
{
ft_putstr_fd(err_str, 2);
ft_putstr_fd(err_str, STDERR_FILENO);
perror(io_file);
}
int ft_reti_perror_io(int ret, char *err_str, char *io_file)
{
ft_putstr_fd(err_str, 2);
ft_putstr_fd(err_str, STDERR_FILENO);
perror(io_file);
return (ret);
}

View File

@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* words_expansions.c :+: :+: :+: */
/* expansions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
/* Updated: 2021/12/10 19:51:33 by lperrey ### ########.fr */
/* Updated: 2021/12/11 20:10:37 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
/* Updated: 2021/12/11 04:22:07 by lperrey ### ########.fr */
/* Updated: 2021/12/11 20:16:31 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,6 +16,7 @@ int here_doc(char *delimiter);
static int redirect_cmd_input(t_token *t, t_cmd *cmd);
static int redirect_cmd_output(t_token *t, t_cmd *cmd);
static int expand_redirection(t_token *t);
int redirections(t_token *t, t_cmd **pipeline)
{
@@ -44,60 +45,6 @@ int redirections(t_token *t, t_cmd **pipeline)
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)
@@ -105,7 +52,7 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd)
perror("close()");
if (t->id == '<')
{
if (!EXPAND_AND_QUOTE_REMOVAL(t))
if (!expand_redirection(t))
{
cmd->error = EXIT_REDIRECTION;
return (0);
@@ -113,7 +60,7 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd)
cmd->fd_in = open(t->next->content, O_RDONLY);
if (cmd->fd_in == -1)
{
ft_perror_io("open() ", t->next->content); // todo error
ft_perror_io("open() ", t->next->content);
cmd->error = EXIT_REDIRECTION;
}
}
@@ -122,7 +69,7 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd)
cmd->fd_in = here_doc(t->next->content);
if (cmd->fd_in == -1)
{
ft_putstr_fd("minishell: heredoc error\n", 2);
ft_putstr_fd("minishell: heredoc error\n", STDERR_FILENO);
cmd->error = EXIT_REDIRECTION;
}
else if (cmd->fd_in > EXIT_SIGNAL)
@@ -141,7 +88,7 @@ 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()");
if (!EXPAND_AND_QUOTE_REMOVAL(t))
if (!expand_redirection(t))
{
cmd->error = EXIT_REDIRECTION;
return (0);
@@ -159,3 +106,32 @@ static int redirect_cmd_output(t_token *t, t_cmd *cmd)
}
return (1);
}
static int expand_redirection(t_token *t)
{
t_token *head;
t_token *next_token;
int ret;
ret = 1;
head = t;
t = t->next;
next_token = t->next;
t->next = NULL;
if (!token_expansions(t))
{
((t_token *)ft_lstlast((t_list *)head))->next = next_token;
return (0);
}
head->next = t->next;
free(t);
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;
return (ret);
}