refactor(std_fds): allow for ability to store multiple std fds savesets

This commit is contained in:
Khaïs COLIN 2025-04-28 12:11:27 +02:00
parent 255a1382da
commit bd06d9f19c
11 changed files with 105 additions and 46 deletions

View file

@ -50,6 +50,7 @@ srcs = \
src/executing/simple_cmd/handle_redirections.c \ src/executing/simple_cmd/handle_redirections.c \
src/executing/simple_cmd/simple_cmd_execute.c \ src/executing/simple_cmd/simple_cmd_execute.c \
src/executing/simple_cmd/simple_cmd_execute_debug.c \ src/executing/simple_cmd/simple_cmd_execute_debug.c \
src/executing/simple_cmd/std_fds.c \
src/executing/simple_cmd/subprocess.c \ src/executing/simple_cmd/subprocess.c \
src/ft_errno.c \ src/ft_errno.c \
src/get_command.c \ src/get_command.c \

View file

@ -6,14 +6,14 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 16:37:21 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 "builtins.h"
#include "libft.h" #include "libft.h"
#include "simple_cmd_execute.h" #include "simple_cmd_execute.h"
#include "handle_redirections.h" #include "std_fds.h"
t_builtin_type get_builtin(t_simple_cmd *cmd) 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); 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; t_builtin_type type;
static t_builtin_type (*builtins[])(t_simple_cmd *, t_minishell *) = { 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); type = get_builtin(cmd);
builtins[type](cmd, app); builtins[type](cmd, app);
if (type != BUILTIN_INVALID) if (type != BUILTIN_INVALID)
restore_std_fds(app); restore_std_fds(fds);
return (type); return (type);
} }

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 14:16:13 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 builtin_unset(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type get_builtin(t_simple_cmd *cmd); 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 #endif // BUILTINS_H

View file

@ -6,11 +6,12 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:14:40 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 "handle_redirections.h"
#include "std_fds.h"
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
@ -43,29 +44,15 @@ static t_redirect *do_redirection(t_redirect *redirection)
return (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) std_fds_save(fds);
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);
while (redirections != NULL) while (redirections != NULL)
{ {
if (do_redirection(redirections) == NULL) if (do_redirection(redirections) == NULL)
{ {
restore_std_fds(app); restore_std_fds(fds);
app->last_return_value = 1; app->last_return_value = 1;
return (NULL); return (NULL);
} }

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:14:29 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" # include "../../minishell.h"
void restore_std_fds(t_minishell *app);
t_minishell *handle_redirections(t_redirect *redirections, t_minishell *handle_redirections(t_redirect *redirections,
t_minishell *app); t_minishell *app, t_std_fds *fds);
#endif // HANDLE_REDIRECTIONS_H #endif // HANDLE_REDIRECTIONS_H

View file

@ -6,12 +6,13 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 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 "../../ft_errno.h"
#include "simple_cmd_execute.h" #include "simple_cmd_execute.h"
#include "std_fds.h"
#include "builtins.h" #include "builtins.h"
#include "subprocess.h" #include "subprocess.h"
#include "libft.h" #include "libft.h"
@ -71,7 +72,8 @@ static t_simple_cmd *post_process_command(t_simple_cmd *cmd, t_minishell *app)
return (cmd); 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; char *exe;
int pid; int pid;
@ -81,8 +83,8 @@ static void exec_external_cmd(t_simple_cmd *cmd, t_minishell *app)
return (command_not_found(cmd, app)); return (command_not_found(cmd, app));
pid = fork(); pid = fork();
if (pid == 0) if (pid == 0)
execute_subprocess(exe, cmd, app); execute_subprocess(exe, cmd, app, fds);
restore_std_fds(app); restore_std_fds(fds);
free(exe); free(exe);
do_waitpid(app, pid); 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, void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app,
bool should_exit) bool should_exit)
{ {
t_std_fds fds;
if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL) if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL)
return ; return ;
ft_errno(FT_ESUCCESS); ft_errno(FT_ESUCCESS);
@ -100,10 +104,10 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app,
return ; return ;
} }
simple_cmd_execute_debug(cmd, app); simple_cmd_execute_debug(cmd, app);
if (handle_redirections(cmd->redirections, app) == NULL) if (handle_redirections(cmd->redirections, app, &fds) == NULL)
return ; return ;
if (execute_builtin(cmd, app) == BUILTIN_INVALID) if (execute_builtin(cmd, app, &fds) == BUILTIN_INVALID)
exec_external_cmd(cmd, app); exec_external_cmd(cmd, app, &fds);
if (should_exit) if (should_exit)
exit(app->last_return_value); exit(app->last_return_value);
} }

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* std_fds.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/28 12:44:14 by khais #+# #+# */
/* Updated: 2025/04/28 12:45:36 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "std_fds.h"
#include <stdio.h>
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);
}

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* std_fds.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/02 18:19:23 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 "../../env/env_convert.h"
#include "../../parser/simple_cmd/simple_cmd.h" #include "../../parser/simple_cmd/simple_cmd.h"
#include "../../subst/path_split.h" #include "../../subst/path_split.h"
#include "std_fds.h"
#include <dirent.h> #include <dirent.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
@ -70,13 +71,12 @@ static int ft_execve(char *exe, char **argv, char **envp)
return (retvalue); 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; int retvalue;
close(app->stdin); std_fds_close(fds);
close(app->stdout);
close(app->stderr);
retvalue = ft_execve(exe, argv_from_wordlist(cmd->words), retvalue = ft_execve(exe, argv_from_wordlist(cmd->words),
envp_from_env(app->env)); envp_from_env(app->env));
simple_cmd_destroy(cmd); simple_cmd_destroy(cmd);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/02 18:17:48 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" # 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 #endif // SUBPROCESS_H

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/09 14:02:47 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 <sys/stat.h> # include <sys/stat.h>
# include <fcntl.h> # include <fcntl.h>
typedef struct s_std_fds
{
int stdin;
int stdout;
int stderr;
} t_std_fds;
typedef union u_redirectee typedef union u_redirectee
{ {
int dest; // file descriptor to redirect int dest; // file descriptor to redirect
@ -122,9 +129,6 @@ typedef struct s_minishell
int last_return_value; int last_return_value;
bool debug; bool debug;
bool exec; bool exec;
int stdin;
int stdout;
int stderr;
} t_minishell; } t_minishell;
t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back); t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back);