home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / larn.t.Z / larn.t / signal.c < prev    next >
C/C++ Source or Header  |  1988-11-13  |  5KB  |  161 lines

  1. #include <signal.h>
  2.         /* "Larn is copyrighted 1986 by Noah Morgan.\n" */
  3. #include "header.h"
  4. #define BIT(a) (1<<((a)-1))
  5. extern char savefilename[],wizard,predostuff,nosignal;
  6. static s2choose()    /* text to be displayed if ^C during intro screen */
  7.     {
  8.     cursor(1,24); lprcat("Press "); setbold(); lprcat("return"); resetbold();
  9.     lprcat(" to continue: ");   lflush(); 
  10.     }
  11.  
  12. static cntlc()    /* what to do for a ^C */
  13.     {
  14.     if (nosignal) return;    /* don't do anything if inhibited */
  15.     signal(SIGQUIT,SIG_IGN);    signal(SIGINT,SIG_IGN);
  16.     quit(); if (predostuff==1) s2choose(); else showplayer();
  17.     lflush();
  18.     signal(SIGQUIT,cntlc);    signal(SIGINT,cntlc);
  19.     }
  20.  
  21. /*
  22.  *    subroutine to save the game if a hangup signal
  23.  */
  24. static sgam()
  25.     {
  26.     savegame(savefilename);  wizard=1;  died(-257); /* hangup signal */
  27.     }
  28.  
  29. #ifdef SIGTSTP
  30. static tstop() /* control Y    */
  31.     {
  32.     if (nosignal)   return;  /* nothing if inhibited */
  33.     lcreat((char*)0);  clearvt100();    lflush();      signal(SIGTSTP,SIG_DFL);
  34. #ifdef SIGVTALRM
  35.     /* looks like BSD4.2 or higher - must clr mask for signal to take effect*/
  36.     sigsetmask(sigblock(0)& ~BIT(SIGTSTP));
  37. #endif
  38.     kill(getpid(),SIGTSTP);
  39.  
  40.     setupvt100();  signal(SIGTSTP,tstop);
  41.     if (predostuff==1) s2choose(); else drawscreen();
  42.     showplayer();    lflush();
  43.     }
  44. #endif SIGTSTP
  45.  
  46. /*
  47.  *    subroutine to issue the needed signal traps  called from main()
  48.  */
  49. # ifndef OSK
  50. static sigill()  { sigpanic(SIGILL); }     static sigtrap() { sigpanic(SIGTRAP); }
  51. static sigiot()  { sigpanic(SIGIOT); }   static sigemt()  { sigpanic(SIGEMT); }
  52. static sigfpe()  { sigpanic(SIGFPE); }   static sigbus()  { sigpanic(SIGBUS); }
  53. static sigsegv() { sigpanic(SIGSEGV); }  static sigsys()  { sigpanic(SIGSYS); }
  54. static sigpipe() { sigpanic(SIGPIPE); }  static sigterm() { sigpanic(SIGTERM); }
  55. # else OSK
  56. static sigill()  { sigpanic(2); }  static sigtrap() { sigpanic(2); }
  57. static sigiot()  { sigpanic(2); }  static sigemt()  { sigpanic(2); }
  58. static sigfpe()  { sigpanic(2); }  static sigbus()  { sigpanic(2); }
  59. static sigsegv() { sigpanic(2); }  static sigsys()  { sigpanic(2); }
  60. static sigpipe() { sigpanic(2); }  static sigterm() { sigpanic(2); }
  61. # endif OSK
  62.  
  63. sigsetup()
  64.     {
  65.     signal(SIGQUIT, cntlc);         signal(SIGINT,  cntlc); 
  66. # ifndef OSK
  67.     signal(SIGKILL, SIG_IGN);        signal(SIGHUP,  sgam);
  68.     signal(SIGILL,  sigill);        signal(SIGTRAP, sigtrap);
  69.     signal(SIGIOT,  sigiot);        signal(SIGEMT,  sigemt);
  70.     signal(SIGFPE,  sigfpe);        signal(SIGBUS,  sigbus);
  71.     signal(SIGSEGV, sigsegv);        signal(SIGSYS,  sigsys);
  72.     signal(SIGPIPE, sigpipe);        signal(SIGTERM, sigterm);
  73. #ifdef SIGTSTP
  74.     signal(SIGTSTP,tstop);        signal(SIGSTOP,tstop);
  75. #endif SIGTSTP
  76. # endif OSK
  77.     }
  78.  
  79. #ifdef BSD    /* for BSD UNIX? */
  80.  
  81. static char *signame[NSIG] = { "",
  82. "SIGHUP",  /*    1     hangup */
  83. "SIGINT",  /*    2     interrupt */
  84. "SIGQUIT", /*    3     quit */
  85. "SIGILL",  /*    4     illegal instruction (not reset when caught) */
  86. "SIGTRAP", /*    5     trace trap (not reset when caught) */
  87. "SIGIOT",  /*    6     IOT instruction */
  88. "SIGEMT",  /*    7     EMT instruction */
  89. "SIGFPE",  /*    8     floating point exception */
  90. "SIGKILL", /*    9     kill (cannot be caught or ignored) */
  91. "SIGBUS",  /*    10     bus error */
  92. "SIGSEGV", /*    11     segmentation violation */
  93. "SIGSYS",  /*    12     bad argument to system call */
  94. "SIGPIPE", /*    13     Write on a pipe with no one to read it */
  95. "SIGALRM", /*    14     alarm clock */
  96. "SIGTERM", /*    15     software termination signal from kill */
  97. "SIGURG",  /*    16     urgent condition on IO channel */
  98. "SIGSTOP", /*    17     sendable stop signal not from tty */
  99. "SIGTSTP", /*    18     stop signal from tty */
  100. "SIGCONT", /*    19     continue a stopped process */
  101. "SIGCHLD", /*    20     to parent on child stop or exit */
  102. "SIGTTIN", /*    21     to readers pgrp upon background tty read */
  103. "SIGTTOU", /*    22     like TTIN for output if (tp->t_local<OSTOP) */
  104. "SIGIO",   /*    23     input/output possible signal */
  105. "SIGXCPU", /*    24     exceeded CPU time limit */
  106. "SIGXFSZ", /*    25     exceeded file size limit */
  107. "SIGVTALRM",/*  26     virtual time alarm */
  108. "SIGPROF", /*    27     profiling time alarm */
  109. "","","","" };
  110.  
  111. #else BSD    /* for system V? */
  112. # ifndef OSK
  113. static char *signame[NSIG] = { "",
  114. "SIGHUP",  /*    1     hangup */
  115. "SIGINT",  /*    2     interrupt */
  116. "SIGQUIT", /*    3     quit */
  117. "SIGILL",  /*    4     illegal instruction (not reset when caught) */
  118. "SIGTRAP", /*    5     trace trap (not reset when caught) */
  119. "SIGIOT",  /*    6     IOT instruction */
  120. "SIGEMT",  /*    7     EMT instruction */
  121. "SIGFPE",  /*    8     floating point exception */
  122. "SIGKILL", /*    9     kill (cannot be caught or ignored) */
  123. "SIGBUS",  /*    10     bus error */
  124. "SIGSEGV", /*    11     segmentation violation */
  125. "SIGSYS",  /*    12     bad argument to system call */
  126. "SIGPIPE", /*    13     Write on a pipe with no one to read it */
  127. "SIGALRM", /*    14     alarm clock */
  128. "SIGTERM", /*    15     software termination signal from kill */
  129. "SIGUSR1",  /*    16     user defines signal 1 */
  130. "SIGUSR2", /*    17     user defines signal 2 */
  131. "SIGCLD",  /*    18     child death */
  132. "SIGPWR",  /*    19     power fail */
  133. };
  134. /* "","","","","","","","","","","","" }; */
  135. # else OSK
  136. static char *signame[5] = { 
  137. "SIGKILL", /*   0        kill (uncatchable) */
  138. "SIGWAKE", /*   1        sig wake up */
  139. "SIGQUIT", /*    2     quit */
  140. "SIGINT",  /*    3     interrupt */
  141. "SIGHUP"   /*    4     hangup */
  142. };
  143. # endif OSK
  144.  
  145. #endif BSD
  146.  
  147. /*
  148.  *    routine to process a fatal error signal
  149.  */
  150. static sigpanic(sig)
  151.     int sig;
  152.     {
  153.     char buf[128];
  154.     signal(sig,SIG_DFL);
  155.     sprintf(buf,"\nLarn - Panic! Signal %d received [%s]",sig,signame[sig]);
  156.     Write(2,buf,strlen(buf));  sleep(2);
  157.     sncbr();
  158.     savegame(savefilename);
  159.     kill(getpid(),sig); /* this will terminate us */
  160.     }
  161.