From c00cc21ae4098d9d66c1c289a77b4aea55315423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Fri, 18 Apr 2025 12:42:22 +0200 Subject: [PATCH] feat(redirect): do variable expansion on redirect targets --- src/parser/redirect/redirect.c | 13 ++++++++- src/parser/redirect/redirect.h | 3 +- src/postprocess/expansion/expand_vars.c | 39 ++++++++++++++++++++++++- test.sh | 12 ++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/parser/redirect/redirect.c b/src/parser/redirect/redirect.c index 1b9bb8b..e81d7d7 100644 --- a/src/parser/redirect/redirect.c +++ b/src/parser/redirect/redirect.c @@ -6,7 +6,7 @@ /* 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 == 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); +} diff --git a/src/parser/redirect/redirect.h b/src/parser/redirect/redirect.h index bf94cd9..d45e10a 100644 --- a/src/parser/redirect/redirect.h +++ b/src/parser/redirect/redirect.h @@ -6,7 +6,7 @@ /* 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); bool is_redir(t_worddesc *token); +t_redirect *redirect_pop(t_redirect **lst); #endif // REDIRECT_H diff --git a/src/postprocess/expansion/expand_vars.c b/src/postprocess/expansion/expand_vars.c index 825a9ff..4117c14 100644 --- a/src/postprocess/expansion/expand_vars.c +++ b/src/postprocess/expansion/expand_vars.c @@ -6,12 +6,45 @@ /* 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 "../../ft_errno.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) { @@ -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); if (cmd->words == 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); } diff --git a/test.sh b/test.sh index 0adb737..cec004f 100755 --- a/test.sh +++ b/test.sh @@ -808,4 +808,16 @@ the nuclear option the nuclear option EOF +when_run <<"EOF" "redirect with expansion in target" +export target=outfile +echo -n "hello " > $target +echo there >> $target +ls +cat < $target +EOF +expecting <