]>
git.saurik.com Git - bison.git/blob - lib/subpipe.c
786de26b8ca49d3117a7f2ed7136dc591335cf41
1 /* Subprocesses with pipes.
3 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Paul Eggert <eggert@twinsun.com>
20 and Florian Krohm <florian@edamail.fishkill.ibm.com>. */
31 #if ! defined SIGCHLD && defined SIGCLD
32 # define SIGCHLD SIGCLD
41 # define STDIN_FILENO 0
44 # define STDOUT_FILENO 1
46 #if ! HAVE_DUP2 && ! defined dup2
50 # define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t))
54 # include <sys/wait.h>
57 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
60 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
66 #if ! HAVE_WORKING_VFORK
73 #define _(Msgid) gettext (Msgid)
76 /* Initialize this module. */
82 /* System V fork+wait does not work if SIGCHLD is ignored. */
83 signal (SIGCHLD
, SIG_DFL
);
88 /* Create a subprocess that is run as a filter. ARGV is the
89 NULL-terminated argument vector for the subprocess. Store read and
90 write file descriptors for communication with the subprocess into
91 FD[0] and FD[1]: input meant for the process can be written into
92 FD[0], and output from the process can be read from FD[1]. Return
95 To avoid deadlock, the invoker must not let incoming data pile up
96 in FD[1] while writing data to FD[0]. */
99 create_subpipe (char const * const *argv
, int fd
[2])
108 if (pipe (pipe_fd
) != 0)
109 error (EXIT_FAILURE
, errno
, "pipe");
110 to_in_fd
= pipe_fd
[0];
111 to_out_fd
= pipe_fd
[1];
113 if (pipe (pipe_fd
) != 0)
114 error (EXIT_FAILURE
, errno
, "pipe");
115 from_in_fd
= pipe_fd
[0];
116 from_out_fd
= pipe_fd
[1];
120 error (EXIT_FAILURE
, errno
, "fork");
128 if (to_in_fd
!= STDIN_FILENO
)
130 dup2 (to_in_fd
, STDIN_FILENO
);
133 if (from_out_fd
!= STDOUT_FILENO
)
135 dup2 (from_out_fd
, STDOUT_FILENO
);
139 /* The cast to (char **) rather than (char * const *) is needed
140 for portability to older hosts with a nonstandard prototype
142 execvp (argv
[0], (char **) argv
);
144 _exit (errno
== ENOENT
? 127 : 126);
156 /* Wait for the subprocess to exit. */
159 reap_subpipe (pid_t pid
, char const *program
)
161 #if HAVE_WAITPID || defined waitpid
163 if (waitpid (pid
, &wstatus
, 0) < 0)
164 error (EXIT_FAILURE
, errno
, "waitpid");
167 int status
= WIFEXITED (wstatus
) ? WEXITSTATUS (wstatus
) : -1;
169 error (EXIT_FAILURE
, 0,
171 ? "subsidiary program `%s' could not be invoked"
173 ? "subsidiary program `%s' not found"
175 ? "subsidiary program `%s' failed"
176 : "subsidiary program `%s' failed (exit status %d)"),