diff options
Diffstat (limited to 'src/eval')
| -rw-r--r-- | src/eval/cmd.c | 9 | ||||
| -rw-r--r-- | src/eval/redir.c | 46 |
2 files changed, 32 insertions, 23 deletions
diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 223725c..ff2c904 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 21:00:38 by charles ### ########.fr */ +/* Updated: 2020/09/14 15:50:15 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,8 +15,6 @@ pid_t g_child_pid = -1; int g_last_status = 0; -void token_debug(void *v); - int wrapped_cmd(void *void_param) { t_fork_param_cmd *param; @@ -59,13 +57,8 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) if ((status = redir_extract(&ast->redirs, env, fds)) != 0) return (status); - if ((argv = preprocess(&ast->cmd_argv, env)) == NULL) - { - ast->cmd_argv = NULL; return (EVAL_FATAL); - } - if (argv[0] == NULL) return (0); param.builtin = builtin_search_func(argv[0]); diff --git a/src/eval/redir.c b/src/eval/redir.c index 39e202d..6c3e45c 100644 --- a/src/eval/redir.c +++ b/src/eval/redir.c @@ -6,13 +6,13 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/15 11:05:34 by charles #+# #+# */ -/* Updated: 2020/09/10 20:25:59 by charles ### ########.fr */ +/* Updated: 2020/09/14 15:41:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "eval.h" -static int st_open_replace(int *fd, char *filename, int oflag) +static int st_open_replace(char *filename, int *fd, int oflag) { if (*fd != FD_NONE) close(*fd); @@ -29,14 +29,35 @@ static int st_open_replace(int *fd, char *filename, int oflag) return (0); } -int redir_extract( - t_tok_lst **redirs, - t_env env, - int fds[2]) +static int st_open_replace_dispatch(char *filename, int fds[2], enum e_tok tag) +{ + int *fd; + int oflag; + + if (tag == TAG_REDIR_IN) + { + fd = &fds[FD_READ]; + oflag = O_RDONLY; + } + else if (tag == TAG_REDIR_OUT) + { + fd = &fds[FD_WRITE]; + oflag = O_WRONLY | O_CREAT | O_TRUNC; + } + else if (tag == TAG_REDIR_APPEND) + { + fd = &fds[FD_WRITE]; + oflag = O_WRONLY | O_CREAT | O_APPEND; + } + return (st_open_replace(filename, fd, oflag)); +} + +int redir_extract(t_tok_lst **redirs, t_env env, int fds[2]) { t_tok_lst *after; t_tok_lst *curr; char *filename; + int status; if (*redirs == NULL) return (0); @@ -54,22 +75,17 @@ int redir_extract( } curr = curr->next; } - if ((filename = preprocess_filename(&(*redirs)->next, env)) == NULL) + if ((status = preprocess_filename(&(*redirs)->next, env, &filename))) { tok_lst_destroy(redirs, free); tok_lst_destroy(&after, free); - return (EVAL_FATAL); + return (status); } - if (((*redirs)->tag == TAG_REDIR_IN - && st_open_replace(&fds[FD_READ], filename, O_RDONLY) != 0) - || ((*redirs)->tag == TAG_REDIR_OUT - && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC) != 0) - || ((*redirs)->tag == TAG_REDIR_APPEND - && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND) != 0)) + if ((status = st_open_replace_dispatch(filename, fds, (*redirs)->tag)) != 0) { tok_lst_destroy(redirs, free); tok_lst_destroy(&after, free); - return (EVAL_FATAL); + return (status); } tok_lst_destroy(redirs, free); free(filename); |
