From c51d31b8b751585153500729c25ae2f02d179e45 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sun, 13 Sep 2020 21:01:18 +0200 Subject: Refactoring eval file structure, Added comment to builtin --- src/eval/cmd.c | 78 ++++++++++---------------------------- src/eval/eval.c | 75 ++++++++++++++++++++++++++++++++++++ src/eval/local.c | 68 --------------------------------- src/eval/op.c | 101 ------------------------------------------------- src/eval/operation.c | 60 +++++++++++++++++++++++++++++ src/eval/parenthesis.c | 37 ++++++++++++++++++ 6 files changed, 193 insertions(+), 226 deletions(-) create mode 100644 src/eval/eval.c delete mode 100644 src/eval/local.c delete mode 100644 src/eval/op.c create mode 100644 src/eval/operation.c create mode 100644 src/eval/parenthesis.c (limited to 'src/eval') diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 82e30c9..223725c 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/09/13 14:19:37 by charles ### ########.fr */ +/* Updated: 2020/09/13 21:00:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,68 +17,16 @@ int g_last_status = 0; void token_debug(void *v); -/* -** \brief Wrap a function in a fork -** \param fd_in fork input file descriptor -** \param fd_out fork output file descriptor -** \param passed param of the wrapped function -** \param wrapped function to wrap -*/ - -int fork_wrap( - int fds[2], - void *passed, - int (*wrapped)(void *param), - pid_t *child_pid -) -{ - int status; - bool waiting; - pid_t pid; - - waiting = child_pid == NULL; - if (waiting) - child_pid = &pid; - if ((*child_pid = fork()) == -1) - 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)) == EVAL_FATAL) // FIXME detect fatal in child (pipe ?) - exit(EXIT_FAILURE); - exit(status); - } - g_child_pid = *child_pid; - if (waiting) - { - waitpid(*child_pid, child_pid, 0); - close(fds[FD_WRITE]); - /* close(fds[FD_READ]); */ - return (WEXITSTATUS(*child_pid)); - } - return (0); -} - -int forked_cmd(void *void_param) +int wrapped_cmd(void *void_param) { t_fork_param_cmd *param; int status; - struct stat statbuf; param = void_param; if (param->builtin != NULL) return (param->builtin->func(param->argv, param->env)); else { - if (stat(param->exec_path, &statbuf) == -1) - return (errorf_ret(126, "%s: %s\n", param->exec_path, strerror(errno))); - if (S_ISDIR(statbuf.st_mode)) - 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) { @@ -90,6 +38,19 @@ int forked_cmd(void *void_param) } } +int check_exec_path(char *exec_path) +{ + struct stat statbuf; + + if (stat(exec_path, &statbuf) == -1) + return (errorf_ret(126, "%s: %s\n", exec_path, strerror(errno))); + if (S_ISDIR(statbuf.st_mode)) + return (errorf_ret(126, "%s: Is a directory\n", exec_path)); + if (!(statbuf.st_mode & 0444)) + return (errorf_ret(126, "%s: %s\n", exec_path, strerror(EACCES))); + return (0); +} + int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) { t_fork_param_cmd param; @@ -108,6 +69,9 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) if (argv[0] == NULL) return (0); param.builtin = builtin_search_func(argv[0]); + if (param.builtin != NULL && !param.builtin->forked && child_pid == NULL) + return (param.builtin->func(argv, env)); + if (param.builtin == NULL) { param.exec_path = exec_search_path(path, env_search(env, "PATH"), argv[0]); @@ -117,13 +81,13 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) ft_split_destroy(argv); return (127); } + if ((status = check_exec_path(param.exec_path)) != 0) + return (status); } - else if (!param.builtin->forked && child_pid == NULL) - return (param.builtin->func(argv, env)); param.argv = argv; param.env = env; - status = fork_wrap(fds, ¶m, &forked_cmd, child_pid); + status = fork_wrap(fds, ¶m, &wrapped_cmd, child_pid); ft_split_destroy(argv); g_last_status = status; return (status); diff --git a/src/eval/eval.c b/src/eval/eval.c new file mode 100644 index 0000000..d35d491 --- /dev/null +++ b/src/eval/eval.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* eval.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/13 20:38:06 by charles #+# #+# */ +/* Updated: 2020/09/13 20:45:20 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "eval.h" + +/* +** \brief Wrap a function in a fork +** \param fds fork read/write file descriptors +** \param passed param of the wrapped function +** \param wrapped function to wrap +** \param child_pid Pointer where to store the child pid +** or NULL if the child should be waited +** \return The child status code or EVAL_FATAL on error +*/ + +int fork_wrap( + int fds[2], + void *passed, + int (*wrapped)(void *param), + pid_t *child_pid +) +{ + int status; + bool waiting; + pid_t pid; + + waiting = child_pid == NULL; + if (waiting) + child_pid = &pid; + if ((*child_pid = fork()) == -1) + 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)) == EVAL_FATAL) // FIXME detect fatal in child (pipe ?) + exit(EXIT_FAILURE); + exit(status); + } + g_child_pid = *child_pid; + if (waiting) + { + waitpid(*child_pid, child_pid, 0); + close(fds[FD_WRITE]); + /* close(fds[FD_READ]); */ + return (WEXITSTATUS(*child_pid)); + } + return (0); +} + +/* +** \brief Evaluate and AST +** \return The last command status or EVAL_FATAL on error +*/ + +int eval(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) +{ + if (ast->tag == AST_PARENT) + return (eval_parenthesis(fds, env, path, ast)); + if (ast->tag == AST_OP) + return (eval_operation(fds, env, path, ast)); + if (ast->tag == AST_CMD) + return (eval_cmd(fds, env, path, ast, child_pid)); + return (EVAL_FATAL); +} diff --git a/src/eval/local.c b/src/eval/local.c deleted file mode 100644 index 089a0c7..0000000 --- a/src/eval/local.c +++ /dev/null @@ -1,68 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* local.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/08/28 16:52:54 by charles #+# #+# */ -/* Updated: 2020/08/28 16:53:04 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "eval.h" - - /* while (ast->cmd_argv != NULL */ - /* && ((t_token*)ast->cmd_argv->data)->tag & TAG_IS_STR */ - /* && utils_start_with_valid_identifier(((t_token*)ast->cmd_argv->data)->content)) */ - /* { */ - /* t_ftlst *start; */ - /* */ - /* id = ((t_token*)ast->cmd_argv->data)->content; */ - /* *ft_strchr(id, '=') = '\0'; */ - /* ((t_token*)ast->cmd_argv->data)->content = ft_strchr(id, '\0') + 1; */ - /* if (*((t_token*)ast->cmd_argv->data)->content == '\0') */ - /* ft_lstpop_front(&ast->cmd_argv, NULL); */ - /* else */ - /* { */ - /* t_ftlst *curr = ast->cmd_argv; */ - /* t_ftlst *prev = curr; */ - /* */ - /* while (curr != NULL */ - /* && ((t_token*)curr->data)->tag & TAG_STICK && ((t_token*)curr->data)->tag & TAG_IS_STR) */ - /* { */ - /* prev = curr; */ - /* curr = curr->next; */ - /* } */ - /* if (curr != NULL && ((t_token*)curr->data)->tag & TAG_IS_STR) */ - /* { */ - /* prev = curr; */ - /* curr = curr->next; */ - /* } */ - /* */ - /* start = ast->cmd_argv; */ - /* ast->cmd_argv = prev->next; */ - /* prev->next = NULL; */ - /* } */ - /* */ - /* #<{(| ft_lstiter(start, token_debug); |)}># */ - /* #<{(| puts(""); |)}># */ - /* #<{(| ft_lstiter(ast->cmd_argv, token_debug); |)}># */ - /* */ - /* char **strs = preprocess(&start, env); */ - /* */ - /* if (env_export(param.env_local, id, strs[0]) == NULL) */ - /* return (-1); */ - /* } */ - /* if (ast->cmd_argv == NULL) // FIXME special env not passed to child processes */ - /* { */ - /* ft_vecpop(param.env_local, NULL); */ - /* if (ft_vecswallow_at(env, env->size - 1, param.env_local) == NULL) */ - /* { */ - /* ft_vecdestroy(param.env_local, free); */ - /* return (-1); */ - /* } */ - /* g_last_status = 0; */ - /* return (0); */ - /* } */ - diff --git a/src/eval/op.c b/src/eval/op.c deleted file mode 100644 index 7181e83..0000000 --- a/src/eval/op.c +++ /dev/null @@ -1,101 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* op.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/06/17 15:27:22 by charles #+# #+# */ -/* Updated: 2020/09/13 14:22:45 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "eval.h" - -int eval_op(int fds[2], t_env env, t_path path, t_ast *ast) -{ - int status; - int left_fds[2]; - int right_fds[2]; - int p[2]; - - 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[FD_WRITE] = p[FD_WRITE]; - right_fds[FD_READ] = p[FD_READ]; - - pid_t left_pid; - pid_t right_pid; - eval(left_fds, env, path, ast->op.left, &left_pid); - close(p[FD_WRITE]); - eval(right_fds, env, path, ast->op.right, &right_pid); - close(p[FD_READ]); - - pid_t finished; - finished = wait(NULL); - if (finished == left_pid) - { - waitpid(right_pid, &right_pid, 0); - } - else if (finished == right_pid) - { - kill(left_pid, SIGKILL); - /* waitpid(left_pid, &left_pid, 0); */ - } - return (0); - } - if ((status = eval(left_fds, env, path, ast->op.left, NULL)) == 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)) - return (status); - return (eval(right_fds, env, path, ast->op.right, NULL)); -} - -int wrapped_eval(void *void_param) -{ - t_fork_param_args *param; - - param = void_param; - return (eval(param->fds, param->env, param->path, param->ast, NULL)); -} - -int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast) -{ - int status; - - if ((status = redir_extract(&ast->redirs, env, fds)) != 0) - return (status); - ast->tag ^= AST_PARENT; - return (eval_forked(fds, env, path, ast->parent_ast, NULL)); -} - -int eval_forked(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) -{ - t_fork_param_args param; - - param.fds[0] = fds[0]; - param.fds[1] = fds[1]; - param.env = env; - param.path = path; - param.ast = ast; - return (fork_wrap(fds, ¶m, wrapped_eval, child_pid)); -} - -int eval(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) -{ - if (ast->tag == AST_PARENT) - return (eval_parent(fds, env, path, ast)); - if (ast->tag == AST_OP) - return (eval_op(fds, env, path, ast)); - if (ast->tag == AST_CMD) - return (eval_cmd(fds, env, path, ast, child_pid)); - return (EVAL_FATAL); -} diff --git a/src/eval/operation.c b/src/eval/operation.c new file mode 100644 index 0000000..eaafcf8 --- /dev/null +++ b/src/eval/operation.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* operation.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/17 15:27:22 by charles #+# #+# */ +/* Updated: 2020/09/13 20:38:26 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "eval.h" + +int eval_operation(int fds[2], t_env env, t_path path, t_ast *ast) +{ + int status; + int left_fds[2]; + int right_fds[2]; + int p[2]; + + 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[FD_WRITE] = p[FD_WRITE]; + right_fds[FD_READ] = p[FD_READ]; + + pid_t left_pid; + pid_t right_pid; + eval(left_fds, env, path, ast->op.left, &left_pid); + close(p[FD_WRITE]); + eval(right_fds, env, path, ast->op.right, &right_pid); + close(p[FD_READ]); + + pid_t finished; + finished = wait(NULL); + if (finished == left_pid) + { + waitpid(right_pid, &right_pid, 0); + } + else if (finished == right_pid) + { + kill(left_pid, SIGKILL); + /* waitpid(left_pid, &left_pid, 0); */ + } + return (0); + } + if ((status = eval(left_fds, env, path, ast->op.left, NULL)) == 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)) + return (status); + return (eval(right_fds, env, path, ast->op.right, NULL)); +} diff --git a/src/eval/parenthesis.c b/src/eval/parenthesis.c new file mode 100644 index 0000000..8983378 --- /dev/null +++ b/src/eval/parenthesis.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parenthesis.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/13 20:38:29 by charles #+# #+# */ +/* Updated: 2020/09/13 20:40:19 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "eval.h" + +int wrapped_eval(void *void_param) +{ + t_fork_param_args *param; + + param = void_param; + return (eval(param->fds, param->env, param->path, param->ast, NULL)); +} + +int eval_parenthesis(int fds[2], t_env env, t_path path, t_ast *ast) +{ + int status; + t_fork_param_args param; + + if ((status = redir_extract(&ast->redirs, env, fds)) != 0) + return (status); + ast->tag ^= AST_PARENT; + param.fds[0] = fds[0]; + param.fds[1] = fds[1]; + param.env = env; + param.path = path; + param.ast = ast->parent_ast; + return (fork_wrap(fds, ¶m, wrapped_eval, NULL)); +} -- cgit