OSDN Git Service

(emit_library_call): Don't pass VOIDmode to type_for_mode.
[pf3gnuchains/gcc-fork.git] / 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 Free Software Foundation, Inc.
4
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.
10
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.
15
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.  */
20
21 /* This file exports two functions: pexecute and pwait.  */
22
23 /* This file lives in at least two places: libiberty and gcc.
24    Don't change one without the other.  */
25
26 #include <stdio.h>
27 #include <errno.h>
28
29 #ifdef IN_GCC
30 #include "config.h"
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
38 #else
39 #include "libiberty.h"
40 #endif
41
42 /* stdin file number.  */
43 #define STDIN_FILE_NO 0
44
45 /* stdout file number.  */
46 #define STDOUT_FILE_NO 1
47
48 /* value of `pipe': port index for reading.  */
49 #define READ_PORT 0
50
51 /* value of `pipe': port index for writing.  */
52 #define WRITE_PORT 1
53
54 static char *install_error_msg = "installation problem, cannot exec `%s'";
55
56 /* pexecute: execute a program.
57
58    PROGRAM and ARGV are the arguments to execv/execvp.
59
60    THIS_PNAME is name of the calling program (i.e. argv[0]).
61
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.
65
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.
74
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
77    the child.
78
79    The result is the WEXITSTATUS on systems like MSDOS where we spawn and wait
80    for the child here.
81
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.
85
86    pwait: cover function for wait.
87
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.
92
93    The result is the pid of the child reaped,
94    or -1 for failure (errno says why).
95
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.
99
100    pfinish: finish generation of script
101
102    pfinish is necessary for systems like MPW where a script is generated that
103    runs the requested programs.
104 */
105
106 #ifdef __MSDOS__
107
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
111    exit code.  */
112
113 #include <process.h>
114
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;
119
120 int
121 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
122      const char *program;
123      char * const *argv;
124      const char *this_pname;
125      const char *temp_base;
126      char **errmsg_fmt, **errmsg_arg;
127      int flags;
128 {
129   int rc;
130
131   last_pid++;
132   if (last_pid < 0)
133     last_pid = 1;
134
135   if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
136     abort ();
137
138 #ifdef __GO32__
139   /* ??? What are the possible return values from spawnv?  */
140   rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
141 #else
142   char *scmd, *rf;
143   FILE *argfile;
144   int i, el = flags & PEXECUTE_SEARCH ? 4 : 0;
145
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");
151   if (argfile == 0)
152     {
153       int errno_save = errno;
154       free (scmd);
155       errno = errno_save;
156       *errmsg_fmt = "cannot open `%s.gp'";
157       *errmsg_arg = temp_base;
158       return -1;
159     }
160
161   for (i=1; argv[i]; i++)
162     {
163       char *cp;
164       for (cp = argv[i]; *cp; cp++)
165         {
166           if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
167             fputc ('\\', argfile);
168           fputc (*cp, argfile);
169         }
170       fputc ('\n', argfile);
171     }
172   fclose (argfile);
173
174   rc = system (scmd);
175
176   {
177     int errno_save = errno;
178     remove (rf);
179     free (scmd);
180     errno = errno_save;
181   }
182 #endif
183
184   if (rc == -1)
185     {
186       *errmsg_fmt = install_error_msg;
187       *errmsg_arg = program;
188       return -1;
189     }
190
191   /* Tuck the status away for pwait, and return a "pid".  */
192   last_status = rc << 8;
193   return last_pid;
194 }
195
196 int
197 pwait (pid, status, flags)
198      int pid;
199      int *status;
200      int flags;
201 {
202   /* On MSDOS each pexecute must be followed by it's associated pwait.  */
203   if (pid != last_pid
204       /* Called twice for the same child?  */
205       || pid == last_reaped)
206     {
207       /* ??? ECHILD would be a better choice.  Can we use it here?  */
208       errno = EINVAL;
209       return -1;
210     }
211   /* ??? Here's an opportunity to canonicalize the values in STATUS.
212      Needed?  */
213   *status = last_status;
214   last_reaped = last_pid;
215   return last_pid;
216 }
217
218 #endif /* MSDOS */
219
220 #if defined (_WIN32) && !defined (__CYGWIN32__)
221
222 #include <process.h>
223 /* ??? Why are these __spawnv{,p} and not _spawnv{,p}?  */
224 extern int __spawnv ();
225 extern int __spawnvp ();
226
227 int
228 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
229      const char *program;
230      char * const *argv;
231      const char *this_pname;
232      const char *temp_base;
233      char **errmsg_fmt, **errmsg_arg;
234      int flags;
235 {
236   int pid;
237
238   if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
239     abort ();
240   pid = (flags & PEXECUTE_SEARCH ? __spawnvp : __spawnv) (_P_NOWAIT, program, argv);
241   if (pid == -1)
242     {
243       *errmsg_fmt = install_error_msg;
244       *errmsg_arg = program;
245       return -1;
246     }
247   return pid;
248 }
249
250 int
251 pwait (pid, status, flags)
252      int pid;
253      int *status;
254      int flags;
255 {
256   /* ??? Here's an opportunity to canonicalize the values in STATUS.
257      Needed?  */
258   int pid = cwait (status, pid, WAIT_CHILD);
259   return pid;
260 }
261
262 #endif /* WIN32 */
263
264 #ifdef OS2
265
266 /* ??? Does OS2 have process.h?  */
267 extern int spawnv ();
268 extern int spawnvp ();
269
270 int
271 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
272      const char *program;
273      char * const *argv;
274      const char *this_pname;
275      const char *temp_base;
276      char **errmsg_fmt, **errmsg_arg;
277      int flags;
278 {
279   int pid;
280
281   if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
282     abort ();
283   /* ??? Presumably 1 == _P_NOWAIT.  */
284   pid = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
285   if (pid == -1)
286     {
287       *errmsg_fmt = install_error_msg;
288       *errmsg_arg = program;
289       return -1;
290     }
291   return pid;
292 }
293
294 int
295 pwait (pid, status, flags)
296      int pid;
297      int *status;
298      int flags;
299 {
300   /* ??? Here's an opportunity to canonicalize the values in STATUS.
301      Needed?  */
302   int pid = wait (status);
303   return pid;
304 }
305
306 #endif /* OS2 */
307
308 #ifdef MPW
309
310 /* MPW pexecute doesn't actually run anything; instead, it writes out
311    script commands that, when run, will do the actual executing.
312
313    For example, in GCC's case, GCC will write out several script commands:
314
315    cpp ...
316    cc1 ...
317    as ...
318    ld ...
319
320    and then exit.  None of the above programs will have run yet.  The task
321    that called GCC will then execute the script and cause cpp,etc. to run.
322    The caller must invoke pfinish before calling exit.  This adds
323    the finishing touches to the generated script.  */
324
325 static int first_time = 1;
326
327 int
328 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
329      const char *program;
330      char * const *argv;
331      const char *this_pname;
332      const char *temp_base;
333      char **errmsg_fmt, **errmsg_arg;
334      int flags;
335 {
336   char tmpprogram[255];
337   char *cp, *tmpname;
338   int i;
339
340   mpwify_filename (program, tmpprogram);
341   if (first_time)
342     {
343       printf ("Set Failed 0\n");
344       first_time = 0;
345     }
346
347   fputs ("If {Failed} == 0\n", stdout);
348   /* If being verbose, output a copy of the command.  It should be
349      accurate enough and escaped enough to be "clickable".  */
350   if (flags & PEXECUTE_VERBOSE)
351     {
352       fputs ("\tEcho ", stdout);
353       fputc ('\'', stdout);
354       fputs (tmpprogram, stdout);
355       fputc ('\'', stdout);
356       fputc (' ', stdout);
357       for (i=1; argv[i]; i++)
358         {
359           fputc ('\'', stdout);
360           /* See if we have an argument that needs fixing.  */
361           if (strchr(argv[i], '/'))
362             {
363               tmpname = xmalloc (256);
364               mpwify_filename (argv[i], tmpname);
365               argv[i] = tmpname;
366             }
367           for (cp = argv[i]; *cp; cp++)
368             {
369               /* Write an Option-d escape char in front of special chars.  */
370               if (strchr("'+", *cp))
371                 fputc ('\266', stdout);
372               fputc (*cp, stdout);
373             }
374           fputc ('\'', stdout);
375           fputc (' ', stdout);
376         }
377       fputs ("\n", stdout);
378     }
379   fputs ("\t", stdout);
380   fputs (tmpprogram, stdout);
381   fputc (' ', stdout);
382
383   for (i=1; argv[i]; i++)
384     {
385       /* See if we have an argument that needs fixing.  */
386       if (strchr(argv[i], '/'))
387         {
388           tmpname = xmalloc (256);
389           mpwify_filename (argv[i], tmpname);
390           argv[i] = tmpname;
391         }
392       if (strchr (argv[i], ' '))
393         fputc ('\'', stdout);
394       for (cp = argv[i]; *cp; cp++)
395         {
396           /* Write an Option-d escape char in front of special chars.  */
397           if (strchr("'+", *cp))
398             fputc ('\266', stdout);
399           fputc (*cp, stdout);
400         }
401       if (strchr (argv[i], ' '))
402         fputc ('\'', stdout);
403       fputc (' ', stdout);
404     }
405
406   fputs ("\n", stdout);
407
408   /* Output commands that arrange to clean up and exit if a failure occurs.
409      We have to be careful to collect the status from the program that was
410      run, rather than some other script command.  Also, we don't exit
411      immediately, since necessary cleanups are at the end of the script.  */
412   fputs ("\tSet TmpStatus {Status}\n", stdout);
413   fputs ("\tIf {TmpStatus} != 0\n", stdout);
414   fputs ("\t\tSet Failed {TmpStatus}\n", stdout);
415   fputs ("\tEnd\n", stdout);
416   fputs ("End\n", stdout);
417
418   /* We're just composing a script, can't fail here.  */
419   return 0;
420 }
421
422 int
423 pwait (pid, status, flags)
424      int pid;
425      int *status;
426      int flags;
427 {
428   *status = 0;
429   return 0;
430 }
431
432 /* Write out commands that will exit with the correct error code
433    if something in the script failed.  */
434
435 void
436 pfinish ()
437 {
438   printf ("\tExit \"{Failed}\"\n");
439 }
440
441 #endif /* MPW */
442
443 /* include for Unix-like environments but not for Dos-like environments */
444 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
445     && (defined (__CYGWIN32__) || ! defined (_WIN32))
446
447 #ifdef VMS
448 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
449                lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
450 #else
451 #ifdef USG
452 #define vfork fork
453 #endif
454 #endif
455
456 extern int execv ();
457 extern int execvp ();
458
459 int
460 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
461      const char *program;
462      char * const *argv;
463      const char *this_pname;
464      const char *temp_base;
465      char **errmsg_fmt, **errmsg_arg;
466      int flags;
467 {
468   int (*func)() = (flags & PEXECUTE_SEARCH ? execvp : execv);
469   int pid;
470   int pdes[2];
471   int input_desc, output_desc;
472   int retries, sleep_interval;
473   /* Pipe waiting from last process, to be used as input for the next one.
474      Value is STDIN_FILE_NO if no pipe is waiting
475      (i.e. the next command is the first of a group).  */
476   static int last_pipe_input;
477
478   /* If this is the first process, initialize.  */
479   if (flags & PEXECUTE_FIRST)
480     last_pipe_input = STDIN_FILE_NO;
481
482   input_desc = last_pipe_input;
483
484   /* If this isn't the last process, make a pipe for its output,
485      and record it as waiting to be the input to the next process.  */
486   if (! (flags & PEXECUTE_LAST))
487     {
488       if (pipe (pdes) < 0)
489         {
490           *errmsg_fmt = "pipe";
491           *errmsg_arg = NULL;
492           return -1;
493         }
494       output_desc = pdes[WRITE_PORT];
495       last_pipe_input = pdes[READ_PORT];
496     }
497   else
498     {
499       /* Last process.  */
500       output_desc = STDOUT_FILE_NO;
501       last_pipe_input = STDIN_FILE_NO;
502     }
503
504   /* Fork a subprocess; wait and retry if it fails.  */
505   sleep_interval = 1;
506   for (retries = 0; retries < 4; retries++)
507     {
508       pid = vfork ();
509       if (pid >= 0)
510         break;
511       sleep (sleep_interval);
512       sleep_interval *= 2;
513     }
514
515   switch (pid)
516     {
517     case -1:
518       {
519 #ifdef vfork
520         *errmsg_fmt = "fork";
521 #else
522         *errmsg_fmt = "vfork";
523 #endif
524         *errmsg_arg = NULL;
525         return -1;
526       }
527
528     case 0: /* child */
529       /* Move the input and output pipes into place, if necessary.  */
530       if (input_desc != STDIN_FILE_NO)
531         {
532           close (STDIN_FILE_NO);
533           dup (input_desc);
534           close (input_desc);
535         }
536       if (output_desc != STDOUT_FILE_NO)
537         {
538           close (STDOUT_FILE_NO);
539           dup (output_desc);
540           close (output_desc);
541         }
542
543       /* Close the parent's descs that aren't wanted here.  */
544       if (last_pipe_input != STDIN_FILE_NO)
545         close (last_pipe_input);
546
547       /* Exec the program.  */
548       (*func) (program, argv);
549
550       /* Note: Calling fprintf and exit here doesn't seem right for vfork.  */
551       fprintf (stderr, "%s: ", this_pname);
552       fprintf (stderr, install_error_msg, program);
553 #ifdef IN_GCC
554       fprintf (stderr, ": %s\n", my_strerror (errno));
555 #else
556       fprintf (stderr, ": %s\n", xstrerror (errno));
557 #endif
558       exit (-1);
559       /* NOTREACHED */
560       return 0;
561
562     default:
563       /* In the parent, after forking.
564          Close the descriptors that we made for this child.  */
565       if (input_desc != STDIN_FILE_NO)
566         close (input_desc);
567       if (output_desc != STDOUT_FILE_NO)
568         close (output_desc);
569
570       /* Return child's process number.  */
571       return pid;
572     }
573 }
574
575 int
576 pwait (pid, status, flags)
577      int pid;
578      int *status;
579      int flags;
580 {
581   /* ??? Here's an opportunity to canonicalize the values in STATUS.
582      Needed?  */
583 #ifdef VMS
584   pid = waitpid (-1, status, 0);
585 #else
586   pid = wait (status);
587 #endif
588   return pid;
589 }
590
591 #endif /* ! __MSDOS__ && ! OS2 && ! MPW && (__CYGWIN32___ || ! _WIN32) */