home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / trn / part03 / final.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  5.2 KB  |  266 lines

  1. /* $Id: final.c,v 4.4.2.1 1991/12/01 18:05:42 sob PATCH_2 sob $
  2.  *
  3.  * $Log: final.c,v $
  4.  * Revision 4.4.2.1  1991/12/01  18:05:42  sob
  5.  * Patchlevel 2 changes
  6.  *
  7.  * Revision 4.4  1991/09/09  20:18:23  sob
  8.  * release 4.4
  9.  *
  10.  *
  11.  * 
  12.  */
  13. /* This software is Copyright 1991 by Stan Barber. 
  14.  *
  15.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  16.  * use this software as long as: there is no monetary profit gained
  17.  * specifically from the use or reproduction of this software, it is not
  18.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  19.  * included prominently in any copy made. 
  20.  *
  21.  * The author make no claims as to the fitness or correctness of this software
  22.  * for any use whatsoever, and it is provided as is. Any use of this software
  23.  * is at the user's own risk. 
  24.  */
  25.  
  26. #include "EXTERN.h"
  27. #include "common.h"
  28. #include "util.h"
  29. #include "term.h"
  30. #include "ng.h"
  31. #include "init.h"
  32. #include "bits.h"
  33. #include "last.h"
  34. #include "rcstuff.h"
  35. #include "ngdata.h"
  36. #include "artio.h"
  37. #ifdef SERVER
  38. #include "server.h"
  39. #endif
  40. #include "INTERN.h"
  41. #include "final.h"
  42.  
  43. void
  44. final_init()
  45. {
  46. #ifdef SIGTSTP
  47.     sigset(SIGTSTP, stop_catcher);    /* job control signals */
  48.     sigset(SIGTTOU, stop_catcher);    /* job control signals */
  49.     sigset(SIGTTIN, stop_catcher);    /* job control signals */
  50. #endif
  51.  
  52.     sigset(SIGINT, int_catcher);    /* always catch interrupts */
  53. #ifdef SIGHUP
  54.     sigset(SIGHUP, sig_catcher);    /* and hangups */
  55. #endif
  56.     sigset(SIGILL, sig_catcher);
  57. #ifdef SIGTRAP
  58.     sigset(SIGTRAP, sig_catcher);
  59. #endif
  60.     sigset(SIGFPE, sig_catcher);
  61. #ifdef SIGBUS
  62.     sigset(SIGBUS, sig_catcher);
  63. #endif
  64.     sigset(SIGSEGV, sig_catcher);
  65. #ifdef SIGSYS
  66.     sigset(SIGSYS, sig_catcher);
  67. #endif
  68.     sigset(SIGTERM, sig_catcher);
  69. #ifdef SIGXCPU
  70.     sigset(SIGXCPU, sig_catcher);
  71. #endif
  72. #ifdef SIGXFSZ
  73.     sigset(SIGXFSZ, sig_catcher);
  74. #endif
  75. #ifdef SIGWINCH
  76.     sigset(SIGWINCH, winch_catcher);
  77. #endif
  78.  
  79. #ifndef lint
  80. #ifdef SIGEMT
  81.     sigignore(SIGEMT);
  82. #endif
  83. #endif /* lint */
  84. }
  85.  
  86. void                    /* very much void */
  87. finalize(status)
  88. int status;
  89. {
  90.     termlib_reset();
  91.     if (bizarre)
  92.     resetty();
  93.     if (lockname && *lockname)
  94.      UNLINK(lockname);
  95. #ifdef USETHREADS
  96.     if (tmpthread_group)
  97.     UNLINK(tmpthread_file);
  98. #endif
  99. #ifdef SERVER
  100.     if (*active_name)
  101.     UNLINK(active_name);
  102.     if (openart) {
  103.      char artname[MAXFILENAME];
  104.     char intrpwork[MAXFILENAME];
  105.     interp(intrpwork,MAXFILENAME,"%P");
  106.      sprintf(artname, "%s/rrn%ld.%d", intrpwork, (long)openart, getpid());
  107.      UNLINK(artname);
  108.     }
  109.     close_server();
  110. #endif /* SERVER */
  111.     if (status < 0) {
  112.     chdir("/usr/tmp");
  113.     sigset(SIGILL,SIG_DFL);
  114.     abort();
  115.     }
  116.     exit(status);
  117. }
  118.  
  119. /* come here on interrupt */
  120.  
  121. SIGRET
  122. int_catcher()
  123. {
  124.     sigset(SIGINT,int_catcher);
  125. #ifdef DEBUGGING
  126.     if (debug)
  127.     write(2,"int_catcher\n",12);
  128. #endif
  129.     if (!waiting) {
  130.     if (int_count) {        /* was there already an interrupt? */
  131.         write(2,"\nBye-bye.\n",10);
  132.         sig_catcher(0);        /* emulate the other signals */
  133.     }
  134.     int_count++;
  135.     }
  136. }
  137.  
  138. /* come here on signal other than interrupt, stop, or cont */
  139.  
  140. SIGRET
  141. sig_catcher(signo)
  142. int signo;
  143. {
  144. #ifdef VERBOSE
  145.     static char *signame[] = {
  146.     "",
  147.     "HUP",
  148.     "INT",
  149.     "QUIT",
  150.     "ILL",
  151.     "TRAP",
  152.     "IOT",
  153.     "EMT",
  154.     "FPE",
  155.     "KILL",
  156.     "BUS",
  157.     "SEGV",
  158.     "SYS",
  159.     "PIPE",
  160.     "ALRM",
  161.     "TERM",
  162.     "???"
  163. #ifdef SIGTSTP
  164.     ,"STOP",
  165.     "TSTP",
  166.     "CONT",
  167.     "CHLD",
  168.     "TTIN",
  169.     "TTOU",
  170.     "TINT",
  171.     "XCPU",
  172.     "XFSZ"
  173. #ifdef SIGPROF
  174.     ,"VTALARM",
  175.     "PROF"
  176. #endif
  177. #endif
  178.     };
  179. #endif
  180.  
  181. #ifdef DEBUGGING
  182.     if (debug) {
  183.     printf("\nSIG%s--.newsrc not restored in debug\n",signame[signo]);
  184.     finalize(-1);
  185.     }
  186. #endif
  187.     if (panic)
  188.     abort();
  189.     (void) sigset(SIGILL,SIG_DFL);
  190.     panic = TRUE;            /* disable terminal I/O */
  191.     if (doing_ng) {            /* need we reconstitute rc line? */
  192. #ifdef DELAYMARK
  193.     yankback();
  194. #endif
  195.     restore_ng();            /* then do so (hope this works) */
  196.     }
  197.     doing_ng = FALSE;
  198.     if (rc_changed)            /* need we write .newsrc out? */
  199.     write_rc();            /* then do so */
  200.     rc_changed = FALSE;
  201. #ifdef SIGHUP
  202.     if (signo != SIGHUP)
  203. #endif
  204. #ifdef VERBOSE
  205.     IF(verbose)
  206.         printf("\nCaught %s%s--.newsrc restored\n",
  207.         signo ? "a SIG" : "an internal error", signame[signo]);
  208.     ELSE
  209. #endif
  210. #ifdef TERSE
  211.         printf("\nSignal %d--bye bye\n",signo);
  212. #endif
  213.     switch (signo) {
  214. #ifdef SIGBUS
  215.     case SIGBUS:
  216. #endif
  217.     case SIGILL:
  218.     case SIGSEGV:
  219.     finalize(-signo);
  220.     }
  221.     finalize(1);                /* and blow up */
  222. }
  223.  
  224. #ifdef SIGTSTP
  225. /* come here on stop signal */
  226.  
  227. SIGRET
  228. stop_catcher(signo)
  229. int signo;
  230. {
  231.     if (!waiting) {
  232.     checkpoint_rc();    /* good chance of crash while stopped */
  233.     if (clear_on_stop) {
  234.         clear();
  235.         putchar('\n') FLUSH;
  236.     }
  237.     termlib_reset();
  238.     resetty();        /* this is the point of all this */
  239. #ifdef DEBUGGING
  240.     if (debug)
  241.         write(2,"stop_catcher\n",13);
  242. #endif
  243.     sigset(signo,SIG_DFL);    /* enable stop */
  244. #ifdef SIGBLOCK
  245.     sigsetmask(sigblock(0) & ~(1 << (signo-1)));
  246. #endif
  247.     kill(0,signo);        /* and do the stop */
  248.         savetty();
  249. #ifdef MAILCALL
  250.         mailcount = 0;            /* force recheck */
  251. #endif
  252.         if (!panic) {
  253.         if (!waiting) {
  254.         termlib_init();
  255.             noecho();            /* set no echo */
  256.             crmode();            /* set cbreak mode */
  257.             forceme("\f");        /* cause a refresh */
  258.                     /* (defined only if TIOCSTI defined) */
  259.         errno = 0;            /* needed for getcmd */
  260.         }
  261.         }
  262.     }
  263.     sigset(signo,stop_catcher);    /* unenable the stop */
  264. }
  265. #endif
  266.