fix(susbt/variable): empty variables on their own are removed

This commit is contained in:
Khaïs COLIN 2025-04-17 14:03:35 +02:00
parent 701174fc1b
commit 655ff36351
4 changed files with 52 additions and 20 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
/* Updated: 2025/04/17 11:16:18 by khais ### ########.fr */
/* Updated: 2025/04/17 17:51:25 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -43,13 +43,13 @@ static t_simple_cmd *post_process_command(t_simple_cmd *cmd, t_minishell *app)
{
simple_cmd_post_process_debug(cmd, app);
if (simple_cmd_expand_vars(cmd, app) == NULL)
return (simple_cmd_destroy(cmd), NULL);
return (NULL);
if (simple_cmd_fieldsplit(cmd) == NULL)
return (simple_cmd_destroy(cmd), NULL);
return (NULL);
if (simple_cmd_expand_wildcards(cmd) == NULL)
return (simple_cmd_destroy(cmd), NULL);
return (NULL);
if (simple_cmd_remove_quotes(cmd) == NULL)
return (simple_cmd_destroy(cmd), NULL);
return (NULL);
return (cmd);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 13:34:51 by khais #+# #+# */
/* Updated: 2025/04/01 13:36:25 by khais ### ########.fr */
/* Updated: 2025/04/17 17:44:53 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,8 @@ t_simple_cmd *simple_cmd_expand_vars(t_simple_cmd *cmd, t_minishell *app)
{
if (cmd == NULL)
return (NULL);
if (wordlist_var_expansion(cmd->words, app) == NULL)
cmd->words = wordlist_var_expansion(cmd->words, app);
if (cmd->words == NULL)
return (NULL);
return (cmd);
}

View file

@ -6,10 +6,11 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 17:28:29 by khais #+# #+# */
/* Updated: 2025/04/16 18:03:52 by khais ### ########.fr */
/* Updated: 2025/04/18 09:22:10 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_errno.h"
#include "subst.h"
#include "../minishell.h"
#include "replace_substr.h"
@ -115,8 +116,11 @@ static char *calculate_replacement(t_worddesc *word, t_minishell *app, size_t i,
** Similarly if the character following the '$' is neither '_' or
** alphanumerical the dollar is removed except for the appearance of a '\0'
** signaling the end of the word.
** Returns NULL in case of error, or the worddesc pointer word itself if
** everything went normally.
** Returns NULL and sets errno in case of error, or the worddesc pointer word
** itself if everything went normally.
**
** returns NULL and does not touch errno if this word should be deleted (because
** of the expansion of an unquoted empty variable).
*/
t_worddesc *word_var_expansion(t_worddesc *word, t_minishell *app)
{
@ -133,10 +137,12 @@ t_worddesc *word_var_expansion(t_worddesc *word, t_minishell *app)
{
rep = calculate_replacement(word, app, i, &id_len);
if (!rep)
return (NULL);
return (ft_errno(FT_EERRNO), NULL);
rep_len = ft_strlen(rep);
if (word_update(word, i, id_len, rep) == NULL)
return (NULL);
return (ft_errno(FT_EERRNO), NULL);
if (ft_strlen(word->word) == 0)
return (worddesc_destroy(word), NULL);
}
i += rep_len;
}
@ -149,18 +155,33 @@ t_worddesc *word_var_expansion(t_worddesc *word, t_minishell *app)
** where the introductory '$' character was not single quoted.
** We DO NOT take the '\' character into account as an escape character here
** under any circumstance per subject requirement.
**
** In case of error, all of list is freed.
*/
t_wordlist *wordlist_var_expansion(t_wordlist *list, t_minishell *app)
{
t_wordlist *tmp_list;
t_wordlist *in_list;
t_wordlist *out_list;
t_worddesc *word;
tmp_list = list;
while (tmp_list)
in_list = list;
out_list = NULL;
while (in_list)
{
if ((tmp_list->word->flags & W_HASDOLLAR)
&& word_var_expansion(tmp_list->word, app) == NULL)
return (NULL);
tmp_list = tmp_list->next;
word = wordlist_pop(&in_list);
if (word->flags & W_HASDOLLAR)
{
ft_errno(FT_ESUCCESS);
word = word_var_expansion(word, app);
if (word == NULL && ft_errno_get() != FT_ESUCCESS)
{
wordlist_destroy(in_list);
wordlist_destroy(out_list);
return (NULL);
}
}
if (word != NULL)
out_list = wordlist_push(out_list, word);
}
return (list);
return (out_list);
}

10
test.sh
View file

@ -704,4 +704,14 @@ expecting <<"EOF"
minishell: syntax error near unexpected token `|'
EOF
when_run <<EOF "empty variables are removed"
""
\$EMPTY_NO_VALUE
echo not \$EMPTY_NO_VALUE printed
EOF
expecting <<EOF
minishell: : command not found
not printed
EOF
finalize