fixed heredoc expansion

This commit is contained in:
marcnava-42cursus
2026-02-14 14:37:56 +01:00
parent 001709139b
commit c1e622947d
4 changed files with 22 additions and 11 deletions

View File

@@ -59,10 +59,15 @@ static char *expand_variable(const char *line, size_t *i, t_minishell *msh)
return (ft_strdup(value)); 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]; 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[0] = line[*i];
value[1] = '\0'; value[1] = '\0';
(*i)++; (*i)++;
@@ -77,17 +82,21 @@ char *executor_expand_heredoc_line(
char *result; char *result;
char *expanded; char *expanded;
size_t i; size_t i;
bool in_single;
bool in_double;
result = ft_strdup(""); result = ft_strdup("");
if (result == NULL) if (result == NULL)
return (malloc_error(), NULL); return (malloc_error(), NULL);
i = 0; i = 0;
in_single = false;
in_double = false;
while (line[i] != '\0') while (line[i] != '\0')
{ {
if (line[i] == '$') if (line[i] == '$' && !in_single)
expanded = expand_variable(line, &i, minishell); expanded = expand_variable(line, &i, minishell);
else else
expanded = expand_literal_char(line, &i); expanded = expand_literal_char(line, &i, &in_single, &in_double);
if (expanded == NULL) if (expanded == NULL)
return (free(result), malloc_error(), NULL); return (free(result), malloc_error(), NULL);
if (!append_text(&result, expanded)) if (!append_text(&result, expanded))

View File

@@ -96,14 +96,12 @@ static bool expand_redirections(
{ {
t_redirection *redirection; t_redirection *redirection;
char *expanded; char *expanded;
bool expand_vars;
while (redirections != NULL) while (redirections != NULL)
{ {
redirection = (t_redirection *)redirections->content; redirection = (t_redirection *)redirections->content;
expand_vars = (redirection->type != TOKEN_HEREDOC);
expanded = parser_expand_word(redirection->target, minishell, expanded = parser_expand_word(redirection->target, minishell,
expand_vars); true);
if (expanded == NULL) if (expanded == NULL)
return (false); return (false);
free(redirection->target); free(redirection->target);

View File

@@ -15,7 +15,7 @@
static t_redirection *redirection_new(t_list **tokens, int io_number); static t_redirection *redirection_new(t_list **tokens, int io_number);
static bool redirection_read_target(t_redirection *rd, t_list **tk); 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. * @brief Creates a new redirection from a list of tokens.
@@ -68,7 +68,7 @@ static bool redirection_read_target(
*tk = (*tk)->next; *tk = (*tk)->next;
return (syntax_error_unexpected_token(token), false); 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->heredoc_expand = false;
rd->target = ft_strdup(token->value); rd->target = ft_strdup(token->value);
if (rd->target == NULL) if (rd->target == NULL)
@@ -77,7 +77,7 @@ static bool redirection_read_target(
return (true); return (true);
} }
static bool has_quote_char( static bool has_single_quote(
const char *value const char *value
) )
{ {
@@ -88,7 +88,7 @@ static bool has_quote_char(
i = 0; i = 0;
while (value[i] != '\0') while (value[i] != '\0')
{ {
if (value[i] == '\'' || value[i] == '\"') if (value[i] == '\'')
return (true); return (true);
i++; i++;
} }

View File

@@ -57,13 +57,17 @@ void set_env(
char *key; char *key;
char *val; char *val;
char *old_value; char *old_value;
bool owns_key;
environment = minishell->variables.environment; environment = minishell->variables.environment;
owns_key = false;
key = (char *)name;
if (name != NULL && !ft_hashmap_contains_key(environment, name)) if (name != NULL && !ft_hashmap_contains_key(environment, name))
{ {
key = ft_strdup(name); key = ft_strdup(name);
if (key == NULL) if (key == NULL)
return (minishell->exit = true, malloc_error()); return (minishell->exit = true, malloc_error());
owns_key = true;
} }
if (value == NULL) if (value == NULL)
val = ft_strdup(""); val = ft_strdup("");
@@ -71,7 +75,7 @@ void set_env(
val = ft_strdup(value); val = ft_strdup(value);
if (val == NULL) if (val == NULL)
{ {
if (key != name) if (owns_key)
free(key); free(key);
return (minishell->exit = true, malloc_error()); return (minishell->exit = true, malloc_error());
} }