From ca8fb52d3457352ff29023ba29c2f4fba98d895a Mon Sep 17 00:00:00 2001 From: "ariard@student.42.fr" Date: Tue, 24 Jan 2017 18:08:32 +0100 Subject: [PATCH] add lst_find_until + exec script multilines ok --- 42sh/.gitignore | 2 ++ 42sh/file1 | 1 + 42sh/includes/lexer.h | 2 +- 42sh/includes/minishell.h | 2 +- 42sh/includes/parser.h | 3 ++- 42sh/includes/types.h | 2 +- 42sh/sample/2lines.sh | 5 ++-- 42sh/sample/condition.sh | 6 +++++ 42sh/sample/exitzero.sh | 2 ++ 42sh/src/lexer/ft_tokenize.c | 2 +- 42sh/src/lexer/lexer_newline.c | 2 +- 42sh/src/lexer/lexer_word.c | 2 +- 42sh/src/lexer/token_cmp_type.c | 2 +- 42sh/src/main/shell_script.c | 42 +++++++++++++++++++++---------- 42sh/src/parser/ft_parse.c | 6 +++-- 42sh/src/parser/parse_great.c | 2 +- 42sh/src/parser/parse_separator.c | 2 +- 42sh/src/parser/parse_word.c | 2 +- 18 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 42sh/file1 create mode 100644 42sh/sample/condition.sh create mode 100644 42sh/sample/exitzero.sh diff --git a/42sh/.gitignore b/42sh/.gitignore index 9f1a7f9b..2c239b7d 100644 --- a/42sh/.gitignore +++ b/42sh/.gitignore @@ -6,3 +6,5 @@ debug *.dSYM .script.sh.swp *.swp +*.swo +*.o diff --git a/42sh/file1 b/42sh/file1 new file mode 100644 index 00000000..108ac6c1 --- /dev/null +++ b/42sh/file1 @@ -0,0 +1 @@ + 11 diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 3263c231..71f65dc3 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/01/24 00:45:06 by ariard ### ########.fr */ +/* Updated: 2017/01/24 16:21:11 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index d211ec07..c331f5a7 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/01/23 23:31:30 by ariard ### ########.fr */ +/* Updated: 2017/01/24 16:35:43 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 9b7025b9..c138504a 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/01/20 18:26:51 by ariard ### ########.fr */ +/* Updated: 2017/01/24 16:46:36 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,5 +66,6 @@ int parse_lessand(t_btree **ast, t_list **start, t_list **lst); int parse_greatand(t_btree **ast, t_list **start, t_list **lst); int parse_word(t_btree **ast, t_list **start, t_list **lst); int parse_subshell(t_btree **ast, t_list **start, t_list **lst); +int parse_newline(t_btree **ast, t_list **start, t_list **lst); #endif diff --git a/42sh/includes/types.h b/42sh/includes/types.h index e5168947..eaf9b3d9 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/01/19 21:52:47 by ariard ### ########.fr */ +/* Updated: 2017/01/24 16:01:58 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/sample/2lines.sh b/42sh/sample/2lines.sh index 7ef6d678..8665b070 100644 --- a/42sh/sample/2lines.sh +++ b/42sh/sample/2lines.sh @@ -1,2 +1,3 @@ -ls pwd -yolo +ls | wc -l > file1 +cd ; ls +pwd ; echo "hello world" diff --git a/42sh/sample/condition.sh b/42sh/sample/condition.sh new file mode 100644 index 00000000..906b4a27 --- /dev/null +++ b/42sh/sample/condition.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +if [ sh exitzero.sh ] +then + echo "hello world" +fi diff --git a/42sh/sample/exitzero.sh b/42sh/sample/exitzero.sh new file mode 100644 index 00000000..691c85b1 --- /dev/null +++ b/42sh/sample/exitzero.sh @@ -0,0 +1,2 @@ +ls +exit (0) diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index d99d4652..6bf1475b 100644 --- a/42sh/src/lexer/ft_tokenize.c +++ b/42sh/src/lexer/ft_tokenize.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */ -/* Updated: 2017/01/24 01:25:40 by ariard ### ########.fr */ +/* Updated: 2017/01/24 17:54:08 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_newline.c b/42sh/src/lexer/lexer_newline.c index de8cef47..3e362cd4 100644 --- a/42sh/src/lexer/lexer_newline.c +++ b/42sh/src/lexer/lexer_newline.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/23 23:19:46 by ariard #+# #+# */ -/* Updated: 2017/01/24 01:25:21 by ariard ### ########.fr */ +/* Updated: 2017/01/24 17:54:12 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index 38e50350..5d52d1ee 100644 --- a/42sh/src/lexer/lexer_word.c +++ b/42sh/src/lexer/lexer_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */ -/* Updated: 2017/01/24 01:02:50 by ariard ### ########.fr */ +/* Updated: 2017/01/24 17:33:09 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/token_cmp_type.c b/42sh/src/lexer/token_cmp_type.c index 69c90510..23ad5562 100644 --- a/42sh/src/lexer/token_cmp_type.c +++ b/42sh/src/lexer/token_cmp_type.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:08:55 by jhalford #+# #+# */ -/* Updated: 2017/01/19 23:12:09 by ariard ### ########.fr */ +/* Updated: 2017/01/24 16:02:48 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_script.c b/42sh/src/main/shell_script.c index f0e9dd4a..e2daa8b1 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/01/24 01:25:38 by ariard ### ########.fr */ +/* Updated: 2017/01/24 18:06:42 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,31 +17,47 @@ int shell_script() t_list *token; t_list *head; t_btree *ast; - t_list *tmp; + t_list *list_tmp; + t_list *tmp2; token = NULL; head = NULL; ast = NULL; - tmp = data_singleton()->script.queue; - while (tmp) + list_tmp = data_singleton()->script.queue; + while (list_tmp) { - if (ft_tokenize(&token, tmp->content , DEFAULT)) + if (ft_tokenize(&token, list_tmp->content , DEFAULT)) return (1); if (!token) return (0); - if (ft_post_tokenize(&token, tmp->content)) + if (ft_post_tokenize(&token, list_tmp->content)) return (1); - tmp = tmp->next; + list_tmp = list_tmp->next; ft_lst_merge(&head, token); token = NULL; } DG("after post_tokenize"); token_print(head); - return (0); - if (ft_parse(&ast, &token)) - return (1); - btree_print(STDBUG, ast, &ft_putast); - if (ft_exec(&ast)) - return (1); + while (head) + { + if (ft_parse(&ast, &head)) + return (1); + ft_lsteadd(&list_tmp, ft_lstnew(ast, sizeof (*ast))); + ast = NULL; + ft_lst_delif(&head, head->content, &ft_addrcmp, &token_free); + } + tmp2 = list_tmp; + while (tmp2) + { + btree_print(STDBUG, tmp2->content, &ft_putast); + tmp2 = tmp2->next; + } + while (list_tmp) + { + ast = list_tmp->content; + if (ft_exec(&ast)) + return (1); + list_tmp = list_tmp->next; + } return (0); } diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index cfd0faf7..ca3e19d8 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/01/23 23:15:05 by ariard ### ########.fr */ +/* Updated: 2017/01/24 18:06:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ t_parser g_parser[] = {TK_GREATAND, &parse_greatand}, {TK_SUBSHELL, &parse_subshell}, {TK_WORD, &parse_word}, + {TK_NEWLINE, NULL}, {0, 0}, }; @@ -45,7 +46,8 @@ int ft_parse(t_btree **ast, t_list **start) } while (g_parser[i].type) { - if ((lst = ft_lst_find(*start, &g_parser[i].type, &token_cmp_type))) + if ((lst = ft_lst_find_until(*start, &g_parser[i].type, + &g_parser[12].type, &token_cmp_type))) { if (g_parser[i].f) (*g_parser[i].f)(ast, start, &lst); diff --git a/42sh/src/parser/parse_great.c b/42sh/src/parser/parse_great.c index 6350fd9f..5155b189 100644 --- a/42sh/src/parser/parse_great.c +++ b/42sh/src/parser/parse_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/01/20 16:23:53 by ariard ### ########.fr */ +/* Updated: 2017/01/24 16:06:00 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_separator.c b/42sh/src/parser/parse_separator.c index 65a506d2..3213006c 100644 --- a/42sh/src/parser/parse_separator.c +++ b/42sh/src/parser/parse_separator.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */ -/* Updated: 2017/01/23 23:17:04 by ariard ### ########.fr */ +/* Updated: 2017/01/24 17:54:27 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_word.c b/42sh/src/parser/parse_word.c index 28ccb098..de8cd8da 100644 --- a/42sh/src/parser/parse_word.c +++ b/42sh/src/parser/parse_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/01/20 00:07:56 by ariard ### ########.fr */ +/* Updated: 2017/01/24 17:54:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */