diff --git a/42sh/Makefile b/42sh/Makefile index 4992fc50..9f9ff9f9 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,14 +6,14 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/02/06 21:49:51 by jhalford ### ########.fr # +# Updated: 2017/02/07 16:11:13 by jhalford ### ########.fr # # # # **************************************************************************** # NAME = 42sh CC = gcc -FLAGS = -Wall -Wextra -Werror -g +FLAGS = -Wall -Wextra -Werror D_FLAGS = -g DELTA = $$(echo "$$(tput cols)-47"|bc) @@ -54,6 +54,7 @@ completion/c_terminal.c\ completion/completion.c\ exec/ast_free.c\ exec/bad_fd.c\ +exec/close_fdsave.c\ exec/exec_ampersand.c\ exec/exec_and_if.c\ exec/exec_command.c\ @@ -67,6 +68,7 @@ exec/ft_findexec.c\ exec/launch_process.c\ exec/process_redirect.c\ exec/process_reset.c\ +exec/process_resetfds.c\ exec/process_setexec.c\ exec/process_setgroup.c\ exec/process_setsig.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 67e47330..679f5e20 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: 2017/02/07 12:09:05 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 17:49:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,6 +59,9 @@ struct s_exec int aol_search; t_job job; t_process process; + int fd0save; + int fd1save; + int fd2save; }; struct s_execmap @@ -93,10 +96,12 @@ int process_setexec(t_type type, t_process *p); int process_setgroup(t_process *p, pid_t pid); void process_setsig(void); void process_free(void *content, size_t content_size); -void process_reset(void); +void process_reset(t_process *p); +void process_resetfds(void); +void close_fdsave(void); int fd_is_valid(int fd); -void bad_fd(int fd); +int bad_fd(int fd); int process_redirect(t_process *p); int redirect_great(t_redir *redir); int redirect_less(t_redir *redir); diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 5e41aab9..43599ec9 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/02/07 12:56:46 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 15:25:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft b/42sh/libft index 92902c1c..6437a054 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 92902c1c6539152d9daadaf2a018b2d344f1224e +Subproject commit 6437a0545bf0eec54bc3ac4fa4bc24d83efcf384 diff --git a/42sh/src/exec/bad_fd.c b/42sh/src/exec/bad_fd.c index 4d5399d3..031c26cf 100644 --- a/42sh/src/exec/bad_fd.c +++ b/42sh/src/exec/bad_fd.c @@ -6,15 +6,15 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:32:43 by jhalford #+# #+# */ -/* Updated: 2017/02/06 22:34:21 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 16:02:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void bad_fd(int fd) +int bad_fd(int fd) { ft_dprintf(2, "{red}%s: %i: Bad file descriptor{eoc}\n", SHELL_NAME, fd); - exit(1); + return (1); } diff --git a/42sh/src/exec/close_fdsave.c b/42sh/src/exec/close_fdsave.c new file mode 100644 index 00000000..a012deeb --- /dev/null +++ b/42sh/src/exec/close_fdsave.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* close_fdsave.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/07 17:45:23 by jhalford #+# #+# */ +/* Updated: 2017/02/07 17:54:07 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +void close_fdsave(void) +{ + t_exec *exec; + + exec = &data_singleton()->exec; + close(exec->fd0save); + close(exec->fd1save); + close(exec->fd2save); +} diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 49bdfc0e..ebd8335f 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: 2017/02/07 12:14:31 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 17:44:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,10 +65,7 @@ int exec_command(t_btree **ast) job->pgid = 0; } } - p->av = NULL; - p->pid = 0; - p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK); - p->redirs = NULL; + process_reset(p); btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_redir.c b/42sh/src/exec/exec_redir.c index 8cb33701..1f5c31b5 100644 --- a/42sh/src/exec/exec_redir.c +++ b/42sh/src/exec/exec_redir.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */ -/* Updated: 2017/02/06 16:12:14 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 15:24:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index c0cb312f..49767b2c 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ -/* Updated: 2017/02/07 12:11:34 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 17:54:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,11 @@ int launch_process(t_process *p) set_exitstatus(127, 1); } else if (p->attributes & PROCESS_BUILTIN && IS_PIPESINGLE(p->attributes)) + { + if (process_redirect(p)) + return (1); set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env), 1); + } else { p->attributes &= ~PROCESS_STATE_MASK; @@ -39,9 +43,11 @@ int launch_process(t_process *p) pid = fork(); if (pid == 0) { + close_fdsave(); process_setgroup(p, 0); process_setsig(); - process_redirect(p); + if (process_redirect(p)) + exit (1); (*p->execf)(p->path, p->av, data_singleton()->env); exit(43); } @@ -52,7 +58,7 @@ int launch_process(t_process *p) return (0); } else if (pid == -1) - ft_dprintf(2, "{red}internal fork error{eoc}\n"); + ft_dprintf(2, "{red}%s: internal fork error{eoc}\n", SHELL_NAME); } return (1); } diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index dfe8615b..a5560582 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2017/02/07 12:01:06 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 17:36:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,18 +32,19 @@ int process_redirect(t_process *p) redirs = p->redirs; while (redirs) { - i = 0; redir = redirs->content; if (redir->n > 9) { bad_fd(redir->n); exit(1); } + i = 0; while (g_redirmap[i].type) { if (g_redirmap[i].type == redir->type) { - (g_redirmap[i].f)(redir); + if ((g_redirmap[i].f)(redir)) + return (1); break ; } i++; @@ -53,14 +54,8 @@ int process_redirect(t_process *p) if (p->toclose != STDIN) close(p->toclose); if (p->fdin != STDIN) - { - dup2(p->fdin, STDIN); - close(p->fdin); - } + dup2_close(p->fdin, STDIN); if (p->fdout != STDOUT) - { - dup2(p->fdout, STDOUT); - close(p->fdout); - } + dup2_close(p->fdout, STDOUT); return (0); } diff --git a/42sh/src/exec/process_reset.c b/42sh/src/exec/process_reset.c index 4b3c02a9..b90a131e 100644 --- a/42sh/src/exec/process_reset.c +++ b/42sh/src/exec/process_reset.c @@ -1,8 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* process_reset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/07 17:44:22 by jhalford #+# #+# */ +/* Updated: 2017/02/07 17:48:22 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "exec.h" -void process_reset(void) +void process_reset(t_process *p) { - t_data *data; - - data = data_singleton(); + process_resetfds(); + p->av = NULL; + p->pid = 0; + p->redirs = NULL; + p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK); } diff --git a/42sh/src/exec/process_resetfds.c b/42sh/src/exec/process_resetfds.c new file mode 100644 index 00000000..dbc9403a --- /dev/null +++ b/42sh/src/exec/process_resetfds.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* process_resetfds.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/07 17:39:14 by jhalford #+# #+# */ +/* Updated: 2017/02/07 17:50:52 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void process_resetfds(void) +{ + t_exec *exec; + + exec = &data_singleton()->exec; + dup2(exec->fd0save, 0); + dup2(exec->fd1save, 1); + dup2(exec->fd2save, 2); +} diff --git a/42sh/src/exec/redirect_greatand.c b/42sh/src/exec/redirect_greatand.c index 10abaa48..6e4bf37f 100644 --- a/42sh/src/exec/redirect_greatand.c +++ b/42sh/src/exec/redirect_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:12:31 by jhalford #+# #+# */ -/* Updated: 2017/02/07 12:01:45 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 17:54:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,26 +17,20 @@ int redirect_greatand(t_redir *redir) int fdold; int fdnew; - if (redir->word.fd == redir->n) - return (0); - if (redir->word.fd > 9) - bad_fd(redir->word.fd); if (redir->close) { close(redir->n); - return (1); - } - else - { - fdold = redir->word.fd; - fdnew = redir->n; + return (0); } + if (redir->word.fd == redir->n) + return (0); + if (redir->word.fd > 9) + return (bad_fd(redir->word.fd)); + fdold = redir->word.fd; + fdnew = redir->n; if (fd_is_valid(fdold)) - { - dup2(fdold, fdnew); - close(fdold); - } + dup2_close(fdold, fdnew); else - bad_fd(fdold); + return (bad_fd(fdold)); return (0); } diff --git a/42sh/src/exec/redirect_less.c b/42sh/src/exec/redirect_less.c index b68ca76a..38fda3bd 100644 --- a/42sh/src/exec/redirect_less.c +++ b/42sh/src/exec/redirect_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:09:53 by jhalford #+# #+# */ -/* Updated: 2017/02/06 22:38:46 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 16:05:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/redirect_lessand.c b/42sh/src/exec/redirect_lessand.c index fa4450b9..15da4123 100644 --- a/42sh/src/exec/redirect_lessand.c +++ b/42sh/src/exec/redirect_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:11:18 by jhalford #+# #+# */ -/* Updated: 2017/02/07 12:01:56 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 17:54:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,26 +17,23 @@ int redirect_lessand(t_redir *redir) int fdold; int fdnew; - if (redir->word.fd == redir->n) - return (0); - if (redir->word.fd > 9) - bad_fd(redir->word.fd); if (redir->close) { close(redir->n); - return (1); - } - else - { - fdold = redir->word.fd; - fdnew = redir->n; + return (0); } + if (redir->word.fd == redir->n) + return (0); + if (redir->word.fd > 9) + return (bad_fd(redir->word.fd)); + fdold = redir->word.fd; + fdnew = redir->n; if (fd_is_valid(fdold)) { dup2(fdold, fdnew); close(fdold); } else - bad_fd(fdold); + return (bad_fd(fdold)); return (0); } diff --git a/42sh/src/exec/set_exitstatus.c b/42sh/src/exec/set_exitstatus.c index 19c79d48..b38da178 100644 --- a/42sh/src/exec/set_exitstatus.c +++ b/42sh/src/exec/set_exitstatus.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2017/01/08 15:58:20 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 16:04:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ void set_exitstatus(int status, int override) exitval = 128 + WTERMSIG(status); else { - DG("fail: process was not exited nor signaled."); + DG("%s: process was not exited nor signaled.", SHELL_NAME); return ; } } diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 76bf0d90..62e5c03d 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: 2017/02/03 15:25:55 by gwojda ### ########.fr */ +/* Updated: 2017/02/07 17:23:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,9 @@ int data_init(void) data->exec.process.pid = 0; data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; data->exec.process.redirs = NULL; + data->exec.fd0save = dup(0); + data->exec.fd1save = dup(1); + data->exec.fd2save = dup(2); data->exec.aol_status = NULL; data->exec.aol_search = 0; diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 30f53822..ae3f8ec2 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/02/06 16:46:48 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 17:18:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_great.c b/42sh/src/parser/parse_great.c index 1c6f5dea..7fbc3110 100644 --- a/42sh/src/parser/parse_great.c +++ b/42sh/src/parser/parse_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/06 16:29:54 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 15:26:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_greatand.c b/42sh/src/parser/parse_greatand.c index fdfbd6ff..3950072f 100644 --- a/42sh/src/parser/parse_greatand.c +++ b/42sh/src/parser/parse_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/06 16:30:41 by jhalford ### ########.fr */ +/* Updated: 2017/02/07 15:32:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */