mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
feat(redir): expand wildcards in targets and handle ambiguous redirects
This commit is contained in:
parent
920ad586e1
commit
38ffac7fc3
2 changed files with 81 additions and 5 deletions
|
|
@ -6,22 +6,67 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/03 19:28:28 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 "expand_wildcard.h"
|
||||||
#include "../../subst/subst.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 *outlist;
|
||||||
t_wordlist *inlist;
|
|
||||||
t_worddesc *current;
|
t_worddesc *current;
|
||||||
t_wordlist *expansion_result;
|
t_wordlist *expansion_result;
|
||||||
|
|
||||||
outlist = NULL;
|
outlist = NULL;
|
||||||
inlist = cmd->words;
|
|
||||||
while (inlist != NULL)
|
while (inlist != NULL)
|
||||||
{
|
{
|
||||||
current = wordlist_pop(&inlist);
|
current = wordlist_pop(&inlist);
|
||||||
|
|
@ -36,6 +81,18 @@ t_simple_cmd *simple_cmd_expand_wildcards(t_simple_cmd *cmd)
|
||||||
worddesc_destroy(current);
|
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);
|
return (cmd);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
test.sh
19
test.sh
|
|
@ -841,4 +841,23 @@ minishell: $target: ambiguous redirect
|
||||||
0
|
0
|
||||||
EOF
|
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 <<EOF
|
||||||
|
0
|
||||||
|
0
|
||||||
|
microsoft windows
|
||||||
|
hello
|
||||||
|
goodbye
|
||||||
|
0
|
||||||
|
EOF
|
||||||
|
|
||||||
finalize
|
finalize
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue