diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-09-10 15:08:28 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-09-10 15:08:28 +0200 |
| commit | b16d4b834d95d5cc0757d09e74fe6042e55f5440 (patch) | |
| tree | aa64f6d4397a0c55337224c8cf474da73750ac30 | |
| parent | 10b54ed457ec0999e9289811401907d3a88970dc (diff) | |
| download | minishell-b16d4b834d95d5cc0757d09e74fe6042e55f5440.tar.gz minishell-b16d4b834d95d5cc0757d09e74fe6042e55f5440.tar.bz2 minishell-b16d4b834d95d5cc0757d09e74fe6042e55f5440.zip | |
Added better error handling in eval
| -rw-r--r-- | include/error.h | 24 | ||||
| -rw-r--r-- | include/eval.h | 10 | ||||
| -rw-r--r-- | include/minishell.h | 2 | ||||
| m--------- | minishell_test | 0 | ||||
| -rw-r--r-- | src/builtin/exit.c | 2 | ||||
| -rw-r--r-- | src/builtin/unset.c | 6 | ||||
| -rw-r--r-- | src/env.c | 4 | ||||
| -rw-r--r-- | src/error.c | 28 | ||||
| -rw-r--r-- | src/eval/cmd.c | 80 | ||||
| -rw-r--r-- | src/eval/local.c | 2 | ||||
| -rw-r--r-- | src/eval/op.c | 18 | ||||
| -rw-r--r-- | src/eval/redir.c | 27 | ||||
| -rw-r--r-- | src/lexer/lexer.c | 6 | ||||
| -rw-r--r-- | src/main.c | 20 | ||||
| -rw-r--r-- | src/signal.c | 4 |
15 files changed, 132 insertions, 101 deletions
diff --git a/include/error.h b/include/error.h index 941f1b2..2326d1a 100644 --- a/include/error.h +++ b/include/error.h @@ -6,7 +6,7 @@ /* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/08/27 20:34:25 by charles #+# #+# */ -/* Updated: 2020/08/27 20:38:52 by charles ### ########.fr */ +/* Updated: 2020/09/10 14:48:42 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,17 +20,23 @@ ** error.c */ -typedef enum +enum { - ERR_FATAL = -1, - ERR_NONE = 0, - ERR_AMBIGUOUS_REDIR = 1, - ERR_OPEN = 1, - ERR_CMD_NOT_FOUND = 127, - ERR_SYNTAX = 2, -} t_err; + ERR_NONE = 1024, + ERR_FATAL, + ERR_SYNTAX, + ERR_OPEN, + ERR_AMBIGUOUS_REDIR, + ERR_CMD_NOT_FOUND, + ERR_IS_DIRECTORY, + ERR_ERRNO, +}; + +typedef int t_err; void errorf(const char *format, ...); void verrorf(const char *format, va_list ap); +void error_set_status(int status); +int error_get_status(int status); #endif diff --git a/include/eval.h b/include/eval.h index a23b249..7139f34 100644 --- a/include/eval.h +++ b/include/eval.h @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:30 by charles #+# #+# */ -/* Updated: 2020/09/09 17:27:21 by charles ### ########.fr */ +/* Updated: 2020/09/10 14:14:56 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,9 +39,9 @@ typedef struct t_builtin_entry *builtin; } t_fork_param_cmd; -# define MS_NO_FD -2 -# define FDS_WRITE 1 -# define FDS_READ 0 +# define FD_NONE -2 +# define FD_WRITE 1 +# define FD_READ 0 extern pid_t g_child_pid; @@ -64,7 +64,7 @@ t_ftlst *split_token(t_ftlst **lst, enum e_tok); ** redir.c */ -bool redir_extract(t_tok_lst **redirs, t_env env, int fds[2]); +int redir_extract(t_tok_lst **redirs, t_env env, int fds[2]); /* ** exec.c diff --git a/include/minishell.h b/include/minishell.h index 085f082..7e755a3 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -51,7 +51,7 @@ typedef t_ftht* t_path; typedef t_ftvec* t_env; -extern int g_last_status_code; +extern int g_last_status; extern char *g_basename; /* diff --git a/minishell_test b/minishell_test -Subproject f571ffc6beb34cf0d4a171e5b5aa70a1d2b8365 +Subproject c0b1a90cf9c52a0c9b1623ac695516031d5ccdb diff --git a/src/builtin/exit.c b/src/builtin/exit.c index 640fc01..52a4462 100644 --- a/src/builtin/exit.c +++ b/src/builtin/exit.c @@ -24,7 +24,7 @@ int builtin_exit(char **argv, t_env env) (void)env; if (argv[1] == NULL) - status = g_last_status_code; + status = g_last_status; else { errno = 0; diff --git a/src/builtin/unset.c b/src/builtin/unset.c index 367f063..30facd3 100644 --- a/src/builtin/unset.c +++ b/src/builtin/unset.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:51 by charles #+# #+# */ -/* Updated: 2020/07/19 18:47:36 by charles ### ########.fr */ +/* Updated: 2020/09/10 13:49:58 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,11 +31,11 @@ int builtin_unset(char **argv, t_env env) { errorf("unset: `%s': not a valid identifier\n", argv[i]); status = 1; - continue; // put invalid identifier + continue ; } found_index = env_search_index(env, argv[i]); if (found_index == -1) - continue; + continue ; ft_vecremove(env, found_index, free); } return (status); @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */ -/* Updated: 2020/09/09 16:56:39 by charles ### ########.fr */ +/* Updated: 2020/09/10 14:19:42 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -105,7 +105,7 @@ char *env_search_first_match(t_env env, const char *haystack) while (ft_isalnum(haystack[len]) || haystack[len] == '_') len++; if (haystack[0] == '?') - return (ft_itoa(g_last_status_code)); // FIXME leak (static buffer) + return (ft_itoa(g_last_status)); // FIXME leak (static buffer) if (len == 0) return (NULL); i = -1; diff --git a/src/error.c b/src/error.c index e19a9ed..6c24635 100644 --- a/src/error.c +++ b/src/error.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 11:02:52 by charles #+# #+# */ -/* Updated: 2020/08/27 10:44:08 by charles ### ########.fr */ +/* Updated: 2020/09/10 14:48:19 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,3 +49,29 @@ void verrorf(const char *format, va_list ap) } } } + +static int g_error_to_status[] = { + [ERR_NONE] = 0, + [ERR_AMBIGUOUS_REDIR] = 1, + [ERR_OPEN] = 1, + [ERR_CMD_NOT_FOUND] = 127, + [ERR_SYNTAX] = 2, + [ERR_IS_DIRECTORY] = 126, + [ERR_ERRNO] = 126, +}; + +int error_get_status(int status) +{ + return (g_error_to_status[status]); +} + +void error_set_status(int status) +{ + if (status == ERR_FATAL) + exit(1); + /* printf("%d\n", status); */ + if (status < ERR_NONE) + g_last_status = status; + else + g_last_status = g_error_to_status[status]; +} diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 5130a45..107a2a6 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,14 +6,14 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/09/09 17:40:54 by charles ### ########.fr */ +/* Updated: 2020/09/10 15:05:54 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "eval.h" pid_t g_child_pid = -1; -int g_last_status_code = 0; +int g_last_status = 0; void token_debug(void *v); /* @@ -33,19 +33,19 @@ int fork_wrap( pid_t child_pid; if ((child_pid = fork()) == -1) - return (-1); + return (ERR_FATAL); if (child_pid == 0) { - if ((fds[FDS_READ] != MS_NO_FD && dup2(fds[FDS_READ], STDIN_FILENO) == -1) || - (fds[FDS_WRITE] != MS_NO_FD && dup2(fds[FDS_WRITE], STDOUT_FILENO) == -1)) + if ((fds[FD_READ] != FD_NONE && dup2(fds[FD_READ], STDIN_FILENO) == -1) || + (fds[FD_WRITE] != FD_NONE && dup2(fds[FD_WRITE], STDOUT_FILENO) == -1)) exit(EXIT_FAILURE); - if ((status = wrapped(passed)) == -1) + if ((status = wrapped(passed)) == ERR_FATAL) exit(EXIT_FAILURE); exit(status); } g_child_pid = child_pid; wait(&child_pid); - close(fds[FDS_WRITE]); + close(fds[FD_WRITE]); // also read end? return (WEXITSTATUS(child_pid)); } @@ -53,38 +53,41 @@ int fork_wrap( int forked_cmd(void *void_param) { t_fork_param_cmd *param; - int ret; + int status; + struct stat statbuf; param = void_param; ft_vecpop(param->env_local, NULL); if (ft_vecswallow_at(param->env, param->env->size - 1, param->env_local) == NULL) { ft_vecdestroy(param->env_local, free); - return (-1); + return (ERR_FATAL); } if (param->builtin != NULL) return (param->builtin->func(param->argv, param->env)); else { + if (stat(param->exec_path, &statbuf) == -1) + { + errorf("%s: %s\n", param->exec_path, strerror(errno)); + return (error_get_status(ERR_ERRNO)); + } + if (S_ISDIR(statbuf.st_mode)) + { + errorf("%s: Is a directory\n", param->exec_path); + return (error_get_status(ERR_IS_DIRECTORY)); + } + errno = 0; - ret = execve(param->exec_path, param->argv, (char**)param->env->data); - if (ret == -1) + status = execve(param->exec_path, param->argv, (char**)param->env->data); + if (status == -1) { if (errno == ENOEXEC) - return (0); - struct stat statbuf; - if (stat(param->exec_path, &statbuf) != -1 && S_ISDIR(statbuf.st_mode)) - { - errorf("%s: Is a directory\n", param->exec_path); - ret = 126; - } - else - { - errorf("%s: %s\n", param->exec_path, strerror(errno)); - ret = 126; - } + return (error_get_status(ERR_NONE)); + errorf("%s: %s\n", param->exec_path, strerror(errno)); + return (error_get_status(ERR_ERRNO)); } - return (ret); + return (status); } } @@ -92,16 +95,18 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) { t_fork_param_cmd param; char **argv; + int status; - if (!redir_extract(&ast->redirs, env, fds)) + if ((status = redir_extract(&ast->redirs, env, fds)) != ERR_NONE) { ast->redirs = NULL; - return (-1); + return (status); } ast->redirs = NULL; if ((param.env_local = env_from_array((char*[]){NULL})) == NULL) - return (-1); - variable_extract(&ast->cmd_argv, env, param.env_local); + return (ERR_FATAL); + if (!variable_extract(&ast->cmd_argv, env, param.env_local)) + return (ERR_FATAL); /* char **strs = preprocess(&start, env); */ /* */ @@ -115,17 +120,16 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) /* ft_vecdestroy(param.env_local, free); */ /* return (-1); */ /* } */ - /* g_last_status_code = 0; */ + /* g_last_status = 0; */ /* return (0); */ /* } */ if ((argv = preprocess(&ast->cmd_argv, env)) == NULL) { ast->cmd_argv = NULL; - return (-1); + return (ERR_FATAL); } - // can have no command (e.g `< file`) if (argv[0] == NULL) return (0); param.builtin = builtin_search_func(argv[0]); @@ -135,23 +139,19 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) param.exec_path = exec_search_path(path, env_search(env, "PATH"), argv[0]); if (param.exec_path == NULL) { - g_last_status_code = 127; errorf("%s: command not found\n", argv[0]); ft_split_destroy(argv); - return (-1); // return error status + return (ERR_CMD_NOT_FOUND); } } else if (!param.builtin->forked) - { - g_last_status_code = param.builtin->func(argv, env); - return (g_last_status_code); - } + return (param.builtin->func(argv, env)); param.argv = argv; param.env = env; - int ret = fork_wrap(fds, ¶m, &forked_cmd); - g_last_status_code = ret; + status = fork_wrap(fds, ¶m, &forked_cmd); + /* g_last_status = status; */ ft_split_destroy(argv); ft_vecdestroy(param.env_local, free); - return (ret); + return (status); } diff --git a/src/eval/local.c b/src/eval/local.c index 13719cd..089a0c7 100644 --- a/src/eval/local.c +++ b/src/eval/local.c @@ -62,7 +62,7 @@ /* ft_vecdestroy(param.env_local, free); */ /* return (-1); */ /* } */ - /* g_last_status_code = 0; */ + /* g_last_status = 0; */ /* return (0); */ /* } */ diff --git a/src/eval/op.c b/src/eval/op.c index 36340be..e06c959 100644 --- a/src/eval/op.c +++ b/src/eval/op.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 15:27:22 by charles #+# #+# */ -/* Updated: 2020/08/28 16:54:39 by charles ### ########.fr */ +/* Updated: 2020/09/10 14:15:08 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,15 +19,15 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast) int right_fds[2]; int p[2]; - left_fds[FDS_READ] = fds[FDS_READ]; - left_fds[FDS_WRITE] = MS_NO_FD; - right_fds[FDS_READ] = MS_NO_FD; - right_fds[FDS_WRITE] = fds[FDS_WRITE]; + left_fds[FD_READ] = fds[FD_READ]; + left_fds[FD_WRITE] = FD_NONE; + right_fds[FD_READ] = FD_NONE; + right_fds[FD_WRITE] = fds[FD_WRITE]; if (ast->op.sep == TAG_PIPE) { pipe(p); - left_fds[FDS_WRITE] = p[FDS_WRITE]; - right_fds[FDS_READ] = p[FDS_READ]; + left_fds[FD_WRITE] = p[FD_WRITE]; + right_fds[FD_READ] = p[FD_READ]; } if ((status = eval(left_fds, env, path, ast->op.left)) == -1) return (-1); @@ -51,7 +51,7 @@ int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast) t_fork_param_parent param; if (!redir_extract(&ast->redirs, env, fds)) - return (-1); + return (ERR_FATAL); param.fds[0] = fds[0]; param.fds[1] = fds[1]; param.env = env; @@ -69,5 +69,5 @@ int eval(int fds[2], t_env env, t_path path, t_ast *ast) return (eval_op(fds, env, path, ast)); if (ast->tag == AST_CMD) return (eval_cmd(fds, env, path, ast)); - return (-1); + return (ERR_FATAL); } diff --git a/src/eval/redir.c b/src/eval/redir.c index e8796a5..9beef17 100644 --- a/src/eval/redir.c +++ b/src/eval/redir.c @@ -6,15 +6,15 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/15 11:05:34 by charles #+# #+# */ -/* Updated: 2020/08/28 17:14:19 by charles ### ########.fr */ +/* Updated: 2020/09/10 14:25:08 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "eval.h" -static bool st_open_replace(int *fd, char *filename, int oflag) +static int st_open_replace(int *fd, char *filename, int oflag) { - if (*fd != MS_NO_FD) + if (*fd != FD_NONE) close(*fd); if (oflag & O_CREAT) *fd = open(filename, oflag, 0644); @@ -22,15 +22,14 @@ static bool st_open_replace(int *fd, char *filename, int oflag) *fd = open(filename, oflag); if (*fd == -1) { - g_last_status_code = 1; errorf("%s: %s\n", filename, strerror(errno)); free(filename); - return (false); + return (ERR_OPEN); } - return (true); + return (ERR_NONE); } -bool redir_extract( +int redir_extract( t_tok_lst **redirs, t_env env, int fds[2]) @@ -40,10 +39,10 @@ bool redir_extract( char *filename; if (*redirs == NULL) - return (true); + return (ERR_NONE); if (!((*redirs)->tag & TAG_IS_REDIR) || (*redirs)->next == NULL || !((*redirs)->next->tag & TAG_IS_STR)) - return (false); + return (ERR_FATAL); curr = (*redirs)->next; after = NULL; while (curr != NULL && curr->tag & TAG_IS_STR) @@ -59,18 +58,18 @@ bool redir_extract( { tok_lst_destroy(redirs, free); tok_lst_destroy(&after, free); - return (false); + return (ERR_FATAL); } if (((*redirs)->tag == TAG_REDIR_IN - && !st_open_replace(&fds[FDS_READ], filename, O_RDONLY)) + && st_open_replace(&fds[FD_READ], filename, O_RDONLY) != ERR_NONE) || ((*redirs)->tag == TAG_REDIR_OUT - && !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC)) + && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC) != ERR_NONE) || ((*redirs)->tag == TAG_REDIR_APPEND - && !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND))) + && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND) != ERR_NONE)) { tok_lst_destroy(redirs, free); tok_lst_destroy(&after, free); - return (false); + return (ERR_FATAL); } tok_lst_destroy(redirs, free); free(filename); diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index befc9b8..122ab75 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -6,7 +6,7 @@ /* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 08:18:25 by nahaddac #+# #+# */ -/* Updated: 2020/09/10 08:33:29 by nahaddac ### ########.fr */ +/* Updated: 2020/09/10 13:58:08 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,9 +22,7 @@ int len_until_sep(char *input) { if (input[i] == '\\') { - i +=2; - if (input[i] == '\\') - ; + i += 2; if (input[i] == ' ') { while(input[++i] == ' ') @@ -6,7 +6,7 @@ /* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/09/09 16:19:00 by charles ### ########.fr */ +/* Updated: 2020/09/10 14:33:22 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,7 +69,7 @@ int main(int argc, char **argv, char **envp) // } // env_export(env, "_", env_exec_path); - g_last_status_code = 0; + g_last_status = 0; signal(SIGINT, signal_sigint); signal(SIGQUIT, signal_sigquit); signal(SIGTERM, signal_sigterm); @@ -104,9 +104,9 @@ int main(int argc, char **argv, char **envp) /* 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(fds, env, path, parser_out->ast); - (void)eval_out; + int fds[2] = {FD_NONE, FD_NONE}; + int status = eval(fds, env, path, parser_out->ast); + error_set_status(status); } else { @@ -134,9 +134,11 @@ int main(int argc, char **argv, char **envp) continue; } - int fds[2] = {MS_NO_FD, MS_NO_FD}; - int eval_out = eval(fds, env, path, parser_out->ast); - (void)eval_out; + int fds[2] = {FD_NONE, FD_NONE}; + int status = eval(fds, env, path, parser_out->ast); + if (status == ERR_FATAL) + exit(1); + error_set_status(status); print_prompt(); } if (ret != FTGL_EOF) @@ -145,5 +147,5 @@ int main(int argc, char **argv, char **envp) ft_htdestroy(path, free); ft_vecdestroy(env, free); - return (g_last_status_code); + return (g_last_status); } diff --git a/src/signal.c b/src/signal.c index e40df26..0dec80c 100644 --- a/src/signal.c +++ b/src/signal.c @@ -16,7 +16,7 @@ void signal_sigint(int signum) { (void)signum; - g_last_status_code = 130; + g_last_status = 130; if (g_child_pid != -1) { kill(g_child_pid, SIGINT); @@ -32,7 +32,7 @@ void signal_sigint(int signum) void signal_sigquit(int signum) { (void)signum; - g_last_status_code = 131; + g_last_status = 131; if (g_child_pid != -1) { kill(g_child_pid, SIGQUIT); |
