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 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 35 | #ifdef QUICKWIN |
| 36 | |
| 37 | #include <io.h> |
| 38 | |
| 39 | void |
| 40 | initintr() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | int |
| 45 | intrcheck() |
| 46 | { |
| 47 | _wyield(); |
| 48 | } |
| 49 | |
| 50 | #define OK |
| 51 | |
| 52 | #endif /* QUICKWIN */ |
| 53 | |
| 54 | #ifdef _M_IX86 |
| 55 | #include <io.h> |
| 56 | #endif |
| 57 | |
| 58 | #if defined(MSDOS) && !defined(QUICKWIN) |
| 59 | |
| 60 | #ifdef __GNUC__ |
| 61 | |
| 62 | /* This is for DJGPP's GO32 extender. I don't know how to trap |
| 63 | * control-C (There's no API for ctrl-C, and I don't want to mess with |
| 64 | * the interrupt vectors.) However, this DOES catch control-break. |
| 65 | * --Amrit |
| 66 | */ |
| 67 | |
| 68 | #include <go32.h> |
| 69 | |
| 70 | void |
| 71 | initintr() |
| 72 | { |
| 73 | _go32_want_ctrl_break(1 /* TRUE */); |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | intrcheck() |
| 78 | { |
| 79 | return _go32_was_ctrl_break_hit(); |
| 80 | } |
| 81 | |
| 82 | #else /* !__GNUC__ */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 84 | /* This might work for MS-DOS (untested though): */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | |
| 86 | void |
| 87 | initintr() |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | int |
| 92 | intrcheck() |
| 93 | { |
| 94 | int interrupted = 0; |
| 95 | while (kbhit()) { |
| 96 | if (getch() == '\003') |
| 97 | interrupted = 1; |
| 98 | } |
| 99 | return interrupted; |
| 100 | } |
| 101 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 102 | #endif /* __GNUC__ */ |
| 103 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | #define OK |
| 105 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 106 | #endif /* MSDOS && !QUICKWIN */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | |
| 108 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 109 | #ifdef macintosh |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | |
Jack Jansen | 9fc3989 | 1995-01-27 14:40:41 +0000 | [diff] [blame] | 111 | /* The Mac interrupt code has moved to macglue.c */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | #define OK |
| 113 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 114 | #endif /* macintosh */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | #ifndef OK |
| 118 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 119 | /* Default version -- for real operating systems and for Standard C */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | |
| 121 | #include <stdio.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 122 | #include <string.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | #include <signal.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | 62e376b | 1995-01-17 16:11:53 +0000 | [diff] [blame] | 125 | static int interrupted; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | 575d561 | 1995-03-10 15:13:03 +0000 | [diff] [blame^] | 127 | void |
| 128 | PyErr_SetInterrupt() |
| 129 | { |
| 130 | interrupted = 1; |
| 131 | } |
| 132 | |
Guido van Rossum | df840d9 | 1992-03-27 17:29:44 +0000 | [diff] [blame] | 133 | /* ARGSUSED */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 134 | static RETSIGTYPE |
| 135 | #ifdef _M_IX86 |
| 136 | intcatcher(int sig) /* So the C compiler shuts up */ |
| 137 | #else /* _M_IX86 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 138 | intcatcher(sig) |
Guido van Rossum | df840d9 | 1992-03-27 17:29:44 +0000 | [diff] [blame] | 139 | int sig; /* Not used by required by interface */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 140 | #endif /* _M_IX86 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 141 | { |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 142 | extern void goaway PROTO((int)); |
| 143 | static char message[] = |
| 144 | "python: to interrupt a truly hanging Python program, interrupt once more.\n"; |
| 145 | switch (interrupted++) { |
| 146 | case 0: |
| 147 | break; |
| 148 | case 1: |
| 149 | write(2, message, strlen(message)); |
| 150 | break; |
| 151 | case 2: |
| 152 | interrupted = 0; |
| 153 | goaway(1); |
| 154 | break; |
| 155 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 156 | signal(SIGINT, intcatcher); |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | initintr() |
| 161 | { |
| 162 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 163 | signal(SIGINT, intcatcher); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 164 | #ifdef HAVE_SIGINTERRUPT |
Guido van Rossum | acbe8da | 1993-04-15 15:33:52 +0000 | [diff] [blame] | 165 | /* This is for SunOS and other modern BSD derivatives. |
| 166 | It means that system calls (like read()) are not restarted |
| 167 | after an interrupt. This is necessary so interrupting a |
| 168 | read() or readline() call works as expected. |
| 169 | XXX On old BSD (pure 4.2 or older) you may have to do this |
| 170 | differently! */ |
| 171 | siginterrupt(SIGINT, 1); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 172 | #endif /* HAVE_SIGINTERRUPT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | int |
| 176 | intrcheck() |
| 177 | { |
| 178 | if (!interrupted) |
| 179 | return 0; |
| 180 | interrupted = 0; |
| 181 | return 1; |
| 182 | } |
| 183 | |
| 184 | #endif /* !OK */ |