OSDN Git Service

* class.c (layout_class_type): Make DECL_MODE match TYPE_MODE for
[pf3gnuchains/gcc-fork.git] / libiberty / pex-win32.c
1 /* Utilities to execute a program in a subprocess (possibly linked by pipes
2    with other subprocesses), and wait for it.  Generic Win32 specialization.
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003
4    Free Software Foundation, Inc.
5
6 This file is part of the libiberty library.
7 Libiberty is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 Libiberty is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public
18 License along with libiberty; see the file COPYING.LIB.  If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "pex-common.h"
23
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #endif
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #ifdef HAVE_SYS_WAIT_H
31 #include <sys/wait.h>
32 #endif
33
34 #include <process.h>
35 #include <io.h>
36 #include <fcntl.h>
37 #include <signal.h>
38
39 /* mingw32 headers may not define the following.  */
40
41 #ifndef _P_WAIT
42 #  define _P_WAIT       0
43 #  define _P_NOWAIT     1
44 #  define _P_OVERLAY    2
45 #  define _P_NOWAITO    3
46 #  define _P_DETACH     4
47
48 #  define WAIT_CHILD            0
49 #  define WAIT_GRANDCHILD       1
50 #endif
51
52 /* This is a kludge to get around the Microsoft C spawn functions' propensity
53    to remove the outermost set of double quotes from all arguments.  */
54
55 static const char * const *
56 fix_argv (argvec)
57      char **argvec;
58 {
59   int i;
60   char * command0 = argvec[0];
61
62   /* Ensure that the executable pathname uses Win32 backslashes.  */
63   for (; *command0 != '\0'; command0++)
64     if (*command0 == '/')
65       *command0 = '\\';
66  
67   for (i = 1; argvec[i] != 0; i++)
68     {
69       int len, j;
70       char *temp, *newtemp;
71
72       temp = argvec[i];
73       len = strlen (temp);
74       for (j = 0; j < len; j++)
75         {
76           if (temp[j] == '"')
77             {
78               newtemp = xmalloc (len + 2);
79               strncpy (newtemp, temp, j);
80               newtemp [j] = '\\';
81               strncpy (&newtemp [j+1], &temp [j], len-j);
82               newtemp [len+1] = 0;
83               temp = newtemp;
84               len++;
85               j++;
86             }
87         }
88
89         argvec[i] = temp;
90       }
91
92   for (i = 0; argvec[i] != 0; i++)
93     {
94       if (strpbrk (argvec[i], " \t"))
95         {
96           int len, trailing_backslash;
97           char *temp;
98
99           len = strlen (argvec[i]);
100           trailing_backslash = 0;
101
102           /* There is an added complication when an arg with embedded white
103              space ends in a backslash (such as in the case of -iprefix arg
104              passed to cpp). The resulting quoted strings gets misinterpreted
105              by the command interpreter -- it thinks that the ending quote
106              is escaped by the trailing backslash and things get confused. 
107              We handle this case by escaping the trailing backslash, provided
108              it was not escaped in the first place.  */
109           if (len > 1 
110               && argvec[i][len-1] == '\\' 
111               && argvec[i][len-2] != '\\')
112             {
113               trailing_backslash = 1;
114               ++len;                    /* to escape the final backslash. */
115             }
116
117           len += 2;                     /* and for the enclosing quotes. */
118
119           temp = xmalloc (len + 1);
120           temp[0] = '"';
121           strcpy (temp + 1, argvec[i]);
122           if (trailing_backslash)
123             temp[len-2] = '\\';
124           temp[len-1] = '"';
125           temp[len] = '\0';
126
127           argvec[i] = temp;
128         }
129     }
130
131   return (const char * const *) argvec;
132 }
133
134 /* Win32 supports pipes */
135 int
136 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
137      const char *program;
138      char * const *argv;
139      const char *this_pname ATTRIBUTE_UNUSED;
140      const char *temp_base ATTRIBUTE_UNUSED;
141      char **errmsg_fmt, **errmsg_arg;
142      int flags;
143 {
144   int pid;
145   int pdes[2];
146   int org_stdin = -1;
147   int org_stdout = -1;
148   int input_desc, output_desc;
149
150   /* Pipe waiting from last process, to be used as input for the next one.
151      Value is STDIN_FILE_NO if no pipe is waiting
152      (i.e. the next command is the first of a group).  */
153   static int last_pipe_input;
154
155   /* If this is the first process, initialize.  */
156   if (flags & PEXECUTE_FIRST)
157     last_pipe_input = STDIN_FILE_NO;
158
159   input_desc = last_pipe_input;
160
161   /* If this isn't the last process, make a pipe for its output,
162      and record it as waiting to be the input to the next process.  */
163   if (! (flags & PEXECUTE_LAST))
164     {
165       if (_pipe (pdes, 256, O_BINARY) < 0)
166         {
167           *errmsg_fmt = "pipe";
168           *errmsg_arg = NULL;
169           return -1;
170         }
171       output_desc = pdes[WRITE_PORT];
172       last_pipe_input = pdes[READ_PORT];
173     }
174   else
175     {
176       /* Last process.  */
177       output_desc = STDOUT_FILE_NO;
178       last_pipe_input = STDIN_FILE_NO;
179     }
180
181   if (input_desc != STDIN_FILE_NO)
182     {
183       org_stdin = dup (STDIN_FILE_NO);
184       dup2 (input_desc, STDIN_FILE_NO);
185       close (input_desc); 
186     }
187
188   if (output_desc != STDOUT_FILE_NO)
189     {
190       org_stdout = dup (STDOUT_FILE_NO);
191       dup2 (output_desc, STDOUT_FILE_NO);
192       close (output_desc);
193     }
194
195   pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
196     (_P_NOWAIT, program, fix_argv(argv));
197
198   if (input_desc != STDIN_FILE_NO)
199     {
200       dup2 (org_stdin, STDIN_FILE_NO);
201       close (org_stdin);
202     }
203
204   if (output_desc != STDOUT_FILE_NO)
205     {
206       dup2 (org_stdout, STDOUT_FILE_NO);
207       close (org_stdout);
208     }
209
210   if (pid == -1)
211     {
212       *errmsg_fmt = install_error_msg;
213       *errmsg_arg = (char*) program;
214       return -1;
215     }
216
217   return pid;
218 }
219
220 /* MS CRTDLL doesn't return enough information in status to decide if the
221    child exited due to a signal or not, rather it simply returns an
222    integer with the exit code of the child; eg., if the child exited with 
223    an abort() call and didn't have a handler for SIGABRT, it simply returns
224    with status = 3. We fix the status code to conform to the usual WIF*
225    macros. Note that WIFSIGNALED will never be true under CRTDLL. */
226
227 int
228 pwait (pid, status, flags)
229      int pid;
230      int *status;
231      int flags ATTRIBUTE_UNUSED;
232 {
233   int termstat;
234
235   pid = _cwait (&termstat, pid, WAIT_CHILD);
236
237   /* ??? Here's an opportunity to canonicalize the values in STATUS.
238      Needed?  */
239
240   /* cwait returns the child process exit code in termstat.
241      A value of 3 indicates that the child caught a signal, but not
242      which one.  Since only SIGABRT, SIGFPE and SIGINT do anything, we
243      report SIGABRT.  */
244   if (termstat == 3)
245     *status = SIGABRT;
246   else
247     *status = (((termstat) & 0xff) << 8);
248
249   return pid;
250 }