update: command list is now freed completely if any error ocurrs

This commit is contained in:
2026-02-12 03:09:45 +01:00
parent 843bdc0061
commit 8882929423
2 changed files with 17 additions and 13 deletions

View File

@@ -6,7 +6,7 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */ /* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/22 19:03:51 by sede-san #+# #+# */ /* Created: 2025/10/22 19:03:51 by sede-san #+# #+# */
/* Updated: 2026/02/11 00:35:08 by sede-san ### ########.fr */ /* Updated: 2026/02/12 03:06:43 by sede-san ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -32,9 +32,9 @@ extern t_list *lex(const char *line);
extern void token_clear(t_token *token); extern void token_clear(t_token *token);
extern t_command *command_new(t_list **tokens); extern t_command *command_new(t_list **tokens);
extern void command_clear(t_command *command); extern void command_clear(t_command *command);
extern void command_add_tokens(t_command *command, t_list **tokens); extern void command_add_tokens(t_command **command, t_list **tokens);
extern bool is_pipe(t_token *token); extern bool is_pipe(t_token *token);
extern bool is_redirection(t_token *token); extern bool is_redirection(t_token *token);
void redirection_add(t_list **tokens, t_token *token, t_command *command); void redirection_add(t_list **tokens, t_token *token, t_command **command);
void words_add(t_list **tokens, t_command *command); void words_add(t_list **tokens, t_command **command);
#endif /* PARSER_H */ #endif /* PARSER_H */

View File

@@ -6,7 +6,7 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */ /* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/22 18:37:38 by sede-san #+# #+# */ /* Created: 2025/10/22 18:37:38 by sede-san #+# #+# */
/* Updated: 2026/02/12 02:55:51 by sede-san ### ########.fr */ /* Updated: 2026/02/12 03:06:20 by sede-san ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -119,7 +119,7 @@ t_command *command_new(
delimiter_token = ft_lstfind(current_token, (bool (*)(void *))is_pipe); delimiter_token = ft_lstfind(current_token, (bool (*)(void *))is_pipe);
while (command != NULL && current_token != delimiter_token) while (command != NULL && current_token != delimiter_token)
{ {
command_add_tokens(command, &current_token); command_add_tokens(&command, &current_token);
} }
*tokens = current_token; *tokens = current_token;
return (command); return (command);
@@ -190,7 +190,7 @@ void redirection_clear(
* the token. * the token.
*/ */
void command_add_tokens( void command_add_tokens(
t_command *command, t_command **command,
t_list **tokens t_list **tokens
) )
{ {
@@ -234,7 +234,7 @@ char **args_to_array(
*/ */
void words_add( void words_add(
t_list **tokens, t_list **tokens,
t_command *command t_command **command
) )
{ {
t_list *args; t_list *args;
@@ -247,20 +247,20 @@ void words_add(
while (arg != NULL && token->type == TOKEN_WORD) while (arg != NULL && token->type == TOKEN_WORD)
{ {
ft_lstadd_back(&args, ft_lstnew(ft_strdup(token->value))); ft_lstadd_back(&args, ft_lstnew(ft_strdup(token->value)));
command->argc++; (*command)->argc++;
arg = arg->next; arg = arg->next;
if (arg != NULL) if (arg != NULL)
token = (t_token *)arg->content; token = (t_token *)arg->content;
} }
*tokens = arg; *tokens = arg;
command->argv = args_to_array(args, command->argc); (*command)->argv = args_to_array(args, (*command)->argc);
ft_lstclear_nodes(&args); ft_lstclear_nodes(&args);
} }
void redirection_add( void redirection_add(
t_list **tokens, t_list **tokens,
t_token *token, t_token *token,
t_command *command t_command **command
) )
{ {
t_redirection *redirection; t_redirection *redirection;
@@ -268,7 +268,11 @@ void redirection_add(
redirection = redirection_new(tokens); redirection = redirection_new(tokens);
if (redirection == NULL) if (redirection == NULL)
{
command_clear(*command);
*command = NULL;
return ; return ;
}
redirection_tokens = ft_lstnew(redirection); redirection_tokens = ft_lstnew(redirection);
if (redirection_tokens == NULL) if (redirection_tokens == NULL)
{ {
@@ -276,9 +280,9 @@ void redirection_add(
return (malloc_error()); return (malloc_error());
} }
if (token->type == TOKEN_HEREDOC) if (token->type == TOKEN_HEREDOC)
ft_lstadd_back(&command->heredocs, redirection_tokens); ft_lstadd_back(&(*command)->heredocs, redirection_tokens);
else else
ft_lstadd_back(&command->redirections, redirection_tokens); ft_lstadd_back(&(*command)->redirections, redirection_tokens);
} }
/** /**