diff --git a/42sh/Makefile b/42sh/Makefile index cfce41bd..7400f7e3 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -63,26 +63,29 @@ exec/bad_fd.c\ exec/error_badidentifier.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_leaf.c\ +exec/exec_math.c\ exec/exec_or_if.c\ exec/exec_pipe.c\ exec/exec_semi.c\ exec/exec_until.c\ exec/exec_var.c\ -exec/exec_while.c\ -exec/exec_math.c\ exec/fd_is_valid.c\ exec/ft_exec.c\ exec/ft_findexec.c\ +exec/launch_builtin.c\ +exec/launch_case.c\ +exec/launch_file.c\ +exec/launch_for.c\ +exec/launch_if.c\ exec/launch_process.c\ +exec/launch_while.c\ exec/mark_process_status.c\ exec/process_redirect.c\ exec/process_reset.c\ @@ -98,6 +101,13 @@ exec/redirect_less.c\ exec/redirect_lessand.c\ exec/set_exitstatus.c\ exec/set_process.c\ +exec/set_process_case.c\ +exec/set_process_cmd.c\ +exec/set_process_for.c\ +exec/set_process_if.c\ +exec/set_process_map.c\ +exec/set_process_while.c\ +exec/token_to_argv.c\ glob/command_getoutput.c\ glob/dir_glob.c\ glob/esc_print.c\ @@ -108,10 +118,12 @@ glob/expand_home.c\ glob/expand_var.c\ glob/ft_strsplit_esc.c\ glob/ft_strsplit_spe.c\ +glob/gen_tab_esc.c\ glob/glob.c\ glob/is_char_esc.c\ glob/lib_perso/ft_ld_back.c\ glob/lib_perso/ft_ld_clear.c\ +glob/lib_perso/ft_ld_copy.c\ glob/lib_perso/ft_ld_del.c\ glob/lib_perso/ft_ld_front.c\ glob/lib_perso/ft_ld_new.c\ @@ -128,6 +140,8 @@ glob/lib_perso/ft_strsubf.c\ glob/lib_perso/ft_tabdel.c\ glob/lib_perso/ft_tablen.c\ glob/match_pattern.c\ +glob/tab_esc_copy.c\ +glob/word_is_assignment.c\ hash_table/ft_add_hash.c\ hash_table/hash.c\ hash_table/hash_free.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 3a84ac84..6101dd9c 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 16:55:12 by wescande ### ########.fr */ +/* Updated: 2017/03/07 20:50:23 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,7 +63,6 @@ struct s_data_cond struct s_data_list { - char *word; t_ld *list_word; t_btree *content; }; @@ -76,7 +75,7 @@ union u_process_data struct s_data_cond d_else; struct s_data_cond d_elif; struct s_data_list d_for; -// struct s_data_cond case; + struct s_data_list d_case; }; enum e_process_type @@ -140,8 +139,8 @@ 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_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); @@ -174,6 +173,7 @@ char *ft_findexec(char *path, char *file); void set_exitstatus(int status, int override); void ast_free(void *data, size_t content_size); +t_btree *ast_copy(t_btree *tree); void redir_free(void *data, size_t content_size); char **token_to_argv(t_ld *ld, int do_match); diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h index 26d18d42..d91bcaef 100644 --- a/42sh/includes/glob.h +++ b/42sh/includes/glob.h @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:31:18 by wescande #+# #+# */ -/* Updated: 2017/03/03 20:39:05 by wescande ### ########.fr */ +/* Updated: 2017/03/07 20:40:28 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,6 +64,8 @@ typedef struct s_bquote char **glob(char *str, unsigned char *esc, unsigned char *dbl_esc, int do_match); void esc_print(char *str, unsigned char *esc); +int word_is_assignment(char **content); +void *tab_esc_copy(void *content); /* ** return TRUE if path file is a directory. @@ -120,6 +122,7 @@ t_ld *ft_ld_back(t_ld *ld); t_ld *ft_ld_swap(t_ld *l_cur); char **ft_ld_to_tab(t_ld *ld); t_ld *ft_ld_order(t_ld *ld, int (*f)(), void (*del)()); +t_ld *ft_ld_copy(t_ld *src, void *(*f)(void *elem)); /* ** str: diff --git a/42sh/src/exec/ast_copy.c b/42sh/src/exec/ast_copy.c new file mode 100644 index 00000000..2776bb49 --- /dev/null +++ b/42sh/src/exec/ast_copy.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ast_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 20:44:42 by wescande #+# #+# */ +/* Updated: 2017/03/07 20:50:43 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +t_btree *ast_copy(t_btree *tree) +{ + t_btree *node; + + if (!tree) + return (NULL); + node = btree_create_node(tree->item, tree->content_size); + node->left = ast_copy(tree->left); + node->right = ast_copy(tree->right); + return (node); +} diff --git a/42sh/src/exec/exec_case_branch.c b/42sh/src/exec/exec_case_branch.c index 2d0db420..921b136b 100644 --- a/42sh/src/exec/exec_case_branch.c +++ b/42sh/src/exec/exec_case_branch.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* exec_if.c :+: :+: :+: */ +/* exec_case_branch.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ +/* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/06 18:07:31 by ariard #+# #+# */ -/* Updated: 2017/03/06 19:36:55 by ariard ### ########.fr */ +/* Created: 2017/03/07 20:33:45 by wescande #+# #+# */ +/* Updated: 2017/03/07 20:34:48 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_for.c b/42sh/src/exec/exec_for.c deleted file mode 100644 index 632f5773..00000000 --- a/42sh/src/exec/exec_for.c +++ /dev/null @@ -1,43 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* exec_until.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/06 20:42:20 by ariard #+# #+# */ -/* Updated: 2017/03/06 19:37:02 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "exec.h" - -int exec_for(t_btree **ast) -{ - t_astnode *node; - t_ld *temp; - char **av; - char *var; - int i; - - node = (*ast)->item; - temp = node->data.cmd.wordlist; - var = ((char **)(temp->content))[0]; - if (ft_isdigit(var[0])) - return (error_badidentifier(var)); - temp = temp->next; - while (temp) - { - i = 0; - av = token_to_argv(temp, 1); - while (av[i]) - { - builtin_setenv("setenv", (char*[]){var, av[i], 0}, - data_singleton()->local_var); - ft_exec(&(*ast)->right); - i++; - } - temp = temp->next; - } - return (0); -} diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 07b28a64..7c70ce5f 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 16:20:18 by wescande ### ########.fr */ +/* Updated: 2017/03/07 17:22:42 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,8 +22,8 @@ t_itof g_execmap[] = {TK_PIPE, &exec_pipe}, {TK_WHILE, &exec_leaf}, {TK_IF, &exec_leaf}, - {TK_ELIF, &exec_leaf}, - {TK_ELSE, &exec_leaf}, + {TK_ELIF, &exec_elif}, + {TK_ELSE, &exec_else}, {TK_UNTIL, &exec_leaf}, {TK_FOR, &exec_leaf}, {TK_CASE, &exec_leaf}, diff --git a/42sh/src/exec/launch_case.c b/42sh/src/exec/launch_case.c new file mode 100644 index 00000000..ed10dcd2 --- /dev/null +++ b/42sh/src/exec/launch_case.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* launch_case.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 19:02:23 by wescande #+# #+# */ +/* Updated: 2017/03/07 20:38:19 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" +/* +int exec_case(t_btree **ast) +{ + t_astnode *node; + char **av; + t_exec *exec; + + (void)ast; + exec = &data_singleton()->exec; + exec->attrs &= ~EXEC_CASE_BRANCH; + + node = (*ast)->item; + av = token_to_argv(node->data.cmd.wordlist, 1); + exec->case_pattern = av; + return (0); +} +*/ + +static int do_case(t_process *p) +{ + t_exec *exec; + + exec = &data_singleton()->exec; + exec->attrs &= ~EXEC_CASE_BRANCH; + exec->case_pattern = token_to_argv(p->data.d_case.list_word, 1); +/* ft_exec(&p->data.d_if.condition); + if (!(ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0"))) + { + exec->attrs |= EXEC_IF_BRANCH; + ft_exec(&p->data.d_if.content); + } + return (ft_atoi(ft_getenv(data_singleton()->env, "?")));*/ + return (0); +} + +int launch_case(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_case(p)); + } + else if (pid > 0) + return (pid); + } + else + do_case(p); + return (0); +} diff --git a/42sh/src/exec/launch_file.c b/42sh/src/exec/launch_file.c index 7f4a1b65..aaa04a6d 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 16:49:31 by wescande ### ########.fr */ +/* Updated: 2017/03/07 17:08:16 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ int launch_file(t_process *p) exit (1); (*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]); + exit(42); } else if (pid > 0) return (pid); diff --git a/42sh/src/exec/launch_for.c b/42sh/src/exec/launch_for.c new file mode 100644 index 00000000..059448c9 --- /dev/null +++ b/42sh/src/exec/launch_for.c @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* launch_for.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 17:34:43 by wescande #+# #+# */ +/* Updated: 2017/03/07 19:00:56 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +static int do_for(t_process *p) +{ + t_ld *temp; + int i; + char **av; + char *var; + + temp = p->data.d_for.list_word; + var = ((char **)temp->content)[0]; + if (!word_is_assignment(temp->content)) + return (error_badidentifier(var)); + temp = temp->next; + while (temp) + { + i = -1; + av = token_to_argv(temp, 1); + while (av[++i]) + { + builtin_setenv("setenv", (char*[]){var, av[i], 0}, + data_singleton()->local_var); + ft_exec(&p->data.d_for.content); + i++; + } + temp = temp->next; + } + return (ft_atoi(ft_getenv(data_singleton()->env, "?"))); +} + +int launch_for(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_for(p)); + } + else if (pid > 0) + return (pid); + } + else + do_for(p); + return (0); +} +/* + int exec_for(t_btree **ast) + { + t_astnode *node; + t_ld *temp; + char **av; + char *var; + int i; + + node = (*ast)->item; + temp = node->data.cmd.wordlist; + var = ((char **)(temp->content))[0]; + if (ft_isdigit(var[0])) + return (error_badidentifier(var)); + temp = temp->next; + while (temp) + { + i = 0; + av = token_to_argv(temp, 1); + while (av[i]) + { + builtin_setenv("setenv", (char*[]){var, av[i], 0}, + data_singleton()->local_var); + ft_exec(&(*ast)->right); + i++; + } + temp = temp->next; + } + return (0); + }*/ diff --git a/42sh/src/exec/launch_if.c b/42sh/src/exec/launch_if.c new file mode 100644 index 00000000..e8eca419 --- /dev/null +++ b/42sh/src/exec/launch_if.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* launch_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 17:26:53 by wescande #+# #+# */ +/* Updated: 2017/03/07 18:36:02 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +static int do_if(t_process *p) +{ + t_exec *exec; + + exec = &data_singleton()->exec; + exec->attrs &= ~EXEC_IF_BRANCH; + ft_exec(&p->data.d_if.condition); + if (!(ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0"))) + { + exec->attrs |= EXEC_IF_BRANCH; + ft_exec(&p->data.d_if.content); + } + return (ft_atoi(ft_getenv(data_singleton()->env, "?"))); +} + +int launch_if(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_if(p)); + } + else if (pid > 0) + return (pid); + } + else + do_if(p); + return (0); +} +/* +int exec_if(t_btree **ast) +{ + t_exec *exec; + + exec = &data_singleton()->exec; + + exec->attrs &= ~EXEC_IF_BRANCH; + ft_exec(&(*ast)->left); + if (ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0") == 0) + { + exec->attrs |= EXEC_IF_BRANCH; + ft_exec(&(*ast)->right); + } + return (0); +}*/ diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index a2d75362..cd6710f8 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 16:49:10 by wescande ### ########.fr */ +/* Updated: 2017/03/07 19:02:40 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,10 +18,10 @@ t_itof g_launchmap[] = {PROCESS_BUILTIN, launch_builtin}, {PROCESS_FILE, launch_file}, {PROCESS_SUBSHELL, NULL}, - {PROCESS_WHILE, NULL}, - {PROCESS_IF, NULL}, - {PROCESS_FOR, NULL}, - {PROCESS_CASE, NULL}, + {PROCESS_WHILE, launch_while}, + {PROCESS_IF, launch_if}, + {PROCESS_FOR, launch_for}, + {PROCESS_CASE, launch_case}, {0, NULL} }; diff --git a/42sh/src/exec/exec_while.c b/42sh/src/exec/launch_while.c similarity index 65% rename from 42sh/src/exec/exec_while.c rename to 42sh/src/exec/launch_while.c index 59699f38..947ea4c6 100644 --- a/42sh/src/exec/exec_while.c +++ b/42sh/src/exec/launch_while.c @@ -1,17 +1,17 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* exec_while.c :+: :+: :+: */ +/* launch_while.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ +/* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/30 17:33:53 by ariard #+# #+# */ -/* Updated: 2017/03/07 16:41:50 by wescande ### ########.fr */ +/* Created: 2017/03/07 17:20:53 by wescande #+# #+# */ +/* Updated: 2017/03/07 17:30:16 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" - +/* int exec_while(t_btree **ast) { int ret; @@ -28,10 +28,19 @@ int exec_while(t_btree **ast) exit(ret); return (0); } - +*/ static int do_while(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); } @@ -43,15 +52,15 @@ int launch_while(t_process *p) { pid = fork(); if (pid == 0) - exit(do_while(p)); - else if (pid > 0) { - p->pid = pid; - process_setgroup(p, pid); - return (0); + data_singleton()->opts &= ~SH_INTERACTIVE; + data_singleton()->opts &= ~SH_OPTS_JOBC; + exit(do_while(p)); } + else if (pid > 0) + return (pid); } else do_while(p); - return (1); + return (0); } diff --git a/42sh/src/exec/exec_case.c b/42sh/src/exec/set_process_case.c similarity index 56% rename from 42sh/src/exec/exec_case.c rename to 42sh/src/exec/set_process_case.c index ed982dac..b69cce86 100644 --- a/42sh/src/exec/exec_case.c +++ b/42sh/src/exec/set_process_case.c @@ -1,29 +1,20 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* exec_if.c :+: :+: :+: */ +/* set_process_case.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ +/* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/06 18:07:31 by ariard #+# #+# */ -/* Updated: 2017/03/06 19:37:20 by ariard ### ########.fr */ +/* Created: 2017/03/07 20:36:04 by wescande #+# #+# */ +/* Updated: 2017/03/07 20:51:41 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_case(t_btree **ast) +int set_process_case(t_process *p, t_btree *ast, t_cmd *cmd) { - t_astnode *node; - char **av; - t_exec *exec; - - (void)ast; - exec = &data_singleton()->exec; - exec->attrs &= ~EXEC_CASE_BRANCH; - - node = (*ast)->item; - av = token_to_argv(node->data.cmd.wordlist, 1); - exec->case_pattern = av; + 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); return (0); } diff --git a/42sh/src/exec/exec_if.c b/42sh/src/exec/set_process_for.c similarity index 56% rename from 42sh/src/exec/exec_if.c rename to 42sh/src/exec/set_process_for.c index c1a59aef..c3bfb0e8 100644 --- a/42sh/src/exec/exec_if.c +++ b/42sh/src/exec/set_process_for.c @@ -1,29 +1,20 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* exec_if.c :+: :+: :+: */ +/* set_process_for.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ariard +#+ +:+ +#+ */ +/* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/06 18:07:31 by ariard #+# #+# */ -/* Updated: 2017/03/05 15:12:28 by jhalford ### ########.fr */ +/* Created: 2017/03/07 19:38:05 by wescande #+# #+# */ +/* Updated: 2017/03/07 20:51:57 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_if(t_btree **ast) +int set_process_for(t_process *p, t_btree *ast, t_cmd *cmd) { - t_exec *exec; - - exec = &data_singleton()->exec; - - exec->attrs &= ~EXEC_IF_BRANCH; - ft_exec(&(*ast)->left); - if (ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0") == 0) - { - exec->attrs |= EXEC_IF_BRANCH; - ft_exec(&(*ast)->right); - } + 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); return (0); } diff --git a/42sh/src/exec/set_process_if.c b/42sh/src/exec/set_process_if.c new file mode 100644 index 00000000..f07f8202 --- /dev/null +++ b/42sh/src/exec/set_process_if.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_process_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 19:19:50 by wescande #+# #+# */ +/* Updated: 2017/03/07 20:35:16 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int set_process_if(t_process *p, t_btree *ast, t_cmd *cmd) +{ + p->data.d_if.condition = btree_map(ast->left, ast_copy); + p->data.d_if.content = btree_map(ast->right, ast_copy); + return (0); +} diff --git a/42sh/src/exec/set_process_while.c b/42sh/src/exec/set_process_while.c new file mode 100644 index 00000000..bc7b4b1a --- /dev/null +++ b/42sh/src/exec/set_process_while.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_process_while.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 19:38:10 by wescande #+# #+# */ +/* Updated: 2017/03/07 20:51:24 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int set_process_while(t_process *p, t_btree *ast, t_cmd *cmd) +{ + p->data.d_while.condition = ast_copy(ast->left); + p->data.d_while.content = ast_copy(ast->right); + return (0); +} diff --git a/42sh/src/glob/expand_brace.c b/42sh/src/glob/expand_brace.c index c0055817..c715ae48 100644 --- a/42sh/src/glob/expand_brace.c +++ b/42sh/src/glob/expand_brace.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/12 19:00:29 by wescande #+# #+# */ -/* Updated: 2017/02/24 22:05:14 by ariard ### ########.fr */ +/* Updated: 2017/03/07 20:03:35 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,36 +20,6 @@ ** -t_glob *gl -> struct of expanding */ -char **gen_tab(const char *pat, - const unsigned char *esc, - const unsigned char *esc2, - int dup) -{ - char **my_tab; - - if (!(my_tab = (char **)malloc(sizeof(char *) * (esc2 ? 4 : 3)))) - return (NULL); - if (dup) - { - my_tab[0] = ft_strdup(pat); - my_tab[1] = ft_strdup((const char *)esc); - if (esc2) - my_tab[2] = ft_strdup((const char *)esc2); - } - else - { - my_tab[0] = (char *)pat; - my_tab[1] = (char *)esc; - if (esc2) - my_tab[2] = (char *)esc2; - } - if (esc2) - my_tab[3] = NULL; - else - my_tab[2] = NULL; - return (my_tab); -} - static void iter_on_each(t_expand *me) { int i; diff --git a/42sh/src/glob/gen_tab_esc.c b/42sh/src/glob/gen_tab_esc.c new file mode 100644 index 00000000..c9f5f8e4 --- /dev/null +++ b/42sh/src/glob/gen_tab_esc.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* gen_tab_esc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 20:04:40 by wescande #+# #+# */ +/* Updated: 2017/03/07 20:07:31 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +char **gen_tab(const char *pat, + const unsigned char *esc, + const unsigned char *esc2, + int dup) +{ + char **my_tab; + int len; + + if (!(my_tab = (char **)malloc(sizeof(char *) * (esc2 ? 4 : 3)))) + return (NULL); + if (dup) + { + len = ft_strlen(pat); + my_tab[0] = ft_strdup(pat); + my_tab[1] = (char *)dup_char_esc(esc, len >> 3); + if (esc2) + my_tab[2] = (char *)dup_char_esc(esc2, len >> 3); + } + else + { + my_tab[0] = (char *)pat; + my_tab[1] = (char *)esc; + if (esc2) + my_tab[2] = (char *)esc2; + } + if (esc2) + my_tab[3] = NULL; + else + my_tab[2] = NULL; + return (my_tab); +} diff --git a/42sh/src/glob/lib_perso/ft_ld_copy.c b/42sh/src/glob/lib_perso/ft_ld_copy.c new file mode 100644 index 00000000..7cc67659 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_copy.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 19:42:23 by wescande #+# #+# */ +/* Updated: 2017/03/07 20:29:28 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +t_ld *ft_ld_copy(t_ld *src, void *(*f)(void *elem)) +{ + t_ld *dst; + void *content; + + dst = NULL; + while (src) + { + content = (*f)(src->content); + ft_ld_pushfront(dst, content); + src = src->next; + } + ft_ld_reverse(dst); + return (dst); +} diff --git a/42sh/src/glob/tab_esc_copy.c b/42sh/src/glob/tab_esc_copy.c new file mode 100644 index 00000000..0661d863 --- /dev/null +++ b/42sh/src/glob/tab_esc_copy.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_process_for.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 19:38:05 by wescande #+# #+# */ +/* Updated: 2017/03/07 20:40:30 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void *tab_esc_copy(void *content) +{ + char **ch_cont; + + ch_cont = (char **)content; + return (gen_tab(ch_cont[0], (unsigned char *)ch_cont[1], (unsigned char *)ch_cont[2], 1)); +} diff --git a/42sh/src/glob/word_is_assignment.c b/42sh/src/glob/word_is_assignment.c new file mode 100644 index 00000000..de819dd1 --- /dev/null +++ b/42sh/src/glob/word_is_assignment.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* word_is_assignment.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 18:59:11 by wescande #+# #+# */ +/* Updated: 2017/03/07 18:59:38 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +int word_is_assignment(char **content) +{ + const int len = ft_strlen(content[0]); + int pos; + char c; + const unsigned char *esc = (const unsigned char *)content[1]; + + pos = -1; + if (ft_isdigit(content[0][0])) + return (0); + while (++pos < len) + { + if (is_char_esc(esc, content[0], content[0] + pos)) + return (0); + c = content[0][pos]; + if (!ft_isalnum(c) && c != '_') + return (0); + } + return (1); +} + diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index d10c9e48..2fcf8faa 100644 --- a/42sh/src/lexer/lexer_word.c +++ b/42sh/src/lexer/lexer_word.c @@ -6,28 +6,31 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */ -/* Updated: 2017/03/05 17:07:26 by ariard ### ########.fr */ +/* Updated: 2017/03/07 19:00:22 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "lexer.h" - +/* static int word_is_assignment(t_token *token) { const int len = ft_strlen(token->data); - int pos = -1; + int pos; char c; + pos = -1; + if (ft_isdigit(token->data[pos])) + return (0); while (++pos < len) { if (is_char_esc(token->esc, token->data, token->data + pos)) return (0); c = token->data[pos]; - if (!ft_isalnum(c) && c != '_' && c != '-') + if (!ft_isalnum(c) && c != '_') return (0); } return (1); -} +}*/ int lexer_word(t_list **alst, t_lexer *lexer) { @@ -42,7 +45,8 @@ int lexer_word(t_list **alst, t_lexer *lexer) lexer->state = state; return (lexer_lex(alst, lexer)); } - if (lexer->str[lexer->pos] == '=' && word_is_assignment(token)) + if (lexer->str[lexer->pos] == '=' + && word_is_assignment((char *[]){token->data, (char *)token->esc})) { lexer->state = ASSIGNEMENT_WORD; return (lexer_lex(alst, lexer)); diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 0518acf6..f88cbaa8 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/06 19:36:34 by ariard ### ########.fr */ +/* Updated: 2017/03/07 20:10:10 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,19 +68,15 @@ int add_cmd(t_btree **ast, t_list **lst) { t_token *token; t_astnode *node; - char **my_tab; int i; - i = 0; - while (i < 14) - { + i = -1; + while (++i < 14) if (g_distrostree[i].test(ast, lst) == 1) { DG("add : %d", i); return (g_distrostree[i].add(ast, lst)); } - i++; - } if (!*ast) gen_node(ast); else if (no_del_token(ast, lst)) @@ -92,16 +88,14 @@ int add_cmd(t_btree **ast, t_list **lst) else node->type = CMD; if (token->type == TK_WORD || token->type == TK_ASSIGNEMENT_WORD) - { - DG("add cmd default"); - if ((my_tab = (char **)malloc(sizeof(char *) * 4))) +/* 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); - } + }*/ + ft_ld_pushback(&node->data.cmd.token, + gen_tab(token->data, token->esc, token->esc2, 1)); return (0); }