From 7708a456a90ae5861e2f7ead2701a9d3f821e39c Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Sat, 11 Mar 2017 15:21:11 +0100 Subject: [PATCH] redir ok if + case --- 42sh/Makefile | 1 + 42sh/includes/minishell.h | 2 +- 42sh/includes/parser.h | 2 + 42sh/includes/types.h | 2 +- 42sh/sample/while/while03.sh | 5 ++ 42sh/src/parser/add_cmd.c | 6 +-- 42sh/src/parser/add_redir.c | 7 ++- 42sh/src/parser/add_redir_condition.c | 69 +++++++++++++++++++++++++++ 8 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 42sh/sample/while/while03.sh create mode 100644 42sh/src/parser/add_redir_condition.c diff --git a/42sh/Makefile b/42sh/Makefile index f2327123..8ebd44b6 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -280,6 +280,7 @@ parser/add_loop.c\ parser/add_math.c\ parser/add_number.c\ parser/add_redir.c\ +parser/add_redir_condition.c\ parser/add_sep.c\ parser/add_subshell.c\ parser/add_var.c\ diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index ec5f0368..6627e316 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/03/09 14:33:59 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 19:59:56 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 1e9aa6d8..8fa80d31 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -110,6 +110,7 @@ int add_condition_sep(t_btree **ast, t_list **lst); int add_branch(t_btree **ast, t_list **lst); int add_redir_word(t_btree **ast, t_list **lst); int add_redir_type(t_btree **ast, t_list **lst); +int add_redir_condition(t_btree **ast, t_list **lst); int add_case_cmd(t_btree **ast, t_list **lst); int add_case_sep(t_btree **ast, t_list **lst); int add_pattern(t_btree **ast, t_list **lst); @@ -139,6 +140,7 @@ int isfunc_name(t_btree **ast, t_list **lst); int isdir(t_btree **ast); int iscondition(t_btree **ast, t_list **list); int isdir_sep(t_btree **ast, t_list **list); +int isdir_condition(t_btree **ast, t_list **list); int isdir_word(t_btree **ast, t_list **list); int isvar(t_btree **ast, t_list **list); int isnull(t_btree **ast, t_list **list); diff --git a/42sh/includes/types.h b/42sh/includes/types.h index faac23be..72955d1d 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/03/09 17:58:18 by ariard ### ########.fr */ +/* Updated: 2017/03/10 20:14:52 by ariard ### ########.fr */ /* Updated: 2017/03/07 18:35:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/sample/while/while03.sh b/42sh/sample/while/while03.sh new file mode 100644 index 00000000..599bc530 --- /dev/null +++ b/42sh/sample/while/while03.sh @@ -0,0 +1,5 @@ +ls +for HELLLO in yolo ; +do + ls +done > file1 diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index d659f594..84e3968c 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/10 17:55:08 by ariard ### ########.fr */ +/* Updated: 2017/03/11 15:06:03 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ t_distrostree g_distrostree[] = { {&superflous_token, &add_null}, + {&isdir_condition, &add_redir_condition}, {&isdir_sep, &add_redir_type}, {&isdir_word, &add_redir_word}, {&isvar, &add_var}, @@ -72,7 +73,7 @@ int add_cmd(t_btree **ast, t_list **lst) int i; i = -1; - while (++i < 18) + while (++i < 19) if (g_distrostree[i].test(ast, lst) == 1) { DG("add : %d", i); @@ -84,7 +85,6 @@ int add_cmd(t_btree **ast, t_list **lst) return (add_cmd(&(*ast)->right, lst)); token = (*lst)->content; node = (*ast)->item; - DG("add cmd"); if (token->type == TK_IF) add_if(ast, lst); else if (token->type != TK_WORD) diff --git a/42sh/src/parser/add_redir.c b/42sh/src/parser/add_redir.c index e28d0837..cb8de962 100644 --- a/42sh/src/parser/add_redir.c +++ b/42sh/src/parser/add_redir.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 16:39:05 by ariard #+# #+# */ -/* Updated: 2017/03/10 19:43:10 by ariard ### ########.fr */ +/* Updated: 2017/03/11 14:51:00 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,10 @@ int isdir_sep(t_btree **ast, t_list **list) if (token->type == TK_LESS || token->type == TK_GREAT || token->type == TK_GREATAND || token->type == TK_LESSAND || token->type == TK_DLESS || token->type == TK_DGREAT) - return (1); + { + DG("add dir sep"); + return (1); + } } return (0); } diff --git a/42sh/src/parser/add_redir_condition.c b/42sh/src/parser/add_redir_condition.c new file mode 100644 index 00000000..f5782a34 --- /dev/null +++ b/42sh/src/parser/add_redir_condition.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_redir_condition.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/11 14:54:18 by ariard #+# #+# */ +/* Updated: 2017/03/11 15:17:58 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static int isdir_sep_condition(t_btree **ast, t_list **lst) +{ + t_token *token; + t_astnode *node; + + token = (*lst)->content; + if (*ast) + { + node = (*ast)->item; + if (node->type == TK_IF || node->type == TK_CASE) + return (1); + } + return (0); +} + +static int isdir_word_condition(t_btree **ast, t_list **lst) +{ + t_token *token; + t_astnode *node; + + token = (*lst)->content; + if (*ast) + { + node = (*ast)->item; + if (node->type == REDIR) + return (1); + } + return (0); +} + +int isdir_condition(t_btree **ast, t_list **lst) +{ + t_token *token; + t_astnode *node; + + token = (*lst)->content; + if (*ast) + { + node = (*ast)->item; + if ((token->type == TK_LESS || token->type == TK_GREAT + || token->type == TK_GREATAND || token->type == TK_LESSAND + || token->type == TK_DLESS || token->type == TK_DGREAT) + && isdir_sep_condition(&(*ast)->left, lst) == 1) + return (1); + if (token->type == TK_WORD + && isdir_word_condition(&(*ast)->left, lst) == 1) + return (1); + } + return (0); +} + +int add_redir_condition(t_btree **ast, t_list **lst) +{ + return (add_cmd(&(*ast)->left, lst)); +}