mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
refactor(std_fds): allow for ability to store multiple std fds savesets
This commit is contained in:
parent
255a1382da
commit
bd06d9f19c
11 changed files with 105 additions and 46 deletions
1
Makefile
1
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 \
|
||||
|
|
|
|||
|
|
@ -6,14 +6,14 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
|
|
|||
|
|
@ -6,11 +6,12 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
|
|
|||
|
|
@ -6,12 +6,13 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
|||
39
src/executing/simple_cmd/std_fds.c
Normal file
39
src/executing/simple_cmd/std_fds.c
Normal 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);
|
||||
}
|
||||
22
src/executing/simple_cmd/std_fds.h
Normal file
22
src/executing/simple_cmd/std_fds.h
Normal 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
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <dirent.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <fcntl.h>
|
||||
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue