From 5fc09400cdbfe5f598649de5b6747ab71657c86e Mon Sep 17 00:00:00 2001 From: whaffman Date: Tue, 4 Mar 2025 16:04:10 +0100 Subject: [PATCH] executo builtin fix? --- inc/typedef.h | 4 +- src/builtin/builtin_router.c | 6 +- src/builtin/is_builtin.c | 4 +- src/builtin/simple_builtins.c | 5 +- src/debug/print_commands.c | 112 ++++++++++++----------- src/executor/executor_execute_pipeline.c | 43 +++++++-- src/parser/parser_alloc_command.c | 6 +- src/parser/parser_get_commands.c | 15 ++- 8 files changed, 111 insertions(+), 84 deletions(-) diff --git a/inc/typedef.h b/inc/typedef.h index 292ed84..ec37231 100644 --- a/inc/typedef.h +++ b/inc/typedef.h @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */ -/* Updated: 2025/02/26 17:26:27 by whaffman ######## odam.nl */ +/* Updated: 2025/03/04 15:25:16 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -60,7 +60,7 @@ typedef struct s_command { char *command; char **args; - t_list *environment; + // t_list *environment; t_list *redirect_in; t_list *redirect_out; int fd_in; diff --git a/src/builtin/builtin_router.c b/src/builtin/builtin_router.c index d8e6960..ddde4b7 100644 --- a/src/builtin/builtin_router.c +++ b/src/builtin/builtin_router.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/20 11:12:38 by whaffman #+# #+# */ -/* Updated: 2025/03/03 14:38:52 by whaffman ######## odam.nl */ +/* Updated: 2025/03/04 15:28:59 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,6 +25,6 @@ int builtin_router(t_minishell *msh, t_command *cmd) if (!is_builtin(cmd->args[0])) return (FALSE); - msh->exit_status = builtin_fn[is_builtin(cmd->args[0])](msh, cmd); - return (TRUE); + return (builtin_fn[is_builtin(cmd->args[0])](msh, cmd)); + } diff --git a/src/builtin/is_builtin.c b/src/builtin/is_builtin.c index 05cb3f9..3d0301f 100644 --- a/src/builtin/is_builtin.c +++ b/src/builtin/is_builtin.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/20 11:03:33 by whaffman #+# #+# */ -/* Updated: 2025/02/26 14:42:49 by whaffman ######## odam.nl */ +/* Updated: 2025/03/04 15:45:38 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,5 +25,5 @@ int is_builtin(char *cmd) return (i); i++; } - return (FALSE); + return (-1); } diff --git a/src/builtin/simple_builtins.c b/src/builtin/simple_builtins.c index 296ddf9..3187cea 100644 --- a/src/builtin/simple_builtins.c +++ b/src/builtin/simple_builtins.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */ -/* Updated: 2025/02/26 16:08:49 by whaffman ######## odam.nl */ +/* Updated: 2025/03/04 15:43:53 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,6 +16,5 @@ void simple_builtins(t_minishell *msh) { if (msh->commands == NULL) return ; - if (!builtin_router(msh, msh->commands->content)) - executor_execute_pipeline(msh); + executor_execute_pipeline(msh); } diff --git a/src/debug/print_commands.c b/src/debug/print_commands.c index 2092b8d..8e0e5c8 100644 --- a/src/debug/print_commands.c +++ b/src/debug/print_commands.c @@ -1,55 +1,57 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* print_commands.c :+: :+: */ -/* +:+ */ -/* By: marvin +#+ */ -/* +#+ */ -/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */ -/* Updated: 2025/02/23 12:43:35 by Quinten ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -void print_commands(void *param) -{ - t_command *command; - int i; - - if (!DEBUG) - return ; - command = (t_command *)param; - if (!command) - return ; - printf("command: %s\n", command->command); - printf("-- Args: "); - i = 0; - while (command->args[i]) - printf("%s ", command->args[i++]); - printf("\n"); - printf("-- Input redirects:\n"); - ft_lstiter(command->redirect_in, print_redirects); - printf("-- Output redirects:\n"); - ft_lstiter(command->redirect_out, print_redirects); -} - -void print_redirects(void *param) -{ - t_redirect *redirect; - - redirect = (t_redirect *)param; - if (!redirect) - return ; - printf(" Redirect %i value %s\n", redirect->type, redirect->value); -} - -void token_print(void *param) -{ - t_token *token; - - if (!DEBUG) - return ; - token = (t_token *)param; - printf("token type %i, value %s\n", token->type, token->value); -} +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* print_commands.c :+: :+: */ +/* +:+ */ +/* By: marvin +#+ */ +/* +#+ */ +/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */ +/* Updated: 2025/03/04 15:49:58 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void print_commands(void *param) +{ + t_command *command; + int i; + + if (!DEBUG) + return ; + command = (t_command *)param; + if (!command) + return ; + printf("command: %s\n", command->command); + printf("-- Args: "); + i = 0; + while (command->args[i]) + printf("%s ", command->args[i++]); + printf("\n"); + printf("-- Input redirects:\n"); + ft_lstiter(command->redirect_in, print_redirects); + printf("-- Output redirects:\n"); + ft_lstiter(command->redirect_out, print_redirects); + printf("-- fd_in: %i\n", command->fd_in); + printf("-- fd_out: %i\n", command->fd_out); +} + +void print_redirects(void *param) +{ + t_redirect *redirect; + + redirect = (t_redirect *)param; + if (!redirect) + return ; + printf(" Redirect %i value %s\n", redirect->type, redirect->value); +} + +void token_print(void *param) +{ + t_token *token; + + if (!DEBUG) + return ; + token = (t_token *)param; + printf("token type %i, value %s\n", token->type, token->value); +} diff --git a/src/executor/executor_execute_pipeline.c b/src/executor/executor_execute_pipeline.c index 166eece..dffc5d2 100644 --- a/src/executor/executor_execute_pipeline.c +++ b/src/executor/executor_execute_pipeline.c @@ -6,32 +6,59 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:25:02 by willem #+# #+# */ -/* Updated: 2025/02/26 17:42:46 by whaffman ######## odam.nl */ +/* Updated: 2025/03/04 16:03:29 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" +static int execute_builtin(t_minishell *msh, t_command *cmd) +{ + int original_stdout; + int original_stdin; + int exit_status; + + original_stdin = dup(STDIN_FILENO); + original_stdout = dup(STDOUT_FILENO); + dup2(cmd->fd_in, STDIN_FILENO); + dup2(cmd->fd_out, STDOUT_FILENO); + signal_init_child(); + exit_status = builtin_router(msh, cmd); + signal_init_minishell(); + close(cmd->fd_in); + close(cmd->fd_out); + dup2(original_stdin, STDIN_FILENO); + dup2(original_stdout, STDOUT_FILENO); + close(original_stdin); + close(original_stdout); + return (exit_status); +} + int executor_execute_pipeline(t_minishell *msh) { t_list *current; - t_command *command; pid_t last_pid; int exit_status; executor_create_pipes(msh); executor_create_redirects(msh); current = msh->commands; + ft_lstiter(current, print_commands); last_pid = 0; while (current) { - command = (t_command *)current->content; - command->environment = msh->environment; - last_pid = executor_fork(msh, command); + if (is_builtin(((t_command *)current->content)->command) >= 0) + msh->exit_status = execute_builtin(msh, + (t_command *)current->content); + else + last_pid = executor_fork(msh, (t_command *)current->content); current = current->next; } - waitpid(last_pid, &exit_status, 0); - msh->exit_status = ((exit_status) & 0xff00) >> 8; + if (last_pid != 0) + { + waitpid(last_pid, &exit_status, 0); + msh->exit_status = ((exit_status) & 0xff00) >> 8; + } signal_init_minishell(); - return (((exit_status) & 0xff00) >> 8); + return (msh->exit_status); } diff --git a/src/parser/parser_alloc_command.c b/src/parser/parser_alloc_command.c index fc49925..7407446 100644 --- a/src/parser/parser_alloc_command.c +++ b/src/parser/parser_alloc_command.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* parser_new_command.c :+: :+: */ +/* parser_alloc_command.c :+: :+: */ /* +:+ */ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/11 16:18:21 by qmennen #+# #+# */ -/* Updated: 2025/02/26 16:14:31 by whaffman ######## odam.nl */ +/* Updated: 2025/03/04 15:41:59 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ t_command *parser_alloc_command(t_minishell *msh, char *cmd) command->fd_out = 1; command->redirect_in = NULL; command->redirect_out = NULL; - command->environment = NULL; + // command->environment = NULL; command->n_fds = 0; command->command = cmd; return (command); diff --git a/src/parser/parser_get_commands.c b/src/parser/parser_get_commands.c index 31ec57d..fd25b2e 100644 --- a/src/parser/parser_get_commands.c +++ b/src/parser/parser_get_commands.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* parser_get_commands.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */ -/* Updated: 2025/02/27 16:07:54 by qmennen ### ########.fr */ +/* :::::::: */ +/* parser_get_commands.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */ +/* Updated: 2025/03/04 15:53:59 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,6 +41,5 @@ t_list *parser_get_commands(t_minishell *msh) if (current && ((t_token *)current->content)->type >= 3) current = current->next; } - ft_lstiter(command_list, print_commands); return (command_list); }