home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / groff-1.09-src.lha / src / amiga / groff-1.09 / groff / pipeline.c < prev    next >
C/C++ Source or Header  |  1994-02-13  |  5KB  |  242 lines

  1. /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  2.      Written by James Clark (jjc@jclark.com)
  3.  
  4. This file is part of groff.
  5.  
  6. groff is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 2, or (at your option) any later
  9. version.
  10.  
  11. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  12. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License along
  17. with groff; see the file COPYING.  If not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  19.  
  20. /*
  21. Compile options are:
  22.  
  23. -DWCOREFLAG=0200 (or whatever)
  24. -DHAVE_VFORK_H
  25. -Dvfork=fork
  26. -DHAVE_SYS_SIGLIST
  27. -DHAVE_UNISTD_H
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <signal.h>
  32. #include <errno.h>
  33. #include <sys/types.h>
  34. #ifdef HAVE_UNISTD_H
  35. #include <unistd.h>
  36. #endif
  37. #ifdef HAVE_VFORK_H
  38. #include <vfork.h>
  39. #endif
  40.  
  41. #ifndef errno
  42. extern int errno;
  43. #endif
  44.  
  45. extern char *strerror();
  46.  
  47. #ifdef _POSIX_VERSION
  48.  
  49. #include <sys/wait.h>
  50.  
  51. #define PID_T pid_t
  52.  
  53. #else /* not _POSIX_VERSION */
  54.  
  55. /* traditional Unix */
  56.  
  57. #define WIFEXITED(s) (((s) & 0377) == 0)
  58. #define WIFSTOPPED(s) (((s) & 0377) == 0177)
  59. #define WIFSIGNALED(s) (((s) & 0377) != 0 && (((s) & 0377) != 0177))
  60. #define WEXITSTATUS(s) (((s) >> 8) & 0377)
  61. #define WTERMSIG(s) ((s) & 0177)
  62. #define WSTOPSIG(s) (((s) >> 8) & 0377)
  63.  
  64. #ifndef WCOREFLAG
  65. #define WCOREFLAG 0200
  66. #endif
  67.  
  68. #define PID_T int
  69.  
  70. #endif /* not _POSIX_VERSION */
  71.  
  72. /* SVR4 uses WCOREFLG; Net 2 uses WCOREFLAG. */
  73. #ifndef WCOREFLAG
  74. #ifdef WCOREFLG
  75. #define WCOREFLAG WCOREFLG
  76. #endif /* WCOREFLG */
  77. #endif /* not WCOREFLAG */
  78.  
  79. #ifndef WCOREDUMP
  80. #ifdef WCOREFLAG
  81. #define WCOREDUMP(s) ((s) & WCOREFLAG)
  82. #else /* not WCOREFLAG */
  83. #define WCOREDUMP(s) (0)
  84. #endif /* WCOREFLAG */
  85. #endif /* not WCOREDUMP */
  86.  
  87. #include "pipeline.h"
  88.  
  89. #ifdef __STDC__
  90. #define P(parms) parms
  91. #else
  92. #define P(parms) ()
  93. #endif
  94.  
  95. #define error c_error
  96. extern void error P((char *, char *, char *, char *));
  97. extern void c_fatal P((char *, char *, char *, char *));
  98.  
  99. static void sys_fatal P((char *));
  100. static char *xstrsignal P((int));
  101. static char *itoa P((int));
  102.  
  103. int run_pipeline(ncommands, commands)
  104.      int ncommands;
  105.      char ***commands;
  106. {
  107.   int i;
  108.   int last_input = 0;
  109.   PID_T pids[MAX_COMMANDS];
  110.   int ret = 0;
  111.   int proc_count = ncommands;
  112.  
  113.   for (i = 0; i < ncommands; i++) {
  114.       int pdes[2];
  115.       PID_T pid;
  116.       if (i != ncommands - 1) {
  117.     if (pipe(pdes) < 0)
  118.       sys_fatal("pipe");
  119.       }
  120.       pid = vfork();
  121.       if (pid < 0)
  122.     sys_fatal("fork");
  123.       if (pid == 0) {
  124.     /* child */
  125.     if (last_input != 0) {
  126.       if (close(0) < 0)
  127.         sys_fatal("close");
  128.       if (dup(last_input) < 0)
  129.         sys_fatal("dup");
  130.       if (close(last_input) < 0)
  131.         sys_fatal("close");
  132.     }
  133.     if (i != ncommands - 1) {
  134.       if (close(1) < 0)
  135.         sys_fatal("close");
  136.       if (dup(pdes[1]) < 0)
  137.         sys_fatal("dup");
  138.       if (close(pdes[1]) < 0)
  139.         sys_fatal("close");
  140.       if (close(pdes[0]))
  141.         sys_fatal("close");
  142.     }
  143.     execvp(commands[i][0], commands[i]);
  144.     error("couldn't exec %1: %2", commands[i][0],
  145.           strerror(errno), (char *)0);
  146.     fflush(stderr);        /* just in case error() doesn't */
  147.     _exit(EXEC_FAILED_EXIT_STATUS);
  148.       }
  149.       /* in the parent */
  150.       if (last_input != 0) {
  151.     if (close(last_input) < 0)
  152.       sys_fatal("close");
  153.       }
  154.       if (i != ncommands - 1) {
  155.     if (close(pdes[1]) < 0)
  156.       sys_fatal("close");
  157.     last_input = pdes[0];
  158.       }
  159.       pids[i] = pid;
  160.     }
  161.   while (proc_count > 0) {
  162.     int status;
  163.     PID_T pid = wait(&status);
  164.     if (pid < 0)
  165.       sys_fatal("wait");
  166.     for (i = 0; i < ncommands; i++)
  167.       if (pids[i] == pid) {
  168.     pids[i] = -1;
  169.     --proc_count;
  170.     if (WIFSIGNALED(status)) {
  171.       int sig = WTERMSIG(status);
  172. #ifdef SIGPIPE
  173.       if (sig == SIGPIPE) {
  174.         if (i == ncommands - 1) {
  175.  
  176.           /* This works around a problem that occurred when using the
  177.          rerasterize action in gxditview.  What seemed to be
  178.          happening (on SunOS 4.1.1) was that pclose() closed the
  179.          pipe and waited for groff, gtroff got a SIGPIPE, but
  180.          gpic blocked writing to gtroff, and so groff blocked
  181.          waiting for gpic and gxditview blocked waiting for
  182.          groff.  I don't understand why gpic wasn't getting a
  183.          SIGPIPE. */
  184.           int j;
  185.           for (j = 0; j < ncommands; j++)
  186.         if (pids[j] > 0)
  187.           (void)kill(pids[j], SIGPIPE);
  188.         }
  189.       }
  190.       else
  191. #endif /* SIGPIPE */
  192.       {
  193.         error("%1: %2%3",
  194.           commands[i][0],
  195.           xstrsignal(sig),
  196.           WCOREDUMP(status) ? " (core dumped)" : "");
  197.         ret |= 2;
  198.       }
  199.     }
  200.     else if (WIFEXITED(status)) {
  201.       int exit_status = WEXITSTATUS(status);
  202.       if (exit_status == EXEC_FAILED_EXIT_STATUS)
  203.         ret |= 4;
  204.       else if (exit_status != 0)
  205.         ret |= 1;
  206.     }
  207.     else
  208.       error("unexpected status %1",
  209.         itoa(status), (char *)0, (char *)0);
  210.     break;
  211.       }
  212.   }
  213.   return ret;
  214. }
  215.  
  216. static void sys_fatal(s)
  217.      char *s;
  218. {
  219.   c_fatal("%1: %2", s, strerror(errno), (char *)0);
  220. }
  221.  
  222. static char *itoa(n)
  223.      int n;
  224. {
  225.   static char buf[12];
  226.   sprintf(buf, "%d", n);
  227.   return buf;
  228. }
  229.  
  230. static char *xstrsignal(n)
  231.      int n;
  232. {
  233.   static char buf[sizeof("Signal ") + 1 + sizeof(int)*3];
  234. #ifdef HAVE_SYS_SIGLIST
  235.   extern char *sys_siglist[];
  236.   if (n >= 0 && n < NSIG && sys_siglist[n] != 0)
  237.     return sys_siglist[n];
  238. #endif /* HAVE_SYS_SIGLIST */
  239.   sprintf(buf, "Signal %d", n);
  240.   return buf;
  241. }
  242.