]>
Commit | Line | Data |
---|---|---|
6599da04 JM |
1 | /* Utilities to execute a program in a subprocess (possibly linked by pipes |
2 | with other subprocesses), and wait for it. | |
3f3726a3 | 3 | Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. |
6599da04 JM |
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 | ||
c5f7c445 | 26 | #ifdef HAVE_CONFIG_H |
19ddc834 | 27 | #include "config.h" |
c5f7c445 KG |
28 | #endif |
29 | #ifdef IN_GCC | |
3f3726a3 JL |
30 | #include "system.h" |
31 | #else | |
6599da04 JM |
32 | #include <stdio.h> |
33 | #include <errno.h> | |
c5f7c445 KG |
34 | #ifdef HAVE_UNISTD_H |
35 | #include <unistd.h> | |
36 | #endif | |
3f3726a3 JL |
37 | #define ISSPACE (x) isspace(x) |
38 | #endif | |
6599da04 | 39 | |
c5f7c445 KG |
40 | #ifdef vfork /* Autoconf may define this to fork for us. */ |
41 | # define VFORK_STRING "fork" | |
42 | #else | |
43 | # define VFORK_STRING "vfork" | |
44 | #endif | |
45 | #ifdef HAVE_VFORK_H | |
46 | #include <vfork.h> | |
47 | #endif | |
48 | #ifdef VMS | |
49 | #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \ | |
50 | lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1) | |
51 | #endif /* VMS */ | |
52 | ||
6599da04 | 53 | #ifdef IN_GCC |
6599da04 JM |
54 | #include "gansidecl.h" |
55 | /* ??? Need to find a suitable header file. */ | |
56 | #define PEXECUTE_FIRST 1 | |
57 | #define PEXECUTE_LAST 2 | |
58 | #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST) | |
59 | #define PEXECUTE_SEARCH 4 | |
60 | #define PEXECUTE_VERBOSE 8 | |
61 | #else | |
62 | #include "libiberty.h" | |
63 | #endif | |
64 | ||
65 | /* stdin file number. */ | |
66 | #define STDIN_FILE_NO 0 | |
67 | ||
68 | /* stdout file number. */ | |
69 | #define STDOUT_FILE_NO 1 | |
70 | ||
71 | /* value of `pipe': port index for reading. */ | |
72 | #define READ_PORT 0 | |
73 | ||
74 | /* value of `pipe': port index for writing. */ | |
75 | #define WRITE_PORT 1 | |
76 | ||
77 | static char *install_error_msg = "installation problem, cannot exec `%s'"; | |
78 | ||
79 | /* pexecute: execute a program. | |
80 | ||
81 | PROGRAM and ARGV are the arguments to execv/execvp. | |
82 | ||
83 | THIS_PNAME is name of the calling program (i.e. argv[0]). | |
84 | ||
85 | TEMP_BASE is the path name, sans suffix, of a temporary file to use | |
86 | if needed. This is currently only needed for MSDOS ports that don't use | |
87 | GO32 (do any still exist?). Ports that don't need it can pass NULL. | |
88 | ||
89 | (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched | |
90 | (??? It's not clear that GCC passes this flag correctly). | |
91 | (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain. | |
92 | (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain. | |
93 | FIRST_LAST could be simplified to only mark the last of a chain of processes | |
94 | but that requires the caller to always mark the last one (and not give up | |
95 | early if some error occurs). It's more robust to require the caller to | |
96 | mark both ends of the chain. | |
97 | ||
98 | The result is the pid on systems like Unix where we fork/exec and on systems | |
99 | like WIN32 and OS2 where we use spawn. It is up to the caller to wait for | |
100 | the child. | |
101 | ||
102 | The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait | |
103 | for the child here. | |
104 | ||
105 | Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error | |
106 | message with an optional argument (if not needed, ERRMSG_ARG is set to | |
107 | NULL), and -1 is returned. `errno' is available to the caller to use. | |
108 | ||
109 | pwait: cover function for wait. | |
110 | ||
111 | PID is the process id of the task to wait for. | |
112 | STATUS is the `status' argument to wait. | |
113 | FLAGS is currently unused (allows future enhancement without breaking | |
114 | upward compatibility). Pass 0 for now. | |
115 | ||
116 | The result is the pid of the child reaped, | |
117 | or -1 for failure (errno says why). | |
118 | ||
119 | On systems that don't support waiting for a particular child, PID is | |
120 | ignored. On systems like MSDOS that don't really multitask pwait | |
121 | is just a mechanism to provide a consistent interface for the caller. | |
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 | ||
129 | #ifdef __MSDOS__ | |
130 | ||
131 | /* MSDOS doesn't multitask, but for the sake of a consistent interface | |
132 | the code behaves like it does. pexecute runs the program, tucks the | |
133 | exit code away, and returns a "pid". pwait must be called to fetch the | |
134 | exit code. */ | |
135 | ||
136 | #include <process.h> | |
137 | ||
138 | /* For communicating information from pexecute to pwait. */ | |
139 | static int last_pid = 0; | |
140 | static int last_status = 0; | |
141 | static int last_reaped = 0; | |
142 | ||
143 | int | |
144 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags) | |
145 | const char *program; | |
146 | char * const *argv; | |
147 | const char *this_pname; | |
148 | const char *temp_base; | |
149 | char **errmsg_fmt, **errmsg_arg; | |
150 | int flags; | |
151 | { | |
152 | int rc; | |
153 | ||
154 | last_pid++; | |
155 | if (last_pid < 0) | |
156 | last_pid = 1; | |
157 | ||
158 | if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE) | |
159 | abort (); | |
160 | ||
161 | #ifdef __GO32__ | |
162 | /* ??? What are the possible return values from spawnv? */ | |
163 | rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv); | |
164 | #else | |
165 | char *scmd, *rf; | |
166 | FILE *argfile; | |
167 | int i, el = flags & PEXECUTE_SEARCH ? 4 : 0; | |
168 | ||
169 | scmd = (char *) xmalloc (strlen (program) + strlen (temp_base) + 6 + el); | |
170 | rf = scmd + strlen(program) + 2 + el; | |
171 | sprintf (scmd, "%s%s @%s.gp", program, | |
172 | (flags & PEXECUTE_SEARCH ? ".exe" : ""), temp_base); | |
173 | argfile = fopen (rf, "w"); | |
174 | if (argfile == 0) | |
175 | { | |
176 | int errno_save = errno; | |
177 | free (scmd); | |
178 | errno = errno_save; | |
179 | *errmsg_fmt = "cannot open `%s.gp'"; | |
180 | *errmsg_arg = temp_base; | |
181 | return -1; | |
182 | } | |
183 | ||
184 | for (i=1; argv[i]; i++) | |
185 | { | |
186 | char *cp; | |
187 | for (cp = argv[i]; *cp; cp++) | |
188 | { | |
3f3726a3 | 189 | if (*cp == '"' || *cp == '\'' || *cp == '\\' || ISSPACE (*cp)) |
6599da04 JM |
190 | fputc ('\\', argfile); |
191 | fputc (*cp, argfile); | |
192 | } | |
193 | fputc ('\n', argfile); | |
194 | } | |
195 | fclose (argfile); | |
196 | ||
197 | rc = system (scmd); | |
198 | ||
199 | { | |
200 | int errno_save = errno; | |
201 | remove (rf); | |
202 | free (scmd); | |
203 | errno = errno_save; | |
204 | } | |
205 | #endif | |
206 | ||
207 | if (rc == -1) | |
208 | { | |
209 | *errmsg_fmt = install_error_msg; | |
210 | *errmsg_arg = program; | |
211 | return -1; | |
212 | } | |
213 | ||
214 | /* Tuck the status away for pwait, and return a "pid". */ | |
215 | last_status = rc << 8; | |
216 | return last_pid; | |
217 | } | |
218 | ||
219 | int | |
220 | pwait (pid, status, flags) | |
221 | int pid; | |
222 | int *status; | |
223 | int flags; | |
224 | { | |
225 | /* On MSDOS each pexecute must be followed by it's associated pwait. */ | |
226 | if (pid != last_pid | |
227 | /* Called twice for the same child? */ | |
228 | || pid == last_reaped) | |
229 | { | |
230 | /* ??? ECHILD would be a better choice. Can we use it here? */ | |
231 | errno = EINVAL; | |
232 | return -1; | |
233 | } | |
234 | /* ??? Here's an opportunity to canonicalize the values in STATUS. | |
235 | Needed? */ | |
236 | *status = last_status; | |
237 | last_reaped = last_pid; | |
238 | return last_pid; | |
239 | } | |
240 | ||
241 | #endif /* MSDOS */ | |
242 | ||
243 | #if defined (_WIN32) | |
244 | ||
245 | #include <process.h> | |
6599da04 | 246 | |
19ddc834 JM |
247 | #ifdef __CYGWIN32__ |
248 | ||
249 | #define fix_argv(argvec) (argvec) | |
250 | ||
77aff459 MK |
251 | extern int _spawnv (); |
252 | extern int _spawnvp (); | |
253 | ||
254 | int | |
255 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags) | |
256 | const char *program; | |
257 | char * const *argv; | |
258 | const char *this_pname; | |
259 | const char *temp_base; | |
260 | char **errmsg_fmt, **errmsg_arg; | |
261 | int flags; | |
262 | { | |
263 | int pid; | |
264 | ||
265 | if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE) | |
266 | abort (); | |
267 | pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv) | |
268 | (_P_NOWAIT, program, fix_argv(argv)); | |
269 | if (pid == -1) | |
270 | { | |
271 | *errmsg_fmt = install_error_msg; | |
272 | *errmsg_arg = program; | |
273 | return -1; | |
274 | } | |
275 | return pid; | |
276 | } | |
277 | ||
278 | int | |
279 | pwait (pid, status, flags) | |
280 | int pid; | |
281 | int *status; | |
282 | int flags; | |
283 | { | |
284 | /* ??? Here's an opportunity to canonicalize the values in STATUS. | |
285 | Needed? */ | |
286 | return cwait (status, pid, WAIT_CHILD); | |
287 | } | |
288 | ||
289 | #else /* ! __CYGWIN32__ */ | |
19ddc834 JM |
290 | |
291 | /* This is a kludge to get around the Microsoft C spawn functions' propensity | |
292 | to remove the outermost set of double quotes from all arguments. */ | |
293 | ||
294 | const char * const * | |
295 | fix_argv (argvec) | |
296 | char **argvec; | |
297 | { | |
298 | int i; | |
299 | ||
300 | for (i = 1; argvec[i] != 0; i++) | |
301 | { | |
302 | int len, j; | |
303 | char *temp, *newtemp; | |
304 | ||
305 | temp = argvec[i]; | |
306 | len = strlen (temp); | |
307 | for (j = 0; j < len; j++) | |
308 | { | |
309 | if (temp[j] == '"') | |
310 | { | |
311 | newtemp = xmalloc (len + 2); | |
312 | strncpy (newtemp, temp, j); | |
313 | newtemp [j] = '\\'; | |
314 | strncpy (&newtemp [j+1], &temp [j], len-j); | |
315 | newtemp [len+1] = 0; | |
316 | temp = newtemp; | |
317 | len++; | |
318 | j++; | |
319 | } | |
320 | } | |
321 | ||
322 | argvec[i] = temp; | |
323 | } | |
324 | ||
325 | return (const char * const *) argvec; | |
326 | } | |
327 | ||
77aff459 MK |
328 | #include <io.h> |
329 | #include <fcntl.h> | |
330 | #include <signal.h> | |
331 | ||
332 | /* mingw32 headers may not define the following. */ | |
333 | ||
334 | #ifndef _P_WAIT | |
335 | # define _P_WAIT 0 | |
336 | # define _P_NOWAIT 1 | |
337 | # define _P_OVERLAY 2 | |
338 | # define _P_NOWAITO 3 | |
339 | # define _P_DETACH 4 | |
340 | ||
341 | # define WAIT_CHILD 0 | |
342 | # define WAIT_GRANDCHILD 1 | |
343 | #endif | |
19ddc834 | 344 | |
77aff459 | 345 | /* Win32 supports pipes */ |
6599da04 JM |
346 | int |
347 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags) | |
348 | const char *program; | |
349 | char * const *argv; | |
350 | const char *this_pname; | |
351 | const char *temp_base; | |
352 | char **errmsg_fmt, **errmsg_arg; | |
353 | int flags; | |
354 | { | |
355 | int pid; | |
77aff459 MK |
356 | int pdes[2], org_stdin, org_stdout; |
357 | int input_desc, output_desc; | |
358 | int retries, sleep_interval; | |
359 | ||
360 | /* Pipe waiting from last process, to be used as input for the next one. | |
361 | Value is STDIN_FILE_NO if no pipe is waiting | |
362 | (i.e. the next command is the first of a group). */ | |
363 | static int last_pipe_input; | |
364 | ||
365 | /* If this is the first process, initialize. */ | |
366 | if (flags & PEXECUTE_FIRST) | |
367 | last_pipe_input = STDIN_FILE_NO; | |
368 | ||
369 | input_desc = last_pipe_input; | |
370 | ||
371 | /* If this isn't the last process, make a pipe for its output, | |
372 | and record it as waiting to be the input to the next process. */ | |
373 | if (! (flags & PEXECUTE_LAST)) | |
374 | { | |
375 | if (_pipe (pdes, 256, O_BINARY) < 0) | |
376 | { | |
377 | *errmsg_fmt = "pipe"; | |
378 | *errmsg_arg = NULL; | |
379 | return -1; | |
380 | } | |
381 | output_desc = pdes[WRITE_PORT]; | |
382 | last_pipe_input = pdes[READ_PORT]; | |
383 | } | |
384 | else | |
385 | { | |
386 | /* Last process. */ | |
387 | output_desc = STDOUT_FILE_NO; | |
388 | last_pipe_input = STDIN_FILE_NO; | |
389 | } | |
390 | ||
391 | if (input_desc != STDIN_FILE_NO) | |
392 | { | |
393 | org_stdin = dup (STDIN_FILE_NO); | |
394 | dup2 (input_desc, STDIN_FILE_NO); | |
395 | close (input_desc); | |
396 | } | |
397 | ||
398 | if (output_desc != STDOUT_FILE_NO) | |
399 | { | |
400 | org_stdout = dup (STDOUT_FILE_NO); | |
401 | dup2 (output_desc, STDOUT_FILE_NO); | |
402 | close (output_desc); | |
403 | } | |
6599da04 | 404 | |
19ddc834 JM |
405 | pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv) |
406 | (_P_NOWAIT, program, fix_argv(argv)); | |
77aff459 MK |
407 | |
408 | if (input_desc != STDIN_FILE_NO) | |
409 | { | |
410 | dup2 (org_stdin, STDIN_FILE_NO); | |
411 | close (org_stdin); | |
412 | } | |
413 | ||
414 | if (output_desc != STDOUT_FILE_NO) | |
415 | { | |
416 | dup2 (org_stdout, STDOUT_FILE_NO); | |
417 | close (org_stdout); | |
418 | } | |
419 | ||
6599da04 JM |
420 | if (pid == -1) |
421 | { | |
422 | *errmsg_fmt = install_error_msg; | |
423 | *errmsg_arg = program; | |
424 | return -1; | |
425 | } | |
77aff459 | 426 | |
6599da04 JM |
427 | return pid; |
428 | } | |
429 | ||
77aff459 MK |
430 | /* MS CRTDLL doesn't return enough information in status to decide if the |
431 | child exited due to a signal or not, rather it simply returns an | |
432 | integer with the exit code of the child; eg., if the child exited with | |
433 | an abort() call and didn't have a handler for SIGABRT, it simply returns | |
434 | with status = 3. We fix the status code to conform to the usual WIF* | |
435 | macros. Note that WIFSIGNALED will never be true under CRTDLL. */ | |
436 | ||
6599da04 JM |
437 | int |
438 | pwait (pid, status, flags) | |
439 | int pid; | |
440 | int *status; | |
441 | int flags; | |
442 | { | |
77aff459 MK |
443 | int termstat; |
444 | ||
445 | pid = _cwait (&termstat, pid, WAIT_CHILD); | |
446 | ||
6599da04 JM |
447 | /* ??? Here's an opportunity to canonicalize the values in STATUS. |
448 | Needed? */ | |
77aff459 MK |
449 | |
450 | /* cwait returns the child process exit code in termstat. | |
451 | A value of 3 indicates that the child caught a signal, but not | |
452 | which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we | |
453 | report SIGABRT. */ | |
454 | if (termstat == 3) | |
455 | *status = SIGABRT; | |
456 | else | |
457 | *status = (((termstat) & 0xff) << 8); | |
458 | ||
459 | return pid; | |
6599da04 JM |
460 | } |
461 | ||
77aff459 MK |
462 | #endif /* ! defined (__CYGWIN32__) */ |
463 | ||
6599da04 JM |
464 | #endif /* _WIN32 */ |
465 | ||
466 | #ifdef OS2 | |
467 | ||
468 | /* ??? Does OS2 have process.h? */ | |
469 | extern int spawnv (); | |
470 | extern int spawnvp (); | |
471 | ||
472 | int | |
473 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags) | |
474 | const char *program; | |
475 | char * const *argv; | |
476 | const char *this_pname; | |
477 | const char *temp_base; | |
478 | char **errmsg_fmt, **errmsg_arg; | |
479 | int flags; | |
480 | { | |
481 | int pid; | |
482 | ||
483 | if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE) | |
484 | abort (); | |
485 | /* ??? Presumably 1 == _P_NOWAIT. */ | |
486 | pid = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv); | |
487 | if (pid == -1) | |
488 | { | |
489 | *errmsg_fmt = install_error_msg; | |
490 | *errmsg_arg = program; | |
491 | return -1; | |
492 | } | |
493 | return pid; | |
494 | } | |
495 | ||
496 | int | |
497 | pwait (pid, status, flags) | |
498 | int pid; | |
499 | int *status; | |
500 | int flags; | |
501 | { | |
502 | /* ??? Here's an opportunity to canonicalize the values in STATUS. | |
503 | Needed? */ | |
504 | int pid = wait (status); | |
505 | return pid; | |
506 | } | |
507 | ||
508 | #endif /* OS2 */ | |
509 | ||
510 | #ifdef MPW | |
511 | ||
512 | /* MPW pexecute doesn't actually run anything; instead, it writes out | |
513 | script commands that, when run, will do the actual executing. | |
514 | ||
515 | For example, in GCC's case, GCC will write out several script commands: | |
516 | ||
517 | cpp ... | |
518 | cc1 ... | |
519 | as ... | |
520 | ld ... | |
521 | ||
522 | and then exit. None of the above programs will have run yet. The task | |
523 | that called GCC will then execute the script and cause cpp,etc. to run. | |
524 | The caller must invoke pfinish before calling exit. This adds | |
525 | the finishing touches to the generated script. */ | |
526 | ||
527 | static int first_time = 1; | |
528 | ||
529 | int | |
530 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags) | |
531 | const char *program; | |
3f3726a3 | 532 | char * const *argv; |
6599da04 JM |
533 | const char *this_pname; |
534 | const char *temp_base; | |
535 | char **errmsg_fmt, **errmsg_arg; | |
536 | int flags; | |
537 | { | |
538 | char tmpprogram[255]; | |
539 | char *cp, *tmpname; | |
540 | int i; | |
541 | ||
542 | mpwify_filename (program, tmpprogram); | |
543 | if (first_time) | |
544 | { | |
545 | printf ("Set Failed 0\n"); | |
546 | first_time = 0; | |
547 | } | |
548 | ||
549 | fputs ("If {Failed} == 0\n", stdout); | |
550 | /* If being verbose, output a copy of the command. It should be | |
551 | accurate enough and escaped enough to be "clickable". */ | |
552 | if (flags & PEXECUTE_VERBOSE) | |
553 | { | |
554 | fputs ("\tEcho ", stdout); | |
555 | fputc ('\'', stdout); | |
556 | fputs (tmpprogram, stdout); | |
557 | fputc ('\'', stdout); | |
558 | fputc (' ', stdout); | |
559 | for (i=1; argv[i]; i++) | |
560 | { | |
6599da04 | 561 | fputc ('\'', stdout); |
3f3726a3 JL |
562 | /* See if we have an argument that needs fixing. */ |
563 | if (strchr(argv[i], '/')) | |
564 | { | |
565 | tmpname = (char *) xmalloc (256); | |
566 | mpwify_filename (argv[i], tmpname); | |
567 | argv[i] = tmpname; | |
568 | } | |
6599da04 JM |
569 | for (cp = argv[i]; *cp; cp++) |
570 | { | |
3f3726a3 JL |
571 | /* Write an Option-d escape char in front of special chars. */ |
572 | if (strchr("'+", *cp)) | |
6599da04 JM |
573 | fputc ('\266', stdout); |
574 | fputc (*cp, stdout); | |
575 | } | |
576 | fputc ('\'', stdout); | |
577 | fputc (' ', stdout); | |
578 | } | |
579 | fputs ("\n", stdout); | |
580 | } | |
581 | fputs ("\t", stdout); | |
582 | fputs (tmpprogram, stdout); | |
583 | fputc (' ', stdout); | |
584 | ||
585 | for (i=1; argv[i]; i++) | |
586 | { | |
3f3726a3 JL |
587 | /* See if we have an argument that needs fixing. */ |
588 | if (strchr(argv[i], '/')) | |
589 | { | |
590 | tmpname = (char *) xmalloc (256); | |
591 | mpwify_filename (argv[i], tmpname); | |
592 | argv[i] = tmpname; | |
593 | } | |
6599da04 JM |
594 | if (strchr (argv[i], ' ')) |
595 | fputc ('\'', stdout); | |
596 | for (cp = argv[i]; *cp; cp++) | |
597 | { | |
3f3726a3 JL |
598 | /* Write an Option-d escape char in front of special chars. */ |
599 | if (strchr("'+", *cp)) | |
600 | fputc ('\266', stdout); | |
6599da04 JM |
601 | fputc (*cp, stdout); |
602 | } | |
603 | if (strchr (argv[i], ' ')) | |
604 | fputc ('\'', stdout); | |
605 | fputc (' ', stdout); | |
606 | } | |
607 | ||
608 | fputs ("\n", stdout); | |
609 | ||
610 | /* Output commands that arrange to clean up and exit if a failure occurs. | |
611 | We have to be careful to collect the status from the program that was | |
612 | run, rather than some other script command. Also, we don't exit | |
613 | immediately, since necessary cleanups are at the end of the script. */ | |
614 | fputs ("\tSet TmpStatus {Status}\n", stdout); | |
615 | fputs ("\tIf {TmpStatus} != 0\n", stdout); | |
616 | fputs ("\t\tSet Failed {TmpStatus}\n", stdout); | |
617 | fputs ("\tEnd\n", stdout); | |
618 | fputs ("End\n", stdout); | |
619 | ||
620 | /* We're just composing a script, can't fail here. */ | |
621 | return 0; | |
622 | } | |
623 | ||
624 | int | |
625 | pwait (pid, status, flags) | |
626 | int pid; | |
627 | int *status; | |
628 | int flags; | |
629 | { | |
630 | *status = 0; | |
631 | return 0; | |
632 | } | |
633 | ||
634 | /* Write out commands that will exit with the correct error code | |
635 | if something in the script failed. */ | |
636 | ||
637 | void | |
638 | pfinish () | |
639 | { | |
640 | printf ("\tExit \"{Failed}\"\n"); | |
641 | } | |
642 | ||
643 | #endif /* MPW */ | |
644 | ||
645 | /* include for Unix-like environments but not for Dos-like environments */ | |
646 | #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \ | |
647 | && ! defined (_WIN32) | |
648 | ||
6599da04 JM |
649 | extern int execv (); |
650 | extern int execvp (); | |
3f3726a3 JL |
651 | #ifdef IN_GCC |
652 | extern char * my_strerror PROTO ((int)); | |
653 | #endif | |
6599da04 JM |
654 | |
655 | int | |
656 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags) | |
657 | const char *program; | |
658 | char * const *argv; | |
659 | const char *this_pname; | |
660 | const char *temp_base; | |
661 | char **errmsg_fmt, **errmsg_arg; | |
662 | int flags; | |
663 | { | |
664 | int (*func)() = (flags & PEXECUTE_SEARCH ? execvp : execv); | |
665 | int pid; | |
666 | int pdes[2]; | |
667 | int input_desc, output_desc; | |
668 | int retries, sleep_interval; | |
669 | /* Pipe waiting from last process, to be used as input for the next one. | |
670 | Value is STDIN_FILE_NO if no pipe is waiting | |
671 | (i.e. the next command is the first of a group). */ | |
672 | static int last_pipe_input; | |
673 | ||
674 | /* If this is the first process, initialize. */ | |
675 | if (flags & PEXECUTE_FIRST) | |
676 | last_pipe_input = STDIN_FILE_NO; | |
677 | ||
678 | input_desc = last_pipe_input; | |
679 | ||
680 | /* If this isn't the last process, make a pipe for its output, | |
681 | and record it as waiting to be the input to the next process. */ | |
682 | if (! (flags & PEXECUTE_LAST)) | |
683 | { | |
684 | if (pipe (pdes) < 0) | |
685 | { | |
686 | *errmsg_fmt = "pipe"; | |
687 | *errmsg_arg = NULL; | |
688 | return -1; | |
689 | } | |
690 | output_desc = pdes[WRITE_PORT]; | |
691 | last_pipe_input = pdes[READ_PORT]; | |
692 | } | |
693 | else | |
694 | { | |
695 | /* Last process. */ | |
696 | output_desc = STDOUT_FILE_NO; | |
697 | last_pipe_input = STDIN_FILE_NO; | |
698 | } | |
699 | ||
700 | /* Fork a subprocess; wait and retry if it fails. */ | |
701 | sleep_interval = 1; | |
702 | for (retries = 0; retries < 4; retries++) | |
703 | { | |
704 | pid = vfork (); | |
705 | if (pid >= 0) | |
706 | break; | |
707 | sleep (sleep_interval); | |
708 | sleep_interval *= 2; | |
709 | } | |
710 | ||
711 | switch (pid) | |
712 | { | |
713 | case -1: | |
714 | { | |
c5f7c445 | 715 | *errmsg_fmt = VFORK_STRING; |
6599da04 JM |
716 | *errmsg_arg = NULL; |
717 | return -1; | |
718 | } | |
719 | ||
720 | case 0: /* child */ | |
721 | /* Move the input and output pipes into place, if necessary. */ | |
722 | if (input_desc != STDIN_FILE_NO) | |
723 | { | |
724 | close (STDIN_FILE_NO); | |
725 | dup (input_desc); | |
726 | close (input_desc); | |
727 | } | |
728 | if (output_desc != STDOUT_FILE_NO) | |
729 | { | |
730 | close (STDOUT_FILE_NO); | |
731 | dup (output_desc); | |
732 | close (output_desc); | |
733 | } | |
734 | ||
735 | /* Close the parent's descs that aren't wanted here. */ | |
736 | if (last_pipe_input != STDIN_FILE_NO) | |
737 | close (last_pipe_input); | |
738 | ||
739 | /* Exec the program. */ | |
740 | (*func) (program, argv); | |
741 | ||
742 | /* Note: Calling fprintf and exit here doesn't seem right for vfork. */ | |
743 | fprintf (stderr, "%s: ", this_pname); | |
744 | fprintf (stderr, install_error_msg, program); | |
745 | #ifdef IN_GCC | |
746 | fprintf (stderr, ": %s\n", my_strerror (errno)); | |
747 | #else | |
748 | fprintf (stderr, ": %s\n", xstrerror (errno)); | |
749 | #endif | |
750 | exit (-1); | |
751 | /* NOTREACHED */ | |
752 | return 0; | |
753 | ||
754 | default: | |
755 | /* In the parent, after forking. | |
756 | Close the descriptors that we made for this child. */ | |
757 | if (input_desc != STDIN_FILE_NO) | |
758 | close (input_desc); | |
759 | if (output_desc != STDOUT_FILE_NO) | |
760 | close (output_desc); | |
761 | ||
762 | /* Return child's process number. */ | |
763 | return pid; | |
764 | } | |
765 | } | |
766 | ||
767 | int | |
768 | pwait (pid, status, flags) | |
769 | int pid; | |
770 | int *status; | |
771 | int flags; | |
772 | { | |
773 | /* ??? Here's an opportunity to canonicalize the values in STATUS. | |
774 | Needed? */ | |
775 | #ifdef VMS | |
776 | pid = waitpid (-1, status, 0); | |
777 | #else | |
778 | pid = wait (status); | |
779 | #endif | |
780 | return pid; | |
781 | } | |
782 | ||
783 | #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */ |