/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* expand_wildcard.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 19:28:28 by khais #+# #+# */ /* 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" 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_worddesc *current; t_wordlist *expansion_result; outlist = NULL; while (inlist != NULL) { current = wordlist_pop(&inlist); expansion_result = expand_star(current); if (expansion_result == NULL) outlist = wordlist_push(outlist, current); else { while (expansion_result != NULL) outlist = wordlist_push(outlist, wordlist_pop(&expansion_result)); worddesc_destroy(current); } } 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); }