From 20e96b751fd7b6a876b1c917d38c160a73e416cb Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Mar 2017 18:50:13 +0100 Subject: [PATCH] pipes work --- 42sh/Makefile | 2 +- 42sh/includes/job_control.h | 4 +-- 42sh/includes/types.h | 2 +- 42sh/src/exec/exec_command.c | 17 +++++++++-- 42sh/src/exec/exec_semi.c | 5 +--- 42sh/src/exec/ft_exec.c | 5 ++-- 42sh/src/exec/launch_process.c | 5 +++- .../mark_process_status.c} | 5 ++-- 42sh/src/exec/process_redirect.c | 6 ++-- 42sh/src/job-control/add_new_job.c | 9 ++---- 42sh/src/job-control/job_update_id.c | 2 +- 42sh/src/job-control/job_update_status.c | 4 +-- 42sh/src/job-control/job_wait.c | 28 ++++++++++++------- 42sh/src/job-control/put_job_in_foreground.c | 4 ++- 42sh/src/parser/read_stack.c | 4 ++- 15 files changed, 62 insertions(+), 40 deletions(-) rename 42sh/src/{job-control/process_mark_status.c => exec/mark_process_status.c} (90%) diff --git a/42sh/Makefile b/42sh/Makefile index 7d39d7e5..be5adabc 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -81,6 +81,7 @@ exec/fd_is_valid.c\ exec/ft_exec.c\ exec/ft_findexec.c\ exec/launch_process.c\ +exec/mark_process_status.c\ exec/process_redirect.c\ exec/process_reset.c\ exec/process_setexec.c\ @@ -159,7 +160,6 @@ job-control/mark_job_as_running.c\ job-control/process_cmp_pid.c\ job-control/process_format.c\ job-control/process_free.c\ -job-control/process_mark_status.c\ job-control/put_job_in_background.c\ job-control/put_job_in_foreground.c\ job-control/sigchld_handler.c\ diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 0437435c..b8f04fa4 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:38:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:35:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,7 +58,7 @@ void job_format_head(t_job *j); void job_update_status(void); void mark_job_as_running (t_job *j); -int process_mark_status(pid_t pid, int status); +int mark_process_status(pid_t pid, int status); int job_is_stopped(int id); int job_is_completed(int id); diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 893007b4..d134e8c9 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/03/03 17:30:47 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:19:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 801cb235..c794768e 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/03/03 17:59:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:49:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,15 +49,19 @@ int exec_cmd(t_btree **ast) job = &data_singleton()->exec.job; process_reset(&p); op = pop(&exec->op_stack); - DG("op=%i", op); fds[PIPE_WRITE] = STDOUT; fds[PIPE_READ] = STDIN; if (op == TK_AMP) exec->attrs |= JOB_BG; else if (op == TK_PIPE) + { pipe(fds); + DG("%i -> PIPE -> %i", fds[PIPE_WRITE], fds[PIPE_READ]); + } p.fdin = exec->fdin; + p.to_close = fds[PIPE_READ]; p.fdout = fds[PIPE_WRITE]; + p.redirs = cmd->redir; exec->fdin = fds[PIPE_READ]; if (IS_PIPESTART(p)) { @@ -69,7 +73,16 @@ int exec_cmd(t_btree **ast) process_setexec(&p); if (!(launch_process(&p))) ft_lstadd(&job->first_process, ft_lstnew(&p, sizeof(p))); + if (fds[PIPE_WRITE] != STDOUT) + close(fds[PIPE_WRITE]); if (IS_PIPEEND(p)) add_new_job(job); + if (JOB_IS_FG(job->attrs)) + put_job_in_foreground(job, 0); + else + { + job_notify_new(job); + put_job_in_background(job, 0); + } return (0); } diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index 58013f17..f664b25e 100644 --- a/42sh/src/exec/exec_semi.c +++ b/42sh/src/exec/exec_semi.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:01:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:10:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,11 +17,8 @@ int exec_semi(t_btree **ast) t_exec *exec; exec = &data_singleton()->exec; - DG(); push(&exec->op_stack, TK_SEMI); - DG(); ft_exec(&(*ast)->left); - DG(); exec->attrs &= ~EXEC_AOL_MASK; ft_exec(&(*ast)->right); // btree_delone(ast, &ast_free); diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 5d3b8090..1c46fea9 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:02:55 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:26:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ t_execmap g_execmap[] = {TK_AMP, &exec_ampersand}, {TK_AND_IF, &exec_and_if}, {TK_OR_IF, &exec_or_if}, - /* {TK_PIPE, &exec_pipe}, */ + {TK_PIPE, &exec_pipe}, {TK_WHILE, &exec_while}, {TK_IF, &exec_if}, {TK_ELIF, &exec_elif}, @@ -36,7 +36,6 @@ int ft_exec(t_btree **ast) int i; i = 0; - DG(); if (!*ast) return (0); item = (*ast)->item; diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 76fbd434..085ce337 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:29:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:49:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,9 @@ int launch_process(t_process *p) int pid; exec = &data_singleton()->exec; + DG("gonna launch [%s]", p->av[0]); + DG("fdin=[%i]", p->fdin); + DG("fdout=[%i]", p->fdout); if (p->attributes & PROCESS_BUILTIN && IS_PIPESINGLE(*p)) { if (process_redirect(p)) diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/exec/mark_process_status.c similarity index 90% rename from 42sh/src/job-control/process_mark_status.c rename to 42sh/src/exec/mark_process_status.c index fd285c0b..2665128d 100644 --- a/42sh/src/job-control/process_mark_status.c +++ b/42sh/src/exec/mark_process_status.c @@ -6,17 +6,18 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:50:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:32:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -int process_mark_status(pid_t pid, int status) +int mark_process_status(pid_t pid, int status) { t_list *plist; t_process *p; + DG("PMS pid=%i,s=%i", pid, status); if (pid > 1) { if ((plist = job_getprocess(pid))) diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index d62970e6..c13cf802 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2017/03/02 19:44:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:49:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,10 +48,10 @@ int process_redirect(t_process *p) } redirs = redirs->next; } - if (p->to_close != 0) + if (p->to_close != STDIN) close(p->to_close); if (p->fdin != STDIN) - dup2_close(p->fdout, STDOUT); + dup2_close(p->fdin, STDIN); if (p->fdout != STDOUT) dup2_close(p->fdout, STDOUT); return (0); diff --git a/42sh/src/job-control/add_new_job.c b/42sh/src/job-control/add_new_job.c index 2797edc2..daf402e6 100644 --- a/42sh/src/job-control/add_new_job.c +++ b/42sh/src/job-control/add_new_job.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/02 20:44:21 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:47:47 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:49:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ int add_new_job(t_job *job) { t_jobc *jobc; + DG("adding new job"); if (!job->first_process) return (1); jobc = &data_singleton()->jobc; @@ -23,10 +24,6 @@ int add_new_job(t_job *job) job->id = jobc->current_id; job->pgid = ((t_process*)job->first_process->content)->pid; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); - if (JOB_IS_FG(job->attrs)) - put_job_in_foreground(job, 0); - else - job_notify_new(job); - put_job_in_background(job, 0); + job = jobc->first_job->content; return (0); } diff --git a/42sh/src/job-control/job_update_id.c b/42sh/src/job-control/job_update_id.c index 1fba6f36..8b423e52 100644 --- a/42sh/src/job-control/job_update_id.c +++ b/42sh/src/job-control/job_update_id.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 13:33:08 by jhalford #+# #+# */ -/* Updated: 2017/03/02 20:59:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:18:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_update_status.c b/42sh/src/job-control/job_update_status.c index 1fa30cd5..6ab2df78 100644 --- a/42sh/src/job-control/job_update_status.c +++ b/42sh/src/job-control/job_update_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:56:11 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:50:30 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:37:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,6 @@ void job_update_status(void) pid_t pid; pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG); - while (!process_mark_status(pid, status)) + while (!mark_process_status(pid, status)) pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG); } diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c index 62f5ebef..eb5a1546 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 11:49:05 by jhalford #+# #+# */ -/* Updated: 2017/01/31 13:44:17 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:38:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,15 +17,23 @@ int job_wait(int id) pid_t pid; int status; - if (job_is_stopped(id)) - return (0); - job_update_status(); - pid = waitpid(WAIT_ANY, &status, WUNTRACED); - while (!process_mark_status(pid, status) - && !job_is_completed(id) - && !job_is_stopped(id)) - { + DG("job wait [%i]", id); + /* if (job_is_stopped(id)) */ + /* return (0); */ + /* job_update_status(); */ + /* DG("after update status"); */ + /* pid = waitpid(WAIT_ANY, &status, WUNTRACED); */ + /* while (!process_mark_status(pid, status) */ + /* && !job_is_completed(id) */ + /* && !job_is_stopped(id)) */ + /* { */ + + /* pid = waitpid(WAIT_ANY, &status, WUNTRACED); */ + /* } */ + do pid = waitpid(WAIT_ANY, &status, WUNTRACED); - } + while (!mark_process_status(pid, status) + && !job_is_stopped(id) + && !job_is_completed(id)); return (0); } diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 3f783821..fefd9cdd 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:58:36 by jhalford #+# #+# */ -/* Updated: 2017/03/02 20:59:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:38:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,9 @@ int put_job_in_foreground(t_job *j, int cont) if (kill(-j->pgid, SIGCONT) < 0) DG("kill(SIGCONT) failed"); } + DG("before wait"); job_wait(j->id); + DG("after wait"); job_remove(j->id); tcsetpgrp(STDIN, jobc->shell_pgid); diff --git a/42sh/src/parser/read_stack.c b/42sh/src/parser/read_stack.c index 38e51155..1f79a713 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 15:32:10 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:28:12 by ariard ### ########.fr */ +/* Updated: 2017/03/03 18:09:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -194,6 +194,8 @@ char *read_state(t_sym current) return ("ALL"); if (current == NEWLINE_LIST) return ("NEWLINE_LIST"); + if (current == CMD) + return ("CMD"); if (current != 0) return ("NON-DEFINED"); if (current == 0)