From 271748861ee12c3770ae68b753fc0fff6f6ac517 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 13 Mar 2017 23:58:17 +0100 Subject: [PATCH 1/3] brace groups and subshell launching re-design --- 42sh/includes/exec.h | 17 +++++------- 42sh/src/builtin/is_builtin.c | 2 +- 42sh/src/exec/exec_leaf.c | 4 +-- 42sh/src/exec/exec_math.c | 2 +- 42sh/src/exec/plaunch_brace.c | 34 ++++++++++++++++++++--- 42sh/src/exec/plaunch_builtin.c | 7 ++--- 42sh/src/exec/plaunch_file.c | 4 +-- 42sh/src/exec/plaunch_subshell.c | 45 ++++++++++--------------------- 42sh/src/exec/process_launch.c | 5 ++-- 42sh/src/exec/process_set.c | 2 +- 42sh/src/exec/process_setgroup.c | 2 +- 42sh/src/exec/pset_cmd.c | 2 +- 42sh/src/exec/set_exitstatus.c | 2 +- 42sh/src/glob/command_getoutput.c | 3 +-- 42sh/src/main/main.c | 4 +-- 15 files changed, 70 insertions(+), 65 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 23457a32..b6564b2b 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/13 22:36:52 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 23:02:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,7 +66,7 @@ struct s_data_list t_btree *content; }; -struct s_data_subshell +struct s_data_tree { t_btree *content; }; @@ -74,9 +74,9 @@ struct s_data_subshell union u_process_data { struct s_data_cmd cmd; - struct s_data_subshell subshell; - struct s_data_subshell brace; - struct s_data_subshell function; + struct s_data_tree subshell; + struct s_data_tree brace; + struct s_data_tree function; struct s_data_cond d_while; struct s_data_cond d_until; struct s_data_cond d_if; @@ -133,9 +133,6 @@ struct s_exec int control_count; }; -/* extern t_itof g_redirmap[]; */ -/* extern t_itof g_execmap[]; */ - int exec_reset(void); int process_setgroup(t_process *p, pid_t pid); void process_setsig(void); @@ -164,10 +161,10 @@ void redir_free(void *data, size_t content_size); char **token_to_argv(t_ld *ld, int do_match); -int add_new_job(t_job *job); +/* int add_new_job(t_job *job); */ int error_badidentifier(char *name); -t_btree *is_function(t_process *p); +t_btree *is_function(t_process *p); /* ** Mapping pour free les process diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index c48252ed..9cdb3d4b 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/03/07 19:42:54 by ariard ### ########.fr */ +/* Updated: 2017/03/13 22:52:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_leaf.c b/42sh/src/exec/exec_leaf.c index d0ca6526..16b4dd82 100644 --- a/42sh/src/exec/exec_leaf.c +++ b/42sh/src/exec/exec_leaf.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:47:30 by wescande #+# #+# */ -/* Updated: 2017/03/13 22:37:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 23:56:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,11 +35,9 @@ int exec_leaf(t_btree **ast) job = &data_singleton()->exec.job; if (process_set(&p, *ast)) return (1); - DG("p.type=%i", p.type); p.map = g_process_map[p.type]; if (!(process_launch(&p))) { - DG("check"); DG("forked pid=[%i]", p.pid); job_addprocess(&p); /* DG("[IS_BG->%i]", JOB_IS_BG(job->attrs)); */ diff --git a/42sh/src/exec/exec_math.c b/42sh/src/exec/exec_math.c index 7f9c994a..cec05f80 100644 --- a/42sh/src/exec/exec_math.c +++ b/42sh/src/exec/exec_math.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 10:58:49 by ariard #+# #+# */ -/* Updated: 2017/03/07 16:00:24 by wescande ### ########.fr */ +/* Updated: 2017/03/13 23:22:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/plaunch_brace.c b/42sh/src/exec/plaunch_brace.c index 7b67d92a..7b052fb4 100644 --- a/42sh/src/exec/plaunch_brace.c +++ b/42sh/src/exec/plaunch_brace.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 19:09:30 by jhalford #+# #+# */ -/* Updated: 2017/03/13 20:28:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 23:57:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,37 @@ int plaunch_brace(t_process *p) { - if (process_redirect(p)) + pid_t pid; + + DG("plaunch_brace"); + if (IS_PIPESINGLE(*p)) { - set_exitstatus(1, 1); + if (process_redirect(p)) + { + DG("BRACE_GROUP redirection failed"); + set_exitstatus(1, 1); + return (0); + } + ft_exec(&p->data.brace.content); return (0); } - ft_exec(&p->data.subshell.content); + pid = fork(); + if (pid == 0) + { + DG("fork!"); + data_singleton()->opts &= ~SH_INTERACTIVE; + /* data_singleton()->opts &= ~SH_OPTS_JOBC; */ + if (process_redirect(p)) + exit (1); + process_setgroup(p, 0); + process_setsig(); + exec_reset(); + ft_exec(&p->data.brace.content); + exit(ft_atoi(ft_getenv(data_singleton()->env, "?"))); + } + else if (pid > 0) + return (pid); + else if (pid == -1) + ft_dprintf(2, "{red}%s: internal fork error{eoc}\n", SHELL_NAME); return (0); } diff --git a/42sh/src/exec/plaunch_builtin.c b/42sh/src/exec/plaunch_builtin.c index f55875f4..88c60800 100644 --- a/42sh/src/exec/plaunch_builtin.c +++ b/42sh/src/exec/plaunch_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:48:24 by jhalford #+# #+# */ -/* Updated: 2017/03/13 20:28:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 23:35:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,11 +29,12 @@ int plaunch_builtin(t_process *p) pid = fork(); if (pid == 0) { - process_setgroup(p, 0); - process_setsig(); if (process_redirect(p)) exit (1); + process_setgroup(p, 0); + process_setsig(); exec_reset();///A FAIRE POUR LES BUILTIN OU PAS ? -> Q de William + // je pense ca ne change rien si on l'enleve car on excve apres exit((*p->data.cmd.execf)(p->data.cmd.path, p->data.cmd.av, data_singleton()->env)); } else if (pid > 0) diff --git a/42sh/src/exec/plaunch_file.c b/42sh/src/exec/plaunch_file.c index cb03c043..0634c483 100644 --- a/42sh/src/exec/plaunch_file.c +++ b/42sh/src/exec/plaunch_file.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ -/* Updated: 2017/03/13 22:39:43 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 23:15:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ int plaunch_file(t_process *p) { /* data_singleton()->opts &= ~SH_INTERACTIVE; */ /* data_singleton()->opts &= ~SH_OPTS_JOBC; */ - /* DG("fork! [%s]", p->data.cmd.av[0]); */ + DG("fork! [%s]", p->data.cmd.av[0]); if (process_redirect(p)) exit (1); process_setgroup(p, 0); diff --git a/42sh/src/exec/plaunch_subshell.c b/42sh/src/exec/plaunch_subshell.c index d24f6ff7..584015f0 100644 --- a/42sh/src/exec/plaunch_subshell.c +++ b/42sh/src/exec/plaunch_subshell.c @@ -6,47 +6,30 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 00:11:44 by wescande #+# #+# */ -/* Updated: 2017/03/13 20:28:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 23:57:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static int do_subshell(t_process *p) -{ - int ret; - - ft_exec(&p->data.subshell.content); - ret = ft_atoi(ft_getenv(data_singleton()->env, "?")); - DG("CHECK, ret=[%i]", ret); - /* p = ft_lstlast(j->first_process)->content; */ - /* ret = p->status */ - /* DG("CHECK, ret=[%i]", ret); */ - return (ret); -} - int plaunch_subshell(t_process *p) { pid_t pid; - if (SH_IS_INTERACTIVE(data_singleton()->opts)) + pid = fork(); + if (pid == 0) { - pid = fork(); - if (pid == 0) - { - data_singleton()->opts &= ~SH_INTERACTIVE; - data_singleton()->opts &= ~SH_OPTS_JOBC; - if (process_redirect(p)) - exit (1); - process_setgroup(p, 0); - process_setsig(); - exec_reset(); - exit(do_subshell(p)); - } - else if (pid > 0) - return (pid); + data_singleton()->opts &= ~SH_INTERACTIVE; + data_singleton()->opts &= ~SH_OPTS_JOBC; + if (process_redirect(p)) + exit (1); + process_setgroup(p, 0); + process_setsig(); + exec_reset(); + ft_exec(&p->data.subshell.content); + exit(ft_atoi(ft_getenv(data_singleton()->env, "?"))); } - else - do_subshell(p); + else if (pid > 0) + return (pid); return (0); } diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index 93ff72aa..1b30ca03 100644 --- a/42sh/src/exec/process_launch.c +++ b/42sh/src/exec/process_launch.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ -/* Updated: 2017/03/13 22:35:24 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 23:10:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,10 +40,11 @@ int process_launch(t_process *p) p->attrs |= PROCESS_RUNNING; if (!(pid = (*p->map.launch)(p))) { + DG("launcher did not fork!"); process_resetfds(p); return (1); } - DG("launcher did not fork!"); + DG("launcher forked!"); p->pid = pid; process_setgroup(p, pid); if (p->fdin != STDIN) diff --git a/42sh/src/exec/process_set.c b/42sh/src/exec/process_set.c index a5b55dd8..f648a7d6 100644 --- a/42sh/src/exec/process_set.c +++ b/42sh/src/exec/process_set.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 14:54:45 by jhalford #+# #+# */ -/* Updated: 2017/03/13 22:39:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 22:40:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index 21577578..059e4f4a 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/13 13:37:03 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 23:33:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/pset_cmd.c b/42sh/src/exec/pset_cmd.c index 12f468a2..3aafb8b1 100644 --- a/42sh/src/exec/pset_cmd.c +++ b/42sh/src/exec/pset_cmd.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:06:05 by wescande #+# #+# */ -/* Updated: 2017/03/13 20:31:21 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 22:52:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/set_exitstatus.c b/42sh/src/exec/set_exitstatus.c index add7281f..5f5941bc 100644 --- a/42sh/src/exec/set_exitstatus.c +++ b/42sh/src/exec/set_exitstatus.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2017/03/11 18:14:33 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 23:33:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/glob/command_getoutput.c b/42sh/src/glob/command_getoutput.c index 96accab5..2588d318 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/03/13 22:25:13 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 23:32:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,6 @@ } */ - static t_btree *gen_t_btree(const char *command) { t_btree *ast; diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 536603c7..decb57a0 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/13 19:55:20 by jhalford ### ########.fr */ +/* Updated: 2017/03/13 23:08:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,7 +62,7 @@ int handle_instruction(int fd) if (parser.state == ERROR) error_syntax(&token, &parser, &ast); lexer.state = data_singleton()->heredoc_queue ? HEREDOC : 0; - DG("lexer.state=%i", lexer.state); + /* DG("lexer.state=%i", lexer.state); */ if (lexer.state) continue; else if (parser.state == SUCCESS) From 65ef17b62c545884fd1334c04ca24f53a09c92f2 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 14 Mar 2017 00:03:15 +0100 Subject: [PATCH 2/3] further subshell/brace launch re-design --- 42sh/src/exec/plaunch_brace.c | 33 +++++--------------------------- 42sh/src/exec/plaunch_subshell.c | 4 +++- 2 files changed, 8 insertions(+), 29 deletions(-) diff --git a/42sh/src/exec/plaunch_brace.c b/42sh/src/exec/plaunch_brace.c index 66904681..d436c697 100644 --- a/42sh/src/exec/plaunch_brace.c +++ b/42sh/src/exec/plaunch_brace.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 19:09:30 by jhalford #+# #+# */ -/* Updated: 2017/03/13 23:59:04 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 00:03:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,37 +14,14 @@ int plaunch_brace(t_process *p) { - pid_t pid; - - DG("plaunch_brace"); if (IS_PIPESINGLE(*p)) { if (process_redirect(p)) - { - DG("BRACE_GROUP redirection failed"); set_exitstatus(1, 1); - return (0); - } - ft_exec(&p->data.brace.content); + else + ft_exec(&p->data.brace.content); return (0); } - pid = fork(); - if (pid == 0) - { - DG("fork!"); - data_singleton()->opts &= ~SH_INTERACTIVE; - data_singleton()->opts &= ~SH_OPTS_JOBC; - if (process_redirect(p)) - exit (1); - process_setgroup(p, 0); - process_setsig(); - exec_reset(); - ft_exec(&p->data.brace.content); - exit(ft_atoi(ft_getenv(data_singleton()->env, "?"))); - } - else if (pid > 0) - return (pid); - else if (pid == -1) - ft_dprintf(2, "{red}%s: internal fork error{eoc}\n", SHELL_NAME); - return (0); + else + return (plaunch_subshell(p)) } diff --git a/42sh/src/exec/plaunch_subshell.c b/42sh/src/exec/plaunch_subshell.c index 9925dc51..de2b3f2f 100644 --- a/42sh/src/exec/plaunch_subshell.c +++ b/42sh/src/exec/plaunch_subshell.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 00:11:44 by wescande #+# #+# */ -/* Updated: 2017/03/13 23:59:10 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 00:00:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,5 +31,7 @@ int plaunch_subshell(t_process *p) } else if (pid > 0) return (pid); + else if (pid == -1) + ft_dprintf(2, "{red}%s: internal fork error{eoc}\n", SHELL_NAME); return (0); } From 7216b0185f7a9986b7157017b4c32f3e3da0977e Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 14 Mar 2017 00:16:51 +0100 Subject: [PATCH 3/3] compile error --- 42sh/src/exec/plaunch_brace.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/42sh/src/exec/plaunch_brace.c b/42sh/src/exec/plaunch_brace.c index d436c697..728842e0 100644 --- a/42sh/src/exec/plaunch_brace.c +++ b/42sh/src/exec/plaunch_brace.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 19:09:30 by jhalford #+# #+# */ -/* Updated: 2017/03/14 00:03:01 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 00:04:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,5 +23,5 @@ int plaunch_brace(t_process *p) return (0); } else - return (plaunch_subshell(p)) + return (plaunch_subshell(p)); }