source: trunk/src/kmk/job.c

Last change on this file was 3656, checked in by bird, 9 months ago

kmk/job.c: Deal with escape sequences inside double quotes when we're using kmk_ash. Simplified some windo32 special double-quote handling, some of which looks bogus. Shut up warning. Classify kmk_ash and kmk_kash as bourne shells.

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