From 72b9e72fc870f5f7b4816edeeea5f531d701ab4a Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 15 Dec 2016 18:32:48 +0100 Subject: [PATCH] days work, still a lot to do --- 42sh/includes/builtin.h | 18 +++++---- 42sh/includes/exec.h | 2 +- 42sh/includes/job_control.h | 14 +++++-- 42sh/libft | 2 +- 42sh/src/builtin/builtin_cd.c | 5 ++- 42sh/src/builtin/builtin_exit.c | 2 +- 42sh/src/builtin/is_builtin.c | 3 +- 42sh/src/exec/ast_free.c | 6 +-- 42sh/src/exec/exec_ampersand.c | 2 +- 42sh/src/exec/exec_command.c | 14 ++++--- 42sh/src/exec/ft_exec.c | 2 +- 42sh/src/exec/launch_process.c | 7 +--- 42sh/src/exec/process_setexec.c | 5 ++- 42sh/src/job-control/builtin_jobs.c | 40 ++++++++++++++++++++ 42sh/src/job-control/check_chlds.c | 2 +- 42sh/src/job-control/do_job_notification.c | 40 ++++++++++++++++++++ 42sh/src/job-control/job_addprocess.c | 11 +++--- 42sh/src/job-control/job_is_stopped.c | 2 +- 42sh/src/job-control/job_notify_change.c | 6 ++- 42sh/src/job-control/job_notify_new.c | 14 ++++--- 42sh/src/job-control/job_remove.c | 28 ++++++++++++++ 42sh/src/job-control/job_update_id.c | 4 +- 42sh/src/job-control/job_update_status.c | 24 ++++++++++++ 42sh/src/job-control/job_wait.c | 25 +++++++++++- 42sh/src/job-control/process_mark_status.c | 12 +++--- 42sh/src/job-control/put_job_in_background.c | 2 +- 42sh/src/job-control/put_job_in_foreground.c | 5 ++- 42sh/src/job-control/sigchld_handler.c | 5 ++- 42sh/src/line-editing/ft_prompt.c | 4 +- 42sh/src/main/data_singleton.c | 2 +- 42sh/src/main/main.c | 9 ++--- 31 files changed, 245 insertions(+), 72 deletions(-) create mode 100644 42sh/src/job-control/builtin_jobs.c create mode 100644 42sh/src/job-control/do_job_notification.c create mode 100644 42sh/src/job-control/job_remove.c create mode 100644 42sh/src/job-control/job_update_status.c diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index 44e7f81c..c56fd5a4 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */ -/* Updated: 2016/12/13 17:58:12 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 17:49:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,11 +16,13 @@ # include "types.h" # include "libft.h" -t_execf *is_builtin(t_process *p); -int builtin_env(const char *path, char *const argv[], char *const envp[]); -int builtin_echo(const char *path, char *const argv[], char *const envp[]); -int builtin_cd(const char *path, char *const argv[], char *const envp[]); -int builtin_exit(const char *path, char *const argv[], char *const envp[]); -int builtin_setenv(const char *path, char *const argv[], char *const envp[]); -int builtin_unsetenv(const char *path, char *const argv[], char *const envp[]); +t_execf *is_builtin(t_process *p); +int builtin_env(const char *path, char *const argv[], char *const envp[]); +int builtin_echo(const char *path, char *const argv[], char *const envp[]); +int builtin_cd(const char *path, char *const argv[], char *const envp[]); +int builtin_exit(const char *path, char *const argv[], char *const envp[]); +int builtin_setenv(const char *path, char *const argv[], char *const envp[]); +int builtin_unsetenv(const char *path, char *const argv[], char *const envp[]); +int builtin_jobs(const char *path, char *const av[], char *const envp[]); + #endif diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index c4dca29a..92324800 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: 2016/12/13 17:50:40 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 17:24:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index b0ab7403..037f4f30 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: 2016/12/13 17:50:50 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 17:49:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,17 +55,25 @@ struct s_jobc t_process *job_getprocess(pid_t pid); int job_addprocess(t_process *p); void job_update_id(void); -void job_print_change(t_job *job, int status); void job_update_rank(void); + +void do_job_notification(void); void job_notify_new(t_job *job); +void job_notify_change(t_job *job, int status); + int job_wait(t_job *job); +void job_update_status(void); +int job_is_stopped(t_job *job); +int job_is_completed(t_job *job); +void job_remove(t_job *job); +void job_free(void *content, size_t content_size); +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); -void job_free(void *content, size_t content_size); int job_cmp_pid(t_job *job, pid_t *pid); int job_cmp_id(t_job *job, int *id); diff --git a/42sh/libft b/42sh/libft index c4890729..e7cece57 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit c4890729647c61fbe8f4fb627d0fcc098d224e54 +Subproject commit e7cece5732986cdcd159b40f1d927137e2d6c9f3 diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index 67544da9..96278410 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -6,11 +6,12 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */ -/* Updated: 2016/12/13 17:56:40 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 17:50:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "builtin.h" + #define CDOPT_L 0x001 #define CDOPT_P 0x002 #define HAS_CDOPT_P(x) (x & CD_OPT_P) diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index e0e250fe..af3a7799 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: 2016/12/13 17:59:05 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 18:00:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index e6796bc9..3438b909 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ -/* Updated: 2016/12/13 17:31:18 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 17:48:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ t_stof g_builtin[] = { {"unsetenv", &builtin_unsetenv}, {"env", &builtin_env}, {"exit", &builtin_exit}, + {"jobs", &builtin_jobs}, {NULL, NULL}, }; diff --git a/42sh/src/exec/ast_free.c b/42sh/src/exec/ast_free.c index db9ce943..3a8b03a6 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: 2016/12/13 17:17:38 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 13:28:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,17 +18,13 @@ void ast_free(void *data, size_t content_size) (void)content_size; node = data; - DG("gonna free a node"); if (node->type == TK_COMMAND) { - DG("ast_free TK_COMMAND"); if (node->data.sstr) ft_sstrfree(node->data.sstr); } else if (node->type == TK_LESS || node->type == TK_GREAT || node->type == TK_DGREAT) { - DG("ast_free TK_REDIR %p", node->data.redir.word.word); ft_strdel(&node->data.redir.word.word); } - DG("ast_free done"); } diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c index 3a93b16c..395785dd 100644 --- a/42sh/src/exec/exec_ampersand.c +++ b/42sh/src/exec/exec_ampersand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */ -/* Updated: 2016/12/13 17:19:19 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 13:40:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index b1ea3584..c5e9198e 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: 2016/12/13 17:41:09 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 18:31:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,10 +22,15 @@ int exec_command(t_btree **ast) p = &data_singleton()->exec.process; job = &data_singleton()->exec.job; p->argv = ft_sstrdup(node->data.sstr); - DG("gonna launch_process"); - DG("job attr=%i", job->attributes); - process_setexec(p); + if (process_setexec(p)) + { + ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->argv[0]); + btree_delone(ast, &ast_free); + return (0); + } + DG("gonna launch_process now"); launch_process(p); + job_addprocess(p); if (p->fdout == STDOUT) { if (JOB_IS_FG(job->attributes)) @@ -33,7 +38,6 @@ int exec_command(t_btree **ast) else put_job_in_background(job, 0); } - job_addprocess(p); btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index dcfc2dd2..7559ed7d 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2016/12/13 17:40:43 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 15:18:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index ecfc451d..e87fc724 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: 2016/12/13 17:50:38 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 15:21:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,11 +42,6 @@ int launch_process(t_process *p) p->pid = pid; else if (pid == -1) perror("fork"); - if (p->fdout == STDOUT) - { - waitpid(pid, &p->status, 0); - set_exitstatus(p->status); - } } return (0); } diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index 8148510c..c3b81dc3 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: 2016/12/13 17:50:26 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 15:19:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,9 +35,10 @@ int process_setexec(t_process *p) } else { - DG("process is unknown type"); + DG("process is '%s' unknown type", p->argv[0]); p->execf = NULL; p->attributes &= PROCESS_UNKNOWN; + return (1); } return (0); } diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c new file mode 100644 index 00000000..a9d9118e --- /dev/null +++ b/42sh/src/job-control/builtin_jobs.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_jobs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ +/* Updated: 2016/12/15 17:54:01 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; + char rank; + + jobc = &data_singleton()->jobc; + jlist = jobc->first_job; + (void)path; + (void)envp; + (void)av; + while (jlist) + { + job = jlist->content; + rank = ' '; + if (job->id == data_singleton()->jobc.rank[0]) + rank = '+'; + else if (job->id == data_singleton()->jobc.rank[1]) + rank = '-'; + ft_printf("{mag}[%i] %c ", job->id, rank); + ft_printf("attributes=%x{eoc}\n", job->attributes); + jlist = jlist->next; + } + return (0); +} diff --git a/42sh/src/job-control/check_chlds.c b/42sh/src/job-control/check_chlds.c index cadacf61..b4e02bc7 100644 --- a/42sh/src/job-control/check_chlds.c +++ b/42sh/src/job-control/check_chlds.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:28:40 by jhalford #+# #+# */ -/* Updated: 2016/12/12 17:23:14 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 17:24:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/do_job_notification.c b/42sh/src/job-control/do_job_notification.c new file mode 100644 index 00000000..b1615415 --- /dev/null +++ b/42sh/src/job-control/do_job_notification.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* do_job_notification.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/15 13:01:19 by jhalford #+# #+# */ +/* Updated: 2016/12/15 17:49:37 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void do_job_notification(void) +{ + t_job *j; + t_list *jlist; + t_jobc *jobc; + + job_update_status(); + jobc = &data_singleton()->jobc; + jlist = jobc->first_job; + while (jlist) + { + j = jlist->content; + DG("checking job [%i]", j->id); + if (job_is_completed(j)) + { + job_notify_change(j, 0); + job_remove(j); + } + else if (job_is_stopped(j) && !(j->attributes & JOB_NOTIFIED)) + { + job_notify_change(j, 8); + j->attributes &= JOB_NOTIFIED; + } + jlist = jlist->next; + } +} diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index c244f2bc..d18a5e20 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/job_addprocess.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:54:51 by jhalford #+# #+# */ -/* Updated: 2016/12/13 17:06:57 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 18:31:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,17 +19,18 @@ int job_addprocess(t_process *p) jobc = &data_singleton()->jobc; job = &data_singleton()->exec.job; - if (JOB_IS_BG(job->attributes) && p->fdin == STDIN) + if (p->fdin == STDIN) { - job_update_id(); job->id = jobc->current_id; + job_update_id(); ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); jobc->rank[1] = jobc->rank[0]; jobc->rank[0] = job->id; } job = jobc->first_job->content; + ft_lstadd(&job->first_process, ft_lstnew(p, sizeof(*p))); if (JOB_IS_BG(job->attributes) && p->fdout == STDOUT) job_notify_new(job); - ft_lstadd(&job->first_process, ft_lstnew(p, sizeof(*p))); - return (0); + DG("adding process to first_job : %i", p->pid); + return(0); } diff --git a/42sh/src/job-control/job_is_stopped.c b/42sh/src/job-control/job_is_stopped.c index 8dd89f34..d6248297 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: 2016/12/13 15:24:26 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 12:42:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_notify_change.c b/42sh/src/job-control/job_notify_change.c index 028461c2..6ca222b3 100644 --- a/42sh/src/job-control/job_notify_change.c +++ b/42sh/src/job-control/job_notify_change.c @@ -6,11 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 15:04:03 by jhalford #+# #+# */ -/* Updated: 2016/12/12 16:39:31 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 17:45:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "job_control.h" void job_notify_change(t_job *job, int status) { @@ -24,6 +24,8 @@ void job_notify_change(t_job *job, int status) ft_printf("{mag}[%i] %c ", job->id, rank); if (status == 0) ft_printf("{gre}done{mag}"); + else if (status == 8) + ft_printf("{red}stopped{mag}"); else if (status == 9) ft_printf("{red}killed{mag}"); else diff --git a/42sh/src/job-control/job_notify_new.c b/42sh/src/job-control/job_notify_new.c index 0f5075b8..1b39f0ea 100644 --- a/42sh/src/job-control/job_notify_new.c +++ b/42sh/src/job-control/job_notify_new.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:27:01 by jhalford #+# #+# */ -/* Updated: 2016/12/13 14:58:23 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 17:15:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,14 +14,16 @@ void job_notify_new(t_job *job) { - t_list *process; + t_list *plist; + t_process *p; ft_printf("{mag}[%i]", job->id); - process = job->first_process; - while (process) + plist = job->first_process; + while (plist) { - ft_printf(" %i", ((t_process*)process->content)->pid); - process=process->next; + p = plist->content; + ft_printf(" %i", p->pid); + plist = plist->next; } ft_printf("{eoc}\n"); } diff --git a/42sh/src/job-control/job_remove.c b/42sh/src/job-control/job_remove.c new file mode 100644 index 00000000..3d93d244 --- /dev/null +++ b/42sh/src/job-control/job_remove.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_remove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/15 12:51:08 by jhalford #+# #+# */ +/* Updated: 2016/12/15 17:58:48 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void job_remove(t_job *job) +{ + t_jobc *jobc; + + jobc = &data_singleton()->jobc; + if (job->id < data_singleton()->jobc.current_id) + { + data_singleton()->jobc.current_id = job->id; + DG("ID_UPDATE(downgrade):%i", job->id); + } + else + DG("ID_UPDATE(no downgrade): %i/%i", job->id, data_singleton()->jobc.current_id); + ft_lst_delif(&jobc->first_job, job, ft_addrcmp, job_free); +} diff --git a/42sh/src/job-control/job_update_id.c b/42sh/src/job-control/job_update_id.c index 68579844..361f8f14 100644 --- a/42sh/src/job-control/job_update_id.c +++ b/42sh/src/job-control/job_update_id.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 13:33:08 by jhalford #+# #+# */ -/* Updated: 2016/12/12 17:27:59 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 17:15:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,6 @@ void job_update_id(void) while (ft_lst_find(start, id, job_cmp_id)) { *id += 1; - DG("id = %i", *id); + DG("ID_UPDATE:%i", *id); } } diff --git a/42sh/src/job-control/job_update_status.c b/42sh/src/job-control/job_update_status.c new file mode 100644 index 00000000..8f91c620 --- /dev/null +++ b/42sh/src/job-control/job_update_status.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_update_status.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/15 12:56:11 by jhalford #+# #+# */ +/* Updated: 2016/12/15 15:09:05 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void job_update_status(void) +{ + int status; + pid_t pid; + + DG("updating job status'"); + pid = waitpid (WAIT_ANY, &status, WUNTRACED|WNOHANG); + while (!process_mark_status(pid, status)) + pid = waitpid (WAIT_ANY, &status, WUNTRACED|WNOHANG); +} diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c index 4c56f65a..8cc0483b 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -1,7 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_wait.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/15 11:49:05 by jhalford #+# #+# */ +/* Updated: 2016/12/15 17:40:00 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "job_control.h" int job_wait(t_job *job) { - (void)job; - return (0); + pid_t pid; + int status; + + pid = waitpid(WAIT_ANY, &status, WUNTRACED); + while (!(process_mark_status(pid, status) + || job_is_stopped(job) + || job_is_completed(job))) + { + pid = waitpid(WAIT_ANY, &status, WUNTRACED); + } + return(0); } diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/job-control/process_mark_status.c index 6ddf8bdd..12199d06 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: 2016/12/12 13:02:05 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 15:12:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,13 +15,13 @@ int process_mark_status(pid_t pid, int status) { t_process *p; - t_jobc *jobc; - jobc = &data_singleton()->jobc; + DG("marking: pid=%i, status=%i", pid, status); if (pid > 0) { if ((p = job_getprocess(pid))) { + DG("found process pid=%i", pid); p->status = status; if (WIFSTOPPED(status)) p->attributes &= PROCESS_STOPPED; @@ -30,13 +30,15 @@ int process_mark_status(pid_t pid, int status) p->attributes &= PROCESS_COMPLETED; if (WIFSIGNALED(status)) ft_printf("%d: Terminated by signal %d.\n", - (int) pid, WTERMSIG (p->status)); + (int) pid, WTERMSIG(p->status)); } return(0); } - ft_dprintf(STDERR, "No child process %d.\n", pid); + ft_dprintf(2, "No child process %d.\n", pid); return(-1); } else + { return(-1); + } } diff --git a/42sh/src/job-control/put_job_in_background.c b/42sh/src/job-control/put_job_in_background.c index 570a1145..38542ffc 100644 --- a/42sh/src/job-control/put_job_in_background.c +++ b/42sh/src/job-control/put_job_in_background.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 15:03:29 by jhalford #+# #+# */ -/* Updated: 2016/12/13 15:04:12 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 17:53:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index ab2e3439..108999e9 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: 2016/12/13 15:06:21 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 17:58:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,10 @@ int put_job_in_foreground(t_job *job, int cont) perror("kill (SIGCONT)"); } /* Wait for it to report. */ + DG("gonna wait for job"); job_wait(job); + DG("gonna remove job"); + job_remove(job); /* Put the shell back in the foreground. */ tcsetpgrp(STDIN_FILENO, jobc->shell_pgid); diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c index 962cb46a..e011ebd2 100644 --- a/42sh/src/job-control/sigchld_handler.c +++ b/42sh/src/job-control/sigchld_handler.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 17:37:56 by jhalford #+# #+# */ -/* Updated: 2016/12/12 16:49:07 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 15:06:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,5 +18,6 @@ void sigchld_handler(int signo) (void)signo; data = data_singleton(); - DG("got asynchronous notification (SIGCHLD)"); + DG("got asynchronous notification (SIGCHLD)"); + job_update_status(); } diff --git a/42sh/src/line-editing/ft_prompt.c b/42sh/src/line-editing/ft_prompt.c index 764eed26..8c7a1338 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: 2016/12/12 16:49:05 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 15:07:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ int ft_prompt(void) t_data *data; data = data_singleton(); + do_job_notification(); + DG("new prompt now"); ft_putstr(SHELL_PROMPT); return (0); } diff --git a/42sh/src/main/data_singleton.c b/42sh/src/main/data_singleton.c index b5f87bbd..d43d0bdf 100644 --- a/42sh/src/main/data_singleton.c +++ b/42sh/src/main/data_singleton.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 11:19:51 by jhalford #+# #+# */ -/* Updated: 2016/12/13 11:52:47 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 15:41:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index f1251d86..5cb63fcf 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2016/12/13 12:05:51 by jhalford ### ########.fr */ +/* Updated: 2016/12/15 17:03:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,15 +33,14 @@ int main(void) return (1); if (!token) continue ; - token_print(token); + /* token_print(token); */ if (ft_parse(&ast, &token)) return (1); - btree_print(STDERR, ast, &ft_putast); - /* ft_dprintf(STDERR, "\n--- INFIX BREAKDOWN ---\n"); */ + btree_print(STDBUG, ast, &ft_putast); + /* ft_dprintf(STDBUG, "\n--- INFIX BREAKDOWN ---\n"); */ /* btree_apply_infix(ast, &ft_putast2); */ if (ft_exec(&ast)) return (1); - DG("end of loop"); } return (0); }