From 16e243d6cd64f6168b0127b07866a4b8209c335f Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Thu, 8 Oct 2020 17:44:59 +0200 Subject: Added tok_lst_pop_front wrapper --- include/lexer.h | 3 ++- include/minishell.h | 3 ++- minishell_test | 2 +- src/lexer/tok_lst.c | 7 ++++++- src/parser/parsed.c | 3 ++- src/parser/parser.c | 41 +++++++++++++++++++++++++---------------- src/utils.c | 6 +++--- 7 files changed, 41 insertions(+), 24 deletions(-) diff --git a/include/lexer.h b/include/lexer.h index 82b4d1d..72480d5 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/19 10:51:26 by nahaddac #+# #+# */ -/* Updated: 2020/10/06 16:21:01 by cacharle ### ########.fr */ +/* Updated: 2020/10/08 17:38:33 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -89,6 +89,7 @@ t_tok_lst *tok_lst_new(enum e_tok tag, char *content); t_tok_lst *tok_lst_new_until(enum e_tok tag, char *content, size_t n); void tok_lst_push_back(t_tok_lst **tokens, t_tok_lst *pushed); t_tok_lst *tok_lst_push_front(t_tok_lst **tokens, t_tok_lst *pushed); +void tok_lst_pop_front(t_tok_lst **tokens, void (*del)(void*)); void *tok_lst_destroy(t_tok_lst **tokens, void (*del)(void*)); t_tok_lst *tok_lst_last(t_tok_lst *tokens); t_tok_lst *tok_lst_uncons(t_tok_lst **tokens); diff --git a/include/minishell.h b/include/minishell.h index 5bc9ad4..cfc8b42 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/26 15:33:51 by cacharle #+# #+# */ -/* Updated: 2020/10/07 14:58:27 by cacharle ### ########.fr */ +/* Updated: 2020/10/08 17:42:07 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -138,6 +138,7 @@ void signal_sigterm(int signum); */ void print_prompt(void); +void exit_if(bool predicate); /* ** setup.c diff --git a/minishell_test b/minishell_test index 93ead39..72954f3 160000 --- a/minishell_test +++ b/minishell_test @@ -1 +1 @@ -Subproject commit 93ead396473526e5f4849ad2f4194b8cc6c9ce45 +Subproject commit 72954f36bfcad29cbb4bb324763d4c03b9fa4601 diff --git a/src/lexer/tok_lst.c b/src/lexer/tok_lst.c index 4f5b2dc..9ae0ce1 100644 --- a/src/lexer/tok_lst.c +++ b/src/lexer/tok_lst.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/08/27 09:32:58 by charles #+# #+# */ -/* Updated: 2020/09/13 20:31:53 by charles ### ########.fr */ +/* Updated: 2020/10/08 17:38:59 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,6 +56,11 @@ t_tok_lst *tok_lst_push_front(t_tok_lst **tokens, t_tok_lst *pushed) return (*tokens); } +void tok_lst_pop_front(t_tok_lst **tokens, void (*del)(void*)) +{ + ft_lstpop_front((t_ftlst**)tokens, del); +} + void *tok_lst_destroy(t_tok_lst **tokens, void (*del)(void*)) { ft_lstdestroy((t_ftlst**)tokens, del); diff --git a/src/parser/parsed.c b/src/parser/parsed.c index c577b32..71ff581 100644 --- a/src/parser/parsed.c +++ b/src/parser/parsed.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/08/27 20:27:42 by charles #+# #+# */ -/* Updated: 2020/09/10 19:31:46 by charles ### ########.fr */ +/* Updated: 2020/10/08 17:07:35 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,5 +35,6 @@ t_parsed *parsed_error(const char *format, ...) va_start(ap, format); verrorf(format, ap); va_end(ap); + ft_putchar_fd('\n', STDERR_FILENO); return (err); } diff --git a/src/parser/parser.c b/src/parser/parser.c index 8b4732f..ce4501c 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ -/* Updated: 2020/10/08 16:58:19 by cacharle ### ########.fr */ +/* Updated: 2020/10/08 17:42:53 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ */ #include "parser.h" +#include "minishell.h" static char *g_sep_str_lookup[] = { [TAG_END] = ";", @@ -45,10 +46,10 @@ t_parsed *parse_redir(t_tok_lst *input, t_tok_lst **redirs) && input->next != NULL && input->next->tag & TAG_IS_STR) tok_lst_push_back(redirs, tok_lst_uncons(&input)); if (input == NULL) - return (parsed_error("syntax error near unexpected token `newline'\n")); + return (parsed_error("syntax error near unexpected token `newline'")); if (!(input->tag & TAG_IS_STR)) { - t_parsed *ret = parsed_error("syntax error near unexpected token `%s'\n", input->content); + t_parsed *ret = parsed_error("syntax error near unexpected token `%s'", input->content); tok_lst_destroy(&input, free); return (ret); } @@ -63,7 +64,7 @@ t_parsed *parse_cmd(t_tok_lst *input) if (input->tag & TAG_IS_SEP || input->tag == TAG_PIPE || input->tag == TAG_PARENT_CLOSE) { - t_parsed *ret = parsed_error("syntax error near unexpected token `%s'\n", input->content); + t_parsed *ret = parsed_error("syntax error near unexpected token `%s'", input->content); tok_lst_destroy(&input, free); return (ret); } @@ -101,13 +102,13 @@ t_parsed *parse_pipeline(t_tok_lst *input) if (expr == NULL || expr->syntax_error || expr->rest == NULL || expr->rest->tag != TAG_PIPE) return (expr); - ft_lstpop_front(&expr->rest, free); + tok_lst_pop_front(&expr->rest, free); if (expr->rest == NULL) { ast_destroy(expr->ast); free(expr); /* tok_lst_destroy(expr->rest, free); */ - return (parsed_error("syntax error expected token\n")); + return (parsed_error("syntax error expected token")); } tail = parse_pipeline(expr->rest); if (tail == NULL || tail->syntax_error) @@ -122,11 +123,11 @@ t_parsed *parse_pipeline(t_tok_lst *input) if (tail->ast->tag == AST_CMD || tail->ast->tag == AST_PARENT) { pipeline_ast = ast_new(AST_PIPELINE); - try(pipeline_ast->pipeline = ft_lstnew(tail->ast)); + exit_if((pipeline_ast->pipeline = ft_lstnew(tail->ast)) == NULL); } else pipeline_ast = tail->ast; - try(ft_lstpush_front_node(&pipeline_ast->pipeline, expr_ast)); + exit_if(ft_lstpush_front_node(&pipeline_ast->pipeline, expr_ast) == NULL); tail->ast = pipeline_ast; return (tail); } @@ -152,9 +153,11 @@ t_parsed *parse_op(t_tok_lst *input) return (left); sep_tag = input->tag; if (!(sep_tag & TAG_IS_SEP)) - return (parsed_error("syntax error near unexpected token `%s'\n", + { + return (parsed_error("syntax error near unexpected token `%s'", g_sep_str_lookup[sep_tag])); - ft_lstpop_front((t_ftlst**)&input, free); + } + tok_lst_pop_front(&input, free); if (input == NULL && sep_tag == TAG_END) { left->rest = NULL; @@ -164,7 +167,7 @@ t_parsed *parse_op(t_tok_lst *input) { ast_destroy(left->ast); free(left); - return (parsed_error("syntax error expected token\n")); + return (parsed_error("syntax error expected token")); } right = parse_op(input); if (right == NULL || right->syntax_error) @@ -197,16 +200,16 @@ t_parsed *parse_expr(t_tok_lst *input) if (input->tag & TAG_PARENT_OPEN) { - ft_lstpop_front(&input, free); + tok_lst_pop_front(&input, free); if (input == NULL) - return (parsed_error("syntax error: unexpected end of file\n")); + return (parsed_error("syntax error expected token")); parsed = parse_op(input); if (parsed == NULL || parsed->syntax_error) return (parsed); input = parsed->rest; if (input == NULL || !(input->tag & TAG_PARENT_CLOSE)) - return (parsed_error("syntax error expected token\n")); - ft_lstpop_front(&input, free); + return (parsed_error("syntax error expected token")); + tok_lst_pop_front(&input, free); if ((ast = ast_new(AST_PARENT)) == NULL) return (NULL); ast->parent_ast = parsed->ast; @@ -233,6 +236,12 @@ t_parsed *parse(t_tok_lst *input) if (parsed == NULL || parsed->syntax_error) return parsed; if (parsed->rest != NULL) - return (parsed_error("syntax error near unexpected token `%s'\n", parsed->rest->content)); + { + t_parsed *ret = parsed_error("syntax error near unexpected token `%s'", parsed->rest->content); + tok_lst_destroy(&parsed->rest, free); + ast_destroy(parsed->ast); + free(parsed); + return ret; + } return (parsed); } diff --git a/src/utils.c b/src/utils.c index 3da5c59..71eae54 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */ -/* Updated: 2020/10/08 17:02:52 by cacharle ### ########.fr */ +/* Updated: 2020/10/08 17:43:06 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,9 +40,9 @@ void print_prompt(void) ft_putstr_fd("\033[0m$ ", STDERR_FILENO); } -void try(void *ptr) +void exit_if(bool predicate) { - if (ptr != NULL) + if (predicate) return ; ft_putendl_fd("minishell: fatal error", STDERR_FILENO); exit(3); -- cgit