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, 1997, 1998 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. */
21 /* This file exports two functions: pexecute and pwait. */
23 /* This file lives in at least two places: libiberty and gcc.
24 Don't change one without the other. */
33 #include "gansidecl.h"
34 /* ??? Need to find a suitable header file. */
35 #define PEXECUTE_FIRST 1
36 #define PEXECUTE_LAST 2
37 #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST)
38 #define PEXECUTE_SEARCH 4
39 #define PEXECUTE_VERBOSE 8
41 #include "libiberty.h"
44 /* stdin file number. */
45 #define STDIN_FILE_NO 0
47 /* stdout file number. */
48 #define STDOUT_FILE_NO 1
50 /* value of `pipe': port index for reading. */
53 /* value of `pipe': port index for writing. */
56 static char *install_error_msg = "installation problem, cannot exec `%s'";
58 /* pexecute: execute a program.
60 PROGRAM and ARGV are the arguments to execv/execvp.
62 THIS_PNAME is name of the calling program (i.e. argv[0]).
64 TEMP_BASE is the path name, sans suffix, of a temporary file to use
65 if needed. This is currently only needed for MSDOS ports that don't use
66 GO32 (do any still exist?). Ports that don't need it can pass NULL.
68 (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched
69 (??? It's not clear that GCC passes this flag correctly).
70 (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain.
71 (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain.
72 FIRST_LAST could be simplified to only mark the last of a chain of processes
73 but that requires the caller to always mark the last one (and not give up
74 early if some error occurs). It's more robust to require the caller to
75 mark both ends of the chain.
77 The result is the pid on systems like Unix where we fork/exec and on systems
78 like WIN32 and OS2 where we use spawn. It is up to the caller to wait for
81 The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
84 Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error
85 message with an optional argument (if not needed, ERRMSG_ARG is set to
86 NULL), and -1 is returned. `errno' is available to the caller to use.
88 pwait: cover function for wait.
90 PID is the process id of the task to wait for.
91 STATUS is the `status' argument to wait.
92 FLAGS is currently unused (allows future enhancement without breaking
93 upward compatibility). Pass 0 for now.
95 The result is the pid of the child reaped,
96 or -1 for failure (errno says why).
98 On systems that don't support waiting for a particular child, PID is
99 ignored. On systems like MSDOS that don't really multitask pwait
100 is just a mechanism to provide a consistent interface for the caller.
102 pfinish: finish generation of script
104 pfinish is necessary for systems like MPW where a script is generated that
105 runs the requested programs.
110 /* MSDOS doesn't multitask, but for the sake of a consistent interface
111 the code behaves like it does. pexecute runs the program, tucks the
112 exit code away, and returns a "pid". pwait must be called to fetch the
117 /* For communicating information from pexecute to pwait. */
118 static int last_pid = 0;
119 static int last_status = 0;
120 static int last_reaped = 0;
123 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
126 const char *this_pname;
127 const char *temp_base;
128 char **errmsg_fmt, **errmsg_arg;
137 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
141 /* ??? What are the possible return values from spawnv? */
142 rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
146 int i, el = flags & PEXECUTE_SEARCH ? 4 : 0;
148 scmd = (char *) xmalloc (strlen (program) + strlen (temp_base) + 6 + el);
149 rf = scmd + strlen(program) + 2 + el;
150 sprintf (scmd, "%s%s @%s.gp", program,
151 (flags & PEXECUTE_SEARCH ? ".exe" : ""), temp_base);
152 argfile = fopen (rf, "w");
155 int errno_save = errno;
158 *errmsg_fmt = "cannot open `%s.gp'";
159 *errmsg_arg = temp_base;
163 for (i=1; argv[i]; i++)
166 for (cp = argv[i]; *cp; cp++)
168 if (*cp == '"' || *cp == '\'' || *cp == '\\' || ISSPACE (*cp))
169 fputc ('\\', argfile);
170 fputc (*cp, argfile);
172 fputc ('\n', argfile);
179 int errno_save = errno;
188 *errmsg_fmt = install_error_msg;
189 *errmsg_arg = program;
193 /* Tuck the status away for pwait, and return a "pid". */
194 last_status = rc << 8;
199 pwait (pid, status, flags)
204 /* On MSDOS each pexecute must be followed by it's associated pwait. */
206 /* Called twice for the same child? */
207 || pid == last_reaped)
209 /* ??? ECHILD would be a better choice. Can we use it here? */
213 /* ??? Here's an opportunity to canonicalize the values in STATUS.
215 *status = last_status;
216 last_reaped = last_pid;
228 #define fix_argv(argvec) (argvec)
230 extern int _spawnv ();
231 extern int _spawnvp ();
234 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
237 const char *this_pname;
238 const char *temp_base;
239 char **errmsg_fmt, **errmsg_arg;
244 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
246 pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
247 (_P_NOWAIT, program, fix_argv(argv));
250 *errmsg_fmt = install_error_msg;
251 *errmsg_arg = program;
258 pwait (pid, status, flags)
263 /* ??? Here's an opportunity to canonicalize the values in STATUS.
265 return cwait (status, pid, WAIT_CHILD);
268 #else /* ! __CYGWIN32__ */
270 /* This is a kludge to get around the Microsoft C spawn functions' propensity
271 to remove the outermost set of double quotes from all arguments. */
279 for (i = 1; argvec[i] != 0; i++)
282 char *temp, *newtemp;
286 for (j = 0; j < len; j++)
290 newtemp = xmalloc (len + 2);
291 strncpy (newtemp, temp, j);
293 strncpy (&newtemp [j+1], &temp [j], len-j);
304 return (const char * const *) argvec;
311 /* mingw32 headers may not define the following. */
316 # define _P_OVERLAY 2
317 # define _P_NOWAITO 3
320 # define WAIT_CHILD 0
321 # define WAIT_GRANDCHILD 1
324 /* Win32 supports pipes */
326 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
329 const char *this_pname;
330 const char *temp_base;
331 char **errmsg_fmt, **errmsg_arg;
335 int pdes[2], org_stdin, org_stdout;
336 int input_desc, output_desc;
337 int retries, sleep_interval;
339 /* Pipe waiting from last process, to be used as input for the next one.
340 Value is STDIN_FILE_NO if no pipe is waiting
341 (i.e. the next command is the first of a group). */
342 static int last_pipe_input;
344 /* If this is the first process, initialize. */
345 if (flags & PEXECUTE_FIRST)
346 last_pipe_input = STDIN_FILE_NO;
348 input_desc = last_pipe_input;
350 /* If this isn't the last process, make a pipe for its output,
351 and record it as waiting to be the input to the next process. */
352 if (! (flags & PEXECUTE_LAST))
354 if (_pipe (pdes, 256, O_BINARY) < 0)
356 *errmsg_fmt = "pipe";
360 output_desc = pdes[WRITE_PORT];
361 last_pipe_input = pdes[READ_PORT];
366 output_desc = STDOUT_FILE_NO;
367 last_pipe_input = STDIN_FILE_NO;
370 if (input_desc != STDIN_FILE_NO)
372 org_stdin = dup (STDIN_FILE_NO);
373 dup2 (input_desc, STDIN_FILE_NO);
377 if (output_desc != STDOUT_FILE_NO)
379 org_stdout = dup (STDOUT_FILE_NO);
380 dup2 (output_desc, STDOUT_FILE_NO);
384 pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
385 (_P_NOWAIT, program, fix_argv(argv));
387 if (input_desc != STDIN_FILE_NO)
389 dup2 (org_stdin, STDIN_FILE_NO);
393 if (output_desc != STDOUT_FILE_NO)
395 dup2 (org_stdout, STDOUT_FILE_NO);
401 *errmsg_fmt = install_error_msg;
402 *errmsg_arg = program;
409 /* MS CRTDLL doesn't return enough information in status to decide if the
410 child exited due to a signal or not, rather it simply returns an
411 integer with the exit code of the child; eg., if the child exited with
412 an abort() call and didn't have a handler for SIGABRT, it simply returns
413 with status = 3. We fix the status code to conform to the usual WIF*
414 macros. Note that WIFSIGNALED will never be true under CRTDLL. */
417 pwait (pid, status, flags)
424 pid = _cwait (&termstat, pid, WAIT_CHILD);
426 /* ??? Here's an opportunity to canonicalize the values in STATUS.
429 /* cwait returns the child process exit code in termstat.
430 A value of 3 indicates that the child caught a signal, but not
431 which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
436 *status = (((termstat) & 0xff) << 8);
441 #endif /* ! defined (__CYGWIN32__) */
447 /* ??? Does OS2 have process.h? */
448 extern int spawnv ();
449 extern int spawnvp ();
452 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
455 const char *this_pname;
456 const char *temp_base;
457 char **errmsg_fmt, **errmsg_arg;
462 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
464 /* ??? Presumably 1 == _P_NOWAIT. */
465 pid = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
468 *errmsg_fmt = install_error_msg;
469 *errmsg_arg = program;
476 pwait (pid, status, flags)
481 /* ??? Here's an opportunity to canonicalize the values in STATUS.
483 int pid = wait (status);
491 /* MPW pexecute doesn't actually run anything; instead, it writes out
492 script commands that, when run, will do the actual executing.
494 For example, in GCC's case, GCC will write out several script commands:
501 and then exit. None of the above programs will have run yet. The task
502 that called GCC will then execute the script and cause cpp,etc. to run.
503 The caller must invoke pfinish before calling exit. This adds
504 the finishing touches to the generated script. */
506 static int first_time = 1;
509 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
512 const char *this_pname;
513 const char *temp_base;
514 char **errmsg_fmt, **errmsg_arg;
517 char tmpprogram[255];
521 mpwify_filename (program, tmpprogram);
524 printf ("Set Failed 0\n");
528 fputs ("If {Failed} == 0\n", stdout);
529 /* If being verbose, output a copy of the command. It should be
530 accurate enough and escaped enough to be "clickable". */
531 if (flags & PEXECUTE_VERBOSE)
533 fputs ("\tEcho ", stdout);
534 fputc ('\'', stdout);
535 fputs (tmpprogram, stdout);
536 fputc ('\'', stdout);
538 for (i=1; argv[i]; i++)
540 fputc ('\'', stdout);
541 /* See if we have an argument that needs fixing. */
542 if (strchr(argv[i], '/'))
544 tmpname = (char *) xmalloc (256);
545 mpwify_filename (argv[i], tmpname);
548 for (cp = argv[i]; *cp; cp++)
550 /* Write an Option-d escape char in front of special chars. */
551 if (strchr("'+", *cp))
552 fputc ('\266', stdout);
555 fputc ('\'', stdout);
558 fputs ("\n", stdout);
560 fputs ("\t", stdout);
561 fputs (tmpprogram, stdout);
564 for (i=1; argv[i]; i++)
566 /* See if we have an argument that needs fixing. */
567 if (strchr(argv[i], '/'))
569 tmpname = (char *) xmalloc (256);
570 mpwify_filename (argv[i], tmpname);
573 if (strchr (argv[i], ' '))
574 fputc ('\'', stdout);
575 for (cp = argv[i]; *cp; cp++)
577 /* Write an Option-d escape char in front of special chars. */
578 if (strchr("'+", *cp))
579 fputc ('\266', stdout);
582 if (strchr (argv[i], ' '))
583 fputc ('\'', stdout);
587 fputs ("\n", stdout);
589 /* Output commands that arrange to clean up and exit if a failure occurs.
590 We have to be careful to collect the status from the program that was
591 run, rather than some other script command. Also, we don't exit
592 immediately, since necessary cleanups are at the end of the script. */
593 fputs ("\tSet TmpStatus {Status}\n", stdout);
594 fputs ("\tIf {TmpStatus} != 0\n", stdout);
595 fputs ("\t\tSet Failed {TmpStatus}\n", stdout);
596 fputs ("\tEnd\n", stdout);
597 fputs ("End\n", stdout);
599 /* We're just composing a script, can't fail here. */
604 pwait (pid, status, flags)
613 /* Write out commands that will exit with the correct error code
614 if something in the script failed. */
619 printf ("\tExit \"{Failed}\"\n");
624 /* include for Unix-like environments but not for Dos-like environments */
625 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
626 && ! defined (_WIN32)
629 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
630 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
638 extern int execvp ();
640 extern char * my_strerror();
644 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
647 const char *this_pname;
648 const char *temp_base;
649 char **errmsg_fmt, **errmsg_arg;
652 int (*func)() = (flags & PEXECUTE_SEARCH ? execvp : execv);
655 int input_desc, output_desc;
656 int retries, sleep_interval;
657 /* Pipe waiting from last process, to be used as input for the next one.
658 Value is STDIN_FILE_NO if no pipe is waiting
659 (i.e. the next command is the first of a group). */
660 static int last_pipe_input;
662 /* If this is the first process, initialize. */
663 if (flags & PEXECUTE_FIRST)
664 last_pipe_input = STDIN_FILE_NO;
666 input_desc = last_pipe_input;
668 /* If this isn't the last process, make a pipe for its output,
669 and record it as waiting to be the input to the next process. */
670 if (! (flags & PEXECUTE_LAST))
674 *errmsg_fmt = "pipe";
678 output_desc = pdes[WRITE_PORT];
679 last_pipe_input = pdes[READ_PORT];
684 output_desc = STDOUT_FILE_NO;
685 last_pipe_input = STDIN_FILE_NO;
688 /* Fork a subprocess; wait and retry if it fails. */
690 for (retries = 0; retries < 4; retries++)
695 sleep (sleep_interval);
704 *errmsg_fmt = "fork";
706 *errmsg_fmt = "vfork";
713 /* Move the input and output pipes into place, if necessary. */
714 if (input_desc != STDIN_FILE_NO)
716 close (STDIN_FILE_NO);
720 if (output_desc != STDOUT_FILE_NO)
722 close (STDOUT_FILE_NO);
727 /* Close the parent's descs that aren't wanted here. */
728 if (last_pipe_input != STDIN_FILE_NO)
729 close (last_pipe_input);
731 /* Exec the program. */
732 (*func) (program, argv);
734 /* Note: Calling fprintf and exit here doesn't seem right for vfork. */
735 fprintf (stderr, "%s: ", this_pname);
736 fprintf (stderr, install_error_msg, program);
738 fprintf (stderr, ": %s\n", my_strerror (errno));
740 fprintf (stderr, ": %s\n", xstrerror (errno));
747 /* In the parent, after forking.
748 Close the descriptors that we made for this child. */
749 if (input_desc != STDIN_FILE_NO)
751 if (output_desc != STDOUT_FILE_NO)
754 /* Return child's process number. */
760 pwait (pid, status, flags)
765 /* ??? Here's an opportunity to canonicalize the values in STATUS.
768 pid = waitpid (-1, status, 0);
775 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */