save commit
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/10/20 20:51:33 by sede-san #+# #+# */
|
/* Created: 2025/10/20 20:51:33 by sede-san #+# #+# */
|
||||||
/* Updated: 2026/02/14 02:03:47 by sede-san ### ########.fr */
|
/* Updated: 2026/02/14 02:23:17 by sede-san ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -29,7 +29,8 @@ void minishell_init(
|
|||||||
|
|
||||||
void minishell_run(
|
void minishell_run(
|
||||||
t_minishell *minishell
|
t_minishell *minishell
|
||||||
){
|
)
|
||||||
|
{
|
||||||
char *line;
|
char *line;
|
||||||
t_list *commands;
|
t_list *commands;
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/02/13 21:24:45 by sede-san #+# #+# */
|
/* 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;
|
*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(
|
static void expand_argument(
|
||||||
char **argument,
|
char **argument,
|
||||||
t_minishell *minishell
|
t_minishell *minishell
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
bool in_single_quote;
|
bool in_single_quote;
|
||||||
bool in_double_quote;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
in_single_quote = false;
|
in_single_quote = false;
|
||||||
in_double_quote = false;
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while ((*argument)[i] != '\0')
|
while ((*argument)[i] != '\0')
|
||||||
{
|
{
|
||||||
@@ -86,13 +168,7 @@ static void expand_argument(
|
|||||||
return (minishell->exit = true, malloc_error());
|
return (minishell->exit = true, malloc_error());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
handle_quotes(argument, &i, &in_single_quote, minishell);
|
||||||
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++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/02/13 21:29:44 by sede-san #+# #+# */
|
/* Created: 2026/02/13 21:29:44 by sede-san #+# #+# */
|
||||||
/* Updated: 2026/02/13 21:29:44 by sede-san ### ########.fr */
|
/* Updated: 2026/02/14 03:34:38 by sede-san ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user