From 89fd444e84c406a1f5e27d511d64806436b62bc8 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 21 Mar 2017 15:13:32 +0100 Subject: [PATCH] job control fine tuning --- 42sh/Makefile | 2 + 42sh/includes/job_control.h | 6 ++- 42sh/includes/minishell.h | 4 +- 42sh/libft/includes/error.h | 2 +- 42sh/libft/src/str/ft_strcat.c | 2 +- 42sh/src/builtin/builtin_exit.c | 12 ++---- 42sh/src/builtin/builtin_read.c | 2 +- 42sh/src/exec/ast_free.c | 2 +- 42sh/src/exec/mark_process_status.c | 4 +- 42sh/src/job_control/builtin_jobs.c | 50 ++++++---------------- 42sh/src/job_control/do_job_notification.c | 4 +- 42sh/src/job_control/has_running_job.c | 31 ++++++++++++++ 42sh/src/job_control/has_stopped_job.c | 31 ++++++++++++++ 42sh/src/job_control/job_format.c | 2 +- 42sh/src/job_control/job_format_head.c | 14 +++--- 42sh/src/job_control/job_getrank.c | 40 +++++++++++------ 42sh/src/job_control/job_is_stopped.c | 2 +- 42sh/src/job_control/job_notify_change.c | 4 +- 42sh/src/job_control/job_run.c | 4 +- 42sh/src/job_control/process_format.c | 6 +-- 42sh/src/main/data_exit.c | 2 +- 42sh/src/main/main.c | 2 +- 22 files changed, 139 insertions(+), 89 deletions(-) create mode 100644 42sh/src/job_control/has_running_job.c create mode 100644 42sh/src/job_control/has_stopped_job.c diff --git a/42sh/Makefile b/42sh/Makefile index c8714fb5..68b287f2 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -174,6 +174,8 @@ job_control/builtin_bg.c\ job_control/builtin_fg.c\ job_control/builtin_jobs.c\ job_control/do_job_notification.c\ +job_control/has_running_job.c\ +job_control/has_stopped_job.c\ job_control/job_addprocess.c\ job_control/job_cmp_id.c\ job_control/job_format.c\ diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 12bc50b4..13e3c824 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/20 15:39:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:40:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ # define JOB_IS_BG(j) (j & JOB_BG) # define JOB_IS_FG(j) (!JOB_IS_BG(j)) -# define JOBS_OPTS_L (1 << 0) +# define JOBS_OPT_L (1 << 0) struct s_jobc { @@ -50,6 +50,8 @@ void mark_job_as_running (t_job *j); int mark_process_status(pid_t pid, int status); int job_is_stopped(t_job *job); int job_is_completed(t_job *job); +int has_running_job(void); +int has_stopped_job(void); void job_new(char **av, pid_t pid); void job_run(t_job *job, int foreground); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index b3ead229..cb531282 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/03/21 14:03:00 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:05:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ /* # define malloc(x) NULL */ # define SH_MSG(s, ...) "{red}%s: " s "{eoc}\n", g_argv[0], ##__VA_ARGS__ -# define SH_ERR(s, ...) ft_dprintf(STDERR, SH_MSG(s, ##__VA_ARGS__)) +# define SH_ERR(s, ...) ft_dprintf(STDERR, SH_MSG(s, ##__VA_ARGS__)) #ifndef DEBUG # define DEBUG_MODE 0 diff --git a/42sh/libft/includes/error.h b/42sh/libft/includes/error.h index 572c8300..c9eb792b 100644 --- a/42sh/libft/includes/error.h +++ b/42sh/libft/includes/error.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 15:34:21 by jhalford #+# #+# */ -/* Updated: 2017/03/21 14:02:59 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:05:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/str/ft_strcat.c b/42sh/libft/src/str/ft_strcat.c index d0d028ef..76f6f25c 100644 --- a/42sh/libft/src/str/ft_strcat.c +++ b/42sh/libft/src/str/ft_strcat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/08/07 10:56:53 by jhalford #+# #+# */ -/* Updated: 2016/12/09 19:11:20 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:21:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index f00f69b1..f238a25a 100644 --- a/42sh/src/builtin/builtin_exit.c +++ b/42sh/src/builtin/builtin_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */ -/* Updated: 2017/03/21 14:01:32 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:48:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,18 +18,14 @@ int builtin_exit(const char *path, char *const av[], char *const envp[]) { int status; static int notified = 0; - t_jobc *jobc; - t_list *jlist; (void)envp; (void)path; - jobc = &data_singleton()->jobc; - jlist = jobc->first_job; - if (SH_HAS_JOBC(data_singleton()->opts) && jlist && !notified) + if (SH_HAS_JOBC(data_singleton()->opts) && !notified) { notified = 1; - ft_dprintf(2, "%s: you have live jobs (running or suspended)\n", - data_singleton()->argv[0]); + if (has_stopped_job() || has_running_job()) + SH_ERR("There are running and/or stopped jobs"); return (0); } if (av && av[1] && !ft_stris(av[1], ft_isdigit)) diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index 63da15d9..01979e13 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ -/* Updated: 2017/03/20 15:34:56 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:10:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ast_free.c b/42sh/src/exec/ast_free.c index 9ec0e141..17576e73 100644 --- a/42sh/src/exec/ast_free.c +++ b/42sh/src/exec/ast_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */ -/* Updated: 2017/03/20 10:24:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:22:58 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/mark_process_status.c b/42sh/src/exec/mark_process_status.c index a4436b64..7c6af2b2 100644 --- a/42sh/src/exec/mark_process_status.c +++ b/42sh/src/exec/mark_process_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/03/21 13:41:58 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:05:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,6 @@ int mark_process_status(pid_t pid, int status) } return (0); } - ft_dprintf(2, "{red}No child process %d.\n", pid); + SH_ERR("No child process %d", pid); return (1); } diff --git a/42sh/src/job_control/builtin_jobs.c b/42sh/src/job_control/builtin_jobs.c index dbae9068..80272c23 100644 --- a/42sh/src/job_control/builtin_jobs.c +++ b/42sh/src/job_control/builtin_jobs.c @@ -6,40 +6,17 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/03/20 11:47:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:48:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static int bt_jobs_parse(char **av, int *i) +t_cliopts g_jobs_opts[] = { - int opts; - int j; - - opts = 0; - *i = 1; - while (av[*i]) - { - j = 0; - if (av[*i][j++] != '-') - break ; - while (av[*i][j]) - { - if (av[*i][j] == 'l') - opts |= JOBS_OPTS_L; - else - { - ft_dprintf(2, "{red}%s: bad option: -%c{eoc}\n", - SHELL_NAME, av[*i][j]); - return (-1); - } - j++; - } - (*i)++; - } - return (opts); -} + {'l', NULL, JOBS_OPT_L, 0, NULL}, + {0, 0, 0, 0, 0}, +}; static void bt_jobs_all(int opts) { @@ -74,7 +51,7 @@ static int bt_jobs_spec(char **av, int opts) id = ft_atoi(*av); if (!(lst = ft_lst_find(jlist, &id, job_cmp_id))) { - ft_dprintf(2, "{red}jobs: %s: no such job{eoc}\n", *av); + SH_ERR("jobs: %s: no such job", *av); return (1); } job_format(lst->content, opts); @@ -85,21 +62,20 @@ static int bt_jobs_spec(char **av, int opts) int builtin_jobs(const char *path, char *const av[], char *const envp[]) { - int opts; - int i; + t_data_template data; (void)path; (void)envp; if (!SH_HAS_JOBC(data_singleton()->opts)) { - ft_dprintf(2, "{red}jobs: %s{eoc}\n", SH_MSG_NOJOBC); + SH_ERR("jobs: %s", SH_MSG_NOJOBC); return (1); } - if ((opts = bt_jobs_parse((char**)av, &i)) < 0) - return (1); - if (!av[i]) - bt_jobs_all(opts); - else if (bt_jobs_spec((char **)av + i, opts)) + if (cliopts_get((char**)av, g_jobs_opts, &data)) + return (ft_perror()); + if (!*data.av_data) + bt_jobs_all(data.flag); + else if (bt_jobs_spec(data.av_data, data.flag)) return (1); return (0); } diff --git a/42sh/src/job_control/do_job_notification.c b/42sh/src/job_control/do_job_notification.c index dfcbbdff..b9536e8a 100644 --- a/42sh/src/job_control/do_job_notification.c +++ b/42sh/src/job_control/do_job_notification.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 13:01:19 by jhalford #+# #+# */ -/* Updated: 2017/03/20 15:23:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:14:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,7 @@ int do_job_notification(void) || (job_is_stopped(j) && !(j->attrs & JOB_NOTIFIED))) { ret = 1; - job_format(j, JOBS_OPTS_L); + job_format(j, JOBS_OPT_L); j->attrs |= JOB_NOTIFIED; job_remove(j->id); } diff --git a/42sh/src/job_control/has_running_job.c b/42sh/src/job_control/has_running_job.c new file mode 100644 index 00000000..d6a211ae --- /dev/null +++ b/42sh/src/job_control/has_running_job.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* has_running_job.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/21 14:27:52 by jhalford #+# #+# */ +/* Updated: 2017/03/21 14:36:47 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int has_running_job(void) +{ + t_jobc *jobc; + t_list *jlist; + t_job *j; + + jobc = &data_singleton()->jobc; + jlist = jobc->first_job; + while (jlist) + { + j = jlist->content; + if (!job_is_stopped(j)) + return (1); + jlist = jlist->next; + } + return (0); +} diff --git a/42sh/src/job_control/has_stopped_job.c b/42sh/src/job_control/has_stopped_job.c new file mode 100644 index 00000000..0260b0d0 --- /dev/null +++ b/42sh/src/job_control/has_stopped_job.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* has_stopped_job.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/21 14:40:36 by jhalford #+# #+# */ +/* Updated: 2017/03/21 14:41:08 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int has_stopped_job(void) +{ + t_jobc *jobc; + t_list *jlist; + t_job *j; + + jobc = &data_singleton()->jobc; + jlist = jobc->first_job; + while (jlist) + { + j = jlist->content; + if (job_is_stopped(j)) + return (1); + jlist = jlist->next; + } + return (0); +} diff --git a/42sh/src/job_control/job_format.c b/42sh/src/job_control/job_format.c index dde4e0fb..b2822863 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/31 15:06:44 by jhalford #+# #+# */ -/* Updated: 2017/01/31 15:06:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:48:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job_control/job_format_head.c b/42sh/src/job_control/job_format_head.c index f874933e..450fcdeb 100644 --- a/42sh/src/job_control/job_format_head.c +++ b/42sh/src/job_control/job_format_head.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:10:38 by jhalford #+# #+# */ -/* Updated: 2017/01/31 14:53:13 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:55:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,12 +18,8 @@ void job_format_head(t_job *j) int rank[2]; job_getrank(&rank); - if (j->id == rank[0]) - crank = '+'; - else if (j->id == rank[1]) - crank = '-'; - else - crank = ' '; - ft_printf("{mag}[%i] %c ", j->id, crank); - ft_printf("{eoc}"); + crank = j->id == rank[0] ? '+' : ' '; + crank = j->id == rank[1] ? '-' : crank; + DG("raks [%i:%i]", rank[0], rank[1]); + ft_printf("{mag}[%i]%c {eoc}", j->id, crank); } diff --git a/42sh/src/job_control/job_getrank.c b/42sh/src/job_control/job_getrank.c index 3f8797b1..319968e2 100644 --- a/42sh/src/job_control/job_getrank.c +++ b/42sh/src/job_control/job_getrank.c @@ -6,29 +6,45 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 12:38:31 by jhalford #+# #+# */ -/* Updated: 2017/03/20 14:27:04 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 15:13:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void job_getrank(int (*rank)[2]) +static int get_jobrank(t_list *jlist, int rank) { t_job *job; - t_jobc *jobc; - t_list *jlist; - int i; + t_list *head; + int ret; - i = 0; - jobc = &data_singleton()->jobc; - jlist = jobc->first_job; - (*rank)[0] = 0; - (*rank)[1] = 0; - while (jlist && i < 2) + head = jlist; + ret = 0; + while (jlist && !ret) { job = jlist->content; if (job_is_stopped(job)) - (*rank)[i++] = job->id; + rank ? rank-- : (ret = job->id); jlist = jlist->next; } + jlist = head; + while (jlist && !ret) + { + job = jlist->content; + if (!job_is_stopped(job)) + rank ? rank-- : (ret = job->id); + jlist = jlist->next; + } + return (ret); +} + +void job_getrank(int (*rank)[2]) +{ + t_jobc *jobc; + t_list *jlist; + + jobc = &data_singleton()->jobc; + jlist = jobc->first_job; + (*rank)[0] = get_jobrank(jlist, 0); + (*rank)[1] = get_jobrank(jlist, 1); } diff --git a/42sh/src/job_control/job_is_stopped.c b/42sh/src/job_control/job_is_stopped.c index 47605f59..177eac99 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/03/20 14:36:37 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:41:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job_control/job_notify_change.c b/42sh/src/job_control/job_notify_change.c index cc594cc6..f854d8ff 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/03/20 14:24:47 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:23:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,5 +19,5 @@ void job_notify_change(int id) jobc = &data_singleton()->jobc; job = ft_lst_find(jobc->first_job, &id, job_cmp_id)->content; - job_format(job, JOBS_OPTS_L); + job_format(job, JOBS_OPT_L); } diff --git a/42sh/src/job_control/job_run.c b/42sh/src/job_control/job_run.c index 7dae037f..3f13a224 100644 --- a/42sh/src/job_control/job_run.c +++ b/42sh/src/job_control/job_run.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 16:54:18 by jhalford #+# #+# */ -/* Updated: 2017/03/20 10:40:16 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:24:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ void job_run(t_job *job, int foreground) { mark_job_as_running(job); - job_format(job, JOBS_OPTS_L); + job_format(job, JOBS_OPT_L); if (foreground) put_job_in_foreground(job, 1); else diff --git a/42sh/src/job_control/process_format.c b/42sh/src/job_control/process_format.c index 7fce0c80..4fba94b2 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/03/20 11:47:49 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:11:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -99,10 +99,10 @@ void process_format(t_list **plist, int firstp, int opts) p = (*plist)->content; if (!firstp) ft_printf(" "); - if (opts & JOBS_OPTS_L) + if (opts & JOBS_OPT_L) ft_printf("%i ", p->pid); process_format_state(p); - if (opts & JOBS_OPTS_L) + if (opts & JOBS_OPT_L) process_format_com_long(plist); else process_format_com_short(plist, p->state); diff --git a/42sh/src/main/data_exit.c b/42sh/src/main/data_exit.c index dfaf2006..f468742d 100644 --- a/42sh/src/main/data_exit.c +++ b/42sh/src/main/data_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/07 18:07:50 by jhalford #+# #+# */ -/* Updated: 2017/03/21 13:42:07 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:24:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 723fd95d..c419a0d2 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */ -/* Updated: 2017/03/21 13:39:17 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 14:04:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */