From 4cfeb62747b9cc352696768539a3a0bf58da36c4 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sat, 10 Dec 2016 17:47:36 +0100 Subject: [PATCH 01/27] job_control first commit: --- 42sh/Makefile | 10 ++--- 42sh/includes/exec.h | 3 +- 42sh/includes/job_control.h | 30 ++++++++++++++ 42sh/includes/lexer.h | 2 +- 42sh/includes/minishell.h | 40 ++++++++++++------- 42sh/src/exec/exec_ampersand.c | 23 +++++++++++ 42sh/src/exec/exec_semi.c | 2 +- 42sh/src/exec/ft_cmd.c | 8 ++-- 42sh/src/exec/ft_exec.c | 5 ++- 42sh/src/job-control/job_announce.c | 18 +++++++++ 42sh/src/job-control/job_new.c | 25 ++++++++++++ 42sh/src/job-control/sigchld_handler.c | 19 +++++++++ 42sh/src/job-control/sigtstp_handler.c | 19 +++++++++ 42sh/src/line-editing/ft_interactive_sh.c | 2 +- 42sh/src/line-editing/ft_key_default.c | 2 +- 42sh/src/line-editing/input_init.c | 2 +- .../sigint_handler.c} | 9 +++-- 42sh/src/main/data_init.c | 9 ++++- 42sh/src/main/main.c | 4 +- 19 files changed, 193 insertions(+), 39 deletions(-) create mode 100644 42sh/includes/job_control.h create mode 100644 42sh/src/exec/exec_ampersand.c create mode 100644 42sh/src/job-control/job_announce.c create mode 100644 42sh/src/job-control/job_new.c create mode 100644 42sh/src/job-control/sigchld_handler.c create mode 100644 42sh/src/job-control/sigtstp_handler.c rename 42sh/src/{main/sig_handler.c => line-editing/sigint_handler.c} (78%) diff --git a/42sh/Makefile b/42sh/Makefile index 488e22b2..3796a9a1 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -42,11 +42,6 @@ $(D_OBJ)/%.o: $(D_SRC)/builtin/%.c includes/minishell.h @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @echo "Compiling "$<"..." -$(D_OBJ)/%.o: $(D_SRC)/minishell-exec/%.c includes/minishell.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." - $(D_OBJ)/%.o: $(D_SRC)/line-editing/%.c includes/line_editing.h @$(MKDIR) $(D_OBJ) @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @@ -67,6 +62,11 @@ $(D_OBJ)/%.o: $(D_SRC)/exec/%.c includes/exec.h @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @echo "Compiling "$<"..." +$(D_OBJ)/%.o: $(D_SRC)/job-control/%.c includes/job_control.h + @$(MKDIR) $(D_OBJ) + @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) + @echo "Compiling "$<"..." + libft/libft.a: libft/src/*/*.c @echo "libft/libft.a" @$(MAKE) -C libft 2>/dev/null diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 15aac6df..ab940088 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/06 18:23:29 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:09:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,6 +30,7 @@ extern t_execfunc g_execfunc[]; int ft_exec(t_btree **ast, t_data *data); 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); diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h new file mode 100644 index 00000000..7e42ea6f --- /dev/null +++ b/42sh/includes/job_control.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_control.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */ +/* Updated: 2016/12/10 17:36:55 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef JOB_CONTROL_H +# define JOB_CONTROL_H + +# include "minishell.h" + +typedef struct s_job t_job; + +struct s_job +{ + int id; + pid_t pid; + char *command; +}; + +void job_new(t_data *data, char **av, pid_t pid); +void job_announce(t_job *job); + +#endif diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 97ba7c4a..fbde230a 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2016/12/05 14:11:52 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 16:00:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 729590a7..ef76d26c 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/07 18:09:27 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:12:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ # include "lexer.h" # include "parser.h" # include "exec.h" +# include "job_control.h" # include # include @@ -31,34 +32,42 @@ typedef long long t_type; typedef struct s_line t_line; typedef struct s_comp t_comp; typedef struct s_exec t_exec; +typedef struct s_jobc t_jobc; struct s_line { - t_dlist *history; - int input_pos; - t_list *qstack; - char *input; + t_dlist *history; + int input_pos; + t_list *qstack; + char *input; }; struct s_comp { - int a; + int a; }; struct s_exec { - int fdin; - int fdout; - char *aol_status; - int aol_search; + int fdin; + int fdout; + int amp; + char *aol_status; + int aol_search; +}; + +struct s_jobc +{ + t_list *list; }; struct s_data { - char **env; - t_exec exec; - t_line line; - t_comp comp; + char **env; + t_line line; + t_comp comp; + t_exec exec; + t_jobc jobc; }; typedef struct s_data t_data; @@ -67,7 +76,8 @@ typedef enum e_qstate t_qstate; extern t_stof g_builtins[]; extern pid_t g_pid; -void sig_handler(int signo); +void sigint_handler(int signo); +void sigstop_handler(int signo); int data_init(t_data *data); void data_exit(t_data *data); void ft_cleanup(void); diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c new file mode 100644 index 00000000..2a7d1ad1 --- /dev/null +++ b/42sh/src/exec/exec_ampersand.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_ampersand.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */ +/* Updated: 2016/12/10 16:53:06 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int exec_ampersand(t_btree **ast, t_data *data) +{ + data->exec.amp = 1; + ft_exec(&(*ast)->left, data); + data->exec.amp = 0; + ft_exec(&(*ast)->right, data); + btree_delone(ast, &ast_free); + return (0); +} diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index 39b7d484..935ef5ee 100644 --- a/42sh/src/exec/exec_semi.c +++ b/42sh/src/exec/exec_semi.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:14:37 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:19:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_cmd.c b/42sh/src/exec/ft_cmd.c index adb9d6db..96492597 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/09 21:50:26 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:34:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ int ft_cmd_process(char **argv, t_data *data) if (ft_builtin(argv, data)) return (0); else if (ft_strchr(argv[0], '/')) - execpath = argv[0]; + execpath = ft_strdup(argv[0]); else if (!(execpath = ft_findexec(ft_getenv(data->env, "PATH"), argv[0]))) { ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, argv[0]); @@ -54,7 +54,9 @@ int ft_cmd_exec(char *execpath, char **argv, t_data *data) { ft_strdel(&execpath); g_pid = pid; - if (data->exec.fdout == STDOUT) + if (data->exec.amp) + job_new(data, argv, pid); + else if (data->exec.fdout == STDOUT) { waitpid(pid, &status, 0); set_exitstatus(data, status); diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 0992b7aa..f7a3f6cc 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/05 13:37:46 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:22:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ t_execfunc g_execfunc[] = {TK_AND_IF, &exec_and_if}, {TK_OR_IF, &exec_or_if}, {TK_SEMI, &exec_semi}, + {TK_AMP, &exec_ampersand}, {TK_PIPE, &exec_pipe}, {TK_LESS, &exec_less}, {TK_GREAT, &exec_great}, @@ -31,9 +32,9 @@ int ft_exec(t_btree **ast, t_data *data) int i; i = 0; - item = (*ast)->item; if (!*ast) return (0); + item = (*ast)->item; while (g_execfunc[i].type) { if (item->type == g_execfunc[i].type) diff --git a/42sh/src/job-control/job_announce.c b/42sh/src/job-control/job_announce.c new file mode 100644 index 00000000..a64322cd --- /dev/null +++ b/42sh/src/job-control/job_announce.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_announce.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 17:05:49 by jhalford #+# #+# */ +/* Updated: 2016/12/10 17:26:44 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void job_announce(t_job *job) +{ + ft_printf("[%i] %i\n", job->id, job->pid); +} diff --git a/42sh/src/job-control/job_new.c b/42sh/src/job-control/job_new.c new file mode 100644 index 00000000..e9fd72b8 --- /dev/null +++ b/42sh/src/job-control/job_new.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 16:51:54 by jhalford #+# #+# */ +/* Updated: 2016/12/10 17:37:33 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void job_new(t_data *data, char **av, pid_t pid, t_type type) +{ + t_job job; + + DG("got new job"); + job.command = ft_sstrcat(av, ' '); + job.pid = pid; + job.id = 42; + ft_lstadd(&data->jobc.list, ft_lstnew(&job, sizeof(job))); + job_announce(data->jobc.list->content); +} diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c new file mode 100644 index 00000000..fd0b3e0e --- /dev/null +++ b/42sh/src/job-control/sigchld_handler.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sigchld_handler.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 17:37:56 by jhalford #+# #+# */ +/* Updated: 2016/12/10 17:46:12 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void sigchld_handler(int signo) +{ + (void)signo; + DG("got SIGCHLD"); +} diff --git a/42sh/src/job-control/sigtstp_handler.c b/42sh/src/job-control/sigtstp_handler.c new file mode 100644 index 00000000..7475ed07 --- /dev/null +++ b/42sh/src/job-control/sigtstp_handler.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sigtstp_handler.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */ +/* Updated: 2016/12/10 17:46:16 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void sigtstp_handler(int signo) +{ + (void)signo; + DG("got SIGTSTP"); +} diff --git a/42sh/src/line-editing/ft_interactive_sh.c b/42sh/src/line-editing/ft_interactive_sh.c index ece0bda1..ee600dcb 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/07 17:27:25 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:18:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_key_default.c b/42sh/src/line-editing/ft_key_default.c index 4cb66621..441918ab 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/09 14:48:01 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:18:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/input_init.c b/42sh/src/line-editing/input_init.c index bea6a347..3328df74 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/07 16:30:40 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:16:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/sig_handler.c b/42sh/src/line-editing/sigint_handler.c similarity index 78% rename from 42sh/src/main/sig_handler.c rename to 42sh/src/line-editing/sigint_handler.c index d56b03ee..d2929ece 100644 --- a/42sh/src/main/sig_handler.c +++ b/42sh/src/line-editing/sigint_handler.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* sig_handler.c :+: :+: :+: */ +/* sigint_handler.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/01 12:43:22 by jhalford #+# #+# */ -/* Updated: 2016/12/03 13:31:33 by jhalford ### ########.fr */ +/* Created: 2016/12/10 15:14:47 by jhalford #+# #+# */ +/* Updated: 2016/12/10 15:31:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,12 @@ pid_t g_pid; -void sig_handler(int signo) +void sigint_handler(int signo) { (void)signo; if (signo == SIGINT) { + DG("got SIGINT"); if (g_pid) kill(g_pid, SIGINT); if (kill(g_pid, 0) == 0) diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 6fc5a928..9b549e58 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/09 19:15:12 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:35:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,11 +26,18 @@ int data_init(t_data *data) data->exec.fdout = STDOUT; data->exec.aol_status = NULL; data->exec.aol_search = 0; + data->exec.amp = 0; + data->jobc.list = NULL; 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) return (0); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index b5eeb81c..1a8a70be 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/09 22:15:07 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:16:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,8 +22,6 @@ int main(void) ast = NULL; if (data_init(&data)) return (1); - if (signal(SIGINT, sig_handler) == SIG_ERR) - ft_dprintf(STDERR, "\ncan't catch SIGINT\n"); DG("{inv}{bol}{gre}start of shell"); while (1) { From 09e823bf2ba14d3f88f7c2621d884c7eced5a428 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sat, 10 Dec 2016 18:26:09 +0100 Subject: [PATCH 02/27] job control: retrieval of dat as a global g_data so signal handlers can access it --- 42sh/includes/job_control.h | 8 +++++++- 42sh/includes/minishell.h | 4 +--- 42sh/libft | 2 +- 42sh/src/job-control/job_new.c | 4 ++-- 42sh/src/job-control/sigchld_handler.c | 14 +++++++++++++- 42sh/src/job-control/sigtstp_handler.c | 2 +- 42sh/src/main/data_init.c | 3 ++- 42sh/src/main/main.c | 5 ++++- 8 files changed, 31 insertions(+), 11 deletions(-) diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 7e42ea6f..a9a3d333 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/10 17:36:55 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 18:20:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,13 @@ struct s_job char *command; }; +extern t_data *g_data; + void job_new(t_data *data, char **av, pid_t pid); void job_announce(t_job *job); +void sigchld_handler(int signo); +void sigint_handler(int signo); +void sigtstp_handler(int signo); + #endif diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index ef76d26c..2f9aaae6 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:12:35 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:56:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,8 +76,6 @@ typedef enum e_qstate t_qstate; extern t_stof g_builtins[]; extern pid_t g_pid; -void sigint_handler(int signo); -void sigstop_handler(int signo); 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 97bc4fed..42e1a190 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 97bc4fed552dce523e2de8fb744ba46ec877f8eb +Subproject commit 42e1a190bd86ea288ee9a367fefeac8284a67cf4 diff --git a/42sh/src/job-control/job_new.c b/42sh/src/job-control/job_new.c index e9fd72b8..28142202 100644 --- a/42sh/src/job-control/job_new.c +++ b/42sh/src/job-control/job_new.c @@ -6,13 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:51:54 by jhalford #+# #+# */ -/* Updated: 2016/12/10 17:37:33 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:58:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -void job_new(t_data *data, char **av, pid_t pid, t_type type) +void job_new(t_data *data, char **av, pid_t pid) { t_job job; diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c index fd0b3e0e..5df1e708 100644 --- a/42sh/src/job-control/sigchld_handler.c +++ b/42sh/src/job-control/sigchld_handler.c @@ -6,14 +6,26 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 17:37:56 by jhalford #+# #+# */ -/* Updated: 2016/12/10 17:46:12 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 18:25:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +t_data *g_data; + void sigchld_handler(int signo) { + t_job *job; + t_list *list; + (void)signo; DG("got SIGCHLD"); + list = g_data->jobc.list; + if (list) + job = list->content; + else + job = NULL; + if (job) + DG("job pid=%i", job->pid); } diff --git a/42sh/src/job-control/sigtstp_handler.c b/42sh/src/job-control/sigtstp_handler.c index 7475ed07..95814e84 100644 --- a/42sh/src/job-control/sigtstp_handler.c +++ b/42sh/src/job-control/sigtstp_handler.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */ -/* Updated: 2016/12/10 17:46:16 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 18:20:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 9b549e58..fe10e2bb 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:35:16 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:58:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,5 +39,6 @@ int data_init(t_data *data) 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 1a8a70be..28044585 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,12 +6,14 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2016/12/10 17:16:19 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 17:58:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +t_data *g_data; + int main(void) { t_data data; @@ -20,6 +22,7 @@ int main(void) token = NULL; ast = NULL; + g_data = &data; if (data_init(&data)) return (1); DG("{inv}{bol}{gre}start of shell"); From 83bde42e02c70ee0f3c3ca777430a6b4695dab63 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 12 Dec 2016 00:43:03 +0100 Subject: [PATCH 03/27] data singleton and sigchld handler advancements --- 42sh/includes/job_control.h | 2 ++ 42sh/includes/minishell.h | 2 ++ 42sh/libft | 2 +- 42sh/script.sh | 1 + 42sh/src/job-control/ft_cmppid.c | 6 ++++++ 42sh/src/job-control/job_new.c | 1 + 42sh/src/job-control/sigchld_handler.c | 23 +++++++++++++++++------ 42sh/src/main/data_singleton.c | 10 ++++++++++ 42sh/src/main/main.c | 17 ++++++++--------- 9 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 42sh/src/job-control/ft_cmppid.c create mode 100644 42sh/src/main/data_singleton.c 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); From 0b3b3e28103cc12ca17a0a9d0e52cfed47a54d0d Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 12 Dec 2016 11:12:53 +0100 Subject: [PATCH 04/27] kill -9 message --- 42sh/src/job-control/sigchld_handler.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c index 81d260c2..e72fc7b7 100644 --- a/42sh/src/job-control/sigchld_handler.c +++ b/42sh/src/job-control/sigchld_handler.c @@ -25,18 +25,17 @@ void sigchld_handler(int signo) 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); + 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] + exit %i\t%s\n", job->id, status, job->command); + ft_printf("[%i] + %i exit %i\t%s\n", + job->id, pid, status, job->command); ft_prompt(); } - else - DG("SIGCHLD but no find"); - } From 6cfac2eaffd2b378342cc44ca950e96cd4862e48 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 12 Dec 2016 18:13:29 +0100 Subject: [PATCH 05/27] 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); + } +} From 87b9fab083df5209ac1eba83855c9084997f2210 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 12 Dec 2016 23:56:05 +0100 Subject: [PATCH 06/27] gonna add processes inside of job --- 42sh/includes/exec.h | 3 --- 42sh/includes/job_control.h | 23 +++++++++++++++++++---- 42sh/src/exec/exec_ampersand.c | 4 ++-- 42sh/src/exec/ft_cmd.c | 2 +- 42sh/src/parser/ft_parse.c | 3 ++- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index abe37ef9..cf9f0714 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -23,9 +23,6 @@ typedef long long t_type; struct s_exec { - int fdin; - int fdout; - int amp; char *aol_status; int aol_search; }; diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 2e01ebf4..44f85489 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -17,19 +17,34 @@ # include # include "libft.h" -typedef struct s_job t_job; -typedef struct s_jobc t_jobc; +typedef struct s_job t_job; +typedef struct s_process t_process; +typedef struct s_jobc t_jobc; struct s_job { int id; - pid_t pid; + pid_t pgid; + char notified; char *command; + int foreground; + t_list *first_process; +}; + +struct s_process +{ + char **argv; + pid_t pid; + int fdin; + int fdout; + char completed; + char stopped; + int status; }; struct s_jobc { - t_list *list; + t_list *first_job; pid_t shell_pgid; int current_id; int rank[2]; diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c index 4e3e48f7..41a5a3c6 100644 --- a/42sh/src/exec/exec_ampersand.c +++ b/42sh/src/exec/exec_ampersand.c @@ -14,9 +14,9 @@ int exec_ampersand(t_btree **ast) { - data_singleton()->exec.amp = 1; + data_singleton()->exec.foreground = 1; ft_exec(&(*ast)->left); - data_singleton()->exec.amp = 0; + data_singleton()->exec.foreground = 0; ft_exec(&(*ast)->right); btree_delone(ast, &ast_free); return (0); diff --git a/42sh/src/exec/ft_cmd.c b/42sh/src/exec/ft_cmd.c index 9dc9e546..cbb09dcc 100644 --- a/42sh/src/exec/ft_cmd.c +++ b/42sh/src/exec/ft_cmd.c @@ -55,7 +55,7 @@ int ft_cmd_exec(char *execpath, char **argv) { ft_strdel(&execpath); g_pid = pid; - if (data_singleton()->exec.amp) + if (data_singleton()->exec.foreground) job_new(argv, pid); else if (data_singleton()->exec.fdout == STDOUT) { diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 99565769..cd669aa6 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -14,8 +14,9 @@ t_parser g_parser[] = { - {TK_SEMI | TK_AMP, &parse_separator}, + {TK_SEMI, &parse_separator}, {TK_AND_IF | TK_OR_IF, &parse_separator}, + {TK_AMP, &parse_separator}, {TK_PIPE, &parse_separator}, {TK_LESS, &parse_less}, {TK_GREAT, &parse_great}, From 160810b3bcd49d99146e8951bb8502600e5fab21 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 13 Dec 2016 12:58:18 +0100 Subject: [PATCH 07/27] stuff i did before pulling --- 42sh/includes/exec.h | 2 +- 42sh/includes/job_control.h | 11 +++++++++-- 42sh/includes/minishell.h | 2 +- 42sh/src/exec/exec_command.c | 4 +++- 42sh/src/exec/exec_less.c | 10 +++++++--- 42sh/src/exec/exec_pipe.c | 14 +++++++------- 42sh/src/exec/fd_redirect.c | 2 +- 42sh/src/exec/ft_cmd.c | 2 +- 42sh/src/job-control/job_new.c | 2 +- 42sh/src/main/data_singleton.c | 12 ++++++++++++ 42sh/src/main/main.c | 2 +- 11 files changed, 44 insertions(+), 19 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index abe37ef9..e64a2aac 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/12 18:02:49 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 12:56:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 2e01ebf4..12b13b54 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/12 17:51:54 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 12:57:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ # include # include "libft.h" +# define TYPE_BUILTIN + typedef struct s_job t_job; typedef struct s_jobc t_jobc; @@ -24,15 +26,20 @@ struct s_job { int id; pid_t pid; + int fdin; + int fdout; char *command; + t_type type; }; struct s_jobc { - t_list *list; + t_list *first_job; pid_t shell_pgid; int current_id; int rank[2]; + t_job job; + t_process process; struct termios shell_tmodes; }; diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index d4f3887d..7cbe29cc 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/12 17:56:30 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 12:56:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 868612d2..bdc592f5 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/12 18:01:19 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 12:35:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ int exec_command(t_btree **ast) t_astnode *node; node = (*ast)->item; + ft_strappend(&data->jobc.process.command, ft_sstrcat(node->data.sstr)); + DG("gonna exec_command '%s'", data->joc.process.command); ft_cmd_process(node->data.sstr); btree_delone(ast, &ast_free); return (0); diff --git a/42sh/src/exec/exec_less.c b/42sh/src/exec/exec_less.c index 2cd7639e..fca7b1e0 100644 --- a/42sh/src/exec/exec_less.c +++ b/42sh/src/exec/exec_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */ -/* Updated: 2016/12/12 18:03:52 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 12:24:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,16 @@ int exec_less(t_btree **ast) { t_astnode *node; int fd; + t_data *data; node = (*ast)->item; fd = open(node->data.redir.word.word, O_RDONLY); - data_singleton()->exec.fdin = fd; + data_singleton()->jobc.process.fdin = fd; + ft_strappend(&data->jobc.process.command, "<"); + ft_strappend(&data->jobc.process.command, node->data.redir.word.word); ft_exec(&(*ast)->left); - data_singleton()->exec.fdin = STDIN; + data_singleton()->jobc.process.fdin = STDIN; + data->jobc.process.command = NULL; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index cac0e63e..2f558fa6 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: 2016/12/12 18:04:20 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 12:31:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,17 +20,17 @@ int exec_pipe(t_btree **ast) data = data_singleton(); pipe(fds); DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); - data->exec.fdout = fds[PIPE_WRITE]; + data->jobc.process.fdout = fds[PIPE_WRITE]; ft_exec(&(*ast)->left); - if (data->exec.fdout != STDOUT) + if (data->jobc.process.fdout != STDOUT) close(data->exec.fdout); - data->exec.fdout = STDOUT; - data->exec.fdin = fds[PIPE_READ]; + data->jobc.process.fdout = STDOUT; + data->jobc.process.fdin = fds[PIPE_READ]; ft_exec(&(*ast)->right); close(fds[PIPE_WRITE]); close(fds[PIPE_READ]); - data->exec.fdin = STDIN; - data->exec.fdout = STDOUT; + data->jobc.process.fdin = STDIN; + data->jobc.process.fdout = STDOUT; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/fd_redirect.c b/42sh/src/exec/fd_redirect.c index 5480b866..07ace92e 100644 --- a/42sh/src/exec/fd_redirect.c +++ b/42sh/src/exec/fd_redirect.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2016/12/12 17:54:45 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 12:13:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_cmd.c b/42sh/src/exec/ft_cmd.c index 9dc9e546..08e1aa91 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/12 18:11:05 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 12:56:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_new.c b/42sh/src/job-control/job_new.c index 66717f66..b01d41c3 100644 --- a/42sh/src/job-control/job_new.c +++ b/42sh/src/job-control/job_new.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:51:54 by jhalford #+# #+# */ -/* Updated: 2016/12/12 15:06:23 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 11:53:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_singleton.c b/42sh/src/main/data_singleton.c index c6139721..b5f87bbd 100644 --- a/42sh/src/main/data_singleton.c +++ b/42sh/src/main/data_singleton.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* data_singleton.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/13 11:19:51 by jhalford #+# #+# */ +/* Updated: 2016/12/13 11:52:47 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "minishell.h" t_data *data_singleton() diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index a6127a06..dd328da2 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/12 18:11:55 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 12:05:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From 03671ed8ec9c4dc0bbe3fb4b60d60ad59aea0281 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 13 Dec 2016 12:59:02 +0100 Subject: [PATCH 08/27] libft --- 42sh/libft | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/42sh/libft b/42sh/libft index 6f467979..2ba016bc 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 6f46797917052ce5d9c4e85ce7aa7d63701d919c +Subproject commit 2ba016bcad85d67ed6a18da54067eda8e3deca5b From e3b344bbb91d9ad998c96bc1fbd53d3bb0ecaa51 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 13 Dec 2016 17:59:58 +0100 Subject: [PATCH 09/27] big refactoring in progress, will finish tomorrow --- 42sh/includes/builtin.h | 26 +++++++ 42sh/includes/exec.h | 40 +++++++---- 42sh/includes/job_control.h | 56 ++++++--------- 42sh/includes/minishell.h | 11 +-- 42sh/includes/types.h | 24 +++++++ 42sh/libft | 2 +- 42sh/src/builtin/builtin_cd.c | 18 ++--- 42sh/src/builtin/builtin_echo.c | 9 +-- 42sh/src/builtin/builtin_env.c | 51 ++++++++------ 42sh/src/builtin/builtin_exit.c | 7 +- 42sh/src/builtin/{builtin.c => is_builtin.c} | 33 ++------- 42sh/src/exec/ast_free.c | 4 +- 42sh/src/exec/exec_ampersand.c | 6 +- 42sh/src/exec/exec_command.c | 14 ++-- 42sh/src/exec/exec_dgreat.c | 6 +- 42sh/src/exec/exec_great.c | 6 +- 42sh/src/exec/exec_less.c | 13 ++-- 42sh/src/exec/exec_pipe.c | 16 ++--- 42sh/src/exec/ft_cmd.c | 68 ------------------- 42sh/src/exec/ft_exec.c | 10 +-- 42sh/src/exec/launch_process.c | 52 ++++++++++++++ .../{fd_redirect.c => process_redirect.c} | 24 +++---- 42sh/src/exec/process_setexec.c | 37 ++++++++++ 42sh/src/exec/process_setgroup.c | 26 +++++++ 42sh/src/job-control/job_addprocess.c | 36 ++++++++++ 42sh/src/job-control/job_is_completed.c | 29 ++++++++ 42sh/src/job-control/job_is_stopped.c | 29 ++++++++ ...job_print_change.c => job_notify_change.c} | 0 42sh/src/job-control/job_notify_new.c | 27 ++++++++ ...job_announce.c => put_job_in_background.c} | 13 ++-- 42sh/src/job-control/put_job_in_foreground.c | 37 ++++++++++ 42sh/src/main/data_init.c | 6 +- 42sh/src/main/shell_init.c | 2 +- 33 files changed, 496 insertions(+), 242 deletions(-) create mode 100644 42sh/includes/builtin.h create mode 100644 42sh/includes/types.h rename 42sh/src/builtin/{builtin.c => is_builtin.c} (62%) delete mode 100644 42sh/src/exec/ft_cmd.c create mode 100644 42sh/src/exec/launch_process.c rename 42sh/src/exec/{fd_redirect.c => process_redirect.c} (66%) create mode 100644 42sh/src/exec/process_setexec.c create mode 100644 42sh/src/exec/process_setgroup.c create mode 100644 42sh/src/job-control/job_addprocess.c create mode 100644 42sh/src/job-control/job_is_completed.c create mode 100644 42sh/src/job-control/job_is_stopped.c rename 42sh/src/job-control/{job_print_change.c => job_notify_change.c} (100%) create mode 100644 42sh/src/job-control/job_notify_new.c rename 42sh/src/job-control/{job_announce.c => put_job_in_background.c} (66%) create mode 100644 42sh/src/job-control/put_job_in_foreground.c diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h new file mode 100644 index 00000000..44e7f81c --- /dev/null +++ b/42sh/includes/builtin.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */ +/* Updated: 2016/12/13 17:58:12 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUILTIN_H +# define BUILTIN_H + +# 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[]); +#endif diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 55d3ffb5..d3190f9d 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 12:56:42 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:50:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,25 +17,38 @@ # define PIPE_WRITE 1 # include "libft.h" +# include "types.h" +# include "job_control.h" -typedef struct s_execfunc t_execfunc; -typedef long long t_type; +struct s_process +{ + char **argv; + char *path; + t_execf *execf; + pid_t pid; + int fdin; + int fdout; + int status; + t_flag attributes; +}; struct s_exec { - char *aol_status; - int aol_search; + char *aol_status; + int aol_search; + t_job job; + t_process process; }; -struct s_execfunc +struct s_execmap { t_type type; int (*f)(t_btree **ast); }; -# include "minishell.h" +#include "minishell.h" -extern t_execfunc g_execfunc[]; +extern t_execmap g_execmap[]; int ft_exec(t_btree **ast); @@ -50,15 +63,18 @@ int exec_great(t_btree **ast); int exec_dgreat(t_btree **ast); int exec_command(t_btree **ast); +int launch_process(t_process *p); +int process_setexec(t_process *p); +int process_setgroup(t_process *p); +int process_redirect(t_process *p); + void fd_redirect(void); void fd_reset(void); -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(int status); +void ast_free(void *data, size_t content_size); + #endif diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index cfd60034..72f00a21 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 13:00:19 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:50:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,32 +16,24 @@ # include # include # include "libft.h" +# include "types.h" +# include "exec.h" -# define TYPE_BUILTIN - -typedef struct s_job t_job; -typedef struct s_process t_process; -typedef struct s_jobc t_jobc; +# define PROCESS_COMPLETED 1 << 0 +# define PROCESS_STOPED 1 << 1 +# define PROCESS_BUILTIN 1 << 2 +# define PROCESS_BINARY 1 << 3 +# define PROCESS_SCRIPT 1 << 4 +# define PROCESS_UNKNOWN 1 << 5 struct s_job { - int id; - pid_t pgid; - char notified; - char *command; - int foreground; - t_list *first_process; -}; - -struct s_process -{ - char **argv; - pid_t pid; - int fdin; - int fdout; - char completed; - char stopped; - int status; + int id; + pid_t pgid; + char notified; + int foreground; + t_list *first_process; + struct termios tmodes; }; struct s_jobc @@ -50,24 +42,20 @@ struct s_jobc pid_t shell_pgid; int current_id; int rank[2]; - t_job job; - t_process process; struct termios shell_tmodes; }; -# include "minishell.h" - -extern t_data *g_data; - -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); +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 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); + int check_chlds(void); void sigchld_handler(int signo); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 7cbe29cc..70f6a7e6 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/13 12:56:56 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:51:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,10 +16,12 @@ # include "libft.h" +# include "types.h" # include "line_editing.h" # include "lexer.h" # include "parser.h" # include "exec.h" +# include "builtin.h" # include "job_control.h" # include @@ -77,13 +79,6 @@ void shell_exit(void); int data_init(void); void data_exit(void); -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); -int builtin_setenv(char **av, t_data *data); -int builtin_unsetenv(char **av, t_data *data); -int builtin_env(char **av, t_data *data); void ft_expand_dollar(char **av, char **env); char *ft_findexec(char *path, char *file); diff --git a/42sh/includes/types.h b/42sh/includes/types.h new file mode 100644 index 00000000..1c3e6c1e --- /dev/null +++ b/42sh/includes/types.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ +/* Updated: 2016/12/13 17:51:11 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TYPES_H +# define TYPES_H + +typedef struct s_job t_job; +typedef struct s_jobc t_jobc; +typedef struct s_execmap t_execmap; +typedef struct s_process t_process; +typedef long long t_type; +typedef long long t_flag; +typedef int (t_execf)(const char *path, char *const argv[], char *const envp[]); + +#endif diff --git a/42sh/libft b/42sh/libft index 2ba016bc..c4890729 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 2ba016bcad85d67ed6a18da54067eda8e3deca5b +Subproject commit c4890729647c61fbe8f4fb627d0fcc098d224e54 diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index d42e6309..67544da9 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */ -/* Updated: 2016/12/03 11:58:14 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:56:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,23 +17,23 @@ #define HAS_CDOPT_L(x) (x & CD_OPT_L) #define CDERR_1 "cd: no such file or directory: %s\n" -static char *builtin_cd_special(char **av, char **env) +static char *builtin_cd_special(char *const av[], char *const env[]) { char *target; if (!*av) { - if (!(target = ft_getenv(env, "HOME"))) + if (!(target = ft_getenv((char**)env, "HOME"))) return (NULL); } else if (ft_strcmp(*av, "-") == 0) - target = ft_getenv(env, "OLDPWD"); + target = ft_getenv((char**)env, "OLDPWD"); else target = *av; return (target); } -static int builtin_cd_opts(char **av, int *opts) +static int builtin_cd_opts(char *const av[], int *opts) { int i; int j; @@ -60,7 +60,7 @@ static int builtin_cd_opts(char **av, int *opts) return (i); } -int builtin_cd(char **av, t_data *data) +int builtin_cd(const char *path, char *const av[], char *const envp[]) { int i; int opts; @@ -68,9 +68,9 @@ int builtin_cd(char **av, t_data *data) opts = 0; i = builtin_cd_opts(av, &opts); - if (!(target = builtin_cd_special(av + i, data->env))) + if (!(target = builtin_cd_special(av + i, envp))) return (0); - builtin_setenv((char*[3]){"OLDPWD", getcwd(NULL, 0)}, data); + builtin_setenv(path, (char*[3]){"OLDPWD", getcwd(NULL, 0)}, envp); if (chdir(target)) { ft_printf(CDERR_1, target); @@ -78,6 +78,6 @@ int builtin_cd(char **av, t_data *data) } else if (target != av[i]) ft_printf("%s\n", target); - builtin_setenv((char*[3]){"PWD", getcwd(NULL, 0)}, data); + builtin_setenv(path, (char*[3]){"PWD", getcwd(NULL, 0)}, envp); return (0); } diff --git a/42sh/src/builtin/builtin_echo.c b/42sh/src/builtin/builtin_echo.c index f559ebea..d044d7cf 100644 --- a/42sh/src/builtin/builtin_echo.c +++ b/42sh/src/builtin/builtin_echo.c @@ -6,15 +6,16 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:21:41 by jhalford #+# #+# */ -/* Updated: 2016/11/28 14:22:02 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:58:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "builtin.h" -int builtin_echo(char **av, t_data *data) +int builtin_echo(const char *path, char *const av[], char *const envp[]) { - (void)data; + (void)envp; + (void)path; av++; while (*av) { diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index e82816a2..f6272abb 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,33 +6,40 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ -/* Updated: 2016/12/12 18:03:37 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:56:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int builtin_env(char **av, t_data *data) +int builtin_env(const char *path, char *const argv[], char *const envp[]) { - int i; - char **env; - - i = 1; - env = NULL; - if (!av[1]) - { - ft_sstrprint(data->env, '\n'); - ft_putchar('\n'); - } - else - { - while (av[i] && ft_strchr(av[i], '=')) - { - env = ft_sstradd(env, av[i]); - i++; - } - if (av[i]) - ft_cmd_process(av + i); - } + (void)argv; + (void)envp; + (void)path; return (0); } +/* int builtin_env(char **av, t_data *data) */ +/* { */ +/* int i; */ +/* char **env; */ + +/* i = 1; */ +/* env = NULL; */ +/* if (!av[1]) */ +/* { */ +/* ft_sstrprint(data->env, '\n'); */ +/* ft_putchar('\n'); */ +/* } */ +/* else */ +/* { */ +/* while (av[i] && ft_strchr(av[i], '=')) */ +/* { */ +/* env = ft_sstradd(env, av[i]); */ +/* i++; */ +/* } */ +/* if (av[i]) */ +/* ft_cmd_process(av + i); */ +/* } */ +/* return (0); */ +/* } */ diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index 25238b9f..e0e250fe 100644 --- a/42sh/src/builtin/builtin_exit.c +++ b/42sh/src/builtin/builtin_exit.c @@ -6,20 +6,21 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */ -/* Updated: 2016/12/01 14:35:36 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:59:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int builtin_exit(char **av, t_data *data) +int builtin_exit(const char *path, char *const av[], char *const envp[]) { int status; + (void)path; if (av[1]) status = ft_atoi(av[1]); else - status = ft_atoi(ft_getenv(data->env, "?")); + status = ft_atoi(ft_getenv((char**)envp, "?")); exit(status); return (0); } diff --git a/42sh/src/builtin/builtin.c b/42sh/src/builtin/is_builtin.c similarity index 62% rename from 42sh/src/builtin/builtin.c rename to 42sh/src/builtin/is_builtin.c index 49fcc9e9..e6796bc9 100644 --- a/42sh/src/builtin/builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* builtin.c :+: :+: :+: */ +/* is_builtin.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/28 14:21:34 by jhalford #+# #+# */ -/* Updated: 2016/12/12 17:56:11 by jhalford ### ########.fr */ +/* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ +/* Updated: 2016/12/13 17:31:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,32 +22,13 @@ t_stof g_builtin[] = { {NULL, NULL}, }; -int ft_builtin(char **av) +t_execf *is_builtin(t_process *p) { 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) - { - if (data->exec.fdout != STDOUT) - { - if (fork() == 0) - { - fd_redirect(); - ret = (g_builtin[i].f)(av, data); - exit(ret); - } - } - else - { - ret = (g_builtin[i].f)(av, data); - set_exitstatus(ret); - } - return (1); - } - return (0); + if (ft_strcmp(g_builtin[i].name, p->argv[0]) == 0) + return (g_builtin[i].f); + return (NULL); } diff --git a/42sh/src/exec/ast_free.c b/42sh/src/exec/ast_free.c index f7b0943a..db9ce943 100644 --- a/42sh/src/exec/ast_free.c +++ b/42sh/src/exec/ast_free.c @@ -6,11 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */ -/* Updated: 2016/12/09 21:28:29 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:17:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "exec.h" void ast_free(void *data, size_t content_size) { diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c index 41a5a3c6..dae89443 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/12 18:03:03 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:19:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,9 @@ int exec_ampersand(t_btree **ast) { - data_singleton()->exec.foreground = 1; + data_singleton()->exec.job.foreground = 1; ft_exec(&(*ast)->left); - data_singleton()->exec.foreground = 0; + data_singleton()->exec.job.foreground = 0; ft_exec(&(*ast)->right); btree_delone(ast, &ast_free); return (0); diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index bdc592f5..34539c85 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 12:35:12 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:41:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,17 @@ int exec_command(t_btree **ast) { t_astnode *node; + t_process *process; + t_job *job; node = (*ast)->item; - ft_strappend(&data->jobc.process.command, ft_sstrcat(node->data.sstr)); - DG("gonna exec_command '%s'", data->joc.process.command); - ft_cmd_process(node->data.sstr); + process = &data_singleton()->exec.process; + job = &data_singleton()->exec.job; + process->argv = ft_sstrdup(node->data.sstr); + DG("gonna launch_process"); + process_setexec(process); + launch_process(process); + job_addprocess(process); btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_dgreat.c b/42sh/src/exec/exec_dgreat.c index a2471e98..9897f54d 100644 --- a/42sh/src/exec/exec_dgreat.c +++ b/42sh/src/exec/exec_dgreat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:15:13 by jhalford #+# #+# */ -/* Updated: 2016/12/12 18:03:11 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:13:58 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,9 @@ int exec_dgreat(t_btree **ast) node = (*ast)->item; fd = open(node->data.redir.word.word, O_WRONLY | O_APPEND | O_CREAT, 0644); - data_singleton()->exec.fdout = fd; + data_singleton()->exec.process.fdout = fd; ft_exec(&(*ast)->left); - data_singleton()->exec.fdout = STDOUT; + data_singleton()->exec.process.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 409a54af..6b410e7b 100644 --- a/42sh/src/exec/exec_great.c +++ b/42sh/src/exec/exec_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */ -/* Updated: 2016/12/12 18:03:23 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:14:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,9 @@ int exec_great(t_btree **ast) node = (*ast)->item; fd = open(node->data.redir.word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); - data_singleton()->exec.fdout = fd; + data_singleton()->exec.process.fdout = fd; ft_exec(&(*ast)->left); - data_singleton()->exec.fdout = STDOUT; + data_singleton()->exec.process.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 7fe46662..f27be538 100644 --- a/42sh/src/exec/exec_less.c +++ b/42sh/src/exec/exec_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */ -/* Updated: 2016/12/13 13:00:20 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:14:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,16 +16,15 @@ int exec_less(t_btree **ast) { t_astnode *node; int fd; - t_data *data; node = (*ast)->item; fd = open(node->data.redir.word.word, O_RDONLY); - data_singleton()->jobc.process.fdin = fd; - ft_strappend(&data->jobc.process.command, "<"); - ft_strappend(&data->jobc.process.command, node->data.redir.word.word); + data_singleton()->exec.process.fdin = fd; + /* ft_strappend(&data->exec.process.command, "<"); */ + /* ft_strappend(&data->exec.process.command, node->data.redir.word.word); */ ft_exec(&(*ast)->left); - data_singleton()->jobc.process.fdin = STDIN; - data->jobc.process.command = NULL; + data_singleton()->exec.process.fdin = STDIN; + /* data->exec.process.command = NULL; */ btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 2f558fa6..7b9025af 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: 2016/12/13 12:31:15 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:15:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,17 +20,17 @@ int exec_pipe(t_btree **ast) data = data_singleton(); pipe(fds); DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); - data->jobc.process.fdout = fds[PIPE_WRITE]; + data->exec.process.fdout = fds[PIPE_WRITE]; ft_exec(&(*ast)->left); - if (data->jobc.process.fdout != STDOUT) - close(data->exec.fdout); - data->jobc.process.fdout = STDOUT; - data->jobc.process.fdin = fds[PIPE_READ]; + if (data->exec.process.fdout != STDOUT) + close(data->exec.process.fdout); + data->exec.process.fdout = STDOUT; + data->exec.process.fdin = fds[PIPE_READ]; ft_exec(&(*ast)->right); close(fds[PIPE_WRITE]); close(fds[PIPE_READ]); - data->jobc.process.fdin = STDIN; - data->jobc.process.fdout = STDOUT; + data->exec.process.fdin = STDIN; + data->exec.process.fdout = STDOUT; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/ft_cmd.c b/42sh/src/exec/ft_cmd.c deleted file mode 100644 index 3869fb32..00000000 --- a/42sh/src/exec/ft_cmd.c +++ /dev/null @@ -1,68 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_cmd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/27 21:13:18 by jhalford #+# #+# */ -/* Updated: 2016/12/13 12:56:18 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -extern pid_t g_pid; - -int ft_cmd_process(char **argv) -{ - char *execpath; - - 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_singleton()->env, "PATH"), argv[0]))) - { - ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, argv[0]); - set_exitstatus(127); - return (-1); - } - return (ft_cmd_exec(execpath, argv)); -} - -int ft_cmd_exec(char *execpath, char **argv) -{ - pid_t pid; - int status; - - if (access(execpath, X_OK) == -1) - { - ft_dprintf(2, "%s: permission denied: %s\n", SHELL_NAME, argv[0]); - ft_strdel(&execpath); - return (-1); - } - if ((pid = fork()) == -1) - return (-1); - else if (pid == 0) - { - fd_redirect(); - execve(execpath, argv, data_singleton()->env); - exit(42); - } - else - { - ft_strdel(&execpath); - g_pid = pid; - if (data_singleton()->exec.foreground) - job_new(argv, pid); - else if (data_singleton()->exec.fdout == STDOUT) - { - waitpid(pid, &status, 0); - set_exitstatus(status); - } - g_pid = 0; - } - return (0); -} diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index c809d017..dcfc2dd2 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,13 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2016/12/12 18:11:48 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:40:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -t_execfunc g_execfunc[] = +t_execmap g_execmap[] = { {TK_AND_IF, &exec_and_if}, {TK_OR_IF, &exec_or_if}, @@ -35,10 +35,10 @@ int ft_exec(t_btree **ast) if (!*ast) return (0); item = (*ast)->item; - while (g_execfunc[i].type) + while (g_execmap[i].type) { - if (item->type == g_execfunc[i].type) - return ((*g_execfunc[i].f)(ast)); + if (item->type == g_execmap[i].type) + return ((*g_execmap[i].f)(ast)); i++; } return (0); diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c new file mode 100644 index 00000000..d2bd124a --- /dev/null +++ b/42sh/src/exec/launch_process.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* launch_process.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ +/* Updated: 2016/12/13 17:50:38 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int launch_process(t_process *p) +{ + t_exec *exec; + int pid; + + exec = &data_singleton()->exec; + if (p->attributes & PROCESS_UNKNOWN) + ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, p->argv[0]); + if (p->attributes & PROCESS_BUILTIN && p->fdout != STDOUT) + set_exitstatus((*p->execf)(p->path, p->argv, data_singleton()->env)); + else + { + if (p->attributes & (PROCESS_BINARY | PROCESS_SCRIPT) + && access(p->path, X_OK) == -1) + { + ft_dprintf(2, "%s: permission denied: %s\n", SHELL_NAME, p->argv[0]); + return (-1); + } + pid = fork(); + if (pid == 0) + { + process_setgroup(p); + process_redirect(p); + (*p->execf)(p->path, p->argv, data_singleton()->env); + exit(42); + } + else if (pid > 0) + 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/fd_redirect.c b/42sh/src/exec/process_redirect.c similarity index 66% rename from 42sh/src/exec/fd_redirect.c rename to 42sh/src/exec/process_redirect.c index 07ace92e..51e1ed1c 100644 --- a/42sh/src/exec/fd_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -1,30 +1,28 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_redirect.c :+: :+: :+: */ +/* process_redirect.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2016/12/13 12:13:24 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 17:49:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "exec.h" -void fd_redirect(void) +int process_redirect(t_process *p) { - t_data *data; - - data = data_singleton(); - if (data->exec.fdin != STDIN) + if (p->fdin != STDIN) { - dup2(data->exec.fdin, STDIN); - close(data->exec.fdin); + dup2(p->fdin, STDIN); + close(p->fdin); } - if (data->exec.fdout != STDOUT) + if (p->fdout != STDOUT) { - dup2(data->exec.fdout, STDOUT); - close(data->exec.fdout); + dup2(p->fdout, STDOUT); + close(p->fdout); } + return (0); } diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c new file mode 100644 index 00000000..aa7a5b2f --- /dev/null +++ b/42sh/src/exec/process_setexec.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* process_setexec.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */ +/* Updated: 2016/12/13 17:50:26 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int process_setexec(t_process *p) +{ + if ((p->execf = is_builtin(p))) + p->attributes &= PROCESS_BUILTIN; + else if (ft_strchr(p->argv[0], '/')) + { + p->execf = &execve; + p->attributes &= PROCESS_SCRIPT; + p->path = ft_strdup(p->argv[0]); + } + else if (!(p->path = ft_findexec(ft_getenv( + data_singleton()->env, "PATH"), p->argv[0]))) + { + p->execf = &execve; + p->attributes &= PROCESS_BINARY; + } + else + { + p->execf = NULL; + p->attributes &= PROCESS_UNKNOWN; + } + return (0); +} diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c new file mode 100644 index 00000000..6a37b49b --- /dev/null +++ b/42sh/src/exec/process_setgroup.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* process_setgroup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/13 17:48:10 by jhalford #+# #+# */ +/* Updated: 2016/12/13 17:48:11 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +int process_setgroup(t_process *p) +{ + t_job *job; + + job = data_singleton()->exec.job; + pid = getpid(); + if (job->pgid == 0) + job->pgid = pid; + setpgid(pid, job->pgid); + if (job->foreground) + tcsetpgrp(STDIN_FILENO, job->pgid); +} diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c new file mode 100644 index 00000000..b2f0f18a --- /dev/null +++ b/42sh/src/job-control/job_addprocess.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_addprocess.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/13 13:54:51 by jhalford #+# #+# */ +/* Updated: 2016/12/13 17:06:57 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +int job_addprocess(t_process *p) +{ + t_jobc *jobc; + t_job *job; + + jobc = &data_singleton()->jobc; + job = &data_singleton()->exec.job; + if (p->fdin == STDIN) + { + job->id = current_id; + job_update_id(); + ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); + if (job->foreground) + put_job_in_foreground(job, 0); + else + put_job_in_background(job, 0); + } + if (p->fdout = STDOUT) + job_notify_new(first_job); + first_job = jobc->first_job->content; + ft_lstadd(first_job->process, ft_lstnew(p, sizeof(*p))); +} diff --git a/42sh/src/job-control/job_is_completed.c b/42sh/src/job-control/job_is_completed.c new file mode 100644 index 00000000..1c35e359 --- /dev/null +++ b/42sh/src/job-control/job_is_completed.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_is_completed.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/13 15:10:20 by jhalford #+# #+# */ +/* Updated: 2016/12/13 15:24:25 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +int job_is_completed(t_job *job) +{ + t_list *lst; + t_process *process; + + lst = job->lst; + while (lst) + { + process = lst->content; + if (!(process->attributes & PROCESS_COMPLETED)) + return (0); + lst = lst->next; + } + return (1); +} diff --git a/42sh/src/job-control/job_is_stopped.c b/42sh/src/job-control/job_is_stopped.c new file mode 100644 index 00000000..43499435 --- /dev/null +++ b/42sh/src/job-control/job_is_stopped.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_is_stopped.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/13 15:06:45 by jhalford #+# #+# */ +/* Updated: 2016/12/13 15:24:26 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +int job_is_stopped(t_job *job) +{ + t_list *lst; + t_process *process; + + lst = job->lst; + while (lst) + { + process = lst->content; + if (!(process->attributes & (PROCESS_COMPLETED | PROCESS_STOPPED))) + return (0); + lst = lst->next; + } + return (1); +} diff --git a/42sh/src/job-control/job_print_change.c b/42sh/src/job-control/job_notify_change.c similarity index 100% rename from 42sh/src/job-control/job_print_change.c rename to 42sh/src/job-control/job_notify_change.c diff --git a/42sh/src/job-control/job_notify_new.c b/42sh/src/job-control/job_notify_new.c new file mode 100644 index 00000000..180ea477 --- /dev/null +++ b/42sh/src/job-control/job_notify_new.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_notify_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/13 14:27:01 by jhalford #+# #+# */ +/* Updated: 2016/12/13 14:58:23 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void job_notify_new(t_job *job) +{ + t_list *process; + + process = job->first_process; + ft_printf("{mag}[%i]", job->id); + while (process) + { + ft_printf(" %i", ((t_process*)process->content)->pid); + process=process->next; + } + ft_printf("{eoc}\n"); +} diff --git a/42sh/src/job-control/job_announce.c b/42sh/src/job-control/put_job_in_background.c similarity index 66% rename from 42sh/src/job-control/job_announce.c rename to 42sh/src/job-control/put_job_in_background.c index 64b6745c..e4be8323 100644 --- a/42sh/src/job-control/job_announce.c +++ b/42sh/src/job-control/put_job_in_background.c @@ -1,18 +1,21 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* job_announce.c :+: :+: :+: */ +/* put_job_in_background.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/10 17:05:49 by jhalford #+# #+# */ -/* Updated: 2016/12/12 12:54:09 by jhalford ### ########.fr */ +/* Created: 2016/12/13 15:03:29 by jhalford #+# #+# */ +/* Updated: 2016/12/13 15:04:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -void job_announce(t_job *job) +int put_job_in_background(t_job *job, int cont) { - ft_printf("{mag}[%i] %i{eoc}\n", job->id, job->pid); + /* Send the job a continue signal, if necessary. */ + if (cont) + if (kill (-j->pgid, SIGCONT) < 0) + perror ("kill (SIGCONT)"); } diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c new file mode 100644 index 00000000..5abc8e4d --- /dev/null +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* put_job_in_foreground.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/13 14:58:36 by jhalford #+# #+# */ +/* Updated: 2016/12/13 15:06:21 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +int put_job_in_foreground(t_job *job, int cont) +{ + t_jobc *jobc; + + /* Put the job into the foreground. */ + tcsetpgrp(shell_terminal, job->pgid); + /* Send the job a continue signal, if necessary. */ + if (cont) + { + tcsetattr (shell_terminal, TCSADRAIN, &job->tmodes); + if (kill(- job->pgid, SIGCONT) < 0) + perror("kill (SIGCONT)"); + } + /* Wait for it to report. */ + wait_for_job(j); + + /* Put the shell back in the foreground. */ + tcsetpgrp(shell_terminal, jobc->shell_pgid); + + /* Restore the shell’s terminal modes. */ + tcgetattr(shell_terminal, &job->tmodes); + tcsetattr(shell_terminal, TCSADRAIN, &jobc->shell_tmodes); +} diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 1c35b6f0..f3ba5985 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/12 17:50:03 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 13:38:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,10 @@ int data_init(void) data->jobc.current_id = 1; data->jobc.rank[0] = 0; data->jobc.rank[1] = 0; + data->jobc.job.id = 0; + data->jobc.job.pgid = 0; + data->jobc.job.notified = 0; + data->jobc.job.foreground = 0; if (!(data->line.history = ft_dlstnew(NULL, 0))) return (-1); if ((term_name = ft_getenv(data->env, "TERM")) == NULL) diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 52552fb4..b3f974c0 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: 2016/12/12 18:11:50 by jhalford ### ########.fr */ +/* Updated: 2016/12/13 15:14:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From fc53e40a4fd71cd1a800904c137bd0197d4653a8 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Wed, 14 Dec 2016 23:26:29 +0100 Subject: [PATCH 10/27] mostly compile-time error fixing --- 42sh/includes/exec.h | 1 + 42sh/includes/job_control.h | 43 +++++++++++++------ 42sh/includes/minishell.h | 11 ----- 42sh/includes/types.h | 12 ++++++ 42sh/src/builtin/builtin_setenv.c | 24 ++++++----- 42sh/src/builtin/builtin_unsetenv.c | 14 +++--- 42sh/src/exec/exec_ampersand.c | 4 +- 42sh/src/exec/exec_command.c | 20 ++++++--- 42sh/src/exec/launch_process.c | 2 +- 42sh/src/exec/process_setexec.c | 8 +++- 42sh/src/exec/process_setgroup.c | 8 +++- 42sh/src/exec/set_exitstatus.c | 2 +- 42sh/src/job-control/check_chlds.c | 40 ++++++++--------- 42sh/src/job-control/job_addprocess.c | 19 ++++---- 42sh/src/job-control/job_free.c | 2 +- 42sh/src/job-control/job_getprocess.c | 20 +++++++++ 42sh/src/job-control/job_is_completed.c | 8 ++-- 42sh/src/job-control/job_is_stopped.c | 8 ++-- 42sh/src/job-control/job_new.c | 36 ---------------- 42sh/src/job-control/job_notify_change.c | 8 ++-- 42sh/src/job-control/job_notify_new.c | 2 +- 42sh/src/job-control/job_update_id.c | 2 +- 42sh/src/job-control/job_update_rank.c | 2 +- 42sh/src/job-control/job_wait.c | 7 +++ .../{job_cmppid.c => process_cmp_pid.c} | 6 +-- 42sh/src/job-control/process_free.c | 24 +++++++++++ 42sh/src/job-control/process_mark_status.c | 42 ++++++++++++++++++ 42sh/src/job-control/put_job_in_background.c | 3 +- 42sh/src/job-control/put_job_in_foreground.c | 14 +++--- 42sh/src/job-control/sigchld_handler.c | 10 +---- 42sh/src/line-editing/ft_prompt.c | 8 ---- 42sh/src/main/data_init.c | 16 +++---- 42sh/src/main/main.c | 1 + 42sh/src/main/shell_init.c | 2 +- 34 files changed, 257 insertions(+), 172 deletions(-) create mode 100644 42sh/src/job-control/job_getprocess.c delete mode 100644 42sh/src/job-control/job_new.c create mode 100644 42sh/src/job-control/job_wait.c rename 42sh/src/job-control/{job_cmppid.c => process_cmp_pid.c} (86%) create mode 100644 42sh/src/job-control/process_free.c create mode 100644 42sh/src/job-control/process_mark_status.c diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index d3190f9d..c4dca29a 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -67,6 +67,7 @@ int launch_process(t_process *p); int process_setexec(t_process *p); int process_setgroup(t_process *p); int process_redirect(t_process *p); +void process_free(void *content, size_t content_size); void fd_redirect(void); void fd_reset(void); diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 72f00a21..b0ab7403 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -14,24 +14,29 @@ # define JOB_CONTROL_H # include +# include # include # include "libft.h" # include "types.h" -# include "exec.h" -# define PROCESS_COMPLETED 1 << 0 -# define PROCESS_STOPED 1 << 1 -# define PROCESS_BUILTIN 1 << 2 -# define PROCESS_BINARY 1 << 3 -# define PROCESS_SCRIPT 1 << 4 -# define PROCESS_UNKNOWN 1 << 5 +# 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 JOB_NOTIFIED (1 << 0) +# define JOB_BG (1 << 1) +# define JOB_IS_BG(j) (j & JOB_BG) +# define JOB_IS_FG(j) !(j & JOB_BG) + struct s_job { int id; pid_t pgid; - char notified; - int foreground; + t_flag attributes; t_list *first_process; struct termios tmodes; }; @@ -45,12 +50,20 @@ struct s_jobc struct termios shell_tmodes; }; -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); +# include "exec.h" -void job_new(char **av, pid_t pid); +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 job_notify_new(t_job *job); +int job_wait(t_job *job); + +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); @@ -62,4 +75,6 @@ void sigchld_handler(int signo); void sigint_handler(int signo); void sigtstp_handler(int signo); +int process_cmp_pid(t_process *p, pid_t *pid); + #endif diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 70f6a7e6..e112ae6d 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -30,12 +30,6 @@ # include # include -typedef long long t_type; -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 { @@ -66,20 +60,15 @@ struct s_data t_jobc jobc; }; -typedef struct s_data t_data; -typedef enum e_qstate t_qstate; extern t_stof g_builtins[]; extern pid_t g_pid; -t_data *data_singleton(); - void shell_init(void); void shell_exit(void); int data_init(void); void data_exit(void); - void ft_expand_dollar(char **av, char **env); char *ft_findexec(char *path, char *file); diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 1c3e6c1e..d3a6fed0 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -13,6 +13,16 @@ #ifndef TYPES_H # define TYPES_H +typedef long long t_type; +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; + +typedef struct s_data t_data; +typedef enum e_qstate t_qstate; + typedef struct s_job t_job; typedef struct s_jobc t_jobc; typedef struct s_execmap t_execmap; @@ -21,4 +31,6 @@ typedef long long t_type; typedef long long t_flag; typedef int (t_execf)(const char *path, char *const argv[], char *const envp[]); +t_data *data_singleton(); + #endif diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 0d547745..fc6ec20a 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -12,34 +12,38 @@ #include "minishell.h" -int builtin_setenv(char **av, t_data *data) +int builtin_setenv(const char *path, char *const av[], char *const envp[]) { char *str; - char **env; + char ***env; + int i; - env = data->env; + (void)envp; + (void)path; + i = 0; + env = &data_singleton()->env; DG("doing setenv now"); if (ft_strcmp(av[0], "setenv") == 0) av++; if (!av[0]) { - ft_sstrprint(data->env, '\n'); + ft_sstrprint(*env, '\n'); ft_putchar('\n'); } else { str = ft_str3join(av[0], "=", av[1]); - while (*env) + while ((*env)[i]) { - if (ft_strcmp(*env, av[0]) == '=') + if (ft_strcmp((*env)[i], av[0]) == '=') { - ft_strdel(env); - *env = str; + ft_strdel(*env); + (*env)[i] = str; return (0); } - env++; + i++; } - data->env = ft_sstradd(data->env, str); + *env = ft_sstradd(*env, str); ft_strdel(&str); } return (0); diff --git a/42sh/src/builtin/builtin_unsetenv.c b/42sh/src/builtin/builtin_unsetenv.c index 93104cda..d684462a 100644 --- a/42sh/src/builtin/builtin_unsetenv.c +++ b/42sh/src/builtin/builtin_unsetenv.c @@ -12,21 +12,23 @@ #include "minishell.h" -int builtin_unsetenv(char **av, t_data *data) +int builtin_unsetenv(const char *path, char *const av[], char *const envp[]) { - char **env; int i; int j; + char ***env; - env = data->env; + (void)envp; + (void)path; + env = &data_singleton()->env; i = 1; while (av[i]) { j = 0; - while (env[j]) + while ((*env)[j]) { - if (ft_strcmp(env[j], av[i]) == '=') - ft_sstrdel(env, j); + if (ft_strcmp((*env)[j], av[i]) == '=') + ft_sstrdel(*env, j); else j++; } diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c index dae89443..3a93b16c 100644 --- a/42sh/src/exec/exec_ampersand.c +++ b/42sh/src/exec/exec_ampersand.c @@ -14,9 +14,9 @@ int exec_ampersand(t_btree **ast) { - data_singleton()->exec.job.foreground = 1; + data_singleton()->exec.job.attributes |= JOB_BG; ft_exec(&(*ast)->left); - data_singleton()->exec.job.foreground = 0; + data_singleton()->exec.job.attributes &= ~JOB_BG; ft_exec(&(*ast)->right); btree_delone(ast, &ast_free); return (0); diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 34539c85..b1ea3584 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -15,17 +15,25 @@ int exec_command(t_btree **ast) { t_astnode *node; - t_process *process; + t_process *p; t_job *job; node = (*ast)->item; - process = &data_singleton()->exec.process; + p = &data_singleton()->exec.process; job = &data_singleton()->exec.job; - process->argv = ft_sstrdup(node->data.sstr); + p->argv = ft_sstrdup(node->data.sstr); DG("gonna launch_process"); - process_setexec(process); - launch_process(process); - job_addprocess(process); + DG("job attr=%i", job->attributes); + process_setexec(p); + launch_process(p); + if (p->fdout == STDOUT) + { + if (JOB_IS_FG(job->attributes)) + put_job_in_foreground(job, 0); + else + put_job_in_background(job, 0); + } + job_addprocess(p); btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index d2bd124a..ecfc451d 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -20,7 +20,7 @@ int launch_process(t_process *p) exec = &data_singleton()->exec; if (p->attributes & PROCESS_UNKNOWN) ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, p->argv[0]); - if (p->attributes & PROCESS_BUILTIN && p->fdout != STDOUT) + else if (p->attributes & PROCESS_BUILTIN && p->fdout != STDOUT) set_exitstatus((*p->execf)(p->path, p->argv, data_singleton()->env)); else { diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index aa7a5b2f..8148510c 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -15,21 +15,27 @@ int process_setexec(t_process *p) { if ((p->execf = is_builtin(p))) + { + DG("process is a builtin"); p->attributes &= PROCESS_BUILTIN; + } else if (ft_strchr(p->argv[0], '/')) { + DG("process is a script"); p->execf = &execve; p->attributes &= PROCESS_SCRIPT; p->path = ft_strdup(p->argv[0]); } - else if (!(p->path = ft_findexec(ft_getenv( + else if ((p->path = ft_findexec(ft_getenv( data_singleton()->env, "PATH"), p->argv[0]))) { + DG("process is binary"); p->execf = &execve; p->attributes &= PROCESS_BINARY; } else { + DG("process is unknown type"); p->execf = NULL; p->attributes &= PROCESS_UNKNOWN; } diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index 6a37b49b..eb05b38b 100644 --- a/42sh/src/exec/process_setgroup.c +++ b/42sh/src/exec/process_setgroup.c @@ -11,16 +11,20 @@ /* ************************************************************************** */ #include "job_control.h" +#include "exec.h" int process_setgroup(t_process *p) { t_job *job; + int pid; - job = data_singleton()->exec.job; + (void)p; + job = &data_singleton()->exec.job; pid = getpid(); if (job->pgid == 0) job->pgid = pid; setpgid(pid, job->pgid); - if (job->foreground) + if (JOB_IS_FG(job->attributes)) tcsetpgrp(STDIN_FILENO, job->pgid); + return (0); } diff --git a/42sh/src/exec/set_exitstatus.c b/42sh/src/exec/set_exitstatus.c index 14c7f462..01055140 100644 --- a/42sh/src/exec/set_exitstatus.c +++ b/42sh/src/exec/set_exitstatus.c @@ -17,6 +17,6 @@ void set_exitstatus(int status) char *astatus; astatus = ft_itoa(status); - builtin_setenv((char*[3]){"?", astatus}, data_singleton()); + builtin_setenv("shell", (char*[3]){"?", astatus}, data_singleton()->env); ft_strdel(&astatus); } diff --git a/42sh/src/job-control/check_chlds.c b/42sh/src/job-control/check_chlds.c index 6b767582..cadacf61 100644 --- a/42sh/src/job-control/check_chlds.c +++ b/42sh/src/job-control/check_chlds.c @@ -14,26 +14,26 @@ int check_chlds() { - int status; - pid_t pid; - t_job *job; - t_list **start; - t_list *list; + /* 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); - } + /* 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/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index b2f0f18a..c244f2bc 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/job_addprocess.c @@ -19,18 +19,17 @@ int job_addprocess(t_process *p) jobc = &data_singleton()->jobc; job = &data_singleton()->exec.job; - if (p->fdin == STDIN) + if (JOB_IS_BG(job->attributes) && p->fdin == STDIN) { - job->id = current_id; job_update_id(); + job->id = jobc->current_id; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); - if (job->foreground) - put_job_in_foreground(job, 0); - else - put_job_in_background(job, 0); + jobc->rank[1] = jobc->rank[0]; + jobc->rank[0] = job->id; } - if (p->fdout = STDOUT) - job_notify_new(first_job); - first_job = jobc->first_job->content; - ft_lstadd(first_job->process, ft_lstnew(p, sizeof(*p))); + job = jobc->first_job->content; + 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); } diff --git a/42sh/src/job-control/job_free.c b/42sh/src/job-control/job_free.c index 09a89abd..1138314e 100644 --- a/42sh/src/job-control/job_free.c +++ b/42sh/src/job-control/job_free.c @@ -18,6 +18,6 @@ void job_free(void *content, size_t content_size) (void)content_size; job = content; - ft_strdel(&job->command); + ft_lstdel(&job->first_process, &process_free); free(job); } diff --git a/42sh/src/job-control/job_getprocess.c b/42sh/src/job-control/job_getprocess.c new file mode 100644 index 00000000..a4fa75ba --- /dev/null +++ b/42sh/src/job-control/job_getprocess.c @@ -0,0 +1,20 @@ +#include "job_control.h" + +t_process *job_getprocess(pid_t pid) +{ + t_jobc *jobc; + t_job *j; + t_list *lst; + t_list *jlist; + + jobc = &data_singleton()->jobc; + jlist = jobc->first_job; + while (jlist) + { + j = jlist->content; + if ((lst = ft_lst_find(j->first_process, &pid, &process_cmp_pid))) + return (lst->content); + jlist = jlist->next; + } + return (NULL); +} diff --git a/42sh/src/job-control/job_is_completed.c b/42sh/src/job-control/job_is_completed.c index 1c35e359..b92bbd6d 100644 --- a/42sh/src/job-control/job_is_completed.c +++ b/42sh/src/job-control/job_is_completed.c @@ -15,13 +15,13 @@ int job_is_completed(t_job *job) { t_list *lst; - t_process *process; + t_process *p; - lst = job->lst; + lst = job->first_process; while (lst) { - process = lst->content; - if (!(process->attributes & PROCESS_COMPLETED)) + p = lst->content; + if (!(p->attributes & PROCESS_COMPLETED)) return (0); lst = lst->next; } diff --git a/42sh/src/job-control/job_is_stopped.c b/42sh/src/job-control/job_is_stopped.c index 43499435..8dd89f34 100644 --- a/42sh/src/job-control/job_is_stopped.c +++ b/42sh/src/job-control/job_is_stopped.c @@ -15,13 +15,13 @@ int job_is_stopped(t_job *job) { t_list *lst; - t_process *process; + t_process *p; - lst = job->lst; + lst = job->first_process; while (lst) { - process = lst->content; - if (!(process->attributes & (PROCESS_COMPLETED | PROCESS_STOPPED))) + p = lst->content; + if (!(p->attributes & (PROCESS_COMPLETED | PROCESS_STOPPED))) return (0); lst = lst->next; } diff --git a/42sh/src/job-control/job_new.c b/42sh/src/job-control/job_new.c deleted file mode 100644 index b01d41c3..00000000 --- a/42sh/src/job-control/job_new.c +++ /dev/null @@ -1,36 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* job_new.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/10 16:51:54 by jhalford #+# #+# */ -/* Updated: 2016/12/13 11:53:11 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "job_control.h" - -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_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_notify_change.c b/42sh/src/job-control/job_notify_change.c index b36e0840..028461c2 100644 --- a/42sh/src/job-control/job_notify_change.c +++ b/42sh/src/job-control/job_notify_change.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* job_print_change.c :+: :+: :+: */ +/* job_notify_change.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -12,7 +12,7 @@ #include "minishell.h" -void job_print_change(t_job *job, int status) +void job_notify_change(t_job *job, int status) { char rank; @@ -21,12 +21,12 @@ void job_print_change(t_job *job, int status) rank = '+'; else if (job->id == data_singleton()->jobc.rank[1]) rank = '-'; - ft_printf("{mag}[%i] %c %i ", job->id, rank, job->pid); + ft_printf("{mag}[%i] %c ", job->id, rank); 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); + /* ft_printf("\t%s{eoc}\n", job->command); */ } diff --git a/42sh/src/job-control/job_notify_new.c b/42sh/src/job-control/job_notify_new.c index 180ea477..0f5075b8 100644 --- a/42sh/src/job-control/job_notify_new.c +++ b/42sh/src/job-control/job_notify_new.c @@ -16,8 +16,8 @@ void job_notify_new(t_job *job) { t_list *process; - process = job->first_process; ft_printf("{mag}[%i]", job->id); + process = job->first_process; while (process) { ft_printf(" %i", ((t_process*)process->content)->pid); diff --git a/42sh/src/job-control/job_update_id.c b/42sh/src/job-control/job_update_id.c index e69ca110..68579844 100644 --- a/42sh/src/job-control/job_update_id.c +++ b/42sh/src/job-control/job_update_id.c @@ -20,7 +20,7 @@ void job_update_id(void) jobc = &data_singleton()->jobc; id = &jobc->current_id; - start = jobc->list; + start = jobc->first_job; while (ft_lst_find(start, id, job_cmp_id)) { *id += 1; diff --git a/42sh/src/job-control/job_update_rank.c b/42sh/src/job-control/job_update_rank.c index 0c8f7c38..49c8475b 100644 --- a/42sh/src/job-control/job_update_rank.c +++ b/42sh/src/job-control/job_update_rank.c @@ -19,7 +19,7 @@ void job_update_rank() t_list *list; jobc = &data_singleton()->jobc; - list = jobc->list; + list = jobc->first_job; if (list) { job = list->content; diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c new file mode 100644 index 00000000..4c56f65a --- /dev/null +++ b/42sh/src/job-control/job_wait.c @@ -0,0 +1,7 @@ +#include "job_control.h" + +int job_wait(t_job *job) +{ + (void)job; + return (0); +} diff --git a/42sh/src/job-control/job_cmppid.c b/42sh/src/job-control/process_cmp_pid.c similarity index 86% rename from 42sh/src/job-control/job_cmppid.c rename to 42sh/src/job-control/process_cmp_pid.c index 0f11c30a..d0b3b09c 100644 --- a/42sh/src/job-control/job_cmppid.c +++ b/42sh/src/job-control/process_cmp_pid.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* job_cmp_pid.c :+: :+: :+: */ +/* process_cmp_pid.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -12,7 +12,7 @@ #include "minishell.h" -int job_cmp_pid(t_job *job, pid_t *pid) +int process_cmp_pid(t_process *p, pid_t *pid) { - return (job->pid - *pid); + return (p->pid - *pid); } diff --git a/42sh/src/job-control/process_free.c b/42sh/src/job-control/process_free.c new file mode 100644 index 00000000..b6573002 --- /dev/null +++ b/42sh/src/job-control/process_free.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* process_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 process_free(void *content, size_t content_size) +{ + t_process *p; + + (void)content_size; + p = content; + ft_strdel(&p->path); + ft_sstrfree(p->argv); + free(p); +} diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/job-control/process_mark_status.c new file mode 100644 index 00000000..6ddf8bdd --- /dev/null +++ b/42sh/src/job-control/process_mark_status.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* process_mark_status.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" + +int process_mark_status(pid_t pid, int status) +{ + t_process *p; + t_jobc *jobc; + + jobc = &data_singleton()->jobc; + if (pid > 0) + { + if ((p = job_getprocess(pid))) + { + p->status = status; + if (WIFSTOPPED(status)) + p->attributes &= PROCESS_STOPPED; + else + { + p->attributes &= PROCESS_COMPLETED; + if (WIFSIGNALED(status)) + ft_printf("%d: Terminated by signal %d.\n", + (int) pid, WTERMSIG (p->status)); + } + return(0); + } + ft_dprintf(STDERR, "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 e4be8323..570a1145 100644 --- a/42sh/src/job-control/put_job_in_background.c +++ b/42sh/src/job-control/put_job_in_background.c @@ -16,6 +16,7 @@ int put_job_in_background(t_job *job, int cont) { /* Send the job a continue signal, if necessary. */ if (cont) - if (kill (-j->pgid, SIGCONT) < 0) + if (kill (-job->pgid, SIGCONT) < 0) perror ("kill (SIGCONT)"); + return (0); } diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 5abc8e4d..ab2e3439 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -16,22 +16,24 @@ int put_job_in_foreground(t_job *job, int cont) { t_jobc *jobc; + jobc = &data_singleton()->jobc; /* Put the job into the foreground. */ - tcsetpgrp(shell_terminal, job->pgid); + tcsetpgrp(STDIN_FILENO, job->pgid); /* Send the job a continue signal, if necessary. */ if (cont) { - tcsetattr (shell_terminal, TCSADRAIN, &job->tmodes); + tcsetattr (STDIN_FILENO, TCSADRAIN, &job->tmodes); if (kill(- job->pgid, SIGCONT) < 0) perror("kill (SIGCONT)"); } /* Wait for it to report. */ - wait_for_job(j); + job_wait(job); /* Put the shell back in the foreground. */ - tcsetpgrp(shell_terminal, jobc->shell_pgid); + tcsetpgrp(STDIN_FILENO, jobc->shell_pgid); /* Restore the shell’s terminal modes. */ - tcgetattr(shell_terminal, &job->tmodes); - tcsetattr(shell_terminal, TCSADRAIN, &jobc->shell_tmodes); + tcgetattr(STDIN_FILENO, &job->tmodes); + tcsetattr(STDIN_FILENO, TCSADRAIN, &jobc->shell_tmodes); + return (0); } diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c index 40dff0ad..962cb46a 100644 --- a/42sh/src/job-control/sigchld_handler.c +++ b/42sh/src/job-control/sigchld_handler.c @@ -18,13 +18,5 @@ void sigchld_handler(int signo) (void)signo; data = data_singleton(); - if (data->mode == MODE_INPUT) - { - 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"); + DG("got asynchronous notification (SIGCHLD)"); } diff --git a/42sh/src/line-editing/ft_prompt.c b/42sh/src/line-editing/ft_prompt.c index 40a62c0a..764eed26 100644 --- a/42sh/src/line-editing/ft_prompt.c +++ b/42sh/src/line-editing/ft_prompt.c @@ -17,14 +17,6 @@ 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/main/data_init.c b/42sh/src/main/data_init.c index f3ba5985..b089b8b9 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -23,19 +23,19 @@ int data_init(void) data->line.input = NULL; data->env = ft_sstrdup(environ); data->line.history = NULL; - data->exec.fdin = STDIN; - data->exec.fdout = STDOUT; + data->exec.process.fdin = STDIN; + data->exec.process.fdout = STDOUT; + data->exec.process.pid = 0; data->exec.aol_status = NULL; data->exec.aol_search = 0; - data->exec.amp = 0; - data->jobc.list = NULL; + data->exec.job.id = 0; + data->exec.job.pgid = 0; + data->exec.job.attributes = 0; + data->exec.job.first_process = 0; + data->jobc.first_job = NULL; data->jobc.current_id = 1; data->jobc.rank[0] = 0; data->jobc.rank[1] = 0; - data->jobc.job.id = 0; - data->jobc.job.pgid = 0; - data->jobc.job.notified = 0; - data->jobc.job.foreground = 0; if (!(data->line.history = ft_dlstnew(NULL, 0))) return (-1); if ((term_name = ft_getenv(data->env, "TERM")) == NULL) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index dd328da2..f1251d86 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -41,6 +41,7 @@ int main(void) /* btree_apply_infix(ast, &ft_putast2); */ if (ft_exec(&ast)) return (1); + DG("end of loop"); } return (0); } diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index b3f974c0..6419fa2c 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -22,7 +22,7 @@ void shell_init(void) if (isatty(STDIN_FILENO)) { while (tcgetpgrp(STDIN_FILENO) != (*shell_pgid = getpgrp())) - kill(*shell_pgid, SIGTTIN); + kill(-*shell_pgid, SIGTTIN); signal(SIGINT, sigint_handler); signal(SIGQUIT, SIG_IGN); signal(SIGTSTP, sigtstp_handler); From 72b9e72fc870f5f7b4816edeeea5f531d701ab4a Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 15 Dec 2016 18:32:48 +0100 Subject: [PATCH 11/27] 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); } From 40378fec732d3c1c2a0de313eeafcd55f699f97b Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 18 Dec 2016 13:13:01 +0100 Subject: [PATCH 12/27] bit more stable --- 42sh/includes/job_control.h | 4 ++-- 42sh/src/exec/exec_command.c | 2 ++ 42sh/src/exec/launch_process.c | 6 +++++- 42sh/src/exec/process_setexec.c | 8 ++++---- 42sh/src/job-control/builtin_jobs.c | 1 + 42sh/src/job-control/do_job_notification.c | 9 +++++++-- 42sh/src/job-control/{job_cmpid.c => job_cmp_id.c} | 0 42sh/src/job-control/job_is_completed.c | 3 +++ 42sh/src/job-control/job_notify_change.c | 2 +- 42sh/src/job-control/job_remove.c | 13 +++++++------ 42sh/src/job-control/process_free.c | 2 ++ 42sh/src/job-control/process_mark_status.c | 4 ++-- 42sh/src/job-control/put_job_in_foreground.c | 4 +--- 42sh/src/job-control/sigchld_handler.c | 2 ++ 42sh/src/line-editing/ft_prompt.c | 4 +--- 15 files changed, 40 insertions(+), 24 deletions(-) rename 42sh/src/job-control/{job_cmpid.c => job_cmp_id.c} (100%) diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 037f4f30..97b81eee 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -57,7 +57,7 @@ int job_addprocess(t_process *p); void job_update_id(void); void job_update_rank(void); -void do_job_notification(void); +int do_job_notification(void); void job_notify_new(t_job *job); void job_notify_change(t_job *job, int status); @@ -65,7 +65,7 @@ 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_remove(int id); void job_free(void *content, size_t content_size); int process_mark_status(pid_t pid, int status); diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index c5e9198e..30749fdb 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -39,5 +39,7 @@ int exec_command(t_btree **ast) put_job_in_background(job, 0); } btree_delone(ast, &ast_free); + p->path = NULL; + p->attributes = 0; return (0); } diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index e87fc724..e27254a1 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -20,10 +20,14 @@ int launch_process(t_process *p) exec = &data_singleton()->exec; if (p->attributes & PROCESS_UNKNOWN) ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, p->argv[0]); - else if (p->attributes & PROCESS_BUILTIN && p->fdout != STDOUT) + else if (p->attributes & PROCESS_BUILTIN && p->fdout == STDOUT) + { + DG("process is not to be forked, fdout=%i, attr=%x", p->fdout, p->attributes); set_exitstatus((*p->execf)(p->path, p->argv, data_singleton()->env)); + } else { + DG("process is to be forked, fdout=%i, attr=%x", p->fdout, p->attributes); if (p->attributes & (PROCESS_BINARY | PROCESS_SCRIPT) && access(p->path, X_OK) == -1) { diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index c3b81dc3..85571934 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -17,13 +17,13 @@ int process_setexec(t_process *p) if ((p->execf = is_builtin(p))) { DG("process is a builtin"); - p->attributes &= PROCESS_BUILTIN; + p->attributes |= PROCESS_BUILTIN; } else if (ft_strchr(p->argv[0], '/')) { DG("process is a script"); p->execf = &execve; - p->attributes &= PROCESS_SCRIPT; + p->attributes |= PROCESS_SCRIPT; p->path = ft_strdup(p->argv[0]); } else if ((p->path = ft_findexec(ft_getenv( @@ -31,13 +31,13 @@ int process_setexec(t_process *p) { DG("process is binary"); p->execf = &execve; - p->attributes &= PROCESS_BINARY; + p->attributes |= PROCESS_BINARY; } else { DG("process is '%s' unknown type", p->argv[0]); p->execf = NULL; - p->attributes &= PROCESS_UNKNOWN; + 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 index a9d9118e..753edb1d 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -26,6 +26,7 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) (void)av; while (jlist) { + DG("jlist->content"); job = jlist->content; rank = ' '; if (job->id == data_singleton()->jobc.rank[0]) diff --git a/42sh/src/job-control/do_job_notification.c b/42sh/src/job-control/do_job_notification.c index b1615415..5894492c 100644 --- a/42sh/src/job-control/do_job_notification.c +++ b/42sh/src/job-control/do_job_notification.c @@ -12,12 +12,14 @@ #include "job_control.h" -void do_job_notification(void) +int do_job_notification(void) { t_job *j; t_list *jlist; t_jobc *jobc; + int ret; + ret = 0; job_update_status(); jobc = &data_singleton()->jobc; jlist = jobc->first_job; @@ -27,14 +29,17 @@ void do_job_notification(void) DG("checking job [%i]", j->id); if (job_is_completed(j)) { + ret = 1; job_notify_change(j, 0); - job_remove(j); + job_remove(j->id); } else if (job_is_stopped(j) && !(j->attributes & JOB_NOTIFIED)) { + ret = 1; job_notify_change(j, 8); j->attributes &= JOB_NOTIFIED; } jlist = jlist->next; } + return (ret); } diff --git a/42sh/src/job-control/job_cmpid.c b/42sh/src/job-control/job_cmp_id.c similarity index 100% rename from 42sh/src/job-control/job_cmpid.c rename to 42sh/src/job-control/job_cmp_id.c diff --git a/42sh/src/job-control/job_is_completed.c b/42sh/src/job-control/job_is_completed.c index b92bbd6d..bafa92e3 100644 --- a/42sh/src/job-control/job_is_completed.c +++ b/42sh/src/job-control/job_is_completed.c @@ -22,7 +22,10 @@ int job_is_completed(t_job *job) { p = lst->content; if (!(p->attributes & PROCESS_COMPLETED)) + { + DG("process %i is not completed", p->pid); return (0); + } lst = lst->next; } return (1); diff --git a/42sh/src/job-control/job_notify_change.c b/42sh/src/job-control/job_notify_change.c index 6ca222b3..b3ca25bf 100644 --- a/42sh/src/job-control/job_notify_change.c +++ b/42sh/src/job-control/job_notify_change.c @@ -30,5 +30,5 @@ void job_notify_change(t_job *job, int status) ft_printf("{red}killed{mag}"); else ft_printf("exit %i", status); - /* ft_printf("\t%s{eoc}\n", job->command); */ + ft_printf("\t 'process command goes here'{eoc}\n"); } diff --git a/42sh/src/job-control/job_remove.c b/42sh/src/job-control/job_remove.c index 3d93d244..900ff591 100644 --- a/42sh/src/job-control/job_remove.c +++ b/42sh/src/job-control/job_remove.c @@ -12,17 +12,18 @@ #include "job_control.h" -void job_remove(t_job *job) +void job_remove(int id) { t_jobc *jobc; jobc = &data_singleton()->jobc; - if (job->id < data_singleton()->jobc.current_id) + DG("job_remove"); + if (id < data_singleton()->jobc.current_id) { - data_singleton()->jobc.current_id = job->id; - DG("ID_UPDATE(downgrade):%i", job->id); + data_singleton()->jobc.current_id = id; + DG("ID_UPDATE(downgrade):%i", 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); + DG("ID_UPDATE(no downgrade): %i/%i", id, data_singleton()->jobc.current_id); + ft_lst_delif(&jobc->first_job, &id, job_cmp_id, job_free); } diff --git a/42sh/src/job-control/process_free.c b/42sh/src/job-control/process_free.c index b6573002..9a4dae4b 100644 --- a/42sh/src/job-control/process_free.c +++ b/42sh/src/job-control/process_free.c @@ -18,7 +18,9 @@ void process_free(void *content, size_t content_size) (void)content_size; p = content; + DG("check 1"); ft_strdel(&p->path); + DG("check 2"); ft_sstrfree(p->argv); free(p); } diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/job-control/process_mark_status.c index 12199d06..4c452266 100644 --- a/42sh/src/job-control/process_mark_status.c +++ b/42sh/src/job-control/process_mark_status.c @@ -24,10 +24,10 @@ int process_mark_status(pid_t pid, int status) DG("found process pid=%i", pid); p->status = status; if (WIFSTOPPED(status)) - p->attributes &= PROCESS_STOPPED; + p->attributes |= PROCESS_STOPPED; else { - p->attributes &= PROCESS_COMPLETED; + p->attributes |= PROCESS_COMPLETED; if (WIFSIGNALED(status)) ft_printf("%d: Terminated by signal %d.\n", (int) pid, WTERMSIG(p->status)); diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 108999e9..f34f1572 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -27,10 +27,8 @@ 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); + job_remove(job->id); /* 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 e011ebd2..40390e12 100644 --- a/42sh/src/job-control/sigchld_handler.c +++ b/42sh/src/job-control/sigchld_handler.c @@ -19,5 +19,7 @@ void sigchld_handler(int signo) (void)signo; data = data_singleton(); DG("got asynchronous notification (SIGCHLD)"); + /* if (do_job_notification()) */ + /* ft_putstr(SHELL_PROMPT); */ job_update_status(); } diff --git a/42sh/src/line-editing/ft_prompt.c b/42sh/src/line-editing/ft_prompt.c index 8c7a1338..8991a190 100644 --- a/42sh/src/line-editing/ft_prompt.c +++ b/42sh/src/line-editing/ft_prompt.c @@ -14,9 +14,7 @@ int ft_prompt(void) { - t_data *data; - - data = data_singleton(); + DG("do_job_notification() before prompt"); do_job_notification(); DG("new prompt now"); ft_putstr(SHELL_PROMPT); From 6144e4eab589e33311917751d143736901795537 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 19 Dec 2016 17:12:02 +0100 Subject: [PATCH 13/27] gotta fix problem with env --- 42sh/includes/job_control.h | 2 +- 42sh/src/exec/exec_command.c | 6 +++++- 42sh/src/exec/process_setexec.c | 2 ++ 42sh/src/exec/set_exitstatus.c | 2 +- 42sh/src/job-control/builtin_jobs.c | 9 +++------ 42sh/src/job-control/do_job_notification.c | 6 +++--- 42sh/src/job-control/job_addprocess.c | 7 +++---- 42sh/src/job-control/job_notify_change.c | 20 ++++++++++++++------ 8 files changed, 32 insertions(+), 22 deletions(-) diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 97b81eee..5597f211 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -59,7 +59,7 @@ void job_update_rank(void); int do_job_notification(void); void job_notify_new(t_job *job); -void job_notify_change(t_job *job, int status); +void job_notify_change(int id, int status); int job_wait(t_job *job); void job_update_status(void); diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 30749fdb..471eda28 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -25,6 +25,9 @@ int exec_command(t_btree **ast) if (process_setexec(p)) { ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->argv[0]); + p->attributes = 0; + p->path = NULL; + p->argv = NULL; btree_delone(ast, &ast_free); return (0); } @@ -39,7 +42,8 @@ int exec_command(t_btree **ast) put_job_in_background(job, 0); } btree_delone(ast, &ast_free); - p->path = NULL; p->attributes = 0; + p->path = NULL; + p->argv = NULL; return (0); } diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index 85571934..fd7d7c47 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -14,6 +14,8 @@ int process_setexec(t_process *p) { + DG("going to setexec:\nenv at %p\nav[0]=%s", data_singleton()->env, p->argv[0]); + DG("going to setexec:\nPATH=%s\nav[0]=%s", ft_getenv(data_singleton()->env, "PATH"), p->argv[0]); if ((p->execf = is_builtin(p))) { DG("process is a builtin"); diff --git a/42sh/src/exec/set_exitstatus.c b/42sh/src/exec/set_exitstatus.c index 01055140..eb22a1ee 100644 --- a/42sh/src/exec/set_exitstatus.c +++ b/42sh/src/exec/set_exitstatus.c @@ -17,6 +17,6 @@ void set_exitstatus(int status) char *astatus; astatus = ft_itoa(status); - builtin_setenv("shell", (char*[3]){"?", astatus}, data_singleton()->env); + builtin_setenv("setenv", (char*[3]){"?", astatus}, data_singleton()->env); ft_strdel(&astatus); } diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c index 753edb1d..90bb3c26 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -24,18 +24,15 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) (void)path; (void)envp; (void)av; + rank = '+'; while (jlist) { DG("jlist->content"); 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("{mag}jobs: [%i] %c ", job->id, rank); ft_printf("attributes=%x{eoc}\n", job->attributes); jlist = jlist->next; + rank = (rank == '+') ? '-' : ' '; } return (0); } diff --git a/42sh/src/job-control/do_job_notification.c b/42sh/src/job-control/do_job_notification.c index 5894492c..a9e4a40c 100644 --- a/42sh/src/job-control/do_job_notification.c +++ b/42sh/src/job-control/do_job_notification.c @@ -30,13 +30,13 @@ int do_job_notification(void) if (job_is_completed(j)) { ret = 1; - job_notify_change(j, 0); - job_remove(j->id); + job_notify_change(j->id, 0); + /* job_remove(j->id); */ } else if (job_is_stopped(j) && !(j->attributes & JOB_NOTIFIED)) { ret = 1; - job_notify_change(j, 8); + job_notify_change(j->id, 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 d18a5e20..03df4939 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/job_addprocess.c @@ -21,16 +21,15 @@ int job_addprocess(t_process *p) job = &data_singleton()->exec.job; if (p->fdin == STDIN) { - job->id = jobc->current_id; job_update_id(); + job->id = jobc->current_id; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); - jobc->rank[1] = jobc->rank[0]; - jobc->rank[0] = job->id; + DG("added new job [%i], rank=%i:%i", job->id, jobc->rank[0], jobc->rank[1]); } 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); - DG("adding process to first_job : %i", p->pid); + DG("added process to first_job : %i", p->pid); return(0); } diff --git a/42sh/src/job-control/job_notify_change.c b/42sh/src/job-control/job_notify_change.c index b3ca25bf..3865dfd7 100644 --- a/42sh/src/job-control/job_notify_change.c +++ b/42sh/src/job-control/job_notify_change.c @@ -12,16 +12,24 @@ #include "job_control.h" -void job_notify_change(t_job *job, int status) +void job_notify_change(int id, int status) { - char rank; + t_job *job; + t_jobc *jobc; + char rank; rank = ' '; - if (job->id == data_singleton()->jobc.rank[0]) + jobc = &data_singleton()->jobc; + job = jobc->first_job->content; + if (id == job->id) rank = '+'; - else if (job->id == data_singleton()->jobc.rank[1]) - rank = '-'; - ft_printf("{mag}[%i] %c ", job->id, rank); + else if (jobc->first_job->next) + { + job = jobc->first_job->next->content; + if (id == job->id) + rank = '-'; + } + ft_printf("{mag}[%i] %c ", id, rank); if (status == 0) ft_printf("{gre}done{mag}"); else if (status == 8) From c9a39637da681f73a4e66efe75257e262688fd27 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 22 Dec 2016 11:44:44 +0100 Subject: [PATCH 14/27] pipeend and pipestart bits in process attribute, env problems solved --- 42sh/includes/exec.h | 12 +++++++++ 42sh/includes/job_control.h | 8 ------ 42sh/libft | 2 +- 42sh/src/builtin/builtin_setenv.c | 2 +- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/exec/exec_pipe.c | 31 +++++++++++++++------- 42sh/src/exec/process_setexec.c | 2 -- 42sh/src/job-control/do_job_notification.c | 2 +- 42sh/src/job-control/job_addprocess.c | 4 +-- 9 files changed, 40 insertions(+), 25 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 92324800..86048c03 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -16,6 +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 IS_PIPESTART(p) (p & (PROCESS_PIPESTART)) +# define IS_PIPEEND(p) (p & (PROCESS_PIPEEND)) + # include "libft.h" # include "types.h" # include "job_control.h" diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 5597f211..78fabb2c 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -19,19 +19,11 @@ # include "libft.h" # include "types.h" -# 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 JOB_NOTIFIED (1 << 0) # define JOB_BG (1 << 1) # define JOB_IS_BG(j) (j & JOB_BG) # define JOB_IS_FG(j) !(j & JOB_BG) - struct s_job { int id; diff --git a/42sh/libft b/42sh/libft index e7cece57..a26a4be4 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit e7cece5732986cdcd159b40f1d927137e2d6c9f3 +Subproject commit a26a4be4adbf4d6d4887af95a7307356d52ce85d diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index fc6ec20a..6cecb3d6 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -37,7 +37,7 @@ int builtin_setenv(const char *path, char *const av[], char *const envp[]) { if (ft_strcmp((*env)[i], av[0]) == '=') { - ft_strdel(*env); + ft_strdel(&(*env)[i]); (*env)[i] = str; return (0); } diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 471eda28..67db7916 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -34,7 +34,7 @@ int exec_command(t_btree **ast) DG("gonna launch_process now"); launch_process(p); job_addprocess(p); - if (p->fdout == STDOUT) + if (IS_PIPEEND(p->fdout == STDOUT)) { if (JOB_IS_FG(job->attributes)) put_job_in_foreground(job, 0); diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 7b9025af..6bd51167 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -16,21 +16,34 @@ int exec_pipe(t_btree **ast) { int fds[2]; t_data *data; + t_process *p; data = data_singleton(); + p = &data_singleton()->exec.process; pipe(fds); DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); - data->exec.process.fdout = fds[PIPE_WRITE]; + p->fdout = fds[PIPE_WRITE]; + if (!IS_PIPEEND(p->attributes)) + { + p->attributes |= PROCESS_PIPESTART; + p->attributes &= ~PROCESS_PIPEEND; + } + ft_exec(&(*ast)->left); - if (data->exec.process.fdout != STDOUT) - close(data->exec.process.fdout); - data->exec.process.fdout = STDOUT; - data->exec.process.fdin = fds[PIPE_READ]; + if (p->fdout != STDOUT) + close(p->fdout); + p->fdout = STDOUT; + p->fdin = fds[PIPE_READ]; + + p->attributes ~= ~PROCESS_PIPESTART; + p->attributes |= PROCESS_PIPEEND; + ft_exec(&(*ast)->right); - close(fds[PIPE_WRITE]); - close(fds[PIPE_READ]); - data->exec.process.fdin = STDIN; - data->exec.process.fdout = STDOUT; + /* close(fds[PIPE_WRITE]); */ + /* close(fds[PIPE_READ]); */ + p->fdin = STDIN; + p->fdout = STDOUT; + p->attributes &= ~PROCESS_PIPEEND; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index fd7d7c47..85571934 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -14,8 +14,6 @@ int process_setexec(t_process *p) { - DG("going to setexec:\nenv at %p\nav[0]=%s", data_singleton()->env, p->argv[0]); - DG("going to setexec:\nPATH=%s\nav[0]=%s", ft_getenv(data_singleton()->env, "PATH"), p->argv[0]); if ((p->execf = is_builtin(p))) { DG("process is a builtin"); diff --git a/42sh/src/job-control/do_job_notification.c b/42sh/src/job-control/do_job_notification.c index a9e4a40c..91c63e8a 100644 --- a/42sh/src/job-control/do_job_notification.c +++ b/42sh/src/job-control/do_job_notification.c @@ -31,7 +31,7 @@ int do_job_notification(void) { ret = 1; job_notify_change(j->id, 0); - /* job_remove(j->id); */ + job_remove(j->id); } else if (job_is_stopped(j) && !(j->attributes & JOB_NOTIFIED)) { diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index 03df4939..48d1930e 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/job_addprocess.c @@ -19,7 +19,7 @@ int job_addprocess(t_process *p) jobc = &data_singleton()->jobc; job = &data_singleton()->exec.job; - if (p->fdin == STDIN) + if (IS_PIPESTART(p->attributes)) { job_update_id(); job->id = jobc->current_id; @@ -28,7 +28,7 @@ int job_addprocess(t_process *p) } job = jobc->first_job->content; ft_lstadd(&job->first_process, ft_lstnew(p, sizeof(*p))); - if (JOB_IS_BG(job->attributes) && p->fdout == STDOUT) + if (JOB_IS_BG(job->attributes) && IS_PIPEEND(p->attributes)) job_notify_new(job); DG("added process to first_job : %i", p->pid); return(0); From 14e5d176aa87be4cfc9923ae0dff37bb31be920a Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 2 Jan 2017 17:22:50 +0100 Subject: [PATCH 15/27] end of holidays commit --- 42sh/includes/job_control.h | 1 - 42sh/src/exec/exec_command.c | 2 +- 42sh/src/exec/exec_pipe.c | 10 +++--- 42sh/src/exec/launch_process.c | 2 +- 42sh/src/job-control/job_addprocess.c | 2 +- 42sh/src/job-control/job_update_rank.c | 35 -------------------- 42sh/src/job-control/process_mark_status.c | 7 ++-- 42sh/src/job-control/put_job_in_foreground.c | 2 +- 42sh/src/line-editing/ft_interactive_sh.c | 1 + 42sh/src/main/data_init.c | 1 + 10 files changed, 13 insertions(+), 50 deletions(-) delete mode 100644 42sh/src/job-control/job_update_rank.c diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 78fabb2c..08eca6df 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -38,7 +38,6 @@ struct s_jobc t_list *first_job; pid_t shell_pgid; int current_id; - int rank[2]; struct termios shell_tmodes; }; diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 67db7916..934d7e03 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -34,7 +34,7 @@ int exec_command(t_btree **ast) DG("gonna launch_process now"); launch_process(p); job_addprocess(p); - if (IS_PIPEEND(p->fdout == STDOUT)) + if (IS_PIPEEND(p->attributes)) { if (JOB_IS_FG(job->attributes)) put_job_in_foreground(job, 0); diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 6bd51167..41cc9d82 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -24,18 +24,17 @@ int exec_pipe(t_btree **ast) DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); p->fdout = fds[PIPE_WRITE]; if (!IS_PIPEEND(p->attributes)) - { p->attributes |= PROCESS_PIPESTART; - p->attributes &= ~PROCESS_PIPEEND; - } + else + p->attributes &= ~PROCESS_PIPESTART; + p->attributes &= ~PROCESS_PIPEEND; ft_exec(&(*ast)->left); if (p->fdout != STDOUT) close(p->fdout); p->fdout = STDOUT; p->fdin = fds[PIPE_READ]; - - p->attributes ~= ~PROCESS_PIPESTART; + p->attributes &= ~PROCESS_PIPESTART; p->attributes |= PROCESS_PIPEEND; ft_exec(&(*ast)->right); @@ -43,6 +42,7 @@ int exec_pipe(t_btree **ast) /* close(fds[PIPE_READ]); */ p->fdin = STDIN; p->fdout = STDOUT; + p->attributes |= PROCESS_PIPESTART; p->attributes &= ~PROCESS_PIPEEND; btree_delone(ast, &ast_free); return (0); diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index e27254a1..511659ff 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -27,7 +27,7 @@ int launch_process(t_process *p) } else { - DG("process is to be forked, fdout=%i, attr=%x", p->fdout, p->attributes); + DG("process is to be forked, fdout=%i, attr=%b", p->fdout, p->attributes); if (p->attributes & (PROCESS_BINARY | PROCESS_SCRIPT) && access(p->path, X_OK) == -1) { diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index 48d1930e..77455423 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/job_addprocess.c @@ -24,7 +24,7 @@ int job_addprocess(t_process *p) job_update_id(); job->id = jobc->current_id; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); - DG("added new job [%i], rank=%i:%i", job->id, jobc->rank[0], jobc->rank[1]); + DG("added new job [%i]", job->id); } job = jobc->first_job->content; ft_lstadd(&job->first_process, ft_lstnew(p, sizeof(*p))); diff --git a/42sh/src/job-control/job_update_rank.c b/42sh/src/job-control/job_update_rank.c deleted file mode 100644 index 49c8475b..00000000 --- a/42sh/src/job-control/job_update_rank.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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->first_job; - 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/process_mark_status.c b/42sh/src/job-control/process_mark_status.c index 4c452266..e517c9eb 100644 --- a/42sh/src/job-control/process_mark_status.c +++ b/42sh/src/job-control/process_mark_status.c @@ -17,7 +17,7 @@ int process_mark_status(pid_t pid, int status) t_process *p; DG("marking: pid=%i, status=%i", pid, status); - if (pid > 0) + if (pid > 1) { if ((p = job_getprocess(pid))) { @@ -37,8 +37,5 @@ int process_mark_status(pid_t pid, int status) ft_dprintf(2, "No child process %d.\n", pid); return(-1); } - else - { - return(-1); - } + return(-1); } diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index f34f1572..8e17a06c 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -23,7 +23,7 @@ int put_job_in_foreground(t_job *job, int cont) if (cont) { tcsetattr (STDIN_FILENO, TCSADRAIN, &job->tmodes); - if (kill(- job->pgid, SIGCONT) < 0) + if (kill(-job->pgid, SIGCONT) < 0) perror("kill (SIGCONT)"); } /* Wait for it to report. */ diff --git a/42sh/src/line-editing/ft_interactive_sh.c b/42sh/src/line-editing/ft_interactive_sh.c index 2c2d58a9..1c6b4465 100644 --- a/42sh/src/line-editing/ft_interactive_sh.c +++ b/42sh/src/line-editing/ft_interactive_sh.c @@ -47,6 +47,7 @@ int ft_interactive_sh(t_data *data) if ((input_init(data))) return (-1); + DG("interactive_sh loop"); while (1) { ft_bzero(buf, 4); diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index b089b8b9..355d591f 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -26,6 +26,7 @@ int data_init(void) data->exec.process.fdin = STDIN; data->exec.process.fdout = STDOUT; data->exec.process.pid = 0; + data->exec.process.attributes = PROCESS_PIPESTART; data->exec.aol_status = NULL; data->exec.aol_search = 0; data->exec.job.id = 0; From 4dcf3c319c3a1dfeca7ad5a5ee7af9e9deb6de14 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 2 Jan 2017 19:09:08 +0100 Subject: [PATCH 16/27] stuff --- 42sh/includes/exec.h | 4 +- 42sh/includes/job_control.h | 2 +- 42sh/libft | 2 +- 42sh/src/exec/exec_command.c | 6 +-- 42sh/src/exec/exec_or_if.c | 2 +- 42sh/src/exec/launch_process.c | 10 ++--- 42sh/src/exec/process_setexec.c | 10 ++--- 42sh/src/job-control/builtin_jobs.c | 41 +++++++++++++++++--- 42sh/src/job-control/check_chlds.c | 2 +- 42sh/src/job-control/do_job_notification.c | 2 +- 42sh/src/job-control/job_addprocess.c | 2 +- 42sh/src/job-control/job_remove.c | 2 +- 42sh/src/job-control/job_wait.c | 2 +- 42sh/src/job-control/process_free.c | 4 +- 42sh/src/job-control/put_job_in_foreground.c | 2 +- 42sh/src/job-control/sigchld_handler.c | 2 +- 42sh/src/line-editing/ft_prompt.c | 2 +- 17 files changed, 63 insertions(+), 34 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 92324800..2091b5dc 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/15 17:24:04 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 18:10:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ struct s_process { - char **argv; + char **av; char *path; t_execf *execf; pid_t pid; diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 037f4f30..2d2e7ec2 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/15 17:49:56 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 18:10:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft b/42sh/libft index e7cece57..a26a4be4 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit e7cece5732986cdcd159b40f1d927137e2d6c9f3 +Subproject commit a26a4be4adbf4d6d4887af95a7307356d52ce85d diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index c5e9198e..01b51f5f 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/15 18:31:12 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 19:07:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,10 +21,10 @@ int exec_command(t_btree **ast) node = (*ast)->item; p = &data_singleton()->exec.process; job = &data_singleton()->exec.job; - p->argv = ft_sstrdup(node->data.sstr); + p->av = ft_sstrdup(node->data.sstr); if (process_setexec(p)) { - ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->argv[0]); + ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); 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 a5170f93..6b18060c 100644 --- a/42sh/src/exec/exec_or_if.c +++ b/42sh/src/exec/exec_or_if.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 21:06:17 by jhalford #+# #+# */ -/* Updated: 2016/12/12 18:04:08 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 18:10:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index e87fc724..8c3df938 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/15 15:21:15 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 18:14:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,15 +19,15 @@ int launch_process(t_process *p) exec = &data_singleton()->exec; if (p->attributes & PROCESS_UNKNOWN) - ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, p->argv[0]); + ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, p->av[0]); else if (p->attributes & PROCESS_BUILTIN && p->fdout != STDOUT) - set_exitstatus((*p->execf)(p->path, p->argv, data_singleton()->env)); + set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env)); else { if (p->attributes & (PROCESS_BINARY | PROCESS_SCRIPT) && access(p->path, X_OK) == -1) { - ft_dprintf(2, "%s: permission denied: %s\n", SHELL_NAME, p->argv[0]); + ft_dprintf(2, "%s: permission denied: %s\n", SHELL_NAME, p->av[0]); return (-1); } pid = fork(); @@ -35,7 +35,7 @@ int launch_process(t_process *p) { process_setgroup(p); process_redirect(p); - (*p->execf)(p->path, p->argv, data_singleton()->env); + (*p->execf)(p->path, p->av, data_singleton()->env); exit(42); } else if (pid > 0) diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index c3b81dc3..802f10a4 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/15 15:19:11 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 18:15:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,15 +19,15 @@ int process_setexec(t_process *p) DG("process is a builtin"); p->attributes &= PROCESS_BUILTIN; } - else if (ft_strchr(p->argv[0], '/')) + else if (ft_strchr(p->av[0], '/')) { DG("process is a script"); p->execf = &execve; p->attributes &= PROCESS_SCRIPT; - p->path = ft_strdup(p->argv[0]); + p->path = ft_strdup(p->av[0]); } else if ((p->path = ft_findexec(ft_getenv( - data_singleton()->env, "PATH"), p->argv[0]))) + data_singleton()->env, "PATH"), p->av[0]))) { DG("process is binary"); p->execf = &execve; @@ -35,7 +35,7 @@ int process_setexec(t_process *p) } else { - DG("process is '%s' unknown type", p->argv[0]); + DG("process is '%s' unknown type", p->av[0]); p->execf = NULL; p->attributes &= PROCESS_UNKNOWN; return (1); diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c index a9d9118e..74fe2865 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2016/12/15 17:54:01 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 19:07:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,18 +14,26 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) { - t_jobc *jobc; - t_list *jlist; - t_job *job; - char rank; + t_jobc *jobc; + t_list *jlist; + t_job *job; + t_list *plist; + t_process *p; + char rank; + int lg; + int firstp; jobc = &data_singleton()->jobc; jlist = jobc->first_job; (void)path; (void)envp; (void)av; + lg = 0; + if (ft_strcmp(av[1], "-l") == 0) + lg = 1; while (jlist) { + firstp = 1; job = jlist->content; rank = ' '; if (job->id == data_singleton()->jobc.rank[0]) @@ -33,8 +41,29 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) 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); + if (lg) + ft_printf("%i ", p->pid); + ft_printf("attr=%x ", 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; + } jlist = jlist->next; + ft_printf("{eoc}\n"); } return (0); } diff --git a/42sh/src/job-control/check_chlds.c b/42sh/src/job-control/check_chlds.c index b4e02bc7..74baaec3 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/15 17:24:33 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 17:32:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/do_job_notification.c b/42sh/src/job-control/do_job_notification.c index b1615415..3e343bd5 100644 --- a/42sh/src/job-control/do_job_notification.c +++ b/42sh/src/job-control/do_job_notification.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 13:01:19 by jhalford #+# #+# */ -/* Updated: 2016/12/15 17:49:37 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 18:21:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index d18a5e20..7aea8e91 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/15 18:31:22 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 18:16:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_remove.c b/42sh/src/job-control/job_remove.c index 3d93d244..ddba50fc 100644 --- a/42sh/src/job-control/job_remove.c +++ b/42sh/src/job-control/job_remove.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:51:08 by jhalford #+# #+# */ -/* Updated: 2016/12/15 17:58:48 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 18:15:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c index 8cc0483b..e0a72b90 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 11:49:05 by jhalford #+# #+# */ -/* Updated: 2016/12/15 17:40:00 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 17:32:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/process_free.c b/42sh/src/job-control/process_free.c index b6573002..cfe28194 100644 --- a/42sh/src/job-control/process_free.c +++ b/42sh/src/job-control/process_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/02 18:16:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,6 @@ void process_free(void *content, size_t content_size) (void)content_size; p = content; ft_strdel(&p->path); - ft_sstrfree(p->argv); + ft_sstrfree(p->av); free(p); } diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 108999e9..e9facc5a 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/15 17:58:51 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 18:15:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c index e011ebd2..afa70944 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/15 15:06:30 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 18:10:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_prompt.c b/42sh/src/line-editing/ft_prompt.c index 8c7a1338..c0db38fa 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/15 15:07:13 by jhalford ### ########.fr */ +/* Updated: 2017/01/02 18:19:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From 9e8939d588ca03871a7d46f83e9a582016e313b9 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 2 Jan 2017 21:31:20 +0100 Subject: [PATCH 17/27] cleanup after merge (few segfaults remained because of auto merge done the wrong way around. process_reset added --- 42sh/includes/exec.h | 5 +++-- 42sh/src/builtin/builtin_setenv.c | 4 ++++ 42sh/src/builtin/is_builtin.c | 2 +- 42sh/src/exec/exec_command.c | 30 +++++++++++++-------------- 42sh/src/exec/exec_pipe.c | 20 +++++++++++------- 42sh/src/exec/launch_process.c | 2 +- 42sh/src/exec/process_reset.c | 14 +++++++++++++ 42sh/src/exec/process_setexec.c | 2 +- 42sh/src/job-control/builtin_jobs.c | 7 ++----- 42sh/src/job-control/job_addprocess.c | 2 ++ 42sh/src/main/data_init.c | 7 +------ 11 files changed, 55 insertions(+), 40 deletions(-) create mode 100644 42sh/src/exec/process_reset.c diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 4e810a89..2f5f338b 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -25,8 +25,8 @@ # define PROCESS_PIPESTART (1 << 6) # define PROCESS_PIPEEND (1 << 7) -# define IS_PIPESTART(p) (p & (PROCESS_PIPESTART)) -# define IS_PIPEEND(p) (p & (PROCESS_PIPEEND)) +# define IS_PIPESTART(a) (a & PROCESS_PIPESTART) +# define IS_PIPEEND(a) (a & PROCESS_PIPEEND) # include "libft.h" # include "types.h" @@ -80,6 +80,7 @@ int process_setexec(t_process *p); int process_setgroup(t_process *p); int process_redirect(t_process *p); void process_free(void *content, size_t content_size); +void process_reset(void); void fd_redirect(void); void fd_reset(void); diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 6cecb3d6..916949e7 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -35,10 +35,13 @@ int builtin_setenv(const char *path, char *const av[], char *const envp[]) str = ft_str3join(av[0], "=", av[1]); while ((*env)[i]) { + /* DG("check 2: i=%i, (*env)[i]=%p",i, (*env)[i]); */ + /* DG("content=%s", (*env)[i]); */ if (ft_strcmp((*env)[i], av[0]) == '=') { ft_strdel(&(*env)[i]); (*env)[i] = str; + DG("done setenv"); return (0); } i++; @@ -46,5 +49,6 @@ 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/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index 3438b909..80963e91 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -29,7 +29,7 @@ t_execf *is_builtin(t_process *p) i = -1; while (g_builtin[++i].name) - if (ft_strcmp(g_builtin[i].name, p->argv[0]) == 0) + if (ft_strcmp(g_builtin[i].name, p->av[0]) == 0) return (g_builtin[i].f); return (NULL); } diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index e7da8fe3..83f0148b 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -25,25 +25,23 @@ int exec_command(t_btree **ast) if (process_setexec(p)) { ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); - p->attributes = 0; - p->path = NULL; - p->argv = NULL; - btree_delone(ast, &ast_free); - return (0); + set_exitstatus(127); } - DG("gonna launch_process now"); - launch_process(p); - job_addprocess(p); - if (IS_PIPEEND(p->attributes)) + else { - if (JOB_IS_FG(job->attributes)) - put_job_in_foreground(job, 0); - else - put_job_in_background(job, 0); + DG("gonna launch_process now"); + launch_process(p); + DG("launch_process done"); + job_addprocess(p); + DG("job_addprocess done"); + if (IS_PIPEEND(p->attributes)) + { + JOB_IS_FG(job->attributes) ? + put_job_in_foreground(job, 0): + put_job_in_background(job, 0); + } } + process_reset(); btree_delone(ast, &ast_free); - p->attributes = 0; - p->path = NULL; - p->argv = NULL; return (0); } diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 41cc9d82..273c9710 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -15,6 +15,7 @@ int exec_pipe(t_btree **ast) { int fds[2]; + int start; t_data *data; t_process *p; @@ -23,27 +24,30 @@ int exec_pipe(t_btree **ast) pipe(fds); DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); p->fdout = fds[PIPE_WRITE]; - if (!IS_PIPEEND(p->attributes)) + start = 0; + if (!IS_PIPESTART(p->attributes)) + { p->attributes |= PROCESS_PIPESTART; - else - p->attributes &= ~PROCESS_PIPESTART; - p->attributes &= ~PROCESS_PIPEEND; + start = 1; + } + p->attributes &= ~PROCESS_PIPESTART; + p->attributes &= ~PROCESS_PIPEEND; ft_exec(&(*ast)->left); + p->attributes &= ~PROCESS_PIPESTART; if (p->fdout != STDOUT) close(p->fdout); p->fdout = STDOUT; p->fdin = fds[PIPE_READ]; - p->attributes &= ~PROCESS_PIPESTART; - p->attributes |= PROCESS_PIPEEND; + p->attributes |= PROCESS_PIPEEND; ft_exec(&(*ast)->right); + if (start) + p->attributes |= PROCESS_PIPESTART; /* close(fds[PIPE_WRITE]); */ /* close(fds[PIPE_READ]); */ p->fdin = STDIN; p->fdout = STDOUT; - p->attributes |= PROCESS_PIPESTART; - p->attributes &= ~PROCESS_PIPEEND; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index be15accc..4c7d43d9 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -20,7 +20,7 @@ int launch_process(t_process *p) exec = &data_singleton()->exec; if (p->attributes & PROCESS_UNKNOWN) ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, p->av[0]); - else if (p->attributes & PROCESS_BUILTIN && p->fdout != STDOUT) + else if (p->attributes & PROCESS_BUILTIN && p->fdout == STDOUT) set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env)); else { diff --git a/42sh/src/exec/process_reset.c b/42sh/src/exec/process_reset.c new file mode 100644 index 00000000..d4ef0777 --- /dev/null +++ b/42sh/src/exec/process_reset.c @@ -0,0 +1,14 @@ +#include "exec.h" + +void process_reset(void) +{ + t_data *data; + + data = data_singleton(); + data->exec.process.path = NULL; + data->exec.process.av = NULL; + data->exec.process.fdin = STDIN; + data->exec.process.fdout = STDOUT; + data->exec.process.pid = 0; + data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; +} diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index e058df46..01fe54f1 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -16,7 +16,7 @@ int process_setexec(t_process *p) { if ((p->execf = is_builtin(p))) { - DG("process is a builtin"); + DG("process is a builtin, attr=%b", p->attributes); p->attributes |= PROCESS_BUILTIN; } else if (ft_strchr(p->av[0], '/')) diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c index b7813eeb..06980138 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -31,15 +31,11 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) lg = 0; if (ft_strcmp(av[1], "-l") == 0) lg = 1; + rank = '+'; while (jlist) { firstp = 1; 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); if (lg) ft_printf("%i ", p->pid); @@ -63,6 +59,7 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) firstp = 0; } jlist = jlist->next; + rank = (rank == '+') ? '-' : ' '; ft_printf("{eoc}\n"); } return (0); diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index be5fb094..1b1e714e 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/job_addprocess.c @@ -19,8 +19,10 @@ int job_addprocess(t_process *p) jobc = &data_singleton()->jobc; job = &data_singleton()->exec.job; + DG("check; attr=%b", p->attributes); if (IS_PIPESTART(p->attributes)) { + DG("check"); job_update_id(); job->id = jobc->current_id; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 355d591f..4953aa39 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -23,10 +23,7 @@ int data_init(void) data->line.input = NULL; data->env = ft_sstrdup(environ); data->line.history = NULL; - data->exec.process.fdin = STDIN; - data->exec.process.fdout = STDOUT; - data->exec.process.pid = 0; - data->exec.process.attributes = PROCESS_PIPESTART; + process_reset(); data->exec.aol_status = NULL; data->exec.aol_search = 0; data->exec.job.id = 0; @@ -35,8 +32,6 @@ int data_init(void) data->exec.job.first_process = 0; data->jobc.first_job = 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) From d2982d89bcdf45040d8195534f44a312d97ecfd3 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 3 Jan 2017 15:36:53 +0100 Subject: [PATCH 18/27] next to do: end of pipe doesnt terminate by itself --- 42sh/includes/job_control.h | 6 +++--- 42sh/libft | 2 +- 42sh/src/exec/exec_command.c | 15 +++------------ 42sh/src/exec/exec_pipe.c | 8 +------- 42sh/src/exec/ft_exec.c | 3 ++- 42sh/src/exec/launch_process.c | 6 +++++- 42sh/src/exec/process_setexec.c | 5 +++-- 42sh/src/job-control/do_job_notification.c | 4 ++-- 42sh/src/job-control/job_is_completed.c | 8 +++++++- 42sh/src/job-control/job_is_stopped.c | 6 +++++- 42sh/src/job-control/job_update_status.c | 4 ++-- 42sh/src/job-control/job_wait.c | 14 +++++++++----- 42sh/src/job-control/process_mark_status.c | 2 +- 42sh/src/job-control/put_job_in_foreground.c | 3 ++- 42sh/src/job-control/sigchld_handler.c | 2 +- 15 files changed, 47 insertions(+), 41 deletions(-) diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 476e108a..08e0952b 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -52,10 +52,10 @@ int do_job_notification(void); void job_notify_new(t_job *job); void job_notify_change(int id, int status); -int job_wait(t_job *job); +int job_wait(int id); void job_update_status(void); -int job_is_stopped(t_job *job); -int job_is_completed(t_job *job); +int job_is_stopped(int id); +int job_is_completed(int id); void job_remove(int id); void job_free(void *content, size_t content_size); int process_mark_status(pid_t pid, int status); diff --git a/42sh/libft b/42sh/libft index a26a4be4..f4af2f64 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit a26a4be4adbf4d6d4887af95a7307356d52ce85d +Subproject commit f4af2f642761111b3395bb69c1766a8c46719d4d diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 83f0148b..fe010c2f 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -21,25 +21,16 @@ int exec_command(t_btree **ast) node = (*ast)->item; p = &data_singleton()->exec.process; job = &data_singleton()->exec.job; + /* job = data_singleton()->jobc.first_job->content; */ p->av = ft_sstrdup(node->data.sstr); - if (process_setexec(p)) + process_setexec(p); + if (!(launch_process(p))) { - ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); - set_exitstatus(127); - } - else - { - DG("gonna launch_process now"); - launch_process(p); - DG("launch_process done"); job_addprocess(p); - DG("job_addprocess done"); if (IS_PIPEEND(p->attributes)) - { JOB_IS_FG(job->attributes) ? put_job_in_foreground(job, 0): put_job_in_background(job, 0); - } } process_reset(); btree_delone(ast, &ast_free); diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 273c9710..06820098 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -24,13 +24,7 @@ int exec_pipe(t_btree **ast) pipe(fds); DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); p->fdout = fds[PIPE_WRITE]; - start = 0; - if (!IS_PIPESTART(p->attributes)) - { - p->attributes |= PROCESS_PIPESTART; - start = 1; - } - p->attributes &= ~PROCESS_PIPESTART; + start = IS_PIPESTART(p->attributes); p->attributes &= ~PROCESS_PIPEEND; ft_exec(&(*ast)->left); diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 7559ed7d..33c2dbe3 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -38,7 +38,8 @@ int ft_exec(t_btree **ast) while (g_execmap[i].type) { if (item->type == g_execmap[i].type) - return ((*g_execmap[i].f)(ast)); + /* return ((*g_execmap[i].f)(ast)); */ + (*g_execmap[i].f)(ast); i++; } return (0); diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 4c7d43d9..fdf799f6 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -19,7 +19,11 @@ int launch_process(t_process *p) exec = &data_singleton()->exec; if (p->attributes & PROCESS_UNKNOWN) - ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, p->av[0]); + { + ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); + set_exitstatus(127); + return (1); + } else if (p->attributes & PROCESS_BUILTIN && p->fdout == STDOUT) set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env)); else diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index 01fe54f1..0208eec8 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -14,16 +14,17 @@ int process_setexec(t_process *p) { + DG("process_setexec, attr=%b", p->attributes); if ((p->execf = is_builtin(p))) { - DG("process is a builtin, attr=%b", p->attributes); + 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->attributes |= PROCESS_SCRIPT; p->path = ft_strdup(p->av[0]); } else if ((p->path = ft_findexec(ft_getenv( diff --git a/42sh/src/job-control/do_job_notification.c b/42sh/src/job-control/do_job_notification.c index 6feb7e9f..c902b1ec 100644 --- a/42sh/src/job-control/do_job_notification.c +++ b/42sh/src/job-control/do_job_notification.c @@ -27,13 +27,13 @@ int do_job_notification(void) { j = jlist->content; DG("checking job [%i]", j->id); - if (job_is_completed(j)) + if (job_is_completed(j->id)) { ret = 1; job_notify_change(j->id, 0); job_remove(j->id); } - else if (job_is_stopped(j) && !(j->attributes & JOB_NOTIFIED)) + else if (job_is_stopped(j->id) && !(j->attributes & JOB_NOTIFIED)) { ret = 1; job_notify_change(j->id, 8); diff --git a/42sh/src/job-control/job_is_completed.c b/42sh/src/job-control/job_is_completed.c index bafa92e3..5c7f5c41 100644 --- a/42sh/src/job-control/job_is_completed.c +++ b/42sh/src/job-control/job_is_completed.c @@ -12,15 +12,21 @@ #include "job_control.h" -int job_is_completed(t_job *job) +int job_is_completed(int id) { t_list *lst; + t_job *job; + t_jobc *jobc; t_process *p; + jobc = &data_singleton()->jobc; + job = ft_lst_find(jobc->first_job, &id, job_cmp_id)->content; lst = job->first_process; + DG("check"); while (lst) { p = lst->content; + DG("checking pid=%i", p->pid); if (!(p->attributes & PROCESS_COMPLETED)) { DG("process %i is not completed", p->pid); diff --git a/42sh/src/job-control/job_is_stopped.c b/42sh/src/job-control/job_is_stopped.c index d6248297..d707dbb3 100644 --- a/42sh/src/job-control/job_is_stopped.c +++ b/42sh/src/job-control/job_is_stopped.c @@ -12,11 +12,15 @@ #include "job_control.h" -int job_is_stopped(t_job *job) +int job_is_stopped(int id) { t_list *lst; + t_job *job; + t_jobc *jobc; t_process *p; + jobc = &data_singleton()->jobc; + job = ft_lst_find(jobc->first_job, &id, job_cmp_id)->content; lst = job->first_process; while (lst) { diff --git a/42sh/src/job-control/job_update_status.c b/42sh/src/job-control/job_update_status.c index 8f91c620..a8702233 100644 --- a/42sh/src/job-control/job_update_status.c +++ b/42sh/src/job-control/job_update_status.c @@ -18,7 +18,7 @@ void job_update_status(void) pid_t pid; DG("updating job status'"); - pid = waitpid (WAIT_ANY, &status, WUNTRACED|WNOHANG); + pid = waitpid (WAIT_ANY, &status, WUNTRACED | WNOHANG); while (!process_mark_status(pid, status)) - pid = waitpid (WAIT_ANY, &status, WUNTRACED|WNOHANG); + 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 e0a72b90..366ac227 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -12,17 +12,21 @@ #include "job_control.h" -int job_wait(t_job *job) +int job_wait(int id) { 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))) + while (!process_mark_status(pid, status) + && !job_is_stopped(id) + && !job_is_completed(id)) { + DG("waitpid now"); pid = waitpid(WAIT_ANY, &status, WUNTRACED); + DG("waitpid done"); } - return(0); + /* DG("stopped: %i", job_is_stopped(job)); */ + /* DG("completed: %i", job_is_completed(job)); */ + return (0); } diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/job-control/process_mark_status.c index e517c9eb..b35dbd68 100644 --- a/42sh/src/job-control/process_mark_status.c +++ b/42sh/src/job-control/process_mark_status.c @@ -16,9 +16,9 @@ int process_mark_status(pid_t pid, int status) { t_process *p; - DG("marking: pid=%i, status=%i", pid, status); if (pid > 1) { + DG("marking: pid=%i, status=%i", pid, status); if ((p = job_getprocess(pid))) { DG("found process pid=%i", pid); diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 8dc9d2f0..dd7bf428 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -27,7 +27,8 @@ int put_job_in_foreground(t_job *job, int cont) perror("kill (SIGCONT)"); } /* Wait for it to report. */ - job_wait(job); + DG("gonna wait for job"); + job_wait(job->id); job_remove(job->id); /* Put the shell back in the foreground. */ diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c index 4f587324..3bdd09e0 100644 --- a/42sh/src/job-control/sigchld_handler.c +++ b/42sh/src/job-control/sigchld_handler.c @@ -21,5 +21,5 @@ void sigchld_handler(int signo) DG("got asynchronous notification (SIGCHLD)"); /* if (do_job_notification()) */ /* ft_putstr(SHELL_PROMPT); */ - job_update_status(); + /* job_update_status(); */ } From ca89fcf53d348c74b263d42f086a5eb25c50436e Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 3 Jan 2017 18:47:41 +0100 Subject: [PATCH 19/27] stuff --- 42sh/src/exec/launch_process.c | 2 +- 42sh/src/exec/process_setgroup.c | 4 ++-- 42sh/src/job-control/builtin_jobs.c | 4 ++-- 42sh/src/job-control/job_addprocess.c | 9 ++++++--- 42sh/src/job-control/job_is_completed.c | 3 +-- 42sh/src/job-control/process_mark_status.c | 4 ++-- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index fdf799f6..1ac985b2 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/02 19:10:46 by jhalford ### ########.fr */ +/* Updated: 2017/01/03 18:01:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index eb05b38b..acd46689 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: 2016/12/13 17:48:11 by jhalford ### ########.fr */ +/* Updated: 2017/01/03 18:04:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ int process_setgroup(t_process *p) (void)p; job = &data_singleton()->exec.job; - pid = getpid(); + pid = p->pid; if (job->pgid == 0) job->pgid = pid; setpgid(pid, job->pgid); diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c index 06980138..9504dca0 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/01/02 19:11:19 by jhalford ### ########.fr */ +/* Updated: 2017/01/03 18:13:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) ft_printf("{mag}[%i] %c ", job->id, rank); if (lg) ft_printf("%i ", p->pid); - ft_printf("attr=%x ", job->attributes); + ft_printf("attr=%#b ", job->attributes); plist = job->first_process; while (plist) { diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index 1b1e714e..c1dd216e 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: 2017/01/02 18:16:12 by jhalford ### ########.fr */ +/* Updated: 2017/01/03 18:16:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,9 +29,12 @@ int job_addprocess(t_process *p) DG("added new job [%i]", job->id); } job = jobc->first_job->content; - ft_lstadd(&job->first_process, ft_lstnew(p, sizeof(*p))); + if (p->pid > 0) + { + ft_lstadd(&job->first_process, ft_lstnew(p, sizeof(*p))); + DG("added process to first_job : %i", p->pid); + } if (JOB_IS_BG(job->attributes) && IS_PIPEEND(p->attributes)) job_notify_new(job); - DG("added process to first_job : %i", p->pid); return(0); } diff --git a/42sh/src/job-control/job_is_completed.c b/42sh/src/job-control/job_is_completed.c index 5c7f5c41..7bd9dc0f 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: 2016/12/13 15:24:25 by jhalford ### ########.fr */ +/* Updated: 2017/01/03 18:12:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,6 @@ int job_is_completed(int id) jobc = &data_singleton()->jobc; job = ft_lst_find(jobc->first_job, &id, job_cmp_id)->content; lst = job->first_process; - DG("check"); while (lst) { p = lst->content; diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/job-control/process_mark_status.c index b35dbd68..85e709e6 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/15 15:12:38 by jhalford ### ########.fr */ +/* Updated: 2017/01/03 18:01:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ int process_mark_status(pid_t pid, int status) { p->attributes |= PROCESS_COMPLETED; if (WIFSIGNALED(status)) - ft_printf("%d: Terminated by signal %d.\n", + ft_printf("{mag}%d: Terminated by signal %d.\n{eoc}", (int) pid, WTERMSIG(p->status)); } return(0); From 9367075a9faddf739adf4c3afcc27c4eb22f2b9e Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 6 Jan 2017 17:42:56 +0100 Subject: [PATCH 20/27] pipelines work. next step: handle trmcaps heavy programs like vim (used to work tho in foreground mode before) --- 42sh/src/exec/exec_pipe.c | 10 +++++----- 42sh/src/exec/process_reset.c | 4 ++-- 42sh/src/job-control/builtin_jobs.c | 2 -- 42sh/src/job-control/job_addprocess.c | 2 +- 42sh/src/job-control/job_wait.c | 6 ++++-- 42sh/src/job-control/put_job_in_foreground.c | 3 ++- 42sh/src/job-control/sigchld_handler.c | 2 +- 7 files changed, 15 insertions(+), 14 deletions(-) diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 06820098..5ec96f8a 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -29,8 +29,9 @@ int exec_pipe(t_btree **ast) p->attributes &= ~PROCESS_PIPEEND; ft_exec(&(*ast)->left); p->attributes &= ~PROCESS_PIPESTART; - if (p->fdout != STDOUT) - close(p->fdout); + + DG("p->fdout=%i", p->fdout); + close(p->fdout); p->fdout = STDOUT; p->fdin = fds[PIPE_READ]; @@ -38,10 +39,9 @@ int exec_pipe(t_btree **ast) ft_exec(&(*ast)->right); if (start) p->attributes |= PROCESS_PIPESTART; - /* close(fds[PIPE_WRITE]); */ - /* close(fds[PIPE_READ]); */ + + close(p->fdin); p->fdin = STDIN; - p->fdout = STDOUT; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/process_reset.c b/42sh/src/exec/process_reset.c index d4ef0777..719862f1 100644 --- a/42sh/src/exec/process_reset.c +++ b/42sh/src/exec/process_reset.c @@ -7,8 +7,8 @@ void process_reset(void) data = data_singleton(); data->exec.process.path = NULL; data->exec.process.av = NULL; - data->exec.process.fdin = STDIN; - data->exec.process.fdout = STDOUT; + /* data->exec.process.fdin = STDIN; */ + /* data->exec.process.fdout = STDOUT; */ data->exec.process.pid = 0; data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; } diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c index 9504dca0..7571f0e9 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -37,8 +37,6 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) firstp = 1; job = jlist->content; ft_printf("{mag}[%i] %c ", job->id, rank); - if (lg) - ft_printf("%i ", p->pid); ft_printf("attr=%#b ", job->attributes); plist = job->first_process; while (plist) diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index c1dd216e..37061a13 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/job_addprocess.c @@ -31,7 +31,7 @@ int job_addprocess(t_process *p) job = jobc->first_job->content; if (p->pid > 0) { - ft_lstadd(&job->first_process, ft_lstnew(p, sizeof(*p))); + ft_lsteadd(&job->first_process, ft_lstnew(p, sizeof(*p))); DG("added process to first_job : %i", p->pid); } if (JOB_IS_BG(job->attributes) && IS_PIPEEND(p->attributes)) diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c index 366ac227..6b5f77de 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -17,6 +17,8 @@ int job_wait(int id) pid_t pid; int status; + if (job_is_stopped(id) || job_is_completed(id)) + return (0); pid = waitpid(WAIT_ANY, &status, WUNTRACED); while (!process_mark_status(pid, status) && !job_is_stopped(id) @@ -26,7 +28,7 @@ int job_wait(int id) pid = waitpid(WAIT_ANY, &status, WUNTRACED); DG("waitpid done"); } - /* DG("stopped: %i", job_is_stopped(job)); */ - /* DG("completed: %i", job_is_completed(job)); */ + DG("stopped: %i", job_is_stopped(id)); + DG("completed: %i", job_is_completed(id)); return (0); } diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index dd7bf428..a7a9185d 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -27,7 +27,8 @@ int put_job_in_foreground(t_job *job, int cont) perror("kill (SIGCONT)"); } /* Wait for it to report. */ - DG("gonna wait for job"); + DG("gonna wait for job id=%i", job->id); + /* if (!(p->attributes & PROCESS_BUILTIN && p->fdout == STDOUT)) */ job_wait(job->id); job_remove(job->id); diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c index 3bdd09e0..4f587324 100644 --- a/42sh/src/job-control/sigchld_handler.c +++ b/42sh/src/job-control/sigchld_handler.c @@ -21,5 +21,5 @@ void sigchld_handler(int signo) DG("got asynchronous notification (SIGCHLD)"); /* if (do_job_notification()) */ /* ft_putstr(SHELL_PROMPT); */ - /* job_update_status(); */ + job_update_status(); } From df1f449778995031fb9aa7a5e5ad60358e02f7a6 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sat, 7 Jan 2017 22:27:13 +0100 Subject: [PATCH 21/27] pipelines fixed --- 42sh/includes/job_control.h | 2 ++ 42sh/src/exec/exec_command.c | 5 +++-- 42sh/src/exec/process_redirect.c | 2 ++ 42sh/src/exec/process_reset.c | 6 ------ 42sh/src/exec/process_setgroup.c | 3 ++- 42sh/src/job-control/job_addprocess.c | 4 +--- 42sh/src/job-control/sigttin_handler.c | 19 +++++++++++++++++++ 42sh/src/job-control/sigttou_handler.c | 19 +++++++++++++++++++ 42sh/src/line-editing/sigint_handler.c | 18 ++++++++---------- 42sh/src/main/data_init.c | 9 ++++++++- 42sh/src/main/shell_init.c | 4 ++-- 11 files changed, 66 insertions(+), 25 deletions(-) create mode 100644 42sh/src/job-control/sigttin_handler.c create mode 100644 42sh/src/job-control/sigttou_handler.c diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 08e0952b..98b0c21c 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -73,6 +73,8 @@ 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); int process_cmp_pid(t_process *p, pid_t *pid); diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index fe010c2f..73248fcf 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -21,7 +21,6 @@ int exec_command(t_btree **ast) node = (*ast)->item; p = &data_singleton()->exec.process; job = &data_singleton()->exec.job; - /* job = data_singleton()->jobc.first_job->content; */ p->av = ft_sstrdup(node->data.sstr); process_setexec(p); if (!(launch_process(p))) @@ -32,7 +31,9 @@ int exec_command(t_btree **ast) put_job_in_foreground(job, 0): put_job_in_background(job, 0); } - process_reset(); + p->av = NULL; + p->pid = 0; + p->attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index 51e1ed1c..04c70cfc 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -16,11 +16,13 @@ int process_redirect(t_process *p) { if (p->fdin != STDIN) { + DG("dup2 %i->%i", p->fdin, STDIN); dup2(p->fdin, STDIN); close(p->fdin); } if (p->fdout != STDOUT) { + DG("dup2 %i->%i", p->fdout, STDOUT); dup2(p->fdout, STDOUT); close(p->fdout); } diff --git a/42sh/src/exec/process_reset.c b/42sh/src/exec/process_reset.c index 719862f1..4b3c02a9 100644 --- a/42sh/src/exec/process_reset.c +++ b/42sh/src/exec/process_reset.c @@ -5,10 +5,4 @@ void process_reset(void) t_data *data; data = data_singleton(); - data->exec.process.path = NULL; - data->exec.process.av = NULL; - /* data->exec.process.fdin = STDIN; */ - /* data->exec.process.fdout = STDOUT; */ - data->exec.process.pid = 0; - data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; } diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index acd46689..88be05e4 100644 --- a/42sh/src/exec/process_setgroup.c +++ b/42sh/src/exec/process_setgroup.c @@ -20,9 +20,10 @@ int process_setgroup(t_process *p) (void)p; job = &data_singleton()->exec.job; - pid = p->pid; + pid = getpid(); if (job->pgid == 0) job->pgid = pid; + DG("job->pgid=%i", job->pgid); setpgid(pid, job->pgid); if (JOB_IS_FG(job->attributes)) tcsetpgrp(STDIN_FILENO, job->pgid); diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index 37061a13..b3d24661 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/job_addprocess.c @@ -19,10 +19,8 @@ int job_addprocess(t_process *p) jobc = &data_singleton()->jobc; job = &data_singleton()->exec.job; - DG("check; attr=%b", p->attributes); if (IS_PIPESTART(p->attributes)) { - DG("check"); job_update_id(); job->id = jobc->current_id; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); @@ -32,7 +30,7 @@ int job_addprocess(t_process *p) if (p->pid > 0) { ft_lsteadd(&job->first_process, ft_lstnew(p, sizeof(*p))); - DG("added process to first_job : %i", p->pid); + DG("added pid=%i to [%i]", p->pid, job->id); } if (JOB_IS_BG(job->attributes) && IS_PIPEEND(p->attributes)) job_notify_new(job); diff --git a/42sh/src/job-control/sigttin_handler.c b/42sh/src/job-control/sigttin_handler.c new file mode 100644 index 00000000..4edd2696 --- /dev/null +++ b/42sh/src/job-control/sigttin_handler.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sigttin_handler.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */ +/* Updated: 2016/12/10 18:20:57 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void sigttin_handler(int signo) +{ + (void)signo; + DG("got SIGTTIN"); +} diff --git a/42sh/src/job-control/sigttou_handler.c b/42sh/src/job-control/sigttou_handler.c new file mode 100644 index 00000000..a0d318d6 --- /dev/null +++ b/42sh/src/job-control/sigttou_handler.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sigttou_handler.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */ +/* Updated: 2016/12/10 18:20:57 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void sigttou_handler(int signo) +{ + (void)signo; + DG("got SIGTTOU"); +} diff --git a/42sh/src/line-editing/sigint_handler.c b/42sh/src/line-editing/sigint_handler.c index d2929ece..123c4d41 100644 --- a/42sh/src/line-editing/sigint_handler.c +++ b/42sh/src/line-editing/sigint_handler.c @@ -12,17 +12,15 @@ #include "minishell.h" -pid_t g_pid; - void sigint_handler(int signo) { + t_job *job; + (void)signo; - if (signo == SIGINT) - { - DG("got SIGINT"); - if (g_pid) - kill(g_pid, SIGINT); - if (kill(g_pid, 0) == 0) - ft_putendl(""); - } + job = &data_singleton()->exec.job; + DG("got SIGINT; job->pgid=%i", job->pgid); + if (job->pgid) + kill(job->pgid, SIGINT); + if (kill(job->pgid, 0) == 0) + ft_putchar('\n'); } diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 4953aa39..9443e93a 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -23,7 +23,14 @@ int data_init(void) data->line.input = NULL; data->env = ft_sstrdup(environ); data->line.history = NULL; - process_reset(); + + data->exec.process.path = NULL; + data->exec.process.av = NULL; + data->exec.process.fdin = STDIN; + data->exec.process.fdout = STDOUT; + data->exec.process.pid = 0; + data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; + data->exec.aol_status = NULL; data->exec.aol_search = 0; data->exec.job.id = 0; diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 6419fa2c..10d4e773 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -26,8 +26,8 @@ void shell_init(void) signal(SIGINT, sigint_handler); signal(SIGQUIT, SIG_IGN); signal(SIGTSTP, sigtstp_handler); - signal(SIGTTIN, SIG_IGN); - signal(SIGTTOU, SIG_IGN); + signal(SIGTTIN, sigttin_handler); + signal(SIGTTOU, sigttou_handler); signal(SIGCHLD, sigchld_handler); *shell_pgid = getpid(); if (setpgid(*shell_pgid, *shell_pgid)) From bbdaedef0685837ca8245e9793fd0547e64be09b Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 8 Jan 2017 16:14:31 +0100 Subject: [PATCH 22/27] fg works with vim but not cat, probablty something to do w/ SIGTTIN or SIGTTOU. --- 42sh/includes/builtin.h | 3 +- 42sh/includes/exec.h | 2 +- 42sh/includes/job_control.h | 4 ++- 42sh/includes/minishell.h | 3 +- 42sh/src/builtin/builtin_exit.c | 21 +++++++++++-- 42sh/src/builtin/is_builtin.c | 3 +- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/exec/exec_pipe.c | 2 +- 42sh/src/exec/ft_exec.c | 2 +- 42sh/src/exec/launch_process.c | 10 ++++-- 42sh/src/exec/process_setgroup.c | 11 +++++-- 42sh/src/exec/set_exitstatus.c | 2 +- 42sh/src/job-control/builtin_fg.c | 31 +++++++++++++++++++ 42sh/src/job-control/builtin_jobs.c | 2 +- 42sh/src/job-control/do_job_notification.c | 6 ++-- 42sh/src/job-control/job_addprocess.c | 3 +- 42sh/src/job-control/job_is_completed.c | 2 +- 42sh/src/job-control/job_is_stopped.c | 2 +- 42sh/src/job-control/job_kill_all.c | 29 ++++++++++++++++++ 42sh/src/job-control/job_notify_change.c | 32 +++++++++++++++----- 42sh/src/job-control/job_remove.c | 19 +++++++----- 42sh/src/job-control/job_wait.c | 4 +-- 42sh/src/job-control/mark_job_as_running.c | 28 +++++++++++++++++ 42sh/src/job-control/process_mark_status.c | 8 +++-- 42sh/src/job-control/put_job_in_background.c | 2 +- 42sh/src/job-control/put_job_in_foreground.c | 28 ++++++++++------- 42sh/src/job-control/sigchld_handler.c | 7 +++-- 42sh/src/job-control/sigtstp_handler.c | 2 +- 42sh/src/job-control/sigttou_handler.c | 2 +- 42sh/src/line-editing/ft_key_ctrl_d.c | 6 ++-- 42sh/src/line-editing/ft_prompt.c | 2 +- 42sh/src/line-editing/ft_set_termios.c | 2 +- 42sh/src/line-editing/input_init.c | 2 +- 42sh/src/line-editing/sigint_handler.c | 11 ++----- 42sh/src/main/data_init.c | 2 +- 42sh/src/main/shell_exit.c | 7 +++-- 42sh/src/main/shell_init.c | 12 ++++---- 37 files changed, 230 insertions(+), 86 deletions(-) create mode 100644 42sh/src/job-control/builtin_fg.c create mode 100644 42sh/src/job-control/job_kill_all.c create mode 100644 42sh/src/job-control/mark_job_as_running.c diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index c56fd5a4..e54018ed 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/15 17:49:35 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 14:34:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,5 +24,6 @@ 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[]); +int builtin_fg(const char *path, char *const av[], char *const envp[]); #endif diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 2f5f338b..09861590 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/02 18:10:15 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 16:10:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 98b0c21c..513ceff3 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/02 18:10:03 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 16:10:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,6 +58,8 @@ int job_is_stopped(int id); int job_is_completed(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); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index e112ae6d..e1bfea6f 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/13 17:51:07 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 16:10:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ # include # include # include +# include enum e_mode diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index af3a7799..9fd927a8 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 18:00:26 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 16:00:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,28 @@ int builtin_exit(const char *path, char *const av[], char *const envp[]) { - int status; + int status; + static int notified = 0; + t_jobc *jobc; + t_list *jlist; + (void)envp; (void)path; + jobc = &data_singleton()->jobc; + jlist = jobc->first_job; + if (jlist && !notified) + { + notified = 1; + ft_dprintf(2, "{red}%s: you have live jobs (running or suspended).{eoc}\n", SHELL_NAME); + return (0); + } if (av[1]) status = ft_atoi(av[1]); else - status = ft_atoi(ft_getenv((char**)envp, "?")); + { + /* status = ft_atoi(ft_getenv(data_singleton()->env, "?")); */ + status = 0; + } exit(status); return (0); } diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index 80963e91..f0f653a5 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/15 17:48:23 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 14:30:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ t_stof g_builtin[] = { {"env", &builtin_env}, {"exit", &builtin_exit}, {"jobs", &builtin_jobs}, + {"fg", &builtin_fg}, {NULL, NULL}, }; diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 73248fcf..5998f3b4 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/02 19:10:01 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 15:24:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 5ec96f8a..f9af2ebe 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: 2016/12/13 17:15:22 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 11:03:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 33c2dbe3..a553c73f 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/15 15:18:21 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 11:03:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 1ac985b2..6bafdc78 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/03 18:01:30 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 15:23:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,13 +32,19 @@ int launch_process(t_process *p) if (p->attributes & (PROCESS_BINARY | PROCESS_SCRIPT) && access(p->path, X_OK) == -1) { - ft_dprintf(2, "%s: permission denied: %s\n", SHELL_NAME, p->av[0]); + ft_dprintf(2, "{red}%s: permission denied: %s{eoc}\n", SHELL_NAME, p->av[0]); return (-1); } pid = fork(); if (pid == 0) { process_setgroup(p); + signal(SIGINT, SIG_DFL); + signal(SIGQUIT, SIG_DFL); + signal(SIGTSTP, sigtstp_handler); + signal(SIGTTIN, SIG_DFL); + signal(SIGTTOU, SIG_DFL); + signal(SIGCHLD, SIG_DFL); process_redirect(p); (*p->execf)(p->path, p->av, data_singleton()->env); exit(42); diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index 88be05e4..9a947b2f 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/03 18:04:09 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 15:23:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,8 +24,13 @@ int process_setgroup(t_process *p) if (job->pgid == 0) job->pgid = pid; DG("job->pgid=%i", job->pgid); - setpgid(pid, job->pgid); + if (setpgid(pid, job->pgid)) + ft_dprintf(2, "{red}setpgid failed{eoc}\n"); if (JOB_IS_FG(job->attributes)) - tcsetpgrp(STDIN_FILENO, job->pgid); + { + signal(SIGTTOU, SIG_IGN); + tcsetpgrp(STDIN, job->pgid); + signal(SIGTTOU, SIG_DFL); + } return (0); } diff --git a/42sh/src/exec/set_exitstatus.c b/42sh/src/exec/set_exitstatus.c index eb22a1ee..584abdcc 100644 --- a/42sh/src/exec/set_exitstatus.c +++ b/42sh/src/exec/set_exitstatus.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2016/12/12 17:54:19 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 15:58:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/builtin_fg.c b/42sh/src/job-control/builtin_fg.c new file mode 100644 index 00000000..d538d039 --- /dev/null +++ b/42sh/src/job-control/builtin_fg.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_fg.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/08 14:30:07 by jhalford #+# #+# */ +/* Updated: 2017/01/08 15:21:23 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +int builtin_fg(const char *path, char *const av[], char *const envp[]) +{ + t_jobc *jobc; + t_job *job; + + (void)path; + (void)envp; + (void)av; + jobc = &data_singleton()->jobc; + job = jobc->first_job->content; + if (job) + { + mark_job_as_running(job); + put_job_in_foreground(job, 1); + } + return (0); +} diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c index 7571f0e9..b0c45498 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/01/03 18:13:47 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 15:33:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/do_job_notification.c b/42sh/src/job-control/do_job_notification.c index c902b1ec..80db0521 100644 --- a/42sh/src/job-control/do_job_notification.c +++ b/42sh/src/job-control/do_job_notification.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 13:01:19 by jhalford #+# #+# */ -/* Updated: 2017/01/02 18:21:20 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 14:00:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,8 +36,8 @@ int do_job_notification(void) else if (job_is_stopped(j->id) && !(j->attributes & JOB_NOTIFIED)) { ret = 1; - job_notify_change(j->id, 8); - j->attributes &= JOB_NOTIFIED; + job_notify_change(j->id, -1); + 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 b3d24661..2dd27595 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: 2017/01/03 18:16:00 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 15:41:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ int job_addprocess(t_process *p) { job_update_id(); job->id = jobc->current_id; + job->pgid = p->pid; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); DG("added new job [%i]", job->id); } diff --git a/42sh/src/job-control/job_is_completed.c b/42sh/src/job-control/job_is_completed.c index 7bd9dc0f..4fba4555 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/03 18:12:26 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 14:04:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_is_stopped.c b/42sh/src/job-control/job_is_stopped.c index d707dbb3..e3bddaa1 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/15 12:42:02 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 14:39:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_kill_all.c b/42sh/src/job-control/job_kill_all.c new file mode 100644 index 00000000..11f343a7 --- /dev/null +++ b/42sh/src/job-control/job_kill_all.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_kill_all.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/08 15:36:56 by jhalford #+# #+# */ +/* Updated: 2017/01/08 15:44:07 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void job_kill_all(void) +{ + t_jobc *jobc; + t_list *jlist; + t_job *job; + + jobc = &data_singleton()->jobc; + jlist = jobc->first_job; + while (jlist) + { + job = jlist->content; + kill(-job->pgid, SIGKILL); + jlist = jlist->next; + } +} diff --git a/42sh/src/job-control/job_notify_change.c b/42sh/src/job-control/job_notify_change.c index 3865dfd7..a86561a2 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: 2016/12/15 17:45:36 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 13:52:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,8 @@ void job_notify_change(int id, int status) { t_job *job; t_jobc *jobc; + t_list *plist; + t_process *p; char rank; rank = ' '; @@ -30,13 +32,27 @@ void job_notify_change(int id, int status) rank = '-'; } ft_printf("{mag}[%i] %c ", id, rank); - if (status == 0) - ft_printf("{gre}done{mag}"); - else if (status == 8) + job = ft_lst_find(jobc->first_job, &id, job_cmp_id)->content; + if (status == -1) ft_printf("{red}stopped{mag}"); - else if (status == 9) - ft_printf("{red}killed{mag}"); else - ft_printf("exit %i", status); - ft_printf("\t 'process command goes here'{eoc}\n"); + { + plist = job->first_process; + p = ft_lstlast(job->first_process)->content; + if (p->status == 0) + ft_printf("{gre}done{mag}"); + else + ft_printf("{red}exit %i{mag}", p->status); + } + ft_printf("\t "); + plist = job->first_process; + while (plist) + { + p = plist->content; + ft_sstrprint(p->av, ' '); + if (plist->next) + ft_printf(" |"); + 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 index 86cf4ee2..697cc018 100644 --- a/42sh/src/job-control/job_remove.c +++ b/42sh/src/job-control/job_remove.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:51:08 by jhalford #+# #+# */ -/* Updated: 2017/01/02 18:15:03 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 14:04:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,13 +17,18 @@ void job_remove(int id) t_jobc *jobc; jobc = &data_singleton()->jobc; - DG("job_remove"); - if (id < data_singleton()->jobc.current_id) + if (job_is_completed(id)) { - data_singleton()->jobc.current_id = id; - DG("ID_UPDATE(downgrade):%i", id); + DG("job_remove"); + if (id < data_singleton()->jobc.current_id) + { + data_singleton()->jobc.current_id = id; + DG("ID_UPDATE(downgrade):%i", id); + } + else + DG("ID_UPDATE(no downgrade): %i/%i", id, data_singleton()->jobc.current_id); + ft_lst_delif(&jobc->first_job, &id, job_cmp_id, job_free); } else - DG("ID_UPDATE(no downgrade): %i/%i", id, data_singleton()->jobc.current_id); - ft_lst_delif(&jobc->first_job, &id, job_cmp_id, job_free); + DG("job_remove failed (not completed)"); } diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c index 6b5f77de..9a892b03 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 11:49:05 by jhalford #+# #+# */ -/* Updated: 2017/01/02 17:32:43 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 16:00:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,5 @@ int job_wait(int id) pid = waitpid(WAIT_ANY, &status, WUNTRACED); DG("waitpid done"); } - DG("stopped: %i", job_is_stopped(id)); - DG("completed: %i", job_is_completed(id)); return (0); } diff --git a/42sh/src/job-control/mark_job_as_running.c b/42sh/src/job-control/mark_job_as_running.c new file mode 100644 index 00000000..4a1c679e --- /dev/null +++ b/42sh/src/job-control/mark_job_as_running.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mark_job_as_running.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/08 14:40:40 by jhalford #+# #+# */ +/* Updated: 2017/01/08 14:53:34 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void mark_job_as_running (t_job *j) +{ + t_list *plist; + t_process *p; + + plist = j->first_process; + while (plist) + { + p = plist->content; + p->attributes &= ~PROCESS_STOPPED; + plist = plist->next; + } + j->attributes &= ~JOB_NOTIFIED; +} diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/job-control/process_mark_status.c index 85e709e6..1ea54e4b 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/03 18:01:15 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 14:55:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,15 +18,17 @@ int process_mark_status(pid_t pid, int status) if (pid > 1) { - DG("marking: pid=%i, status=%i", pid, status); if ((p = job_getprocess(pid))) { - DG("found process pid=%i", pid); p->status = status; if (WIFSTOPPED(status)) + { + DG("marking: pid=%i, status=%i (stopped)", pid, status); p->attributes |= PROCESS_STOPPED; + } else { + DG("marking: pid=%i, status=%i (completed)", pid, status); 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_background.c b/42sh/src/job-control/put_job_in_background.c index 38542ffc..a23dee5e 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/15 17:53:24 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 13:24:53 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 a7a9185d..ede7df10 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/02 18:15:09 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 15:33:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,26 +17,32 @@ int put_job_in_foreground(t_job *job, int cont) t_jobc *jobc; jobc = &data_singleton()->jobc; - /* Put the job into the foreground. */ - tcsetpgrp(STDIN_FILENO, job->pgid); - /* Send the job a continue signal, if necessary. */ + /* 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) { - tcsetattr (STDIN_FILENO, TCSADRAIN, &job->tmodes); + tcsetattr (STDIN, TCSANOW, &job->tmodes); if (kill(-job->pgid, SIGCONT) < 0) perror("kill (SIGCONT)"); } /* Wait for it to report. */ DG("gonna wait for job id=%i", job->id); - /* if (!(p->attributes & PROCESS_BUILTIN && p->fdout == STDOUT)) */ job_wait(job->id); job_remove(job->id); - /* Put the shell back in the foreground. */ - tcsetpgrp(STDIN_FILENO, jobc->shell_pgid); + /* Put the shell back in the foreground. */ + signal(SIGTTOU, SIG_IGN); + tcsetpgrp(STDIN, jobc->shell_pgid); + signal(SIGTTOU, sigttou_handler); - /* Restore the shell’s terminal modes. */ - tcgetattr(STDIN_FILENO, &job->tmodes); - tcsetattr(STDIN_FILENO, TCSADRAIN, &jobc->shell_tmodes); + /* Restore the shell’s terminal modes. */ + tcgetattr(STDIN, &job->tmodes); + tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); return (0); } diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c index 4f587324..c750bba6 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: 2017/01/02 18:10:01 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 11:28:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,8 +18,9 @@ void sigchld_handler(int signo) (void)signo; data = data_singleton(); - DG("got asynchronous notification (SIGCHLD)"); + DG("got SIGCHLD"); /* if (do_job_notification()) */ /* ft_putstr(SHELL_PROMPT); */ - job_update_status(); + if (data_singleton()->mode != MODE_EXEC) + job_update_status(); } diff --git a/42sh/src/job-control/sigtstp_handler.c b/42sh/src/job-control/sigtstp_handler.c index 95814e84..5c02ec84 100644 --- a/42sh/src/job-control/sigtstp_handler.c +++ b/42sh/src/job-control/sigtstp_handler.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */ -/* Updated: 2016/12/10 18:20:57 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 14:30:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/sigttou_handler.c b/42sh/src/job-control/sigttou_handler.c index a0d318d6..a999d1e1 100644 --- a/42sh/src/job-control/sigttou_handler.c +++ b/42sh/src/job-control/sigttou_handler.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */ -/* Updated: 2016/12/10 18:20:57 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 13:24:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_key_ctrl_d.c b/42sh/src/line-editing/ft_key_ctrl_d.c index 1b586098..037f084b 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/12 17:40:27 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 15:56:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,5 +17,7 @@ int ft_key_ctrl_d(t_data *data, char *buf) (void)data; (void)buf; ft_putendl("exit"); - exit(0); + builtin_exit("exit", (char *[]){"exit", NULL}, data_singleton()->env); + ft_prompt(); + return (0); } diff --git a/42sh/src/line-editing/ft_prompt.c b/42sh/src/line-editing/ft_prompt.c index 548fb4fe..5f37d810 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/02 18:19:24 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 16:00:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_set_termios.c b/42sh/src/line-editing/ft_set_termios.c index 04fa8453..b0277f51 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/12 17:35:27 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 14:26:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/input_init.c b/42sh/src/line-editing/input_init.c index 84ce1af7..6ef69843 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/12 17:35:15 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 14:27:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/sigint_handler.c b/42sh/src/line-editing/sigint_handler.c index 123c4d41..7d45df67 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: 2016/12/10 15:31:56 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 15:12:58 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,6 @@ void sigint_handler(int signo) { - t_job *job; - (void)signo; - job = &data_singleton()->exec.job; - DG("got SIGINT; job->pgid=%i", job->pgid); - if (job->pgid) - kill(job->pgid, SIGINT); - if (kill(job->pgid, 0) == 0) - ft_putchar('\n'); + DG("got SIGINT"); } diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 9443e93a..e6911ed2 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/13 13:38:33 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 14:26:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_exit.c b/42sh/src/main/shell_exit.c index 3d12fa63..882a3f31 100644 --- a/42sh/src/main/shell_exit.c +++ b/42sh/src/main/shell_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:37:04 by jhalford #+# #+# */ -/* Updated: 2016/12/12 17:51:20 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 15:55:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,9 @@ extern char *BC; void shell_exit(void) { - DG("cleanup. char * UP at %p", UP); - DG("cleanup. char * BC at %p", BC); + /* DG("cleanup. char * UP at %p", UP); */ + /* DG("cleanup. char * BC at %p", BC); */ data_exit(); + /* job_kill_all(); */ tcsetattr(0, TCSANOW, &data_singleton()->jobc.shell_tmodes); } diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 10d4e773..70a149ef 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: 2016/12/13 15:14:35 by jhalford ### ########.fr */ +/* Updated: 2017/01/08 15:11:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,13 +19,13 @@ void shell_init(void) shell_pgid = &data_singleton()->jobc.shell_pgid; data_init(); atexit(&shell_exit); - if (isatty(STDIN_FILENO)) + if (isatty(STDIN)) { - while (tcgetpgrp(STDIN_FILENO) != (*shell_pgid = getpgrp())) + while (tcgetpgrp(STDIN) != (*shell_pgid = getpgrp())) kill(-*shell_pgid, SIGTTIN); signal(SIGINT, sigint_handler); signal(SIGQUIT, SIG_IGN); - signal(SIGTSTP, sigtstp_handler); + signal(SIGTSTP, SIG_IGN); signal(SIGTTIN, sigttin_handler); signal(SIGTTOU, sigttou_handler); signal(SIGCHLD, sigchld_handler); @@ -35,7 +35,7 @@ void shell_init(void) 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); + tcsetpgrp(STDIN, *shell_pgid); + tcgetattr(STDIN, &data_singleton()->jobc.shell_tmodes); } } From 204d5b9795a6bfac588635699e4bbeef918a1e4b Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 9 Jan 2017 16:01:09 +0100 Subject: [PATCH 23/27] pipelines broken in this commit --- 42sh/includes/exec.h | 22 ++++--- 42sh/includes/job_control.h | 35 ++++++----- 42sh/includes/minishell.h | 2 +- 42sh/includes/types.h | 2 +- 42sh/libft | 2 +- 42sh/src/builtin/builtin_setenv.c | 5 +- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/exec/exec_pipe.c | 5 +- 42sh/src/exec/launch_process.c | 6 +- 42sh/src/exec/process_redirect.c | 6 +- 42sh/src/exec/process_setexec.c | 7 +-- 42sh/src/exec/process_setgroup.c | 5 +- 42sh/src/job-control/builtin_jobs.c | 51 +++++----------- 42sh/src/job-control/job_cmp_id.c | 2 +- 42sh/src/job-control/job_format.c | 28 +++++++++ 42sh/src/job-control/job_format_head.c | 27 +++++++++ 42sh/src/job-control/job_free.c | 2 +- 42sh/src/job-control/job_getrank.c | 36 ++++++++++++ 42sh/src/job-control/job_is_completed.c | 2 +- 42sh/src/job-control/job_is_stopped.c | 4 +- 42sh/src/job-control/job_notify_change.c | 2 +- 42sh/src/job-control/mark_job_as_running.c | 5 +- 42sh/src/job-control/process_format.c | 61 ++++++++++++++++++++ 42sh/src/job-control/process_mark_status.c | 6 +- 42sh/src/job-control/put_job_in_foreground.c | 17 +++--- 42sh/src/line-editing/ft_interactive_sh.c | 2 +- 42sh/src/line-editing/ft_prompt.c | 2 +- 42sh/src/line-editing/ft_set_termios.c | 6 +- 42sh/src/line-editing/input_init.c | 2 +- 42sh/src/line-editing/sigint_handler.c | 2 +- 42sh/src/main/shell_init.c | 2 +- 31 files changed, 249 insertions(+), 109 deletions(-) create mode 100644 42sh/src/job-control/job_format.c create mode 100644 42sh/src/job-control/job_format_head.c create mode 100644 42sh/src/job-control/job_getrank.c create mode 100644 42sh/src/job-control/process_format.c 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 */ /* */ /* ************************************************************************** */ From 153b9aec7927a3e5ef5fe18d9f6373db3bd836d5 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 9 Jan 2017 16:26:54 +0100 Subject: [PATCH 24/27] pipelines fixed, job messaging better for notification & for builtin_jobs. still gotta fix cat (See previous commits msgs) --- 42sh/includes/exec.h | 2 +- 42sh/includes/job_control.h | 5 +-- 42sh/src/builtin/builtin_exit.c | 2 +- 42sh/src/exec/exec_pipe.c | 4 +- 42sh/src/job-control/builtin_jobs.c | 2 +- 42sh/src/job-control/do_job_notification.c | 8 ++-- 42sh/src/job-control/job_kill_all.c | 2 +- 42sh/src/job-control/job_notify_change.c | 43 +++------------------- 42sh/src/line-editing/sigint_handler.c | 2 +- 42sh/src/main/shell_exit.c | 4 +- 10 files changed, 20 insertions(+), 54 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 5c01cda8..b3346742 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/09 15:57:08 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:22:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index b7147f44..f83713c0 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/09 14:05:43 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:23:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,7 @@ 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_notify_change(int id); void job_format(t_job *j, int rank[2], int opts); void job_format_head(t_job *j, int rank[2]); @@ -71,7 +71,6 @@ void job_kill_all(void); int put_job_in_foreground(t_job *job, int cont); int put_job_in_background(t_job *job, int cont); - 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]); diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index 9fd927a8..80148cb8 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: 2017/01/08 16:00:21 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:25:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 2907c020..8e777ae1 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/09 15:56:40 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:19:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ int exec_pipe(t_btree **ast) if (start) p->attributes |= PROCESS_PIPESTART; - close(p->fdin); + close(fds[PIPE_READ]); p->fdin = STDIN; btree_delone(ast, &ast_free); return (0); diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c index 032e0a47..8ce060dc 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/01/09 14:05:27 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:20:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/do_job_notification.c b/42sh/src/job-control/do_job_notification.c index 80db0521..98db5d4d 100644 --- a/42sh/src/job-control/do_job_notification.c +++ b/42sh/src/job-control/do_job_notification.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 13:01:19 by jhalford #+# #+# */ -/* Updated: 2017/01/08 14:00:23 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:24:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,17 +26,17 @@ int do_job_notification(void) while (jlist) { j = jlist->content; - DG("checking job [%i]", j->id); + DG("checking [%i]", j->id); if (job_is_completed(j->id)) { ret = 1; - job_notify_change(j->id, 0); + job_notify_change(j->id); job_remove(j->id); } else if (job_is_stopped(j->id) && !(j->attributes & JOB_NOTIFIED)) { ret = 1; - job_notify_change(j->id, -1); + job_notify_change(j->id); j->attributes |= JOB_NOTIFIED; } jlist = jlist->next; diff --git a/42sh/src/job-control/job_kill_all.c b/42sh/src/job-control/job_kill_all.c index 11f343a7..158b716a 100644 --- a/42sh/src/job-control/job_kill_all.c +++ b/42sh/src/job-control/job_kill_all.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/08 15:36:56 by jhalford #+# #+# */ -/* Updated: 2017/01/08 15:44:07 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:25:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_notify_change.c b/42sh/src/job-control/job_notify_change.c index 8a99d535..d1e4a292 100644 --- a/42sh/src/job-control/job_notify_change.c +++ b/42sh/src/job-control/job_notify_change.c @@ -6,53 +6,20 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 15:04:03 by jhalford #+# #+# */ -/* Updated: 2017/01/09 12:39:33 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:24:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -void job_notify_change(int id, int status) +void job_notify_change(int id) { t_job *job; t_jobc *jobc; - t_list *plist; - t_process *p; - char rank; + int rank[2]; - rank = ' '; jobc = &data_singleton()->jobc; - job = jobc->first_job->content; - if (id == job->id) - rank = '+'; - else if (jobc->first_job->next) - { - job = jobc->first_job->next->content; - if (id == job->id) - rank = '-'; - } - ft_printf("{mag}[%i] %c ", id, rank); job = ft_lst_find(jobc->first_job, &id, job_cmp_id)->content; - if (status == -1) - ft_printf("{red}stopped{mag}"); - else - { - plist = job->first_process; - p = ft_lstlast(job->first_process)->content; - if (p->status == 0) - ft_printf("{gre}done{mag}"); - else - ft_printf("{red}exit %i{mag}", p->status); - } - ft_printf("\t "); - plist = job->first_process; - while (plist) - { - p = plist->content; - ft_sstrprint(p->av, ' '); - if (plist->next) - ft_printf(" |"); - plist = plist->next; - } - ft_printf("{eoc}\n"); + job_getrank(&rank); + job_format(job, rank, 0); } diff --git a/42sh/src/line-editing/sigint_handler.c b/42sh/src/line-editing/sigint_handler.c index 0c2dad9c..f15da9d3 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/09 15:59:05 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:16:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_exit.c b/42sh/src/main/shell_exit.c index 882a3f31..02e32f49 100644 --- a/42sh/src/main/shell_exit.c +++ b/42sh/src/main/shell_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:37:04 by jhalford #+# #+# */ -/* Updated: 2017/01/08 15:55:39 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:25:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,6 @@ void shell_exit(void) /* DG("cleanup. char * UP at %p", UP); */ /* DG("cleanup. char * BC at %p", BC); */ data_exit(); - /* job_kill_all(); */ + job_kill_all(); tcsetattr(0, TCSANOW, &data_singleton()->jobc.shell_tmodes); } From 9d02933f729b0d6463e24cbdb8070889a45701a3 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 9 Jan 2017 17:00:56 +0100 Subject: [PATCH 25/27] builtin_bg done, doesnt take arguments yet --- 42sh/includes/builtin.h | 3 +- 42sh/includes/exec.h | 5 ++-- 42sh/includes/job_control.h | 2 +- 42sh/src/builtin/is_builtin.c | 3 +- 42sh/src/exec/launch_process.c | 6 ++-- 42sh/src/job-control/builtin_bg.c | 35 ++++++++++++++++++++++ 42sh/src/job-control/builtin_fg.c | 2 +- 42sh/src/job-control/job_format.c | 2 +- 42sh/src/job-control/job_kill_all.c | 2 +- 42sh/src/job-control/job_notify_change.c | 4 +-- 42sh/src/job-control/job_remove.c | 7 +---- 42sh/src/job-control/mark_job_as_running.c | 4 +-- 42sh/src/job-control/process_format.c | 15 ++++++++-- 42sh/src/job-control/sigtstp_handler.c | 2 +- 42sh/src/job-control/sigttin_handler.c | 2 +- 42sh/src/main/shell_exit.c | 2 +- 16 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 42sh/src/job-control/builtin_bg.c diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index e54018ed..42f2543a 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: 2017/01/08 14:34:40 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:57:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,5 +25,6 @@ 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[]); int builtin_fg(const char *path, char *const av[], char *const envp[]); +int builtin_bg(const char *path, char *const av[], char *const envp[]); #endif diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index b3346742..5d047b1b 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/09 16:22:39 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:58:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,9 +25,10 @@ # define PROCESS_COMPLETED (1 << 6) # define PROCESS_SUSPENDED (1 << 7) # define PROCESS_RUNNING (1 << 8) +# define PROCESS_CONTINUED (1 << 9) # define PROCESS_TYPE_MASK (1 << 0 | 1 << 1 | 1 << 2 | 1 << 3) -# define PROCESS_STATE_MASK (1 << 6 | 1 << 7 | 1 << 8) +# define PROCESS_STATE_MASK (1 << 6 | 1 << 7 | 1 << 8 | 1 << 9) # 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 f83713c0..bea10d93 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/09 16:23:37 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:56:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index f0f653a5..7dbdda0c 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: 2017/01/08 14:30:52 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:58:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ t_stof g_builtin[] = { {"exit", &builtin_exit}, {"jobs", &builtin_jobs}, {"fg", &builtin_fg}, + {"bg", &builtin_bg}, {NULL, NULL}, }; diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index b07ede0f..1ca9757e 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/09 16:00:07 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:38:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,8 +44,8 @@ int launch_process(t_process *p) signal(SIGINT, SIG_DFL); signal(SIGQUIT, SIG_DFL); signal(SIGTSTP, sigtstp_handler); - signal(SIGTTIN, SIG_DFL); - signal(SIGTTOU, SIG_DFL); + signal(SIGTTIN, sigttin_handler); + signal(SIGTTOU, sigttou_handler); signal(SIGCHLD, SIG_DFL); process_redirect(p); (*p->execf)(p->path, p->av, data_singleton()->env); diff --git a/42sh/src/job-control/builtin_bg.c b/42sh/src/job-control/builtin_bg.c new file mode 100644 index 00000000..427bf7d2 --- /dev/null +++ b/42sh/src/job-control/builtin_bg.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_bg.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/09 16:54:18 by jhalford #+# #+# */ +/* Updated: 2017/01/09 16:57:20 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + + +#include "job_control.h" + +int builtin_bg(const char *path, char *const av[], char *const envp[]) +{ + t_jobc *jobc; + t_job *job; + int rank[2]; + + (void)path; + (void)envp; + (void)av; + jobc = &data_singleton()->jobc; + job = jobc->first_job->content; + job_getrank(&rank); + if (job) + { + mark_job_as_running(job); + job_format(job, rank, JOBS_OPTS_L); + put_job_in_background(job, 1); + } + return (0); +} diff --git a/42sh/src/job-control/builtin_fg.c b/42sh/src/job-control/builtin_fg.c index d538d039..2f350ea6 100644 --- a/42sh/src/job-control/builtin_fg.c +++ b/42sh/src/job-control/builtin_fg.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/08 14:30:07 by jhalford #+# #+# */ -/* Updated: 2017/01/08 15:21:23 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:54:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_format.c b/42sh/src/job-control/job_format.c index 58ee1a10..0117d3f8 100644 --- a/42sh/src/job-control/job_format.c +++ b/42sh/src/job-control/job_format.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 12:47:17 by jhalford #+# #+# */ -/* Updated: 2017/01/09 14:05:06 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:55:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_kill_all.c b/42sh/src/job-control/job_kill_all.c index 158b716a..1d36a8a6 100644 --- a/42sh/src/job-control/job_kill_all.c +++ b/42sh/src/job-control/job_kill_all.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/08 15:36:56 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:25:25 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:35:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_notify_change.c b/42sh/src/job-control/job_notify_change.c index d1e4a292..7d0c4ead 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/09 16:24:32 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:49:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,5 +21,5 @@ void job_notify_change(int id) jobc = &data_singleton()->jobc; job = ft_lst_find(jobc->first_job, &id, job_cmp_id)->content; job_getrank(&rank); - job_format(job, rank, 0); + job_format(job, rank, JOBS_OPTS_L); } diff --git a/42sh/src/job-control/job_remove.c b/42sh/src/job-control/job_remove.c index 697cc018..297969b9 100644 --- a/42sh/src/job-control/job_remove.c +++ b/42sh/src/job-control/job_remove.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:51:08 by jhalford #+# #+# */ -/* Updated: 2017/01/08 14:04:48 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:46:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,12 +21,7 @@ void job_remove(int id) { DG("job_remove"); if (id < data_singleton()->jobc.current_id) - { data_singleton()->jobc.current_id = id; - DG("ID_UPDATE(downgrade):%i", id); - } - else - DG("ID_UPDATE(no downgrade): %i/%i", id, data_singleton()->jobc.current_id); ft_lst_delif(&jobc->first_job, &id, job_cmp_id, job_free); } else diff --git a/42sh/src/job-control/mark_job_as_running.c b/42sh/src/job-control/mark_job_as_running.c index 3938cd9f..c10a2b5b 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/09 14:03:36 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:58:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ void mark_job_as_running (t_job *j) { p = plist->content; p->attributes &= ~PROCESS_STATE_MASK; - p->attributes |= PROCESS_RUNNING; + p->attributes |= PROCESS_CONTINUED; 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 index db30bcaf..d9b0dd4f 100644 --- a/42sh/src/job-control/process_format.c +++ b/42sh/src/job-control/process_format.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:05:55 by jhalford #+# #+# */ -/* Updated: 2017/01/09 14:19:50 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:58:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,12 @@ void process_format(t_list **plist, int firstp, int opts) ft_putstr("running "); else if (state == PROCESS_SUSPENDED) ft_putstr("suspended"); + else if (state == PROCESS_CONTINUED) + { + ft_putstr("continued"); + p->attributes &= ~PROCESS_STATE_MASK; + p->attributes &= ~PROCESS_RUNNING; + } else if (state == PROCESS_COMPLETED) { if (p->status == 0) @@ -51,9 +57,14 @@ void process_format(t_list **plist, int firstp, int opts) if (!(p->attributes & state) || (state == PROCESS_COMPLETED && p->status != 0)) break; + if (p->attributes & PROCESS_CONTINUED) + { + p->attributes &= ~PROCESS_STATE_MASK; + p->attributes &= ~PROCESS_RUNNING; + } ft_sstrprint(p->av, ' '); if ((*plist)->next) - ft_putstr(" |"); + ft_putstr(" | "); (*plist) = (*plist)->next; } } diff --git a/42sh/src/job-control/sigtstp_handler.c b/42sh/src/job-control/sigtstp_handler.c index 5c02ec84..15f3316c 100644 --- a/42sh/src/job-control/sigtstp_handler.c +++ b/42sh/src/job-control/sigtstp_handler.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */ -/* Updated: 2017/01/08 14:30:53 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:37:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/sigttin_handler.c b/42sh/src/job-control/sigttin_handler.c index 4edd2696..1b424bf5 100644 --- a/42sh/src/job-control/sigttin_handler.c +++ b/42sh/src/job-control/sigttin_handler.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */ -/* Updated: 2016/12/10 18:20:57 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:37:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_exit.c b/42sh/src/main/shell_exit.c index 02e32f49..2cf67c0e 100644 --- a/42sh/src/main/shell_exit.c +++ b/42sh/src/main/shell_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:37:04 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:25:04 by jhalford ### ########.fr */ +/* Updated: 2017/01/09 16:35:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From 0ec9313b4c6232332dd519c551a7d83256bae7da Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 10 Jan 2017 11:32:02 +0100 Subject: [PATCH 26/27] pretty stable version of job control, ready to merge --- 42sh/includes/exec.h | 2 +- 42sh/libft | 2 +- 42sh/src/exec/exec_command.c | 5 ++++- 42sh/src/exec/launch_process.c | 2 +- 42sh/src/job-control/builtin_bg.c | 14 +++++++++++--- 42sh/src/job-control/builtin_fg.c | 16 +++++++++++++--- 42sh/src/job-control/builtin_jobs.c | 2 +- 42sh/src/job-control/job_cmp_id.c | 2 +- 42sh/src/job-control/job_format.c | 2 +- 42sh/src/job-control/job_getrank.c | 20 +++++++++----------- 42sh/src/job-control/job_is_stopped.c | 2 +- 42sh/src/job-control/job_notify_change.c | 2 +- 42sh/src/job-control/mark_job_as_running.c | 9 ++++++--- 42sh/src/job-control/process_format.c | 5 ++--- 42sh/src/job-control/process_mark_status.c | 6 +++--- 42sh/src/job-control/put_job_in_foreground.c | 3 +-- 42sh/src/job-control/sigtstp_handler.c | 2 +- 17 files changed, 58 insertions(+), 38 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 5d047b1b..4146a16e 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/09 16:58:55 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:23:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft b/42sh/libft index eded1f1d..d799465c 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit eded1f1d189a6fd631b705f9d4a466fe088b94b3 +Subproject commit d799465c2e0d51f24fe4d5cf1e51c5109a1617a0 diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index fcb2e0ab..23c8d28a 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/09 12:20:24 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:03:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,9 +27,12 @@ int exec_command(t_btree **ast) { job_addprocess(p); if (IS_PIPEEND(p->attributes)) + { JOB_IS_FG(job->attributes) ? put_job_in_foreground(job, 0): put_job_in_background(job, 0); + job->pgid = 0; + } } p->av = NULL; p->pid = 0; diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 1ca9757e..70b1175f 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/09 16:38:21 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:30:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/builtin_bg.c b/42sh/src/job-control/builtin_bg.c index 427bf7d2..9e98b5e6 100644 --- a/42sh/src/job-control/builtin_bg.c +++ b/42sh/src/job-control/builtin_bg.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 16:54:18 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:57:20 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:11:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,19 +17,27 @@ int builtin_bg(const char *path, char *const av[], char *const envp[]) { t_jobc *jobc; t_job *job; + t_list *jlist; int rank[2]; + int id; (void)path; (void)envp; (void)av; jobc = &data_singleton()->jobc; - job = jobc->first_job->content; job_getrank(&rank); - if (job) + id = av[1] ? ft_atoi(av[1]) : rank[0]; + if (id == 0 && !av[1]) + return (0); + jlist = ft_lst_find(jobc->first_job, &id, job_cmp_id); + if (jlist) { + job = jlist->content; mark_job_as_running(job); job_format(job, rank, JOBS_OPTS_L); put_job_in_background(job, 1); } + else + ft_dprintf(2, "{red}bg: job not found: %i{eoc}\n", id); return (0); } diff --git a/42sh/src/job-control/builtin_fg.c b/42sh/src/job-control/builtin_fg.c index 2f350ea6..c594df80 100644 --- a/42sh/src/job-control/builtin_fg.c +++ b/42sh/src/job-control/builtin_fg.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/08 14:30:07 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:54:39 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:16:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,16 +16,26 @@ int builtin_fg(const char *path, char *const av[], char *const envp[]) { t_jobc *jobc; t_job *job; + t_list *jlist; + int rank[2]; + int id; (void)path; (void)envp; (void)av; jobc = &data_singleton()->jobc; - job = jobc->first_job->content; - if (job) + job_getrank(&rank); + id = av[1] ? ft_atoi(av[1]) : rank[0]; + if (id == 0 && !av[1]) + return (0); + jlist = ft_lst_find(jobc->first_job, &id, job_cmp_id); + if (jlist) { + job = jlist->content; mark_job_as_running(job); put_job_in_foreground(job, 1); } + else + ft_dprintf(2, "{red}fg: job not found: [%i]{eoc}\n", id); return (0); } diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c index 8ce060dc..46b73513 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:20:09 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:16:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_cmp_id.c b/42sh/src/job-control/job_cmp_id.c index 50d6ada8..cc9614d2 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: 2017/01/09 12:34:05 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:08:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_format.c b/42sh/src/job-control/job_format.c index 0117d3f8..1c8f3dd1 100644 --- a/42sh/src/job-control/job_format.c +++ b/42sh/src/job-control/job_format.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 12:47:17 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:55:36 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:16:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_getrank.c b/42sh/src/job-control/job_getrank.c index e24f3e03..8c7b6996 100644 --- a/42sh/src/job-control/job_getrank.c +++ b/42sh/src/job-control/job_getrank.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 12:38:31 by jhalford #+# #+# */ -/* Updated: 2017/01/09 12:47:08 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:30:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,23 +14,21 @@ void job_getrank(int (*rank)[2]) { - t_job *job; - t_jobc *jobc; - t_list *jlist; + t_job *job; + t_jobc *jobc; + t_list *jlist; + int i; + i = 0; jobc = &data_singleton()->jobc; jlist = jobc->first_job; (*rank)[0] = 0; (*rank)[1] = 0; - if (jlist) + while (jlist && i < 2) { job = jlist->content; - (*rank)[0] = job->id; + if (job_is_stopped(job->id)) + (*rank)[i++] = job->id; jlist = jlist->next; - if (jlist) - { - job = jlist->content; - (*rank)[1] = job->id; - } } } diff --git a/42sh/src/job-control/job_is_stopped.c b/42sh/src/job-control/job_is_stopped.c index 135988d6..b83c9024 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/09 14:02:13 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:31:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_notify_change.c b/42sh/src/job-control/job_notify_change.c index 7d0c4ead..73f0a594 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/09 16:49:16 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:16:17 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 c10a2b5b..0854b8d9 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/09 16:58:38 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:52:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,8 +21,11 @@ void mark_job_as_running (t_job *j) while (plist) { p = plist->content; - p->attributes &= ~PROCESS_STATE_MASK; - p->attributes |= PROCESS_CONTINUED; + if (p->attributes & PROCESS_SUSPENDED) + { + p->attributes &= ~PROCESS_STATE_MASK; + p->attributes |= PROCESS_CONTINUED; + } 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 index d9b0dd4f..4a7e734a 100644 --- a/42sh/src/job-control/process_format.c +++ b/42sh/src/job-control/process_format.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:05:55 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:58:36 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:30:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,6 @@ void process_format(t_list **plist, int firstp, int opts) 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 "); @@ -32,7 +31,7 @@ void process_format(t_list **plist, int firstp, int opts) { ft_putstr("continued"); p->attributes &= ~PROCESS_STATE_MASK; - p->attributes &= ~PROCESS_RUNNING; + p->attributes |= PROCESS_RUNNING; } else if (state == PROCESS_COMPLETED) { diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/job-control/process_mark_status.c index 429d1c44..83e7b921 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/09 14:04:19 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:55:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,13 +23,13 @@ int process_mark_status(pid_t pid, int status) p->status = status; if (WIFSTOPPED(status)) { - DG("marking: pid=%i, status=%i (stopped)", pid, status); + DG("marking: pid=%i, status=%i (stopped sig %i)", pid, status, WTERMSIG(status)); p->attributes &= ~PROCESS_STATE_MASK; p->attributes |= PROCESS_SUSPENDED; } else { - DG("marking: pid=%i, status=%i (completed)", pid, status); + DG("marking: pid=%i, status=%i (completed sig %i)", pid, status, WTERMSIG(status)); p->attributes &= ~PROCESS_STATE_MASK; p->attributes |= PROCESS_COMPLETED; if (WIFSIGNALED(status)) diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 9272aa08..fd3ed42a 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/09 12:29:04 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 11:06:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,6 @@ 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/job-control/sigtstp_handler.c b/42sh/src/job-control/sigtstp_handler.c index 15f3316c..aa138934 100644 --- a/42sh/src/job-control/sigtstp_handler.c +++ b/42sh/src/job-control/sigtstp_handler.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:37:41 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 10:30:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From da285e7c1fbfa9ca125a59c78844c9a818a3aedc Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 10 Jan 2017 12:34:27 +0100 Subject: [PATCH 27/27] removed debug messages in job control before merge --- 42sh/libft | 2 +- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/exec/launch_process.c | 4 ++-- 42sh/src/job-control/job_addprocess.c | 6 +++--- 42sh/src/job-control/job_is_completed.c | 6 +++--- 42sh/src/job-control/job_is_stopped.c | 2 +- 42sh/src/job-control/job_remove.c | 8 ++++---- 42sh/src/job-control/job_update_status.c | 3 +-- 42sh/src/job-control/process_format.c | 2 +- 42sh/src/job-control/process_mark_status.c | 6 +++--- 42sh/src/job-control/put_job_in_foreground.c | 6 ++---- 42sh/src/job-control/sigchld_handler.c | 4 ++-- 42sh/src/line-editing/ft_interactive_sh.c | 2 +- 42sh/src/line-editing/ft_prompt.c | 4 +--- 42sh/src/line-editing/input_init.c | 2 +- 42sh/src/main/shell_init.c | 2 +- 16 files changed, 28 insertions(+), 33 deletions(-) diff --git a/42sh/libft b/42sh/libft index d799465c..af71e8b8 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit d799465c2e0d51f24fe4d5cf1e51c5109a1617a0 +Subproject commit af71e8b8de26051cd63fde8ab82c90801bd835d8 diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 23c8d28a..788dbb1c 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/10 11:03:44 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:28:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 70b1175f..64310ff9 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/10 10:30:35 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:28:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int launch_process(t_process *p) set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env)); else { - DG("process is to be forked, %i->[]->%i, attr=%b", p->fdin, 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) diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index 2dd27595..0215844a 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: 2017/01/08 15:41:56 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:30:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,13 +25,13 @@ int job_addprocess(t_process *p) job->id = jobc->current_id; job->pgid = p->pid; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); - DG("added new job [%i]", job->id); + /* DG("added new job [%i]", job->id); */ } job = jobc->first_job->content; if (p->pid > 0) { ft_lsteadd(&job->first_process, ft_lstnew(p, sizeof(*p))); - DG("added pid=%i to [%i]", p->pid, job->id); + /* DG("added pid=%i to [%i]", p->pid, job->id); */ } if (JOB_IS_BG(job->attributes) && IS_PIPEEND(p->attributes)) job_notify_new(job); diff --git a/42sh/src/job-control/job_is_completed.c b/42sh/src/job-control/job_is_completed.c index a2a84610..11ce8b55 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/09 14:03:40 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:30:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,10 +25,10 @@ int job_is_completed(int id) while (lst) { p = lst->content; - DG("checking pid=%i", p->pid); + /* DG("checking pid=%i", p->pid); */ if (!(p->attributes & PROCESS_COMPLETED)) { - DG("process %i is not completed", p->pid); + /* DG("process %i is not completed", p->pid); */ return (0); } lst = lst->next; diff --git a/42sh/src/job-control/job_is_stopped.c b/42sh/src/job-control/job_is_stopped.c index b83c9024..7b4b5b85 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/10 10:31:56 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:30:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_remove.c b/42sh/src/job-control/job_remove.c index 297969b9..ffeb3933 100644 --- a/42sh/src/job-control/job_remove.c +++ b/42sh/src/job-control/job_remove.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:51:08 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:46:42 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:31:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,11 +19,11 @@ void job_remove(int id) jobc = &data_singleton()->jobc; if (job_is_completed(id)) { - DG("job_remove"); + /* DG("job_remove"); */ if (id < data_singleton()->jobc.current_id) data_singleton()->jobc.current_id = id; ft_lst_delif(&jobc->first_job, &id, job_cmp_id, job_free); } - else - DG("job_remove failed (not completed)"); + /* else */ + /* DG("job_remove failed (not completed)"); */ } diff --git a/42sh/src/job-control/job_update_status.c b/42sh/src/job-control/job_update_status.c index a8702233..5426c5fc 100644 --- a/42sh/src/job-control/job_update_status.c +++ b/42sh/src/job-control/job_update_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:56:11 by jhalford #+# #+# */ -/* Updated: 2016/12/15 15:09:05 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:33:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,6 @@ 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/process_format.c b/42sh/src/job-control/process_format.c index 4a7e734a..92ad7d88 100644 --- a/42sh/src/job-control/process_format.c +++ b/42sh/src/job-control/process_format.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:05:55 by jhalford #+# #+# */ -/* Updated: 2017/01/10 11:30:52 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:27:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/job-control/process_mark_status.c index 83e7b921..49784124 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/10 10:55:31 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:28:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,13 +23,13 @@ int process_mark_status(pid_t pid, int status) p->status = status; if (WIFSTOPPED(status)) { - DG("marking: pid=%i, status=%i (stopped sig %i)", pid, status, WTERMSIG(status)); + /* DG("marking: pid=%i, status=%i (stopped sig %i)", pid, status, WTERMSIG(status)); */ p->attributes &= ~PROCESS_STATE_MASK; p->attributes |= PROCESS_SUSPENDED; } else { - DG("marking: pid=%i, status=%i (completed sig %i)", pid, status, WTERMSIG(status)); + /* DG("marking: pid=%i, status=%i (completed sig %i)", pid, status, WTERMSIG(status)); */ p->attributes &= ~PROCESS_STATE_MASK; p->attributes |= PROCESS_COMPLETED; if (WIFSIGNALED(status)) diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index fd3ed42a..59e265fc 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/10 11:06:02 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:32:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,15 +24,13 @@ int put_job_in_foreground(t_job *job, int cont) 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)"); } /* Wait for it to report. */ - DG("gonna wait for job id=%i", job->id); + /* DG("gonna wait for job id=%i", job->id); */ job_wait(job->id); job_remove(job->id); diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c index c750bba6..210f52a2 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: 2017/01/08 11:28:29 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:33:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ void sigchld_handler(int signo) (void)signo; data = data_singleton(); - DG("got SIGCHLD"); + /* DG("got SIGCHLD"); */ /* if (do_job_notification()) */ /* ft_putstr(SHELL_PROMPT); */ if (data_singleton()->mode != MODE_EXEC) diff --git a/42sh/src/line-editing/ft_interactive_sh.c b/42sh/src/line-editing/ft_interactive_sh.c index 62654c96..113e0f93 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: 2017/01/09 14:28:54 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:31:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_prompt.c b/42sh/src/line-editing/ft_prompt.c index a9e1162f..b86610cb 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/09 14:28:20 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:31:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,7 @@ int ft_prompt(void) { - DG("do_job_notification() before prompt"); do_job_notification(); - DG("new prompt now"); ft_putstr(SHELL_PROMPT); return (0); } diff --git a/42sh/src/line-editing/input_init.c b/42sh/src/line-editing/input_init.c index b76ce92e..21d5b7c3 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/09 15:35:56 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:31:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index f33b8a0d..2c8578c5 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/09 15:36:12 by jhalford ### ########.fr */ +/* Updated: 2017/01/10 12:31:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */