feat(redirect): do variable expansion on redirect targets

This commit is contained in:
Khaïs COLIN 2025-04-18 12:42:22 +02:00
parent 2ae001e00b
commit c00cc21ae4
4 changed files with 64 additions and 3 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 17:27:53 by khais #+# #+# */ /* Created: 2025/04/14 17:27:53 by khais #+# #+# */
/* Updated: 2025/04/14 17:33:09 by khais ### ########.fr */ /* Updated: 2025/04/18 12:55:08 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,3 +35,14 @@ bool is_redir(t_worddesc *token)
|| token->token_type == IN_REDIR_TOKEN || token->token_type == IN_REDIR_TOKEN
|| token->token_type == HERE_DOC_REDIR_TOKEN); || token->token_type == HERE_DOC_REDIR_TOKEN);
} }
t_redirect *redirect_pop(t_redirect **lst)
{
t_redirect *first;
if ((*lst) == NULL)
return (NULL);
first = *lst;
(*lst) = first->next;
return (first);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 17:27:27 by khais #+# #+# */ /* Created: 2025/04/14 17:27:27 by khais #+# #+# */
/* Updated: 2025/04/14 17:32:01 by khais ### ########.fr */ /* Updated: 2025/04/18 12:55:13 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,5 +17,6 @@
t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back); t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back);
bool is_redir(t_worddesc *token); bool is_redir(t_worddesc *token);
t_redirect *redirect_pop(t_redirect **lst);
#endif // REDIRECT_H #endif // REDIRECT_H

View file

@ -6,12 +6,45 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 13:34:51 by khais #+# #+# */ /* Created: 2025/04/01 13:34:51 by khais #+# #+# */
/* Updated: 2025/04/17 17:44:53 by khais ### ########.fr */ /* Updated: 2025/04/21 08:15:45 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "expand_vars.h" #include "expand_vars.h"
#include "../../ft_errno.h"
#include "../../subst/subst.h" #include "../../subst/subst.h"
#include "../../parser/redirect/redirect.h"
#include "../../parser/cmd/cmd_destroy.h"
static t_redirect *redirection_var_expansion(t_redirect *redirects,
t_minishell *app)
{
t_redirect *in_list;
t_redirect *out_list;
t_redirect *current;
in_list = redirects;
out_list = NULL;
(void)app;
while (in_list)
{
current = redirect_pop(&in_list);
if (current->type != FT_HEREDOC)
{
current->redirectee.filename
= word_var_expansion(current->redirectee.filename, app);
if (current->redirectee.filename == NULL
&& ft_errno_get() != FT_ESUCCESS)
{
redirect_destroy(in_list);
redirect_destroy(out_list);
return (redirect_destroy(current), NULL);
}
}
out_list = t_redirect_add_back(&out_list, current);
}
return (out_list);
}
t_simple_cmd *simple_cmd_expand_vars(t_simple_cmd *cmd, t_minishell *app) t_simple_cmd *simple_cmd_expand_vars(t_simple_cmd *cmd, t_minishell *app)
{ {
@ -20,5 +53,9 @@ t_simple_cmd *simple_cmd_expand_vars(t_simple_cmd *cmd, t_minishell *app)
cmd->words = wordlist_var_expansion(cmd->words, app); cmd->words = wordlist_var_expansion(cmd->words, app);
if (cmd->words == NULL) if (cmd->words == NULL)
return (NULL); return (NULL);
ft_errno(FT_ESUCCESS);
cmd->redirections = redirection_var_expansion(cmd->redirections, app);
if (cmd->redirections == NULL && ft_errno_get() != FT_ESUCCESS)
return (NULL);
return (cmd); return (cmd);
} }

12
test.sh
View file

@ -808,4 +808,16 @@ the nuclear option
the nuclear option the nuclear option
EOF EOF
when_run <<"EOF" "redirect with expansion in target"
export target=outfile
echo -n "hello " > $target
echo there >> $target
ls
cat < $target
EOF
expecting <<EOF
outfile
hello there
EOF
finalize finalize