From 38ffac7fc356515ec6e12703c0e069c0126ad170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 21 Apr 2025 13:07:02 +0200 Subject: [PATCH] feat(redir): expand wildcards in targets and handle ambiguous redirects --- src/postprocess/expansion/expand_wildcard.c | 67 +++++++++++++++++++-- test.sh | 19 ++++++ 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/src/postprocess/expansion/expand_wildcard.c b/src/postprocess/expansion/expand_wildcard.c index 12c7587..f13f7f5 100644 --- a/src/postprocess/expansion/expand_wildcard.c +++ b/src/postprocess/expansion/expand_wildcard.c @@ -6,22 +6,67 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 19:28:28 by khais #+# #+# */ -/* Updated: 2025/04/03 19:52:29 by khais ### ########.fr */ +/* Updated: 2025/04/21 14:42:41 by khais ### ########.fr */ /* */ /* ************************************************************************** */ +#include "../../ft_errno.h" #include "expand_wildcard.h" #include "../../subst/subst.h" +#include "../../parser/redirect/redirect.h" +#include "../../parser/cmd/cmd_destroy.h" +#include "../ambiguous_redirect.h" -t_simple_cmd *simple_cmd_expand_wildcards(t_simple_cmd *cmd) +static t_redirect *redirect_expand_wildcard_one(t_redirect *current) +{ + t_wordlist *expansion_result; + + expansion_result = expand_star(current->redirectee.filename); + if (expansion_result != NULL) + { + worddesc_destroy(current->redirectee.filename); + current->redirectee.filename = wordlist_pop(&expansion_result); + if (expansion_result != NULL) + { + ambiguous_redirect(current->unexpanded_filename); + redirect_destroy(current); + wordlist_destroy(expansion_result); + return (ft_errno(FT_EERRNO), NULL); + } + } + return (current); +} + +static t_redirect *redirect_expand_wildcards(t_redirect *in_list) +{ + t_redirect *out_list; + t_redirect *current; + + out_list = NULL; + while (in_list != NULL) + { + current = redirect_pop(&in_list); + if (current->type != FT_HEREDOC) + { + if (redirect_expand_wildcard_one(current) == NULL) + { + redirect_destroy(in_list); + redirect_destroy(out_list); + return (NULL); + } + } + out_list = t_redirect_add_back(&out_list, current); + } + return (out_list); +} + +static t_wordlist *wordlist_expand_wildcards(t_wordlist *inlist) { t_wordlist *outlist; - t_wordlist *inlist; t_worddesc *current; t_wordlist *expansion_result; outlist = NULL; - inlist = cmd->words; while (inlist != NULL) { current = wordlist_pop(&inlist); @@ -36,6 +81,18 @@ t_simple_cmd *simple_cmd_expand_wildcards(t_simple_cmd *cmd) worddesc_destroy(current); } } - cmd->words = outlist; + return (outlist); +} + +t_simple_cmd *simple_cmd_expand_wildcards(t_simple_cmd *cmd) +{ + cmd->words = wordlist_expand_wildcards(cmd->words); + ft_errno(FT_ESUCCESS); + if (redirect_expand_wildcards(cmd->redirections) == NULL + && ft_errno_get() != FT_ESUCCESS) + { + cmd->redirections = NULL; + return (NULL); + } return (cmd); } diff --git a/test.sh b/test.sh index 2c9d130..36ff100 100755 --- a/test.sh +++ b/test.sh @@ -841,4 +841,23 @@ minishell: $target: ambiguous redirect 0 EOF +when_run <<"EOF" "wildcard expansion in redirect" +touch "microsoft windows" +echo hello > *m*cros*ft* +echo $? +echo goodbye >> *w*nd*ws* +echo $? +ls +cat < *soft* +echo $? +EOF +expecting <