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) {