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 /src/eval/cmd.c | |
| parent | 10b54ed457ec0999e9289811401907d3a88970dc (diff) | |
| download | minishell-b16d4b834d95d5cc0757d09e74fe6042e55f5440.tar.gz minishell-b16d4b834d95d5cc0757d09e74fe6042e55f5440.tar.bz2 minishell-b16d4b834d95d5cc0757d09e74fe6042e55f5440.zip | |
Added better error handling in eval
Diffstat (limited to 'src/eval/cmd.c')
| -rw-r--r-- | src/eval/cmd.c | 80 |
1 files changed, 40 insertions, 40 deletions
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); } |
