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