1 | /* Utilities to execute a program in a subprocess (possibly linked by pipes
|
---|
2 | with other subprocesses), and wait for it.
|
---|
3 | Copyright (C) 1996-2000 Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | This file is part of the libiberty library.
|
---|
6 | Libiberty is free software; you can redistribute it and/or
|
---|
7 | modify it under the terms of the GNU Library General Public
|
---|
8 | License as published by the Free Software Foundation; either
|
---|
9 | version 2 of the License, or (at your option) any later version.
|
---|
10 |
|
---|
11 | Libiberty 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 GNU
|
---|
14 | Library General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU Library General Public
|
---|
17 | License along with libiberty; see the file COPYING.LIB. If not,
|
---|
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
---|
19 | Boston, MA 02111-1307, USA. */
|
---|
20 |
|
---|
21 | /* This file exports two functions: pexecute and pwait. */
|
---|
22 |
|
---|
23 | /* This file lives in at least two places: libiberty and gcc.
|
---|
24 | Don't change one without the other. */
|
---|
25 |
|
---|
26 | #ifdef HAVE_CONFIG_H
|
---|
27 | #include "config.h"
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <errno.h>
|
---|
32 | #ifdef NEED_DECLARATION_ERRNO
|
---|
33 | extern int errno;
|
---|
34 | #endif
|
---|
35 | #ifdef HAVE_STRING_H
|
---|
36 | #include <string.h>
|
---|
37 | #endif
|
---|
38 | #ifdef HAVE_UNISTD_H
|
---|
39 | #include <unistd.h>
|
---|
40 | #endif
|
---|
41 | #ifdef HAVE_STDLIB_H
|
---|
42 | #include <stdlib.h>
|
---|
43 | #endif
|
---|
44 | #ifdef HAVE_SYS_WAIT_H
|
---|
45 | #include <sys/wait.h>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #include "libiberty.h"
|
---|
49 | #include "safe-ctype.h"
|
---|
50 |
|
---|
51 | /* stdin file number. */
|
---|
52 | #define STDIN_FILE_NO 0
|
---|
53 |
|
---|
54 | /* stdout file number. */
|
---|
55 | #define STDOUT_FILE_NO 1
|
---|
56 |
|
---|
57 | /* value of `pipe': port index for reading. */
|
---|
58 | #define READ_PORT 0
|
---|
59 |
|
---|
60 | /* value of `pipe': port index for writing. */
|
---|
61 | #define WRITE_PORT 1
|
---|
62 |
|
---|
63 | static char *install_error_msg = "installation problem, cannot exec `%s'";
|
---|
64 |
|
---|
65 | /* pexecute: execute a program.
|
---|
66 |
|
---|
67 | @deftypefn Extension int pexecute (const char *@var{program}, char * const *@var{argv}, const char *@var{this_pname}, const char *@var{temp_base}, char **@var{errmsg_fmt}, char **@var{errmsg_arg}, int flags)
|
---|
68 |
|
---|
69 | Executes a program.
|
---|
70 |
|
---|
71 | @var{program} and @var{argv} are the arguments to
|
---|
72 | @code{execv}/@code{execvp}.
|
---|
73 |
|
---|
74 | @var{this_pname} is name of the calling program (i.e., @code{argv[0]}).
|
---|
75 |
|
---|
76 | @var{temp_base} is the path name, sans suffix, of a temporary file to
|
---|
77 | use if needed. This is currently only needed for MS-DOS ports that
|
---|
78 | don't use @code{go32} (do any still exist?). Ports that don't need it
|
---|
79 | can pass @code{NULL}.
|
---|
80 |
|
---|
81 | (@code{@var{flags} & PEXECUTE_SEARCH}) is non-zero if @env{PATH} should be searched
|
---|
82 | (??? It's not clear that GCC passes this flag correctly). (@code{@var{flags} &
|
---|
83 | PEXECUTE_FIRST}) is nonzero for the first process in chain.
|
---|
84 | (@code{@var{flags} & PEXECUTE_LAST}) is nonzero for the last process
|
---|
85 | in chain. The first/last flags could be simplified to only mark the
|
---|
86 | last of a chain of processes but that requires the caller to always
|
---|
87 | mark the last one (and not give up early if some error occurs).
|
---|
88 | It's more robust to require the caller to mark both ends of the chain.
|
---|
89 |
|
---|
90 | The result is the pid on systems like Unix where we
|
---|
91 | @code{fork}/@code{exec} and on systems like WIN32 and OS/2 where we
|
---|
92 | use @code{spawn}. It is up to the caller to wait for the child.
|
---|
93 |
|
---|
94 | The result is the @code{WEXITSTATUS} on systems like MS-DOS where we
|
---|
95 | @code{spawn} and wait for the child here.
|
---|
96 |
|
---|
97 | Upon failure, @var{errmsg_fmt} and @var{errmsg_arg} are set to the
|
---|
98 | text of the error message with an optional argument (if not needed,
|
---|
99 | @var{errmsg_arg} is set to @code{NULL}), and @minus{}1 is returned.
|
---|
100 | @code{errno} is available to the caller to use.
|
---|
101 |
|
---|
102 | @end deftypefn
|
---|
103 |
|
---|
104 | @deftypefn Extension int pwait (int @var{pid}, int *@var{status}, int @var{flags})
|
---|
105 |
|
---|
106 | Waits for a program started by @code{pexecute} to finish.
|
---|
107 |
|
---|
108 | @var{pid} is the process id of the task to wait for. @var{status} is
|
---|
109 | the `status' argument to wait. @var{flags} is currently unused (allows
|
---|
110 | future enhancement without breaking upward compatibility). Pass 0 for now.
|
---|
111 |
|
---|
112 | The result is the pid of the child reaped, or -1 for failure
|
---|
113 | (@code{errno} says why).
|
---|
114 |
|
---|
115 | On systems that don't support waiting for a particular child, @var{pid} is
|
---|
116 | ignored. On systems like MS-DOS that don't really multitask @code{pwait}
|
---|
117 | is just a mechanism to provide a consistent interface for the caller.
|
---|
118 |
|
---|
119 | @end deftypefn
|
---|
120 |
|
---|
121 | @undocumented pfinish
|
---|
122 |
|
---|
123 | pfinish: finish generation of script
|
---|
124 |
|
---|
125 | pfinish is necessary for systems like MPW where a script is generated that
|
---|
126 | runs the requested programs. */
|
---|
127 |
|
---|
128 | #ifdef __MSDOS__
|
---|
129 |
|
---|
130 | /* MSDOS doesn't multitask, but for the sake of a consistent interface
|
---|
131 | the code behaves like it does. pexecute runs the program, tucks the
|
---|
132 | exit code away, and returns a "pid". pwait must be called to fetch the
|
---|
133 | exit code. */
|
---|
134 |
|
---|
135 | #include <process.h>
|
---|
136 |
|
---|
137 | /* For communicating information from pexecute to pwait. */
|
---|
138 | static int last_pid = 0;
|
---|
139 | static int last_status = 0;
|
---|
140 | static int last_reaped = 0;
|
---|
141 |
|
---|
142 | int
|
---|
143 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
|
---|
144 | const char *program;
|
---|
145 | char * const *argv;
|
---|
146 | const char *this_pname;
|
---|
147 | const char *temp_base;
|
---|
148 | char **errmsg_fmt, **errmsg_arg;
|
---|
149 | int flags;
|
---|
150 | {
|
---|
151 | int rc;
|
---|
152 |
|
---|
153 | last_pid++;
|
---|
154 | if (last_pid < 0)
|
---|
155 | last_pid = 1;
|
---|
156 |
|
---|
157 | if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
|
---|
158 | abort ();
|
---|
159 |
|
---|
160 | #ifdef __DJGPP__
|
---|
161 | /* ??? What are the possible return values from spawnv? */
|
---|
162 | rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (P_WAIT, program, argv);
|
---|
163 | #else
|
---|
164 | char *scmd, *rf;
|
---|
165 | FILE *argfile;
|
---|
166 | int i, el = flags & PEXECUTE_SEARCH ? 4 : 0;
|
---|
167 |
|
---|
168 | if (temp_base == 0)
|
---|
169 | temp_base = choose_temp_base ();
|
---|
170 | scmd = (char *) xmalloc (strlen (program) + strlen (temp_base) + 6 + el);
|
---|
171 | rf = scmd + strlen(program) + 2 + el;
|
---|
172 | sprintf (scmd, "%s%s @%s.gp", program,
|
---|
173 | (flags & PEXECUTE_SEARCH ? ".exe" : ""), temp_base);
|
---|
174 | argfile = fopen (rf, "w");
|
---|
175 | if (argfile == 0)
|
---|
176 | {
|
---|
177 | int errno_save = errno;
|
---|
178 | free (scmd);
|
---|
179 | errno = errno_save;
|
---|
180 | *errmsg_fmt = "cannot open `%s.gp'";
|
---|
181 | *errmsg_arg = temp_base;
|
---|
182 | return -1;
|
---|
183 | }
|
---|
184 |
|
---|
185 | for (i=1; argv[i]; i++)
|
---|
186 | {
|
---|
187 | char *cp;
|
---|
188 | for (cp = argv[i]; *cp; cp++)
|
---|
189 | {
|
---|
190 | if (*cp == '"' || *cp == '\'' || *cp == '\\' || ISSPACE (*cp))
|
---|
191 | fputc ('\\', argfile);
|
---|
192 | fputc (*cp, argfile);
|
---|
193 | }
|
---|
194 | fputc ('\n', argfile);
|
---|
195 | }
|
---|
196 | fclose (argfile);
|
---|
197 |
|
---|
198 | rc = system (scmd);
|
---|
199 |
|
---|
200 | {
|
---|
201 | int errno_save = errno;
|
---|
202 | remove (rf);
|
---|
203 | free (scmd);
|
---|
204 | errno = errno_save;
|
---|
205 | }
|
---|
206 | #endif
|
---|
207 |
|
---|
208 | if (rc == -1)
|
---|
209 | {
|
---|
210 | *errmsg_fmt = install_error_msg;
|
---|
211 | *errmsg_arg = (char *)program;
|
---|
212 | return -1;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /* Tuck the status away for pwait, and return a "pid". */
|
---|
216 | last_status = rc << 8;
|
---|
217 | return last_pid;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* Use ECHILD if available, otherwise use EINVAL. */
|
---|
221 | #ifdef ECHILD
|
---|
222 | #define PWAIT_ERROR ECHILD
|
---|
223 | #else
|
---|
224 | #define PWAIT_ERROR EINVAL
|
---|
225 | #endif
|
---|
226 |
|
---|
227 | int
|
---|
228 | pwait (pid, status, flags)
|
---|
229 | int pid;
|
---|
230 | int *status;
|
---|
231 | int flags;
|
---|
232 | {
|
---|
233 | /* On MSDOS each pexecute must be followed by it's associated pwait. */
|
---|
234 | if (pid != last_pid
|
---|
235 | /* Called twice for the same child? */
|
---|
236 | || pid == last_reaped)
|
---|
237 | {
|
---|
238 | errno = PWAIT_ERROR;
|
---|
239 | return -1;
|
---|
240 | }
|
---|
241 | /* ??? Here's an opportunity to canonicalize the values in STATUS.
|
---|
242 | Needed? */
|
---|
243 | #ifdef __DJGPP__
|
---|
244 | *status = (last_status >> 8);
|
---|
245 | #else
|
---|
246 | *status = last_status;
|
---|
247 | #endif
|
---|
248 | last_reaped = last_pid;
|
---|
249 | return last_pid;
|
---|
250 | }
|
---|
251 |
|
---|
252 | #endif /* MSDOS */
|
---|
253 |
|
---|
254 | #if defined (_WIN32) && ! defined (_UWIN)
|
---|
255 |
|
---|
256 | #include <process.h>
|
---|
257 |
|
---|
258 | #ifdef __CYGWIN__
|
---|
259 |
|
---|
260 | #define fix_argv(argvec) (argvec)
|
---|
261 |
|
---|
262 | extern int _spawnv ();
|
---|
263 | extern int _spawnvp ();
|
---|
264 |
|
---|
265 | #else /* ! __CYGWIN__ */
|
---|
266 |
|
---|
267 | /* This is a kludge to get around the Microsoft C spawn functions' propensity
|
---|
268 | to remove the outermost set of double quotes from all arguments. */
|
---|
269 |
|
---|
270 | static const char * const *
|
---|
271 | fix_argv (argvec)
|
---|
272 | char **argvec;
|
---|
273 | {
|
---|
274 | int i;
|
---|
275 |
|
---|
276 | for (i = 1; argvec[i] != 0; i++)
|
---|
277 | {
|
---|
278 | int len, j;
|
---|
279 | char *temp, *newtemp;
|
---|
280 |
|
---|
281 | temp = argvec[i];
|
---|
282 | len = strlen (temp);
|
---|
283 | for (j = 0; j < len; j++)
|
---|
284 | {
|
---|
285 | if (temp[j] == '"')
|
---|
286 | {
|
---|
287 | newtemp = xmalloc (len + 2);
|
---|
288 | strncpy (newtemp, temp, j);
|
---|
289 | newtemp [j] = '\\';
|
---|
290 | strncpy (&newtemp [j+1], &temp [j], len-j);
|
---|
291 | newtemp [len+1] = 0;
|
---|
292 | temp = newtemp;
|
---|
293 | len++;
|
---|
294 | j++;
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | argvec[i] = temp;
|
---|
299 | }
|
---|
300 |
|
---|
301 | for (i = 0; argvec[i] != 0; i++)
|
---|
302 | {
|
---|
303 | if (strpbrk (argvec[i], " \t"))
|
---|
304 | {
|
---|
305 | int len, trailing_backslash;
|
---|
306 | char *temp;
|
---|
307 |
|
---|
308 | len = strlen (argvec[i]);
|
---|
309 | trailing_backslash = 0;
|
---|
310 |
|
---|
311 | /* There is an added complication when an arg with embedded white
|
---|
312 | space ends in a backslash (such as in the case of -iprefix arg
|
---|
313 | passed to cpp). The resulting quoted strings gets misinterpreted
|
---|
314 | by the command interpreter -- it thinks that the ending quote
|
---|
315 | is escaped by the trailing backslash and things get confused.
|
---|
316 | We handle this case by escaping the trailing backslash, provided
|
---|
317 | it was not escaped in the first place. */
|
---|
318 | if (len > 1
|
---|
319 | && argvec[i][len-1] == '\\'
|
---|
320 | && argvec[i][len-2] != '\\')
|
---|
321 | {
|
---|
322 | trailing_backslash = 1;
|
---|
323 | ++len; /* to escape the final backslash. */
|
---|
324 | }
|
---|
325 |
|
---|
326 | len += 2; /* and for the enclosing quotes. */
|
---|
327 |
|
---|
328 | temp = xmalloc (len + 1);
|
---|
329 | temp[0] = '"';
|
---|
330 | strcpy (temp + 1, argvec[i]);
|
---|
331 | if (trailing_backslash)
|
---|
332 | temp[len-2] = '\\';
|
---|
333 | temp[len-1] = '"';
|
---|
334 | temp[len] = '\0';
|
---|
335 |
|
---|
336 | argvec[i] = temp;
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | return (const char * const *) argvec;
|
---|
341 | }
|
---|
342 | #endif /* __CYGWIN__ */
|
---|
343 |
|
---|
344 | #include <io.h>
|
---|
345 | #include <fcntl.h>
|
---|
346 | #include <signal.h>
|
---|
347 |
|
---|
348 | /* mingw32 headers may not define the following. */
|
---|
349 |
|
---|
350 | #ifndef _P_WAIT
|
---|
351 | # define _P_WAIT 0
|
---|
352 | # define _P_NOWAIT 1
|
---|
353 | # define _P_OVERLAY 2
|
---|
354 | # define _P_NOWAITO 3
|
---|
355 | # define _P_DETACH 4
|
---|
356 |
|
---|
357 | # define WAIT_CHILD 0
|
---|
358 | # define WAIT_GRANDCHILD 1
|
---|
359 | #endif
|
---|
360 |
|
---|
361 | /* Win32 supports pipes */
|
---|
362 | int
|
---|
363 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
|
---|
364 | const char *program;
|
---|
365 | char * const *argv;
|
---|
366 | const char *this_pname;
|
---|
367 | const char *temp_base;
|
---|
368 | char **errmsg_fmt, **errmsg_arg;
|
---|
369 | int flags;
|
---|
370 | {
|
---|
371 | int pid;
|
---|
372 | int pdes[2], org_stdin, org_stdout;
|
---|
373 | int input_desc, output_desc;
|
---|
374 | int retries, sleep_interval;
|
---|
375 |
|
---|
376 | /* Pipe waiting from last process, to be used as input for the next one.
|
---|
377 | Value is STDIN_FILE_NO if no pipe is waiting
|
---|
378 | (i.e. the next command is the first of a group). */
|
---|
379 | static int last_pipe_input;
|
---|
380 |
|
---|
381 | /* If this is the first process, initialize. */
|
---|
382 | if (flags & PEXECUTE_FIRST)
|
---|
383 | last_pipe_input = STDIN_FILE_NO;
|
---|
384 |
|
---|
385 | input_desc = last_pipe_input;
|
---|
386 |
|
---|
387 | /* If this isn't the last process, make a pipe for its output,
|
---|
388 | and record it as waiting to be the input to the next process. */
|
---|
389 | if (! (flags & PEXECUTE_LAST))
|
---|
390 | {
|
---|
391 | if (_pipe (pdes, 256, O_BINARY) < 0)
|
---|
392 | {
|
---|
393 | *errmsg_fmt = "pipe";
|
---|
394 | *errmsg_arg = NULL;
|
---|
395 | return -1;
|
---|
396 | }
|
---|
397 | output_desc = pdes[WRITE_PORT];
|
---|
398 | last_pipe_input = pdes[READ_PORT];
|
---|
399 | }
|
---|
400 | else
|
---|
401 | {
|
---|
402 | /* Last process. */
|
---|
403 | output_desc = STDOUT_FILE_NO;
|
---|
404 | last_pipe_input = STDIN_FILE_NO;
|
---|
405 | }
|
---|
406 |
|
---|
407 | if (input_desc != STDIN_FILE_NO)
|
---|
408 | {
|
---|
409 | org_stdin = dup (STDIN_FILE_NO);
|
---|
410 | dup2 (input_desc, STDIN_FILE_NO);
|
---|
411 | close (input_desc);
|
---|
412 | }
|
---|
413 |
|
---|
414 | if (output_desc != STDOUT_FILE_NO)
|
---|
415 | {
|
---|
416 | org_stdout = dup (STDOUT_FILE_NO);
|
---|
417 | dup2 (output_desc, STDOUT_FILE_NO);
|
---|
418 | close (output_desc);
|
---|
419 | }
|
---|
420 |
|
---|
421 | pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
|
---|
422 | (_P_NOWAIT, program, fix_argv(argv));
|
---|
423 |
|
---|
424 | if (input_desc != STDIN_FILE_NO)
|
---|
425 | {
|
---|
426 | dup2 (org_stdin, STDIN_FILE_NO);
|
---|
427 | close (org_stdin);
|
---|
428 | }
|
---|
429 |
|
---|
430 | if (output_desc != STDOUT_FILE_NO)
|
---|
431 | {
|
---|
432 | dup2 (org_stdout, STDOUT_FILE_NO);
|
---|
433 | close (org_stdout);
|
---|
434 | }
|
---|
435 |
|
---|
436 | if (pid == -1)
|
---|
437 | {
|
---|
438 | *errmsg_fmt = install_error_msg;
|
---|
439 | *errmsg_arg = program;
|
---|
440 | return -1;
|
---|
441 | }
|
---|
442 |
|
---|
443 | return pid;
|
---|
444 | }
|
---|
445 |
|
---|
446 | /* MS CRTDLL doesn't return enough information in status to decide if the
|
---|
447 | child exited due to a signal or not, rather it simply returns an
|
---|
448 | integer with the exit code of the child; eg., if the child exited with
|
---|
449 | an abort() call and didn't have a handler for SIGABRT, it simply returns
|
---|
450 | with status = 3. We fix the status code to conform to the usual WIF*
|
---|
451 | macros. Note that WIFSIGNALED will never be true under CRTDLL. */
|
---|
452 |
|
---|
453 | int
|
---|
454 | pwait (pid, status, flags)
|
---|
455 | int pid;
|
---|
456 | int *status;
|
---|
457 | int flags;
|
---|
458 | {
|
---|
459 | #ifdef __CYGWIN__
|
---|
460 | return wait (status);
|
---|
461 | #else
|
---|
462 | int termstat;
|
---|
463 |
|
---|
464 | pid = _cwait (&termstat, pid, WAIT_CHILD);
|
---|
465 |
|
---|
466 | /* ??? Here's an opportunity to canonicalize the values in STATUS.
|
---|
467 | Needed? */
|
---|
468 |
|
---|
469 | /* cwait returns the child process exit code in termstat.
|
---|
470 | A value of 3 indicates that the child caught a signal, but not
|
---|
471 | which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
|
---|
472 | report SIGABRT. */
|
---|
473 | if (termstat == 3)
|
---|
474 | *status = SIGABRT;
|
---|
475 | else
|
---|
476 | *status = (((termstat) & 0xff) << 8);
|
---|
477 |
|
---|
478 | return pid;
|
---|
479 | #endif /* __CYGWIN__ */
|
---|
480 | }
|
---|
481 |
|
---|
482 | #endif /* _WIN32 && ! _UWIN */
|
---|
483 |
|
---|
484 | #ifdef OS2
|
---|
485 |
|
---|
486 | /* This is for non-EMX OS/2 port (is it floating around anyway?) */
|
---|
487 | /* ??? Does OS2 have process.h? */
|
---|
488 | extern int spawnv ();
|
---|
489 | extern int spawnvp ();
|
---|
490 |
|
---|
491 | int
|
---|
492 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
|
---|
493 | const char *program;
|
---|
494 | char * const *argv;
|
---|
495 | const char *this_pname;
|
---|
496 | const char *temp_base;
|
---|
497 | char **errmsg_fmt, **errmsg_arg;
|
---|
498 | int flags;
|
---|
499 | {
|
---|
500 | int pid;
|
---|
501 |
|
---|
502 | if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
|
---|
503 | abort ();
|
---|
504 | /* ??? Presumably 1 == _P_NOWAIT. */
|
---|
505 | pid = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
|
---|
506 | if (pid == -1)
|
---|
507 | {
|
---|
508 | *errmsg_fmt = install_error_msg;
|
---|
509 | *errmsg_arg = program;
|
---|
510 | return -1;
|
---|
511 | }
|
---|
512 | return pid;
|
---|
513 | }
|
---|
514 |
|
---|
515 | int
|
---|
516 | pwait (pid, status, flags)
|
---|
517 | int pid;
|
---|
518 | int *status;
|
---|
519 | int flags;
|
---|
520 | {
|
---|
521 | /* ??? Here's an opportunity to canonicalize the values in STATUS.
|
---|
522 | Needed? */
|
---|
523 | int pid = wait (status);
|
---|
524 | return pid;
|
---|
525 | }
|
---|
526 |
|
---|
527 | #endif /* OS2 */
|
---|
528 |
|
---|
529 | #ifdef MPW
|
---|
530 |
|
---|
531 | /* MPW pexecute doesn't actually run anything; instead, it writes out
|
---|
532 | script commands that, when run, will do the actual executing.
|
---|
533 |
|
---|
534 | For example, in GCC's case, GCC will write out several script commands:
|
---|
535 |
|
---|
536 | cpp ...
|
---|
537 | cc1 ...
|
---|
538 | as ...
|
---|
539 | ld ...
|
---|
540 |
|
---|
541 | and then exit. None of the above programs will have run yet. The task
|
---|
542 | that called GCC will then execute the script and cause cpp,etc. to run.
|
---|
543 | The caller must invoke pfinish before calling exit. This adds
|
---|
544 | the finishing touches to the generated script. */
|
---|
545 |
|
---|
546 | static int first_time = 1;
|
---|
547 |
|
---|
548 | int
|
---|
549 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
|
---|
550 | const char *program;
|
---|
551 | char * const *argv;
|
---|
552 | const char *this_pname;
|
---|
553 | const char *temp_base;
|
---|
554 | char **errmsg_fmt, **errmsg_arg;
|
---|
555 | int flags;
|
---|
556 | {
|
---|
557 | char tmpprogram[255];
|
---|
558 | char *cp, *tmpname;
|
---|
559 | int i;
|
---|
560 |
|
---|
561 | mpwify_filename (program, tmpprogram);
|
---|
562 | if (first_time)
|
---|
563 | {
|
---|
564 | printf ("Set Failed 0\n");
|
---|
565 | first_time = 0;
|
---|
566 | }
|
---|
567 |
|
---|
568 | fputs ("If {Failed} == 0\n", stdout);
|
---|
569 | /* If being verbose, output a copy of the command. It should be
|
---|
570 | accurate enough and escaped enough to be "clickable". */
|
---|
571 | if (flags & PEXECUTE_VERBOSE)
|
---|
572 | {
|
---|
573 | fputs ("\tEcho ", stdout);
|
---|
574 | fputc ('\'', stdout);
|
---|
575 | fputs (tmpprogram, stdout);
|
---|
576 | fputc ('\'', stdout);
|
---|
577 | fputc (' ', stdout);
|
---|
578 | for (i=1; argv[i]; i++)
|
---|
579 | {
|
---|
580 | fputc ('\'', stdout);
|
---|
581 | /* See if we have an argument that needs fixing. */
|
---|
582 | if (strchr(argv[i], '/'))
|
---|
583 | {
|
---|
584 | tmpname = (char *) xmalloc (256);
|
---|
585 | mpwify_filename (argv[i], tmpname);
|
---|
586 | argv[i] = tmpname;
|
---|
587 | }
|
---|
588 | for (cp = argv[i]; *cp; cp++)
|
---|
589 | {
|
---|
590 | /* Write an Option-d escape char in front of special chars. */
|
---|
591 | if (strchr("'+", *cp))
|
---|
592 | fputc ('\266', stdout);
|
---|
593 | fputc (*cp, stdout);
|
---|
594 | }
|
---|
595 | fputc ('\'', stdout);
|
---|
596 | fputc (' ', stdout);
|
---|
597 | }
|
---|
598 | fputs ("\n", stdout);
|
---|
599 | }
|
---|
600 | fputs ("\t", stdout);
|
---|
601 | fputs (tmpprogram, stdout);
|
---|
602 | fputc (' ', stdout);
|
---|
603 |
|
---|
604 | for (i=1; argv[i]; i++)
|
---|
605 | {
|
---|
606 | /* See if we have an argument that needs fixing. */
|
---|
607 | if (strchr(argv[i], '/'))
|
---|
608 | {
|
---|
609 | tmpname = (char *) xmalloc (256);
|
---|
610 | mpwify_filename (argv[i], tmpname);
|
---|
611 | argv[i] = tmpname;
|
---|
612 | }
|
---|
613 | if (strchr (argv[i], ' '))
|
---|
614 | fputc ('\'', stdout);
|
---|
615 | for (cp = argv[i]; *cp; cp++)
|
---|
616 | {
|
---|
617 | /* Write an Option-d escape char in front of special chars. */
|
---|
618 | if (strchr("'+", *cp))
|
---|
619 | fputc ('\266', stdout);
|
---|
620 | fputc (*cp, stdout);
|
---|
621 | }
|
---|
622 | if (strchr (argv[i], ' '))
|
---|
623 | fputc ('\'', stdout);
|
---|
624 | fputc (' ', stdout);
|
---|
625 | }
|
---|
626 |
|
---|
627 | fputs ("\n", stdout);
|
---|
628 |
|
---|
629 | /* Output commands that arrange to clean up and exit if a failure occurs.
|
---|
630 | We have to be careful to collect the status from the program that was
|
---|
631 | run, rather than some other script command. Also, we don't exit
|
---|
632 | immediately, since necessary cleanups are at the end of the script. */
|
---|
633 | fputs ("\tSet TmpStatus {Status}\n", stdout);
|
---|
634 | fputs ("\tIf {TmpStatus} != 0\n", stdout);
|
---|
635 | fputs ("\t\tSet Failed {TmpStatus}\n", stdout);
|
---|
636 | fputs ("\tEnd\n", stdout);
|
---|
637 | fputs ("End\n", stdout);
|
---|
638 |
|
---|
639 | /* We're just composing a script, can't fail here. */
|
---|
640 | return 0;
|
---|
641 | }
|
---|
642 |
|
---|
643 | int
|
---|
644 | pwait (pid, status, flags)
|
---|
645 | int pid;
|
---|
646 | int *status;
|
---|
647 | int flags;
|
---|
648 | {
|
---|
649 | *status = 0;
|
---|
650 | return 0;
|
---|
651 | }
|
---|
652 |
|
---|
653 | /* Write out commands that will exit with the correct error code
|
---|
654 | if something in the script failed. */
|
---|
655 |
|
---|
656 | void
|
---|
657 | pfinish ()
|
---|
658 | {
|
---|
659 | printf ("\tExit \"{Failed}\"\n");
|
---|
660 | }
|
---|
661 |
|
---|
662 | #endif /* MPW */
|
---|
663 |
|
---|
664 | /* include for Unix-like environments but not for Dos-like environments */
|
---|
665 | #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
|
---|
666 | && ! (defined (_WIN32) && ! defined (_UWIN)) && ! defined (__EMX__)
|
---|
667 |
|
---|
668 | extern int execv ();
|
---|
669 | extern int execvp ();
|
---|
670 |
|
---|
671 | int
|
---|
672 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
|
---|
673 | const char *program;
|
---|
674 | char * const *argv;
|
---|
675 | const char *this_pname;
|
---|
676 | const char *temp_base ATTRIBUTE_UNUSED;
|
---|
677 | char **errmsg_fmt, **errmsg_arg;
|
---|
678 | int flags;
|
---|
679 | {
|
---|
680 | int (*func)() = (flags & PEXECUTE_SEARCH ? execvp : execv);
|
---|
681 | int pid;
|
---|
682 | int pdes[2];
|
---|
683 | int input_desc, output_desc;
|
---|
684 | int retries, sleep_interval;
|
---|
685 | /* Pipe waiting from last process, to be used as input for the next one.
|
---|
686 | Value is STDIN_FILE_NO if no pipe is waiting
|
---|
687 | (i.e. the next command is the first of a group). */
|
---|
688 | static int last_pipe_input;
|
---|
689 |
|
---|
690 | /* If this is the first process, initialize. */
|
---|
691 | if (flags & PEXECUTE_FIRST)
|
---|
692 | last_pipe_input = STDIN_FILE_NO;
|
---|
693 |
|
---|
694 | input_desc = last_pipe_input;
|
---|
695 |
|
---|
696 | /* If this isn't the last process, make a pipe for its output,
|
---|
697 | and record it as waiting to be the input to the next process. */
|
---|
698 | if (! (flags & PEXECUTE_LAST))
|
---|
699 | {
|
---|
700 | if (pipe (pdes) < 0)
|
---|
701 | {
|
---|
702 | *errmsg_fmt = "pipe";
|
---|
703 | *errmsg_arg = NULL;
|
---|
704 | return -1;
|
---|
705 | }
|
---|
706 | output_desc = pdes[WRITE_PORT];
|
---|
707 | last_pipe_input = pdes[READ_PORT];
|
---|
708 | }
|
---|
709 | else
|
---|
710 | {
|
---|
711 | /* Last process. */
|
---|
712 | output_desc = STDOUT_FILE_NO;
|
---|
713 | last_pipe_input = STDIN_FILE_NO;
|
---|
714 | }
|
---|
715 |
|
---|
716 | /* Fork a subprocess; wait and retry if it fails. */
|
---|
717 | sleep_interval = 1;
|
---|
718 | pid = -1;
|
---|
719 | for (retries = 0; retries < 4; retries++)
|
---|
720 | {
|
---|
721 | pid = fork ();
|
---|
722 | if (pid >= 0)
|
---|
723 | break;
|
---|
724 | sleep (sleep_interval);
|
---|
725 | sleep_interval *= 2;
|
---|
726 | }
|
---|
727 |
|
---|
728 | switch (pid)
|
---|
729 | {
|
---|
730 | case -1:
|
---|
731 | *errmsg_fmt = "fork";
|
---|
732 | *errmsg_arg = NULL;
|
---|
733 | return -1;
|
---|
734 |
|
---|
735 | case 0: /* child */
|
---|
736 | /* Move the input and output pipes into place, if necessary. */
|
---|
737 | if (input_desc != STDIN_FILE_NO)
|
---|
738 | {
|
---|
739 | close (STDIN_FILE_NO);
|
---|
740 | dup (input_desc);
|
---|
741 | close (input_desc);
|
---|
742 | }
|
---|
743 | if (output_desc != STDOUT_FILE_NO)
|
---|
744 | {
|
---|
745 | close (STDOUT_FILE_NO);
|
---|
746 | dup (output_desc);
|
---|
747 | close (output_desc);
|
---|
748 | }
|
---|
749 |
|
---|
750 | /* Close the parent's descs that aren't wanted here. */
|
---|
751 | if (last_pipe_input != STDIN_FILE_NO)
|
---|
752 | close (last_pipe_input);
|
---|
753 |
|
---|
754 | /* Exec the program. */
|
---|
755 | (*func) (program, argv);
|
---|
756 |
|
---|
757 | fprintf (stderr, "%s: ", this_pname);
|
---|
758 | fprintf (stderr, install_error_msg, program);
|
---|
759 | fprintf (stderr, ": %s\n", xstrerror (errno));
|
---|
760 | exit (-1);
|
---|
761 | /* NOTREACHED */
|
---|
762 | return 0;
|
---|
763 |
|
---|
764 | default:
|
---|
765 | /* In the parent, after forking.
|
---|
766 | Close the descriptors that we made for this child. */
|
---|
767 | if (input_desc != STDIN_FILE_NO)
|
---|
768 | close (input_desc);
|
---|
769 | if (output_desc != STDOUT_FILE_NO)
|
---|
770 | close (output_desc);
|
---|
771 |
|
---|
772 | /* Return child's process number. */
|
---|
773 | return pid;
|
---|
774 | }
|
---|
775 | }
|
---|
776 |
|
---|
777 | int
|
---|
778 | pwait (pid, status, flags)
|
---|
779 | int pid;
|
---|
780 | int *status;
|
---|
781 | int flags ATTRIBUTE_UNUSED;
|
---|
782 | {
|
---|
783 | /* ??? Here's an opportunity to canonicalize the values in STATUS.
|
---|
784 | Needed? */
|
---|
785 | #ifdef VMS
|
---|
786 | pid = waitpid (-1, status, 0);
|
---|
787 | #else
|
---|
788 | pid = wait (status);
|
---|
789 | #endif
|
---|
790 | return pid;
|
---|
791 | }
|
---|
792 |
|
---|
793 | #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! (_WIN32 && ! _UWIN) && ! __EMX__ */
|
---|
794 |
|
---|
795 | #ifdef __EMX__
|
---|
796 |
|
---|
797 | #undef abort
|
---|
798 | #include <process.h>
|
---|
799 | #include <stdlib.h>
|
---|
800 | #define INCL_DOSPROCESS
|
---|
801 | #include <os2.h>
|
---|
802 |
|
---|
803 | int
|
---|
804 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
|
---|
805 | const char *program;
|
---|
806 | char * const *argv;
|
---|
807 | const char *this_pname;
|
---|
808 | const char *temp_base;
|
---|
809 | char **errmsg_fmt, **errmsg_arg;
|
---|
810 | int flags;
|
---|
811 | {
|
---|
812 | static int last_pipe_input = STDIN_FILE_NO;
|
---|
813 | int pid;
|
---|
814 | int pdes[2], org_stdin, org_stdout;
|
---|
815 | int input_desc = last_pipe_input;
|
---|
816 | int output_desc = STDOUT_FILE_NO;
|
---|
817 | #ifdef __OS2__
|
---|
818 | #define pipes_supported 1
|
---|
819 | #else
|
---|
820 | int pipes_supported = _osmode != DOS_MODE || (_emx_env & 0x1000);
|
---|
821 | #endif
|
---|
822 |
|
---|
823 | /* Since the function can reside in a DLL, we should take care
|
---|
824 | environ to not be NULL. Unfortunately this involves os2.h :-( - A.Z. */
|
---|
825 | if (!environ)
|
---|
826 | {
|
---|
827 | PTIB tib;
|
---|
828 | PPIB pib;
|
---|
829 | if (!DosGetInfoBlocks (&tib, &pib))
|
---|
830 | {
|
---|
831 | int count = 1;
|
---|
832 | char *env = pib->pib_pchenv;
|
---|
833 | char **cur;
|
---|
834 | while (*env++)
|
---|
835 | if (!*env)
|
---|
836 | count++, env++;
|
---|
837 | cur = environ = malloc (sizeof (char *) * count);
|
---|
838 | env = pib->pib_pchenv;
|
---|
839 | *cur++ = env;
|
---|
840 | while (*env++)
|
---|
841 | if (!*env)
|
---|
842 | env++, *cur++ = env;
|
---|
843 | cur [-1] = NULL;
|
---|
844 | }
|
---|
845 | }
|
---|
846 |
|
---|
847 | if (!pipes_supported && (flags & PEXECUTE_ONE) != PEXECUTE_ONE)
|
---|
848 | {
|
---|
849 | static char *errtpl = "%s: exec %s (pipes not supported)";
|
---|
850 | *errmsg_fmt = (char *) xmalloc (strlen(errtpl) + \
|
---|
851 | strlen(this_pname) + strlen(program));
|
---|
852 | sprintf (*errmsg_fmt, errtpl, this_pname, program);
|
---|
853 | *errmsg_arg = NULL;
|
---|
854 | return -1;
|
---|
855 | }
|
---|
856 | /* If this isn't the last process, make a pipe for its output,
|
---|
857 | and record it as waiting to be the input to the next process. */
|
---|
858 | if (!(flags & PEXECUTE_LAST))
|
---|
859 | {
|
---|
860 | if (pipe (pdes) < 0)
|
---|
861 | {
|
---|
862 | static char *errtpl = "%s: pipe to/from %s";
|
---|
863 | *errmsg_fmt = (char *) xmalloc (strlen(errtpl) + \
|
---|
864 | strlen(this_pname) + strlen(program));
|
---|
865 | sprintf (*errmsg_fmt, errtpl, this_pname, program);
|
---|
866 | *errmsg_arg = NULL;
|
---|
867 | return -1;
|
---|
868 | }
|
---|
869 | output_desc = pdes[WRITE_PORT];
|
---|
870 | last_pipe_input = pdes[READ_PORT];
|
---|
871 | }
|
---|
872 | else
|
---|
873 | last_pipe_input = STDIN_FILE_NO;
|
---|
874 | if (pipes_supported && input_desc != STDIN_FILE_NO)
|
---|
875 | {
|
---|
876 | org_stdin = dup (STDIN_FILE_NO);
|
---|
877 | dup2 (input_desc, STDIN_FILE_NO);
|
---|
878 | close (input_desc);
|
---|
879 | }
|
---|
880 | if (pipes_supported && output_desc != STDOUT_FILE_NO)
|
---|
881 | {
|
---|
882 | org_stdout = dup (STDOUT_FILE_NO);
|
---|
883 | dup2 (output_desc, STDOUT_FILE_NO);
|
---|
884 | close (output_desc);
|
---|
885 | }
|
---|
886 | pid = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (P_NOWAIT, program, argv);
|
---|
887 | if (pipes_supported && input_desc != STDIN_FILE_NO)
|
---|
888 | {
|
---|
889 | dup2 (org_stdin, STDIN_FILE_NO);
|
---|
890 | close (org_stdin);
|
---|
891 | }
|
---|
892 | if (pipes_supported && output_desc != STDOUT_FILE_NO)
|
---|
893 | {
|
---|
894 | dup2 (org_stdout, STDOUT_FILE_NO);
|
---|
895 | close (org_stdout);
|
---|
896 | }
|
---|
897 | if (pid == -1)
|
---|
898 | {
|
---|
899 | static char *errtpl = "%s: error executing %s";
|
---|
900 | *errmsg_fmt = (char *) xmalloc (strlen(errtpl) + \
|
---|
901 | strlen(this_pname) + strlen(program));
|
---|
902 | sprintf (*errmsg_fmt, errtpl, this_pname, program);
|
---|
903 | *errmsg_arg = NULL;
|
---|
904 | }
|
---|
905 | return pid;
|
---|
906 | }
|
---|
907 |
|
---|
908 | int
|
---|
909 | pwait (pid, status, flags)
|
---|
910 | int pid;
|
---|
911 | int *status;
|
---|
912 | int flags;
|
---|
913 | {
|
---|
914 | int rc;
|
---|
915 | do
|
---|
916 | rc = waitpid (pid, status, flags);
|
---|
917 | while (rc < 0 && errno == EINTR);
|
---|
918 | return rc;
|
---|
919 | }
|
---|
920 |
|
---|
921 | #endif /* __EMX__ */
|
---|