home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Parser / intrcheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  3.1 KB  |  207 lines

  1.  
  2. /* Check for interrupts */
  3.  
  4. #include "Python.h"
  5.  
  6. #ifdef QUICKWIN
  7.  
  8. #include <io.h>
  9.  
  10. void
  11. PyOS_InitInterrupts(void)
  12. {
  13. }
  14.  
  15. void
  16. PyOS_FiniInterrupts(void)
  17. {
  18. }
  19.  
  20. int
  21. PyOS_InterruptOccurred(void)
  22. {
  23.     _wyield();
  24. }
  25.  
  26. #define OK
  27.  
  28. #endif /* QUICKWIN */
  29.  
  30. #if defined(_M_IX86) && !defined(__QNX__)
  31. #include <io.h>
  32. #endif
  33.  
  34. #if defined(MSDOS) && !defined(QUICKWIN)
  35.  
  36. #ifdef __GNUC__
  37.  
  38. /* This is for DJGPP's GO32 extender.  I don't know how to trap
  39.  * control-C  (There's no API for ctrl-C, and I don't want to mess with
  40.  * the interrupt vectors.)  However, this DOES catch control-break.
  41.  * --Amrit
  42.  */
  43.  
  44. #include <go32.h>
  45.  
  46. void
  47. PyOS_InitInterrupts(void)
  48. {
  49.     _go32_want_ctrl_break(1 /* TRUE */);
  50. }
  51.  
  52. void
  53. PyOS_FiniInterrupts(void)
  54. {
  55. }
  56.  
  57. int
  58. PyOS_InterruptOccurred(void)
  59. {
  60.     return _go32_was_ctrl_break_hit();
  61. }
  62.  
  63. #else /* !__GNUC__ */
  64.  
  65. /* This might work for MS-DOS (untested though): */
  66.  
  67. void
  68. PyOS_InitInterrupts(void)
  69. {
  70. }
  71.  
  72. void
  73. PyOS_FiniInterrupts(void)
  74. {
  75. }
  76.  
  77. int
  78. PyOS_InterruptOccurred(void)
  79. {
  80.     int interrupted = 0;
  81.     while (kbhit()) {
  82.         if (getch() == '\003')
  83.             interrupted = 1;
  84.     }
  85.     return interrupted;
  86. }
  87.  
  88. #endif /* __GNUC__ */
  89.  
  90. #define OK
  91.  
  92. #endif /* MSDOS && !QUICKWIN */
  93.  
  94.  
  95. #ifdef macintosh
  96.  
  97. /* The Mac interrupt code has moved to macglue.c */
  98. #define OK
  99.  
  100. #endif /* macintosh */
  101.  
  102.  
  103. #ifndef OK
  104.  
  105. /* Default version -- for real operating systems and for Standard C */
  106.  
  107. #include <stdio.h>
  108. #include <string.h>
  109. #include <signal.h>
  110. #ifdef HAVE_UNISTD_H
  111. #include <unistd.h>
  112. #endif
  113.  
  114. static int interrupted;
  115.  
  116. void
  117. PyErr_SetInterrupt(void)
  118. {
  119.     interrupted = 1;
  120. }
  121.  
  122. extern int PyErr_CheckSignals(void);
  123.  
  124. static int
  125. checksignals_witharg(void * arg)
  126. {
  127.     return PyErr_CheckSignals();
  128. }
  129.  
  130. static void
  131. intcatcher(int sig)
  132. {
  133.     extern void Py_Exit(int);
  134.     static char message[] =
  135. "python: to interrupt a truly hanging Python program, interrupt once more.\n";
  136.     switch (interrupted++) {
  137.     case 0:
  138.         break;
  139.     case 1:
  140.         write(2, message, strlen(message));
  141.         break;
  142.     case 2:
  143.         interrupted = 0;
  144.         Py_Exit(1);
  145.         break;
  146.     }
  147.     signal(SIGINT, intcatcher);
  148.     Py_AddPendingCall(checksignals_witharg, NULL);
  149. }
  150.  
  151. static void (*old_siginthandler)(int) = SIG_DFL;
  152.  
  153. void
  154. PyOS_InitInterrupts(void)
  155. {
  156.     if ((old_siginthandler = signal(SIGINT, SIG_IGN)) != SIG_IGN)
  157.         signal(SIGINT, intcatcher);
  158. #ifdef HAVE_SIGINTERRUPT
  159.     /* This is for SunOS and other modern BSD derivatives.
  160.        It means that system calls (like read()) are not restarted
  161.        after an interrupt.  This is necessary so interrupting a
  162.        read() or readline() call works as expected.
  163.        XXX On old BSD (pure 4.2 or older) you may have to do this
  164.        differently! */
  165.     siginterrupt(SIGINT, 1);
  166. #endif /* HAVE_SIGINTERRUPT */
  167. }
  168.  
  169. void
  170. PyOS_FiniInterrupts(void)
  171. {
  172.     signal(SIGINT, old_siginthandler);
  173. }
  174.  
  175. int
  176. PyOS_InterruptOccurred(void)
  177. {
  178. #ifdef __SASC
  179.     extern void __regargs __chkabort(void);
  180.     extern void chkabort(void);
  181.  
  182.     chkabort();        /* explicit Amiga SAS/C ^C check */
  183. #endif
  184.     if (!interrupted)
  185.         return 0;
  186.     interrupted = 0;
  187.     return 1;
  188. }
  189.  
  190. #ifdef __SASC
  191. /* Amiga SAS/C replacement ^C handler */
  192. void __regargs _CXBRK(void)
  193. {
  194.     interrupted=1;
  195. }
  196. #endif
  197.  
  198. #endif /* !OK */
  199.  
  200. void
  201. PyOS_AfterFork(void)
  202. {
  203. #ifdef WITH_THREAD
  204.     PyEval_ReInitThreads();
  205. #endif
  206. }
  207.