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.
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.
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.
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. */
23 /* This file exports two functions: pexecute and pwait. */
25 /* This file lives in at least two places: libiberty and gcc.
26 Don't change one without the other. */
34 #ifdef NEED_DECLARATION_ERRNO
46 #ifdef HAVE_SYS_WAIT_H
50 #include "libiberty.h"
51 #include "safe-ctype.h"
53 /* stdin file number. */
54 #define STDIN_FILE_NO 0
56 /* stdout file number. */
57 #define STDOUT_FILE_NO 1
59 /* value of `pipe': port index for reading. */
62 /* value of `pipe': port index for writing. */
65 static char *install_error_msg = "installation problem, cannot exec `%s'";
67 /* pexecute: execute a program.
69 @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)
73 @var{program} and @var{argv} are the arguments to
74 @code{execv}/@code{execvp}.
76 @var{this_pname} is name of the calling program (i.e., @code{argv[0]}).
78 @var{temp_base} is the path name, sans suffix, of a temporary file to
79 use if needed. This is currently only needed for MS-DOS ports that
80 don't use @code{go32} (do any still exist?). Ports that don't need it
83 (@code{@var{flags} & PEXECUTE_SEARCH}) is non-zero if @env{PATH} should be searched
84 (??? It's not clear that GCC passes this flag correctly). (@code{@var{flags} &
85 PEXECUTE_FIRST}) is nonzero for the first process in chain.
86 (@code{@var{flags} & PEXECUTE_FIRST}) is nonzero for the last process
87 in chain. The first/last flags could be simplified to only mark the
88 last of a chain of processes but that requires the caller to always
89 mark the last one (and not give up early if some error occurs).
90 It's more robust to require the caller to mark both ends of the chain.
92 The result is the pid on systems like Unix where we
93 @code{fork}/@code{exec} and on systems like WIN32 and OS/2 where we
94 use @code{spawn}. It is up to the caller to wait for the child.
96 The result is the @code{WEXITSTATUS} on systems like MS-DOS where we
97 @code{spawn} and wait for the child here.
99 Upon failure, @var{errmsg_fmt} and @var{errmsg_arg} are set to the
100 text of the error message with an optional argument (if not needed,
101 @var{errmsg_arg} is set to @code{NULL}), and @minus{}1 is returned.
102 @code{errno} is available to the caller to use.
106 @deftypefn Extension int pwait (int @var{pid}, int *@var{status}, int @var{flags})
108 Waits for a program started by @code{pexecute} to finish.
110 @var{pid} is the process id of the task to wait for. @var{status} is
111 the `status' argument to wait. @var{flags} is currently unused (allows
112 future enhancement without breaking upward compatibility). Pass 0 for now.
114 The result is the pid of the child reaped, or -1 for failure
115 (@code{errno} says why).
117 On systems that don't support waiting for a particular child, @var{pid} is
118 ignored. On systems like MS-DOS that don't really multitask @code{pwait}
119 is just a mechanism to provide a consistent interface for the caller.
123 @undocumented pfinish
125 pfinish: finish generation of script
127 pfinish is necessary for systems like MPW where a script is generated that
128 runs the requested programs. */
132 /* MSDOS doesn't multitask, but for the sake of a consistent interface
133 the code behaves like it does. pexecute runs the program, tucks the
134 exit code away, and returns a "pid". pwait must be called to fetch the
139 /* For communicating information from pexecute to pwait. */
140 static int last_pid = 0;
141 static int last_status = 0;
142 static int last_reaped = 0;
145 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
148 const char *this_pname;
149 const char *temp_base;
150 char **errmsg_fmt, **errmsg_arg;
159 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
163 /* ??? What are the possible return values from spawnv? */
164 rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (P_WAIT, program, argv);
168 int i, el = flags & PEXECUTE_SEARCH ? 4 : 0;
171 temp_base = choose_temp_base ();
172 scmd = (char *) xmalloc (strlen (program) + strlen (temp_base) + 6 + el);
173 rf = scmd + strlen(program) + 2 + el;
174 sprintf (scmd, "%s%s @%s.gp", program,
175 (flags & PEXECUTE_SEARCH ? ".exe" : ""), temp_base);
176 argfile = fopen (rf, "w");
179 int errno_save = errno;
182 *errmsg_fmt = "cannot open `%s.gp'";
183 *errmsg_arg = temp_base;
187 for (i=1; argv[i]; i++)
190 for (cp = argv[i]; *cp; cp++)
192 if (*cp == '"' || *cp == '\'' || *cp == '\\' || ISSPACE (*cp))
193 fputc ('\\', argfile);
194 fputc (*cp, argfile);
196 fputc ('\n', argfile);
203 int errno_save = errno;
212 *errmsg_fmt = install_error_msg;
213 *errmsg_arg = (char *)program;
217 /* Tuck the status away for pwait, and return a "pid". */
218 last_status = rc << 8;
222 /* Use ECHILD if available, otherwise use EINVAL. */
224 #define PWAIT_ERROR ECHILD
226 #define PWAIT_ERROR EINVAL
230 pwait (pid, status, flags)
235 /* On MSDOS each pexecute must be followed by it's associated pwait. */
237 /* Called twice for the same child? */
238 || pid == last_reaped)
243 /* ??? Here's an opportunity to canonicalize the values in STATUS.
246 *status = (last_status >> 8);
248 *status = last_status;
250 last_reaped = last_pid;
256 #if defined (_WIN32) && ! defined (_UWIN)
262 #define fix_argv(argvec) (argvec)
264 extern int _spawnv ();
265 extern int _spawnvp ();
267 #else /* ! __CYGWIN__ */
269 /* This is a kludge to get around the Microsoft C spawn functions' propensity
270 to remove the outermost set of double quotes from all arguments. */
272 static const char * const *
278 for (i = 1; argvec[i] != 0; i++)
281 char *temp, *newtemp;
285 for (j = 0; j < len; j++)
289 newtemp = xmalloc (len + 2);
290 strncpy (newtemp, temp, j);
292 strncpy (&newtemp [j+1], &temp [j], len-j);
303 for (i = 0; argvec[i] != 0; i++)
305 if (strpbrk (argvec[i], " \t"))
307 int len, trailing_backslash;
310 len = strlen (argvec[i]);
311 trailing_backslash = 0;
313 /* There is an added complication when an arg with embedded white
314 space ends in a backslash (such as in the case of -iprefix arg
315 passed to cpp). The resulting quoted strings gets misinterpreted
316 by the command interpreter -- it thinks that the ending quote
317 is escaped by the trailing backslash and things get confused.
318 We handle this case by escaping the trailing backslash, provided
319 it was not escaped in the first place. */
321 && argvec[i][len-1] == '\\'
322 && argvec[i][len-2] != '\\')
324 trailing_backslash = 1;
325 ++len; /* to escape the final backslash. */
328 len += 2; /* and for the enclosing quotes. */
330 temp = xmalloc (len + 1);
332 strcpy (temp + 1, argvec[i]);
333 if (trailing_backslash)
342 return (const char * const *) argvec;
344 #endif /* __CYGWIN__ */
350 /* mingw32 headers may not define the following. */
355 # define _P_OVERLAY 2
356 # define _P_NOWAITO 3
359 # define WAIT_CHILD 0
360 # define WAIT_GRANDCHILD 1
363 /* Win32 supports pipes */
365 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
368 const char *this_pname;
369 const char *temp_base;
370 char **errmsg_fmt, **errmsg_arg;
374 int pdes[2], org_stdin, org_stdout;
375 int input_desc, output_desc;
376 int retries, sleep_interval;
378 /* Pipe waiting from last process, to be used as input for the next one.
379 Value is STDIN_FILE_NO if no pipe is waiting
380 (i.e. the next command is the first of a group). */
381 static int last_pipe_input;
383 /* If this is the first process, initialize. */
384 if (flags & PEXECUTE_FIRST)
385 last_pipe_input = STDIN_FILE_NO;
387 input_desc = last_pipe_input;
389 /* If this isn't the last process, make a pipe for its output,
390 and record it as waiting to be the input to the next process. */
391 if (! (flags & PEXECUTE_LAST))
393 if (_pipe (pdes, 256, O_BINARY) < 0)
395 *errmsg_fmt = "pipe";
399 output_desc = pdes[WRITE_PORT];
400 last_pipe_input = pdes[READ_PORT];
405 output_desc = STDOUT_FILE_NO;
406 last_pipe_input = STDIN_FILE_NO;
409 if (input_desc != STDIN_FILE_NO)
411 org_stdin = dup (STDIN_FILE_NO);
412 dup2 (input_desc, STDIN_FILE_NO);
416 if (output_desc != STDOUT_FILE_NO)
418 org_stdout = dup (STDOUT_FILE_NO);
419 dup2 (output_desc, STDOUT_FILE_NO);
423 pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
424 (_P_NOWAIT, program, fix_argv(argv));
426 if (input_desc != STDIN_FILE_NO)
428 dup2 (org_stdin, STDIN_FILE_NO);
432 if (output_desc != STDOUT_FILE_NO)
434 dup2 (org_stdout, STDOUT_FILE_NO);
440 *errmsg_fmt = install_error_msg;
441 *errmsg_arg = program;
448 /* MS CRTDLL doesn't return enough information in status to decide if the
449 child exited due to a signal or not, rather it simply returns an
450 integer with the exit code of the child; eg., if the child exited with
451 an abort() call and didn't have a handler for SIGABRT, it simply returns
452 with status = 3. We fix the status code to conform to the usual WIF*
453 macros. Note that WIFSIGNALED will never be true under CRTDLL. */
456 pwait (pid, status, flags)
462 return wait (status);
466 pid = _cwait (&termstat, pid, WAIT_CHILD);
468 /* ??? Here's an opportunity to canonicalize the values in STATUS.
471 /* cwait returns the child process exit code in termstat.
472 A value of 3 indicates that the child caught a signal, but not
473 which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
478 *status = (((termstat) & 0xff) << 8);
481 #endif /* __CYGWIN__ */
484 #endif /* _WIN32 && ! _UWIN */
488 /* ??? Does OS2 have process.h? */
489 extern int spawnv ();
490 extern int spawnvp ();
493 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
496 const char *this_pname;
497 const char *temp_base;
498 char **errmsg_fmt, **errmsg_arg;
503 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
505 /* ??? Presumably 1 == _P_NOWAIT. */
506 pid = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
509 *errmsg_fmt = install_error_msg;
510 *errmsg_arg = program;
517 pwait (pid, status, flags)
522 /* ??? Here's an opportunity to canonicalize the values in STATUS.
524 int pid = wait (status);
532 /* MPW pexecute doesn't actually run anything; instead, it writes out
533 script commands that, when run, will do the actual executing.
535 For example, in GCC's case, GCC will write out several script commands:
542 and then exit. None of the above programs will have run yet. The task
543 that called GCC will then execute the script and cause cpp,etc. to run.
544 The caller must invoke pfinish before calling exit. This adds
545 the finishing touches to the generated script. */
547 static int first_time = 1;
550 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
553 const char *this_pname;
554 const char *temp_base;
555 char **errmsg_fmt, **errmsg_arg;
558 char tmpprogram[255];
562 mpwify_filename (program, tmpprogram);
565 printf ("Set Failed 0\n");
569 fputs ("If {Failed} == 0\n", stdout);
570 /* If being verbose, output a copy of the command. It should be
571 accurate enough and escaped enough to be "clickable". */
572 if (flags & PEXECUTE_VERBOSE)
574 fputs ("\tEcho ", stdout);
575 fputc ('\'', stdout);
576 fputs (tmpprogram, stdout);
577 fputc ('\'', stdout);
579 for (i=1; argv[i]; i++)
581 fputc ('\'', stdout);
582 /* See if we have an argument that needs fixing. */
583 if (strchr(argv[i], '/'))
585 tmpname = (char *) xmalloc (256);
586 mpwify_filename (argv[i], tmpname);
589 for (cp = argv[i]; *cp; cp++)
591 /* Write an Option-d escape char in front of special chars. */
592 if (strchr("'+", *cp))
593 fputc ('\266', stdout);
596 fputc ('\'', stdout);
599 fputs ("\n", stdout);
601 fputs ("\t", stdout);
602 fputs (tmpprogram, stdout);
605 for (i=1; argv[i]; i++)
607 /* See if we have an argument that needs fixing. */
608 if (strchr(argv[i], '/'))
610 tmpname = (char *) xmalloc (256);
611 mpwify_filename (argv[i], tmpname);
614 if (strchr (argv[i], ' '))
615 fputc ('\'', stdout);
616 for (cp = argv[i]; *cp; cp++)
618 /* Write an Option-d escape char in front of special chars. */
619 if (strchr("'+", *cp))
620 fputc ('\266', stdout);
623 if (strchr (argv[i], ' '))
624 fputc ('\'', stdout);
628 fputs ("\n", stdout);
630 /* Output commands that arrange to clean up and exit if a failure occurs.
631 We have to be careful to collect the status from the program that was
632 run, rather than some other script command. Also, we don't exit
633 immediately, since necessary cleanups are at the end of the script. */
634 fputs ("\tSet TmpStatus {Status}\n", stdout);
635 fputs ("\tIf {TmpStatus} != 0\n", stdout);
636 fputs ("\t\tSet Failed {TmpStatus}\n", stdout);
637 fputs ("\tEnd\n", stdout);
638 fputs ("End\n", stdout);
640 /* We're just composing a script, can't fail here. */
645 pwait (pid, status, flags)
654 /* Write out commands that will exit with the correct error code
655 if something in the script failed. */
660 printf ("\tExit \"{Failed}\"\n");
665 /* include for Unix-like environments but not for Dos-like environments */
666 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
667 && ! (defined (_WIN32) && ! defined (_UWIN))
670 extern int execvp ();
673 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
676 const char *this_pname;
677 const char *temp_base ATTRIBUTE_UNUSED;
678 char **errmsg_fmt, **errmsg_arg;
681 int (*func)() = (flags & PEXECUTE_SEARCH ? execvp : execv);
684 int input_desc, output_desc;
685 int retries, sleep_interval;
686 /* Pipe waiting from last process, to be used as input for the next one.
687 Value is STDIN_FILE_NO if no pipe is waiting
688 (i.e. the next command is the first of a group). */
689 static int last_pipe_input;
691 /* If this is the first process, initialize. */
692 if (flags & PEXECUTE_FIRST)
693 last_pipe_input = STDIN_FILE_NO;
695 input_desc = last_pipe_input;
697 /* If this isn't the last process, make a pipe for its output,
698 and record it as waiting to be the input to the next process. */
699 if (! (flags & PEXECUTE_LAST))
703 *errmsg_fmt = "pipe";
707 output_desc = pdes[WRITE_PORT];
708 last_pipe_input = pdes[READ_PORT];
713 output_desc = STDOUT_FILE_NO;
714 last_pipe_input = STDIN_FILE_NO;
717 /* Fork a subprocess; wait and retry if it fails. */
720 for (retries = 0; retries < 4; retries++)
725 sleep (sleep_interval);
732 *errmsg_fmt = "fork";
737 /* Move the input and output pipes into place, if necessary. */
738 if (input_desc != STDIN_FILE_NO)
740 close (STDIN_FILE_NO);
744 if (output_desc != STDOUT_FILE_NO)
746 close (STDOUT_FILE_NO);
751 /* Close the parent's descs that aren't wanted here. */
752 if (last_pipe_input != STDIN_FILE_NO)
753 close (last_pipe_input);
755 /* Exec the program. */
756 (*func) (program, argv);
758 fprintf (stderr, "%s: ", this_pname);
759 fprintf (stderr, install_error_msg, program);
760 fprintf (stderr, ": %s\n", xstrerror (errno));
766 /* In the parent, after forking.
767 Close the descriptors that we made for this child. */
768 if (input_desc != STDIN_FILE_NO)
770 if (output_desc != STDOUT_FILE_NO)
773 /* Return child's process number. */
779 pwait (pid, status, flags)
782 int flags ATTRIBUTE_UNUSED;
784 /* ??? Here's an opportunity to canonicalize the values in STATUS.
787 pid = waitpid (-1, status, 0);
794 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! (_WIN32 && ! _UWIN) */