diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lexer/tok_lst.c | 7 | ||||
| -rw-r--r-- | src/parser/parsed.c | 3 | ||||
| -rw-r--r-- | src/parser/parser.c | 41 | ||||
| -rw-r--r-- | src/utils.c | 6 |
4 files changed, 36 insertions, 21 deletions
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 <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <nahaddac@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <cacharle@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); |
