From 6efd4c7e1fa3450c9e62c7d330663f0a47f3201d Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 16 Mar 2017 14:20:44 +0100 Subject: [PATCH 1/5] main refactoring --- 42sh/Makefile | 1 + 42sh/includes/builtin.h | 12 ++--- 42sh/includes/ft_readline.h | 4 +- 42sh/includes/minishell.h | 9 ++-- 42sh/includes/parser.h | 7 ++- 42sh/libft | 2 +- 42sh/src/builtin/builtin_cd.c | 2 +- 42sh/src/builtin/builtin_math.c | 12 ++--- 42sh/src/exec/process_redirect.c | 2 +- 42sh/src/exec/process_setgroup.c | 2 +- 42sh/src/line-editing/control_features.c | 2 +- 42sh/src/line-editing/readline.c | 6 +-- 42sh/src/main/error_msg.c | 19 ++++++++ 42sh/src/main/instruction_free.c | 5 ++- 42sh/src/main/main.c | 56 +++++++++++++----------- 42sh/src/parser/error_syntax.c | 12 ++--- 16 files changed, 90 insertions(+), 63 deletions(-) create mode 100644 42sh/src/main/error_msg.c diff --git a/42sh/Makefile b/42sh/Makefile index 8e5c6248..f32921cf 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -266,6 +266,7 @@ line-editing/resize.c\ main/data_exit.c\ main/data_init.c\ main/data_singleton.c\ +main/error_msg.c\ main/ft_putast.c\ main/instruction_free.c\ main/main.c\ diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index 20c0be86..f7b728c4 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */ -/* Updated: 2017/03/15 21:55:22 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 14:19:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,11 +19,11 @@ # define BT_EXPORT_LP (1 << 0) -# define MATHERR_0 "{red}math : invalid number of arguments{eoc}\n" -# define MATHERR_1 "{red}math : invalid variable name{eoc}\n" -# define MATHERR_2 "{red}math : invalid operator{eoc}\n" -# define MATHERR_3 "{red}math : invalid operand{eoc}\n" -# define MATHERR_4 "{red}math : division by 0{eoc}\n" +# define MATHERR_0 "math : invalid number of arguments\n" +# define MATHERR_1 "math : invalid variable name\n" +# define MATHERR_2 "math : invalid operator\n" +# define MATHERR_3 "math : invalid operand\n" +# define MATHERR_4 "math : division by 0\n" t_execf *is_builtin(t_process *p); int builtin_export(const char *path, char *const av[], char *const envp[]); diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index 3647d65a..ae9f9a38 100644 --- a/42sh/includes/ft_readline.h +++ b/42sh/includes/ft_readline.h @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */ -/* Updated: 2017/03/16 12:10:24 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 13:55:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -155,7 +155,7 @@ void ft_c(void); void ft_x(void); void ft_v(void); void ft_read_it(int input, size_t *pos, char **str); -int readline(int fd, int prompt, char **input); +int readline(int prompt, char **input); int ft_completion(int ret); struct termios *ft_save_termios(int save); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 4d690594..3be6d97a 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/15 16:35:42 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 14:15:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,10 +51,11 @@ int shell_init(int ac, char **av); void shell_exit(void); int data_init(int ac, char **av); void data_exit(void); +int error_msg(char *msg); -int instruction_free(t_list **token, t_parser *parser, - t_btree **ast); -void content_free(void *data, size_t content_size); +int instruction_free( + t_list **token, t_btree **ast, t_lexer *lexer, t_parser *parser); +/* void content_free(void *data, size_t content_size); */ char *ft_putast(void *node); void ft_putast2(void *node); diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 34fb8718..29755d63 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -59,10 +59,9 @@ struct s_errormatch char *error; }; -extern t_aggrematch g_aggrematch[]; -extern t_prodmatch g_prodmatch[]; -extern t_stackmatch g_stackmatch[]; -extern t_errormatch g_errormatch[]; +/* extern t_aggrematch g_aggrematch[]; */ +/* extern t_prodmatch g_prodmatch[]; */ +/* extern t_stackmatch g_stackmatch[]; */ void parser_init(t_parser *parser); int ft_parse(t_btree **ast, t_list **token, t_parser *parser); diff --git a/42sh/libft b/42sh/libft index 0c1dbf5b..04b8d5cd 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 0c1dbf5b89c99612c49d6fd63dd25d0310bad3d9 +Subproject commit 04b8d5cd090e86b0f9d8160b01e49d43666deddf diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index c99503f6..d8f46e2e 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */ -/* Updated: 2017/03/15 20:55:40 by ariard ### ########.fr */ +/* Updated: 2017/03/16 14:17:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_math.c b/42sh/src/builtin/builtin_math.c index 05f80f7f..f854d709 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/15 21:02:59 by ariard ### ########.fr */ +/* Updated: 2017/03/16 14:18:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ static int get_value(char **var, char **value) char *temp; if (!word_is_assignment(var)) - return (ft_error_message(MATHERR_1)); + return (error_msg(MATHERR_1)); temp = ft_sstrstr(data_singleton()->local_var, *var); if (temp) { @@ -54,7 +54,7 @@ static int do_math(char **value, char *operator, char *operand) else ope2 = 0; if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0) - return (ft_error_message(MATHERR_4)); + return (error_msg(MATHERR_4)); else { ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1; @@ -78,7 +78,7 @@ int builtin_math(const char *path, char *const av[], char *const envp[]) (void)path; (void)envp; if (!av || !av[1] || !av[2] || !av[3] || av[4]) - return (ft_error_message(MATHERR_0)); + return (error_msg(MATHERR_0)); init_math(&var, &value, &operator, &operand); var = av[1]; if (get_value(&var, &value) == -1) @@ -86,10 +86,10 @@ int builtin_math(const char *path, char *const av[], char *const envp[]) operator = av[2]; if (!(ft_strlen(operator) == 1 && (operator[0] == '+' || operator[0] == '-' || operator[0] == '/' || operator[0] == '*' || operator[0] == '%'))) - return (ft_error_message(MATHERR_2)); + return (error_msg(MATHERR_2)); operand = av[3]; if (!ft_stris(operand, &ft_isdigit)) - return (ft_error_message(MATHERR_3)); + return (error_msg(MATHERR_3)); if (do_math(&value, operator, operand) == -1) return (-1); builtin_setenv("setenv", (char *[]){"local", var, value, 0}, data_singleton()->local_var); diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index fb2c6436..df4e9359 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2017/03/15 18:06:06 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 14:01:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index 575edca9..8664eac8 100644 --- a/42sh/src/exec/process_setgroup.c +++ b/42sh/src/exec/process_setgroup.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:48:10 by jhalford #+# #+# */ -/* Updated: 2017/03/15 18:11:49 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 13:54:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/control_features.c b/42sh/src/line-editing/control_features.c index 72c78e2d..d6543663 100644 --- a/42sh/src/line-editing/control_features.c +++ b/42sh/src/line-editing/control_features.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/02 15:17:28 by gwojda #+# #+# */ -/* Updated: 2017/03/16 10:17:13 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 14:05:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index 9f9137e6..87ff70b7 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,18 +6,18 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/03/15 03:52:15 by wescande ### ########.fr */ +/* Updated: 2017/03/16 14:06:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int readline(int fd, int prompt, char **input) +int readline(int prompt, char **input) { int ret; if (!SH_IS_INTERACTIVE(data_singleton()->opts)) - return ((ret = get_next_line(fd, input)) >= 0 ? !ret : ret); + return ((ret = get_next_line(STDIN, input)) >= 0 ? !ret : ret); readline_init(prompt); *input = ft_read_stdin(); if (STR) diff --git a/42sh/src/main/error_msg.c b/42sh/src/main/error_msg.c new file mode 100644 index 00000000..d53f828a --- /dev/null +++ b/42sh/src/main/error_msg.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error_msg.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/16 14:14:47 by jhalford #+# #+# */ +/* Updated: 2017/03/16 14:17:29 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int error_msg(char *msg) +{ + ft_dprintf(2, "{red}%s{eoc}\n", msg); + return (-1); +} diff --git a/42sh/src/main/instruction_free.c b/42sh/src/main/instruction_free.c index e4b66c19..4d4103e9 100644 --- a/42sh/src/main/instruction_free.c +++ b/42sh/src/main/instruction_free.c @@ -6,13 +6,13 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/15 15:54:59 by gwojda #+# #+# */ -/* Updated: 2017/03/15 16:49:10 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 14:13:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int instruction_free(t_list **token, t_parser *parser, t_btree **ast) +int instruction_free(t_list **token, t_btree **ast, t_lexer *lexer, t_parser *parser) { ft_lstdel(token, &token_free); token = NULL; @@ -20,6 +20,7 @@ int instruction_free(t_list **token, t_parser *parser, t_btree **ast) btree_del(ast, &ft_lst_cfree); ft_lstdel(&data_singleton()->heredoc_queue, &redir_free); ft_lstdel(&data_singleton()->exec.op_stack, &ft_lst_cfree); + ft_strdel(&lexer->str); free(parser->new_sym); return (0); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 706b663d..78e4ade1 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,35 +6,30 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/15 17:55:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 14:10:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int handle_instruction(int fd) +static int handle_instruction( t_list **token, + t_btree **ast, + t_lexer lexer, + t_parser parser) { - t_list *token; t_list *ltoken; - t_lexer lexer; - t_parser parser; - t_btree *ast; char *str; int ret; - lexer_init(&lexer); - parser_init(&parser); - token = NULL; - ast = NULL; while (1) { - if ((ret = readline(fd, get_lexer_stack(lexer) || + 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); + return (parser.state == UNDEFINED ? error_eof(token, + &parser, ast) : 1); } if (lexer.state == HEREDOC) { @@ -44,21 +39,21 @@ int handle_instruction(int fd) 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)) + ltoken = ft_lstlast(*token); + if (lexer_lex(token ? <oken : token, &lexer)) return (1); if (get_lexer_stack(lexer) > 2) continue ; lexer.state = DEFAULT; - if (get_reserved_words(&token)) + if (get_reserved_words(token)) return (1); - token_print(token); - if (insert_newline(&token)) + token_print(*token); + if (insert_newline(token)) return (1); - if (ft_parse(&ast, &token, &parser)) + if (ft_parse(ast, token, &parser)) continue ; if (parser.state == ERROR) - error_syntax(&token, &parser, &ast); + error_syntax(token, &parser, ast); lexer.state = data_singleton()->heredoc_queue ? HEREDOC : 0; if (lexer.state) continue; @@ -67,16 +62,14 @@ int handle_instruction(int fd) else if (parser.state == ERROR && SH_IS_INTERACTIVE(data_singleton()->opts)) { ft_add_str_in_history(lexer.str); - return (error_syntax(&token, &parser, &ast)); + return (error_syntax(token, &parser, ast)); } } /* btree_print(STDBUG, ast, &ft_putast); */ - if (ft_exec(&ast)) + if (ft_exec(ast)) return (2); - instruction_free(&token, &parser, &ast); if (SH_IS_INTERACTIVE(data_singleton()->opts) && *lexer.str) ft_add_str_in_history(lexer.str); - ft_strdel(&lexer.str); return (0); } @@ -118,6 +111,10 @@ int get_input_fd(char **av) int main(int ac, char **av) { int fd; + t_list *token; + t_lexer lexer; + t_parser parser; + t_btree *ast; g_argv = av; setlocale(LC_ALL, ""); @@ -126,9 +123,16 @@ int main(int ac, char **av) return (1); if ((fd = get_input_fd(av)) < 0) return (1); + dup2_close(fd, STDIN); + lexer_init(&lexer); + parser_init(&parser); + token = NULL; + ast = NULL; DG("JOBC is %s, fd=[%i]", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd); - while (handle_instruction(fd) == 0) - ; + while (handle_instruction(&token, &ast, lexer, parser) == 0) + { + instruction_free(&token, &ast, &lexer, &parser); + } builtin_exit(NULL, NULL, NULL); return (0); } diff --git a/42sh/src/parser/error_syntax.c b/42sh/src/parser/error_syntax.c index 58e09f82..b4dc6225 100644 --- a/42sh/src/parser/error_syntax.c +++ b/42sh/src/parser/error_syntax.c @@ -6,13 +6,13 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:15:35 by ariard #+# #+# */ -/* Updated: 2017/03/15 04:12:31 by wescande ### ########.fr */ +/* Updated: 2017/03/16 14:12:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_errormatch g_errormatch[] = +static t_errormatch g_errormatch[] = { {TK_NEWLINE, "newline"}, {TK_SEMI, ";"}, @@ -57,6 +57,8 @@ int error_syntax(t_list **lst, t_parser *parser, t_token *token; int i; + (void)parser; + (void)ast; if (!*lst) return (0); token = (*lst)->content; @@ -67,19 +69,19 @@ int error_syntax(t_list **lst, t_parser *parser, { ft_dprintf(2, "syntax error near unexpected token « %s »\n", g_errormatch[i].error); - instruction_free(lst, parser, ast); return (0); } i++; } ft_dprintf(2, "syntax error near unexpected token « %s »\n", token->data); - instruction_free(lst, parser, ast); return (0); } int error_eof(t_list **lst, t_parser *parser, t_btree **ast) { + (void)lst; + (void)parser; + (void)ast; ft_putstr_fd("syntax error near unexpected EOF", 2); - instruction_free(lst, parser, ast); return (0); } From bed9f1d6c245bcf09a0bb15b23dd8f1421b09469 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 16 Mar 2017 15:04:21 +0100 Subject: [PATCH 2/5] 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 */ /* */ /* ************************************************************************** */ From 3da58bcb52c24ec7b07845f11c1e7b4ef65274a1 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 16 Mar 2017 15:47:04 +0100 Subject: [PATCH 3/5] cleanup --- 42sh/includes/ft_readline.h | 3 +- 42sh/includes/minishell.h | 3 +- 42sh/includes/parser.h | 2 +- 42sh/src/line-editing/init_line.c | 4 +- 42sh/src/line-editing/readline.c | 2 +- 42sh/src/main/data_singleton.c | 2 +- 42sh/src/main/main.c | 111 ++++++++++-------------------- 42sh/src/main/shell_init.c | 41 ++++++++++- 42sh/src/parser/error_syntax.c | 9 +-- 9 files changed, 88 insertions(+), 89 deletions(-) diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index ae9f9a38..15bc59a5 100644 --- a/42sh/includes/ft_readline.h +++ b/42sh/includes/ft_readline.h @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */ -/* Updated: 2017/03/16 13:55:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 15:13:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,6 +74,7 @@ typedef struct s_list_history typedef struct s_line { char *input; + int fd; char *copy_tmp; size_t pos; int prompt_size; diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index c1d8fa3f..011445df 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 15:04:04 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 15:41:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,7 @@ struct s_data { t_flag opts; + int fd; char **env; char **local_var; int argc; diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 29755d63..bb370204 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -76,7 +76,7 @@ int pop_stack(t_list **stack, t_sym erase_sym); int pop_heredoc(t_list **lst); int error_syntax(t_list **token, t_parser *parser, t_btree **ast); -int error_eof(t_list **token, t_parser *parser, t_btree **ast); +int error_eof(void); int ft_read_stack(t_sym *stack); char *read_state(t_sym current); diff --git a/42sh/src/line-editing/init_line.c b/42sh/src/line-editing/init_line.c index 489d3286..3631a3fc 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 14:58:45 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 15:32:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,5 +41,5 @@ void readline_init(int has_prompt) data_singleton()->line.list_cur = data_singleton()->line.list_beg; POS = 0; has_prompt ? ft_prompt() : ft_putstr("> "); - data_singleton()->line.is_prompt = prompt ? 0 : 1; + data_singleton()->line.is_prompt = has_prompt ? 1 : 0; } diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index f2227e43..68aaef15 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/03/16 14:58:47 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 15:22:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_singleton.c b/42sh/src/main/data_singleton.c index b43daac3..4dc9731c 100644 --- a/42sh/src/main/data_singleton.c +++ b/42sh/src/main/data_singleton.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 11:36:39 by jhalford #+# #+# */ -/* Updated: 2017/03/09 15:22:54 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 15:12:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 837914be..64f83380 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,46 +6,46 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/16 15:02:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 15:45:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static int do_lexer_routine(t_list **token, t_lexer *lexer) +static int do_lexer_routine(t_list **token, t_lexer *lexer, t_parser *parser, char *str) { t_list *ltoken; - if (lexer->state == HEREDOC) + if (lexer->state == HEREDOC || parser->state == UNDEFINED) { ft_strappend(&lexer->str, (char[]){'\n', 0}); lexer->pos++; } ft_strappend(&lexer->str, str); - if (get_lexer_stack(lexer) == BACKSLASH) + 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) + 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) +static int do_parser_routine(t_list **token, t_btree **ast, 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); + ft_parse(ast, token, parser); if ((lexer->state = data_singleton()->heredoc_queue ? HEREDOC : DEFAULT)) return (0); - if (parse->state == ERROR) + if (parser->state == ERROR) { - error_syntax(token, &parser, ast); + error_syntax(token, parser, ast); return (1); } else if (parser->state == SUCCESS) @@ -53,103 +53,68 @@ static int do_parser_routine(t_list **token, t_lexer *lexer, t_parser *parser) return (0); } -static int do_readline_routine(t_list **token, t_lexer *lexer, t_parser *parser) +static int do_readline_routine(t_lexer *lexer, t_parser *parser, char **str) { int ret; - int prompt; - char *str; + int has_prompt; - 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); - } + has_prompt = !(get_lexer_stack(*lexer) || parser->state == UNDEFINED || lexer->state == HEREDOC); + ret = readline(has_prompt, str); + if (ret == -1) + exit(1); + if (ret == 1 && parser->state == UNDEFINED) + error_eof(); + return (ret); } -static int handle_instruction( t_list **token, - t_btree **ast, - t_lexer *lexer, - t_parser *parser) +static int handle_instruction(t_lexer *lexer, t_parser *parser) { + t_list *token; + t_btree *ast; + int ret; + char *str; + token = NULL; + ast = NULL; + str = NULL; while (1) { - if (do_readline_routine(lexer, parser) > 0) - if (do_lexer_routine(token, lexer) > 0) + if ((ret = do_readline_routine(lexer, parser, &str)) > 0) + return (ret); + if (do_lexer_routine(&token, lexer, parser, str) > 0) continue ; - if (do_parser_routine(token, lexer, parser) > 0) + if (do_parser_routine(&token, &ast, lexer, parser) > 0) break ; } /* btree_print(STDBUG, ast, &ft_putast); */ - if (parser->state == SUCCESS && ft_exec(ast) < 0) + 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); } -int get_input_fd(char **av) -{ - t_data *data; - char *file; - int fds[2]; - int fd; - struct stat buf; - - data = data_singleton(); - if (SH_IS_INTERACTIVE(data->opts)) - return (STDIN); - else if (data->opts & SH_OPTS_LC) - { - pipe(fds); - dup2_close(fds[PIPE_READ], 10); - fd = 10; - file = *cliopts_getdata(av); - write(fds[PIPE_WRITE], file, ft_strlen(file)); - close(fds[PIPE_WRITE]); - fcntl(fd, F_SETFD, FD_CLOEXEC); - return (fd); - } - else if ((file = *cliopts_getdata(av)) && !stat(file, &buf)) - { - fd = -1; - if (S_ISDIR(buf.st_mode)) - ft_printf("{red}%s: %s: is a directory\n{eoc}", av[0], file); - else if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0) - ft_printf("{red}%s: %s: No such file or directory\n{eoc}", av[0], file); - if (fd > 0 && !dup2_close(fd, 10) && (fd = 10)) - return (fd); - } - return (STDIN); -} int main(int ac, char **av) { - int fd; - t_list *token; + int ret; t_lexer lexer; t_parser parser; - t_btree *ast; g_argv = av; setlocale(LC_ALL, ""); DG("{inv}{bol}{gre}start of shell{eoc}"); if (shell_init(ac, av)) return (1); - if ((fd = get_input_fd(av)) < 0) - return (1); - dup2_close(fd, STDIN); lexer_init(&lexer); parser_init(&parser); - token = NULL; - ast = NULL; - DG("JOBC is %s, fd=[%i]", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd); - while (handle_instruction(&token, &ast, lexer, parser) == 0) + DG("JOBC is %s", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF"); + while (1) { - instruction_free(&token, &ast, &lexer, &parser); + ret = handle_instruction(&lexer, &parser); + /* instruction_free(&token, &ast, &lexer, &parser); */ + if (ret == 1) + break ; } builtin_exit(NULL, NULL, NULL); return (0); diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 7d2fce00..ad394679 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ -/* Updated: 2017/03/15 13:55:49 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 15:31:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,41 @@ static t_cliopts g_opts[] = {0, 0, 0, 0, 0}, }; +int get_input_fd(char **av) +{ + t_data *data; + char *file; + int fds[2]; + int fd; + struct stat buf; + + data = data_singleton(); + if (SH_IS_INTERACTIVE(data->opts)) + return (STDIN); + else if (data->opts & SH_OPTS_LC) + { + pipe(fds); + dup2_close(fds[PIPE_READ], 10); + fd = 10; + file = *cliopts_getdata(av); + write(fds[PIPE_WRITE], file, ft_strlen(file)); + close(fds[PIPE_WRITE]); + fcntl(fd, F_SETFD, FD_CLOEXEC); + return (fd); + } + else if ((file = *cliopts_getdata(av)) && !stat(file, &buf)) + { + fd = -1; + if (S_ISDIR(buf.st_mode)) + ft_printf("{red}%s: %s: is a directory\n{eoc}", av[0], file); + else if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0) + ft_printf("{red}%s: %s: No such file or directory\n{eoc}", av[0], file); + if (fd > 0 && !dup2_close(fd, 10) && (fd = 10)) + return (fd); + } + return (STDIN); +} + static int interactive_settings(void) { int *shell_pgid; @@ -64,7 +99,7 @@ int shell_init(int ac, char **av) return (ft_perror()); if (SH_IS_INTERACTIVE(data->opts)) interactive_settings(); - else - DG("not interactive"); + if ((data->fd = get_input_fd(av)) < 0) + return (1); return (0); } diff --git a/42sh/src/parser/error_syntax.c b/42sh/src/parser/error_syntax.c index b4dc6225..d1d79d72 100644 --- a/42sh/src/parser/error_syntax.c +++ b/42sh/src/parser/error_syntax.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:15:35 by ariard #+# #+# */ -/* Updated: 2017/03/16 14:12:14 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 15:34:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,11 +77,8 @@ int error_syntax(t_list **lst, t_parser *parser, return (0); } -int error_eof(t_list **lst, t_parser *parser, t_btree **ast) +int error_eof(void) { - (void)lst; - (void)parser; - (void)ast; ft_putstr_fd("syntax error near unexpected EOF", 2); - return (0); + return (1); } From 64d5500c8d5c0efaa7e3f4e621d20490400d4025 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Thu, 16 Mar 2017 22:30:02 +0100 Subject: [PATCH 4/5] main good --- 42sh/Makefile | 3 + 42sh/includes/lexer.h | 4 +- 42sh/includes/minishell.h | 29 +++--- 42sh/includes/parser.h | 6 +- 42sh/includes/types.h | 6 +- 42sh/libft | 2 +- 42sh/src/builtin/builtin_math.c | 2 +- 42sh/src/lexer/get_state_global.c | 2 +- 42sh/src/lexer/get_state_redir.c | 2 +- 42sh/src/lexer/lexer_default.c | 4 +- 42sh/src/lexer/lexer_destroy.c | 22 +++++ 42sh/src/lexer/lexer_init.c | 2 +- 42sh/src/lexer/lexer_lex.c | 2 +- 42sh/src/lexer/lexer_sep.c | 4 +- 42sh/src/lexer/token_print.c | 3 +- 42sh/src/line-editing/readline.c | 2 +- 42sh/src/main/data_init.c | 7 +- 42sh/src/main/instruction_free.c | 4 +- 42sh/src/main/main.c | 152 ++++++++++++++++-------------- 42sh/src/parser/add_number.c | 2 +- 42sh/src/parser/add_redir.c | 6 +- 42sh/src/parser/error_syntax.c | 13 +-- 42sh/src/parser/eval_sym.c | 4 +- 42sh/src/parser/ft_parse.c | 7 +- 42sh/src/parser/heredoc_parser.c | 6 +- 42sh/src/parser/parser_destroy.c | 22 +++++ 42sh/src/parser/parser_init.c | 7 +- 42sh/src/parser/produce_sym.c | 8 +- 42sh/src/parser/stack_init.c | 23 +++++ 29 files changed, 221 insertions(+), 135 deletions(-) create mode 100644 42sh/src/lexer/lexer_destroy.c create mode 100644 42sh/src/parser/parser_destroy.c create mode 100644 42sh/src/parser/stack_init.c diff --git a/42sh/Makefile b/42sh/Makefile index f32921cf..a584d1a7 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -220,6 +220,7 @@ lexer/lexer_bquote.c\ lexer/lexer_curly_braces.c\ lexer/lexer_default.c\ lexer/lexer_delim.c\ +lexer/lexer_destroy.c\ lexer/lexer_dquote.c\ lexer/lexer_end.c\ lexer/lexer_great.c\ @@ -287,11 +288,13 @@ parser/error_syntax.c\ parser/eval_sym.c\ parser/ft_parse.c\ parser/heredoc_parser.c\ +parser/parser_destroy.c\ parser/parser_init.c\ parser/pop_stack.c\ parser/produce_sym.c\ parser/push_stack.c\ parser/read_stack.c\ +parser/stack_init.c\ parser/tree_wrapper.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 618939db..285a5329 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/03/08 23:20:20 by ariard ### ########.fr */ +/* Updated: 2017/03/16 20:37:26 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -101,7 +101,7 @@ int get_reserved_words(t_list **alst); int insert_newline(t_list **alst); void lexer_init(t_lexer *lexer); - +void lexer_destroy(t_lexer *lexer); int lexer_lex(t_list **alst, t_lexer *lexer); int lexer_default(t_list **alst, t_lexer *lexer); int lexer_newline(t_list **alst, t_lexer *lexer); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 011445df..262ed34e 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 15:41:33 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 22:18:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,18 +34,21 @@ struct s_data { - t_flag opts; - int fd; - char **env; - char **local_var; - int argc; - char **argv; - t_line line; - t_comp *comp; - t_exec exec; - t_jobc jobc; - t_list *heredoc_queue; - t_list *lst_func; + t_flag opts; + int fd; + char **env; + int argc; + char **argv; + t_line line; + t_list *token; + t_btree *ast; + t_lexer lexer; + t_parser parser; + t_comp *comp; + t_exec exec; + t_jobc jobc; + char **local_var; + t_list *lst_func; }; int shell_init(int ac, char **av); diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index bb370204..df6149d7 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -30,6 +30,7 @@ struct s_parser t_parstate state; t_list *stack; t_sym *new_sym; + t_list *heredoc_queue; }; struct s_aggrematch @@ -64,6 +65,9 @@ struct s_errormatch /* extern t_stackmatch g_stackmatch[]; */ void parser_init(t_parser *parser); +void parser_destroy(t_parser *parser); +int stack_init(t_parser *parser); + int ft_parse(t_btree **ast, t_list **token, t_parser *parser); int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst); @@ -75,7 +79,7 @@ int pop_stack(t_list **stack, t_sym erase_sym); int pop_heredoc(t_list **lst); -int error_syntax(t_list **token, t_parser *parser, t_btree **ast); +int error_syntax(t_list **token); int error_eof(void); int ft_read_stack(t_sym *stack); diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 9098de75..aad1f77e 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/14 22:53:17 by ariard ### ########.fr */ +/* Updated: 2017/03/16 18:17:16 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,8 @@ typedef struct s_line t_line; typedef struct s_comp t_comp; typedef struct s_exec t_exec; typedef struct s_jobc t_jobc; +typedef struct s_lexer t_lexer; +typedef struct s_parser t_parser; typedef enum e_mode t_mode; typedef struct s_data_template t_btexport; @@ -43,7 +45,6 @@ typedef struct s_data_template t_btexport; ** Lexer types */ -typedef struct s_lexer t_lexer; typedef enum e_lexstate t_lexstate; typedef struct s_token t_token; typedef struct s_rvwords t_rvwords; @@ -77,7 +78,6 @@ typedef struct s_redir t_redir; typedef struct s_cmd t_cmd; typedef union u_astdata t_astdata; typedef union u_word t_word; -typedef struct s_parser t_parser; typedef int t_sym; typedef enum e_parstate t_parstate; typedef struct s_aggrematch t_aggrematch; diff --git a/42sh/libft b/42sh/libft index 04b8d5cd..89d3f6e6 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 04b8d5cd090e86b0f9d8160b01e49d43666deddf +Subproject commit 89d3f6e67e707e42fca42cc89e3f39e5f840908a diff --git a/42sh/src/builtin/builtin_math.c b/42sh/src/builtin/builtin_math.c index c37771c6..a2ec3343 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:24:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 17:44:15 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/get_state_global.c b/42sh/src/lexer/get_state_global.c index 305e03a9..aca394e5 100644 --- a/42sh/src/lexer/get_state_global.c +++ b/42sh/src/lexer/get_state_global.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:39:06 by jhalford #+# #+# */ -/* Updated: 2017/03/13 19:03:43 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 19:00:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/get_state_redir.c b/42sh/src/lexer/get_state_redir.c index 5ac62b6c..997dde9a 100644 --- a/42sh/src/lexer/get_state_redir.c +++ b/42sh/src/lexer/get_state_redir.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:37:28 by jhalford #+# #+# */ -/* Updated: 2017/03/10 13:12:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 19:00:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index a2802c5c..2ab8a22d 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/03/05 16:20:18 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 19:05:25 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ int lexer_default(t_list **alst, t_lexer *lexer) t_token *token; char c; - c = lexer->str[lexer->pos]; + c = lexer->str[lexer->pos]; if ((lexer->state = get_state_global(lexer))) return (lexer_lex(alst, lexer)); if ((lexer->state = get_state_redir(lexer))) diff --git a/42sh/src/lexer/lexer_destroy.c b/42sh/src/lexer/lexer_destroy.c new file mode 100644 index 00000000..73596cd0 --- /dev/null +++ b/42sh/src/lexer/lexer_destroy.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/16 20:25:08 by ariard #+# #+# */ +/* Updated: 2017/03/16 20:43:06 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void lexer_destroy(t_lexer *lexer) +{ + ft_strdel(&lexer->str); + lexer->pos = 0; + lexer->state = 0; + lexer->stack = NULL; + lexer->heredoc_stack = NULL; +} diff --git a/42sh/src/lexer/lexer_init.c b/42sh/src/lexer/lexer_init.c index a7000895..261bf644 100644 --- a/42sh/src/lexer/lexer_init.c +++ b/42sh/src/lexer/lexer_init.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/21 16:14:08 by ariard #+# #+# */ -/* Updated: 2017/03/04 16:32:15 by ariard ### ########.fr */ +/* Updated: 2017/03/16 17:57:47 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_lex.c b/42sh/src/lexer/lexer_lex.c index 39ab2483..ff86f0e6 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/16 14:45:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 19:04:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_sep.c b/42sh/src/lexer/lexer_sep.c index 76c6b7e8..b3f4fcf9 100644 --- a/42sh/src/lexer/lexer_sep.c +++ b/42sh/src/lexer/lexer_sep.c @@ -6,11 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 16:29:57 by jhalford #+# #+# */ -/* Updated: 2017/03/09 18:48:00 by ariard ### ########.fr */ +/* Updated: 2017/03/16 18:21:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ -#include "lexer.h" +#include "minishell.h" int lexer_sep(t_list **alst, t_lexer *lexer) { diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index c17e1605..a5269ccb 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ -/* Updated: 2017/03/08 12:14:32 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 19:10:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ void token_print(t_list *lst) while (lst) { + DG("in token print"); if (lst->content) token = lst->content; if (token->type) diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index 68aaef15..eb80b9e8 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/03/16 15:22:11 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 18:40:39 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 11e9f92e..0a66550a 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2017/03/15 16:23:18 by wescande ### ########.fr */ +/* Updated: 2017/03/16 22:15:29 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,10 @@ int data_init(int ac, char **av) data->opts = 0; exec_reset(); data->lst_func = NULL; - data->heredoc_queue = NULL; + data->token = NULL; + data->ast = NULL; + lexer_init(&data->lexer); + parser_init(&data->parser); if ((term_name = ft_getenv(data->env, "TERM")) == NULL) return (-1); if (tgetent(NULL, term_name) != 1) diff --git a/42sh/src/main/instruction_free.c b/42sh/src/main/instruction_free.c index 4d4103e9..4b644120 100644 --- a/42sh/src/main/instruction_free.c +++ b/42sh/src/main/instruction_free.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/15 15:54:59 by gwojda #+# #+# */ -/* Updated: 2017/03/16 14:13:33 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 18:27:57 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ int instruction_free(t_list **token, t_btree **ast, t_lexer *lexer, t_parser *pa token = NULL; ft_lstdel(&parser->stack, &ft_lst_cfree); btree_del(ast, &ft_lst_cfree); - ft_lstdel(&data_singleton()->heredoc_queue, &redir_free); + ft_lstdel(&data_singleton()->parser.heredoc_queue, &redir_free); ft_lstdel(&data_singleton()->exec.op_stack, &ft_lst_cfree); ft_strdel(&lexer->str); free(parser->new_sym); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 64f83380..b9b9537c 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,113 +6,123 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/16 15:45:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 22:26:56 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static int do_lexer_routine(t_list **token, t_lexer *lexer, t_parser *parser, char *str) -{ - t_list *ltoken; - - if (lexer->state == HEREDOC || parser->state == UNDEFINED) - { - 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_btree **ast, 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 (parser->state == ERROR) - { - error_syntax(token, parser, ast); - return (1); - } - else if (parser->state == SUCCESS) - return (1); - return (0); -} - -static int do_readline_routine(t_lexer *lexer, t_parser *parser, char **str) +static int do_readline_routine(char **stream) { int ret; int has_prompt; + t_data *data; - has_prompt = !(get_lexer_stack(*lexer) || parser->state == UNDEFINED || lexer->state == HEREDOC); - ret = readline(has_prompt, str); + data = data_singleton(); + has_prompt = !(get_lexer_stack(data->lexer) + || data->parser.state == UNDEFINED || data->lexer.state == HEREDOC); + ret = readline(has_prompt, stream); if (ret == -1) exit(1); - if (ret == 1 && parser->state == UNDEFINED) + if (ret == 1 && data->parser.state == UNDEFINED) error_eof(); return (ret); } -static int handle_instruction(t_lexer *lexer, t_parser *parser) +static int do_lexer_routine(char *stream) { - t_list *token; - t_btree *ast; - int ret; - char *str; + t_list *ltoken; + t_data *data; - token = NULL; - ast = NULL; - str = NULL; - while (1) + data = data_singleton(); + if (data->lexer.state == HEREDOC || data->parser.state == UNDEFINED) { - if ((ret = do_readline_routine(lexer, parser, &str)) > 0) - return (ret); - if (do_lexer_routine(&token, lexer, parser, str) > 0) - continue ; - if (do_parser_routine(&token, &ast, lexer, parser) > 0) - break ; + ft_strappend(&data->lexer.str, (char[]){'\n', 0}); + data->lexer.pos++; } - /* btree_print(STDBUG, ast, &ft_putast); */ - if (parser->state == SUCCESS && ft_exec(&ast) < 0) + ft_strappend(&data->lexer.str, stream); + if (get_lexer_stack(data->lexer) == BACKSLASH) + pop(&data->lexer.stack); + ltoken = ft_lstlast(data->token); + if (lexer_lex(data->token ? <oken : &data->token, &data->lexer) < 0) exit(1); - if (SH_IS_INTERACTIVE(data_singleton()->opts) && *lexer->str) - ft_add_str_in_history(lexer->str); + if (get_lexer_stack(data->lexer) > 2) + return (1); + data->lexer.state = DEFAULT; return (0); } +static int do_parser_routine(void) +{ + t_data *data; + + data = data_singleton(); + if (get_reserved_words(&data->token)) + return (1); + if (insert_newline(&data->token)) + return (2); + if (data->parser.state == SUCCESS && stack_init(&data->parser)) + exit(1); + if (ft_parse(&data->ast, &data->token, &data->parser)) + exit(1); + if ((data->lexer.state = data->parser.heredoc_queue ? HEREDOC : DEFAULT)) + return (0); + if (data->parser.state == ERROR) + { + error_syntax(&data->token); + data->parser.state = SUCCESS; + return (1); + } + else if (data->parser.state == SUCCESS) + return (1); + return (0); +} + +static int handle_instruction() +{ + int ret; + char *stream; + t_data *data; + + stream = NULL; + data = data_singleton(); + while (1) + { + if ((ret = do_readline_routine(&stream)) > 0) + return (ret); + if (do_lexer_routine(stream) > 0) + continue ; + if (do_parser_routine() > 0) + break ; + } + /* btree_print(STDBUG, ast, &ft_putast); */ + if (data->parser.state == SUCCESS && ft_exec(&data->ast) < 0) + exit(1); + DG("stream [%s]", data->lexer.str); + if (SH_IS_INTERACTIVE(data->opts) && data->lexer.str) + ft_add_str_in_history(data->lexer.str); + return (0); +} int main(int ac, char **av) { int ret; - t_lexer lexer; - t_parser parser; + t_data *data; g_argv = av; setlocale(LC_ALL, ""); DG("{inv}{bol}{gre}start of shell{eoc}"); if (shell_init(ac, av)) return (1); - lexer_init(&lexer); - parser_init(&parser); DG("JOBC is %s", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF"); + data = data_singleton(); while (1) { - ret = handle_instruction(&lexer, &parser); - /* instruction_free(&token, &ast, &lexer, &parser); */ + ret = handle_instruction(); + lexer_destroy(&data->lexer); + parser_destroy(&data->parser); + ft_lstdel(&data->token, &ft_lst_cfree); + btree_del(&data->ast, &ft_lst_cfree); if (ret == 1) break ; } diff --git a/42sh/src/parser/add_number.c b/42sh/src/parser/add_number.c index d044c123..3ee2de29 100644 --- a/42sh/src/parser/add_number.c +++ b/42sh/src/parser/add_number.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 17:28:31 by ariard #+# #+# */ -/* Updated: 2017/03/13 16:58:33 by ariard ### ########.fr */ +/* Updated: 2017/03/16 17:56:16 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_redir.c b/42sh/src/parser/add_redir.c index b4f9611a..57ef23ea 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/15 02:09:54 by ariard ### ########.fr */ +/* Updated: 2017/03/16 18:34:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,7 +87,7 @@ static int add_redir_type_number(t_btree **ast, t_list **lst) t_list **queue; t_list *redir_lst; - queue = &data_singleton()->heredoc_queue; + queue = &data_singleton()->parser.heredoc_queue; token = (*lst)->content; node = (*ast)->item; redir_lst = ft_lstlast(node->data.cmd.redir); @@ -116,7 +116,7 @@ int add_redir_type(t_btree **ast, t_list **lst) t_list *redir_lst; t_list **queue; - queue = &data_singleton()->heredoc_queue; + queue = &data_singleton()->parser.heredoc_queue; if (!*ast) gen_node(ast); token = (*lst)->content; diff --git a/42sh/src/parser/error_syntax.c b/42sh/src/parser/error_syntax.c index d1d79d72..aef8478a 100644 --- a/42sh/src/parser/error_syntax.c +++ b/42sh/src/parser/error_syntax.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:15:35 by ariard #+# #+# */ -/* Updated: 2017/03/16 15:34:39 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 17:49:23 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,14 +51,11 @@ static t_errormatch g_errormatch[] = {0, NULL}, }; -int error_syntax(t_list **lst, t_parser *parser, - t_btree **ast) +int error_syntax(t_list **lst) { t_token *token; int i; - (void)parser; - (void)ast; if (!*lst) return (0); token = (*lst)->content; @@ -67,18 +64,18 @@ int error_syntax(t_list **lst, t_parser *parser, { if (g_errormatch[i].token == token->type) { - ft_dprintf(2, "syntax error near unexpected token « %s »\n", + ft_dprintf(2, "{red}syntax error near unexpected token « %s »{eoc}\n", g_errormatch[i].error); return (0); } i++; } - ft_dprintf(2, "syntax error near unexpected token « %s »\n", token->data); + ft_dprintf(2, "{red}syntax error near unexpected token « %s »\n{eoc}", token->data); return (0); } int error_eof(void) { - ft_putstr_fd("syntax error near unexpected EOF", 2); + ft_dprintf(2, "{red}syntax error near unexpected EOF{eoc}"); return (1); } diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index ffaf9334..0965e10a 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 16:11:21 by ariard #+# #+# */ -/* Updated: 2017/03/14 22:01:19 by ariard ### ########.fr */ +/* Updated: 2017/03/16 21:24:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -1243,7 +1243,7 @@ int eval_sym(t_list **stack, t_sym new_sym) if (!*stack) return (1); head = (*stack)->content; -// DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); + DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); i = 0; while (g_stackmatch[i].top) { diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index d65aa8b8..f0e30113 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/16 14:32:34 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 22:11:41 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,13 +35,12 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) { t_sym *head; - (void)ast; if (pop_heredoc(token)) return (0); while (*token) { produce_sym(&parser->stack, parser->new_sym, token); -// DG("new sym %s", read_state(*parser->new_sym)); + DG("new sym %s", read_state(*parser->new_sym)); if (eval_sym(&parser->stack, *parser->new_sym)) return ((parser->state = ERROR)); else @@ -62,7 +61,5 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) else ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free); } - if (parser->state == SUCCESS) - DG("sucessful parsing"); return (0); } diff --git a/42sh/src/parser/heredoc_parser.c b/42sh/src/parser/heredoc_parser.c index 78a74e01..02776e5e 100644 --- a/42sh/src/parser/heredoc_parser.c +++ b/42sh/src/parser/heredoc_parser.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 16:21:05 by ariard #+# #+# */ -/* Updated: 2017/03/14 15:02:34 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 17:51:54 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,9 +20,9 @@ int pop_heredoc(t_list **lst) char *tmp; token = (*lst)->content; - if (token->type == HEREDOCDATA && data_singleton()->heredoc_queue != NULL) + if (token->type == HEREDOCDATA && data_singleton()->parser.heredoc_queue != NULL) { - head = &data_singleton()->heredoc_queue; + head = &data_singleton()->parser.heredoc_queue; redir = *(t_redir**)(*head)->content; if (head && token) { diff --git a/42sh/src/parser/parser_destroy.c b/42sh/src/parser/parser_destroy.c new file mode 100644 index 00000000..f24f7087 --- /dev/null +++ b/42sh/src/parser/parser_destroy.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/16 19:30:17 by ariard #+# #+# */ +/* Updated: 2017/03/16 22:13:50 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void parser_destroy(t_parser *parser) +{ + parser->state = SUCCESS; + ft_lstdel(&parser->stack, &ft_lst_cfree); + ft_lstdel(&data_singleton()->parser.heredoc_queue, &ft_lst_cfree); + free(parser->new_sym); + parser->new_sym = NULL; +} diff --git a/42sh/src/parser/parser_init.c b/42sh/src/parser/parser_init.c index 8b8ca5c1..8f86e68c 100644 --- a/42sh/src/parser/parser_init.c +++ b/42sh/src/parser/parser_init.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/21 16:14:04 by ariard #+# #+# */ -/* Updated: 2017/03/11 16:20:31 by ariard ### ########.fr */ +/* Updated: 2017/03/16 22:13:39 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,8 +15,7 @@ void parser_init(t_parser *parser) { parser->state = SUCCESS; - parser->new_sym = ft_memalloc(sizeof(t_sym)); + parser->new_sym = NULL; parser->stack = NULL; - push_stack(&parser->stack, TERMINUS); - push_stack(&parser->stack, LINEBREAK); + parser->heredoc_queue = NULL; } diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index bc71df07..b73514d0 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ -/* Updated: 2017/03/14 22:03:42 by ariard ### ########.fr */ +/* Updated: 2017/03/16 21:31:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -127,12 +127,14 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst) t_sym *head; int i; + if (!*stack) + DG("stack absente"); if (!*stack || !*lst) return (1); token = (*lst)->content; head = (*stack)->content; -// DG("produce stack : %s && token : %s", read_state(*head), -// read_state(token->type)); + DG("produce stack : %s && token : %s", read_state(*head), + read_state(token->type)); i = 0; *new_sym = 0; while (g_prodmatch[i].new_sym) diff --git a/42sh/src/parser/stack_init.c b/42sh/src/parser/stack_init.c new file mode 100644 index 00000000..6abc9386 --- /dev/null +++ b/42sh/src/parser/stack_init.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* stack_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/16 20:31:32 by ariard #+# #+# */ +/* Updated: 2017/03/16 22:11:59 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int stack_init(t_parser *parser) +{ + ft_lstdel(&parser->stack, NULL); + push_stack(&parser->stack, TERMINUS); + push_stack(&parser->stack, LINEBREAK); + if (!parser->new_sym && !(parser->new_sym = ft_memalloc(sizeof(t_sym)))) + return (-1); + return (0); +} From 3d620816a980b9f8da55b0b7d732f7d67186b4ea Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Thu, 16 Mar 2017 23:22:30 +0100 Subject: [PATCH 5/5] error syntax good --- 42sh/src/main/main.c | 5 ++--- 42sh/src/parser/parser_destroy.c | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index b9b9537c..b42c94dc 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/03/16 22:26:56 by ariard ### ########.fr */ +/* Updated: 2017/03/16 23:21:19 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,7 +60,7 @@ static int do_parser_routine(void) if (get_reserved_words(&data->token)) return (1); if (insert_newline(&data->token)) - return (2); + return (1); if (data->parser.state == SUCCESS && stack_init(&data->parser)) exit(1); if (ft_parse(&data->ast, &data->token, &data->parser)) @@ -70,7 +70,6 @@ static int do_parser_routine(void) if (data->parser.state == ERROR) { error_syntax(&data->token); - data->parser.state = SUCCESS; return (1); } else if (data->parser.state == SUCCESS) diff --git a/42sh/src/parser/parser_destroy.c b/42sh/src/parser/parser_destroy.c index f24f7087..d5c1be5d 100644 --- a/42sh/src/parser/parser_destroy.c +++ b/42sh/src/parser/parser_destroy.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/16 19:30:17 by ariard #+# #+# */ -/* Updated: 2017/03/16 22:13:50 by ariard ### ########.fr */ +/* Updated: 2017/03/16 23:21:28 by ariard ### ########.fr */ /* */ /* ************************************************************************** */