]>
git.saurik.com Git - bison.git/blob - djgpp/subpipe.c
1 /* Subprocesses with pipes.
3 Copyright (C) 2005, 2006, 2007, 2008-2009 Free Software Foundation,
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by Juan Manuel Guerrero <juan.guerrero@gmx.de>. */
39 # define STDIN_FILENO 0
42 # define STDOUT_FILENO 1
49 #define _(Msgid) gettext (Msgid)
52 /* Initialize this module. */
56 static int old_stdout
;
57 static char **arguments
;
58 static char tmp_file_name
[2][L_tmpnam
];
60 #define remove_tmp_file(fd, name) \
63 if (unlink ((name))) \
64 error (EXIT_FAILURE, 0, _("removing of `%s' failed"), (name)); \
74 tmpdir
= getenv("TMPDIR");
76 tmpdir
= getenv("TMP");
78 tmpdir
= getenv("TEMP");
79 if (access(tmpdir
, D_OK
))
82 strcpy(tmp_file_name
[0], tmpdir
);
83 strcat(tmp_file_name
[0], "/bnXXXXXX");
84 fd
= mkstemp(tmp_file_name
[0]);
86 error(EXIT_FAILURE
, 0, _("creation of a temporary file failed"));
89 strcpy(tmp_file_name
[1], tmpdir
);
90 strcat(tmp_file_name
[1], "/bnXXXXXX");
91 fd
= mkstemp(tmp_file_name
[1]);
93 error(EXIT_FAILURE
, 0, _("creation of a temporary file failed"));
98 /* Create a subprocess that is run as a filter. ARGV is the
99 NULL-terminated argument vector for the subprocess. Store read and
100 write file descriptors for communication with the subprocess into
101 FD[0] and FD[1]: input meant for the process can be written into
102 FD[0], and output from the process can be read from FD[1]. Return
105 Because DOS has neither fork nor pipe functionality to run the subprocess
106 as a filter, the filter is reproduced using temporary files. First bison's
107 stdout is redirected to a temporary file. After bison has produced all of
108 is output, this file is closed and connected to m4's stdin. All m4's output
109 is redirected from m4's stdout to a second temporary file and reopened as
113 create_subpipe(char const *const *argv
, int fd
[2])
116 int from_in_fd
; /* pipe from bison to m4. */
123 * Save original stdin and stdout
124 * for later restauration.
126 old_stdin
= dup(STDIN_FILENO
);
128 error(EXIT_FAILURE
, 0, _("saving stdin failed"));
130 old_stdout
= dup(STDOUT_FILENO
);
132 error(EXIT_FAILURE
, 0, _("saving stdout failed"));
135 * Save argv for later use.
137 for (argc
= 0; argv
[argc
]; argc
++)
140 arguments
= xmalloc(argc
* sizeof(arguments
[0]));
141 for (argc
= 0; argv
[argc
]; argc
++)
143 arguments
[argc
] = xmalloc((strlen(argv
[argc
]) + 1) * sizeof(arguments
[0][0]));
144 strcpy(arguments
[argc
], argv
[argc
]);
146 arguments
[argc
] = NULL
;
149 * All bison's output will be gathered in this temporary file
150 * and will be redirected to m4's stdin.
152 from_in_fd
= open(tmp_file_name
[0], O_WRONLY
| O_CREAT
| O_TRUNC
, S_IWUSR
);
154 error(EXIT_FAILURE
, 0, _("opening of tmpfile failed"));
155 if (dup2(from_in_fd
, STDOUT_FILENO
) < 0)
157 remove_tmp_file(from_in_fd
, tmp_file_name
[0]);
158 error(EXIT_FAILURE
, 0, _("redirecting bison's stdout to the temporary file failed"));
163 fd
[0] = STDOUT_FILENO
;
168 /* A signal handler that just records that a signal has happened. */
169 static int child_interrupted
;
172 signal_catcher(int signo
)
179 end_of_output_subpipe(pid_t pid
, int fd
[2])
182 int from_out_fd
= open(tmp_file_name
[0], O_RDONLY
, S_IRUSR
); /* pipe from bison to m4. */
183 int to_in_fd
= open(tmp_file_name
[1], O_WRONLY
| O_CREAT
| O_TRUNC
, S_IWUSR
); /* pipe from m4 to bison. */
185 void (*previous_handler
)(int);
188 program
= strrchr(arguments
[0], '/');
192 program
= arguments
[0];
195 * Redirect bison's output to m4's stdin.
198 error(EXIT_FAILURE
, 0, _("opening of tmpfile failed"));
199 if (dup2(from_out_fd
, STDIN_FILENO
) < 0)
201 remove_tmp_file(from_out_fd
, tmp_file_name
[0]);
202 error(EXIT_FAILURE
, 0, _("redirecting m4's stdin from the temporary file failed"));
207 * All m4's output will be gathered in this temporary file
208 * and will be redirected to bison's stdin.
212 remove_tmp_file(STDIN_FILENO
, tmp_file_name
[0]);
213 error(EXIT_FAILURE
, 0, _("opening of a temporary file failed"));
215 if (dup2(to_in_fd
, STDOUT_FILENO
) < 0)
217 remove_tmp_file(STDIN_FILENO
, tmp_file_name
[0]);
218 remove_tmp_file(to_in_fd
, tmp_file_name
[1]);
219 error(EXIT_FAILURE
, 0, _("redirecting m4's stdout to a temporary file failed"));
226 child_interrupted
= 0;
228 previous_handler
= signal(SIGINT
, signal_catcher
);
229 status
= spawnvp(P_WAIT
, program
, arguments
);
230 signal(SIGINT
, previous_handler
);
231 if (child_interrupted
)
233 remove_tmp_file(STDIN_FILENO
, tmp_file_name
[0]);
234 remove_tmp_file(STDOUT_FILENO
, tmp_file_name
[1]);
235 error(EXIT_FAILURE
, 0, _("subsidiary program `%s' interrupted"), program
);
239 remove_tmp_file(STDIN_FILENO
, tmp_file_name
[0]);
240 remove_tmp_file(STDOUT_FILENO
, tmp_file_name
[1]);
241 error(EXIT_FAILURE
, 0, _(errno
== ENOENT
242 ? "subsidiary program `%s' not found"
244 ? "subsidiary program `%s' failed"
245 : "subsidiary program `%s' failed (status=%i, errno=%i)"), program
, status
, errno
);
250 * Redirect m4's output to bison's stdin.
252 if (dup2(old_stdout
, STDOUT_FILENO
) < 0)
253 error(EXIT_FAILURE
, 0, "restore of bison's stdout failed");
255 to_in_fd
= open(tmp_file_name
[1], O_RDONLY
, S_IRUSR
); /* pipe from m4 to bison. */
258 remove_tmp_file(STDIN_FILENO
, tmp_file_name
[0]);
259 error(EXIT_FAILURE
, 0, _("opening of tmpfile failed"));
261 if (dup2(to_in_fd
, STDIN_FILENO
) < 0)
263 remove_tmp_file(STDIN_FILENO
, tmp_file_name
[0]);
264 remove_tmp_file(to_in_fd
, tmp_file_name
[1]);
265 error(EXIT_FAILURE
, -1, "dup2");
266 error(EXIT_FAILURE
, 0, _("redirecting bison's stdin from the temporary file failed"));
271 fd
[1] = STDIN_FILENO
;
275 /* Free resources, unlink temporary files and restore stdin and stdout. */
278 reap_subpipe(pid_t pid
, char const *program
)
282 for (argc
= 0; arguments
[argc
]; argc
++)
283 free(arguments
[argc
]);
286 if (unlink(tmp_file_name
[0]))
287 error(EXIT_FAILURE
, 0, _("removing of `%s' failed"), tmp_file_name
[0]);
288 if (unlink(tmp_file_name
[1]))
289 error(EXIT_FAILURE
, 0, _("removing of `%s' failed"), tmp_file_name
[1]);
291 if (dup2(old_stdin
, STDIN_FILENO
) < 0)
292 error(EXIT_FAILURE
, 0, "restore of bison's stdin failed");