diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-09-10 19:46:07 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-09-10 20:32:25 +0200 |
| commit | 98990d5195e93154abbfd16eaa9d1fcc3572bc5c (patch) | |
| tree | 110a4cedf6ada1281cdc356b21ebe2b55acec32a /src/eval | |
| parent | b16d4b834d95d5cc0757d09e74fe6042e55f5440 (diff) | |
| download | minishell-98990d5195e93154abbfd16eaa9d1fcc3572bc5c.tar.gz minishell-98990d5195e93154abbfd16eaa9d1fcc3572bc5c.tar.bz2 minishell-98990d5195e93154abbfd16eaa9d1fcc3572bc5c.zip | |
Added errorf_ret helper, Removing error to status (just status code and fatal error)
Diffstat (limited to 'src/eval')
| -rw-r--r-- | src/eval/cmd.c | 43 | ||||
| -rw-r--r-- | src/eval/op.c | 14 | ||||
| -rw-r--r-- | src/eval/redir.c | 20 |
3 files changed, 34 insertions, 43 deletions
diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 107a2a6..36e5a00 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/09/10 15:05:54 by charles ### ########.fr */ +/* Updated: 2020/09/10 20:32:13 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,20 +33,19 @@ int fork_wrap( pid_t child_pid; if ((child_pid = fork()) == -1) - return (ERR_FATAL); + return (EVAL_FATAL); if (child_pid == 0) { 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)) == ERR_FATAL) + if ((status = wrapped(passed)) == EVAL_FATAL) // FIXME detect fatal in child (pipe ?) exit(EXIT_FAILURE); exit(status); } g_child_pid = child_pid; wait(&child_pid); close(fds[FD_WRITE]); - // also read end? return (WEXITSTATUS(child_pid)); } @@ -61,31 +60,25 @@ int forked_cmd(void *void_param) if (ft_vecswallow_at(param->env, param->env->size - 1, param->env_local) == NULL) { ft_vecdestroy(param->env_local, free); - return (ERR_FATAL); + return (EVAL_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)); - } + return (errorf_ret(126, "%s: %s\n", param->exec_path, strerror(errno))); if (S_ISDIR(statbuf.st_mode)) - { - errorf("%s: Is a directory\n", param->exec_path); - return (error_get_status(ERR_IS_DIRECTORY)); - } - + return (errorf_ret(126, "%s: Is a directory\n", param->exec_path)); + if (!(statbuf.st_mode & 0444)) + return (errorf_ret(126, "%s: %s\n", param->exec_path, strerror(EACCES))); errno = 0; status = execve(param->exec_path, param->argv, (char**)param->env->data); if (status == -1) { if (errno == ENOEXEC) - return (error_get_status(ERR_NONE)); - errorf("%s: %s\n", param->exec_path, strerror(errno)); - return (error_get_status(ERR_ERRNO)); + return (0); + return (errorf_ret(126, "%s: %s\n", param->exec_path, strerror(errno))); } return (status); } @@ -97,16 +90,12 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) char **argv; int status; - if ((status = redir_extract(&ast->redirs, env, fds)) != ERR_NONE) - { - ast->redirs = NULL; + if ((status = redir_extract(&ast->redirs, env, fds)) != 0) return (status); - } - ast->redirs = NULL; if ((param.env_local = env_from_array((char*[]){NULL})) == NULL) - return (ERR_FATAL); + return (EVAL_FATAL); if (!variable_extract(&ast->cmd_argv, env, param.env_local)) - return (ERR_FATAL); + return (EVAL_FATAL); /* char **strs = preprocess(&start, env); */ /* */ @@ -127,7 +116,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) if ((argv = preprocess(&ast->cmd_argv, env)) == NULL) { ast->cmd_argv = NULL; - return (ERR_FATAL); + return (EVAL_FATAL); } if (argv[0] == NULL) @@ -141,7 +130,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) { errorf("%s: command not found\n", argv[0]); ft_split_destroy(argv); - return (ERR_CMD_NOT_FOUND); + return (127); } } else if (!param.builtin->forked) @@ -150,8 +139,8 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) param.argv = argv; param.env = env; status = fork_wrap(fds, ¶m, &forked_cmd); - /* g_last_status = status; */ ft_split_destroy(argv); ft_vecdestroy(param.env_local, free); + g_last_status = status; return (status); } diff --git a/src/eval/op.c b/src/eval/op.c index e06c959..9a37736 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/09/10 14:15:08 by charles ### ########.fr */ +/* Updated: 2020/09/10 20:28:57 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,8 +29,9 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast) 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); + if ((status = eval(left_fds, env, path, ast->op.left)) == EVAL_FATAL) + return (EVAL_FATAL); + g_last_status = status; if ((ast->op.sep == TAG_AND && status != 0) || (ast->op.sep == TAG_PIPE && status != 0) || (ast->op.sep == TAG_OR && status == 0)) @@ -49,9 +50,10 @@ int wrapped_eval(void *void_param) int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast) { t_fork_param_parent param; + int status; - if (!redir_extract(&ast->redirs, env, fds)) - return (ERR_FATAL); + if ((status = redir_extract(&ast->redirs, env, fds)) != 0) + return (status); param.fds[0] = fds[0]; param.fds[1] = fds[1]; param.env = env; @@ -69,5 +71,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 (ERR_FATAL); + return (EVAL_FATAL); } diff --git a/src/eval/redir.c b/src/eval/redir.c index 9beef17..39e202d 100644 --- a/src/eval/redir.c +++ b/src/eval/redir.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/15 11:05:34 by charles #+# #+# */ -/* Updated: 2020/09/10 14:25:08 by charles ### ########.fr */ +/* Updated: 2020/09/10 20:25:59 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,9 +24,9 @@ static int st_open_replace(int *fd, char *filename, int oflag) { errorf("%s: %s\n", filename, strerror(errno)); free(filename); - return (ERR_OPEN); + return (1); } - return (ERR_NONE); + return (0); } int redir_extract( @@ -39,10 +39,10 @@ int redir_extract( char *filename; if (*redirs == NULL) - return (ERR_NONE); + return (0); if (!((*redirs)->tag & TAG_IS_REDIR) || (*redirs)->next == NULL || !((*redirs)->next->tag & TAG_IS_STR)) - return (ERR_FATAL); + return (EVAL_FATAL); curr = (*redirs)->next; after = NULL; while (curr != NULL && curr->tag & TAG_IS_STR) @@ -58,18 +58,18 @@ int redir_extract( { tok_lst_destroy(redirs, free); tok_lst_destroy(&after, free); - return (ERR_FATAL); + return (EVAL_FATAL); } if (((*redirs)->tag == TAG_REDIR_IN - && st_open_replace(&fds[FD_READ], filename, O_RDONLY) != ERR_NONE) + && st_open_replace(&fds[FD_READ], filename, O_RDONLY) != 0) || ((*redirs)->tag == TAG_REDIR_OUT - && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC) != ERR_NONE) + && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC) != 0) || ((*redirs)->tag == TAG_REDIR_APPEND - && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND) != ERR_NONE)) + && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND) != 0)) { tok_lst_destroy(redirs, free); tok_lst_destroy(&after, free); - return (ERR_FATAL); + return (EVAL_FATAL); } tok_lst_destroy(redirs, free); free(filename); |
