From 29aff9ebb740f60de15de10f2352e8baefd6a5a0 Mon Sep 17 00:00:00 2001 From: wescande Date: Tue, 7 Mar 2017 16:43:32 +0100 Subject: [PATCH] exec en cours --- 42sh/includes/exec.h | 30 ++++++++++++++--------- 42sh/libft | 2 +- 42sh/src/exec/exec_command.c | 29 +++------------------- 42sh/src/exec/exec_leaf.c | 38 +++++++++++++++++++++++++++++ 42sh/src/exec/exec_math.c | 4 +-- 42sh/src/exec/exec_while.c | 34 +++++++++++++++++++++++--- 42sh/src/exec/ft_exec.c | 22 ++++++++--------- 42sh/src/exec/launch_file.c | 18 ++++++++------ 42sh/src/exec/launch_process.c | 7 +++--- 42sh/src/exec/set_process_cmd.c | 2 +- 42sh/src/exec/set_process_map.c | 43 +++++++++++++++++---------------- 42sh/src/exec/token_to_argv.c | 38 +++++++++++++++++++++++++++++ 12 files changed, 180 insertions(+), 87 deletions(-) create mode 100644 42sh/src/exec/exec_leaf.c create mode 100644 42sh/src/exec/token_to_argv.c diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 849a1654..0ddfc138 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 15:17:21 by wescande ### ########.fr */ +/* Updated: 2017/03/07 16:21:44 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,7 @@ struct s_data_cmd char **av; char *path; t_execf *execf; + struct stat stat; }; struct s_data_cond @@ -134,18 +135,19 @@ 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_cmd(t_btree **ast); +//int exec_cmd(t_btree **ast); +int exec_leaf(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); -int exec_until(t_btree **ast); -int exec_default(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); +//int exec_until(t_btree **ast); +//int exec_default(t_btree **ast); int exec_var(t_btree **ast); -int exec_for(t_btree **ast); -int exec_case(t_btree **ast); -int exec_case_branch(t_btree **ast); +//int exec_for(t_btree **ast); +//int exec_case(t_btree **ast); +//int exec_case_branch(t_btree **ast); int exec_math(t_btree **ast); int launch_process(t_process *p); @@ -180,4 +182,10 @@ int add_new_job(t_job *job); int error_badidentifier(char *name); +/* +** Mapping pour set les process +*/ +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); #endif diff --git a/42sh/libft b/42sh/libft index bc489f86..4a36b1a5 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit bc489f8664fdc24317c31b3069811f54b1178643 +Subproject commit 4a36b1a5e650ada2f66034d13312367694a6481a diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 1054cc05..b35e7048 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,35 +6,12 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/03/07 15:17:47 by wescande ### ########.fr */ +/* Updated: 2017/03/07 16:22:44 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" - -char **token_to_argv(t_ld *ld, int do_match) -{ - char **my_tab; - int index; - char **expand; - char **content; - - my_tab = NULL; - while (ld) - { - content = ld->content; - if ((expand = glob(content[0], (unsigned char *)content[1], (unsigned char *)content[2], do_match))) - { - index = -1; - while (expand[++index]) - my_tab = ft_sstradd(my_tab, expand[index]); - ft_tabdel(&expand); - } - ld = ld->next; - } - return (my_tab); -} - +/* int exec_cmd(t_btree **ast) { t_process p; @@ -58,4 +35,4 @@ int exec_cmd(t_btree **ast) if (p.fdout != STDOUT) close(p.fdout); return (0); -} +}*/ diff --git a/42sh/src/exec/exec_leaf.c b/42sh/src/exec/exec_leaf.c new file mode 100644 index 00000000..88a36bcc --- /dev/null +++ b/42sh/src/exec/exec_leaf.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_leaf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 15:47:30 by wescande #+# #+# */ +/* Updated: 2017/03/07 15:50:04 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int exec_leaf(t_btree **ast) +{ + t_process p; + t_job *job; + + job = &data_singleton()->exec.job; + if (set_process(&p, *ast)) + return (1); + if (!(launch_process(&p))) + { + job_addprocess(&p); + if (IS_PIPEEND(p)) + { + if (JOB_IS_FG(job->attrs)) + put_job_in_foreground(job, 0); + else + put_job_in_background(job, 0); + } + job->pgid = 0; + } + if (p.fdout != STDOUT) + close(p.fdout); + return (0); +} diff --git a/42sh/src/exec/exec_math.c b/42sh/src/exec/exec_math.c index e157ef4c..0bbcc8b2 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 13:42:11 by ariard ### ########.fr */ +/* Updated: 2017/03/07 16:00:24 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ static int do_math(char **value, char *operator) ope2 = ft_atoi(&operator[2]); else ope2 = 0; - if (operator[0] == '/' && ope2 == 0) + if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0) ope1 = 0; else { diff --git a/42sh/src/exec/exec_while.c b/42sh/src/exec/exec_while.c index a2cf6932..59699f38 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/03/07 14:33:10 by wescande ### ########.fr */ +/* Updated: 2017/03/07 16:41:50 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,16 +14,44 @@ int exec_while(t_btree **ast) { + int ret; DG("exec while condition"); ft_exec(&(*ast)->left); DG("ret :[%s]", ft_getenv(data_singleton()->env, "?")); - ++data_singleton()->exec.control_count; while (!(ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0"))) { DG("in the while"); ft_exec(&(*ast)->right); + ret = ft_atoi(ft_getenv(data_singleton()->env, "?")); ft_exec(&(*ast)->left); } - --data_singleton()->exec.control_count; + exit(ret); return (0); } + +static int do_while(t_process *p) +{ + int ret; + return (ret); +} + +int launch_while(t_process *p) +{ + pid_t pid; + + if (SH_IS_INTERACTIVE(data_singleton()->opts)) + { + pid = fork(); + if (pid == 0) + exit(do_while(p)); + else if (pid > 0) + { + p->pid = pid; + process_setgroup(p, pid); + return (0); + } + } + else + do_while(p); + return (1); +} diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 00043e4b..07b28a64 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/07 15:18:27 by wescande ### ########.fr */ +/* Updated: 2017/03/07 16:20:18 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,18 +20,18 @@ t_itof g_execmap[] = {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_FOR, &exec_for}, - {TK_CASE, &exec_case}, + {TK_WHILE, &exec_leaf}, + {TK_IF, &exec_leaf}, + {TK_ELIF, &exec_leaf}, + {TK_ELSE, &exec_leaf}, + {TK_UNTIL, &exec_leaf}, + {TK_FOR, &exec_leaf}, + {TK_CASE, &exec_leaf}, {TK_PAREN_OPEN, &exec_case_branch}, {TK_ASSIGNEMENT_WORD, &exec_var}, {MATH, &exec_math}, /* {TK_SUBSHELL, &exec_}, */ - {CMD, &exec_cmd}, + {CMD, &exec_leaf}, {0, 0}, }; @@ -44,9 +44,9 @@ int ft_exec(t_btree **ast) if (!*ast) return (0); item = (*ast)->item; - while (g_execmap[i].type) + while (g_execmap[i].id) { - if (item->type == g_execmap[i].type) + if (item->type == g_execmap[i].id) return ((*g_execmap[i].f)(ast)); i++; } diff --git a/42sh/src/exec/launch_file.c b/42sh/src/exec/launch_file.c index c482d419..3d07abf7 100644 --- a/42sh/src/exec/launch_file.c +++ b/42sh/src/exec/launch_file.c @@ -6,10 +6,12 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ -/* Updated: 2017/03/07 14:54:18 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 15:53:45 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ +#include "exec.h" + int launch_file(t_process *p) { int pid; @@ -18,15 +20,15 @@ int launch_file(t_process *p) { if (process_redirect(p)) return (1); - set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env), 1); + set_exitstatus((*p->data.cmd.execf)(p->data.cmd.path, p->data.cmd.av, data_singleton()->env), 1); return (1); } p->attrs &= ~PROCESS_STATE_MASK; p->attrs |= PROCESS_RUNNING; if (p->attrs & (PROCESS_BINARY | PROCESS_SCRIPT) - && access(p->path, X_OK) == -1) + && access(p->data.cmd.path, X_OK) == -1) { - ft_dprintf(2, "{red}%s: permission denied: %s{eoc}\n", SHELL_NAME, p->av[0]); + ft_dprintf(2, "{red}%s: permission denied: %s{eoc}\n", SHELL_NAME, p->data.cmd.av[0]); set_exitstatus(126, 1); return (1); } @@ -35,7 +37,7 @@ int launch_file(t_process *p) { if (p->attrs & PROCESS_UNKNOWN) { - ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); + ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->data.cmd.av[0]); exit(127); } process_setgroup(p, 0); @@ -43,9 +45,9 @@ int launch_file(t_process *p) if (process_redirect(p)) exit (1); if (p->attrs & PROCESS_BUILTIN) - exit((*p->execf)(p->path, p->av, data_singleton()->env)); - (*p->execf)(p->path, p->av, data_singleton()->env); - ft_dprintf(2, "{red}%s: internal execve error on %s{eoc}\n", SHELL_NAME, p->av[0]); + exit((*p->data.cmd.execf)(p->data.cmd.path, p->data.cmd.av, data_singleton()->env)); + (*p->data.cmd.execf)(p->data.cmd.path, p->data.cmd.av, data_singleton()->env); + ft_dprintf(2, "{red}%s: internal execve error on %s{eoc}\n", SHELL_NAME, p->data.cmd.av[0]); } else if (pid > 0) { diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 6b3a1df0..1396f6bd 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 15:19:05 by wescande ### ########.fr */ +/* Updated: 2017/03/07 15:42:49 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ t_itof g_launchmap[] = {PROCESS_IF, NULL}, {PROCESS_FOR, NULL}, {PROCESS_CASE, NULL}, + {0, NULL} }; int launch_process(t_process *p) @@ -29,9 +30,9 @@ int launch_process(t_process *p) int i; i = 0; - while (g_launchmap[i].type) + while (g_launchmap[i].id) { - if (p->type == g_launchmap[i].type) + if (p->type == g_launchmap[i].id) if (!g_launchmap[i].f) return (-1); return ((*g_launchmap[i].f)(p)); diff --git a/42sh/src/exec/set_process_cmd.c b/42sh/src/exec/set_process_cmd.c index 869f5e8f..e47714e6 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 15:07:39 by wescande ### ########.fr */ +/* Updated: 2017/03/07 15:50:51 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/set_process_map.c b/42sh/src/exec/set_process_map.c index 7959c7ea..ced5af20 100644 --- a/42sh/src/exec/set_process_map.c +++ b/42sh/src/exec/set_process_map.c @@ -6,32 +6,33 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:08:12 by wescande #+# #+# */ -/* Updated: 2017/03/07 15:11:17 by wescande ### ########.fr */ +/* Updated: 2017/03/07 15:50:56 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -t_setprocessmap g_setprocessmap[] = +t_itof g_setprocessmap[] = { - {TK_NEWLINE, &exec_semi}, - {TK_SEMI, &exec_semi}, - {TK_AMP, &exec_ampersand}, - {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_FOR, &exec_for}, - {TK_CASE, &exec_case}, - {TK_PAREN_OPEN, &exec_case_branch}, - {TK_ASSIGNEMENT_WORD, &exec_var}, + {TK_NEWLINE, NULL}, + {TK_SEMI, NULL}, + {TK_AMP, NULL}, + {TK_AND_IF, NULL}, + {TK_OR_IF,NULL}, + {TK_PIPE, NULL}, + {TK_WHILE, NULL}, + {TK_IF, NULL}, + {TK_ELIF, NULL}, + {TK_ELSE, NULL}, + {TK_UNTIL, NULL}, + {TK_FOR, NULL}, + {TK_CASE, NULL}, + {TK_PAREN_OPEN, NULL}, + {TK_ASSIGNEMENT_WORD, NULL}, + {MATH, NULL}, /* {TK_SUBSHELL, &exec_}, */ - {CMD, &exec_cmd}, - {0, 0}, + {CMD, &set_process_cmd}, + {0, NULL} }; int set_process_map(t_process *p, t_btree *ast, t_cmd *cmd) @@ -41,9 +42,9 @@ int set_process_map(t_process *p, t_btree *ast, t_cmd *cmd) i = 0; if (!ast) return (0); - while (g_execmap[i].type) + while (g_setprocessmap[i].id) { - if (p->type == g_setprocessmap[i].type) + if (p->type == g_setprocessmap[i].id) return ((*g_setprocessmap[i].f)(p, ast, cmd)); i++; } diff --git a/42sh/src/exec/token_to_argv.c b/42sh/src/exec/token_to_argv.c new file mode 100644 index 00000000..ae10fae7 --- /dev/null +++ b/42sh/src/exec/token_to_argv.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* token_to_argv.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 15:55:53 by wescande #+# #+# */ +/* Updated: 2017/03/07 15:56:25 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +char **token_to_argv(t_ld *ld, int do_match) +{ + char **my_tab; + int index; + char **expand; + char **content; + + my_tab = NULL; + while (ld) + { + content = ld->content; + if ((expand = glob(content[0], + (unsigned char *)content[1], + (unsigned char *)content[2], do_match))) + { + index = -1; + while (expand[++index]) + my_tab = ft_sstradd(my_tab, expand[index]); + ft_tabdel(&expand); + } + ld = ld->next; + } + return (my_tab); +}