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