Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | /* quit.h -- How to handle SIGINT gracefully. */ |
| 2 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3 | /* Copyright (C) 1993-2013 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 7 | Bash is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 11 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 12 | Bash is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 16 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 17 | You should have received a copy of the GNU General Public License |
| 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 21 | #if !defined (_QUIT_H_) |
| 22 | #define _QUIT_H_ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 23 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 24 | #include "sig.h" /* for sig_atomic_t */ |
| 25 | |
| 26 | /* Non-zero means SIGINT has already occurred. */ |
| 27 | extern volatile sig_atomic_t interrupt_state; |
| 28 | extern volatile sig_atomic_t terminating_signal; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 29 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 30 | /* Macro to call a great deal. SIGINT just sets the interrupt_state variable. |
| 31 | When it is safe, put QUIT in the code, and the "interrupt" will take |
| 32 | place. The same scheme is used for terminating signals (e.g., SIGHUP) |
| 33 | and the terminating_signal variable. That calls a function which will |
| 34 | end up exiting the shell. */ |
| 35 | #define QUIT \ |
| 36 | do { \ |
| 37 | if (terminating_signal) termsig_handler (terminating_signal); \ |
| 38 | if (interrupt_state) throw_to_top_level (); \ |
| 39 | } while (0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 40 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 41 | #define CHECK_ALRM \ |
| 42 | do { \ |
| 43 | if (sigalrm_seen) \ |
| 44 | longjmp (alrmbuf, 1); \ |
| 45 | } while (0) |
| 46 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 47 | #define SETINTERRUPT interrupt_state = 1 |
| 48 | #define CLRINTERRUPT interrupt_state = 0 |
| 49 | |
| 50 | #define ADDINTERRUPT interrupt_state++ |
| 51 | #define DELINTERRUPT interrupt_state-- |
| 52 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 53 | /* The same sort of thing, this time just for signals that would ordinarily |
| 54 | cause the shell to terminate. */ |
| 55 | |
| 56 | #define CHECK_TERMSIG \ |
| 57 | do { \ |
| 58 | if (terminating_signal) termsig_handler (terminating_signal); \ |
| 59 | } while (0) |
| 60 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 61 | #define LASTSIG() \ |
| 62 | (terminating_signal ? terminating_signal : (interrupt_state ? SIGINT : 0)) |
| 63 | |
| 64 | #define CHECK_WAIT_INTR \ |
| 65 | do { \ |
| 66 | if (wait_signal_received && this_shell_builtin && (this_shell_builtin == wait_builtin)) \ |
| 67 | longjmp (wait_intr_buf, 1); \ |
| 68 | } while (0) |
| 69 | |
| 70 | #define RESET_SIGTERM \ |
| 71 | do { \ |
| 72 | sigterm_received = 0; \ |
| 73 | } while (0) |
| 74 | |
| 75 | #define CHECK_SIGTERM \ |
| 76 | do { \ |
| 77 | if (sigterm_received) termsig_handler (SIGTERM); \ |
| 78 | } while (0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 79 | #endif /* _QUIT_H_ */ |