diff --git a/42sh/Makefile b/42sh/Makefile index 84a9ac20..119c5015 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -54,7 +54,6 @@ completion/c_terminal.c\ completion/completion.c\ exec/ast_free.c\ exec/bad_fd.c\ -exec/close_fdsave.c\ exec/exec_ampersand.c\ exec/exec_and_if.c\ exec/exec_command.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 679f5e20..c2743bea 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -98,7 +98,6 @@ void process_setsig(void); void process_free(void *content, size_t content_size); void process_reset(t_process *p); void process_resetfds(void); -void close_fdsave(void); int fd_is_valid(int fd); int bad_fd(int fd); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index d3959f46..fbaaac4c 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -33,15 +33,17 @@ # include # include -# define SHELL_OPTS_JOBC (1 << 0) -# define SHELL_OPTS_LC (1 << 1) -# define SHELL_MODE_INPUT (1 << 2) -# define SHELL_MODE_EXEC (1 << 3) +# define SH_INTERACTIVE (1 << 0) +# define SH_OPTS_JOBC (1 << 1) +# define SH_OPTS_LC (1 << 2) +# define SH_MODE_INPUT (1 << 3) +# define SH_MODE_EXEC (1 << 4) -# define SHELL_MODE_MASK (SHELL_MODE_INPUT | SHELL_MODE_EXEC) -# define SHELL_HAS_JOBC(b) (b & SHELL_OPTS_JOBC) +# define SH_MODE_MASK (SH_MODE_INPUT | SH_MODE_EXEC) +# define SH_HAS_JOBC(b) (b & SH_OPTS_JOBC) +# define SH_IS_INTERACTIVE(b) (b & SH_INTERACTIVE) -# define SHELL_MSG_NOJOBC "no job-control" +# define SH_MSG_NOJOBC "no job-control" struct s_data { diff --git a/42sh/libft b/42sh/libft index d79c3810..b53fcb9d 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit d79c38104bbd45018d03a11d0c60cd6616e77ed8 +Subproject commit b53fcb9db8b4baf53c73b17726f1740c4af12be0 diff --git a/42sh/src/exec/close_fdsave.c b/42sh/src/exec/close_fdsave.c deleted file mode 100644 index a012deeb..00000000 --- a/42sh/src/exec/close_fdsave.c +++ /dev/null @@ -1,23 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* close_fdsave.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/07 17:45:23 by jhalford #+# #+# */ -/* Updated: 2017/02/07 17:54:07 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "exec.h" - -void close_fdsave(void) -{ - t_exec *exec; - - exec = &data_singleton()->exec; - close(exec->fd0save); - close(exec->fd1save); - close(exec->fd2save); -} diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c index ad4123d8..7dad7d31 100644 --- a/42sh/src/exec/exec_ampersand.c +++ b/42sh/src/exec/exec_ampersand.c @@ -14,10 +14,10 @@ int exec_ampersand(t_btree **ast) { - if (SHELL_HAS_JOBC(data_singleton()->opts)) + if (SH_HAS_JOBC(data_singleton()->opts)) data_singleton()->exec.job.attributes |= JOB_BG; ft_exec(&(*ast)->left); - if (SHELL_HAS_JOBC(data_singleton()->opts)) + if (SH_HAS_JOBC(data_singleton()->opts)) data_singleton()->exec.job.attributes &= ~JOB_BG; ft_exec(&(*ast)->right); btree_delone(ast, &ast_free); diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 49767b2c..055109e0 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -43,7 +43,6 @@ int launch_process(t_process *p) pid = fork(); if (pid == 0) { - close_fdsave(); process_setgroup(p, 0); process_setsig(); if (process_redirect(p)) diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index 186ed6d9..a7ada487 100644 --- a/42sh/src/exec/process_setgroup.c +++ b/42sh/src/exec/process_setgroup.c @@ -16,11 +16,13 @@ int process_setgroup(t_process *p, pid_t pid) { t_job *j; + t_data *data; (void)p; - if (!SHELL_HAS_JOBC(data_singleton()->opts)) + data = data_singleton(); + if (!SH_HAS_JOBC(data->opts)) return (0); - j = &data_singleton()->exec.job; + j = &data->exec.job; if (!j->pgid) j->pgid = pid ? pid : getpid(); setpgid(pid, j->pgid); diff --git a/42sh/src/job-control/builtin_bg.c b/42sh/src/job-control/builtin_bg.c index 9a9928fc..d3f91c1f 100644 --- a/42sh/src/job-control/builtin_bg.c +++ b/42sh/src/job-control/builtin_bg.c @@ -21,9 +21,9 @@ int builtin_bg(const char *path, char *const av[], char *const envp[]) (void)path; (void)envp; - if (!SHELL_HAS_JOBC(data_singleton()->opts)) + if (!SH_HAS_JOBC(data_singleton()->opts)) { - ft_dprintf(2, "{red}bg: %s{eoc}\n", SHELL_MSG_NOJOBC); + ft_dprintf(2, "{red}bg: %s{eoc}\n", SH_MSG_NOJOBC); return (-1); } jobc = &data_singleton()->jobc; diff --git a/42sh/src/job-control/builtin_fg.c b/42sh/src/job-control/builtin_fg.c index 48945eca..373ba8a9 100644 --- a/42sh/src/job-control/builtin_fg.c +++ b/42sh/src/job-control/builtin_fg.c @@ -21,9 +21,9 @@ int builtin_fg(const char *path, char *const av[], char *const envp[]) (void)path; (void)envp; - if (!SHELL_HAS_JOBC(data_singleton()->opts)) + if (!SH_HAS_JOBC(data_singleton()->opts)) { - ft_dprintf(2, "{red}fg: %s{eoc}\n", SHELL_MSG_NOJOBC); + ft_dprintf(2, "{red}fg: %s{eoc}\n", SH_MSG_NOJOBC); return (-1); } jobc = &data_singleton()->jobc; diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c index 9e3fe05d..f2a83ee0 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -90,9 +90,9 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) (void)path; (void)envp; - if (!SHELL_HAS_JOBC(data_singleton()->opts)) + if (!SH_HAS_JOBC(data_singleton()->opts)) { - ft_dprintf(2, "{red}jobs: %s{eoc}\n", SHELL_MSG_NOJOBC); + ft_dprintf(2, "{red}jobs: %s{eoc}\n", SH_MSG_NOJOBC); return (1); } if ((opts = bt_jobs_parse((char**)av, &i)) < 0) diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c index 62f5ebef..6cd87253 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -17,6 +17,7 @@ int job_wait(int id) pid_t pid; int status; + DG("gonna wait [%i]", id); if (job_is_stopped(id)) return (0); job_update_status(); diff --git a/42sh/src/job-control/sigtstp_handler.c b/42sh/src/job-control/sigtstp_handler.c index ecae88cb..3ef1c541 100644 --- a/42sh/src/job-control/sigtstp_handler.c +++ b/42sh/src/job-control/sigtstp_handler.c @@ -18,5 +18,6 @@ void sigtstp_handler(int signo) (void)signo; jobc = &data_singleton()->jobc; - DG("got SIGTSTP in process %i", getpid()); + DG("got SIGTSTP pid=%i, pgrp=%i, shell_pgid=%i", getpid(), getpgrp(), data_singleton()->jobc.shell_pgid); + ft_putchar('\x1A'); } diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 62e5c03d..8c11688e 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -16,24 +16,24 @@ extern char **environ; int data_init(void) { - char *term_name; t_data *data; data = data_singleton(); data->env = ft_sstrdup(environ); data->comp = NULL; - data->opts = SHELL_OPTS_JOBC; + data->opts = SH_OPTS_JOBC; data->exec.process.path = NULL; data->exec.process.av = NULL; + data->exec.process.toclose = STDIN; 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.process.redirs = NULL; - data->exec.fd0save = dup(0); - data->exec.fd1save = dup(1); - data->exec.fd2save = dup(2); + data->exec.fd0save = fcntl(0, F_DUPFD_CLOEXEC); + data->exec.fd1save = fcntl(1, F_DUPFD_CLOEXEC); + data->exec.fd2save = fcntl(2, F_DUPFD_CLOEXEC); data->exec.aol_status = NULL; data->exec.aol_search = 0; @@ -41,11 +41,8 @@ int data_init(void) 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; - if ((term_name = ft_getenv(data->env, "TERM")) == NULL) - return (-1); - if (tgetent(NULL, term_name) != 1) - return (-1); return (0); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index ae3f8ec2..b9cc077b 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -36,10 +36,14 @@ int shell_single_command(char *command) int main(int ac, char **av) { + t_data *data; + + data = data_singleton(); setlocale(LC_ALL, ""); + DG("{inv}{bol}{gre}start of shell{eoc} pid=%i pgrp=%i job_control is %s", getpid(), getpgrp(), SH_HAS_JOBC(data->opts) ? "ON" : "OFF"); shell_init(ac, av); - DG("{inv}{bol}{gre}start of shell{eoc} job_control is %s", data_singleton()->opts & SHELL_OPTS_JOBC ? "ON" : "OFF"); - if (data_singleton()->opts & SHELL_OPTS_LC) + DG("{inv}{bol}{gre}start of shell{eoc} pid=%i pgrp=%i job_control is %s", getpid(), getpgrp(), SH_HAS_JOBC(data->opts) ? "ON" : "OFF"); + if (data_singleton()->opts & SH_OPTS_LC) { shell_single_command(ft_strdup(shell_get_avdata())); return (0); diff --git a/42sh/src/main/shell_get_opts.c b/42sh/src/main/shell_get_opts.c index d91fce50..f9f861fc 100644 --- a/42sh/src/main/shell_get_opts.c +++ b/42sh/src/main/shell_get_opts.c @@ -15,7 +15,7 @@ static void shell_parse_long_opt(char *str) { if (ft_strcmp("no-jobcontrol", str) == 0) - data_singleton()->opts &= ~SHELL_OPTS_JOBC; + data_singleton()->opts &= ~SH_OPTS_JOBC; } static void shell_parse_short_opt(char *str) @@ -27,8 +27,8 @@ static void shell_parse_short_opt(char *str) { if (*str == 'c') { - data_singleton()->opts |= SHELL_OPTS_LC; - data_singleton()->opts &= ~SHELL_OPTS_JOBC; + data_singleton()->opts |= SH_OPTS_LC; + data_singleton()->opts &= ~SH_OPTS_JOBC; } i++; } @@ -39,6 +39,8 @@ void shell_get_opts(int ac, char **av) int i; i = 1; + if (isatty(STDIN)) + data_singleton()->opts |= SH_INTERACTIVE; while (i < ac && av[i][0] == '-') { if (ft_strcmp(av[i], "--") == 0) diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index d33221da..e27ac196 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -15,14 +15,18 @@ void shell_init(int ac, char **av) { int *shell_pgid; + t_data *data; data_init(); - data_singleton()->argc = ac; - data_singleton()->argv = ft_sstrdup(av); + data = data_singleton(); + data->argc = ac; + data->argv = ft_sstrdup(av); atexit(&shell_exit); - if (isatty(STDIN)) + shell_get_opts(ac, av); + if (SH_IS_INTERACTIVE(data->opts)) { - shell_pgid = &data_singleton()->jobc.shell_pgid; + DG("interactive shell settings"); + shell_pgid = &data->jobc.shell_pgid; while (tcgetpgrp(STDIN) != (*shell_pgid = getpgrp())) kill(-*shell_pgid, SIGTTIN); signal(SIGINT, sigint_handler); @@ -38,7 +42,6 @@ void shell_init(int ac, char **av) exit (1); } tcsetpgrp(STDIN, *shell_pgid); - tcgetattr(STDIN, &data_singleton()->jobc.shell_tmodes); + tcgetattr(STDIN, &data->jobc.shell_tmodes); } - shell_get_opts(ac, av); } diff --git a/42sh/src/parser/parse_subshell.c b/42sh/src/parser/parse_subshell.c index 8f8aef17..eff75a54 100644 --- a/42sh/src/parser/parse_subshell.c +++ b/42sh/src/parser/parse_subshell.c @@ -19,7 +19,6 @@ int parse_subshell(t_btree **ast, t_list **start, t_list **lst) (void)start; token = (*lst)->content; - DG("parsing subshell"); if ((*lst)->next && ((t_token*)(*lst)->next->content)->type & TK_WORD) { ft_dprintf(2, "{red}%s: parse error near ')'{eoc}\n", SHELL_NAME);