aboutsummaryrefslogtreecommitdiff
path: root/src/eval/op.c
diff options
context:
space:
mode:
authornass1pro <nass1pro@gmail.com>2020-09-13 09:17:35 +0200
committernass1pro <nass1pro@gmail.com>2020-09-13 09:17:35 +0200
commit935d1ed5ad3d20dd901a4c3cfbb8453626460f18 (patch)
tree247b7059873131f047337dd468ab00a3be2a9d41 /src/eval/op.c
parent5146be239d6ab71db602ec7b6826b6484aa0f178 (diff)
parent65006d0c14d3efa647b3c866ab54bdb1749fa31d (diff)
downloadminishell-935d1ed5ad3d20dd901a4c3cfbb8453626460f18.tar.gz
minishell-935d1ed5ad3d20dd901a4c3cfbb8453626460f18.tar.bz2
minishell-935d1ed5ad3d20dd901a4c3cfbb8453626460f18.zip
Merge branch 'master' of https://github.com/ouaisbrefbams/minishell
Diffstat (limited to 'src/eval/op.c')
-rw-r--r--src/eval/op.c62
1 files changed, 45 insertions, 17 deletions
diff --git a/src/eval/op.c b/src/eval/op.c
index 36340be..a39e380 100644
--- a/src/eval/op.c
+++ b/src/eval/op.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/17 15:27:22 by charles #+# #+# */
-/* Updated: 2020/08/28 16:54:39 by charles ### ########.fr */
+/* Updated: 2020/09/12 17:00:11 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,18 +19,39 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast)
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];
+ left_fds[FD_READ] = fds[FD_READ];
+ left_fds[FD_WRITE] = FD_NONE;
+ right_fds[FD_READ] = FD_NONE;
+ right_fds[FD_WRITE] = fds[FD_WRITE];
if (ast->op.sep == TAG_PIPE)
{
pipe(p);
- left_fds[FDS_WRITE] = p[FDS_WRITE];
- right_fds[FDS_READ] = p[FDS_READ];
+ left_fds[FD_WRITE] = p[FD_WRITE];
+ right_fds[FD_READ] = p[FD_READ];
+
+ pid_t left_pid;
+ pid_t right_pid;
+ eval_forked(left_fds, env, path, ast->op.left, &left_pid);
+ eval_forked(right_fds, env, path, ast->op.right, &right_pid);
+
+ pid_t finished;
+ finished = wait(NULL);
+ close(p[FD_READ]);
+ close(p[FD_WRITE]);
+ if (finished == left_pid)
+ {
+ /* waitpid(right_pid, &right_pid, 0); */
+ }
+ else if (finished == right_pid)
+ {
+ kill(left_pid, SIGKILL);
+ /* waitpid(left_pid, &left_pid, 0); */
+ }
+ return (0);
}
- if ((status = eval(left_fds, env, path, ast->op.left)) == -1)
- return (-1);
+ if ((status = eval(left_fds, env, path, ast->op.left)) == EVAL_FATAL)
+ return (EVAL_FATAL);
+ g_last_status = status;
if ((ast->op.sep == TAG_AND && status != 0) ||
(ast->op.sep == TAG_PIPE && status != 0) ||
(ast->op.sep == TAG_OR && status == 0))
@@ -40,7 +61,7 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast)
int wrapped_eval(void *void_param)
{
- t_fork_param_parent *param;
+ t_fork_param_args *param;
param = void_param;
return (eval(param->fds, param->env, param->path, param->ast));
@@ -48,17 +69,24 @@ int wrapped_eval(void *void_param)
int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast)
{
- t_fork_param_parent param;
+ int status;
+
+ if ((status = redir_extract(&ast->redirs, env, fds)) != 0)
+ return (status);
+ ast->tag ^= AST_PARENT;
+ return (eval_forked(fds, env, path, ast->parent_ast, NULL));
+}
+
+int eval_forked(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid)
+{
+ t_fork_param_args param;
- if (!redir_extract(&ast->redirs, env, fds))
- return (-1);
param.fds[0] = fds[0];
param.fds[1] = fds[1];
param.env = env;
param.path = path;
- ast->tag ^= AST_PARENT;
- param.ast = ast->parent_ast;
- return (fork_wrap(fds, &param, wrapped_eval));
+ param.ast = ast;
+ return (fork_wrap(fds, &param, wrapped_eval, child_pid));
}
int eval(int fds[2], t_env env, t_path path, t_ast *ast)
@@ -69,5 +97,5 @@ int eval(int fds[2], t_env env, t_path path, t_ast *ast)
return (eval_op(fds, env, path, ast));
if (ast->tag == AST_CMD)
return (eval_cmd(fds, env, path, ast));
- return (-1);
+ return (EVAL_FATAL);
}