From 1f36fc1501a0b1ce1d7214ab7385a20c74726f82 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 1 Dec 2016 14:47:28 +0100 Subject: [PATCH] ft_cleanup called at exit, reset termios to ISIG,ICANON,ECHO --- 42sh/includes/minishell.h | 5 ++--- 42sh/src/builtin/builtin_exit.c | 2 +- 42sh/src/exec/ft_cmd.c | 4 ++-- 42sh/src/line-editing/ft_set_termios.c | 4 ++-- 42sh/src/main/data_init.c | 5 ++--- 42sh/src/main/ft_cleanup.c | 24 ++++++++++++++++++++++++ 42sh/src/main/sig_handler.c | 17 ++++++++--------- 7 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 42sh/src/main/ft_cleanup.c diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 6fdf1c2f..31f3a836 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/01 12:13:46 by jhalford ### ########.fr */ +/* Updated: 2016/12/01 14:46:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,8 +48,6 @@ struct s_data t_list *qstack; int fdin; int fdout; - int save_in; - int save_out; }; extern t_stof g_builtins[]; @@ -57,6 +55,7 @@ extern pid_t g_pid; void sig_handler(int signo); int data_init(t_data *data); +void ft_cleanup(void); int ft_builtin(char **av, t_data *data); int builtin_echo(char **av, t_data *data); diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index 4202349c..25238b9f 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/11/28 14:30:07 by jhalford ### ########.fr */ +/* Updated: 2016/12/01 14:35:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_cmd.c b/42sh/src/exec/ft_cmd.c index b16bd187..aae7b1e9 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/01 13:47:33 by jhalford ### ########.fr */ +/* Updated: 2016/12/01 14:46:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,7 +57,7 @@ int ft_cmd_exec(char *execpath, char **argv, t_data *data) g_pid = pid; if (data->fdout == STDOUT) { - ft_printf("[waiting for PID = %i]\n", pid); + ft_dprintf(2, "[waiting for PID = %i]\n", pid); waitpid(pid, &status, 0); builtin_setenv((char*[3]){"?", ft_itoa(status)}, data); } diff --git a/42sh/src/line-editing/ft_set_termios.c b/42sh/src/line-editing/ft_set_termios.c index 44ae788a..3037e940 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/01 14:26:56 by jhalford ### ########.fr */ +/* Updated: 2016/12/01 14:46:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ int ft_set_termios(t_data *data, int input_mode) if (input_mode) term.c_lflag &= ~(ICANON) & ~(ISIG) & ~(ECHO); else - term.c_lflag |= ICANON | ECHO; + term.c_lflag |= ICANON | ISIG | ECHO; term.c_cc[VMIN] = 1; term.c_cc[VTIME] = 0; if (tcsetattr(0, TCSADRAIN, &term) == -1) diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 7977737d..41f32763 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/01 12:12:59 by jhalford ### ########.fr */ +/* Updated: 2016/12/01 14:46:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,11 @@ extern char **environ; int data_init(t_data *data) { + atexit(&ft_cleanup); data->env = ft_sstrdup(environ); data->history = NULL; data->fdin = STDIN; data->fdout = STDOUT; - data->save_in = dup(STDIN); - data->save_out = dup(STDOUT); if (!(data->history = ft_dlstnew(NULL, 0))) return (-1); return (0); diff --git a/42sh/src/main/ft_cleanup.c b/42sh/src/main/ft_cleanup.c new file mode 100644 index 00000000..c7c424c2 --- /dev/null +++ b/42sh/src/main/ft_cleanup.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_cleanup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 14:42:42 by jhalford #+# #+# */ +/* Updated: 2016/12/01 14:46:07 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_cleanup(void) +{ + struct termios term; + + if (tcgetattr(0, &term) == -1) + return ; + term.c_lflag |= ICANON | ISIG | ECHO; + if (tcsetattr(0, TCSANOW, &term) == -1) + return ; +} diff --git a/42sh/src/main/sig_handler.c b/42sh/src/main/sig_handler.c index 33efbb46..f9be53fb 100644 --- a/42sh/src/main/sig_handler.c +++ b/42sh/src/main/sig_handler.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:43:22 by jhalford #+# #+# */ -/* Updated: 2016/12/01 14:27:00 by jhalford ### ########.fr */ +/* Updated: 2016/12/01 14:46:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,11 @@ pid_t g_pid; void sig_handler(int signo) { (void)signo; - /* if (signo == SIGINT) */ - /* { */ - /* ft_printf("got SIGINT, g_pid = %i\n", g_pid); */ - /* if (g_pid) */ - /* kill(g_pid, SIGINT); */ - /* if (kill(g_pid, 0) == 0) */ - /* ft_putendl(""); */ - /* } */ + if (signo == SIGINT) + { + if (g_pid) + kill(g_pid, SIGINT); + if (kill(g_pid, 0) == 0) + ft_putendl(""); + } }