From 0a8f2cf987a55d6e1c4b210f0f99a1a1e4e4460a Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Tue, 15 Sep 2020 20:35:24 +0200 Subject: Partially fixing pipes (more than 1 still breaks) --- include/eval.h | 10 +++++----- include/minishell.h | 2 +- src/eval/cmd.c | 6 +++--- src/eval/eval.c | 27 ++++++++++++++++++++------- src/eval/operation.c | 39 +++++++++++++++++++++++---------------- src/eval/parenthesis.c | 8 ++++---- src/main.c | 6 +++--- subject.pdf | Bin 1544202 -> 1275142 bytes 8 files changed, 59 insertions(+), 39 deletions(-) diff --git a/include/eval.h b/include/eval.h index 0a14406..819b3cc 100644 --- a/include/eval.h +++ b/include/eval.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:30 by charles #+# #+# */ -/* Updated: 2020/09/15 17:50:35 by charles ### ########.fr */ +/* Updated: 2020/09/15 20:09:27 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,14 +49,14 @@ extern pid_t g_child_pid; ** eval.c */ -int fork_wrap(int fds[2], void *passed, t_wrapped_func wrapped, pid_t *child_pid); -int eval(int fds[2], t_env env, t_ast *ast, pid_t *child_pid); +int fork_wrap(int fds[2], void *passed, t_wrapped_func wrapped, pid_t *child_pid, int fd_to_close); +int eval(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int fd_to_close); /* ** cmd.c */ -int eval_cmd(int fds[2], t_env env, t_ast *ast, pid_t *child_pid); +int eval_cmd(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int fd_to_close); /* ** operation.c @@ -68,7 +68,7 @@ int eval_operation(int fds[2], t_env env, t_ast *ast); ** parenthesis.c */ -int eval_parenthesis(int fds[2], t_env env, t_ast *ast); +int eval_parenthesis(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int fd_to_close); /* ** redir.c diff --git a/include/minishell.h b/include/minishell.h index 3622463..6c7eaf0 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/26 15:33:51 by cacharle #+# #+# */ -/* Updated: 2020/09/15 17:43:16 by charles ### ########.fr */ +/* Updated: 2020/09/15 20:34:19 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/eval/cmd.c b/src/eval/cmd.c index cc22441..3e301b0 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/09/15 17:51:05 by charles ### ########.fr */ +/* Updated: 2020/09/15 20:06:39 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,7 @@ int wrapped_cmd(t_fork_param_cmd *param) return (status); } -int eval_cmd(int fds[2], t_env env, t_ast *ast, pid_t *child_pid) +int eval_cmd(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int fd_to_close) { t_fork_param_cmd param; char **argv; @@ -56,7 +56,7 @@ int eval_cmd(int fds[2], t_env env, t_ast *ast, pid_t *child_pid) param.argv = argv; param.env = env; - status = fork_wrap(fds, ¶m, (t_wrapped_func)wrapped_cmd, child_pid); + status = fork_wrap(fds, ¶m, (t_wrapped_func)wrapped_cmd, child_pid, fd_to_close); ft_split_destroy(argv); g_last_status = status; return (status); diff --git a/src/eval/eval.c b/src/eval/eval.c index 9f2fc28..899a880 100644 --- a/src/eval/eval.c +++ b/src/eval/eval.c @@ -6,12 +6,22 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/13 20:38:06 by charles #+# #+# */ -/* Updated: 2020/09/15 17:45:58 by charles ### ########.fr */ +/* Updated: 2020/09/15 20:09:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "eval.h" +int st_replace(int oldfd, int newfd) +{ + if (oldfd != FD_NONE) + { + dup2(oldfd, newfd); + close(oldfd); + } + return 0; +} + /* ** \brief Wrap a function in a fork ** \param fds fork read/write file descriptors @@ -23,7 +33,7 @@ */ int fork_wrap( - int fds[2], void *passed, t_wrapped_func wrapped, pid_t *child_pid + int fds[2], void *passed, t_wrapped_func wrapped, pid_t *child_pid, int fd_to_close ) { int status; @@ -37,9 +47,12 @@ int fork_wrap( return (EVAL_FATAL); if (*child_pid == 0) { - if ((fds[FD_READ] != FD_NONE && dup2(fds[FD_READ], STDIN_FILENO) == -1) || - (fds[FD_WRITE] != FD_NONE && dup2(fds[FD_WRITE], STDOUT_FILENO) == -1)) + if (st_replace(fds[FD_READ], STDIN_FILENO) != 0) + exit(EXIT_FAILURE); + if (st_replace(fds[FD_WRITE], STDOUT_FILENO) != 0) exit(EXIT_FAILURE); + if (fd_to_close != FD_NONE) + close(fd_to_close); if ((status = wrapped(passed)) == EVAL_FATAL) exit(EXIT_FAILURE); exit(status); @@ -59,13 +72,13 @@ int fork_wrap( ** \return The last command status or EVAL_FATAL on error */ -int eval(int fds[2], t_env env, t_ast *ast, pid_t *child_pid) +int eval(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int fd_to_close) { if (ast->tag == AST_PARENT) - return (eval_parenthesis(fds, env, ast)); + return (eval_parenthesis(fds, env, ast, child_pid, fd_to_close)); if (ast->tag == AST_OP) return (eval_operation(fds, env, ast)); if (ast->tag == AST_CMD) - return (eval_cmd(fds, env, ast, child_pid)); + return (eval_cmd(fds, env, ast, child_pid, fd_to_close)); return (EVAL_FATAL); } diff --git a/src/eval/operation.c b/src/eval/operation.c index e2ce55b..cd154b5 100644 --- a/src/eval/operation.c +++ b/src/eval/operation.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 15:27:22 by charles #+# #+# */ -/* Updated: 2020/09/15 16:43:21 by charles ### ########.fr */ +/* Updated: 2020/09/15 20:30:27 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,30 +31,37 @@ int eval_operation(int fds[2], t_env env, t_ast *ast) pid_t left_pid; pid_t right_pid; - eval(left_fds, env, ast->op.left, &left_pid); + + /* left_pid = fork(); */ + /* if (left_pid != 0) */ + /* { */ + /* dup2(p[FD_WRITE], STDOUT_FILENO); */ + /* close(p[FD_READ]); */ + /* exit(eval(left_fds, env, ast->op.left, NULL)); */ + /* } */ + /* */ + /* right_pid = fork(); */ + /* if (right_pid != 0) */ + /* { */ + /* dup2(p[FD_READ], STDIN_FILENO); */ + /* close(p[FD_WRITE]); */ + /* exit(eval(right_fds, env, ast->op.right, NULL)); */ + /* } */ + eval(left_fds, env, ast->op.left, &left_pid, p[FD_READ]); close(p[FD_WRITE]); - status = eval(right_fds, env, ast->op.right, &right_pid); + status = eval(right_fds, env, ast->op.right, &right_pid, p[FD_WRITE]); close(p[FD_READ]); - pid_t finished; - finished = wait(&finished); - if (finished == left_pid) - { - waitpid(right_pid, &finished, 0); - } - else if (finished == right_pid) - { - /* kill(left_pid, SIGTERM); // FIXME weird bug with parent on both sides */ - waitpid(left_pid, &finished, 0); - } + wait(&left_pid); + wait(&left_pid); return (status); } - if ((status = eval(left_fds, env, ast->op.left, NULL)) == EVAL_FATAL) + if ((status = eval(left_fds, env, ast->op.left, NULL, FD_NONE)) == 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)) return (status); - return (eval(right_fds, env, ast->op.right, NULL)); + return (eval(right_fds, env, ast->op.right, NULL, FD_NONE)); } diff --git a/src/eval/parenthesis.c b/src/eval/parenthesis.c index 249e24d..83edf40 100644 --- a/src/eval/parenthesis.c +++ b/src/eval/parenthesis.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/13 20:38:29 by charles #+# #+# */ -/* Updated: 2020/09/15 16:50:21 by charles ### ########.fr */ +/* Updated: 2020/09/15 20:09:13 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,10 +14,10 @@ int wrapped_eval(t_fork_param_args *param) { - return (eval(param->fds, param->env, param->ast, NULL)); + return (eval(param->fds, param->env, param->ast, NULL, FD_NONE)); } -int eval_parenthesis(int fds[2], t_env env, t_ast *ast) +int eval_parenthesis(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int fd_to_close) { int status; t_fork_param_args param; @@ -29,5 +29,5 @@ int eval_parenthesis(int fds[2], t_env env, t_ast *ast) param.fds[1] = fds[1]; param.env = env; param.ast = ast->parent_ast; - return (fork_wrap(fds, ¶m, (t_wrapped_func)wrapped_eval, NULL)); + return (fork_wrap(fds, ¶m, (t_wrapped_func)wrapped_eval, child_pid, fd_to_close)); } diff --git a/src/main.c b/src/main.c index 194967c..9ce9bfe 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/09/15 17:04:20 by charles ### ########.fr */ +/* Updated: 2020/09/15 20:34:09 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -120,7 +120,7 @@ int main(int argc, char **argv, char **envp) /* printf("===redirs===\n"); */ /* ft_lstiter(parser_out->ast->redirs, token_debug); */ int fds[2] = {FD_NONE, FD_NONE}; - status = eval(fds, env, parser_out->ast, NULL); + status = eval(fds, env, parser_out->ast, NULL, FD_NONE); if (status == EVAL_FATAL) exit(1); g_last_status = status; @@ -156,7 +156,7 @@ int main(int argc, char **argv, char **envp) } int fds[2] = {FD_NONE, FD_NONE}; - int status = eval(fds, env, parser_out->ast, NULL); + int status = eval(fds, env, parser_out->ast, NULL, FD_NONE); if (status == EVAL_FATAL) exit(1); g_last_status = status; diff --git a/subject.pdf b/subject.pdf index da124ad..03da8be 100644 Binary files a/subject.pdf and b/subject.pdf differ -- cgit