From fc8dcb8d24e16d3e500b72b52bb3b72e8ce6e632 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Wed, 15 Mar 2017 00:47:56 +0100 Subject: [PATCH] divers modifs + better tree case --- 42sh/includes/builtin.h | 4 +++- 42sh/src/exec/exec_var.c | 4 +++- 42sh/src/exec/plaunch_case.c | 20 +----------------- 42sh/src/exec/plaunch_for.c | 37 +++----------------------------- 42sh/src/main/main.c | 6 +++--- 42sh/src/parser/add_case.c | 41 ++++++------------------------------ 42sh/src/parser/add_cmd.c | 23 ++++++++++---------- 42sh/src/parser/add_func.c | 2 +- 8 files changed, 31 insertions(+), 106 deletions(-) diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index 98162ae9..e3240700 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */ -/* Updated: 2017/03/14 22:59:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 23:09:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ # include "libft.h" # include "builtin_read.h" +# define BT_EXPORT_LP (1 << 0) + t_execf *is_builtin(t_process *p); int builtin_export(const char *path, char *const av[], char *const envp[]); int builtin_unset(const char *path, char *const av[], char *const envp[]); diff --git a/42sh/src/exec/exec_var.c b/42sh/src/exec/exec_var.c index ad4d6071..9b1136c2 100644 --- a/42sh/src/exec/exec_var.c +++ b/42sh/src/exec/exec_var.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 11:12:05 by ariard #+# #+# */ -/* Updated: 2017/03/14 22:55:04 by ariard ### ########.fr */ +/* Updated: 2017/03/14 23:55:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,6 +30,8 @@ int exec_var(t_btree **ast) node = (*ast)->item; av = token_to_argv(node->data.cmd.token, 1); set_var(av[0], &var, &value); + if (ft_getenv(data_singleton()->env, var)) + return (0); builtin_setenv("internal", (char*[]){"local", var, value, 0}, NULL); return (0); } diff --git a/42sh/src/exec/plaunch_case.c b/42sh/src/exec/plaunch_case.c index 1a9bccab..8d88d699 100644 --- a/42sh/src/exec/plaunch_case.c +++ b/42sh/src/exec/plaunch_case.c @@ -6,29 +6,11 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 19:02:23 by wescande #+# #+# */ -/* Updated: 2017/03/13 20:28:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/15 00:04:09 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.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; diff --git a/42sh/src/exec/plaunch_for.c b/42sh/src/exec/plaunch_for.c index 46c8f5bb..aa7fd313 100644 --- a/42sh/src/exec/plaunch_for.c +++ b/42sh/src/exec/plaunch_for.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 17:34:43 by wescande #+# #+# */ -/* Updated: 2017/03/13 20:28:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/15 00:03:12 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,10 +30,9 @@ static int do_for(t_process *p) av = token_to_argv(temp, 1); while (av[++i]) { - builtin_setenv("setenv", (char*[]){var, av[i], 0}, - data_singleton()->local_var); + builtin_setenv("setenv", (char*[]){"local" ,var, av[i], 0}, + NULL); ft_exec(&p->data.d_for.content); - i++; } temp = temp->next; } @@ -65,33 +64,3 @@ int plaunch_for(t_process *p) 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/main/main.c b/42sh/src/main/main.c index 4db84122..e4771b10 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/14 23:06:10 by ariard ### ########.fr */ +/* Updated: 2017/03/15 00:07:41 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,8 +71,8 @@ int handle_instruction(int fd) } } btree_print(STDBUG, ast, &ft_putast); - if (ft_exec(&ast)) - return (2); +// if (ft_exec(&ast)) +// return (2); instruction_free(&token, &parser, &ast); if (SH_IS_INTERACTIVE(data_singleton()->opts) && *lexer.str) ft_add_str_in_history(lexer.str); diff --git a/42sh/src/parser/add_case.c b/42sh/src/parser/add_case.c index 4f1511ee..5f1083a7 100644 --- a/42sh/src/parser/add_case.c +++ b/42sh/src/parser/add_case.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/04 20:42:13 by ariard #+# #+# */ -/* Updated: 2017/03/14 21:45:03 by ariard ### ########.fr */ +/* Updated: 2017/03/15 00:46:52 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,24 +25,8 @@ int iscase(t_btree **ast, t_list **lst) || node->type == TK_AMP || node->type == TK_PIPE) && iscase(&(*ast)->right, lst) == 1) return (1); - if (node->type == TK_CASE) - return (1); - } - return (0); -} - -int iscase_pattern(t_btree **ast, t_list **lst) -{ - t_astnode *node; - t_token *token; - - node = NULL; - token = (*lst)->content; - if (*ast) - { - node = (*ast)->item; if ((node->type == TK_CASE || node->type == TK_PAREN_OPEN) - && token->type == TK_WORD && node->pattern == 0) + && node->pattern == 1 && node->full == 0) return (1); } return (0); @@ -58,7 +42,7 @@ int iscase_branch(t_btree **ast, t_list **lst) if (*ast) { node = (*ast)->item; - if ((node->type == TK_PAREN_OPEN || node->type == TK_CASE) + if ((node->type == TK_PAREN_OPEN) && node->nest == 0 && token->type == TK_PAREN_OPEN) return (1); } @@ -81,23 +65,10 @@ int add_case_cmd(t_btree **ast, t_list **lst) else if (token->type == TK_DSEMI && node->type == TK_PAREN_OPEN && node->nest == 0) return ((node->full = 1)); - else if ((token->type == TK_ESAC || token->type == TK_PAREN_CLOSE) + else if ((token->type == TK_ESAC) && node->nest == 0) + return ((node->full = 1)); + else if (token->type == TK_PAREN_CLOSE) return (0); return (add_cmd(&(*ast)->right, lst)); } - -int add_pattern(t_btree **ast, t_list **lst) -{ - t_astnode *node; - t_token *token; - - token = (*lst)->content; - node = (*ast)->item; - DG("token type is %s", read_state(token->type)); - DG("node type is %s", read_state(node->type)); - ft_ld_pushback(&node->data.cmd.token, - gen_tab(token->data, token->esc, token->esc2, 1)); - node->pattern = 1; - return (0); -} diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 9a5a42e0..4b7eaff3 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/14 22:22:15 by ariard ### ########.fr */ +/* Updated: 2017/03/15 00:45:46 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,6 @@ t_distrostree g_distrostree[] = {&isloop, &add_loop_cmd}, {&iscondition_branch, &add_branch}, {&iscondition, &add_condition_cmd}, - {&iscase_pattern, &add_pattern}, {&iscase, &add_case_cmd}, {&iscase_branch, &add_branch}, {&issubshell, &add_subshell_cmd}, @@ -40,7 +39,7 @@ int superflous_token(t_btree **ast, t_list **lst) if (*lst) { token = (*lst)->content; - if (token->type == TK_IN || token->type == TK_DSEMI) + if (token->type == TK_IN) return (1); } return (0); @@ -49,12 +48,16 @@ int superflous_token(t_btree **ast, t_list **lst) static int no_del_token(t_btree **ast, t_list **lst) { t_astnode *node; + t_token *token; - (void)lst; node = NULL; + token = (*lst)->content; if (*ast) { node = (*ast)->item; + if (token->type == TK_WORD && (node->type == TK_CASE + || node->type == TK_PAREN_OPEN)) + return (0); if (node->type != TK_DO && node->type != TK_THEN && node->type != CMD && node->type != REDIR && node->type != TK_ASSIGNMENT_WORD) @@ -70,7 +73,7 @@ int add_cmd(t_btree **ast, t_list **lst) int i; i = -1; - while (++i < 16) + while (++i < 15) { if (g_distrostree[i].test(ast, lst) == 1) { @@ -87,17 +90,13 @@ int add_cmd(t_btree **ast, t_list **lst) if (token->type == TK_IF) add_if(ast, lst); else if (token->type != TK_WORD) - { - DG("type is %s", read_state(token->type)); node->type = token->type; - } - else + else if (node->type != TK_CASE && node->type != TK_PAREN_OPEN) node->type = CMD; if (token->type == TK_WORD || token->type == TK_ASSIGNMENT_WORD) - { - DG("token data is %s", token->data); ft_ld_pushback(&node->data.cmd.token, gen_tab(token->data, token->esc, token->esc2, 1)); - } + if (token->type == TK_WORD) + node->pattern = 1; return (0); } diff --git a/42sh/src/parser/add_func.c b/42sh/src/parser/add_func.c index c2419d00..a90a4b39 100644 --- a/42sh/src/parser/add_func.c +++ b/42sh/src/parser/add_func.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/24 23:43:07 by ariard #+# #+# */ -/* Updated: 2017/03/13 19:27:58 by ariard ### ########.fr */ +/* Updated: 2017/03/15 00:26:57 by ariard ### ########.fr */ /* */ /* ************************************************************************** */