]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/pexecute.c
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 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. */
34 #include "gansidecl.h"
35 /* ??? Need to find a suitable header file. */
36 #define PEXECUTE_FIRST 1
37 #define PEXECUTE_LAST 2
38 #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST)
39 #define PEXECUTE_SEARCH 4
40 #define PEXECUTE_VERBOSE 8
42 #include "libiberty.h"
45 /* stdin file number. */
46 #define STDIN_FILE_NO 0
48 /* stdout file number. */
49 #define STDOUT_FILE_NO 1
51 /* value of `pipe': port index for reading. */
54 /* value of `pipe': port index for writing. */
57 static char *install_error_msg
= "installation problem, cannot exec `%s'";
59 /* pexecute: execute a program.
61 PROGRAM and ARGV are the arguments to execv/execvp.
63 THIS_PNAME is name of the calling program (i.e. argv[0]).
65 TEMP_BASE is the path name, sans suffix, of a temporary file to use
66 if needed. This is currently only needed for MSDOS ports that don't use
67 GO32 (do any still exist?). Ports that don't need it can pass NULL.
69 (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched
70 (??? It's not clear that GCC passes this flag correctly).
71 (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain.
72 (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain.
73 FIRST_LAST could be simplified to only mark the last of a chain of processes
74 but that requires the caller to always mark the last one (and not give up
75 early if some error occurs). It's more robust to require the caller to
76 mark both ends of the chain.
78 The result is the pid on systems like Unix where we fork/exec and on systems
79 like WIN32 and OS2 where we use spawn. It is up to the caller to wait for
82 The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
85 Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error
86 message with an optional argument (if not needed, ERRMSG_ARG is set to
87 NULL), and -1 is returned. `errno' is available to the caller to use.
89 pwait: cover function for wait.
91 PID is the process id of the task to wait for.
92 STATUS is the `status' argument to wait.
93 FLAGS is currently unused (allows future enhancement without breaking
94 upward compatibility). Pass 0 for now.
96 The result is the pid of the child reaped,
97 or -1 for failure (errno says why).
99 On systems that don't support waiting for a particular child, PID is
100 ignored. On systems like MSDOS that don't really multitask pwait
101 is just a mechanism to provide a consistent interface for the caller.
103 pfinish: finish generation of script
105 pfinish is necessary for systems like MPW where a script is generated that
106 runs the requested programs.
111 /* MSDOS doesn't multitask, but for the sake of a consistent interface
112 the code behaves like it does. pexecute runs the program, tucks the
113 exit code away, and returns a "pid". pwait must be called to fetch the
118 /* For communicating information from pexecute to pwait. */
119 static int last_pid
= 0;
120 static int last_status
= 0;
121 static int last_reaped
= 0;
124 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
127 const char *this_pname
;
128 const char *temp_base
;
129 char **errmsg_fmt
, **errmsg_arg
;
138 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
142 /* ??? What are the possible return values from spawnv? */
143 rc
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
147 int i
, el
= flags
& PEXECUTE_SEARCH
? 4 : 0;
149 scmd
= (char *) xmalloc (strlen (program
) + strlen (temp_base
) + 6 + el
);
150 rf
= scmd
+ strlen(program
) + 2 + el
;
151 sprintf (scmd
, "%s%s @%s.gp", program
,
152 (flags
& PEXECUTE_SEARCH
? ".exe" : ""), temp_base
);
153 argfile
= fopen (rf
, "w");
156 int errno_save
= errno
;
159 *errmsg_fmt
= "cannot open `%s.gp'";
160 *errmsg_arg
= temp_base
;
164 for (i
=1; argv
[i
]; i
++)
167 for (cp
= argv
[i
]; *cp
; cp
++)
169 if (*cp
== '"' || *cp
== '\'' || *cp
== '\\' || isspace (*cp
))
170 fputc ('\\', argfile
);
171 fputc (*cp
, argfile
);
173 fputc ('\n', argfile
);
180 int errno_save
= errno
;
189 *errmsg_fmt
= install_error_msg
;
190 *errmsg_arg
= program
;
194 /* Tuck the status away for pwait, and return a "pid". */
195 last_status
= rc
<< 8;
200 pwait (pid
, status
, flags
)
205 /* On MSDOS each pexecute must be followed by it's associated pwait. */
207 /* Called twice for the same child? */
208 || pid
== last_reaped
)
210 /* ??? ECHILD would be a better choice. Can we use it here? */
214 /* ??? Here's an opportunity to canonicalize the values in STATUS.
216 *status
= last_status
;
217 last_reaped
= last_pid
;
227 extern int _spawnv ();
228 extern int _spawnvp ();
232 #define fix_argv(argvec) (argvec)
236 /* This is a kludge to get around the Microsoft C spawn functions' propensity
237 to remove the outermost set of double quotes from all arguments. */
245 for (i
= 1; argvec
[i
] != 0; i
++)
248 char *temp
, *newtemp
;
252 for (j
= 0; j
< len
; j
++)
256 newtemp
= (char *) xmalloc (len
+ 2);
257 strncpy (newtemp
, temp
, j
);
259 strncpy (&newtemp
[j
+1], &temp
[j
], len
-j
);
270 return (const char * const *) argvec
;
273 #endif /* ! defined (__CYGWIN32__) */
276 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
279 const char *this_pname
;
280 const char *temp_base
;
281 char **errmsg_fmt
, **errmsg_arg
;
286 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
288 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
289 (_P_NOWAIT
, program
, fix_argv(argv
));
292 *errmsg_fmt
= install_error_msg
;
293 *errmsg_arg
= program
;
300 pwait (pid
, status
, flags
)
307 pid
= cwait (&termstat
, pid
, WAIT_CHILD
);
309 /* ??? Here's an opportunity to canonicalize the values in STATUS.
312 /* cwait returns the child process exit code in termstat.
313 A value of 3 indicates that the child caught a signal, but not
314 which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
319 *status
= (((termstat
) & 0xff) << 8);
328 /* ??? Does OS2 have process.h? */
329 extern int spawnv ();
330 extern int spawnvp ();
333 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
336 const char *this_pname
;
337 const char *temp_base
;
338 char **errmsg_fmt
, **errmsg_arg
;
343 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
345 /* ??? Presumably 1 == _P_NOWAIT. */
346 pid
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
349 *errmsg_fmt
= install_error_msg
;
350 *errmsg_arg
= program
;
357 pwait (pid
, status
, flags
)
362 /* ??? Here's an opportunity to canonicalize the values in STATUS.
364 int pid
= wait (status
);
372 /* MPW pexecute doesn't actually run anything; instead, it writes out
373 script commands that, when run, will do the actual executing.
375 For example, in GCC's case, GCC will write out several script commands:
382 and then exit. None of the above programs will have run yet. The task
383 that called GCC will then execute the script and cause cpp,etc. to run.
384 The caller must invoke pfinish before calling exit. This adds
385 the finishing touches to the generated script. */
387 static int first_time
= 1;
390 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
393 const char *this_pname
;
394 const char *temp_base
;
395 char **errmsg_fmt
, **errmsg_arg
;
398 char tmpprogram
[255];
402 mpwify_filename (program
, tmpprogram
);
405 printf ("Set Failed 0\n");
409 fputs ("If {Failed} == 0\n", stdout
);
410 /* If being verbose, output a copy of the command. It should be
411 accurate enough and escaped enough to be "clickable". */
412 if (flags
& PEXECUTE_VERBOSE
)
414 fputs ("\tEcho ", stdout
);
415 fputc ('\'', stdout
);
416 fputs (tmpprogram
, stdout
);
417 fputc ('\'', stdout
);
419 for (i
=1; argv
[i
]; i
++)
421 fputc ('\'', stdout
);
422 /* See if we have an argument that needs fixing. */
423 if (strchr(argv
[i
], '/'))
425 tmpname
= (char *) xmalloc (256);
426 mpwify_filename (argv
[i
], tmpname
);
429 for (cp
= argv
[i
]; *cp
; cp
++)
431 /* Write an Option-d escape char in front of special chars. */
432 if (strchr("'+", *cp
))
433 fputc ('\266', stdout
);
436 fputc ('\'', stdout
);
439 fputs ("\n", stdout
);
441 fputs ("\t", stdout
);
442 fputs (tmpprogram
, stdout
);
445 for (i
=1; argv
[i
]; i
++)
447 /* See if we have an argument that needs fixing. */
448 if (strchr(argv
[i
], '/'))
450 tmpname
= (char *) xmalloc (256);
451 mpwify_filename (argv
[i
], tmpname
);
454 if (strchr (argv
[i
], ' '))
455 fputc ('\'', stdout
);
456 for (cp
= argv
[i
]; *cp
; cp
++)
458 /* Write an Option-d escape char in front of special chars. */
459 if (strchr("'+", *cp
))
460 fputc ('\266', stdout
);
463 if (strchr (argv
[i
], ' '))
464 fputc ('\'', stdout
);
468 fputs ("\n", stdout
);
470 /* Output commands that arrange to clean up and exit if a failure occurs.
471 We have to be careful to collect the status from the program that was
472 run, rather than some other script command. Also, we don't exit
473 immediately, since necessary cleanups are at the end of the script. */
474 fputs ("\tSet TmpStatus {Status}\n", stdout
);
475 fputs ("\tIf {TmpStatus} != 0\n", stdout
);
476 fputs ("\t\tSet Failed {TmpStatus}\n", stdout
);
477 fputs ("\tEnd\n", stdout
);
478 fputs ("End\n", stdout
);
480 /* We're just composing a script, can't fail here. */
485 pwait (pid
, status
, flags
)
494 /* Write out commands that will exit with the correct error code
495 if something in the script failed. */
500 printf ("\tExit \"{Failed}\"\n");
505 /* include for Unix-like environments but not for Dos-like environments */
506 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
507 && ! defined (_WIN32)
510 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
511 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
519 extern int execvp ();
522 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
525 const char *this_pname
;
526 const char *temp_base
;
527 char **errmsg_fmt
, **errmsg_arg
;
530 int (*func
)() = (flags
& PEXECUTE_SEARCH
? execvp
: execv
);
533 int input_desc
, output_desc
;
534 int retries
, sleep_interval
;
535 /* Pipe waiting from last process, to be used as input for the next one.
536 Value is STDIN_FILE_NO if no pipe is waiting
537 (i.e. the next command is the first of a group). */
538 static int last_pipe_input
;
540 /* If this is the first process, initialize. */
541 if (flags
& PEXECUTE_FIRST
)
542 last_pipe_input
= STDIN_FILE_NO
;
544 input_desc
= last_pipe_input
;
546 /* If this isn't the last process, make a pipe for its output,
547 and record it as waiting to be the input to the next process. */
548 if (! (flags
& PEXECUTE_LAST
))
552 *errmsg_fmt
= "pipe";
556 output_desc
= pdes
[WRITE_PORT
];
557 last_pipe_input
= pdes
[READ_PORT
];
562 output_desc
= STDOUT_FILE_NO
;
563 last_pipe_input
= STDIN_FILE_NO
;
566 /* Fork a subprocess; wait and retry if it fails. */
568 for (retries
= 0; retries
< 4; retries
++)
573 sleep (sleep_interval
);
582 *errmsg_fmt
= "fork";
584 *errmsg_fmt
= "vfork";
591 /* Move the input and output pipes into place, if necessary. */
592 if (input_desc
!= STDIN_FILE_NO
)
594 close (STDIN_FILE_NO
);
598 if (output_desc
!= STDOUT_FILE_NO
)
600 close (STDOUT_FILE_NO
);
605 /* Close the parent's descs that aren't wanted here. */
606 if (last_pipe_input
!= STDIN_FILE_NO
)
607 close (last_pipe_input
);
609 /* Exec the program. */
610 (*func
) (program
, argv
);
612 /* Note: Calling fprintf and exit here doesn't seem right for vfork. */
613 fprintf (stderr
, "%s: ", this_pname
);
614 fprintf (stderr
, install_error_msg
, program
);
616 fprintf (stderr
, ": %s\n", my_strerror (errno
));
618 fprintf (stderr
, ": %s\n", xstrerror (errno
));
625 /* In the parent, after forking.
626 Close the descriptors that we made for this child. */
627 if (input_desc
!= STDIN_FILE_NO
)
629 if (output_desc
!= STDOUT_FILE_NO
)
632 /* Return child's process number. */
638 pwait (pid
, status
, flags
)
643 /* ??? Here's an opportunity to canonicalize the values in STATUS.
646 pid
= waitpid (-1, status
, 0);
653 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */