diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 09861590..5c01cda8 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/08 16:10:11 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 15:57:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,18 @@ # define PIPE_READ 0 # define PIPE_WRITE 1 -# define PROCESS_COMPLETED (1 << 0) -# define PROCESS_STOPPED (1 << 1) -# define PROCESS_BUILTIN (1 << 2) -# define PROCESS_BINARY (1 << 3) -# define PROCESS_SCRIPT (1 << 4) -# define PROCESS_UNKNOWN (1 << 5) -# define PROCESS_PIPESTART (1 << 6) -# define PROCESS_PIPEEND (1 << 7) +# define PROCESS_BUILTIN (1 << 0) +# define PROCESS_BINARY (1 << 1) +# define PROCESS_SCRIPT (1 << 2) +# define PROCESS_UNKNOWN (1 << 3) +# define PROCESS_PIPESTART (1 << 4) +# define PROCESS_PIPEEND (1 << 5) +# define PROCESS_COMPLETED (1 << 6) +# define PROCESS_SUSPENDED (1 << 7) +# define PROCESS_RUNNING (1 << 8) + +# define PROCESS_TYPE_MASK (1 << 0 | 1 << 1 | 1 << 2 | 1 << 3) +# define PROCESS_STATE_MASK (1 << 6 | 1 << 7 | 1 << 8) # define IS_PIPESTART(a) (a & PROCESS_PIPESTART) # define IS_PIPEEND(a) (a & PROCESS_PIPEEND) diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 513ceff3..b7147f44 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/01/08 16:10:10 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 14:05:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,8 @@ # define JOB_IS_BG(j) (j & JOB_BG) # define JOB_IS_FG(j) !(j & JOB_BG) +#define JOBS_OPTS_L (1 << 0) + struct s_job { int id; @@ -51,33 +53,36 @@ void job_update_rank(void); int do_job_notification(void); void job_notify_new(t_job *job); void job_notify_change(int id, int status); +void job_format(t_job *j, int rank[2], int opts); +void job_format_head(t_job *j, int rank[2]); -int job_wait(int id); void job_update_status(void); +void mark_job_as_running (t_job *j); +int process_mark_status(pid_t pid, int status); int job_is_stopped(int id); int job_is_completed(int id); + +void job_new(char **av, pid_t pid); +int job_wait(int id); void job_remove(int id); void job_free(void *content, size_t content_size); void job_kill_all(void); -void mark_job_as_running (t_job *j); -int process_mark_status(pid_t pid, int status); int put_job_in_foreground(t_job *job, int cont); int put_job_in_background(t_job *job, int cont); -void job_new(char **av, pid_t pid); -int job_cmp_pid(t_job *job, pid_t *pid); -int job_cmp_id(t_job *job, int *id); +int job_cmp_pid(t_job *job, pid_t *pid); +int job_cmp_id(t_job *job, int *id); +void job_getrank(int (*rank)[2]); -int check_chlds(void); +void sigchld_handler(int signo); +void sigint_handler(int signo); +void sigtstp_handler(int signo); +void sigttin_handler(int signo); +void sigttou_handler(int signo); -void sigchld_handler(int signo); -void sigint_handler(int signo); -void sigtstp_handler(int signo); -void sigttin_handler(int signo); -void sigttou_handler(int signo); - -int process_cmp_pid(t_process *p, pid_t *pid); +int process_cmp_pid(t_process *p, pid_t *pid); +void process_format(t_list **p, int firstp, int opts); #endif diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index e1bfea6f..2d646bbf 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/01/08 16:10:25 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 12:14:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/types.h b/42sh/includes/types.h index d3a6fed0..7fa21e75 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: 2016/12/13 17:51:11 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 13:58:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft b/42sh/libft index f4af2f64..eded1f1d 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit f4af2f642761111b3395bb69c1766a8c46719d4d +Subproject commit eded1f1d189a6fd631b705f9d4a466fe088b94b3 diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 916949e7..98f92890 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2016/12/07 16:29:11 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 15:53:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,6 @@ int builtin_setenv(const char *path, char *const av[], char *const envp[]) (void)path; i = 0; env = &data_singleton()->env; - DG("doing setenv now"); if (ft_strcmp(av[0], "setenv") == 0) av++; if (!av[0]) @@ -41,7 +40,6 @@ int builtin_setenv(const char *path, char *const av[], char *const envp[]) { ft_strdel(&(*env)[i]); (*env)[i] = str; - DG("done setenv"); return (0); } i++; @@ -49,6 +47,5 @@ int builtin_setenv(const char *path, char *const av[], char *const envp[]) *env = ft_sstradd(*env, str); ft_strdel(&str); } - DG("done setenv"); return (0); } diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 5998f3b4..fcb2e0ab 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/08 15:24:20 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 12:20:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index f9af2ebe..2907c020 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */ -/* Updated: 2017/01/08 11:03:05 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 15:56:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,8 +30,7 @@ int exec_pipe(t_btree **ast) ft_exec(&(*ast)->left); p->attributes &= ~PROCESS_PIPESTART; - DG("p->fdout=%i", p->fdout); - close(p->fdout); + close(fds[PIPE_WRITE]); p->fdout = STDOUT; p->fdin = fds[PIPE_READ]; diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 6bafdc78..b07ede0f 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/08 15:23:37 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:00:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,9 @@ int launch_process(t_process *p) set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env)); else { - DG("process is to be forked, fdout=%i, attr=%b", p->fdout, p->attributes); + DG("process is to be forked, %i->[]->%i, attr=%b", p->fdin, p->fdout, p->attributes); + p->attributes &= ~PROCESS_STATE_MASK; + p->attributes |= PROCESS_RUNNING; if (p->attributes & (PROCESS_BINARY | PROCESS_SCRIPT) && access(p->path, X_OK) == -1) { diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index 04c70cfc..91357682 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: 2016/12/13 17:49:09 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 15:35:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,13 +16,13 @@ int process_redirect(t_process *p) { if (p->fdin != STDIN) { - DG("dup2 %i->%i", p->fdin, STDIN); + DG("redirect STDIN to %i", p->fdin); dup2(p->fdin, STDIN); close(p->fdin); } if (p->fdout != STDOUT) { - DG("dup2 %i->%i", p->fdout, STDOUT); + DG("redirect STDOUT to %i", p->fdout); dup2(p->fdout, STDOUT); close(p->fdout); } diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index 0208eec8..dac9102f 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */ -/* Updated: 2017/01/02 19:11:00 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 15:49:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,15 +14,12 @@ int process_setexec(t_process *p) { - DG("process_setexec, attr=%b", p->attributes); if ((p->execf = is_builtin(p))) { - DG("process is builtin"); p->attributes |= PROCESS_BUILTIN; } else if (ft_strchr(p->av[0], '/')) { - DG("process is a script"); p->execf = &execve; p->attributes |= PROCESS_SCRIPT; p->path = ft_strdup(p->av[0]); @@ -30,13 +27,11 @@ int process_setexec(t_process *p) else if ((p->path = ft_findexec(ft_getenv( data_singleton()->env, "PATH"), p->av[0]))) { - DG("process is binary"); p->execf = &execve; p->attributes |= PROCESS_BINARY; } else { - DG("process is '%s' unknown type", p->av[0]); p->execf = NULL; p->attributes |= PROCESS_UNKNOWN; return (1); diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index 9a947b2f..4dcdeda3 100644 --- a/42sh/src/exec/process_setgroup.c +++ b/42sh/src/exec/process_setgroup.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:48:10 by jhalford #+# #+# */ -/* Updated: 2017/01/08 15:23:36 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 15:58:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,9 +23,8 @@ int process_setgroup(t_process *p) pid = getpid(); if (job->pgid == 0) job->pgid = pid; - DG("job->pgid=%i", job->pgid); if (setpgid(pid, job->pgid)) - ft_dprintf(2, "{red}setpgid failed{eoc}\n"); + DG("setpgid(%i, %i) failed", pid, job->pgid); if (JOB_IS_FG(job->attributes)) { signal(SIGTTOU, SIG_IGN); diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c index b0c45498..032e0a47 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -6,59 +6,38 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/01/08 15:33:32 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 14:05:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" + int builtin_jobs(const char *path, char *const av[], char *const envp[]) { t_jobc *jobc; t_list *jlist; - t_job *job; - t_list *plist; - t_process *p; - char rank; - int lg; - int firstp; + t_list *tmplist; + int rank[2]; + int opts; - jobc = &data_singleton()->jobc; - jlist = jobc->first_job; (void)path; (void)envp; (void)av; - lg = 0; + jobc = &data_singleton()->jobc; + jlist = jobc->first_job; + job_getrank(&rank); + opts = 0; if (ft_strcmp(av[1], "-l") == 0) - lg = 1; - rank = '+'; + opts |= JOBS_OPTS_L; + tmplist = ft_lst_filter(jlist, NULL, NULL); + ft_lstsort(&tmplist, job_cmp_id); + jlist = tmplist; while (jlist) { - firstp = 1; - job = jlist->content; - ft_printf("{mag}[%i] %c ", job->id, rank); - ft_printf("attr=%#b ", job->attributes); - plist = job->first_process; - while (plist) - { - p = plist->content; - if (lg) - { - if (!firstp) - ft_printf("\n "); - ft_printf("%i ", p->pid); - } - else - ft_putchar(' '); - ft_sstrprint(p->av, ' '); - if (plist->next) - ft_printf(" |"); - plist = plist->next; - firstp = 0; - } + job_format(jlist->content, rank, opts); jlist = jlist->next; - rank = (rank == '+') ? '-' : ' '; - ft_printf("{eoc}\n"); } + ft_lstdel(&tmplist, ft_lst_cfree); return (0); } diff --git a/42sh/src/job-control/job_cmp_id.c b/42sh/src/job-control/job_cmp_id.c index 3424cf13..50d6ada8 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: 2016/12/12 13:44:23 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 12:34:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_format.c b/42sh/src/job-control/job_format.c new file mode 100644 index 00000000..58ee1a10 --- /dev/null +++ b/42sh/src/job-control/job_format.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_format.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/09 12:47:17 by jhalford #+# #+# */ +/* Updated: 2017/01/09 14:05:06 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void job_format(t_job *j, int rank[2], int opts) +{ + t_list *plist; + int firstp; + + job_format_head(j, rank); + plist = j->first_process; + firstp = 1; + while (plist) + { + process_format(&plist, firstp, opts); + firstp = 0; + } +} diff --git a/42sh/src/job-control/job_format_head.c b/42sh/src/job-control/job_format_head.c new file mode 100644 index 00000000..0dfcaaf9 --- /dev/null +++ b/42sh/src/job-control/job_format_head.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_format_head.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/09 13:10:38 by jhalford #+# #+# */ +/* Updated: 2017/01/09 13:53:48 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void job_format_head(t_job *j, int rank[2]) +{ + char crank; + + 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}"); +} diff --git a/42sh/src/job-control/job_free.c b/42sh/src/job-control/job_free.c index 1138314e..db63e7f2 100644 --- a/42sh/src/job-control/job_free.c +++ b/42sh/src/job-control/job_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2016/12/12 13:02:05 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 13:22:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_getrank.c b/42sh/src/job-control/job_getrank.c new file mode 100644 index 00000000..e24f3e03 --- /dev/null +++ b/42sh/src/job-control/job_getrank.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_getrank.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/09 12:38:31 by jhalford #+# #+# */ +/* Updated: 2017/01/09 12:47:08 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void job_getrank(int (*rank)[2]) +{ + t_job *job; + t_jobc *jobc; + t_list *jlist; + + jobc = &data_singleton()->jobc; + jlist = jobc->first_job; + (*rank)[0] = 0; + (*rank)[1] = 0; + if (jlist) + { + job = jlist->content; + (*rank)[0] = job->id; + jlist = jlist->next; + if (jlist) + { + job = jlist->content; + (*rank)[1] = job->id; + } + } +} diff --git a/42sh/src/job-control/job_is_completed.c b/42sh/src/job-control/job_is_completed.c index 4fba4555..a2a84610 100644 --- a/42sh/src/job-control/job_is_completed.c +++ b/42sh/src/job-control/job_is_completed.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 15:10:20 by jhalford #+# #+# */ -/* Updated: 2017/01/08 14:04:16 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 14:03:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_is_stopped.c b/42sh/src/job-control/job_is_stopped.c index e3bddaa1..135988d6 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/08 14:39:35 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 14:02:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ int job_is_stopped(int id) while (lst) { p = lst->content; - if (!(p->attributes & (PROCESS_COMPLETED | PROCESS_STOPPED))) + if (!(p->attributes & (PROCESS_COMPLETED | PROCESS_SUSPENDED))) return (0); lst = lst->next; } diff --git a/42sh/src/job-control/job_notify_change.c b/42sh/src/job-control/job_notify_change.c index a86561a2..8a99d535 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/08 13:52:53 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 12:39:33 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 4a1c679e..3938cd9f 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/08 14:53:34 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 14:03:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,8 @@ void mark_job_as_running (t_job *j) while (plist) { p = plist->content; - p->attributes &= ~PROCESS_STOPPED; + p->attributes &= ~PROCESS_STATE_MASK; + p->attributes |= PROCESS_RUNNING; 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 new file mode 100644 index 00000000..db30bcaf --- /dev/null +++ b/42sh/src/job-control/process_format.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* process_format.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/09 13:05:55 by jhalford #+# #+# */ +/* Updated: 2017/01/09 14:19:50 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void process_format(t_list **plist, int firstp, int opts) +{ + t_process *p; + int state; + + p = (*plist)->content; + if (!firstp) + 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 "); + else if (state == PROCESS_SUSPENDED) + ft_putstr("suspended"); + else if (state == PROCESS_COMPLETED) + { + if (p->status == 0) + ft_putstr("done "); + else + ft_printf("exit %i ", p->status); + } + ft_putchar('\t'); + if (opts & JOBS_OPTS_L) + { + ft_sstrprint(p->av, ' '); + if ((*plist)->next) + ft_putstr(" |"); + (*plist) = (*plist)->next; + } + else + { + while (*plist) + { + p = (*plist)->content; + if (!(p->attributes & state) || + (state == PROCESS_COMPLETED && p->status != 0)) + break; + ft_sstrprint(p->av, ' '); + if ((*plist)->next) + ft_putstr(" |"); + (*plist) = (*plist)->next; + } + } + ft_putchar('\n'); +} diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/job-control/process_mark_status.c index 1ea54e4b..429d1c44 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/08 14:55:52 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 14:04:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,11 +24,13 @@ int process_mark_status(pid_t pid, int status) if (WIFSTOPPED(status)) { DG("marking: pid=%i, status=%i (stopped)", pid, status); - p->attributes |= PROCESS_STOPPED; + p->attributes &= ~PROCESS_STATE_MASK; + p->attributes |= PROCESS_SUSPENDED; } else { DG("marking: pid=%i, status=%i (completed)", pid, status); + p->attributes &= ~PROCESS_STATE_MASK; p->attributes |= PROCESS_COMPLETED; if (WIFSIGNALED(status)) ft_printf("{mag}%d: Terminated by signal %d.\n{eoc}", diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index ede7df10..9272aa08 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/08 15:33:03 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 12:29:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,16 +17,16 @@ int put_job_in_foreground(t_job *job, int cont) t_jobc *jobc; jobc = &data_singleton()->jobc; - /* Put the job into the foreground. */ - signal(SIGTTOU, SIG_IGN); - if (tcsetpgrp(STDIN, job->pgid) == -1) - DG("couldn't put process in control. errno=%i, pgid=%i", errno, job->pgid); - else - DG("pgid %i is now in control.", job->pgid); - signal(SIGTTOU, sigttou_handler); /* Send the job a continue signal, if necessary. */ if (cont) { + /* Put the job into the foreground. */ + signal(SIGTTOU, SIG_IGN); + if (tcsetpgrp(STDIN, job->pgid) == -1) + DG("couldn't put process in control. errno=%i, pgid=%i", errno, job->pgid); + else + DG("pgid %i is now in control.", job->pgid); + signal(SIGTTOU, sigttou_handler); tcsetattr (STDIN, TCSANOW, &job->tmodes); if (kill(-job->pgid, SIGCONT) < 0) perror("kill (SIGCONT)"); @@ -35,6 +35,7 @@ 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/line-editing/ft_interactive_sh.c b/42sh/src/line-editing/ft_interactive_sh.c index 1c6b4465..62654c96 100644 --- a/42sh/src/line-editing/ft_interactive_sh.c +++ b/42sh/src/line-editing/ft_interactive_sh.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:14:12 by jhalford #+# #+# */ -/* Updated: 2016/12/12 17:42:04 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 14:28:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_prompt.c b/42sh/src/line-editing/ft_prompt.c index 5f37d810..a9e1162f 100644 --- a/42sh/src/line-editing/ft_prompt.c +++ b/42sh/src/line-editing/ft_prompt.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 14:22:34 by jhalford #+# #+# */ -/* Updated: 2017/01/08 16:00:49 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 14:28:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_set_termios.c b/42sh/src/line-editing/ft_set_termios.c index b0277f51..ea21de84 100644 --- a/42sh/src/line-editing/ft_set_termios.c +++ b/42sh/src/line-editing/ft_set_termios.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:14:09 by jhalford #+# #+# */ -/* Updated: 2017/01/08 14:26:55 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 15:44:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,12 @@ int ft_set_termios(t_data *data, int input_mode) struct termios term; (void)data; + /* term = data_singleton()->jobc.shell_tmodes; */ if (tcgetattr(0, &term) == -1) + { + DG("tcgetattr failed, errno=%i", errno); return (-1); + } if (input_mode) term.c_lflag &= ~(ICANON) & ~(ISIG) & ~(ECHO); else diff --git a/42sh/src/line-editing/input_init.c b/42sh/src/line-editing/input_init.c index 6ef69843..b76ce92e 100644 --- a/42sh/src/line-editing/input_init.c +++ b/42sh/src/line-editing/input_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 13:35:03 by jhalford #+# #+# */ -/* Updated: 2017/01/08 14:27:01 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 15:35:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/sigint_handler.c b/42sh/src/line-editing/sigint_handler.c index 7d45df67..0c2dad9c 100644 --- a/42sh/src/line-editing/sigint_handler.c +++ b/42sh/src/line-editing/sigint_handler.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 15:14:47 by jhalford #+# #+# */ -/* Updated: 2017/01/08 15:12:58 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 15:59:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 70a149ef..f33b8a0d 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ -/* Updated: 2017/01/08 15:11:34 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 15:36:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */