blob: b1cfb7cd92c22a1d5e26f9e31696d52423098157 [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
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Check for interrupts */
33
Guido van Rossum1d5735e1994-08-30 08:27:36 +000034#include "config.h"
Guido van Rossumf2615261998-12-04 18:50:20 +000035
36/* config.h may or may not define DL_IMPORT */
37#ifndef DL_IMPORT /* declarations for DLL import/export */
38#define DL_IMPORT(RTYPE) RTYPE
39#endif
40
Guido van Rossum1d5735e1994-08-30 08:27:36 +000041#include "myproto.h"
Guido van Rossumbae95181997-02-14 21:12:24 +000042#include "mymalloc.h" /* For ANY */
Guido van Rossum189e8f91992-01-19 16:29:05 +000043#include "intrcheck.h"
44
Guido van Rossumbae95181997-02-14 21:12:24 +000045/* Copied here from ceval.h -- can't include that file. */
46int Py_AddPendingCall Py_PROTO((int (*func) Py_PROTO((ANY *)), ANY *arg));
47
Guido van Rossumd6a15ad1991-06-24 22:30:42 +000048
Guido van Rossum1d5735e1994-08-30 08:27:36 +000049#ifdef QUICKWIN
50
51#include <io.h>
52
53void
Guido van Rossum86bea461997-04-29 21:03:06 +000054PyOS_InitInterrupts()
Guido van Rossum1d5735e1994-08-30 08:27:36 +000055{
56}
57
Guido van Rossumaee094c1997-08-02 03:02:27 +000058void
59PyOS_FiniInterrupts()
60{
61}
62
Guido van Rossum1d5735e1994-08-30 08:27:36 +000063int
Guido van Rossum86bea461997-04-29 21:03:06 +000064PyOS_InterruptOccurred()
Guido van Rossum1d5735e1994-08-30 08:27:36 +000065{
66 _wyield();
67}
68
69#define OK
70
71#endif /* QUICKWIN */
72
Guido van Rossum7bf22de1997-12-02 20:34:19 +000073#if defined(_M_IX86) && !defined(__QNX__)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000074#include <io.h>
75#endif
76
77#if defined(MSDOS) && !defined(QUICKWIN)
78
79#ifdef __GNUC__
80
81/* This is for DJGPP's GO32 extender. I don't know how to trap
82 * control-C (There's no API for ctrl-C, and I don't want to mess with
83 * the interrupt vectors.) However, this DOES catch control-break.
84 * --Amrit
85 */
86
87#include <go32.h>
88
89void
Guido van Rossum86bea461997-04-29 21:03:06 +000090PyOS_InitInterrupts()
Guido van Rossum1d5735e1994-08-30 08:27:36 +000091{
92 _go32_want_ctrl_break(1 /* TRUE */);
93}
94
Guido van Rossumaee094c1997-08-02 03:02:27 +000095void
96PyOS_FiniInterrupts()
97{
98}
99
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000100int
Guido van Rossum86bea461997-04-29 21:03:06 +0000101PyOS_InterruptOccurred()
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000102{
103 return _go32_was_ctrl_break_hit();
104}
105
106#else /* !__GNUC__ */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108/* This might work for MS-DOS (untested though): */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109
110void
Guido van Rossum86bea461997-04-29 21:03:06 +0000111PyOS_InitInterrupts()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
113}
114
Guido van Rossumaee094c1997-08-02 03:02:27 +0000115void
116PyOS_FiniInterrupts()
117{
118}
119
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120int
Guido van Rossum86bea461997-04-29 21:03:06 +0000121PyOS_InterruptOccurred()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122{
123 int interrupted = 0;
124 while (kbhit()) {
125 if (getch() == '\003')
126 interrupted = 1;
127 }
128 return interrupted;
129}
130
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000131#endif /* __GNUC__ */
132
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133#define OK
134
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000135#endif /* MSDOS && !QUICKWIN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136
137
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000138#ifdef macintosh
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139
Jack Jansen9fc39891995-01-27 14:40:41 +0000140/* The Mac interrupt code has moved to macglue.c */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141#define OK
142
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000143#endif /* macintosh */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144
145
146#ifndef OK
147
Guido van Rossum3f5da241990-12-20 15:06:42 +0000148/* Default version -- for real operating systems and for Standard C */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149
150#include <stdio.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000151#include <string.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152#include <signal.h>
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000153#ifdef HAVE_UNISTD_H
154#include <unistd.h>
155#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156
Guido van Rossum62e376b1995-01-17 16:11:53 +0000157static int interrupted;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158
Guido van Rossum575d5611995-03-10 15:13:03 +0000159void
160PyErr_SetInterrupt()
161{
162 interrupted = 1;
163}
164
Guido van Rossum86bea461997-04-29 21:03:06 +0000165extern int PyErr_CheckSignals();
Guido van Rossumad74fa61997-01-21 06:00:33 +0000166
Guido van Rossumdf840d91992-03-27 17:29:44 +0000167/* ARGSUSED */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000168static RETSIGTYPE
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000169#if defined(_M_IX86) && !defined(__QNX__)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000170intcatcher(int sig) /* So the C compiler shuts up */
171#else /* _M_IX86 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172intcatcher(sig)
Guido van Rossumdf840d91992-03-27 17:29:44 +0000173 int sig; /* Not used by required by interface */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000174#endif /* _M_IX86 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175{
Guido van Rossum86bea461997-04-29 21:03:06 +0000176 extern void Py_Exit Py_PROTO((int));
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000177 static char message[] =
178"python: to interrupt a truly hanging Python program, interrupt once more.\n";
179 switch (interrupted++) {
180 case 0:
181 break;
182 case 1:
183 write(2, message, strlen(message));
184 break;
185 case 2:
186 interrupted = 0;
Guido van Rossum86bea461997-04-29 21:03:06 +0000187 Py_Exit(1);
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000188 break;
189 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190 signal(SIGINT, intcatcher);
Guido van Rossum86bea461997-04-29 21:03:06 +0000191 Py_AddPendingCall(PyErr_CheckSignals, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192}
193
Guido van Rossumaee094c1997-08-02 03:02:27 +0000194static RETSIGTYPE (*old_siginthandler)() = SIG_DFL;
195
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000196void
Guido van Rossum86bea461997-04-29 21:03:06 +0000197PyOS_InitInterrupts()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198{
Guido van Rossumaee094c1997-08-02 03:02:27 +0000199 if ((old_siginthandler = signal(SIGINT, SIG_IGN)) != SIG_IGN)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200 signal(SIGINT, intcatcher);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000201#ifdef HAVE_SIGINTERRUPT
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000202 /* This is for SunOS and other modern BSD derivatives.
203 It means that system calls (like read()) are not restarted
204 after an interrupt. This is necessary so interrupting a
205 read() or readline() call works as expected.
206 XXX On old BSD (pure 4.2 or older) you may have to do this
207 differently! */
208 siginterrupt(SIGINT, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000209#endif /* HAVE_SIGINTERRUPT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210}
211
Guido van Rossumaee094c1997-08-02 03:02:27 +0000212void
213PyOS_FiniInterrupts()
214{
215 signal(SIGINT, old_siginthandler);
216}
217
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218int
Guido van Rossum86bea461997-04-29 21:03:06 +0000219PyOS_InterruptOccurred()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220{
221 if (!interrupted)
222 return 0;
223 interrupted = 0;
224 return 1;
225}
226
227#endif /* !OK */
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000228
229void
230PyOS_AfterFork()
231{
232}