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