From 0ec9313b4c6232332dd519c551a7d83256bae7da Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 10 Jan 2017 11:32:02 +0100 Subject: [PATCH] pretty stable version of job control, ready to merge --- 42sh/includes/exec.h | 2 +- 42sh/libft | 2 +- 42sh/src/exec/exec_command.c | 5 ++++- 42sh/src/exec/launch_process.c | 2 +- 42sh/src/job-control/builtin_bg.c | 14 +++++++++++--- 42sh/src/job-control/builtin_fg.c | 16 +++++++++++++--- 42sh/src/job-control/builtin_jobs.c | 2 +- 42sh/src/job-control/job_cmp_id.c | 2 +- 42sh/src/job-control/job_format.c | 2 +- 42sh/src/job-control/job_getrank.c | 20 +++++++++----------- 42sh/src/job-control/job_is_stopped.c | 2 +- 42sh/src/job-control/job_notify_change.c | 2 +- 42sh/src/job-control/mark_job_as_running.c | 9 ++++++--- 42sh/src/job-control/process_format.c | 5 ++--- 42sh/src/job-control/process_mark_status.c | 6 +++--- 42sh/src/job-control/put_job_in_foreground.c | 3 +-- 42sh/src/job-control/sigtstp_handler.c | 2 +- 17 files changed, 58 insertions(+), 38 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 5d047b1b..4146a16e 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:58:55 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:23:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft b/42sh/libft index eded1f1d..d799465c 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit eded1f1d189a6fd631b705f9d4a466fe088b94b3 +Subproject commit d799465c2e0d51f24fe4d5cf1e51c5109a1617a0 diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index fcb2e0ab..23c8d28a 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/01/09 12:20:24 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:03:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,9 +27,12 @@ int exec_command(t_btree **ast) { job_addprocess(p); if (IS_PIPEEND(p->attributes)) + { JOB_IS_FG(job->attributes) ? put_job_in_foreground(job, 0): put_job_in_background(job, 0); + job->pgid = 0; + } } p->av = NULL; p->pid = 0; diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 1ca9757e..70b1175f 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/01/09 16:38:21 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:30:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/builtin_bg.c b/42sh/src/job-control/builtin_bg.c index 427bf7d2..9e98b5e6 100644 --- a/42sh/src/job-control/builtin_bg.c +++ b/42sh/src/job-control/builtin_bg.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 16:54:18 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:57:20 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:11:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,19 +17,27 @@ int builtin_bg(const char *path, char *const av[], char *const envp[]) { t_jobc *jobc; t_job *job; + t_list *jlist; int rank[2]; + int id; (void)path; (void)envp; (void)av; jobc = &data_singleton()->jobc; - job = jobc->first_job->content; job_getrank(&rank); - if (job) + id = av[1] ? ft_atoi(av[1]) : rank[0]; + if (id == 0 && !av[1]) + return (0); + jlist = ft_lst_find(jobc->first_job, &id, job_cmp_id); + if (jlist) { + job = jlist->content; mark_job_as_running(job); job_format(job, rank, JOBS_OPTS_L); put_job_in_background(job, 1); } + else + ft_dprintf(2, "{red}bg: job not found: %i{eoc}\n", id); return (0); } diff --git a/42sh/src/job-control/builtin_fg.c b/42sh/src/job-control/builtin_fg.c index 2f350ea6..c594df80 100644 --- a/42sh/src/job-control/builtin_fg.c +++ b/42sh/src/job-control/builtin_fg.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/08 14:30:07 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:54:39 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:16:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,16 +16,26 @@ int builtin_fg(const char *path, char *const av[], char *const envp[]) { t_jobc *jobc; t_job *job; + t_list *jlist; + int rank[2]; + int id; (void)path; (void)envp; (void)av; jobc = &data_singleton()->jobc; - job = jobc->first_job->content; - if (job) + job_getrank(&rank); + id = av[1] ? ft_atoi(av[1]) : rank[0]; + if (id == 0 && !av[1]) + return (0); + jlist = ft_lst_find(jobc->first_job, &id, job_cmp_id); + if (jlist) { + job = jlist->content; mark_job_as_running(job); put_job_in_foreground(job, 1); } + else + ft_dprintf(2, "{red}fg: job not found: [%i]{eoc}\n", id); return (0); } diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c index 8ce060dc..46b73513 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:20:09 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:16:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_cmp_id.c b/42sh/src/job-control/job_cmp_id.c index 50d6ada8..cc9614d2 100644 --- a/42sh/src/job-control/job_cmp_id.c +++ b/42sh/src/job-control/job_cmp_id.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 13:38:13 by jhalford #+# #+# */ -/* Updated: 2017/01/09 12:34:05 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:08:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_format.c b/42sh/src/job-control/job_format.c index 0117d3f8..1c8f3dd1 100644 --- a/42sh/src/job-control/job_format.c +++ b/42sh/src/job-control/job_format.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 12:47:17 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:55:36 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:16:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_getrank.c b/42sh/src/job-control/job_getrank.c index e24f3e03..8c7b6996 100644 --- a/42sh/src/job-control/job_getrank.c +++ b/42sh/src/job-control/job_getrank.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 12:38:31 by jhalford #+# #+# */ -/* Updated: 2017/01/09 12:47:08 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:30:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,23 +14,21 @@ void job_getrank(int (*rank)[2]) { - t_job *job; - t_jobc *jobc; - t_list *jlist; + t_job *job; + t_jobc *jobc; + t_list *jlist; + int i; + i = 0; jobc = &data_singleton()->jobc; jlist = jobc->first_job; (*rank)[0] = 0; (*rank)[1] = 0; - if (jlist) + while (jlist && i < 2) { job = jlist->content; - (*rank)[0] = job->id; + if (job_is_stopped(job->id)) + (*rank)[i++] = job->id; jlist = jlist->next; - if (jlist) - { - job = jlist->content; - (*rank)[1] = job->id; - } } } diff --git a/42sh/src/job-control/job_is_stopped.c b/42sh/src/job-control/job_is_stopped.c index 135988d6..b83c9024 100644 --- a/42sh/src/job-control/job_is_stopped.c +++ b/42sh/src/job-control/job_is_stopped.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 15:06:45 by jhalford #+# #+# */ -/* Updated: 2017/01/09 14:02:13 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:31:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_notify_change.c b/42sh/src/job-control/job_notify_change.c index 7d0c4ead..73f0a594 100644 --- a/42sh/src/job-control/job_notify_change.c +++ b/42sh/src/job-control/job_notify_change.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 15:04:03 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:49:16 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:16:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/mark_job_as_running.c b/42sh/src/job-control/mark_job_as_running.c index c10a2b5b..0854b8d9 100644 --- a/42sh/src/job-control/mark_job_as_running.c +++ b/42sh/src/job-control/mark_job_as_running.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/08 14:40:40 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:58:38 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:52:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,8 +21,11 @@ void mark_job_as_running (t_job *j) while (plist) { p = plist->content; - p->attributes &= ~PROCESS_STATE_MASK; - p->attributes |= PROCESS_CONTINUED; + if (p->attributes & PROCESS_SUSPENDED) + { + p->attributes &= ~PROCESS_STATE_MASK; + p->attributes |= PROCESS_CONTINUED; + } plist = plist->next; } j->attributes &= ~JOB_NOTIFIED; diff --git a/42sh/src/job-control/process_format.c b/42sh/src/job-control/process_format.c index d9b0dd4f..4a7e734a 100644 --- a/42sh/src/job-control/process_format.c +++ b/42sh/src/job-control/process_format.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:05:55 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:58:36 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:30:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,6 @@ void process_format(t_list **plist, int firstp, int opts) ft_printf(" "); if (opts & JOBS_OPTS_L) ft_printf("%i ", p->pid); - state = p->attributes & PROCESS_STATE_MASK; if (state == PROCESS_RUNNING) ft_putstr("running "); @@ -32,7 +31,7 @@ void process_format(t_list **plist, int firstp, int opts) { ft_putstr("continued"); p->attributes &= ~PROCESS_STATE_MASK; - p->attributes &= ~PROCESS_RUNNING; + p->attributes |= PROCESS_RUNNING; } else if (state == PROCESS_COMPLETED) { diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/job-control/process_mark_status.c index 429d1c44..83e7b921 100644 --- a/42sh/src/job-control/process_mark_status.c +++ b/42sh/src/job-control/process_mark_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/01/09 14:04:19 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:55:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,13 +23,13 @@ int process_mark_status(pid_t pid, int status) p->status = status; if (WIFSTOPPED(status)) { - DG("marking: pid=%i, status=%i (stopped)", pid, status); + DG("marking: pid=%i, status=%i (stopped sig %i)", pid, status, WTERMSIG(status)); p->attributes &= ~PROCESS_STATE_MASK; p->attributes |= PROCESS_SUSPENDED; } else { - DG("marking: pid=%i, status=%i (completed)", pid, status); + DG("marking: pid=%i, status=%i (completed sig %i)", pid, status, WTERMSIG(status)); p->attributes &= ~PROCESS_STATE_MASK; p->attributes |= PROCESS_COMPLETED; if (WIFSIGNALED(status)) diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 9272aa08..fd3ed42a 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/01/09 12:29:04 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:06:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,6 @@ int put_job_in_foreground(t_job *job, int cont) DG("gonna wait for job id=%i", job->id); job_wait(job->id); job_remove(job->id); - job->pgid = 0; /* Put the shell back in the foreground. */ signal(SIGTTOU, SIG_IGN); diff --git a/42sh/src/job-control/sigtstp_handler.c b/42sh/src/job-control/sigtstp_handler.c index 15f3316c..aa138934 100644 --- a/42sh/src/job-control/sigtstp_handler.c +++ b/42sh/src/job-control/sigtstp_handler.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:37:41 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:30:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */