diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index a9a3d333..a9d62d78 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -29,6 +29,8 @@ extern t_data *g_data; void job_new(t_data *data, char **av, pid_t pid); void job_announce(t_job *job); +int ft_cmppid(t_job *job, pid_t *pid); + void sigchld_handler(int signo); void sigint_handler(int signo); void sigtstp_handler(int signo); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 2f9aaae6..0dafe98b 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -76,6 +76,8 @@ typedef enum e_qstate t_qstate; extern t_stof g_builtins[]; extern pid_t g_pid; +t_data *data_singleton(); + int data_init(t_data *data); void data_exit(t_data *data); void ft_cleanup(void); diff --git a/42sh/libft b/42sh/libft index 42e1a190..5fab2c76 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 42e1a190bd86ea288ee9a367fefeac8284a67cf4 +Subproject commit 5fab2c76b033729a3b16a15f2b5ac63f2deaafc4 diff --git a/42sh/script.sh b/42sh/script.sh index 1a566882..8fb9fff0 100755 --- a/42sh/script.sh +++ b/42sh/script.sh @@ -1,4 +1,5 @@ #!/bin/sh while [ 1 ]; do sleep 1 + echo "a" done diff --git a/42sh/src/job-control/ft_cmppid.c b/42sh/src/job-control/ft_cmppid.c new file mode 100644 index 00000000..117db889 --- /dev/null +++ b/42sh/src/job-control/ft_cmppid.c @@ -0,0 +1,6 @@ +#include "minishell.h" + +int ft_cmppid(t_job *job, pid_t *pid) +{ + return (job->pid - *pid); +} diff --git a/42sh/src/job-control/job_new.c b/42sh/src/job-control/job_new.c index 28142202..6f6e8b72 100644 --- a/42sh/src/job-control/job_new.c +++ b/42sh/src/job-control/job_new.c @@ -18,6 +18,7 @@ void job_new(t_data *data, char **av, pid_t pid) DG("got new job"); job.command = ft_sstrcat(av, ' '); + DG("job command '%s'", job.command); job.pid = pid; job.id = 42; ft_lstadd(&data->jobc.list, ft_lstnew(&job, sizeof(job))); diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c index 5df1e708..81d260c2 100644 --- a/42sh/src/job-control/sigchld_handler.c +++ b/42sh/src/job-control/sigchld_handler.c @@ -12,20 +12,31 @@ #include "minishell.h" -t_data *g_data; - void sigchld_handler(int signo) { + int status; + pid_t pid; t_job *job; + t_list *start; t_list *list; (void)signo; DG("got SIGCHLD"); - list = g_data->jobc.list; + start = data_singleton()->jobc.list; + pid = waitpid(-1, &status, WNOHANG); + DG("SIGCHLD pid=%i", pid); + /* start = NULL; */ + list = start ? ft_lst_find(start, &pid, ft_cmppid) : NULL; if (list) + { job = list->content; + if (status == 0) + ft_printf("[%i] + done\t%s\n", job->id, job->command); + else + ft_printf("[%i] + exit %i\t%s\n", job->id, status, job->command); + ft_prompt(); + } else - job = NULL; - if (job) - DG("job pid=%i", job->pid); + DG("SIGCHLD but no find"); + } diff --git a/42sh/src/main/data_singleton.c b/42sh/src/main/data_singleton.c new file mode 100644 index 00000000..c6139721 --- /dev/null +++ b/42sh/src/main/data_singleton.c @@ -0,0 +1,10 @@ +#include "minishell.h" + +t_data *data_singleton() +{ + static t_data *data = NULL; + + if (data == NULL) + data = malloc(sizeof(t_data)); + return (data); +} diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 28044585..9ecce134 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -12,27 +12,26 @@ #include "minishell.h" -t_data *g_data; - int main(void) { - t_data data; t_list *token; t_btree *ast; + /* t_data data; */ + t_data *data; token = NULL; ast = NULL; - g_data = &data; - if (data_init(&data)) + data = data_singleton(); + if (data_init(data)) return (1); DG("{inv}{bol}{gre}start of shell"); while (1) { - if (ft_interactive_sh(&data)) + if (ft_interactive_sh(data)) return (1); - DG("{inv}{mag}got command '%s'", data.line.input); + DG("{inv}{mag}got command '%s'", data->line.input); token = NULL; - if (ft_tokenize(&token, data.line.input, DEFAULT)) + if (ft_tokenize(&token, data->line.input, DEFAULT)) return (1); if (!token) continue ; @@ -42,7 +41,7 @@ int main(void) btree_print(STDERR, ast, &ft_putast); /* ft_dprintf(STDERR, "\n--- INFIX BREAKDOWN ---\n"); */ /* btree_apply_infix(ast, &ft_putast2); */ - if (ft_exec(&ast, &data)) + if (ft_exec(&ast, data)) return (1); } return (0);