blob: c3fabe7abdcf60a3fcec3244fb7adf7a28c4f782 [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#include "config.h"
Guido van Rossum1d5735e1994-08-30 08:27:36 +000028#include "myproto.h"
Guido van Rossum189e8f91992-01-19 16:29:05 +000029#include "intrcheck.h"
30
Guido van Rossumd6a15ad1991-06-24 22:30:42 +000031
Guido van Rossum1d5735e1994-08-30 08:27:36 +000032#ifdef QUICKWIN
33
34#include <io.h>
35
36void
37initintr()
38{
39}
40
41int
42intrcheck()
43{
44 _wyield();
45}
46
47#define OK
48
49#endif /* QUICKWIN */
50
51#ifdef _M_IX86
52#include <io.h>
53#endif
54
55#if defined(MSDOS) && !defined(QUICKWIN)
56
57#ifdef __GNUC__
58
59/* This is for DJGPP's GO32 extender. I don't know how to trap
60 * control-C (There's no API for ctrl-C, and I don't want to mess with
61 * the interrupt vectors.) However, this DOES catch control-break.
62 * --Amrit
63 */
64
65#include <go32.h>
66
67void
68initintr()
69{
70 _go32_want_ctrl_break(1 /* TRUE */);
71}
72
73int
74intrcheck()
75{
76 return _go32_was_ctrl_break_hit();
77}
78
79#else /* !__GNUC__ */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080
Guido van Rossum3f5da241990-12-20 15:06:42 +000081/* This might work for MS-DOS (untested though): */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082
83void
84initintr()
85{
86}
87
88int
89intrcheck()
90{
91 int interrupted = 0;
92 while (kbhit()) {
93 if (getch() == '\003')
94 interrupted = 1;
95 }
96 return interrupted;
97}
98
Guido van Rossum1d5735e1994-08-30 08:27:36 +000099#endif /* __GNUC__ */
100
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101#define OK
102
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000103#endif /* MSDOS && !QUICKWIN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104
105
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000106#ifdef macintosh
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107
Jack Jansen9fc39891995-01-27 14:40:41 +0000108/* The Mac interrupt code has moved to macglue.c */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109#define OK
110
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000111#endif /* macintosh */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112
113
114#ifndef OK
115
Guido van Rossum3f5da241990-12-20 15:06:42 +0000116/* Default version -- for real operating systems and for Standard C */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117
118#include <stdio.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000119#include <string.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120#include <signal.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121
Guido van Rossum62e376b1995-01-17 16:11:53 +0000122static int interrupted;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123
Guido van Rossum575d5611995-03-10 15:13:03 +0000124void
125PyErr_SetInterrupt()
126{
127 interrupted = 1;
128}
129
Guido van Rossumdf840d91992-03-27 17:29:44 +0000130/* ARGSUSED */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000131static RETSIGTYPE
132#ifdef _M_IX86
133intcatcher(int sig) /* So the C compiler shuts up */
134#else /* _M_IX86 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135intcatcher(sig)
Guido van Rossumdf840d91992-03-27 17:29:44 +0000136 int sig; /* Not used by required by interface */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000137#endif /* _M_IX86 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138{
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000139 extern void goaway PROTO((int));
140 static char message[] =
141"python: to interrupt a truly hanging Python program, interrupt once more.\n";
142 switch (interrupted++) {
143 case 0:
144 break;
145 case 1:
146 write(2, message, strlen(message));
147 break;
148 case 2:
149 interrupted = 0;
150 goaway(1);
151 break;
152 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 signal(SIGINT, intcatcher);
154}
155
156void
157initintr()
158{
159 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
160 signal(SIGINT, intcatcher);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000161#ifdef HAVE_SIGINTERRUPT
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000162 /* This is for SunOS and other modern BSD derivatives.
163 It means that system calls (like read()) are not restarted
164 after an interrupt. This is necessary so interrupting a
165 read() or readline() call works as expected.
166 XXX On old BSD (pure 4.2 or older) you may have to do this
167 differently! */
168 siginterrupt(SIGINT, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000169#endif /* HAVE_SIGINTERRUPT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
172int
173intrcheck()
174{
175 if (!interrupted)
176 return 0;
177 interrupted = 0;
178 return 1;
179}
180
181#endif /* !OK */