Clarify portability and main program.
[python/dscho.git] / Parser / intrcheck.c
blob1320bd8f20e73b1406d4ecb45e1524a3ebd41acc
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Check for interrupts */
34 #include "config.h"
35 #include "myproto.h"
36 #include "mymalloc.h" /* For ANY */
37 #include "intrcheck.h"
39 /* Copied here from ceval.h -- can't include that file. */
40 int Py_AddPendingCall Py_PROTO((int (*func) Py_PROTO((ANY *)), ANY *arg));
43 #ifdef QUICKWIN
45 #include <io.h>
47 void
48 PyOS_InitInterrupts()
52 void
53 PyOS_FiniInterrupts()
57 int
58 PyOS_InterruptOccurred()
60 _wyield();
63 #define OK
65 #endif /* QUICKWIN */
67 #if defined(_M_IX86) && !defined(__QNX__)
68 #include <io.h>
69 #endif
71 #if defined(MSDOS) && !defined(QUICKWIN)
73 #ifdef __GNUC__
75 /* This is for DJGPP's GO32 extender. I don't know how to trap
76 * control-C (There's no API for ctrl-C, and I don't want to mess with
77 * the interrupt vectors.) However, this DOES catch control-break.
78 * --Amrit
81 #include <go32.h>
83 void
84 PyOS_InitInterrupts()
86 _go32_want_ctrl_break(1 /* TRUE */);
89 void
90 PyOS_FiniInterrupts()
94 int
95 PyOS_InterruptOccurred()
97 return _go32_was_ctrl_break_hit();
100 #else /* !__GNUC__ */
102 /* This might work for MS-DOS (untested though): */
104 void
105 PyOS_InitInterrupts()
109 void
110 PyOS_FiniInterrupts()
115 PyOS_InterruptOccurred()
117 int interrupted = 0;
118 while (kbhit()) {
119 if (getch() == '\003')
120 interrupted = 1;
122 return interrupted;
125 #endif /* __GNUC__ */
127 #define OK
129 #endif /* MSDOS && !QUICKWIN */
132 #ifdef macintosh
134 /* The Mac interrupt code has moved to macglue.c */
135 #define OK
137 #endif /* macintosh */
140 #ifndef OK
142 /* Default version -- for real operating systems and for Standard C */
144 #include <stdio.h>
145 #include <string.h>
146 #include <signal.h>
147 #ifdef HAVE_UNISTD_H
148 #include <unistd.h>
149 #endif
151 static int interrupted;
153 void
154 PyErr_SetInterrupt()
156 interrupted = 1;
159 extern int PyErr_CheckSignals();
161 /* ARGSUSED */
162 static RETSIGTYPE
163 #if defined(_M_IX86) && !defined(__QNX__)
164 intcatcher(int sig) /* So the C compiler shuts up */
165 #else /* _M_IX86 */
166 intcatcher(sig)
167 int sig; /* Not used by required by interface */
168 #endif /* _M_IX86 */
170 extern void Py_Exit Py_PROTO((int));
171 static char message[] =
172 "python: to interrupt a truly hanging Python program, interrupt once more.\n";
173 switch (interrupted++) {
174 case 0:
175 break;
176 case 1:
177 write(2, message, strlen(message));
178 break;
179 case 2:
180 interrupted = 0;
181 Py_Exit(1);
182 break;
184 signal(SIGINT, intcatcher);
185 Py_AddPendingCall(PyErr_CheckSignals, NULL);
188 static RETSIGTYPE (*old_siginthandler)() = SIG_DFL;
190 void
191 PyOS_InitInterrupts()
193 if ((old_siginthandler = signal(SIGINT, SIG_IGN)) != SIG_IGN)
194 signal(SIGINT, intcatcher);
195 #ifdef HAVE_SIGINTERRUPT
196 /* This is for SunOS and other modern BSD derivatives.
197 It means that system calls (like read()) are not restarted
198 after an interrupt. This is necessary so interrupting a
199 read() or readline() call works as expected.
200 XXX On old BSD (pure 4.2 or older) you may have to do this
201 differently! */
202 siginterrupt(SIGINT, 1);
203 #endif /* HAVE_SIGINTERRUPT */
206 void
207 PyOS_FiniInterrupts()
209 signal(SIGINT, old_siginthandler);
213 PyOS_InterruptOccurred()
215 if (!interrupted)
216 return 0;
217 interrupted = 0;
218 return 1;
221 #endif /* !OK */
223 void
224 PyOS_AfterFork()