From 6cfac2eaffd2b378342cc44ca950e96cd4862e48 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 12 Dec 2016 18:13:29 +0100 Subject: [PATCH] job control advances, still have to do ctrl-z, fg, bg, jobs --- 42sh/includes/exec.h | 49 +++++++++++++------- 42sh/includes/job_control.h | 28 +++++++++-- 42sh/includes/line_editing.h | 2 +- 42sh/includes/minishell.h | 33 ++++++------- 42sh/libft | 2 +- 42sh/src/builtin/builtin.c | 10 ++-- 42sh/src/builtin/builtin_env.c | 4 +- 42sh/src/exec/exec_ampersand.c | 12 ++--- 42sh/src/exec/exec_and_if.c | 11 +++-- 42sh/src/exec/exec_command.c | 6 +-- 42sh/src/exec/exec_dgreat.c | 10 ++-- 42sh/src/exec/exec_great.c | 10 ++-- 42sh/src/exec/exec_less.c | 10 ++-- 42sh/src/exec/exec_or_if.c | 11 +++-- 42sh/src/exec/exec_pipe.c | 12 +++-- 42sh/src/exec/exec_semi.c | 8 ++-- 42sh/src/exec/fd_redirect.c | 7 ++- 42sh/src/exec/ft_cmd.c | 29 ++++++------ 42sh/src/exec/ft_exec.c | 6 +-- 42sh/src/exec/set_exitstatus.c | 6 +-- 42sh/src/job-control/check_chlds.c | 39 ++++++++++++++++ 42sh/src/job-control/ft_cmppid.c | 6 --- 42sh/src/job-control/job_announce.c | 4 +- 42sh/src/job-control/job_cmpid.c | 19 ++++++++ 42sh/src/job-control/job_cmppid.c | 18 +++++++ 42sh/src/job-control/job_free.c | 23 +++++++++ 42sh/src/job-control/job_new.c | 16 +++++-- 42sh/src/job-control/job_print_change.c | 32 +++++++++++++ 42sh/src/job-control/job_update_id.c | 29 ++++++++++++ 42sh/src/job-control/job_update_rank.c | 35 ++++++++++++++ 42sh/src/job-control/sigchld_handler.c | 31 ++++--------- 42sh/src/lexer/qstate_update.c | 2 +- 42sh/src/line-editing/ft_interactive_sh.c | 5 +- 42sh/src/line-editing/ft_key_ctrl_d.c | 3 +- 42sh/src/line-editing/ft_key_default.c | 2 +- 42sh/src/line-editing/ft_prompt.c | 13 +++++- 42sh/src/line-editing/ft_set_termios.c | 2 +- 42sh/src/line-editing/input_init.c | 5 +- 42sh/src/main/data_exit.c | 7 ++- 42sh/src/main/data_init.c | 18 ++++--- 42sh/src/main/main.c | 8 ++-- 42sh/src/main/{ft_cleanup.c => shell_exit.c} | 17 +++---- 42sh/src/main/shell_init.c | 41 ++++++++++++++++ 43 files changed, 459 insertions(+), 182 deletions(-) create mode 100644 42sh/src/job-control/check_chlds.c delete mode 100644 42sh/src/job-control/ft_cmppid.c create mode 100644 42sh/src/job-control/job_cmpid.c create mode 100644 42sh/src/job-control/job_cmppid.c create mode 100644 42sh/src/job-control/job_free.c create mode 100644 42sh/src/job-control/job_print_change.c create mode 100644 42sh/src/job-control/job_update_id.c create mode 100644 42sh/src/job-control/job_update_rank.c rename 42sh/src/main/{ft_cleanup.c => shell_exit.c} (70%) create mode 100644 42sh/src/main/shell_init.c diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index ab940088..abe37ef9 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,49 +6,62 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2016/12/10 17:09:03 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:02:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef EXEC_H # define EXEC_H -# include "minishell.h" # define PIPE_READ 0 # define PIPE_WRITE 1 +# include "libft.h" + typedef struct s_execfunc t_execfunc; +typedef long long t_type; + +struct s_exec +{ + int fdin; + int fdout; + int amp; + char *aol_status; + int aol_search; +}; struct s_execfunc { t_type type; - int (*f)(t_btree **ast, t_data *data); + int (*f)(t_btree **ast); }; +# include "minishell.h" + extern t_execfunc g_execfunc[]; -int ft_exec(t_btree **ast, t_data *data); +int ft_exec(t_btree **ast); -int exec_semi(t_btree **ast, t_data *data); -int exec_ampersand(t_btree **ast, t_data *data); -int exec_or_if(t_btree **ast, t_data *data); -int exec_and_if(t_btree **ast, t_data *data); -int exec_pipe(t_btree **ast, t_data *data); +int exec_semi(t_btree **ast); +int exec_ampersand(t_btree **ast); +int exec_or_if(t_btree **ast); +int exec_and_if(t_btree **ast); +int exec_pipe(t_btree **ast); -int exec_less(t_btree **ast, t_data *data); -int exec_great(t_btree **ast, t_data *data); -int exec_dgreat(t_btree **ast, t_data *data); -int exec_command(t_btree **ast, t_data *data); +int exec_less(t_btree **ast); +int exec_great(t_btree **ast); +int exec_dgreat(t_btree **ast); +int exec_command(t_btree **ast); -void fd_redirect(t_data *data); -void fd_reset(t_data *data); +void fd_redirect(void); +void fd_reset(void); -int ft_cmd_process(char **argv, t_data *data); -int ft_cmd_exec(char *execpath, char **argv, t_data *data); +int ft_cmd_process(char **argv); +int ft_cmd_exec(char *execpath, char **argv); char *ft_findexec(char *path, char *file); void ast_free(void *data, size_t content_size); -void set_exitstatus(t_data *data, int status); +void set_exitstatus(int status); #endif diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index a9d62d78..2e01ebf4 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -6,16 +6,19 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */ -/* Updated: 2016/12/10 18:20:40 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 17:51:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef JOB_CONTROL_H # define JOB_CONTROL_H -# include "minishell.h" +# include +# include +# include "libft.h" typedef struct s_job t_job; +typedef struct s_jobc t_jobc; struct s_job { @@ -24,12 +27,29 @@ struct s_job char *command; }; +struct s_jobc +{ + t_list *list; + pid_t shell_pgid; + int current_id; + int rank[2]; + struct termios shell_tmodes; +}; + +# include "minishell.h" + extern t_data *g_data; -void job_new(t_data *data, char **av, pid_t pid); +void job_new(char **av, pid_t pid); void job_announce(t_job *job); +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); +void job_update_id(void); +void job_print_change(t_job *job, int status); +void job_update_rank(void); -int ft_cmppid(t_job *job, pid_t *pid); +int check_chlds(void); void sigchld_handler(int signo); void sigint_handler(int signo); diff --git a/42sh/includes/line_editing.h b/42sh/includes/line_editing.h index 435c9444..e09926bc 100644 --- a/42sh/includes/line_editing.h +++ b/42sh/includes/line_editing.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:38:21 by jhalford #+# #+# */ -/* Updated: 2016/12/07 16:57:40 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 17:49:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 0dafe98b..d4f3887d 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: 2016/12/10 17:56:39 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 17:56:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,13 @@ typedef struct s_line t_line; typedef struct s_comp t_comp; typedef struct s_exec t_exec; typedef struct s_jobc t_jobc; +typedef enum e_mode t_mode; + +enum e_mode +{ + MODE_INPUT, + MODE_EXEC, +}; struct s_line { @@ -47,23 +54,10 @@ struct s_comp int a; }; -struct s_exec -{ - int fdin; - int fdout; - int amp; - char *aol_status; - int aol_search; -}; - -struct s_jobc -{ - t_list *list; -}; - struct s_data { char **env; + t_mode mode; t_line line; t_comp comp; t_exec exec; @@ -78,11 +72,12 @@ 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); +void shell_init(void); +void shell_exit(void); +int data_init(void); +void data_exit(void); -int ft_builtin(char **av, t_data *data); +int ft_builtin(char **av); int builtin_echo(char **av, t_data *data); int builtin_cd(char **av, t_data *data); int builtin_exit(char **av, t_data *data); diff --git a/42sh/libft b/42sh/libft index 5fab2c76..6f467979 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 5fab2c76b033729a3b16a15f2b5ac63f2deaafc4 +Subproject commit 6f46797917052ce5d9c4e85ce7aa7d63701d919c diff --git a/42sh/src/builtin/builtin.c b/42sh/src/builtin/builtin.c index 1966a63e..49fcc9e9 100644 --- a/42sh/src/builtin/builtin.c +++ b/42sh/src/builtin/builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:21:34 by jhalford #+# #+# */ -/* Updated: 2016/12/03 15:17:21 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 17:56:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,12 +22,14 @@ t_stof g_builtin[] = { {NULL, NULL}, }; -int ft_builtin(char **av, t_data *data) +int ft_builtin(char **av) { int i; int ret; + t_data *data; i = -1; + data = data_singleton(); while (g_builtin[++i].name) if (ft_strcmp(g_builtin[i].name, *av) == 0) { @@ -35,7 +37,7 @@ int ft_builtin(char **av, t_data *data) { if (fork() == 0) { - fd_redirect(data); + fd_redirect(); ret = (g_builtin[i].f)(av, data); exit(ret); } @@ -43,7 +45,7 @@ int ft_builtin(char **av, t_data *data) else { ret = (g_builtin[i].f)(av, data); - set_exitstatus(data, ret); + set_exitstatus(ret); } return (1); } diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 90859d01..e82816a2 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ -/* Updated: 2016/11/28 14:28:37 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:03:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ int builtin_env(char **av, t_data *data) i++; } if (av[i]) - ft_cmd_process(av + i, data); + ft_cmd_process(av + i); } return (0); } diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c index 2a7d1ad1..4e3e48f7 100644 --- a/42sh/src/exec/exec_ampersand.c +++ b/42sh/src/exec/exec_ampersand.c @@ -6,18 +6,18 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */ -/* Updated: 2016/12/10 16:53:06 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:03:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_ampersand(t_btree **ast, t_data *data) +int exec_ampersand(t_btree **ast) { - data->exec.amp = 1; - ft_exec(&(*ast)->left, data); - data->exec.amp = 0; - ft_exec(&(*ast)->right, data); + data_singleton()->exec.amp = 1; + ft_exec(&(*ast)->left); + data_singleton()->exec.amp = 0; + ft_exec(&(*ast)->right); btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_and_if.c b/42sh/src/exec/exec_and_if.c index 4858321d..0728310d 100644 --- a/42sh/src/exec/exec_and_if.c +++ b/42sh/src/exec/exec_and_if.c @@ -6,27 +6,30 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:28 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:17:33 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:01:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_and_if(t_btree **ast, t_data *data) +int exec_and_if(t_btree **ast) { + t_data *data; + + data = data_singleton(); if (data->exec.aol_status == NULL || (data->exec.aol_search == TK_AND_IF && *data->exec.aol_status == '0') || (data->exec.aol_search == TK_OR_IF && *data->exec.aol_status != '0')) { - ft_exec(&(*ast)->left, data); + ft_exec(&(*ast)->left); data->exec.aol_status = ft_getenv(data->env, "?"); } data->exec.aol_search = TK_AND_IF; if (*data->exec.aol_status == '0' || ((t_astnode*)(*ast)->right->item)->type != TK_COMMAND) - ft_exec(&(*ast)->right, data); + ft_exec(&(*ast)->right); data->exec.aol_status = NULL; data->exec.aol_search = 0; btree_delone(ast, &ast_free); diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 82eb4e52..868612d2 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,18 +6,18 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2016/12/05 14:06:34 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:01:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_command(t_btree **ast, t_data *data) +int exec_command(t_btree **ast) { t_astnode *node; node = (*ast)->item; - ft_cmd_process(node->data.sstr, data); + ft_cmd_process(node->data.sstr); btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_dgreat.c b/42sh/src/exec/exec_dgreat.c index a338c59d..a2471e98 100644 --- a/42sh/src/exec/exec_dgreat.c +++ b/42sh/src/exec/exec_dgreat.c @@ -6,22 +6,22 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:15:13 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:13:45 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:03:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_dgreat(t_btree **ast, t_data *data) +int exec_dgreat(t_btree **ast) { t_astnode *node; int fd; node = (*ast)->item; fd = open(node->data.redir.word.word, O_WRONLY | O_APPEND | O_CREAT, 0644); - data->exec.fdout = fd; - ft_exec(&(*ast)->left, data); - data->exec.fdout = STDOUT; + data_singleton()->exec.fdout = fd; + ft_exec(&(*ast)->left); + data_singleton()->exec.fdout = STDOUT; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_great.c b/42sh/src/exec/exec_great.c index 996283e3..409a54af 100644 --- a/42sh/src/exec/exec_great.c +++ b/42sh/src/exec/exec_great.c @@ -6,22 +6,22 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:13:28 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:03:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_great(t_btree **ast, t_data *data) +int exec_great(t_btree **ast) { t_astnode *node; int fd; node = (*ast)->item; fd = open(node->data.redir.word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); - data->exec.fdout = fd; - ft_exec(&(*ast)->left, data); - data->exec.fdout = STDOUT; + data_singleton()->exec.fdout = fd; + ft_exec(&(*ast)->left); + data_singleton()->exec.fdout = STDOUT; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_less.c b/42sh/src/exec/exec_less.c index 63dd4dbf..2cd7639e 100644 --- a/42sh/src/exec/exec_less.c +++ b/42sh/src/exec/exec_less.c @@ -6,22 +6,22 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:12:59 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:03:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_less(t_btree **ast, t_data *data) +int exec_less(t_btree **ast) { t_astnode *node; int fd; node = (*ast)->item; fd = open(node->data.redir.word.word, O_RDONLY); - data->exec.fdin = fd; - ft_exec(&(*ast)->left, data); - data->exec.fdin = STDIN; + data_singleton()->exec.fdin = fd; + ft_exec(&(*ast)->left); + data_singleton()->exec.fdin = STDIN; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_or_if.c b/42sh/src/exec/exec_or_if.c index 1b60dbc3..a5170f93 100644 --- a/42sh/src/exec/exec_or_if.c +++ b/42sh/src/exec/exec_or_if.c @@ -6,27 +6,30 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 21:06:17 by jhalford #+# #+# */ -/* Updated: 2016/12/09 21:50:19 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:04:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_or_if(t_btree **ast, t_data *data) +int exec_or_if(t_btree **ast) { + t_data *data; + + data = data_singleton(); if (data->exec.aol_status == NULL || (data->exec.aol_search == TK_AND_IF && *data->exec.aol_status == '0') || (data->exec.aol_search == TK_OR_IF && *data->exec.aol_status != '0')) { - ft_exec(&(*ast)->left, data); + ft_exec(&(*ast)->left); data->exec.aol_status = ft_getenv(data->env, "?"); } data->exec.aol_search = TK_OR_IF; if (*data->exec.aol_status != '0' || ((t_astnode*)(*ast)->right->item)->type != TK_COMMAND) - ft_exec(&(*ast)->right, data); + ft_exec(&(*ast)->right); data->exec.aol_status = NULL; data->exec.aol_search = 0; btree_delone(ast, &ast_free); diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 0d0c65ae..cac0e63e 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -6,25 +6,27 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */ -/* Updated: 2016/12/06 20:26:55 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:04:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_pipe(t_btree **ast, t_data *data) +int exec_pipe(t_btree **ast) { - int fds[2]; + int fds[2]; + t_data *data; + data = data_singleton(); pipe(fds); DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); data->exec.fdout = fds[PIPE_WRITE]; - ft_exec(&(*ast)->left, data); + ft_exec(&(*ast)->left); if (data->exec.fdout != STDOUT) close(data->exec.fdout); data->exec.fdout = STDOUT; data->exec.fdin = fds[PIPE_READ]; - ft_exec(&(*ast)->right, data); + ft_exec(&(*ast)->right); close(fds[PIPE_WRITE]); close(fds[PIPE_READ]); data->exec.fdin = STDIN; diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index 935ef5ee..335ac2ac 100644 --- a/42sh/src/exec/exec_semi.c +++ b/42sh/src/exec/exec_semi.c @@ -6,16 +6,16 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ -/* Updated: 2016/12/10 17:19:57 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:00:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_semi(t_btree **ast, t_data *data) +int exec_semi(t_btree **ast) { - ft_exec(&(*ast)->left, data); - ft_exec(&(*ast)->right, data); + ft_exec(&(*ast)->left); + ft_exec(&(*ast)->right); btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/fd_redirect.c b/42sh/src/exec/fd_redirect.c index 6c1d8c47..5480b866 100644 --- a/42sh/src/exec/fd_redirect.c +++ b/42sh/src/exec/fd_redirect.c @@ -6,14 +6,17 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2016/12/03 15:24:08 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 17:54:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void fd_redirect(t_data *data) +void fd_redirect(void) { + t_data *data; + + data = data_singleton(); if (data->exec.fdin != STDIN) { dup2(data->exec.fdin, STDIN); diff --git a/42sh/src/exec/ft_cmd.c b/42sh/src/exec/ft_cmd.c index 96492597..9dc9e546 100644 --- a/42sh/src/exec/ft_cmd.c +++ b/42sh/src/exec/ft_cmd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:18 by jhalford #+# #+# */ -/* Updated: 2016/12/10 17:34:14 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:11:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,25 +14,25 @@ extern pid_t g_pid; -int ft_cmd_process(char **argv, t_data *data) +int ft_cmd_process(char **argv) { char *execpath; - ft_expand_dollar(argv, data->env); - if (ft_builtin(argv, data)) + ft_expand_dollar(argv, data_singleton()->env); + if (ft_builtin(argv)) return (0); else if (ft_strchr(argv[0], '/')) execpath = ft_strdup(argv[0]); - else if (!(execpath = ft_findexec(ft_getenv(data->env, "PATH"), argv[0]))) + else if (!(execpath = ft_findexec(ft_getenv(data_singleton()->env, "PATH"), argv[0]))) { ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, argv[0]); - set_exitstatus(data, 127); + set_exitstatus(127); return (-1); } - return (ft_cmd_exec(execpath, argv, data)); + return (ft_cmd_exec(execpath, argv)); } -int ft_cmd_exec(char *execpath, char **argv, t_data *data) +int ft_cmd_exec(char *execpath, char **argv) { pid_t pid; int status; @@ -47,19 +47,20 @@ int ft_cmd_exec(char *execpath, char **argv, t_data *data) return (-1); else if (pid == 0) { - fd_redirect(data); - execve(execpath, argv, data->env); + fd_redirect(); + execve(execpath, argv, data_singleton()->env); + exit(42); } else { ft_strdel(&execpath); g_pid = pid; - if (data->exec.amp) - job_new(data, argv, pid); - else if (data->exec.fdout == STDOUT) + if (data_singleton()->exec.amp) + job_new(argv, pid); + else if (data_singleton()->exec.fdout == STDOUT) { waitpid(pid, &status, 0); - set_exitstatus(data, status); + set_exitstatus(status); } g_pid = 0; } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index f7a3f6cc..c809d017 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/10 17:22:42 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:11:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ t_execfunc g_execfunc[] = {0, 0}, }; -int ft_exec(t_btree **ast, t_data *data) +int ft_exec(t_btree **ast) { t_astnode *item; int i; @@ -38,7 +38,7 @@ int ft_exec(t_btree **ast, t_data *data) while (g_execfunc[i].type) { if (item->type == g_execfunc[i].type) - return ((*g_execfunc[i].f)(ast, data)); + return ((*g_execfunc[i].f)(ast)); i++; } return (0); diff --git a/42sh/src/exec/set_exitstatus.c b/42sh/src/exec/set_exitstatus.c index 3a7dfd13..14c7f462 100644 --- a/42sh/src/exec/set_exitstatus.c +++ b/42sh/src/exec/set_exitstatus.c @@ -6,17 +6,17 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2016/12/07 16:29:11 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 17:54:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void set_exitstatus(t_data *data, int status) +void set_exitstatus(int status) { char *astatus; astatus = ft_itoa(status); - builtin_setenv((char*[3]){"?", astatus}, data); + builtin_setenv((char*[3]){"?", astatus}, data_singleton()); ft_strdel(&astatus); } diff --git a/42sh/src/job-control/check_chlds.c b/42sh/src/job-control/check_chlds.c new file mode 100644 index 00000000..6b767582 --- /dev/null +++ b/42sh/src/job-control/check_chlds.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_chlds.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/12 12:28:40 by jhalford #+# #+# */ +/* Updated: 2016/12/12 17:23:14 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int check_chlds() +{ + int status; + pid_t pid; + t_job *job; + t_list **start; + t_list *list; + + DG("gonna check childs"); + start = &data_singleton()->jobc.list; + pid = waitpid(-1, &status, WNOHANG); + DG("waitpid = %i", pid); + list = *start ? ft_lst_find(*start, &pid, job_cmp_pid) : NULL; + if (list) + { + job = list->content; + if (job->id < data_singleton()->jobc.current_id) + data_singleton()->jobc.current_id = job->id; + job_print_change(job, status); + ft_lst_delif(start, list->content, ft_addrcmp, job_free); + job_update_rank(); + return (1); + } + return (0); +} diff --git a/42sh/src/job-control/ft_cmppid.c b/42sh/src/job-control/ft_cmppid.c deleted file mode 100644 index 117db889..00000000 --- a/42sh/src/job-control/ft_cmppid.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "minishell.h" - -int ft_cmppid(t_job *job, pid_t *pid) -{ - return (job->pid - *pid); -} diff --git a/42sh/src/job-control/job_announce.c b/42sh/src/job-control/job_announce.c index a64322cd..64b6745c 100644 --- a/42sh/src/job-control/job_announce.c +++ b/42sh/src/job-control/job_announce.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 17:05:49 by jhalford #+# #+# */ -/* Updated: 2016/12/10 17:26:44 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 12:54:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,5 @@ void job_announce(t_job *job) { - ft_printf("[%i] %i\n", job->id, job->pid); + ft_printf("{mag}[%i] %i{eoc}\n", job->id, job->pid); } diff --git a/42sh/src/job-control/job_cmpid.c b/42sh/src/job-control/job_cmpid.c new file mode 100644 index 00000000..3424cf13 --- /dev/null +++ b/42sh/src/job-control/job_cmpid.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_cmp_id.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/12 13:38:13 by jhalford #+# #+# */ +/* Updated: 2016/12/12 13:44:23 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int job_cmp_id(t_job *job, int *id) +{ + return (job->id - *id); +} + diff --git a/42sh/src/job-control/job_cmppid.c b/42sh/src/job-control/job_cmppid.c new file mode 100644 index 00000000..0f11c30a --- /dev/null +++ b/42sh/src/job-control/job_cmppid.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_cmp_pid.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/12 13:00:21 by jhalford #+# #+# */ +/* Updated: 2016/12/12 13:40:13 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int job_cmp_pid(t_job *job, pid_t *pid) +{ + return (job->pid - *pid); +} diff --git a/42sh/src/job-control/job_free.c b/42sh/src/job-control/job_free.c new file mode 100644 index 00000000..09a89abd --- /dev/null +++ b/42sh/src/job-control/job_free.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ +/* Updated: 2016/12/12 13:02:05 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void job_free(void *content, size_t content_size) +{ + t_job *job; + + (void)content_size; + job = content; + ft_strdel(&job->command); + free(job); +} diff --git a/42sh/src/job-control/job_new.c b/42sh/src/job-control/job_new.c index 6f6e8b72..66717f66 100644 --- a/42sh/src/job-control/job_new.c +++ b/42sh/src/job-control/job_new.c @@ -6,21 +6,31 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:51:54 by jhalford #+# #+# */ -/* Updated: 2016/12/10 17:58:04 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 15:06:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -void job_new(t_data *data, char **av, pid_t pid) +void job_new(char **av, pid_t pid) { t_job job; + t_data *data; + data = data_singleton(); DG("got new job"); + if (data->mode == MODE_INPUT) + DG("am in MODE_INPUT"); + else if (data->mode == MODE_EXEC) + DG("am in MODE_EXEC"); job.command = ft_sstrcat(av, ' '); DG("job command '%s'", job.command); job.pid = pid; - job.id = 42; + job_update_id(); + DG("id = %i", data->jobc.current_id); + job.id = data->jobc.current_id; + data->jobc.rank[1] = data->jobc.rank[0]; + data->jobc.rank[0] = job.id; ft_lstadd(&data->jobc.list, ft_lstnew(&job, sizeof(job))); job_announce(data->jobc.list->content); } diff --git a/42sh/src/job-control/job_print_change.c b/42sh/src/job-control/job_print_change.c new file mode 100644 index 00000000..b36e0840 --- /dev/null +++ b/42sh/src/job-control/job_print_change.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_print_change.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/12 15:04:03 by jhalford #+# #+# */ +/* Updated: 2016/12/12 16:39:31 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void job_print_change(t_job *job, int status) +{ + char rank; + + 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 %i ", job->id, rank, job->pid); + if (status == 0) + ft_printf("{gre}done{mag}"); + else if (status == 9) + ft_printf("{red}killed{mag}"); + else + ft_printf("exit %i", status); + ft_printf("\t%s{eoc}\n", job->command); +} diff --git a/42sh/src/job-control/job_update_id.c b/42sh/src/job-control/job_update_id.c new file mode 100644 index 00000000..e69ca110 --- /dev/null +++ b/42sh/src/job-control/job_update_id.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_update_id.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/12 13:33:08 by jhalford #+# #+# */ +/* Updated: 2016/12/12 17:27:59 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void job_update_id(void) +{ + int *id; + t_jobc *jobc; + t_list *start; + + jobc = &data_singleton()->jobc; + id = &jobc->current_id; + start = jobc->list; + while (ft_lst_find(start, id, job_cmp_id)) + { + *id += 1; + DG("id = %i", *id); + } +} diff --git a/42sh/src/job-control/job_update_rank.c b/42sh/src/job-control/job_update_rank.c new file mode 100644 index 00000000..0c8f7c38 --- /dev/null +++ b/42sh/src/job-control/job_update_rank.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_shiftstatus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/12 13:05:10 by jhalford #+# #+# */ +/* Updated: 2016/12/12 17:23:12 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void job_update_rank() +{ + t_jobc *jobc; + t_job *job; + t_list *list; + + jobc = &data_singleton()->jobc; + list = jobc->list; + if (list) + { + job = list->content; + jobc->rank[0] = job->id; + jobc->rank[1] = list->next ? ((t_job*)list->next->content)->id : 0; + } + else + { + jobc->rank[0] = 0; + jobc->rank[1] = 0; + } + DG("updated rank: %i,%i", jobc->rank[0], jobc->rank[1]); +} diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c index e72fc7b7..40dff0ad 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/10 18:25:01 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 16:49:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,28 +14,17 @@ void sigchld_handler(int signo) { - int status; - pid_t pid; - t_job *job; - t_list *start; - t_list *list; + t_data *data; (void)signo; - DG("got SIGCHLD"); - start = data_singleton()->jobc.list; - pid = waitpid(-1, &status, WNOHANG); - DG("SIGCHLD pid=%i", pid); - list = start ? ft_lst_find(start, &pid, ft_cmppid) : NULL; - if (list) + data = data_singleton(); + if (data->mode == MODE_INPUT) { - job = list->content; - if (status == 0) - ft_printf("[%i] + %i done\t%s\n", job->id, pid, job->command); - else if (status == 9) - ft_printf("[%i] + %i killed\t%s\n", job->id, pid, job->command); - else - ft_printf("[%i] + %i exit %i\t%s\n", - job->id, pid, status, job->command); - ft_prompt(); + DG("got SIGCHLD in MODE_INPUT (asynchronos notification)"); + check_chlds(); + ft_putstr(SHELL_PROMPT); + ft_putstr(data->line.input); } + else + DG("got SIGCHLD in MODE_EXEC, will check before next prompt"); } diff --git a/42sh/src/lexer/qstate_update.c b/42sh/src/lexer/qstate_update.c index 351b552e..8ccba7aa 100644 --- a/42sh/src/lexer/qstate_update.c +++ b/42sh/src/lexer/qstate_update.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 15:56:40 by jhalford #+# #+# */ -/* Updated: 2016/12/03 15:36:24 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 13:59:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_interactive_sh.c b/42sh/src/line-editing/ft_interactive_sh.c index ee600dcb..2c2d58a9 100644 --- a/42sh/src/line-editing/ft_interactive_sh.c +++ b/42sh/src/line-editing/ft_interactive_sh.c @@ -6,11 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:14:12 by jhalford #+# #+# */ -/* Updated: 2016/12/10 17:18:29 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 17:42:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "line_editing.h" +#include "minishell.h" t_stof g_keys[] = { {FT_KEY_C_K, NULL}, @@ -60,6 +60,7 @@ int ft_interactive_sh(t_data *data) return (-1); else if (ret == 2) { + data->mode = MODE_EXEC; ft_lstdel(&data->line.qstack, &ft_lst_cfree); ft_set_termios(data, 0); return (0); diff --git a/42sh/src/line-editing/ft_key_ctrl_d.c b/42sh/src/line-editing/ft_key_ctrl_d.c index c882cc99..1b586098 100644 --- a/42sh/src/line-editing/ft_key_ctrl_d.c +++ b/42sh/src/line-editing/ft_key_ctrl_d.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:44:24 by jhalford #+# #+# */ -/* Updated: 2016/12/07 18:12:29 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 17:40:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,6 @@ int ft_key_ctrl_d(t_data *data, char *buf) { (void)data; (void)buf; - data_exit(data); ft_putendl("exit"); exit(0); } diff --git a/42sh/src/line-editing/ft_key_default.c b/42sh/src/line-editing/ft_key_default.c index 441918ab..25b20247 100644 --- a/42sh/src/line-editing/ft_key_default.c +++ b/42sh/src/line-editing/ft_key_default.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:45:23 by jhalford #+# #+# */ -/* Updated: 2016/12/10 17:18:25 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 13:59:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_prompt.c b/42sh/src/line-editing/ft_prompt.c index 32aa615e..40a62c0a 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/11/28 15:16:30 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 16:49:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,17 @@ int ft_prompt(void) { + t_data *data; + + data = data_singleton(); + while (data->jobc.list) + { + /* usleep(500 * 1000); */ + if (check_chlds()) + continue ; + else + break ; + } ft_putstr(SHELL_PROMPT); return (0); } diff --git a/42sh/src/line-editing/ft_set_termios.c b/42sh/src/line-editing/ft_set_termios.c index f51cb9de..04fa8453 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: 2016/12/07 14:20:59 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 17:35:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/input_init.c b/42sh/src/line-editing/input_init.c index 3328df74..84ce1af7 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: 2016/12/10 17:16:40 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 17:35:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,8 +22,9 @@ int input_init(t_data *data) data->line.input_pos = 0; data->line.qstack = ft_lstnew(NULL, sizeof(t_qstate)); *((t_qstate*)data->line.qstack->content) = Q_NONE; + ft_prompt(); if (ft_set_termios(data, 1)) return (-1); - ft_prompt(); + data->mode = MODE_INPUT; return (0); } diff --git a/42sh/src/main/data_exit.c b/42sh/src/main/data_exit.c index cd912115..b233dc36 100644 --- a/42sh/src/main/data_exit.c +++ b/42sh/src/main/data_exit.c @@ -6,14 +6,17 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/07 18:07:50 by jhalford #+# #+# */ -/* Updated: 2016/12/07 18:12:34 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 17:39:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void data_exit(t_data *data) +void data_exit(void) { + t_data *data; + + data = data_singleton(); ft_strdel(&data->line.input); ft_dlstdel(&data->line.history, &ft_lst_bfree); ft_lstdel(&data->line.qstack, &ft_lst_cfree); diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index fe10e2bb..1c35b6f0 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2016/12/10 17:58:36 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 17:50:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,12 @@ extern char **environ; -int data_init(t_data *data) +int data_init(void) { - char *term_name; + char *term_name; + t_data *data; - atexit(&ft_cleanup); + data = data_singleton(); data->line.input = NULL; data->env = ft_sstrdup(environ); data->line.history = NULL; @@ -28,17 +29,14 @@ int data_init(t_data *data) data->exec.aol_search = 0; data->exec.amp = 0; data->jobc.list = NULL; + data->jobc.current_id = 1; + data->jobc.rank[0] = 0; + data->jobc.rank[1] = 0; if (!(data->line.history = ft_dlstnew(NULL, 0))) return (-1); if ((term_name = ft_getenv(data->env, "TERM")) == NULL) return (-1); if (tgetent(NULL, term_name) != 1) return (-1); - if (signal(SIGINT, sigint_handler) == SIG_ERR) - ft_dprintf(STDERR, "\ncan't catch SIGINT\n"); - if (signal(SIGTSTP, sigtstp_handler) == SIG_ERR) - ft_dprintf(STDERR, "\ncan't catch SIGTSTP\n"); - if (signal(SIGCHLD, sigchld_handler) == SIG_ERR) - ft_dprintf(STDERR, "\ncan't catch SIGCHLD\n"); return (0); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 9ecce134..a6127a06 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/10 17:58:39 by jhalford ### ########.fr */ +/* Updated: 2016/12/12 18:11:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,12 @@ int main(void) { t_list *token; t_btree *ast; - /* t_data data; */ t_data *data; token = NULL; ast = NULL; data = data_singleton(); - if (data_init(data)) - return (1); + shell_init(); DG("{inv}{bol}{gre}start of shell"); while (1) { @@ -41,7 +39,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)) return (1); } return (0); diff --git a/42sh/src/main/ft_cleanup.c b/42sh/src/main/shell_exit.c similarity index 70% rename from 42sh/src/main/ft_cleanup.c rename to 42sh/src/main/shell_exit.c index 7cf6aa3b..3d12fa63 100644 --- a/42sh/src/main/ft_cleanup.c +++ b/42sh/src/main/shell_exit.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_cleanup.c :+: :+: :+: */ +/* shell_exit.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/01 14:42:42 by jhalford #+# #+# */ -/* Updated: 2016/12/09 21:50:38 by jhalford ### ########.fr */ +/* Created: 2016/12/12 17:37:04 by jhalford #+# #+# */ +/* Updated: 2016/12/12 17:51:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,15 +17,10 @@ extern char PC; extern char *UP; extern char *BC; -void ft_cleanup(void) +void shell_exit(void) { - struct termios term; - DG("cleanup. char * UP at %p", UP); DG("cleanup. char * BC at %p", BC); - if (tcgetattr(0, &term) == -1) - return ; - term.c_lflag |= ICANON | ISIG | ECHO; - if (tcsetattr(0, TCSANOW, &term) == -1) - return ; + data_exit(); + tcsetattr(0, TCSANOW, &data_singleton()->jobc.shell_tmodes); } diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c new file mode 100644 index 00000000..52552fb4 --- /dev/null +++ b/42sh/src/main/shell_init.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* shell_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ +/* Updated: 2016/12/12 18:11:50 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void shell_init(void) +{ + int *shell_pgid; + + shell_pgid = &data_singleton()->jobc.shell_pgid; + data_init(); + atexit(&shell_exit); + if (isatty(STDIN_FILENO)) + { + while (tcgetpgrp(STDIN_FILENO) != (*shell_pgid = getpgrp())) + kill(*shell_pgid, SIGTTIN); + signal(SIGINT, sigint_handler); + signal(SIGQUIT, SIG_IGN); + signal(SIGTSTP, sigtstp_handler); + signal(SIGTTIN, SIG_IGN); + signal(SIGTTOU, SIG_IGN); + signal(SIGCHLD, sigchld_handler); + *shell_pgid = getpid(); + if (setpgid(*shell_pgid, *shell_pgid)) + { + ft_dprintf(2, "Couldnt put the shell in it's own process group"); + exit (1); + } + tcsetpgrp(STDIN_FILENO, *shell_pgid); + tcgetattr(STDIN_FILENO, &data_singleton()->jobc.shell_tmodes); + } +}