OSDN Git Service

Fix typo.
[pf3gnuchains/gcc-fork.git] / libiberty / pexecute.c
index ab5f392..34d2deb 100644 (file)
@@ -1,6 +1,6 @@
 /* Utilities to execute a program in a subprocess (possibly linked by pipes
    with other subprocesses), and wait for it.
-   Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+   Copyright (C) 1996-2000 Free Software Foundation, Inc.
 
 This file is part of the libiberty library.
 Libiberty is free software; you can redistribute it and/or
@@ -23,21 +23,30 @@ Boston, MA 02111-1307, USA.  */
 /* This file lives in at least two places: libiberty and gcc.
    Don't change one without the other.  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <stdio.h>
 #include <errno.h>
+#ifdef NEED_DECLARATION_ERRNO
+extern int errno;
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#define ISSPACE (x) isspace(x)
+#ifdef HAVE_SYS_WAIT_H
+#include <sys/wait.h>
+#endif
 
-#ifdef IN_GCC
-#include "config.h"
-#include "gansidecl.h"
-/* ??? Need to find a suitable header file.  */
-#define PEXECUTE_FIRST   1
-#define PEXECUTE_LAST    2
-#define PEXECUTE_ONE     (PEXECUTE_FIRST + PEXECUTE_LAST)
-#define PEXECUTE_SEARCH  4
-#define PEXECUTE_VERBOSE 8
-#else
 #include "libiberty.h"
-#endif
 
 /* stdin file number.  */
 #define STDIN_FILE_NO 0
@@ -135,14 +144,16 @@ pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
   if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
     abort ();
 
-#ifdef __GO32__
+#ifdef __DJGPP__
   /* ??? What are the possible return values from spawnv?  */
-  rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv);
+  rc = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (P_WAIT, program, argv);
 #else
   char *scmd, *rf;
   FILE *argfile;
   int i, el = flags & PEXECUTE_SEARCH ? 4 : 0;
 
+  if (temp_base == 0)
+    temp_base = choose_temp_base ();
   scmd = (char *) xmalloc (strlen (program) + strlen (temp_base) + 6 + el);
   rf = scmd + strlen(program) + 2 + el;
   sprintf (scmd, "%s%s @%s.gp", program,
@@ -163,7 +174,7 @@ pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
       char *cp;
       for (cp = argv[i]; *cp; cp++)
        {
-         if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
+         if (*cp == '"' || *cp == '\'' || *cp == '\\' || ISSPACE (*cp))
            fputc ('\\', argfile);
          fputc (*cp, argfile);
        }
@@ -184,7 +195,7 @@ pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
   if (rc == -1)
     {
       *errmsg_fmt = install_error_msg;
-      *errmsg_arg = program;
+      *errmsg_arg = (char *)program;
       return -1;
     }
 
@@ -193,6 +204,13 @@ pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
   return last_pid;
 }
 
+/* Use ECHILD if available, otherwise use EINVAL.  */
+#ifdef ECHILD
+#define PWAIT_ERROR ECHILD
+#else
+#define PWAIT_ERROR EINVAL
+#endif
+
 int
 pwait (pid, status, flags)
      int pid;
@@ -204,25 +222,130 @@ pwait (pid, status, flags)
       /* Called twice for the same child?  */
       || pid == last_reaped)
     {
-      /* ??? ECHILD would be a better choice.  Can we use it here?  */
-      errno = EINVAL;
+      errno = PWAIT_ERROR;
       return -1;
     }
   /* ??? Here's an opportunity to canonicalize the values in STATUS.
      Needed?  */
+#ifdef __DJGPP__
+  *status = (last_status >> 8);
+#else
   *status = last_status;
+#endif
   last_reaped = last_pid;
   return last_pid;
 }
 
 #endif /* MSDOS */
 
-#if defined (_WIN32)
+#if defined (_WIN32) && ! defined (_UWIN)
 
 #include <process.h>
+
+#ifdef __CYGWIN__
+
+#define fix_argv(argvec) (argvec)
+
 extern int _spawnv ();
 extern int _spawnvp ();
 
