Fix autoconf macros for detecting DOS-style pathnames.
[make.git] / job.c
blobd56bfe9eb8a06c34cb51082c4694d1935cdc84de
1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
10 version.
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "make.h"
21 #include <assert.h>
23 #include "job.h"
24 #include "debug.h"
25 #include "filedef.h"
26 #include "commands.h"
27 #include "variable.h"
28 #include "debug.h"
30 #include <string.h>
32 /* Default shell to use. */
33 #ifdef WINDOWS32
34 #include <windows.h>
36 char *default_shell = "sh.exe";
37 int no_default_sh_exe = 1;
38 int batch_mode_shell = 1;
39 HANDLE main_thread;
41 #elif defined (_AMIGA)
43 char default_shell[] = "";
44 extern int MyExecute (char **);
45 int batch_mode_shell = 0;
47 #elif defined (__MSDOS__)
49 /* The default shell is a pointer so we can change it if Makefile
50 says so. It is without an explicit path so we get a chance
51 to search the $PATH for it (since MSDOS doesn't have standard
52 directories we could trust). */
53 char *default_shell = "command.com";
54 int batch_mode_shell = 0;
56 #elif defined (__EMX__)
58 char *default_shell = "/bin/sh";
59 int batch_mode_shell = 0;
61 #elif defined (VMS)
63 # include <descrip.h>
64 char default_shell[] = "";
65 int batch_mode_shell = 0;
67 #elif defined (__riscos__)
69 char default_shell[] = "";
70 int batch_mode_shell = 0;
72 #else
74 char default_shell[] = "/bin/sh";
75 int batch_mode_shell = 0;
77 #endif
79 #ifdef __MSDOS__
80 # include <process.h>
81 static int execute_by_shell;
82 static int dos_pid = 123;
83 int dos_status;
84 int dos_command_running;
85 #endif /* __MSDOS__ */
87 #ifdef _AMIGA
88 # include <proto/dos.h>
89 static int amiga_pid = 123;
90 static int amiga_status;
91 static char amiga_bname[32];
92 static int amiga_batch_file;
93 #endif /* Amiga. */
95 #ifdef VMS
96 # ifndef __GNUC__
97 # include <processes.h>
98 # endif
99 # include <starlet.h>
100 # include <lib$routines.h>
101 static void vmsWaitForChildren (int *);
102 #endif
104 #ifdef WINDOWS32
105 # include <windows.h>
106 # include <io.h>
107 # include <process.h>
108 # include "sub_proc.h"
109 # include "w32err.h"
110 # include "pathstuff.h"
111 # define WAIT_NOHANG 1
112 #endif /* WINDOWS32 */
114 #ifdef __EMX__
115 # include <process.h>
116 #endif
118 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
119 # include <sys/wait.h>
120 #endif
122 #ifdef HAVE_WAITPID
123 # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
124 #else /* Don't have waitpid. */
125 # ifdef HAVE_WAIT3
126 # ifndef wait3
127 extern int wait3 ();
128 # endif
129 # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
130 # endif /* Have wait3. */
131 #endif /* Have waitpid. */
133 #if !defined (wait) && !defined (POSIX)
134 int wait ();
135 #endif
137 #ifndef HAVE_UNION_WAIT
139 # define WAIT_T int
141 # ifndef WTERMSIG
142 # define WTERMSIG(x) ((x) & 0x7f)
143 # endif
144 # ifndef WCOREDUMP
145 # define WCOREDUMP(x) ((x) & 0x80)
146 # endif
147 # ifndef WEXITSTATUS
148 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
149 # endif
150 # ifndef WIFSIGNALED
151 # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
152 # endif
153 # ifndef WIFEXITED
154 # define WIFEXITED(x) (WTERMSIG (x) == 0)
155 # endif
157 #else /* Have `union wait'. */
159 # define WAIT_T union wait
160 # ifndef WTERMSIG
161 # define WTERMSIG(x) ((x).w_termsig)
162 # endif
163 # ifndef WCOREDUMP
164 # define WCOREDUMP(x) ((x).w_coredump)
165 # endif
166 # ifndef WEXITSTATUS
167 # define WEXITSTATUS(x) ((x).w_retcode)
168 # endif
169 # ifndef WIFSIGNALED
170 # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
171 # endif
172 # ifndef WIFEXITED
173 # define WIFEXITED(x) (WTERMSIG(x) == 0)
174 # endif
176 #endif /* Don't have `union wait'. */
178 #if !defined(HAVE_UNISTD_H) && !defined(WINDOWS32)
179 int dup2 ();
180 int execve ();
181 void _exit ();
182 # ifndef VMS
183 int geteuid ();
184 int getegid ();
185 int setgid ();
186 int getgid ();
187 # endif
188 #endif
190 /* Different systems have different requirements for pid_t.
191 Plus we have to support gettext string translation... Argh. */
192 static const char *
193 pid2str (pid_t pid)
195 static char pidstring[100];
196 #if defined(WINDOWS32) && (__GNUC__ > 3 || _MSC_VER > 1300)
197 /* %Id is only needed for 64-builds, which were not supported by
198 older versions of Windows compilers. */
199 sprintf (pidstring, "%Id", pid);
200 #else
201 sprintf (pidstring, "%lu", (unsigned long) pid);
202 #endif
203 return pidstring;
206 int getloadavg (double loadavg[], int nelem);
207 int start_remote_job (char **argv, char **envp, int stdin_fd, int *is_remote,
208 int *id_ptr, int *used_stdin);
209 int start_remote_job_p (int);
210 int remote_status (int *exit_code_ptr, int *signal_ptr, int *coredump_ptr,
211 int block);
213 RETSIGTYPE child_handler (int);
214 static void free_child (struct child *);
215 static void start_job_command (struct child *child);
216 static int load_too_high (void);
217 static int job_next_command (struct child *);
218 static int start_waiting_job (struct child *);
220 /* Chain of all live (or recently deceased) children. */
222 struct child *children = 0;
224 /* Number of children currently running. */
226 unsigned int job_slots_used = 0;
228 /* Nonzero if the `good' standard input is in use. */
230 static int good_stdin_used = 0;
232 /* Chain of children waiting to run until the load average goes down. */
234 static struct child *waiting_jobs = 0;
236 /* Non-zero if we use a *real* shell (always so on Unix). */
238 int unixy_shell = 1;
240 /* Number of jobs started in the current second. */
242 unsigned long job_counter = 0;
244 /* Number of jobserver tokens this instance is currently using. */
246 unsigned int jobserver_tokens = 0;
248 #ifdef WINDOWS32
250 * The macro which references this function is defined in make.h.
253 w32_kill(pid_t pid, int sig)
255 return ((process_kill((HANDLE)pid, sig) == TRUE) ? 0 : -1);
258 /* This function creates a temporary file name with an extension specified
259 * by the unixy arg.
260 * Return an xmalloc'ed string of a newly created temp file and its
261 * file descriptor, or die. */
262 static char *
263 create_batch_file (char const *base, int unixy, int *fd)
265 const char *const ext = unixy ? "sh" : "bat";
266 const char *error_string = NULL;
267 char temp_path[MAXPATHLEN]; /* need to know its length */
268 unsigned path_size = GetTempPath(sizeof temp_path, temp_path);
269 int path_is_dot = 0;
270 unsigned uniq = 1;
271 const unsigned sizemax = strlen (base) + strlen (ext) + 10;
273 if (path_size == 0)
275 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
276 path_is_dot = 1;
279 while (path_size > 0 &&
280 path_size + sizemax < sizeof temp_path &&
281 uniq < 0x10000)
283 unsigned size = sprintf (temp_path + path_size,
284 "%s%s-%x.%s",
285 temp_path[path_size - 1] == '\\' ? "" : "\\",
286 base, uniq, ext);
287 HANDLE h = CreateFile (temp_path, /* file name */
288 GENERIC_READ | GENERIC_WRITE, /* desired access */
289 0, /* no share mode */
290 NULL, /* default security attributes */
291 CREATE_NEW, /* creation disposition */
292 FILE_ATTRIBUTE_NORMAL | /* flags and attributes */
293 FILE_ATTRIBUTE_TEMPORARY, /* we'll delete it */
294 NULL); /* no template file */
296 if (h == INVALID_HANDLE_VALUE)
298 const DWORD er = GetLastError();
300 if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS)
301 ++uniq;
303 /* the temporary path is not guaranteed to exist */
304 else if (path_is_dot == 0)
306 path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
307 path_is_dot = 1;
310 else
312 error_string = map_windows32_error_to_string (er);
313 break;
316 else
318 const unsigned final_size = path_size + size + 1;
319 char *const path = xmalloc (final_size);
320 memcpy (path, temp_path, final_size);
321 *fd = _open_osfhandle ((intptr_t)h, 0);
322 if (unixy)
324 char *p;
325 int ch;
326 for (p = path; (ch = *p) != 0; ++p)
327 if (ch == '\\')
328 *p = '/';
330 return path; /* good return */
334 *fd = -1;
335 if (error_string == NULL)
336 error_string = _("Cannot create a temporary file\n");
337 fatal (NILF, error_string);
339 /* not reached */
340 return NULL;
342 #endif /* WINDOWS32 */
344 #ifdef __EMX__
345 /* returns whether path is assumed to be a unix like shell. */
347 _is_unixy_shell (const char *path)
349 /* list of non unix shells */
350 const char *known_os2shells[] = {
351 "cmd.exe",
352 "cmd",
353 "4os2.exe",
354 "4os2",
355 "4dos.exe",
356 "4dos",
357 "command.com",
358 "command",
359 NULL
362 /* find the rightmost '/' or '\\' */
363 const char *name = strrchr (path, '/');
364 const char *p = strrchr (path, '\\');
365 unsigned i;
367 if (name && p) /* take the max */
368 name = (name > p) ? name : p;
369 else if (p) /* name must be 0 */
370 name = p;
371 else if (!name) /* name and p must be 0 */
372 name = path;
374 if (*name == '/' || *name == '\\') name++;
376 i = 0;
377 while (known_os2shells[i] != NULL) {
378 if (strcasecmp (name, known_os2shells[i]) == 0)
379 return 0; /* not a unix shell */
380 i++;
383 /* in doubt assume a unix like shell */
384 return 1;
386 #endif /* __EMX__ */
388 /* determines whether path looks to be a Bourne-like shell. */
390 is_bourne_compatible_shell (const char *path)
392 /* list of known unix (Bourne-like) shells */
393 const char *unix_shells[] = {
394 "sh",
395 "bash",
396 "ksh",
397 "rksh",
398 "zsh",
399 "ash",
400 "dash",
401 NULL
403 unsigned i, len;
405 /* find the rightmost '/' or '\\' */
406 const char *name = strrchr (path, '/');
407 char *p = strrchr (path, '\\');
409 if (name && p) /* take the max */
410 name = (name > p) ? name : p;
411 else if (p) /* name must be 0 */
412 name = p;
413 else if (!name) /* name and p must be 0 */
414 name = path;
416 if (*name == '/' || *name == '\\') name++;
418 /* this should be able to deal with extensions on Windows-like systems */
419 for (i = 0; unix_shells[i] != NULL; i++) {
420 len = strlen(unix_shells[i]);
421 #if defined(WINDOWS32) || defined(__MSDOS__)
422 if ((strncasecmp (name, unix_shells[i], len) == 0) &&
423 (strlen(name) >= len && (name[len] == '\0' || name[len] == '.')))
424 #else
425 if ((strncmp (name, unix_shells[i], len) == 0) &&
426 (strlen(name) >= len && name[len] == '\0'))
427 #endif
428 return 1; /* a known unix-style shell */
431 /* if not on the list, assume it's not a Bourne-like shell */
432 return 0;
436 /* Write an error message describing the exit status given in
437 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
438 Append "(ignored)" if IGNORED is nonzero. */
440 static void
441 child_error (const struct file *file,
442 int exit_code, int exit_sig, int coredump, int ignored)
444 const char *nm;
445 const char *pre = "*** ";
446 const char *post = "";
447 const char *dump = "";
448 struct floc *flocp = &file->cmds->fileinfo;
450 if (ignored && silent_flag)
451 return;
453 if (exit_sig && coredump)
454 dump = _(" (core dumped)");
456 if (ignored)
458 pre = "";
459 post = _(" (ignored)");
462 if (! flocp->filenm)
463 nm = _("<builtin>");
464 else
466 char *a = alloca (strlen (flocp->filenm) + 1 + 11 + 1);
467 sprintf (a, "%s:%lu", flocp->filenm, flocp->lineno);
468 nm = a;
470 message (0, _("%s: recipe for target `%s' failed"), nm, file->name);
472 #ifdef VMS
473 if (!(exit_code & 1))
474 error (NILF, _("%s[%s] Error 0x%x%s"), pre, file->name, exit_code, post);
475 #else
476 if (exit_sig == 0)
477 error (NILF, _("%s[%s] Error %d%s"), pre, file->name, exit_code, post);
478 else
479 error (NILF, _("%s[%s] %s%s%s"),
480 pre, file->name, strsignal (exit_sig), dump, post);
481 #endif /* VMS */
485 /* Handle a dead child. This handler may or may not ever be installed.
487 If we're using the jobserver feature, we need it. First, installing it
488 ensures the read will interrupt on SIGCHLD. Second, we close the dup'd
489 read FD to ensure we don't enter another blocking read without reaping all
490 the dead children. In this case we don't need the dead_children count.
492 If we don't have either waitpid or wait3, then make is unreliable, but we
493 use the dead_children count to reap children as best we can. */
495 static unsigned int dead_children = 0;
497 RETSIGTYPE
498 child_handler (int sig UNUSED)
500 ++dead_children;
502 if (job_rfd >= 0)
504 close (job_rfd);
505 job_rfd = -1;
508 #ifdef __EMX__
509 /* The signal handler must called only once! */
510 signal (SIGCHLD, SIG_DFL);
511 #endif
513 /* This causes problems if the SIGCHLD interrupts a printf().
514 DB (DB_JOBS, (_("Got a SIGCHLD; %u unreaped children.\n"), dead_children));
518 extern int shell_function_pid, shell_function_completed;
520 /* Reap all dead children, storing the returned status and the new command
521 state (`cs_finished') in the `file' member of the `struct child' for the
522 dead child, and removing the child from the chain. In addition, if BLOCK
523 nonzero, we block in this function until we've reaped at least one
524 complete child, waiting for it to die if necessary. If ERR is nonzero,
525 print an error message first. */
527 void
528 reap_children (int block, int err)
530 #ifndef WINDOWS32
531 WAIT_T status;
532 #endif
533 /* Initially, assume we have some. */
534 int reap_more = 1;
536 #ifdef WAIT_NOHANG
537 # define REAP_MORE reap_more
538 #else
539 # define REAP_MORE dead_children
540 #endif
542 /* As long as:
544 We have at least one child outstanding OR a shell function in progress,
546 We're blocking for a complete child OR there are more children to reap
548 we'll keep reaping children. */
550 while ((children != 0 || shell_function_pid != 0)
551 && (block || REAP_MORE))
553 int remote = 0;
554 pid_t pid;
555 int exit_code, exit_sig, coredump;
556 struct child *lastc, *c;
557 int child_failed;
558 int any_remote, any_local;
559 int dontcare;
561 if (err && block)
563 static int printed = 0;
565 /* We might block for a while, so let the user know why.
566 Only print this message once no matter how many jobs are left. */
567 fflush (stdout);
568 if (!printed)
569 error (NILF, _("*** Waiting for unfinished jobs...."));
570 printed = 1;
573 /* We have one less dead child to reap. As noted in
574 child_handler() above, this count is completely unimportant for
575 all modern, POSIX-y systems that support wait3() or waitpid().
576 The rest of this comment below applies only to early, broken
577 pre-POSIX systems. We keep the count only because... it's there...
579 The test and decrement are not atomic; if it is compiled into:
580 register = dead_children - 1;
581 dead_children = register;
582 a SIGCHLD could come between the two instructions.
583 child_handler increments dead_children.
584 The second instruction here would lose that increment. But the
585 only effect of dead_children being wrong is that we might wait
586 longer than necessary to reap a child, and lose some parallelism;
587 and we might print the "Waiting for unfinished jobs" message above
588 when not necessary. */
590 if (dead_children > 0)
591 --dead_children;
593 any_remote = 0;
594 any_local = shell_function_pid != 0;
595 for (c = children; c != 0; c = c->next)
597 any_remote |= c->remote;
598 any_local |= ! c->remote;
599 DB (DB_JOBS, (_("Live child %p (%s) PID %s %s\n"),
600 c, c->file->name, pid2str (c->pid),
601 c->remote ? _(" (remote)") : ""));
602 #ifdef VMS
603 break;
604 #endif
607 /* First, check for remote children. */
608 if (any_remote)
609 pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
610 else
611 pid = 0;
613 if (pid > 0)
614 /* We got a remote child. */
615 remote = 1;
616 else if (pid < 0)
618 /* A remote status command failed miserably. Punt. */
619 remote_status_lose:
620 pfatal_with_name ("remote_status");
622 else
624 /* No remote children. Check for local children. */
625 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
626 if (any_local)
628 #ifdef VMS
629 vmsWaitForChildren (&status);
630 pid = c->pid;
631 #else
632 #ifdef WAIT_NOHANG
633 if (!block)
634 pid = WAIT_NOHANG (&status);
635 else
636 #endif
637 EINTRLOOP(pid, wait (&status));
638 #endif /* !VMS */
640 else
641 pid = 0;
643 if (pid < 0)
645 /* The wait*() failed miserably. Punt. */
646 pfatal_with_name ("wait");
648 else if (pid > 0)
650 /* We got a child exit; chop the status word up. */
651 exit_code = WEXITSTATUS (status);
652 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
653 coredump = WCOREDUMP (status);
655 /* If we have started jobs in this second, remove one. */
656 if (job_counter)
657 --job_counter;
659 else
661 /* No local children are dead. */
662 reap_more = 0;
664 if (!block || !any_remote)
665 break;
667 /* Now try a blocking wait for a remote child. */
668 pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
669 if (pid < 0)
670 goto remote_status_lose;
671 else if (pid == 0)
672 /* No remote children either. Finally give up. */
673 break;
675 /* We got a remote child. */
676 remote = 1;
678 #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
680 #ifdef __MSDOS__
681 /* Life is very different on MSDOS. */
682 pid = dos_pid - 1;
683 status = dos_status;
684 exit_code = WEXITSTATUS (status);
685 if (exit_code == 0xff)
686 exit_code = -1;
687 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
688 coredump = 0;
689 #endif /* __MSDOS__ */
690 #ifdef _AMIGA
691 /* Same on Amiga */
692 pid = amiga_pid - 1;
693 status = amiga_status;
694 exit_code = amiga_status;
695 exit_sig = 0;
696 coredump = 0;
697 #endif /* _AMIGA */
698 #ifdef WINDOWS32
700 HANDLE hPID;
701 int werr;
702 HANDLE hcTID, hcPID;
703 DWORD dwWaitStatus = 0;
704 exit_code = 0;
705 exit_sig = 0;
706 coredump = 0;
708 /* Record the thread ID of the main process, so that we
709 could suspend it in the signal handler. */
710 if (!main_thread)
712 hcTID = GetCurrentThread ();
713 hcPID = GetCurrentProcess ();
714 if (!DuplicateHandle (hcPID, hcTID, hcPID, &main_thread, 0,
715 FALSE, DUPLICATE_SAME_ACCESS))
717 DWORD e = GetLastError ();
718 fprintf (stderr,
719 "Determine main thread ID (Error %ld: %s)\n",
720 e, map_windows32_error_to_string(e));
722 else
723 DB (DB_VERBOSE, ("Main thread handle = %p\n", main_thread));
726 /* wait for anything to finish */
727 hPID = process_wait_for_any(block, &dwWaitStatus);
728 if (hPID)
731 /* was an error found on this process? */
732 werr = process_last_err(hPID);
734 /* get exit data */
735 exit_code = process_exit_code(hPID);
737 if (werr)
738 fprintf(stderr, "make (e=%d): %s",
739 exit_code, map_windows32_error_to_string(exit_code));
741 /* signal */
742 exit_sig = process_signal(hPID);
744 /* cleanup process */
745 process_cleanup(hPID);
747 coredump = 0;
749 else if (dwWaitStatus == WAIT_FAILED)
751 /* The WaitForMultipleObjects() failed miserably. Punt. */
752 pfatal_with_name ("WaitForMultipleObjects");
754 else if (dwWaitStatus == WAIT_TIMEOUT)
756 /* No child processes are finished. Give up waiting. */
757 reap_more = 0;
758 break;
761 pid = (pid_t) hPID;
763 #endif /* WINDOWS32 */
766 /* Check if this is the child of the `shell' function. */
767 if (!remote && pid == shell_function_pid)
769 /* It is. Leave an indicator for the `shell' function. */
770 if (exit_sig == 0 && exit_code == 127)
771 shell_function_completed = -1;
772 else
773 shell_function_completed = 1;
774 break;
777 child_failed = exit_sig != 0 || exit_code != 0;
779 /* Search for a child matching the deceased one. */
780 lastc = 0;
781 for (c = children; c != 0; lastc = c, c = c->next)
782 if (c->remote == remote && c->pid == pid)
783 break;
785 if (c == 0)
786 /* An unknown child died.
787 Ignore it; it was inherited from our invoker. */
788 continue;
790 DB (DB_JOBS, (child_failed
791 ? _("Reaping losing child %p PID %s %s\n")
792 : _("Reaping winning child %p PID %s %s\n"),
793 c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
795 if (c->sh_batch_file) {
796 DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
797 c->sh_batch_file));
799 /* just try and remove, don't care if this fails */
800 remove (c->sh_batch_file);
802 /* all done with memory */
803 free (c->sh_batch_file);
804 c->sh_batch_file = NULL;
807 /* If this child had the good stdin, say it is now free. */
808 if (c->good_stdin)
809 good_stdin_used = 0;
811 dontcare = c->dontcare;
813 if (child_failed && !c->noerror && !ignore_errors_flag)
815 /* The commands failed. Write an error message,
816 delete non-precious targets, and abort. */
817 static int delete_on_error = -1;
819 if (!dontcare)
820 child_error (c->file, exit_code, exit_sig, coredump, 0);
822 c->file->update_status = 2;
823 if (delete_on_error == -1)
825 struct file *f = lookup_file (".DELETE_ON_ERROR");
826 delete_on_error = f != 0 && f->is_target;
828 if (exit_sig != 0 || delete_on_error)
829 delete_child_targets (c);
831 else
833 if (child_failed)
835 /* The commands failed, but we don't care. */
836 child_error (c->file, exit_code, exit_sig, coredump, 1);
837 child_failed = 0;
840 /* If there are more commands to run, try to start them. */
841 if (job_next_command (c))
843 if (handling_fatal_signal)
845 /* Never start new commands while we are dying.
846 Since there are more commands that wanted to be run,
847 the target was not completely remade. So we treat
848 this as if a command had failed. */
849 c->file->update_status = 2;
851 else
853 /* Check again whether to start remotely.
854 Whether or not we want to changes over time.
855 Also, start_remote_job may need state set up
856 by start_remote_job_p. */
857 c->remote = start_remote_job_p (0);
858 start_job_command (c);
859 /* Fatal signals are left blocked in case we were
860 about to put that child on the chain. But it is
861 already there, so it is safe for a fatal signal to
862 arrive now; it will clean up this child's targets. */
863 unblock_sigs ();
864 if (c->file->command_state == cs_running)
865 /* We successfully started the new command.
866 Loop to reap more children. */
867 continue;
870 if (c->file->update_status != 0)
871 /* We failed to start the commands. */
872 delete_child_targets (c);
874 else
875 /* There are no more commands. We got through them all
876 without an unignored error. Now the target has been
877 successfully updated. */
878 c->file->update_status = 0;
881 /* When we get here, all the commands for C->file are finished
882 (or aborted) and C->file->update_status contains 0 or 2. But
883 C->file->command_state is still cs_running if all the commands
884 ran; notice_finish_file looks for cs_running to tell it that
885 it's interesting to check the file's modtime again now. */
887 if (! handling_fatal_signal)
888 /* Notice if the target of the commands has been changed.
889 This also propagates its values for command_state and
890 update_status to its also_make files. */
891 notice_finished_file (c->file);
893 DB (DB_JOBS, (_("Removing child %p PID %s%s from chain.\n"),
894 c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
896 /* Block fatal signals while frobnicating the list, so that
897 children and job_slots_used are always consistent. Otherwise
898 a fatal signal arriving after the child is off the chain and
899 before job_slots_used is decremented would believe a child was
900 live and call reap_children again. */
901 block_sigs ();
903 /* There is now another slot open. */
904 if (job_slots_used > 0)
905 --job_slots_used;
907 /* Remove the child from the chain and free it. */
908 if (lastc == 0)
909 children = c->next;
910 else
911 lastc->next = c->next;
913 free_child (c);
915 unblock_sigs ();
917 /* If the job failed, and the -k flag was not given, die,
918 unless we are already in the process of dying. */
919 if (!err && child_failed && !dontcare && !keep_going_flag &&
920 /* fatal_error_signal will die with the right signal. */
921 !handling_fatal_signal)
922 die (2);
924 /* Only block for one child. */
925 block = 0;
928 return;
931 /* Free the storage allocated for CHILD. */
933 static void
934 free_child (struct child *child)
936 if (!jobserver_tokens)
937 fatal (NILF, "INTERNAL: Freeing child %p (%s) but no tokens left!\n",
938 child, child->file->name);
940 /* If we're using the jobserver and this child is not the only outstanding
941 job, put a token back into the pipe for it. */
943 #ifdef WINDOWS32
944 if (has_jobserver_semaphore() && jobserver_tokens > 1)
946 if (! release_jobserver_semaphore())
948 DWORD err = GetLastError();
949 fatal (NILF, _("release jobserver semaphore: (Error %ld: %s)"),
950 err, map_windows32_error_to_string(err));
953 DB (DB_JOBS, (_("Released token for child %p (%s).\n"), child, child->file->name));
955 #else
956 if (job_fds[1] >= 0 && jobserver_tokens > 1)
958 char token = '+';
959 int r;
961 /* Write a job token back to the pipe. */
963 EINTRLOOP (r, write (job_fds[1], &token, 1));
964 if (r != 1)
965 pfatal_with_name (_("write jobserver"));
967 DB (DB_JOBS, (_("Released token for child %p (%s).\n"),
968 child, child->file->name));
970 #endif
972 --jobserver_tokens;
974 if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
975 return;
977 if (child->command_lines != 0)
979 register unsigned int i;
980 for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
981 free (child->command_lines[i]);
982 free (child->command_lines);
985 if (child->environment != 0)
987 register char **ep = child->environment;
988 while (*ep != 0)
989 free (*ep++);
990 free (child->environment);
993 free (child);
996 #ifdef POSIX
997 extern sigset_t fatal_signal_set;
998 #endif
1000 void
1001 block_sigs (void)
1003 #ifdef POSIX
1004 (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
1005 #else
1006 # ifdef HAVE_SIGSETMASK
1007 (void) sigblock (fatal_signal_mask);
1008 # endif
1009 #endif
1012 #ifdef POSIX
1013 void
1014 unblock_sigs (void)
1016 sigset_t empty;
1017 sigemptyset (&empty);
1018 sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
1020 #endif
1022 #if defined(MAKE_JOBSERVER) && !defined(WINDOWS32)
1023 RETSIGTYPE
1024 job_noop (int sig UNUSED)
1027 /* Set the child handler action flags to FLAGS. */
1028 static void
1029 set_child_handler_action_flags (int set_handler, int set_alarm)
1031 struct sigaction sa;
1033 #ifdef __EMX__
1034 /* The child handler must be turned off here. */
1035 signal (SIGCHLD, SIG_DFL);
1036 #endif
1038 memset (&sa, '\0', sizeof sa);
1039 sa.sa_handler = child_handler;
1040 sa.sa_flags = set_handler ? 0 : SA_RESTART;
1041 #if defined SIGCHLD
1042 sigaction (SIGCHLD, &sa, NULL);
1043 #endif
1044 #if defined SIGCLD && SIGCLD != SIGCHLD
1045 sigaction (SIGCLD, &sa, NULL);
1046 #endif
1047 #if defined SIGALRM
1048 if (set_alarm)
1050 /* If we're about to enter the read(), set an alarm to wake up in a
1051 second so we can check if the load has dropped and we can start more
1052 work. On the way out, turn off the alarm and set SIG_DFL. */
1053 alarm (set_handler ? 1 : 0);
1054 sa.sa_handler = set_handler ? job_noop : SIG_DFL;
1055 sa.sa_flags = 0;
1056 sigaction (SIGALRM, &sa, NULL);
1058 #endif
1060 #endif
1063 /* Start a job to run the commands specified in CHILD.
1064 CHILD is updated to reflect the commands and ID of the child process.
1066 NOTE: On return fatal signals are blocked! The caller is responsible
1067 for calling `unblock_sigs', once the new child is safely on the chain so
1068 it can be cleaned up in the event of a fatal signal. */
1070 static void
1071 start_job_command (struct child *child)
1073 #if !defined(_AMIGA) && !defined(WINDOWS32)
1074 static int bad_stdin = -1;
1075 #endif
1076 char *p;
1077 /* Must be volatile to silence bogus GCC warning about longjmp/vfork. */
1078 volatile int flags;
1079 #ifdef VMS
1080 char *argv;
1081 #else
1082 char **argv;
1083 #endif
1085 /* If we have a completely empty commandset, stop now. */
1086 if (!child->command_ptr)
1087 goto next_command;
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]);
1094 p = child->command_ptr;
1095 child->noerror = ((flags & COMMANDS_NOERROR) != 0);
1097 while (*p != '\0')
1099 if (*p == '@')
1100 flags |= COMMANDS_SILENT;
1101 else if (*p == '+')
1102 flags |= COMMANDS_RECURSE;
1103 else if (*p == '-')
1104 child->noerror = 1;
1105 else if (!isblank ((unsigned char)*p))
1106 break;
1107 ++p;
1110 /* Update the file's command flags with any new ones we found. We only
1111 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
1112 now marking more commands recursive than should be in the case of
1113 multiline define/endef scripts where only one line is marked "+". In
1114 order to really fix this, we'll have to keep a lines_flags for every
1115 actual line, after expansion. */
1116 child->file->cmds->lines_flags[child->command_line - 1]
1117 |= flags & COMMANDS_RECURSE;
1119 /* POSIX requires that a recipe prefix after a backslash-newline should
1120 be ignored. Remove it now so the output is correct. */
1122 char prefix = child->file->cmds->recipe_prefix;
1123 char *p1, *p2;
1124 p1 = p2 = p;
1125 while (*p1 != '\0')
1127 *(p2++) = *p1;
1128 if (p1[0] == '\n' && p1[1] == prefix)
1129 ++p1;
1130 ++p1;
1132 *p2 = *p1;
1135 /* Figure out an argument list from this command line. */
1137 char *end = 0;
1138 #ifdef VMS
1139 argv = p;
1140 #else
1141 argv = construct_command_argv (p, &end, child->file,
1142 child->file->cmds->lines_flags[child->command_line - 1],
1143 &child->sh_batch_file);
1144 #endif
1145 if (end == NULL)
1146 child->command_ptr = NULL;
1147 else
1149 *end++ = '\0';
1150 child->command_ptr = end;
1154 /* If -q was given, say that updating `failed' if there was any text on the
1155 command line, or `succeeded' otherwise. The exit status of 1 tells the
1156 user that -q is saying `something to do'; the exit status for a random
1157 error is 2. */
1158 if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
1160 #ifndef VMS
1161 free (argv[0]);
1162 free (argv);
1163 #endif
1164 child->file->update_status = 1;
1165 notice_finished_file (child->file);
1166 return;
1169 if (touch_flag && !(flags & COMMANDS_RECURSE))
1171 /* Go on to the next command. It might be the recursive one.
1172 We construct ARGV only to find the end of the command line. */
1173 #ifndef VMS
1174 if (argv)
1176 free (argv[0]);
1177 free (argv);
1179 #endif
1180 argv = 0;
1183 if (argv == 0)
1185 next_command:
1186 #ifdef __MSDOS__
1187 execute_by_shell = 0; /* in case construct_command_argv sets it */
1188 #endif
1189 /* This line has no commands. Go to the next. */
1190 if (job_next_command (child))
1191 start_job_command (child);
1192 else
1194 /* No more commands. Make sure we're "running"; we might not be if
1195 (e.g.) all commands were skipped due to -n. */
1196 set_command_state (child->file, cs_running);
1197 child->file->update_status = 0;
1198 notice_finished_file (child->file);
1200 return;
1203 /* Print out the command. If silent, we call `message' with null so it
1204 can log the working directory before the command's own error messages
1205 appear. */
1207 message (0, (just_print_flag || trace_flag
1208 || (!(flags & COMMANDS_SILENT) && !silent_flag))
1209 ? "%s" : (char *) 0, p);
1211 /* Tell update_goal_chain that a command has been started on behalf of
1212 this target. It is important that this happens here and not in
1213 reap_children (where we used to do it), because reap_children might be
1214 reaping children from a different target. We want this increment to
1215 guaranteedly indicate that a command was started for the dependency
1216 chain (i.e., update_file recursion chain) we are processing. */
1218 ++commands_started;
1220 /* Optimize an empty command. People use this for timestamp rules,
1221 so avoid forking a useless shell. Do this after we increment
1222 commands_started so make still treats this special case as if it
1223 performed some action (makes a difference as to what messages are
1224 printed, etc. */
1226 #if !defined(VMS) && !defined(_AMIGA)
1227 if (
1228 #if defined __MSDOS__ || defined (__EMX__)
1229 unixy_shell /* the test is complicated and we already did it */
1230 #else
1231 (argv[0] && is_bourne_compatible_shell(argv[0]))
1232 #endif
1233 && (argv[1] && argv[1][0] == '-'
1235 ((argv[1][1] == 'c' && argv[1][2] == '\0')
1237 (argv[1][1] == 'e' && argv[1][2] == 'c' && argv[1][3] == '\0')))
1238 && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
1239 && argv[3] == NULL)
1241 free (argv[0]);
1242 free (argv);
1243 goto next_command;
1245 #endif /* !VMS && !_AMIGA */
1247 /* If -n was given, recurse to get the next line in the sequence. */
1249 if (just_print_flag && !(flags & COMMANDS_RECURSE))
1251 #ifndef VMS
1252 free (argv[0]);
1253 free (argv);
1254 #endif
1255 goto next_command;
1258 /* Flush the output streams so they won't have things written twice. */
1260 fflush (stdout);
1261 fflush (stderr);
1263 #ifndef VMS
1264 #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
1266 /* Set up a bad standard input that reads from a broken pipe. */
1268 if (bad_stdin == -1)
1270 /* Make a file descriptor that is the read end of a broken pipe.
1271 This will be used for some children's standard inputs. */
1272 int pd[2];
1273 if (pipe (pd) == 0)
1275 /* Close the write side. */
1276 (void) close (pd[1]);
1277 /* Save the read side. */
1278 bad_stdin = pd[0];
1280 /* Set the descriptor to close on exec, so it does not litter any
1281 child's descriptor table. When it is dup2'd onto descriptor 0,
1282 that descriptor will not close on exec. */
1283 CLOSE_ON_EXEC (bad_stdin);
1287 #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
1289 /* Decide whether to give this child the `good' standard input
1290 (one that points to the terminal or whatever), or the `bad' one
1291 that points to the read side of a broken pipe. */
1293 child->good_stdin = !good_stdin_used;
1294 if (child->good_stdin)
1295 good_stdin_used = 1;
1297 #endif /* !VMS */
1299 child->deleted = 0;
1301 #ifndef _AMIGA
1302 /* Set up the environment for the child. */
1303 if (child->environment == 0)
1304 child->environment = target_environment (child->file);
1305 #endif
1307 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1309 #ifndef VMS
1310 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1311 if (child->remote)
1313 int is_remote, id, used_stdin;
1314 if (start_remote_job (argv, child->environment,
1315 child->good_stdin ? 0 : bad_stdin,
1316 &is_remote, &id, &used_stdin))
1317 /* Don't give up; remote execution may fail for various reasons. If
1318 so, simply run the job locally. */
1319 goto run_local;
1320 else
1322 if (child->good_stdin && !used_stdin)
1324 child->good_stdin = 0;
1325 good_stdin_used = 0;
1327 child->remote = is_remote;
1328 child->pid = id;
1331 else
1332 #endif /* !VMS */
1334 /* Fork the child process. */
1336 char **parent_environ;
1338 run_local:
1339 block_sigs ();
1341 child->remote = 0;
1343 #ifdef VMS
1344 if (!child_execute_job (argv, child)) {
1345 /* Fork failed! */
1346 perror_with_name ("vfork", "");
1347 goto error;
1350 #else
1352 parent_environ = environ;
1354 # ifdef __EMX__
1355 /* If we aren't running a recursive command and we have a jobserver
1356 pipe, close it before exec'ing. */
1357 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1359 CLOSE_ON_EXEC (job_fds[0]);
1360 CLOSE_ON_EXEC (job_fds[1]);
1362 if (job_rfd >= 0)
1363 CLOSE_ON_EXEC (job_rfd);
1365 /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
1366 child->pid = child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
1367 argv, child->environment);
1368 if (child->pid < 0)
1370 /* spawn failed! */
1371 unblock_sigs ();
1372 perror_with_name ("spawn", "");
1373 goto error;
1376 /* undo CLOSE_ON_EXEC() after the child process has been started */
1377 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1379 fcntl (job_fds[0], F_SETFD, 0);
1380 fcntl (job_fds[1], F_SETFD, 0);
1382 if (job_rfd >= 0)
1383 fcntl (job_rfd, F_SETFD, 0);
1385 #else /* !__EMX__ */
1387 child->pid = vfork ();
1388 environ = parent_environ; /* Restore value child may have clobbered. */
1389 if (child->pid == 0)
1391 /* We are the child side. */
1392 unblock_sigs ();
1394 /* If we aren't running a recursive command and we have a jobserver
1395 pipe, close it before exec'ing. */
1396 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
1398 close (job_fds[0]);
1399 close (job_fds[1]);
1401 if (job_rfd >= 0)
1402 close (job_rfd);
1404 #ifdef SET_STACK_SIZE
1405 /* Reset limits, if necessary. */
1406 if (stack_limit.rlim_cur)
1407 setrlimit (RLIMIT_STACK, &stack_limit);
1408 #endif
1410 child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
1411 argv, child->environment);
1413 else if (child->pid < 0)
1415 /* Fork failed! */
1416 unblock_sigs ();
1417 perror_with_name ("vfork", "");
1418 goto error;
1420 # endif /* !__EMX__ */
1421 #endif /* !VMS */
1424 #else /* __MSDOS__ or Amiga or WINDOWS32 */
1425 #ifdef __MSDOS__
1427 int proc_return;
1429 block_sigs ();
1430 dos_status = 0;
1432 /* We call `system' to do the job of the SHELL, since stock DOS
1433 shell is too dumb. Our `system' knows how to handle long
1434 command lines even if pipes/redirection is needed; it will only
1435 call COMMAND.COM when its internal commands are used. */
1436 if (execute_by_shell)
1438 char *cmdline = argv[0];
1439 /* We don't have a way to pass environment to `system',
1440 so we need to save and restore ours, sigh... */
1441 char **parent_environ = environ;
1443 environ = child->environment;
1445 /* If we have a *real* shell, tell `system' to call
1446 it to do everything for us. */
1447 if (unixy_shell)
1449 /* A *real* shell on MSDOS may not support long
1450 command lines the DJGPP way, so we must use `system'. */
1451 cmdline = argv[2]; /* get past "shell -c" */
1454 dos_command_running = 1;
1455 proc_return = system (cmdline);
1456 environ = parent_environ;
1457 execute_by_shell = 0; /* for the next time */
1459 else
1461 dos_command_running = 1;
1462 proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
1465 /* Need to unblock signals before turning off
1466 dos_command_running, so that child's signals
1467 will be treated as such (see fatal_error_signal). */
1468 unblock_sigs ();
1469 dos_command_running = 0;
1471 /* If the child got a signal, dos_status has its
1472 high 8 bits set, so be careful not to alter them. */
1473 if (proc_return == -1)
1474 dos_status |= 0xff;
1475 else
1476 dos_status |= (proc_return & 0xff);
1477 ++dead_children;
1478 child->pid = dos_pid++;
1480 #endif /* __MSDOS__ */
1481 #ifdef _AMIGA
1482 amiga_status = MyExecute (argv);
1484 ++dead_children;
1485 child->pid = amiga_pid++;
1486 if (amiga_batch_file)
1488 amiga_batch_file = 0;
1489 DeleteFile (amiga_bname); /* Ignore errors. */
1491 #endif /* Amiga */
1492 #ifdef WINDOWS32
1494 HANDLE hPID;
1495 char* arg0;
1497 /* make UNC paths safe for CreateProcess -- backslash format */
1498 arg0 = argv[0];
1499 if (arg0 && arg0[0] == '/' && arg0[1] == '/')
1500 for ( ; arg0 && *arg0; arg0++)
1501 if (*arg0 == '/')
1502 *arg0 = '\\';
1504 /* make sure CreateProcess() has Path it needs */
1505 sync_Path_environment();
1507 hPID = process_easy(argv, child->environment);
1509 if (hPID != INVALID_HANDLE_VALUE)
1510 child->pid = (pid_t) hPID;
1511 else {
1512 int i;
1513 unblock_sigs();
1514 fprintf(stderr,
1515 _("process_easy() failed to launch process (e=%ld)\n"),
1516 process_last_err(hPID));
1517 for (i = 0; argv[i]; i++)
1518 fprintf(stderr, "%s ", argv[i]);
1519 fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
1520 goto error;
1523 #endif /* WINDOWS32 */
1524 #endif /* __MSDOS__ or Amiga or WINDOWS32 */
1526 /* Bump the number of jobs started in this second. */
1527 ++job_counter;
1529 /* We are the parent side. Set the state to
1530 say the commands are running and return. */
1532 set_command_state (child->file, cs_running);
1534 /* Free the storage used by the child's argument list. */
1535 #ifndef VMS
1536 free (argv[0]);
1537 free (argv);
1538 #endif
1540 return;
1542 error:
1543 child->file->update_status = 2;
1544 notice_finished_file (child->file);
1545 return;
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. */
1552 static int
1553 start_waiting_job (struct child *c)
1555 struct file *f = c->file;
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. */
1561 c->remote = start_remote_job_p (1);
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
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;
1580 /* Start the first command; reap_children will run later command lines. */
1581 start_job_command (c);
1583 switch (f->command_state)
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;
1596 case cs_not_started:
1597 /* All the command lines turned out to be empty. */
1598 f->update_status = 0;
1599 /* FALLTHROUGH */
1601 case cs_finished:
1602 notice_finished_file (f);
1603 free_child (c);
1604 break;
1606 default:
1607 assert (f->command_state == cs_finished);
1608 break;
1611 return 1;
1614 /* Create a `struct child' for FILE and start its commands running. */
1616 void
1617 new_job (struct file *file)
1619 struct commands *cmds = file->cmds;
1620 struct child *c;
1621 char **lines;
1622 unsigned int i;
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 ();
1628 /* Reap any children that might have finished recently. */
1629 reap_children (0, 0);
1631 /* Chop the commands up into lines if they aren't already. */
1632 chop_commands (cmds);
1634 /* Expand the command lines and store the results in LINES. */
1635 lines = xmalloc (cmds->ncommand_lines * sizeof (char *));
1636 for (i = 0; i < cmds->ncommand_lines; ++i)
1638 /* Collapse backslash-newline combinations that are inside variable
1639 or function references. These are left alone by the parser so
1640 that they will appear in the echoing of commands (where they look
1641 nice); and collapsed by construct_command_argv when it tokenizes.
1642 But letting them survive inside function invocations loses because
1643 we don't want the functions to see them as part of the text. */
1645 char *in, *out, *ref;
1647 /* IN points to where in the line we are scanning.
1648 OUT points to where in the line we are writing.
1649 When we collapse a backslash-newline combination,
1650 IN gets ahead of OUT. */
1652 in = out = cmds->command_lines[i];
1653 while ((ref = strchr (in, '$')) != 0)
1655 ++ref; /* Move past the $. */
1657 if (out != in)
1658 /* Copy the text between the end of the last chunk
1659 we processed (where IN points) and the new chunk
1660 we are about to process (where REF points). */
1661 memmove (out, in, ref - in);
1663 /* Move both pointers past the boring stuff. */
1664 out += ref - in;
1665 in = ref;
1667 if (*ref == '(' || *ref == '{')
1669 char openparen = *ref;
1670 char closeparen = openparen == '(' ? ')' : '}';
1671 int count;
1672 char *p;
1674 *out++ = *in++; /* Copy OPENPAREN. */
1675 /* IN now points past the opening paren or brace.
1676 Count parens or braces until it is matched. */
1677 count = 0;
1678 while (*in != '\0')
1680 if (*in == closeparen && --count < 0)
1681 break;
1682 else if (*in == '\\' && in[1] == '\n')
1684 /* We have found a backslash-newline inside a
1685 variable or function reference. Eat it and
1686 any following whitespace. */
1688 int quoted = 0;
1689 for (p = in - 1; p > ref && *p == '\\'; --p)
1690 quoted = !quoted;
1692 if (quoted)
1693 /* There were two or more backslashes, so this is
1694 not really a continuation line. We don't collapse
1695 the quoting backslashes here as is done in
1696 collapse_continuations, because the line will
1697 be collapsed again after expansion. */
1698 *out++ = *in++;
1699 else
1701 /* Skip the backslash, newline and
1702 any following whitespace. */
1703 in = next_token (in + 2);
1705 /* Discard any preceding whitespace that has
1706 already been written to the output. */
1707 while (out > ref
1708 && isblank ((unsigned char)out[-1]))
1709 --out;
1711 /* Replace it all with a single space. */
1712 *out++ = ' ';
1715 else
1717 if (*in == openparen)
1718 ++count;
1720 *out++ = *in++;
1726 /* There are no more references in this line to worry about.
1727 Copy the remaining uninteresting text to the output. */
1728 if (out != in)
1729 memmove (out, in, strlen (in) + 1);
1731 /* Finally, expand the line. */
1732 lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
1733 file);
1736 /* Start the command sequence, record it in a new
1737 `struct child', and add that to the chain. */
1739 c = xcalloc (sizeof (struct child));
1740 c->file = file;
1741 c->command_lines = lines;
1742 c->sh_batch_file = NULL;
1744 /* Cache dontcare flag because file->dontcare can be changed once we
1745 return. Check dontcare inheritance mechanism for details. */
1746 c->dontcare = file->dontcare;
1748 /* Fetch the first command line to be run. */
1749 job_next_command (c);
1751 /* Wait for a job slot to be freed up. If we allow an infinite number
1752 don't bother; also job_slots will == 0 if we're using the jobserver. */
1754 if (job_slots != 0)
1755 while (job_slots_used == job_slots)
1756 reap_children (1, 0);
1758 #ifdef MAKE_JOBSERVER
1759 /* If we are controlling multiple jobs make sure we have a token before
1760 starting the child. */
1762 /* This can be inefficient. There's a decent chance that this job won't
1763 actually have to run any subprocesses: the command script may be empty
1764 or otherwise optimized away. It would be nice if we could defer
1765 obtaining a token until just before we need it, in start_job_command.
1766 To do that we'd need to keep track of whether we'd already obtained a
1767 token (since start_job_command is called for each line of the job, not
1768 just once). Also more thought needs to go into the entire algorithm;
1769 this is where the old parallel job code waits, so... */
1771 #ifdef WINDOWS32
1772 else if (has_jobserver_semaphore())
1773 #else
1774 else if (job_fds[0] >= 0)
1775 #endif
1776 while (1)
1778 int got_token;
1779 #ifndef WINDOWS32
1780 char token;
1781 int saved_errno;
1782 #endif
1784 DB (DB_JOBS, ("Need a job token; we %shave children\n",
1785 children ? "" : "don't "));
1787 /* If we don't already have a job started, use our "free" token. */
1788 if (!jobserver_tokens)
1789 break;
1791 #ifndef WINDOWS32
1792 /* Read a token. As long as there's no token available we'll block.
1793 We enable interruptible system calls before the read(2) so that if
1794 we get a SIGCHLD while we're waiting, we'll return with EINTR and
1795 we can process the death(s) and return tokens to the free pool.
1797 Once we return from the read, we immediately reinstate restartable
1798 system calls. This allows us to not worry about checking for
1799 EINTR on all the other system calls in the program.
1801 There is one other twist: there is a span between the time
1802 reap_children() does its last check for dead children and the time
1803 the read(2) call is entered, below, where if a child dies we won't
1804 notice. This is extremely serious as it could cause us to
1805 deadlock, given the right set of events.
1807 To avoid this, we do the following: before we reap_children(), we
1808 dup(2) the read FD on the jobserver pipe. The read(2) call below
1809 uses that new FD. In the signal handler, we close that FD. That
1810 way, if a child dies during the section mentioned above, the
1811 read(2) will be invoked with an invalid FD and will return
1812 immediately with EBADF. */
1814 /* Make sure we have a dup'd FD. */
1815 if (job_rfd < 0)
1817 DB (DB_JOBS, ("Duplicate the job FD\n"));
1818 job_rfd = dup (job_fds[0]);
1820 #endif
1822 /* Reap anything that's currently waiting. */
1823 reap_children (0, 0);
1825 /* Kick off any jobs we have waiting for an opportunity that
1826 can run now (ie waiting for load). */
1827 start_waiting_jobs ();
1829 /* If our "free" slot has become available, use it; we don't need an
1830 actual token. */
1831 if (!jobserver_tokens)
1832 break;
1834 /* There must be at least one child already, or we have no business
1835 waiting for a token. */
1836 if (!children)
1837 fatal (NILF, "INTERNAL: no children as we go to sleep on read\n");
1839 #ifdef WINDOWS32
1840 /* On Windows we simply wait for the jobserver semaphore to become
1841 * signalled or one of our child processes to terminate.
1843 got_token = wait_for_semaphore_or_child_process();
1844 if (got_token < 0)
1846 DWORD err = GetLastError();
1847 fatal (NILF, _("semaphore or child process wait: (Error %ld: %s)"),
1848 err, map_windows32_error_to_string(err));
1850 #else
1851 /* Set interruptible system calls, and read() for a job token. */
1852 set_child_handler_action_flags (1, waiting_jobs != NULL);
1853 got_token = read (job_rfd, &token, 1);
1854 saved_errno = errno;
1855 set_child_handler_action_flags (0, waiting_jobs != NULL);
1856 #endif
1858 /* If we got one, we're done here. */
1859 if (got_token == 1)
1861 DB (DB_JOBS, (_("Obtained token for child %p (%s).\n"),
1862 c, c->file->name));
1863 break;
1866 #ifndef WINDOWS32
1867 /* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,
1868 go back and reap_children(), and try again. */
1869 errno = saved_errno;
1870 if (errno != EINTR && errno != EBADF)
1871 pfatal_with_name (_("read jobs pipe"));
1872 if (errno == EBADF)
1873 DB (DB_JOBS, ("Read returned EBADF.\n"));
1874 #endif
1876 #endif
1878 ++jobserver_tokens;
1880 /* Trace the build.
1881 Use message here so that changes to working directories are logged. */
1882 if (trace_flag)
1884 char *newer = allocated_variable_expand_for_file ("$?", c->file);
1885 char *nm;
1887 if (! cmds->fileinfo.filenm)
1888 nm = _("<builtin>");
1889 else
1891 nm = alloca (strlen (cmds->fileinfo.filenm) + 1 + 11 + 1);
1892 sprintf (nm, "%s:%lu", cmds->fileinfo.filenm, cmds->fileinfo.lineno);
1895 if (newer[0] == '\0')
1896 message (0, _("%s: target `%s' does not exist"), nm, c->file->name);
1897 else
1898 message (0, _("%s: update target `%s' due to: %s"), nm,
1899 c->file->name, newer);
1901 free (newer);
1905 /* The job is now primed. Start it running.
1906 (This will notice if there is in fact no recipe.) */
1907 start_waiting_job (c);
1909 if (job_slots == 1 || not_parallel)
1910 /* Since there is only one job slot, make things run linearly.
1911 Wait for the child to die, setting the state to `cs_finished'. */
1912 while (file->command_state == cs_running)
1913 reap_children (1, 0);
1915 return;
1918 /* Move CHILD's pointers to the next command for it to execute.
1919 Returns nonzero if there is another command. */
1921 static int
1922 job_next_command (struct child *child)
1924 while (child->command_ptr == 0 || *child->command_ptr == '\0')
1926 /* There are no more lines in the expansion of this line. */
1927 if (child->command_line == child->file->cmds->ncommand_lines)
1929 /* There are no more lines to be expanded. */
1930 child->command_ptr = 0;
1931 return 0;
1933 else
1934 /* Get the next line to run. */
1935 child->command_ptr = child->command_lines[child->command_line++];
1937 return 1;
1940 /* Determine if the load average on the system is too high to start a new job.
1941 The real system load average is only recomputed once a second. However, a
1942 very parallel make can easily start tens or even hundreds of jobs in a
1943 second, which brings the system to its knees for a while until that first
1944 batch of jobs clears out.
1946 To avoid this we use a weighted algorithm to try to account for jobs which
1947 have been started since the last second, and guess what the load average
1948 would be now if it were computed.
1950 This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
1951 who writes:
1953 ! calculate something load-oid and add to the observed sys.load,
1954 ! so that latter can catch up:
1955 ! - every job started increases jobctr;
1956 ! - every dying job decreases a positive jobctr;
1957 ! - the jobctr value gets zeroed every change of seconds,
1958 ! after its value*weight_b is stored into the 'backlog' value last_sec
1959 ! - weight_a times the sum of jobctr and last_sec gets
1960 ! added to the observed sys.load.
1962 ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
1963 ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
1964 ! sub-shelled commands (rm, echo, sed...) for tests.
1965 ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
1966 ! resulted in significant excession of the load limit, raising it
1967 ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
1968 ! reach the limit in most test cases.
1970 ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
1971 ! exceeding the limit for longer-running stuff (compile jobs in
1972 ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
1973 ! small jobs' effects.
1977 #define LOAD_WEIGHT_A 0.25
1978 #define LOAD_WEIGHT_B 0.25
1980 static int
1981 load_too_high (void)
1983 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__)
1984 return 1;
1985 #else
1986 static double last_sec;
1987 static time_t last_now;
1988 double load, guess;
1989 time_t now;
1991 #ifdef WINDOWS32
1992 /* sub_proc.c cannot wait for more than MAXIMUM_WAIT_OBJECTS children */
1993 if (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
1994 return 1;
1995 #endif
1997 if (max_load_average < 0)
1998 return 0;
2000 /* Find the real system load average. */
2001 make_access ();
2002 if (getloadavg (&load, 1) != 1)
2004 static int lossage = -1;
2005 /* Complain only once for the same error. */
2006 if (lossage == -1 || errno != lossage)
2008 if (errno == 0)
2009 /* An errno value of zero means getloadavg is just unsupported. */
2010 error (NILF,
2011 _("cannot enforce load limits on this operating system"));
2012 else
2013 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
2015 lossage = errno;
2016 load = 0;
2018 user_access ();
2020 /* If we're in a new second zero the counter and correct the backlog
2021 value. Only keep the backlog for one extra second; after that it's 0. */
2022 now = time (NULL);
2023 if (last_now < now)
2025 if (last_now == now - 1)
2026 last_sec = LOAD_WEIGHT_B * job_counter;
2027 else
2028 last_sec = 0.0;
2030 job_counter = 0;
2031 last_now = now;
2034 /* Try to guess what the load would be right now. */
2035 guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
2037 DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
2038 guess, load, max_load_average));
2040 return guess >= max_load_average;
2041 #endif
2044 /* Start jobs that are waiting for the load to be lower. */
2046 void
2047 start_waiting_jobs (void)
2049 struct child *job;
2051 if (waiting_jobs == 0)
2052 return;
2056 /* Check for recently deceased descendants. */
2057 reap_children (0, 0);
2059 /* Take a job off the waiting list. */
2060 job = waiting_jobs;
2061 waiting_jobs = job->next;
2063 /* Try to start that job. We break out of the loop as soon
2064 as start_waiting_job puts one back on the waiting list. */
2066 while (start_waiting_job (job) && waiting_jobs != 0);
2068 return;
2071 #ifndef WINDOWS32
2073 /* EMX: Start a child process. This function returns the new pid. */
2074 # if defined __EMX__
2076 child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
2078 int pid;
2079 /* stdin_fd == 0 means: nothing to do for stdin;
2080 stdout_fd == 1 means: nothing to do for stdout */
2081 int save_stdin = (stdin_fd != 0) ? dup (0) : 0;
2082 int save_stdout = (stdout_fd != 1) ? dup (1): 1;
2084 /* < 0 only if dup() failed */
2085 if (save_stdin < 0)
2086 fatal (NILF, _("no more file handles: could not duplicate stdin\n"));
2087 if (save_stdout < 0)
2088 fatal (NILF, _("no more file handles: could not duplicate stdout\n"));
2090 /* Close unnecessary file handles for the child. */
2091 if (save_stdin != 0)
2092 CLOSE_ON_EXEC (save_stdin);
2093 if (save_stdout != 1)
2094 CLOSE_ON_EXEC (save_stdout);
2096 /* Connect the pipes to the child process. */
2097 if (stdin_fd != 0)
2098 (void) dup2 (stdin_fd, 0);
2099 if (stdout_fd != 1)
2100 (void) dup2 (stdout_fd, 1);
2102 /* stdin_fd and stdout_fd must be closed on exit because we are
2103 still in the parent process */
2104 if (stdin_fd != 0)
2105 CLOSE_ON_EXEC (stdin_fd);
2106 if (stdout_fd != 1)
2107 CLOSE_ON_EXEC (stdout_fd);
2109 /* Run the command. */
2110 pid = exec_command (argv, envp);
2112 /* Restore stdout/stdin of the parent and close temporary FDs. */
2113 if (stdin_fd != 0)
2115 if (dup2 (save_stdin, 0) != 0)
2116 fatal (NILF, _("Could not restore stdin\n"));
2117 else
2118 close (save_stdin);
2121 if (stdout_fd != 1)
2123 if (dup2 (save_stdout, 1) != 1)
2124 fatal (NILF, _("Could not restore stdout\n"));
2125 else
2126 close (save_stdout);
2129 return pid;
2132 #elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS)
2134 /* UNIX:
2135 Replace the current process with one executing the command in ARGV.
2136 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
2137 the environment of the new program. This function does not return. */
2138 void
2139 child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
2141 if (stdin_fd != 0)
2142 (void) dup2 (stdin_fd, 0);
2143 if (stdout_fd != 1)
2144 (void) dup2 (stdout_fd, 1);
2145 if (stdin_fd != 0)
2146 (void) close (stdin_fd);
2147 if (stdout_fd != 1)
2148 (void) close (stdout_fd);
2150 /* Run the command. */
2151 exec_command (argv, envp);
2153 #endif /* !AMIGA && !__MSDOS__ && !VMS */
2154 #endif /* !WINDOWS32 */
2156 #ifndef _AMIGA
2157 /* Replace the current process with one running the command in ARGV,
2158 with environment ENVP. This function does not return. */
2160 /* EMX: This function returns the pid of the child process. */
2161 # ifdef __EMX__
2163 # else
2164 void
2165 # endif
2166 exec_command (char **argv, char **envp)
2168 #ifdef VMS
2169 /* to work around a problem with signals and execve: ignore them */
2170 #ifdef SIGCHLD
2171 signal (SIGCHLD,SIG_IGN);
2172 #endif
2173 /* Run the program. */
2174 execve (argv[0], argv, envp);
2175 perror_with_name ("execve: ", argv[0]);
2176 _exit (EXIT_FAILURE);
2177 #else
2178 #ifdef WINDOWS32
2179 HANDLE hPID;
2180 HANDLE hWaitPID;
2181 int err = 0;
2182 int exit_code = EXIT_FAILURE;
2184 /* make sure CreateProcess() has Path it needs */
2185 sync_Path_environment();
2187 /* launch command */
2188 hPID = process_easy(argv, envp);
2190 /* make sure launch ok */
2191 if (hPID == INVALID_HANDLE_VALUE)
2193 int i;
2194 fprintf(stderr,
2195 _("process_easy() failed to launch process (e=%ld)\n"),
2196 process_last_err(hPID));
2197 for (i = 0; argv[i]; i++)
2198 fprintf(stderr, "%s ", argv[i]);
2199 fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
2200 exit(EXIT_FAILURE);
2203 /* wait and reap last child */
2204 hWaitPID = process_wait_for_any(1, 0);
2205 while (hWaitPID)
2207 /* was an error found on this process? */
2208 err = process_last_err(hWaitPID);
2210 /* get exit data */
2211 exit_code = process_exit_code(hWaitPID);
2213 if (err)
2214 fprintf(stderr, "make (e=%d, rc=%d): %s",
2215 err, exit_code, map_windows32_error_to_string(err));
2217 /* cleanup process */
2218 process_cleanup(hWaitPID);
2220 /* expect to find only last pid, warn about other pids reaped */
2221 if (hWaitPID == hPID)
2222 break;
2223 else
2225 char *pidstr = xstrdup (pid2str ((pid_t)hWaitPID));
2227 fprintf(stderr,
2228 _("make reaped child pid %s, still waiting for pid %s\n"),
2229 pidstr, pid2str ((pid_t)hPID));
2230 free (pidstr);
2234 /* return child's exit code as our exit code */
2235 exit(exit_code);
2237 #else /* !WINDOWS32 */
2239 # ifdef __EMX__
2240 int pid;
2241 # endif
2243 /* Be the user, permanently. */
2244 child_access ();
2246 # ifdef __EMX__
2248 /* Run the program. */
2249 pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
2251 if (pid >= 0)
2252 return pid;
2254 /* the file might have a strange shell extension */
2255 if (errno == ENOENT)
2256 errno = ENOEXEC;
2258 # else
2260 /* Run the program. */
2261 environ = envp;
2262 execvp (argv[0], argv);
2264 # endif /* !__EMX__ */
2266 switch (errno)
2268 case ENOENT:
2269 error (NILF, _("%s: Command not found"), argv[0]);
2270 break;
2271 case ENOEXEC:
2273 /* The file is not executable. Try it as a shell script. */
2274 extern char *getenv ();
2275 char *shell;
2276 char **new_argv;
2277 int argc;
2278 int i=1;
2280 # ifdef __EMX__
2281 /* Do not use $SHELL from the environment */
2282 struct variable *p = lookup_variable ("SHELL", 5);
2283 if (p)
2284 shell = p->value;
2285 else
2286 shell = 0;
2287 # else
2288 shell = getenv ("SHELL");
2289 # endif
2290 if (shell == 0)
2291 shell = default_shell;
2293 argc = 1;
2294 while (argv[argc] != 0)
2295 ++argc;
2297 # ifdef __EMX__
2298 if (!unixy_shell)
2299 ++argc;
2300 # endif
2302 new_argv = alloca ((1 + argc + 1) * sizeof (char *));
2303 new_argv[0] = shell;
2305 # ifdef __EMX__
2306 if (!unixy_shell)
2308 new_argv[1] = "/c";
2309 ++i;
2310 --argc;
2312 # endif
2314 new_argv[i] = argv[0];
2315 while (argc > 0)
2317 new_argv[i + argc] = argv[argc];
2318 --argc;
2321 # ifdef __EMX__
2322 pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
2323 if (pid >= 0)
2324 break;
2325 # else
2326 execvp (shell, new_argv);
2327 # endif
2328 if (errno == ENOENT)
2329 error (NILF, _("%s: Shell program not found"), shell);
2330 else
2331 perror_with_name ("execvp: ", shell);
2332 break;
2335 # ifdef __EMX__
2336 case EINVAL:
2337 /* this nasty error was driving me nuts :-( */
2338 error (NILF, _("spawnvpe: environment space might be exhausted"));
2339 /* FALLTHROUGH */
2340 # endif
2342 default:
2343 perror_with_name ("execvp: ", argv[0]);
2344 break;
2347 # ifdef __EMX__
2348 return pid;
2349 # else
2350 _exit (127);
2351 # endif
2352 #endif /* !WINDOWS32 */
2353 #endif /* !VMS */
2355 #else /* On Amiga */
2356 void exec_command (char **argv)
2358 MyExecute (argv);
2361 void clean_tmp (void)
2363 DeleteFile (amiga_bname);
2366 #endif /* On Amiga */
2368 #ifndef VMS
2369 /* Figure out the argument list necessary to run LINE as a command. Try to
2370 avoid using a shell. This routine handles only ' quoting, and " quoting
2371 when no backslash, $ or ` characters are seen in the quotes. Starting
2372 quotes may be escaped with a backslash. If any of the characters in
2373 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2374 is the first word of a line, the shell is used.
2376 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2377 If *RESTP is NULL, newlines will be ignored.
2379 SHELL is the shell to use, or nil to use the default shell.
2380 IFS is the value of $IFS, or nil (meaning the default).
2382 FLAGS is the value of lines_flags for this command line. It is
2383 used in the WINDOWS32 port to check whether + or $(MAKE) were found
2384 in this command line, in which case the effect of just_print_flag
2385 is overridden. */
2387 static char **
2388 construct_command_argv_internal (char *line, char **restp, char *shell,
2389 char *shellflags, char *ifs, int flags,
2390 char **batch_filename UNUSED)
2392 #ifdef __MSDOS__
2393 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
2394 We call `system' for anything that requires ``slow'' processing,
2395 because DOS shells are too dumb. When $SHELL points to a real
2396 (unix-style) shell, `system' just calls it to do everything. When
2397 $SHELL points to a DOS shell, `system' does most of the work
2398 internally, calling the shell only for its internal commands.
2399 However, it looks on the $PATH first, so you can e.g. have an
2400 external command named `mkdir'.
2402 Since we call `system', certain characters and commands below are
2403 actually not specific to COMMAND.COM, but to the DJGPP implementation
2404 of `system'. In particular:
2406 The shell wildcard characters are in DOS_CHARS because they will
2407 not be expanded if we call the child via `spawnXX'.
2409 The `;' is in DOS_CHARS, because our `system' knows how to run
2410 multiple commands on a single line.
2412 DOS_CHARS also include characters special to 4DOS/NDOS, so we
2413 won't have to tell one from another and have one more set of
2414 commands and special characters. */
2415 static char sh_chars_dos[] = "*?[];|<>%^&()";
2416 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2417 "copy", "ctty", "date", "del", "dir", "echo",
2418 "erase", "exit", "for", "goto", "if", "md",
2419 "mkdir", "path", "pause", "prompt", "rd",
2420 "rmdir", "rem", "ren", "rename", "set",
2421 "shift", "time", "type", "ver", "verify",
2422 "vol", ":", 0 };
2424 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
2425 static char *sh_cmds_sh[] = { "cd", "echo", "eval", "exec", "exit", "login",
2426 "logout", "set", "umask", "wait", "while",
2427 "for", "case", "if", ":", ".", "break",
2428 "continue", "export", "read", "readonly",
2429 "shift", "times", "trap", "switch", "unset",
2430 "ulimit", 0 };
2432 char *sh_chars;
2433 char **sh_cmds;
2434 #elif defined (__EMX__)
2435 static char sh_chars_dos[] = "*?[];|<>%^&()";
2436 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2437 "copy", "ctty", "date", "del", "dir", "echo",
2438 "erase", "exit", "for", "goto", "if", "md",
2439 "mkdir", "path", "pause", "prompt", "rd",
2440 "rmdir", "rem", "ren", "rename", "set",
2441 "shift", "time", "type", "ver", "verify",
2442 "vol", ":", 0 };
2444 static char sh_chars_os2[] = "*?[];|<>%^()\"'&";
2445 static char *sh_cmds_os2[] = { "call", "cd", "chcp", "chdir", "cls", "copy",
2446 "date", "del", "detach", "dir", "echo",
2447 "endlocal", "erase", "exit", "for", "goto", "if",
2448 "keys", "md", "mkdir", "move", "path", "pause",
2449 "prompt", "rd", "rem", "ren", "rename", "rmdir",
2450 "set", "setlocal", "shift", "start", "time",
2451 "type", "ver", "verify", "vol", ":", 0 };
2453 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~'";
2454 static char *sh_cmds_sh[] = { "echo", "cd", "eval", "exec", "exit", "login",
2455 "logout", "set", "umask", "wait", "while",
2456 "for", "case", "if", ":", ".", "break",
2457 "continue", "export", "read", "readonly",
2458 "shift", "times", "trap", "switch", "unset",
2459 0 };
2460 char *sh_chars;
2461 char **sh_cmds;
2463 #elif defined (_AMIGA)
2464 static char sh_chars[] = "#;\"|<>()?*$`";
2465 static char *sh_cmds[] = { "cd", "eval", "if", "delete", "echo", "copy",
2466 "rename", "set", "setenv", "date", "makedir",
2467 "skip", "else", "endif", "path", "prompt",
2468 "unset", "unsetenv", "version",
2469 0 };
2470 #elif defined (WINDOWS32)
2471 static char sh_chars_dos[] = "\"|&<>";
2472 static char *sh_cmds_dos[] = { "assoc", "break", "call", "cd", "chcp",
2473 "chdir", "cls", "color", "copy", "ctty",
2474 "date", "del", "dir", "echo", "echo.",
2475 "endlocal", "erase", "exit", "for", "ftype",
2476 "goto", "if", "if", "md", "mkdir", "path",
2477 "pause", "prompt", "rd", "rem", "ren",
2478 "rename", "rmdir", "set", "setlocal",
2479 "shift", "time", "title", "type", "ver",
2480 "verify", "vol", ":", 0 };
2481 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
2482 static char *sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login",
2483 "logout", "set", "umask", "wait", "while", "for",
2484 "case", "if", ":", ".", "break", "continue",
2485 "export", "read", "readonly", "shift", "times",
2486 "trap", "switch", "test",
2487 #ifdef BATCH_MODE_ONLY_SHELL
2488 "echo",
2489 #endif
2490 0 };
2491 char* sh_chars;
2492 char** sh_cmds;
2493 #elif defined(__riscos__)
2494 static char sh_chars[] = "";
2495 static char *sh_cmds[] = { 0 };
2496 #else /* must be UNIX-ish */
2497 static char sh_chars[] = "#;\"*?[]&|<>(){}$`^~!";
2498 static char *sh_cmds[] = { ".", ":", "break", "case", "cd", "continue",
2499 "eval", "exec", "exit", "export", "for", "if",
2500 "login", "logout", "read", "readonly", "set",
2501 "shift", "switch", "test", "times", "trap",
2502 "ulimit", "umask", "unset", "wait", "while", 0 };
2503 # ifdef HAVE_DOS_PATHS
2504 /* This is required if the MSYS/Cygwin ports (which do not define
2505 WINDOWS32) are compiled with HAVE_DOS_PATHS defined, which uses
2506 sh_chars_sh[] directly (see below). */
2507 static char *sh_chars_sh = sh_chars;
2508 # endif /* HAVE_DOS_PATHS */
2509 #endif
2510 int i;
2511 char *p;
2512 char *ap;
2513 char *end;
2514 int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
2515 char **new_argv = 0;
2516 char *argstr = 0;
2517 #ifdef WINDOWS32
2518 int slow_flag = 0;
2520 if (!unixy_shell) {
2521 sh_cmds = sh_cmds_dos;
2522 sh_chars = sh_chars_dos;
2523 } else {
2524 sh_cmds = sh_cmds_sh;
2525 sh_chars = sh_chars_sh;
2527 #endif /* WINDOWS32 */
2529 if (restp != NULL)
2530 *restp = NULL;
2532 /* Make sure not to bother processing an empty line. */
2533 while (isblank ((unsigned char)*line))
2534 ++line;
2535 if (*line == '\0')
2536 return 0;
2538 if (shellflags == 0)
2539 shellflags = posix_pedantic ? "-ec" : "-c";
2541 /* See if it is safe to parse commands internally. */
2542 if (shell == 0)
2543 shell = default_shell;
2544 #ifdef WINDOWS32
2545 else if (strcmp (shell, default_shell))
2547 char *s1 = _fullpath (NULL, shell, 0);
2548 char *s2 = _fullpath (NULL, default_shell, 0);
2550 slow_flag = strcmp ((s1 ? s1 : ""), (s2 ? s2 : ""));
2552 if (s1)
2553 free (s1);
2554 if (s2)
2555 free (s2);
2557 if (slow_flag)
2558 goto slow;
2559 #else /* not WINDOWS32 */
2560 #if defined (__MSDOS__) || defined (__EMX__)
2561 else if (strcasecmp (shell, default_shell))
2563 extern int _is_unixy_shell (const char *_path);
2565 DB (DB_BASIC, (_("$SHELL changed (was `%s', now `%s')\n"),
2566 default_shell, shell));
2567 unixy_shell = _is_unixy_shell (shell);
2568 /* we must allocate a copy of shell: construct_command_argv() will free
2569 * shell after this function returns. */
2570 default_shell = xstrdup (shell);
2572 if (unixy_shell)
2574 sh_chars = sh_chars_sh;
2575 sh_cmds = sh_cmds_sh;
2577 else
2579 sh_chars = sh_chars_dos;
2580 sh_cmds = sh_cmds_dos;
2581 # ifdef __EMX__
2582 if (_osmode == OS2_MODE)
2584 sh_chars = sh_chars_os2;
2585 sh_cmds = sh_cmds_os2;
2587 # endif
2589 #else /* !__MSDOS__ */
2590 else if (strcmp (shell, default_shell))
2591 goto slow;
2592 #endif /* !__MSDOS__ && !__EMX__ */
2593 #endif /* not WINDOWS32 */
2595 if (ifs != 0)
2596 for (ap = ifs; *ap != '\0'; ++ap)
2597 if (*ap != ' ' && *ap != '\t' && *ap != '\n')
2598 goto slow;
2600 if (shellflags != 0)
2601 if (shellflags[0] != '-'
2602 || ((shellflags[1] != 'c' || shellflags[2] != '\0')
2603 && (shellflags[1] != 'e' || shellflags[2] != 'c' || shellflags[3] != '\0')))
2604 goto slow;
2606 i = strlen (line) + 1;
2608 /* More than 1 arg per character is impossible. */
2609 new_argv = xmalloc (i * sizeof (char *));
2611 /* All the args can fit in a buffer as big as LINE is. */
2612 ap = new_argv[0] = argstr = xmalloc (i);
2613 end = ap + i;
2615 /* I is how many complete arguments have been found. */
2616 i = 0;
2617 instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
2618 for (p = line; *p != '\0'; ++p)
2620 assert (ap <= end);
2622 if (instring)
2624 /* Inside a string, just copy any char except a closing quote
2625 or a backslash-newline combination. */
2626 if (*p == instring)
2628 instring = 0;
2629 if (ap == new_argv[0] || *(ap-1) == '\0')
2630 last_argument_was_empty = 1;
2632 else if (*p == '\\' && p[1] == '\n')
2634 /* Backslash-newline is handled differently depending on what
2635 kind of string we're in: inside single-quoted strings you
2636 keep them; in double-quoted strings they disappear. For
2637 DOS/Windows/OS2, if we don't have a POSIX shell, we keep the
2638 pre-POSIX behavior of removing the backslash-newline. */
2639 if (instring == '"'
2640 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2641 || !unixy_shell
2642 #endif
2644 ++p;
2645 else
2647 *(ap++) = *(p++);
2648 *(ap++) = *p;
2651 else if (*p == '\n' && restp != NULL)
2653 /* End of the command line. */
2654 *restp = p;
2655 goto end_of_line;
2657 /* Backslash, $, and ` are special inside double quotes.
2658 If we see any of those, punt.
2659 But on MSDOS, if we use COMMAND.COM, double and single
2660 quotes have the same effect. */
2661 else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
2662 goto slow;
2663 else
2664 *ap++ = *p;
2666 else if (strchr (sh_chars, *p) != 0)
2667 /* Not inside a string, but it's a special char. */
2668 goto slow;
2669 else if (one_shell && *p == '\n')
2670 /* In .ONESHELL mode \n is a separator like ; or && */
2671 goto slow;
2672 #ifdef __MSDOS__
2673 else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
2674 /* `...' is a wildcard in DJGPP. */
2675 goto slow;
2676 #endif
2677 else
2678 /* Not a special char. */
2679 switch (*p)
2681 case '=':
2682 /* Equals is a special character in leading words before the
2683 first word with no equals sign in it. This is not the case
2684 with sh -k, but we never get here when using nonstandard
2685 shell flags. */
2686 if (! seen_nonequals && unixy_shell)
2687 goto slow;
2688 word_has_equals = 1;
2689 *ap++ = '=';
2690 break;
2692 case '\\':
2693 /* Backslash-newline has special case handling, ref POSIX.
2694 We're in the fastpath, so emulate what the shell would do. */
2695 if (p[1] == '\n')
2697 /* Throw out the backslash and newline. */
2698 ++p;
2700 /* If there's nothing in this argument yet, skip any
2701 whitespace before the start of the next word. */
2702 if (ap == new_argv[i])
2703 p = next_token (p + 1) - 1;
2705 else if (p[1] != '\0')
2707 #ifdef HAVE_DOS_PATHS
2708 /* Only remove backslashes before characters special to Unixy
2709 shells. All other backslashes are copied verbatim, since
2710 they are probably DOS-style directory separators. This
2711 still leaves a small window for problems, but at least it
2712 should work for the vast majority of naive users. */
2714 #ifdef __MSDOS__
2715 /* A dot is only special as part of the "..."
2716 wildcard. */
2717 if (strneq (p + 1, ".\\.\\.", 5))
2719 *ap++ = '.';
2720 *ap++ = '.';
2721 p += 4;
2723 else
2724 #endif
2725 if (p[1] != '\\' && p[1] != '\''
2726 && !isspace ((unsigned char)p[1])
2727 && strchr (sh_chars_sh, p[1]) == 0)
2728 /* back up one notch, to copy the backslash */
2729 --p;
2730 #endif /* HAVE_DOS_PATHS */
2732 /* Copy and skip the following char. */
2733 *ap++ = *++p;
2735 break;
2737 case '\'':
2738 case '"':
2739 instring = *p;
2740 break;
2742 case '\n':
2743 if (restp != NULL)
2745 /* End of the command line. */
2746 *restp = p;
2747 goto end_of_line;
2749 else
2750 /* Newlines are not special. */
2751 *ap++ = '\n';
2752 break;
2754 case ' ':
2755 case '\t':
2756 /* We have the end of an argument.
2757 Terminate the text of the argument. */
2758 *ap++ = '\0';
2759 new_argv[++i] = ap;
2760 last_argument_was_empty = 0;
2762 /* Update SEEN_NONEQUALS, which tells us if every word
2763 heretofore has contained an `='. */
2764 seen_nonequals |= ! word_has_equals;
2765 if (word_has_equals && ! seen_nonequals)
2766 /* An `=' in a word before the first
2767 word without one is magical. */
2768 goto slow;
2769 word_has_equals = 0; /* Prepare for the next word. */
2771 /* If this argument is the command name,
2772 see if it is a built-in shell command.
2773 If so, have the shell handle it. */
2774 if (i == 1)
2776 register int j;
2777 for (j = 0; sh_cmds[j] != 0; ++j)
2779 if (streq (sh_cmds[j], new_argv[0]))
2780 goto slow;
2781 # ifdef __EMX__
2782 /* Non-Unix shells are case insensitive. */
2783 if (!unixy_shell
2784 && strcasecmp (sh_cmds[j], new_argv[0]) == 0)
2785 goto slow;
2786 # endif
2790 /* Ignore multiple whitespace chars. */
2791 p = next_token (p) - 1;
2792 break;
2794 default:
2795 *ap++ = *p;
2796 break;
2799 end_of_line:
2801 if (instring)
2802 /* Let the shell deal with an unterminated quote. */
2803 goto slow;
2805 /* Terminate the last argument and the argument list. */
2807 *ap = '\0';
2808 if (new_argv[i][0] != '\0' || last_argument_was_empty)
2809 ++i;
2810 new_argv[i] = 0;
2812 if (i == 1)
2814 register int j;
2815 for (j = 0; sh_cmds[j] != 0; ++j)
2816 if (streq (sh_cmds[j], new_argv[0]))
2817 goto slow;
2820 if (new_argv[0] == 0)
2822 /* Line was empty. */
2823 free (argstr);
2824 free (new_argv);
2825 return 0;
2828 return new_argv;
2830 slow:;
2831 /* We must use the shell. */
2833 if (new_argv != 0)
2835 /* Free the old argument list we were working on. */
2836 free (argstr);
2837 free (new_argv);
2840 #ifdef __MSDOS__
2841 execute_by_shell = 1; /* actually, call `system' if shell isn't unixy */
2842 #endif
2844 #ifdef _AMIGA
2846 char *ptr;
2847 char *buffer;
2848 char *dptr;
2850 buffer = xmalloc (strlen (line)+1);
2852 ptr = line;
2853 for (dptr=buffer; *ptr; )
2855 if (*ptr == '\\' && ptr[1] == '\n')
2856 ptr += 2;
2857 else if (*ptr == '@') /* Kludge: multiline commands */
2859 ptr += 2;
2860 *dptr++ = '\n';
2862 else
2863 *dptr++ = *ptr++;
2865 *dptr = 0;
2867 new_argv = xmalloc (2 * sizeof (char *));
2868 new_argv[0] = buffer;
2869 new_argv[1] = 0;
2871 #else /* Not Amiga */
2872 #ifdef WINDOWS32
2874 * Not eating this whitespace caused things like
2876 * sh -c "\n"
2878 * which gave the shell fits. I think we have to eat
2879 * whitespace here, but this code should be considered
2880 * suspicious if things start failing....
2883 /* Make sure not to bother processing an empty line. */
2884 while (isspace ((unsigned char)*line))
2885 ++line;
2886 if (*line == '\0')
2887 return 0;
2888 #endif /* WINDOWS32 */
2891 /* SHELL may be a multi-word command. Construct a command line
2892 "$(SHELL) $(.SHELLFLAGS) LINE", with all special chars in LINE escaped.
2893 Then recurse, expanding this command line to get the final
2894 argument list. */
2896 unsigned int shell_len = strlen (shell);
2897 unsigned int line_len = strlen (line);
2898 unsigned int sflags_len = shellflags ? strlen (shellflags) : 0;
2899 char *command_ptr = NULL; /* used for batch_mode_shell mode */
2900 char *new_line;
2902 # ifdef __EMX__ /* is this necessary? */
2903 if (!unixy_shell && shellflags)
2904 shellflags[0] = '/'; /* "/c" */
2905 # endif
2907 /* In .ONESHELL mode we are allowed to throw the entire current
2908 recipe string at a single shell and trust that the user
2909 has configured the shell and shell flags, and formatted
2910 the string, appropriately. */
2911 if (one_shell)
2913 /* If the shell is Bourne compatible, we must remove and ignore
2914 interior special chars [@+-] because they're meaningless to
2915 the shell itself. If, however, we're in .ONESHELL mode and
2916 have changed SHELL to something non-standard, we should
2917 leave those alone because they could be part of the
2918 script. In this case we must also leave in place
2919 any leading [@+-] for the same reason. */
2921 /* Remove and ignore interior prefix chars [@+-] because they're
2922 meaningless given a single shell. */
2923 #if defined __MSDOS__ || defined (__EMX__)
2924 if (unixy_shell) /* the test is complicated and we already did it */
2925 #else
2926 if (is_bourne_compatible_shell(shell))
2927 #endif
2929 const char *f = line;
2930 char *t = line;
2932 /* Copy the recipe, removing and ignoring interior prefix chars
2933 [@+-]: they're meaningless in .ONESHELL mode. */
2934 while (f[0] != '\0')
2936 int esc = 0;
2938 /* This is the start of a new recipe line.
2939 Skip whitespace and prefix characters. */
2940 while (isblank (*f) || *f == '-' || *f == '@' || *f == '+')
2941 ++f;
2943 /* Copy until we get to the next logical recipe line. */
2944 while (*f != '\0')
2946 *(t++) = *(f++);
2947 if (f[-1] == '\\')
2948 esc = !esc;
2949 else
2951 /* On unescaped newline, we're done with this line. */
2952 if (f[-1] == '\n' && ! esc)
2953 break;
2955 /* Something else: reset the escape sequence. */
2956 esc = 0;
2960 *t = '\0';
2963 new_argv = xmalloc (4 * sizeof (char *));
2964 new_argv[0] = xstrdup(shell);
2965 new_argv[1] = xstrdup(shellflags ? shellflags : "");
2966 new_argv[2] = line;
2967 new_argv[3] = NULL;
2968 return new_argv;
2971 new_line = alloca ((shell_len*2) + 1 + sflags_len + 1
2972 + (line_len*2) + 1);
2973 ap = new_line;
2974 /* Copy SHELL, escaping any characters special to the shell. If
2975 we don't escape them, construct_command_argv_internal will
2976 recursively call itself ad nauseam, or until stack overflow,
2977 whichever happens first. */
2978 for (p = shell; *p != '\0'; ++p)
2980 if (strchr (sh_chars, *p) != 0)
2981 *(ap++) = '\\';
2982 *(ap++) = *p;
2984 *(ap++) = ' ';
2985 if (shellflags)
2986 memcpy (ap, shellflags, sflags_len);
2987 ap += sflags_len;
2988 *(ap++) = ' ';
2989 command_ptr = ap;
2990 for (p = line; *p != '\0'; ++p)
2992 if (restp != NULL && *p == '\n')
2994 *restp = p;
2995 break;
2997 else if (*p == '\\' && p[1] == '\n')
2999 /* POSIX says we keep the backslash-newline. If we don't have a
3000 POSIX shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
3001 and remove the backslash/newline. */
3002 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
3003 # define PRESERVE_BSNL unixy_shell
3004 #else
3005 # define PRESERVE_BSNL 1
3006 #endif
3007 if (PRESERVE_BSNL)
3009 *(ap++) = '\\';
3010 /* Only non-batch execution needs another backslash,
3011 because it will be passed through a recursive
3012 invocation of this function. */
3013 if (!batch_mode_shell)
3014 *(ap++) = '\\';
3015 *(ap++) = '\n';
3017 ++p;
3018 continue;
3021 /* DOS shells don't know about backslash-escaping. */
3022 if (unixy_shell && !batch_mode_shell &&
3023 (*p == '\\' || *p == '\'' || *p == '"'
3024 || isspace ((unsigned char)*p)
3025 || strchr (sh_chars, *p) != 0))
3026 *ap++ = '\\';
3027 #ifdef __MSDOS__
3028 else if (unixy_shell && strneq (p, "...", 3))
3030 /* The case of `...' wildcard again. */
3031 strcpy (ap, "\\.\\.\\");
3032 ap += 5;
3033 p += 2;
3035 #endif
3036 *ap++ = *p;
3038 if (ap == new_line + shell_len + sflags_len + 2)
3039 /* Line was empty. */
3040 return 0;
3041 *ap = '\0';
3043 #ifdef WINDOWS32
3044 /* Some shells do not work well when invoked as 'sh -c xxx' to run a
3045 command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
3046 cases, run commands via a script file. */
3047 if (just_print_flag && !(flags & COMMANDS_RECURSE)) {
3048 /* Need to allocate new_argv, although it's unused, because
3049 start_job_command will want to free it and its 0'th element. */
3050 new_argv = xmalloc(2 * sizeof (char *));
3051 new_argv[0] = xstrdup ("");
3052 new_argv[1] = NULL;
3053 } else if ((no_default_sh_exe || batch_mode_shell) && batch_filename) {
3054 int temp_fd;
3055 FILE* batch = NULL;
3056 int id = GetCurrentProcessId();
3057 PATH_VAR(fbuf);
3059 /* create a file name */
3060 sprintf(fbuf, "make%d", id);
3061 *batch_filename = create_batch_file (fbuf, unixy_shell, &temp_fd);
3063 DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
3064 *batch_filename));
3066 /* Create a FILE object for the batch file, and write to it the
3067 commands to be executed. Put the batch file in TEXT mode. */
3068 _setmode (temp_fd, _O_TEXT);
3069 batch = _fdopen (temp_fd, "wt");
3070 if (!unixy_shell)
3071 fputs ("@echo off\n", batch);
3072 fputs (command_ptr, batch);
3073 fputc ('\n', batch);
3074 fclose (batch);
3075 DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"),
3076 !unixy_shell ? "\n\t@echo off" : "", command_ptr));
3078 /* create argv */
3079 new_argv = xmalloc(3 * sizeof (char *));
3080 if (unixy_shell) {
3081 new_argv[0] = xstrdup (shell);
3082 new_argv[1] = *batch_filename; /* only argv[0] gets freed later */
3083 } else {
3084 new_argv[0] = xstrdup (*batch_filename);
3085 new_argv[1] = NULL;
3087 new_argv[2] = NULL;
3088 } else
3089 #endif /* WINDOWS32 */
3091 if (unixy_shell)
3092 new_argv = construct_command_argv_internal (new_line, 0, 0, 0, 0,
3093 flags, 0);
3095 #ifdef __EMX__
3096 else if (!unixy_shell)
3098 /* new_line is local, must not be freed therefore
3099 We use line here instead of new_line because we run the shell
3100 manually. */
3101 size_t line_len = strlen (line);
3102 char *p = new_line;
3103 char *q = new_line;
3104 memcpy (new_line, line, line_len + 1);
3105 /* Replace all backslash-newline combination and also following tabs.
3106 Important: stop at the first '\n' because that's what the loop above
3107 did. The next line starting at restp[0] will be executed during the
3108 next call of this function. */
3109 while (*q != '\0' && *q != '\n')
3111 if (q[0] == '\\' && q[1] == '\n')
3112 q += 2; /* remove '\\' and '\n' */
3113 else
3114 *p++ = *q++;
3116 *p = '\0';
3118 # ifndef NO_CMD_DEFAULT
3119 if (strnicmp (new_line, "echo", 4) == 0
3120 && (new_line[4] == ' ' || new_line[4] == '\t'))
3122 /* the builtin echo command: handle it separately */
3123 size_t echo_len = line_len - 5;
3124 char *echo_line = new_line + 5;
3126 /* special case: echo 'x="y"'
3127 cmd works this way: a string is printed as is, i.e., no quotes
3128 are removed. But autoconf uses a command like echo 'x="y"' to
3129 determine whether make works. autoconf expects the output x="y"
3130 so we will do exactly that.
3131 Note: if we do not allow cmd to be the default shell
3132 we do not need this kind of voodoo */
3133 if (echo_line[0] == '\''
3134 && echo_line[echo_len - 1] == '\''
3135 && strncmp (echo_line + 1, "ac_maketemp=",
3136 strlen ("ac_maketemp=")) == 0)
3138 /* remove the enclosing quotes */
3139 memmove (echo_line, echo_line + 1, echo_len - 2);
3140 echo_line[echo_len - 2] = '\0';
3143 # endif
3146 /* Let the shell decide what to do. Put the command line into the
3147 2nd command line argument and hope for the best ;-) */
3148 size_t sh_len = strlen (shell);
3150 /* exactly 3 arguments + NULL */
3151 new_argv = xmalloc (4 * sizeof (char *));
3152 /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
3153 the trailing '\0' */
3154 new_argv[0] = xmalloc (sh_len + line_len + 5);
3155 memcpy (new_argv[0], shell, sh_len + 1);
3156 new_argv[1] = new_argv[0] + sh_len + 1;
3157 memcpy (new_argv[1], "/c", 3);
3158 new_argv[2] = new_argv[1] + 3;
3159 memcpy (new_argv[2], new_line, line_len + 1);
3160 new_argv[3] = NULL;
3163 #elif defined(__MSDOS__)
3164 else
3166 /* With MSDOS shells, we must construct the command line here
3167 instead of recursively calling ourselves, because we
3168 cannot backslash-escape the special characters (see above). */
3169 new_argv = xmalloc (sizeof (char *));
3170 line_len = strlen (new_line) - shell_len - sflags_len - 2;
3171 new_argv[0] = xmalloc (line_len + 1);
3172 strncpy (new_argv[0],
3173 new_line + shell_len + sflags_len + 2, line_len);
3174 new_argv[0][line_len] = '\0';
3176 #else
3177 else
3178 fatal (NILF, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
3179 __FILE__, __LINE__);
3180 #endif
3182 #endif /* ! AMIGA */
3184 return new_argv;
3186 #endif /* !VMS */
3188 /* Figure out the argument list necessary to run LINE as a command. Try to
3189 avoid using a shell. This routine handles only ' quoting, and " quoting
3190 when no backslash, $ or ` characters are seen in the quotes. Starting
3191 quotes may be escaped with a backslash. If any of the characters in
3192 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
3193 is the first word of a line, the shell is used.
3195 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
3196 If *RESTP is NULL, newlines will be ignored.
3198 FILE is the target whose commands these are. It is used for
3199 variable expansion for $(SHELL) and $(IFS). */
3201 char **
3202 construct_command_argv (char *line, char **restp, struct file *file,
3203 int cmd_flags, char **batch_filename)
3205 char *shell, *ifs, *shellflags;
3206 char **argv;
3208 #ifdef VMS
3209 char *cptr;
3210 int argc;
3212 argc = 0;
3213 cptr = line;
3214 for (;;)
3216 while ((*cptr != 0)
3217 && (isspace ((unsigned char)*cptr)))
3218 cptr++;
3219 if (*cptr == 0)
3220 break;
3221 while ((*cptr != 0)
3222 && (!isspace((unsigned char)*cptr)))
3223 cptr++;
3224 argc++;
3227 argv = xmalloc (argc * sizeof (char *));
3228 if (argv == 0)
3229 abort ();
3231 cptr = line;
3232 argc = 0;
3233 for (;;)
3235 while ((*cptr != 0)
3236 && (isspace ((unsigned char)*cptr)))
3237 cptr++;
3238 if (*cptr == 0)
3239 break;
3240 DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
3241 argv[argc++] = cptr;
3242 while ((*cptr != 0)
3243 && (!isspace((unsigned char)*cptr)))
3244 cptr++;
3245 if (*cptr != 0)
3246 *cptr++ = 0;
3248 #else
3250 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
3251 int save = warn_undefined_variables_flag;
3252 warn_undefined_variables_flag = 0;
3254 shell = allocated_variable_expand_for_file ("$(SHELL)", file);
3255 #ifdef WINDOWS32
3257 * Convert to forward slashes so that construct_command_argv_internal()
3258 * is not confused.
3260 if (shell) {
3261 char *p = w32ify (shell, 0);
3262 strcpy (shell, p);
3264 #endif
3265 #ifdef __EMX__
3267 static const char *unixroot = NULL;
3268 static const char *last_shell = "";
3269 static int init = 0;
3270 if (init == 0)
3272 unixroot = getenv ("UNIXROOT");
3273 /* unixroot must be NULL or not empty */
3274 if (unixroot && unixroot[0] == '\0') unixroot = NULL;
3275 init = 1;
3278 /* if we have an unixroot drive and if shell is not default_shell
3279 (which means it's either cmd.exe or the test has already been
3280 performed) and if shell is an absolute path without drive letter,
3281 try whether it exists e.g.: if "/bin/sh" does not exist use
3282 "$UNIXROOT/bin/sh" instead. */
3283 if (unixroot && shell && strcmp (shell, last_shell) != 0
3284 && (shell[0] == '/' || shell[0] == '\\'))
3286 /* trying a new shell, check whether it exists */
3287 size_t size = strlen (shell);
3288 char *buf = xmalloc (size + 7);
3289 memcpy (buf, shell, size);
3290 memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
3291 if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
3293 /* try the same for the unixroot drive */
3294 memmove (buf + 2, buf, size + 5);
3295 buf[0] = unixroot[0];
3296 buf[1] = unixroot[1];
3297 if (access (buf, F_OK) == 0)
3298 /* we have found a shell! */
3299 /* free(shell); */
3300 shell = buf;
3301 else
3302 free (buf);
3304 else
3305 free (buf);
3308 #endif /* __EMX__ */
3310 shellflags = allocated_variable_expand_for_file ("$(.SHELLFLAGS)", file);
3311 ifs = allocated_variable_expand_for_file ("$(IFS)", file);
3313 warn_undefined_variables_flag = save;
3316 argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs,
3317 cmd_flags, batch_filename);
3319 free (shell);
3320 free (shellflags);
3321 free (ifs);
3322 #endif /* !VMS */
3323 return argv;
3326 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
3328 dup2 (int old, int new)
3330 int fd;
3332 (void) close (new);
3333 fd = dup (old);
3334 if (fd != new)
3336 (void) close (fd);
3337 errno = EMFILE;
3338 return -1;
3341 return fd;
3343 #endif /* !HAVE_DUP2 && !_AMIGA */
3345 /* On VMS systems, include special VMS functions. */
3347 #ifdef VMS
3348 #include "vmsjobs.c"
3349 #endif