]> git.saurik.com Git - bison.git/blob - lib/subpipe.c
* tests/actions.at (Actions after errors): New test case.
[bison.git] / lib / subpipe.c
1 /* Subprocesses with pipes.
2
3 Copyright (C) 2002 Free Software Foundation, Inc.
4
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)
8 any later version.
9
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.
14
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19 /* Written by Paul Eggert <eggert@twinsun.com>
20 and Florian Krohm <florian@edamail.fishkill.ibm.com>. */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #if HAVE_SYS_TYPES_H
27 # include <sys/types.h>
28 #endif
29
30 #include <errno.h>
31 #ifndef errno
32 extern int errno;
33 #endif
34
35 #include <signal.h>
36 #if ! defined SIGCHLD && defined SIGCLD
37 # define SIGCHLD SIGCLD
38 #endif
39
40 #if HAVE_STDLIB_H
41 # include <stdlib.h>
42 #endif
43 /* The following test is to work around the gross typo in
44 systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
45 is defined to 0, not 1. */
46 #if ! EXIT_FAILURE
47 # undef EXIT_FAILURE
48 # define EXIT_FAILURE 1
49 #endif
50
51 #if HAVE_UNISTD_H
52 # include <unistd.h>
53 #endif
54 #ifndef STDIN_FILENO
55 # define STDIN_FILENO 0
56 #endif
57 #ifndef STDOUT_FILENO
58 # define STDOUT_FILENO 1
59 #endif
60 #if ! HAVE_DUP2 && ! defined dup2
61 # if HAVE_FCNTL_H
62 # include <fcntl.h>
63 # endif
64 # define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t))
65 #endif
66
67 #if HAVE_SYS_WAIT_H
68 # include <sys/wait.h>
69 #endif
70 #ifndef WEXITSTATUS
71 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
72 #endif
73 #ifndef WIFEXITED
74 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
75 #endif
76
77 #if HAVE_VFORK_H
78 # include <vfork.h>
79 #endif
80 #if ! HAVE_WORKING_VFORK
81 # define vfork fork
82 #endif
83
84 #include "error.h"
85
86 #include "gettext.h"
87 #define _(Msgid) gettext (Msgid)
88
89 #include "subpipe.h"
90
91
92 /* Initialize this module. */
93
94 void
95 init_subpipe (void)
96 {
97 #ifdef SIGCHLD
98 /* System V fork+wait does not work if SIGCHLD is ignored. */
99 signal (SIGCHLD, SIG_DFL);
100 #endif
101 }
102
103
104 /* Create a subprocess that is run as a filter. ARGV is the
105 NULL-terminated argument vector for the subprocess. Store read and
106 write file descriptors for communication with the subprocess into
107 FD[0] and FD[1]: input meant for the process can be written into
108 FD[0], and output from the process can be read from FD[1]. Return
109 the subprocess id.
110
111 To avoid deadlock, the invoker must not let incoming data pile up
112 in FD[1] while writing data to FD[0]. */
113
114 pid_t
115 create_subpipe (char const * const *argv, int fd[2])
116 {
117 int pipe_fd[2];
118 int from_in_fd;
119 int from_out_fd;
120 int to_in_fd;
121 int to_out_fd;
122 pid_t pid;
123
124 if (pipe (pipe_fd) != 0)
125 error (EXIT_FAILURE, errno, "pipe");
126 to_in_fd = pipe_fd[0];
127 to_out_fd = pipe_fd[1];
128
129 if (pipe (pipe_fd) != 0)
130 error (EXIT_FAILURE, errno, "pipe");
131 from_in_fd = pipe_fd[0];
132 from_out_fd = pipe_fd[1];
133
134 pid = vfork ();
135 if (pid < 0)
136 error (EXIT_FAILURE, errno, "fork");
137
138 if (! pid)
139 {
140 /* Child. */
141 close (to_out_fd);
142 close (from_in_fd);
143
144 if (to_in_fd != STDIN_FILENO)
145 {
146 dup2 (to_in_fd, STDIN_FILENO);
147 close (to_in_fd);
148 }
149 if (from_out_fd != STDOUT_FILENO)
150 {
151 dup2 (from_out_fd, STDOUT_FILENO);
152 close (from_out_fd);
153 }
154
155 /* The cast to (char **) rather than (char * const *) is needed
156 for portability to older hosts with a nonstandard prototype
157 for execvp. */
158 execvp (argv[0], (char **) argv);
159
160 _exit (errno == ENOENT ? 127 : 126);
161 }
162
163 /* Parent. */
164 close (to_in_fd);
165 close (from_out_fd);
166 fd[0] = to_out_fd;
167 fd[1] = from_in_fd;
168 return pid;
169 }
170
171
172 /* Wait for the subprocess to exit. */
173
174 void
175 reap_subpipe (pid_t pid, char const *program)
176 {
177 #if HAVE_WAITPID || defined waitpid
178 int wstatus;
179 if (waitpid (pid, &wstatus, 0) < 0)
180 error (EXIT_FAILURE, errno, "waitpid");
181 else
182 {
183 int status = WIFEXITED (wstatus) ? WEXITSTATUS (wstatus) : -1;
184 if (status)
185 error (EXIT_FAILURE, 0,
186 _(status == 126
187 ? "subsidiary program `%s' could not be invoked"
188 : status == 127
189 ? "subsidiary program `%s' not found"
190 : status < 0
191 ? "subsidiary program `%s' failed"
192 : "subsidiary program `%s' failed (exit status %d)"),
193 program, status);
194 }
195 #endif
196 }