minishell: do wildcard expansion

This commit is contained in:
Khaïs COLIN 2025-04-03 15:50:32 +02:00
parent f06f6e26e2
commit be183b99ee
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
5 changed files with 78 additions and 1 deletions

View file

@ -84,6 +84,7 @@ srcs = \
src/parser/wordsplit/wordsplit.c \
src/parser/wordsplit/wordsplit_utils.c \
src/postprocess/expansion/expand_vars.c \
src/postprocess/expansion/expand_wildcard.c \
src/postprocess/fieldsplit/fieldsplit.c \
src/subst/path_split.c \
src/subst/replace_substr.c \

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */
/* Updated: 2025/04/15 15:05:48 by khais ### ########.fr */
/* Updated: 2025/04/15 11:49:23 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,6 +24,7 @@
#include "postprocess/expansion/expand_vars.h"
#include "parser/cmd_parsing.h"
#include "postprocess/fieldsplit/fieldsplit.h"
#include "postprocess/expansion/expand_wildcard.h"
/*
** execute command
@ -43,6 +44,8 @@ static t_cmd *post_process_command(t_cmd *cmd, t_minishell *app)
return (simple_cmd_destroy(cmd), NULL);
if (simple_cmd_fieldsplit(cmd) == NULL)
return (simple_cmd_destroy(cmd), NULL);
if (simple_cmd_expand_wildcards(cmd) == NULL)
return (simple_cmd_destroy(cmd), NULL);
return (cmd);
}

View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expand_wildcard.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 19:28:28 by khais #+# #+# */
/* Updated: 2025/04/03 19:52:29 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "expand_wildcard.h"
#include "../../subst/subst.h"
t_simple_cmd *simple_cmd_expand_wildcards(t_simple_cmd *cmd)
{
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);
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);
}
}
cmd->words = outlist;
return (cmd);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expand_wildcard.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 19:22:10 by khais #+# #+# */
/* Updated: 2025/04/03 19:23:24 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef EXPAND_WILDCARD_H
# define EXPAND_WILDCARD_H
# include "../../minishell.h"
t_simple_cmd *simple_cmd_expand_wildcards(t_simple_cmd *cmd);
#endif // EXPAND_WILDCARD_H

12
test.sh
View file

@ -509,6 +509,18 @@ expecting <<EOF
-hello there-
EOF
when_run <<EOF "wildcards"
echo *
touch hi there hello
echo *
echo h*
EOF
expecting <<EOF
*
hello hi there
hello hi
EOF
when_run <<EOF "quoted parentheses are not operators"
echo unclosed '('
EOF