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));
}
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))

View File

@@ -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);

View File

@@ -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++;
}

View File

@@ -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());
}