pipes work

This commit is contained in:
Jack Halford 2017-03-03 18:50:13 +01:00
parent bc40339015
commit 20e96b751f
15 changed files with 62 additions and 40 deletions

View file

@ -81,6 +81,7 @@ exec/fd_is_valid.c\
exec/ft_exec.c\ exec/ft_exec.c\
exec/ft_findexec.c\ exec/ft_findexec.c\
exec/launch_process.c\ exec/launch_process.c\
exec/mark_process_status.c\
exec/process_redirect.c\ exec/process_redirect.c\
exec/process_reset.c\ exec/process_reset.c\
exec/process_setexec.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_cmp_pid.c\
job-control/process_format.c\ job-control/process_format.c\
job-control/process_free.c\ job-control/process_free.c\
job-control/process_mark_status.c\
job-control/put_job_in_background.c\ job-control/put_job_in_background.c\
job-control/put_job_in_foreground.c\ job-control/put_job_in_foreground.c\
job-control/sigchld_handler.c\ job-control/sigchld_handler.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 16:55:09 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 job_update_status(void);
void mark_job_as_running (t_job *j); 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_stopped(int id);
int job_is_completed(int id); int job_is_completed(int id);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:11:48 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:28:14 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; job = &data_singleton()->exec.job;
process_reset(&p); process_reset(&p);
op = pop(&exec->op_stack); op = pop(&exec->op_stack);
DG("op=%i", op);
fds[PIPE_WRITE] = STDOUT; fds[PIPE_WRITE] = STDOUT;
fds[PIPE_READ] = STDIN; fds[PIPE_READ] = STDIN;
if (op == TK_AMP) if (op == TK_AMP)
exec->attrs |= JOB_BG; exec->attrs |= JOB_BG;
else if (op == TK_PIPE) else if (op == TK_PIPE)
{
pipe(fds); pipe(fds);
DG("%i -> PIPE -> %i", fds[PIPE_WRITE], fds[PIPE_READ]);
}
p.fdin = exec->fdin; p.fdin = exec->fdin;
p.to_close = fds[PIPE_READ];
p.fdout = fds[PIPE_WRITE]; p.fdout = fds[PIPE_WRITE];
p.redirs = cmd->redir;
exec->fdin = fds[PIPE_READ]; exec->fdin = fds[PIPE_READ];
if (IS_PIPESTART(p)) if (IS_PIPESTART(p))
{ {
@ -69,7 +73,16 @@ int exec_cmd(t_btree **ast)
process_setexec(&p); process_setexec(&p);
if (!(launch_process(&p))) if (!(launch_process(&p)))
ft_lstadd(&job->first_process, ft_lstnew(&p, sizeof(p))); ft_lstadd(&job->first_process, ft_lstnew(&p, sizeof(p)));
if (fds[PIPE_WRITE] != STDOUT)
close(fds[PIPE_WRITE]);
if (IS_PIPEEND(p)) if (IS_PIPEEND(p))
add_new_job(job); 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); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 20:52:05 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; t_exec *exec;
exec = &data_singleton()->exec; exec = &data_singleton()->exec;
DG();
push(&exec->op_stack, TK_SEMI); push(&exec->op_stack, TK_SEMI);
DG();
ft_exec(&(*ast)->left); ft_exec(&(*ast)->left);
DG();
exec->attrs &= ~EXEC_AOL_MASK; exec->attrs &= ~EXEC_AOL_MASK;
ft_exec(&(*ast)->right); ft_exec(&(*ast)->right);
// btree_delone(ast, &ast_free); // btree_delone(ast, &ast_free);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 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_AMP, &exec_ampersand},
{TK_AND_IF, &exec_and_if}, {TK_AND_IF, &exec_and_if},
{TK_OR_IF, &exec_or_if}, {TK_OR_IF, &exec_or_if},
/* {TK_PIPE, &exec_pipe}, */ {TK_PIPE, &exec_pipe},
{TK_WHILE, &exec_while}, {TK_WHILE, &exec_while},
{TK_IF, &exec_if}, {TK_IF, &exec_if},
{TK_ELIF, &exec_elif}, {TK_ELIF, &exec_elif},
@ -36,7 +36,6 @@ int ft_exec(t_btree **ast)
int i; int i;
i = 0; i = 0;
DG();
if (!*ast) if (!*ast)
return (0); return (0);
item = (*ast)->item; item = (*ast)->item;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 14:20:45 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; int pid;
exec = &data_singleton()->exec; 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 (p->attributes & PROCESS_BUILTIN && IS_PIPESINGLE(*p))
{ {
if (process_redirect(p)) if (process_redirect(p))

View file

@ -6,17 +6,18 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 12:41:11 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" #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_list *plist;
t_process *p; t_process *p;
DG("PMS pid=%i,s=%i", pid, status);
if (pid > 1) if (pid > 1)
{ {
if ((plist = job_getprocess(pid))) if ((plist = job_getprocess(pid)))

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 16:04:18 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; redirs = redirs->next;
} }
if (p->to_close != 0) if (p->to_close != STDIN)
close(p->to_close); close(p->to_close);
if (p->fdin != STDIN) if (p->fdin != STDIN)
dup2_close(p->fdout, STDOUT); dup2_close(p->fdin, STDIN);
if (p->fdout != STDOUT) if (p->fdout != STDOUT)
dup2_close(p->fdout, STDOUT); dup2_close(p->fdout, STDOUT);
return (0); return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/02 20:44:21 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; t_jobc *jobc;
DG("adding new job");
if (!job->first_process) if (!job->first_process)
return (1); return (1);
jobc = &data_singleton()->jobc; jobc = &data_singleton()->jobc;
@ -23,10 +24,6 @@ int add_new_job(t_job *job)
job->id = jobc->current_id; job->id = jobc->current_id;
job->pgid = ((t_process*)job->first_process->content)->pid; job->pgid = ((t_process*)job->first_process->content)->pid;
ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job)));
if (JOB_IS_FG(job->attrs)) job = jobc->first_job->content;
put_job_in_foreground(job, 0);
else
job_notify_new(job);
put_job_in_background(job, 0);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 13:33:08 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 12:56:11 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_t pid;
pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG); 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); pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 11:49:05 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; pid_t pid;
int status; int status;
if (job_is_stopped(id)) DG("job wait [%i]", id);
return (0); /* if (job_is_stopped(id)) */
job_update_status(); /* return (0); */
pid = waitpid(WAIT_ANY, &status, WUNTRACED); /* job_update_status(); */
while (!process_mark_status(pid, status) /* DG("after update status"); */
&& !job_is_completed(id) /* pid = waitpid(WAIT_ANY, &status, WUNTRACED); */
&& !job_is_stopped(id)) /* 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); pid = waitpid(WAIT_ANY, &status, WUNTRACED);
} while (!mark_process_status(pid, status)
&& !job_is_stopped(id)
&& !job_is_completed(id));
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 14:58:36 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) if (kill(-j->pgid, SIGCONT) < 0)
DG("kill(SIGCONT) failed"); DG("kill(SIGCONT) failed");
} }
DG("before wait");
job_wait(j->id); job_wait(j->id);
DG("after wait");
job_remove(j->id); job_remove(j->id);
tcsetpgrp(STDIN, jobc->shell_pgid); tcsetpgrp(STDIN, jobc->shell_pgid);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 15:32:10 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"); return ("ALL");
if (current == NEWLINE_LIST) if (current == NEWLINE_LIST)
return ("NEWLINE_LIST"); return ("NEWLINE_LIST");
if (current == CMD)
return ("CMD");
if (current != 0) if (current != 0)
return ("NON-DEFINED"); return ("NON-DEFINED");
if (current == 0) if (current == 0)