diff --git a/Makefile b/Makefile index 2d6956b..5a2740f 100644 --- a/Makefile +++ b/Makefile @@ -44,8 +44,9 @@ srcs = \ src/executing/simple_cmd/builtin_export.c \ src/executing/simple_cmd/builtin_invalid.c \ src/executing/simple_cmd/builtin_pwd.c \ - src/executing/simple_cmd/builtins.c \ src/executing/simple_cmd/builtin_unset.c \ + src/executing/simple_cmd/builtins.c \ + 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/subprocess.c \ diff --git a/src/executing/simple_cmd/handle_redirections.c b/src/executing/simple_cmd/handle_redirections.c new file mode 100644 index 0000000..d2025f8 --- /dev/null +++ b/src/executing/simple_cmd/handle_redirections.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* handle_redirections.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/17 11:14:40 by khais #+# #+# */ +/* Updated: 2025/04/17 11:15:44 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "handle_redirections.h" +#include + +static t_redirect *do_redirection(t_redirect *redirection) +{ + int fd; + + if (redirection->type == FT_HEREDOC) + fd = redirection->redirectee.dest; + else + { + fd = open(redirection->redirectee.filename->word, + redirection->open_flags, redirection->c_flags); + if (fd < 0) + return (perror("minishell: open"), NULL); + } + if (dup2(fd, redirection->source) < 0) + return (perror("minishell: dup2"), NULL); + close(fd); + return (redirection); +} + +void restore_std_fds(t_minishell *app) +{ + 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_simple_cmd *handle_redirections(t_simple_cmd *cmd, t_minishell *app) +{ + t_redirect *redirections; + + app->stdin = dup(STDIN_FILENO); + app->stdout = dup(STDOUT_FILENO); + app->stderr = dup(STDERR_FILENO); + redirections = cmd->redirections; + while (redirections != NULL) + { + if (do_redirection(redirections) == NULL) + return (NULL); + redirections = redirections->next; + } + return (cmd); +} diff --git a/src/executing/simple_cmd/handle_redirections.h b/src/executing/simple_cmd/handle_redirections.h new file mode 100644 index 0000000..7a82c84 --- /dev/null +++ b/src/executing/simple_cmd/handle_redirections.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* handle_redirections.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/17 11:14:29 by khais #+# #+# */ +/* Updated: 2025/04/17 11:15:56 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef HANDLE_REDIRECTIONS_H +# define HANDLE_REDIRECTIONS_H + +# include "../../minishell.h" + +void restore_std_fds(t_minishell *app); +t_simple_cmd *handle_redirections(t_simple_cmd *cmd, t_minishell *app); + +#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 cf10def..27f2422 100644 --- a/src/executing/simple_cmd/simple_cmd_execute.c +++ b/src/executing/simple_cmd/simple_cmd_execute.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/27 16:21:56 by khais #+# #+# */ -/* Updated: 2025/04/16 16:54:19 by khais ### ########.fr */ +/* Updated: 2025/04/17 11:16:18 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,6 +27,7 @@ #include "../../postprocess/expansion/expand_wildcard.h" #include "simple_cmd_execute_debug.h" #include "../../parser/simple_cmd/simple_cmd.h" +#include "handle_redirections.h" static void command_not_found(t_simple_cmd *cmd, t_minishell *app) { @@ -35,40 +36,6 @@ static void command_not_found(t_simple_cmd *cmd, t_minishell *app) app->last_return_value = 127; } -static t_redirect *do_redirection(t_redirect *redirection) -{ - int fd; - - if (redirection->type == FT_HEREDOC) - fd = redirection->redirectee.dest; - else - { - fd = open(redirection->redirectee.filename->word, - redirection->open_flags, redirection->c_flags); - if (fd < 0) - return (perror("minishell: open"), NULL); - } - if (dup2(fd, redirection->source) < 0) - return (perror("minishell: dup2"), NULL); - close(fd); - return (redirection); -} - -static t_simple_cmd *handle_redirections(t_simple_cmd *cmd, t_minishell *app) -{ - t_redirect *redirections; - - (void)app; - redirections = cmd->redirections; - while (redirections != NULL) - { - if (do_redirection(redirections) == NULL) - return (NULL); - redirections = redirections->next; - } - return (cmd); -} - /* ** Do all the post-processing steps relating to a command. */ @@ -103,13 +70,11 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app, return ; exe = get_cmdpath(cmd->words->word->word, app); if (exe == NULL) - { - command_not_found(cmd, app); - return ; - } + return (command_not_found(cmd, app)); pid = fork(); if (pid == 0) execute_subprocess(exe, cmd, app); + restore_std_fds(app); free(exe); do_waitpid(app, pid); if (should_exit) diff --git a/src/executing/simple_cmd/subprocess.c b/src/executing/simple_cmd/subprocess.c index 7783c8f..5ad5aa7 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/08 13:23:59 by khais ### ########.fr */ +/* Updated: 2025/04/17 11:10:16 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,6 +76,9 @@ void execute_subprocess(char *exe, t_simple_cmd *cmd, t_minishell *app) { int retvalue; + close(app->stdin); + close(app->stdout); + close(app->stderr); retvalue = ft_execve(exe, argv_from_wordlist(cmd->words), envp_from_env(app->env)); simple_cmd_destroy(cmd); diff --git a/src/minishell.h b/src/minishell.h index d6f9c0f..983760a 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/15 12:02:18 by khais ### ########.fr */ +/* Updated: 2025/04/17 11:08:59 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -120,6 +120,9 @@ 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); diff --git a/src/parser/cmd/cmd_destroy.c b/src/parser/cmd/cmd_destroy.c index b6f2bc9..dbc0494 100644 --- a/src/parser/cmd/cmd_destroy.c +++ b/src/parser/cmd/cmd_destroy.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/09 16:53:02 by khais #+# #+# */ -/* Updated: 2025/04/15 13:49:15 by khais ### ########.fr */ +/* Updated: 2025/04/17 10:20:38 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,8 @@ void redirect_destroy(t_redirect *redirect) { next = redirect->next; free(redirect->here_doc_eof); - worddesc_destroy(redirect->redirectee.filename); + if (redirect->type != FT_HEREDOC) + worddesc_destroy(redirect->redirectee.filename); free(redirect); redirect = next; } diff --git a/src/parser/redirect/redirect_from_words.c b/src/parser/redirect/redirect_from_words.c index 0bdbb31..0d6b778 100644 --- a/src/parser/redirect/redirect_from_words.c +++ b/src/parser/redirect/redirect_from_words.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 17:31:35 by khais #+# #+# */ -/* Updated: 2025/04/14 17:32:47 by khais ### ########.fr */ +/* Updated: 2025/04/17 10:28:35 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,7 @@ static int redir_source_from_type(t_redir_type type) { if (type == FT_OUTPUT_APPEND_REDIR || type == FT_OUTPUT_TRUNC_REDIR) return (STDOUT_FILENO); - if (type == FT_INPUT_REDIR) + if (type == FT_INPUT_REDIR || type == FT_HEREDOC) return (STDIN_FILENO); return (-1); } diff --git a/test.sh b/test.sh index 06d465a..45fb30e 100755 --- a/test.sh +++ b/test.sh @@ -688,4 +688,13 @@ expecting <<"EOF" minishell: syntax error near unexpected token `>' EOF +when_run <