Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | b9f8d6e | 1995-01-04 19:08:09 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Check for interrupts */ |
| 33 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 34 | #include "config.h" |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 35 | #include "myproto.h" |
Guido van Rossum | bae9518 | 1997-02-14 21:12:24 +0000 | [diff] [blame] | 36 | #include "mymalloc.h" /* For ANY */ |
Guido van Rossum | 189e8f9 | 1992-01-19 16:29:05 +0000 | [diff] [blame] | 37 | #include "intrcheck.h" |
| 38 | |
Guido van Rossum | bae9518 | 1997-02-14 21:12:24 +0000 | [diff] [blame] | 39 | /* Copied here from ceval.h -- can't include that file. */ |
| 40 | int Py_AddPendingCall Py_PROTO((int (*func) Py_PROTO((ANY *)), ANY *arg)); |
| 41 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 43 | #ifdef QUICKWIN |
| 44 | |
| 45 | #include <io.h> |
| 46 | |
| 47 | void |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 48 | PyOS_InitInterrupts() |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 52 | void |
| 53 | PyOS_FiniInterrupts() |
| 54 | { |
| 55 | } |
| 56 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 57 | int |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 58 | PyOS_InterruptOccurred() |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 59 | { |
| 60 | _wyield(); |
| 61 | } |
| 62 | |
| 63 | #define OK |
| 64 | |
| 65 | #endif /* QUICKWIN */ |
| 66 | |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 67 | #if defined(_M_IX86) && !defined(__QNX__) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 68 | #include <io.h> |
| 69 | #endif |
| 70 | |
| 71 | #if defined(MSDOS) && !defined(QUICKWIN) |
| 72 | |
| 73 | #ifdef __GNUC__ |
| 74 | |
| 75 | /* This is for DJGPP's GO32 extender. I don't know how to trap |
| 76 | * control-C (There's no API for ctrl-C, and I don't want to mess with |
| 77 | * the interrupt vectors.) However, this DOES catch control-break. |
| 78 | * --Amrit |
| 79 | */ |
| 80 | |
| 81 | #include <go32.h> |
| 82 | |
| 83 | void |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 84 | PyOS_InitInterrupts() |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 85 | { |
| 86 | _go32_want_ctrl_break(1 /* TRUE */); |
| 87 | } |
| 88 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 89 | void |
| 90 | PyOS_FiniInterrupts() |
| 91 | { |
| 92 | } |
| 93 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 94 | int |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 95 | PyOS_InterruptOccurred() |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 96 | { |
| 97 | return _go32_was_ctrl_break_hit(); |
| 98 | } |
| 99 | |
| 100 | #else /* !__GNUC__ */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 102 | /* This might work for MS-DOS (untested though): */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | |
| 104 | void |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 105 | PyOS_InitInterrupts() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | { |
| 107 | } |
| 108 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 109 | void |
| 110 | PyOS_FiniInterrupts() |
| 111 | { |
| 112 | } |
| 113 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | int |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 115 | PyOS_InterruptOccurred() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | { |
| 117 | int interrupted = 0; |
| 118 | while (kbhit()) { |
| 119 | if (getch() == '\003') |
| 120 | interrupted = 1; |
| 121 | } |
| 122 | return interrupted; |
| 123 | } |
| 124 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 125 | #endif /* __GNUC__ */ |
| 126 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 127 | #define OK |
| 128 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 129 | #endif /* MSDOS && !QUICKWIN */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | |
| 131 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 132 | #ifdef macintosh |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | |
Jack Jansen | 9fc3989 | 1995-01-27 14:40:41 +0000 | [diff] [blame] | 134 | /* The Mac interrupt code has moved to macglue.c */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | #define OK |
| 136 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 137 | #endif /* macintosh */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 138 | |
| 139 | |
| 140 | #ifndef OK |
| 141 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 142 | /* Default version -- for real operating systems and for Standard C */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 143 | |
| 144 | #include <stdio.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 145 | #include <string.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 146 | #include <signal.h> |
Guido van Rossum | fd8a393 | 1996-12-02 18:27:33 +0000 | [diff] [blame] | 147 | #ifdef HAVE_UNISTD_H |
| 148 | #include <unistd.h> |
| 149 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | 62e376b | 1995-01-17 16:11:53 +0000 | [diff] [blame] | 151 | static int interrupted; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | 575d561 | 1995-03-10 15:13:03 +0000 | [diff] [blame] | 153 | void |
| 154 | PyErr_SetInterrupt() |
| 155 | { |
| 156 | interrupted = 1; |
| 157 | } |
| 158 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 159 | extern int PyErr_CheckSignals(); |
Guido van Rossum | ad74fa6 | 1997-01-21 06:00:33 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | df840d9 | 1992-03-27 17:29:44 +0000 | [diff] [blame] | 161 | /* ARGSUSED */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 162 | static RETSIGTYPE |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 163 | #if defined(_M_IX86) && !defined(__QNX__) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 164 | intcatcher(int sig) /* So the C compiler shuts up */ |
| 165 | #else /* _M_IX86 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 166 | intcatcher(sig) |
Guido van Rossum | df840d9 | 1992-03-27 17:29:44 +0000 | [diff] [blame] | 167 | int sig; /* Not used by required by interface */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 168 | #endif /* _M_IX86 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 169 | { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 170 | extern void Py_Exit Py_PROTO((int)); |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 171 | static char message[] = |
| 172 | "python: to interrupt a truly hanging Python program, interrupt once more.\n"; |
| 173 | switch (interrupted++) { |
| 174 | case 0: |
| 175 | break; |
| 176 | case 1: |
| 177 | write(2, message, strlen(message)); |
| 178 | break; |
| 179 | case 2: |
| 180 | interrupted = 0; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 181 | Py_Exit(1); |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 182 | break; |
| 183 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | signal(SIGINT, intcatcher); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 185 | Py_AddPendingCall(PyErr_CheckSignals, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 188 | static RETSIGTYPE (*old_siginthandler)() = SIG_DFL; |
| 189 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 190 | void |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 191 | PyOS_InitInterrupts() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 192 | { |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 193 | if ((old_siginthandler = signal(SIGINT, SIG_IGN)) != SIG_IGN) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 194 | signal(SIGINT, intcatcher); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 195 | #ifdef HAVE_SIGINTERRUPT |
Guido van Rossum | acbe8da | 1993-04-15 15:33:52 +0000 | [diff] [blame] | 196 | /* This is for SunOS and other modern BSD derivatives. |
| 197 | It means that system calls (like read()) are not restarted |
| 198 | after an interrupt. This is necessary so interrupting a |
| 199 | read() or readline() call works as expected. |
| 200 | XXX On old BSD (pure 4.2 or older) you may have to do this |
| 201 | differently! */ |
| 202 | siginterrupt(SIGINT, 1); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 203 | #endif /* HAVE_SIGINTERRUPT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 206 | void |
| 207 | PyOS_FiniInterrupts() |
| 208 | { |
| 209 | signal(SIGINT, old_siginthandler); |
| 210 | } |
| 211 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 212 | int |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 213 | PyOS_InterruptOccurred() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | { |
| 215 | if (!interrupted) |
| 216 | return 0; |
| 217 | interrupted = 0; |
| 218 | return 1; |
| 219 | } |
| 220 | |
| 221 | #endif /* !OK */ |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 222 | |
| 223 | void |
| 224 | PyOS_AfterFork() |
| 225 | { |
| 226 | } |