From bd06d9f19cf85d115dbcf7279c68baf4236e94b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 28 Apr 2025 12:11:27 +0200 Subject: [PATCH] refactor(std_fds): allow for ability to store multiple std fds savesets --- Makefile | 1 + src/executing/simple_cmd/builtins.c | 9 +++-- src/executing/simple_cmd/builtins.h | 5 ++- .../simple_cmd/handle_redirections.c | 25 +++--------- .../simple_cmd/handle_redirections.h | 5 +-- src/executing/simple_cmd/simple_cmd_execute.c | 18 +++++---- src/executing/simple_cmd/std_fds.c | 39 +++++++++++++++++++ src/executing/simple_cmd/std_fds.h | 22 +++++++++++ src/executing/simple_cmd/subprocess.c | 10 ++--- src/executing/simple_cmd/subprocess.h | 5 ++- src/minishell.h | 12 ++++-- 11 files changed, 105 insertions(+), 46 deletions(-) create mode 100644 src/executing/simple_cmd/std_fds.c create mode 100644 src/executing/simple_cmd/std_fds.h diff --git a/Makefile b/Makefile index 72759be..d16cdec 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,7 @@ srcs = \ src/executing/simple_cmd/handle_redirections.c \ src/executing/simple_cmd/simple_cmd_execute.c \ src/executing/simple_cmd/simple_cmd_execute_debug.c \ + src/executing/simple_cmd/std_fds.c \ src/executing/simple_cmd/subprocess.c \ src/ft_errno.c \ src/get_command.c \ diff --git a/src/executing/simple_cmd/builtins.c b/src/executing/simple_cmd/builtins.c index 8a9831c..bcf8974 100644 --- a/src/executing/simple_cmd/builtins.c +++ b/src/executing/simple_cmd/builtins.c @@ -6,14 +6,14 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/01 16:37:21 by khais #+# #+# */ -/* Updated: 2025/04/18 14:12:50 by khais ### ########.fr */ +/* Updated: 2025/04/28 12:46:14 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "builtins.h" #include "libft.h" #include "simple_cmd_execute.h" -#include "handle_redirections.h" +#include "std_fds.h" t_builtin_type get_builtin(t_simple_cmd *cmd) { @@ -37,7 +37,8 @@ t_builtin_type get_builtin(t_simple_cmd *cmd) return (BUILTIN_INVALID); } -t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app) +t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app, + t_std_fds *fds) { t_builtin_type type; static t_builtin_type (*builtins[])(t_simple_cmd *, t_minishell *) = { @@ -54,6 +55,6 @@ t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app) type = get_builtin(cmd); builtins[type](cmd, app); if (type != BUILTIN_INVALID) - restore_std_fds(app); + restore_std_fds(fds); return (type); } diff --git a/src/executing/simple_cmd/builtins.h b/src/executing/simple_cmd/builtins.h index 3953d11..93309f7 100644 --- a/src/executing/simple_cmd/builtins.h +++ b/src/executing/simple_cmd/builtins.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/31 14:16:13 by khais #+# #+# */ -/* Updated: 2025/04/18 14:03:06 by khais ### ########.fr */ +/* Updated: 2025/04/28 12:48:00 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,7 @@ t_builtin_type builtin_env(t_simple_cmd *cmd, t_minishell *app); t_builtin_type builtin_unset(t_simple_cmd *cmd, t_minishell *app); t_builtin_type get_builtin(t_simple_cmd *cmd); -t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app); +t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app, + t_std_fds *fds); #endif // BUILTINS_H diff --git a/src/executing/simple_cmd/handle_redirections.c b/src/executing/simple_cmd/handle_redirections.c index f248eae..7ed2b76 100644 --- a/src/executing/simple_cmd/handle_redirections.c +++ b/src/executing/simple_cmd/handle_redirections.c @@ -6,11 +6,12 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:14:40 by khais #+# #+# */ -/* Updated: 2025/04/28 11:38:17 by khais ### ########.fr */ +/* Updated: 2025/04/28 12:47:50 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "handle_redirections.h" +#include "std_fds.h" #include #include @@ -43,29 +44,15 @@ static t_redirect *do_redirection(t_redirect *redirection) return (redirection); } -void restore_std_fds(t_minishell *app) +t_minishell *handle_redirections(t_redirect *redirections, t_minishell *app, + t_std_fds *fds) { - if (dup2(app->stdin, STDIN_FILENO) < 0) - perror("minishell: dup2"); - if (dup2(app->stdout, STDOUT_FILENO) < 0) - perror("minishell: dup2"); - if (dup2(app->stderr, STDERR_FILENO) < 0) - perror("minishell: dup2"); - close(app->stdin); - close(app->stdout); - close(app->stderr); -} - -t_minishell *handle_redirections(t_redirect *redirections, t_minishell *app) -{ - app->stdin = dup(STDIN_FILENO); - app->stdout = dup(STDOUT_FILENO); - app->stderr = dup(STDERR_FILENO); + std_fds_save(fds); while (redirections != NULL) { if (do_redirection(redirections) == NULL) { - restore_std_fds(app); + restore_std_fds(fds); app->last_return_value = 1; return (NULL); } diff --git a/src/executing/simple_cmd/handle_redirections.h b/src/executing/simple_cmd/handle_redirections.h index 472b975..b79e87a 100644 --- a/src/executing/simple_cmd/handle_redirections.h +++ b/src/executing/simple_cmd/handle_redirections.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:14:29 by khais #+# #+# */ -/* Updated: 2025/04/28 11:39:41 by khais ### ########.fr */ +/* Updated: 2025/04/28 12:45:17 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,8 +15,7 @@ # include "../../minishell.h" -void restore_std_fds(t_minishell *app); t_minishell *handle_redirections(t_redirect *redirections, - t_minishell *app); + t_minishell *app, t_std_fds *fds); #endif // HANDLE_REDIRECTIONS_H diff --git a/src/executing/simple_cmd/simple_cmd_execute.c b/src/executing/simple_cmd/simple_cmd_execute.c index 7b52786..876999f 100644 --- a/src/executing/simple_cmd/simple_cmd_execute.c +++ b/src/executing/simple_cmd/simple_cmd_execute.c @@ -6,12 +6,13 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/27 16:21:56 by khais #+# #+# */ -/* Updated: 2025/04/28 11:38:41 by khais ### ########.fr */ +/* Updated: 2025/04/28 12:48:38 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "../../ft_errno.h" #include "simple_cmd_execute.h" +#include "std_fds.h" #include "builtins.h" #include "subprocess.h" #include "libft.h" @@ -71,7 +72,8 @@ static t_simple_cmd *post_process_command(t_simple_cmd *cmd, t_minishell *app) return (cmd); } -static void exec_external_cmd(t_simple_cmd *cmd, t_minishell *app) +static void exec_external_cmd(t_simple_cmd *cmd, t_minishell *app, + t_std_fds *fds) { char *exe; int pid; @@ -81,8 +83,8 @@ static void exec_external_cmd(t_simple_cmd *cmd, t_minishell *app) return (command_not_found(cmd, app)); pid = fork(); if (pid == 0) - execute_subprocess(exe, cmd, app); - restore_std_fds(app); + execute_subprocess(exe, cmd, app, fds); + restore_std_fds(fds); free(exe); do_waitpid(app, pid); } @@ -90,6 +92,8 @@ static void exec_external_cmd(t_simple_cmd *cmd, t_minishell *app) void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app, bool should_exit) { + t_std_fds fds; + if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL) return ; ft_errno(FT_ESUCCESS); @@ -100,10 +104,10 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app, return ; } simple_cmd_execute_debug(cmd, app); - if (handle_redirections(cmd->redirections, app) == NULL) + if (handle_redirections(cmd->redirections, app, &fds) == NULL) return ; - if (execute_builtin(cmd, app) == BUILTIN_INVALID) - exec_external_cmd(cmd, app); + if (execute_builtin(cmd, app, &fds) == BUILTIN_INVALID) + exec_external_cmd(cmd, app, &fds); if (should_exit) exit(app->last_return_value); } diff --git a/src/executing/simple_cmd/std_fds.c b/src/executing/simple_cmd/std_fds.c new file mode 100644 index 0000000..aeccfca --- /dev/null +++ b/src/executing/simple_cmd/std_fds.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* std_fds.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/28 12:44:14 by khais #+# #+# */ +/* Updated: 2025/04/28 12:45:36 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "std_fds.h" +#include + +void std_fds_close(t_std_fds *fds) +{ + close(fds->stdin); + close(fds->stdout); + close(fds->stderr); +} + +void restore_std_fds(t_std_fds *fds) +{ + if (dup2(fds->stdin, STDIN_FILENO) < 0) + perror("minishell: dup2"); + if (dup2(fds->stdout, STDOUT_FILENO) < 0) + perror("minishell: dup2"); + if (dup2(fds->stderr, STDERR_FILENO) < 0) + perror("minishell: dup2"); + std_fds_close(fds); +} + +void std_fds_save(t_std_fds *fds) +{ + fds->stdin = dup(STDIN_FILENO); + fds->stdout = dup(STDOUT_FILENO); + fds->stderr = dup(STDERR_FILENO); +} diff --git a/src/executing/simple_cmd/std_fds.h b/src/executing/simple_cmd/std_fds.h new file mode 100644 index 0000000..a7a96d8 --- /dev/null +++ b/src/executing/simple_cmd/std_fds.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* std_fds.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/28 12:42:28 by khais #+# #+# */ +/* Updated: 2025/04/28 12:45:40 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STD_FDS_H +# define STD_FDS_H + +# include "../../minishell.h" + +void std_fds_close(t_std_fds *fds); +void restore_std_fds(t_std_fds *fds); +void std_fds_save(t_std_fds *fds); + +#endif // STD_FDS_H diff --git a/src/executing/simple_cmd/subprocess.c b/src/executing/simple_cmd/subprocess.c index 4e9f2e5..3f670c2 100644 --- a/src/executing/simple_cmd/subprocess.c +++ b/src/executing/simple_cmd/subprocess.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/02 18:19:23 by khais #+# #+# */ -/* Updated: 2025/04/24 17:40:01 by khais ### ########.fr */ +/* Updated: 2025/04/28 12:48:18 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #include "../../env/env_convert.h" #include "../../parser/simple_cmd/simple_cmd.h" #include "../../subst/path_split.h" +#include "std_fds.h" #include #include #include @@ -70,13 +71,12 @@ static int ft_execve(char *exe, char **argv, char **envp) return (retvalue); } -void execute_subprocess(char *exe, t_simple_cmd *cmd, t_minishell *app) +void execute_subprocess(char *exe, t_simple_cmd *cmd, t_minishell *app, + t_std_fds *fds) { int retvalue; - close(app->stdin); - close(app->stdout); - close(app->stderr); + std_fds_close(fds); retvalue = ft_execve(exe, argv_from_wordlist(cmd->words), envp_from_env(app->env)); simple_cmd_destroy(cmd); diff --git a/src/executing/simple_cmd/subprocess.h b/src/executing/simple_cmd/subprocess.h index 3e877d4..a2bccbd 100644 --- a/src/executing/simple_cmd/subprocess.h +++ b/src/executing/simple_cmd/subprocess.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/02 18:17:48 by khais #+# #+# */ -/* Updated: 2025/04/02 18:21:30 by khais ### ########.fr */ +/* Updated: 2025/04/28 12:42:06 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ # include "../../minishell.h" -void execute_subprocess(char *exe, t_simple_cmd *cmd, t_minishell *app); +void execute_subprocess(char *exe, t_simple_cmd *cmd, t_minishell *app, + t_std_fds *fds); #endif // SUBPROCESS_H diff --git a/src/minishell.h b/src/minishell.h index e7ff80a..e1dd053 100644 --- a/src/minishell.h +++ b/src/minishell.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/09 14:02:47 by khais #+# #+# */ -/* Updated: 2025/04/21 10:59:33 by khais ### ########.fr */ +/* Updated: 2025/04/28 12:11:44 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,13 @@ # include # include +typedef struct s_std_fds +{ + int stdin; + int stdout; + int stderr; +} t_std_fds; + typedef union u_redirectee { int dest; // file descriptor to redirect @@ -122,9 +129,6 @@ typedef struct s_minishell int last_return_value; bool debug; bool exec; - int stdin; - int stdout; - int stderr; } t_minishell; t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back);