diff --git a/42sh/Makefile b/42sh/Makefile index 23e13026..3f641915 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -102,7 +102,6 @@ exec/plaunch_until.c\ exec/plaunch_while.c\ exec/process_launch.c\ exec/process_redirect.c\ -exec/process_resetfds.c\ exec/process_set.c\ exec/process_setgroup.c\ exec/process_setsig.c\ @@ -274,6 +273,7 @@ main/data_singleton.c\ main/ft_putast.c\ main/main.c\ main/shell_init.c\ +main/shell_reset.c\ parser/add_bang.c\ parser/add_case.c\ parser/add_cmd.c\ diff --git a/42sh/includes/builtin_read.h b/42sh/includes/builtin_read.h index 8526ef14..76fd5695 100644 --- a/42sh/includes/builtin_read.h +++ b/42sh/includes/builtin_read.h @@ -6,24 +6,25 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */ -/* Updated: 2017/03/20 15:34:47 by jhalford ### ########.fr */ +/* Updated: 2017/03/22 16:12:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef BUILTIN_READ_H # define BUILTIN_READ_H -# define READ_OPT_LA (1 << 0) -# define READ_OPT_LD (1 << 1) -# define READ_OPT_LE (1 << 2) -# define READ_OPT_LI (1 << 3) -# define READ_OPT_LN (1 << 4) -# define READ_OPT_UN (1 << 5) -# define READ_OPT_LP (1 << 6) -# define READ_OPT_LR (1 << 7) -# define READ_OPT_LS (1 << 8) -# define READ_OPT_LT (1 << 9) -# define READ_OPT_LU (1 << 10) +# define BT_READ_LA (1 << 0) +# define BT_READ_LD (1 << 1) +# define BT_READ_LE (1 << 2) +# define BT_READ_LI (1 << 3) +# define BT_READ_LN (1 << 4) +# define BT_READ_UN (1 << 5) +# define BT_READ_LP (1 << 6) +# define BT_READ_LR (1 << 7) +# define BT_READ_LS (1 << 8) +# define BT_READ_LT (1 << 9) +# define BT_READ_LU (1 << 10) +# define BT_READ_INTER (1 << 11) typedef struct s_read t_read; typedef struct s_readopt t_readopt; diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index d0ee6e3c..7313f13b 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/03/21 20:17:27 by ariard ### ########.fr */ +/* Updated: 2017/03/22 16:35:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -144,7 +144,6 @@ int process_fork(t_process *p); int process_setgroup(t_process *p, pid_t pid); void process_setsig(void); void process_reset(t_process *p); -void process_resetfds(t_process *p); int fd_is_valid(int fd, int flag); int bad_fd(int fd); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 26b20751..31949f0a 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/03/21 18:09:39 by ariard ### ########.fr */ +/* Updated: 2017/03/22 16:32:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,6 +67,8 @@ int data_init(int ac, char **av); void data_exit(void); int get_c_arg(char ***av, t_data *data); +void shell_resetfds(void); +void shell_resetsig(void); /* void content_free(void *data, size_t content_size); */ char *ft_putast(void *node); diff --git a/42sh/src/builtin/bt_read_term.c b/42sh/src/builtin/bt_read_term.c index 2fb1d5f1..1df15cbc 100644 --- a/42sh/src/builtin/bt_read_term.c +++ b/42sh/src/builtin/bt_read_term.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */ -/* Updated: 2017/03/20 14:23:46 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 16:48:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,17 +25,23 @@ int bt_read_terminit(t_read *data) { struct termios term; - (void)data; term = bt_read_term(1); + if (!(data->opts & BT_READ_INTER)) + return (0); term.c_lflag = ECHO | ECHOE | ECHOK | ICANON; - term.c_lflag &= data->timeout ? ~ICANON : ~0; - if (data->opts & READ_OPT_LS) + { + term.c_lflag &= data->timeout ? ~ICANON : ~0; + term.c_cc[VTIME] = data->timeout * 10; + term.c_cc[VMIN] = !data->timeout; + } + if (data->opts & BT_READ_LS) term.c_lflag &= ~ECHO; - term.c_cc[VTIME] = data->timeout * 10; - term.c_cc[VMIN] = !data->timeout; term.c_cc[VEOL] = data->delim; if (tcsetattr(0, TCSANOW, &term) < 0) + { + SH_ERR("tcsetattr(): %s", strerror(errno)); return (-1); + } return (0); } diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index 7410da42..3f23df8d 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:16:34 by jhalford ### ########.fr */ +/* Updated: 2017/03/22 16:50:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,13 +17,13 @@ t_cliopts g_read_opts[] = { - {'d', NULL, READ_OPT_LD, 0, bt_read_getdelim}, - {'n', NULL, READ_OPT_LN, 0, bt_read_getnchars}, - {'p', NULL, READ_OPT_LP, 0, bt_read_getprompt}, - {'r', NULL, READ_OPT_LR, 0, NULL}, - {'s', NULL, READ_OPT_LS, 0, NULL}, - {'t', NULL, READ_OPT_LT, 0, bt_read_gettimeout}, - {'u', NULL, READ_OPT_LU, 0, bt_read_getfd}, + {'d', NULL, BT_READ_LD, 0, bt_read_getdelim}, + {'n', NULL, BT_READ_LN, 0, bt_read_getnchars}, + {'p', NULL, BT_READ_LP, 0, bt_read_getprompt}, + {'r', NULL, BT_READ_LR, 0, NULL}, + {'s', NULL, BT_READ_LS, 0, NULL}, + {'t', NULL, BT_READ_LT, 0, bt_read_gettimeout}, + {'u', NULL, BT_READ_LU, 0, bt_read_getfd}, {0, 0, 0, 0, 0}, }; @@ -38,6 +38,8 @@ int bt_read_init(t_read *data, char **av) data->input = NULL; if ((cliopts_get(av, g_read_opts, data))) return (ft_perror() ? 2 : 2); + if (isatty(STDIN)) + data->opts |= BT_READ_INTER; if (bt_read_terminit(data) < 0) return (-1); return (0); @@ -52,7 +54,7 @@ int bt_read_loop(t_read *data) esc = 0; i = 0; - if (data->prompt) + if (data->prompt && data->opts & BT_READ_INTER) ft_printf(data->prompt); while (42) { @@ -61,11 +63,12 @@ int bt_read_loop(t_read *data) buf[ret] = 0; if (!esc && *buf == data->delim) break ; - esc = esc ? 0 : !(data->opts & READ_OPT_LR) && (*buf == '\\'); + esc = esc ? 0 : !(data->opts & BT_READ_LR) && (*buf == '\\'); ft_strappend(&data->input, buf); - if (*buf == '\n' && !(data->opts & (READ_OPT_LR | READ_OPT_LS))) + if (*buf == '\n' && !(data->opts & + (BT_READ_LR | BT_READ_LS | BT_READ_INTER))) ft_putstr("> "); - if ((data->opts & READ_OPT_LN) && ++i >= data->nchars) + if ((data->opts & BT_READ_LN) && ++i >= data->nchars) break ; } return (0); diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c index 8fe4a385..9b64ae05 100644 --- a/42sh/src/exec/exec_reset.c +++ b/42sh/src/exec/exec_reset.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ -/* Updated: 2017/03/20 20:40:43 by jhalford ### ########.fr */ +/* Updated: 2017/03/22 16:09:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index 0d4d86dd..697d9194 100644 --- a/42sh/src/exec/process_launch.c +++ b/42sh/src/exec/process_launch.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ -/* Updated: 2017/03/21 18:11:33 by jhalford ### ########.fr */ +/* Updated: 2017/03/22 16:34:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,8 @@ int process_launch(t_process *p) set_exitstatus(1, 1); else set_exitstatus(p->map.launch(p), 1); - process_resetfds(p); + shell_resetfds(); + shell_resetsig(); process_free(p, 0); return (0); } diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index 1632df9c..ed66829a 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/03/20 15:58:33 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 16:45:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,8 +24,7 @@ int process_setgroup(t_process *p, pid_t pid) if (!SH_IS_INTERACTIVE(data_singleton()->opts)) return (0); if (setpgid(pid, j->pgid) == -1) - ft_dprintf(2, "{red}%s: internal setpgid() errno=%i{eoc}\n", - SHELL_NAME, errno); + SH_ERR("setpgid(): %s", strerror(errno)); if (pid == 0 && JOB_IS_FG(j->attrs)) tcsetpgrp(STDIN, j->pgid); return (0); diff --git a/42sh/src/exec/process_setsig.c b/42sh/src/exec/process_setsig.c index 21f96de6..126c0267 100644 --- a/42sh/src/exec/process_setsig.c +++ b/42sh/src/exec/process_setsig.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 14:08:35 by jhalford #+# #+# */ -/* Updated: 2017/03/18 00:16:37 by wescande ### ########.fr */ +/* Updated: 2017/03/22 16:05:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job_control/process_format.c b/42sh/src/job_control/process_format.c index 4fba94b2..19e9e66a 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/03/21 14:11:30 by jhalford ### ########.fr */ +/* Updated: 2017/03/22 16:40:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -98,7 +98,7 @@ void process_format(t_list **plist, int firstp, int opts) p = (*plist)->content; if (!firstp) - ft_printf(" "); + ft_putstr(" "); if (opts & JOBS_OPT_L) ft_printf("%i ", p->pid); process_format_state(p); diff --git a/42sh/src/job_control/put_job_in_foreground.c b/42sh/src/job_control/put_job_in_foreground.c index 74bafa70..7714628a 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/03/16 16:51:24 by jhalford ### ########.fr */ +/* Updated: 2017/03/22 16:32:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 9fcce223..0abc6637 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: 2017/03/21 01:50:24 by wescande ### ########.fr */ +/* Updated: 2017/03/22 15:55:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,8 +68,8 @@ int data_init(int ac, char **av) parser_init(&data->parser); if ((term_name = ft_getenv(data->env, "TERM")) == NULL) { - /* ft_dprintf(2, "{red}TERM not set\n{eoc}"); */ term_name = "dumb"; + /* ft_dprintf(2, "{red}TERM not set\n{eoc}"); */ /* return (-1); */ } if (tgetent(NULL, term_name) != 1) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index d0ac9720..e90cc360 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */ -/* Updated: 2017/03/22 15:22:59 by ariard ### ########.fr */ +/* Updated: 2017/03/22 15:54:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 0def2805..b734b6c5 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/03/20 18:12:27 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 16:08:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,13 +71,8 @@ static int interactive_settings(void) shell_pgid = &data->jobc.shell_pgid; while (tcgetpgrp(STDIN) != (*shell_pgid = getpgrp())) kill(-*shell_pgid, SIGTTIN); - signal(SIGINT, SIG_IGN); - signal(SIGQUIT, SIG_IGN); - signal(SIGTSTP, SIG_IGN); - signal(SIGTTIN, SIG_IGN); - signal(SIGTTOU, SIG_IGN); - signal(SIGCHLD, SIG_DFL); *shell_pgid = getpid(); + shell_resetsig(); if (setpgid(*shell_pgid, *shell_pgid)) { ft_dprintf(2, diff --git a/42sh/src/exec/process_resetfds.c b/42sh/src/main/shell_reset.c similarity index 68% rename from 42sh/src/exec/process_resetfds.c rename to 42sh/src/main/shell_reset.c index 4eb0da6c..fd0d3546 100644 --- a/42sh/src/exec/process_resetfds.c +++ b/42sh/src/main/shell_reset.c @@ -1,23 +1,32 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* process_resetfds.c :+: :+: :+: */ +/* shell_reset.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/09 14:51:23 by jhalford #+# #+# */ -/* Updated: 2017/03/21 20:54:20 by ariard ### ########.fr */ +/* Created: 2017/03/22 16:07:14 by jhalford #+# #+# */ +/* Updated: 2017/03/22 16:25:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void process_resetfds(t_process *p) +void shell_resetsig(void) +{ + signal(SIGINT, SIG_IGN); + signal(SIGQUIT, SIG_IGN); + signal(SIGTSTP, SIG_IGN); + signal(SIGTTIN, SIG_IGN); + signal(SIGTTOU, SIG_IGN); + signal(SIGCHLD, SIG_DFL); +} + +void shell_resetfds(void) { t_exec *exec; int i; - (void)p; exec = &data_singleton()->exec; i = -1; while (++i < 10)