blob: 92fab1c3103de1079ba3b8b9bb7334a2a9024ba1 [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 Rossum189e8f91992-01-19 16:29:05 +000036#include "intrcheck.h"
37
Guido van Rossumd6a15ad1991-06-24 22:30:42 +000038
Guido van Rossum1d5735e1994-08-30 08:27:36 +000039#ifdef QUICKWIN
40
41#include <io.h>
42
43void
44initintr()
45{
46}
47
48int
49intrcheck()
50{
51 _wyield();
52}
53
54#define OK
55
56#endif /* QUICKWIN */
57
58#ifdef _M_IX86
59#include <io.h>
60#endif
61
62#if defined(MSDOS) && !defined(QUICKWIN)
63
64#ifdef __GNUC__
65
66/* This is for DJGPP's GO32 extender. I don't know how to trap
67 * control-C (There's no API for ctrl-C, and I don't want to mess with
68 * the interrupt vectors.) However, this DOES catch control-break.
69 * --Amrit
70 */
71
72#include <go32.h>
73
74void
75initintr()
76{
77 _go32_want_ctrl_break(1 /* TRUE */);
78}
79
80int
81intrcheck()
82{
83 return _go32_was_ctrl_break_hit();
84}
85
86#else /* !__GNUC__ */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087
Guido van Rossum3f5da241990-12-20 15:06:42 +000088/* This might work for MS-DOS (untested though): */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089
90void
91initintr()
92{
93}
94
95int
96intrcheck()
97{
98 int interrupted = 0;
99 while (kbhit()) {
100 if (getch() == '\003')
101 interrupted = 1;
102 }
103 return interrupted;
104}
105
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000106#endif /* __GNUC__ */
107
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108#define OK
109
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000110#endif /* MSDOS && !QUICKWIN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111
112
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000113#ifdef macintosh
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114
Jack Jansen9fc39891995-01-27 14:40:41 +0000115/* The Mac interrupt code has moved to macglue.c */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116#define OK
117
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000118#endif /* macintosh */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119
120
121#ifndef OK
122
Guido van Rossum3f5da241990-12-20 15:06:42 +0000123/* Default version -- for real operating systems and for Standard C */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124
125#include <stdio.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000126#include <string.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127#include <signal.h>
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000128#ifdef HAVE_UNISTD_H
129#include <unistd.h>
130#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131
Guido van Rossum62e376b1995-01-17 16:11:53 +0000132static int interrupted;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133
Guido van Rossum575d5611995-03-10 15:13:03 +0000134void
135PyErr_SetInterrupt()
136{
137 interrupted = 1;
138}
139
Guido van Rossumad74fa61997-01-21 06:00:33 +0000140extern int sigcheck();
141
Guido van Rossumdf840d91992-03-27 17:29:44 +0000142/* ARGSUSED */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000143static RETSIGTYPE
144#ifdef _M_IX86
145intcatcher(int sig) /* So the C compiler shuts up */
146#else /* _M_IX86 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147intcatcher(sig)
Guido van Rossumdf840d91992-03-27 17:29:44 +0000148 int sig; /* Not used by required by interface */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000149#endif /* _M_IX86 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000151 extern void goaway PROTO((int));
152 static char message[] =
153"python: to interrupt a truly hanging Python program, interrupt once more.\n";
154 switch (interrupted++) {
155 case 0:
156 break;
157 case 1:
158 write(2, message, strlen(message));
159 break;
160 case 2:
161 interrupted = 0;
162 goaway(1);
163 break;
164 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165 signal(SIGINT, intcatcher);
Guido van Rossumad74fa61997-01-21 06:00:33 +0000166 Py_AddPendingCall(sigcheck, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167}
168
169void
170initintr()
171{
172 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
173 signal(SIGINT, intcatcher);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000174#ifdef HAVE_SIGINTERRUPT
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000175 /* This is for SunOS and other modern BSD derivatives.
176 It means that system calls (like read()) are not restarted
177 after an interrupt. This is necessary so interrupting a
178 read() or readline() call works as expected.
179 XXX On old BSD (pure 4.2 or older) you may have to do this
180 differently! */
181 siginterrupt(SIGINT, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000182#endif /* HAVE_SIGINTERRUPT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183}
184
185int
186intrcheck()
187{
188 if (!interrupted)
189 return 0;
190 interrupted = 0;
191 return 1;
192}
193
194#endif /* !OK */