home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games (Alt) / INFESPGAMES.iso / os2 / backgam / source / signal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-22  |  4.5 KB  |  181 lines

  1. /*************************************************************
  2.  *    ______                                                 *
  3.  *   /     /\  TinyFugue was derived from a client initially *
  4.  *  /   __/  \ written by Anton Rang (Tarrant) and later     *
  5.  *  |  / /\  | modified by Leo Plotkin (Grod).  The early    *
  6.  *  |  |/    | versions of TinyFugue written by Greg Hudson  *
  7.  *  |  X__/  | (Explorer_Bob).  The current version is       *
  8.  *  \ /      / written and maintained by Ken Keys (Hawkeye), *
  9.  *   \______/  who can be reached at kkeys@ucsd.edu.         *
  10.  *                                                           *
  11.  *             No copyright 1992, no rights reserved.        *
  12.  *             Fugue is in the public domain.                *
  13.  *************************************************************/
  14.  
  15. /**************************************
  16.  * Fugue signal handlers              *
  17.  *                                    *
  18.  * The following signals are trapped: *
  19.  * SIGINT                             *
  20.  * SIGTSTP                            *
  21.  * SIGSEGV                            *
  22.  * SIGBUS                             *
  23.  * SIGQUIT                            *
  24.  * SIGILL                             *
  25.  * SIGTRAP                            *
  26.  * SIGFPE                             *
  27.  * SIGWINCH                           *
  28.  * SIGPIPE                            *
  29.  **************************************/
  30.  
  31. #include <stdio.h>
  32. #include <signal.h>
  33. #include "tf.h"
  34. #include "dstring.h"
  35. #include "util.h"
  36. #include "history.h"
  37. #include "world.h"
  38. #include "process.h"
  39. #include "socket.h"
  40. #include "keyboard.h"
  41. #include "output.h"
  42.  
  43. #define TEST_SIG(sig) (pending_signals & (1 << ((sig) - 1)))
  44. #define SET_SIG(sig) (pending_signals |= (1 << ((sig) - 1)))
  45. #define CLR_SIG(sig) (pending_signals &= ~(1 << ((sig) - 1)))
  46. #define ZERO_SIG() (pending_signals = 0)
  47.  
  48. static unsigned long pending_signals;
  49.  
  50. extern int visual;
  51.  
  52. static void NDECL(sigint_run);
  53. #ifndef WINS
  54. # ifdef SIGTSTP
  55. static void NDECL(sigtstp_run);
  56. # endif
  57. #endif
  58. #ifndef NOCOREHANDLERS
  59. static void FDECL(core_handler,(int sig));
  60. static void FDECL(coremsg,(int sig));
  61. #endif
  62. static void FDECL(ignore_signal,(int sig));
  63. static void FDECL(signal_scheduler,(int sig));
  64.  
  65. void NDECL(process_signals);
  66. void NDECL(init_signals);
  67.  
  68.  
  69. void init_signals()
  70. {
  71.     ZERO_SIG();
  72.     signal(SIGINT  , signal_scheduler);
  73. #ifndef WINS
  74. # ifdef SIGTSTP
  75.     signal(SIGTSTP , signal_scheduler);
  76. # endif
  77. #endif
  78. #ifndef NO_COREHANDLERS
  79.     signal(SIGSEGV , core_handler);
  80.     signal(SIGBUS  , core_handler);
  81.     signal(SIGQUIT , core_handler);
  82.     signal(SIGILL  , core_handler);
  83.     signal(SIGTRAP , core_handler);
  84.     signal(SIGFPE  , core_handler);
  85. #endif
  86.     signal(SIGPIPE , ignore_signal);
  87. #ifdef SIGWINCH
  88.     signal(SIGWINCH, signal_scheduler);
  89. #endif
  90. }
  91.  
  92. static void ignore_signal(sig)
  93.     int sig;
  94. {
  95.     signal(sig, ignore_signal);
  96. }
  97.  
  98. static void sigint_run()
  99. {
  100.     int c;
  101.     extern int borg;
  102.  
  103.     if (visual) fix_screen();
  104.     else clear_input_window();
  105.     printf("C) continue; X) exit; T) disable triggers; P) kill processes ");
  106.     fflush(stdout);
  107.     c = getch();
  108.     clear_input_window();
  109.     if (strchr("xyXY", c)) die("Interrupt, exiting.\n");
  110.     setup_screen();
  111.     do_replace();
  112.     if (c == 't' || c == 'T') {
  113.         borg = FALSE;
  114.         oputs("% Cyborg triggers disabled");
  115.     } else if (c == 'p' || c == 'P') {
  116.         kill_procs();
  117.     }
  118. }
  119.  
  120. #ifndef WINS
  121. # ifdef SIGTSTP
  122. static void sigtstp_run()
  123. {
  124.     extern Stringp keybuf;
  125.  
  126.     cooked_echo_mode();
  127.     if (visual) fix_screen();
  128.     kill(getpid(), SIGSTOP);
  129.     cbreak_noecho_mode();
  130.     if (keybuf->len) set_refresh_pending();
  131.     setup_screen();
  132.     do_replace();
  133. #ifdef MAILDELAY
  134.     check_mail();
  135. #endif
  136. }
  137. # endif
  138. #endif
  139.  
  140. #ifndef NOCOREHANDLERS
  141. static void coremsg(sig)
  142.     int sig;
  143. {
  144.     printf("Core dumped - signal %d\n", sig);
  145.     puts("Please, if you can, examine the core and report the location");
  146.     puts("of it to kkeys@ucsd.edu.");
  147. }
  148.  
  149. static void core_handler(sig)
  150.     int sig;
  151. {
  152.     cleanup();
  153.     if (sig != SIGQUIT) coremsg(sig);
  154.     signal(sig, SIG_DFL);
  155.     kill(getpid(), sig);
  156. }
  157. #endif
  158.  
  159. static void signal_scheduler(sig)
  160.     int sig;
  161. {
  162.     signal(sig, signal_scheduler);  /* restore handler (SysV) */
  163.     SET_SIG(sig);                   /* set flag to deal with it later */
  164. }
  165.  
  166. void process_signals()
  167. {
  168.     if (pending_signals == 0) return;
  169.  
  170.     if (TEST_SIG(SIGINT))   sigint_run();
  171. #ifndef WINS
  172. # ifdef SIGTSTP
  173.     if (TEST_SIG(SIGTSTP))  sigtstp_run();
  174. # endif
  175. #endif
  176. #ifdef SIGWINCH
  177.     if (TEST_SIG(SIGWINCH)) get_window_size();
  178. #endif
  179.     ZERO_SIG();
  180. }
  181.