home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume30 / rc / part06 / signal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-30  |  1.5 KB  |  76 lines

  1. /* signal.c: a Hugh-approved signal handler. */
  2.  
  3. #include <signal.h>
  4. #include <setjmp.h>
  5. #include "rc.h"
  6. #include "sigmsgs.h"
  7. #include "jbwrap.h"
  8.  
  9. Jbwrap slowbuf;
  10. volatile SIG_ATOMIC_T slow, interrupt_happened;
  11. void (*sighandlers[NUMOFSIGNALS])(int);
  12.  
  13. static volatile SIG_ATOMIC_T sigcount, caught[NUMOFSIGNALS];
  14.  
  15. extern void catcher(int s) {
  16.     if (forked)
  17.         exit(1); /* exit unconditionally on a signal in a child process */
  18.     if (caught[s] == 0) {
  19.         sigcount++;
  20.         caught[s] = 1;
  21.     }
  22.     signal(s, catcher);
  23.     interrupt_happened = TRUE;
  24. #ifndef SVSIGS
  25.     if (slow)
  26.         longjmp(slowbuf.j, 1);
  27. #endif
  28. }
  29.  
  30. extern void sigchk() {
  31.     void (*h)(int);
  32.     int s, i;
  33.     if (sigcount == 0)
  34.         return; /* ho hum; life as usual */
  35.     if (forked)
  36.         exit(1); /* exit unconditionally on a signal in a child process */
  37.     for (i = 0, s = -1; i < NUMOFSIGNALS; i++)
  38.         if (caught[i] != 0) {
  39.             s = i;
  40.             --sigcount;
  41.             caught[s] = 0;
  42.             break;
  43.         }
  44.     if (s == -1)
  45.         panic("all-zero sig vector with nonzero sigcount");
  46.     if ((h = sighandlers[s]) == SIG_DFL)
  47.         panic("caught signal set to SIG_DFL");
  48.     if (h == SIG_IGN)
  49.         panic("caught signal set to SIG_IGN");
  50.     (*h)(s);
  51. }
  52.  
  53. extern void (*rc_signal(int s, void (*h)(int)))(int) {
  54.     void (*old)(int);
  55.     SIGCHK;
  56.     old = sighandlers[s];
  57.     if (h == SIG_DFL || h == SIG_IGN) {
  58.         signal(s, h);
  59.         sighandlers[s] = h;
  60.     } else {
  61.         sighandlers[s] = h;
  62.         signal(s, catcher);
  63.     }
  64.     return old;
  65. }
  66.  
  67. extern void initsignal() {
  68.     void (*h)(int);
  69.     int i;
  70.     for (i = 1; i < NUMOFSIGNALS; i++) {
  71.         if ((h = signal(i, SIG_DFL)) != SIG_DFL)
  72.             signal(i, h);
  73.         sighandlers[i] = h;
  74.     }
  75. }
  76.