From 490f81ee2363ba8ecd68e7b14b67368b528f3da8 Mon Sep 17 00:00:00 2001 From: "ariard@student.42.fr" Date: Mon, 6 Feb 2017 19:48:17 +0100 Subject: [PATCH] exec if + elif + else ok to-do:syntax error --- 42sh/Makefile | 6 ++-- 42sh/includes/exec.h | 6 ++-- 42sh/includes/minishell.h | 3 +- 42sh/includes/parser.h | 3 +- 42sh/sample/if.sh | 14 ++++++--- 42sh/src/exec/exec_elif.c | 28 ++++++++++++++++++ 42sh/src/exec/exec_else.c | 20 +++++++++++++ 42sh/src/exec/exec_if.c | 21 ++++++++++++++ 42sh/src/exec/exec_semi.c | 2 +- 42sh/src/exec/exec_while.c | 2 +- 42sh/src/exec/ft_exec.c | 5 +++- 42sh/src/main/ft_putast.c | 4 ++- 42sh/src/main/main.c | 2 +- 42sh/src/main/shell_script.c | 14 ++++----- 42sh/src/parser/ft_parse.c | 4 +-- 42sh/src/parser/get_instruction.c | 2 +- 42sh/src/parser/get_sub_instruction.c | 4 ++- 42sh/src/parser/parse.c | 2 +- 42sh/src/parser/parse_elif.c | 6 ++-- 42sh/src/parser/parse_else.c | 42 +++++++++++++++++++++++++++ 42sh/src/parser/parse_if.c | 4 +-- 42sh/src/parser/parse_while.c | 1 - 22 files changed, 162 insertions(+), 33 deletions(-) create mode 100644 42sh/src/exec/exec_elif.c create mode 100644 42sh/src/exec/exec_else.c create mode 100644 42sh/src/exec/exec_if.c create mode 100644 42sh/src/parser/parse_else.c diff --git a/42sh/Makefile b/42sh/Makefile index 41613a82..00263478 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -52,11 +52,12 @@ exec/exec_or_if.c\ exec/exec_pipe.c\ exec/exec_semi.c\ exec/exec_while.c\ +exec/exec_if.c\ +exec/exec_else.c\ +exec/exec_elif.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_setexec.c\ @@ -183,6 +184,7 @@ parser/parse_subshell.c\ parser/parse_while.c\ parser/parse_if.c\ parser/parse_elif.c\ +parser/parse_else.c\ parser/parse_word.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index f15c74f8..4e4279a3 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/02/03 15:09:26 by ariard ### ########.fr */ +/* Updated: 2017/02/06 18:59:24 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -89,7 +89,9 @@ int exec_dgreat(t_btree **ast); int exec_command(t_btree **ast); int exec_while(t_btree **ast); -int exec_list(t_btree **ast); +int exec_if(t_btree **ast); +int exec_elif(t_btree **ast); +int exec_else(t_btree **ast); int exec_default(t_btree **ast); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 630b5b70..f5831140 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/02/03 14:42:54 by ariard ### ########.fr */ +/* Updated: 2017/02/06 18:33:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,7 @@ struct s_script { char *buffer; int size; + int lc; }; typedef struct s_script t_script; diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 8e2efa2a..71d07a8d 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/02/06 15:45:33 by ariard ### ########.fr */ +/* Updated: 2017/02/06 19:02:48 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -81,6 +81,7 @@ int parse_if(t_btree **ast, t_list **start, t_list **lst); int parse_do(t_btree **ast, t_list **start, t_list **lst); int parse_done(t_btree **ast, t_list **start, t_list **lst); int parse_elif(t_btree **ast, t_list **start, t_list **lst); +int parse_else(t_btree **ast, t_list **start, t_list **lst); int delete_newline(t_list **start, t_list **lst); int parse_head(t_btree **ast, t_btree **new_ast, t_list **start, t_list **lst); diff --git a/42sh/sample/if.sh b/42sh/sample/if.sh index 7a6a1fb4..cfbccc74 100644 --- a/42sh/sample/if.sh +++ b/42sh/sample/if.sh @@ -1,7 +1,13 @@ -if ls +while ls +do +if ls then echo hello -elif ls -then - echo world +else + while ls + do + echo hello world + pwd + done fi +done diff --git a/42sh/src/exec/exec_elif.c b/42sh/src/exec/exec_elif.c new file mode 100644 index 00000000..c248e434 --- /dev/null +++ b/42sh/src/exec/exec_elif.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_elif.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/06 18:08:53 by ariard #+# #+# */ +/* Updated: 2017/02/06 19:26:00 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int exec_elif(t_btree **ast) +{ + int test; + + (void)ast; + test = 1; + if (test && data_singleton()->script.lc == 0) + { + ft_exec(&(*ast)->right); + data_singleton()->script.lc = 1; + } + // btree_delone(ast, &ast_free); + return (0); +} diff --git a/42sh/src/exec/exec_else.c b/42sh/src/exec/exec_else.c new file mode 100644 index 00000000..f94735c1 --- /dev/null +++ b/42sh/src/exec/exec_else.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_else.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/06 18:55:07 by ariard #+# #+# */ +/* Updated: 2017/02/06 19:13:05 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int exec_else(t_btree **ast) +{ + ft_exec(&(*ast)->right); + //btree_delone(ast, &ast_free); + return (0); +} diff --git a/42sh/src/exec/exec_if.c b/42sh/src/exec/exec_if.c new file mode 100644 index 00000000..82c18ec9 --- /dev/null +++ b/42sh/src/exec/exec_if.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/06 18:07:31 by ariard #+# #+# */ +/* Updated: 2017/02/06 18:55:00 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int exec_if(t_btree **ast) +{ + ft_exec(&(*ast)->right); + data_singleton()->script.lc = 0; +// btree_delone(ast, &ast_free); + return (0); +} diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index 83084619..33d99589 100644 --- a/42sh/src/exec/exec_semi.c +++ b/42sh/src/exec/exec_semi.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ -/* Updated: 2017/02/05 22:19:21 by ariard ### ########.fr */ +/* Updated: 2017/02/06 18:34:38 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_while.c b/42sh/src/exec/exec_while.c index e7baf181..68fa713b 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/02/06 17:05:21 by ariard ### ########.fr */ +/* Updated: 2017/02/06 18:12:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 772792d4..a7070ea0 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/02/06 14:53:27 by ariard ### ########.fr */ +/* Updated: 2017/02/06 18:58:09 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,9 @@ t_execmap g_execmap[] = {TK_GREAT, &exec_great}, {TK_DGREAT, &exec_dgreat}, {TK_WHILE, &exec_while}, + {TK_IF, &exec_if}, + {TK_ELIF, &exec_elif}, + {TK_ELSE, &exec_else}, {TK_COMMAND | TK_SUBSHELL, &exec_command}, {0, 0}, }; diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index 0a2c1b85..8ca640d5 100644 --- a/42sh/src/main/ft_putast.c +++ b/42sh/src/main/ft_putast.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ -/* Updated: 2017/02/06 16:14:40 by ariard ### ########.fr */ +/* Updated: 2017/02/06 19:16:59 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,6 +38,8 @@ char *ft_putast(void *nodein) return ("IF"); else if (node->type == TK_ELIF) return ("ELIF"); + else if (node->type == TK_ELSE) + return ("ELSE"); else if (node->type == TK_NEWLINE) return ("NEW"); else if (node->type == TK_GREAT) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 0f363b1f..c2f55c2e 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/02/03 16:33:52 by ariard ### ########.fr */ +/* Updated: 2017/02/06 18:54:43 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_script.c b/42sh/src/main/shell_script.c index f48168a4..eb2b3d9c 100644 --- a/42sh/src/main/shell_script.c +++ b/42sh/src/main/shell_script.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/22 23:06:34 by ariard #+# #+# */ -/* Updated: 2017/02/06 17:59:20 by ariard ### ########.fr */ +/* Updated: 2017/02/06 19:26:06 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,14 +26,14 @@ int shell_script() DG("after post_tokenize"); token_print(token); -// while (token) -// { + while (token) + { if (parse(&ast, &token)) return (1); - btree_print(STDBUG, ast, &ft_putast); - // if (ft_exec(&ast)) - // return (1); +// btree_print(STDBUG, ast, &ft_putast); + if (ft_exec(&ast)) + return (1); ast = NULL; -// } + } return (0); } diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 943e8d47..de27102c 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ -/* Updated: 2017/02/06 17:11:26 by ariard ### ########.fr */ +/* Updated: 2017/02/06 19:26:04 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ t_parser g_parser[] = { - {TK_WHILE | TK_IF | TK_ELIF | TK_NEWLINE | TK_SEMI, &get_sub_instruction}, + {TK_WHILE | TK_IF | TK_ELIF | TK_NEWLINE | TK_SEMI | TK_ELSE, &get_sub_instruction}, {TK_AND_IF | TK_OR_IF, &parse_separator}, {TK_AMP, &parse_separator}, {TK_PIPE, &parse_separator}, diff --git a/42sh/src/parser/get_instruction.c b/42sh/src/parser/get_instruction.c index a025bcdb..00e7e110 100644 --- a/42sh/src/parser/get_instruction.c +++ b/42sh/src/parser/get_instruction.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 16:56:55 by ariard #+# #+# */ -/* Updated: 2017/02/06 17:17:59 by ariard ### ########.fr */ +/* Updated: 2017/02/06 19:18:17 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/get_sub_instruction.c b/42sh/src/parser/get_sub_instruction.c index c2c5f48f..59519558 100644 --- a/42sh/src/parser/get_sub_instruction.c +++ b/42sh/src/parser/get_sub_instruction.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/05 22:05:26 by ariard #+# #+# */ -/* Updated: 2017/02/06 15:49:31 by ariard ### ########.fr */ +/* Updated: 2017/02/06 19:08:17 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,8 @@ int get_sub_instruction(t_btree **ast, t_list **start, t_list **lst) return (parse_if(ast, start, lst)); else if (token->type == TK_ELIF) return (parse_elif(ast, start, lst)); + else if (token->type == TK_ELSE) + return (parse_else(ast, start, lst)); else if (token->type == TK_SEMI) return (parse_separator(ast, start, lst)); return (0); diff --git a/42sh/src/parser/parse.c b/42sh/src/parser/parse.c index e41d5f8d..b96698de 100644 --- a/42sh/src/parser/parse.c +++ b/42sh/src/parser/parse.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/04 16:52:51 by ariard #+# #+# */ -/* Updated: 2017/02/06 17:42:08 by ariard ### ########.fr */ +/* Updated: 2017/02/06 19:26:02 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_elif.c b/42sh/src/parser/parse_elif.c index 4fdfb249..4867a3b9 100644 --- a/42sh/src/parser/parse_elif.c +++ b/42sh/src/parser/parse_elif.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 14:50:54 by ariard #+# #+# */ -/* Updated: 2017/02/06 17:54:22 by ariard ### ########.fr */ +/* Updated: 2017/02/06 19:22:58 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ static int parse_after_elif(t_btree **ast, t_list **lst) while ((temp = temp->next)) { token = temp->content; - if (nest == 0 && (token->type & TK_ELIF)) + if (nest == 0 && (token->type & (TK_ELIF | TK_ELSE))) break; else if (token->type & TK_IF) nest++; @@ -52,7 +52,7 @@ static int parse_loop(t_btree **ast, t_list **start, t_list **lst) token = (*lst)->content; if (token->type & TK_THEN) nest++; - else if (token->type & TK_ELIF) + else if (token->type & (TK_ELIF | TK_ELSE)) nest--; if (nest == 1 && (token->type & TK_THEN)) break; diff --git a/42sh/src/parser/parse_else.c b/42sh/src/parser/parse_else.c new file mode 100644 index 00000000..b343eb9b --- /dev/null +++ b/42sh/src/parser/parse_else.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_elif.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/06 14:50:54 by ariard #+# #+# */ +/* Updated: 2017/02/06 19:22:56 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static int parse_after_else(t_btree **ast, t_list **lst) +{ + t_list *temp; + + temp = (*lst); + while ((temp = temp->next)); + if (temp) + ft_parse(ast, &temp); + return (0); +} + +static int parse_loop(t_btree **ast, t_list **start, t_list **lst) +{ + delete_newline(start, lst); + ft_parse(&(*ast)->right, lst); + return (0); +} + +int parse_else(t_btree **ast, t_list **start, t_list **lst) +{ + t_btree *new_ast; + + parse_after_else(ast, lst); + parse_head(ast, &new_ast, start, lst); + *start = *lst; + parse_loop(&new_ast, start, lst); + return (0); +} diff --git a/42sh/src/parser/parse_if.c b/42sh/src/parser/parse_if.c index 4af694c4..f1e13014 100644 --- a/42sh/src/parser/parse_if.c +++ b/42sh/src/parser/parse_if.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/05 23:15:23 by ariard #+# #+# */ -/* Updated: 2017/02/06 17:46:01 by ariard ### ########.fr */ +/* Updated: 2017/02/06 19:20:47 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,7 +72,7 @@ static int parse_head_condition(t_btree **ast, int parse_if(t_btree **ast, t_list **start, t_list **lst) { t_btree *new_ast; - + parse_after_condition(ast, start, lst); parse_head_condition(ast, &new_ast, start, lst); return (0); diff --git a/42sh/src/parser/parse_while.c b/42sh/src/parser/parse_while.c index b3c89ccb..23a2038f 100644 --- a/42sh/src/parser/parse_while.c +++ b/42sh/src/parser/parse_while.c @@ -83,7 +83,6 @@ int parse_head(t_btree **ast, del = (*lst); *lst = (*lst)->next; //check that// - del->next = NULL; ft_lst_delif(start, del->content, &ft_addrcmp, &token_free); delete_newline(start, lst); return (0);