Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame^] | 2 | Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3 | Amsterdam, 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 | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 27 | #ifdef THINK_C |
Guido van Rossum | 1055ece | 1991-07-01 18:46:03 +0000 | [diff] [blame] | 28 | #include <MacHeaders> |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 29 | #define macintosh |
| 30 | #endif |
| 31 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame^] | 32 | #ifdef HAVE_CONFIG_H |
| 33 | #include "config.h" |
| 34 | #endif |
| 35 | |
| 36 | #include "myproto.h" |
Guido van Rossum | 189e8f9 | 1992-01-19 16:29:05 +0000 | [diff] [blame] | 37 | #include "intrcheck.h" |
| 38 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame^] | 40 | #ifdef QUICKWIN |
| 41 | |
| 42 | #include <io.h> |
| 43 | |
| 44 | void |
| 45 | initintr() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | int |
| 50 | intrcheck() |
| 51 | { |
| 52 | _wyield(); |
| 53 | } |
| 54 | |
| 55 | #define OK |
| 56 | |
| 57 | #endif /* QUICKWIN */ |
| 58 | |
| 59 | #ifdef _M_IX86 |
| 60 | #include <io.h> |
| 61 | #endif |
| 62 | |
| 63 | #if defined(MSDOS) && !defined(QUICKWIN) |
| 64 | |
| 65 | #ifdef __GNUC__ |
| 66 | |
| 67 | /* This is for DJGPP's GO32 extender. I don't know how to trap |
| 68 | * control-C (There's no API for ctrl-C, and I don't want to mess with |
| 69 | * the interrupt vectors.) However, this DOES catch control-break. |
| 70 | * --Amrit |
| 71 | */ |
| 72 | |
| 73 | #include <go32.h> |
| 74 | |
| 75 | void |
| 76 | initintr() |
| 77 | { |
| 78 | _go32_want_ctrl_break(1 /* TRUE */); |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | intrcheck() |
| 83 | { |
| 84 | return _go32_was_ctrl_break_hit(); |
| 85 | } |
| 86 | |
| 87 | #else /* !__GNUC__ */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 89 | /* This might work for MS-DOS (untested though): */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | |
| 91 | void |
| 92 | initintr() |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | int |
| 97 | intrcheck() |
| 98 | { |
| 99 | int interrupted = 0; |
| 100 | while (kbhit()) { |
| 101 | if (getch() == '\003') |
| 102 | interrupted = 1; |
| 103 | } |
| 104 | return interrupted; |
| 105 | } |
| 106 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame^] | 107 | #endif /* __GNUC__ */ |
| 108 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | #define OK |
| 110 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame^] | 111 | #endif /* MSDOS && !QUICKWIN */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | |
| 113 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 114 | #ifdef macintosh |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | 1055ece | 1991-07-01 18:46:03 +0000 | [diff] [blame] | 116 | #ifdef applec /* MPW */ |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 117 | #include <OSEvents.h> |
| 118 | #include <SysEqu.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame^] | 119 | #endif /* applec */ |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 121 | #include <signal.h> |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 122 | |
| 123 | static int interrupted; |
| 124 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame^] | 125 | static RETSIGTYPE intcatcher PROTO((int)); |
| 126 | static RETSIGTYPE |
Guido van Rossum | 1055ece | 1991-07-01 18:46:03 +0000 | [diff] [blame] | 127 | intcatcher(sig) |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 128 | int sig; |
| 129 | { |
| 130 | interrupted = 1; |
| 131 | signal(SIGINT, intcatcher); |
| 132 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | |
| 134 | void |
| 135 | initintr() |
| 136 | { |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 137 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 138 | signal(SIGINT, intcatcher); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | int |
| 142 | intrcheck() |
| 143 | { |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 144 | register EvQElPtr q; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 146 | q = (EvQElPtr) GetEvQHdr()->qHead; |
| 147 | |
| 148 | for (; q; q = (EvQElPtr)q->qLink) { |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 149 | if (q->evtQWhat == keyDown && |
| 150 | (char)q->evtQMessage == '.' && |
| 151 | (q->evtQModifiers & cmdKey) != 0) { |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 152 | FlushEvents(keyDownMask, 0); |
Guido van Rossum | 875eb7d | 1991-01-16 14:04:51 +0000 | [diff] [blame] | 153 | interrupted = 1; |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | if (interrupted) { |
| 158 | interrupted = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 159 | return 1; |
| 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | #define OK |
| 165 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 166 | #endif /* macintosh */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 167 | |
| 168 | |
| 169 | #ifndef OK |
| 170 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 171 | /* Default version -- for real operating systems and for Standard C */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 172 | |
| 173 | #include <stdio.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame^] | 174 | #include <string.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 175 | #include <signal.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 176 | |
| 177 | static int interrupted; |
| 178 | |
Guido van Rossum | df840d9 | 1992-03-27 17:29:44 +0000 | [diff] [blame] | 179 | /* ARGSUSED */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame^] | 180 | static RETSIGTYPE |
| 181 | #ifdef _M_IX86 |
| 182 | intcatcher(int sig) /* So the C compiler shuts up */ |
| 183 | #else /* _M_IX86 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | intcatcher(sig) |
Guido van Rossum | df840d9 | 1992-03-27 17:29:44 +0000 | [diff] [blame] | 185 | int sig; /* Not used by required by interface */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame^] | 186 | #endif /* _M_IX86 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 187 | { |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 188 | extern void goaway PROTO((int)); |
| 189 | static char message[] = |
| 190 | "python: to interrupt a truly hanging Python program, interrupt once more.\n"; |
| 191 | switch (interrupted++) { |
| 192 | case 0: |
| 193 | break; |
| 194 | case 1: |
| 195 | write(2, message, strlen(message)); |
| 196 | break; |
| 197 | case 2: |
| 198 | interrupted = 0; |
| 199 | goaway(1); |
| 200 | break; |
| 201 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 202 | signal(SIGINT, intcatcher); |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | initintr() |
| 207 | { |
| 208 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 209 | signal(SIGINT, intcatcher); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame^] | 210 | #ifdef HAVE_SIGINTERRUPT |
Guido van Rossum | acbe8da | 1993-04-15 15:33:52 +0000 | [diff] [blame] | 211 | /* This is for SunOS and other modern BSD derivatives. |
| 212 | It means that system calls (like read()) are not restarted |
| 213 | after an interrupt. This is necessary so interrupting a |
| 214 | read() or readline() call works as expected. |
| 215 | XXX On old BSD (pure 4.2 or older) you may have to do this |
| 216 | differently! */ |
| 217 | siginterrupt(SIGINT, 1); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame^] | 218 | #endif /* HAVE_SIGINTERRUPT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | int |
| 222 | intrcheck() |
| 223 | { |
| 224 | if (!interrupted) |
| 225 | return 0; |
| 226 | interrupted = 0; |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | #endif /* !OK */ |