home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / pac / system.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  3.8 KB  |  152 lines

  1. /* system.c */
  2. /**********************************************************************
  3. *    File Name     : system.c
  4. *    Function      : clean up prior to pac exit, pipe readers
  5. *    Author        : Istvan Mohos, 1987
  6. ***********************************************************************/
  7.  
  8. #include "defs.h"
  9.  
  10. go_away(message, die)
  11. char *message;
  12. int die;
  13. {
  14.     static char *fid = "go_away";
  15.     int tput_ok = 1;
  16.  
  17.     _TR
  18.     signal(SIGINT, SIG_IGN);
  19.     signal(SIGHUP, SIG_IGN);
  20.     signal(SIGTERM, SIG_IGN);
  21.     signal(SIGQUIT, SIG_IGN);
  22.  
  23.     if (Context != INIT) {
  24.         write_rc();
  25.         clockoff();
  26.         Clockstat = DISA;
  27.  
  28.         if (message != ZERO && *message == 'I') {
  29.             standend();
  30.             mvaddch(LINES-1, 0, ' ');
  31.             clear();
  32.             pfresh();
  33.             mvcur(0, COLS-1, LINES-1, 0);
  34.             endwin();
  35.         }
  36.         else {
  37.             standend();
  38.             mvaddch(LINES-1, 0, ' ');
  39.             pfresh();
  40.             mvcur(0, COLS-1, LINES-1, 0);
  41.             endwin();
  42.             fprintf(stderr, "\n");
  43.         }
  44.     }
  45.  
  46.     if (die)
  47.         fprintf(stderr, "%s\n", ierbuf);
  48.     TR_
  49.     exit(die);
  50. }
  51.  
  52. fatal(msg) /* kill pipes, print error messages and go_away */
  53. char *msg;
  54. {
  55.     extern int errno, sys_nerr;
  56.     extern char *sys_errlist[];
  57.  
  58.     if (A_ret)
  59.         kill(A_ret, SIGTERM), A_ret = 0;
  60.     if (B_ret)
  61.         kill(B_ret, SIGTERM), B_ret = 0;
  62.     go_away(ierbuf, ierror(msg, 1));
  63. }
  64.  
  65. int
  66. wait_main_pipe() /* empty main pipe to prevent deadlock */
  67.           /* null delimited Mainbuf string may get PIPEMAX long */
  68. {
  69.     register ri;
  70.     register char *accp1, *accp2;
  71.     int done, totalread;
  72.     static char *fid = "wait_main_pipe";
  73.  
  74.     _TR
  75.     accp1 = Mainbuf;
  76.     done = FALSE;
  77.     totalread = 0;
  78.     while (!done) {
  79.         switch
  80.            (Nread = read(A_read[0], accp1, PIPEMAX)) {
  81.         case -1:
  82.             fatal("wait main read");
  83.         case  0:
  84.             fatal("main bc: mysterious end of file");
  85.         default:
  86.             if ((totalread += Nread) >= PIPEMAX - 80)
  87.                      fatal("main pipe overflow");
  88.             accp2 = accp1;
  89.             for (ri = Nread; --ri >= 0;) {
  90.                 if(*accp2 == '\n' && *(accp2 -1) != '\\') {
  91.                     *(accp2) = '\0';
  92.                     --totalread;
  93.                     done = TRUE;
  94.                     break;
  95.                 }
  96.                 ++accp2;
  97.             }
  98.             if (!done)
  99.                 accp1 = accp2;
  100.             break;
  101.         }
  102.     }
  103.     TR_
  104. }
  105.  
  106. /* empty conv pipe into buf, to prevent deadlock.
  107.    if  non-ZERO, buf size must not be less than PIPEMAX.
  108.    if ZERO, Convbuf is used.  Null byte added at end of buf
  109. */
  110. wait_conv_pipe(buf)
  111. char *buf;
  112. {
  113.     register ri;
  114.     register char *convp1, *convp2;
  115.     int done, totalread;
  116.     static char *fid = "wait_conv_pipe";
  117.  
  118.     _TR
  119.     if (buf == ZERO)
  120.         convp1 = Convbuf;
  121.     else
  122.         convp1 = buf;
  123.     done = FALSE;
  124.     totalread = 0;
  125.     while (!done) {
  126.         switch
  127.            (Convread = read(B_read[0], convp1, PIPEMAX)) {
  128.         case -1:
  129.             fatal("wait conv read");
  130.         case  0:
  131.             fatal("conv: mysterious end of file");
  132.         default:
  133.             if ((totalread += Convread) >= PIPEMAX - 80)
  134.                      fatal("conv pipe overflow");
  135.             convp2 = convp1;
  136.             for (ri = Convread; --ri >= 0;) {
  137.                 if(*convp2 == '\n' && *(convp2 -1) != '\\') {
  138.                     *(convp2) = '\0';
  139.                     --totalread;
  140.                     done = TRUE;
  141.                     break;
  142.                 }
  143.                 ++convp2;
  144.             }
  145.             if (!done)
  146.                 convp1 = convp2;
  147.             break;
  148.         }
  149.     }
  150.     TR_
  151. }
  152.