From 29e1af2b65d097e533189db4e7d5e20534c17b35 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 14 Jul 2020 10:04:44 +0200 Subject: Refactoring error handling during parsing --- include/ast.h | 4 ++-- include/eval.h | 34 ++++++----------------------- include/minishell.h | 27 ++++++++++++++++++++++- minishell_test | 2 +- sllsls | 1 - src/builtin/exit.c | 4 ++-- src/env.c | 5 ++++- src/error.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ src/eval/cmd.c | 4 +++- src/eval/error.c | 56 ------------------------------------------------ src/main.c | 31 ++++++++++++++++++++------- src/parse/parse.c | 10 ++++----- src/parse/parse_error.c | 2 +- src/preprocess.c | 2 +- src/utils.c | 4 +++- 15 files changed, 133 insertions(+), 110 deletions(-) delete mode 100644 sllsls create mode 100644 src/error.c delete mode 100644 src/eval/error.c diff --git a/include/ast.h b/include/ast.h index 597c444..a767444 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:38 by charles #+# #+# */ -/* Updated: 2020/06/19 13:29:27 by charles ### ########.fr */ +/* Updated: 2020/07/14 09:57:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,7 +77,7 @@ typedef struct s_ast typedef struct s_ret { - t_token *unexpected; + bool syntax_error; t_ast *ast; t_ftlst *rest; } t_ret; diff --git a/include/eval.h b/include/eval.h index 38671aa..261e1a2 100644 --- a/include/eval.h +++ b/include/eval.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:30 by charles #+# #+# */ -/* Updated: 2020/07/13 09:54:40 by charles ### ########.fr */ +/* Updated: 2020/07/14 09:35:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,30 +48,8 @@ extern pid_t g_child_pid; ** op.c */ -int eval_op(int fds[2], t_env env, t_path path, t_ast *ast); -int eval(int fds[2], t_env env, t_path path, t_ast *ast); - -enum e_error -{ - ERROR_AMBIGUOUS_REDIR, - ERROR_OPEN, - ERROR_CMD_NOT_FOUND, - ERROR_SYNTAX, -}; - -typedef struct -{ - enum e_error id; - int status; - char *msg; // if NULL call strerror - // char *basename; -} t_error; - -/* -** error.c -*/ - -void error_eval_put(enum e_error id, char *unexpected); +int eval_op(int fds[2], t_env env, t_path path, t_ast *ast); +int eval(int fds[2], t_env env, t_path path, t_ast *ast); /* ** cmd.c @@ -90,8 +68,8 @@ bool redir_extract(t_ftlst *redirs, t_env env, int fds[2]); ** exec.c */ -bool exec_is_path(char *exec_name); -bool exec_is_valid(char *exec_path); -char *exec_search_path(t_path path, char *path_var, char *exec_name); +bool exec_is_path(char *exec_name); +bool exec_is_valid(char *exec_path); +char *exec_search_path(t_path path, char *path_var, char *exec_name); #endif diff --git a/include/minishell.h b/include/minishell.h index f2b237f..9405109 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/06/23 08:32:46 by charles ### ########.fr */ +/* Updated: 2020/07/14 09:35:14 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,6 +47,8 @@ typedef t_ftht* t_path; typedef t_ftvec* t_env; +extern int g_last_status_code; + /* ** path.c */ @@ -103,4 +105,27 @@ int builtin_exit(char **argv, t_env env); char **preprocess(t_ftlst **tokens, t_env env); char *preprocess_filename(t_ftlst **tokens, t_env env); +/* +** error.c +*/ + +enum e_error +{ + ERROR_AMBIGUOUS_REDIR, + ERROR_OPEN, + ERROR_CMD_NOT_FOUND, + ERROR_SYNTAX, + ERROR_CMD_FOUND_ERROR, +}; + +typedef struct +{ + enum e_error id; + int status; + char *msg; // if NULL call strerror + // char basename; +} t_error; + +void error_eval_put(enum e_error id, char *unexpected); + #endif diff --git a/minishell_test b/minishell_test index 3f2db95..cc041d1 160000 --- a/minishell_test +++ b/minishell_test @@ -1 +1 @@ -Subproject commit 3f2db95d5563c9c2b92a0531ddee28f79f438706 +Subproject commit cc041d1901daa8be9197a59d963466fdc7e2b404 diff --git a/sllsls b/sllsls deleted file mode 100644 index e601e59..0000000 --- a/sllsls +++ /dev/null @@ -1 +0,0 @@ -salut diff --git a/src/builtin/exit.c b/src/builtin/exit.c index aa5d10b..15271f6 100644 --- a/src/builtin/exit.c +++ b/src/builtin/exit.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:16 by charles #+# #+# */ -/* Updated: 2020/06/23 10:03:39 by charles ### ########.fr */ +/* Updated: 2020/07/13 15:35:44 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ int builtin_exit(char **argv, t_env env) (void)env; if (argv[1] == NULL) - status = 0; + status = g_last_status_code; else if (argv[2] != NULL) { // replace with minishell error system diff --git a/src/env.c b/src/env.c index 89dafeb..bdc2498 100644 --- a/src/env.c +++ b/src/env.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */ -/* Updated: 2020/06/18 14:33:26 by charles ### ########.fr */ +/* Updated: 2020/07/13 11:09:39 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ */ #include "minishell.h" +#include "eval.h" #define ENV_VEC_DEFAULT_SIZE 64 @@ -87,6 +88,8 @@ char *env_search_first_match(t_env env, const char *haystack) len = 0; while (ft_isalnum(haystack[len]) || haystack[len] == '_') len++; + if (haystack[0] == '?') + return (ft_itoa(g_last_status_code)); // FIXME leak if (len == 0) return (NULL); i = -1; diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..f5848a6 --- /dev/null +++ b/src/error.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/14 11:02:52 by charles #+# #+# */ +/* Updated: 2020/07/14 09:23:38 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "eval.h" + +static t_error g_errors[] = +{ + {ERROR_AMBIGUOUS_REDIR, 1, "ambiguous redirect"}, + {ERROR_OPEN, 1, NULL}, + {ERROR_CMD_NOT_FOUND, 127, "command not found"}, + /* {ERROR_CMD_FOUND_ERROR, 126, NULL}, */ + {ERROR_SYNTAX, 2, "syntax error near unexpected token "}, +}; + +t_error *st_error_get(enum e_error id) +{ + size_t i; + t_error *match; + + match = NULL; + i = 0; + while (i < sizeof(g_errors) / sizeof(t_error)) + { + if (g_errors[i].id == id) + match = &g_errors[i]; + i++; + } + return (match); +} + +void error_eval_put(enum e_error id, char *unexpected) +{ + t_error *err; + + err = st_error_get(id); + ft_putstr_fd("minishell: ", STDERR_FILENO); + ft_putstr_fd(unexpected, STDERR_FILENO); + ft_putstr_fd(": ", STDERR_FILENO); + if (err->msg == NULL) + ft_putendl_fd(strerror(errno), STDERR_FILENO); + else + ft_putendl_fd(err->msg, STDERR_FILENO); +} + +int error_status(enum e_error id) +{ + return (st_error_get(id)->status); +} diff --git a/src/eval/cmd.c b/src/eval/cmd.c index fc73ed0..e79cb88 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,13 +6,14 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/07/13 09:54:12 by charles ### ########.fr */ +/* Updated: 2020/07/13 11:03:09 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "eval.h" pid_t g_child_pid = -1; +int g_last_status_code = 0; /* ** \brief Wrap a function in a fork @@ -97,6 +98,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) param.argv = argv; param.env = env; int ret = fork_wrap(fds, ¶m, &forked_cmd); + g_last_status_code = ret; ft_split_destroy(argv); return (ret); } diff --git a/src/eval/error.c b/src/eval/error.c deleted file mode 100644 index 016a751..0000000 --- a/src/eval/error.c +++ /dev/null @@ -1,56 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* error.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/06/14 11:02:52 by charles #+# #+# */ -/* Updated: 2020/06/14 12:19:14 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "eval.h" - -static t_error g_errors[] = -{ - {ERROR_AMBIGUOUS_REDIR, 1, "ambiguous redirect"}, - {ERROR_OPEN, 1, NULL}, - {ERROR_CMD_NOT_FOUND, 127, "command not found"}, - {ERROR_SYNTAX, 2, "syntax error near unexpected token "}, -}; - -t_error *st_error_get(enum e_error id) -{ - size_t i; - t_error *match; - - match = NULL; - i = 0; - while (i < sizeof(g_errors) / sizeof(t_error)) - { - if (g_errors[i].id == id) - match = &g_errors[i]; - i++; - } - return (match); -} - -void error_eval_put(enum e_error id, char *unexpected) -{ - t_error *err; - - err = st_error_get(id); - ft_putstr_fd("minishell: ", STDERR_FILENO); - ft_putstr_fd(unexpected, STDERR_FILENO); - ft_putstr_fd(": ", STDERR_FILENO); - if (err->msg == NULL) - ft_putendl_fd(strerror(errno), STDERR_FILENO); - else - ft_putendl_fd(err->msg, STDERR_FILENO); -} - -int error_status(enum e_error id) -{ - return (st_error_get(id)->status); -} diff --git a/src/main.c b/src/main.c index e5c9bf9..ee98e1a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/07/14 09:41:42 by nahaddac ### ########.fr */ +/* Updated: 2020/07/14 10:03:35 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,6 +57,23 @@ void signal_sigquit(int signum) } } +void signal_sigterm(int signum) +{ + (void)signum; +} + + +/* +** TODO +** $? +** syntax error +** signal +** pipeline +** cmd are path +** interpolation order +** PATH with no permission +*/ + int main(int argc, char **argv, char **envp) { t_path path; @@ -68,6 +85,7 @@ int main(int argc, char **argv, char **envp) signal(SIGINT, signal_sigint); signal(SIGQUIT, signal_sigquit); + signal(SIGTERM, signal_sigterm); if (argc == 3 && ft_strcmp(argv[1], "-l") == 0) { @@ -85,8 +103,8 @@ int main(int argc, char **argv, char **envp) /* ft_lstiter(lex_out, token_debug); */ t_ret *parser_out = parse(lex_out); - if (parser_out == NULL || parser_out->unexpected != NULL) - return (1); + if (parser_out == NULL || parser_out->syntax_error) + return (1); /* ast_print(0, parser_out->ast); */ /* printf("\n"); */ @@ -119,11 +137,8 @@ int main(int argc, char **argv, char **envp) return (1); t_ret *parser_out = parse(lex_out); - if (parser_out == NULL || parser_out->unexpected != NULL) - { - printf("%s\n",((t_token *)parser_out->unexpected)->content); + if (parser_out == NULL || parser_out->syntax_error) return (1); - } int fds[2] = {MS_NO_FD, MS_NO_FD}; int eval_out = eval(fds, env, path, parser_out->ast); @@ -134,7 +149,7 @@ int main(int argc, char **argv, char **envp) ft_htdestroy(path, free); ft_vecdestroy(env, free); - return (0); + return (g_last_status_code); } // else // { diff --git a/src/parse/parse.c b/src/parse/parse.c index 6e4721a..b2532ac 100644 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ -/* Updated: 2020/07/14 09:43:41 by nahaddac ### ########.fr */ +/* Updated: 2020/07/14 10:03:00 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ t_ret *ret_wrap_ast(t_ast *ast, t_ftlst *rest) if ((ret = malloc(sizeof(t_ret))) == NULL) return (NULL); - ret->unexpected = NULL; + ret->syntax_error = false; ret->rest = rest; ret->ast = ast; return ret; @@ -165,17 +165,15 @@ t_ret *parse_expr(t_ftlst *input) t_ret *parse(t_ftlst *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))) - return (ret); + /* if((ret->unexpected = error_syntax_simple(input))) */ + /* return (ret); */ ret = parse_op(input); return (ret); } diff --git a/src/parse/parse_error.c b/src/parse/parse_error.c index b63715d..3d988ae 100644 --- a/src/parse/parse_error.c +++ b/src/parse/parse_error.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/18 15:09:48 by nahaddac #+# #+# */ -/* Updated: 2020/07/14 09:43:35 by nahaddac ### ########.fr */ +/* Updated: 2020/07/14 10:01:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/preprocess.c b/src/preprocess.c index eb6ac3b..13f297c 100644 --- a/src/preprocess.c +++ b/src/preprocess.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 08:58:49 by charles #+# #+# */ -/* Updated: 2020/06/16 13:49:38 by charles ### ########.fr */ +/* Updated: 2020/07/13 11:08:18 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/utils.c b/src/utils.c index 84a1920..7c1f496 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/07/14 09:42:14 by nahaddac ### ########.fr */ +/* Updated: 2020/07/14 10:00:16 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,6 +54,8 @@ size_t utils_var_end(char *name) { size_t i; + if (*name == '?') + return (2); if (ft_isdigit(*name)) return (0); i = 0; -- cgit