aboutsummaryrefslogtreecommitdiff
path: root/src/eval
diff options
context:
space:
mode:
Diffstat (limited to 'src/eval')
-rw-r--r--src/eval/eval.c300
-rw-r--r--src/eval/pipe.c60
2 files changed, 150 insertions, 210 deletions
diff --git a/src/eval/eval.c b/src/eval/eval.c
index c4df1c9..1e0a8d7 100644
--- a/src/eval/eval.c
+++ b/src/eval/eval.c
@@ -6,156 +6,156 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:05:21 by charles #+# #+# */
-/* Updated: 2020/05/15 00:12:51 by charles ### ########.fr */
+/* Updated: 2020/06/14 10:34:11 by charles ### ########.fr */
/* */
/* ************************************************************************** */
-/*
-** \file eval.c
-** \brief Evaluation of an AST
-*/
-
-#include "eval.h"
-
-/*
-** \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 fd_in,
- int fd_out,
- void *passed,
- int (*wrapped)(void *param))
-{
- int status;
- pid_t child_pid;
-
- if ((child_pid = fork()) == -1)
- return (-1);
- if (child_pid == 0)
- {
- if (dup2(STDIN_FILENO, fd_in) == -1 ||
- dup2(STDOUT_FILENO, fd_out) == -1)
- exit(EXIT_FAILURE);
- if ((status = wrapped(passed)) == -1)
- exit(EXIT_FAILURE);
- exit(status);
- }
- wait(&child_pid);
- return (WEXITSTATUS(child_pid));
-}
-
-int run_builtin(t_eval_state *state, char **argv)
-{
- return (builtin_dispatch_run(argv, state->env));
-}
-
-/*
-** \brief execve syscall wrapper passed it to fork_wrap
-** \param param function params
-** \return execve return value
-*/
-
-int execve_wrapper(void *param)
-{
- return (execve(
- ((t_fork_param_execve*)param)->exec_path,
- ((t_fork_param_execve*)param)->argv,
- ((t_fork_param_execve*)param)->envp
- ));
-}
-
-/*
-** \brief Evaluate a command
-** \param state Evaluation state
-** \param cmd Command to evaluate
-** \return Executable status or -1 on error
-*/
-
-static int eval_cmd(int fd_in, int fd_out, t_eval_state *state, t_cmd *cmd)
-{
- t_fork_param_execve param;
-
- if (builtin_check_exec_name(cmd->argv[0]))
- return (run_builtin(state, cmd->argv));
- param.exec_path = exec_search_path(
- state->path, env_search(state->env, "PATH"), cmd->argv[0]);
- if (param.exec_path == NULL)
- return (-1);
- if (cmd->in != NULL && (fd_in = open(cmd->in, O_RDONLY)) == -1)
- return (-1);
- if (cmd->out != NULL && (fd_out = open(cmd->out,
- (cmd->is_append ? O_APPEND : O_WRONLY) | O_CREAT)) == -1)
- return (-1);
- param.argv = cmd->argv;
- param.envp = (char**)state->env->data;
- return (fork_wrap(fd_in, fd_out, &param, &execve_wrapper));
-}
-
-/*
-** \brief Evaluate a line
-** \param state State of the evaluation
-** \param line Line to evaluate
-** \return Last Executed command status or -1 on error
-*/
-
-static int eval_line(void *param)
-{
- int status;
- t_eval_state *state;
- t_line *line;
- int fd_in;
- int fd_out;
-
- state = ((t_fork_param_line*)param)->state;
- line = ((t_fork_param_line*)param)->line;
- fd_in = ((t_fork_param_line*)param)->fd_in;
- fd_out = ((t_fork_param_line*)param)->fd_out;
-
- /* if (line->right == NULL) */
- /* return (eval(state, line->left)); */
-
- /* if (line->sep == SEP_PIPE) */
- /* pipe(state->p); */
-
- if (line->left->tag == TAG_LINE)
- {
- return (fork_wrap(fd_in, fd_out, param, &eval_line));
- }
- if ((status = eval(fd_in, fd_out, state, line->left)) == -1)
- return (-1);
- if ((line->sep == SEP_AND && status != 0) ||
- (line->sep == SEP_OR && status == 0))
- return (status);
-
- return (eval(fd_in, fd_out, state, line->right));
-}
-
-/*
-** \brief Evaluate an AST
-** \param state State of the evaluation
-** \param ast Abstract syntax tree to evaluate
-** \return Last command status or -1 on error
-*/
-
-int eval(int fd_in, int fd_out, t_eval_state *state, t_ast *ast)
-{
- t_fork_param_line param;
-
- errno = 0;
- if (ast->tag == TAG_LINE)
- {
- param.state = state;
- param.line = &ast->line;
- param.fd_in = fd_in;
- param.fd_out = fd_out;
- return (eval_line(&param));
- }
- if (ast->tag == TAG_CMD)
- return (eval_cmd(fd_in, fd_out, state, &ast->cmd));
- return (-1);
-}
+/* #<{(| */
+/* ** \file eval.c */
+/* ** \brief Evaluation of an AST */
+/* |)}># */
+/* */
+/* #include "eval.h" */
+/* */
+/* #<{(| */
+/* ** \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 fd_in, */
+/* int fd_out, */
+/* void *passed, */
+/* int (*wrapped)(void *param)) */
+/* { */
+/* int status; */
+/* pid_t child_pid; */
+/* */
+/* if ((child_pid = fork()) == -1) */
+/* return (-1); */
+/* if (child_pid == 0) */
+/* { */
+/* if (dup2(STDIN_FILENO, fd_in) == -1 || */
+/* dup2(STDOUT_FILENO, fd_out) == -1) */
+/* exit(EXIT_FAILURE); */
+/* if ((status = wrapped(passed)) == -1) */
+/* exit(EXIT_FAILURE); */
+/* exit(status); */
+/* } */
+/* wait(&child_pid); */
+/* return (WEXITSTATUS(child_pid)); */
+/* } */
+/* */
+/* int run_builtin(t_eval_state *state, char **argv) */
+/* { */
+/* return (builtin_dispatch_run(argv, state->env)); */
+/* } */
+/* */
+/* #<{(| */
+/* ** \brief execve syscall wrapper passed it to fork_wrap */
+/* ** \param param function params */
+/* ** \return execve return value */
+/* |)}># */
+/* */
+/* int execve_wrapper(void *param) */
+/* { */
+/* return (execve( */
+/* ((t_fork_param_execve*)param)->exec_path, */
+/* ((t_fork_param_execve*)param)->argv, */
+/* ((t_fork_param_execve*)param)->envp */
+/* )); */
+/* } */
+/* */
+/* #<{(| */
+/* ** \brief Evaluate a command */
+/* ** \param state Evaluation state */
+/* ** \param cmd Command to evaluate */
+/* ** \return Executable status or -1 on error */
+/* |)}># */
+/* */
+/* static int eval_cmd(int fd_in, int fd_out, t_eval_state *state, t_cmd *cmd) */
+/* { */
+/* t_fork_param_execve param; */
+/* */
+/* if (builtin_check_exec_name(cmd->argv[0])) */
+/* return (run_builtin(state, cmd->argv)); */
+/* param.exec_path = exec_search_path( */
+/* state->path, env_search(state->env, "PATH"), cmd->argv[0]); */
+/* if (param.exec_path == NULL) */
+/* return (-1); */
+/* if (cmd->in != NULL && (fd_in = open(cmd->in, O_RDONLY)) == -1) */
+/* return (-1); */
+/* if (cmd->out != NULL && (fd_out = open(cmd->out, */
+/* (cmd->is_append ? O_APPEND : O_WRONLY) | O_CREAT)) == -1) */
+/* return (-1); */
+/* param.argv = cmd->argv; */
+/* param.envp = (char**)state->env->data; */
+/* return (fork_wrap(fd_in, fd_out, &param, &execve_wrapper)); */
+/* } */
+/* */
+/* #<{(| */
+/* ** \brief Evaluate a line */
+/* ** \param state State of the evaluation */
+/* ** \param line Line to evaluate */
+/* ** \return Last Executed command status or -1 on error */
+/* |)}># */
+/* */
+/* static int eval_line(void *param) */
+/* { */
+/* int status; */
+/* t_eval_state *state; */
+/* t_line *line; */
+/* int fd_in; */
+/* int fd_out; */
+/* */
+/* state = ((t_fork_param_line*)param)->state; */
+/* line = ((t_fork_param_line*)param)->line; */
+/* fd_in = ((t_fork_param_line*)param)->fd_in; */
+/* fd_out = ((t_fork_param_line*)param)->fd_out; */
+/* */
+/* #<{(| if (line->right == NULL) |)}># */
+/* #<{(| return (eval(state, line->left)); |)}># */
+/* */
+/* #<{(| if (line->sep == SEP_PIPE) |)}># */
+/* #<{(| pipe(state->p); |)}># */
+/* */
+/* if (line->left->tag == AST_LINE) */
+/* { */
+/* return (fork_wrap(fd_in, fd_out, param, &eval_line)); */
+/* } */
+/* if ((status = eval(fd_in, fd_out, state, line->left)) == -1) */
+/* return (-1); */
+/* if ((line->sep == SEP_AND && status != 0) || */
+/* (line->sep == SEP_OR && status == 0)) */
+/* return (status); */
+/* */
+/* return (eval(fd_in, fd_out, state, line->right)); */
+/* } */
+/* */
+/* #<{(| */
+/* ** \brief Evaluate an AST */
+/* ** \param state State of the evaluation */
+/* ** \param ast Abstract syntax tree to evaluate */
+/* ** \return Last command status or -1 on error */
+/* |)}># */
+/* */
+/* int eval(int fd_in, int fd_out, t_eval_state *state, t_ast *ast) */
+/* { */
+/* t_fork_param_line param; */
+/* */
+/* errno = 0; */
+/* if (ast->tag == TAG_LINE) */
+/* { */
+/* param.state = state; */
+/* param.line = &ast->line; */
+/* param.fd_in = fd_in; */
+/* param.fd_out = fd_out; */
+/* return (eval_line(&param)); */
+/* } */
+/* if (ast->tag == TAG_CMD) */
+/* return (eval_cmd(fd_in, fd_out, state, &ast->cmd)); */
+/* return (-1); */
+/* } */
diff --git a/src/eval/pipe.c b/src/eval/pipe.c
deleted file mode 100644
index 125c013..0000000
--- a/src/eval/pipe.c
+++ /dev/null
@@ -1,60 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* pipe.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/04/01 17:05:58 by charles #+# #+# */
-/* Updated: 2020/04/01 17:05:59 by charles ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-/*
-** \file pipe.c
-** \brief Pipes setup
-*/
-
-#include "eval.h"
-
-/*
-** \brief Setup STDIN and STDOUT pipe in the parent process
-** \param cmd Command to setup
-** \param pipe_in STDIN pipe
-** \param pipe_out STDOUT pipe
-** \return -1 on error, 0 otherwise
-*/
-
-int pipe_setup_parent(t_cmd *cmd, int pipe_in[2], int pipe_out[2])
-{
- if (cmd->in != NULL)
- {
- if ((pipe_in[PIPE_WRITE] = open(cmd->in, O_RDONLY)) < 0)
- return (-1);
- }
- if (cmd->out != NULL)
- {
- if ((pipe_out[PIPE_READ] = open(cmd->out,
- (cmd->is_append ? O_WRONLY : O_APPEND) | O_CREAT)) < 0)
- return (-1);
- }
- return (0);
-}
-
-/*
-** \brief Setup STDIN and STDOUT pipe in the child process
-** \param pipe_in STDIN pipe
-** \param pipe_out STDOUT pipe
-** \return -1 on error, 0 otherwise
-*/
-
-int pipe_setup_child(int pipe_in[2], int pipe_out[2])
-{
- if (pipe_in[PIPE_READ] != PIPE_CLOSED)
- if (dup2(STDIN_FILENO, pipe_in[PIPE_READ]) == -1)
- return (-1);
- if (pipe_out[PIPE_WRITE] != PIPE_CLOSED)
- if (dup2(STDOUT_FILENO, pipe_out[PIPE_WRITE]) == -1)
- return (-1);
- return (0);
-}