]> git.saurik.com Git - bison.git/blame - lib/subpipe.c
Update POTFILES.
[bison.git] / lib / subpipe.c
CommitLineData
5b2d69cc
PE
1/* Subprocesses with pipes.
2
219c26ea 3 Copyright (C) 2002, 2004-2006, 2009-2010 Free Software Foundation,
98744608 4 Inc.
5b2d69cc 5
f16b0819 6 This program is free software: you can redistribute it and/or modify
5b2d69cc 7 it under the terms of the GNU General Public License as published by
f16b0819
PE
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
5b2d69cc
PE
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
f16b0819 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
5b2d69cc
PE
18
19/* Written by Paul Eggert <eggert@twinsun.com>
20 and Florian Krohm <florian@edamail.fishkill.ibm.com>. */
21
231ed89a 22#include <config.h>
5b2d69cc 23
b970803c 24#include "subpipe.h"
5b2d69cc
PE
25
26#include <errno.h>
5b2d69cc
PE
27
28#include <signal.h>
29#if ! defined SIGCHLD && defined SIGCLD
30# define SIGCHLD SIGCLD
31#endif
32
b970803c 33#include <stdlib.h>
5b2d69cc 34
3b2942e6 35#include <unistd.h>
5b2d69cc
PE
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
04098407 43# include <fcntl.h>
5b2d69cc
PE
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"
3ea5f0ec 65#include "unistd-safer.h"
5b2d69cc
PE
66
67#include "gettext.h"
68#define _(Msgid) gettext (Msgid)
69
c66dfadd
PE
70#ifndef __attribute__
71/* This feature is available in gcc versions 2.5 and later. */
02650b7f 72# if ! defined __GNUC__ || __GNUC__ < 2 || \
c66dfadd
PE
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
5b2d69cc
PE
82
83/* Initialize this module. */
84
85void
86init_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
105pid_t
106create_subpipe (char const * const *argv, int fd[2])
107{
108 int pipe_fd[2];
0fab8d7b 109 int child_fd[2];
5b2d69cc
PE
110 pid_t pid;
111
b4c2a797
PE
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];
5b2d69cc
PE
117
118 pid = vfork ();
119 if (pid < 0)
b4c2a797 120 error (EXIT_FAILURE, errno, "fork");
5b2d69cc
PE
121
122 if (! pid)
123 {
124 /* Child. */
0fab8d7b
PE
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]);
5b2d69cc
PE
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);
3ea5f0ec 136
5b2d69cc
PE
137 _exit (errno == ENOENT ? 127 : 126);
138 }
139
140 /* Parent. */
0fab8d7b
PE
141 close (child_fd[0]);
142 close (child_fd[1]);
5b2d69cc
PE
143 return pid;
144}
145
146
147/* Wait for the subprocess to exit. */
148
149void
150reap_subpipe (pid_t pid, char const *program)
151{
152#if HAVE_WAITPID || defined waitpid
153 int wstatus;
154 if (waitpid (pid, &wstatus, 0) < 0)
b4c2a797 155 error (EXIT_FAILURE, errno, "waitpid");
5b2d69cc
PE
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}
3d86656c
JMG
172
173void
c66dfadd
PE
174end_of_output_subpipe (pid_t pid ATTRIBUTE_UNUSED,
175 int fd[2] ATTRIBUTE_UNUSED)
3d86656c
JMG
176{
177}