home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / mor4873s.lzh / SIGNALS.C < prev    next >
C/C++ Source or Header  |  1988-11-01  |  6KB  |  306 lines

  1. #include <curses.h>
  2. #if defined(MSDOS) && defined(ANSI)
  3. #include "ms_ansi.h"
  4. #endif
  5. #include <signal.h>
  6.  
  7. #include "constant.h"
  8. #include "config.h"
  9. #include "types.h"
  10. #include "externs.h"
  11.  
  12. #ifdef USG
  13. #include <string.h>
  14. #else
  15. #include <strings.h>
  16. #endif
  17.  
  18. extern int total_winner;
  19. extern int moria_flag;
  20. extern int search_flag;
  21.  
  22. #ifdef USG
  23. /* no local special characters */
  24. #else
  25. extern struct ltchars save_special_chars;
  26. #endif
  27.  
  28. int error_sig, error_code;
  29.  
  30. /* This signal package was brought to you by           -JEW-  */
  31.  
  32. /* on some systems, signal and suspend_handler must be declared as 'void'
  33.    instead of 'int', this varies even among different OS releases, so there
  34.    is no good way to solve the problem with ifdefs */
  35. #ifndef MSDOS
  36. int (*signal())();
  37. #endif
  38. int signal_save_core();
  39. int signal_save_no_core();
  40. int signal_ask_quit();
  41.  
  42. #ifdef USG
  43. /* no suspend signal */
  44. #else
  45. int (*suspend_handler)();
  46. #endif
  47.  
  48. /* only allow one signal handler to execute, otherwise can get multiple save
  49.        files by sending multiple signals */
  50. static int caught_signal = 0;
  51.  
  52. init_signals()
  53. {
  54.   (void) signal(SIGINT, signal_ask_quit);
  55.   (void) signal(SIGFPE, signal_save_core);
  56. #ifdef MSDOS
  57.   /* many fewer signals under MSDOS */
  58. #else
  59.   (void) signal(SIGHUP, signal_save_no_core);
  60.   (void) signal(SIGQUIT, signal_save_core);
  61.   (void) signal(SIGILL, signal_save_core);
  62.   (void) signal(SIGTRAP, signal_save_core);
  63.   (void) signal(SIGIOT, signal_save_core);
  64.   (void) signal(SIGEMT, signal_save_core);
  65.   (void) signal(SIGKILL, signal_save_core);
  66.   (void) signal(SIGBUS, signal_save_core);
  67.   (void) signal(SIGSEGV, signal_save_core);
  68.   (void) signal(SIGSYS, signal_save_core);
  69.   (void) signal(SIGTERM, signal_save_core);
  70. #endif
  71. }
  72.  
  73. /*ARGSUSED*/
  74. #ifdef USG
  75. signal_save_core(sig)
  76. int sig;
  77. {
  78.   if (caught_signal)
  79.     return;
  80.   caught_signal = 1;
  81.   panic_save = 1;
  82.  
  83.   error_sig = sig;
  84.   error_code = 0;
  85.   if (!character_saved)
  86.     {
  87.       prt("OH NO!!!!!!!!!!  Attempting panic save.", 23, 0);
  88.       save_char(FALSE, FALSE);
  89.     }
  90. #ifdef SIGQUIT
  91.   (void) signal(SIGQUIT, SIG_DFL);
  92. #endif
  93. #ifdef MSDOS 
  94.   /* terminal gets reset in exit_game() ... */
  95. #else
  96.   /* restore terminal settings */
  97. #ifndef BUGGY_CURSES
  98.   nl();
  99. #endif
  100. #if defined(ultrix)
  101.   nocrmode();
  102. #else
  103.   nocbreak();
  104. #endif
  105.   echo();
  106.   resetterm();
  107.   /* restore the saved values of the local special chars */
  108.   /* no local special characters */
  109.   /* allow QUIT signal */
  110.   /* nothing needs to be done here */
  111.   (void) kill(getpid(), 3);
  112. #endif
  113.   exit_game();
  114. }
  115. #else
  116. signal_save_core(sig, code, scp)
  117. int sig, code;
  118. struct sigcontext *scp;
  119. {
  120.   if (caught_signal)
  121.     return;
  122.   caught_signal = 1;
  123.   panic_save = 1;
  124.  
  125.   error_sig = sig;
  126.   error_code = code;
  127.   if (!character_saved)
  128.     {
  129.       prt("OH NO!!!!!!!!!!  Attempting panic save.", 23, 0);
  130.       save_char(FALSE, FALSE);
  131.     }
  132.   (void) signal(SIGQUIT, SIG_DFL);
  133.   /* restore terminal settings */
  134. #ifndef BUGGY_CURSES
  135.   nl();
  136. #endif
  137. #if defined(ultrix)
  138.   nocrmode();
  139. #else
  140.   nocbreak();
  141. #endif
  142.   echo();
  143.   /* restore the saved values of the local special chars */
  144.   (void) ioctl(0, TIOCSLTC, (char *)&save_special_chars);
  145.   /* allow QUIT signal */
  146.   (void) sigsetmask(0);
  147.   (void) kill(getpid(), 3);
  148.   exit_game();
  149. }
  150. #endif
  151.  
  152. /*ARGSUSED*/
  153. #ifdef USG
  154. # ifndef MSDOS
  155. signal_save_no_core(sig)
  156. int sig;
  157. {
  158.   if (caught_signal)
  159.     return;
  160.   caught_signal = 1;
  161.   panic_save = 1;
  162.  
  163.   error_sig = sig;
  164.   error_code = 0;
  165.   if (!character_saved)
  166.     save_char(FALSE, TRUE);
  167.   exit_game();
  168. }
  169. # endif /* MSDOS */
  170. #else
  171. signal_save_no_core(sig, code, scp)
  172. int sig, code;
  173. struct sigcontext *scp;
  174. {
  175.   if (caught_signal)
  176.     return;
  177.   caught_signal = 1;
  178.   panic_save = 1;
  179.  
  180.   error_sig = sig;
  181.   error_code = code;
  182.   if (!character_saved)
  183.     save_char(FALSE, TRUE);
  184.   exit_game();
  185. }
  186. #endif
  187.  
  188. /*ARGSUSED*/
  189. #ifdef USG
  190. signal_ask_quit(sig)
  191. int sig;
  192. {
  193.   char command;
  194.  
  195.   /* reset signal handler */
  196.   (void) signal(sig, signal_ask_quit);
  197.   find_flag = FALSE;
  198.   if (search_flag)
  199.     search_off();
  200.   if (py.flags.rest > 0)
  201.     {
  202.       rest_off();
  203.       return;
  204.     }
  205.   if (get_com("Do you really want to quit?", &command))
  206.     switch(command)
  207.       {
  208.       case 'y': case 'Y':
  209.         if (character_generated)
  210.       {
  211.         (void) strcpy(died_from, "Quitting.");
  212.         upon_death();
  213.       }
  214.         else
  215.           exit_game();
  216.     break;
  217.       }
  218.   erase_line(MSG_LINE, 0);
  219.   /* in case control-c typed during msg_print */
  220.   if (wait_for_more)
  221.     {
  222.       put_buffer(" -more-", MSG_LINE, 0);
  223.       put_qio();
  224.     }
  225. }
  226. #else
  227. signal_ask_quit(sig, code, scp)
  228. int sig, code;
  229. struct sigcontext *scp;
  230. {
  231.   char command;
  232.  
  233.   /* no need to reset signal handler */
  234.   find_flag = FALSE;
  235.   if (search_flag)
  236.     search_off();
  237.   if (py.flags.rest > 0)
  238.     {
  239.       rest_off();
  240.       return;
  241.     }
  242.   if (get_com("Do you really want to quit?", &command))
  243.     switch(command)
  244.       {
  245.       case 'y': case 'Y':
  246.     if (character_generated)
  247.       {
  248.         (void) strcpy(died_from, "Quitting.");
  249.         upon_death();
  250.       }
  251.     else
  252.       exit_game();
  253.     break;
  254.       }
  255.   erase_line(MSG_LINE, 0);
  256.   /* in case control-c typed during msg_print */
  257.   if (wait_for_more)
  258.     {
  259.       put_buffer(" -more-", MSG_LINE, 0);
  260.       put_qio();
  261.     }
  262. }
  263. #endif
  264.  
  265. no_controlz()
  266. {
  267. #ifdef USG
  268.   /* no suspend signal */
  269. #else
  270.   suspend_handler = signal(SIGTSTP, SIG_IGN);
  271. #endif
  272. }
  273.  
  274. controlz()
  275. {
  276. #ifdef USG
  277.   /* no suspend signal */
  278. #else
  279.   (void) signal(SIGTSTP, suspend_handler);
  280. #endif
  281. }
  282.  
  283. ignore_signals()
  284. {
  285.   (void) signal(SIGINT, SIG_IGN);
  286. #ifdef SIGQUIT
  287.   (void) signal(SIGQUIT, SIG_IGN);
  288. #endif
  289. }
  290.  
  291. default_signals()
  292. {
  293.   (void) signal(SIGINT, SIG_DFL);
  294. #ifdef SIGQUIT
  295.   (void) signal(SIGQUIT, SIG_DFL);
  296. #endif
  297. }
  298.  
  299. restore_signals()
  300. {
  301.   (void) signal(SIGINT, signal_ask_quit);
  302. #ifdef SIGQUIT
  303.   (void) signal(SIGQUIT, signal_save_core);
  304. #endif
  305. }
  306.