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 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Check for interrupts */ |
| 26 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 27 | #ifdef HAVE_CONFIG_H |
| 28 | #include "config.h" |
| 29 | #endif |
| 30 | |
| 31 | #include "myproto.h" |
Guido van Rossum | 189e8f9 | 1992-01-19 16:29:05 +0000 | [diff] [blame] | 32 | #include "intrcheck.h" |
| 33 | |
Jack Jansen | febf811 | 1995-01-19 12:12:36 +0000 | [diff] [blame] | 34 | #ifdef macintosh |
| 35 | #ifdef THINK_C |
| 36 | #include <OSEvents.h> |
| 37 | #endif |
| 38 | #include <Events.h> |
| 39 | #endif |
| 40 | |
| 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 |
| 48 | initintr() |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | int |
| 53 | intrcheck() |
| 54 | { |
| 55 | _wyield(); |
| 56 | } |
| 57 | |
| 58 | #define OK |
| 59 | |
| 60 | #endif /* QUICKWIN */ |
| 61 | |
| 62 | #ifdef _M_IX86 |
| 63 | #include <io.h> |
| 64 | #endif |
| 65 | |
| 66 | #if defined(MSDOS) && !defined(QUICKWIN) |
| 67 | |
| 68 | #ifdef __GNUC__ |
| 69 | |
| 70 | /* This is for DJGPP's GO32 extender. I don't know how to trap |
| 71 | * control-C (There's no API for ctrl-C, and I don't want to mess with |
| 72 | * the interrupt vectors.) However, this DOES catch control-break. |
| 73 | * --Amrit |
| 74 | */ |
| 75 | |
| 76 | #include <go32.h> |
| 77 | |
| 78 | void |
| 79 | initintr() |
| 80 | { |
| 81 | _go32_want_ctrl_break(1 /* TRUE */); |
| 82 | } |
| 83 | |
| 84 | int |
| 85 | intrcheck() |
| 86 | { |
| 87 | return _go32_was_ctrl_break_hit(); |
| 88 | } |
| 89 | |
| 90 | #else /* !__GNUC__ */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 92 | /* This might work for MS-DOS (untested though): */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | |
| 94 | void |
| 95 | initintr() |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | int |
| 100 | intrcheck() |
| 101 | { |
| 102 | int interrupted = 0; |
| 103 | while (kbhit()) { |
| 104 | if (getch() == '\003') |
| 105 | interrupted = 1; |
| 106 | } |
| 107 | return interrupted; |
| 108 | } |
| 109 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 110 | #endif /* __GNUC__ */ |
| 111 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | #define OK |
| 113 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 114 | #endif /* MSDOS && !QUICKWIN */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 115 | |
| 116 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 117 | #ifdef macintosh |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | 1055ece | 1991-07-01 18:46:03 +0000 | [diff] [blame] | 119 | #ifdef applec /* MPW */ |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 120 | #include <OSEvents.h> |
| 121 | #include <SysEqu.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 122 | #endif /* applec */ |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 123 | |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 124 | #include <signal.h> |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 125 | |
| 126 | static int interrupted; |
| 127 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 128 | static RETSIGTYPE intcatcher PROTO((int)); |
| 129 | static RETSIGTYPE |
Guido van Rossum | 1055ece | 1991-07-01 18:46:03 +0000 | [diff] [blame] | 130 | intcatcher(sig) |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 131 | int sig; |
| 132 | { |
| 133 | interrupted = 1; |
| 134 | signal(SIGINT, intcatcher); |
| 135 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | |
| 137 | void |
| 138 | initintr() |
| 139 | { |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 140 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 141 | signal(SIGINT, intcatcher); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int |
| 145 | intrcheck() |
| 146 | { |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 147 | register EvQElPtr q; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 149 | q = (EvQElPtr) GetEvQHdr()->qHead; |
| 150 | |
| 151 | for (; q; q = (EvQElPtr)q->qLink) { |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 152 | if (q->evtQWhat == keyDown && |
| 153 | (char)q->evtQMessage == '.' && |
| 154 | (q->evtQModifiers & cmdKey) != 0) { |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 155 | FlushEvents(keyDownMask, 0); |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 156 | interrupted = 1; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | if (interrupted) { |
| 161 | interrupted = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 162 | return 1; |
| 163 | } |
| 164 | return 0; |
| 165 | } |
| 166 | |
Jack Jansen | febf811 | 1995-01-19 12:12:36 +0000 | [diff] [blame] | 167 | /* intrpeek() is like intrcheck(), but it doesn't flush the events. The |
| 168 | ** idea is that you call intrpeek() somewhere in a busy-wait loop, and return |
| 169 | ** None as soon as it returns 0. The mainloop will then pick up the cmd-. soon |
| 170 | ** thereafter. |
| 171 | */ |
| 172 | int |
| 173 | intrpeek() |
| 174 | { |
| 175 | register EvQElPtr q; |
| 176 | |
| 177 | q = (EvQElPtr) GetEvQHdr()->qHead; |
| 178 | |
| 179 | for (; q; q = (EvQElPtr)q->qLink) { |
| 180 | if (q->evtQWhat == keyDown && |
| 181 | (char)q->evtQMessage == '.' && |
| 182 | (q->evtQModifiers & cmdKey) != 0) { |
| 183 | return 1; |
| 184 | } |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | #define OK |
| 190 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 191 | #endif /* macintosh */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 192 | |
| 193 | |
| 194 | #ifndef OK |
| 195 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 196 | /* Default version -- for real operating systems and for Standard C */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 197 | |
| 198 | #include <stdio.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 199 | #include <string.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 200 | #include <signal.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | 62e376b | 1995-01-17 16:11:53 +0000 | [diff] [blame] | 202 | static int interrupted; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | df840d9 | 1992-03-27 17:29:44 +0000 | [diff] [blame] | 204 | /* ARGSUSED */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 205 | static RETSIGTYPE |
| 206 | #ifdef _M_IX86 |
| 207 | intcatcher(int sig) /* So the C compiler shuts up */ |
| 208 | #else /* _M_IX86 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 209 | intcatcher(sig) |
Guido van Rossum | df840d9 | 1992-03-27 17:29:44 +0000 | [diff] [blame] | 210 | int sig; /* Not used by required by interface */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 211 | #endif /* _M_IX86 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 212 | { |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 213 | extern void goaway PROTO((int)); |
| 214 | static char message[] = |
| 215 | "python: to interrupt a truly hanging Python program, interrupt once more.\n"; |
| 216 | switch (interrupted++) { |
| 217 | case 0: |
| 218 | break; |
| 219 | case 1: |
| 220 | write(2, message, strlen(message)); |
| 221 | break; |
| 222 | case 2: |
| 223 | interrupted = 0; |
| 224 | goaway(1); |
| 225 | break; |
| 226 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 227 | signal(SIGINT, intcatcher); |
| 228 | } |
| 229 | |
| 230 | void |
| 231 | initintr() |
| 232 | { |
| 233 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 234 | signal(SIGINT, intcatcher); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 235 | #ifdef HAVE_SIGINTERRUPT |
Guido van Rossum | acbe8da | 1993-04-15 15:33:52 +0000 | [diff] [blame] | 236 | /* This is for SunOS and other modern BSD derivatives. |
| 237 | It means that system calls (like read()) are not restarted |
| 238 | after an interrupt. This is necessary so interrupting a |
| 239 | read() or readline() call works as expected. |
| 240 | XXX On old BSD (pure 4.2 or older) you may have to do this |
| 241 | differently! */ |
| 242 | siginterrupt(SIGINT, 1); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 243 | #endif /* HAVE_SIGINTERRUPT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | int |
| 247 | intrcheck() |
| 248 | { |
| 249 | if (!interrupted) |
| 250 | return 0; |
| 251 | interrupted = 0; |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | #endif /* !OK */ |