diff options
| author | nass1pro <nass1pro@gmail.com> | 2020-06-19 13:28:41 +0200 |
|---|---|---|
| committer | nass1pro <nass1pro@gmail.com> | 2020-06-19 13:28:41 +0200 |
| commit | c8091831c4ce1c4cf8703b18de22441aff191f44 (patch) | |
| tree | b30e542c9345135892f971d8e7566fd4cc8c64a4 /src | |
| parent | 677ebe4c32fc7b7bb99f3abb48f7f6f1a428d102 (diff) | |
| download | minishell-c8091831c4ce1c4cf8703b18de22441aff191f44.tar.gz minishell-c8091831c4ce1c4cf8703b18de22441aff191f44.tar.bz2 minishell-c8091831c4ce1c4cf8703b18de22441aff191f44.zip | |
Update parser_error
Diffstat (limited to 'src')
| -rw-r--r-- | src/ast.c | 4 | ||||
| -rw-r--r-- | src/builtin/export.c | 10 | ||||
| -rw-r--r-- | src/main.c | 308 | ||||
| -rwxr-xr-x | src/parse/parse.c | 69 | ||||
| -rw-r--r-- | src/parse/parse_error.c | 106 |
5 files changed, 316 insertions, 181 deletions
@@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:42 by charles #+# #+# */ -/* Updated: 2020/06/18 13:39:30 by charles ### ########.fr */ +/* Updated: 2020/06/18 14:46:07 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,7 +46,7 @@ void ast_destroy(t_ast *ast) { if (ast == NULL) return ; - ft_lstdestroy(&ast->cmd_argv, (void (*)(void*))token_destroy); + //ft_lstdestroy(&ast->cmd_argv, (void (*)(void*))token_destroy); if (ast->tag == AST_CMD) { ft_lstdestroy(&ast->cmd_argv, (void (*)(void*))token_destroy); diff --git a/src/builtin/export.c b/src/builtin/export.c index c60d8f0..f41bcb0 100644 --- a/src/builtin/export.c +++ b/src/builtin/export.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:11:34 by charles #+# #+# */ -/* Updated: 2020/06/18 13:50:47 by charles ### ########.fr */ +/* Updated: 2020/06/19 11:21:50 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int builtin_export(char **argv, t_env env) (void)env; if (argv[1] == NULL) - return (4); + return (0); if(ft_isdigit(argv[1][0])) return(0); i = 0; @@ -36,14 +36,14 @@ int builtin_export(char **argv, t_env env) while(temp[i] != '\0') { if(temp[i] == ' ' || ft_isalnum(temp[i]) == 0) - return(2); + return(0); if (temp[i] == '=') { temp[i] = '\0'; - env_export(env, temp, argv[1][i + 1]); + env_export(env, temp, &argv[1][i + 1]); return(0); } i++; } - return (3); + return (0); } @@ -6,7 +6,7 @@ /* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/06/18 13:46:10 by charles ### ########.fr */ +/* Updated: 2020/06/18 14:57:30 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,169 +21,167 @@ #include "parser.h" #include "eval.h" -/* void token_debug(void *v) */ -/* { */ -/* t_token *t; */ -/* */ -/* t= v; */ -/* printf("[%4d %d] (%s)\n", t->tag, !!(t->tag & TAG_STICK), t->content); */ -/* } */ -/* */ -/* int main(int argc, char **argv, char **envp) */ -/* { */ -/* t_path path; */ -/* t_env env; */ -/* */ -/* env = env_from_array(envp); */ -/* path = path_update(NULL, env_search(env, "PATH")); */ -/* #<{(| printf("%s\n", argv[2]); |)}># */ -/* */ -/* if (argc == 3 && ft_strcmp(argv[1], "-c") == 0) */ -/* { */ -/* //printf("%s\n", argv[2]); */ -/* t_ftlst *lex_out = lexer(ft_strdup(argv[2])); */ -/* */ -/* //ft_lstiter(lex_out, token_debug); */ -/* */ -/* t_ret *parser_out = parse(lex_out); */ -/* */ -/* #<{(| printf("===cmd_argv===\n"); |)}># */ -/* #<{(| ft_lstiter(parser_out->ast->cmd_argv, token_debug); |)}># */ -/* #<{(| printf("===redirs===\n"); |)}># */ -/* #<{(| ft_lstiter(parser_out->ast->redirs, token_debug); |)}># */ -/* */ -/* int fds[2] = {MS_NO_FD, MS_NO_FD}; */ -/* int eval_out = eval_cmd(fds, env, path, parser_out->ast); */ -/* (void)eval_out; */ -/* } */ -/* */ -/* ft_htdestroy(path, free); */ -/* ft_vecdestroy(env, free); */ -/* return (0); */ -/* } */ - -void token_put(void *v) -{ - t_token *t; - - t= v; - printf("%s ", t->content); -} - -void print_level(int level) -{ - while (level-- > 0) - printf(" "); -} - -void ast_print(int level, t_ast *ast) -{ - if (ast->tag == AST_CMD) - { - print_level(level); - printf("cmd: [ "); - ft_lstiter(ast->cmd_argv, token_put); - printf(" ] redirs: ["); - ft_lstiter(ast->redirs, token_put); - printf(" ]"); - } - else - { - /* print_level(level); */ - /* printf("SEP: %d\n", ast->op.sep); */ - print_level(level); - printf("{\n"); - - print_level(level); - printf(" left:\n"); - ast_print(level + 1, ast->op.left); - - printf("\n"); - print_level(level); - printf(" right:\n"); - ast_print(level + 1, ast->op.right); - - printf("\n"); - print_level(level); - printf("}\n"); - } -} - -int main(int argc, char **argv, char **envp) -{ - (void)argc; - (void)argv; - /* (void)envp; */ - t_path path; - t_env env; - /* char *line; */ - /* int ret; */ - env = env_from_array(envp); - path = path_update(NULL, env_search(env, "PATH")); - - t_ftlst *args1 = NULL; - ft_lstpush_back(&args1, ft_lstnew(token_new(TAG_STR, "ls"))); - ft_lstpush_back(&args1, ft_lstnew(token_new(TAG_STR, "-l"))); - - t_ftlst *args2 = NULL; - ft_lstpush_back(&args2, ft_lstnew(token_new(TAG_STR, "cat"))); - ft_lstpush_back(&args2, ft_lstnew(token_new(TAG_STR, "-e"))); - /* ft_lstpush_back(&args2, ft_lstnew(token_new(TAG_STR, "je"))); */ + void token_debug(void *v) + { + t_token *t; - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "ls"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "-a"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "-l"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "$$LFS$TERM$TERM."))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "*.c"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "src.c include*.h"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "$A$B"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "\\$TERM"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "$TER\\M"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "\\\\"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_SINGLE, "''''$TEST\\TEST"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE, ",$TEST,$B,"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE , "$TEST"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST"))); */ - /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_SINGLE, "$TEST"))); */ + t= v; + printf("[%4d %d] (%s)\n", t->tag, !!(t->tag & TAG_STICK), t->content); + } - t_ftlst *redirs = NULL; - ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_REDIR_OUT, NULL))); - ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_STR, "bonjour"))); - /* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_REDIR_APPEND, NULL))); */ - /* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_STR, "yo"))); */ - /* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_REDIR_OUT, NULL))); */ - /* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_STR, "yo1"))); */ + int main(int argc, char **argv, char **envp) + { + t_path path; + t_env env; - t_ast *cmd1 = ast_new(AST_CMD); - cmd1->cmd_argv = args1; - cmd1->redirs = NULL; + env = env_from_array(envp); + path = path_update(NULL, env_search(env, "PATH")); + //#<{(| printf("%s\n", argv[2]); |)}># - t_ast *cmd2 = ast_new(AST_CMD); - cmd2->cmd_argv = args2; - cmd2->redirs = NULL; + if (argc == 3 && ft_strcmp(argv[1], "-c") == 0) + { + //printf("%s\n", argv[2]); + t_ftlst *lex_out = lexer(ft_strdup(argv[2])); + //ft_lstiter(lex_out, token_debug); - t_ast *op_ast = ast_new(AST_OP); - op_ast->op.left = cmd1; - op_ast->op.right = cmd2; - op_ast->op.sep = TAG_PIPE; + parse(lex_out); + // <{(| printf("===cmd_argv===\n"); |)}> + // <{(| ft_lstiter(parser_out->ast->cmd_argv, token_debug); |)}> + // <{(| printf("===redirs===\n"); |)}> + // <{(| ft_lstiter(parser_out->ast->redirs, token_debug); |)}> + //int fds[2] = {MS_NO_FD, MS_NO_FD}; + //int eval_out = eval_cmd(fds, env, path, parser_out->ast); + //(void)eval_out; + } - int fds[2] = {MS_NO_FD, MS_NO_FD}; - printf("eval %d\n", eval(fds, env, path, op_ast)); + //ft_htdestroy(path, free); + //ft_vecdestroy(env, free); - /* char **as = preprocess(l, env); */ - /* printf("%p\n", as); */ - /* printf("%p\n", *as); */ - /* char **tmp = as; */ - /* while (*as != NULL) */ - /* puts(*as++); */ - /* ft_split_destroy(tmp); */ - ft_htdestroy(path, free); - ft_vecdestroy(env, free); - return (0); + return (0); } +// void token_put(void *v) +// { +// t_token *t; +// +// t= v; +// printf("%s ", t->content); +// } +// +// void print_level(int level) +// { +// while (level-- > 0) +// printf(" "); +// } +// +// void ast_print(int level, t_ast *ast) +// { +// if (ast->tag == AST_CMD) +// { +// print_level(level); +// printf("cmd: [ "); +// ft_lstiter(ast->cmd_argv, token_put); +// printf(" ] redirs: ["); +// ft_lstiter(ast->redirs, token_put); +// printf(" ]"); +// } +// else +// { +// /* print_level(level); */ +// /* printf("SEP: %d\n", ast->op.sep); */ +// print_level(level); +// printf("{\n"); +// +// print_level(level); +// printf(" left:\n"); +// ast_print(level + 1, ast->op.left); +// +// printf("\n"); +// print_level(level); +// printf(" right:\n"); +// ast_print(level + 1, ast->op.right); +// +// printf("\n"); +// print_level(level); +// printf("}\n"); +// } +// } + +// int main(int argc, char **argv, char **envp) +// { +// (void)argc; +// (void)argv; +// /* (void)envp; */ +// t_path path; +// t_env env; +// /* char *line; */ +// /* int ret; */ +// env = env_from_array(envp); +// path = path_update(NULL, env_search(env, "PATH")); +// +// t_ftlst *args1 = NULL; +// ft_lstpush_back(&args1, ft_lstnew(token_new(TAG_STR, "ls"))); +// ft_lstpush_back(&args1, ft_lstnew(token_new(TAG_STR, "-l"))); +// +// t_ftlst *args2 = NULL; +// ft_lstpush_back(&args2, ft_lstnew(token_new(TAG_STR, "cat"))); +// ft_lstpush_back(&args2, ft_lstnew(token_new(TAG_STR, "-e"))); +// /* ft_lstpush_back(&args2, ft_lstnew(token_new(TAG_STR, "je"))); */ +// +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "ls"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "-a"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "-l"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "$$LFS$TERM$TERM."))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "*.c"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "src.c include*.h"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "$A$B"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "\\$TERM"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "$TER\\M"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "\\\\"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_SINGLE, "''''$TEST\\TEST"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE, ",$TEST,$B,"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE , "$TEST"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST"))); */ +// /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_SINGLE, "$TEST"))); */ +// +// t_ftlst *redirs = NULL; +// ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_REDIR_OUT, NULL))); +// ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_STR, "bonjour"))); +// /* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_REDIR_APPEND, NULL))); */ +// /* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_STR, "yo"))); */ +// /* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_REDIR_OUT, NULL))); */ +// /* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_STR, "yo1"))); */ +// +// t_ast *cmd1 = ast_new(AST_CMD); +// cmd1->cmd_argv = args1; +// cmd1->redirs = NULL; +// +// t_ast *cmd2 = ast_new(AST_CMD); +// cmd2->cmd_argv = args2; +// cmd2->redirs = NULL; +// +// t_ast *op_ast = ast_new(AST_OP); +// op_ast->op.left = cmd1; +// op_ast->op.right = cmd2; +// op_ast->op.sep = TAG_PIPE; +// +// +// int fds[2] = {MS_NO_FD, MS_NO_FD}; +// printf("eval %d\n", eval(fds, env, path, op_ast)); +// +// /* char **as = preprocess(l, env); */ +// /* printf("%p\n", as); */ +// /* printf("%p\n", *as); */ +// /* char **tmp = as; */ +// /* while (*as != NULL) */ +// /* puts(*as++); */ +// /* ft_split_destroy(tmp); */ +// ft_htdestroy(path, free); +// ft_vecdestroy(env, free); +// return (0); +// } ///////////////////////////////////////////////////////////////////////////////////////// // lexer main diff --git a/src/parse/parse.c b/src/parse/parse.c index 198ac65..cc4d56d 100755 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ -/* Updated: 2020/06/18 12:48:20 by nahaddac ### ########.fr */ +/* Updated: 2020/06/19 13:28:12 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -104,27 +104,58 @@ t_ret *parse_op(t_ftlst *input) return ret_wrap_ast(ast, right_ret->rest); } -t_ret *parse_expr(t_ftlst *input) +t_ret *parse_expr(t_ftlst *input) { - t_ret *tmp; - enum e_token_tag tag; - - tag = ((t_token*)input->data)->tag; - if (tag == TAG_PARENT_OPEN) - { - tmp = parse_op(input->next); - input = tmp->rest; - tag = ((t_token*)input->data)->tag; - if (tag != TAG_PARENT_CLOSE) - return (NULL); - input = input->next; - tmp->rest = input; - return tmp; - } - return parse_cmd(input); + t_ret *tmp; + enum e_token_tag tag; + tag = ((t_token*)input->data)->tag; + if (tag == TAG_PARENT_OPEN) + { + tmp = parse_op(input->next); + input = tmp->rest; + tag = ((t_token*)input->data)->tag; + if (tag != TAG_PARENT_CLOSE) + return (NULL); + input = input->next; + t_ast *new_ast = ast_new(AST_PARENT); + new_ast = tmp->ast; + tmp->ast = new_ast; + if (tag & TAG_IS_REDIR) + { + while(input != NULL) + { + push_token(&tmp->ast->redirs, input->data); + if (tag & TAG_IS_STR && tag & TAG_STICK) + input = input->next; + else if (tag & TAG_IS_REDIR) + input = input->next; + else + break; + tag = ((t_token *)input->data)->tag; + } + } + tmp->rest = input; + return tmp; + } + return parse_cmd(input); } t_ret *parse(t_ftlst *input) { - return parse_op(input); + t_ret *ret; + t_ftlst *in_f; + + in_f = input; + if (input == NULL) + return NULL; + if (!(ret = malloc(sizeof(t_ret) * 1))) + return (NULL); + ret->ast = NULL; + ret->rest = NULL; + if((ret->unexpected = error_syntax_simple(input)) != NULL) + printf("%s\n", ret->unexpected->content); + ret = parse_op(in_f); + ast_destroy(ret->ast); + ft_lstdestroy(&ret->rest, (void (*)(void*))token_destroy); + return (NULL); } diff --git a/src/parse/parse_error.c b/src/parse/parse_error.c new file mode 100644 index 0000000..b721c28 --- /dev/null +++ b/src/parse/parse_error.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/18 15:09:48 by nahaddac #+# #+# */ +/* Updated: 2020/06/19 12:46:56 by nahaddac ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +t_token *error_syntax_parent(t_ftlst *in) +{ + int op; + int cl; + t_token *tk; + + op = 0; + cl = 0; + while(in != NULL) + { + tk = in->data; + if (tk->tag & TAG_PARENT_OPEN) + op++; + if(tk->tag & TAG_PARENT_CLOSE) + cl++; + if (cl && op == 0) + { + tk->content = ft_strjoin3( + "minishell: syntax error near unexpected token `", + tk->content, "'"); + return tk; + } + in = in->next; + } + return NULL; +} + +int out_error_first(t_token *tk) +{ + int i; + + i = 0; + if(tk->tag & TAG_IS_SEP) + return(1); + if (tk->tag & TAG_IS_REDIR) + { + while(tk->content[i]) + i++; + if (tk->tag & TAG_REDIR_APPEND && i <= 2) + return (0); + else + return(1); + } + return(0); +} + +t_token *error_syntax_simple(t_ftlst *in) +{ + t_token *tk; + size_t i; + t_ftlst *first; + + tk = in->data; + first = in; + if(tk->tag & TAG_IS_SEP || (tk->tag & TAG_IS_REDIR)) + { + if (out_error_first(tk)) + { + i = ft_strlen(tk->content); + if (i >= 2) + tk->content[2] = '\0'; + tk->content = + ft_strjoin3("minishell: syntax error near unexpected token `", + tk->content, "'"); + return(tk); + } + } + while(in != NULL) + { + i = 0; + tk = in->data; + if(tk->tag & TAG_IS_SEP || (tk->tag & TAG_IS_REDIR)) + { + if (((t_token *)in->next->data)->tag & + ((t_token*)in->next->data)->tag & TAG_IS_SEP || + (((t_token*)in->next->data)->tag & TAG_IS_REDIR)) + { + tk = in->next->data; + i = ft_strlen(tk->content); + if (i >= 3) + tk->content[3] = '\0'; + tk->content = + ft_strjoin3("minishell: syntax error near unexpected token `", + tk->content, "'"); + return(tk); + } + } + in = in->next; + } + return error_syntax_parent(first); + +} |
