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