+#else /* ! __CYGWIN__ */
+
+/* This is a kludge to get around the Microsoft C spawn functions' propensity
+   to remove the outermost set of double quotes from all arguments.  */
+
+const char * const *
+fix_argv (argvec)
+     char **argvec;
+{
+  int i;
+
+  for (i = 1; argvec[i] != 0; i++)
+    {
+      int len, j;
+      char *temp, *newtemp;
+
+      temp = argvec[i];
+      len = strlen (temp);
+      for (j = 0; j < len; j++)
+        {
+          if (temp[j] == '"')
+            {
+              newtemp = xmalloc (len + 2);
+              strncpy (newtemp, temp, j);
+              newtemp [j] = '\\';
+              strncpy (&newtemp [j+1], &temp [j], len-j);
+              newtemp [len+1] = 0;
+              temp = newtemp;
+              len++;
+              j++;
+            }
+        }
+
+        argvec[i] = temp;
+      }
+
+  for (i = 0; argvec[i] != 0; i++)
+    {
+      if (strpbrk (argvec[i], " \t"))
+        {
+         int len, trailing_backslash;
+         char *temp;
+
+         len = strlen (argvec[i]);
+         trailing_backslash = 0;
+
+         /* There is an added complication when an arg with embedded white
+            space ends in a backslash (such as in the case of -iprefix arg
+            passed to cpp). The resulting quoted strings gets misinterpreted
+            by the command interpreter -- it thinks that the ending quote
+            is escaped by the trailing backslash and things get confused. 
+            We handle this case by escaping the trailing backslash, provided
+            it was not escaped in the first place.  */
+         if (len > 1 
+             && argvec[i][len-1] == '\\' 
+             && argvec[i][len-2] != '\\')
+           {
+             trailing_backslash = 1;
+             ++len;                    /* to escape the final backslash. */
+           }
+
+         len += 2;                     /* and for the enclosing quotes. */
+
+         temp = xmalloc (len + 1);
+         temp[0] = '"';
+         strcpy (temp + 1, argvec[i]);
+         if (trailing_backslash)
+           temp[len-2] = '\\';
+         temp[len-1] = '"';
+         temp[len] = '\0';
+
+         argvec[i] = temp;
+       }
+    }
+
+  return (const char * const *) argvec;
+}
+#endif /* __CYGWIN__ */
+
+#include <io.h>
+#include <fcntl.h>
+#include <signal.h>
+
+/* mingw32 headers may not define the following.  */
+
+#ifndef _P_WAIT
+#  define _P_WAIT      0
+#  define _P_NOWAIT    1
+#  define _P_OVERLAY   2
+#  define _P_NOWAITO   3
+#  define _P_DETACH    4
+
+#  define WAIT_CHILD   0
+#  define WAIT_GRANDCHILD      1
+#endif
+
+/* Win32 supports pipes */
 int
 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
      const char *program;
@@ -233,31 +356,117 @@ pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
      int flags;
 {
   int pid;
+  int pdes[2], org_stdin, org_stdout;
+  int input_desc, output_desc;
+  int retries, sleep_interval;
+
+  /* Pipe waiting from last process, to be used as input for the next one.
+     Value is STDIN_FILE_NO if no pipe is waiting
+     (i.e. the next command is the first of a group).  */
+  static int last_pipe_input;
+
+  /* If this is the first process, initialize.  */
+  if (flags & PEXECUTE_FIRST)
+    last_pipe_input = STDIN_FILE_NO;
+
+  input_desc = last_pipe_input;
+
+  /* If this isn't the last process, make a pipe for its output,
+     and record it as waiting to be the input to the next process.  */
+  if (! (flags & PEXECUTE_LAST))
+    {
+      if (_pipe (pdes, 256, O_BINARY) < 0)
+       {
+         *errmsg_fmt = "pipe";
+         *errmsg_arg = NULL;
+         return -1;
+       }
+      output_desc = pdes[WRITE_PORT];
+      last_pipe_input = pdes[READ_PORT];
+    }
+  else
+    {
+      /* Last process.  */
+      output_desc = STDOUT_FILE_NO;
+      last_pipe_input = STDIN_FILE_NO;
+    }
+
+  if (input_desc != STDIN_FILE_NO)
+    {
+      org_stdin = dup (STDIN_FILE_NO);
+      dup2 (input_desc, STDIN_FILE_NO);
+      close (input_desc); 
+    }
+
+  if (output_desc != STDOUT_FILE_NO)
+    {
+      org_stdout = dup (STDOUT_FILE_NO);
+      dup2 (output_desc, STDOUT_FILE_NO);
+      close (output_desc);
+    }
+
+  pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
+    (_P_NOWAIT, program, fix_argv(argv));
+
+  if (input_desc != STDIN_FILE_NO)
+    {
+      dup2 (org_stdin, STDIN_FILE_NO);
+      close (org_stdin);
+    }
+
+  if (output_desc != STDOUT_FILE_NO)
+    {
+      dup2 (org_stdout, STDOUT_FILE_NO);
+      close (org_stdout);
+    }
 
-  if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE)
-    abort ();
-  pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv) (_P_NOWAIT, program, argv);
   if (pid == -1)
     {
       *errmsg_fmt = install_error_msg;
       *errmsg_arg = program;
       return -1;
     }
+
   return pid;
 }
 
+/* MS CRTDLL doesn't return enough information in status to decide if the
+   child exited due to a signal or not, rather it simply returns an
+   integer with the exit code of the child; eg., if the child exited with 
+   an abort() call and didn't have a handler for SIGABRT, it simply returns
+   with status = 3. We fix the status code to conform to the usual WIF*
+   macros. Note that WIFSIGNALED will never be true under CRTDLL. */
+
 int
 pwait (pid, status, flags)
      int pid;
      int *status;
      int flags;
 {
+#ifdef __CYGWIN__
+  return wait (status);
+#else
+  int termstat;
+
+  pid = _cwait (&termstat, pid, WAIT_CHILD);
+
   /* ??? Here's an opportunity to canonicalize the values in STATUS.
      Needed?  */
-  return cwait (status, pid, WAIT_CHILD);
+
+  /* cwait returns the child process exit code in termstat.
+     A value of 3 indicates that the child caught a signal, but not
+     which one.  Since only SIGABRT, SIGFPE and SIGINT do anything, we
+     report SIGABRT.  */
+  if (termstat == 3)
+    *status = SIGABRT;
+  else
+    *status = (((termstat) & 0xff) << 8);
+
+  return pid;
+#endif /* __CYGWIN__ */
 }
 
