diff --git a/42sh/Makefile b/42sh/Makefile index e42b63b4..5a69ec4f 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -74,12 +74,14 @@ 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/fd_is_valid.c\ exec/ft_exec.c\ exec/ft_findexec.c\ exec/launch_process.c\ -exec/loop_del.c\ -exec/loop_exec.c\ exec/process_redirect.c\ exec/process_reset.c\ exec/process_resetfds.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 58aabc7a..a115f3cb 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -35,8 +35,6 @@ # define IS_PIPEEND(a) (a & PROCESS_PIPEEND) # define IS_PIPESINGLE(a) ((a & PROCESS_PIPESTART) && (a & PROCESS_PIPEEND)) -# define SCRIPT_LOOP (1 << 0) - # include "libft.h" # include "types.h" # include "job_control.h" @@ -53,7 +51,9 @@ struct s_process int toclose; int status; t_flag attributes; - t_flag script; + t_condition if_branch; + t_condition case_branch; + char *case_pattern; }; struct s_exec @@ -102,7 +102,10 @@ 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 launch_process(t_process *p); int process_setexec(t_type type, t_process *p); @@ -128,7 +131,6 @@ void set_exitstatus(int status, int override); void ast_free(void *data, size_t content_size); - - +char **token_to_argv(t_astnode *node); #endif diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 23c31ae2..fd9768fd 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -65,6 +65,8 @@ struct s_data t_comp *comp; t_exec exec; t_jobc jobc; + char **local_var; + t_list *lst_func; }; extern t_stof g_builtins[]; diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 80ffcebb..1812e496 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -121,6 +121,7 @@ int add_subshell_cmd(t_btree **ast, t_list **lst); int add_subshell_sep(t_btree **ast, t_list **lst); int add_func_cmd(t_btree **ast, t_list **lst); int add_func_sep(t_btree **ast, t_list **lst); +int add_one_func(t_btree **ast, t_list **lst); int isloop(t_btree **ast, t_list **lst); int isdir(t_btree **ast, t_list **lst); int iscase(t_btree **ast, t_list **lst); diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 474b8b7a..e0f7ade6 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -34,6 +34,7 @@ typedef union u_astdata t_astdata; typedef union u_word t_word; typedef long long t_type; +typedef int t_condition; typedef struct s_job t_job; typedef struct s_execmap t_execmap; typedef struct s_redirmap t_redirmap; diff --git a/42sh/src/exec/loop_exec.c b/42sh/src/exec/exec_case.c similarity index 64% rename from 42sh/src/exec/loop_exec.c rename to 42sh/src/exec/exec_case.c index cf285ee4..3a2847f8 100644 --- a/42sh/src/exec/loop_exec.c +++ b/42sh/src/exec/exec_case.c @@ -1,27 +1,25 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* loop_exec.c :+: :+: :+: */ +/* exec_if.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/30 21:02:10 by ariard #+# #+# */ -/* Updated: 2017/01/31 19:27:49 by ariard ### ########.fr */ +/* Created: 2017/02/06 18:07:31 by ariard #+# #+# */ +/* Updated: 2017/02/20 22:31:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int loop_exec(t_list *list_ast) +int exec_case(t_btree **ast) { + t_astnode *node; + char **av; -// DG("begin exec loop"); -// ft_print_all_ast(*list_ast); - while (list_ast) - { - if (ft_exec((t_btree **)list_ast->content)) - return (1); - list_ast = list_ast->next; - } + data_singleton()->exec.process.case_branch = 0; + node = (*ast)->item; + av = token_to_argv(node); + data_singleton()->exec.process.case_pattern = av[0]; return (0); } diff --git a/42sh/src/exec/exec_case_branch.c b/42sh/src/exec/exec_case_branch.c new file mode 100644 index 00000000..2102769c --- /dev/null +++ b/42sh/src/exec/exec_case_branch.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/06 18:07:31 by ariard #+# #+# */ +/* Updated: 2017/02/20 22:31:46 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int exec_case_branch(t_btree **ast) +{ + t_astnode *node; + char **av; + + if (data_singleton()->exec.process.case_branch == 1) + 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); + } + return (0); +} diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 558804af..358e5887 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -12,7 +12,7 @@ #include "exec.h" -static char **token_to_argv(t_astnode *node) +char **token_to_argv(t_astnode *node) { char **my_tab; int index; @@ -20,7 +20,7 @@ static char **token_to_argv(t_astnode *node) char **content; t_ld *ld; - if (node->type == TK_COMMAND) + if (node->type == TK_WORD || node->type == TK_ASSIGNEMENT_WORD) { ld = node->data.token; my_tab = NULL; diff --git a/42sh/src/exec/exec_elif.c b/42sh/src/exec/exec_elif.c index 235b1938..ffefcdba 100644 --- a/42sh/src/exec/exec_elif.c +++ b/42sh/src/exec/exec_elif.c @@ -14,14 +14,13 @@ int exec_elif(t_btree **ast) { - int test; - - (void)ast; - test = 0; - if (test) + if (data_singleton()->exec.process.if_branch == 1) + return (0); + ft_exec(&(*ast)->left); + if (data_singleton()->exec.process.status == 1) { + data_singleton()->exec.process.if_branch = 1; ft_exec(&(*ast)->right); } - // btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_else.c b/42sh/src/exec/exec_else.c index f94735c1..9b31f5c6 100644 --- a/42sh/src/exec/exec_else.c +++ b/42sh/src/exec/exec_else.c @@ -14,7 +14,10 @@ int exec_else(t_btree **ast) { - ft_exec(&(*ast)->right); - //btree_delone(ast, &ast_free); + if (data_singleton()->exec.process.if_branch == 0) + { + 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 new file mode 100644 index 00000000..e1defebc --- /dev/null +++ b/42sh/src/exec/exec_for.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_until.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/06 20:42:20 by ariard #+# #+# */ +/* Updated: 2017/02/06 20:42:21 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int exec_for(t_btree **ast) +{ + t_astnode *node; + t_list *temp; +// char **av = NULL; + char *var; + + + node = (*ast)->item; + temp = node->data.wordlist; + var = temp->content; + builtin_setenv("setenv", (char*[]){var, 0}, data_singleton()->local_var); + while (temp) + { + //process expansion av = token_to_char(temp->content) + builtin_setenv("setenv", (char*[]){var, 0}, data_singleton()->local_var); + ft_exec(&(*ast)->right); + temp = temp->next; + } + return (0); +} diff --git a/42sh/src/exec/exec_func.c b/42sh/src/exec/exec_func.c new file mode 100644 index 00000000..a4a34f66 --- /dev/null +++ b/42sh/src/exec/exec_func.c @@ -0,0 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_while.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/30 17:33:53 by ariard #+# #+# */ +/* Updated: 2017/02/06 22:05:35 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + + diff --git a/42sh/src/exec/exec_if.c b/42sh/src/exec/exec_if.c index 667c32de..81aab2a7 100644 --- a/42sh/src/exec/exec_if.c +++ b/42sh/src/exec/exec_if.c @@ -14,7 +14,12 @@ int exec_if(t_btree **ast) { - ft_exec(&(*ast)->right); -// btree_delone(ast, &ast_free); + data_singleton()->exec.process.if_branch = 0; + ft_exec(&(*ast)->left); + if (data_singleton()->exec.process.status == 1) + { + data_singleton()->exec.process.if_branch = 1; + ft_exec(&(*ast)->right); + } return (0); } diff --git a/42sh/src/exec/exec_until.c b/42sh/src/exec/exec_until.c index 62b2a567..6f1e0c33 100644 --- a/42sh/src/exec/exec_until.c +++ b/42sh/src/exec/exec_until.c @@ -14,12 +14,11 @@ int exec_until(t_btree **ast) { - int test; - - test = 0; - while (test++ != 10) + ft_exec(&(*ast)->left); + while (data_singleton()->exec.process.status == 0) + { ft_exec(&(*ast)->right); - -// btree_delone(ast, &ast_free); + ft_exec(&(*ast)->left); + } return (0); } diff --git a/42sh/src/exec/loop_del.c b/42sh/src/exec/exec_var.c similarity index 65% rename from 42sh/src/exec/loop_del.c rename to 42sh/src/exec/exec_var.c index 1bdda0fd..e830950d 100644 --- a/42sh/src/exec/loop_del.c +++ b/42sh/src/exec/exec_var.c @@ -1,27 +1,24 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* loop_del.c :+: :+: :+: */ +/* exec_while.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/31 22:39:20 by ariard #+# #+# */ -/* Updated: 2017/01/31 23:20:40 by ariard ### ########.fr */ +/* Created: 2017/01/30 17:33:53 by ariard #+# #+# */ +/* Updated: 2017/02/06 22:05:35 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int loop_del(t_list *list_ast) +int exec_var(t_btree **ast) { - t_list *temp; + t_astnode *node; + char **av; - while (list_ast) - { - btree_del((t_btree **)list_ast->content, &ast_free); - temp = list_ast; - list_ast = list_ast->next; - ft_lstdelone(&temp, NULL); - } + node = (*ast)->item; + 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 cae778e1..d9824950 100644 --- a/42sh/src/exec/exec_while.c +++ b/42sh/src/exec/exec_while.c @@ -14,12 +14,11 @@ int exec_while(t_btree **ast) { - int test; - - test = 10; - while (test--) + ft_exec(&(*ast)->left); + while (data_singleton()->exec.process.status == 1) + { ft_exec(&(*ast)->right); - -// btree_delone(ast, &ast_free); + ft_exec(&(*ast)->left); + } return (0); } diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 12c47e05..4a0f872f 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -23,7 +23,6 @@ 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.toclose = STDIN; @@ -45,6 +44,9 @@ int data_init(void) data->jobc.first_job = NULL; data->jobc.current_id = 1; + + data->local_var = NULL; + data->lst_func = NULL; if ((term_name = ft_getenv(data->env, "TERM")) == NULL) return (-1); if (tgetent(NULL, term_name) != 1) diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index d372954d..d2e3e825 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -49,7 +49,7 @@ int add_cmd(t_btree **ast, t_list **lst) my_tab = NULL; node = (*ast)->item; node->type = token->type; - if (token->type == TK_WORD) + if (token->type == TK_WORD || token->type == TK_ASSIGNEMENT_WORD) { my_tab = ft_sstradd(my_tab, token->data); my_tab = ft_sstradd(my_tab, (char *)token->esc); diff --git a/42sh/src/parser/add_func.c b/42sh/src/parser/add_func.c index e2eda0fe..ada2a7be 100644 --- a/42sh/src/parser/add_func.c +++ b/42sh/src/parser/add_func.c @@ -23,7 +23,10 @@ int isfunc(t_btree **ast, t_list **lst) { node = (*ast)->item; if (node->type == TK_WORD && token->type == CLOSE_LIST) + { node->type = FNAME; + add_one_func(ast, lst); + } if (node->type == FNAME && node->full == 0) return (1); if (isfunc(&(*ast)->right, lst) == 1) @@ -62,4 +65,9 @@ int add_func_sep(t_btree **ast, t_list **lst) return (add_sep(&(*ast)->right, lst)); } - +int add_one_func(t_btree **ast, t_list **lst) +{ + (void)lst; + ft_lsteadd(&data_singleton()->lst_func, ft_lstnew(ast, sizeof(*ast))); + return (0); +} diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index afccf9df..dba05036 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -187,7 +187,7 @@ t_aggrematch g_aggrematch[] = {CLOSE_LIST, PATTERN, CASE_LIST_NS, PATTERN_CASE}, {CLOSE_LIST, FUNC_NAME, FUNCTION_DEFINITION, FUNC_NAME}, {SUBSHELL, ALL, COMPOUND_COMMAND, 0}, - {BRACE_CLAUSE, ALL, COMPOUND_COMMAND, 0}, + {BRACE_CLAUSE, ALL, COMPOUND_COMMAND, 0}, {COMPOUND_COMMAND, ALL, COMMAND, 0}, {AND_OR_MINOR, PIPE_SEMI_SEQUENCE, AND_OR_MAJOR, PIPE_SEMI_SEQUENCE}, {AND_OR_MINOR, LINEBREAK, AND_OR_MAJOR, 0},