From c1e622947d1a4be718d540d1b5b0396e8b18e3e7 Mon Sep 17 00:00:00 2001 From: marcnava-42cursus Date: Sat, 14 Feb 2026 14:37:56 +0100 Subject: [PATCH] fixed heredoc expansion --- src/executor/heredoc_expand.c | 15 ++++++++++++--- src/parser/parser_expand.c | 4 +--- src/parser/parser_redirection.c | 8 ++++---- src/variables/environment/environment.c | 6 +++++- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/executor/heredoc_expand.c b/src/executor/heredoc_expand.c index 09850d4..7396921 100644 --- a/src/executor/heredoc_expand.c +++ b/src/executor/heredoc_expand.c @@ -59,10 +59,15 @@ static char *expand_variable(const char *line, size_t *i, t_minishell *msh) return (ft_strdup(value)); } -static char *expand_literal_char(const char *line, size_t *i) +static char *expand_literal_char(const char *line, size_t *i, + bool *in_single, bool *in_double) { char value[2]; + if (line[*i] == '\'' && !*in_double) + *in_single = !*in_single; + else if (line[*i] == '\"' && !*in_single) + *in_double = !*in_double; value[0] = line[*i]; value[1] = '\0'; (*i)++; @@ -77,17 +82,21 @@ char *executor_expand_heredoc_line( char *result; char *expanded; size_t i; + bool in_single; + bool in_double; result = ft_strdup(""); if (result == NULL) return (malloc_error(), NULL); i = 0; + in_single = false; + in_double = false; while (line[i] != '\0') { - if (line[i] == '$') + if (line[i] == '$' && !in_single) expanded = expand_variable(line, &i, minishell); else - expanded = expand_literal_char(line, &i); + expanded = expand_literal_char(line, &i, &in_single, &in_double); if (expanded == NULL) return (free(result), malloc_error(), NULL); if (!append_text(&result, expanded)) diff --git a/src/parser/parser_expand.c b/src/parser/parser_expand.c index 18357cc..b538e3f 100644 --- a/src/parser/parser_expand.c +++ b/src/parser/parser_expand.c @@ -96,14 +96,12 @@ static bool expand_redirections( { t_redirection *redirection; char *expanded; - bool expand_vars; while (redirections != NULL) { redirection = (t_redirection *)redirections->content; - expand_vars = (redirection->type != TOKEN_HEREDOC); expanded = parser_expand_word(redirection->target, minishell, - expand_vars); + true); if (expanded == NULL) return (false); free(redirection->target); diff --git a/src/parser/parser_redirection.c b/src/parser/parser_redirection.c index 12d9582..bec719d 100644 --- a/src/parser/parser_redirection.c +++ b/src/parser/parser_redirection.c @@ -15,7 +15,7 @@ static t_redirection *redirection_new(t_list **tokens, int io_number); static bool redirection_read_target(t_redirection *rd, t_list **tk); -static bool has_quote_char(const char *value); +static bool has_single_quote(const char *value); /** * @brief Creates a new redirection from a list of tokens. @@ -68,7 +68,7 @@ static bool redirection_read_target( *tk = (*tk)->next; return (syntax_error_unexpected_token(token), false); } - if (rd->type == TOKEN_HEREDOC && has_quote_char(token->value)) + if (rd->type == TOKEN_HEREDOC && has_single_quote(token->value)) rd->heredoc_expand = false; rd->target = ft_strdup(token->value); if (rd->target == NULL) @@ -77,7 +77,7 @@ static bool redirection_read_target( return (true); } -static bool has_quote_char( +static bool has_single_quote( const char *value ) { @@ -88,7 +88,7 @@ static bool has_quote_char( i = 0; while (value[i] != '\0') { - if (value[i] == '\'' || value[i] == '\"') + if (value[i] == '\'') return (true); i++; } diff --git a/src/variables/environment/environment.c b/src/variables/environment/environment.c index b05369e..e030c6c 100644 --- a/src/variables/environment/environment.c +++ b/src/variables/environment/environment.c @@ -57,13 +57,17 @@ void set_env( char *key; char *val; char *old_value; + bool owns_key; environment = minishell->variables.environment; + owns_key = false; + key = (char *)name; if (name != NULL && !ft_hashmap_contains_key(environment, name)) { key = ft_strdup(name); if (key == NULL) return (minishell->exit = true, malloc_error()); + owns_key = true; } if (value == NULL) val = ft_strdup(""); @@ -71,7 +75,7 @@ void set_env( val = ft_strdup(value); if (val == NULL) { - if (key != name) + if (owns_key) free(key); return (minishell->exit = true, malloc_error()); }