aboutsummaryrefslogtreecommitdiff
path: root/src/eval/eval.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-04-01 15:55:57 +0200
committerCharles <sircharlesaze@gmail.com>2020-04-01 15:55:57 +0200
commit2eb59ee61e49b60472f82c000dd4f3536bd1987c (patch)
tree190ac7bc42051ee9fcc7c1d781be16afa4d43075 /src/eval/eval.c
parent551e668e1b7a030fdff236067963100c7d8747a5 (diff)
downloadminishell-2eb59ee61e49b60472f82c000dd4f3536bd1987c.tar.gz
minishell-2eb59ee61e49b60472f82c000dd4f3536bd1987c.tar.bz2
minishell-2eb59ee61e49b60472f82c000dd4f3536bd1987c.zip
Added builtin support in command eval, Refactoring eval/builtin function, Added doc
Diffstat (limited to 'src/eval/eval.c')
-rw-r--r--src/eval/eval.c85
1 files changed, 33 insertions, 52 deletions
diff --git a/src/eval/eval.c b/src/eval/eval.c
index 28cb386..d2fab39 100644
--- a/src/eval/eval.c
+++ b/src/eval/eval.c
@@ -5,89 +5,70 @@
#include "eval.h"
+/**
+** \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(t_eval_state *state, t_line *line)
{
int status;
if (line->right == NULL)
return (eval(state, line->left));
- status = eval(state, line->left);
+ if ((status = eval(state, line->left)) == -1)
+ return (-1);
if ((line->sep == SEP_AND && status != 0) ||
(line->sep == SEP_OR && status == 0))
return (status);
return (eval(state, line->right));
}
-static bool is_exec_path(char *path_str)
-{
- return (ft_strncmp(path_str, "../", 3) == 0
- || ft_strncmp(path_str, "./", 2) == 0
- || ft_strncmp(path_str, "/", 1) == 0);
-}
-
-static bool is_valid_exec(char *exec_path)
-{
- struct stat statbuf;
-
- if (stat(exec_path, &statbuf) != 0)
- return (false);
- if (!S_ISREG(statbuf.st_mode)) // also need to manage link
- return (false);
- // could test permission but probably handled by execve
- return (true);
-}
-
-static char *search_exec_path(t_path path, char *path_var, char *exec_name)
-{
- char *exec_path;
-
- if (is_exec_path(exec_name))
- return (exec_name);
- // try current first
- if ((exec_path = ft_htget(path, exec_name)) == NULL)
- {
- if (path_update(path, path_var) == NULL) // optimise by not updating not changed path in ht
- return (NULL);
- if ((exec_path = ft_htget(path, exec_name)) == NULL)
- return (NULL);
- }
- return (exec_path);
-}
+/**
+** \brief Evaluate a command
+** \param state Evaluation state
+** \param cmd Command to evaluate
+** \return Executable status or -1 on error
+*/
static int eval_cmd(t_eval_state *state, t_cmd *cmd)
{
int child_pid;
char *exec_path;
+ bool is_builtin;
- if ((exec_path = search_exec_path(state->path,
- ft_htget(state->env, "PATH"), cmd->argv[0])) == NULL)
- return (-1);
- if (cmd->in != NULL)
+ is_builtin = builtin_check_exec_name(cmd->argv[0]);
+ if (!is_builtin)
{
- if ((state->in_pipe[PIPE_WRITE] = open(cmd->in, O_RDONLY)) < 0)
- return (-1);
- }
- if (cmd->out != NULL)
- {
- if ((state->out_pipe[PIPE_READ] = open(cmd->out,
- (cmd->is_append ? O_WRONLY : O_APPEND) | O_CREAT)) < 0)
+ if ((exec_path = exec_search_path(state->path,
+ ft_htget(state->env, "PATH"), cmd->argv[0])) == NULL)
return (-1);
}
+ pipe_setup_parent(cmd, state->pipe_in, state->pipe_out);
if ((child_pid = fork()) == -1)
return (-1);
if (child_pid == 0)
{
- if (state->in_pipe[PIPE_READ] != PIPE_CLOSED)
- dup2(STDIN_FILENO, state->in_pipe[PIPE_READ]);
- if (state->out_pipe[PIPE_WRITE] != PIPE_CLOSED)
- dup2(STDOUT_FILENO, state->out_pipe[PIPE_WRITE]);
- if (execve(exec_path, cmd->argv, NULL /*env_array*/) == -1)
+ pipe_setup_child(state->pipe_in, state->pipe_out);
+ if (is_builtin)
+ exit(builtin_dispatch_run(cmd->argv, state->env));
+ else if (execve(exec_path, cmd->argv, NULL /*env_array*/) == -1)
exit(EXIT_FAILURE);
+ exit(EXIT_SUCCESS);
}
wait(&child_pid);
return (WEXITSTATUS(child_pid));
}
+/**
+** \brief Evaluate an AST
+** \param state State of the evaluation
+** \param ast Abstract syntax tree to evaluate
+** \return Executable status or -1 on error
+*/
+
int eval(t_eval_state *state, t_ast *ast)
{
errno = 0;