From bed9f1d6c245bcf09a0bb15b23dd8f1421b09469 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 16 Mar 2017 15:04:21 +0100 Subject: [PATCH] mean cleanup --- 42sh/includes/minishell.h | 4 +- 42sh/src/builtin/builtin_math.c | 2 +- 42sh/src/exec/ft_exec.c | 2 +- 42sh/src/lexer/get_reserved_words.c | 2 +- 42sh/src/lexer/insert_newline.c | 2 +- 42sh/src/lexer/lexer_end.c | 2 +- 42sh/src/lexer/lexer_lex.c | 5 +- 42sh/src/lexer/lexer_word.c | 2 +- 42sh/src/line-editing/init_line.c | 6 +- 42sh/src/line-editing/readline.c | 8 +- 42sh/src/main/main.c | 118 ++++++++++++++++------------ 42sh/src/parser/ft_parse.c | 2 +- 12 files changed, 87 insertions(+), 68 deletions(-) diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 3be6d97a..c1d8fa3f 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/16 14:15:02 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 15:04:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,7 @@ struct s_data { t_flag opts; char **env; + char **local_var; int argc; char **argv; t_line line; @@ -43,7 +44,6 @@ struct s_data t_exec exec; t_jobc jobc; t_list *heredoc_queue; - char **local_var; t_list *lst_func; }; diff --git a/42sh/src/builtin/builtin_math.c b/42sh/src/builtin/builtin_math.c index f854d709..c37771c6 100644 --- a/42sh/src/builtin/builtin_math.c +++ b/42sh/src/builtin/builtin_math.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 10:58:49 by ariard #+# #+# */ -/* Updated: 2017/03/16 14:18:05 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 14:24:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 1f4a97ae..c0dc425a 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/03/15 02:04:38 by ariard ### ########.fr */ +/* Updated: 2017/03/16 14:56:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/get_reserved_words.c b/42sh/src/lexer/get_reserved_words.c index dc63c869..1340a49e 100644 --- a/42sh/src/lexer/get_reserved_words.c +++ b/42sh/src/lexer/get_reserved_words.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 00:07:05 by ariard #+# #+# */ -/* Updated: 2017/03/13 16:55:08 by ariard ### ########.fr */ +/* Updated: 2017/03/16 14:41:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/insert_newline.c b/42sh/src/lexer/insert_newline.c index 47e7d1bf..d91048e4 100644 --- a/42sh/src/lexer/insert_newline.c +++ b/42sh/src/lexer/insert_newline.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/21 21:05:23 by ariard #+# #+# */ -/* Updated: 2017/03/15 16:31:49 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 14:41:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_end.c b/42sh/src/lexer/lexer_end.c index c06e06d4..c502b53c 100644 --- a/42sh/src/lexer/lexer_end.c +++ b/42sh/src/lexer/lexer_end.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 16:58:24 by jhalford #+# #+# */ -/* Updated: 2017/03/09 14:53:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 14:44:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_lex.c b/42sh/src/lexer/lexer_lex.c index 14faa960..39ab2483 100644 --- a/42sh/src/lexer/lexer_lex.c +++ b/42sh/src/lexer/lexer_lex.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:08:51 by jhalford #+# #+# */ -/* Updated: 2017/03/13 15:41:30 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 14:45:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,7 +58,8 @@ int lexer_lex(t_list **alst, t_lexer *lexer) if (!*alst) { token = token_init(); - *alst = ft_lstnew(token, sizeof(*token)); + if (!(*alst = ft_lstnew(token, sizeof(*token)))) + return (-1); } return ((*g_lexer[lexer->state])(alst, lexer)); } diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index 1ac6cad3..8fc1ac99 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/03/11 20:46:32 by ariard ### ########.fr */ +/* Updated: 2017/03/16 14:44:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/init_line.c b/42sh/src/line-editing/init_line.c index f25e1df6..489d3286 100644 --- a/42sh/src/line-editing/init_line.c +++ b/42sh/src/line-editing/init_line.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 17:34:44 by gwojda #+# #+# */ -/* Updated: 2017/03/16 10:37:03 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 14:58:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ void ft_init_line(void) data_singleton()->line.opt = 0; } -void readline_init(int prompt) +void readline_init(int has_prompt) { static int beg = 0; @@ -40,6 +40,6 @@ void readline_init(int prompt) ft_strdel(&STR); data_singleton()->line.list_cur = data_singleton()->line.list_beg; POS = 0; - prompt ? ft_putstr("> ") : ft_prompt(); + has_prompt ? ft_prompt() : ft_putstr("> "); data_singleton()->line.is_prompt = prompt ? 0 : 1; } diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index 87ff70b7..f2227e43 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,24 +6,24 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/03/16 14:06:39 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 14:58:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int readline(int prompt, char **input) +int readline(int has_prompt, char **input) { int ret; if (!SH_IS_INTERACTIVE(data_singleton()->opts)) return ((ret = get_next_line(STDIN, input)) >= 0 ? !ret : ret); - readline_init(prompt); + readline_init(has_prompt); *input = ft_read_stdin(); if (STR) ft_current_str(STR, POS); ft_putchar('\n'); - if (!prompt) + if (has_prompt) *input = ft_history_parsing(); if (tcsetattr(0, TCSANOW, ft_save_termios(0)) == -1) return (-1); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 78e4ade1..837914be 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,70 +6,88 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/16 14:10:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 15:02:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static int handle_instruction( t_list **token, - t_btree **ast, - t_lexer lexer, - t_parser parser) +static int do_lexer_routine(t_list **token, t_lexer *lexer) { t_list *ltoken; - char *str; - int ret; + + if (lexer->state == HEREDOC) + { + ft_strappend(&lexer->str, (char[]){'\n', 0}); + lexer->pos++; + } + ft_strappend(&lexer->str, str); + if (get_lexer_stack(lexer) == BACKSLASH) + pop(&lexer->stack); + ltoken = ft_lstlast(*token); + if (lexer_lex(token ? <oken : token, lexer) < 1) + exit(1); + if (get_lexer_stack(lexer) > 2) + return (1); + lexer->state = DEFAULT; + return (0); +} + +static int do_parser_routine(t_list **token, t_lexer *lexer, t_parser *parser) +{ + if (get_reserved_words(token)) + return (1); + token_print(*token); + if (insert_newline(token)) + return (1); + ft_parse(ast, token, &parser); + if ((lexer->state = data_singleton()->heredoc_queue ? HEREDOC : DEFAULT)) + return (0); + if (parse->state == ERROR) + { + error_syntax(token, &parser, ast); + return (1); + } + else if (parser->state == SUCCESS) + return (1); + return (0); +} + +static int do_readline_routine(t_list **token, t_lexer *lexer, t_parser *parser) +{ + int ret; + int prompt; + char *str; + + has_prompt = !(get_lexer_stack(lexer) || parser->state == UNDEFINED || lexer->state == HEREDOC); + ret = readline(has_prompt, &str); + { + if (ret == -1) + return (-1); + return (parser.state == UNDEFINED ? error_eof(token, + &parser, ast) : 1); + } +} + +static int handle_instruction( t_list **token, + t_btree **ast, + t_lexer *lexer, + t_parser *parser) +{ while (1) { - if ((ret = readline(get_lexer_stack(lexer) || - parser.state == UNDEFINED || lexer.state == HEREDOC, &str))) - { - if (ret == -1) - return (-1); - return (parser.state == UNDEFINED ? error_eof(token, - &parser, ast) : 1); - } - if (lexer.state == HEREDOC) - { - ft_strappend(&lexer.str, (char[]){'\n', 0}); - lexer.pos++; - } - ft_strappend(&lexer.str, str); - if (get_lexer_stack(lexer) == BACKSLASH) - pop(&lexer.stack); - ltoken = ft_lstlast(*token); - if (lexer_lex(token ? <oken : token, &lexer)) - return (1); - if (get_lexer_stack(lexer) > 2) + if (do_readline_routine(lexer, parser) > 0) + if (do_lexer_routine(token, lexer) > 0) continue ; - lexer.state = DEFAULT; - if (get_reserved_words(token)) - return (1); - token_print(*token); - if (insert_newline(token)) - return (1); - if (ft_parse(ast, token, &parser)) - continue ; - if (parser.state == ERROR) - error_syntax(token, &parser, ast); - lexer.state = data_singleton()->heredoc_queue ? HEREDOC : 0; - if (lexer.state) - continue; - else if (parser.state == SUCCESS) + if (do_parser_routine(token, lexer, parser) > 0) break ; - else if (parser.state == ERROR && SH_IS_INTERACTIVE(data_singleton()->opts)) - { - ft_add_str_in_history(lexer.str); - return (error_syntax(token, &parser, ast)); - } } /* btree_print(STDBUG, ast, &ft_putast); */ - if (ft_exec(ast)) - return (2); - if (SH_IS_INTERACTIVE(data_singleton()->opts) && *lexer.str) - ft_add_str_in_history(lexer.str); + if (parser->state == SUCCESS && ft_exec(ast) < 0) + exit(1); + if (SH_IS_INTERACTIVE(data_singleton()->opts) && *lexer->str) + ft_add_str_in_history(lexer->str); return (0); } diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 079a4acd..d65aa8b8 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 16:17:38 by ariard #+# #+# */ -/* Updated: 2017/03/15 19:59:19 by ariard ### ########.fr */ +/* Updated: 2017/03/16 14:32:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */