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> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */ /* 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); simple_cmd_post_process_debug(cmd, app);
if (simple_cmd_expand_vars(cmd, app) == NULL) if (simple_cmd_expand_vars(cmd, app) == NULL)
return (simple_cmd_destroy(cmd), NULL); return (NULL);
if (simple_cmd_fieldsplit(cmd) == NULL) if (simple_cmd_fieldsplit(cmd) == NULL)
return (simple_cmd_destroy(cmd), NULL); return (NULL);
if (simple_cmd_expand_wildcards(cmd) == NULL) if (simple_cmd_expand_wildcards(cmd) == NULL)
return (simple_cmd_destroy(cmd), NULL); return (NULL);
if (simple_cmd_remove_quotes(cmd) == NULL) if (simple_cmd_remove_quotes(cmd) == NULL)
return (simple_cmd_destroy(cmd), NULL); return (NULL);
return (cmd); return (cmd);
} }

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 13:34:51 by khais #+# #+# */ /* 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) if (cmd == NULL)
return (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 (NULL);
return (cmd); return (cmd);
} }

View file

@ -6,10 +6,11 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 17:28:29 by khais #+# #+# */ /* 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 "subst.h"
#include "../minishell.h" #include "../minishell.h"
#include "replace_substr.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 ** Similarly if the character following the '$' is neither '_' or
** alphanumerical the dollar is removed except for the appearance of a '\0' ** alphanumerical the dollar is removed except for the appearance of a '\0'
** signaling the end of the word. ** signaling the end of the word.
** Returns NULL in case of error, or the worddesc pointer word itself if ** Returns NULL and sets errno in case of error, or the worddesc pointer word
** everything went normally. ** 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) 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); rep = calculate_replacement(word, app, i, &id_len);
if (!rep) if (!rep)
return (NULL); return (ft_errno(FT_EERRNO), NULL);
rep_len = ft_strlen(rep); rep_len = ft_strlen(rep);
if (word_update(word, i, id_len, rep) == NULL) 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; 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. ** where the introductory '$' character was not single quoted.
** We DO NOT take the '\' character into account as an escape character here ** We DO NOT take the '\' character into account as an escape character here
** under any circumstance per subject requirement. ** 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 *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; in_list = list;
while (tmp_list) out_list = NULL;
while (in_list)
{ {
if ((tmp_list->word->flags & W_HASDOLLAR) word = wordlist_pop(&in_list);
&& word_var_expansion(tmp_list->word, app) == NULL) if (word->flags & W_HASDOLLAR)
return (NULL); {
tmp_list = tmp_list->next; 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 `|' minishell: syntax error near unexpected token `|'
EOF 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 finalize