VirtualBox

source: kBuild/trunk/src/kmk/job.c

Last change on this file was 3432, checked in by bird, 4 years ago

kmk: Quick output hack to repeat the failure output at the end of die() as it may easily get lost in output from other jobs (problem on systems with lots of cores).

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette