diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index afc7afa3..345b5afb 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2016/11/28 18:26:20 by jhalford ### ########.fr */ +/* Updated: 2016/11/29 19:22:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,9 @@ int exec_great(t_btree *ast, t_data *data); int exec_dgreat(t_btree *ast, t_data *data); int exec_command(t_btree *ast, t_data *data); +void fd_redirect(t_data *data); +void fd_reset(t_data *data); + int ft_cmd_process(char **argv, t_data *data); int ft_cmd_exec(char *execpath, char **argv, t_data *data); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index ec2e308b..d6efd524 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2016/11/29 15:32:43 by jhalford ### ########.fr */ +/* Updated: 2016/11/29 20:08:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,8 @@ struct s_data t_list *qstack; int fdin; int fdout; + int save_in; + int save_out; }; extern t_stof g_builtins[]; diff --git a/42sh/src/builtin/builtin.c b/42sh/src/builtin/builtin.c index 773c8feb..c1f162bb 100644 --- a/42sh/src/builtin/builtin.c +++ b/42sh/src/builtin/builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:21:34 by jhalford #+# #+# */ -/* Updated: 2016/11/28 14:27:36 by jhalford ### ########.fr */ +/* Updated: 2016/11/29 19:45:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,8 +32,10 @@ int ft_builtin(char **av, t_data *data) { if (ft_strcmp(g_builtin[i].name, *av) == 0) { + fd_redirect(data); ret = (g_builtin[i].f)(av, data); builtin_setenv((char*[3]){"?", ft_itoa(ret)}, data); + fd_reset(data); return (1); } i++; diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index e05934b4..fb2edb63 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2016/11/28 18:34:13 by jhalford ### ########.fr */ +/* Updated: 2016/11/29 20:29:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_dgreat.c b/42sh/src/exec/exec_dgreat.c index efe0e3e7..cfb62633 100644 --- a/42sh/src/exec/exec_dgreat.c +++ b/42sh/src/exec/exec_dgreat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:15:13 by jhalford #+# #+# */ -/* Updated: 2016/11/28 18:36:11 by jhalford ### ########.fr */ +/* Updated: 2016/11/29 20:29:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_great.c b/42sh/src/exec/exec_great.c index b757ae4d..d9b193de 100644 --- a/42sh/src/exec/exec_great.c +++ b/42sh/src/exec/exec_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */ -/* Updated: 2016/11/28 18:35:15 by jhalford ### ########.fr */ +/* Updated: 2016/11/29 20:26:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_less.c b/42sh/src/exec/exec_less.c index 2fe9a2d9..9e2250ef 100644 --- a/42sh/src/exec/exec_less.c +++ b/42sh/src/exec/exec_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */ -/* Updated: 2016/11/28 18:19:26 by jhalford ### ########.fr */ +/* Updated: 2016/11/29 20:29:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,10 @@ int exec_less(t_btree *ast, t_data *data) node = ast->item; fd = open(node->u_data.redir.u_word.word, O_RDONLY); - dup2(fd, node->u_data.redir.n); - close(fd); + data->fdin = fd; - return (ft_exec(ast->left, data)); + ft_exec(ast->left, data); + + data->fdin = STDIN; + return (0); } diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 20344d8c..fa194bc6 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */ -/* Updated: 2016/11/28 18:10:26 by jhalford ### ########.fr */ +/* Updated: 2016/11/29 19:19:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,8 @@ int exec_pipe(t_btree *ast, t_data *data) ft_exec(ast->left, data); + if (data->fdout != STDOUT) + close(data->fdout); data->fdout = STDOUT; data->fdin = fds[PIPE_READ]; diff --git a/42sh/src/exec/fd_redirect.c b/42sh/src/exec/fd_redirect.c new file mode 100644 index 00000000..95220ca2 --- /dev/null +++ b/42sh/src/exec/fd_redirect.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_redirect.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ +/* Updated: 2016/11/29 20:21:17 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void fd_redirect(t_data *data) +{ + if (data->fdin != STDIN) + { + /* ft_dprintf(2, "redirecting input: %i->STDIN\n", data->fdin); */ + dup2(data->fdin, STDIN); + close(data->fdin); + } + if (data->fdout != STDOUT) + { + /* ft_dprintf(2, "redirecting output: %i->STDOUT\n", data->fdout); */ + dup2(data->fdout, STDOUT); + close(data->fdout); + } +} diff --git a/42sh/src/exec/fd_reset.c b/42sh/src/exec/fd_reset.c new file mode 100644 index 00000000..60d195df --- /dev/null +++ b/42sh/src/exec/fd_reset.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fd_reset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/29 19:20:05 by jhalford #+# #+# */ +/* Updated: 2016/11/29 19:47:01 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void fd_reset(t_data *data) +{ + dup2(data->save_in, STDIN); + dup2(data->save_out, STDOUT); + data->fdin = STDIN; + data->fdout = STDOUT; +} diff --git a/42sh/src/minishell-exec/ft_cmd.c b/42sh/src/exec/ft_cmd.c similarity index 75% rename from 42sh/src/minishell-exec/ft_cmd.c rename to 42sh/src/exec/ft_cmd.c index d6087da9..eaf76ba6 100644 --- a/42sh/src/minishell-exec/ft_cmd.c +++ b/42sh/src/exec/ft_cmd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:18 by jhalford #+# #+# */ -/* Updated: 2016/11/28 18:32:49 by jhalford ### ########.fr */ +/* Updated: 2016/11/29 20:22:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ int ft_cmd_process(char **argv, t_data *data) execpath = argv[0]; else if (!(execpath = ft_findexec(path, argv[0]))) { - ft_dprintf(2, "minishell: command not found: %s\n", argv[0]); + ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, argv[0]); return (-1); } return (ft_cmd_exec(execpath, argv, data)); @@ -40,35 +40,22 @@ int ft_cmd_exec(char *execpath, char **argv, t_data *data) if (access(execpath, X_OK) == -1) { - ft_dprintf(2, "minishell: permission denied: %s\n", argv[0]); + ft_dprintf(2, "%s: permission denied: %s\n", SHELL_NAME, argv[0]); return (-1); } - ft_dprintf(2, "gonna fork, in=%i, out=%i\n", data->fdin, data->fdout); + /* ft_dprintf(2, "gonna fork, in=%i, out=%i\n", data->fdin, data->fdout); */ if ((pid = fork()) == -1) return (-1); else if (pid == 0) { - if (data->fdin != STDIN) - { - dup2(data->fdin, STDIN); - close(data->fdin); - } - if (data->fdout != STDOUT) - { - dup2(data->fdout, STDOUT); - close(data->fdout); - } + fd_redirect(data); environ = ft_sstrdup(data->env); execve(execpath, argv, environ); } else { g_pid = pid; - if (data->fdin != STDIN) - close(data->fdin); - if (data->fdout != STDOUT) - close(data->fdout); - else + if (data->fdout == STDOUT) { waitpid(pid, &status, 0); builtin_setenv((char*[3]){"?", ft_itoa(status)}, data); diff --git a/42sh/src/lexer/qstate_update.c b/42sh/src/lexer/qstate_update.c index a415553b..12c62b27 100644 --- a/42sh/src/lexer/qstate_update.c +++ b/42sh/src/lexer/qstate_update.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/28 16:56:11 by jhalford #+# #+# */ -/* Updated: 2016/11/29 15:38:51 by jhalford ### ########.fr */ +/* Created: 2016/11/29 15:56:40 by jhalford #+# #+# */ +/* Updated: 2016/11/29 15:57:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_key_del.c b/42sh/src/line-editing/ft_key_del.c index d8f4df97..f2c34cbc 100644 --- a/42sh/src/line-editing/ft_key_del.c +++ b/42sh/src/line-editing/ft_key_del.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_key_del.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/10 13:44:26 by jhalford #+# #+# */ -/* Updated: 2016/11/29 13:46:30 by jhalford ### ########.fr */ +/* Created: 2016/11/29 15:57:43 by jhalford #+# #+# */ +/* Updated: 2016/11/29 15:57:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_key_enter.c b/42sh/src/line-editing/ft_key_enter.c index e8739a4e..39531035 100644 --- a/42sh/src/line-editing/ft_key_enter.c +++ b/42sh/src/line-editing/ft_key_enter.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_key_enter.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/10 13:44:30 by jhalford #+# #+# */ -/* Updated: 2016/11/29 13:50:20 by jhalford ### ########.fr */ +/* Created: 2016/11/29 15:56:36 by jhalford #+# #+# */ +/* Updated: 2016/11/29 15:56:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 283a9dc4..83fa5658 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2016/11/28 19:28:22 by jhalford ### ########.fr */ +/* Updated: 2016/11/29 19:46:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,8 @@ int data_init(t_data *data) data->history = NULL; data->fdin = STDIN; data->fdout = STDOUT; + data->save_in = dup(STDIN); + data->save_out = dup(STDOUT); if (!(data->history = ft_dlstnew(NULL, 0))) return (-1); return (0); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 46f3fff9..169473be 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:34 by jhalford #+# #+# */ -/* Updated: 2016/11/28 19:28:03 by jhalford ### ########.fr */ +/* Updated: 2016/11/29 20:08:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,18 +27,18 @@ int main(void) { if (ft_interactive_sh(&data)) return (1); - ft_dprintf(STDERR, "command='%s'\n", data.input); + /* ft_dprintf(STDERR, "command='%s'\n", data.input); */ if (ft_tokenize(&token, data.input, DEFAULT)) return (1); if (!token) continue ; - token_print(token); + /* token_print(token); */ ast = NULL; if (ft_parse(&ast, &token)) return (1); - btree_print(STDERR, ast, &ft_putast); - ft_dprintf(STDERR, "\n--- INFIX BREAKDOWN ---\n"); - btree_apply_infix(ast, &ft_putast2); + /* btree_print(STDERR, ast, &ft_putast); */ + /* ft_dprintf(STDERR, "\n--- INFIX BREAKDOWN ---\n"); */ + /* btree_apply_infix(ast, &ft_putast2); */ /* ft_lstdel(&token, &token_free); */ token = NULL; if (ft_exec(ast, &data))