-#endif /* _WIN32 */
+#endif /* _WIN32 && ! _UWIN */
 
 #ifdef OS2
 
@@ -325,7 +534,7 @@ static int first_time = 1;
 int
 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
      const char *program;
-     char **argv;
+     char * const *argv;
      const char *this_pname;
      const char *temp_base;
      char **errmsg_fmt, **errmsg_arg;
@@ -354,14 +563,18 @@ pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
       fputc (' ', stdout);
       for (i=1; argv[i]; i++)
        {
-         /* We have to quote every arg, so that when the echo is
-            executed, the quotes are stripped and the original arg
-            is left. */
          fputc ('\'', stdout);
+         /* See if we have an argument that needs fixing.  */
+         if (strchr(argv[i], '/'))
+           {
+             tmpname = (char *) xmalloc (256);
+             mpwify_filename (argv[i], tmpname);
+             argv[i] = tmpname;
+           }
          for (cp = argv[i]; *cp; cp++)
            {
-             /* Write an Option-d esc char in front of special chars.  */
-             if (strchr ("\"'+", *cp))
+             /* Write an Option-d escape char in front of special chars.  */
+             if (strchr("'+", *cp))
                fputc ('\266', stdout);
              fputc (*cp, stdout);
            }
@@ -376,15 +589,20 @@ pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
 
   for (i=1; argv[i]; i++)
     {
+      /* See if we have an argument that needs fixing.  */
+      if (strchr(argv[i], '/'))
+       {
+         tmpname = (char *) xmalloc (256);
+         mpwify_filename (argv[i], tmpname);
+         argv[i] = tmpname;
+       }
       if (strchr (argv[i], ' '))
        fputc ('\'', stdout);
       for (cp = argv[i]; *cp; cp++)
        {
-         /* Write an Option-d esc char in front of special chars.  */
-         if (strchr ("\"'+", *cp))
-           {
-             fputc ('\266', stdout);
-           }
+         /* Write an Option-d escape char in front of special chars.  */
+         if (strchr("'+", *cp))
+           fputc ('\266', stdout);
          fputc (*cp, stdout);
        }
       if (strchr (argv[i], ' '))
@@ -431,16 +649,7 @@ pfinish ()
 
 /* include for Unix-like environments but not for Dos-like environments */
 #if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \
-    && ! defined (_WIN32)
-
-#ifdef VMS
-#define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
-               lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
-#else
-#ifdef USG
-#define vfork fork
-#endif
-#endif
+    && ! (defined (_WIN32) && ! defined (_UWIN))
 
 extern int execv ();
 extern int execvp ();
@@ -450,7 +659,7 @@ pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
      const char *program;
      char * const *argv;
      const char *this_pname;
-     const char *temp_base;
+     const char *temp_base ATTRIBUTE_UNUSED;
      char **errmsg_fmt, **errmsg_arg;
      int flags;
 {
@@ -492,9 +701,10 @@ pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
 
   /* Fork a subprocess; wait and retry if it fails.  */
   sleep_interval = 1;
+  pid = -1;
   for (retries = 0; retries < 4; retries++)
     {
-      pid = vfork ();
+      pid = fork ();
       if (pid >= 0)
        break;
       sleep (sleep_interval);
@@ -504,15 +714,9 @@ pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
   switch (pid)
     {
     case -1:
-      {
-#ifdef vfork
-       *errmsg_fmt = "fork";
-#else
-       *errmsg_fmt = "vfork";
-#endif
-       *errmsg_arg = NULL;
-       return -1;
-      }
+      *errmsg_fmt = "fork";
+      *errmsg_arg = NULL;
+      return -1;
 
     case 0: /* child */
       /* Move the input and output pipes into place, if necessary.  */
@@ -536,14 +740,9 @@ pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
       /* Exec the program.  */
       (*func) (program, argv);
 
-      /* Note: Calling fprintf and exit here doesn't seem right for vfork.  */
       fprintf (stderr, "%s: ", this_pname);
       fprintf (stderr, install_error_msg, program);
-#ifdef IN_GCC
-      fprintf (stderr, ": %s\n", my_strerror (errno));
-#else
       fprintf (stderr, ": %s\n", xstrerror (errno));
-#endif
       exit (-1);
       /* NOTREACHED */
       return 0;
@@ -565,7 +764,7 @@ int
 pwait (pid, status, flags)
      int pid;
      int *status;
-     int flags;
+     int flags ATTRIBUTE_UNUSED;
 {
   /* ??? Here's an opportunity to canonicalize the values in STATUS.
      Needed?  */
@@ -577,4 +776,4 @@ pwait (pid, status, flags)
   return pid;
 }
 
-#endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */
+#endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! (_WIN32 && ! _UWIN) */