VirtualBox

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

Last change on this file since 2861 was 2861, checked in by bird, 9 years ago

Updates

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

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