blob: 96dd4841c88b587d16636ed78b7d6717ef24df0d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumb9f8d6e1995-01-04 19:08:09 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Check for interrupts */
26
Guido van Rossum1d5735e1994-08-30 08:27:36 +000027#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
31#include "myproto.h"
Guido van Rossum189e8f91992-01-19 16:29:05 +000032#include "intrcheck.h"
33
Jack Jansenfebf8111995-01-19 12:12:36 +000034#ifdef macintosh
35#ifdef THINK_C
36#include <OSEvents.h>
37#endif
38#include <Events.h>
39#endif
40
41
Guido van Rossumd6a15ad1991-06-24 22:30:42 +000042
Guido van Rossum1d5735e1994-08-30 08:27:36 +000043#ifdef QUICKWIN
44
45#include <io.h>
46
47void
48initintr()
49{
50}
51
52int
53intrcheck()
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
78void
79initintr()
80{
81 _go32_want_ctrl_break(1 /* TRUE */);
82}
83
84int
85intrcheck()
86{
87 return _go32_was_ctrl_break_hit();
88}
89
90#else /* !__GNUC__ */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091
Guido van Rossum3f5da241990-12-20 15:06:42 +000092/* This might work for MS-DOS (untested though): */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093
94void
95initintr()
96{
97}
98
99int
100intrcheck()
101{
102 int interrupted = 0;
103 while (kbhit()) {
104 if (getch() == '\003')
105 interrupted = 1;
106 }
107 return interrupted;
108}
109
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000110#endif /* __GNUC__ */
111
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112#define OK
113
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000114#endif /* MSDOS && !QUICKWIN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115
116
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000117#ifdef macintosh
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118
Guido van Rossum1055ece1991-07-01 18:46:03 +0000119#ifdef applec /* MPW */
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000120#include <OSEvents.h>
121#include <SysEqu.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000122#endif /* applec */
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000123
Guido van Rossum875eb7d1991-01-16 14:04:51 +0000124#include <signal.h>
Guido van Rossum875eb7d1991-01-16 14:04:51 +0000125
126static int interrupted;
127
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000128static RETSIGTYPE intcatcher PROTO((int));
129static RETSIGTYPE
Guido van Rossum1055ece1991-07-01 18:46:03 +0000130intcatcher(sig)
Guido van Rossum875eb7d1991-01-16 14:04:51 +0000131 int sig;
132{
133 interrupted = 1;
134 signal(SIGINT, intcatcher);
135}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136
137void
138initintr()
139{
Guido van Rossum875eb7d1991-01-16 14:04:51 +0000140 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
141 signal(SIGINT, intcatcher);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142}
143
Jack Jansen51f4b8d1995-01-26 16:20:38 +0000144#ifdef THINK_C
145/* MPW and MW runtime catch cmd-. and raise SIGINT, THINK does not, it seems */
146static void
147scan_event_queue(flush)
148 int flush;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149{
Guido van Rossum875eb7d1991-01-16 14:04:51 +0000150 register EvQElPtr q;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000152 q = (EvQElPtr) GetEvQHdr()->qHead;
153
154 for (; q; q = (EvQElPtr)q->qLink) {
Guido van Rossum875eb7d1991-01-16 14:04:51 +0000155 if (q->evtQWhat == keyDown &&
156 (char)q->evtQMessage == '.' &&
157 (q->evtQModifiers & cmdKey) != 0) {
Jack Jansen51f4b8d1995-01-26 16:20:38 +0000158 if ( flush )
159 FlushEvents(keyDownMask, 0);
Guido van Rossum875eb7d1991-01-16 14:04:51 +0000160 interrupted = 1;
161 break;
162 }
163 }
Jack Jansen51f4b8d1995-01-26 16:20:38 +0000164}
165#endif
166
167int
168intrcheck()
169{
170#ifdef THINK_C
171 scan_event_queue(1);
172#endif
173 PyMac_Yield();
Guido van Rossum875eb7d1991-01-16 14:04:51 +0000174 if (interrupted) {
175 interrupted = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176 return 1;
177 }
178 return 0;
179}
180
Jack Jansenfebf8111995-01-19 12:12:36 +0000181/* intrpeek() is like intrcheck(), but it doesn't flush the events. The
182** idea is that you call intrpeek() somewhere in a busy-wait loop, and return
Guido van Rossumbadadd21995-01-20 16:58:43 +0000183** None as soon as it returns 1. The mainloop will then pick up the cmd-. soon
Jack Jansenfebf8111995-01-19 12:12:36 +0000184** thereafter.
185*/
186int
187intrpeek()
188{
Jack Jansen51f4b8d1995-01-26 16:20:38 +0000189#ifdef THINK_C
190 scan_event_queue(0);
191#endif
192 return interrupted;
Jack Jansenfebf8111995-01-19 12:12:36 +0000193}
194
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195#define OK
196
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000197#endif /* macintosh */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198
199
200#ifndef OK
201
Guido van Rossum3f5da241990-12-20 15:06:42 +0000202/* Default version -- for real operating systems and for Standard C */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203
204#include <stdio.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000205#include <string.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206#include <signal.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207
Guido van Rossum62e376b1995-01-17 16:11:53 +0000208static int interrupted;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209
Guido van Rossumdf840d91992-03-27 17:29:44 +0000210/* ARGSUSED */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000211static RETSIGTYPE
212#ifdef _M_IX86
213intcatcher(int sig) /* So the C compiler shuts up */
214#else /* _M_IX86 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215intcatcher(sig)
Guido van Rossumdf840d91992-03-27 17:29:44 +0000216 int sig; /* Not used by required by interface */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000217#endif /* _M_IX86 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218{
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000219 extern void goaway PROTO((int));
220 static char message[] =
221"python: to interrupt a truly hanging Python program, interrupt once more.\n";
222 switch (interrupted++) {
223 case 0:
224 break;
225 case 1:
226 write(2, message, strlen(message));
227 break;
228 case 2:
229 interrupted = 0;
230 goaway(1);
231 break;
232 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233 signal(SIGINT, intcatcher);
234}
235
236void
237initintr()
238{
239 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
240 signal(SIGINT, intcatcher);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000241#ifdef HAVE_SIGINTERRUPT
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000242 /* This is for SunOS and other modern BSD derivatives.
243 It means that system calls (like read()) are not restarted
244 after an interrupt. This is necessary so interrupting a
245 read() or readline() call works as expected.
246 XXX On old BSD (pure 4.2 or older) you may have to do this
247 differently! */
248 siginterrupt(SIGINT, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000249#endif /* HAVE_SIGINTERRUPT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250}
251
252int
253intrcheck()
254{
255 if (!interrupted)
256 return 0;
257 interrupted = 0;
258 return 1;
259}
260
261#endif /* !OK */