home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Parser / intrcheck.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  5KB  |  268 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  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.
  16.  
  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.
  20.  
  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.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Check for interrupts */
  33. #include "config.h"
  34. #include "other/all_segments.h"
  35. #include "stdio.h"
  36.  
  37.  
  38. /* config.h may or may not define DL_IMPORT */
  39. #ifndef DL_IMPORT    /* declarations for DLL import/export */
  40. #define DL_IMPORT(RTYPE) RTYPE
  41. #endif
  42.  
  43. #include "myproto.h"
  44. #include "mymalloc.h" /* For ANY */
  45. #include "intrcheck.h"
  46.  
  47. #ifndef WITHOUT_INTERRUPTS
  48.  
  49. /* Copied here from ceval.h -- can't include that file. */
  50. DL_IMPORT(int) Py_AddPendingCall Py_PROTO((int (*func) Py_PROTO((ANY *)), ANY *arg)) SEG_CEVAL_H;
  51.  
  52.  
  53. #ifdef QUICKWIN
  54.  
  55. #include <io.h>
  56.  
  57. void
  58. PyOS_InitInterrupts()
  59. {
  60. }
  61.  
  62. void
  63. PyOS_FiniInterrupts()
  64. {
  65. }
  66.  
  67. int
  68. PyOS_InterruptOccurred()
  69. {
  70.     _wyield();
  71. }
  72.  
  73. #define OK
  74.  
  75. #endif /* QUICKWIN */
  76.  
  77. #if defined(_M_IX86) && !defined(__QNX__)
  78. #include <io.h>
  79. #endif
  80.  
  81. #if defined(MSDOS) && !defined(QUICKWIN)
  82.  
  83. #ifdef __GNUC__
  84.  
  85. /* This is for DJGPP's GO32 extender.  I don't know how to trap
  86.  * control-C  (There's no API for ctrl-C, and I don't want to mess with
  87.  * the interrupt vectors.)  However, this DOES catch control-break.
  88.  * --Amrit
  89.  */
  90.  
  91. #include <go32.h>
  92.  
  93. void
  94. PyOS_InitInterrupts()
  95. {
  96.     _go32_want_ctrl_break(1 /* TRUE */);
  97. }
  98.  
  99. void
  100. PyOS_FiniInterrupts()
  101. {
  102. }
  103.  
  104. int
  105. PyOS_InterruptOccurred()
  106. {
  107.     return _go32_was_ctrl_break_hit();
  108. }
  109.  
  110. #else /* !__GNUC__ */
  111.  
  112. /* This might work for MS-DOS (untested though): */
  113.  
  114. void
  115. PyOS_InitInterrupts()
  116. {
  117. }
  118.  
  119. void
  120. PyOS_FiniInterrupts()
  121. {
  122. }
  123.  
  124. int
  125. PyOS_InterruptOccurred()
  126. {
  127.     int interrupted = 0;
  128.     while (kbhit()) {
  129.         if (getch() == '\003')
  130.             interrupted = 1;
  131.     }
  132.     return interrupted;
  133. }
  134.  
  135. #endif /* __GNUC__ */
  136.  
  137. #define OK
  138.  
  139. #endif /* MSDOS && !QUICKWIN */
  140.  
  141.  
  142. #ifdef macintosh
  143.  
  144. /* The Mac interrupt code has moved to macglue.c */
  145. #define OK
  146.  
  147. #endif /* macintosh */
  148.  
  149.  
  150. #ifndef OK
  151.  
  152. /* Default version -- for real operating systems and for Standard C */
  153.  
  154. #include <stdio.h>
  155. #include <string.h>
  156. #include <signal.h>
  157. #ifdef HAVE_UNISTD_H
  158. #include <unistd.h>
  159. #endif
  160.  
  161. static int interrupted;
  162.  
  163. void
  164. PyErr_SetInterrupt()
  165. {
  166.     interrupted = 1;
  167. }
  168.  
  169. extern int PyErr_CheckSignals();
  170.  
  171. /* ARGSUSED */
  172. static RETSIGTYPE
  173. #if defined(_M_IX86) && !defined(__QNX__)
  174. intcatcher(int sig)    /* So the C compiler shuts up */
  175. #else /* _M_IX86 */
  176. intcatcher(sig)
  177.     int sig; /* Not used by required by interface */
  178. #endif /* _M_IX86 */
  179. {
  180.     extern void Py_Exit Py_PROTO((int));
  181.     static char message[] =
  182. "python: to interrupt a truly hanging Python program, interrupt once more.\n";
  183.     switch (interrupted++) {
  184.     case 0:
  185.         break;
  186.     case 1:
  187.         write(2, message, strlen(message));
  188.         break;
  189.     case 2:
  190.         interrupted = 0;
  191.         Py_Exit(1);
  192.         break;
  193.     }
  194.     signal(SIGINT, intcatcher);
  195.     Py_AddPendingCall(PyErr_CheckSignals, NULL);
  196. }
  197.  
  198. static RETSIGTYPE (*old_siginthandler)() = SIG_DFL;
  199.  
  200. void
  201. PyOS_InitInterrupts()
  202. {
  203.     if ((old_siginthandler = signal(SIGINT, SIG_IGN)) != SIG_IGN)
  204.         signal(SIGINT, intcatcher);
  205. #ifdef HAVE_SIGINTERRUPT
  206.     /* This is for SunOS and other modern BSD derivatives.
  207.        It means that system calls (like read()) are not restarted
  208.        after an interrupt.  This is necessary so interrupting a
  209.        read() or readline() call works as expected.
  210.        XXX On old BSD (pure 4.2 or older) you may have to do this
  211.        differently! */
  212.     siginterrupt(SIGINT, 1);
  213. #endif /* HAVE_SIGINTERRUPT */
  214. }
  215.  
  216. void
  217. PyOS_FiniInterrupts()
  218. {
  219.     signal(SIGINT, old_siginthandler);
  220. }
  221.  
  222. int
  223. PyOS_InterruptOccurred()
  224. {
  225.     if (!interrupted)
  226.         return 0;
  227.     interrupted = 0;
  228.     return 1;
  229. }
  230.  
  231. #endif /* !OK */
  232.  
  233. #else /* ! WITHOUT_INTERRUPTS */
  234.  
  235. static int (*handler)(void) = NULL;
  236.  
  237. void
  238. PyOS_SetInterruptChecker( int (*func)(void) )
  239. {
  240.     handler = func;
  241. }
  242.  
  243. void
  244. PyOS_InitInterrupts()
  245. {
  246.     DMESSAGE("in InitInterrupts");
  247. }
  248.  
  249. void
  250. PyOS_FiniInterrupts()
  251. {
  252. }
  253.  
  254. int
  255. PyOS_InterruptOccurred()
  256. {
  257.     if (handler == NULL)
  258.         return 0;
  259.     return handler();
  260.  
  261. }
  262. #endif /* ! WITHOUT_INTERRUPTS */
  263.  
  264. void
  265. PyOS_AfterFork()
  266. {
  267. }
  268.