From 26222441cc88462f82257ef6334115cdf446276e Mon Sep 17 00:00:00 2001 From: wescande Date: Tue, 7 Mar 2017 22:45:48 +0100 Subject: [PATCH] ajout execution des while, if, for, case, until --- 42sh/Makefile | 2 ++ 42sh/includes/exec.h | 10 ++++++- 42sh/src/exec/exec_leaf.c | 4 ++- 42sh/src/exec/launch_file.c | 3 +- 42sh/src/exec/launch_process.c | 18 ++++++++++-- 42sh/src/exec/launch_until.c | 49 +++++++++++++++++++++++++++++++ 42sh/src/exec/process_setexec.c | 2 +- 42sh/src/exec/set_process_case.c | 3 +- 42sh/src/exec/set_process_cmd.c | 2 +- 42sh/src/exec/set_process_for.c | 3 +- 42sh/src/exec/set_process_if.c | 3 +- 42sh/src/exec/set_process_map.c | 27 +++++++++-------- 42sh/src/exec/set_process_until.c | 22 ++++++++++++++ 42sh/src/exec/set_process_while.c | 3 +- 14 files changed, 128 insertions(+), 23 deletions(-) create mode 100644 42sh/src/exec/launch_until.c create mode 100644 42sh/src/exec/set_process_until.c diff --git a/42sh/Makefile b/42sh/Makefile index c7bc80e0..306a1bdc 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -86,6 +86,7 @@ exec/launch_file.c\ exec/launch_for.c\ exec/launch_if.c\ exec/launch_process.c\ +exec/launch_until.c\ exec/launch_while.c\ exec/mark_process_status.c\ exec/process_redirect.c\ @@ -107,6 +108,7 @@ exec/set_process_cmd.c\ exec/set_process_for.c\ exec/set_process_if.c\ exec/set_process_map.c\ +exec/set_process_until.c\ exec/set_process_while.c\ exec/token_to_argv.c\ glob/command_getoutput.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 9d3936ea..5cbc107c 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/07 21:08:54 by wescande ### ########.fr */ +/* Updated: 2017/03/07 22:23:06 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -83,9 +83,11 @@ enum e_process_type PROCESS_FILE, PROCESS_SUBSHELL, PROCESS_WHILE, + PROCESS_UNTIL, PROCESS_IF, PROCESS_FOR, PROCESS_CASE, + PROCESS_MAX }; typedef enum e_process_type t_process_type; @@ -184,6 +186,7 @@ int error_badidentifier(char *name); int launch_process(t_process *p); int launch_if(t_process *p); int launch_while(t_process *p); +int launch_until(t_process *p); int launch_for(t_process *p); int launch_case(t_process *p); int launch_file(t_process *p); @@ -195,4 +198,9 @@ int launch_builtin(t_process *p); int set_process(t_process *p, t_btree *ast); int set_process_map(t_process *p, t_btree *ast, t_cmd *cmd); int set_process_cmd(t_process *p, t_btree *ast, t_cmd *cmd); +int set_process_while(t_process *p, t_btree *ast, t_cmd *cmd); +int set_process_until(t_process *p, t_btree *ast, t_cmd *cmd); +int set_process_if(t_process *p, t_btree *ast, t_cmd *cmd); +int set_process_for(t_process *p, t_btree *ast, t_cmd *cmd); +int set_process_case(t_process *p, t_btree *ast, t_cmd *cmd); #endif diff --git a/42sh/src/exec/exec_leaf.c b/42sh/src/exec/exec_leaf.c index e237cb99..9a0ae4fd 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/07 17:34:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 21:30:29 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,9 +17,11 @@ int exec_leaf(t_btree **ast) t_process p; t_job *job; + DG("in exec leaf"); job = &data_singleton()->exec.job; if (set_process(&p, *ast)) return (1); + DG("set_process done"); if (!(launch_process(&p))) { job_addprocess(&p); diff --git a/42sh/src/exec/launch_file.c b/42sh/src/exec/launch_file.c index 80c990b3..afe5cb81 100644 --- a/42sh/src/exec/launch_file.c +++ b/42sh/src/exec/launch_file.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ -/* Updated: 2017/03/07 20:58:31 by wescande ### ########.fr */ +/* Updated: 2017/03/07 21:33:56 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ int launch_file(t_process *p) { int pid; + DG("in file"); pid = fork(); if (pid == 0) { diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index a677f76d..708a64d3 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/07 21:07:33 by wescande ### ########.fr */ +/* Updated: 2017/03/07 22:21:07 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ t_itof g_launchmap[] = {PROCESS_FILE, launch_file}, {PROCESS_SUBSHELL, NULL}, {PROCESS_WHILE, launch_while}, + {PROCESS_UNTIL, launch_until}, {PROCESS_IF, launch_if}, {PROCESS_FOR, launch_for}, {PROCESS_CASE, launch_case}, @@ -27,9 +28,21 @@ t_itof g_launchmap[] = int launch_process(t_process *p) { - int i; +// int i; int pid; + if (p->type >= PROCESS_MAX) + return (-1); + if (!g_launchmap[p->type].f) + return (-1); + p->attrs &= ~PROCESS_STATE_MASK; + p->attrs |= PROCESS_RUNNING; + if (!(pid = (*g_launchmap[p->type].f)(p))) + return (-1); + p->pid = pid; + process_setgroup(p, pid); + return (0); + /* i = 0; while (g_launchmap[i].id) { @@ -49,4 +62,5 @@ int launch_process(t_process *p) i++; } return (-1); + */ } diff --git a/42sh/src/exec/launch_until.c b/42sh/src/exec/launch_until.c new file mode 100644 index 00000000..66a23a36 --- /dev/null +++ b/42sh/src/exec/launch_until.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* launch_until.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 22:04:42 by wescande #+# #+# */ +/* Updated: 2017/03/07 22:06:50 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static int do_until(t_process *p) +{ + int ret; + + ft_exec(&p->data.d_while.condition); + ret = ft_atoi(ft_getenv(data_singleton()->env, "?")); + while (ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0")) + { + ft_exec(&p->data.d_while.content); + ret = ft_atoi(ft_getenv(data_singleton()->env, "?")); + ft_exec(&p->data.d_while.condition); + } + return (ret); +} + +int launch_until(t_process *p) +{ + pid_t pid; + + if (SH_IS_INTERACTIVE(data_singleton()->opts)) + { + pid = fork(); + if (pid == 0) + { + data_singleton()->opts &= ~SH_INTERACTIVE; + data_singleton()->opts &= ~SH_OPTS_JOBC; + exit(do_until(p)); + } + else if (pid > 0) + return (pid); + } + else + do_until(p); + return (0); +} diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index 2f4ba13a..4242fb47 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/03/07 16:54:13 by wescande ### ########.fr */ +/* Updated: 2017/03/07 21:44:37 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/set_process_case.c b/42sh/src/exec/set_process_case.c index d8dcaba8..b03cb62e 100644 --- a/42sh/src/exec/set_process_case.c +++ b/42sh/src/exec/set_process_case.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 20:36:04 by wescande #+# #+# */ -/* Updated: 2017/03/07 21:14:19 by wescande ### ########.fr */ +/* Updated: 2017/03/07 22:01:20 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,5 +17,6 @@ int set_process_case(t_process *p, t_btree *ast, t_cmd *cmd) (void)cmd; p->data.d_case.list_word = ft_ld_copy(((t_astnode *)ast->item)->data.cmd.wordlist, tab_esc_copy); p->data.d_case.content = ast_copy(ast->right); + p->type = PROCESS_CASE; return (0); } diff --git a/42sh/src/exec/set_process_cmd.c b/42sh/src/exec/set_process_cmd.c index e66d1645..4a7c6a1b 100644 --- a/42sh/src/exec/set_process_cmd.c +++ b/42sh/src/exec/set_process_cmd.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:06:05 by wescande #+# #+# */ -/* Updated: 2017/03/07 21:12:31 by wescande ### ########.fr */ +/* Updated: 2017/03/07 21:44:40 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/set_process_for.c b/42sh/src/exec/set_process_for.c index 34134a33..b6aa5f30 100644 --- a/42sh/src/exec/set_process_for.c +++ b/42sh/src/exec/set_process_for.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 19:38:05 by wescande #+# #+# */ -/* Updated: 2017/03/07 21:14:33 by wescande ### ########.fr */ +/* Updated: 2017/03/07 22:00:34 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,5 +17,6 @@ int set_process_for(t_process *p, t_btree *ast, t_cmd *cmd) (void)cmd; p->data.d_for.list_word = ft_ld_copy(((t_astnode *)ast->item)->data.cmd.wordlist, tab_esc_copy); p->data.d_for.content = ast_copy(ast->right); + p->type = PROCESS_FOR; return (0); } diff --git a/42sh/src/exec/set_process_if.c b/42sh/src/exec/set_process_if.c index 3f8d721a..6807bfd6 100644 --- a/42sh/src/exec/set_process_if.c +++ b/42sh/src/exec/set_process_if.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 19:19:50 by wescande #+# #+# */ -/* Updated: 2017/03/07 21:10:47 by wescande ### ########.fr */ +/* Updated: 2017/03/07 21:59:44 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,5 +17,6 @@ int set_process_if(t_process *p, t_btree *ast, t_cmd *cmd) (void)cmd; p->data.d_if.condition = ast_copy(ast->left); p->data.d_if.content = ast_copy(ast->right); + p->type = PROCESS_IF; return (0); } diff --git a/42sh/src/exec/set_process_map.c b/42sh/src/exec/set_process_map.c index 004bb01a..7aaa6fef 100644 --- a/42sh/src/exec/set_process_map.c +++ b/42sh/src/exec/set_process_map.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:08:12 by wescande #+# #+# */ -/* Updated: 2017/03/07 15:50:56 by wescande ### ########.fr */ +/* Updated: 2017/03/07 22:24:21 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,13 +20,13 @@ t_itof g_setprocessmap[] = {TK_AND_IF, NULL}, {TK_OR_IF,NULL}, {TK_PIPE, NULL}, - {TK_WHILE, NULL}, - {TK_IF, NULL}, + {TK_WHILE, &set_process_while}, + {TK_IF, &set_process_if}, {TK_ELIF, NULL}, {TK_ELSE, NULL}, - {TK_UNTIL, NULL}, - {TK_FOR, NULL}, - {TK_CASE, NULL}, + {TK_UNTIL, &set_process_until}, + {TK_FOR, &set_process_for}, + {TK_CASE, &set_process_case}, {TK_PAREN_OPEN, NULL}, {TK_ASSIGNEMENT_WORD, NULL}, {MATH, NULL}, @@ -38,15 +38,18 @@ t_itof g_setprocessmap[] = int set_process_map(t_process *p, t_btree *ast, t_cmd *cmd) { int i; + t_astnode *item; - i = 0; + i = -1; if (!ast) return (0); - while (g_setprocessmap[i].id) - { - if (p->type == g_setprocessmap[i].id) + item = ast->item; + while (g_setprocessmap[++i].id) + if (item->type == g_setprocessmap[i].id) + { + if (!g_setprocessmap[i].f) + return (0); return ((*g_setprocessmap[i].f)(p, ast, cmd)); - i++; - } + } return (0); } diff --git a/42sh/src/exec/set_process_until.c b/42sh/src/exec/set_process_until.c new file mode 100644 index 00000000..1238d828 --- /dev/null +++ b/42sh/src/exec/set_process_until.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_process_until.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 22:22:24 by wescande #+# #+# */ +/* Updated: 2017/03/07 22:22:36 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int set_process_until(t_process *p, t_btree *ast, t_cmd *cmd) +{ + (void)cmd; + p->data.d_while.condition = ast_copy(ast->left); + p->data.d_while.content = ast_copy(ast->right); + p->type = PROCESS_UNTIL; + return (0); +} diff --git a/42sh/src/exec/set_process_while.c b/42sh/src/exec/set_process_while.c index f0908887..7fd90ea1 100644 --- a/42sh/src/exec/set_process_while.c +++ b/42sh/src/exec/set_process_while.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 19:38:10 by wescande #+# #+# */ -/* Updated: 2017/03/07 21:11:38 by wescande ### ########.fr */ +/* Updated: 2017/03/07 21:57:55 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,5 +17,6 @@ int set_process_while(t_process *p, t_btree *ast, t_cmd *cmd) (void)cmd; p->data.d_while.condition = ast_copy(ast->left); p->data.d_while.content = ast_copy(ast->right); + p->type = PROCESS_WHILE; return (0); }