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.
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.
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.
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. */
22 #include "pex-common.h"
30 #ifdef HAVE_SYS_WAIT_H
39 /* mingw32 headers may not define the following. */
49 # define WAIT_GRANDCHILD 1
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. */
55 static const char * const *
60 char * command0
= argvec
[0];
62 /* Ensure that the executable pathname uses Win32 backslashes. */
63 for (; *command0
!= '\0'; command0
++)
67 for (i
= 1; argvec
[i
] != 0; i
++)
74 for (j
= 0; j
< len
; j
++)
78 newtemp
= xmalloc (len
+ 2);
79 strncpy (newtemp
, temp
, j
);
81 strncpy (&newtemp
[j
+1], &temp
[j
], len
-j
);
92 for (i
= 0; argvec
[i
] != 0; i
++)
94 if (strpbrk (argvec
[i
], " \t"))
96 int len
, trailing_backslash
;
99 len
= strlen (argvec
[i
]);
100 trailing_backslash
= 0;
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. */
110 && argvec
[i
][len
-1] == '\\'
111 && argvec
[i
][len
-2] != '\\')
113 trailing_backslash
= 1;
114 ++len
; /* to escape the final backslash. */
117 len
+= 2; /* and for the enclosing quotes. */
119 temp
= xmalloc (len
+ 1);
121 strcpy (temp
+ 1, argvec
[i
]);
122 if (trailing_backslash
)
131 return (const char * const *) argvec
;
134 /* Win32 supports pipes */
136 pexecute (program
, argv
, this_pname
, temp_base
, errmsg_fmt
, errmsg_arg
, flags
)
139 const char *this_pname
;
140 const char *temp_base
;
141 char **errmsg_fmt
, **errmsg_arg
;
145 int pdes
[2], org_stdin
, org_stdout
;
146 int input_desc
, output_desc
;
147 int retries
, sleep_interval
;
149 /* Pipe waiting from last process, to be used as input for the next one.
150 Value is STDIN_FILE_NO if no pipe is waiting
151 (i.e. the next command is the first of a group). */
152 static int last_pipe_input
;
154 /* If this is the first process, initialize. */
155 if (flags
& PEXECUTE_FIRST
)
156 last_pipe_input
= STDIN_FILE_NO
;
158 input_desc
= last_pipe_input
;
160 /* If this isn't the last process, make a pipe for its output,
161 and record it as waiting to be the input to the next process. */
162 if (! (flags
& PEXECUTE_LAST
))
164 if (_pipe (pdes
, 256, O_BINARY
) < 0)
166 *errmsg_fmt
= "pipe";
170 output_desc
= pdes
[WRITE_PORT
];
171 last_pipe_input
= pdes
[READ_PORT
];
176 output_desc
= STDOUT_FILE_NO
;
177 last_pipe_input
= STDIN_FILE_NO
;
180 if (input_desc
!= STDIN_FILE_NO
)
182 org_stdin
= dup (STDIN_FILE_NO
);
183 dup2 (input_desc
, STDIN_FILE_NO
);
187 if (output_desc
!= STDOUT_FILE_NO
)
189 org_stdout
= dup (STDOUT_FILE_NO
);
190 dup2 (output_desc
, STDOUT_FILE_NO
);
194 pid
= (flags
& PEXECUTE_SEARCH
? _spawnvp
: _spawnv
)
195 (_P_NOWAIT
, program
, fix_argv(argv
));
197 if (input_desc
!= STDIN_FILE_NO
)
199 dup2 (org_stdin
, STDIN_FILE_NO
);
203 if (output_desc
!= STDOUT_FILE_NO
)
205 dup2 (org_stdout
, STDOUT_FILE_NO
);
211 *errmsg_fmt
= install_error_msg
;
212 *errmsg_arg
= (char*) program
;
219 /* MS CRTDLL doesn't return enough information in status to decide if the
220 child exited due to a signal or not, rather it simply returns an
221 integer with the exit code of the child; eg., if the child exited with
222 an abort() call and didn't have a handler for SIGABRT, it simply returns
223 with status = 3. We fix the status code to conform to the usual WIF*
224 macros. Note that WIFSIGNALED will never be true under CRTDLL. */
227 pwait (pid
, status
, flags
)
234 pid
= _cwait (&termstat
, pid
, WAIT_CHILD
);
236 /* ??? Here's an opportunity to canonicalize the values in STATUS.
239 /* cwait returns the child process exit code in termstat.
240 A value of 3 indicates that the child caught a signal, but not
241 which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
246 *status
= (((termstat
) & 0xff) << 8);