]> git.saurik.com Git - bison.git/blob - lib/subpipe.c
Get rid of broken %no-parser, -n, and --no-parser implementation and
[bison.git] / lib / subpipe.c
1 /* Subprocesses with pipes.
2
3 Copyright (C) 2002, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19 /* Written by Paul Eggert <eggert@twinsun.com>
20 and Florian Krohm <florian@edamail.fishkill.ibm.com>. */
21
22 #include <config.h>
23
24 #include "subpipe.h"
25
26 #include <errno.h>
27
28 #include <signal.h>
29 #if ! defined SIGCHLD && defined SIGCLD
30 # define SIGCHLD SIGCLD
31 #endif
32
33 #include <stdlib.h>
34
35 #include <unistd.h>
36 #ifndef STDIN_FILENO
37 # define STDIN_FILENO 0
38 #endif
39 #ifndef STDOUT_FILENO
40 # define STDOUT_FILENO 1
41 #endif
42 #if ! HAVE_DUP2 && ! defined dup2
43 # include <fcntl.h>
44 # define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t))
45 #endif
46
47 #if HAVE_SYS_WAIT_H
48 # include <sys/wait.h>
49 #endif
50 #ifndef WEXITSTATUS
51 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
52 #endif
53 #ifndef WIFEXITED
54 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
55 #endif
56
57 #if HAVE_VFORK_H
58 # include <vfork.h>
59 #endif
60 #if ! HAVE_WORKING_VFORK
61 # define vfork fork
62 #endif
63
64 #include "error.h"
65 #include "unistd-safer.h"
66
67 #include "gettext.h"
68 #define _(Msgid) gettext (Msgid)
69
70 #ifndef __attribute__
71 /* This feature is available in gcc versions 2.5 and later. */
72 # if ! defined __GNUC__ || __GNUC__ < 2 || \
73 (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
74 # define __attribute__(Spec) /* empty */
75 # endif
76 #endif
77
78 #ifndef ATTRIBUTE_UNUSED
79 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
80 #endif
81
82
83 /* Initialize this module. */
84
85 void
86 init_subpipe (void)
87 {
88 #ifdef SIGCHLD
89 /* System V fork+wait does not work if SIGCHLD is ignored. */
90 signal (SIGCHLD, SIG_DFL);
91 #endif
92 }
93
94
95 /* Create a subprocess that is run as a filter. ARGV is the
96 NULL-terminated argument vector for the subprocess. Store read and
97 write file descriptors for communication with the subprocess into
98 FD[0] and FD[1]: input meant for the process can be written into
99 FD[0], and output from the process can be read from FD[1]. Return
100 the subprocess id.
101
102 To avoid deadlock, the invoker must not let incoming data pile up
103 in FD[1] while writing data to FD[0]. */
104
105 pid_t
106 create_subpipe (char const * const *argv, int fd[2])
107 {
108 int pipe_fd[2];
109 int child_fd[2];
110 pid_t pid;
111
112 if (pipe_safer (child_fd) != 0 || pipe_safer (pipe_fd) != 0)
113 error (EXIT_FAILURE, errno, "pipe");
114 fd[0] = child_fd[1];
115 fd[1] = pipe_fd[0];
116 child_fd[1] = pipe_fd[1];
117
118 pid = vfork ();
119 if (pid < 0)
120 error (EXIT_FAILURE, errno, "fork");
121
122 if (! pid)
123 {
124 /* Child. */
125 close (fd[0]);
126 close (fd[1]);
127 dup2 (child_fd[0], STDIN_FILENO);
128 close (child_fd[0]);
129 dup2 (child_fd[1], STDOUT_FILENO);
130 close (child_fd[1]);
131
132 /* The cast to (char **) rather than (char * const *) is needed
133 for portability to older hosts with a nonstandard prototype
134 for execvp. */
135 execvp (argv[0], (char **) argv);
136
137 _exit (errno == ENOENT ? 127 : 126);
138 }
139
140 /* Parent. */
141 close (child_fd[0]);
142 close (child_fd[1]);
143 return pid;
144 }
145
146
147 /* Wait for the subprocess to exit. */
148
149 void
150 reap_subpipe (pid_t pid, char const *program)
151 {
152 #if HAVE_WAITPID || defined waitpid
153 int wstatus;
154 if (waitpid (pid, &wstatus, 0) < 0)
155 error (EXIT_FAILURE, errno, "waitpid");
156 else
157 {
158 int status = WIFEXITED (wstatus) ? WEXITSTATUS (wstatus) : -1;
159 if (status)
160 error (EXIT_FAILURE, 0,
161 _(status == 126
162 ? "subsidiary program `%s' could not be invoked"
163 : status == 127
164 ? "subsidiary program `%s' not found"
165 : status < 0
166 ? "subsidiary program `%s' failed"
167 : "subsidiary program `%s' failed (exit status %d)"),
168 program, status);
169 }
170 #endif
171 }
172
173 void
174 end_of_output_subpipe (pid_t pid ATTRIBUTE_UNUSED,
175 int fd[2] ATTRIBUTE_UNUSED)
176 {
177 }