job control fine tuning

This commit is contained in:
Jack Halford 2017-03-21 15:13:32 +01:00
parent 95e828b7b7
commit 89fd444e84
22 changed files with 139 additions and 89 deletions

View file

@ -174,6 +174,8 @@ job_control/builtin_bg.c\
job_control/builtin_fg.c\ job_control/builtin_fg.c\
job_control/builtin_jobs.c\ job_control/builtin_jobs.c\
job_control/do_job_notification.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_addprocess.c\
job_control/job_cmp_id.c\ job_control/job_cmp_id.c\
job_control/job_format.c\ job_control/job_format.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/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_BG(j) (j & JOB_BG)
# define JOB_IS_FG(j) (!JOB_IS_BG(j)) # define JOB_IS_FG(j) (!JOB_IS_BG(j))
# define JOBS_OPTS_L (1 << 0) # define JOBS_OPT_L (1 << 0)
struct s_jobc 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 mark_process_status(pid_t pid, int status);
int job_is_stopped(t_job *job); int job_is_stopped(t_job *job);
int job_is_completed(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_new(char **av, pid_t pid);
void job_run(t_job *job, int foreground); void job_run(t_job *job, int foreground);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 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 malloc(x) NULL */
# define SH_MSG(s, ...) "{red}%s: " s "{eoc}\n", g_argv[0], ##__VA_ARGS__ # 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 #ifndef DEBUG
# define DEBUG_MODE 0 # define DEBUG_MODE 0

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 15:34:21 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/07 10:56:53 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:28:41 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; int status;
static int notified = 0; static int notified = 0;
t_jobc *jobc;
t_list *jlist;
(void)envp; (void)envp;
(void)path; (void)path;
jobc = &data_singleton()->jobc; if (SH_HAS_JOBC(data_singleton()->opts) && !notified)
jlist = jobc->first_job;
if (SH_HAS_JOBC(data_singleton()->opts) && jlist && !notified)
{ {
notified = 1; notified = 1;
ft_dprintf(2, "%s: you have live jobs (running or suspended)\n", if (has_stopped_job() || has_running_job())
data_singleton()->argv[0]); SH_ERR("There are running and/or stopped jobs");
return (0); return (0);
} }
if (av && av[1] && !ft_stris(av[1], ft_isdigit)) if (av && av[1] && !ft_stris(av[1], ft_isdigit))

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/20 15:01:45 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

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

View file

@ -6,7 +6,7 @@
/* 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/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); return (0);
} }
ft_dprintf(2, "{red}No child process %d.\n", pid); SH_ERR("No child process %d", pid);
return (1); return (1);
} }

View file

@ -6,40 +6,17 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 17:43:01 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" #include "minishell.h"
static int bt_jobs_parse(char **av, int *i) t_cliopts g_jobs_opts[] =
{ {
int opts; {'l', NULL, JOBS_OPT_L, 0, NULL},
int j; {0, 0, 0, 0, 0},
};
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);
}
static void bt_jobs_all(int opts) static void bt_jobs_all(int opts)
{ {
@ -74,7 +51,7 @@ static int bt_jobs_spec(char **av, int opts)
id = ft_atoi(*av); id = ft_atoi(*av);
if (!(lst = ft_lst_find(jlist, &id, job_cmp_id))) 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); return (1);
} }
job_format(lst->content, opts); 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 builtin_jobs(const char *path, char *const av[], char *const envp[])
{ {
int opts; t_data_template data;
int i;
(void)path; (void)path;
(void)envp; (void)envp;
if (!SH_HAS_JOBC(data_singleton()->opts)) 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); return (1);
} }
if ((opts = bt_jobs_parse((char**)av, &i)) < 0) if (cliopts_get((char**)av, g_jobs_opts, &data))
return (1); return (ft_perror());
if (!av[i]) if (!*data.av_data)
bt_jobs_all(opts); bt_jobs_all(data.flag);
else if (bt_jobs_spec((char **)av + i, opts)) else if (bt_jobs_spec(data.av_data, data.flag))
return (1); return (1);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 13:01:19 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))) || (job_is_stopped(j) && !(j->attrs & JOB_NOTIFIED)))
{ {
ret = 1; ret = 1;
job_format(j, JOBS_OPTS_L); job_format(j, JOBS_OPT_L);
j->attrs |= JOB_NOTIFIED; j->attrs |= JOB_NOTIFIED;
job_remove(j->id); job_remove(j->id);
} }

View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* has_running_job.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* has_stopped_job.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/31 15:06:44 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/09 13:10:38 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]; int rank[2];
job_getrank(&rank); job_getrank(&rank);
if (j->id == rank[0]) crank = j->id == rank[0] ? '+' : ' ';
crank = '+'; crank = j->id == rank[1] ? '-' : crank;
else if (j->id == rank[1]) DG("raks [%i:%i]", rank[0], rank[1]);
crank = '-'; ft_printf("{mag}[%i]%c {eoc}", j->id, crank);
else
crank = ' ';
ft_printf("{mag}[%i] %c ", j->id, crank);
ft_printf("{eoc}");
} }

View file

@ -6,29 +6,45 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/09 12:38:31 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" #include "minishell.h"
void job_getrank(int (*rank)[2]) static int get_jobrank(t_list *jlist, int rank)
{ {
t_job *job; t_job *job;
t_jobc *jobc; t_list *head;
t_list *jlist; int ret;
int i;
i = 0; head = jlist;
jobc = &data_singleton()->jobc; ret = 0;
jlist = jobc->first_job; while (jlist && !ret)
(*rank)[0] = 0;
(*rank)[1] = 0;
while (jlist && i < 2)
{ {
job = jlist->content; job = jlist->content;
if (job_is_stopped(job)) if (job_is_stopped(job))
(*rank)[i++] = job->id; rank ? rank-- : (ret = job->id);
jlist = jlist->next; 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);
} }

View file

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

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 15:04:03 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; jobc = &data_singleton()->jobc;
job = ft_lst_find(jobc->first_job, &id, job_cmp_id)->content; job = ft_lst_find(jobc->first_job, &id, job_cmp_id)->content;
job_format(job, JOBS_OPTS_L); job_format(job, JOBS_OPT_L);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/09 16:54:18 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) void job_run(t_job *job, int foreground)
{ {
mark_job_as_running(job); mark_job_as_running(job);
job_format(job, JOBS_OPTS_L); job_format(job, JOBS_OPT_L);
if (foreground) if (foreground)
put_job_in_foreground(job, 1); put_job_in_foreground(job, 1);
else else

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/09 13:05:55 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; p = (*plist)->content;
if (!firstp) if (!firstp)
ft_printf(" "); ft_printf(" ");
if (opts & JOBS_OPTS_L) if (opts & JOBS_OPT_L)
ft_printf("%i ", p->pid); ft_printf("%i ", p->pid);
process_format_state(p); process_format_state(p);
if (opts & JOBS_OPTS_L) if (opts & JOBS_OPT_L)
process_format_com_long(plist); process_format_com_long(plist);
else else
process_format_com_short(plist, p->state); process_format_com_short(plist, p->state);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/07 18:07:50 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 14:45:40 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */