save commit

This commit is contained in:
2026-02-14 05:48:18 +01:00
parent 32b3bd72b5
commit e2b734cf0c
3 changed files with 90 additions and 13 deletions

View File

@@ -6,7 +6,7 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 21:24:45 by sede-san #+# #+# */
/* Updated: 2026/02/14 01:51:57 by sede-san ### ########.fr */
/* Updated: 2026/02/14 05:45:59 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
@@ -65,17 +65,99 @@ static void expand_variable(
*argument = expanded;
}
static void toggle_quotes(
char quote,
bool *in_single_quote,
bool *in_double_quote
)
{
if (quote == '\'' && !*in_double_quote)
*in_single_quote = !*in_single_quote;
else if (quote == '\"' && !*in_single_quote)
*in_double_quote = !*in_double_quote;
}
static void remove_quotes(
char **argument,
int start,
int end,
int length
)
{
char *before;
char *quoted;
char *after;
if ((*argument)[start] != '\'' && (*argument)[start] != '\"')
return ;
before = ft_substr(*argument, 0, start);
quoted = ft_substr(*argument, start + 1, end - start - 1);
after = ft_substr(*argument, end + 1, ft_strlen(*argument) - end - 1);
free(*argument);
*argument = ft_strnjoin(3, before, quoted, after);
free((char *)before);
free((char *)after);
free((char *)quoted);
}
static void check_boundaries(
bool in_single_quote,
bool in_double_quote,
int *quote_boundaries,
int i
)
{
if ((in_single_quote || in_double_quote) && quote_boundaries[0] == -1)
quote_boundaries[0] = i;
else if (!in_single_quote && !in_double_quote
&& quote_boundaries[0] != -1 && quote_boundaries[1] == -1)
quote_boundaries[1] = i;
}
static void handle_quotes(
char **argument,
int *i,
bool *in_single_quote,
t_minishell *minishell
)
{
bool in_double_quote;
int quote_boundaries[2];
quote_boundaries[0] = -1;
quote_boundaries[1] = -1;
in_double_quote = false;
while ((*argument)[*i] != '\0')
{
if ((*argument)[*i] == '$' && !*in_single_quote)
{
expand_variable(argument, i, minishell);
if (*argument == NULL)
return (minishell->exit = true, malloc_error());
}
else
{
toggle_quotes((*argument)[*i], in_single_quote, &in_double_quote);
check_boundaries(
*in_single_quote, in_double_quote, quote_boundaries, *i);
(*i)++;
}
}
if (*in_single_quote || in_double_quote)
return (syntax_error_unexpected_token(NULL));
return (remove_quotes(
argument, quote_boundaries[0], quote_boundaries[1], *i));
}
static void expand_argument(
char **argument,
t_minishell *minishell
)
{
bool in_single_quote;
bool in_double_quote;
int i;
in_single_quote = false;
in_double_quote = false;
i = 0;
while ((*argument)[i] != '\0')
{
@@ -86,13 +168,7 @@ static void expand_argument(
return (minishell->exit = true, malloc_error());
}
else
{
if ((*argument)[i] == '\'' && !in_double_quote)
in_single_quote = !in_single_quote;
else if ((*argument)[i] == '"' && !in_single_quote)
in_double_quote = !in_double_quote;
i++;
}
handle_quotes(argument, &i, &in_single_quote, minishell);
}
}