]>
gcc.gnu.org Git - gcc.git/blob - libiberty/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. */
31 #include "gansidecl.h"
32 /* ??? Need to find a suitable header file. */
33 #define PEXECUTE_FIRST 1
34 #define PEXECUTE_LAST 2
35 #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST)
36 #define PEXECUTE_SEARCH 4
37 #define PEXECUTE_VERBOSE 8
39 #include "libiberty.h"
42 /* stdin file number. */
43 #define STDIN_FILE_NO 0
45 /* stdout file number. */
46 #define STDOUT_FILE_NO 1
48 /* value of `pipe': port index for reading. */
51 /* value of `pipe': port index for writing. */
54 static char *install_error_msg
= "installation problem, cannot exec `%s'";
56 /* pexecute: execute a program.
58 PROGRAM and ARGV are the arguments to execv/execvp.
60 THIS_PNAME is name of the calling program (i.e. argv[0]).
62 TEMP_BASE is the path name, sans suffix, of a temporary file to use
63 if needed. This is currently only needed for MSDOS ports that don't use
64 GO32 (do any still exist?). Ports that don't need it can pass NULL.
66 (FLAGS & PEXECUTE_SEARCH) is non-zero if $PATH should be searched
67 (??? It's not clear that GCC passes this flag correctly).
68 (FLAGS & PEXECUTE_FIRST) is nonzero for the first process in chain.
69 (FLAGS & PEXECUTE_FIRST) is nonzero for the last process in chain.
70 FIRST_LAST could be simplified to only mark the last of a chain of processes
71 but that requires the caller to always mark the last one (and not give up
72 early if some error occurs). It's more robust to require the caller to
73 mark both ends of the chain.
75 The result is the pid on systems like Unix where we fork/exec and on systems
76 like WIN32 and OS2 where we use spawn. It is up to the caller to wait for
79 The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
82 Upon failure, ERRMSG_FMT and ERRMSG_ARG are set to the text of the error
83 message with an optional argument (if not needed, ERRMSG_ARG is set to
84 NULL), and -1 is returned. `errno' is available to the caller to use.
86 pwait: cover function for wait.
88 PID is the process id of the task to wait for.
89 STATUS is the `status' argument to wait.
90 FLAGS is currently unused (allows future enhancement without breaking
91 upward compatibility). Pass 0 for now.
93 The result is the pid of the child reaped,
94 or -1 for failure (errno says why).
96 On systems that don't support waiting for a particular child, PID is
97 ignored. On systems like MSDOS that don't really multitask pwait
98 is just a mechanism to provide a consistent interface for the caller.
100 pfinish: finish generation of script
102 pfinish is necessary for systems like MPW where a script is generated that
103 runs the requested programs.
108 /* MSDOS doesn't multitask, but for the sake of a consistent interface
109 the code behaves like it does. pexecute runs the program, tucks the
110 exit code away, and returns a "pid". pwait must be called to fetch the
115 /* For communicating information from pexecute to pwait. */
116 static int last_pid
= 0;
117 static int last_status
= 0;
118 static int last_reaped
= 0;
121 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
124 const char *this_pname
;
125 const char *temp_base
;
126 char **errmsg_fmt
, **errmsg_arg
;
135 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
139 /* ??? What are the possible return values from spawnv? */
140 rc
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
144 int i
, el
= flags
& PEXECUTE_SEARCH
? 4 : 0;
146 scmd
= (char *) xmalloc (strlen (program
) + strlen (temp_base
) + 6 + el
);
147 rf
= scmd
+ strlen(program
) + 2 + el
;
148 sprintf (scmd
, "%s%s @%s.gp", program
,
149 (flags
& PEXECUTE_SEARCH
? ".exe" : ""), temp_base
);
150 argfile
= fopen (rf
, "w");
153 int errno_save
= errno
;
156 *errmsg_fmt
= "cannot open `%s.gp'";
157 *errmsg_arg
= temp_base
;
161 for (i
=1; argv
[i
]; i
++)
164 for (cp
= argv
[i
]; *cp
; cp
++)
166 if (*cp
== '"' || *cp
== '\'' || *cp
== '\\' || isspace (*cp
))
167 fputc ('\\', argfile
);
168 fputc (*cp
, argfile
);
170 fputc ('\n', argfile
);
177 int errno_save
= errno
;
186 *errmsg_fmt
= install_error_msg
;
187 *errmsg_arg
= program
;
191 /* Tuck the status away for pwait, and return a "pid". */
192 last_status
= rc
<< 8;
197 pwait (pid
, status
, flags
)
202 /* On MSDOS each pexecute must be followed by it's associated pwait. */
204 /* Called twice for the same child? */
205 || pid
== last_reaped
)
207 /* ??? ECHILD would be a better choice. Can we use it here? */
211 /* ??? Here's an opportunity to canonicalize the values in STATUS.
213 *status
= last_status
;
214 last_reaped
= last_pid
;
223 extern int _spawnv ();
224 extern int _spawnvp ();
227 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
230 const char *this_pname
;
231 const char *temp_base
;
232 char **errmsg_fmt
, **errmsg_arg
;
237 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
239 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
) (_P_NOWAIT
, program
, argv
);
242 *errmsg_fmt
= install_error_msg
;
243 *errmsg_arg
= program
;
250 pwait (pid
, status
, flags
)
255 /* ??? Here's an opportunity to canonicalize the values in STATUS.
257 return cwait (status
, pid
, WAIT_CHILD
);
264 /* ??? Does OS2 have process.h? */
265 extern int spawnv ();
266 extern int spawnvp ();
269 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
272 const char *this_pname
;
273 const char *temp_base
;
274 char **errmsg_fmt
, **errmsg_arg
;
279 if ((flags
& PEXECUTE_ONE
) != PEXECUTE_ONE
)
281 /* ??? Presumably 1 == _P_NOWAIT. */
282 pid
= (flags
& PEXECUTE_SEARCH
? spawnvp
: spawnv
) (1, program
, argv
);
285 *errmsg_fmt
= install_error_msg
;
286 *errmsg_arg
= program
;
293 pwait (pid
, status
, flags
)
298 /* ??? Here's an opportunity to canonicalize the values in STATUS.
300 int pid
= wait (status
);
308 /* MPW pexecute doesn't actually run anything; instead, it writes out
309 script commands that, when run, will do the actual executing.
311 For example, in GCC's case, GCC will write out several script commands:
318 and then exit. None of the above programs will have run yet. The task
319 that called GCC will then execute the script and cause cpp,etc. to run.
320 The caller must invoke pfinish before calling exit. This adds
321 the finishing touches to the generated script. */
323 static int first_time
= 1;
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
;
334 char tmpprogram
[255];
338 mpwify_filename (program
, tmpprogram
);
341 printf ("Set Failed 0\n");
345 fputs ("If {Failed} == 0\n", stdout
);
346 /* If being verbose, output a copy of the command. It should be
347 accurate enough and escaped enough to be "clickable". */
348 if (flags
& PEXECUTE_VERBOSE
)
350 fputs ("\tEcho ", stdout
);
351 fputc ('\'', stdout
);
352 fputs (tmpprogram
, stdout
);
353 fputc ('\'', stdout
);
355 for (i
=1; argv
[i
]; i
++)
357 /* We have to quote every arg, so that when the echo is
358 executed, the quotes are stripped and the original arg
360 fputc ('\'', stdout
);
361 for (cp
= argv
[i
]; *cp
; cp
++)
363 /* Write an Option-d esc char in front of special chars. */
364 if (strchr ("\"'+", *cp
))
365 fputc ('\266', stdout
);
368 fputc ('\'', stdout
);
371 fputs ("\n", stdout
);
373 fputs ("\t", stdout
);
374 fputs (tmpprogram
, stdout
);
377 for (i
=1; argv
[i
]; i
++)
379 if (strchr (argv
[i
], ' '))
380 fputc ('\'', stdout
);
381 for (cp
= argv
[i
]; *cp
; cp
++)
383 /* Write an Option-d esc char in front of special chars. */
384 if (strchr ("\"'+", *cp
))
386 fputc ('\266', stdout
);
390 if (strchr (argv
[i
], ' '))
391 fputc ('\'', stdout
);
395 fputs ("\n", stdout
);
397 /* Output commands that arrange to clean up and exit if a failure occurs.
398 We have to be careful to collect the status from the program that was
399 run, rather than some other script command. Also, we don't exit
400 immediately, since necessary cleanups are at the end of the script. */
401 fputs ("\tSet TmpStatus {Status}\n", stdout
);
402 fputs ("\tIf {TmpStatus} != 0\n", stdout
);
403 fputs ("\t\tSet Failed {TmpStatus}\n", stdout
);
404 fputs ("\tEnd\n", stdout
);
405 fputs ("End\n", stdout
);
407 /* We're just composing a script, can't fail here. */
412 pwait (pid
, status
, flags
)
421 /* Write out commands that will exit with the correct error code
422 if something in the script failed. */
427 printf ("\tExit \"{Failed}\"\n");
432 /* include for Unix-like environments but not for Dos-like environments */
433 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
434 && ! defined (_WIN32)
437 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
438 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
446 extern int execvp ();
449 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
452 const char *this_pname
;
453 const char *temp_base
;
454 char **errmsg_fmt
, **errmsg_arg
;
457 int (*func
)() = (flags
& PEXECUTE_SEARCH
? execvp
: execv
);
460 int input_desc
, output_desc
;
461 int retries
, sleep_interval
;
462 /* Pipe waiting from last process, to be used as input for the next one.
463 Value is STDIN_FILE_NO if no pipe is waiting
464 (i.e. the next command is the first of a group). */
465 static int last_pipe_input
;
467 /* If this is the first process, initialize. */
468 if (flags
& PEXECUTE_FIRST
)
469 last_pipe_input
= STDIN_FILE_NO
;
471 input_desc
= last_pipe_input
;
473 /* If this isn't the last process, make a pipe for its output,
474 and record it as waiting to be the input to the next process. */
475 if (! (flags
& PEXECUTE_LAST
))
479 *errmsg_fmt
= "pipe";
483 output_desc
= pdes
[WRITE_PORT
];
484 last_pipe_input
= pdes
[READ_PORT
];
489 output_desc
= STDOUT_FILE_NO
;
490 last_pipe_input
= STDIN_FILE_NO
;
493 /* Fork a subprocess; wait and retry if it fails. */
495 for (retries
= 0; retries
< 4; retries
++)
500 sleep (sleep_interval
);
509 *errmsg_fmt
= "fork";
511 *errmsg_fmt
= "vfork";
518 /* Move the input and output pipes into place, if necessary. */
519 if (input_desc
!= STDIN_FILE_NO
)
521 close (STDIN_FILE_NO
);
525 if (output_desc
!= STDOUT_FILE_NO
)
527 close (STDOUT_FILE_NO
);
532 /* Close the parent's descs that aren't wanted here. */
533 if (last_pipe_input
!= STDIN_FILE_NO
)
534 close (last_pipe_input
);
536 /* Exec the program. */
537 (*func
) (program
, argv
);
539 /* Note: Calling fprintf and exit here doesn't seem right for vfork. */
540 fprintf (stderr
, "%s: ", this_pname
);
541 fprintf (stderr
, install_error_msg
, program
);
543 fprintf (stderr
, ": %s\n", my_strerror (errno
));
545 fprintf (stderr
, ": %s\n", xstrerror (errno
));
552 /* In the parent, after forking.
553 Close the descriptors that we made for this child. */
554 if (input_desc
!= STDIN_FILE_NO
)
556 if (output_desc
!= STDOUT_FILE_NO
)
559 /* Return child's process number. */
565 pwait (pid
, status
, flags
)
570 /* ??? Here's an opportunity to canonicalize the values in STATUS.
573 pid
= waitpid (-1, status
, 0);
580 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */
This page took 0.064517 seconds and 6 git commands to generate.