From 8084ffb6a1360d9c091c3add5e5bd7853b690338 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 21 Feb 2017 22:42:13 +0100 Subject: [PATCH 01/13] pipes done --- 42sh/Makefile | 1 - 42sh/includes/exec.h | 25 +++++++++++---------- 42sh/includes/lexer.h | 2 +- 42sh/src/builtin/is_builtin.c | 4 +++- 42sh/src/exec/exec_command.c | 20 ++++++++++++++--- 42sh/src/exec/exec_pipe.c | 31 +++++---------------------- 42sh/src/exec/ft_exec.c | 6 +++--- 42sh/src/exec/launch_process.c | 4 ++-- 42sh/src/exec/process_redirect.c | 9 ++++---- 42sh/src/exec/process_reset.c | 5 +++-- 42sh/src/exec/process_resetfds.c | 23 -------------------- 42sh/src/exec/process_setexec.c | 2 +- 42sh/src/job-control/job_addprocess.c | 6 +++--- 42sh/src/lexer/lexer_delim.c | 2 +- 42sh/src/lexer/lexer_lex.c | 2 +- 42sh/src/line-editing/readline.c | 2 +- 42sh/src/main/data_init.c | 7 +++--- 42sh/src/main/main.c | 14 ++++-------- 42sh/src/parser/add_cmd.c | 2 +- 19 files changed, 66 insertions(+), 101 deletions(-) delete mode 100644 42sh/src/exec/process_resetfds.c 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 */ /* */ /* ************************************************************************** */ From 3857b0678525907e8cc363b026848a327bf0a713 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 2 Mar 2017 21:17:16 +0100 Subject: [PATCH 02/13] execution much simpler with new parser, thx ariard! --- 42sh/Makefile | 28 +++--- 42sh/includes/exec.h | 30 ++++--- 42sh/includes/job_control.h | 6 +- 42sh/includes/lexer.h | 2 +- 42sh/includes/minishell.h | 2 +- 42sh/includes/parser.h | 5 +- 42sh/includes/types.h | 2 +- 42sh/libft | 2 +- 42sh/src/builtin/is_builtin.c | 2 +- 42sh/src/exec/exec_ampersand.c | 18 ++-- 42sh/src/exec/exec_and_if.c | 43 ++++++---- 42sh/src/exec/exec_command.c | 86 ++++++++----------- 42sh/src/exec/exec_or_if.c | 43 ++++++---- 42sh/src/exec/exec_redir.c | 27 ------ 42sh/src/exec/exec_semi.c | 10 ++- 42sh/src/exec/ft_exec.c | 21 ++--- 42sh/src/exec/launch_process.c | 2 +- 42sh/src/exec/process_redirect.c | 2 +- 42sh/src/exec/process_reset.c | 12 ++- 42sh/src/exec/process_setexec.c | 21 ++--- 42sh/src/exec/process_setgroup.c | 2 +- .../{job_addprocess.c => add_new_job.c} | 36 ++++---- 42sh/src/job-control/job_update_id.c | 2 +- 42sh/src/job-control/process_free.c | 2 +- 42sh/src/job-control/put_job_in_background.c | 2 +- 42sh/src/job-control/put_job_in_foreground.c | 2 +- 42sh/src/lexer/get_state_global.c | 2 +- 42sh/src/lexer/lexer_assignement_word.c | 2 +- 42sh/src/lexer/lexer_word.c | 2 +- 42sh/src/main/data_init.c | 39 +++++---- 42sh/src/main/ft_putast.c | 2 +- 42sh/src/main/main.c | 2 +- 42sh/src/main/shell_init.c | 2 +- 42sh/src/parser/add_cmd.c | 15 +++- 34 files changed, 234 insertions(+), 242 deletions(-) delete mode 100644 42sh/src/exec/exec_redir.c rename 42sh/src/job-control/{job_addprocess.c => add_new_job.c} (56%) diff --git a/42sh/Makefile b/42sh/Makefile index 9929b18b..6492f853 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/03/01 16:42:16 by ariard ### ########.fr # +# Updated: 2017/03/02 16:23:48 by jhalford ### ########.fr # # # # **************************************************************************** # @@ -14,7 +14,7 @@ NAME = 42sh CC = gcc FLAGS = -Wall -Wextra -Werror -D_FLAGS = -g +D_FLAGS = -g DELTA = $$(echo "$$(tput cols)-47"|bc) @@ -62,22 +62,22 @@ exec/ast_free.c\ exec/bad_fd.c\ exec/exec_ampersand.c\ exec/exec_and_if.c\ +exec/exec_case.c\ +exec/exec_case_branch.c\ exec/exec_command.c\ exec/exec_default.c\ exec/exec_elif.c\ exec/exec_else.c\ +exec/exec_for.c\ +exec/exec_func.c\ exec/exec_if.c\ exec/exec_less.c\ exec/exec_or_if.c\ exec/exec_pipe.c\ -exec/exec_redir.c\ exec/exec_semi.c\ exec/exec_until.c\ -exec/exec_while.c\ exec/exec_var.c\ -exec/exec_for.c\ -exec/exec_case.c\ -exec/exec_case_branch.c\ +exec/exec_while.c\ exec/fd_is_valid.c\ exec/ft_exec.c\ exec/ft_findexec.c\ @@ -173,9 +173,11 @@ lexer/get_reserved_words.c\ lexer/get_state_global.c\ lexer/get_state_redir.c\ lexer/insert_newline.c\ +lexer/lexer_assignement_word.c\ lexer/lexer_backslash.c\ lexer/lexer_bquote.c\ lexer/lexer_comment.c\ +lexer/lexer_curly_brackets.c\ lexer/lexer_default.c\ lexer/lexer_delim.c\ lexer/lexer_dless.c\ @@ -190,8 +192,6 @@ lexer/lexer_lex.c\ lexer/lexer_newline.c\ lexer/lexer_number.c\ lexer/lexer_paren.c\ -lexer/lexer_curly_brackets.c\ -lexer/lexer_assignement_word.c\ lexer/lexer_quote.c\ lexer/lexer_sep.c\ lexer/lexer_word.c\ @@ -227,14 +227,14 @@ main/shell_exit.c\ main/shell_get_avdata.c\ main/shell_get_opts.c\ main/shell_init.c\ -parser/add_cmd.c\ -parser/add_subshell.c\ -parser/add_condition.c\ -parser/add_loop.c\ -parser/add_sep.c\ parser/add_case.c\ +parser/add_cmd.c\ +parser/add_condition.c\ parser/add_func.c\ +parser/add_loop.c\ parser/add_redir.c\ +parser/add_sep.c\ +parser/add_subshell.c\ parser/aggregate_sym.c\ parser/build_tree.c\ parser/error_syntax.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 22280dc8..3090d73e 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/21 21:41:18 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 21:02:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,15 @@ # define IS_PIPEEND(p) (p->fdout == STDOUT) # define IS_PIPESINGLE(p) (IS_PIPESTART(p) && IS_PIPEEND(p)) +# define EXEC_BG (1 << 1) +# define EXEC_AND_IF (1 << 2) +# define EXEC_OR_IF (1 << 3) +# define EXEC_IS_BG(j) (j & EXEC_BG) +# define EXEC_IS_FG(j) (!EXEC_IS_BG(j)) +# define EXEC_IS_AND_IF(j) (j & EXEC_AND_IF) +# define EXEC_IS_OR_IF(j) (j & EXEC_JOB_OR_IF) +# define EXEC_AOL_MASK (EXEC_AND_IF | EXEC_OR_IF) + # include "libft.h" # include "types.h" # include "job_control.h" @@ -45,7 +54,6 @@ struct s_process pid_t pid; int fdin; int fdout; - int pipe_count; int to_close; t_list *redirs; int status; @@ -57,13 +65,13 @@ struct s_process struct s_exec { - char *aol_status; - int aol_search; - t_job job; - t_process process; - int fd0save; - int fd1save; - int fd2save; + /* char *aol_status; */ + /* int aol_search; */ + /* t_job job; */ + /* t_process process; */ + int fd_save[3]; + t_flag attrs; + t_list *op_stack; }; struct s_execmap @@ -92,8 +100,8 @@ int exec_ampersand(t_btree **ast); int exec_or_if(t_btree **ast); int exec_and_if(t_btree **ast); int exec_pipe(t_btree **ast); -int exec_redir(t_btree **ast); -int exec_command(t_btree **ast); +/* int exec_redir(t_btree **ast); */ +int exec_job(t_btree **ast); int exec_while(t_btree **ast); int exec_if(t_btree **ast); diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 418de6eb..744dcad4 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */ -/* Updated: 2017/02/03 14:43:34 by ariard ### ########.fr */ +/* Updated: 2017/03/02 21:01:59 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,10 +21,8 @@ # define JOB_NOTIFIED (1 << 0) # define JOB_BG (1 << 1) -# define JOB_IS_BG(j) (j & JOB_BG) -# define JOB_IS_FG(j) !(j & JOB_BG) -#define JOBS_OPTS_L (1 << 0) +# define JOBS_OPTS_L (1 << 0) struct s_job { diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 3551dd8f..927d5dec 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/24 21:46:08 by ariard ### ########.fr */ +/* Updated: 2017/03/02 17:15:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index fd9768fd..3b482e3c 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: 2017/02/21 18:51:18 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 17:15:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 7001687d..82401142 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -1,12 +1,11 @@ /* ************************************************************************** */ -/* */ -/* ::: :::::::: */ +/* */ /* ::: :::::::: */ /* parser.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/03/01 22:39:00 by ariard ### ########.fr */ +/* Updated: 2017/03/02 17:10:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 3445dc7b..365aea4c 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/03/01 22:39:16 by ariard ### ########.fr */ +/* Updated: 2017/03/02 17:49:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft b/42sh/libft index bfc8ca20..8f6e64fa 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit bfc8ca207ab4d39f0140322c0f1d368137304a3c +Subproject commit 8f6e64fa9b4ac1dd3e3d5200fb93471ddfeedd40 diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index 2725127f..c0f39c2f 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/21 22:40:59 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 21:00:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c index f69ed62c..dcd4e16f 100644 --- a/42sh/src/exec/exec_ampersand.c +++ b/42sh/src/exec/exec_ampersand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */ -/* Updated: 2017/02/05 22:10:08 by ariard ### ########.fr */ +/* Updated: 2017/03/02 21:02:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,21 @@ int exec_ampersand(t_btree **ast) { - if (SH_HAS_JOBC(data_singleton()->opts)) - data_singleton()->exec.job.attributes |= JOB_BG; + t_exec *exec; + + exec = &data_singleton()->exec; + push(&exec->op_stack, TK_AMP); ft_exec(&(*ast)->left); - if (SH_HAS_JOBC(data_singleton()->opts)) - data_singleton()->exec.job.attributes &= ~JOB_BG; + exec->attrs &= ~JOB_BG; ft_exec(&(*ast)->right); + /* if (SH_HAS_JOBC(data_singleton()->opts)) */ + /* data_singleton()->exec.job.attributes |= JOB_BG; */ + /* ft_exec(&(*ast)->left); */ + /* if (SH_HAS_JOBC(data_singleton()->opts)) */ + /* data_singleton()->exec.job.attributes &= ~JOB_BG; */ + /* ft_exec(&(*ast)->right); */ + // btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_and_if.c b/42sh/src/exec/exec_and_if.c index 794cfdb9..b475724e 100644 --- a/42sh/src/exec/exec_and_if.c +++ b/42sh/src/exec/exec_and_if.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:28 by jhalford #+# #+# */ -/* Updated: 2017/02/05 22:10:38 by ariard ### ########.fr */ +/* Updated: 2017/03/02 20:41:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,24 +14,31 @@ int exec_and_if(t_btree **ast) { - t_data *data; + /* t_data *data; */ + t_exec *exec; - data = data_singleton(); - if (data->exec.aol_status == NULL - || (data->exec.aol_search == TK_AND_IF - && *data->exec.aol_status == '0') - || (data->exec.aol_search == TK_OR_IF - && *data->exec.aol_status != '0')) - { - ft_exec(&(*ast)->left); - data->exec.aol_status = ft_getenv(data->env, "?"); - } - data->exec.aol_search = TK_AND_IF; - if (*data->exec.aol_status == '0' - || ((t_astnode*)(*ast)->right->item)->type != TK_COMMAND) - ft_exec(&(*ast)->right); - data->exec.aol_status = NULL; - data->exec.aol_search = 0; + exec = &data_singleton()->exec; + push(&exec->op_stack, TK_AND_IF); + ft_exec(&(*ast)->left); + exec->attrs |= JOB_AND_IF; + ft_exec(&(*ast)->right); + + /* data = data_singleton(); */ + /* if (data->exec.aol_status == NULL */ + /* || (data->exec.aol_search == TK_AND_IF */ + /* && *data->exec.aol_status == '0') */ + /* || (data->exec.aol_search == TK_OR_IF */ + /* && *data->exec.aol_status != '0')) */ + /* { */ + /* ft_exec(&(*ast)->left); */ + /* data->exec.aol_status = ft_getenv(data->env, "?"); */ + /* } */ + /* data->exec.aol_search = TK_AND_IF; */ + /* if (*data->exec.aol_status == '0' */ + /* || ((t_astnode*)(*ast)->right->item)->type != TK_COMMAND) */ + /* ft_exec(&(*ast)->right); */ + /* data->exec.aol_status = NULL; */ + /* data->exec.aol_search = 0; */ // btree_delone(ast, &ast_free); return (0); diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 3d0807b5..e22d5585 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,13 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/03/02 12:36:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 21:16:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -char **token_to_argv(t_astnode *node) +char **token_to_argv(t_cmd *cmd) { char **my_tab; int index; @@ -20,66 +20,50 @@ char **token_to_argv(t_astnode *node) char **content; t_ld *ld; - if (node->type == TK_WORD || node->type == TK_ASSIGNEMENT_WORD) + ld = cmd->token; + my_tab = NULL; + while (ld) { - ld = node->data.cmd.token; - my_tab = NULL; - while (ld) + content = ld->content; + if ((expand = glob(content[0], (unsigned char *)content[1], (unsigned char *)content[2]))) { - content = ld->content; - if ((expand = glob(content[0], (unsigned char *)content[1], (unsigned char *)content[2]))) - { - index = -1; - while (expand[++index]) - my_tab = ft_sstradd(my_tab, expand[index]); - ft_tabdel(&expand); - } - ld = ld->next; + index = -1; + while (expand[++index]) + my_tab = ft_sstradd(my_tab, expand[index]); + ft_tabdel(&expand); } - return (my_tab); + ld = ld->next; } - else if (node->type == TK_SUBSHELL) - return (ft_sstrdup(node->data.sstr)); - return (NULL); + return (my_tab); } -int exec_command(t_btree **ast) +int exec_job(t_btree **ast) { - t_astnode *node; - t_process *p; - t_job *job; + t_list *cmd; + t_process p; + t_list *first_process; int fds[2]; - node = (*ast)->item; - p = &data_singleton()->exec.process; - job = &data_singleton()->exec.job; - if (!(p->av = token_to_argv(node))) + cmd = ((t_astnode *)(*ast)->item)->data; + exec = &data_singleton()->exec; + if (pop(&exec.op_stack) == TK_AMP) + exec->attrs |= JOB_BG; + first_process = NULL; + fds[PIPE_READ] = STDIN; + while (cmd) { - DG("globbing error"); - return (1); + p.fdin = fds[PIPE_READ]; + p.fdout = cmd->next ? pipe(fds) && fds[PIPE_WRITE] : STDOUT; + process_reset(&p); + if (!(p.av = token_to_argv(cmd->content))) + return (1); + process_setexec(cmd->content, &p); + if (!(launch_process(p))) + ft_lstadd(&first_process, ft_lstnew(&p, sizeof(p))); + cmd = cmd->next; } - 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)) - { - JOB_IS_FG(job->attributes) ? - put_job_in_foreground(job, 0): - put_job_in_background(job, 0); - job->pgid = 0; - } - } - if (p->fdout == fds[PIPE_WRITE]) - p->fdin = fds[PIPE_READ]; - process_reset(p); + add_new_job(first_process, EXEC_IS_FG(exec->attrs)); + ft_lstadd(&jobc->first_job, ft_lstnew(&job, sizeof(*job))); // btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_or_if.c b/42sh/src/exec/exec_or_if.c index 63995843..a0c8e3f6 100644 --- a/42sh/src/exec/exec_or_if.c +++ b/42sh/src/exec/exec_or_if.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 21:06:17 by jhalford #+# #+# */ -/* Updated: 2017/02/05 22:12:08 by ariard ### ########.fr */ +/* Updated: 2017/03/02 21:02:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,24 +14,31 @@ int exec_or_if(t_btree **ast) { - t_data *data; + /* t_data *data; */ + t_exec *exec; - data = data_singleton(); - if (data->exec.aol_status == NULL - || (data->exec.aol_search == TK_AND_IF - && *data->exec.aol_status == '0') - || (data->exec.aol_search == TK_OR_IF - && *data->exec.aol_status != '0')) - { - ft_exec(&(*ast)->left); - data->exec.aol_status = ft_getenv(data->env, "?"); - } - data->exec.aol_search = TK_OR_IF; - if (*data->exec.aol_status != '0' - || ((t_astnode*)(*ast)->right->item)->type != TK_COMMAND) - ft_exec(&(*ast)->right); - data->exec.aol_status = NULL; - data->exec.aol_search = 0; + exec = &data_singleton()->exec; + push(&exec->op_stack, TK_OR_IF); + ft_exec(&(*ast)->left); + exec->attrs |= JOB_OR_IF; + ft_exec(&(*ast)->right); + + /* data = data_singleton(); */ + /* if (data->exec.aol_status == NULL */ + /* || (data->exec.aol_search == TK_AND_IF */ + /* && *data->exec.aol_status == '0') */ + /* || (data->exec.aol_search == TK_OR_IF */ + /* && *data->exec.aol_status != '0')) */ + /* { */ + /* ft_exec(&(*ast)->left); */ + /* data->exec.aol_status = ft_getenv(data->env, "?"); */ + /* } */ + /* data->exec.aol_search = TK_OR_IF; */ + /* if (*data->exec.aol_status != '0' */ + /* || ((t_astnode*)(*ast)->right->item)->type != TK_COMMAND) */ + /* ft_exec(&(*ast)->right); */ + /* data->exec.aol_status = NULL; */ + /* data->exec.aol_search = 0; */ // btree_delone(ast, &ast_free); return (0); diff --git a/42sh/src/exec/exec_redir.c b/42sh/src/exec/exec_redir.c deleted file mode 100644 index 52b8c96d..00000000 --- a/42sh/src/exec/exec_redir.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* exec_redir.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */ -/* Updated: 2017/03/01 16:38:01 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "exec.h" - -int exec_redir(t_btree **ast) -{ - t_astnode *node; - t_process *p; - - p = &data_singleton()->exec.process; - node = (*ast)->item; -// node->data.redir.type = node->type; -// ft_lsteadd(&p->redirs, ft_lstnew(&node->data.redir,sizeof(node->data.redir))); - ft_exec(&(*ast)->left); -// btree_delone(ast, &ast_free); - return (0); -} diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index 33d99589..0d49fe4f 100644 --- a/42sh/src/exec/exec_semi.c +++ b/42sh/src/exec/exec_semi.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ -/* Updated: 2017/02/06 18:34:38 by ariard ### ########.fr */ +/* Updated: 2017/03/02 20:41:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,13 @@ int exec_semi(t_btree **ast) { - ft_exec(&(*ast)->left); - ft_exec(&(*ast)->right); + t_exec *exec; + exec = &data_singleton()->exec; + push(&exec->op_stack, TK_SEMI); + ft_exec(&(*ast)->left); + exec->attrs ~= ~JOB_AOL_MASK; + ft_exec(&(*ast)->right); // btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 1994a502..42875868 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:50:05 by ariard ### ########.fr */ +/* Updated: 2017/03/02 20:53:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,24 +15,18 @@ t_execmap g_execmap[] = { {TK_NEWLINE, &exec_semi}, - {TK_AND_IF, &exec_and_if}, - {TK_OR_IF, &exec_or_if}, {TK_SEMI, &exec_semi}, {TK_AMP, &exec_ampersand}, - {TK_PIPE, &exec_pipe}, - {TK_LESS, &exec_redir}, - {TK_GREAT, &exec_redir}, - {TK_DLESS, &exec_redir}, - {TK_DGREAT, &exec_redir}, - {TK_LESSAND, &exec_redir}, - {TK_GREATAND, &exec_redir}, + {TK_AND_IF, &exec_and_if}, + {TK_OR_IF, &exec_or_if}, + /* {TK_PIPE, &exec_pipe}, */ {TK_WHILE, &exec_while}, {TK_IF, &exec_if}, {TK_ELIF, &exec_elif}, {TK_ELSE, &exec_else}, {TK_UNTIL, &exec_until}, - {TK_SUBSHELL, &exec_command}, - {TK_WORD, &exec_command}, + /* {TK_SUBSHELL, &exec_}, */ + {TK_WORD, &exec_job}, {0, 0}, }; @@ -51,8 +45,7 @@ int ft_exec(t_btree **ast) { /* 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); + return ((*g_execmap[i].f)(ast)); } i++; } diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 1676f69f..65184fc3 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 21:39:15 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 20:29:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index a4253467..d62970e6 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/21 21:44:23 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 19:44:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_reset.c b/42sh/src/exec/process_reset.c index 853325b4..638f1e75 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/21 21:42:40 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 20:44:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,10 +14,14 @@ void process_reset(t_process *p) { - p->fdout = STDOUT; - p->to_close = 0; p->av = NULL; + p->path = NULL; + p->execf = NULL; p->pid = 0; + /* p->fdin = STDIN; */ + /* p->fdout = STDOUT; */ + p->to_close = 0; p->redirs = NULL; - p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK); + p->status = -1; + p->attributes = 0; } diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index f54afb2c..fd2f9848 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -6,25 +6,26 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */ -/* Updated: 2017/02/21 22:41:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 21:00:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int process_setexec(t_type type, t_process *p) +int process_setexec(t_cmd *cmd, t_process *p) { + t_flag type; + + type = cmd->type; p->path = NULL; - if (type == TK_SUBSHELL) - { - p->execf = &execve; - p->attributes |= PROCESS_SUBSHELL; - p->path = ft_strdup(p->av[0]); - } + /* if (type == TK_SUBSHELL) */ + /* { */ + /* p->execf = &execve; */ + /* p->attributes |= PROCESS_SUBSHELL; */ + /* p->path = ft_strdup(p->av[0]); */ + /* } */ else if ((p->execf = is_builtin(p))) - { p->attributes |= PROCESS_BUILTIN; - } else if (ft_strchr(p->av[0], '/')) { p->execf = &execve; diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index a7ada487..16223c08 100644 --- a/42sh/src/exec/process_setgroup.c +++ b/42sh/src/exec/process_setgroup.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:48:10 by jhalford #+# #+# */ -/* Updated: 2017/01/11 14:45:36 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 20:13:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/add_new_job.c similarity index 56% rename from 42sh/src/job-control/job_addprocess.c rename to 42sh/src/job-control/add_new_job.c index 2e946d6a..88e31d8b 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/add_new_job.c @@ -1,37 +1,35 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* job_addprocess.c :+: :+: :+: */ +/* add_new_job.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/13 13:54:51 by jhalford #+# #+# */ -/* Updated: 2017/02/21 21:42:53 by jhalford ### ########.fr */ +/* Created: 2017/03/02 20:44:21 by jhalford #+# #+# */ +/* Updated: 2017/03/02 21:04:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -int job_addprocess(t_process *p) +int add_new_job(t_list *first_process, int foreground) { t_jobc *jobc; - t_job *job; + t_job job; + if (!first_process) + return (1); jobc = &data_singleton()->jobc; - job = &data_singleton()->exec.job; - if (IS_PIPESTART(p)) - { - job_update_id(); - job->id = jobc->current_id; - job->pgid = p->pid; - ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); - } - job = jobc->first_job->content; - if (p->pid > 0) - { - ft_lsteadd(&job->first_process, ft_lstnew(p, sizeof(*p))); - } - if (JOB_IS_BG(job->attributes) && IS_PIPEEND(p)) + job_update_id(); + job->id = jobc->current_id; + job->pgid = ((t_process*)first_process->content)->pid; + job->attrs = foreground ? 0 : JOB_BG; + job->first_process = first_process; + ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); + if (JOB_IS_FG(job->attrs)) + put_job_in_foreground(job, 0); + else job_notify_new(job); + put_job_in_background(job, 0); return (0); } diff --git a/42sh/src/job-control/job_update_id.c b/42sh/src/job-control/job_update_id.c index 08ac73d2..1fba6f36 100644 --- a/42sh/src/job-control/job_update_id.c +++ b/42sh/src/job-control/job_update_id.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 13:33:08 by jhalford #+# #+# */ -/* Updated: 2017/01/10 13:22:11 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 20:59:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/process_free.c b/42sh/src/job-control/process_free.c index ce23bf71..eb84a650 100644 --- a/42sh/src/job-control/process_free.c +++ b/42sh/src/job-control/process_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/02/03 13:59:25 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 17:48:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/put_job_in_background.c b/42sh/src/job-control/put_job_in_background.c index 09f1b482..895b58a9 100644 --- a/42sh/src/job-control/put_job_in_background.c +++ b/42sh/src/job-control/put_job_in_background.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 15:03:29 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:52:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 20:57:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 187ca02f..3f783821 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:58:36 by jhalford #+# #+# */ -/* Updated: 2017/01/31 15:10:45 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 20:59:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/get_state_global.c b/42sh/src/lexer/get_state_global.c index 78530edc..1e56f815 100644 --- a/42sh/src/lexer/get_state_global.c +++ b/42sh/src/lexer/get_state_global.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:39:06 by jhalford #+# #+# */ -/* Updated: 2017/02/24 21:39:47 by ariard ### ########.fr */ +/* Updated: 2017/03/02 18:20:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_assignement_word.c b/42sh/src/lexer/lexer_assignement_word.c index 70429bed..a6bae9dd 100644 --- a/42sh/src/lexer/lexer_assignement_word.c +++ b/42sh/src/lexer/lexer_assignement_word.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/24 20:28:13 by ariard #+# #+# */ -/* Updated: 2017/02/24 21:00:13 by ariard ### ########.fr */ +/* Updated: 2017/03/02 19:11:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index 25f2e6ef..f3253e06 100644 --- a/42sh/src/lexer/lexer_word.c +++ b/42sh/src/lexer/lexer_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */ -/* Updated: 2017/03/01 14:06:03 by ariard ### ########.fr */ +/* Updated: 2017/03/02 18:12:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 56e9179d..554fe821 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/21 22:41:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 21:02:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,25 +23,26 @@ int data_init(void) data->env = ft_sstrdup(environ); data->comp = NULL; data->opts = SH_OPTS_JOBC; - data->exec.process.path = NULL; - data->exec.process.av = NULL; - 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 = 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); + /* data->exec.process.path = NULL; */ + /* data->exec.process.av = NULL; */ + /* 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 = 0; */ + /* data->exec.process.redirs = NULL; */ + data->exec.fd_save[0] = fcntl(0, F_DUPFD_CLOEXEC); + data->exec.fd_save[1] = fcntl(1, F_DUPFD_CLOEXEC); + data->exec.fd_save[2] = fcntl(2, F_DUPFD_CLOEXEC); + data->exec.op_stack = NULL; + data->attrs = 0; - data->exec.aol_status = NULL; - data->exec.aol_search = 0; - data->exec.job.id = 0; - data->exec.job.pgid = 0; - data->exec.job.attributes = 0; - data->exec.job.first_process = 0; + /* data->exec.aol_status = NULL; */ + /* data->exec.aol_search = 0; */ + /* data->exec.job.id = 0; */ + /* data->exec.job.pgid = 0; */ + /* data->exec.job.attributes = 0; */ + /* data->exec.job.first_process = 0; */ data->jobc.first_job = NULL; data->jobc.current_id = 1; diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index 6b35fe87..58fb1d73 100644 --- a/42sh/src/main/ft_putast.c +++ b/42sh/src/main/ft_putast.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ -/* Updated: 2017/03/01 15:56:54 by ariard ### ########.fr */ +/* Updated: 2017/03/02 17:16:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index f0a1e2d9..109005cf 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/03/02 12:37:07 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 21:00:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 4e64df18..7ad9da23 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ -/* Updated: 2017/02/21 20:14:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/02 20:38:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 3faa94a2..e2af80b4 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/03/01 22:46:31 by ariard ### ########.fr */ +/* Updated: 2017/03/02 19:11:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,14 +48,21 @@ int add_cmd(t_btree **ast, t_list **lst) && node->type != TK_PAREN_CLOSE && node->type != TK_WORD && node->type != REDIR) return (add_cmd(&(*ast)->right, lst)); - my_tab = NULL; node = (*ast)->item; node->type = token->type; if (token->type == TK_WORD || token->type == TK_ASSIGNEMENT_WORD) { DG("add data"); - my_tab = ft_sstradd(my_tab, token->data); - my_tab = ft_sstradd(my_tab, (char *)token->esc); + /* my_tab = ft_sstradd(NULL, token->data); */ + /* my_tab = ft_sstradd(my_tab, (char *)token->esc); */ + /* my_tab = ft_sstradd(my_tab, (char *)token->esc2); */ + if ((my_tab = (char **)malloc(sizeof(char *) * 4))) + { + my_tab[0] = ft_strdup(token->data); + my_tab[1] = (char *)dup_char_esc(token->esc, token->size >> 3); + my_tab[2] = (char *)dup_char_esc(token->esc2, token->size >> 3); + my_tab[3] = NULL; + } ft_ld_pushback(&node->data.cmd.token, my_tab); } return (0); From 418859e9e89db3b1632a6731ff7fe671ee1ea4bb Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Mar 2017 16:51:18 +0100 Subject: [PATCH 03/13] it compiles ! much more testing for simple commands to work --- 42sh/Makefile | 3 +- 42sh/includes/exec.h | 42 ++++++++------- 42sh/includes/job_control.h | 6 ++- 42sh/src/builtin/builtin_env.c | 2 +- 42sh/src/exec/exec_ampersand.c | 4 +- 42sh/src/exec/exec_and_if.c | 4 +- 42sh/src/exec/exec_case.c | 15 ++++-- 42sh/src/exec/exec_case_branch.c | 21 ++++---- 42sh/src/exec/exec_command.c | 53 ++++++++++--------- 42sh/src/exec/exec_elif.c | 14 +++-- 42sh/src/exec/exec_else.c | 11 ++-- 42sh/src/exec/exec_for.c | 2 +- 42sh/src/exec/exec_if.c | 15 ++++-- 42sh/src/exec/exec_less.c | 32 ------------ 42sh/src/exec/exec_or_if.c | 4 +- 42sh/src/exec/exec_pipe.c | 11 ++-- 42sh/src/exec/exec_semi.c | 4 +- 42sh/src/exec/exec_until.c | 4 +- 42sh/src/exec/exec_var.c | 4 +- 42sh/src/exec/exec_while.c | 4 +- 42sh/src/exec/ft_exec.c | 4 +- 42sh/src/exec/launch_process.c | 15 +++--- 42sh/src/exec/process_reset.c | 2 +- 42sh/src/exec/process_setexec.c | 15 ++---- 42sh/src/exec/process_setgroup.c | 4 +- 42sh/src/glob/command_getoutput.c | 59 +++++++++++----------- 42sh/src/job-control/add_new_job.c | 11 ++-- 42sh/src/job-control/do_job_notification.c | 6 +-- 42sh/src/job-control/mark_job_as_running.c | 4 +- 42sh/src/lexer/lexer_bquote.c | 4 +- 42sh/src/main/data_init.c | 5 +- 42sh/src/main/main.c | 2 +- 32 files changed, 190 insertions(+), 196 deletions(-) delete mode 100644 42sh/src/exec/exec_less.c diff --git a/42sh/Makefile b/42sh/Makefile index 6492f853..73049a56 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -71,7 +71,6 @@ exec/exec_else.c\ exec/exec_for.c\ exec/exec_func.c\ exec/exec_if.c\ -exec/exec_less.c\ exec/exec_or_if.c\ exec/exec_pipe.c\ exec/exec_semi.c\ @@ -135,11 +134,11 @@ history/history_parsing_toolz.c\ history/history_parsing_toolz_2.c\ history/list_toolz.c\ history/surch_in_history.c\ +job-control/add_new_job.c\ job-control/builtin_bg.c\ job-control/builtin_fg.c\ job-control/builtin_jobs.c\ job-control/do_job_notification.c\ -job-control/job_addprocess.c\ job-control/job_cmp_id.c\ job-control/job_format.c\ job-control/job_format_head.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 3090d73e..bace5d56 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/03/02 21:02:14 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:39:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,18 +29,23 @@ # define PROCESS_TYPE_MASK (1 << 0 | 1 << 1 | 1 << 2 | 1 << 3 | 1 << 4) # define PROCESS_STATE_MASK (1 << 5 | 1 << 6 | 1 << 7 | 1 << 8) -# define IS_PIPESTART(p) (p->fdin == STDIN) -# define IS_PIPEEND(p) (p->fdout == STDOUT) +# 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 EXEC_BG (1 << 1) -# define EXEC_AND_IF (1 << 2) -# define EXEC_OR_IF (1 << 3) -# define EXEC_IS_BG(j) (j & EXEC_BG) -# define EXEC_IS_FG(j) (!EXEC_IS_BG(j)) -# define EXEC_IS_AND_IF(j) (j & EXEC_AND_IF) -# define EXEC_IS_OR_IF(j) (j & EXEC_JOB_OR_IF) -# define EXEC_AOL_MASK (EXEC_AND_IF | EXEC_OR_IF) +# define EXEC_BG (1 << 1) +# define EXEC_AND_IF (1 << 2) +# define EXEC_OR_IF (1 << 3) +# define EXEC_IF_BRANCH (1 << 4) +# define EXEC_CASE_BRANCH (1 << 5) +# define EXEC_IS_BG(j) (j & EXEC_BG) +# define EXEC_IS_FG(j) (!EXEC_IS_BG(j)) +# define EXEC_IS_AND_IF(j) (j & EXEC_AND_IF) +# define EXEC_IS_OR_IF(j) (j & EXEC_JOB_OR_IF) +# define EXEC_AOL_MASK (EXEC_AND_IF | EXEC_OR_IF) + +# define EXEC_IS_IF_BRANCH(j) (j & EXEC_IF_BRANCH) +# define EXEC_IS_CASE_BRANCH(j) (j & EXEC_CASE_BRANCH) # include "libft.h" # include "types.h" @@ -58,20 +63,19 @@ struct s_process t_list *redirs; int status; t_flag attributes; - t_condition if_branch; - t_condition case_branch; - char *case_pattern; }; struct s_exec { /* char *aol_status; */ /* int aol_search; */ - /* t_job job; */ + t_job job; /* t_process process; */ int fd_save[3]; t_flag attrs; + int fdin; t_list *op_stack; + char *case_pattern; }; struct s_execmap @@ -101,7 +105,7 @@ int exec_or_if(t_btree **ast); int exec_and_if(t_btree **ast); int exec_pipe(t_btree **ast); /* int exec_redir(t_btree **ast); */ -int exec_job(t_btree **ast); +int exec_cmd(t_btree **ast); int exec_while(t_btree **ast); int exec_if(t_btree **ast); @@ -115,7 +119,7 @@ int exec_case(t_btree **ast); int exec_case_branch(t_btree **ast); int launch_process(t_process *p); -int process_setexec(t_type type, t_process *p); +int process_setexec(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); @@ -138,6 +142,8 @@ void set_exitstatus(int status, int override); void ast_free(void *data, size_t content_size); -char **token_to_argv(t_astnode *node); +char **token_to_argv(t_ld *ld); + +int add_new_job(t_job *job); #endif diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 744dcad4..0437435c 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */ -/* Updated: 2017/03/02 21:01:59 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:38:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ # define JOB_NOTIFIED (1 << 0) # define JOB_BG (1 << 1) +# define JOB_IS_BG(j) (j & JOB_BG) +# define JOB_IS_FG(j) (!JOB_IS_BG(j)) # define JOBS_OPTS_L (1 << 0) @@ -28,7 +30,7 @@ struct s_job { int id; pid_t pgid; - t_flag attributes; + t_flag attrs; t_list *first_process; struct termios tmodes; }; diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 64b2f931..100cd236 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ -/* Updated: 2017/02/17 15:56:55 by gwojda ### ########.fr */ +/* Updated: 2017/03/03 16:07:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c index dcd4e16f..d32ad177 100644 --- a/42sh/src/exec/exec_ampersand.c +++ b/42sh/src/exec/exec_ampersand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */ -/* Updated: 2017/03/02 21:02:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:05:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int exec_ampersand(t_btree **ast) exec = &data_singleton()->exec; push(&exec->op_stack, TK_AMP); ft_exec(&(*ast)->left); - exec->attrs &= ~JOB_BG; + exec->attrs &= ~EXEC_BG; ft_exec(&(*ast)->right); /* if (SH_HAS_JOBC(data_singleton()->opts)) */ diff --git a/42sh/src/exec/exec_and_if.c b/42sh/src/exec/exec_and_if.c index b475724e..874f8004 100644 --- a/42sh/src/exec/exec_and_if.c +++ b/42sh/src/exec/exec_and_if.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:28 by jhalford #+# #+# */ -/* Updated: 2017/03/02 20:41:02 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:05:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ int exec_and_if(t_btree **ast) exec = &data_singleton()->exec; push(&exec->op_stack, TK_AND_IF); ft_exec(&(*ast)->left); - exec->attrs |= JOB_AND_IF; + exec->attrs |= EXEC_AND_IF; ft_exec(&(*ast)->right); /* data = data_singleton(); */ diff --git a/42sh/src/exec/exec_case.c b/42sh/src/exec/exec_case.c index dd671440..29f478ab 100644 --- a/42sh/src/exec/exec_case.c +++ b/42sh/src/exec/exec_case.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 18:07:31 by ariard #+# #+# */ -/* Updated: 2017/03/01 16:29:20 by ariard ### ########.fr */ +/* Updated: 2017/03/03 16:29:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,16 @@ int exec_case(t_btree **ast) { t_astnode *node; - char **av; + /* char **av; */ + t_exec *exec; + + exec = &data_singleton()->exec; + /* data_singleton()->exec.process.case_branch = 0; */ + exec->attrs |= EXEC_CASE_BRANCH; - data_singleton()->exec.process.case_branch = 0; node = (*ast)->item; - av = token_to_argv(node); - data_singleton()->exec.process.case_pattern = av[0]; + /* av = token_to_argv(node); */ + /* data_singleton()->exec.process.case_pattern = av[0]; */ + /* exec->case_pattern = av[0]; */ return (0); } diff --git a/42sh/src/exec/exec_case_branch.c b/42sh/src/exec/exec_case_branch.c index 2102769c..1d45eeb5 100644 --- a/42sh/src/exec/exec_case_branch.c +++ b/42sh/src/exec/exec_case_branch.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 18:07:31 by ariard #+# #+# */ -/* Updated: 2017/02/20 22:31:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:28:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,16 +15,19 @@ int exec_case_branch(t_btree **ast) { t_astnode *node; - char **av; + /* char **av; */ + t_exec *exec; - if (data_singleton()->exec.process.case_branch == 1) + exec = &data_singleton()->exec; + /* if (data_singleton()->exec.process.case_branch == 1) */ + if (EXEC_IS_CASE_BRANCH(exec->attrs)) return (0); node = (*ast)->item; - av = token_to_argv(node); - if (ft_strcmp(av[0], data_singleton()->exec.process.case_pattern) == 1) - { - data_singleton()->exec.process.case_branch = 1; - ft_exec(&(*ast)->right); - } + /* av = token_to_argv(node); */ + /* if (ft_strcmp(av[0], data_singleton()->exec.process.case_pattern) == 1) */ + /* { */ + /* data_singleton()->exec.process.case_branch = 1; */ + /* ft_exec(&(*ast)->right); */ + /* } */ return (0); } diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index e22d5585..ac3023bb 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,21 +6,19 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/03/02 21:16:23 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:36:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -char **token_to_argv(t_cmd *cmd) +char **token_to_argv(t_ld *ld) { char **my_tab; int index; char **expand; char **content; - t_ld *ld; - ld = cmd->token; my_tab = NULL; while (ld) { @@ -37,33 +35,40 @@ char **token_to_argv(t_cmd *cmd) return (my_tab); } -int exec_job(t_btree **ast) +int exec_cmd(t_btree **ast) { - t_list *cmd; + t_cmd *cmd; + t_job *job; + t_exec *exec; t_process p; - t_list *first_process; int fds[2]; + int op; - cmd = ((t_astnode *)(*ast)->item)->data; + cmd = &((t_astnode *)(*ast)->item)->data.cmd; exec = &data_singleton()->exec; - if (pop(&exec.op_stack) == TK_AMP) - exec->attrs |= JOB_BG; - first_process = NULL; + job = &data_singleton()->exec.job; + process_reset(&p); + op = pop(&exec->op_stack); + fds[PIPE_WRITE] = STDOUT; fds[PIPE_READ] = STDIN; - while (cmd) + if (op == TK_AMP) + exec->attrs |= JOB_BG; + else if (op == TK_PIPE) + pipe(fds); + p.fdin = exec->fdin; + p.fdout = fds[PIPE_WRITE]; + exec->fdin = fds[PIPE_READ]; + if (IS_PIPESTART(p)) { - p.fdin = fds[PIPE_READ]; - p.fdout = cmd->next ? pipe(fds) && fds[PIPE_WRITE] : STDOUT; - process_reset(&p); - if (!(p.av = token_to_argv(cmd->content))) - return (1); - process_setexec(cmd->content, &p); - if (!(launch_process(p))) - ft_lstadd(&first_process, ft_lstnew(&p, sizeof(p))); - cmd = cmd->next; + job->first_process = NULL; + job->attrs = EXEC_IS_FG(exec->attrs) ? 0 : JOB_BG; } - add_new_job(first_process, EXEC_IS_FG(exec->attrs)); - ft_lstadd(&jobc->first_job, ft_lstnew(&job, sizeof(*job))); -// btree_delone(ast, &ast_free); + if (!(p.av = token_to_argv(cmd->token))) + return (1); + process_setexec(&p); + if (!(launch_process(&p))) + ft_lstadd(&job->first_process, ft_lstnew(&p, sizeof(p))); + if (IS_PIPEEND(p)) + add_new_job(job); return (0); } diff --git a/42sh/src/exec/exec_elif.c b/42sh/src/exec/exec_elif.c index ffefcdba..be3bc119 100644 --- a/42sh/src/exec/exec_elif.c +++ b/42sh/src/exec/exec_elif.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 18:08:53 by ariard #+# #+# */ -/* Updated: 2017/02/20 22:35:47 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:14:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,12 +14,18 @@ int exec_elif(t_btree **ast) { - if (data_singleton()->exec.process.if_branch == 1) + t_exec *exec; + + exec = &data_singleton()->exec; + /* if (data_singleton()->exec.process.if_branch == 1) */ + if (EXEC_IS_IF_BRANCH(exec->attrs)) return (0); ft_exec(&(*ast)->left); - if (data_singleton()->exec.process.status == 1) + if (ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0")) + /* if (data_singleton()->exec.process.status == 1) */ { - data_singleton()->exec.process.if_branch = 1; + /* data_singleton()->exec.process.if_branch = 1; */ + exec->attrs |= EXEC_IF_BRANCH; ft_exec(&(*ast)->right); } return (0); diff --git a/42sh/src/exec/exec_else.c b/42sh/src/exec/exec_else.c index 9b31f5c6..739753c9 100644 --- a/42sh/src/exec/exec_else.c +++ b/42sh/src/exec/exec_else.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 18:55:07 by ariard #+# #+# */ -/* Updated: 2017/02/06 19:13:05 by ariard ### ########.fr */ +/* Updated: 2017/03/03 15:56:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,14 @@ int exec_else(t_btree **ast) { - if (data_singleton()->exec.process.if_branch == 0) + t_exec *exec; + + exec = &data_singleton()->exec; + if (EXEC_IS_IF_BRANCH(exec->attrs)) + /* if (data_singleton()->exec.process.if_branch == 0) */ { - data_singleton()->exec.process.if_branch = 1; + exec->attrs |= EXEC_IF_BRANCH; + /* data_singleton()->exec.process.if_branch = 1; */ ft_exec(&(*ast)->right); } return (0); diff --git a/42sh/src/exec/exec_for.c b/42sh/src/exec/exec_for.c index e1defebc..1eed20e7 100644 --- a/42sh/src/exec/exec_for.c +++ b/42sh/src/exec/exec_for.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 20:42:20 by ariard #+# #+# */ -/* Updated: 2017/02/06 20:42:21 by ariard ### ########.fr */ +/* Updated: 2017/03/03 16:26:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_if.c b/42sh/src/exec/exec_if.c index 81aab2a7..cd03b39f 100644 --- a/42sh/src/exec/exec_if.c +++ b/42sh/src/exec/exec_if.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 18:07:31 by ariard #+# #+# */ -/* Updated: 2017/02/20 22:31:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:30:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,18 @@ int exec_if(t_btree **ast) { - data_singleton()->exec.process.if_branch = 0; + t_exec *exec; + + exec = &data_singleton()->exec; + + /* data_singleton()->exec.process.if_branch = 0; */ + exec->attrs &= ~EXEC_IF_BRANCH; ft_exec(&(*ast)->left); - if (data_singleton()->exec.process.status == 1) + /* if (data_singleton()->exec.process.status == 1) */ + if (ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0") == 0) { - data_singleton()->exec.process.if_branch = 1; + /* data_singleton()->exec.process.if_branch = 1; */ + exec->attrs |= EXEC_IF_BRANCH; ft_exec(&(*ast)->right); } return (0); diff --git a/42sh/src/exec/exec_less.c b/42sh/src/exec/exec_less.c deleted file mode 100644 index 1bbdb78c..00000000 --- a/42sh/src/exec/exec_less.c +++ /dev/null @@ -1,32 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* exec_less.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */ -/* Updated: 2017/03/01 16:37:28 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "exec.h" - -int exec_less(t_btree **ast) -{ - t_astnode *node; - int fd; - - fd = 0; - node = (*ast)->item; -// fd = open(node->data.redir.word.word, O_RDONLY); - data_singleton()->exec.process.fdin = fd; - /* ft_strappend(&data->exec.process.command, "<"); */ - /* ft_strappend(&data->exec.process.command, node->data.redir.word.word); */ - ft_exec(&(*ast)->left); - data_singleton()->exec.process.fdin = STDIN; - /* data->exec.process.command = NULL; */ - -// btree_delone(ast, &ast_free); - return (0); -} diff --git a/42sh/src/exec/exec_or_if.c b/42sh/src/exec/exec_or_if.c index a0c8e3f6..ec6bc226 100644 --- a/42sh/src/exec/exec_or_if.c +++ b/42sh/src/exec/exec_or_if.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 21:06:17 by jhalford #+# #+# */ -/* Updated: 2017/03/02 21:02:34 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:07:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ int exec_or_if(t_btree **ast) exec = &data_singleton()->exec; push(&exec->op_stack, TK_OR_IF); ft_exec(&(*ast)->left); - exec->attrs |= JOB_OR_IF; + exec->attrs |= EXEC_OR_IF; ft_exec(&(*ast)->right); /* data = data_singleton(); */ diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 9a1a90bc..63c280c0 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/21 21:47:43 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:27:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,10 @@ int exec_pipe(t_btree **ast) { - t_data *data; - t_process *p; + t_exec *exec; - DG("exec pipe"); - data = data_singleton(); - p = &data->exec.process; - p->pipe_count++; + exec = &data_singleton()->exec; + push(&exec->op_stack, TK_PIPE); ft_exec(&(*ast)->left); ft_exec(&(*ast)->right); /* btree_delone(ast, &ast_free); */ diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index 0d49fe4f..e8720350 100644 --- a/42sh/src/exec/exec_semi.c +++ b/42sh/src/exec/exec_semi.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ -/* Updated: 2017/03/02 20:41:25 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:26:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int exec_semi(t_btree **ast) exec = &data_singleton()->exec; push(&exec->op_stack, TK_SEMI); ft_exec(&(*ast)->left); - exec->attrs ~= ~JOB_AOL_MASK; + exec->attrs &= ~EXEC_AOL_MASK; ft_exec(&(*ast)->right); // btree_delone(ast, &ast_free); return (0); diff --git a/42sh/src/exec/exec_until.c b/42sh/src/exec/exec_until.c index 6f1e0c33..e2cf5791 100644 --- a/42sh/src/exec/exec_until.c +++ b/42sh/src/exec/exec_until.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 20:42:20 by ariard #+# #+# */ -/* Updated: 2017/02/06 20:42:21 by ariard ### ########.fr */ +/* Updated: 2017/03/03 16:30:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ int exec_until(t_btree **ast) { ft_exec(&(*ast)->left); - while (data_singleton()->exec.process.status == 0) + while (ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0") == 0) { ft_exec(&(*ast)->right); ft_exec(&(*ast)->left); diff --git a/42sh/src/exec/exec_var.c b/42sh/src/exec/exec_var.c index e830950d..016c24a6 100644 --- a/42sh/src/exec/exec_var.c +++ b/42sh/src/exec/exec_var.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/30 17:33:53 by ariard #+# #+# */ -/* Updated: 2017/02/06 22:05:35 by ariard ### ########.fr */ +/* Updated: 2017/03/03 16:28:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ int exec_var(t_btree **ast) char **av; node = (*ast)->item; - av = token_to_argv(node); + /* av = token_to_argv(node); */ builtin_setenv("setenv", av, data_singleton()->local_var); return (0); } diff --git a/42sh/src/exec/exec_while.c b/42sh/src/exec/exec_while.c index d9824950..e8ff4ac8 100644 --- a/42sh/src/exec/exec_while.c +++ b/42sh/src/exec/exec_while.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/30 17:33:53 by ariard #+# #+# */ -/* Updated: 2017/02/06 22:05:35 by ariard ### ########.fr */ +/* Updated: 2017/03/03 16:05:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ int exec_while(t_btree **ast) { ft_exec(&(*ast)->left); - while (data_singleton()->exec.process.status == 1) + while (ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0")) { ft_exec(&(*ast)->right); ft_exec(&(*ast)->left); diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 42875868..e3579dc9 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/03/02 20:53:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:28:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ t_execmap g_execmap[] = {TK_ELSE, &exec_else}, {TK_UNTIL, &exec_until}, /* {TK_SUBSHELL, &exec_}, */ - {TK_WORD, &exec_job}, + {TK_WORD, &exec_cmd}, {0, 0}, }; diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 65184fc3..76fbd434 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/03/02 20:29:33 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:29:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,12 +18,7 @@ int launch_process(t_process *p) int pid; exec = &data_singleton()->exec; - if (p->attributes & PROCESS_UNKNOWN) - { - 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)) + if (p->attributes & PROCESS_BUILTIN && IS_PIPESINGLE(*p)) { if (process_redirect(p)) return (1); @@ -43,6 +38,12 @@ int launch_process(t_process *p) pid = fork(); if (pid == 0) { + if (p->attributes & PROCESS_UNKNOWN) + { + ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); + exit(127); + /* set_exitstatus(127, 1); */ + } process_setgroup(p, 0); process_setsig(); if (process_redirect(p)) diff --git a/42sh/src/exec/process_reset.c b/42sh/src/exec/process_reset.c index 638f1e75..54dffa26 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/03/02 20:44:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:36:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index fd2f9848..4f74a7be 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -6,25 +6,16 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */ -/* Updated: 2017/03/02 21:00:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:32:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int process_setexec(t_cmd *cmd, t_process *p) +int process_setexec(t_process *p) { - t_flag type; - - type = cmd->type; p->path = NULL; - /* if (type == TK_SUBSHELL) */ - /* { */ - /* p->execf = &execve; */ - /* p->attributes |= PROCESS_SUBSHELL; */ - /* p->path = ft_strdup(p->av[0]); */ - /* } */ - else if ((p->execf = is_builtin(p))) + if ((p->execf = is_builtin(p))) p->attributes |= PROCESS_BUILTIN; else if (ft_strchr(p->av[0], '/')) { diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index 16223c08..8654d2f8 100644 --- a/42sh/src/exec/process_setgroup.c +++ b/42sh/src/exec/process_setgroup.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:48:10 by jhalford #+# #+# */ -/* Updated: 2017/03/02 20:13:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:34:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ int process_setgroup(t_process *p, pid_t pid) if (!j->pgid) j->pgid = pid ? pid : getpid(); setpgid(pid, j->pgid); - if (pid == 0 && JOB_IS_FG(j->attributes)) + if (pid == 0 && JOB_IS_FG(j->attrs)) tcsetpgrp(STDIN, j->pgid); return (0); } diff --git a/42sh/src/glob/command_getoutput.c b/42sh/src/glob/command_getoutput.c index fdf3dda4..30cbc11f 100644 --- a/42sh/src/glob/command_getoutput.c +++ b/42sh/src/glob/command_getoutput.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/12 14:01:59 by jhalford #+# #+# */ -/* Updated: 2017/02/24 22:04:43 by ariard ### ########.fr */ +/* Updated: 2017/03/03 16:45:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,33 +15,34 @@ char *command_getoutput(char *command) { - int fds[2]; - t_btree *ast; - t_astnode item; - char *output; - char buf[BUF_SIZE + 1]; - int ret; - t_exec *exec; + return (command); + /* int fds[2]; */ + /* t_btree *ast; */ + /* t_astnode item; */ + /* char *output; */ + /* char buf[BUF_SIZE + 1]; */ + /* int ret; */ + /* t_exec *exec; */ - output = NULL; - exec = &data_singleton()->exec; - item.type = TK_SUBSHELL; - item.data.sstr = malloc(4 * sizeof(char *)); - item.data.sstr[0] = ft_strdup(data_singleton()->argv[0]); - item.data.sstr[1] = ft_strdup("-c"); - item.data.sstr[2] = ft_strdup(command); - item.data.sstr[3] = NULL; - ast = btree_create_node(&item, sizeof(item)); - pipe(fds); - exec->process.fdout = fds[PIPE_WRITE]; - exec_command(&ast); - exec->process.fdout = STDOUT; - close(fds[PIPE_WRITE]); - while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE))) - { - buf[ret] = 0; - ft_strappend(&output, buf); - } - close(fds[PIPE_READ]); - return (output); + /* output = NULL; */ + /* exec = &data_singleton()->exec; */ + /* item.type = TK_SUBSHELL; */ + /* item.data.sstr = malloc(4 * sizeof(char *)); */ + /* item.data.sstr[0] = ft_strdup(data_singleton()->argv[0]); */ + /* item.data.sstr[1] = ft_strdup("-c"); */ + /* item.data.sstr[2] = ft_strdup(command); */ + /* item.data.sstr[3] = NULL; */ + /* ast = btree_create_node(&item, sizeof(item)); */ + /* pipe(fds); */ + /* exec->process.fdout = fds[PIPE_WRITE]; */ + /* exec_command(&ast); */ + /* exec->process.fdout = STDOUT; */ + /* close(fds[PIPE_WRITE]); */ + /* while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE))) */ + /* { */ + /* buf[ret] = 0; */ + /* ft_strappend(&output, buf); */ + /* } */ + /* close(fds[PIPE_READ]); */ + /* return (output); */ } diff --git a/42sh/src/job-control/add_new_job.c b/42sh/src/job-control/add_new_job.c index 88e31d8b..2797edc2 100644 --- a/42sh/src/job-control/add_new_job.c +++ b/42sh/src/job-control/add_new_job.c @@ -6,25 +6,22 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/02 20:44:21 by jhalford #+# #+# */ -/* Updated: 2017/03/02 21:04:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:47:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -int add_new_job(t_list *first_process, int foreground) +int add_new_job(t_job *job) { t_jobc *jobc; - t_job job; - if (!first_process) + if (!job->first_process) return (1); jobc = &data_singleton()->jobc; job_update_id(); job->id = jobc->current_id; - job->pgid = ((t_process*)first_process->content)->pid; - job->attrs = foreground ? 0 : JOB_BG; - job->first_process = first_process; + job->pgid = ((t_process*)job->first_process->content)->pid; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); if (JOB_IS_FG(job->attrs)) put_job_in_foreground(job, 0); diff --git a/42sh/src/job-control/do_job_notification.c b/42sh/src/job-control/do_job_notification.c index 74eff978..0e2aeef6 100644 --- a/42sh/src/job-control/do_job_notification.c +++ b/42sh/src/job-control/do_job_notification.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 13:01:19 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:47:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:46:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,14 +27,14 @@ int do_job_notification(void) { j = jlist->content; if (job_is_completed(j->id) - || (job_is_stopped(j->id) && !(j->attributes & JOB_NOTIFIED))) + || (job_is_stopped(j->id) && !(j->attrs & JOB_NOTIFIED))) { ret = 1; job_notify_change(j->id); if (job_is_completed(j->id)) job_remove(j->id); else - j->attributes |= JOB_NOTIFIED; + j->attrs |= JOB_NOTIFIED; } jlist = jlist->next; } diff --git a/42sh/src/job-control/mark_job_as_running.c b/42sh/src/job-control/mark_job_as_running.c index ea82a0e7..86e5cf01 100644 --- a/42sh/src/job-control/mark_job_as_running.c +++ b/42sh/src/job-control/mark_job_as_running.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/08 14:40:40 by jhalford #+# #+# */ -/* Updated: 2017/01/31 15:08:11 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:47:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,5 +28,5 @@ void mark_job_as_running(t_job *j) } plist = plist->next; } - j->attributes &= ~JOB_NOTIFIED; + j->attrs &= ~JOB_NOTIFIED; } diff --git a/42sh/src/lexer/lexer_bquote.c b/42sh/src/lexer/lexer_bquote.c index 0ffbc04e..bf8a9b7f 100644 --- a/42sh/src/lexer/lexer_bquote.c +++ b/42sh/src/lexer/lexer_bquote.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 22:03:48 by jhalford #+# #+# */ -/* Updated: 2017/02/17 15:36:49 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:48:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ int lexer_bquote(t_list **alst, t_lexer *lexer) push(&lexer->stack, lexer->state); return (lexer_lex(alst, lexer)); } - top_state = *(int*)pop(&lexer->stack)->content; + top_state = pop(&lexer->stack); lexer->state = top_state == DQUOTE_BQUOTE ? DQUOTE : DEFAULT; return (lexer_lex(alst, lexer)); } diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 554fe821..0a157747 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/03/02 21:02:17 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 16:48:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,8 @@ int data_init(void) data->exec.fd_save[1] = fcntl(1, F_DUPFD_CLOEXEC); data->exec.fd_save[2] = fcntl(2, F_DUPFD_CLOEXEC); data->exec.op_stack = NULL; - data->attrs = 0; + data->exec.fdin = STDIN; + data->exec.attrs = 0; /* 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 109005cf..9b6b8fb6 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/03/02 21:00:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 15:59:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From 5159c30b67077e62fddbf39ffe3b0ecf97bdc9ae Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Mar 2017 16:59:21 +0100 Subject: [PATCH 04/13] new libft for pop --- 42sh/libft | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/42sh/libft b/42sh/libft index 8f6e64fa..318efc7c 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 8f6e64fa9b4ac1dd3e3d5200fb93471ddfeedd40 +Subproject commit 318efc7cfb7b7cc9d3714fa19fd2be7382b6adec From 2ad34631e30e663ba6634ae607f10ccef63abfd6 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Mar 2017 17:58:08 +0100 Subject: [PATCH 05/13] parentheses dont trigger more input after lexing --- 42sh/Makefile | 6 +----- 42sh/includes/lexer.h | 4 ++-- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/lexer/get_lexer_stack.c | 12 ++++++++++++ 42sh/src/lexer/get_state_global.c | 2 +- 42sh/src/lexer/lexer_bquote.c | 2 +- 42sh/src/lexer/lexer_default.c | 2 +- 42sh/src/lexer/lexer_lex.c | 4 ++-- 42sh/src/lexer/lexer_number.c | 2 +- 42sh/src/lexer/lexer_paren.c | 12 ++++++++++++ 42sh/src/lexer/lexer_word.c | 26 +------------------------- 42sh/src/main/data_init.c | 2 +- 42sh/src/main/main.c | 8 +++++--- 13 files changed, 41 insertions(+), 43 deletions(-) diff --git a/42sh/Makefile b/42sh/Makefile index ae67e26f..3b26beb7 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,11 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -<<<<<<< HEAD -# Updated: 2017/03/03 14:36:32 by ariard ### ########.fr # -======= -# Updated: 2017/03/02 16:23:48 by jhalford ### ########.fr # ->>>>>>> pda_execution +# Updated: 2017/03/03 17:56:12 by jhalford ### ########.fr # # # # **************************************************************************** # diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 927d5dec..078b2005 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/03/02 17:15:15 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 17:55:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ | TK_DO | TK_IF | TK_FI | TK_THEN | TK_ELIF | TK_ELSE) enum e_lexstate { + PAREN, DEFAULT, NEWLINE, DELIM, @@ -39,7 +40,6 @@ enum e_lexstate BQUOTE, DQUOTE_BQUOTE, BACKSLASH, - PAREN, CURLY_BRACKETS, ASSIGNEMENT_WORD, COMMENT, diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index d39de6f2..cf915722 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/03/03 17:31:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 17:37:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/get_lexer_stack.c b/42sh/src/lexer/get_lexer_stack.c index 10ce9b70..d8c449ec 100644 --- a/42sh/src/lexer/get_lexer_stack.c +++ b/42sh/src/lexer/get_lexer_stack.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_lexer_stack.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/03 17:39:45 by jhalford #+# #+# */ +/* Updated: 2017/03/03 17:40:24 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "lexer.h" int get_lexer_stack(t_lexer lexer) diff --git a/42sh/src/lexer/get_state_global.c b/42sh/src/lexer/get_state_global.c index e677ed39..550e7b2c 100644 --- a/42sh/src/lexer/get_state_global.c +++ b/42sh/src/lexer/get_state_global.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:39:06 by jhalford #+# #+# */ -/* Updated: 2017/03/03 17:31:47 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 17:56:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_bquote.c b/42sh/src/lexer/lexer_bquote.c index bf8a9b7f..b4360556 100644 --- a/42sh/src/lexer/lexer_bquote.c +++ b/42sh/src/lexer/lexer_bquote.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 22:03:48 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:48:07 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 17:45:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index d2acede7..94bf796c 100644 --- a/42sh/src/lexer/lexer_default.c +++ b/42sh/src/lexer/lexer_default.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ -/* Updated: 2017/03/01 23:40:16 by ariard ### ########.fr */ +/* Updated: 2017/03/03 17:55:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_lex.c b/42sh/src/lexer/lexer_lex.c index 27535a56..75f0e4f1 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/24 21:38:42 by ariard ### ########.fr */ +/* Updated: 2017/03/03 17:55:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ int (*g_lexer[])(t_list **alst, t_lexer *lexer) = { + &lexer_paren, &lexer_default, &lexer_newline, &lexer_delim, @@ -30,7 +31,6 @@ int (*g_lexer[])(t_list **alst, t_lexer *lexer) = &lexer_bquote, &lexer_bquote, &lexer_backslash, - &lexer_paren, &lexer_curly_brackets, &lexer_assignement_word, &lexer_comment, diff --git a/42sh/src/lexer/lexer_number.c b/42sh/src/lexer/lexer_number.c index 8759c156..90562cb1 100644 --- a/42sh/src/lexer/lexer_number.c +++ b/42sh/src/lexer/lexer_number.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:45 by jhalford #+# #+# */ -/* Updated: 2017/03/01 23:39:37 by ariard ### ########.fr */ +/* Updated: 2017/03/03 17:35:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_paren.c b/42sh/src/lexer/lexer_paren.c index dd795357..e80b6a1a 100644 --- a/42sh/src/lexer/lexer_paren.c +++ b/42sh/src/lexer/lexer_paren.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_paren.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/03 17:37:15 by jhalford #+# #+# */ +/* Updated: 2017/03/03 17:48:28 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "lexer.h" int lexer_paren(t_list **alst, t_lexer *lexer) diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index 50ad864f..066379f2 100644 --- a/42sh/src/lexer/lexer_word.c +++ b/42sh/src/lexer/lexer_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */ -/* Updated: 2017/03/03 17:31:56 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 17:56:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,27 +51,3 @@ int lexer_word(t_list **alst, t_lexer *lexer) lexer->pos++; return (lexer_lex(alst, lexer)); } - -/* -int lexer_word(t_list **alst, t_lexer *lexer) -{ - t_token *token; - t_lexstate state; - - token = (*alst)->content; - token->type = TK_WORD; - if ((state = get_state_global(lexer))) - { - lexer->state = state; - return (lexer_lex(alst, lexer)); - } - if ((state = get_state_redir(lexer))) - { - lexer->state = state; - return (lexer_lex(alst, lexer)); - } - token_append(token, lexer, 0, 0); - lexer->pos++; - return (lexer_lex(alst, lexer)); -} -*/ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 0a157747..1aa6c777 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/03/03 16:48:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 17:39:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 75ce40bd..820abfb9 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/03/03 17:32:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 17:55:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,6 @@ int handle_instruction(int fd) if ((ret = readline(fd, get_lexer_stack(lexer) || parser.state == UNDEFINED, &str))) { - /* ft_putstr("bonjour"); */ if (ret == -1) return (-1); return (parser.state == UNDEFINED ? error_EOF() : 1); @@ -42,9 +41,11 @@ int handle_instruction(int fd) else if (get_lexer_stack(lexer) == DLESS) lexer.state = DLESS; ltoken = ft_lstlast(token); + DG(); if (lexer_lex(token ? <oken : &token, &lexer)) return (1); - if (get_lexer_stack(lexer)) + DG(); + if (get_lexer_stack(lexer) > 1) continue ; lexer.state = DEFAULT; token_print(token); @@ -62,6 +63,7 @@ int handle_instruction(int fd) error_syntax(&token); token = NULL; } + DG(); DG("Before execution:"); btree_print(STDBUG, ast, &ft_putast); if (ft_exec(&ast)) From 67751202645e0bc05c0467b82cc0414d5cd892d9 Mon Sep 17 00:00:00 2001 From: wescande Date: Fri, 3 Mar 2017 17:59:39 +0100 Subject: [PATCH 06/13] protection de segv au lancement du shell (redirection entrante & nom de fichier non valid) --- 42sh/src/main/main.c | 9 +++++++-- 42sh/src/main/shell_get_avdata.c | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index a4ca3108..8ac2065d 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/03/02 21:40:50 by ariard ### ########.fr */ +/* Updated: 2017/03/03 17:49:18 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,7 @@ int handle_instruction(int fd) if ((ret = readline(fd, get_lexer_stack(lexer) || parser.state == UNDEFINED, &str))) { + ft_putnbr(ret); ft_putstr("bonjour"); if (ret == -1) return (-1); @@ -108,7 +109,11 @@ int main(int ac, char **av) shell_init(ac, av); DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF"); - fd = get_input_fd(); + if ((fd = get_input_fd() < 0)) + { + ft_printf("{red}%s: No such file or directory\n{eoc}", SHELL_NAME); + return (1); + } while (handle_instruction(fd) == 0) { // lexer_clean; diff --git a/42sh/src/main/shell_get_avdata.c b/42sh/src/main/shell_get_avdata.c index 6194cbf3..16a34d13 100644 --- a/42sh/src/main/shell_get_avdata.c +++ b/42sh/src/main/shell_get_avdata.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 17:14:52 by jhalford #+# #+# */ -/* Updated: 2017/01/19 20:56:05 by ariard ### ########.fr */ +/* Updated: 2017/03/03 17:33:33 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ char *shell_get_avdata() data = data_singleton(); av = data->argv; i = 1; - while (av[i][0] == '-') + while (av[i] && av[i][0] == '-') { if (ft_strcmp(av[i], "--") == 0) { From d146126135c19da7df2293bfaae34b5d90589aef Mon Sep 17 00:00:00 2001 From: wescande Date: Fri, 3 Mar 2017 18:01:25 +0100 Subject: [PATCH 07/13] rectif makefile --- 42sh/Makefile | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/42sh/Makefile b/42sh/Makefile index ae67e26f..eb5de9c9 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,11 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -<<<<<<< HEAD -# Updated: 2017/03/03 14:36:32 by ariard ### ########.fr # -======= -# Updated: 2017/03/02 16:23:48 by jhalford ### ########.fr # ->>>>>>> pda_execution +# Updated: 2017/03/03 18:00:59 by wescande ### ########.fr # # # # **************************************************************************** # From 03c62f69a691cb0886daf1b73278cf21a193dc1e Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Mar 2017 18:03:17 +0100 Subject: [PATCH 08/13] stuff --- 42sh/Makefile | 2 +- 42sh/includes/lexer.h | 2 +- 42sh/src/exec/exec_command.c | 3 ++- 42sh/src/exec/exec_semi.c | 5 ++++- 42sh/src/exec/ft_exec.c | 9 +++++---- 42sh/src/lexer/lexer_newline.c | 2 +- 42sh/src/main/main.c | 5 +---- 7 files changed, 15 insertions(+), 13 deletions(-) diff --git a/42sh/Makefile b/42sh/Makefile index 3b26beb7..e13fd4e3 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/03/03 17:56:12 by jhalford ### ########.fr # +# Updated: 2017/03/03 17:58:15 by jhalford ### ########.fr # # # # **************************************************************************** # diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 078b2005..12f76e17 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/03/03 17:55:18 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 17:58:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index cf915722..801cb235 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/03/03 17:37:07 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 17:59:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,6 +49,7 @@ int exec_cmd(t_btree **ast) job = &data_singleton()->exec.job; process_reset(&p); op = pop(&exec->op_stack); + DG("op=%i", op); fds[PIPE_WRITE] = STDOUT; fds[PIPE_READ] = STDIN; if (op == TK_AMP) diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index e8720350..58013f17 100644 --- a/42sh/src/exec/exec_semi.c +++ b/42sh/src/exec/exec_semi.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:26:08 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:01:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,11 @@ int exec_semi(t_btree **ast) t_exec *exec; exec = &data_singleton()->exec; + DG(); push(&exec->op_stack, TK_SEMI); + DG(); ft_exec(&(*ast)->left); + DG(); exec->attrs &= ~EXEC_AOL_MASK; ft_exec(&(*ast)->right); // btree_delone(ast, &ast_free); diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index e3579dc9..5d3b8090 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/03/03 16:28:22 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:02:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ t_execmap g_execmap[] = {TK_ELSE, &exec_else}, {TK_UNTIL, &exec_until}, /* {TK_SUBSHELL, &exec_}, */ - {TK_WORD, &exec_cmd}, + {CMD, &exec_cmd}, {0, 0}, }; @@ -36,6 +36,7 @@ int ft_exec(t_btree **ast) int i; i = 0; + DG(); if (!*ast) return (0); item = (*ast)->item; @@ -43,8 +44,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)); } i++; diff --git a/42sh/src/lexer/lexer_newline.c b/42sh/src/lexer/lexer_newline.c index 396dc6d3..31b45b4b 100644 --- a/42sh/src/lexer/lexer_newline.c +++ b/42sh/src/lexer/lexer_newline.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/23 23:19:46 by ariard #+# #+# */ -/* Updated: 2017/02/09 19:55:04 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:00:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 820abfb9..6e67591b 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/03/03 17:55:01 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 17:58:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,10 +41,8 @@ int handle_instruction(int fd) else if (get_lexer_stack(lexer) == DLESS) lexer.state = DLESS; ltoken = ft_lstlast(token); - DG(); if (lexer_lex(token ? <oken : &token, &lexer)) return (1); - DG(); if (get_lexer_stack(lexer) > 1) continue ; lexer.state = DEFAULT; @@ -63,7 +61,6 @@ int handle_instruction(int fd) error_syntax(&token); token = NULL; } - DG(); DG("Before execution:"); btree_print(STDBUG, ast, &ft_putast); if (ft_exec(&ast)) From 4c44407b65c8d734138eea855584293e0fbb9c21 Mon Sep 17 00:00:00 2001 From: wescande Date: Fri, 3 Mar 2017 18:49:39 +0100 Subject: [PATCH 09/13] correctif on some stuff + ajout framework for test --- 42sh/TESTSHELL/simple_cmd/echo.test | 1 + 42sh/TESTSHELL/simple_cmd/ls.test | 1 + 42sh/TESTSHELL/simple_cmd/pwd.test | 1 + 42sh/TESTSHELL/stderr_ref | 0 42sh/TESTSHELL/stderr_test | 0 42sh/TESTSHELL/stdin_ref | 14 +++++++ 42sh/TESTSHELL/stdin_test | 43 ++++++++++++++++++++ 42sh/src/exec/exec_command.c | 2 +- 42sh/src/job-control/add_new_job.c | 4 +- 42sh/src/job-control/put_job_in_foreground.c | 3 +- 42sh/src/line-editing/readline.c | 2 +- 42sh/src/main/main.c | 9 +++- 42sh/test_framework.sh | 41 +++++++++++++++++++ 13 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 42sh/TESTSHELL/simple_cmd/echo.test create mode 100644 42sh/TESTSHELL/simple_cmd/ls.test create mode 100644 42sh/TESTSHELL/simple_cmd/pwd.test create mode 100644 42sh/TESTSHELL/stderr_ref create mode 100644 42sh/TESTSHELL/stderr_test create mode 100644 42sh/TESTSHELL/stdin_ref create mode 100644 42sh/TESTSHELL/stdin_test create mode 100755 42sh/test_framework.sh diff --git a/42sh/TESTSHELL/simple_cmd/echo.test b/42sh/TESTSHELL/simple_cmd/echo.test new file mode 100644 index 00000000..3997594c --- /dev/null +++ b/42sh/TESTSHELL/simple_cmd/echo.test @@ -0,0 +1 @@ +echo 'je suis un test' diff --git a/42sh/TESTSHELL/simple_cmd/ls.test b/42sh/TESTSHELL/simple_cmd/ls.test new file mode 100644 index 00000000..9e2740c6 --- /dev/null +++ b/42sh/TESTSHELL/simple_cmd/ls.test @@ -0,0 +1 @@ +ls diff --git a/42sh/TESTSHELL/simple_cmd/pwd.test b/42sh/TESTSHELL/simple_cmd/pwd.test new file mode 100644 index 00000000..f748bdd0 --- /dev/null +++ b/42sh/TESTSHELL/simple_cmd/pwd.test @@ -0,0 +1 @@ +pwd diff --git a/42sh/TESTSHELL/stderr_ref b/42sh/TESTSHELL/stderr_ref new file mode 100644 index 00000000..e69de29b diff --git a/42sh/TESTSHELL/stderr_test b/42sh/TESTSHELL/stderr_test new file mode 100644 index 00000000..e69de29b diff --git a/42sh/TESTSHELL/stdin_ref b/42sh/TESTSHELL/stdin_ref new file mode 100644 index 00000000..b689ff6b --- /dev/null +++ b/42sh/TESTSHELL/stdin_ref @@ -0,0 +1,14 @@ +42sh +Makefile +TESTSHELL +donovan_segaults_06-02 +file1 +file2 +includes +libft +objs +pdf +sample +src +test_framework.sh +update_makefile.sh diff --git a/42sh/TESTSHELL/stdin_test b/42sh/TESTSHELL/stdin_test new file mode 100644 index 00000000..4b6a7bbd --- /dev/null +++ b/42sh/TESTSHELL/stdin_test @@ -0,0 +1,43 @@ + main.c 107start of shell JOBC is ON + token_print.c 21 token print + token_print.c 25 token : TK_WORD data [ls] + + insert_newline.c 19 insert newline + ft_parse.c 45  + + build_tree.c 83 func TK : 'TK_WORD' TK : 'TK_WORD' + add_cmd.c 55 add data + ft_parse.c 45  + + build_tree.c 83 func TK : 'TK_NEWLINE' TK : 'TK_NEWLINE' + add_sep.c 21 add sep + ft_parse.c 45  + + ft_parse.c 58 sucessful parsing + main.c 64 Before execution: + NEW + +----+ + CMD + ft_exec.c 39  + ft_exec.c 48 match : TK_NEWLINE and TK_NEWLINE + exec_semi.c 20  + exec_semi.c 22  + ft_exec.c 39  + ft_exec.c 48 match : NON-DEFINED and NON-DEFINED + exec_command.c 52 op=2118139328 +42sh +Makefile +TESTSHELL +donovan_segaults_06-02 +file1 +file2 +includes +libft +objs +pdf +sample +src +test_framework.sh +update_makefile.sh + exec_semi.c 24  + ft_exec.c 39  diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 801cb235..e3133e1f 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/03/03 17:59:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:44:43 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/add_new_job.c b/42sh/src/job-control/add_new_job.c index 2797edc2..3838a178 100644 --- a/42sh/src/job-control/add_new_job.c +++ b/42sh/src/job-control/add_new_job.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/02 20:44:21 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:47:47 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:44:42 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,9 @@ int add_new_job(t_job *job) if (JOB_IS_FG(job->attrs)) put_job_in_foreground(job, 0); else + { job_notify_new(job); put_job_in_background(job, 0); + } return (0); } diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 3f783821..636853ed 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:58:36 by jhalford #+# #+# */ -/* Updated: 2017/03/02 20:59:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:44:44 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,6 @@ int put_job_in_foreground(t_job *j, int cont) jobc = &data_singleton()->jobc; tcsetpgrp(STDIN, j->pgid); tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); - if (cont) { tcsetattr(STDIN, TCSADRAIN, &j->tmodes); diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index 8f79d589..6d8f7f48 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/03/02 12:36:35 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:29:03 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 07287a11..9f6b966d 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/03/03 18:05:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:33:40 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,10 +63,14 @@ int handle_instruction(int fd) } DG("Before execution:"); btree_print(STDBUG, ast, &ft_putast); + DG(); if (ft_exec(&ast)) return (1); + DG(); btree_del(&ast, &ast_free); + DG(); ft_add_str_in_history(lexer.str); + DG(); return (0); } @@ -83,6 +87,7 @@ int get_input_fd() return (fd); else if (data->opts & SH_OPTS_LC) { + DG(); pipe(fds); fd = fds[PIPE_READ]; file = shell_get_avdata(); @@ -105,7 +110,7 @@ int main(int ac, char **av) shell_init(ac, av); DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF"); - if ((fd = get_input_fd() < 0)) + if ((fd = get_input_fd()) < 0) { ft_printf("{red}%s: No such file or directory\n{eoc}", SHELL_NAME); return (1); diff --git a/42sh/test_framework.sh b/42sh/test_framework.sh new file mode 100755 index 00000000..eb97fe43 --- /dev/null +++ b/42sh/test_framework.sh @@ -0,0 +1,41 @@ +SHELL=$1 +TOTAL_TEST=0 +NBR_TEST_SUCCESS=0 +unset LIST_TEST +LIST_TEST='./TESTSHELL/*'/'*'.test +DIR_TEST=./TESTSHELL + +do_test() { + +for TEST in $LIST_TEST +do + + ($SHELL < $TEST 1> ${DIR_TEST}/stdin_test 2> ${DIR_TEST}/stderr_test) + (bash < $TEST 1> ${DIR_TEST}/stdin_ref 2> ${DIR_TEST}/stderr_ref) + + if ! diff ${DIR_TEST}/stdin_test ${DIR_TEST}/stdin_ref > /dev/null || + ! diff ${DIR_TEST}/stderr_test ${DIR_TEST}/stderr_test > /dev/null + then + echo "\033[0;31mFAILURE $TEST" >> ${DIR_TEST}/resultat + else + ((NBR_TEST_SUCCESS++)) + fi + ((TOTAL_TEST++)) +done +} + +do_test + +if [ -e ${DIR_TEST}/resultat ] +then + cat ${DIR_TEST}/resultat +fi +echo "\033[0;32mYou succeed $NBR_TEST_SUCCESS tests on $TOTAL_TEST" + +rm -f $DIR_TEST/resultat + +#add random input sed -n $RANDOM "p" /usr/share/dict/words +#add timeout +#charging phase of test +#real verification of references +#to add extranous comportement From 20e96b751fd7b6a876b1c917d38c160a73e416cb Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Mar 2017 18:50:13 +0100 Subject: [PATCH 10/13] pipes work --- 42sh/Makefile | 2 +- 42sh/includes/job_control.h | 4 +-- 42sh/includes/types.h | 2 +- 42sh/src/exec/exec_command.c | 17 +++++++++-- 42sh/src/exec/exec_semi.c | 5 +--- 42sh/src/exec/ft_exec.c | 5 ++-- 42sh/src/exec/launch_process.c | 5 +++- .../mark_process_status.c} | 5 ++-- 42sh/src/exec/process_redirect.c | 6 ++-- 42sh/src/job-control/add_new_job.c | 9 ++---- 42sh/src/job-control/job_update_id.c | 2 +- 42sh/src/job-control/job_update_status.c | 4 +-- 42sh/src/job-control/job_wait.c | 28 ++++++++++++------- 42sh/src/job-control/put_job_in_foreground.c | 4 ++- 42sh/src/parser/read_stack.c | 4 ++- 15 files changed, 62 insertions(+), 40 deletions(-) rename 42sh/src/{job-control/process_mark_status.c => exec/mark_process_status.c} (90%) diff --git a/42sh/Makefile b/42sh/Makefile index 7d39d7e5..be5adabc 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -81,6 +81,7 @@ exec/fd_is_valid.c\ exec/ft_exec.c\ exec/ft_findexec.c\ exec/launch_process.c\ +exec/mark_process_status.c\ exec/process_redirect.c\ exec/process_reset.c\ exec/process_setexec.c\ @@ -159,7 +160,6 @@ job-control/mark_job_as_running.c\ job-control/process_cmp_pid.c\ job-control/process_format.c\ job-control/process_free.c\ -job-control/process_mark_status.c\ job-control/put_job_in_background.c\ job-control/put_job_in_foreground.c\ job-control/sigchld_handler.c\ diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 0437435c..b8f04fa4 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:38:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:35:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,7 +58,7 @@ void job_format_head(t_job *j); void job_update_status(void); void mark_job_as_running (t_job *j); -int process_mark_status(pid_t pid, int status); +int mark_process_status(pid_t pid, int status); int job_is_stopped(int id); int job_is_completed(int id); diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 893007b4..d134e8c9 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/03/03 17:30:47 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:19:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 801cb235..c794768e 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/03/03 17:59:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:49:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,15 +49,19 @@ int exec_cmd(t_btree **ast) job = &data_singleton()->exec.job; process_reset(&p); op = pop(&exec->op_stack); - DG("op=%i", op); fds[PIPE_WRITE] = STDOUT; fds[PIPE_READ] = STDIN; if (op == TK_AMP) exec->attrs |= JOB_BG; else if (op == TK_PIPE) + { pipe(fds); + DG("%i -> PIPE -> %i", fds[PIPE_WRITE], fds[PIPE_READ]); + } p.fdin = exec->fdin; + p.to_close = fds[PIPE_READ]; p.fdout = fds[PIPE_WRITE]; + p.redirs = cmd->redir; exec->fdin = fds[PIPE_READ]; if (IS_PIPESTART(p)) { @@ -69,7 +73,16 @@ int exec_cmd(t_btree **ast) process_setexec(&p); if (!(launch_process(&p))) ft_lstadd(&job->first_process, ft_lstnew(&p, sizeof(p))); + if (fds[PIPE_WRITE] != STDOUT) + close(fds[PIPE_WRITE]); if (IS_PIPEEND(p)) add_new_job(job); + if (JOB_IS_FG(job->attrs)) + put_job_in_foreground(job, 0); + else + { + job_notify_new(job); + put_job_in_background(job, 0); + } return (0); } diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index 58013f17..f664b25e 100644 --- a/42sh/src/exec/exec_semi.c +++ b/42sh/src/exec/exec_semi.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:01:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:10:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,11 +17,8 @@ int exec_semi(t_btree **ast) t_exec *exec; exec = &data_singleton()->exec; - DG(); push(&exec->op_stack, TK_SEMI); - DG(); ft_exec(&(*ast)->left); - DG(); exec->attrs &= ~EXEC_AOL_MASK; ft_exec(&(*ast)->right); // btree_delone(ast, &ast_free); diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 5d3b8090..1c46fea9 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/03/03 18:02:55 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:26:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ t_execmap g_execmap[] = {TK_AMP, &exec_ampersand}, {TK_AND_IF, &exec_and_if}, {TK_OR_IF, &exec_or_if}, - /* {TK_PIPE, &exec_pipe}, */ + {TK_PIPE, &exec_pipe}, {TK_WHILE, &exec_while}, {TK_IF, &exec_if}, {TK_ELIF, &exec_elif}, @@ -36,7 +36,6 @@ int ft_exec(t_btree **ast) int i; i = 0; - DG(); if (!*ast) return (0); item = (*ast)->item; diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 76fbd434..085ce337 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/03/03 16:29:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:49:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,9 @@ int launch_process(t_process *p) int pid; exec = &data_singleton()->exec; + DG("gonna launch [%s]", p->av[0]); + DG("fdin=[%i]", p->fdin); + DG("fdout=[%i]", p->fdout); if (p->attributes & PROCESS_BUILTIN && IS_PIPESINGLE(*p)) { if (process_redirect(p)) diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/exec/mark_process_status.c similarity index 90% rename from 42sh/src/job-control/process_mark_status.c rename to 42sh/src/exec/mark_process_status.c index fd285c0b..2665128d 100644 --- a/42sh/src/job-control/process_mark_status.c +++ b/42sh/src/exec/mark_process_status.c @@ -6,17 +6,18 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:50:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:32:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -int process_mark_status(pid_t pid, int status) +int mark_process_status(pid_t pid, int status) { t_list *plist; t_process *p; + DG("PMS pid=%i,s=%i", pid, status); if (pid > 1) { if ((plist = job_getprocess(pid))) diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index d62970e6..c13cf802 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/03/02 19:44:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:49:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,10 +48,10 @@ int process_redirect(t_process *p) } redirs = redirs->next; } - if (p->to_close != 0) + if (p->to_close != STDIN) close(p->to_close); if (p->fdin != STDIN) - dup2_close(p->fdout, STDOUT); + dup2_close(p->fdin, STDIN); if (p->fdout != STDOUT) dup2_close(p->fdout, STDOUT); return (0); diff --git a/42sh/src/job-control/add_new_job.c b/42sh/src/job-control/add_new_job.c index 2797edc2..daf402e6 100644 --- a/42sh/src/job-control/add_new_job.c +++ b/42sh/src/job-control/add_new_job.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/02 20:44:21 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:47:47 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:49:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ int add_new_job(t_job *job) { t_jobc *jobc; + DG("adding new job"); if (!job->first_process) return (1); jobc = &data_singleton()->jobc; @@ -23,10 +24,6 @@ int add_new_job(t_job *job) job->id = jobc->current_id; job->pgid = ((t_process*)job->first_process->content)->pid; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); - if (JOB_IS_FG(job->attrs)) - put_job_in_foreground(job, 0); - else - job_notify_new(job); - put_job_in_background(job, 0); + job = jobc->first_job->content; return (0); } diff --git a/42sh/src/job-control/job_update_id.c b/42sh/src/job-control/job_update_id.c index 1fba6f36..8b423e52 100644 --- a/42sh/src/job-control/job_update_id.c +++ b/42sh/src/job-control/job_update_id.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 13:33:08 by jhalford #+# #+# */ -/* Updated: 2017/03/02 20:59:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:18:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_update_status.c b/42sh/src/job-control/job_update_status.c index 1fa30cd5..6ab2df78 100644 --- a/42sh/src/job-control/job_update_status.c +++ b/42sh/src/job-control/job_update_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:56:11 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:50:30 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:37:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,6 @@ void job_update_status(void) pid_t pid; pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG); - while (!process_mark_status(pid, status)) + while (!mark_process_status(pid, status)) pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG); } diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c index 62f5ebef..eb5a1546 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 11:49:05 by jhalford #+# #+# */ -/* Updated: 2017/01/31 13:44:17 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:38:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,15 +17,23 @@ int job_wait(int id) pid_t pid; int status; - if (job_is_stopped(id)) - return (0); - job_update_status(); - pid = waitpid(WAIT_ANY, &status, WUNTRACED); - while (!process_mark_status(pid, status) - && !job_is_completed(id) - && !job_is_stopped(id)) - { + DG("job wait [%i]", id); + /* if (job_is_stopped(id)) */ + /* return (0); */ + /* job_update_status(); */ + /* DG("after update status"); */ + /* pid = waitpid(WAIT_ANY, &status, WUNTRACED); */ + /* while (!process_mark_status(pid, status) */ + /* && !job_is_completed(id) */ + /* && !job_is_stopped(id)) */ + /* { */ + + /* pid = waitpid(WAIT_ANY, &status, WUNTRACED); */ + /* } */ + do pid = waitpid(WAIT_ANY, &status, WUNTRACED); - } + while (!mark_process_status(pid, status) + && !job_is_stopped(id) + && !job_is_completed(id)); return (0); } diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 3f783821..fefd9cdd 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:58:36 by jhalford #+# #+# */ -/* Updated: 2017/03/02 20:59:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:38:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,9 @@ int put_job_in_foreground(t_job *j, int cont) if (kill(-j->pgid, SIGCONT) < 0) DG("kill(SIGCONT) failed"); } + DG("before wait"); job_wait(j->id); + DG("after wait"); job_remove(j->id); tcsetpgrp(STDIN, jobc->shell_pgid); diff --git a/42sh/src/parser/read_stack.c b/42sh/src/parser/read_stack.c index 38e51155..1f79a713 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 15:32:10 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:28:12 by ariard ### ########.fr */ +/* Updated: 2017/03/03 18:09:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -194,6 +194,8 @@ char *read_state(t_sym current) return ("ALL"); if (current == NEWLINE_LIST) return ("NEWLINE_LIST"); + if (current == CMD) + return ("CMD"); if (current != 0) return ("NON-DEFINED"); if (current == 0) From 8b88d41ed9556819936ffdd4480f348b8a7c68d4 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Mar 2017 19:05:42 +0100 Subject: [PATCH 11/13] trying to make files work --- 42sh/sample/ls | 1 + 42sh/src/exec/exec_command.c | 16 +++++---- 42sh/src/exec/ft_exec.c | 2 +- 42sh/src/exec/mark_process_status.c | 4 +-- 42sh/src/job-control/add_new_job.c | 3 +- 42sh/src/job-control/job_getprocess.c | 2 +- 42sh/src/job-control/job_remove.c | 2 +- 42sh/src/job-control/job_update_status.c | 8 ++--- 42sh/src/job-control/job_wait.c | 14 +------- 42sh/src/job-control/put_job_in_foreground.c | 37 ++++++++++++-------- 42sh/src/main/data_init.c | 2 +- 42sh/src/main/main.c | 11 +++--- 12 files changed, 51 insertions(+), 51 deletions(-) create mode 100644 42sh/sample/ls diff --git a/42sh/sample/ls b/42sh/sample/ls new file mode 100644 index 00000000..9e2740c6 --- /dev/null +++ b/42sh/sample/ls @@ -0,0 +1 @@ +ls diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 32162a94..b63752a6 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/03/03 18:50:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:02:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,13 +76,15 @@ int exec_cmd(t_btree **ast) if (fds[PIPE_WRITE] != STDOUT) close(fds[PIPE_WRITE]); if (IS_PIPEEND(p)) - add_new_job(job); - if (JOB_IS_FG(job->attrs)) - put_job_in_foreground(job, 0); - else { - job_notify_new(job); - put_job_in_background(job, 0); + add_new_job(job); + if (JOB_IS_FG(job->attrs)) + put_job_in_foreground(job, 0); + else + { + job_notify_new(job); + put_job_in_background(job, 0); + } } return (0); } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 1c46fea9..b4b3c578 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/03/03 18:26:31 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:03:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/mark_process_status.c b/42sh/src/exec/mark_process_status.c index 2665128d..32679242 100644 --- a/42sh/src/exec/mark_process_status.c +++ b/42sh/src/exec/mark_process_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:32:23 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:02:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,9 +17,9 @@ int mark_process_status(pid_t pid, int status) t_list *plist; t_process *p; - DG("PMS pid=%i,s=%i", pid, status); if (pid > 1) { + DG("MPS pid=%i,s=%i", pid, status); if ((plist = job_getprocess(pid))) { p = plist->content; diff --git a/42sh/src/job-control/add_new_job.c b/42sh/src/job-control/add_new_job.c index 4343cb0d..3e345d1d 100644 --- a/42sh/src/job-control/add_new_job.c +++ b/42sh/src/job-control/add_new_job.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/02 20:44:21 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:51:22 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:54:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,5 @@ int add_new_job(t_job *job) job->id = jobc->current_id; job->pgid = ((t_process*)job->first_process->content)->pid; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); - job = jobc->first_job->content; return (0); } diff --git a/42sh/src/job-control/job_getprocess.c b/42sh/src/job-control/job_getprocess.c index a5d421c3..52bb8229 100644 --- a/42sh/src/job-control/job_getprocess.c +++ b/42sh/src/job-control/job_getprocess.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/31 15:07:30 by jhalford #+# #+# */ -/* Updated: 2017/01/31 15:07:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:03:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_remove.c b/42sh/src/job-control/job_remove.c index 9c94db11..2ffe9c8a 100644 --- a/42sh/src/job-control/job_remove.c +++ b/42sh/src/job-control/job_remove.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:51:08 by jhalford #+# #+# */ -/* Updated: 2017/01/31 13:44:26 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:53:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_update_status.c b/42sh/src/job-control/job_update_status.c index 6ab2df78..5e314792 100644 --- a/42sh/src/job-control/job_update_status.c +++ b/42sh/src/job-control/job_update_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:56:11 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:37:10 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:56:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ void job_update_status(void) int status; pid_t pid; - pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG); - while (!mark_process_status(pid, status)) - pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG); + do + pid = waitpid (WAIT_ANY, &status, WUNTRACED|WNOHANG); + while (!mark_process_status (pid, status)); } diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c index eb5a1546..6a1c380a 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 11:49:05 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:38:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:54:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,18 +18,6 @@ int job_wait(int id) int status; DG("job wait [%i]", id); - /* if (job_is_stopped(id)) */ - /* return (0); */ - /* job_update_status(); */ - /* DG("after update status"); */ - /* pid = waitpid(WAIT_ANY, &status, WUNTRACED); */ - /* while (!process_mark_status(pid, status) */ - /* && !job_is_completed(id) */ - /* && !job_is_stopped(id)) */ - /* { */ - - /* pid = waitpid(WAIT_ANY, &status, WUNTRACED); */ - /* } */ do pid = waitpid(WAIT_ANY, &status, WUNTRACED); while (!mark_process_status(pid, status) diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 50b18485..71411673 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:58:36 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:52:03 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:57:58 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,21 +16,28 @@ int put_job_in_foreground(t_job *j, int cont) { t_jobc *jobc; - jobc = &data_singleton()->jobc; - tcsetpgrp(STDIN, j->pgid); - tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); - if (cont) + if (SH_HAS_JOBC(data_singleton()->opts)) { - tcsetattr(STDIN, TCSADRAIN, &j->tmodes); - if (kill(-j->pgid, SIGCONT) < 0) - DG("kill(SIGCONT) failed"); + jobc = &data_singleton()->jobc; + + tcsetpgrp(STDIN, j->pgid); + tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); + if (cont) + { + tcsetattr(STDIN, TCSADRAIN, &j->tmodes); + if (kill(-j->pgid, SIGCONT) < 0) + DG("kill(SIGCONT) failed"); + } + job_wait(j->id); + job_remove(j->id); + + tcsetpgrp(STDIN, jobc->shell_pgid); + + tcgetattr(STDIN, &j->tmodes); + tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); + tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); } - job_wait(j->id); - job_remove(j->id); - - tcsetpgrp(STDIN, jobc->shell_pgid); - - tcgetattr(STDIN, &j->tmodes); - tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); + else + job_wait(j->id); return (0); } diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 1aa6c777..591cc8c0 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/03/03 17:39:30 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:55:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 9f6b966d..f79cb83a 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/03/03 18:33:40 by wescande ### ########.fr */ +/* Updated: 2017/03/03 19:05:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -97,7 +97,10 @@ int get_input_fd() return (fd); } else if ((file = shell_get_avdata())) - return (open(file, O_RDONLY)); + { + if (fd = open(file, O_RDONLY)) + return (fd); + } else return (STDIN); } @@ -108,13 +111,13 @@ int main(int ac, char **av) setlocale(LC_ALL, ""); shell_init(ac, av); - DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s", - SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF"); if ((fd = get_input_fd()) < 0) { ft_printf("{red}%s: No such file or directory\n{eoc}", SHELL_NAME); return (1); } + DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s, fd=[%i]", + SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd); while (handle_instruction(fd) == 0) { // lexer_clean; From f62ef4d9c0f147f2fc917af1b57424ba6e7257e2 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Mar 2017 19:47:06 +0100 Subject: [PATCH 12/13] reading from files now works --- 42sh/src/exec/exec_ampersand.c | 5 +++-- 42sh/src/exec/exec_command.c | 5 +---- 42sh/src/job-control/add_new_job.c | 2 +- 42sh/src/job-control/job_getprocess.c | 2 +- 42sh/src/job-control/job_wait.c | 2 +- 42sh/src/job-control/put_job_in_foreground.c | 10 +++++----- 42sh/src/main/data_init.c | 4 ++-- 42sh/src/main/main.c | 7 ++++--- 42sh/src/main/shell_get_opts.c | 5 ++++- 42sh/src/main/shell_init.c | 2 +- 10 files changed, 23 insertions(+), 21 deletions(-) diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c index d32ad177..4480fc2e 100644 --- a/42sh/src/exec/exec_ampersand.c +++ b/42sh/src/exec/exec_ampersand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:05:30 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:35:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,13 @@ int exec_ampersand(t_btree **ast) { t_exec *exec; + if (!SH_HAS_JOBC(data_singleton()->opts)) + return (exec_semi(ast)); exec = &data_singleton()->exec; push(&exec->op_stack, TK_AMP); ft_exec(&(*ast)->left); exec->attrs &= ~EXEC_BG; ft_exec(&(*ast)->right); - /* if (SH_HAS_JOBC(data_singleton()->opts)) */ /* data_singleton()->exec.job.attributes |= JOB_BG; */ /* ft_exec(&(*ast)->left); */ diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index b63752a6..b4acaccf 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/03/03 19:02:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:46:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,10 +54,7 @@ int exec_cmd(t_btree **ast) if (op == TK_AMP) exec->attrs |= JOB_BG; else if (op == TK_PIPE) - { pipe(fds); - DG("%i -> PIPE -> %i", fds[PIPE_WRITE], fds[PIPE_READ]); - } p.fdin = exec->fdin; p.to_close = fds[PIPE_READ]; p.fdout = fds[PIPE_WRITE]; diff --git a/42sh/src/job-control/add_new_job.c b/42sh/src/job-control/add_new_job.c index 3e345d1d..3e866fac 100644 --- a/42sh/src/job-control/add_new_job.c +++ b/42sh/src/job-control/add_new_job.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/02 20:44:21 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:54:10 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:33:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_getprocess.c b/42sh/src/job-control/job_getprocess.c index 52bb8229..3ad82217 100644 --- a/42sh/src/job-control/job_getprocess.c +++ b/42sh/src/job-control/job_getprocess.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/31 15:07:30 by jhalford #+# #+# */ -/* Updated: 2017/03/03 19:03:01 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c index 6a1c380a..38f18f8f 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 11:49:05 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:54:18 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:42:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 71411673..c5b75d0b 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:58:36 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:57:58 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:46:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,10 @@ int put_job_in_foreground(t_job *j, int cont) { t_jobc *jobc; + jobc = &data_singleton()->jobc; if (SH_HAS_JOBC(data_singleton()->opts)) { - jobc = &data_singleton()->jobc; - tcsetpgrp(STDIN, j->pgid); - tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); if (cont) { tcsetattr(STDIN, TCSADRAIN, &j->tmodes); @@ -35,9 +33,11 @@ int put_job_in_foreground(t_job *j, int cont) tcgetattr(STDIN, &j->tmodes); tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); - tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); } else + { job_wait(j->id); + job_remove(j->id); + } return (0); } diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 591cc8c0..78d413f7 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/03/03 18:55:05 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:45:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ int data_init(void) data = data_singleton(); data->env = ft_sstrdup(environ); data->comp = NULL; - data->opts = SH_OPTS_JOBC; + data->opts = 0; /* data->exec.process.path = NULL; */ /* data->exec.process.av = NULL; */ /* data->exec.process.to_close = 0; */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index f79cb83a..9bbf2a54 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/03/03 19:05:22 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:37:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -98,7 +98,8 @@ int get_input_fd() } else if ((file = shell_get_avdata())) { - if (fd = open(file, O_RDONLY)) + if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0) + return (-1); return (fd); } else @@ -113,7 +114,7 @@ int main(int ac, char **av) shell_init(ac, av); if ((fd = get_input_fd()) < 0) { - ft_printf("{red}%s: No such file or directory\n{eoc}", SHELL_NAME); + ft_printf("{red}%s: %s: No such file or directory\n{eoc}", SHELL_NAME, shell_get_avdata()); return (1); } DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s, fd=[%i]", diff --git a/42sh/src/main/shell_get_opts.c b/42sh/src/main/shell_get_opts.c index 215f2145..b0789b58 100644 --- a/42sh/src/main/shell_get_opts.c +++ b/42sh/src/main/shell_get_opts.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 14:04:48 by jhalford #+# #+# */ -/* Updated: 2017/02/21 15:37:22 by ariard ### ########.fr */ +/* Updated: 2017/03/03 19:45:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,10 @@ void shell_get_opts(int ac, char **av) i = 1; if (isatty(STDIN) && !av[1]) + { data_singleton()->opts |= SH_INTERACTIVE; + data_singleton()->opts |= SH_OPTS_JOBC; + } while (i < ac && av[i][0] == '-') { if (ft_strcmp(av[i], "--") == 0) diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 7ad9da23..272440d7 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ -/* Updated: 2017/03/02 20:38:23 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:45:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From b1b0cbdd92ddcf26726f76669572c10fe2892494 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Mar 2017 19:53:59 +0100 Subject: [PATCH 13/13] builtins segfault patch --- 42sh/src/exec/launch_process.c | 3 ++- 42sh/src/main/shell_get_opts.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 085ce337..52919e69 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/03/03 18:49:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:53:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ int launch_process(t_process *p) if (process_redirect(p)) return (1); set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env), 1); + return (0); } else { diff --git a/42sh/src/main/shell_get_opts.c b/42sh/src/main/shell_get_opts.c index b0789b58..98742c4b 100644 --- a/42sh/src/main/shell_get_opts.c +++ b/42sh/src/main/shell_get_opts.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 14:04:48 by jhalford #+# #+# */ -/* Updated: 2017/03/03 19:45:39 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:53:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */