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