]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/pex-msdos.c
Update the address and phone number of the FSF organization.
[thirdparty/gcc.git] / libiberty / pex-msdos.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 MSDOS specialization.
a584cf65 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 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#include <stdio.h>
25#include <errno.h>
26#ifdef NEED_DECLARATION_ERRNO
27extern int errno;
28#endif
29#ifdef HAVE_STRING_H
30#include <string.h>
31#endif
32#ifdef HAVE_STDLIB_H
33#include <stdlib.h>
34#endif
35
36#include "safe-ctype.h"
37#include <process.h>
38
a584cf65 39/* The structure we keep in obj->sysdep. */
55d0e5e0 40
a584cf65 41#define PEX_MSDOS_FILE_COUNT 3
55d0e5e0 42
a584cf65
ILT
43#define PEX_MSDOS_FD_OFFSET 10
44
45struct pex_msdos
55d0e5e0 46{
a584cf65
ILT
47 /* An array of file names. We refer to these using file descriptors
48 of 10 + array index. */
49 const char *files[PEX_MSDOS_FILE_COUNT];
50 /* Exit statuses of programs which have been run. */
51 int *statuses;
52};
53
54static int pex_msdos_open (struct pex_obj *, const char *, int);
55static int pex_msdos_open (struct pex_obj *, const char *, int);
56static int pex_msdos_fdindex (struct pex_msdos *, int);
57static long pex_msdos_exec_child (struct pex_obj *, int, const char *,
58 char * const *, int, int, int,
59 const char **, int *);
60static int pex_msdos_close (struct pex_obj *, int);
61static int pex_msdos_wait (struct pex_obj *, long, int *, struct pex_time *,
62 int, const char **, int *);
63static void pex_msdos_cleanup (struct pex_obj *);
64
65/* The list of functions we pass to the common routines. */
66
67const struct pex_funcs funcs =
68{
69 pex_msdos_open,
70 pex_msdos_open,
71 pex_msdos_exec_child,
72 pex_msdos_close,
73 pex_msdos_wait,
74 NULL, /* pipe */
75 NULL, /* fdopenr */
76 pex_msdos_cleanup
77};
78
79/* Return a newly initialized pex_obj structure. */
80
81struct pex_obj *
82pex_init (int flags, const char *pname, const char *tempbase)
83{
84 struct pex_obj *ret;
85 int i;
86
87 /* MSDOS does not support pipes. */
88 flags &= ~ PEX_USE_PIPES;
55d0e5e0 89
a584cf65 90 ret = pex_init_common (flags, pname, tempbase, funcs);
55d0e5e0 91
a584cf65
ILT
92 ret->sysdep = xmalloc (sizeof (struct pex_msdos));
93 for (i = 0; i < PEX_MSDOS_FILE_COUNT; ++i)
94 ret->files[i] = NULL;
95 ret->statuses = NULL;
96
97 return ret;
98}
99
100/* Open a file. FIXME: We ignore the binary argument, since we have
101 no way to handle it. */
102
103static int
104pex_msdos_open (struct pex_obj *obj, const char *name,
105 int binary ATTRIBUTE_UNUSED)
106{
107 struct pex_msdos *ms;
108 int i;
109
110 ms = (struct pex_msdos *) obj->sysdep;
111
112 for (i = 0; i < PEX_MSDOS_FILE_COUNT; ++i)
113 {
114 if (ms->files[i] == NULL)
115 {
116 ms->files[i] = xstrdup (name);
117 return i + PEX_MSDOS_FD_OFFSET;
118 }
119 }
120
121 abort ();
122}
123
124/* Get the index into msdos->files associated with an open file
125 descriptor. */
126
127static int
128pex_msdos_fdindex (struct pex_msdos *ms, int fd)
129{
130 fd -= PEX_MSDOS_FD_OFFSET;
131 if (fd < 0 || fd >= PEX_MSDOS_FILE_COUNT || ms->files[fd] == NULL)
55d0e5e0 132 abort ();
a584cf65
ILT
133 return fd;
134}
135
136
137/* Close a file. */
138
139static int
140pex_msdos_close (struct pex_obj *obj, int fd)
141{
142 struct pex_msdos *ms;
143 int fdinex;
144
145 ms = (struct pex_msdos *) obj->sysdep;
146 fdindex = pe_msdos_fdindex (ms, fd);
147 free (ms->files[fdindex]);
148 ms->files[fdindex] = NULL;
149}
150
151/* Execute a child. */
152
153static long
154pex_msdos_exec_child (struct pex_obj *obj, int flags, const char *executable,
155 char * const * argv, int in, int out,
156 int errdes ATTRIBUTE_UNUSED, const char **errmsg,
157 int *err)
158{
159 struct pex_msdos *ms;
160 char *temp_base;
161 int temp_base_allocated;
162 char *rf;
163 int inindex;
164 char *infile;
165 int outindex;
166 char *outfile;
167 char *scmd;
168 FILE *argfile;
169 int i;
170 int status;
171
172 ms = (struct pex_msdos *) obj->sysdep;
173
174 /* FIXME: I don't know how to redirect stderr, so we ignore ERRDES
175 and PEX_STDERR_TO_STDOUT. */
176
177 temp_base = obj->temp_base;
178 if (temp_base != NULL)
179 temp_base_allocated = 0;
180 else
181 {
182 temp_base = choose_temp_base ();
183 temp_base_allocated = 1;
184 }
185
186 rf = concat (temp_base, ".gp", NULL);
187
188 if (temp_base_allocated)
189 free (temp_base);
190
191 if (in == STDIN_FILE_NO)
192 {
193 inindex = -1;
194 infile = "";
195 }
196 else
197 {
198 inindex = pex_msdos_fdindex (ms, in);
199 infile = ms->files[inindex];
200 }
201
202 if (out == STDOUT_FILE_NO)
203 {
204 outindex = -1;
205 outfile = "";
206 }
207 else
208 {
209 outindex = pex_msdos_fdindex (ms, out);
210 outfile = ms->files[outindex];
211 }
212
213 scmd = xmalloc (strlen (program)
214 + ((flags & PEXECUTE_SEARCH) != 0 ? 4 : 0)
215 + strlen (rf)
216 + strlen (infile)
217 + strlen (outfile)
218 + 10);
219 sprintf (scmd, "%s%s @%s%s%s%s%s",
220 program,
221 (flags & PEXECUTE_SEARCH) != 0 ? ".exe" : "",
222 rf,
223 inindex != -1 ? " <" : "",
224 infile,
225 outindex != -1 ? " >" : "",
226 outfile);
55d0e5e0 227
55d0e5e0 228 argfile = fopen (rf, "w");
a584cf65 229 if (argfile == NULL)
55d0e5e0 230 {
a584cf65 231 *err = errno;
55d0e5e0 232 free (scmd);
a584cf65
ILT
233 free (rf);
234 *errmsg = "cannot open temporary command file";
55d0e5e0
ZW
235 return -1;
236 }
237
a584cf65 238 for (i = 1; argv[i] != NULL; ++i)
55d0e5e0 239 {
a584cf65
ILT
240 char *p;
241
242 for (p = argv[i]; *p != '\0'; ++p)
55d0e5e0 243 {
a584cf65
ILT
244 if (*p == '"' || *p == '\'' || *p == '\\' || ISSPACE (*p))
245 putc ('\\', argfile);
246 putc (*p, argfile);
55d0e5e0 247 }
a584cf65 248 putc ('\n', argfile);
55d0e5e0 249 }
55d0e5e0 250
a584cf65 251 fclose (argfile);
55d0e5e0 252
a584cf65 253 status = system (scmd);
55d0e5e0 254
a584cf65 255 if (status == -1)
55d0e5e0 256 {
a584cf65
ILT
257 *err = errno;
258 remove (rf);
259 free (scmd);
260 free (rf);
261 *errmsg = "system";
55d0e5e0
ZW
262 return -1;
263 }
264
a584cf65
ILT
265 remove (rf);
266 free (scmd);
267 free (rf);
268
269 /* Save the exit status for later. When we are called, obj->count
270 is the number of children which have executed before this
271 one. */
272 ms->statuses = xrealloc (ms->statuses, (obj->count + 1) * sizeof (int));
273 ms->statuses[obj->count] = status;
274
275 return obj->count;
55d0e5e0
ZW
276}
277
a584cf65
ILT
278/* Wait for a child process to complete. Actually the child process
279 has already completed, and we just need to return the exit
280 status. */
55d0e5e0 281
a584cf65
ILT
282static int
283pex_msdos_wait (struct pex_obj *obj, long pid, int *status,
284 struct pex_time *time, int done ATTRIBUTE_UNUSED,
285 const char **errmsg ATTRIBUTE_UNUSED,
286 int *err ATTRIBUTE_UNUSED)
55d0e5e0 287{
a584cf65
ILT
288 struct pex_msdos *ms;
289
290 ms = (struct pex_msdos *) obj->sysdep;
291
292 if (time != NULL)
293 memset (time, 0, sizeof *time);
294
295 *status = ms->statuses[pid];
296
297 return 0;
298}
299
300/* Clean up the pex_msdos structure. */
301
302static void
303pex_msdos_cleanup (struct pex_obj *obj)
304{
305 struct pex_msdos *ms;
306 int i;
307
308 ms = (struct pex_msdos *) obj->sysdep;
309 for (i = 0; i < PEX_MSDOS_FILE_COUNT; ++i)
310 if (msdos->files[i] != NULL)
311 free (msdos->files[i]);
312 if (msdos->statuses != NULL)
313 free (msdos->statuses);
314 free (msdos);
315 obj->sysdep = NULL;
55d0e5e0 316}