diff --git a/src/executing/simple_cmd/simple_cmd_execute.c b/src/executing/simple_cmd/simple_cmd_execute.c index 27f2422..40b8cf3 100644 --- a/src/executing/simple_cmd/simple_cmd_execute.c +++ b/src/executing/simple_cmd/simple_cmd_execute.c @@ -6,7 +6,7 @@ /* 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); 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); } diff --git a/src/postprocess/expansion/expand_vars.c b/src/postprocess/expansion/expand_vars.c index aee184c..825a9ff 100644 --- a/src/postprocess/expansion/expand_vars.c +++ b/src/postprocess/expansion/expand_vars.c @@ -6,7 +6,7 @@ /* 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) 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); } diff --git a/src/subst/variable_subst.c b/src/subst/variable_subst.c index 1d04e6f..3641255 100644 --- a/src/subst/variable_subst.c +++ b/src/subst/variable_subst.c @@ -6,10 +6,11 @@ /* 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 "../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); } diff --git a/test.sh b/test.sh index 7123840..3718eca 100755 --- a/test.sh +++ b/test.sh @@ -704,4 +704,14 @@ expecting <<"EOF" minishell: syntax error near unexpected token `|' EOF +when_run <