From a011f94bcbaf38fb762cba7866c3381f2b066bc7 Mon Sep 17 00:00:00 2001 From: "ariard@student.42.fr" Date: Tue, 24 Jan 2017 20:17:46 +0100 Subject: [PATCH] comment same bug that with a space in input execution, tree empty cause bug execution --- 42sh/file1 | 1 + 42sh/includes/exec.h | 2 +- 42sh/includes/lexer.h | 4 +++- 42sh/includes/minishell.h | 2 +- 42sh/includes/parser.h | 2 +- 42sh/sample/2lines.sh | 3 +-- 42sh/sample/4lines.sh | 4 ++++ 42sh/sample/comment.sh | 2 ++ 42sh/sample/condition.sh | 6 ------ 42sh/sample/exitzero.sh | 2 -- 42sh/sample/file1 | 1 + 42sh/src/exec/ft_exec.c | 2 +- 42sh/src/lexer/ft_tokenize.c | 3 ++- 42sh/src/lexer/get_lexer_state.c | 2 +- 42sh/src/lexer/lexer_comment.c | 24 ++++++++++++++++++++++++ 42sh/src/lexer/lexer_default.c | 4 +++- 42sh/src/lexer/lexer_delim.c | 6 ++++-- 42sh/src/lexer/lexer_great.c | 2 +- 42sh/src/lexer/lexer_greatand.c | 2 +- 42sh/src/lexer/lexer_newline.c | 2 +- 42sh/src/lexer/lexer_word.c | 2 +- 42sh/src/main/read_script.c | 2 +- 42sh/src/main/shell_script.c | 11 ++++++----- 42sh/src/parser/ft_parse.c | 2 +- 42sh/src/parser/parse_separator.c | 2 +- 25 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 42sh/file1 create mode 100644 42sh/sample/4lines.sh create mode 100644 42sh/sample/comment.sh delete mode 100644 42sh/sample/condition.sh delete mode 100644 42sh/sample/exitzero.sh create mode 100644 42sh/sample/file1 create mode 100644 42sh/src/lexer/lexer_comment.c diff --git a/42sh/file1 b/42sh/file1 new file mode 100644 index 00000000..1acf61e2 --- /dev/null +++ b/42sh/file1 @@ -0,0 +1 @@ + 0 diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 279d7450..ed52dfeb 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/01/22 22:42:30 by ariard ### ########.fr */ +/* Updated: 2017/01/24 20:01:46 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 71f65dc3..909a1a8c 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 16:21:11 by ariard ### ########.fr */ +/* Updated: 2017/01/24 20:13:54 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,6 +62,7 @@ enum e_lexstate BACKSLASH, VAR, SPECIAL, + COMMENT, }; struct s_token @@ -94,6 +95,7 @@ int ft_is_delim(char c); t_lexstate get_lexer_state(char *str); int lexer_default(t_list **alst, char *str); +int lexer_comment(t_list **alst, char *str); int lexer_newline(t_list **alst, char *str); int lexer_delim(t_list **alst, char *str); int lexer_sep(t_list **alst, char *str); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index c331f5a7..2f50212a 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/24 16:35:43 by ariard ### ########.fr */ +/* Updated: 2017/01/24 20:04:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index c138504a..dff89697 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/24 16:46:36 by ariard ### ########.fr */ +/* Updated: 2017/01/24 20:08:01 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/sample/2lines.sh b/42sh/sample/2lines.sh index 8665b070..038db839 100644 --- a/42sh/sample/2lines.sh +++ b/42sh/sample/2lines.sh @@ -1,3 +1,2 @@ -ls | wc -l > file1 -cd ; ls +ls | wc -l > file1 ; cd pwd ; echo "hello world" diff --git a/42sh/sample/4lines.sh b/42sh/sample/4lines.sh new file mode 100644 index 00000000..999b725d --- /dev/null +++ b/42sh/sample/4lines.sh @@ -0,0 +1,4 @@ +ls | wc -l > file1 +cd ; pwd | wc -l ; echo "bonjour" +ls -l > file2 +pwd ; echo "hello world" diff --git a/42sh/sample/comment.sh b/42sh/sample/comment.sh new file mode 100644 index 00000000..2d580555 --- /dev/null +++ b/42sh/sample/comment.sh @@ -0,0 +1,2 @@ +ls #ls +pwd diff --git a/42sh/sample/condition.sh b/42sh/sample/condition.sh deleted file mode 100644 index 906b4a27..00000000 --- a/42sh/sample/condition.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -if [ sh exitzero.sh ] -then - echo "hello world" -fi diff --git a/42sh/sample/exitzero.sh b/42sh/sample/exitzero.sh deleted file mode 100644 index 691c85b1..00000000 --- a/42sh/sample/exitzero.sh +++ /dev/null @@ -1,2 +0,0 @@ -ls -exit (0) diff --git a/42sh/sample/file1 b/42sh/sample/file1 new file mode 100644 index 00000000..1acf61e2 --- /dev/null +++ b/42sh/sample/file1 @@ -0,0 +1 @@ + 0 diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 462674d4..27a8fdff 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/01/22 22:08:22 by ariard ### ########.fr */ +/* Updated: 2017/01/24 20:03:01 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index 6bf1475b..5135db09 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 17:54:08 by ariard ### ########.fr */ +/* Updated: 2017/01/24 20:13:42 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ int (*g_lexer[])(t_list **alst, char *str) = &lexer_backslash, &lexer_var, &lexer_special, + &lexer_comment, }; int ft_is_delim(char c) diff --git a/42sh/src/lexer/get_lexer_state.c b/42sh/src/lexer/get_lexer_state.c index 63833382..aea725da 100644 --- a/42sh/src/lexer/get_lexer_state.c +++ b/42sh/src/lexer/get_lexer_state.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 13:45:46 by jhalford #+# #+# */ -/* Updated: 2017/01/24 00:55:05 by ariard ### ########.fr */ +/* Updated: 2017/01/24 19:09:26 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_comment.c b/42sh/src/lexer/lexer_comment.c new file mode 100644 index 00000000..613b9acb --- /dev/null +++ b/42sh/src/lexer/lexer_comment.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_comment.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/24 18:22:35 by ariard #+# #+# */ +/* Updated: 2017/01/24 20:07:14 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +int lexer_comment(t_list **alst, char *str) +{ + t_token *token; + + (void)str; + token = (*alst)->content; + if (!token->type) + ft_lstdel(alst, &token_free); + return (0); +} diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index 4d9d18fc..28bb9821 100644 --- a/42sh/src/lexer/lexer_default.c +++ b/42sh/src/lexer/lexer_default.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ -/* Updated: 2017/01/24 01:01:43 by ariard ### ########.fr */ +/* Updated: 2017/01/24 19:22:58 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,8 @@ int lexer_default(t_list **alst, char *str) return (ft_tokenize(alst, str, GREAT)); else if (*str == '<') return (ft_tokenize(alst, str, LESS)); + else if (*str == '#') + return (ft_tokenize(alst, str, COMMENT)); else if (ft_isdigit(*str)) state = NUMBER; else diff --git a/42sh/src/lexer/lexer_delim.c b/42sh/src/lexer/lexer_delim.c index 62507813..2ad6374b 100644 --- a/42sh/src/lexer/lexer_delim.c +++ b/42sh/src/lexer/lexer_delim.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:58:44 by jhalford #+# #+# */ -/* Updated: 2017/01/24 00:32:55 by ariard ### ########.fr */ +/* Updated: 2017/01/24 19:48:31 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,9 @@ int lexer_delim(t_list **alst, char *str) token = (*alst)->content; while (ft_is_delim(*str)) str++; - if (token->type) + if (*(str + 1) == '#') + return (ft_tokenize(alst, str + 1, COMMENT)); + else if (token->type) return (ft_tokenize(&(*alst)->next, str, DEFAULT)); else return (ft_tokenize(alst, str, DEFAULT)); diff --git a/42sh/src/lexer/lexer_great.c b/42sh/src/lexer/lexer_great.c index 81f6a4f2..18d65b20 100644 --- a/42sh/src/lexer/lexer_great.c +++ b/42sh/src/lexer/lexer_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:35 by jhalford #+# #+# */ -/* Updated: 2017/01/23 22:51:25 by ariard ### ########.fr */ +/* Updated: 2017/01/24 19:06:00 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_greatand.c b/42sh/src/lexer/lexer_greatand.c index 7a32ef24..f1f20e8e 100644 --- a/42sh/src/lexer/lexer_greatand.c +++ b/42sh/src/lexer/lexer_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:56:58 by jhalford #+# #+# */ -/* Updated: 2016/12/03 11:57:09 by jhalford ### ########.fr */ +/* Updated: 2017/01/24 19:02:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_newline.c b/42sh/src/lexer/lexer_newline.c index 3e362cd4..69f178f6 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 17:54:12 by ariard ### ########.fr */ +/* Updated: 2017/01/24 20:13:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index 5d52d1ee..836f9f0a 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 17:33:09 by ariard ### ########.fr */ +/* Updated: 2017/01/24 18:50:42 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/read_script.c b/42sh/src/main/read_script.c index 4341b747..3b4cad36 100644 --- a/42sh/src/main/read_script.c +++ b/42sh/src/main/read_script.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/21 22:49:31 by ariard #+# #+# */ -/* Updated: 2017/01/24 00:20:01 by ariard ### ########.fr */ +/* Updated: 2017/01/24 18:50:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_script.c b/42sh/src/main/shell_script.c index e2daa8b1..34f5f04d 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 18:06:42 by ariard ### ########.fr */ +/* Updated: 2017/01/24 20:14:52 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int shell_script() { if (ft_tokenize(&token, list_tmp->content , DEFAULT)) return (1); - if (!token) + if (!token && !list_tmp->next) return (0); if (ft_post_tokenize(&token, list_tmp->content)) return (1); @@ -38,11 +38,12 @@ int shell_script() } DG("after post_tokenize"); token_print(head); + list_tmp = NULL; while (head) { if (ft_parse(&ast, &head)) return (1); - ft_lsteadd(&list_tmp, ft_lstnew(ast, sizeof (*ast))); + ft_lsteadd(&list_tmp, ft_lstnew(ast, sizeof(*ast))); ast = NULL; ft_lst_delif(&head, head->content, &ft_addrcmp, &token_free); } @@ -51,9 +52,9 @@ int shell_script() { btree_print(STDBUG, tmp2->content, &ft_putast); tmp2 = tmp2->next; - } + } while (list_tmp) - { + { ast = list_tmp->content; if (ft_exec(&ast)) return (1); diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index ca3e19d8..823c9331 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/24 18:06:40 by ariard ### ########.fr */ +/* Updated: 2017/01/24 20:12:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_separator.c b/42sh/src/parser/parse_separator.c index 3213006c..91d3fbc4 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/24 17:54:27 by ariard ### ########.fr */ +/* Updated: 2017/01/24 19:42:12 by ariard ### ########.fr */ /* */ /* ************************************************************************** */