84 lines
2.3 KiB
C
84 lines
2.3 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* parser_expand.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2026/02/13 21:24:45 by sede-san #+# #+# */
|
|
/* Updated: 2026/02/14 06:56:00 by sede-san ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "parser_expand_internal.h"
|
|
|
|
static bool expand_argv(
|
|
t_command *command,
|
|
t_minishell *minishell
|
|
)
|
|
{
|
|
int i;
|
|
char *expanded;
|
|
|
|
i = 0;
|
|
while (i < command->argc)
|
|
{
|
|
expanded = parser_expand_word(command->argv[i], minishell, true);
|
|
if (expanded == NULL)
|
|
return (false);
|
|
free(command->argv[i]);
|
|
command->argv[i] = expanded;
|
|
i++;
|
|
}
|
|
return (true);
|
|
}
|
|
|
|
static bool expand_redirections(
|
|
t_list *redirections,
|
|
t_minishell *minishell,
|
|
bool expand_vars
|
|
)
|
|
{
|
|
t_redirection *redirection;
|
|
char *expanded;
|
|
|
|
while (redirections != NULL)
|
|
{
|
|
redirection = (t_redirection *)redirections->content;
|
|
expanded = parser_expand_word(redirection->target, minishell,
|
|
expand_vars);
|
|
if (expanded == NULL)
|
|
return (false);
|
|
free(redirection->target);
|
|
redirection->target = expanded;
|
|
redirections = redirections->next;
|
|
}
|
|
return (true);
|
|
}
|
|
|
|
void expand(
|
|
t_list **commands,
|
|
t_minishell *minishell
|
|
)
|
|
{
|
|
t_list *current;
|
|
t_command *command;
|
|
|
|
if (commands == NULL || *commands == NULL)
|
|
return ;
|
|
current = *commands;
|
|
while (current != NULL)
|
|
{
|
|
command = (t_command *)current->content;
|
|
if (!expand_argv(command, minishell)
|
|
|| !expand_redirections(command->redirections, minishell, true)
|
|
|| !expand_redirections(command->heredocs, minishell, false))
|
|
{
|
|
ft_lstclear(commands, (void (*)(void *))command_clear);
|
|
*commands = NULL;
|
|
return ;
|
|
}
|
|
current = current->next;
|
|
}
|
|
}
|