diff --git a/42sh/Makefile b/42sh/Makefile index e647c32f..0585fe4c 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -82,7 +82,6 @@ exec/loop_del.c\ exec/loop_exec.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 74e303ee..d5666a6e 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/20 20:20:15 by ariard ### ########.fr */ +/* Updated: 2017/02/21 21:41:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,19 +21,17 @@ # define PROCESS_SCRIPT (1 << 2) # define PROCESS_SUBSHELL (1 << 3) # define PROCESS_UNKNOWN (1 << 4) -# define PROCESS_PIPESTART (1 << 5) -# define PROCESS_PIPEEND (1 << 6) -# define PROCESS_COMPLETED (1 << 7) -# define PROCESS_SUSPENDED (1 << 8) -# define PROCESS_RUNNING (1 << 9) -# define PROCESS_CONTINUED (1 << 10) +# define PROCESS_COMPLETED (1 << 5) +# define PROCESS_SUSPENDED (1 << 6) +# define PROCESS_RUNNING (1 << 7) +# define PROCESS_CONTINUED (1 << 8) # define PROCESS_TYPE_MASK (1 << 0 | 1 << 1 | 1 << 2 | 1 << 3 | 1 << 4) -# define PROCESS_STATE_MASK (1 << 7 | 1 << 8 | 1 << 9 | 1 << 10) +# define PROCESS_STATE_MASK (1 << 5 | 1 << 6 | 1 << 7 | 1 << 8) -# define IS_PIPESTART(a) (a & PROCESS_PIPESTART) -# define IS_PIPEEND(a) (a & PROCESS_PIPEEND) -# define IS_PIPESINGLE(a) ((a & PROCESS_PIPESTART) && (a & PROCESS_PIPEEND)) +# define IS_PIPESTART(p) (p->fdin == STDIN) +# define IS_PIPEEND(p) (p->fdout == STDOUT) +# define IS_PIPESINGLE(p) (IS_PIPESTART(p) && IS_PIPEEND(p)) # define SCRIPT_LOOP (1 << 0) @@ -49,8 +47,9 @@ struct s_process pid_t pid; int fdin; int fdout; + int pipe_count; + int to_close; t_list *redirs; - int toclose; int status; t_flag attributes; t_flag script; @@ -96,7 +95,7 @@ int exec_pipe(t_btree **ast); int exec_redir(t_btree **ast); int exec_command(t_btree **ast); -int exec_while(t_btree **ast); +int exec_while(t_btree **ast); int exec_if(t_btree **ast); int exec_elif(t_btree **ast); int exec_else(t_btree **ast); diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 042aed97..f9d7f709 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/20 22:26:26 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 22:39:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index 597dcb1f..2725127f 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ -/* Updated: 2017/02/20 20:30:54 by ariard ### ########.fr */ +/* Updated: 2017/02/21 22:40:59 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,9 @@ t_execf *is_builtin(t_process *p) i = -1; while (g_builtin[++i].name) + { if (ft_strcmp(g_builtin[i].name, p->av[0]) == 0) return (g_builtin[i].f); + } return (NULL); } diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 558804af..37d8153a 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/20 22:43:53 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 22:41:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,16 +48,28 @@ int exec_command(t_btree **ast) t_astnode *node; t_process *p; t_job *job; + int fds[2]; node = (*ast)->item; p = &data_singleton()->exec.process; job = &data_singleton()->exec.job; - p->av = token_to_argv(node); + if (!(p->av = token_to_argv(node))) + { + DG("globbing error"); + return (1); + } process_setexec(node->type, p); + if (p->pipe_count) + { + pipe(fds); + p->fdout = fds[PIPE_WRITE]; + p->to_close = fds[PIPE_READ]; + p->pipe_count--; + } if (!(launch_process(p))) { job_addprocess(p); - if (IS_PIPEEND(p->attributes)) + if (IS_PIPEEND(p)) { JOB_IS_FG(job->attributes) ? put_job_in_foreground(job, 0): @@ -65,6 +77,8 @@ int exec_command(t_btree **ast) job->pgid = 0; } } + if (p->fdout == fds[PIPE_WRITE]) + p->fdin = fds[PIPE_READ]; process_reset(p); // btree_delone(ast, &ast_free); return (0); diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 142f18c3..9a1a90bc 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: 2017/02/20 20:32:52 by ariard ### ########.fr */ +/* Updated: 2017/02/21 21:47:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,36 +14,15 @@ int exec_pipe(t_btree **ast) { - int fds[2]; - int start; t_data *data; t_process *p; + DG("exec pipe"); data = data_singleton(); - p = &data_singleton()->exec.process; - pipe(fds); - /* DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); */ - p->fdout = fds[PIPE_WRITE]; - start = IS_PIPESTART(p->attributes); - p->toclose = fds[PIPE_READ]; - - p->attributes &= ~PROCESS_PIPEEND; + p = &data->exec.process; + p->pipe_count++; ft_exec(&(*ast)->left); - p->attributes &= ~PROCESS_PIPESTART; - - p->toclose = STDIN; - close(fds[PIPE_WRITE]); - p->fdout = STDOUT; - p->fdin = fds[PIPE_READ]; - - p->attributes |= PROCESS_PIPEEND; ft_exec(&(*ast)->right); - if (start) - p->attributes |= PROCESS_PIPESTART; - - close(fds[PIPE_READ]); - p->fdin = STDIN; - - btree_delone(ast, &ast_free); + /* btree_delone(ast, &ast_free); */ return (0); } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index d2916952..6313a504 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2017/02/21 18:15:31 by ariard ### ########.fr */ +/* Updated: 2017/02/21 21:37:58 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,8 +49,8 @@ int ft_exec(t_btree **ast) { if (item->type == g_execmap[i].type) { - DG("match : %s and %s", - read_state(item->type), read_state(g_execmap[i].type)); + /* DG("match : %s and %s", */ + /* read_state(item->type), read_state(g_execmap[i].type)); */ /* return ((*g_execmap[i].f)(ast)); */ (*g_execmap[i].f)(ast); } diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 098c44c4..1676f69f 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/21 20:09:54 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 21:39:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ int launch_process(t_process *p) ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); set_exitstatus(127, 1); } - else if (p->attributes & PROCESS_BUILTIN && IS_PIPESINGLE(p->attributes)) + else if (p->attributes & PROCESS_BUILTIN && IS_PIPESINGLE(p)) { if (process_redirect(p)) return (1); diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index 33654306..a4253467 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 17:36:46 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 21:44:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,12 +48,11 @@ int process_redirect(t_process *p) } redirs = redirs->next; } - if (p->toclose != STDIN) - close(p->toclose); + if (p->to_close != 0) + close(p->to_close); if (p->fdin != STDIN) - dup2_close(p->fdin, STDIN); + dup2_close(p->fdout, STDOUT); if (p->fdout != STDOUT) dup2_close(p->fdout, STDOUT); - ft_lstdel(&p->redirs, ft_lst_cfree); return (0); } diff --git a/42sh/src/exec/process_reset.c b/42sh/src/exec/process_reset.c index b90a131e..853325b4 100644 --- a/42sh/src/exec/process_reset.c +++ b/42sh/src/exec/process_reset.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/07 17:44:22 by jhalford #+# #+# */ -/* Updated: 2017/02/07 17:48:22 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 21:42:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,8 @@ void process_reset(t_process *p) { - process_resetfds(); + p->fdout = STDOUT; + p->to_close = 0; p->av = NULL; p->pid = 0; p->redirs = NULL; diff --git a/42sh/src/exec/process_resetfds.c b/42sh/src/exec/process_resetfds.c deleted file mode 100644 index dbc9403a..00000000 --- a/42sh/src/exec/process_resetfds.c +++ /dev/null @@ -1,23 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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/process_setexec.c b/42sh/src/exec/process_setexec.c index d184531f..f54afb2c 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */ -/* Updated: 2017/02/20 20:42:02 by ariard ### ########.fr */ +/* Updated: 2017/02/21 22:41:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index 171df690..2e946d6a 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/job_addprocess.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:54:51 by jhalford #+# #+# */ -/* Updated: 2017/01/31 15:07:16 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 21:42:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int job_addprocess(t_process *p) jobc = &data_singleton()->jobc; job = &data_singleton()->exec.job; - if (IS_PIPESTART(p->attributes)) + if (IS_PIPESTART(p)) { job_update_id(); job->id = jobc->current_id; @@ -31,7 +31,7 @@ int job_addprocess(t_process *p) { ft_lsteadd(&job->first_process, ft_lstnew(p, sizeof(*p))); } - if (JOB_IS_BG(job->attributes) && IS_PIPEEND(p->attributes)) + if (JOB_IS_BG(job->attributes) && IS_PIPEEND(p)) job_notify_new(job); return (0); } diff --git a/42sh/src/lexer/lexer_delim.c b/42sh/src/lexer/lexer_delim.c index 4c7f4eb7..3809a674 100644 --- a/42sh/src/lexer/lexer_delim.c +++ b/42sh/src/lexer/lexer_delim.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:58:44 by jhalford #+# #+# */ -/* Updated: 2017/02/20 20:52:10 by ariard ### ########.fr */ +/* Updated: 2017/02/21 22:14:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_lex.c b/42sh/src/lexer/lexer_lex.c index 8e9a7720..a8e8818f 100644 --- a/42sh/src/lexer/lexer_lex.c +++ b/42sh/src/lexer/lexer_lex.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:08:51 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:55:26 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 22:14:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index 383a4001..8ccc2fdb 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/02/21 20:04:31 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 20:30:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 12c47e05..95e59dec 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/20 20:57:14 by ariard ### ########.fr */ +/* Updated: 2017/02/21 22:41:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,12 +26,13 @@ int data_init(void) data->exec.process.path = NULL; data->exec.process.av = NULL; - data->exec.process.toclose = STDIN; + data->exec.process.to_close = 0; data->exec.process.fdin = STDIN; data->exec.process.fdout = STDOUT; data->exec.process.pid = 0; - data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; + data->exec.process.attributes = 0; data->exec.process.redirs = NULL; + data->exec.process.pipe_count = 0; data->exec.fd0save = fcntl(0, F_DUPFD_CLOEXEC); data->exec.fd1save = fcntl(1, F_DUPFD_CLOEXEC); data->exec.fd2save = fcntl(2, F_DUPFD_CLOEXEC); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 6619b322..d0b607d1 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/21 20:07:27 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 20:32:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,18 +26,14 @@ int handle_instruction(int fd) parser_init(&parser); token = NULL; ast = NULL; - /* str = NULL; */ - DG("START: state=%i", parser.state); while (1) { if ((ret = readline(fd, get_lexer_stack(lexer), &str))) { - DG("ret=%i, str=%s, state=%i", ret, str, parser.state); if (ret == -1) return (-1); return (parser.state == UNDEFINED ? error_EOF() : 1); } - DG("ret=%i, str=%s", ret, str); ft_strappend(&lexer.str, str); if (get_lexer_stack(lexer) == BACKSLASH) pop(&lexer.stack); @@ -46,22 +42,20 @@ int handle_instruction(int fd) ltoken = ft_lstlast(token); if (lexer_lex(token ? <oken : &token, &lexer)) return (1); - //token_print(token); if (get_lexer_stack(lexer)) continue ; if (ft_parse(&ast, &token, &parser)) continue ; - DG("AFTER PARSING: state=%i", parser.state); if (parser.state == SUCCESS) break ; else if (parser.state == ERROR) return (error_syntax(&token)); } - DG("succesful parsing:"); + DG("Before execution:"); btree_print(STDBUG, ast, &ft_putast); - /* if (ft_exec(&ast)) */ - /* return (1); */ ft_add_str_in_history(lexer.str); + if (ft_exec(&ast)) + return (1); return (0); } diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 23bf28bf..bf8038ce 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 20:49:15 by ariard #+# #+# */ -/* Updated: 2017/02/20 22:27:06 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 22:40:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */