home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / trn_12.zip / src / final.c < prev    next >
C/C++ Source or Header  |  1993-12-04  |  6KB  |  291 lines

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