* ropeimpl.h: Check __STL_PTHREADS instead of _PTHREADS.
[official-gcc.git] / gcc / pexecute.c
blob38b5874f27c9ed527915af6faf90e306f26a62b5
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. */
26 #ifdef IN_GCC
27 #include "config.h"
28 #endif
30 #include <stdio.h>
31 #include <errno.h>
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
36 #ifdef IN_GCC
37 #include "gansidecl.h"
38 /* ??? Need to find a suitable header file. */
39 #define PEXECUTE_FIRST 1
40 #define PEXECUTE_LAST 2
41 #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST)
42 #define PEXECUTE_SEARCH 4
43 #define PEXECUTE_VERBOSE 8
44 #else
45 #include "libiberty.h"
46 #endif
48 /* stdin file number. */
49 #define STDIN_FILE_NO 0
51 /* stdout file number. */
52 #define STDOUT_FILE_NO 1
54 /* value of `pipe': port index for reading. */
55 #define READ_PORT 0
57 /* value of `pipe': port index for writing. */
58 #define WRITE_PORT 1
60 static char *install_error_msg = "installation problem, cannot exec `%s'";
62 /* pexecute: execute a program.
64 PROGRAM and ARGV are the arguments to execv/execvp.
66 THIS_PNAME is name of the calling program (i.e. argv[0]).
68 TEMP_BASE is the path name, sans suffix, of a temporary file to use
69 if needed. This is currently only needed for MSDOS ports that don't use
70 GO32 (do any still exist?). Ports that don't need it can pass NULL.
72 (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched
73 (??? It's not clear that GCC passes this flag correctly).
74 (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain.
75 (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain.
76 FIRST_LAST could be simplified to only mark the last of a chain of processes
77 but that requires the caller to always mark the last one (and not give up
78 early if some error occurs). It's more robust to require the caller to
79 mark both ends of the chain.
81 The result is the pid on systems like Unix where we fork/exec and on systems
82 like WIN32 and OS2 where we use spawn. It is up to the caller to wait for
83 the child.
85 The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
86 for the child here.
88 Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error
89 message with an optional argument (if not needed, ERRMSG_ARG is set to
90 NULL), and -1 is returned. `errno' is available to the caller to use.
92 pwait: cover function for wait.
94 PID is the process id of the task to wait for.
95 STATUS is the `status' argument to wait.
96 FLAGS is currently unused (allows future enhancement without breaking
97 upward compatibility). Pass 0 for now.
99 The result is the pid of the child reaped,
100 or -1 for failure (errno says why).
102 On systems that don't support waiting for a particular child, PID is
103 ignored. On systems like MSDOS that don't really multitask pwait
104 is just a mechanism to provide a consistent interface for the caller.
106 pfinish: finish generation of script
108 pfinish is necessary for systems like MPW where a script is generated that
109 runs the requested programs.
112 #ifdef __MSDOS__
114 /* MSDOS doesn't multitask, but for the sake of a consistent interface
115 the code behaves like it does. pexecute runs the program, tucks the
116 exit code away, and returns a "pid". pwait must be called to fetch the
117 exit code. */
119 #include <process.h>
121 /* For communicating information from pexecute to pwait. */
122 static int last_pid = 0;
123 static int last_status = 0;
124 static int last_reaped = 0;
127 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
128 const char *program;
129 char * const *argv;
130 const char *this_pname;
131 const char *temp_base;
132 char **errmsg_fmt, **errmsg_arg;
133 int flags;
135 int rc;
137 last_pid++;
138 if (last_pid < 0)
139 last_pid = 1;
141 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
142 abort ();
144 #ifdef __GO32__
145 /* ??? What are the possible return values from spawnv? */
146 rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
147 #else
148 char *scmd, *rf;
149 FILE *argfile;
150 int i, el = flags & PEXECUTE_SEARCH ? 4 : 0;
152 scmd = (char *) xmalloc (strlen (program) + strlen (temp_base) + 6 + el);
153 rf = scmd + strlen(program) + 2 + el;
154 sprintf (scmd, "%s%s @%s.gp", program,
155 (flags & PEXECUTE_SEARCH ? ".exe" : ""), temp_base);
156 argfile = fopen (rf, "w");
157 if (argfile == 0)
159 int errno_save = errno;
160 free (scmd);
161 errno = errno_save;
162 *errmsg_fmt = "cannot open `%s.gp'";
163 *errmsg_arg = temp_base;
164 return -1;
167 for (i=1; argv[i]; i++)
169 char *cp;
170 for (cp = argv[i]; *cp; cp++)
172 if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
173 fputc ('\\', argfile);
174 fputc (*cp, argfile);
176 fputc ('\n', argfile);
178 fclose (argfile);
180 rc = system (scmd);
183 int errno_save = errno;
184 remove (rf);
185 free (scmd);
186 errno = errno_save;
188 #endif
190 if (rc == -1)
192 *errmsg_fmt = install_error_msg;
193 *errmsg_arg = program;
194 return -1;
197 /* Tuck the status away for pwait, and return a "pid". */
198 last_status = rc << 8;
199 return last_pid;
203 pwait (pid, status, flags)
204 int pid;
205 int *status;
206 int flags;
208 /* On MSDOS each pexecute must be followed by it's associated pwait. */
209 if (pid != last_pid
210 /* Called twice for the same child? */
211 || pid == last_reaped)
213 /* ??? ECHILD would be a better choice. Can we use it here? */
214 errno = EINVAL;
215 return -1;
217 /* ??? Here's an opportunity to canonicalize the values in STATUS.
218 Needed? */
219 *status = last_status;
220 last_reaped = last_pid;
221 return last_pid;
224 #endif /* MSDOS */
226 #if defined (_WIN32)
228 #include <process.h>
230 #ifdef __CYGWIN32__
232 #define fix_argv(argvec) (argvec)
234 extern int _spawnv ();
235 extern int _spawnvp ();
238 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
239 const char *program;
240 char * const *argv;
241 const char *this_pname;
242 const char *temp_base;
243 char **errmsg_fmt, **errmsg_arg;
244 int flags;
246 int pid;
248 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
249 abort ();
250 pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
251 (_P_NOWAIT, program, fix_argv(argv));
252 if (pid == -1)
254 *errmsg_fmt = install_error_msg;
255 *errmsg_arg = program;
256 return -1;
258 return pid;
262 pwait (pid, status, flags)
263 int pid;
264 int *status;
265 int flags;
267 /* ??? Here's an opportunity to canonicalize the values in STATUS.
268 Needed? */
269 return cwait (status, pid, WAIT_CHILD);
272 #else /* ! __CYGWIN32__ */
274 /* This is a kludge to get around the Microsoft C spawn functions' propensity
275 to remove the outermost set of double quotes from all arguments. */
277 const char * const *
278 fix_argv (argvec)
279 char **argvec;
281 int i;
283 for (i = 1; argvec[i] != 0; i++)
285 int len, j;
286 char *temp, *newtemp;
288 temp = argvec[i];
289 len = strlen (temp);
290 for (j = 0; j < len; j++)
292 if (temp[j] == '"')
294 newtemp = xmalloc (len + 2);
295 strncpy (newtemp, temp, j);
296 newtemp [j] = '\\';
297 strncpy (&newtemp [j+1], &temp [j], len-j);
298 newtemp [len+1] = 0;
299 temp = newtemp;
300 len++;
301 j++;
305 argvec[i] = temp;
308 return (const char * const *) argvec;
311 #include <io.h>
312 #include <fcntl.h>
313 #include <signal.h>
315 /* mingw32 headers may not define the following. */
317 #ifndef _P_WAIT
318 # define _P_WAIT 0
319 # define _P_NOWAIT 1
320 # define _P_OVERLAY 2
321 # define _P_NOWAITO 3
322 # define _P_DETACH 4
324 # define WAIT_CHILD 0
325 # define WAIT_GRANDCHILD 1
326 #endif
328 /* Win32 supports pipes */
330 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
331 const char *program;
332 char * const *argv;
333 const char *this_pname;
334 const char *temp_base;
335 char **errmsg_fmt, **errmsg_arg;
336 int flags;
338 int pid;
339 int pdes[2], org_stdin, org_stdout;
340 int input_desc, output_desc;
341 int retries, sleep_interval;
343 /* Pipe waiting from last process, to be used as input for the next one.
344 Value is STDIN_FILE_NO if no pipe is waiting
345 (i.e. the next command is the first of a group). */
346 static int last_pipe_input;
348 /* If this is the first process, initialize. */
349 if (flags & PEXECUTE_FIRST)
350 last_pipe_input = STDIN_FILE_NO;
352 input_desc = last_pipe_input;
354 /* If this isn't the last process, make a pipe for its output,
355 and record it as waiting to be the input to the next process. */
356 if (! (flags & PEXECUTE_LAST))
358 if (_pipe (pdes, 256, O_BINARY) < 0)
360 *errmsg_fmt = "pipe";
361 *errmsg_arg = NULL;
362 return -1;
364 output_desc = pdes[WRITE_PORT];
365 last_pipe_input = pdes[READ_PORT];
367 else
369 /* Last process. */
370 output_desc = STDOUT_FILE_NO;
371 last_pipe_input = STDIN_FILE_NO;
374 if (input_desc != STDIN_FILE_NO)
376 org_stdin = dup (STDIN_FILE_NO);
377 dup2 (input_desc, STDIN_FILE_NO);
378 close (input_desc);
381 if (output_desc != STDOUT_FILE_NO)
383 org_stdout = dup (STDOUT_FILE_NO);
384 dup2 (output_desc, STDOUT_FILE_NO);
385 close (output_desc);
388 pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
389 (_P_NOWAIT, program, fix_argv(argv));
391 if (input_desc != STDIN_FILE_NO)
393 dup2 (org_stdin, STDIN_FILE_NO);
394 close (org_stdin);
397 if (output_desc != STDOUT_FILE_NO)
399 dup2 (org_stdout, STDOUT_FILE_NO);
400 close (org_stdout);
403 if (pid == -1)
405 *errmsg_fmt = install_error_msg;
406 *errmsg_arg = program;
407 return -1;
410 return pid;
413 /* MS CRTDLL doesn't return enough information in status to decide if the
414 child exited due to a signal or not, rather it simply returns an
415 integer with the exit code of the child; eg., if the child exited with
416 an abort() call and didn't have a handler for SIGABRT, it simply returns
417 with status = 3. We fix the status code to conform to the usual WIF*
418 macros. Note that WIFSIGNALED will never be true under CRTDLL. */
421 pwait (pid, status, flags)
422 int pid;
423 int *status;
424 int flags;
426 int termstat;
428 pid = _cwait (&termstat, pid, WAIT_CHILD);
430 /* ??? Here's an opportunity to canonicalize the values in STATUS.
431 Needed? */
433 /* cwait returns the child process exit code in termstat.
434 A value of 3 indicates that the child caught a signal, but not
435 which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
436 report SIGABRT. */
437 if (termstat == 3)
438 *status = SIGABRT;
439 else
440 *status = (((termstat) & 0xff) << 8);
442 return pid;
445 #endif /* ! defined (__CYGWIN32__) */
447 #endif /* _WIN32 */
449 #ifdef OS2
451 /* ??? Does OS2 have process.h? */
452 extern int spawnv ();
453 extern int spawnvp ();
456 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
457 const char *program;
458 char * const *argv;
459 const char *this_pname;
460 const char *temp_base;
461 char **errmsg_fmt, **errmsg_arg;
462 int flags;
464 int pid;
466 if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
467 abort ();
468 /* ??? Presumably 1 == _P_NOWAIT. */
469 pid = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
470 if (pid == -1)
472 *errmsg_fmt = install_error_msg;
473 *errmsg_arg = program;
474 return -1;
476 return pid;
480 pwait (pid, status, flags)
481 int pid;
482 int *status;
483 int flags;
485 /* ??? Here's an opportunity to canonicalize the values in STATUS.
486 Needed? */
487 int pid = wait (status);
488 return pid;
491 #endif /* OS2 */
493 #ifdef MPW
495 /* MPW pexecute doesn't actually run anything; instead, it writes out
496 script commands that, when run, will do the actual executing.
498 For example, in GCC's case, GCC will write out several script commands:
500 cpp ...
501 cc1 ...
502 as ...
503 ld ...
505 and then exit. None of the above programs will have run yet. The task
506 that called GCC will then execute the script and cause cpp,etc. to run.
507 The caller must invoke pfinish before calling exit. This adds
508 the finishing touches to the generated script. */
510 static int first_time = 1;
513 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
514 const char *program;
515 char * const *argv;
516 const char *this_pname;
517 const char *temp_base;
518 char **errmsg_fmt, **errmsg_arg;
519 int flags;
521 char tmpprogram[255];
522 char *cp, *tmpname;
523 int i;
525 mpwify_filename (program, tmpprogram);
526 if (first_time)
528 printf ("Set Failed 0\n");
529 first_time = 0;
532 fputs ("If {Failed} == 0\n", stdout);
533 /* If being verbose, output a copy of the command. It should be
534 accurate enough and escaped enough to be "clickable". */
535 if (flags & PEXECUTE_VERBOSE)
537 fputs ("\tEcho ", stdout);
538 fputc ('\'', stdout);
539 fputs (tmpprogram, stdout);
540 fputc ('\'', stdout);
541 fputc (' ', stdout);
542 for (i=1; argv[i]; i++)
544 fputc ('\'', stdout);
545 /* See if we have an argument that needs fixing. */
546 if (strchr(argv[i], '/'))
548 tmpname = (char *) xmalloc (256);
549 mpwify_filename (argv[i], tmpname);
550 argv[i] = tmpname;
552 for (cp = argv[i]; *cp; cp++)
554 /* Write an Option-d escape char in front of special chars. */
555 if (strchr("'+", *cp))
556 fputc ('\266', stdout);
557 fputc (*cp, stdout);
559 fputc ('\'', stdout);
560 fputc (' ', stdout);
562 fputs ("\n", stdout);
564 fputs ("\t", stdout);
565 fputs (tmpprogram, stdout);
566 fputc (' ', stdout);
568 for (i=1; argv[i]; i++)
570 /* See if we have an argument that needs fixing. */
571 if (strchr(argv[i], '/'))
573 tmpname = (char *) xmalloc (256);
574 mpwify_filename (argv[i], tmpname);
575 argv[i] = tmpname;
577 if (strchr (argv[i], ' '))
578 fputc ('\'', stdout);
579 for (cp = argv[i]; *cp; cp++)
581 /* Write an Option-d escape char in front of special chars. */
582 if (strchr("'+", *cp))
583 fputc ('\266', stdout);
584 fputc (*cp, stdout);
586 if (strchr (argv[i], ' '))
587 fputc ('\'', stdout);
588 fputc (' ', stdout);
591 fputs ("\n", stdout);
593 /* Output commands that arrange to clean up and exit if a failure occurs.
594 We have to be careful to collect the status from the program that was
595 run, rather than some other script command. Also, we don't exit
596 immediately, since necessary cleanups are at the end of the script. */
597 fputs ("\tSet TmpStatus {Status}\n", stdout);
598 fputs ("\tIf {TmpStatus} != 0\n", stdout);
599 fputs ("\t\tSet Failed {TmpStatus}\n", stdout);
600 fputs ("\tEnd\n", stdout);
601 fputs ("End\n", stdout);
603 /* We're just composing a script, can't fail here. */
604 return 0;
608 pwait (pid, status, flags)
609 int pid;
610 int *status;
611 int flags;
613 *status = 0;
614 return 0;
617 /* Write out commands that will exit with the correct error code
618 if something in the script failed. */
620 void
621 pfinish ()
623 printf ("\tExit \"{Failed}\"\n");
626 #endif /* MPW */
628 /* include for Unix-like environments but not for Dos-like environments */
629 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
630 && ! defined (_WIN32)
632 #ifdef VMS
633 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
634 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
635 #else
636 #ifdef USG
637 #define vfork fork
638 #endif
639 #endif
641 extern int execv ();
642 extern int execvp ();
643 #ifdef IN_GCC
644 extern char * my_strerror();
645 #endif
648 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
649 const char *program;
650 char * const *argv;
651 const char *this_pname;
652 const char *temp_base;
653 char **errmsg_fmt, **errmsg_arg;
654 int flags;
656 int (*func)() = (flags & PEXECUTE_SEARCH ? execvp : execv);
657 int pid;
658 int pdes[2];
659 int input_desc, output_desc;
660 int retries, sleep_interval;
661 /* Pipe waiting from last process, to be used as input for the next one.
662 Value is STDIN_FILE_NO if no pipe is waiting
663 (i.e. the next command is the first of a group). */
664 static int last_pipe_input;
666 /* If this is the first process, initialize. */
667 if (flags & PEXECUTE_FIRST)
668 last_pipe_input = STDIN_FILE_NO;
670 input_desc = last_pipe_input;
672 /* If this isn't the last process, make a pipe for its output,
673 and record it as waiting to be the input to the next process. */
674 if (! (flags & PEXECUTE_LAST))
676 if (pipe (pdes) < 0)
678 *errmsg_fmt = "pipe";
679 *errmsg_arg = NULL;
680 return -1;
682 output_desc = pdes[WRITE_PORT];
683 last_pipe_input = pdes[READ_PORT];
685 else
687 /* Last process. */
688 output_desc = STDOUT_FILE_NO;
689 last_pipe_input = STDIN_FILE_NO;
692 /* Fork a subprocess; wait and retry if it fails. */
693 sleep_interval = 1;
694 for (retries = 0; retries < 4; retries++)
696 pid = vfork ();
697 if (pid >= 0)
698 break;
699 sleep (sleep_interval);
700 sleep_interval *= 2;
703 switch (pid)
705 case -1:
707 #ifdef vfork
708 *errmsg_fmt = "fork";
709 #else
710 *errmsg_fmt = "vfork";
711 #endif
712 *errmsg_arg = NULL;
713 return -1;
716 case 0: /* child */
717 /* Move the input and output pipes into place, if necessary. */
718 if (input_desc != STDIN_FILE_NO)
720 close (STDIN_FILE_NO);
721 dup (input_desc);
722 close (input_desc);
724 if (output_desc != STDOUT_FILE_NO)
726 close (STDOUT_FILE_NO);
727 dup (output_desc);
728 close (output_desc);
731 /* Close the parent's descs that aren't wanted here. */
732 if (last_pipe_input != STDIN_FILE_NO)
733 close (last_pipe_input);
735 /* Exec the program. */
736 (*func) (program, argv);
738 /* Note: Calling fprintf and exit here doesn't seem right for vfork. */
739 fprintf (stderr, "%s: ", this_pname);
740 fprintf (stderr, install_error_msg, program);
741 #ifdef IN_GCC
742 fprintf (stderr, ": %s\n", my_strerror (errno));
743 #else
744 fprintf (stderr, ": %s\n", xstrerror (errno));
745 #endif
746 exit (-1);
747 /* NOTREACHED */
748 return 0;
750 default:
751 /* In the parent, after forking.
752 Close the descriptors that we made for this child. */
753 if (input_desc != STDIN_FILE_NO)
754 close (input_desc);
755 if (output_desc != STDOUT_FILE_NO)
756 close (output_desc);
758 /* Return child's process number. */
759 return pid;
764 pwait (pid, status, flags)
765 int pid;
766 int *status;
767 int flags;
769 /* ??? Here's an opportunity to canonicalize the values in STATUS.
770 Needed? */
771 #ifdef VMS
772 pid = waitpid (-1, status, 0);
773 #else
774 pid = wait (status);
775 #endif
776 return pid;
779 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */