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