diff options
Diffstat (limited to 'src/eval')
| -rw-r--r-- | src/eval/cmd.c | 21 | ||||
| -rw-r--r-- | src/eval/op.c | 49 | ||||
| -rw-r--r-- | src/eval/redir.c | 13 |
3 files changed, 64 insertions, 19 deletions
diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 9468cb2..f502984 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/15 11:09:38 by charles ### ########.fr */ +/* Updated: 2020/06/17 17:02:07 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,8 +21,7 @@ */ int fork_wrap( - int fd_in, - int fd_out, + int fds[2], void *passed, int (*wrapped)(void *param)) { @@ -33,14 +32,16 @@ int fork_wrap( return (-1); if (child_pid == 0) { - if ((fd_in != MS_NO_FD && dup2(fd_in, STDIN_FILENO) == -1) || - (fd_out != MS_NO_FD && dup2(fd_out, STDOUT_FILENO) == -1)) + if ((fds[FDS_READ] != MS_NO_FD && dup2(fds[FDS_READ], STDIN_FILENO) == -1) || + (fds[FDS_WRITE] != MS_NO_FD && dup2(fds[FDS_WRITE], STDOUT_FILENO) == -1)) exit(EXIT_FAILURE); if ((status = wrapped(passed)) == -1) exit(EXIT_FAILURE); exit(status); } wait(&child_pid); + close(fds[FDS_WRITE]); + // also read end? return (WEXITSTATUS(child_pid)); } @@ -56,16 +57,12 @@ int forked_cmd(void *void_param) return (execve(param->exec_path, param->argv, (char**)param->env->data)); } -int eval_cmd(t_env env, t_path path, t_ast *ast) +int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) { t_fork_param_cmd param; - int fd_in; - int fd_out; char **argv; - fd_in = MS_NO_FD; - fd_out = MS_NO_FD; - if (!redir_extract(ast->redirs, env, &fd_in, &fd_out)) + if (!redir_extract(ast->redirs, env, fds)) { ast->redirs = NULL; return (-1); @@ -92,7 +89,7 @@ int eval_cmd(t_env env, t_path path, t_ast *ast) param.argv = argv; param.env = env; - int ret = fork_wrap(fd_in, fd_out, ¶m, &forked_cmd); + int ret = fork_wrap(fds, ¶m, &forked_cmd); ft_split_destroy(argv); return (ret); } diff --git a/src/eval/op.c b/src/eval/op.c new file mode 100644 index 0000000..e47698b --- /dev/null +++ b/src/eval/op.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* op.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/17 15:27:22 by charles #+# #+# */ +/* Updated: 2020/06/17 17:03:48 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "eval.h" + +// TODO: add parent tag on first operation of parent to fork +int eval_op(int fds[2], t_env env, t_path path, t_op *op) +{ + int status; + int left_fds[2]; + int right_fds[2]; + int p[2]; + + left_fds[FDS_READ] = fds[FDS_READ]; + left_fds[FDS_WRITE] = MS_NO_FD; + right_fds[FDS_READ] = MS_NO_FD; + right_fds[FDS_WRITE] = fds[FDS_WRITE]; + if (op->sep == TAG_PIPE) + { + pipe(p); + left_fds[FDS_WRITE] = p[FDS_WRITE]; + right_fds[FDS_READ] = p[FDS_READ]; + } + if ((status = eval(left_fds, env, path, op->left)) == -1) + return (-1); + if ((op->sep == TAG_AND && status != 0) || + (op->sep == TAG_PIPE && status != 0) || + (op->sep == TAG_OR && status == 0)) + return (status); + return (eval(right_fds, env, path, op->right)); +} + +int eval(int fds[2], t_env env, t_path path, t_ast *ast) +{ + if (ast->tag == AST_OP) + return (eval_op(fds, env, path, &ast->op)); + if (ast->tag == AST_CMD) + return (eval_cmd(fds, env, path, ast)); + return (-1); +} diff --git a/src/eval/redir.c b/src/eval/redir.c index 5a9d074..7249ad5 100644 --- a/src/eval/redir.c +++ b/src/eval/redir.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/15 11:05:34 by charles #+# #+# */ -/* Updated: 2020/06/15 16:00:40 by charles ### ########.fr */ +/* Updated: 2020/06/17 16:17:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,8 +37,7 @@ static bool st_open_replace(int *fd, char *filename, int oflag) bool redir_extract( t_ftlst *redirs, t_env env, - int *fd_in, - int *fd_out) + int fds[2]) { t_ftlst *after; t_ftlst *curr; @@ -66,16 +65,16 @@ bool redir_extract( return (false); } if ((st_lst_tag(redirs) == TAG_REDIR_IN - && !st_open_replace(fd_in, filename, O_RDONLY)) + && !st_open_replace(&fds[FDS_READ], filename, O_RDONLY)) || (st_lst_tag(redirs) == TAG_REDIR_OUT - && !st_open_replace(fd_out, filename, O_WRONLY | O_CREAT | O_TRUNC)) + && !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC)) || (st_lst_tag(redirs) == TAG_REDIR_APPEND - && !st_open_replace(fd_out, filename, O_WRONLY | O_CREAT | O_APPEND))) + && !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND))) { token_destroy_lst2(redirs, after); return (false); } token_destroy_lst(redirs); free(filename); - return (redir_extract(after, env, fd_in, fd_out)); + return (redir_extract(after, env, fds)); } |
