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); }