aboutsummaryrefslogtreecommitdiff
path: root/src/eval
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-13 21:01:18 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-13 21:01:18 +0200
commitc51d31b8b751585153500729c25ae2f02d179e45 (patch)
treeeab34e14508f5a2a3ad2c5d06c56afdd7ba72e9e /src/eval
parent490237aece240c05b5a9035665a88327e1be87ed (diff)
downloadminishell-c51d31b8b751585153500729c25ae2f02d179e45.tar.gz
minishell-c51d31b8b751585153500729c25ae2f02d179e45.tar.bz2
minishell-c51d31b8b751585153500729c25ae2f02d179e45.zip
Refactoring eval file structure, Added comment to builtin
Diffstat (limited to 'src/eval')
-rw-r--r--src/eval/cmd.c78
-rw-r--r--src/eval/eval.c75
-rw-r--r--src/eval/local.c68
-rw-r--r--src/eval/operation.c (renamed from src/eval/op.c)47
-rw-r--r--src/eval/parenthesis.c37
5 files changed, 136 insertions, 169 deletions
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 <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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, &param, &forked_cmd, child_pid);
+ status = fork_wrap(fds, &param, &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 <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <me@cacharle.xyz> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* 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/operation.c
index 7181e83..eaafcf8 100644
--- a/src/eval/op.c
+++ b/src/eval/operation.c
@@ -1,18 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* op.c :+: :+: :+: */
+/* operation.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/17 15:27:22 by charles #+# #+# */
-/* Updated: 2020/09/13 14:22:45 by charles ### ########.fr */
+/* Updated: 2020/09/13 20:38:26 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "eval.h"
-int eval_op(int fds[2], t_env env, t_path path, t_ast *ast)
+int eval_operation(int fds[2], t_env env, t_path path, t_ast *ast)
{
int status;
int left_fds[2];
@@ -58,44 +58,3 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast)
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, &param, 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/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 <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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, &param, wrapped_eval, NULL));
+}