blob: 685e0667780d33c05e77a626afcc59d52bcf4a95 [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 Rossum1d5735e1994-08-30 08:27:36 +000035#include "myproto.h"
Guido van Rossumbae95181997-02-14 21:12:24 +000036#include "mymalloc.h" /* For ANY */
Guido van Rossum189e8f91992-01-19 16:29:05 +000037#include "intrcheck.h"
38
Guido van Rossumbae95181997-02-14 21:12:24 +000039/* Copied here from ceval.h -- can't include that file. */
40int Py_AddPendingCall Py_PROTO((int (*func) Py_PROTO((ANY *)), ANY *arg));
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
Guido van Rossum86bea461997-04-29 21:03:06 +000048PyOS_InitInterrupts()
Guido van Rossum1d5735e1994-08-30 08:27:36 +000049{
50}
51
52int
Guido van Rossum86bea461997-04-29 21:03:06 +000053PyOS_InterruptOccurred()
Guido van Rossum1d5735e1994-08-30 08:27:36 +000054{
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
Guido van Rossum86bea461997-04-29 21:03:06 +000079PyOS_InitInterrupts()
Guido van Rossum1d5735e1994-08-30 08:27:36 +000080{
81 _go32_want_ctrl_break(1 /* TRUE */);
82}
83
84int
Guido van Rossum86bea461997-04-29 21:03:06 +000085PyOS_InterruptOccurred()
Guido van Rossum1d5735e1994-08-30 08:27:36 +000086{
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
Guido van Rossum86bea461997-04-29 21:03:06 +000095PyOS_InitInterrupts()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096{
97}
98
99int
Guido van Rossum86bea461997-04-29 21:03:06 +0000100PyOS_InterruptOccurred()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101{
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
Jack Jansen9fc39891995-01-27 14:40:41 +0000119/* The Mac interrupt code has moved to macglue.c */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120#define OK
121
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000122#endif /* macintosh */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123
124
125#ifndef OK
126
Guido van Rossum3f5da241990-12-20 15:06:42 +0000127/* Default version -- for real operating systems and for Standard C */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128
129#include <stdio.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000130#include <string.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131#include <signal.h>
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000132#ifdef HAVE_UNISTD_H
133#include <unistd.h>
134#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135
Guido van Rossum62e376b1995-01-17 16:11:53 +0000136static int interrupted;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137
Guido van Rossum575d5611995-03-10 15:13:03 +0000138void
139PyErr_SetInterrupt()
140{
141 interrupted = 1;
142}
143
Guido van Rossum86bea461997-04-29 21:03:06 +0000144extern int PyErr_CheckSignals();
Guido van Rossumad74fa61997-01-21 06:00:33 +0000145
Guido van Rossumdf840d91992-03-27 17:29:44 +0000146/* ARGSUSED */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000147static RETSIGTYPE
148#ifdef _M_IX86
149intcatcher(int sig) /* So the C compiler shuts up */
150#else /* _M_IX86 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151intcatcher(sig)
Guido van Rossumdf840d91992-03-27 17:29:44 +0000152 int sig; /* Not used by required by interface */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000153#endif /* _M_IX86 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154{
Guido van Rossum86bea461997-04-29 21:03:06 +0000155 extern void Py_Exit Py_PROTO((int));
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000156 static char message[] =
157"python: to interrupt a truly hanging Python program, interrupt once more.\n";
158 switch (interrupted++) {
159 case 0:
160 break;
161 case 1:
162 write(2, message, strlen(message));
163 break;
164 case 2:
165 interrupted = 0;
Guido van Rossum86bea461997-04-29 21:03:06 +0000166 Py_Exit(1);
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000167 break;
168 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169 signal(SIGINT, intcatcher);
Guido van Rossum86bea461997-04-29 21:03:06 +0000170 Py_AddPendingCall(PyErr_CheckSignals, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171}
172
173void
Guido van Rossum86bea461997-04-29 21:03:06 +0000174PyOS_InitInterrupts()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175{
176 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
177 signal(SIGINT, intcatcher);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000178#ifdef HAVE_SIGINTERRUPT
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000179 /* This is for SunOS and other modern BSD derivatives.
180 It means that system calls (like read()) are not restarted
181 after an interrupt. This is necessary so interrupting a
182 read() or readline() call works as expected.
183 XXX On old BSD (pure 4.2 or older) you may have to do this
184 differently! */
185 siginterrupt(SIGINT, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000186#endif /* HAVE_SIGINTERRUPT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187}
188
189int
Guido van Rossum86bea461997-04-29 21:03:06 +0000190PyOS_InterruptOccurred()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191{
192 if (!interrupted)
193 return 0;
194 interrupted = 0;
195 return 1;
196}
197
198#endif /* !OK */