1 | /* Job execution and handling for GNU Make.
|
---|
2 | Copyright (C) 1988,89,90,91,92,93,94,95,96,97,99 Free Software Foundation, Inc.
|
---|
3 | This file is part of GNU Make.
|
---|
4 |
|
---|
5 | GNU Make is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation; either version 2, or (at your option)
|
---|
8 | any later version.
|
---|
9 |
|
---|
10 | GNU Make is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with GNU Make; see the file COPYING. If not, write to
|
---|
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
---|
18 | Boston, MA 02111-1307, USA. */
|
---|
19 |
|
---|
20 | #include "make.h"
|
---|
21 |
|
---|
22 | #include <assert.h>
|
---|
23 |
|
---|
24 | #include "job.h"
|
---|
25 | #include "debug.h"
|
---|
26 | #include "filedef.h"
|
---|
27 | #include "commands.h"
|
---|
28 | #include "variable.h"
|
---|
29 | #include "debug.h"
|
---|
30 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
---|
31 | #include "kmkbuiltin.h"
|
---|
32 | #endif
|
---|
33 |
|
---|
34 |
|
---|
35 | #include <string.h>
|
---|
36 |
|
---|
37 | #ifdef MAKE_DLLSHELL
|
---|
38 | #include <dlfcn.h>
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | /* Default shell to use. */
|
---|
42 | #ifdef WINDOWS32
|
---|
43 |
|
---|
44 | char *default_shell = "sh.exe";
|
---|
45 | int no_default_sh_exe = 1;
|
---|
46 | int batch_mode_shell = 1;
|
---|
47 |
|
---|
48 | #elif defined (_AMIGA)
|
---|
49 |
|
---|
50 | char default_shell[] = "";
|
---|
51 | extern int MyExecute (char **);
|
---|
52 | int batch_mode_shell = 0;
|
---|
53 |
|
---|
54 | #elif defined (__MSDOS__)
|
---|
55 |
|
---|
56 | /* The default shell is a pointer so we can change it if Makefile
|
---|
57 | says so. It is without an explicit path so we get a chance
|
---|
58 | to search the $PATH for it (since MSDOS doesn't have standard
|
---|
59 | directories we could trust). */
|
---|
60 | char *default_shell = "command.com";
|
---|
61 | int batch_mode_shell = 0;
|
---|
62 |
|
---|
63 | #elif defined (__EMX__)
|
---|
64 |
|
---|
65 | char *default_shell = "sh.exe";
|
---|
66 | int batch_mode_shell = 0;
|
---|
67 |
|
---|
68 | #elif defined (VMS)
|
---|
69 |
|
---|
70 | # include <descrip.h>
|
---|
71 | char default_shell[] = "";
|
---|
72 | int batch_mode_shell = 0;
|
---|
73 |
|
---|
74 | #else
|
---|
75 |
|
---|
76 | char default_shell[] = "/bin/sh";
|
---|
77 | int batch_mode_shell = 0;
|
---|
78 |
|
---|
79 | #endif
|
---|
80 |
|
---|
81 | #ifdef __MSDOS__
|
---|
82 | # include <process.h>
|
---|
83 | static int execute_by_shell;
|
---|
84 | static int dos_pid = 123;
|
---|
85 | int dos_status;
|
---|
86 | int dos_command_running;
|
---|
87 | #endif /* __MSDOS__ */
|
---|
88 |
|
---|
89 | #ifdef _AMIGA
|
---|
90 | # include <proto/dos.h>
|
---|
91 | static int amiga_pid = 123;
|
---|
92 | static int amiga_status;
|
---|
93 | static char amiga_bname[32];
|
---|
94 | static int amiga_batch_file;
|
---|
95 | #endif /* Amiga. */
|
---|
96 |
|
---|
97 | #ifdef VMS
|
---|
98 | # ifndef __GNUC__
|
---|
99 | # include <processes.h>
|
---|
100 | # endif
|
---|
101 | # include <starlet.h>
|
---|
102 | # include <lib$routines.h>
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | #ifdef WINDOWS32
|
---|
106 | # include <windows.h>
|
---|
107 | # include <io.h>
|
---|
108 | # include <process.h>
|
---|
109 | # include "sub_proc.h"
|
---|
110 | # include "w32err.h"
|
---|
111 | # include "pathstuff.h"
|
---|
112 | #endif /* WINDOWS32 */
|
---|
113 |
|
---|
114 | #ifdef __EMX__
|
---|
115 | # include <process.h>
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
|
---|
119 | # include <sys/wait.h>
|
---|
120 | #endif
|
---|
121 |
|
---|
122 | #ifdef HAVE_WAITPID
|
---|
123 | # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
|
---|
124 | #else /* Don't have waitpid. */
|
---|
125 | # ifdef HAVE_WAIT3
|
---|
126 | # ifndef wait3
|
---|
127 | extern int wait3 ();
|
---|
128 | # endif
|
---|
129 | # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
|
---|
130 | # endif /* Have wait3. */
|
---|
131 | #endif /* Have waitpid. */
|
---|
132 |
|
---|
133 | #if !defined (wait) && !defined (POSIX)
|
---|
134 | extern int wait ();
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | #ifndef HAVE_UNION_WAIT
|
---|
138 |
|
---|
139 | # define WAIT_T int
|
---|
140 |
|
---|
141 | # ifndef WTERMSIG
|
---|
142 | # define WTERMSIG(x) ((x) & 0x7f)
|
---|
143 | # endif
|
---|
144 | # ifndef WCOREDUMP
|
---|
145 | # define WCOREDUMP(x) ((x) & 0x80)
|
---|
146 | # endif
|
---|
147 | # ifndef WEXITSTATUS
|
---|
148 | # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
|
---|
149 | # endif
|
---|
150 | # ifndef WIFSIGNALED
|
---|
151 | # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
|
---|
152 | # endif
|
---|
153 | # ifndef WIFEXITED
|
---|
154 | # define WIFEXITED(x) (WTERMSIG (x) == 0)
|
---|
155 | # endif
|
---|
156 |
|
---|
157 | #else /* Have `union wait'. */
|
---|
158 |
|
---|
159 | # define WAIT_T union wait
|
---|
160 | # ifndef WTERMSIG
|
---|
161 | # define WTERMSIG(x) ((x).w_termsig)
|
---|
162 | # endif
|
---|
163 | # ifndef WCOREDUMP
|
---|
164 | # define WCOREDUMP(x) ((x).w_coredump)
|
---|
165 | # endif
|
---|
166 | # ifndef WEXITSTATUS
|
---|
167 | # define WEXITSTATUS(x) ((x).w_retcode)
|
---|
168 | # endif
|
---|
169 | # ifndef WIFSIGNALED
|
---|
170 | # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
|
---|
171 | # endif
|
---|
172 | # ifndef WIFEXITED
|
---|
173 | # define WIFEXITED(x) (WTERMSIG(x) == 0)
|
---|
174 | # endif
|
---|
175 |
|
---|
176 | #endif /* Don't have `union wait'. */
|
---|
177 |
|
---|
178 | #ifdef VMS
|
---|
179 | static int vms_jobsefnmask = 0;
|
---|
180 | #endif /* !VMS */
|
---|
181 |
|
---|
182 | #ifndef HAVE_UNISTD_H
|
---|
183 | extern int dup2 ();
|
---|
184 | extern int execve ();
|
---|
185 | extern void _exit ();
|
---|
186 | # ifndef VMS
|
---|
187 | extern int geteuid ();
|
---|
188 | extern int getegid ();
|
---|
189 | extern int setgid ();
|
---|
190 | extern int getgid ();
|
---|
191 | # endif
|
---|
192 | #endif
|
---|
193 |
|
---|
194 | extern char *allocated_variable_expand_for_file PARAMS ((char *line, struct file *file));
|
---|
195 |
|
---|
196 | extern int getloadavg PARAMS ((double loadavg[], int nelem));
|
---|
197 | extern int start_remote_job PARAMS ((char **argv, char **envp, int stdin_fd,
|
---|
198 | int *is_remote, int *id_ptr, int *used_stdin));
|
---|
199 | extern int start_remote_job_p PARAMS ((int));
|
---|
200 | extern int remote_status PARAMS ((int *exit_code_ptr, int *signal_ptr,
|
---|
201 | int *coredump_ptr, int block));
|
---|
202 |
|
---|
203 | RETSIGTYPE child_handler PARAMS ((int));
|
---|
204 | static void free_child PARAMS ((struct child *));
|
---|
205 | static void start_job_command PARAMS ((struct child *child));
|
---|
206 | static int load_too_high PARAMS ((void));
|
---|
207 | static int job_next_command PARAMS ((struct child *));
|
---|
208 | static int start_waiting_job PARAMS ((struct child *));
|
---|
209 | #ifdef VMS
|
---|
210 | static void vmsWaitForChildren PARAMS ((int *));
|
---|
211 | #endif
|
---|
212 | #ifdef MAKE_DLLSHELL
|
---|
213 | static int spawn_command PARAMS ((char **argv, char **envp, struct child *child));
|
---|
214 | #endif
|
---|
215 | |
---|
216 |
|
---|
217 | /* Chain of all live (or recently deceased) children. */
|
---|
218 |
|
---|
219 | struct child *children = 0;
|
---|
220 |
|
---|
221 | /* Number of children currently running. */
|
---|
222 |
|
---|
223 | unsigned int job_slots_used = 0;
|
---|
224 |
|
---|
225 | /* Nonzero if the `good' standard input is in use. */
|
---|
226 |
|
---|
227 | static int good_stdin_used = 0;
|
---|
228 |
|
---|
229 | /* Chain of children waiting to run until the load average goes down. */
|
---|
230 |
|
---|
231 | static struct child *waiting_jobs = 0;
|
---|
232 |
|
---|
233 | /* Non-zero if we use a *real* shell (always so on Unix). */
|
---|
234 |
|
---|
235 | int unixy_shell = 1;
|
---|
236 |
|
---|
237 | /* Number of jobs started in the current second. */
|
---|
238 |
|
---|
239 | unsigned long job_counter = 0;
|
---|
240 |
|
---|
241 | |
---|
242 |
|
---|
243 | #ifdef WINDOWS32
|
---|
244 | /*
|
---|
245 | * The macro which references this function is defined in make.h.
|
---|
246 | */
|
---|
247 | int
|
---|
248 | w32_kill(int pid, int sig)
|
---|
249 | {
|
---|
250 | return ((process_kill(pid, sig) == TRUE) ? 0 : -1);
|
---|
251 | }
|
---|
252 | #endif /* WINDOWS32 */
|
---|
253 |
|
---|
254 | #ifdef __EMX__
|
---|
255 | /* returns whether path is assumed to be a unix like shell. */
|
---|
256 | int
|
---|
257 | _is_unixy_shell (const char *path)
|
---|
258 | {
|
---|
259 | /* list of non unix shells */
|
---|
260 | const char *known_os2shells[] = {
|
---|
261 | "cmd.exe",
|
---|
262 | "cmd",
|
---|
263 | "4os2.exe",
|
---|
264 | "4os2",
|
---|
265 | "4dos.exe",
|
---|
266 | "4dos",
|
---|
267 | "command.com",
|
---|
268 | "command",
|
---|
269 | NULL
|
---|
270 | };
|
---|
271 |
|
---|
272 | /* find the rightmost '/' or '\\' */
|
---|
273 | const char *name = strrchr (path, '/');
|
---|
274 | const char *p = strrchr (path, '\\');
|
---|
275 | unsigned i;
|
---|
276 |
|
---|
277 | if (name && p) /* take the max */
|
---|
278 | name = (name > p) ? name : p;
|
---|
279 | else if (p) /* name must be 0 */
|
---|
280 | name = p;
|
---|
281 | else if (!name) /* name and p must be 0 */
|
---|
282 | name = path;
|
---|
283 |
|
---|
284 | if (*name == '/' || *name == '\\') name++;
|
---|
285 |
|
---|
286 | i = 0;
|
---|
287 | while (known_os2shells[i] != NULL) {
|
---|
288 | if (stricmp (name, known_os2shells[i]) == 0) /* strcasecmp() */
|
---|
289 | return 0; /* not a unix shell */
|
---|
290 | i++;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /* in doubt assume a unix like shell */
|
---|
294 | return 1;
|
---|
295 | }
|
---|
296 | #endif /* __EMX__ */
|
---|
297 |
|
---|
298 | |
---|
299 |
|
---|
300 | /* Write an error message describing the exit status given in
|
---|
301 | EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
|
---|
302 | Append "(ignored)" if IGNORED is nonzero. */
|
---|
303 |
|
---|
304 | static void
|
---|
305 | child_error (char *target_name, int exit_code, int exit_sig, int coredump,
|
---|
306 | int ignored)
|
---|
307 | {
|
---|
308 | if (ignored && silent_flag)
|
---|
309 | return;
|
---|
310 |
|
---|
311 | #ifdef VMS
|
---|
312 | if (!(exit_code & 1))
|
---|
313 | error (NILF,
|
---|
314 | (ignored ? _("*** [%s] Error 0x%x (ignored)")
|
---|
315 | : _("*** [%s] Error 0x%x")),
|
---|
316 | target_name, exit_code);
|
---|
317 | #else
|
---|
318 | if (exit_sig == 0)
|
---|
319 | error (NILF, ignored ? _("[%s] Error %d (ignored)") :
|
---|
320 | _("*** [%s] Error %d"),
|
---|
321 | target_name, exit_code);
|
---|
322 | else
|
---|
323 | error (NILF, "*** [%s] %s%s",
|
---|
324 | target_name, strsignal (exit_sig),
|
---|
325 | coredump ? _(" (core dumped)") : "");
|
---|
326 | #endif /* VMS */
|
---|
327 | }
|
---|
328 | |
---|
329 |
|
---|
330 | #ifdef VMS
|
---|
331 | /* Wait for nchildren children to terminate */
|
---|
332 | static void
|
---|
333 | vmsWaitForChildren(int *status)
|
---|
334 | {
|
---|
335 | while (1)
|
---|
336 | {
|
---|
337 | if (!vms_jobsefnmask)
|
---|
338 | {
|
---|
339 | *status = 0;
|
---|
340 | return;
|
---|
341 | }
|
---|
342 |
|
---|
343 | *status = sys$wflor (32, vms_jobsefnmask);
|
---|
344 | }
|
---|
345 | return;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /* Set up IO redirection. */
|
---|
349 |
|
---|
350 | char *
|
---|
351 | vms_redirect (struct dsc$descriptor_s *desc, char *fname, char *ibuf)
|
---|
352 | {
|
---|
353 | char *fptr;
|
---|
354 | extern char *vmsify ();
|
---|
355 |
|
---|
356 | ibuf++;
|
---|
357 | while (isspace ((unsigned char)*ibuf))
|
---|
358 | ibuf++;
|
---|
359 | fptr = ibuf;
|
---|
360 | while (*ibuf && !isspace ((unsigned char)*ibuf))
|
---|
361 | ibuf++;
|
---|
362 | *ibuf = 0;
|
---|
363 | if (strcmp (fptr, "/dev/null") != 0)
|
---|
364 | {
|
---|
365 | strcpy (fname, vmsify (fptr, 0));
|
---|
366 | if (strchr (fname, '.') == 0)
|
---|
367 | strcat (fname, ".");
|
---|
368 | }
|
---|
369 | desc->dsc$w_length = strlen(fname);
|
---|
370 | desc->dsc$a_pointer = fname;
|
---|
371 | desc->dsc$b_dtype = DSC$K_DTYPE_T;
|
---|
372 | desc->dsc$b_class = DSC$K_CLASS_S;
|
---|
373 |
|
---|
374 | if (*fname == 0)
|
---|
375 | printf (_("Warning: Empty redirection\n"));
|
---|
376 | return ibuf;
|
---|
377 | }
|
---|
378 |
|
---|
379 |
|
---|
380 | /* found apostrophe at (p-1)
|
---|
381 | inc p until after closing apostrophe.
|
---|
382 | */
|
---|
383 |
|
---|
384 | static char *
|
---|
385 | vms_handle_apos (char *p)
|
---|
386 | {
|
---|
387 | int alast;
|
---|
388 |
|
---|
389 | #define SEPCHARS ",/()= "
|
---|
390 |
|
---|
391 | alast = 0;
|
---|
392 |
|
---|
393 | while (*p != 0)
|
---|
394 | {
|
---|
395 | if (*p == '"')
|
---|
396 | {
|
---|
397 | if (alast)
|
---|
398 | {
|
---|
399 | alast = 0;
|
---|
400 | p++;
|
---|
401 | }
|
---|
402 | else
|
---|
403 | {
|
---|
404 | p++;
|
---|
405 | if (strchr (SEPCHARS, *p))
|
---|
406 | break;
|
---|
407 | alast = 1;
|
---|
408 | }
|
---|
409 | }
|
---|
410 | else
|
---|
411 | p++;
|
---|
412 | }
|
---|
413 |
|
---|
414 | return p;
|
---|
415 | }
|
---|
416 |
|
---|
417 | #endif
|
---|
418 |
|
---|
419 |
|
---|
420 | /* Handle a dead child. This handler may or may not ever be installed.
|
---|
421 |
|
---|
422 | If we're using the jobserver feature, we need it. First, installing it
|
---|
423 | ensures the read will interrupt on SIGCHLD. Second, we close the dup'd
|
---|
424 | read FD to ensure we don't enter another blocking read without reaping all
|
---|
425 | the dead children. In this case we don't need the dead_children count.
|
---|
426 |
|
---|
427 | If we don't have either waitpid or wait3, then make is unreliable, but we
|
---|
428 | use the dead_children count to reap children as best we can. */
|
---|
429 |
|
---|
430 | static unsigned int dead_children = 0;
|
---|
431 |
|
---|
432 | RETSIGTYPE
|
---|
433 | child_handler (int sig UNUSED)
|
---|
434 | {
|
---|
435 | ++dead_children;
|
---|
436 |
|
---|
437 | if (job_rfd >= 0)
|
---|
438 | {
|
---|
439 | close (job_rfd);
|
---|
440 | job_rfd = -1;
|
---|
441 | }
|
---|
442 |
|
---|
443 | #if defined __EMX__ && !defined(__INNOTEK_LIBC__)
|
---|
444 | /* The signal handler must called only once! */
|
---|
445 | signal (SIGCHLD, SIG_DFL);
|
---|
446 | #endif
|
---|
447 |
|
---|
448 | /* This causes problems if the SIGCHLD interrupts a printf().
|
---|
449 | DB (DB_JOBS, (_("Got a SIGCHLD; %u unreaped children.\n"), dead_children));
|
---|
450 | */
|
---|
451 | }
|
---|
452 |
|
---|
453 | extern int shell_function_pid, shell_function_completed;
|
---|
454 |
|
---|
455 | /* Reap all dead children, storing the returned status and the new command
|
---|
456 | state (`cs_finished') in the `file' member of the `struct child' for the
|
---|
457 | dead child, and removing the child from the chain. In addition, if BLOCK
|
---|
458 | nonzero, we block in this function until we've reaped at least one
|
---|
459 | complete child, waiting for it to die if necessary. If ERR is nonzero,
|
---|
460 | print an error message first. */
|
---|
461 |
|
---|
462 | void
|
---|
463 | reap_children (int block, int err)
|
---|
464 | {
|
---|
465 | WAIT_T status;
|
---|
466 | /* Initially, assume we have some. */
|
---|
467 | int reap_more = 1;
|
---|
468 |
|
---|
469 | #ifdef WAIT_NOHANG
|
---|
470 | # define REAP_MORE reap_more
|
---|
471 | #else
|
---|
472 | # define REAP_MORE dead_children
|
---|
473 | #endif
|
---|
474 |
|
---|
475 | /* As long as:
|
---|
476 |
|
---|
477 | We have at least one child outstanding OR a shell function in progress,
|
---|
478 | AND
|
---|
479 | We're blocking for a complete child OR there are more children to reap
|
---|
480 |
|
---|
481 | we'll keep reaping children. */
|
---|
482 |
|
---|
483 | while ((children != 0 || shell_function_pid != 0)
|
---|
484 | && (block || REAP_MORE))
|
---|
485 | {
|
---|
486 | int remote = 0;
|
---|
487 | register int pid;
|
---|
488 | int exit_code, exit_sig, coredump;
|
---|
489 | register struct child *lastc, *c;
|
---|
490 | int child_failed;
|
---|
491 | int any_remote, any_local;
|
---|
492 | #if defined(CONFIG_WITH_KMK_BUILTIN) || defined(MAKE_DLLSHELL)
|
---|
493 | struct child *completed_child = 0;
|
---|
494 | #endif
|
---|
495 |
|
---|
496 | if (err && block)
|
---|
497 | {
|
---|
498 | /* We might block for a while, so let the user know why. */
|
---|
499 | fflush (stdout);
|
---|
500 | error (NILF, _("*** Waiting for unfinished jobs...."));
|
---|
501 | }
|
---|
502 |
|
---|
503 | /* We have one less dead child to reap. As noted in
|
---|
504 | child_handler() above, this count is completely unimportant for
|
---|
505 | all modern, POSIX-y systems that support wait3() or waitpid().
|
---|
506 | The rest of this comment below applies only to early, broken
|
---|
507 | pre-POSIX systems. We keep the count only because... it's there...
|
---|
508 |
|
---|
509 | The test and decrement are not atomic; if it is compiled into:
|
---|
510 | register = dead_children - 1;
|
---|
511 | dead_children = register;
|
---|
512 | a SIGCHLD could come between the two instructions.
|
---|
513 | child_handler increments dead_children.
|
---|
514 | The second instruction here would lose that increment. But the
|
---|
515 | only effect of dead_children being wrong is that we might wait
|
---|
516 | longer than necessary to reap a child, and lose some parallelism;
|
---|
517 | and we might print the "Waiting for unfinished jobs" message above
|
---|
518 | when not necessary. */
|
---|
519 |
|
---|
520 | if (dead_children > 0)
|
---|
521 | --dead_children;
|
---|
522 |
|
---|
523 | any_remote = 0;
|
---|
524 | any_local = shell_function_pid != 0;
|
---|
525 | for (c = children; c != 0; c = c->next)
|
---|
526 | {
|
---|
527 | any_remote |= c->remote;
|
---|
528 | any_local |= ! c->remote;
|
---|
529 | #if defined(CONFIG_WITH_KMK_BUILTIN) || defined(MAKE_DLLSHELL)
|
---|
530 | if (c->have_status)
|
---|
531 | completed_child = c;
|
---|
532 | #endif
|
---|
533 | DB (DB_JOBS, (_("Live child 0x%08lx (%s) PID %ld %s\n"),
|
---|
534 | (unsigned long int) c, c->file->name,
|
---|
535 | (long) c->pid, c->remote ? _(" (remote)") : ""));
|
---|
536 | #ifdef VMS
|
---|
537 | break;
|
---|
538 | #endif
|
---|
539 | }
|
---|
540 |
|
---|
541 | /* First, check for remote children. */
|
---|
542 | if (any_remote)
|
---|
543 | pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
|
---|
544 | else
|
---|
545 | pid = 0;
|
---|
546 |
|
---|
547 | if (pid > 0)
|
---|
548 | /* We got a remote child. */
|
---|
549 | remote = 1;
|
---|
550 | else if (pid < 0)
|
---|
551 | {
|
---|
552 | /* A remote status command failed miserably. Punt. */
|
---|
553 | remote_status_lose:
|
---|
554 | pfatal_with_name ("remote_status");
|
---|
555 | }
|
---|
556 | else
|
---|
557 | {
|
---|
558 | /* No remote children. Check for local children. */
|
---|
559 | #if defined(CONFIG_WITH_KMK_BUILTIN) || defined(MAKE_DLLSHELL)
|
---|
560 | if (completed_child)
|
---|
561 | {
|
---|
562 | pid = completed_child->pid;
|
---|
563 | status = (WAIT_T)completed_child->status;
|
---|
564 | }
|
---|
565 | else
|
---|
566 | #endif
|
---|
567 | #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
|
---|
568 | if (any_local)
|
---|
569 | {
|
---|
570 | #ifdef VMS
|
---|
571 | vmsWaitForChildren (&status);
|
---|
572 | pid = c->pid;
|
---|
573 | #elif MAKE_DLLSHELL
|
---|
574 | pid = wait_jobs((int*)&status, block);
|
---|
575 | #else
|
---|
576 | #ifdef WAIT_NOHANG
|
---|
577 | if (!block)
|
---|
578 | pid = WAIT_NOHANG (&status);
|
---|
579 | else
|
---|
580 | #endif
|
---|
581 | pid = wait (&status);
|
---|
582 | #endif /* !VMS */
|
---|
583 | }
|
---|
584 | else
|
---|
585 | pid = 0;
|
---|
586 |
|
---|
587 | if (pid < 0)
|
---|
588 | {
|
---|
589 | /* The wait*() failed miserably. Punt. */
|
---|
590 | pfatal_with_name ("wait");
|
---|
591 | }
|
---|
592 | else if (pid > 0)
|
---|
593 | {
|
---|
594 | /* We got a child exit; chop the status word up. */
|
---|
595 | exit_code = WEXITSTATUS (status);
|
---|
596 | exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
|
---|
597 | coredump = WCOREDUMP (status);
|
---|
598 |
|
---|
599 | /* If we have started jobs in this second, remove one. */
|
---|
600 | if (job_counter)
|
---|
601 | --job_counter;
|
---|
602 | }
|
---|
603 | else
|
---|
604 | {
|
---|
605 | /* No local children are dead. */
|
---|
606 | reap_more = 0;
|
---|
607 |
|
---|
608 | if (!block || !any_remote)
|
---|
609 | break;
|
---|
610 |
|
---|
611 | /* Now try a blocking wait for a remote child. */
|
---|
612 | pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
|
---|
613 | if (pid < 0)
|
---|
614 | goto remote_status_lose;
|
---|
615 | else if (pid == 0)
|
---|
616 | /* No remote children either. Finally give up. */
|
---|
617 | break;
|
---|
618 |
|
---|
619 | /* We got a remote child. */
|
---|
620 | remote = 1;
|
---|
621 | }
|
---|
622 | #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
|
---|
623 |
|
---|
624 | #ifdef __MSDOS__
|
---|
625 | /* Life is very different on MSDOS. */
|
---|
626 | pid = dos_pid - 1;
|
---|
627 | status = dos_status;
|
---|
628 | exit_code = WEXITSTATUS (status);
|
---|
629 | if (exit_code == 0xff)
|
---|
630 | exit_code = -1;
|
---|
631 | exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
|
---|
632 | coredump = 0;
|
---|
633 | #endif /* __MSDOS__ */
|
---|
634 | #ifdef _AMIGA
|
---|
635 | /* Same on Amiga */
|
---|
636 | pid = amiga_pid - 1;
|
---|
637 | status = amiga_status;
|
---|
638 | exit_code = amiga_status;
|
---|
639 | exit_sig = 0;
|
---|
640 | coredump = 0;
|
---|
641 | #endif /* _AMIGA */
|
---|
642 | #ifdef WINDOWS32
|
---|
643 | {
|
---|
644 | HANDLE hPID;
|
---|
645 | int err;
|
---|
646 |
|
---|
647 | /* wait for anything to finish */
|
---|
648 | if (hPID = process_wait_for_any()) {
|
---|
649 |
|
---|
650 | /* was an error found on this process? */
|
---|
651 | err = process_last_err(hPID);
|
---|
652 |
|
---|
653 | /* get exit data */
|
---|
654 | exit_code = process_exit_code(hPID);
|
---|
655 |
|
---|
656 | if (err)
|
---|
657 | fprintf(stderr, "make (e=%d): %s",
|
---|
658 | exit_code, map_windows32_error_to_string(exit_code));
|
---|
659 |
|
---|
660 | /* signal */
|
---|
661 | exit_sig = process_signal(hPID);
|
---|
662 |
|
---|
663 | /* cleanup process */
|
---|
664 | process_cleanup(hPID);
|
---|
665 |
|
---|
666 | coredump = 0;
|
---|
667 | }
|
---|
668 | pid = (int) hPID;
|
---|
669 | }
|
---|
670 | #endif /* WINDOWS32 */
|
---|
671 | }
|
---|
672 |
|
---|
673 | /* Check if this is the child of the `shell' function. */
|
---|
674 | if (!remote && pid == shell_function_pid)
|
---|
675 | {
|
---|
676 | /* It is. Leave an indicator for the `shell' function. */
|
---|
677 | if (exit_sig == 0 && exit_code == 127)
|
---|
678 | shell_function_completed = -1;
|
---|
679 | else
|
---|
680 | shell_function_completed = 1;
|
---|
681 | break;
|
---|
682 | }
|
---|
683 |
|
---|
684 | child_failed = exit_sig != 0 || exit_code != 0;
|
---|
685 |
|
---|
686 | /* Search for a child matching the deceased one. */
|
---|
687 | lastc = 0;
|
---|
688 | for (c = children; c != 0; lastc = c, c = c->next)
|
---|
689 | if (c->remote == remote && c->pid == pid)
|
---|
690 | break;
|
---|
691 |
|
---|
692 | if (c == 0)
|
---|
693 | /* An unknown child died.
|
---|
694 | Ignore it; it was inherited from our invoker. */
|
---|
695 | continue;
|
---|
696 |
|
---|
697 | DB (DB_JOBS, (child_failed
|
---|
698 | ? _("Reaping losing child 0x%08lx PID %ld %s\n")
|
---|
699 | : _("Reaping winning child 0x%08lx PID %ld %s\n"),
|
---|
700 | (unsigned long int) c, (long) c->pid,
|
---|
701 | c->remote ? _(" (remote)") : ""));
|
---|
702 |
|
---|
703 | if (c->sh_batch_file) {
|
---|
704 | DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
|
---|
705 | c->sh_batch_file));
|
---|
706 |
|
---|
707 | /* just try and remove, don't care if this fails */
|
---|
708 | remove (c->sh_batch_file);
|
---|
709 |
|
---|
710 | /* all done with memory */
|
---|
711 | free (c->sh_batch_file);
|
---|
712 | c->sh_batch_file = NULL;
|
---|
713 | }
|
---|
714 |
|
---|
715 | /* If this child had the good stdin, say it is now free. */
|
---|
716 | if (c->good_stdin)
|
---|
717 | good_stdin_used = 0;
|
---|
718 |
|
---|
719 | if (child_failed && !c->noerror && !ignore_errors_flag)
|
---|
720 | {
|
---|
721 | /* The commands failed. Write an error message,
|
---|
722 | delete non-precious targets, and abort. */
|
---|
723 | static int delete_on_error = -1;
|
---|
724 | child_error (c->file->name, exit_code, exit_sig, coredump, 0);
|
---|
725 | c->file->update_status = 2;
|
---|
726 | if (delete_on_error == -1)
|
---|
727 | {
|
---|
728 | struct file *f = lookup_file (".DELETE_ON_ERROR");
|
---|
729 | delete_on_error = f != 0 && f->is_target;
|
---|
730 | }
|
---|
731 | if (exit_sig != 0 || delete_on_error)
|
---|
732 | delete_child_targets (c);
|
---|
733 | }
|
---|
734 | else
|
---|
735 | {
|
---|
736 | if (child_failed)
|
---|
737 | {
|
---|
738 | /* The commands failed, but we don't care. */
|
---|
739 | child_error (c->file->name,
|
---|
740 | exit_code, exit_sig, coredump, 1);
|
---|
741 | child_failed = 0;
|
---|
742 | }
|
---|
743 |
|
---|
744 | /* If there are more commands to run, try to start them. */
|
---|
745 | if (job_next_command (c))
|
---|
746 | {
|
---|
747 | if (handling_fatal_signal)
|
---|
748 | {
|
---|
749 | /* Never start new commands while we are dying.
|
---|
750 | Since there are more commands that wanted to be run,
|
---|
751 | the target was not completely remade. So we treat
|
---|
752 | this as if a command had failed. */
|
---|
753 | c->file->update_status = 2;
|
---|
754 | }
|
---|
755 | else
|
---|
756 | {
|
---|
757 | /* Check again whether to start remotely.
|
---|
758 | Whether or not we want to changes over time.
|
---|
759 | Also, start_remote_job may need state set up
|
---|
760 | by start_remote_job_p. */
|
---|
761 | c->remote = start_remote_job_p (0);
|
---|
762 | start_job_command (c);
|
---|
763 | /* Fatal signals are left blocked in case we were
|
---|
764 | about to put that child on the chain. But it is
|
---|
765 | already there, so it is safe for a fatal signal to
|
---|
766 | arrive now; it will clean up this child's targets. */
|
---|
767 | unblock_sigs ();
|
---|
768 | if (c->file->command_state == cs_running)
|
---|
769 | /* We successfully started the new command.
|
---|
770 | Loop to reap more children. */
|
---|
771 | continue;
|
---|
772 | }
|
---|
773 |
|
---|
774 | if (c->file->update_status != 0)
|
---|
775 | /* We failed to start the commands. */
|
---|
776 | delete_child_targets (c);
|
---|
777 | }
|
---|
778 | else
|
---|
779 | /* There are no more commands. We got through them all
|
---|
780 | without an unignored error. Now the target has been
|
---|
781 | successfully updated. */
|
---|
782 | c->file->update_status = 0;
|
---|
783 | }
|
---|
784 |
|
---|
785 | /* When we get here, all the commands for C->file are finished
|
---|
786 | (or aborted) and C->file->update_status contains 0 or 2. But
|
---|
787 | C->file->command_state is still cs_running if all the commands
|
---|
788 | ran; notice_finish_file looks for cs_running to tell it that
|
---|
789 | it's interesting to check the file's modtime again now. */
|
---|
790 |
|
---|
791 | if (! handling_fatal_signal)
|
---|
792 | /* Notice if the target of the commands has been changed.
|
---|
793 | This also propagates its values for command_state and
|
---|
794 | update_status to its also_make files. */
|
---|
795 | notice_finished_file (c->file);
|
---|
796 |
|
---|
797 | DB (DB_JOBS, (_("Removing child 0x%08lx PID %ld%s from chain.\n"),
|
---|
798 | (unsigned long int) c, (long) c->pid,
|
---|
799 | c->remote ? _(" (remote)") : ""));
|
---|
800 |
|
---|
801 | /* Block fatal signals while frobnicating the list, so that
|
---|
802 | children and job_slots_used are always consistent. Otherwise
|
---|
803 | a fatal signal arriving after the child is off the chain and
|
---|
804 | before job_slots_used is decremented would believe a child was
|
---|
805 | live and call reap_children again. */
|
---|
806 | block_sigs ();
|
---|
807 |
|
---|
808 | /* There is now another slot open. */
|
---|
809 | if (job_slots_used > 0)
|
---|
810 | --job_slots_used;
|
---|
811 |
|
---|
812 | /* Remove the child from the chain and free it. */
|
---|
813 | if (lastc == 0)
|
---|
814 | children = c->next;
|
---|
815 | else
|
---|
816 | lastc->next = c->next;
|
---|
817 |
|
---|
818 | free_child (c);
|
---|
819 |
|
---|
820 | unblock_sigs ();
|
---|
821 |
|
---|
822 | /* If the job failed, and the -k flag was not given, die,
|
---|
823 | unless we are already in the process of dying. */
|
---|
824 | if (!err && child_failed && !keep_going_flag &&
|
---|
825 | /* fatal_error_signal will die with the right signal. */
|
---|
826 | !handling_fatal_signal)
|
---|
827 | die (2);
|
---|
828 |
|
---|
829 | /* Only block for one child. */
|
---|
830 | block = 0;
|
---|
831 | }
|
---|
832 |
|
---|
833 | return;
|
---|
834 | }
|
---|
835 | |
---|
836 |
|
---|
837 | /* Free the storage allocated for CHILD. */
|
---|
838 |
|
---|
839 | static void
|
---|
840 | free_child (struct child *child)
|
---|
841 | {
|
---|
842 | /* If this child is the only one it was our "free" job, so don't put a
|
---|
843 | token back for it. This child has already been removed from the list,
|
---|
844 | so if there any left this wasn't the last one. */
|
---|
845 |
|
---|
846 | if (job_fds[1] >= 0 && children)
|
---|
847 | {
|
---|
848 | char token = '+';
|
---|
849 | int r;
|
---|
850 |
|
---|
851 | /* Write a job token back to the pipe. */
|
---|
852 |
|
---|
853 | EINTRLOOP (r, write (job_fds[1], &token, 1));
|
---|
854 | if (r != 1)
|
---|
855 | pfatal_with_name (_("write jobserver"));
|
---|
856 |
|
---|
857 | DB (DB_JOBS, (_("Released token for child 0x%08lx (%s).\n"),
|
---|
858 | (unsigned long int) child, child->file->name));
|
---|
859 | }
|
---|
860 |
|
---|
861 | if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
|
---|
862 | return;
|
---|
863 |
|
---|
864 | if (child->command_lines != 0)
|
---|
865 | {
|
---|
866 | register unsigned int i;
|
---|
867 | for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
|
---|
868 | free (child->command_lines[i]);
|
---|
869 | free ((char *) child->command_lines);
|
---|
870 | }
|
---|
871 |
|
---|
872 | if (child->environment != 0)
|
---|
873 | {
|
---|
874 | register char **ep = child->environment;
|
---|
875 | while (*ep != 0)
|
---|
876 | free (*ep++);
|
---|
877 | free ((char *) child->environment);
|
---|
878 | }
|
---|
879 |
|
---|
880 | free ((char *) child);
|
---|
881 | }
|
---|
882 | |
---|
883 |
|
---|
884 | #ifdef POSIX
|
---|
885 | extern sigset_t fatal_signal_set;
|
---|
886 | #endif
|
---|
887 |
|
---|
888 | void
|
---|
889 | block_sigs (void)
|
---|
890 | {
|
---|
891 | #ifdef POSIX
|
---|
892 | (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
|
---|
893 | #else
|
---|
894 | # ifdef HAVE_SIGSETMASK
|
---|
895 | (void) sigblock (fatal_signal_mask);
|
---|
896 | # endif
|
---|
897 | #endif
|
---|
898 | }
|
---|
899 |
|
---|
900 | #ifdef POSIX
|
---|
901 | void
|
---|
902 | unblock_sigs (void)
|
---|
903 | {
|
---|
904 | sigset_t empty;
|
---|
905 | sigemptyset (&empty);
|
---|
906 | sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
|
---|
907 | }
|
---|
908 | #endif
|
---|
909 |
|
---|
910 | #ifdef MAKE_JOBSERVER
|
---|
911 | /* Set the child handler action flags to FLAGS. */
|
---|
912 | static void
|
---|
913 | set_child_handler_action_flags (int flags)
|
---|
914 | {
|
---|
915 | struct sigaction sa;
|
---|
916 | bzero ((char *) &sa, sizeof sa);
|
---|
917 | sa.sa_handler = child_handler;
|
---|
918 | sa.sa_flags = flags;
|
---|
919 | #if defined SIGCHLD
|
---|
920 | sigaction (SIGCHLD, &sa, NULL);
|
---|
921 | #endif
|
---|
922 | #if defined SIGCLD && SIGCLD != SIGCHLD
|
---|
923 | sigaction (SIGCLD, &sa, NULL);
|
---|
924 | #endif
|
---|
925 | }
|
---|
926 | #endif
|
---|
927 |
|
---|
928 |
|
---|
929 | /* Start a job to run the commands specified in CHILD.
|
---|
930 | CHILD is updated to reflect the commands and ID of the child process.
|
---|
931 |
|
---|
932 | NOTE: On return fatal signals are blocked! The caller is responsible
|
---|
933 | for calling `unblock_sigs', once the new child is safely on the chain so
|
---|
934 | it can be cleaned up in the event of a fatal signal. */
|
---|
935 |
|
---|
936 | static void
|
---|
937 | start_job_command (struct child *child)
|
---|
938 | {
|
---|
939 | #ifndef _AMIGA
|
---|
940 | static int bad_stdin = -1;
|
---|
941 | #endif
|
---|
942 | register char *p;
|
---|
943 | int flags;
|
---|
944 | #ifdef VMS
|
---|
945 | char *argv;
|
---|
946 | #else
|
---|
947 | char **argv;
|
---|
948 | #endif
|
---|
949 |
|
---|
950 | /* If we have a completely empty commandset, stop now. */
|
---|
951 | if (!child->command_ptr)
|
---|
952 | goto next_command;
|
---|
953 |
|
---|
954 | /* Combine the flags parsed for the line itself with
|
---|
955 | the flags specified globally for this target. */
|
---|
956 | flags = (child->file->command_flags
|
---|
957 | | child->file->cmds->lines_flags[child->command_line - 1]);
|
---|
958 |
|
---|
959 | p = child->command_ptr;
|
---|
960 | child->noerror = flags & COMMANDS_NOERROR;
|
---|
961 |
|
---|
962 | while (*p != '\0')
|
---|
963 | {
|
---|
964 | if (*p == '@')
|
---|
965 | flags |= COMMANDS_SILENT;
|
---|
966 | else if (*p == '+')
|
---|
967 | flags |= COMMANDS_RECURSE;
|
---|
968 | else if (*p == '-')
|
---|
969 | child->noerror = 1;
|
---|
970 | else if (!isblank ((unsigned char)*p))
|
---|
971 | {
|
---|
972 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
---|
973 | if ( !(flags & COMMANDS_BUILTIN)
|
---|
974 | && !strncmp(p, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
|
---|
975 | flags |= COMMANDS_BUILTIN;
|
---|
976 | #endif /* CONFIG_WITH_KMK_BUILTIN */
|
---|
977 | break;
|
---|
978 | }
|
---|
979 | ++p;
|
---|
980 | }
|
---|
981 |
|
---|
982 | /* Update the file's command flags with any new ones we found. We only
|
---|
983 | keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
|
---|
984 | now marking more commands recursive than should be in the case of
|
---|
985 | multiline define/endef scripts where only one line is marked "+". In
|
---|
986 | order to really fix this, we'll have to keep a lines_flags for every
|
---|
987 | actual line, after expansion. */
|
---|
988 | child->file->cmds->lines_flags[child->command_line - 1]
|
---|
989 | |= flags & COMMANDS_RECURSE;
|
---|
990 |
|
---|
991 | /* Figure out an argument list from this command line. */
|
---|
992 |
|
---|
993 | {
|
---|
994 | char *end = 0;
|
---|
995 | #ifdef VMS
|
---|
996 | argv = p;
|
---|
997 | #else
|
---|
998 | argv = construct_command_argv (p, &end, child->file, &child->sh_batch_file);
|
---|
999 | #endif
|
---|
1000 | if (end == NULL)
|
---|
1001 | child->command_ptr = NULL;
|
---|
1002 | else
|
---|
1003 | {
|
---|
1004 | *end++ = '\0';
|
---|
1005 | child->command_ptr = end;
|
---|
1006 | }
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | /* If -q was given, say that updating `failed' if there was any text on the
|
---|
1010 | command line, or `succeeded' otherwise. The exit status of 1 tells the
|
---|
1011 | user that -q is saying `something to do'; the exit status for a random
|
---|
1012 | error is 2. */
|
---|
1013 | if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
|
---|
1014 | {
|
---|
1015 | #ifndef VMS
|
---|
1016 | free (argv[0]);
|
---|
1017 | free ((char *) argv);
|
---|
1018 | #endif
|
---|
1019 | child->file->update_status = 1;
|
---|
1020 | notice_finished_file (child->file);
|
---|
1021 | return;
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | if (touch_flag && !(flags & COMMANDS_RECURSE))
|
---|
1025 | {
|
---|
1026 | /* Go on to the next command. It might be the recursive one.
|
---|
1027 | We construct ARGV only to find the end of the command line. */
|
---|
1028 | #ifndef VMS
|
---|
1029 | if (argv)
|
---|
1030 | {
|
---|
1031 | free (argv[0]);
|
---|
1032 | free ((char *) argv);
|
---|
1033 | }
|
---|
1034 | #endif
|
---|
1035 | argv = 0;
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | if (argv == 0)
|
---|
1039 | {
|
---|
1040 | next_command:
|
---|
1041 | #ifdef __MSDOS__
|
---|
1042 | execute_by_shell = 0; /* in case construct_command_argv sets it */
|
---|
1043 | #endif
|
---|
1044 | /* This line has no commands. Go to the next. */
|
---|
1045 | if (job_next_command (child))
|
---|
1046 | start_job_command (child);
|
---|
1047 | else
|
---|
1048 | {
|
---|
1049 | /* No more commands. Make sure we're "running"; we might not be if
|
---|
1050 | (e.g.) all commands were skipped due to -n. */
|
---|
1051 | set_command_state (child->file, cs_running);
|
---|
1052 | child->file->update_status = 0;
|
---|
1053 | notice_finished_file (child->file);
|
---|
1054 | }
|
---|
1055 | return;
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | /* Print out the command. If silent, we call `message' with null so it
|
---|
1059 | can log the working directory before the command's own error messages
|
---|
1060 | appear. */
|
---|
1061 |
|
---|
1062 | message (0, (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag))
|
---|
1063 | ? "%s" : (char *) 0, p);
|
---|
1064 |
|
---|
1065 | /* Tell update_goal_chain that a command has been started on behalf of
|
---|
1066 | this target. It is important that this happens here and not in
|
---|
1067 | reap_children (where we used to do it), because reap_children might be
|
---|
1068 | reaping children from a different target. We want this increment to
|
---|
1069 | guaranteedly indicate that a command was started for the dependency
|
---|
1070 | chain (i.e., update_file recursion chain) we are processing. */
|
---|
1071 |
|
---|
1072 | ++commands_started;
|
---|
1073 |
|
---|
1074 | /* Optimize an empty command. People use this for timestamp rules,
|
---|
1075 | so avoid forking a useless shell. Do this after we increment
|
---|
1076 | commands_started so make still treats this special case as if it
|
---|
1077 | performed some action (makes a difference as to what messages are
|
---|
1078 | printed, etc. */
|
---|
1079 |
|
---|
1080 | #if !defined(VMS) && !defined(_AMIGA)
|
---|
1081 | if (
|
---|
1082 | #if defined __MSDOS__ || defined (__EMX__)
|
---|
1083 | unixy_shell /* the test is complicated and we already did it */
|
---|
1084 | #else
|
---|
1085 | (argv[0] && !strcmp (argv[0], "/bin/sh"))
|
---|
1086 | #endif
|
---|
1087 | && (argv[1]
|
---|
1088 | && argv[1][0] == '-' && argv[1][1] == 'c' && argv[1][2] == '\0')
|
---|
1089 | && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
|
---|
1090 | && argv[3] == NULL)
|
---|
1091 | {
|
---|
1092 | free (argv[0]);
|
---|
1093 | free ((char *) argv);
|
---|
1094 | goto next_command;
|
---|
1095 | }
|
---|
1096 | #endif /* !VMS && !_AMIGA */
|
---|
1097 |
|
---|
1098 | /* If -n was given, recurse to get the next line in the sequence. */
|
---|
1099 |
|
---|
1100 | if (just_print_flag && !(flags & COMMANDS_RECURSE))
|
---|
1101 | {
|
---|
1102 | #ifndef VMS
|
---|
1103 | free (argv[0]);
|
---|
1104 | free ((char *) argv);
|
---|
1105 | #endif
|
---|
1106 | goto next_command;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
---|
1110 | /* If builtin command then pass it on to the builtin shell interpreter. */
|
---|
1111 |
|
---|
1112 | if ((flags & COMMANDS_BUILTIN) && !just_print_flag)
|
---|
1113 | {
|
---|
1114 | int rc;
|
---|
1115 | char **p2 = argv;
|
---|
1116 | while (*p2 && strncmp(*p2, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
|
---|
1117 | p2++;
|
---|
1118 | assert(*p2);
|
---|
1119 | set_command_state (child->file, cs_running);
|
---|
1120 | if (p2 != argv)
|
---|
1121 | rc = kmk_builtin_command(*p2);
|
---|
1122 | else
|
---|
1123 | {
|
---|
1124 | int argc = 1;
|
---|
1125 | while (argv[argc])
|
---|
1126 | argc++;
|
---|
1127 | rc = kmk_builtin_command_parsed(argc, argv);
|
---|
1128 | }
|
---|
1129 | printf("rc=%d\n", rc);
|
---|
1130 | #ifndef VMS
|
---|
1131 | free (argv[0]);
|
---|
1132 | free ((char *) argv);
|
---|
1133 | #endif
|
---|
1134 | if (!rc)
|
---|
1135 | goto next_command;
|
---|
1136 | child->pid = (pid_t)42424242;
|
---|
1137 | child->status = rc << 8;
|
---|
1138 | child->have_status = 1;
|
---|
1139 | return;
|
---|
1140 | }
|
---|
1141 | #endif /* CONFIG_WITH_KMK_BUILTIN */
|
---|
1142 |
|
---|
1143 | /* Flush the output streams so they won't have things written twice. */
|
---|
1144 |
|
---|
1145 | fflush (stdout);
|
---|
1146 | fflush (stderr);
|
---|
1147 |
|
---|
1148 | #ifndef VMS
|
---|
1149 | #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
|
---|
1150 |
|
---|
1151 | /* Set up a bad standard input that reads from a broken pipe. */
|
---|
1152 |
|
---|
1153 | if (bad_stdin == -1)
|
---|
1154 | {
|
---|
1155 | /* Make a file descriptor that is the read end of a broken pipe.
|
---|
1156 | This will be used for some children's standard inputs. */
|
---|
1157 | int pd[2];
|
---|
1158 | if (pipe (pd) == 0)
|
---|
1159 | {
|
---|
1160 | /* Close the write side. */
|
---|
1161 | (void) close (pd[1]);
|
---|
1162 | /* Save the read side. */
|
---|
1163 | bad_stdin = pd[0];
|
---|
1164 |
|
---|
1165 | /* Set the descriptor to close on exec, so it does not litter any
|
---|
1166 | child's descriptor table. When it is dup2'd onto descriptor 0,
|
---|
1167 | that descriptor will not close on exec. */
|
---|
1168 | CLOSE_ON_EXEC (bad_stdin);
|
---|
1169 | }
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
|
---|
1173 |
|
---|
1174 | /* Decide whether to give this child the `good' standard input
|
---|
1175 | (one that points to the terminal or whatever), or the `bad' one
|
---|
1176 | that points to the read side of a broken pipe. */
|
---|
1177 |
|
---|
1178 | child->good_stdin = !good_stdin_used;
|
---|
1179 | if (child->good_stdin)
|
---|
1180 | good_stdin_used = 1;
|
---|
1181 |
|
---|
1182 | #endif /* !VMS */
|
---|
1183 |
|
---|
1184 | child->deleted = 0;
|
---|
1185 |
|
---|
1186 | #ifndef _AMIGA
|
---|
1187 | /* Set up the environment for the child. */
|
---|
1188 | if (child->environment == 0)
|
---|
1189 | child->environment = target_environment (child->file);
|
---|
1190 | #endif
|
---|
1191 |
|
---|
1192 | #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
|
---|
1193 |
|
---|
1194 | #ifndef VMS
|
---|
1195 | /* start_waiting_job has set CHILD->remote if we can start a remote job. */
|
---|
1196 | if (child->remote)
|
---|
1197 | {
|
---|
1198 | int is_remote, id, used_stdin;
|
---|
1199 | if (start_remote_job (argv, child->environment,
|
---|
1200 | child->good_stdin ? 0 : bad_stdin,
|
---|
1201 | &is_remote, &id, &used_stdin))
|
---|
1202 | /* Don't give up; remote execution may fail for various reasons. If
|
---|
1203 | so, simply run the job locally. */
|
---|
1204 | goto run_local;
|
---|
1205 | else
|
---|
1206 | {
|
---|
1207 | if (child->good_stdin && !used_stdin)
|
---|
1208 | {
|
---|
1209 | child->good_stdin = 0;
|
---|
1210 | good_stdin_used = 0;
|
---|
1211 | }
|
---|
1212 | child->remote = is_remote;
|
---|
1213 | child->pid = id;
|
---|
1214 | }
|
---|
1215 | }
|
---|
1216 | else
|
---|
1217 | #endif /* !VMS */
|
---|
1218 | {
|
---|
1219 | /* Fork the child process. */
|
---|
1220 |
|
---|
1221 | char **parent_environ;
|
---|
1222 |
|
---|
1223 | run_local:
|
---|
1224 | block_sigs ();
|
---|
1225 |
|
---|
1226 | child->remote = 0;
|
---|
1227 |
|
---|
1228 | #ifdef VMS
|
---|
1229 |
|
---|
1230 | if (!child_execute_job (argv, child)) {
|
---|
1231 | /* Fork failed! */
|
---|
1232 | perror_with_name ("vfork", "");
|
---|
1233 | goto error;
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | #else
|
---|
1237 |
|
---|
1238 | parent_environ = environ;
|
---|
1239 |
|
---|
1240 | # ifdef __EMX__
|
---|
1241 | /* If we aren't running a recursive command and we have a jobserver
|
---|
1242 | pipe, close it before exec'ing. */
|
---|
1243 | if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
|
---|
1244 | {
|
---|
1245 | CLOSE_ON_EXEC (job_fds[0]);
|
---|
1246 | CLOSE_ON_EXEC (job_fds[1]);
|
---|
1247 | }
|
---|
1248 | if (job_rfd >= 0)
|
---|
1249 | CLOSE_ON_EXEC (job_rfd);
|
---|
1250 |
|
---|
1251 | /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
|
---|
1252 | child->pid = child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
|
---|
1253 | argv, child->environment, child);
|
---|
1254 | if (child->pid < 0)
|
---|
1255 | {
|
---|
1256 | /* spawn failed! */
|
---|
1257 | unblock_sigs ();
|
---|
1258 | perror_with_name ("spawn", "");
|
---|
1259 | goto error;
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | /* undo CLOSE_ON_EXEC() after the child process has been started */
|
---|
1263 | if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
|
---|
1264 | {
|
---|
1265 | fcntl (job_fds[0], F_SETFD, 0);
|
---|
1266 | fcntl (job_fds[1], F_SETFD, 0);
|
---|
1267 | }
|
---|
1268 | if (job_rfd >= 0)
|
---|
1269 | fcntl (job_rfd, F_SETFD, 0);
|
---|
1270 |
|
---|
1271 | #else /* !__EMX__ */
|
---|
1272 |
|
---|
1273 | child->pid = vfork ();
|
---|
1274 | environ = parent_environ; /* Restore value child may have clobbered. */
|
---|
1275 | if (child->pid == 0)
|
---|
1276 | {
|
---|
1277 | /* We are the child side. */
|
---|
1278 | unblock_sigs ();
|
---|
1279 |
|
---|
1280 | /* If we aren't running a recursive command and we have a jobserver
|
---|
1281 | pipe, close it before exec'ing. */
|
---|
1282 | if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
|
---|
1283 | {
|
---|
1284 | close (job_fds[0]);
|
---|
1285 | close (job_fds[1]);
|
---|
1286 | }
|
---|
1287 | if (job_rfd >= 0)
|
---|
1288 | close (job_rfd);
|
---|
1289 |
|
---|
1290 | child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
|
---|
1291 | argv, child->environment);
|
---|
1292 | }
|
---|
1293 | else if (child->pid < 0)
|
---|
1294 | {
|
---|
1295 | /* Fork failed! */
|
---|
1296 | unblock_sigs ();
|
---|
1297 | perror_with_name ("vfork", "");
|
---|
1298 | goto error;
|
---|
1299 | }
|
---|
1300 | # endif /* !__EMX__ */
|
---|
1301 | #endif /* !VMS */
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | #else /* __MSDOS__ or Amiga or WINDOWS32 */
|
---|
1305 | #ifdef __MSDOS__
|
---|
1306 | {
|
---|
1307 | int proc_return;
|
---|
1308 |
|
---|
1309 | block_sigs ();
|
---|
1310 | dos_status = 0;
|
---|
1311 |
|
---|
1312 | /* We call `system' to do the job of the SHELL, since stock DOS
|
---|
1313 | shell is too dumb. Our `system' knows how to handle long
|
---|
1314 | command lines even if pipes/redirection is needed; it will only
|
---|
1315 | call COMMAND.COM when its internal commands are used. */
|
---|
1316 | if (execute_by_shell)
|
---|
1317 | {
|
---|
1318 | char *cmdline = argv[0];
|
---|
1319 | /* We don't have a way to pass environment to `system',
|
---|
1320 | so we need to save and restore ours, sigh... */
|
---|
1321 | char **parent_environ = environ;
|
---|
1322 |
|
---|
1323 | environ = child->environment;
|
---|
1324 |
|
---|
1325 | /* If we have a *real* shell, tell `system' to call
|
---|
1326 | it to do everything for us. */
|
---|
1327 | if (unixy_shell)
|
---|
1328 | {
|
---|
1329 | /* A *real* shell on MSDOS may not support long
|
---|
1330 | command lines the DJGPP way, so we must use `system'. */
|
---|
1331 | cmdline = argv[2]; /* get past "shell -c" */
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | dos_command_running = 1;
|
---|
1335 | proc_return = system (cmdline);
|
---|
1336 | environ = parent_environ;
|
---|
1337 | execute_by_shell = 0; /* for the next time */
|
---|
1338 | }
|
---|
1339 | else
|
---|
1340 | {
|
---|
1341 | dos_command_running = 1;
|
---|
1342 | proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | /* Need to unblock signals before turning off
|
---|
1346 | dos_command_running, so that child's signals
|
---|
1347 | will be treated as such (see fatal_error_signal). */
|
---|
1348 | unblock_sigs ();
|
---|
1349 | dos_command_running = 0;
|
---|
1350 |
|
---|
1351 | /* If the child got a signal, dos_status has its
|
---|
1352 | high 8 bits set, so be careful not to alter them. */
|
---|
1353 | if (proc_return == -1)
|
---|
1354 | dos_status |= 0xff;
|
---|
1355 | else
|
---|
1356 | dos_status |= (proc_return & 0xff);
|
---|
1357 | ++dead_children;
|
---|
1358 | child->pid = dos_pid++;
|
---|
1359 | }
|
---|
1360 | #endif /* __MSDOS__ */
|
---|
1361 | #ifdef _AMIGA
|
---|
1362 | amiga_status = MyExecute (argv);
|
---|
1363 |
|
---|
1364 | ++dead_children;
|
---|
1365 | child->pid = amiga_pid++;
|
---|
1366 | if (amiga_batch_file)
|
---|
1367 | {
|
---|
1368 | amiga_batch_file = 0;
|
---|
1369 | DeleteFile (amiga_bname); /* Ignore errors. */
|
---|
1370 | }
|
---|
1371 | #endif /* Amiga */
|
---|
1372 | #ifdef WINDOWS32
|
---|
1373 | {
|
---|
1374 | HANDLE hPID;
|
---|
1375 | char* arg0;
|
---|
1376 |
|
---|
1377 | /* make UNC paths safe for CreateProcess -- backslash format */
|
---|
1378 | arg0 = argv[0];
|
---|
1379 | if (arg0 && arg0[0] == '/' && arg0[1] == '/')
|
---|
1380 | for ( ; arg0 && *arg0; arg0++)
|
---|
1381 | if (*arg0 == '/')
|
---|
1382 | *arg0 = '\\';
|
---|
1383 |
|
---|
1384 | /* make sure CreateProcess() has Path it needs */
|
---|
1385 | sync_Path_environment();
|
---|
1386 |
|
---|
1387 | hPID = process_easy(argv, child->environment);
|
---|
1388 |
|
---|
1389 | if (hPID != INVALID_HANDLE_VALUE)
|
---|
1390 | child->pid = (int) hPID;
|
---|
1391 | else {
|
---|
1392 | int i;
|
---|
1393 | unblock_sigs();
|
---|
1394 | fprintf(stderr,
|
---|
1395 | _("process_easy() failed failed to launch process (e=%d)\n"),
|
---|
1396 | process_last_err(hPID));
|
---|
1397 | for (i = 0; argv[i]; i++)
|
---|
1398 | fprintf(stderr, "%s ", argv[i]);
|
---|
1399 | fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
|
---|
1400 | }
|
---|
1401 | }
|
---|
1402 | #endif /* WINDOWS32 */
|
---|
1403 | #endif /* __MSDOS__ or Amiga or WINDOWS32 */
|
---|
1404 |
|
---|
1405 | /* Bump the number of jobs started in this second. */
|
---|
1406 | ++job_counter;
|
---|
1407 |
|
---|
1408 | /* We are the parent side. Set the state to
|
---|
1409 | say the commands are running and return. */
|
---|
1410 |
|
---|
1411 | set_command_state (child->file, cs_running);
|
---|
1412 |
|
---|
1413 | /* Free the storage used by the child's argument list. */
|
---|
1414 | #ifndef VMS
|
---|
1415 | free (argv[0]);
|
---|
1416 | free ((char *) argv);
|
---|
1417 | #endif
|
---|
1418 |
|
---|
1419 | return;
|
---|
1420 |
|
---|
1421 | error:
|
---|
1422 | child->file->update_status = 2;
|
---|
1423 | notice_finished_file (child->file);
|
---|
1424 | return;
|
---|
1425 | }
|
---|
1426 |
|
---|
1427 | /* Try to start a child running.
|
---|
1428 | Returns nonzero if the child was started (and maybe finished), or zero if
|
---|
1429 | the load was too high and the child was put on the `waiting_jobs' chain. */
|
---|
1430 |
|
---|
1431 | static int
|
---|
1432 | start_waiting_job (struct child *c)
|
---|
1433 | {
|
---|
1434 | struct file *f = c->file;
|
---|
1435 | DB (DB_KMK, (_("start_waiting_job %p (`%s') command_flags=%#x\n"), c, c->file->name, c->file->command_flags));
|
---|
1436 |
|
---|
1437 | /* If we can start a job remotely, we always want to, and don't care about
|
---|
1438 | the local load average. We record that the job should be started
|
---|
1439 | remotely in C->remote for start_job_command to test. */
|
---|
1440 |
|
---|
1441 | c->remote = start_remote_job_p (1);
|
---|
1442 |
|
---|
1443 | /* If we are running at least one job already and the load average
|
---|
1444 | is too high, make this one wait. */
|
---|
1445 | if (!c->remote && job_slots_used > 0 &&
|
---|
1446 | (not_parallel || (c->file->command_flags & COMMANDS_NOTPARALLEL) || load_too_high ()))
|
---|
1447 | {
|
---|
1448 | /* Put this child on the chain of children waiting for the load average
|
---|
1449 | to go down. if not paralell, put it last. */
|
---|
1450 | set_command_state (f, cs_running);
|
---|
1451 | c->next = waiting_jobs;
|
---|
1452 | if (c->next && (c->file->command_flags & COMMANDS_NOTPARALLEL))
|
---|
1453 | {
|
---|
1454 | struct child *prev = waiting_jobs;
|
---|
1455 | while (prev->next)
|
---|
1456 | prev = prev->next;
|
---|
1457 | c->next = 0;
|
---|
1458 | prev->next = c;
|
---|
1459 | }
|
---|
1460 | else
|
---|
1461 | waiting_jobs = c;
|
---|
1462 | DB (DB_KMK, (_("queued child %p (`%s')\n"), c, c->file->name));
|
---|
1463 | return 0;
|
---|
1464 | }
|
---|
1465 |
|
---|
1466 | if (c->file->command_flags & COMMANDS_NOTPARALLEL)
|
---|
1467 | {
|
---|
1468 | assert(not_parallel == 0);
|
---|
1469 | DB (DB_KMK, (_("not_parallel %d -> %d (file=%p `%s')\n"), not_parallel, not_parallel + 1, c->file, c->file->name));
|
---|
1470 | ++not_parallel;
|
---|
1471 | }
|
---|
1472 |
|
---|
1473 |
|
---|
1474 | /* Start the first command; reap_children will run later command lines. */
|
---|
1475 | start_job_command (c);
|
---|
1476 |
|
---|
1477 | switch (f->command_state)
|
---|
1478 | {
|
---|
1479 | case cs_running:
|
---|
1480 | c->next = children;
|
---|
1481 | DB (DB_JOBS, (_("Putting child 0x%08lx (%s) PID %ld%s on the chain.\n"),
|
---|
1482 | (unsigned long int) c, c->file->name,
|
---|
1483 | (long) c->pid, c->remote ? _(" (remote)") : ""));
|
---|
1484 | children = c;
|
---|
1485 | /* One more job slot is in use. */
|
---|
1486 | ++job_slots_used;
|
---|
1487 | unblock_sigs ();
|
---|
1488 | break;
|
---|
1489 |
|
---|
1490 | case cs_not_started:
|
---|
1491 | /* All the command lines turned out to be empty. */
|
---|
1492 | f->update_status = 0;
|
---|
1493 | /* FALLTHROUGH */
|
---|
1494 |
|
---|
1495 | case cs_finished:
|
---|
1496 | notice_finished_file (f);
|
---|
1497 | free_child (c);
|
---|
1498 | break;
|
---|
1499 |
|
---|
1500 | default:
|
---|
1501 | assert (f->command_state == cs_finished);
|
---|
1502 | break;
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | return 1;
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | /* Create a `struct child' for FILE and start its commands running. */
|
---|
1509 |
|
---|
1510 | void
|
---|
1511 | new_job (struct file *file)
|
---|
1512 | {
|
---|
1513 | register struct commands *cmds = file->cmds;
|
---|
1514 | register struct child *c;
|
---|
1515 | char **lines;
|
---|
1516 | register unsigned int i;
|
---|
1517 |
|
---|
1518 | /* Let any previously decided-upon jobs that are waiting
|
---|
1519 | for the load to go down start before this new one. */
|
---|
1520 | start_waiting_jobs ();
|
---|
1521 |
|
---|
1522 | /* Reap any children that might have finished recently. */
|
---|
1523 | reap_children (0, 0);
|
---|
1524 |
|
---|
1525 | /* Chop the commands up into lines if they aren't already. */
|
---|
1526 | chop_commands (cmds);
|
---|
1527 |
|
---|
1528 | /* Expand the command lines and store the results in LINES. */
|
---|
1529 | lines = (char **) xmalloc (cmds->ncommand_lines * sizeof (char *));
|
---|
1530 | for (i = 0; i < cmds->ncommand_lines; ++i)
|
---|
1531 | {
|
---|
1532 | /* Collapse backslash-newline combinations that are inside variable
|
---|
1533 | or function references. These are left alone by the parser so
|
---|
1534 | that they will appear in the echoing of commands (where they look
|
---|
1535 | nice); and collapsed by construct_command_argv when it tokenizes.
|
---|
1536 | But letting them survive inside function invocations loses because
|
---|
1537 | we don't want the functions to see them as part of the text. */
|
---|
1538 |
|
---|
1539 | char *in, *out, *ref;
|
---|
1540 |
|
---|
1541 | /* IN points to where in the line we are scanning.
|
---|
1542 | OUT points to where in the line we are writing.
|
---|
1543 | When we collapse a backslash-newline combination,
|
---|
1544 | IN gets ahead of OUT. */
|
---|
1545 |
|
---|
1546 | in = out = cmds->command_lines[i];
|
---|
1547 | while ((ref = strchr (in, '$')) != 0)
|
---|
1548 | {
|
---|
1549 | ++ref; /* Move past the $. */
|
---|
1550 |
|
---|
1551 | if (out != in)
|
---|
1552 | /* Copy the text between the end of the last chunk
|
---|
1553 | we processed (where IN points) and the new chunk
|
---|
1554 | we are about to process (where REF points). */
|
---|
1555 | bcopy (in, out, ref - in);
|
---|
1556 |
|
---|
1557 | /* Move both pointers past the boring stuff. */
|
---|
1558 | out += ref - in;
|
---|
1559 | in = ref;
|
---|
1560 |
|
---|
1561 | if (*ref == '(' || *ref == '{')
|
---|
1562 | {
|
---|
1563 | char openparen = *ref;
|
---|
1564 | char closeparen = openparen == '(' ? ')' : '}';
|
---|
1565 | int count;
|
---|
1566 | char *p;
|
---|
1567 |
|
---|
1568 | *out++ = *in++; /* Copy OPENPAREN. */
|
---|
1569 | /* IN now points past the opening paren or brace.
|
---|
1570 | Count parens or braces until it is matched. */
|
---|
1571 | count = 0;
|
---|
1572 | while (*in != '\0')
|
---|
1573 | {
|
---|
1574 | if (*in == closeparen && --count < 0)
|
---|
1575 | break;
|
---|
1576 | else if (*in == '\\' && in[1] == '\n')
|
---|
1577 | {
|
---|
1578 | /* We have found a backslash-newline inside a
|
---|
1579 | variable or function reference. Eat it and
|
---|
1580 | any following whitespace. */
|
---|
1581 |
|
---|
1582 | int quoted = 0;
|
---|
1583 | for (p = in - 1; p > ref && *p == '\\'; --p)
|
---|
1584 | quoted = !quoted;
|
---|
1585 |
|
---|
1586 | if (quoted)
|
---|
1587 | /* There were two or more backslashes, so this is
|
---|
1588 | not really a continuation line. We don't collapse
|
---|
1589 | the quoting backslashes here as is done in
|
---|
1590 | collapse_continuations, because the line will
|
---|
1591 | be collapsed again after expansion. */
|
---|
1592 | *out++ = *in++;
|
---|
1593 | else
|
---|
1594 | {
|
---|
1595 | /* Skip the backslash, newline and
|
---|
1596 | any following whitespace. */
|
---|
1597 | in = next_token (in + 2);
|
---|
1598 |
|
---|
1599 | /* Discard any preceding whitespace that has
|
---|
1600 | already been written to the output. */
|
---|
1601 | while (out > ref
|
---|
1602 | && isblank ((unsigned char)out[-1]))
|
---|
1603 | --out;
|
---|
1604 |
|
---|
1605 | /* Replace it all with a single space. */
|
---|
1606 | *out++ = ' ';
|
---|
1607 | }
|
---|
1608 | }
|
---|
1609 | else
|
---|
1610 | {
|
---|
1611 | if (*in == openparen)
|
---|
1612 | ++count;
|
---|
1613 |
|
---|
1614 | *out++ = *in++;
|
---|
1615 | }
|
---|
1616 | }
|
---|
1617 | }
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 | /* There are no more references in this line to worry about.
|
---|
1621 | Copy the remaining uninteresting text to the output. */
|
---|
1622 | if (out != in)
|
---|
1623 | strcpy (out, in);
|
---|
1624 |
|
---|
1625 | /* Finally, expand the line. */
|
---|
1626 | lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
|
---|
1627 | file);
|
---|
1628 | }
|
---|
1629 |
|
---|
1630 | /* Start the command sequence, record it in a new
|
---|
1631 | `struct child', and add that to the chain. */
|
---|
1632 |
|
---|
1633 | c = (struct child *) xmalloc (sizeof (struct child));
|
---|
1634 | bzero ((char *)c, sizeof (struct child));
|
---|
1635 | c->file = file;
|
---|
1636 | c->command_lines = lines;
|
---|
1637 | c->sh_batch_file = NULL;
|
---|
1638 |
|
---|
1639 | /* Fetch the first command line to be run. */
|
---|
1640 | job_next_command (c);
|
---|
1641 |
|
---|
1642 | /* Wait for a job slot to be freed up. If we allow an infinite number
|
---|
1643 | don't bother; also job_slots will == 0 if we're using the jobserver. */
|
---|
1644 |
|
---|
1645 | if (job_slots != 0)
|
---|
1646 | while (job_slots_used == job_slots)
|
---|
1647 | reap_children (1, 0);
|
---|
1648 |
|
---|
1649 | #ifdef MAKE_JOBSERVER
|
---|
1650 | /* If we are controlling multiple jobs make sure we have a token before
|
---|
1651 | starting the child. */
|
---|
1652 |
|
---|
1653 | /* This can be inefficient. There's a decent chance that this job won't
|
---|
1654 | actually have to run any subprocesses: the command script may be empty
|
---|
1655 | or otherwise optimized away. It would be nice if we could defer
|
---|
1656 | obtaining a token until just before we need it, in start_job_command.
|
---|
1657 | To do that we'd need to keep track of whether we'd already obtained a
|
---|
1658 | token (since start_job_command is called for each line of the job, not
|
---|
1659 | just once). Also more thought needs to go into the entire algorithm;
|
---|
1660 | this is where the old parallel job code waits, so... */
|
---|
1661 |
|
---|
1662 | else if (job_fds[0] >= 0)
|
---|
1663 | while (1)
|
---|
1664 | {
|
---|
1665 | char token;
|
---|
1666 | int got_token;
|
---|
1667 | int saved_errno;
|
---|
1668 |
|
---|
1669 | DB (DB_JOBS, ("Need a job token; we %shave children\n",
|
---|
1670 | children ? "" : "don't "));
|
---|
1671 |
|
---|
1672 | /* If we don't already have a job started, use our "free" token. */
|
---|
1673 | if (!children)
|
---|
1674 | break;
|
---|
1675 |
|
---|
1676 | /* Read a token. As long as there's no token available we'll block.
|
---|
1677 | We enable interruptible system calls before the read(2) so that if
|
---|
1678 | we get a SIGCHLD while we're waiting, we'll return with EINTR and
|
---|
1679 | we can process the death(s) and return tokens to the free pool.
|
---|
1680 |
|
---|
1681 | Once we return from the read, we immediately reinstate restartable
|
---|
1682 | system calls. This allows us to not worry about checking for
|
---|
1683 | EINTR on all the other system calls in the program.
|
---|
1684 |
|
---|
1685 | There is one other twist: there is a span between the time
|
---|
1686 | reap_children() does its last check for dead children and the time
|
---|
1687 | the read(2) call is entered, below, where if a child dies we won't
|
---|
1688 | notice. This is extremely serious as it could cause us to
|
---|
1689 | deadlock, given the right set of events.
|
---|
1690 |
|
---|
1691 | To avoid this, we do the following: before we reap_children(), we
|
---|
1692 | dup(2) the read FD on the jobserver pipe. The read(2) call below
|
---|
1693 | uses that new FD. In the signal handler, we close that FD. That
|
---|
1694 | way, if a child dies during the section mentioned above, the
|
---|
1695 | read(2) will be invoked with an invalid FD and will return
|
---|
1696 | immediately with EBADF. */
|
---|
1697 |
|
---|
1698 | /* Make sure we have a dup'd FD. */
|
---|
1699 | if (job_rfd < 0)
|
---|
1700 | {
|
---|
1701 | DB (DB_JOBS, ("Duplicate the job FD\n"));
|
---|
1702 | job_rfd = dup (job_fds[0]);
|
---|
1703 | }
|
---|
1704 |
|
---|
1705 | /* Reap anything that's currently waiting. */
|
---|
1706 | reap_children (0, 0);
|
---|
1707 |
|
---|
1708 | /* If our "free" token has become available, use it. */
|
---|
1709 | if (!children)
|
---|
1710 | break;
|
---|
1711 |
|
---|
1712 | /* Set interruptible system calls, and read() for a job token. */
|
---|
1713 | set_child_handler_action_flags (0);
|
---|
1714 | got_token = read (job_rfd, &token, 1);
|
---|
1715 | saved_errno = errno;
|
---|
1716 | #if defined(__EMX__) && !defined(__INNOTEK_LIBC__)
|
---|
1717 | /* The child handler must be turned off here. */
|
---|
1718 | signal (SIGCHLD, SIG_DFL);
|
---|
1719 | #endif
|
---|
1720 | set_child_handler_action_flags (SA_RESTART);
|
---|
1721 |
|
---|
1722 | /* If we got one, we're done here. */
|
---|
1723 | if (got_token == 1)
|
---|
1724 | {
|
---|
1725 | DB (DB_JOBS, (_("Obtained token for child 0x%08lx (%s).\n"),
|
---|
1726 | (unsigned long int) c, c->file->name));
|
---|
1727 | break;
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 | /* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,
|
---|
1731 | go back and reap_children(), and try again. */
|
---|
1732 | errno = saved_errno;
|
---|
1733 | if (errno != EINTR && errno != EBADF)
|
---|
1734 | pfatal_with_name (_("read jobs pipe"));
|
---|
1735 | if (errno == EBADF)
|
---|
1736 | DB (DB_JOBS, ("Read returned EBADF.\n"));
|
---|
1737 | }
|
---|
1738 | #endif
|
---|
1739 |
|
---|
1740 | /* The job is now primed. Start it running.
|
---|
1741 | (This will notice if there are in fact no commands.) */
|
---|
1742 | (void) start_waiting_job (c);
|
---|
1743 |
|
---|
1744 | if (job_slots == 1 || not_parallel < 0)
|
---|
1745 | /* Since there is only one job slot, make things run linearly.
|
---|
1746 | Wait for the child to die, setting the state to `cs_finished'. */
|
---|
1747 | while (file->command_state == cs_running)
|
---|
1748 | reap_children (1, 0);
|
---|
1749 |
|
---|
1750 | return;
|
---|
1751 | }
|
---|
1752 | |
---|
1753 |
|
---|
1754 | /* Move CHILD's pointers to the next command for it to execute.
|
---|
1755 | Returns nonzero if there is another command. */
|
---|
1756 |
|
---|
1757 | static int
|
---|
1758 | job_next_command (struct child *child)
|
---|
1759 | {
|
---|
1760 | while (child->command_ptr == 0 || *child->command_ptr == '\0')
|
---|
1761 | {
|
---|
1762 | /* There are no more lines in the expansion of this line. */
|
---|
1763 | if (child->command_line == child->file->cmds->ncommand_lines)
|
---|
1764 | {
|
---|
1765 | /* There are no more lines to be expanded. */
|
---|
1766 | child->command_ptr = 0;
|
---|
1767 | return 0;
|
---|
1768 | }
|
---|
1769 | else
|
---|
1770 | /* Get the next line to run. */
|
---|
1771 | child->command_ptr = child->command_lines[child->command_line++];
|
---|
1772 | }
|
---|
1773 | return 1;
|
---|
1774 | }
|
---|
1775 |
|
---|
1776 | /* Determine if the load average on the system is too high to start a new job.
|
---|
1777 | The real system load average is only recomputed once a second. However, a
|
---|
1778 | very parallel make can easily start tens or even hundreds of jobs in a
|
---|
1779 | second, which brings the system to its knees for a while until that first
|
---|
1780 | batch of jobs clears out.
|
---|
1781 |
|
---|
1782 | To avoid this we use a weighted algorithm to try to account for jobs which
|
---|
1783 | have been started since the last second, and guess what the load average
|
---|
1784 | would be now if it were computed.
|
---|
1785 |
|
---|
1786 | This algorithm was provided by Thomas Riedl <[email protected]>,
|
---|
1787 | who writes:
|
---|
1788 |
|
---|
1789 | ! calculate something load-oid and add to the observed sys.load,
|
---|
1790 | ! so that latter can catch up:
|
---|
1791 | ! - every job started increases jobctr;
|
---|
1792 | ! - every dying job decreases a positive jobctr;
|
---|
1793 | ! - the jobctr value gets zeroed every change of seconds,
|
---|
1794 | ! after its value*weight_b is stored into the 'backlog' value last_sec
|
---|
1795 | ! - weight_a times the sum of jobctr and last_sec gets
|
---|
1796 | ! added to the observed sys.load.
|
---|
1797 | !
|
---|
1798 | ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
|
---|
1799 | ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
|
---|
1800 | ! sub-shelled commands (rm, echo, sed...) for tests.
|
---|
1801 | ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
|
---|
1802 | ! resulted in significant excession of the load limit, raising it
|
---|
1803 | ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
|
---|
1804 | ! reach the limit in most test cases.
|
---|
1805 | !
|
---|
1806 | ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
|
---|
1807 | ! exceeding the limit for longer-running stuff (compile jobs in
|
---|
1808 | ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
|
---|
1809 | ! small jobs' effects.
|
---|
1810 |
|
---|
1811 | */
|
---|
1812 |
|
---|
1813 | #define LOAD_WEIGHT_A 0.25
|
---|
1814 | #define LOAD_WEIGHT_B 0.25
|
---|
1815 |
|
---|
1816 | static int
|
---|
1817 | load_too_high (void)
|
---|
1818 | {
|
---|
1819 | #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA)
|
---|
1820 | return 1;
|
---|
1821 | #else
|
---|
1822 | static double last_sec;
|
---|
1823 | static time_t last_now;
|
---|
1824 | double load, guess;
|
---|
1825 | time_t now;
|
---|
1826 |
|
---|
1827 | if (max_load_average < 0)
|
---|
1828 | return 0;
|
---|
1829 |
|
---|
1830 | /* Find the real system load average. */
|
---|
1831 | make_access ();
|
---|
1832 | if (getloadavg (&load, 1) != 1)
|
---|
1833 | {
|
---|
1834 | static int lossage = -1;
|
---|
1835 | /* Complain only once for the same error. */
|
---|
1836 | if (lossage == -1 || errno != lossage)
|
---|
1837 | {
|
---|
1838 | if (errno == 0)
|
---|
1839 | /* An errno value of zero means getloadavg is just unsupported. */
|
---|
1840 | error (NILF,
|
---|
1841 | _("cannot enforce load limits on this operating system"));
|
---|
1842 | else
|
---|
1843 | perror_with_name (_("cannot enforce load limit: "), "getloadavg");
|
---|
1844 | }
|
---|
1845 | lossage = errno;
|
---|
1846 | load = 0;
|
---|
1847 | }
|
---|
1848 | user_access ();
|
---|
1849 |
|
---|
1850 | /* If we're in a new second zero the counter and correct the backlog
|
---|
1851 | value. Only keep the backlog for one extra second; after that it's 0. */
|
---|
1852 | now = time (NULL);
|
---|
1853 | if (last_now < now)
|
---|
1854 | {
|
---|
1855 | if (last_now == now - 1)
|
---|
1856 | last_sec = LOAD_WEIGHT_B * job_counter;
|
---|
1857 | else
|
---|
1858 | last_sec = 0.0;
|
---|
1859 |
|
---|
1860 | job_counter = 0;
|
---|
1861 | last_now = now;
|
---|
1862 | }
|
---|
1863 |
|
---|
1864 | /* Try to guess what the load would be right now. */
|
---|
1865 | guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
|
---|
1866 |
|
---|
1867 | DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
|
---|
1868 | guess, load, max_load_average));
|
---|
1869 |
|
---|
1870 | return guess >= max_load_average;
|
---|
1871 | #endif
|
---|
1872 | }
|
---|
1873 |
|
---|
1874 | /* Start jobs that are waiting for the load to be lower. */
|
---|
1875 |
|
---|
1876 | void
|
---|
1877 | start_waiting_jobs (void)
|
---|
1878 | {
|
---|
1879 | struct child *job;
|
---|
1880 |
|
---|
1881 | if (waiting_jobs == 0)
|
---|
1882 | return;
|
---|
1883 |
|
---|
1884 | do
|
---|
1885 | {
|
---|
1886 | /* Check for recently deceased descendants. */
|
---|
1887 | reap_children (0, 0);
|
---|
1888 |
|
---|
1889 | /* Take a job off the waiting list. */
|
---|
1890 | job = waiting_jobs;
|
---|
1891 | waiting_jobs = job->next;
|
---|
1892 |
|
---|
1893 | /* Try to start that job. We break out of the loop as soon
|
---|
1894 | as start_waiting_job puts one back on the waiting list. */
|
---|
1895 | }
|
---|
1896 | while (start_waiting_job (job) && waiting_jobs != 0);
|
---|
1897 |
|
---|
1898 | return;
|
---|
1899 | }
|
---|
1900 | |
---|
1901 |
|
---|
1902 | #ifndef WINDOWS32
|
---|
1903 | #ifdef VMS
|
---|
1904 | #include <descrip.h>
|
---|
1905 | #include <clidef.h>
|
---|
1906 |
|
---|
1907 | /* This is called as an AST when a child process dies (it won't get
|
---|
1908 | interrupted by anything except a higher level AST).
|
---|
1909 | */
|
---|
1910 | int vmsHandleChildTerm(struct child *child)
|
---|
1911 | {
|
---|
1912 | int status;
|
---|
1913 | register struct child *lastc, *c;
|
---|
1914 | int child_failed;
|
---|
1915 |
|
---|
1916 | vms_jobsefnmask &= ~(1 << (child->efn - 32));
|
---|
1917 |
|
---|
1918 | lib$free_ef(&child->efn);
|
---|
1919 |
|
---|
1920 | (void) sigblock (fatal_signal_mask);
|
---|
1921 |
|
---|
1922 | child_failed = !(child->cstatus & 1 || ((child->cstatus & 7) == 0));
|
---|
1923 |
|
---|
1924 | /* Search for a child matching the deceased one. */
|
---|
1925 | lastc = 0;
|
---|
1926 | #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
|
---|
1927 | for (c = children; c != 0 && c != child; lastc = c, c = c->next);
|
---|
1928 | #else
|
---|
1929 | c = child;
|
---|
1930 | #endif
|
---|
1931 |
|
---|
1932 | if (child_failed && !c->noerror && !ignore_errors_flag)
|
---|
1933 | {
|
---|
1934 | /* The commands failed. Write an error message,
|
---|
1935 | delete non-precious targets, and abort. */
|
---|
1936 | child_error (c->file->name, c->cstatus, 0, 0, 0);
|
---|
1937 | c->file->update_status = 1;
|
---|
1938 | delete_child_targets (c);
|
---|
1939 | }
|
---|
1940 | else
|
---|
1941 | {
|
---|
1942 | if (child_failed)
|
---|
1943 | {
|
---|
1944 | /* The commands failed, but we don't care. */
|
---|
1945 | child_error (c->file->name, c->cstatus, 0, 0, 1);
|
---|
1946 | child_failed = 0;
|
---|
1947 | }
|
---|
1948 |
|
---|
1949 | #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
|
---|
1950 | /* If there are more commands to run, try to start them. */
|
---|
1951 | start_job (c);
|
---|
1952 |
|
---|
1953 | switch (c->file->command_state)
|
---|
1954 | {
|
---|
1955 | case cs_running:
|
---|
1956 | /* Successfully started. */
|
---|
1957 | break;
|
---|
1958 |
|
---|
1959 | case cs_finished:
|
---|
1960 | if (c->file->update_status != 0) {
|
---|
1961 | /* We failed to start the commands. */
|
---|
1962 | delete_child_targets (c);
|
---|
1963 | }
|
---|
1964 | break;
|
---|
1965 |
|
---|
1966 | default:
|
---|
1967 | error (NILF, _("internal error: `%s' command_state"),
|
---|
1968 | c->file->name);
|
---|
1969 | abort ();
|
---|
1970 | break;
|
---|
1971 | }
|
---|
1972 | #endif /* RECURSIVEJOBS */
|
---|
1973 | }
|
---|
1974 |
|
---|
1975 | /* Set the state flag to say the commands have finished. */
|
---|
1976 | c->file->command_state = cs_finished;
|
---|
1977 | notice_finished_file (c->file);
|
---|
1978 |
|
---|
1979 | #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
|
---|
1980 | /* Remove the child from the chain and free it. */
|
---|
1981 | if (lastc == 0)
|
---|
1982 | children = c->next;
|
---|
1983 | else
|
---|
1984 | lastc->next = c->next;
|
---|
1985 | free_child (c);
|
---|
1986 | #endif /* RECURSIVEJOBS */
|
---|
1987 |
|
---|
1988 | /* There is now another slot open. */
|
---|
1989 | if (job_slots_used > 0)
|
---|
1990 | --job_slots_used;
|
---|
1991 |
|
---|
1992 | /* If the job failed, and the -k flag was not given, die. */
|
---|
1993 | if (child_failed && !keep_going_flag)
|
---|
1994 | die (EXIT_FAILURE);
|
---|
1995 |
|
---|
1996 | (void) sigsetmask (sigblock (0) & ~(fatal_signal_mask));
|
---|
1997 |
|
---|
1998 | return 1;
|
---|
1999 | }
|
---|
2000 |
|
---|
2001 | /* VMS:
|
---|
2002 | Spawn a process executing the command in ARGV and return its pid. */
|
---|
2003 |
|
---|
2004 | #define MAXCMDLEN 200
|
---|
2005 |
|
---|
2006 | /* local helpers to make ctrl+c and ctrl+y working, see below */
|
---|
2007 | #include <iodef.h>
|
---|
2008 | #include <libclidef.h>
|
---|
2009 | #include <ssdef.h>
|
---|
2010 |
|
---|
2011 | static int ctrlMask= LIB$M_CLI_CTRLY;
|
---|
2012 | static int oldCtrlMask;
|
---|
2013 | static int setupYAstTried= 0;
|
---|
2014 | static int pidToAbort= 0;
|
---|
2015 | static int chan= 0;
|
---|
2016 |
|
---|
2017 | static void reEnableAst(void) {
|
---|
2018 | lib$enable_ctrl (&oldCtrlMask,0);
|
---|
2019 | }
|
---|
2020 |
|
---|
2021 | static astHandler (void) {
|
---|
2022 | if (pidToAbort) {
|
---|
2023 | sys$forcex (&pidToAbort, 0, SS$_ABORT);
|
---|
2024 | pidToAbort= 0;
|
---|
2025 | }
|
---|
2026 | kill (getpid(),SIGQUIT);
|
---|
2027 | }
|
---|
2028 |
|
---|
2029 | static void tryToSetupYAst(void) {
|
---|
2030 | $DESCRIPTOR(inputDsc,"SYS$COMMAND");
|
---|
2031 | int status;
|
---|
2032 | struct {
|
---|
2033 | short int status, count;
|
---|
2034 | int dvi;
|
---|
2035 | } iosb;
|
---|
2036 |
|
---|
2037 | setupYAstTried++;
|
---|
2038 |
|
---|
2039 | if (!chan) {
|
---|
2040 | status= sys$assign(&inputDsc,&chan,0,0);
|
---|
2041 | if (!(status&SS$_NORMAL)) {
|
---|
2042 | lib$signal(status);
|
---|
2043 | return;
|
---|
2044 | }
|
---|
2045 | }
|
---|
2046 | status= sys$qiow (0, chan, IO$_SETMODE|IO$M_CTRLYAST,&iosb,0,0,
|
---|
2047 | astHandler,0,0,0,0,0);
|
---|
2048 | if (status==SS$_ILLIOFUNC) {
|
---|
2049 | sys$dassgn(chan);
|
---|
2050 | #ifdef CTRLY_ENABLED_ANYWAY
|
---|
2051 | fprintf (stderr,
|
---|
2052 | _("-warning, CTRL-Y will leave sub-process(es) around.\n"));
|
---|
2053 | #else
|
---|
2054 | return;
|
---|
2055 | #endif
|
---|
2056 | }
|
---|
2057 | if (status==SS$_NORMAL)
|
---|
2058 | status= iosb.status;
|
---|
2059 | if (!(status&SS$_NORMAL)) {
|
---|
2060 | lib$signal(status);
|
---|
2061 | return;
|
---|
2062 | }
|
---|
2063 |
|
---|
2064 | /* called from AST handler ? */
|
---|
2065 | if (setupYAstTried>1)
|
---|
2066 | return;
|
---|
2067 | if (atexit(reEnableAst))
|
---|
2068 | fprintf (stderr,
|
---|
2069 | _("-warning, you may have to re-enable CTRL-Y handling from DCL.\n"));
|
---|
2070 | status= lib$disable_ctrl (&ctrlMask, &oldCtrlMask);
|
---|
2071 | if (!(status&SS$_NORMAL)) {
|
---|
2072 | lib$signal(status);
|
---|
2073 | return;
|
---|
2074 | }
|
---|
2075 | }
|
---|
2076 | int
|
---|
2077 | child_execute_job (char *argv, struct child *child)
|
---|
2078 | {
|
---|
2079 | int i;
|
---|
2080 | static struct dsc$descriptor_s cmddsc;
|
---|
2081 | static struct dsc$descriptor_s pnamedsc;
|
---|
2082 | static struct dsc$descriptor_s ifiledsc;
|
---|
2083 | static struct dsc$descriptor_s ofiledsc;
|
---|
2084 | static struct dsc$descriptor_s efiledsc;
|
---|
2085 | int have_redirection = 0;
|
---|
2086 | int have_newline = 0;
|
---|
2087 |
|
---|
2088 | int spflags = CLI$M_NOWAIT;
|
---|
2089 | int status;
|
---|
2090 | char *cmd = alloca (strlen (argv) + 512), *p, *q;
|
---|
2091 | char ifile[256], ofile[256], efile[256];
|
---|
2092 | char *comname = 0;
|
---|
2093 | char procname[100];
|
---|
2094 |
|
---|
2095 | /* Parse IO redirection. */
|
---|
2096 |
|
---|
2097 | ifile[0] = 0;
|
---|
2098 | ofile[0] = 0;
|
---|
2099 | efile[0] = 0;
|
---|
2100 |
|
---|
2101 | DB (DB_JOBS, ("child_execute_job (%s)\n", argv));
|
---|
2102 |
|
---|
2103 | while (isspace ((unsigned char)*argv))
|
---|
2104 | argv++;
|
---|
2105 |
|
---|
2106 | if (*argv == 0)
|
---|
2107 | return 0;
|
---|
2108 |
|
---|
2109 | sprintf (procname, "GMAKE_%05x", getpid () & 0xfffff);
|
---|
2110 | pnamedsc.dsc$w_length = strlen(procname);
|
---|
2111 | pnamedsc.dsc$a_pointer = procname;
|
---|
2112 | pnamedsc.dsc$b_dtype = DSC$K_DTYPE_T;
|
---|
2113 | pnamedsc.dsc$b_class = DSC$K_CLASS_S;
|
---|
2114 |
|
---|
2115 | /* Handle comments and redirection. */
|
---|
2116 | for (p = argv, q = cmd; *p; p++, q++)
|
---|
2117 | {
|
---|
2118 | switch (*p)
|
---|
2119 | {
|
---|
2120 | case '#':
|
---|
2121 | *p-- = 0;
|
---|
2122 | *q-- = 0;
|
---|
2123 | break;
|
---|
2124 | case '\\':
|
---|
2125 | p++;
|
---|
2126 | if (*p == '\n')
|
---|
2127 | p++;
|
---|
2128 | if (isspace ((unsigned char)*p))
|
---|
2129 | {
|
---|
2130 | do { p++; } while (isspace ((unsigned char)*p));
|
---|
2131 | p--;
|
---|
2132 | }
|
---|
2133 | *q = *p;
|
---|
2134 | break;
|
---|
2135 | case '<':
|
---|
2136 | p = vms_redirect (&ifiledsc, ifile, p);
|
---|
2137 | *q = ' ';
|
---|
2138 | have_redirection = 1;
|
---|
2139 | break;
|
---|
2140 | case '>':
|
---|
2141 | have_redirection = 1;
|
---|
2142 | if (*(p-1) == '2')
|
---|
2143 | {
|
---|
2144 | q--;
|
---|
2145 | if (strncmp (p, ">&1", 3) == 0)
|
---|
2146 | {
|
---|
2147 | p += 3;
|
---|
2148 | strcpy (efile, "sys$output");
|
---|
2149 | efiledsc.dsc$w_length = strlen(efile);
|
---|
2150 | efiledsc.dsc$a_pointer = efile;
|
---|
2151 | efiledsc.dsc$b_dtype = DSC$K_DTYPE_T;
|
---|
2152 | efiledsc.dsc$b_class = DSC$K_CLASS_S;
|
---|
2153 | }
|
---|
2154 | else
|
---|
2155 | {
|
---|
2156 | p = vms_redirect (&efiledsc, efile, p);
|
---|
2157 | }
|
---|
2158 | }
|
---|
2159 | else
|
---|
2160 | {
|
---|
2161 | p = vms_redirect (&ofiledsc, ofile, p);
|
---|
2162 | }
|
---|
2163 | *q = ' ';
|
---|
2164 | break;
|
---|
2165 | case '\n':
|
---|
2166 | have_newline = 1;
|
---|
2167 | default:
|
---|
2168 | *q = *p;
|
---|
2169 | break;
|
---|
2170 | }
|
---|
2171 | }
|
---|
2172 | *q = *p;
|
---|
2173 |
|
---|
2174 | if (strncmp (cmd, "builtin_", 8) == 0)
|
---|
2175 | {
|
---|
2176 | child->pid = 270163;
|
---|
2177 | child->efn = 0;
|
---|
2178 | child->cstatus = 1;
|
---|
2179 |
|
---|
2180 | DB (DB_JOBS, (_("BUILTIN [%s][%s]\n"), cmd, cmd+8));
|
---|
2181 |
|
---|
2182 | p = cmd + 8;
|
---|
2183 |
|
---|
2184 | if ((*(p) == 'c')
|
---|
2185 | && (*(p+1) == 'd')
|
---|
2186 | && ((*(p+2) == ' ') || (*(p+2) == '\t')))
|
---|
2187 | {
|
---|
2188 | p += 3;
|
---|
2189 | while ((*p == ' ') || (*p == '\t'))
|
---|
2190 | p++;
|
---|
2191 | DB (DB_JOBS, (_("BUILTIN CD %s\n"), p));
|
---|
2192 | if (chdir (p))
|
---|
2193 | return 0;
|
---|
2194 | else
|
---|
2195 | return 1;
|
---|
2196 | }
|
---|
2197 | else if ((*(p) == 'r')
|
---|
2198 | && (*(p+1) == 'm')
|
---|
2199 | && ((*(p+2) == ' ') || (*(p+2) == '\t')))
|
---|
2200 | {
|
---|
2201 | int in_arg;
|
---|
2202 |
|
---|
2203 | /* rm */
|
---|
2204 | p += 3;
|
---|
2205 | while ((*p == ' ') || (*p == '\t'))
|
---|
2206 | p++;
|
---|
2207 | in_arg = 1;
|
---|
2208 |
|
---|
2209 | DB (DB_JOBS, (_("BUILTIN RM %s\n"), p));
|
---|
2210 | while (*p)
|
---|
2211 | {
|
---|
2212 | switch (*p)
|
---|
2213 | {
|
---|
2214 | case ' ':
|
---|
2215 | case '\t':
|
---|
2216 | if (in_arg)
|
---|
2217 | {
|
---|
2218 | *p++ = ';';
|
---|
2219 | in_arg = 0;
|
---|
2220 | }
|
---|
2221 | break;
|
---|
2222 | default:
|
---|
2223 | break;
|
---|
2224 | }
|
---|
2225 | p++;
|
---|
2226 | }
|
---|
2227 | }
|
---|
2228 | else
|
---|
2229 | {
|
---|
2230 | printf(_("Unknown builtin command '%s'\n"), cmd);
|
---|
2231 | fflush(stdout);
|
---|
2232 | return 0;
|
---|
2233 | }
|
---|
2234 | }
|
---|
2235 |
|
---|
2236 | /* Create a *.com file if either the command is too long for
|
---|
2237 | lib$spawn, or the command contains a newline, or if redirection
|
---|
2238 | is desired. Forcing commands with newlines into DCLs allows to
|
---|
2239 | store search lists on user mode logicals. */
|
---|
2240 |
|
---|
2241 | if (strlen (cmd) > MAXCMDLEN
|
---|
2242 | || (have_redirection != 0)
|
---|
2243 | || (have_newline != 0))
|
---|
2244 | {
|
---|
2245 | FILE *outfile;
|
---|
2246 | char c;
|
---|
2247 | char *sep;
|
---|
2248 | int alevel = 0; /* apostrophe level */
|
---|
2249 |
|
---|
2250 | if (strlen (cmd) == 0)
|
---|
2251 | {
|
---|
2252 | printf (_("Error, empty command\n"));
|
---|
2253 | fflush (stdout);
|
---|
2254 | return 0;
|
---|
2255 | }
|
---|
2256 |
|
---|
2257 | outfile = open_tmpfile (&comname, "sys$scratch:CMDXXXXXX.COM");
|
---|
2258 | if (outfile == 0)
|
---|
2259 | pfatal_with_name (_("fopen (temporary file)"));
|
---|
2260 |
|
---|
2261 | if (ifile[0])
|
---|
2262 | {
|
---|
2263 | fprintf (outfile, "$ assign/user %s sys$input\n", ifile);
|
---|
2264 | DB (DB_JOBS, (_("Redirected input from %s\n"), ifile));
|
---|
2265 | ifiledsc.dsc$w_length = 0;
|
---|
2266 | }
|
---|
2267 |
|
---|
2268 | if (efile[0])
|
---|
2269 | {
|
---|
2270 | fprintf (outfile, "$ define sys$error %s\n", efile);
|
---|
2271 | DB (DB_JOBS, (_("Redirected error to %s\n"), efile));
|
---|
2272 | efiledsc.dsc$w_length = 0;
|
---|
2273 | }
|
---|
2274 |
|
---|
2275 | if (ofile[0])
|
---|
2276 | {
|
---|
2277 | fprintf (outfile, "$ define sys$output %s\n", ofile);
|
---|
2278 | DB (DB_JOBS, (_("Redirected output to %s\n"), ofile));
|
---|
2279 | ofiledsc.dsc$w_length = 0;
|
---|
2280 | }
|
---|
2281 |
|
---|
2282 | p = sep = q = cmd;
|
---|
2283 | for (c = '\n'; c; c = *q++)
|
---|
2284 | {
|
---|
2285 | switch (c)
|
---|
2286 | {
|
---|
2287 | case '\n':
|
---|
2288 | /* At a newline, skip any whitespace around a leading $
|
---|
2289 | from the command and issue exactly one $ into the DCL. */
|
---|
2290 | while (isspace ((unsigned char)*p))
|
---|
2291 | p++;
|
---|
2292 | if (*p == '$')
|
---|
2293 | p++;
|
---|
2294 | while (isspace ((unsigned char)*p))
|
---|
2295 | p++;
|
---|
2296 | fwrite (p, 1, q - p, outfile);
|
---|
2297 | fputc ('$', outfile);
|
---|
2298 | fputc (' ', outfile);
|
---|
2299 | /* Reset variables. */
|
---|
2300 | p = sep = q;
|
---|
2301 | break;
|
---|
2302 |
|
---|
2303 | /* Nice places for line breaks are after strings, after
|
---|
2304 | comma or space and before slash. */
|
---|
2305 | case '"':
|
---|
2306 | q = vms_handle_apos (q);
|
---|
2307 | sep = q;
|
---|
2308 | break;
|
---|
2309 | case ',':
|
---|
2310 | case ' ':
|
---|
2311 | sep = q;
|
---|
2312 | break;
|
---|
2313 | case '/':
|
---|
2314 | case '\0':
|
---|
2315 | sep = q - 1;
|
---|
2316 | break;
|
---|
2317 | default:
|
---|
2318 | break;
|
---|
2319 | }
|
---|
2320 | if (sep - p > 78)
|
---|
2321 | {
|
---|
2322 | /* Enough stuff for a line. */
|
---|
2323 | fwrite (p, 1, sep - p, outfile);
|
---|
2324 | p = sep;
|
---|
2325 | if (*sep)
|
---|
2326 | {
|
---|
2327 | /* The command continues. */
|
---|
2328 | fputc ('-', outfile);
|
---|
2329 | }
|
---|
2330 | fputc ('\n', outfile);
|
---|
2331 | }
|
---|
2332 | }
|
---|
2333 |
|
---|
2334 | fwrite (p, 1, q - p, outfile);
|
---|
2335 | fputc ('\n', outfile);
|
---|
2336 |
|
---|
2337 | fclose (outfile);
|
---|
2338 |
|
---|
2339 | sprintf (cmd, "$ @%s", comname);
|
---|
2340 |
|
---|
2341 | DB (DB_JOBS, (_("Executing %s instead\n"), cmd));
|
---|
2342 | }
|
---|
2343 |
|
---|
2344 | cmddsc.dsc$w_length = strlen(cmd);
|
---|
2345 | cmddsc.dsc$a_pointer = cmd;
|
---|
2346 | cmddsc.dsc$b_dtype = DSC$K_DTYPE_T;
|
---|
2347 | cmddsc.dsc$b_class = DSC$K_CLASS_S;
|
---|
2348 |
|
---|
2349 | child->efn = 0;
|
---|
2350 | while (child->efn < 32 || child->efn > 63)
|
---|
2351 | {
|
---|
2352 | status = lib$get_ef ((unsigned long *)&child->efn);
|
---|
2353 | if (!(status & 1))
|
---|
2354 | return 0;
|
---|
2355 | }
|
---|
2356 |
|
---|
2357 | sys$clref (child->efn);
|
---|
2358 |
|
---|
2359 | vms_jobsefnmask |= (1 << (child->efn - 32));
|
---|
2360 |
|
---|
2361 | /*
|
---|
2362 | LIB$SPAWN [command-string]
|
---|
2363 | [,input-file]
|
---|
2364 | [,output-file]
|
---|
2365 | [,flags]
|
---|
2366 | [,process-name]
|
---|
2367 | [,process-id] [,completion-status-address] [,byte-integer-event-flag-num]
|
---|
2368 | [,AST-address] [,varying-AST-argument]
|
---|
2369 | [,prompt-string] [,cli] [,table]
|
---|
2370 | */
|
---|
2371 |
|
---|
2372 | #ifndef DONTWAITFORCHILD
|
---|
2373 | /*
|
---|
2374 | * Code to make ctrl+c and ctrl+y working.
|
---|
2375 | * The problem starts with the synchronous case where after lib$spawn is
|
---|
2376 | * called any input will go to the child. But with input re-directed,
|
---|
2377 | * both control characters won't make it to any of the programs, neither
|
---|
2378 | * the spawning nor to the spawned one. Hence the caller needs to spawn
|
---|
2379 | * with CLI$M_NOWAIT to NOT give up the input focus. A sys$waitfr
|
---|
2380 | * has to follow to simulate the wanted synchronous behaviour.
|
---|
2381 | * The next problem is ctrl+y which isn't caught by the crtl and
|
---|
2382 | * therefore isn't converted to SIGQUIT (for a signal handler which is
|
---|
2383 | * already established). The only way to catch ctrl+y, is an AST
|
---|
2384 | * assigned to the input channel. But ctrl+y handling of DCL needs to be
|
---|
2385 | * disabled, otherwise it will handle it. Not to mention the previous
|
---|
2386 | * ctrl+y handling of DCL needs to be re-established before make exits.
|
---|
2387 | * One more: At the time of LIB$SPAWN signals are blocked. SIGQUIT will
|
---|
2388 | * make it to the signal handler after the child "normally" terminates.
|
---|
2389 | * This isn't enough. It seems reasonable for simple command lines like
|
---|
2390 | * a 'cc foobar.c' spawned in a subprocess but it is unacceptable for
|
---|
2391 | * spawning make. Therefore we need to abort the process in the AST.
|
---|
2392 | *
|
---|
2393 | * Prior to the spawn it is checked if an AST is already set up for
|
---|
2394 | * ctrl+y, if not one is set up for a channel to SYS$COMMAND. In general
|
---|
2395 | * this will work except if make is run in a batch environment, but there
|
---|
2396 | * nobody can press ctrl+y. During the setup the DCL handling of ctrl+y
|
---|
2397 | * is disabled and an exit handler is established to re-enable it.
|
---|
2398 | * If the user interrupts with ctrl+y, the assigned AST will fire, force
|
---|
2399 | * an abort to the subprocess and signal SIGQUIT, which will be caught by
|
---|
2400 | * the already established handler and will bring us back to common code.
|
---|
2401 | * After the spawn (now /nowait) a sys$waitfr simulates the /wait and
|
---|
2402 | * enables the ctrl+y be delivered to this code. And the ctrl+c too,
|
---|
2403 | * which the crtl converts to SIGINT and which is caught by the common
|
---|
2404 | * signal handler. Because signals were blocked before entering this code
|
---|
2405 | * sys$waitfr will always complete and the SIGQUIT will be processed after
|
---|
2406 | * it (after termination of the current block, somewhere in common code).
|
---|
2407 | * And SIGINT too will be delayed. That is ctrl+c can only abort when the
|
---|
2408 | * current command completes. Anyway it's better than nothing :-)
|
---|
2409 | */
|
---|
2410 |
|
---|
2411 | if (!setupYAstTried)
|
---|
2412 | tryToSetupYAst();
|
---|
2413 | status = lib$spawn (&cmddsc, /* cmd-string */
|
---|
2414 | (ifiledsc.dsc$w_length == 0)?0:&ifiledsc, /* input-file */
|
---|
2415 | (ofiledsc.dsc$w_length == 0)?0:&ofiledsc, /* output-file */
|
---|
2416 | &spflags, /* flags */
|
---|
2417 | &pnamedsc, /* proc name */
|
---|
2418 | &child->pid, &child->cstatus, &child->efn,
|
---|
2419 | 0, 0,
|
---|
2420 | 0, 0, 0);
|
---|
2421 | if (status & 1)
|
---|
2422 | {
|
---|
2423 | pidToAbort= child->pid;
|
---|
2424 | status= sys$waitfr (child->efn);
|
---|
2425 | pidToAbort= 0;
|
---|
2426 | vmsHandleChildTerm(child);
|
---|
2427 | }
|
---|
2428 | #else
|
---|
2429 | status = lib$spawn (&cmddsc,
|
---|
2430 | (ifiledsc.dsc$w_length == 0)?0:&ifiledsc,
|
---|
2431 | (ofiledsc.dsc$w_length == 0)?0:&ofiledsc,
|
---|
2432 | &spflags,
|
---|
2433 | &pnamedsc,
|
---|
2434 | &child->pid, &child->cstatus, &child->efn,
|
---|
2435 | vmsHandleChildTerm, child,
|
---|
2436 | 0, 0, 0);
|
---|
2437 | #endif
|
---|
2438 |
|
---|
2439 | if (!(status & 1))
|
---|
2440 | {
|
---|
2441 | printf (_("Error spawning, %d\n") ,status);
|
---|
2442 | fflush (stdout);
|
---|
2443 | switch (status)
|
---|
2444 | {
|
---|
2445 | case 0x1c:
|
---|
2446 | errno = EPROCLIM;
|
---|
2447 | break;
|
---|
2448 | default:
|
---|
2449 | errno = EFAIL;
|
---|
2450 | }
|
---|
2451 | }
|
---|
2452 |
|
---|
2453 | if (comname && !ISDB (DB_JOBS))
|
---|
2454 | unlink (comname);
|
---|
2455 |
|
---|
2456 | return (status & 1);
|
---|
2457 | }
|
---|
2458 |
|
---|
2459 | #else /* !VMS */
|
---|
2460 |
|
---|
2461 | /* EMX: Start a child process. This function returns the new pid. */
|
---|
2462 | # if defined __MSDOS__ || defined __EMX__
|
---|
2463 | /* The child argument can be NULL (that's why we return the pid), if it is
|
---|
2464 | and the shell is a dllshell:// a child structure is created and inserted
|
---|
2465 | into the child list so reap_children can do its job.
|
---|
2466 |
|
---|
2467 | BTW. the name of this function in this port is very misleading, spawn_job
|
---|
2468 | would perhaps be more appropriate. */
|
---|
2469 |
|
---|
2470 | int
|
---|
2471 | child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp,
|
---|
2472 | struct child *child)
|
---|
2473 | {
|
---|
2474 | int pid;
|
---|
2475 | /* stdin_fd == 0 means: nothing to do for stdin;
|
---|
2476 | stdout_fd == 1 means: nothing to do for stdout */
|
---|
2477 | int save_stdin = (stdin_fd != 0) ? dup (0) : 0;
|
---|
2478 | int save_stdout = (stdout_fd != 1) ? dup (1): 1;
|
---|
2479 |
|
---|
2480 | /* < 0 only if dup() failed */
|
---|
2481 | if (save_stdin < 0)
|
---|
2482 | fatal (NILF, _("no more file handles: could not duplicate stdin\n"));
|
---|
2483 | if (save_stdout < 0)
|
---|
2484 | fatal (NILF, _("no more file handles: could not duplicate stdout\n"));
|
---|
2485 |
|
---|
2486 | /* Close unnecessary file handles for the child. */
|
---|
2487 | if (save_stdin != 0)
|
---|
2488 | CLOSE_ON_EXEC (save_stdin);
|
---|
2489 | if (save_stdout != 1)
|
---|
2490 | CLOSE_ON_EXEC (save_stdout);
|
---|
2491 |
|
---|
2492 | /* Connect the pipes to the child process. */
|
---|
2493 | if (stdin_fd != 0)
|
---|
2494 | (void) dup2 (stdin_fd, 0);
|
---|
2495 | if (stdout_fd != 1)
|
---|
2496 | (void) dup2 (stdout_fd, 1);
|
---|
2497 |
|
---|
2498 | /* stdin_fd and stdout_fd must be closed on exit because we are
|
---|
2499 | still in the parent process */
|
---|
2500 | if (stdin_fd != 0)
|
---|
2501 | CLOSE_ON_EXEC (stdin_fd);
|
---|
2502 | if (stdout_fd != 1)
|
---|
2503 | CLOSE_ON_EXEC (stdout_fd);
|
---|
2504 |
|
---|
2505 | #ifdef MAKE_DLLSHELL
|
---|
2506 | pid = spawn_command(argv, envp, child);
|
---|
2507 | #else
|
---|
2508 | /* Run the command. */
|
---|
2509 | pid = exec_command (argv, envp);
|
---|
2510 | #endif
|
---|
2511 |
|
---|
2512 | /* Restore stdout/stdin of the parent process. */
|
---|
2513 | if (stdin_fd != 0 && dup2 (save_stdin, 0) != 0)
|
---|
2514 | fatal (NILF, _("restoring of stdin failed\n"));
|
---|
2515 | if (stdout_fd != 1 && dup2 (save_stdout, 1) != 1)
|
---|
2516 | fatal (NILF, _("restoring of stdout failed\n"));
|
---|
2517 |
|
---|
2518 | /* Cleanup handles */
|
---|
2519 | if (stdin_fd != 0)
|
---|
2520 | close (save_stdin);
|
---|
2521 | if (stdout_fd != 1)
|
---|
2522 | close (save_stdout);
|
---|
2523 |
|
---|
2524 | return pid;
|
---|
2525 | }
|
---|
2526 |
|
---|
2527 | #elif !defined (_AMIGA) && !defined (__MSDOS__)
|
---|
2528 |
|
---|
2529 | /* UNIX:
|
---|
2530 | Replace the current process with one executing the command in ARGV.
|
---|
2531 | STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
|
---|
2532 | the environment of the new program. This function does not return. */
|
---|
2533 | void
|
---|
2534 | child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
|
---|
2535 | {
|
---|
2536 | if (stdin_fd != 0)
|
---|
2537 | (void) dup2 (stdin_fd, 0);
|
---|
2538 | if (stdout_fd != 1)
|
---|
2539 | (void) dup2 (stdout_fd, 1);
|
---|
2540 | if (stdin_fd != 0)
|
---|
2541 | (void) close (stdin_fd);
|
---|
2542 | if (stdout_fd != 1)
|
---|
2543 | (void) close (stdout_fd);
|
---|
2544 |
|
---|
2545 | /* Run the command. */
|
---|
2546 | exec_command (argv, envp);
|
---|
2547 | }
|
---|
2548 | #endif /* !AMIGA && !__MSDOS__ */
|
---|
2549 | #endif /* !VMS */
|
---|
2550 | #endif /* !WINDOWS32 */
|
---|
2551 | |
---|
2552 |
|
---|
2553 | #ifdef MAKE_DLLSHELL
|
---|
2554 | /* Globals for the currently loaded dllshell. */
|
---|
2555 | char *dllshell_spec;
|
---|
2556 | void *dllshell_dl;
|
---|
2557 | void *dllshell_instance;
|
---|
2558 | void *(*dllshell_init) PARAMS ((const char *spec));
|
---|
2559 | pid_t (*dllshell_spawn) PARAMS ((void *instance, char **argv, char **envp, int *status, char *done));
|
---|
2560 | pid_t (*dllshell_wait) PARAMS ((void *instance, int *status, int block));
|
---|
2561 |
|
---|
2562 | /* This is called when all pipes and such are configured for the
|
---|
2563 | child process. The child argument may be null, see child_execute_job. */
|
---|
2564 | static int spawn_command (char **argv, char **envp, struct child *c)
|
---|
2565 | {
|
---|
2566 | /* Now let's see if there is a DLLSHELL specifier in the
|
---|
2567 | first argument. */
|
---|
2568 | if (!strncmp(argv[0], "dllshell://", 11))
|
---|
2569 | {
|
---|
2570 | /* dllshell://<dllname>[!<realshell>[!whatever]] */
|
---|
2571 | char *name, *name_end;
|
---|
2572 | int insert_child = 0;
|
---|
2573 |
|
---|
2574 | /* parse it */
|
---|
2575 | name = argv[0] + 11;
|
---|
2576 | name_end = strchr (name, '!');
|
---|
2577 | if (!name_end)
|
---|
2578 | name_end = strchr (name, '\0');
|
---|
2579 | if (name_end == name)
|
---|
2580 | fatal (NILF, _("%s : malformed specifier!\n"), argv[0]);
|
---|
2581 |
|
---|
2582 | /* need loading? */
|
---|
2583 | if (!dllshell_spec || strcmp (argv[0], dllshell_spec))
|
---|
2584 | {
|
---|
2585 | if (dllshell_spec)
|
---|
2586 | fatal (NILF, _("cannot change the dllshell!!!\n"));
|
---|
2587 |
|
---|
2588 | dllshell_spec = strdup (argv[0]);
|
---|
2589 | dllshell_spec[name_end - argv[0]] = '\0';
|
---|
2590 | dllshell_dl = dlopen (dllshell_spec + (name - argv[0]), RTLD_LOCAL);
|
---|
2591 | if (!dllshell_dl)
|
---|
2592 | fatal (NILF, _("%s : failed to load! dlerror: '%s'\n"), argv[0], dlerror());
|
---|
2593 | dllshell_spec[name_end - name] = '!';
|
---|
2594 |
|
---|
2595 | /* get symbols */
|
---|
2596 | dllshell_init = dlsym (dllshell_dl, "dllshell_init");
|
---|
2597 | if (!dllshell_init)
|
---|
2598 | fatal (NILF, _("%s : failed to find symbols 'dllshell_init' dlerror: %s\n"), argv[0], dlerror());
|
---|
2599 | dllshell_spawn = dlsym (dllshell_dl, "dllshell_spawn");
|
---|
2600 | if (!dllshell_spawn)
|
---|
2601 | fatal (NILF, _("%s : failed to find symbols 'dllshell_spawn' dlerror: %s\n"), argv[0], dlerror());
|
---|
2602 | dllshell_wait = dlsym (dllshell_dl, "dllshell_wait");
|
---|
2603 | if (!dllshell_wait)
|
---|
2604 | fatal (NILF, _("%s : failed to find symbols 'dllshell_wait' dlerror: %s\n"), argv[0], dlerror());
|
---|
2605 |
|
---|
2606 | /* init */
|
---|
2607 | dllshell_instance = dllshell_init(dllshell_spec);
|
---|
2608 | if (!dllshell_instance)
|
---|
2609 | fatal (NILF, _("%s : init failed!!!\n"), argv[0]);
|
---|
2610 | }
|
---|
2611 |
|
---|
2612 | /* make child struct? */
|
---|
2613 | if (!c)
|
---|
2614 | {
|
---|
2615 | c = (struct child *) xmalloc (sizeof (struct child));
|
---|
2616 | bzero ((char *)c, sizeof (struct child));
|
---|
2617 | insert_child = 1;
|
---|
2618 | }
|
---|
2619 |
|
---|
2620 | /* call it. return value is 0 on succes, -1 on failure. */
|
---|
2621 | c->pid = dllshell_spawn (dllshell_instance, argv, envp, &c->status, &c->dllshell_done);
|
---|
2622 | DB (DB_JOBS, (_("dllshell pid=%x\n"), c->pid));
|
---|
2623 |
|
---|
2624 | if (insert_child && c->pid > 0)
|
---|
2625 | {
|
---|
2626 | c->next = children;
|
---|
2627 | DB (DB_JOBS, (_("Putting child 0x%08lx (-) PID %ld on the chain.\n"),
|
---|
2628 | (unsigned long int) c, (long) c->pid));
|
---|
2629 | children = c;
|
---|
2630 | /* One more job slot is in use. */
|
---|
2631 | ++job_slots_used;
|
---|
2632 | }
|
---|
2633 | }
|
---|
2634 | else
|
---|
2635 | {
|
---|
2636 | /* Run the command. */
|
---|
2637 | #ifdef __EMX__
|
---|
2638 | c->pid =
|
---|
2639 | exec_command (argv, envp);
|
---|
2640 | #else
|
---|
2641 | # error MAKE_DLLSHELL is not ported to your platform yet.
|
---|
2642 | #endif
|
---|
2643 | DB (DB_JOBS, (_("spawn pid=%x\n"), c->pid));
|
---|
2644 | }
|
---|
2645 |
|
---|
2646 | return c->pid;
|
---|
2647 | }
|
---|
2648 |
|
---|
2649 | /* Waits or pools for a job to finish.
|
---|
2650 | If the block argument the the function will not return
|
---|
2651 | till a job is completed (if there are any jobs).
|
---|
2652 | Returns pid of completed job.
|
---|
2653 | Returns 0 if no jobs are finished.
|
---|
2654 | Returns -1 if no jobs are running. */
|
---|
2655 | pid_t wait_jobs (int *status, int block)
|
---|
2656 | {
|
---|
2657 | pid_t pid;
|
---|
2658 | if (dllshell_wait)
|
---|
2659 | pid = dllshell_wait(dllshell_instance, status, block);
|
---|
2660 | else
|
---|
2661 | {
|
---|
2662 | if (block)
|
---|
2663 | pid = WAIT_NOHANG(status);
|
---|
2664 | else
|
---|
2665 | pid = wait(status);
|
---|
2666 | }
|
---|
2667 | return pid;
|
---|
2668 | }
|
---|
2669 |
|
---|
2670 | #endif /* MAKE_DLLSHELL */
|
---|
2671 | |
---|
2672 |
|
---|
2673 | #ifndef _AMIGA
|
---|
2674 | /* Replace the current process with one running the command in ARGV,
|
---|
2675 | with environment ENVP. This function does not return. */
|
---|
2676 |
|
---|
2677 | /* EMX: This function returns the pid of the child process. */
|
---|
2678 | # ifdef __EMX__
|
---|
2679 | int
|
---|
2680 | # else
|
---|
2681 | void
|
---|
2682 | # endif
|
---|
2683 | exec_command (char **argv, char **envp)
|
---|
2684 | {
|
---|
2685 | #ifdef VMS
|
---|
2686 | /* to work around a problem with signals and execve: ignore them */
|
---|
2687 | #ifdef SIGCHLD
|
---|
2688 | signal (SIGCHLD,SIG_IGN);
|
---|
2689 | #endif
|
---|
2690 | /* Run the program. */
|
---|
2691 | execve (argv[0], argv, envp);
|
---|
2692 | perror_with_name ("execve: ", argv[0]);
|
---|
2693 | _exit (EXIT_FAILURE);
|
---|
2694 | #else
|
---|
2695 | #ifdef WINDOWS32
|
---|
2696 | HANDLE hPID;
|
---|
2697 | HANDLE hWaitPID;
|
---|
2698 | int err = 0;
|
---|
2699 | int exit_code = EXIT_FAILURE;
|
---|
2700 |
|
---|
2701 | /* make sure CreateProcess() has Path it needs */
|
---|
2702 | sync_Path_environment();
|
---|
2703 |
|
---|
2704 | /* launch command */
|
---|
2705 | hPID = process_easy(argv, envp);
|
---|
2706 |
|
---|
2707 | /* make sure launch ok */
|
---|
2708 | if (hPID == INVALID_HANDLE_VALUE)
|
---|
2709 | {
|
---|
2710 | int i;
|
---|
2711 | fprintf(stderr,
|
---|
2712 | _("process_easy() failed failed to launch process (e=%d)\n"),
|
---|
2713 | process_last_err(hPID));
|
---|
2714 | for (i = 0; argv[i]; i++)
|
---|
2715 | fprintf(stderr, "%s ", argv[i]);
|
---|
2716 | fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
|
---|
2717 | exit(EXIT_FAILURE);
|
---|
2718 | }
|
---|
2719 |
|
---|
2720 | /* wait and reap last child */
|
---|
2721 | while (hWaitPID = process_wait_for_any())
|
---|
2722 | {
|
---|
2723 | /* was an error found on this process? */
|
---|
2724 | err = process_last_err(hWaitPID);
|
---|
2725 |
|
---|
2726 | /* get exit data */
|
---|
2727 | exit_code = process_exit_code(hWaitPID);
|
---|
2728 |
|
---|
2729 | if (err)
|
---|
2730 | fprintf(stderr, "make (e=%d, rc=%d): %s",
|
---|
2731 | err, exit_code, map_windows32_error_to_string(err));
|
---|
2732 |
|
---|
2733 | /* cleanup process */
|
---|
2734 | process_cleanup(hWaitPID);
|
---|
2735 |
|
---|
2736 | /* expect to find only last pid, warn about other pids reaped */
|
---|
2737 | if (hWaitPID == hPID)
|
---|
2738 | break;
|
---|
2739 | else
|
---|
2740 | fprintf(stderr,
|
---|
2741 | _("make reaped child pid %d, still waiting for pid %d\n"),
|
---|
2742 | hWaitPID, hPID);
|
---|
2743 | }
|
---|
2744 |
|
---|
2745 | /* return child's exit code as our exit code */
|
---|
2746 | exit(exit_code);
|
---|
2747 |
|
---|
2748 | #else /* !WINDOWS32 */
|
---|
2749 |
|
---|
2750 | # ifdef __EMX__
|
---|
2751 | int pid;
|
---|
2752 | # endif
|
---|
2753 |
|
---|
2754 | /* Be the user, permanently. */
|
---|
2755 | child_access ();
|
---|
2756 |
|
---|
2757 | # ifdef __EMX__
|
---|
2758 |
|
---|
2759 | /* Run the program. */
|
---|
2760 | pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
|
---|
2761 |
|
---|
2762 | if (pid >= 0)
|
---|
2763 | return pid;
|
---|
2764 |
|
---|
2765 | /* the file might have a strange shell extension */
|
---|
2766 | if (errno == ENOENT)
|
---|
2767 | errno = ENOEXEC;
|
---|
2768 |
|
---|
2769 | # else
|
---|
2770 |
|
---|
2771 | /* Run the program. */
|
---|
2772 | environ = envp;
|
---|
2773 | execvp (argv[0], argv);
|
---|
2774 |
|
---|
2775 | # endif /* !__EMX__ */
|
---|
2776 |
|
---|
2777 | switch (errno)
|
---|
2778 | {
|
---|
2779 | case ENOENT:
|
---|
2780 | error (NILF, _("%s: Command not found"), argv[0]);
|
---|
2781 | break;
|
---|
2782 | case ENOEXEC:
|
---|
2783 | {
|
---|
2784 | /* The file is not executable. Try it as a shell script. */
|
---|
2785 | extern char *getenv ();
|
---|
2786 | char *shell;
|
---|
2787 | char **new_argv;
|
---|
2788 | int argc;
|
---|
2789 |
|
---|
2790 | # ifdef __EMX__
|
---|
2791 | /* Do not use $SHELL from the environment */
|
---|
2792 | struct variable *p = lookup_variable ("SHELL", 5);
|
---|
2793 | if (p)
|
---|
2794 | shell = p->value;
|
---|
2795 | else
|
---|
2796 | shell = 0;
|
---|
2797 | # else
|
---|
2798 | shell = getenv ("SHELL");
|
---|
2799 | # endif
|
---|
2800 | if (shell == 0)
|
---|
2801 | shell = default_shell;
|
---|
2802 |
|
---|
2803 | argc = 1;
|
---|
2804 | while (argv[argc] != 0)
|
---|
2805 | ++argc;
|
---|
2806 |
|
---|
2807 | new_argv = (char **) alloca ((1 + argc + 1) * sizeof (char *));
|
---|
2808 | new_argv[0] = shell;
|
---|
2809 | new_argv[1] = argv[0];
|
---|
2810 | while (argc > 0)
|
---|
2811 | {
|
---|
2812 | new_argv[1 + argc] = argv[argc];
|
---|
2813 | --argc;
|
---|
2814 | }
|
---|
2815 |
|
---|
2816 | # ifdef __EMX__
|
---|
2817 | pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
|
---|
2818 | if (pid >= 0)
|
---|
2819 | break;
|
---|
2820 | # else
|
---|
2821 | execvp (shell, new_argv);
|
---|
2822 | # endif
|
---|
2823 | if (errno == ENOENT)
|
---|
2824 | error (NILF, _("%s: Shell program not found"), shell);
|
---|
2825 | else
|
---|
2826 | perror_with_name ("execvp: ", shell);
|
---|
2827 | break;
|
---|
2828 | }
|
---|
2829 |
|
---|
2830 | # ifdef __EMX__
|
---|
2831 | case EINVAL:
|
---|
2832 | /* this nasty error was driving me nuts :-( */
|
---|
2833 | error (NILF, _("spawnvpe: environment space might be exhausted"));
|
---|
2834 | /* FALLTHROUGH */
|
---|
2835 | # endif
|
---|
2836 |
|
---|
2837 | default:
|
---|
2838 | perror_with_name ("execvp: ", argv[0]);
|
---|
2839 | break;
|
---|
2840 | }
|
---|
2841 |
|
---|
2842 | # ifdef __EMX__
|
---|
2843 | return pid;
|
---|
2844 | # else
|
---|
2845 | _exit (127);
|
---|
2846 | # endif
|
---|
2847 | #endif /* !WINDOWS32 */
|
---|
2848 | #endif /* !VMS */
|
---|
2849 | }
|
---|
2850 | #else /* On Amiga */
|
---|
2851 | void exec_command (char **argv)
|
---|
2852 | {
|
---|
2853 | MyExecute (argv);
|
---|
2854 | }
|
---|
2855 |
|
---|
2856 | void clean_tmp (void)
|
---|
2857 | {
|
---|
2858 | DeleteFile (amiga_bname);
|
---|
2859 | }
|
---|
2860 |
|
---|
2861 | #endif /* On Amiga */
|
---|
2862 | |
---|
2863 |
|
---|
2864 | #ifndef VMS
|
---|
2865 | /* Figure out the argument list necessary to run LINE as a command. Try to
|
---|
2866 | avoid using a shell. This routine handles only ' quoting, and " quoting
|
---|
2867 | when no backslash, $ or ` characters are seen in the quotes. Starting
|
---|
2868 | quotes may be escaped with a backslash. If any of the characters in
|
---|
2869 | sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
|
---|
2870 | is the first word of a line, the shell is used.
|
---|
2871 |
|
---|
2872 | If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
|
---|
2873 | If *RESTP is NULL, newlines will be ignored.
|
---|
2874 |
|
---|
2875 | SHELL is the shell to use, or nil to use the default shell.
|
---|
2876 | IFS is the value of $IFS, or nil (meaning the default). */
|
---|
2877 |
|
---|
2878 | static char **
|
---|
2879 | construct_command_argv_internal (char *line, char **restp, char *shell,
|
---|
2880 | char *ifs, char **batch_filename_ptr)
|
---|
2881 | {
|
---|
2882 | #ifdef __MSDOS__
|
---|
2883 | /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
|
---|
2884 | We call `system' for anything that requires ``slow'' processing,
|
---|
2885 | because DOS shells are too dumb. When $SHELL points to a real
|
---|
2886 | (unix-style) shell, `system' just calls it to do everything. When
|
---|
2887 | $SHELL points to a DOS shell, `system' does most of the work
|
---|
2888 | internally, calling the shell only for its internal commands.
|
---|
2889 | However, it looks on the $PATH first, so you can e.g. have an
|
---|
2890 | external command named `mkdir'.
|
---|
2891 |
|
---|
2892 | Since we call `system', certain characters and commands below are
|
---|
2893 | actually not specific to COMMAND.COM, but to the DJGPP implementation
|
---|
2894 | of `system'. In particular:
|
---|
2895 |
|
---|
2896 | The shell wildcard characters are in DOS_CHARS because they will
|
---|
2897 | not be expanded if we call the child via `spawnXX'.
|
---|
2898 |
|
---|
2899 | The `;' is in DOS_CHARS, because our `system' knows how to run
|
---|
2900 | multiple commands on a single line.
|
---|
2901 |
|
---|
2902 | DOS_CHARS also include characters special to 4DOS/NDOS, so we
|
---|
2903 | won't have to tell one from another and have one more set of
|
---|
2904 | commands and special characters. */
|
---|
2905 | static char sh_chars_dos[] = "*?[];|<>%^&()";
|
---|
2906 | static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
|
---|
2907 | "copy", "ctty", "date", "del", "dir", "echo",
|
---|
2908 | "erase", "exit", "for", "goto", "if", "md",
|
---|
2909 | "mkdir", "path", "pause", "prompt", "rd",
|
---|
2910 | "rmdir", "rem", "ren", "rename", "set",
|
---|
2911 | "shift", "time", "type", "ver", "verify",
|
---|
2912 | "vol", ":", 0 };
|
---|
2913 |
|
---|
2914 | static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
|
---|
2915 | static char *sh_cmds_sh[] = { "cd", "echo", "eval", "exec", "exit", "login",
|
---|
2916 | "logout", "set", "umask", "wait", "while",
|
---|
2917 | "for", "case", "if", ":", ".", "break",
|
---|
2918 | "continue", "export", "read", "readonly",
|
---|
2919 | "shift", "times", "trap", "switch", "unset",
|
---|
2920 | 0 };
|
---|
2921 |
|
---|
2922 | char *sh_chars;
|
---|
2923 | char **sh_cmds;
|
---|
2924 | #elif defined (__EMX__)
|
---|
2925 | static char sh_chars_dos[] = "*?[];|<>%^&()";
|
---|
2926 | static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
|
---|
2927 | "copy", "ctty", "date", "del", "dir", "echo",
|
---|
2928 | "erase", "exit", "for", "goto", "if", "md",
|
---|
2929 | "mkdir", "path", "pause", "prompt", "rd",
|
---|
2930 | "rmdir", "rem", "ren", "rename", "set",
|
---|
2931 | "shift", "time", "type", "ver", "verify",
|
---|
2932 | "vol", ":", 0 };
|
---|
2933 |
|
---|
2934 | static char sh_chars_os2[] = "*?[];|<>%^()\"'&";
|
---|
2935 | static char *sh_cmds_os2[] = { "call", "cd", "chcp", "chdir", "cls", "copy",
|
---|
2936 | "date", "del", "detach", "dir", "echo",
|
---|
2937 | "endlocal", "erase", "exit", "for", "goto", "if",
|
---|
2938 | "keys", "md", "mkdir", "move", "path", "pause",
|
---|
2939 | "prompt", "rd", "rem", "ren", "rename", "rmdir",
|
---|
2940 | "set", "setlocal", "shift", "start", "time",
|
---|
2941 | "type", "ver", "verify", "vol", ":", 0 };
|
---|
2942 |
|
---|
2943 | static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~'";
|
---|
2944 | static char *sh_cmds_sh[] = { "echo", "cd", "eval", "exec", "exit", "login",
|
---|
2945 | "logout", "set", "umask", "wait", "while",
|
---|
2946 | "for", "case", "if", ":", ".", "break",
|
---|
2947 | "continue", "export", "read", "readonly",
|
---|
2948 | "shift", "times", "trap", "switch", "unset",
|
---|
2949 | 0 };
|
---|
2950 | char *sh_chars;
|
---|
2951 | char **sh_cmds;
|
---|
2952 |
|
---|
2953 | #elif defined (_AMIGA)
|
---|
2954 | static char sh_chars[] = "#;\"|<>()?*$`";
|
---|
2955 | static char *sh_cmds[] = { "cd", "eval", "if", "delete", "echo", "copy",
|
---|
2956 | "rename", "set", "setenv", "date", "makedir",
|
---|
2957 | "skip", "else", "endif", "path", "prompt",
|
---|
2958 | "unset", "unsetenv", "version",
|
---|
2959 | 0 };
|
---|
2960 | #elif defined (WINDOWS32)
|
---|
2961 | static char sh_chars_dos[] = "\"|&<>";
|
---|
2962 | static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
|
---|
2963 | "copy", "ctty", "date", "del", "dir", "echo",
|
---|
2964 | "erase", "exit", "for", "goto", "if", "if", "md",
|
---|
2965 | "mkdir", "path", "pause", "prompt", "rd", "rem",
|
---|
2966 | "ren", "rename", "rmdir", "set", "shift", "time",
|
---|
2967 | "type", "ver", "verify", "vol", ":", 0 };
|
---|
2968 | static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
|
---|
2969 | static char *sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login",
|
---|
2970 | "logout", "set", "umask", "wait", "while", "for",
|
---|
2971 | "case", "if", ":", ".", "break", "continue",
|
---|
2972 | "export", "read", "readonly", "shift", "times",
|
---|
2973 | "trap", "switch", "test",
|
---|
2974 | #ifdef BATCH_MODE_ONLY_SHELL
|
---|
2975 | "echo",
|
---|
2976 | #endif
|
---|
2977 | 0 };
|
---|
2978 | char* sh_chars;
|
---|
2979 | char** sh_cmds;
|
---|
2980 | #else /* must be UNIX-ish */
|
---|
2981 | static char sh_chars[] = "#;\"*?[]&|<>(){}$`^~!";
|
---|
2982 | static char *sh_cmds[] = { ".", ":", "break", "case", "cd", "continue",
|
---|
2983 | "eval", "exec", "exit", "export", "for", "if",
|
---|
2984 | "login", "logout", "read", "readonly", "set",
|
---|
2985 | "shift", "switch", "test", "times", "trap",
|
---|
2986 | "umask", "wait", "while", 0 };
|
---|
2987 | #endif
|
---|
2988 | register int i;
|
---|
2989 | register char *p;
|
---|
2990 | register char *ap;
|
---|
2991 | char *end;
|
---|
2992 | int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
|
---|
2993 | char **new_argv = 0;
|
---|
2994 | #ifdef WINDOWS32
|
---|
2995 | int slow_flag = 0;
|
---|
2996 |
|
---|
2997 | if (no_default_sh_exe) {
|
---|
2998 | sh_cmds = sh_cmds_dos;
|
---|
2999 | sh_chars = sh_chars_dos;
|
---|
3000 | } else {
|
---|
3001 | sh_cmds = sh_cmds_sh;
|
---|
3002 | sh_chars = sh_chars_sh;
|
---|
3003 | }
|
---|
3004 | #endif /* WINDOWS32 */
|
---|
3005 |
|
---|
3006 | if (restp != NULL)
|
---|
3007 | *restp = NULL;
|
---|
3008 |
|
---|
3009 | /* Make sure not to bother processing an empty line. */
|
---|
3010 | while (isblank ((unsigned char)*line))
|
---|
3011 | ++line;
|
---|
3012 | if (*line == '\0')
|
---|
3013 | return 0;
|
---|
3014 |
|
---|
3015 | /* See if it is safe to parse commands internally. */
|
---|
3016 | if (shell == 0)
|
---|
3017 | shell = default_shell;
|
---|
3018 | #ifdef WINDOWS32
|
---|
3019 | else if (strcmp (shell, default_shell))
|
---|
3020 | {
|
---|
3021 | char *s1 = _fullpath(NULL, shell, 0);
|
---|
3022 | char *s2 = _fullpath(NULL, default_shell, 0);
|
---|
3023 |
|
---|
3024 | slow_flag = strcmp((s1 ? s1 : ""), (s2 ? s2 : ""));
|
---|
3025 |
|
---|
3026 | if (s1)
|
---|
3027 | free (s1);
|
---|
3028 | if (s2)
|
---|
3029 | free (s2);
|
---|
3030 | }
|
---|
3031 | if (slow_flag)
|
---|
3032 | goto slow;
|
---|
3033 | #else /* not WINDOWS32 */
|
---|
3034 | #if defined (__MSDOS__) || defined (__EMX__)
|
---|
3035 | else if (stricmp (shell, default_shell))
|
---|
3036 | {
|
---|
3037 | extern int _is_unixy_shell (const char *_path);
|
---|
3038 |
|
---|
3039 | DB (DB_BASIC, (_("$SHELL changed (was `%s', now `%s')\n"),
|
---|
3040 | default_shell, shell));
|
---|
3041 | unixy_shell = _is_unixy_shell (shell);
|
---|
3042 | /* we must allocate a copy of shell: construct_command_argv() will free
|
---|
3043 | * shell after this function returns. */
|
---|
3044 | default_shell = xstrdup (shell);
|
---|
3045 | }
|
---|
3046 | if (unixy_shell)
|
---|
3047 | {
|
---|
3048 | sh_chars = sh_chars_sh;
|
---|
3049 | sh_cmds = sh_cmds_sh;
|
---|
3050 | }
|
---|
3051 | else
|
---|
3052 | {
|
---|
3053 | sh_chars = sh_chars_dos;
|
---|
3054 | sh_cmds = sh_cmds_dos;
|
---|
3055 | # ifdef __EMX__
|
---|
3056 | if (_osmode == OS2_MODE)
|
---|
3057 | {
|
---|
3058 | sh_chars = sh_chars_os2;
|
---|
3059 | sh_cmds = sh_cmds_os2;
|
---|
3060 | }
|
---|
3061 | # endif
|
---|
3062 | }
|
---|
3063 | #else /* !__MSDOS__ */
|
---|
3064 | else if (strcmp (shell, default_shell))
|
---|
3065 | goto slow;
|
---|
3066 | #endif /* !__MSDOS__ && !__EMX__ */
|
---|
3067 | #endif /* not WINDOWS32 */
|
---|
3068 |
|
---|
3069 | if (ifs != 0)
|
---|
3070 | for (ap = ifs; *ap != '\0'; ++ap)
|
---|
3071 | if (*ap != ' ' && *ap != '\t' && *ap != '\n')
|
---|
3072 | goto slow;
|
---|
3073 |
|
---|
3074 | i = strlen (line) + 1;
|
---|
3075 |
|
---|
3076 | /* More than 1 arg per character is impossible. */
|
---|
3077 | new_argv = (char **) xmalloc (i * sizeof (char *));
|
---|
3078 |
|
---|
3079 | /* All the args can fit in a buffer as big as LINE is. */
|
---|
3080 | ap = new_argv[0] = (char *) xmalloc (i);
|
---|
3081 | end = ap + i;
|
---|
3082 |
|
---|
3083 | /* I is how many complete arguments have been found. */
|
---|
3084 | i = 0;
|
---|
3085 | instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
|
---|
3086 | for (p = line; *p != '\0'; ++p)
|
---|
3087 | {
|
---|
3088 | if (ap > end)
|
---|
3089 | abort ();
|
---|
3090 |
|
---|
3091 | if (instring)
|
---|
3092 | {
|
---|
3093 | string_char:
|
---|
3094 | /* Inside a string, just copy any char except a closing quote
|
---|
3095 | or a backslash-newline combination. */
|
---|
3096 | if (*p == instring)
|
---|
3097 | {
|
---|
3098 | instring = 0;
|
---|
3099 | if (ap == new_argv[0] || *(ap-1) == '\0')
|
---|
3100 | last_argument_was_empty = 1;
|
---|
3101 | }
|
---|
3102 | else if (*p == '\\' && p[1] == '\n')
|
---|
3103 | goto swallow_escaped_newline;
|
---|
3104 | else if (*p == '\n' && restp != NULL)
|
---|
3105 | {
|
---|
3106 | /* End of the command line. */
|
---|
3107 | *restp = p;
|
---|
3108 | goto end_of_line;
|
---|
3109 | }
|
---|
3110 | /* Backslash, $, and ` are special inside double quotes.
|
---|
3111 | If we see any of those, punt.
|
---|
3112 | But on MSDOS, if we use COMMAND.COM, double and single
|
---|
3113 | quotes have the same effect. */
|
---|
3114 | else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
|
---|
3115 | goto slow;
|
---|
3116 | else
|
---|
3117 | *ap++ = *p;
|
---|
3118 | }
|
---|
3119 | else if (strchr (sh_chars, *p) != 0)
|
---|
3120 | /* Not inside a string, but it's a special char. */
|
---|
3121 | goto slow;
|
---|
3122 | #ifdef __MSDOS__
|
---|
3123 | else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
|
---|
3124 | /* `...' is a wildcard in DJGPP. */
|
---|
3125 | goto slow;
|
---|
3126 | #endif
|
---|
3127 | else
|
---|
3128 | /* Not a special char. */
|
---|
3129 | switch (*p)
|
---|
3130 | {
|
---|
3131 | case '=':
|
---|
3132 | /* Equals is a special character in leading words before the
|
---|
3133 | first word with no equals sign in it. This is not the case
|
---|
3134 | with sh -k, but we never get here when using nonstandard
|
---|
3135 | shell flags. */
|
---|
3136 | if (! seen_nonequals && unixy_shell)
|
---|
3137 | goto slow;
|
---|
3138 | word_has_equals = 1;
|
---|
3139 | *ap++ = '=';
|
---|
3140 | break;
|
---|
3141 |
|
---|
3142 | case '\\':
|
---|
3143 | /* Backslash-newline combinations are eaten. */
|
---|
3144 | if (p[1] == '\n')
|
---|
3145 | {
|
---|
3146 | swallow_escaped_newline:
|
---|
3147 |
|
---|
3148 | /* Eat the backslash, the newline, and following whitespace,
|
---|
3149 | replacing it all with a single space. */
|
---|
3150 | p += 2;
|
---|
3151 |
|
---|
3152 | /* If there is a tab after a backslash-newline,
|
---|
3153 | remove it from the source line which will be echoed,
|
---|
3154 | since it was most likely used to line
|
---|
3155 | up the continued line with the previous one. */
|
---|
3156 | if (*p == '\t')
|
---|
3157 | /* Note these overlap and strcpy() is undefined for
|
---|
3158 | overlapping objects in ANSI C. The strlen() _IS_ right,
|
---|
3159 | since we need to copy the nul byte too. */
|
---|
3160 | bcopy (p + 1, p, strlen (p));
|
---|
3161 |
|
---|
3162 | if (instring)
|
---|
3163 | goto string_char;
|
---|
3164 | else
|
---|
3165 | {
|
---|
3166 | if (ap != new_argv[i])
|
---|
3167 | /* Treat this as a space, ending the arg.
|
---|
3168 | But if it's at the beginning of the arg, it should
|
---|
3169 | just get eaten, rather than becoming an empty arg. */
|
---|
3170 | goto end_of_arg;
|
---|
3171 | else
|
---|
3172 | p = next_token (p) - 1;
|
---|
3173 | }
|
---|
3174 | }
|
---|
3175 | else if (p[1] != '\0')
|
---|
3176 | {
|
---|
3177 | #ifdef HAVE_DOS_PATHS
|
---|
3178 | /* Only remove backslashes before characters special
|
---|
3179 | to Unixy shells. All other backslashes are copied
|
---|
3180 | verbatim, since they are probably DOS-style
|
---|
3181 | directory separators. This still leaves a small
|
---|
3182 | window for problems, but at least it should work
|
---|
3183 | for the vast majority of naive users. */
|
---|
3184 |
|
---|
3185 | #ifdef __MSDOS__
|
---|
3186 | /* A dot is only special as part of the "..."
|
---|
3187 | wildcard. */
|
---|
3188 | if (strneq (p + 1, ".\\.\\.", 5))
|
---|
3189 | {
|
---|
3190 | *ap++ = '.';
|
---|
3191 | *ap++ = '.';
|
---|
3192 | p += 4;
|
---|
3193 | }
|
---|
3194 | else
|
---|
3195 | #endif
|
---|
3196 | if (p[1] != '\\' && p[1] != '\''
|
---|
3197 | && !isspace ((unsigned char)p[1])
|
---|
3198 | && (strchr (sh_chars_sh, p[1]) == 0))
|
---|
3199 | /* back up one notch, to copy the backslash */
|
---|
3200 | --p;
|
---|
3201 | #endif /* HAVE_DOS_PATHS */
|
---|
3202 |
|
---|
3203 | /* Copy and skip the following char. */
|
---|
3204 | *ap++ = *++p;
|
---|
3205 | }
|
---|
3206 | break;
|
---|
3207 |
|
---|
3208 | case '\'':
|
---|
3209 | case '"':
|
---|
3210 | instring = *p;
|
---|
3211 | break;
|
---|
3212 |
|
---|
3213 | case '\n':
|
---|
3214 | if (restp != NULL)
|
---|
3215 | {
|
---|
3216 | /* End of the command line. */
|
---|
3217 | *restp = p;
|
---|
3218 | goto end_of_line;
|
---|
3219 | }
|
---|
3220 | else
|
---|
3221 | /* Newlines are not special. */
|
---|
3222 | *ap++ = '\n';
|
---|
3223 | break;
|
---|
3224 |
|
---|
3225 | case ' ':
|
---|
3226 | case '\t':
|
---|
3227 | end_of_arg:
|
---|
3228 | /* We have the end of an argument.
|
---|
3229 | Terminate the text of the argument. */
|
---|
3230 | *ap++ = '\0';
|
---|
3231 | new_argv[++i] = ap;
|
---|
3232 | last_argument_was_empty = 0;
|
---|
3233 |
|
---|
3234 | /* Update SEEN_NONEQUALS, which tells us if every word
|
---|
3235 | heretofore has contained an `='. */
|
---|
3236 | seen_nonequals |= ! word_has_equals;
|
---|
3237 | if (word_has_equals && ! seen_nonequals)
|
---|
3238 | /* An `=' in a word before the first
|
---|
3239 | word without one is magical. */
|
---|
3240 | goto slow;
|
---|
3241 | word_has_equals = 0; /* Prepare for the next word. */
|
---|
3242 |
|
---|
3243 | /* If this argument is the command name,
|
---|
3244 | see if it is a built-in shell command.
|
---|
3245 | If so, have the shell handle it. */
|
---|
3246 | if (i == 1)
|
---|
3247 | {
|
---|
3248 | register int j;
|
---|
3249 | for (j = 0; sh_cmds[j] != 0; ++j)
|
---|
3250 | if (streq (sh_cmds[j], new_argv[0]))
|
---|
3251 | goto slow;
|
---|
3252 | }
|
---|
3253 |
|
---|
3254 | /* Ignore multiple whitespace chars. */
|
---|
3255 | p = next_token (p);
|
---|
3256 | /* Next iteration should examine the first nonwhite char. */
|
---|
3257 | --p;
|
---|
3258 | break;
|
---|
3259 |
|
---|
3260 | default:
|
---|
3261 | *ap++ = *p;
|
---|
3262 | break;
|
---|
3263 | }
|
---|
3264 | }
|
---|
3265 | end_of_line:
|
---|
3266 |
|
---|
3267 | if (instring)
|
---|
3268 | /* Let the shell deal with an unterminated quote. */
|
---|
3269 | goto slow;
|
---|
3270 |
|
---|
3271 | /* Terminate the last argument and the argument list. */
|
---|
3272 |
|
---|
3273 | *ap = '\0';
|
---|
3274 | if (new_argv[i][0] != '\0' || last_argument_was_empty)
|
---|
3275 | ++i;
|
---|
3276 | new_argv[i] = 0;
|
---|
3277 |
|
---|
3278 | if (i == 1)
|
---|
3279 | {
|
---|
3280 | register int j;
|
---|
3281 | for (j = 0; sh_cmds[j] != 0; ++j)
|
---|
3282 | if (streq (sh_cmds[j], new_argv[0]))
|
---|
3283 | goto slow;
|
---|
3284 | }
|
---|
3285 |
|
---|
3286 | if (new_argv[0] == 0)
|
---|
3287 | /* Line was empty. */
|
---|
3288 | return 0;
|
---|
3289 | else
|
---|
3290 | return new_argv;
|
---|
3291 |
|
---|
3292 | slow:;
|
---|
3293 | /* We must use the shell. */
|
---|
3294 |
|
---|
3295 | if (new_argv != 0)
|
---|
3296 | {
|
---|
3297 | /* Free the old argument list we were working on. */
|
---|
3298 | free (new_argv[0]);
|
---|
3299 | free ((void *)new_argv);
|
---|
3300 | }
|
---|
3301 |
|
---|
3302 | #ifdef __MSDOS__
|
---|
3303 | execute_by_shell = 1; /* actually, call `system' if shell isn't unixy */
|
---|
3304 | #endif
|
---|
3305 |
|
---|
3306 | #ifdef _AMIGA
|
---|
3307 | {
|
---|
3308 | char *ptr;
|
---|
3309 | char *buffer;
|
---|
3310 | char *dptr;
|
---|
3311 |
|
---|
3312 | buffer = (char *)xmalloc (strlen (line)+1);
|
---|
3313 |
|
---|
3314 | ptr = line;
|
---|
3315 | for (dptr=buffer; *ptr; )
|
---|
3316 | {
|
---|
3317 | if (*ptr == '\\' && ptr[1] == '\n')
|
---|
3318 | ptr += 2;
|
---|
3319 | else if (*ptr == '@') /* Kludge: multiline commands */
|
---|
3320 | {
|
---|
3321 | ptr += 2;
|
---|
3322 | *dptr++ = '\n';
|
---|
3323 | }
|
---|
3324 | else
|
---|
3325 | *dptr++ = *ptr++;
|
---|
3326 | }
|
---|
3327 | *dptr = 0;
|
---|
3328 |
|
---|
3329 | new_argv = (char **) xmalloc (2 * sizeof (char *));
|
---|
3330 | new_argv[0] = buffer;
|
---|
3331 | new_argv[1] = 0;
|
---|
3332 | }
|
---|
3333 | #else /* Not Amiga */
|
---|
3334 | #ifdef WINDOWS32
|
---|
3335 | /*
|
---|
3336 | * Not eating this whitespace caused things like
|
---|
3337 | *
|
---|
3338 | * sh -c "\n"
|
---|
3339 | *
|
---|
3340 | * which gave the shell fits. I think we have to eat
|
---|
3341 | * whitespace here, but this code should be considered
|
---|
3342 | * suspicious if things start failing....
|
---|
3343 | */
|
---|
3344 |
|
---|
3345 | /* Make sure not to bother processing an empty line. */
|
---|
3346 | while (isspace ((unsigned char)*line))
|
---|
3347 | ++line;
|
---|
3348 | if (*line == '\0')
|
---|
3349 | return 0;
|
---|
3350 | #endif /* WINDOWS32 */
|
---|
3351 | {
|
---|
3352 | /* SHELL may be a multi-word command. Construct a command line
|
---|
3353 | "SHELL -c LINE", with all special chars in LINE escaped.
|
---|
3354 | Then recurse, expanding this command line to get the final
|
---|
3355 | argument list. */
|
---|
3356 |
|
---|
3357 | unsigned int shell_len = strlen (shell);
|
---|
3358 | #ifndef VMS
|
---|
3359 | static char minus_c[] = " -c ";
|
---|
3360 | #else
|
---|
3361 | static char minus_c[] = "";
|
---|
3362 | #endif
|
---|
3363 | unsigned int line_len = strlen (line);
|
---|
3364 |
|
---|
3365 | char *new_line = (char *) alloca (shell_len + (sizeof (minus_c) - 1)
|
---|
3366 | + (line_len * 2) + 1);
|
---|
3367 | char *command_ptr = NULL; /* used for batch_mode_shell mode */
|
---|
3368 |
|
---|
3369 | # ifdef __EMX__ /* is this necessary? */
|
---|
3370 | if (!unixy_shell)
|
---|
3371 | minus_c[1] = '/'; /* " /c " */
|
---|
3372 | # endif
|
---|
3373 |
|
---|
3374 | ap = new_line;
|
---|
3375 | bcopy (shell, ap, shell_len);
|
---|
3376 | ap += shell_len;
|
---|
3377 | bcopy (minus_c, ap, sizeof (minus_c) - 1);
|
---|
3378 | ap += sizeof (minus_c) - 1;
|
---|
3379 | command_ptr = ap;
|
---|
3380 | for (p = line; *p != '\0'; ++p)
|
---|
3381 | {
|
---|
3382 | if (restp != NULL && *p == '\n')
|
---|
3383 | {
|
---|
3384 | *restp = p;
|
---|
3385 | break;
|
---|
3386 | }
|
---|
3387 | else if (*p == '\\' && p[1] == '\n')
|
---|
3388 | {
|
---|
3389 | /* Eat the backslash, the newline, and following whitespace,
|
---|
3390 | replacing it all with a single space (which is escaped
|
---|
3391 | from the shell). */
|
---|
3392 | p += 2;
|
---|
3393 |
|
---|
3394 | /* If there is a tab after a backslash-newline,
|
---|
3395 | remove it from the source line which will be echoed,
|
---|
3396 | since it was most likely used to line
|
---|
3397 | up the continued line with the previous one. */
|
---|
3398 | if (*p == '\t')
|
---|
3399 | bcopy (p + 1, p, strlen (p));
|
---|
3400 |
|
---|
3401 | p = next_token (p);
|
---|
3402 | --p;
|
---|
3403 | if (unixy_shell && !batch_mode_shell)
|
---|
3404 | *ap++ = '\\';
|
---|
3405 | *ap++ = ' ';
|
---|
3406 | continue;
|
---|
3407 | }
|
---|
3408 |
|
---|
3409 | /* DOS shells don't know about backslash-escaping. */
|
---|
3410 | if (unixy_shell && !batch_mode_shell &&
|
---|
3411 | (*p == '\\' || *p == '\'' || *p == '"'
|
---|
3412 | || isspace ((unsigned char)*p)
|
---|
3413 | || strchr (sh_chars, *p) != 0))
|
---|
3414 | *ap++ = '\\';
|
---|
3415 | #ifdef __MSDOS__
|
---|
3416 | else if (unixy_shell && strneq (p, "...", 3))
|
---|
3417 | {
|
---|
3418 | /* The case of `...' wildcard again. */
|
---|
3419 | strcpy (ap, "\\.\\.\\");
|
---|
3420 | ap += 5;
|
---|
3421 | p += 2;
|
---|
3422 | }
|
---|
3423 | #endif
|
---|
3424 | *ap++ = *p;
|
---|
3425 | }
|
---|
3426 | if (ap == new_line + shell_len + sizeof (minus_c) - 1)
|
---|
3427 | /* Line was empty. */
|
---|
3428 | return 0;
|
---|
3429 | *ap = '\0';
|
---|
3430 |
|
---|
3431 | #ifdef WINDOWS32
|
---|
3432 | /* Some shells do not work well when invoked as 'sh -c xxx' to run a
|
---|
3433 | command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
|
---|
3434 | cases, run commands via a script file. */
|
---|
3435 | if ((no_default_sh_exe || batch_mode_shell) && batch_filename_ptr) {
|
---|
3436 | FILE* batch = NULL;
|
---|
3437 | int id = GetCurrentProcessId();
|
---|
3438 | PATH_VAR(fbuf);
|
---|
3439 | char* fname = NULL;
|
---|
3440 |
|
---|
3441 | /* create a file name */
|
---|
3442 | sprintf(fbuf, "make%d", id);
|
---|
3443 | fname = tempnam(".", fbuf);
|
---|
3444 |
|
---|
3445 | /* create batch file name */
|
---|
3446 | *batch_filename_ptr = xmalloc(strlen(fname) + 5);
|
---|
3447 | strcpy(*batch_filename_ptr, fname);
|
---|
3448 |
|
---|
3449 | /* make sure path name is in DOS backslash format */
|
---|
3450 | if (!unixy_shell) {
|
---|
3451 | fname = *batch_filename_ptr;
|
---|
3452 | for (i = 0; fname[i] != '\0'; ++i)
|
---|
3453 | if (fname[i] == '/')
|
---|
3454 | fname[i] = '\\';
|
---|
3455 | strcat(*batch_filename_ptr, ".bat");
|
---|
3456 | } else {
|
---|
3457 | strcat(*batch_filename_ptr, ".sh");
|
---|
3458 | }
|
---|
3459 |
|
---|
3460 | DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
|
---|
3461 | *batch_filename_ptr));
|
---|
3462 |
|
---|
3463 | /* create batch file to execute command */
|
---|
3464 | batch = fopen (*batch_filename_ptr, "w");
|
---|
3465 | if (!unixy_shell)
|
---|
3466 | fputs ("@echo off\n", batch);
|
---|
3467 | fputs (command_ptr, batch);
|
---|
3468 | fputc ('\n', batch);
|
---|
3469 | fclose (batch);
|
---|
3470 |
|
---|
3471 | /* create argv */
|
---|
3472 | new_argv = (char **) xmalloc(3 * sizeof (char *));
|
---|
3473 | if (unixy_shell) {
|
---|
3474 | new_argv[0] = xstrdup (shell);
|
---|
3475 | new_argv[1] = *batch_filename_ptr; /* only argv[0] gets freed later */
|
---|
3476 | } else {
|
---|
3477 | new_argv[0] = xstrdup (*batch_filename_ptr);
|
---|
3478 | new_argv[1] = NULL;
|
---|
3479 | }
|
---|
3480 | new_argv[2] = NULL;
|
---|
3481 | } else
|
---|
3482 | #endif /* WINDOWS32 */
|
---|
3483 | if (unixy_shell)
|
---|
3484 | new_argv = construct_command_argv_internal (new_line, (char **) NULL,
|
---|
3485 | (char *) 0, (char *) 0,
|
---|
3486 | (char **) 0);
|
---|
3487 | # ifdef __EMX__
|
---|
3488 | else if (!unixy_shell)
|
---|
3489 | {
|
---|
3490 | /* new_line is local, must not be freed therefore */
|
---|
3491 | char *p, *q;
|
---|
3492 | int quote;
|
---|
3493 | size_t index;
|
---|
3494 | size_t len;
|
---|
3495 |
|
---|
3496 | /* handle quotes
|
---|
3497 | We have to remove all double quotes and to split the line
|
---|
3498 | into distinct arguments because of the strange handling
|
---|
3499 | of builtin commands by cmd: 'echo "bla"' prints "bla"
|
---|
3500 | (with quotes) while 'c:\bin\echo.exe "bla"' prints bla
|
---|
3501 | (without quotes). Some programs like autoconf rely
|
---|
3502 | on the second behaviour. */
|
---|
3503 |
|
---|
3504 | len = strlen (new_line) + 1;
|
---|
3505 |
|
---|
3506 | /* More than 1 arg per character is impossible. */
|
---|
3507 | new_argv = (char **) xmalloc (len * sizeof (char *));
|
---|
3508 |
|
---|
3509 | /* All the args can fit in a buffer as big as new_line is. */
|
---|
3510 | new_argv[0] = (char *) xmalloc (len);
|
---|
3511 |
|
---|
3512 | index = 0;
|
---|
3513 | quote = 0;
|
---|
3514 | q = new_line;
|
---|
3515 | p = new_argv[index];
|
---|
3516 | while(*q != '\0')
|
---|
3517 | {
|
---|
3518 | /* searching for closing quote */
|
---|
3519 | if (quote)
|
---|
3520 | {
|
---|
3521 | if (*q == quote)
|
---|
3522 | {
|
---|
3523 | /* remove the quote */
|
---|
3524 | q++;
|
---|
3525 | quote = 0;
|
---|
3526 | }
|
---|
3527 | else /* normal character: copy it */
|
---|
3528 | *p++ = *q++;
|
---|
3529 | }
|
---|
3530 |
|
---|
3531 | /* searching for opening quote */
|
---|
3532 | else if (*q == '\"'
|
---|
3533 | # ifndef NO_CMD_DEFAULT
|
---|
3534 | || *q == '\''
|
---|
3535 | # endif
|
---|
3536 | )
|
---|
3537 | {
|
---|
3538 | /* remove opening quote */
|
---|
3539 | quote = *q;
|
---|
3540 | q++;
|
---|
3541 | }
|
---|
3542 |
|
---|
3543 | /* spaces outside of a quoted string: remove them
|
---|
3544 | and start a new argument */
|
---|
3545 | else if (*q == ' ' || *q == '\t')
|
---|
3546 | {
|
---|
3547 | *p++ = '\0'; /* trailing '\0' for last argument */
|
---|
3548 |
|
---|
3549 | /* remove all successive spaces */
|
---|
3550 | do
|
---|
3551 | {
|
---|
3552 | q++;
|
---|
3553 | }
|
---|
3554 | while(*q == ' ' || *q == '\t');
|
---|
3555 |
|
---|
3556 | /* start new argument */
|
---|
3557 | index++;
|
---|
3558 | new_argv[index] = p;
|
---|
3559 | }
|
---|
3560 |
|
---|
3561 | /* normal character (no space) outside a quoted string*/
|
---|
3562 | else
|
---|
3563 | *p++ = *q++;
|
---|
3564 | } /* end while() */
|
---|
3565 |
|
---|
3566 | *p = '\0'; /* trailing '\0' for the last argument */
|
---|
3567 | new_argv[index + 1] = NULL;
|
---|
3568 |
|
---|
3569 | # ifndef NO_CMD_DEFAULT
|
---|
3570 | /* special case: echo x="y"
|
---|
3571 | (e.g. autoconf uses this to determine whether make works)
|
---|
3572 | this is pure idioty but cmd works this way:
|
---|
3573 | if 'echo' and 'x="y"' are two different arguments cmd
|
---|
3574 | will print '"x="y""' but if they are only one argument
|
---|
3575 | cmd will print 'bla="blurb"' as it should be
|
---|
3576 | note: if we do not allow cmd to be the default shell
|
---|
3577 | we do not need this kind of voodoo */
|
---|
3578 | if (index == 3 && strcasecmp(new_argv[2], "echo") == 0)
|
---|
3579 | {
|
---|
3580 | new_argv[2][4] = ' ';
|
---|
3581 | new_argv[3] = NULL;
|
---|
3582 | }
|
---|
3583 | # endif
|
---|
3584 | }
|
---|
3585 | #elif defined(__MSDOS__)
|
---|
3586 | else
|
---|
3587 | {
|
---|
3588 | /* With MSDOS shells, we must construct the command line here
|
---|
3589 | instead of recursively calling ourselves, because we
|
---|
3590 | cannot backslash-escape the special characters (see above). */
|
---|
3591 | new_argv = (char **) xmalloc (sizeof (char *));
|
---|
3592 | line_len = strlen (new_line) - shell_len - sizeof (minus_c) + 1;
|
---|
3593 | new_argv[0] = xmalloc (line_len + 1);
|
---|
3594 | strncpy (new_argv[0],
|
---|
3595 | new_line + shell_len + sizeof (minus_c) - 1, line_len);
|
---|
3596 | new_argv[0][line_len] = '\0';
|
---|
3597 | }
|
---|
3598 | #else
|
---|
3599 | else
|
---|
3600 | fatal (NILF, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
|
---|
3601 | __FILE__, __LINE__);
|
---|
3602 | #endif
|
---|
3603 | }
|
---|
3604 | #endif /* ! AMIGA */
|
---|
3605 |
|
---|
3606 | return new_argv;
|
---|
3607 | }
|
---|
3608 | #endif /* !VMS */
|
---|
3609 |
|
---|
3610 | /* Figure out the argument list necessary to run LINE as a command. Try to
|
---|
3611 | avoid using a shell. This routine handles only ' quoting, and " quoting
|
---|
3612 | when no backslash, $ or ` characters are seen in the quotes. Starting
|
---|
3613 | quotes may be escaped with a backslash. If any of the characters in
|
---|
3614 | sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
|
---|
3615 | is the first word of a line, the shell is used.
|
---|
3616 |
|
---|
3617 | If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
|
---|
3618 | If *RESTP is NULL, newlines will be ignored.
|
---|
3619 |
|
---|
3620 | FILE is the target whose commands these are. It is used for
|
---|
3621 | variable expansion for $(SHELL) and $(IFS). */
|
---|
3622 |
|
---|
3623 | char **
|
---|
3624 | construct_command_argv (char *line, char **restp, struct file *file,
|
---|
3625 | char **batch_filename_ptr)
|
---|
3626 | {
|
---|
3627 | char *shell, *ifs;
|
---|
3628 | char **argv;
|
---|
3629 |
|
---|
3630 | #ifdef VMS
|
---|
3631 | char *cptr;
|
---|
3632 | int argc;
|
---|
3633 |
|
---|
3634 | argc = 0;
|
---|
3635 | cptr = line;
|
---|
3636 | for (;;)
|
---|
3637 | {
|
---|
3638 | while ((*cptr != 0)
|
---|
3639 | && (isspace ((unsigned char)*cptr)))
|
---|
3640 | cptr++;
|
---|
3641 | if (*cptr == 0)
|
---|
3642 | break;
|
---|
3643 | while ((*cptr != 0)
|
---|
3644 | && (!isspace((unsigned char)*cptr)))
|
---|
3645 | cptr++;
|
---|
3646 | argc++;
|
---|
3647 | }
|
---|
3648 |
|
---|
3649 | argv = (char **)malloc (argc * sizeof (char *));
|
---|
3650 | if (argv == 0)
|
---|
3651 | abort ();
|
---|
3652 |
|
---|
3653 | cptr = line;
|
---|
3654 | argc = 0;
|
---|
3655 | for (;;)
|
---|
3656 | {
|
---|
3657 | while ((*cptr != 0)
|
---|
3658 | && (isspace ((unsigned char)*cptr)))
|
---|
3659 | cptr++;
|
---|
3660 | if (*cptr == 0)
|
---|
3661 | break;
|
---|
3662 | DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
|
---|
3663 | argv[argc++] = cptr;
|
---|
3664 | while ((*cptr != 0)
|
---|
3665 | && (!isspace((unsigned char)*cptr)))
|
---|
3666 | cptr++;
|
---|
3667 | if (*cptr != 0)
|
---|
3668 | *cptr++ = 0;
|
---|
3669 | }
|
---|
3670 | #else
|
---|
3671 | {
|
---|
3672 | /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
|
---|
3673 | int save = warn_undefined_variables_flag;
|
---|
3674 | warn_undefined_variables_flag = 0;
|
---|
3675 |
|
---|
3676 | shell = allocated_variable_expand_for_file ("$(SHELL)", file);
|
---|
3677 | #ifdef WINDOWS32
|
---|
3678 | /*
|
---|
3679 | * Convert to forward slashes so that construct_command_argv_internal()
|
---|
3680 | * is not confused.
|
---|
3681 | */
|
---|
3682 | if (shell) {
|
---|
3683 | char *p = w32ify (shell, 0);
|
---|
3684 | strcpy (shell, p);
|
---|
3685 | }
|
---|
3686 | #endif
|
---|
3687 | #ifdef __EMX__
|
---|
3688 | {
|
---|
3689 | static const char *unixroot = NULL;
|
---|
3690 | static const char *last_shell = "";
|
---|
3691 | static int init = 0;
|
---|
3692 | if (init == 0)
|
---|
3693 | {
|
---|
3694 | unixroot = getenv ("UNIXROOT");
|
---|
3695 | /* unixroot must be NULL or not empty */
|
---|
3696 | if (unixroot && unixroot[0] == '\0') unixroot = NULL;
|
---|
3697 | init = 1;
|
---|
3698 | }
|
---|
3699 |
|
---|
3700 | /* if we have an unixroot drive and if shell is not default_shell
|
---|
3701 | (which means it's either cmd.exe or the test has already been
|
---|
3702 | performed) and if shell is an absolute path without drive letter,
|
---|
3703 | try whether it exists e.g.: if "/bin/sh" does not exist use
|
---|
3704 | "$UNIXROOT/bin/sh" instead. */
|
---|
3705 | if (unixroot && shell && strcmp (shell, last_shell) != 0
|
---|
3706 | && (shell[0] == '/' || shell[0] == '\\'))
|
---|
3707 | {
|
---|
3708 | /* trying a new shell, check whether it exists */
|
---|
3709 | size_t size = strlen (shell);
|
---|
3710 | char *buf = xmalloc (size + 7);
|
---|
3711 | memcpy (buf, shell, size);
|
---|
3712 | memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
|
---|
3713 | if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
|
---|
3714 | {
|
---|
3715 | /* try the same for the unixroot drive */
|
---|
3716 | memmove (buf + 2, buf, size + 5);
|
---|
3717 | buf[0] = unixroot[0];
|
---|
3718 | buf[1] = unixroot[1];
|
---|
3719 | if (access (buf, F_OK) == 0)
|
---|
3720 | /* we have found a shell! */
|
---|
3721 | /* free(shell); */
|
---|
3722 | shell = buf;
|
---|
3723 | else
|
---|
3724 | free (buf);
|
---|
3725 | }
|
---|
3726 | else
|
---|
3727 | free (buf);
|
---|
3728 | }
|
---|
3729 | }
|
---|
3730 | #endif /* __EMX__ */
|
---|
3731 |
|
---|
3732 | ifs = allocated_variable_expand_for_file ("$(IFS)", file);
|
---|
3733 |
|
---|
3734 | warn_undefined_variables_flag = save;
|
---|
3735 | }
|
---|
3736 |
|
---|
3737 | argv = construct_command_argv_internal (line, restp, shell, ifs, batch_filename_ptr);
|
---|
3738 |
|
---|
3739 | free (shell);
|
---|
3740 | free (ifs);
|
---|
3741 | #endif /* !VMS */
|
---|
3742 | return argv;
|
---|
3743 | }
|
---|
3744 | |
---|
3745 |
|
---|
3746 | #if !defined(HAVE_DUP2) && !defined(_AMIGA)
|
---|
3747 | int
|
---|
3748 | dup2 (int old, int new)
|
---|
3749 | {
|
---|
3750 | int fd;
|
---|
3751 |
|
---|
3752 | (void) close (new);
|
---|
3753 | fd = dup (old);
|
---|
3754 | if (fd != new)
|
---|
3755 | {
|
---|
3756 | (void) close (fd);
|
---|
3757 | errno = EMFILE;
|
---|
3758 | return -1;
|
---|
3759 | }
|
---|
3760 |
|
---|
3761 | return fd;
|
---|
3762 | }
|
---|
3763 | #endif /* !HAPE_DUP2 && !_AMIGA */
|
---|