VirtualBox

source: kBuild/vendor/gnumake/4.2.1-git/job.c

Last change on this file was 3138, checked in by bird, 7 years ago

Imported make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6) from https://git.savannah.gnu.org/git/make.git.

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

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