/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* sig.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/02 12:16:37 by kcolin #+# #+# */ /* Updated: 2025/05/02 12:18:17 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "sig.h" #include "signal.h" #include "sig_handlers.h" /* ** g_signum exists to store the value of a signal if relevant for later ** processing. ** NOTE: g_signum starts at 0 and should be set back to 0 after the information ** it stores has been properly processed. */ int g_signum = 0; /* ** Catches SIGINT and SIGQUIT. ** Set to ignore SIGQUIT and catch SIGINT to set g_signum for ** further processing when in interactive mode. */ int set_interactive_mode_sig_handling(void) { struct sigaction sig_act; ft_bzero(&sig_act, sizeof(struct sigaction)); sig_act.sa_handler = &sig_interactive; sig_act.sa_flags |= SA_RESTART; if (sigemptyset(&sig_act.sa_mask) == -1) return (-1); if (sigaction(SIGINT, &sig_act, NULL) == -1) return (-1); sig_act.sa_handler = SIG_IGN; if (sigaction(SIGQUIT, &sig_act, NULL) == -1) return (-1); return (0); } /* ** Set to ignore SIGINT and SIGQUIT signals if they are generated through ** a call to kill when the program is currently executing commands. ** Otherwise set g_signum to the number corresponding to the caught signal. */ int set_exec_mode_sig_handling(void) { struct sigaction sig_act; ft_bzero(&sig_act, sizeof(struct sigaction)); if (sigemptyset(&sig_act.sa_mask) == -1) return (-1); sig_act.sa_sigaction = &sig_cmd; sig_act.sa_flags |= SA_SIGINFO; sig_act.sa_flags |= SA_RESTART; if (sigaction(SIGINT, &sig_act, NULL) == -1) return (-1); if (sigaction(SIGQUIT, &sig_act, NULL) == -1) return (-1); return (0); } /* ** Set to ignore SIGINT and SIGQUIT signals if they are generated through ** a call to kill when the program is currently executing commands. ** Otherwise set g_signum to the number corresponding to the caught signal. */ int set_here_doc_mode_sig_handling(void) { struct sigaction sig_act; ft_bzero(&sig_act, sizeof(struct sigaction)); if (sigemptyset(&sig_act.sa_mask) == -1) return (-1); sig_act.sa_sigaction = &sig_cmd; sig_act.sa_flags |= SA_SIGINFO; if (sigaction(SIGINT, &sig_act, NULL) == -1) return (-1); sig_act.sa_handler = SIG_IGN; sig_act.sa_flags = 0; if (sigaction(SIGQUIT, &sig_act, NULL) == -1) return (-1); return (0); }