From 3f3028880bf35bdccef18ed218d10f50088fe42e Mon Sep 17 00:00:00 2001 From: mcolonna Date: Fri, 28 Jun 2024 12:55:55 +0200 Subject: [PATCH] fix: signals (^C in `$ cat`) --- dev/TODO | 1 - include/signalhandler.h | 11 +++++++++-- src/exec_command.c | 3 ++- src/main.c | 5 +++-- src/signals.c | 34 ++++++++++++++-------------------- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/dev/TODO b/dev/TODO index 5180b8d..c517849 100644 --- a/dev/TODO +++ b/dev/TODO @@ -10,7 +10,6 @@ x exit (with no options) - fix - the builtins should get all arguments and check if they're good - - `$ cat` then ^C (unnecessary part) - working history (rl_* functions?) diff --git a/include/signalhandler.h b/include/signalhandler.h index cbf35c8..bc72f2f 100644 --- a/include/signalhandler.h +++ b/include/signalhandler.h @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/06/13 13:08:35 by mcolonna ### ########.fr */ +/* Updated: 2024/06/28 12:55:17 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,13 @@ # include "include.h" -void handle_signals(void); +typedef enum e_sigmode { + SIGMODE_PROMPT, SIGMODE_IGNORE, SIGMODE_DEFAULT +} t_sigmode; + +// If sigmode is SIGMODE_PROMPT, ^C redisplays the prompt and ^\ is ignored. +// If sigmode is SIGMODE_IGNORE, ^C and ^\ are ignored. +// If sigmode is SIGMODE_DEFAULT, ^C and ^\ are reset by default. +void handle_signals(t_sigmode sigmode); #endif diff --git a/src/exec_command.c b/src/exec_command.c index 6e0510b..95cf450 100644 --- a/src/exec_command.c +++ b/src/exec_command.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */ -/* Updated: 2024/06/28 12:03:49 by mcolonna ### ########.fr */ +/* Updated: 2024/06/28 12:54:07 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,6 +56,7 @@ static int execute_call( return (minishell_error("errno"), errno); if (pid == 0) { + handle_signals(SIGMODE_DEFAULT); if (dup2(inout[0], 0) < 0 || dup2(inout[1], 1) < 0) return (minishell_error("errno"), errno); close_all_pipes(global); diff --git a/src/main.c b/src/main.c index 021d39e..dd27111 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */ -/* Updated: 2024/06/28 11:51:01 by mcolonna ### ########.fr */ +/* Updated: 2024/06/28 12:53:23 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,7 +62,9 @@ static void prompt_command(t_env *env) const char *command_str; env->mc_command = mem_subclass(fatal_error, env->mc_global); + handle_signals(SIGMODE_PROMPT); command_str = ask_command(env); + handle_signals(SIGMODE_IGNORE); if (!command_str) env->exit = true; else @@ -93,7 +95,6 @@ int main(const int argc, const char *argv[], char *const envp[]) env.envp = envp; variables = variables_from_envp(env, envp); env.variables = &variables; - handle_signals(); start(&env); while (!env.exit) prompt_command(&env); diff --git a/src/signals.c b/src/signals.c index 8a2039e..2ad38bc 100644 --- a/src/signals.c +++ b/src/signals.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/12 14:04:30 by mcolonna #+# #+# */ -/* Updated: 2024/06/18 15:40:09 by mcolonna ### ########.fr */ +/* Updated: 2024/06/28 12:52:32 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,33 +21,27 @@ static void handle_sigint(int _) rl_redisplay(); } -void handle_signals(void) -{ - struct sigaction sa; - - sa.sa_flags = SA_RESTART; - sa.sa_handler = handle_sigint; - sigemptyset(&sa.sa_mask); - if (sigaction(SIGINT, &sa, NULL) == -1) - fatal_error(NULL); - sa.sa_flags = 0; - sa.sa_handler = SIG_IGN; - sigemptyset(&sa.sa_mask); - if (sigaction(SIGQUIT, &sa, NULL) == -1) - fatal_error(NULL); -} - -void handle_signals_default(void) +void handle_signals(t_sigmode sigmode) { struct sigaction sa; sa.sa_flags = 0; - sa.sa_handler = SIG_DFL; + if (sigmode == SIGMODE_PROMPT) + sa.sa_flags = SA_RESTART; + if (sigmode == SIGMODE_PROMPT) + sa.sa_handler = handle_sigint; + else if (sigmode == SIGMODE_IGNORE) + sa.sa_handler = SIG_IGN; + else + sa.sa_handler = SIG_DFL; sigemptyset(&sa.sa_mask); if (sigaction(SIGINT, &sa, NULL) == -1) fatal_error(NULL); sa.sa_flags = 0; - sa.sa_handler = SIG_DFL; + if (sigmode == SIGMODE_DEFAULT) + sa.sa_handler = SIG_DFL; + else + sa.sa_handler = SIG_IGN; sigemptyset(&sa.sa_mask); if (sigaction(SIGQUIT, &sa, NULL) == -1) fatal_error(NULL);