From 1e4b3fb0a82468e168c3c6ec37dd378afa124c8e Mon Sep 17 00:00:00 2001 From: Matteo Flebus Date: Tue, 27 Jan 2026 21:06:36 +0100 Subject: [PATCH] feat(redirections): ast commands now have a field for the list of redirections + redirections implemented in parser --- src/parser/grammar_basic.c | 10 ++++++---- src/utils/ast/ast_command.c | 5 ++++- src/utils/ast/ast_command.h | 4 +++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/parser/grammar_basic.c b/src/parser/grammar_basic.c index d4a740f..f3e63b6 100644 --- a/src/parser/grammar_basic.c +++ b/src/parser/grammar_basic.c @@ -171,6 +171,7 @@ struct ast *parse_command(struct lexer_context *ctx) struct ast *parse_simple_command(struct lexer_context *ctx) { struct list *command_elements = NULL; + struct list *redirections = NULL; // list of redirection ASTs // WORD struct token *token = POP_TOKEN(); @@ -204,15 +205,15 @@ struct ast *parse_simple_command(struct lexer_context *ctx) } else if (ast_is_redir(element)) { - // TODO - perror("NOT IMPLEMENTED"); - return NULL; + // append redirections to the list of redirections + redirections = list_append(redirections, element); } else { perror("Internal error: unexpected return value from parse_element " "in parse_simple_command"); list_deep_destroy(command_elements); + list_deep_destroy(redirections); return NULL; } @@ -221,10 +222,11 @@ struct ast *parse_simple_command(struct lexer_context *ctx) } // Result - struct ast *result = ast_create_command(command_elements); + struct ast *result = ast_create_command(command_elements, redirections); if (result == NULL) { list_deep_destroy(command_elements); + list_deep_destroy(redirections); return NULL; } return result; diff --git a/src/utils/ast/ast_command.c b/src/utils/ast/ast_command.c index 14904ae..c4fe3e6 100644 --- a/src/utils/ast/ast_command.c +++ b/src/utils/ast/ast_command.c @@ -5,13 +5,15 @@ #include "../lists/lists.h" -struct ast *ast_create_command(struct list *command) +struct ast *ast_create_command(struct list *command, + struct list *redirections) { struct ast_command *command_data = malloc(sizeof(struct ast_command)); if (!command_data) return NULL; command_data->command = command; + command_data->redirections = redirections; return ast_create(AST_CMD, command_data); } @@ -33,5 +35,6 @@ void ast_free_command(struct ast_command *command_data) if (command_data == NULL) return; list_deep_destroy(command_data->command); + ast_list_deep_destroy(command_data->redirections); free(command_data); } diff --git a/src/utils/ast/ast_command.h b/src/utils/ast/ast_command.h index cf6b913..835bf5d 100644 --- a/src/utils/ast/ast_command.h +++ b/src/utils/ast/ast_command.h @@ -7,6 +7,7 @@ struct ast_command { struct list *command; // A list of words (char*) + struct ast_list *redirections; // A list of ASTs, all ast_redir }; /** @@ -23,7 +24,8 @@ struct ast_command *ast_get_command(struct ast *node); /** * Creates a new AST node representing a command. */ -struct ast *ast_create_command(struct list *command); +struct ast *ast_create_command(struct list *command, + struct list *redirections); /* * @brief: frees the given ast_command and sets the pointer to NULL.