From 6ca10b673a8ac2c5f58c06e636dc8c2be272306d Mon Sep 17 00:00:00 2001 From: Jean <47366872+jean-voila@users.noreply.github.com> Date: Thu, 29 Jan 2026 19:35:08 +0100 Subject: [PATCH] feat(compiling en fait): Execution --- src/execution/execution_helpers.c | 28 ++++++++++------------------ src/utils/Makefile.am | 3 ++- src/utils/ast/ast_assignment.c | 3 ++- src/utils/ast/ast_command.c | 1 + 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/execution/execution_helpers.c b/src/execution/execution_helpers.c index 501b159..edbe220 100644 --- a/src/execution/execution_helpers.c +++ b/src/execution/execution_helpers.c @@ -42,16 +42,15 @@ static int set_all_redir(struct list *redir_list) { while (redir_list) { - struct ast_redir *redir = (struct ast_redir *)redir_list->data; + struct ast *redir_node = (struct ast *)redir_list->data; + struct ast_redir *redir = ast_get_redir(redir_node); int target_fd; - if (redir->io_number != -1) { target_fd = redir->io_number; } else { - // assign target_fd depending on redir type if (redir->type == AST_REDIR_TYPE_LESS || redir->type == AST_REDIR_TYPE_LESSGREAT || redir->type == AST_REDIR_TYPE_LESSAND) @@ -63,7 +62,6 @@ static int set_all_redir(struct list *redir_list) target_fd = 1; } } - redir->saved_fd = dup(target_fd); int new_fd = -1; int flags = 0; @@ -130,8 +128,7 @@ static int try_builtin(char **argv, struct hash_map *vars); int exec_ast_command(struct ast_command *command, struct hash_map *vars) { (void)vars; - set_all_redir(command->redirections ? command->redirections->children - : NULL); + set_all_redir(command->redirections); if (!command || !(command->command)) { @@ -142,8 +139,7 @@ int exec_ast_command(struct ast_command *command, struct hash_map *vars) if (!argv || !(argv[0])) { free(argv); - unset_all_redir(command->redirections ? command->redirections->children - : NULL); + unset_all_redir(command->redirections); return 0; } @@ -151,8 +147,7 @@ int exec_ast_command(struct ast_command *command, struct hash_map *vars) if (builtin_ret != -1) { free(argv); - unset_all_redir(command->redirections ? command->redirections->children - : NULL); + unset_all_redir(command->redirections); return builtin_ret; } @@ -161,8 +156,7 @@ int exec_ast_command(struct ast_command *command, struct hash_map *vars) { perror("fork"); free(argv); - unset_all_redir(command->redirections ? command->redirections->children - : NULL); + unset_all_redir(command->redirections); return 1; } @@ -170,16 +164,14 @@ int exec_ast_command(struct ast_command *command, struct hash_map *vars) { execvp(argv[0], argv); perror("execvp"); - unset_all_redir(command->redirections ? command->redirections->children - : NULL); + unset_all_redir(command->redirections); _exit(127); } int status = 0; waitpid(pid, &status, 0); free(argv); - unset_all_redir(command->redirections ? command->redirections->children - : NULL); + unset_all_redir(command->redirections); if (WIFEXITED(status)) { @@ -233,7 +225,8 @@ void unset_all_redir(struct list *redir_list) { while (redir_list) { - struct ast_redir *redir = (struct ast_redir *)redir_list->data; + struct ast *redir_node = (struct ast *)redir_list->data; + struct ast_redir *redir = ast_get_redir(redir_node); int target_fd; if (redir->io_number != -1) { @@ -258,7 +251,6 @@ void unset_all_redir(struct list *redir_list) close(redir->saved_fd); redir->saved_fd = -1; } - redir_list = redir_list->next; } } diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am index c2cf250..7876682 100644 --- a/src/utils/Makefile.am +++ b/src/utils/Makefile.am @@ -18,7 +18,8 @@ libutils_a_SOURCES = \ ast/ast_neg.c \ ast/ast_pipe.c \ args/args.c \ - vars/vars.c + vars/vars.c \ + ast/ast_assignment.c libutils_a_CPPFLAGS = -I$(top_srcdir)/src diff --git a/src/utils/ast/ast_assignment.c b/src/utils/ast/ast_assignment.c index a839e9e..e859912 100644 --- a/src/utils/ast/ast_assignment.c +++ b/src/utils/ast/ast_assignment.c @@ -3,6 +3,7 @@ #include "ast_assignment.h" #include +#include bool ast_is_assignment(struct ast *node) { @@ -40,7 +41,7 @@ struct ast *ast_create_assignment(char *assignment) if (!assignment_data) return NULL; - init_assignments(assignement_data); + init_assignments(assignment_data, assignment); return ast_create(AST_ASSIGNMENT, assignment_data); } diff --git a/src/utils/ast/ast_command.c b/src/utils/ast/ast_command.c index 7e192ca..81dd10e 100644 --- a/src/utils/ast/ast_command.c +++ b/src/utils/ast/ast_command.c @@ -15,6 +15,7 @@ struct ast *ast_create_command(struct list *command, struct list *redirections, command_data->command = command; command_data->redirections = redirections; + command_data->assignments = assignments; return ast_create(AST_CMD, command_data); }