aboutsummaryrefslogtreecommitdiff
path: root/src/eval/cmd.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/eval/cmd.c')
-rw-r--r--src/eval/cmd.c100
1 files changed, 59 insertions, 41 deletions
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index 027c75b..958c7ae 100644
--- a/src/eval/cmd.c
+++ b/src/eval/cmd.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 10:41:31 by charles #+# #+# */
-/* Updated: 2020/06/14 21:23:20 by charles ### ########.fr */
+/* Updated: 2020/06/15 10:58:59 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,10 +23,10 @@
*/
int fork_wrap(
- int fd_in,
- int fd_out,
- void *passed,
- int (*wrapped)(void *param))
+ int fd_in,
+ int fd_out,
+ void *passed,
+ int (*wrapped)(void *param))
{
int status;
pid_t child_pid;
@@ -46,12 +46,6 @@ int fork_wrap(
return (WEXITSTATUS(child_pid));
}
-/*
-** \brief execve syscall wrapper passed it to fork_wrap
-** \param param function params
-** \return execve return value
-*/
-
int forked_cmd(void *void_param)
{
t_fork_param_cmd *param;
@@ -64,26 +58,6 @@ int forked_cmd(void *void_param)
return (execve(param->exec_path, param->argv, (char**)param->env->data));
}
-int open_redirection(t_ftlst *filename_tokens, t_env env, int oflag)
-{
- // check in and out (single string)
- // argv[0]: [string]: ambiguous redirect code 1
-
- // check in and out (exist)
- // argv[0]: [string]: No such file or directory (probably from errno)
-
- char *filename;
- int fd;
-
- if (filename_tokens == NULL)
- return (MS_NO_FD);
- if ((filename = preprocess_filename(filename_tokens, env)) == NULL)
- return (-1);
- if ((fd = open(filename, oflag)) == -1)
- return (-1); //file error;
- return (fd);
-}
-
bool redir_has_tag(t_ftlst *redir, enum e_token_tag tags)
{
return (((t_token*)redir->data)->tag & tags);
@@ -111,27 +85,59 @@ bool redir_extract(t_ftlst *redirs, t_env env, int *fd_in, int *fd_out)
}
curr = curr->next;
}
- filename = preprocess_filename(redirs->next, env);
+ if ((filename = preprocess_filename(&redirs->next, env)) == NULL)
+ {
+ ft_lstdestroy(&redirs, (void (*)(void*))token_destroy);
+ ft_lstdestroy(&after, (void (*)(void*))token_destroy);
+ return (false);
+ }
if (redir_has_tag(redirs, TAG_REDIR_IN))
{
if (*fd_in != STDIN_FILENO)
close(*fd_in);
if ((*fd_in = open(filename, O_RDONLY)) == -1)
+ {
+ error_eval_put(ERROR_OPEN, filename);
+ ft_lstdestroy(&redirs, (void (*)(void*))token_destroy);
+ ft_lstdestroy(&after, (void (*)(void*))token_destroy);
+ free(filename);
return (false);
+ }
}
- else if (redir_has_tag(redirs, TAG_REDIR_OUT | TAG_REDIR_APPEND))
+ else if (redir_has_tag(redirs, TAG_REDIR_OUT))
{
if (*fd_out != STDOUT_FILENO)
close(*fd_out);
if ((*fd_out = open(filename,
- (redir_has_tag(redirs, TAG_REDIR_APPEND) ? O_APPEND : O_WRONLY)
- | O_CREAT | O_TRUNC, 0644)) == -1)
+ O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)
+ {
+ error_eval_put(ERROR_OPEN, filename);
+ ft_lstdestroy(&redirs, (void (*)(void*))token_destroy);
+ ft_lstdestroy(&after, (void (*)(void*))token_destroy);
+ free(filename);
return (false);
+ }
+ }
+ else if (redir_has_tag(redirs, TAG_REDIR_APPEND))
+ {
+ if (*fd_out != STDOUT_FILENO)
+ close(*fd_out);
+ if ((*fd_out = open(filename,
+ O_WRONLY | O_APPEND | O_CREAT, 0644)) == -1)
+ {
+ error_eval_put(ERROR_OPEN, filename);
+ ft_lstdestroy(&redirs, (void (*)(void*))token_destroy);
+ ft_lstdestroy(&after, (void (*)(void*))token_destroy);
+ free(filename);
+ return (false);
+ }
}
+ ft_lstdestroy(&redirs, (void (*)(void*))token_destroy);
+ free(filename);
return (redir_extract(after, env, fd_in, fd_out));
}
-int eval_cmd(t_env env, t_path path, t_ast *ast)
+int eval_cmd(t_env env, t_path path, t_ast *ast)
{
t_fork_param_cmd param;
int fd_in;
@@ -141,21 +147,33 @@ int eval_cmd(t_env env, t_path path, t_ast *ast)
fd_in = STDIN_FILENO;
fd_out = STDOUT_FILENO;
if (!redir_extract(ast->redirs, env, &fd_in, &fd_out))
+ {
+ ast->redirs = NULL;
return (-1);
+ }
+ ast->redirs = NULL;
- argv = preprocess(ast->cmd_argv, env);
+ if ((argv = preprocess(&ast->cmd_argv, env)) == NULL)
+ {
+ ast->cmd_argv = NULL;
+ return (-1);
+ }
param.builtin = builtin_search_func(argv[0]);
if (param.builtin == NULL)
{
param.exec_path = exec_search_path(path, env_search(env, "PATH"), argv[0]);
- /* // get cmd path */
- /* // argv[0]: [string]: command not found code 127 */
if (param.exec_path == NULL)
- return (-1); //not_found;
+ {
+ error_eval_put(ERROR_CMD_NOT_FOUND, argv[0]);
+ ft_split_destroy(argv);
+ return (-1); // return error status
+ }
}
param.argv = argv;
param.env = env;
- return (fork_wrap(fd_in, fd_out, &param, &forked_cmd));
+ int ret = fork_wrap(fd_in, fd_out, &param, &forked_cmd);
+ ft_split_destroy(argv);
+ return (ret);
}