summaryrefslogtreecommitdiff
path: root/job.c
diff options
context:
space:
mode:
Diffstat (limited to 'job.c')
-rw-r--r--job.c2124
1 files changed, 1253 insertions, 871 deletions
diff --git a/job.c b/job.c
index aacfb84..febfac0 100644
--- a/job.c
+++ b/job.c
@@ -1,7 +1,5 @@
/* Job execution and handling for GNU Make.
-Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
-1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
-2010 Free Software Foundation, Inc.
+Copyright (C) 1988-2013 Free Software Foundation, Inc.
This file is part of GNU Make.
GNU Make is free software; you can redistribute it and/or modify it under the
@@ -16,7 +14,7 @@ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. */
-#include "make.h"
+#include "makeint.h"
#include <assert.h>
@@ -108,6 +106,7 @@ static void vmsWaitForChildren (int *);
# include "sub_proc.h"
# include "w32err.h"
# include "pathstuff.h"
+# define WAIT_NOHANG 1
#endif /* WINDOWS32 */
#ifdef __EMX__
@@ -119,13 +118,13 @@ static void vmsWaitForChildren (int *);
#endif
#ifdef HAVE_WAITPID
-# define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
-#else /* Don't have waitpid. */
+# define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
+#else /* Don't have waitpid. */
# ifdef HAVE_WAIT3
# ifndef wait3
extern int wait3 ();
# endif
-# define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
+# define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
# endif /* Have wait3. */
#endif /* Have waitpid. */
@@ -133,7 +132,7 @@ extern int wait3 ();
int wait ();
#endif
-#ifndef HAVE_UNION_WAIT
+#ifndef HAVE_UNION_WAIT
# define WAIT_T int
@@ -153,7 +152,7 @@ int wait ();
# define WIFEXITED(x) (WTERMSIG (x) == 0)
# endif
-#else /* Have `union wait'. */
+#else /* Have 'union wait'. */
# define WAIT_T union wait
# ifndef WTERMSIG
@@ -172,7 +171,7 @@ int wait ();
# define WIFEXITED(x) (WTERMSIG(x) == 0)
# endif
-#endif /* Don't have `union wait'. */
+#endif /* Don't have 'union wait'. */
#if !defined(HAVE_UNISTD_H) && !defined(WINDOWS32)
int dup2 ();
@@ -224,7 +223,7 @@ struct child *children = 0;
unsigned int job_slots_used = 0;
-/* Nonzero if the `good' standard input is in use. */
+/* Nonzero if the 'good' standard input is in use. */
static int good_stdin_used = 0;
@@ -244,14 +243,15 @@ unsigned long job_counter = 0;
unsigned int jobserver_tokens = 0;
+
#ifdef WINDOWS32
/*
- * The macro which references this function is defined in make.h.
+ * The macro which references this function is defined in makeint.h.
*/
int
-w32_kill(pid_t pid, int sig)
+w32_kill (pid_t pid, int sig)
{
- return ((process_kill((HANDLE)pid, sig) == TRUE) ? 0 : -1);
+ return ((process_kill ((HANDLE)pid, sig) == TRUE) ? 0 : -1);
}
/* This function creates a temporary file name with an extension specified
@@ -264,9 +264,18 @@ create_batch_file (char const *base, int unixy, int *fd)
const char *const ext = unixy ? "sh" : "bat";
const char *error_string = NULL;
char temp_path[MAXPATHLEN]; /* need to know its length */
- unsigned path_size = GetTempPath(sizeof temp_path, temp_path);
+ unsigned path_size = GetTempPath (sizeof temp_path, temp_path);
int path_is_dot = 0;
- unsigned uniq = 1;
+ /* The following variable is static so we won't try to reuse a name
+ that was generated a little while ago, because that file might
+ not be on disk yet, since we use FILE_ATTRIBUTE_TEMPORARY below,
+ which tells the OS it doesn't need to flush the cache to disk.
+ If the file is not yet on disk, we might think the name is
+ available, while it really isn't. This happens in parallel
+ builds, where Make doesn't wait for one job to finish before it
+ launches the next one. */
+ static unsigned uniq = 0;
+ static int second_loop = 0;
const unsigned sizemax = strlen (base) + strlen (ext) + 10;
if (path_size == 0)
@@ -275,9 +284,19 @@ create_batch_file (char const *base, int unixy, int *fd)
path_is_dot = 1;
}
+ ++uniq;
+ if (uniq >= 0x10000 && !second_loop)
+ {
+ /* If we already had 64K batch files in this
+ process, make a second loop through the numbers,
+ looking for free slots, i.e. files that were
+ deleted in the meantime. */
+ second_loop = 1;
+ uniq = 1;
+ }
while (path_size > 0 &&
path_size + sizemax < sizeof temp_path &&
- uniq < 0x10000)
+ !(uniq >= 0x10000 && second_loop))
{
unsigned size = sprintf (temp_path + path_size,
"%s%s-%x.%s",
@@ -294,10 +313,17 @@ create_batch_file (char const *base, int unixy, int *fd)
if (h == INVALID_HANDLE_VALUE)
{
- const DWORD er = GetLastError();
+ const DWORD er = GetLastError ();
if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS)
- ++uniq;
+ {
+ ++uniq;
+ if (uniq == 0x10000 && !second_loop)
+ {
+ second_loop = 1;
+ uniq = 1;
+ }
+ }
/* the temporary path is not guaranteed to exist */
else if (path_is_dot == 0)
@@ -373,11 +399,12 @@ _is_unixy_shell (const char *path)
if (*name == '/' || *name == '\\') name++;
i = 0;
- while (known_os2shells[i] != NULL) {
- if (strcasecmp (name, known_os2shells[i]) == 0)
- return 0; /* not a unix shell */
- i++;
- }
+ while (known_os2shells[i] != NULL)
+ {
+ if (strcasecmp (name, known_os2shells[i]) == 0)
+ return 0; /* not a unix shell */
+ i++;
+ }
/* in doubt assume a unix like shell */
return 1;
@@ -388,8 +415,8 @@ _is_unixy_shell (const char *path)
int
is_bourne_compatible_shell (const char *path)
{
- /* list of known unix (Bourne-like) shells */
- const char *unix_shells[] = {
+ /* List of known POSIX (or POSIX-ish) shells. */
+ static const char *unix_shells[] = {
"sh",
"bash",
"ksh",
@@ -399,7 +426,7 @@ is_bourne_compatible_shell (const char *path)
"dash",
NULL
};
- unsigned i, len;
+ const char **s;
/* find the rightmost '/' or '\\' */
const char *name = strrchr (path, '/');
@@ -412,20 +439,21 @@ is_bourne_compatible_shell (const char *path)
else if (!name) /* name and p must be 0 */
name = path;
- if (*name == '/' || *name == '\\') name++;
+ if (*name == '/' || *name == '\\')
+ ++name;
/* this should be able to deal with extensions on Windows-like systems */
- for (i = 0; unix_shells[i] != NULL; i++) {
- len = strlen(unix_shells[i]);
+ for (s = unix_shells; *s != NULL; ++s)
+ {
#if defined(WINDOWS32) || defined(__MSDOS__)
- if ((strncasecmp (name, unix_shells[i], len) == 0) &&
- (strlen(name) >= len && (name[len] == '\0' || name[len] == '.')))
+ unsigned int len = strlen (*s);
+ if ((strlen (name) >= len && STOP_SET (name[len], MAP_DOT|MAP_NUL))
+ && strncasecmp (name, *s, len) == 0)
#else
- if ((strncmp (name, unix_shells[i], len) == 0) &&
- (strlen(name) >= len && name[len] == '\0'))
+ if (strcmp (name, *s) == 0)
#endif
- return 1; /* a known unix-style shell */
- }
+ return 1; /* a known unix-style shell */
+ }
/* if not on the list, assume it's not a Bourne-like shell */
return 0;
@@ -437,28 +465,60 @@ is_bourne_compatible_shell (const char *path)
Append "(ignored)" if IGNORED is nonzero. */
static void
-child_error (const char *target_name,
+child_error (struct child *child,
int exit_code, int exit_sig, int coredump, int ignored)
{
+ const char *pre = "*** ";
+ const char *post = "";
+ const char *dump = "";
+ const struct file *f = child->file;
+ const gmk_floc *flocp = &f->cmds->fileinfo;
+ const char *nm;
+
if (ignored && silent_flag)
return;
+ if (exit_sig && coredump)
+ dump = _(" (core dumped)");
+
+ if (ignored)
+ {
+ pre = "";
+ post = _(" (ignored)");
+ }
+
+ if (! flocp->filenm)
+ nm = _("<builtin>");
+ else
+ {
+ char *a = alloca (strlen (flocp->filenm) + 1 + 11 + 1);
+ sprintf (a, "%s:%lu", flocp->filenm, flocp->lineno);
+ nm = a;
+ }
+
+ OUTPUT_SET (&child->output);
+
+ message (0, _("%s: recipe for target '%s' failed"), nm, f->name);
+
#ifdef VMS
- if (!(exit_code & 1))
- error (NILF,
- (ignored ? _("*** [%s] Error 0x%x (ignored)")
- : _("*** [%s] Error 0x%x")),
- target_name, exit_code);
+ if ((exit_code & 1) != 0)
+ {
+ OUTPUT_UNSET ();
+ return;
+ }
+
+ error (NILF, _("%s[%s] Error 0x%x%s"), pre, f->name, exit_code, post);
#else
if (exit_sig == 0)
- error (NILF, ignored ? _("[%s] Error %d (ignored)") :
- _("*** [%s] Error %d"),
- target_name, exit_code);
+ error (NILF, _("%s[%s] Error %d%s"), pre, f->name, exit_code, post);
else
- error (NILF, "*** [%s] %s%s",
- target_name, strsignal (exit_sig),
- coredump ? _(" (core dumped)") : "");
+ {
+ const char *s = strsignal (exit_sig);
+ error (NILF, _("%s[%s] %s%s%s"), pre, f->name, s, dump, post);
+ }
#endif /* VMS */
+
+ OUTPUT_UNSET ();
}
@@ -498,7 +558,7 @@ child_handler (int sig UNUSED)
extern int shell_function_pid, shell_function_completed;
/* Reap all dead children, storing the returned status and the new command
- state (`cs_finished') in the `file' member of the `struct child' for the
+ state ('cs_finished') in the 'file' member of the 'struct child' for the
dead child, and removing the child from the chain. In addition, if BLOCK
nonzero, we block in this function until we've reaped at least one
complete child, waiting for it to die if necessary. If ERR is nonzero,
@@ -509,9 +569,9 @@ reap_children (int block, int err)
{
#ifndef WINDOWS32
WAIT_T status;
+#endif
/* Initially, assume we have some. */
int reap_more = 1;
-#endif
#ifdef WAIT_NOHANG
# define REAP_MORE reap_more
@@ -533,115 +593,115 @@ reap_children (int block, int err)
int remote = 0;
pid_t pid;
int exit_code, exit_sig, coredump;
- register struct child *lastc, *c;
+ struct child *lastc, *c;
int child_failed;
int any_remote, any_local;
int dontcare;
if (err && block)
- {
+ {
static int printed = 0;
- /* We might block for a while, so let the user know why.
+ /* We might block for a while, so let the user know why.
Only print this message once no matter how many jobs are left. */
- fflush (stdout);
+ fflush (stdout);
if (!printed)
error (NILF, _("*** Waiting for unfinished jobs...."));
printed = 1;
- }
+ }
/* We have one less dead child to reap. As noted in
- child_handler() above, this count is completely unimportant for
- all modern, POSIX-y systems that support wait3() or waitpid().
- The rest of this comment below applies only to early, broken
- pre-POSIX systems. We keep the count only because... it's there...
-
- The test and decrement are not atomic; if it is compiled into:
- register = dead_children - 1;
- dead_children = register;
- a SIGCHLD could come between the two instructions.
- child_handler increments dead_children.
- The second instruction here would lose that increment. But the
- only effect of dead_children being wrong is that we might wait
- longer than necessary to reap a child, and lose some parallelism;
- and we might print the "Waiting for unfinished jobs" message above
- when not necessary. */
+ child_handler() above, this count is completely unimportant for
+ all modern, POSIX-y systems that support wait3() or waitpid().
+ The rest of this comment below applies only to early, broken
+ pre-POSIX systems. We keep the count only because... it's there...
+
+ The test and decrement are not atomic; if it is compiled into:
+ register = dead_children - 1;
+ dead_children = register;
+ a SIGCHLD could come between the two instructions.
+ child_handler increments dead_children.
+ The second instruction here would lose that increment. But the
+ only effect of dead_children being wrong is that we might wait
+ longer than necessary to reap a child, and lose some parallelism;
+ and we might print the "Waiting for unfinished jobs" message above
+ when not necessary. */
if (dead_children > 0)
- --dead_children;
+ --dead_children;
any_remote = 0;
any_local = shell_function_pid != 0;
for (c = children; c != 0; c = c->next)
- {
- any_remote |= c->remote;
- any_local |= ! c->remote;
- DB (DB_JOBS, (_("Live child %p (%s) PID %s %s\n"),
+ {
+ any_remote |= c->remote;
+ any_local |= ! c->remote;
+ DB (DB_JOBS, (_("Live child %p (%s) PID %s %s\n"),
c, c->file->name, pid2str (c->pid),
c->remote ? _(" (remote)") : ""));
#ifdef VMS
- break;
+ break;
#endif
- }
+ }
/* First, check for remote children. */
if (any_remote)
- pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
+ pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
else
- pid = 0;
+ pid = 0;
if (pid > 0)
- /* We got a remote child. */
- remote = 1;
+ /* We got a remote child. */
+ remote = 1;
else if (pid < 0)
- {
+ {
/* A remote status command failed miserably. Punt. */
- remote_status_lose:
- pfatal_with_name ("remote_status");
- }
+ remote_status_lose:
+ pfatal_with_name ("remote_status");
+ }
else
- {
- /* No remote children. Check for local children. */
+ {
+ /* No remote children. Check for local children. */
#if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
- if (any_local)
- {
+ if (any_local)
+ {
#ifdef VMS
- vmsWaitForChildren (&status);
- pid = c->pid;
+ vmsWaitForChildren (&status);
+ pid = c->pid;
#else
#ifdef WAIT_NOHANG
- if (!block)
- pid = WAIT_NOHANG (&status);
- else
+ if (!block)
+ pid = WAIT_NOHANG (&status);
+ else
#endif
- EINTRLOOP(pid, wait (&status));
+ EINTRLOOP(pid, wait (&status));
#endif /* !VMS */
- }
- else
- pid = 0;
+ }
+ else
+ pid = 0;
- if (pid < 0)
- {
+ if (pid < 0)
+ {
/* The wait*() failed miserably. Punt. */
- pfatal_with_name ("wait");
- }
- else if (pid > 0)
- {
- /* We got a child exit; chop the status word up. */
- exit_code = WEXITSTATUS (status);
- exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
- coredump = WCOREDUMP (status);
+ pfatal_with_name ("wait");
+ }
+ else if (pid > 0)
+ {
+ /* We got a child exit; chop the status word up. */
+ exit_code = WEXITSTATUS (status);
+ exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
+ coredump = WCOREDUMP (status);
/* If we have started jobs in this second, remove one. */
if (job_counter)
--job_counter;
- }
- else
- {
- /* No local children are dead. */
+ }
+ else
+ {
+ /* No local children are dead. */
reap_more = 0;
- if (!block || !any_remote)
+ if (!block || !any_remote)
break;
/* Now try a blocking wait for a remote child. */
@@ -654,32 +714,32 @@ reap_children (int block, int err)
/* We got a remote child. */
remote = 1;
- }
+ }
#endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
#ifdef __MSDOS__
- /* Life is very different on MSDOS. */
- pid = dos_pid - 1;
- status = dos_status;
- exit_code = WEXITSTATUS (status);
- if (exit_code == 0xff)
- exit_code = -1;
- exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
- coredump = 0;
+ /* Life is very different on MSDOS. */
+ pid = dos_pid - 1;
+ status = dos_status;
+ exit_code = WEXITSTATUS (status);
+ if (exit_code == 0xff)
+ exit_code = -1;
+ exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
+ coredump = 0;
#endif /* __MSDOS__ */
#ifdef _AMIGA
- /* Same on Amiga */
- pid = amiga_pid - 1;
- status = amiga_status;
- exit_code = amiga_status;
- exit_sig = 0;
- coredump = 0;
+ /* Same on Amiga */
+ pid = amiga_pid - 1;
+ status = amiga_status;
+ exit_code = amiga_status;
+ exit_sig = 0;
+ coredump = 0;
#endif /* _AMIGA */
#ifdef WINDOWS32
{
HANDLE hPID;
- int werr;
HANDLE hcTID, hcPID;
+ DWORD dwWaitStatus = 0;
exit_code = 0;
exit_sig = 0;
coredump = 0;
@@ -696,58 +756,69 @@ reap_children (int block, int err)
DWORD e = GetLastError ();
fprintf (stderr,
"Determine main thread ID (Error %ld: %s)\n",
- e, map_windows32_error_to_string(e));
+ e, map_windows32_error_to_string (e));
}
else
DB (DB_VERBOSE, ("Main thread handle = %p\n", main_thread));
}
/* wait for anything to finish */
- hPID = process_wait_for_any();
+ hPID = process_wait_for_any (block, &dwWaitStatus);
if (hPID)
{
-
/* was an error found on this process? */
- werr = process_last_err(hPID);
+ int werr = process_last_err (hPID);
/* get exit data */
- exit_code = process_exit_code(hPID);
+ exit_code = process_exit_code (hPID);
if (werr)
- fprintf(stderr, "make (e=%d): %s",
- exit_code, map_windows32_error_to_string(exit_code));
+ fprintf (stderr, "make (e=%d): %s", exit_code,
+ map_windows32_error_to_string (exit_code));
/* signal */
- exit_sig = process_signal(hPID);
+ exit_sig = process_signal (hPID);
/* cleanup process */
- process_cleanup(hPID);
+ process_cleanup (hPID);
coredump = 0;
}
+ else if (dwWaitStatus == WAIT_FAILED)
+ {
+ /* The WaitForMultipleObjects() failed miserably. Punt. */
+ pfatal_with_name ("WaitForMultipleObjects");
+ }
+ else if (dwWaitStatus == WAIT_TIMEOUT)
+ {
+ /* No child processes are finished. Give up waiting. */
+ reap_more = 0;
+ break;
+ }
+
pid = (pid_t) hPID;
}
#endif /* WINDOWS32 */
- }
+ }
- /* Check if this is the child of the `shell' function. */
+ /* Check if this is the child of the 'shell' function. */
if (!remote && pid == shell_function_pid)
- {
- /* It is. Leave an indicator for the `shell' function. */
- if (exit_sig == 0 && exit_code == 127)
- shell_function_completed = -1;
- else
- shell_function_completed = 1;
- break;
- }
+ {
+ /* It is. Leave an indicator for the 'shell' function. */
+ if (exit_sig == 0 && exit_code == 127)
+ shell_function_completed = -1;
+ else
+ shell_function_completed = 1;
+ break;
+ }
child_failed = exit_sig != 0 || exit_code != 0;
/* Search for a child matching the deceased one. */
lastc = 0;
for (c = children; c != 0; lastc = c, c = c->next)
- if (c->remote == remote && c->pid == pid)
- break;
+ if (c->pid == pid && c->remote == remote)
+ break;
if (c == 0)
/* An unknown child died.
@@ -759,17 +830,23 @@ reap_children (int block, int err)
: _("Reaping winning child %p PID %s %s\n"),
c, pid2str (c->pid), c->remote ? _(" (remote)") : ""));
- if (c->sh_batch_file) {
- DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
- c->sh_batch_file));
+ if (c->sh_batch_file)
+ {
+ int rm_status;
- /* just try and remove, don't care if this fails */
- remove (c->sh_batch_file);
+ DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
+ c->sh_batch_file));
- /* all done with memory */
- free (c->sh_batch_file);
- c->sh_batch_file = NULL;
- }
+ errno = 0;
+ rm_status = remove (c->sh_batch_file);
+ if (rm_status)
+ DB (DB_JOBS, (_("Cleaning up temp batch file %s failed (%d)\n"),
+ c->sh_batch_file, errno));
+
+ /* all done with memory */
+ free (c->sh_batch_file);
+ c->sh_batch_file = NULL;
+ }
/* If this child had the good stdin, say it is now free. */
if (c->good_stdin)
@@ -784,9 +861,9 @@ reap_children (int block, int err)
static int delete_on_error = -1;
if (!dontcare)
- child_error (c->file->name, exit_code, exit_sig, coredump, 0);
+ child_error (c, exit_code, exit_sig, coredump, 0);
- c->file->update_status = 2;
+ c->file->update_status = us_failed;
if (delete_on_error == -1)
{
struct file *f = lookup_file (".DELETE_ON_ERROR");
@@ -800,8 +877,7 @@ reap_children (int block, int err)
if (child_failed)
{
/* The commands failed, but we don't care. */
- child_error (c->file->name,
- exit_code, exit_sig, coredump, 1);
+ child_error (c, exit_code, exit_sig, coredump, 1);
child_failed = 0;
}
@@ -814,10 +890,16 @@ reap_children (int block, int err)
Since there are more commands that wanted to be run,
the target was not completely remade. So we treat
this as if a command had failed. */
- c->file->update_status = 2;
+ c->file->update_status = us_failed;
}
else
{
+#ifndef NO_OUTPUT_SYNC
+ /* If we're sync'ing per line, write the previous line's
+ output before starting the next one. */
+ if (output_sync == OUTPUT_SYNC_LINE)
+ output_dump (&c->output);
+#endif
/* Check again whether to start remotely.
Whether or not we want to changes over time.
Also, start_remote_job may need state set up
@@ -835,7 +917,7 @@ reap_children (int block, int err)
continue;
}
- if (c->file->update_status != 0)
+ if (c->file->update_status != us_success)
/* We failed to start the commands. */
delete_child_targets (c);
}
@@ -843,12 +925,18 @@ reap_children (int block, int err)
/* There are no more commands. We got through them all
without an unignored error. Now the target has been
successfully updated. */
- c->file->update_status = 0;
+ c->file->update_status = us_success;
}
- /* When we get here, all the commands for C->file are finished
- (or aborted) and C->file->update_status contains 0 or 2. But
- C->file->command_state is still cs_running if all the commands
+ /* When we get here, all the commands for c->file are finished. */
+
+#ifndef NO_OUTPUT_SYNC
+ /* Synchronize any remaining parallel output. */
+ output_dump (&c->output);
+#endif
+
+ /* At this point c->file->update_status is success or failed. But
+ c->file->command_state is still cs_running if all the commands
ran; notice_finish_file looks for cs_running to tell it that
it's interesting to check the file's modtime again now. */
@@ -901,6 +989,8 @@ reap_children (int block, int err)
static void
free_child (struct child *child)
{
+ output_close (&child->output);
+
if (!jobserver_tokens)
fatal (NILF, "INTERNAL: Freeing child %p (%s) but no tokens left!\n",
child, child->file->name);
@@ -908,6 +998,19 @@ free_child (struct child *child)
/* If we're using the jobserver and this child is not the only outstanding
job, put a token back into the pipe for it. */
+#ifdef WINDOWS32
+ if (has_jobserver_semaphore () && jobserver_tokens > 1)
+ {
+ if (! release_jobserver_semaphore ())
+ {
+ DWORD err = GetLastError ();
+ fatal (NILF, _("release jobserver semaphore: (Error %ld: %s)"),
+ err, map_windows32_error_to_string (err));
+ }
+
+ DB (DB_JOBS, (_("Released token for child %p (%s).\n"), child, child->file->name));
+ }
+#else
if (job_fds[1] >= 0 && jobserver_tokens > 1)
{
char token = '+';
@@ -917,11 +1020,12 @@ free_child (struct child *child)
EINTRLOOP (r, write (job_fds[1], &token, 1));
if (r != 1)
- pfatal_with_name (_("write jobserver"));
+ pfatal_with_name (_("write jobserver"));
DB (DB_JOBS, (_("Released token for child %p (%s).\n"),
child, child->file->name));
}
+#endif
--jobserver_tokens;
@@ -932,7 +1036,7 @@ free_child (struct child *child)
{
register unsigned int i;
for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
- free (child->command_lines[i]);
+ free (child->command_lines[i]);
free (child->command_lines);
}
@@ -940,7 +1044,7 @@ free_child (struct child *child)
{
register char **ep = child->environment;
while (*ep != 0)
- free (*ep++);
+ free (*ep++);
free (child->environment);
}
@@ -973,7 +1077,7 @@ unblock_sigs (void)
}
#endif
-#ifdef MAKE_JOBSERVER
+#if defined(MAKE_JOBSERVER) && !defined(WINDOWS32)
RETSIGTYPE
job_noop (int sig UNUSED)
{
@@ -1018,7 +1122,7 @@ set_child_handler_action_flags (int set_handler, int set_alarm)
CHILD is updated to reflect the commands and ID of the child process.
NOTE: On return fatal signals are blocked! The caller is responsible
- for calling `unblock_sigs', once the new child is safely on the chain so
+ for calling 'unblock_sigs', once the new child is safely on the chain so
it can be cleaned up in the event of a fatal signal. */
static void
@@ -1027,9 +1131,8 @@ start_job_command (struct child *child)
#if !defined(_AMIGA) && !defined(WINDOWS32)
static int bad_stdin = -1;
#endif
+ int flags;
char *p;
- /* Must be volatile to silence bogus GCC warning about longjmp/vfork. */
- volatile int flags;
#ifdef VMS
char *argv;
#else
@@ -1043,7 +1146,7 @@ start_job_command (struct child *child)
/* Combine the flags parsed for the line itself with
the flags specified globally for this target. */
flags = (child->file->command_flags
- | child->file->cmds->lines_flags[child->command_line - 1]);
+ | child->file->cmds->lines_flags[child->command_line - 1]);
p = child->command_ptr;
child->noerror = ((flags & COMMANDS_NOERROR) != 0);
@@ -1051,13 +1154,13 @@ start_job_command (struct child *child)
while (*p != '\0')
{
if (*p == '@')
- flags |= COMMANDS_SILENT;
+ flags |= COMMANDS_SILENT;
else if (*p == '+')
- flags |= COMMANDS_RECURSE;
+ flags |= COMMANDS_RECURSE;
else if (*p == '-')
- child->noerror = 1;
+ child->noerror = 1;
else if (!isblank ((unsigned char)*p))
- break;
+ break;
++p;
}
@@ -1070,29 +1173,44 @@ start_job_command (struct child *child)
child->file->cmds->lines_flags[child->command_line - 1]
|= flags & COMMANDS_RECURSE;
- /* Figure out an argument list from this command line. */
+ /* POSIX requires that a recipe prefix after a backslash-newline should
+ be ignored. Remove it now so the output is correct. */
+ {
+ char prefix = child->file->cmds->recipe_prefix;
+ char *p1, *p2;
+ p1 = p2 = p;
+ while (*p1 != '\0')
+ {
+ *(p2++) = *p1;
+ if (p1[0] == '\n' && p1[1] == prefix)
+ ++p1;
+ ++p1;
+ }
+ *p2 = *p1;
+ }
+ /* Figure out an argument list from this command line. */
{
char *end = 0;
#ifdef VMS
argv = p;
#else
argv = construct_command_argv (p, &end, child->file,
- child->file->cmds->lines_flags[child->command_line - 1],
- &child->sh_batch_file);
+ child->file->cmds->lines_flags[child->command_line - 1],
+ &child->sh_batch_file);
#endif
if (end == NULL)
child->command_ptr = NULL;
else
{
- *end++ = '\0';
- child->command_ptr = end;
+ *end++ = '\0';
+ child->command_ptr = end;
}
}
- /* If -q was given, say that updating `failed' if there was any text on the
- command line, or `succeeded' otherwise. The exit status of 1 tells the
- user that -q is saying `something to do'; the exit status for a random
+ /* If -q was given, say that updating 'failed' if there was any text on the
+ command line, or 'succeeded' otherwise. The exit status of 1 tells the
+ user that -q is saying 'something to do'; the exit status for a random
error is 2. */
if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
{
@@ -1100,7 +1218,7 @@ start_job_command (struct child *child)
free (argv[0]);
free (argv);
#endif
- child->file->update_status = 1;
+ child->file->update_status = us_question;
notice_finished_file (child->file);
return;
}
@@ -1108,7 +1226,7 @@ start_job_command (struct child *child)
if (touch_flag && !(flags & COMMANDS_RECURSE))
{
/* Go on to the next command. It might be the recursive one.
- We construct ARGV only to find the end of the command line. */
+ We construct ARGV only to find the end of the command line. */
#ifndef VMS
if (argv)
{
@@ -1127,24 +1245,39 @@ start_job_command (struct child *child)
#endif
/* This line has no commands. Go to the next. */
if (job_next_command (child))
- start_job_command (child);
+ start_job_command (child);
else
- {
- /* No more commands. Make sure we're "running"; we might not be if
+ {
+ /* No more commands. Make sure we're "running"; we might not be if
(e.g.) all commands were skipped due to -n. */
set_command_state (child->file, cs_running);
- child->file->update_status = 0;
- notice_finished_file (child->file);
- }
+ child->file->update_status = us_success;
+ notice_finished_file (child->file);
+ }
+
+ OUTPUT_UNSET();
return;
}
- /* Print out the command. If silent, we call `message' with null so it
- can log the working directory before the command's own error messages
- appear. */
+ /* Are we going to synchronize this command's output? Do so if either we're
+ in SYNC_RECURSE mode or this command is not recursive. We'll also check
+ output_sync separately below in case it changes due to error. */
+ child->output.syncout = output_sync && (output_sync == OUTPUT_SYNC_RECURSE
+ || !(flags & COMMANDS_RECURSE));
- message (0, (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag))
- ? "%s" : (char *) 0, p);
+ OUTPUT_SET (&child->output);
+
+#ifndef NO_OUTPUT_SYNC
+ if (! child->output.syncout)
+ /* We don't want to sync this command: to avoid misordered
+ output ensure any already-synced content is written. */
+ output_dump (&child->output);
+#endif
+
+ /* Print the command if appropriate. */
+ if (just_print_flag || trace_flag
+ || (!(flags & COMMANDS_SILENT) && !silent_flag))
+ message (0, "%s", p);
/* Tell update_goal_chain that a command has been started on behalf of
this target. It is important that this happens here and not in
@@ -1164,15 +1297,15 @@ start_job_command (struct child *child)
#if !defined(VMS) && !defined(_AMIGA)
if (
#if defined __MSDOS__ || defined (__EMX__)
- unixy_shell /* the test is complicated and we already did it */
+ unixy_shell /* the test is complicated and we already did it */
#else
- (argv[0] && is_bourne_compatible_shell(argv[0]))
+ (argv[0] && is_bourne_compatible_shell (argv[0]))
#endif
&& (argv[1] && argv[1][0] == '-'
- &&
- ((argv[1][1] == 'c' && argv[1][2] == '\0')
- ||
- (argv[1][1] == 'e' && argv[1][2] == 'c' && argv[1][3] == '\0')))
+ &&
+ ((argv[1][1] == 'c' && argv[1][2] == '\0')
+ ||
+ (argv[1][1] == 'e' && argv[1][2] == 'c' && argv[1][3] == '\0')))
&& (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
&& argv[3] == NULL)
{
@@ -1193,6 +1326,9 @@ start_job_command (struct child *child)
goto next_command;
}
+ /* We're sure we're going to invoke a command: set up the output. */
+ output_start ();
+
/* Flush the output streams so they won't have things written twice. */
fflush (stdout);
@@ -1206,26 +1342,26 @@ start_job_command (struct child *child)
if (bad_stdin == -1)
{
/* Make a file descriptor that is the read end of a broken pipe.
- This will be used for some children's standard inputs. */
+ This will be used for some children's standard inputs. */
int pd[2];
if (pipe (pd) == 0)
- {
- /* Close the write side. */
- (void) close (pd[1]);
- /* Save the read side. */
- bad_stdin = pd[0];
-
- /* Set the descriptor to close on exec, so it does not litter any
- child's descriptor table. When it is dup2'd onto descriptor 0,
- that descriptor will not close on exec. */
- CLOSE_ON_EXEC (bad_stdin);
- }
+ {
+ /* Close the write side. */
+ (void) close (pd[1]);
+ /* Save the read side. */
+ bad_stdin = pd[0];
+
+ /* Set the descriptor to close on exec, so it does not litter any
+ child's descriptor table. When it is dup2'd onto descriptor 0,
+ that descriptor will not close on exec. */
+ CLOSE_ON_EXEC (bad_stdin);
+ }
}
#endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
- /* Decide whether to give this child the `good' standard input
- (one that points to the terminal or whatever), or the `bad' one
+ /* Decide whether to give this child the 'good' standard input
+ (one that points to the terminal or whatever), or the 'bad' one
that points to the read side of a broken pipe. */
child->good_stdin = !good_stdin_used;
@@ -1250,21 +1386,21 @@ start_job_command (struct child *child)
{
int is_remote, id, used_stdin;
if (start_remote_job (argv, child->environment,
- child->good_stdin ? 0 : bad_stdin,
- &is_remote, &id, &used_stdin))
+ child->good_stdin ? 0 : bad_stdin,
+ &is_remote, &id, &used_stdin))
/* Don't give up; remote execution may fail for various reasons. If
so, simply run the job locally. */
- goto run_local;
+ goto run_local;
else
- {
- if (child->good_stdin && !used_stdin)
- {
- child->good_stdin = 0;
- good_stdin_used = 0;
- }
- child->remote = is_remote;
- child->pid = id;
- }
+ {
+ if (child->good_stdin && !used_stdin)
+ {
+ child->good_stdin = 0;
+ good_stdin_used = 0;
+ }
+ child->remote = is_remote;
+ child->pid = id;
+ }
}
else
#endif /* !VMS */
@@ -1279,11 +1415,12 @@ start_job_command (struct child *child)
child->remote = 0;
#ifdef VMS
- if (!child_execute_job (argv, child)) {
- /* Fork failed! */
- perror_with_name ("vfork", "");
- goto error;
- }
+ if (!child_execute_job (argv, child))
+ {
+ /* Fork failed! */
+ perror_with_name ("fork", "");
+ goto error;
+ }
#else
@@ -1293,41 +1430,45 @@ start_job_command (struct child *child)
/* If we aren't running a recursive command and we have a jobserver
pipe, close it before exec'ing. */
if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
- {
- CLOSE_ON_EXEC (job_fds[0]);
- CLOSE_ON_EXEC (job_fds[1]);
- }
+ {
+ CLOSE_ON_EXEC (job_fds[0]);
+ CLOSE_ON_EXEC (job_fds[1]);
+ }
if (job_rfd >= 0)
- CLOSE_ON_EXEC (job_rfd);
+ CLOSE_ON_EXEC (job_rfd);
/* Never use fork()/exec() here! Use spawn() instead in exec_command() */
- child->pid = child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
+ child->pid = child_execute_job (child->good_stdin ? FD_STDIN : bad_stdin,
+ FD_STDOUT, FD_STDERR,
argv, child->environment);
if (child->pid < 0)
- {
- /* spawn failed! */
- unblock_sigs ();
- perror_with_name ("spawn", "");
- goto error;
- }
+ {
+ /* spawn failed! */
+ unblock_sigs ();
+ perror_with_name ("spawn", "");
+ goto error;
+ }
/* undo CLOSE_ON_EXEC() after the child process has been started */
if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
- {
- fcntl (job_fds[0], F_SETFD, 0);
- fcntl (job_fds[1], F_SETFD, 0);
- }
+ {
+ fcntl (job_fds[0], F_SETFD, 0);
+ fcntl (job_fds[1], F_SETFD, 0);
+ }
if (job_rfd >= 0)
- fcntl (job_rfd, F_SETFD, 0);
+ fcntl (job_rfd, F_SETFD, 0);
#else /* !__EMX__ */
- child->pid = vfork ();
- environ = parent_environ; /* Restore value child may have clobbered. */
+ child->pid = fork ();
+ environ = parent_environ; /* Restore value child may have clobbered. */
if (child->pid == 0)
- {
- /* We are the child side. */
- unblock_sigs ();
+ {
+ int outfd = FD_STDOUT;
+ int errfd = FD_STDERR;
+
+ /* We are the child side. */
+ unblock_sigs ();
/* If we aren't running a recursive command and we have a jobserver
pipe, close it before exec'ing. */
@@ -1345,21 +1486,30 @@ start_job_command (struct child *child)
setrlimit (RLIMIT_STACK, &stack_limit);
#endif
- child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
- argv, child->environment);
- }
+ /* Divert child output if output_sync in use. */
+ if (child->output.syncout)
+ {
+ if (child->output.out >= 0)
+ outfd = child->output.out;
+ if (child->output.err >= 0)
+ errfd = child->output.err;
+ }
+
+ child_execute_job (child->good_stdin ? FD_STDIN : bad_stdin,
+ outfd, errfd, argv, child->environment);
+ }
else if (child->pid < 0)
- {
- /* Fork failed! */
- unblock_sigs ();
- perror_with_name ("vfork", "");
- goto error;
- }
+ {
+ /* Fork failed! */
+ unblock_sigs ();
+ perror_with_name ("fork", "");
+ goto error;
+ }
# endif /* !__EMX__ */
#endif /* !VMS */
}
-#else /* __MSDOS__ or Amiga or WINDOWS32 */
+#else /* __MSDOS__ or Amiga or WINDOWS32 */
#ifdef __MSDOS__
{
int proc_return;
@@ -1367,37 +1517,37 @@ start_job_command (struct child *child)
block_sigs ();
dos_status = 0;
- /* We call `system' to do the job of the SHELL, since stock DOS
- shell is too dumb. Our `system' knows how to handle long
+ /* We call 'system' to do the job of the SHELL, since stock DOS
+ shell is too dumb. Our 'system' knows how to handle long
command lines even if pipes/redirection is needed; it will only
call COMMAND.COM when its internal commands are used. */
if (execute_by_shell)
{
- char *cmdline = argv[0];
- /* We don't have a way to pass environment to `system',
- so we need to save and restore ours, sigh... */
- char **parent_environ = environ;
-
- environ = child->environment;
-
- /* If we have a *real* shell, tell `system' to call
- it to do everything for us. */
- if (unixy_shell)
- {
- /* A *real* shell on MSDOS may not support long
- command lines the DJGPP way, so we must use `system'. */
- cmdline = argv[2]; /* get past "shell -c" */
- }
-
- dos_command_running = 1;
- proc_return = system (cmdline);
- environ = parent_environ;
- execute_by_shell = 0; /* for the next time */
+ char *cmdline = argv[0];
+ /* We don't have a way to pass environment to 'system',
+ so we need to save and restore ours, sigh... */
+ char **parent_environ = environ;
+
+ environ = child->environment;
+
+ /* If we have a *real* shell, tell 'system' to call
+ it to do everything for us. */
+ if (unixy_shell)
+ {
+ /* A *real* shell on MSDOS may not support long
+ command lines the DJGPP way, so we must use 'system'. */
+ cmdline = argv[2]; /* get past "shell -c" */
+ }
+
+ dos_command_running = 1;
+ proc_return = system (cmdline);
+ environ = parent_environ;
+ execute_by_shell = 0; /* for the next time */
}
else
{
- dos_command_running = 1;
- proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
+ dos_command_running = 1;
+ proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
}
/* Need to unblock signals before turning off
@@ -1426,7 +1576,7 @@ start_job_command (struct child *child)
amiga_batch_file = 0;
DeleteFile (amiga_bname); /* Ignore errors. */
}
-#endif /* Amiga */
+#endif /* Amiga */
#ifdef WINDOWS32
{
HANDLE hPID;
@@ -1440,26 +1590,35 @@ start_job_command (struct child *child)
*arg0 = '\\';
/* make sure CreateProcess() has Path it needs */
- sync_Path_environment();
-
- hPID = process_easy(argv, child->environment);
+ sync_Path_environment ();
+
+#ifndef NO_OUTPUT_SYNC
+ /* Divert child output if output_sync in use. Don't capture
+ recursive make output unless we are synchronizing "make" mode. */
+ if (child->output.syncout)
+ hPID = process_easy (argv, child->environment,
+ child->output.out, child->output.err);
+ else
+#endif
+ hPID = process_easy (argv, child->environment, -1, -1);
if (hPID != INVALID_HANDLE_VALUE)
child->pid = (pid_t) hPID;
- else {
- int i;
- unblock_sigs();
- fprintf(stderr,
- _("process_easy() failed to launch process (e=%ld)\n"),
- process_last_err(hPID));
- for (i = 0; argv[i]; i++)
- fprintf(stderr, "%s ", argv[i]);
- fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
- goto error;
- }
+ else
+ {
+ int i;
+ unblock_sigs ();
+ fprintf (stderr,
+ _("process_easy() failed to launch process (e=%ld)\n"),
+ process_last_err (hPID));
+ for (i = 0; argv[i]; i++)
+ fprintf (stderr, "%s ", argv[i]);
+ fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
+ goto error;
+ }
}
#endif /* WINDOWS32 */
-#endif /* __MSDOS__ or Amiga or WINDOWS32 */
+#endif /* __MSDOS__ or Amiga or WINDOWS32 */
/* Bump the number of jobs started in this second. */
++job_counter;
@@ -1475,17 +1634,18 @@ start_job_command (struct child *child)
free (argv);
#endif
+ OUTPUT_UNSET();
return;
error:
- child->file->update_status = 2;
+ child->file->update_status = us_failed;
notice_finished_file (child->file);
- return;
+ OUTPUT_UNSET();
}
/* Try to start a child running.
Returns nonzero if the child was started (and maybe finished), or zero if
- the load was too high and the child was put on the `waiting_jobs' chain. */
+ the load was too high and the child was put on the 'waiting_jobs' chain. */
static int
start_waiting_job (struct child *c)
@@ -1503,9 +1663,9 @@ start_waiting_job (struct child *c)
if (!c->remote
&& ((job_slots_used > 0 && load_too_high ())
#ifdef WINDOWS32
- || (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
+ || (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
#endif
- ))
+ ))
{
/* Put this child on the chain of children waiting for the load average
to go down. */
@@ -1533,7 +1693,7 @@ start_waiting_job (struct child *c)
case cs_not_started:
/* All the command lines turned out to be empty. */
- f->update_status = 0;
+ f->update_status = us_success;
/* FALLTHROUGH */
case cs_finished:
@@ -1549,7 +1709,7 @@ start_waiting_job (struct child *c)
return 1;
}
-/* Create a `struct child' for FILE and start its commands running. */
+/* Create a 'struct child' for FILE and start its commands running. */
void
new_job (struct file *file)
@@ -1569,119 +1729,127 @@ new_job (struct file *file)
/* Chop the commands up into lines if they aren't already. */
chop_commands (cmds);
+ /* Start the command sequence, record it in a new
+ 'struct child', and add that to the chain. */
+
+ c = xcalloc (sizeof (struct child));
+ output_init (&c->output);
+
+ c->file = file;
+ c->sh_batch_file = NULL;
+
+ /* Cache dontcare flag because file->dontcare can be changed once we
+ return. Check dontcare inheritance mechanism for details. */
+ c->dontcare = file->dontcare;
+
+ /* Start saving output in case the expansion uses $(info ...) etc. */
+ OUTPUT_SET (&c->output);
+
/* Expand the command lines and store the results in LINES. */
lines = xmalloc (cmds->ncommand_lines * sizeof (char *));
for (i = 0; i < cmds->ncommand_lines; ++i)
{
/* Collapse backslash-newline combinations that are inside variable
- or function references. These are left alone by the parser so
- that they will appear in the echoing of commands (where they look
- nice); and collapsed by construct_command_argv when it tokenizes.
- But letting them survive inside function invocations loses because
- we don't want the functions to see them as part of the text. */
+ or function references. These are left alone by the parser so
+ that they will appear in the echoing of commands (where they look
+ nice); and collapsed by construct_command_argv when it tokenizes.
+ But letting them survive inside function invocations loses because
+ we don't want the functions to see them as part of the text. */
char *in, *out, *ref;
/* IN points to where in the line we are scanning.
- OUT points to where in the line we are writing.
- When we collapse a backslash-newline combination,
- IN gets ahead of OUT. */
+ OUT points to where in the line we are writing.
+ When we collapse a backslash-newline combination,
+ IN gets ahead of OUT. */
in = out = cmds->command_lines[i];
while ((ref = strchr (in, '$')) != 0)
- {
- ++ref; /* Move past the $. */
-
- if (out != in)
- /* Copy the text between the end of the last chunk
- we processed (where IN points) and the new chunk
- we are about to process (where REF points). */
- memmove (out, in, ref - in);
-
- /* Move both pointers past the boring stuff. */
- out += ref - in;
- in = ref;
-
- if (*ref == '(' || *ref == '{')
- {
- char openparen = *ref;
- char closeparen = openparen == '(' ? ')' : '}';
- int count;
- char *p;
-
- *out++ = *in++; /* Copy OPENPAREN. */
- /* IN now points past the opening paren or brace.
- Count parens or braces until it is matched. */
- count = 0;
- while (*in != '\0')
- {
- if (*in == closeparen && --count < 0)
- break;
- else if (*in == '\\' && in[1] == '\n')
- {
- /* We have found a backslash-newline inside a
- variable or function reference. Eat it and
- any following whitespace. */
-
- int quoted = 0;
- for (p = in - 1; p > ref && *p == '\\'; --p)
- quoted = !quoted;
-
- if (quoted)
- /* There were two or more backslashes, so this is
- not really a continuation line. We don't collapse
- the quoting backslashes here as is done in
- collapse_continuations, because the line will
- be collapsed again after expansion. */
- *out++ = *in++;
- else
- {
- /* Skip the backslash, newline and
- any following whitespace. */
- in = next_token (in + 2);
-
- /* Discard any preceding whitespace that has
- already been written to the output. */
- while (out > ref
- && isblank ((unsigned char)out[-1]))
- --out;
-
- /* Replace it all with a single space. */
- *out++ = ' ';
- }
- }
- else
- {
- if (*in == openparen)
- ++count;
-
- *out++ = *in++;
- }
- }
- }
- }
+ {
+ ++ref; /* Move past the $. */
+
+ if (out != in)
+ /* Copy the text between the end of the last chunk
+ we processed (where IN points) and the new chunk
+ we are about to process (where REF points). */
+ memmove (out, in, ref - in);
+
+ /* Move both pointers past the boring stuff. */
+ out += ref - in;
+ in = ref;
+
+ if (*ref == '(' || *ref == '{')
+ {
+ char openparen = *ref;
+ char closeparen = openparen == '(' ? ')' : '}';
+ char *outref;
+ int count;
+ char *p;
+
+ *out++ = *in++; /* Copy OPENPAREN. */
+ outref = out;
+ /* IN now points past the opening paren or brace.
+ Count parens or braces until it is matched. */
+ count = 0;
+ while (*in != '\0')
+ {
+ if (*in == closeparen && --count < 0)
+ break;
+ else if (*in == '\\' && in[1] == '\n')
+ {
+ /* We have found a backslash-newline inside a
+ variable or function reference. Eat it and
+ any following whitespace. */
+
+ int quoted = 0;
+ for (p = in - 1; p > ref && *p == '\\'; --p)
+ quoted = !quoted;
+
+ if (quoted)
+ /* There were two or more backslashes, so this is
+ not really a continuation line. We don't collapse
+ the quoting backslashes here as is done in
+ collapse_continuations, because the line will
+ be collapsed again after expansion. */
+ *out++ = *in++;
+ else
+ {
+ /* Skip the backslash, newline and
+ any following whitespace. */
+ in = next_token (in + 2);
+
+ /* Discard any preceding whitespace that has
+ already been written to the output. */
+ while (out > outref
+ && isblank ((unsigned char)out[-1]))
+ --out;
+
+ /* Replace it all with a single space. */
+ *out++ = ' ';
+ }
+ }
+ else
+ {
+ if (*in == openparen)
+ ++count;
+
+ *out++ = *in++;
+ }
+ }
+ }
+ }
/* There are no more references in this line to worry about.
- Copy the remaining uninteresting text to the output. */
+ Copy the remaining uninteresting text to the output. */
if (out != in)
- memmove (out, in, strlen (in) + 1);
+ memmove (out, in, strlen (in) + 1);
/* Finally, expand the line. */
lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
- file);
+ file);
}
- /* Start the command sequence, record it in a new
- `struct child', and add that to the chain. */
-
- c = xcalloc (sizeof (struct child));
- c->file = file;
c->command_lines = lines;
- c->sh_batch_file = NULL;
-
- /* Cache dontcare flag because file->dontcare can be changed once we
- return. Check dontcare inheritance mechanism for details. */
- c->dontcare = file->dontcare;
/* Fetch the first command line to be run. */
job_next_command (c);
@@ -1706,12 +1874,18 @@ new_job (struct file *file)
just once). Also more thought needs to go into the entire algorithm;
this is where the old parallel job code waits, so... */
+#ifdef WINDOWS32
+ else if (has_jobserver_semaphore ())
+#else
else if (job_fds[0] >= 0)
+#endif
while (1)
{
+ int got_token;
+#ifndef WINDOWS32
char token;
- int got_token;
- int saved_errno;
+ int saved_errno;
+#endif
DB (DB_JOBS, ("Need a job token; we %shave children\n",
children ? "" : "don't "));
@@ -1720,6 +1894,7 @@ new_job (struct file *file)
if (!jobserver_tokens)
break;
+#ifndef WINDOWS32
/* Read a token. As long as there's no token available we'll block.
We enable interruptible system calls before the read(2) so that if
we get a SIGCHLD while we're waiting, we'll return with EINTR and
@@ -1748,12 +1923,13 @@ new_job (struct file *file)
DB (DB_JOBS, ("Duplicate the job FD\n"));
job_rfd = dup (job_fds[0]);
}
+#endif
/* Reap anything that's currently waiting. */
reap_children (0, 0);
/* Kick off any jobs we have waiting for an opportunity that
- can run now (ie waiting for load). */
+ can run now (i.e., waiting for load). */
start_waiting_jobs ();
/* If our "free" slot has become available, use it; we don't need an
@@ -1766,51 +1942,83 @@ new_job (struct file *file)
if (!children)
fatal (NILF, "INTERNAL: no children as we go to sleep on read\n");
+#ifdef WINDOWS32
+ /* On Windows we simply wait for the jobserver semaphore to become
+ * signalled or one of our child processes to terminate.
+ */
+ got_token = wait_for_semaphore_or_child_process ();
+ if (got_token < 0)
+ {
+ DWORD err = GetLastError ();
+ fatal (NILF, _("semaphore or child process wait: (Error %ld: %s)"),
+ err, map_windows32_error_to_string (err));
+ }
+#else
/* Set interruptible system calls, and read() for a job token. */
- set_child_handler_action_flags (1, waiting_jobs != NULL);
- got_token = read (job_rfd, &token, 1);
- saved_errno = errno;
- set_child_handler_action_flags (0, waiting_jobs != NULL);
+ set_child_handler_action_flags (1, waiting_jobs != NULL);
+ got_token = read (job_rfd, &token, 1);
+ saved_errno = errno;
+ set_child_handler_action_flags (0, waiting_jobs != NULL);
+#endif
/* If we got one, we're done here. */
- if (got_token == 1)
+ if (got_token == 1)
{
DB (DB_JOBS, (_("Obtained token for child %p (%s).\n"),
c, c->file->name));
break;
}
+#ifndef WINDOWS32
/* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,
go back and reap_children(), and try again. */
- errno = saved_errno;
+ errno = saved_errno;
if (errno != EINTR && errno != EBADF)
pfatal_with_name (_("read jobs pipe"));
if (errno == EBADF)
DB (DB_JOBS, ("Read returned EBADF.\n"));
+#endif
}
#endif
++jobserver_tokens;
- /* The job is now primed. Start it running.
- (This will notice if there is in fact no recipe.) */
- if (cmds->fileinfo.filenm)
- DB (DB_BASIC, (_("Invoking recipe from %s:%lu to update target `%s'.\n"),
- cmds->fileinfo.filenm, cmds->fileinfo.lineno,
- c->file->name));
- else
- DB (DB_BASIC, (_("Invoking builtin recipe to update target `%s'.\n"),
- c->file->name));
+ /* Trace the build.
+ Use message here so that changes to working directories are logged. */
+ if (trace_flag)
+ {
+ char *newer = allocated_variable_expand_for_file ("$?", c->file);
+ const char *nm;
+
+ if (! cmds->fileinfo.filenm)
+ nm = _("<builtin>");
+ else
+ {
+ char *n = alloca (strlen (cmds->fileinfo.filenm) + 1 + 11 + 1);
+ sprintf (n, "%s:%lu", cmds->fileinfo.filenm, cmds->fileinfo.lineno);
+ nm = n;
+ }
+
+ if (newer[0] == '\0')
+ message (0, _("%s: target '%s' does not exist"), nm, c->file->name);
+ else
+ message (0, _("%s: update target '%s' due to: %s"), nm,
+ c->file->name, newer);
+ free (newer);
+ }
+ /* The job is now primed. Start it running.
+ (This will notice if there is in fact no recipe.) */
start_waiting_job (c);
if (job_slots == 1 || not_parallel)
/* Since there is only one job slot, make things run linearly.
- Wait for the child to die, setting the state to `cs_finished'. */
+ Wait for the child to die, setting the state to 'cs_finished'. */
while (file->command_state == cs_running)
reap_children (1, 0);
+ OUTPUT_UNSET ();
return;
}
@@ -1824,14 +2032,14 @@ job_next_command (struct child *child)
{
/* There are no more lines in the expansion of this line. */
if (child->command_line == child->file->cmds->ncommand_lines)
- {
- /* There are no more lines to be expanded. */
- child->command_ptr = 0;
- return 0;
- }
+ {
+ /* There are no more lines to be expanded. */
+ child->command_ptr = 0;
+ return 0;
+ }
else
- /* Get the next line to run. */
- child->command_ptr = child->command_lines[child->command_line++];
+ /* Get the next line to run. */
+ child->command_ptr = child->command_lines[child->command_line++];
}
return 1;
}
@@ -1903,14 +2111,14 @@ load_too_high (void)
static int lossage = -1;
/* Complain only once for the same error. */
if (lossage == -1 || errno != lossage)
- {
- if (errno == 0)
- /* An errno value of zero means getloadavg is just unsupported. */
- error (NILF,
+ {
+ if (errno == 0)
+ /* An errno value of zero means getloadavg is just unsupported. */
+ error (NILF,
_("cannot enforce load limits on this operating system"));
- else
- perror_with_name (_("cannot enforce load limit: "), "getloadavg");
- }
+ else
+ perror_with_name (_("cannot enforce load limit: "), "getloadavg");
+ }
lossage = errno;
load = 0;
}
@@ -1960,7 +2168,7 @@ start_waiting_jobs (void)
waiting_jobs = job->next;
/* Try to start that job. We break out of the loop as soon
- as start_waiting_job puts one back on the waiting list. */
+ as start_waiting_job puts one back on the waiting list. */
}
while (start_waiting_job (job) && waiting_jobs != 0);
@@ -1972,59 +2180,82 @@ start_waiting_jobs (void)
/* EMX: Start a child process. This function returns the new pid. */
# if defined __EMX__
int
-child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
+child_execute_job (int stdin_fd, int stdout_fd, int stderr_fd,
+ char **argv, char **envp)
{
int pid;
- /* stdin_fd == 0 means: nothing to do for stdin;
- stdout_fd == 1 means: nothing to do for stdout */
- int save_stdin = (stdin_fd != 0) ? dup (0) : 0;
- int save_stdout = (stdout_fd != 1) ? dup (1): 1;
-
- /* < 0 only if dup() failed */
- if (save_stdin < 0)
- fatal (NILF, _("no more file handles: could not duplicate stdin\n"));
- if (save_stdout < 0)
- fatal (NILF, _("no more file handles: could not duplicate stdout\n"));
-
- /* Close unnecessary file handles for the child. */
- if (save_stdin != 0)
- CLOSE_ON_EXEC (save_stdin);
- if (save_stdout != 1)
- CLOSE_ON_EXEC (save_stdout);
-
- /* Connect the pipes to the child process. */
- if (stdin_fd != 0)
- (void) dup2 (stdin_fd, 0);
- if (stdout_fd != 1)
- (void) dup2 (stdout_fd, 1);
-
- /* stdin_fd and stdout_fd must be closed on exit because we are
- still in the parent process */
- if (stdin_fd != 0)
- CLOSE_ON_EXEC (stdin_fd);
- if (stdout_fd != 1)
- CLOSE_ON_EXEC (stdout_fd);
+ int save_stdin = -1;
+ int save_stdout = -1;
+ int save_stderr = -1;
+
+ /* For each FD which needs to be redirected first make a dup of the standard
+ FD to save and mark it close on exec so our child won't see it. Then
+ dup2() the standard FD to the redirect FD, and also mark the redirect FD
+ as close on exec. */
+ if (stdin_fd != FD_STDIN)
+ {
+ save_stdin = dup (FD_STDIN);
+ if (save_stdin < 0)
+ fatal (NILF, _("no more file handles: could not duplicate stdin\n"));
+ CLOSE_ON_EXEC (save_stdin);
+
+ dup2 (stdin_fd, FD_STDIN);
+ CLOSE_ON_EXEC (stdin_fd);
+ }
+
+ if (stdout_fd != FD_STDOUT)
+ {
+ save_stdout = dup (FD_STDOUT);
+ if (save_stdout < 0)
+ fatal (NILF, _("no more file handles: could not duplicate stdout\n"));
+ CLOSE_ON_EXEC (save_stdout);
+
+ dup2 (stdout_fd, FD_STDOUT);
+ CLOSE_ON_EXEC (stdout_fd);
+ }
+
+ if (stderr_fd != FD_STDERR)
+ {
+ if (stderr_fd != stdout_fd)
+ {
+ save_stderr = dup (FD_STDERR);
+ if (save_stderr < 0)
+ fatal (NILF, _("no more file handles: could not duplicate stderr\n"));
+ CLOSE_ON_EXEC (save_stderr);
+ }
+
+ dup2 (stderr_fd, FD_STDERR);
+ CLOSE_ON_EXEC (stderr_fd);
+ }
/* Run the command. */
pid = exec_command (argv, envp);
- /* Restore stdout/stdin of the parent and close temporary FDs. */
- if (stdin_fd != 0)
+ /* Restore stdout/stdin/stderr of the parent and close temporary FDs. */
+ if (save_stdin >= 0)
{
- if (dup2 (save_stdin, 0) != 0)
+ if (dup2 (save_stdin, FD_STDIN) != FD_STDIN)
fatal (NILF, _("Could not restore stdin\n"));
else
close (save_stdin);
}
- if (stdout_fd != 1)
+ if (save_stdout >= 0)
{
- if (dup2 (save_stdout, 1) != 1)
+ if (dup2 (save_stdout, FD_STDOUT) != FD_STDOUT)
fatal (NILF, _("Could not restore stdout\n"));
else
close (save_stdout);
}
+ if (save_stderr >= 0)
+ {
+ if (dup2 (save_stderr, FD_STDERR) != FD_STDERR)
+ fatal (NILF, _("Could not restore stderr\n"));
+ else
+ close (save_stderr);
+ }
+
return pid;
}
@@ -2032,19 +2263,28 @@ child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
/* UNIX:
Replace the current process with one executing the command in ARGV.
- STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
- the environment of the new program. This function does not return. */
+ STDIN_FD/STDOUT_FD/STDERR_FD are used as the process's stdin/stdout/stderr;
+ ENVP is the environment of the new program. This function does not return. */
void
-child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
+child_execute_job (int stdin_fd, int stdout_fd, int stderr_fd,
+ char **argv, char **envp)
{
- if (stdin_fd != 0)
- (void) dup2 (stdin_fd, 0);
- if (stdout_fd != 1)
- (void) dup2 (stdout_fd, 1);
- if (stdin_fd != 0)
- (void) close (stdin_fd);
- if (stdout_fd != 1)
- (void) close (stdout_fd);
+ /* For any redirected FD, dup2() it to the standard FD then close it. */
+ if (stdin_fd != FD_STDIN)
+ {
+ dup2 (stdin_fd, FD_STDIN);
+ close (stdin_fd);
+ }
+
+ if (stdout_fd != FD_STDOUT)
+ dup2 (stdout_fd, FD_STDOUT);
+ if (stderr_fd != FD_STDERR)
+ dup2 (stderr_fd, FD_STDERR);
+
+ if (stdout_fd != FD_STDOUT)
+ close (stdout_fd);
+ if (stderr_fd != FD_STDERR && stderr_fd != stdout_fd)
+ close (stderr_fd);
/* Run the command. */
exec_command (argv, envp);
@@ -2077,61 +2317,59 @@ exec_command (char **argv, char **envp)
#ifdef WINDOWS32
HANDLE hPID;
HANDLE hWaitPID;
- int err = 0;
int exit_code = EXIT_FAILURE;
/* make sure CreateProcess() has Path it needs */
- sync_Path_environment();
+ sync_Path_environment ();
/* launch command */
- hPID = process_easy(argv, envp);
+ hPID = process_easy (argv, envp, -1, -1);
/* make sure launch ok */
if (hPID == INVALID_HANDLE_VALUE)
{
int i;
- fprintf(stderr,
- _("process_easy() failed to launch process (e=%ld)\n"),
- process_last_err(hPID));
+ fprintf (stderr, _("process_easy() failed to launch process (e=%ld)\n"),
+ process_last_err (hPID));
for (i = 0; argv[i]; i++)
- fprintf(stderr, "%s ", argv[i]);
- fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
- exit(EXIT_FAILURE);
+ fprintf (stderr, "%s ", argv[i]);
+ fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);
+ exit (EXIT_FAILURE);
}
/* wait and reap last child */
- hWaitPID = process_wait_for_any();
+ hWaitPID = process_wait_for_any (1, 0);
while (hWaitPID)
{
/* was an error found on this process? */
- err = process_last_err(hWaitPID);
+ int err = process_last_err (hWaitPID);
/* get exit data */
- exit_code = process_exit_code(hWaitPID);
+ exit_code = process_exit_code (hWaitPID);
if (err)
- fprintf(stderr, "make (e=%d, rc=%d): %s",
- err, exit_code, map_windows32_error_to_string(err));
+ fprintf (stderr, "make (e=%d, rc=%d): %s",
+ err, exit_code, map_windows32_error_to_string (err));
/* cleanup process */
- process_cleanup(hWaitPID);
+ process_cleanup (hWaitPID);
/* expect to find only last pid, warn about other pids reaped */
if (hWaitPID == hPID)
break;
else
- {
- char *pidstr = xstrdup (pid2str ((pid_t)hWaitPID));
-
- fprintf(stderr,
- _("make reaped child pid %s, still waiting for pid %s\n"),
- pidstr, pid2str ((pid_t)hPID));
- free (pidstr);
- }
+ {
+ char *pidstr = xstrdup (pid2str ((pid_t)hWaitPID));
+
+ fprintf (stderr,
+ _("make reaped child pid %s, still waiting for pid %s\n"),
+ pidstr, pid2str ((pid_t)hPID));
+ free (pidstr);
+ }
}
/* return child's exit code as our exit code */
- exit(exit_code);
+ exit (exit_code);
#else /* !WINDOWS32 */
@@ -2143,10 +2381,8 @@ exec_command (char **argv, char **envp)
child_access ();
# ifdef __EMX__
-
/* Run the program. */
pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
-
if (pid >= 0)
return pid;
@@ -2155,7 +2391,6 @@ exec_command (char **argv, char **envp)
errno = ENOEXEC;
# else
-
/* Run the program. */
environ = envp;
execvp (argv[0], argv);
@@ -2169,37 +2404,37 @@ exec_command (char **argv, char **envp)
break;
case ENOEXEC:
{
- /* The file is not executable. Try it as a shell script. */
- extern char *getenv ();
- char *shell;
- char **new_argv;
- int argc;
+ /* The file is not executable. Try it as a shell script. */
+ extern char *getenv ();
+ char *shell;
+ char **new_argv;
+ int argc;
int i=1;
# ifdef __EMX__
/* Do not use $SHELL from the environment */
- struct variable *p = lookup_variable ("SHELL", 5);
- if (p)
- shell = p->value;
+ struct variable *p = lookup_variable ("SHELL", 5);
+ if (p)
+ shell = p->value;
else
shell = 0;
# else
- shell = getenv ("SHELL");
+ shell = getenv ("SHELL");
# endif
- if (shell == 0)
- shell = default_shell;
+ if (shell == 0)
+ shell = default_shell;
- argc = 1;
- while (argv[argc] != 0)
- ++argc;
+ argc = 1;
+ while (argv[argc] != 0)
+ ++argc;
# ifdef __EMX__
if (!unixy_shell)
++argc;
# endif
- new_argv = alloca ((1 + argc + 1) * sizeof (char *));
- new_argv[0] = shell;
+ new_argv = alloca ((1 + argc + 1) * sizeof (char *));
+ new_argv[0] = shell;
# ifdef __EMX__
if (!unixy_shell)
@@ -2211,24 +2446,24 @@ exec_command (char **argv, char **envp)
# endif
new_argv[i] = argv[0];
- while (argc > 0)
- {
- new_argv[i + argc] = argv[argc];
- --argc;
- }
+ while (argc > 0)
+ {
+ new_argv[i + argc] = argv[argc];
+ --argc;
+ }
# ifdef __EMX__
- pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
- if (pid >= 0)
+ pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
+ if (pid >= 0)
break;
# else
- execvp (shell, new_argv);
+ execvp (shell, new_argv);
# endif
- if (errno == ENOENT)
- error (NILF, _("%s: Shell program not found"), shell);
- else
- perror_with_name ("execvp: ", shell);
- break;
+ if (errno == ENOENT)
+ error (NILF, _("%s: Shell program not found"), shell);
+ else
+ perror_with_name ("execvp: ", shell);
+ break;
}
# ifdef __EMX__
@@ -2267,7 +2502,7 @@ void clean_tmp (void)
#ifndef VMS
/* Figure out the argument list necessary to run LINE as a command. Try to
avoid using a shell. This routine handles only ' quoting, and " quoting
- when no backslash, $ or ` characters are seen in the quotes. Starting
+ when no backslash, $ or ' characters are seen in the quotes. Starting
quotes may be escaped with a backslash. If any of the characters in
sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
is the first word of a line, the shell is used.
@@ -2286,26 +2521,26 @@ void clean_tmp (void)
static char **
construct_command_argv_internal (char *line, char **restp, char *shell,
char *shellflags, char *ifs, int flags,
- char **batch_filename_ptr)
+ char **batch_filename UNUSED)
{
#ifdef __MSDOS__
/* MSDOS supports both the stock DOS shell and ports of Unixy shells.
- We call `system' for anything that requires ``slow'' processing,
+ We call 'system' for anything that requires ''slow'' processing,
because DOS shells are too dumb. When $SHELL points to a real
- (unix-style) shell, `system' just calls it to do everything. When
- $SHELL points to a DOS shell, `system' does most of the work
+ (unix-style) shell, 'system' just calls it to do everything. When
+ $SHELL points to a DOS shell, 'system' does most of the work
internally, calling the shell only for its internal commands.
However, it looks on the $PATH first, so you can e.g. have an
- external command named `mkdir'.
+ external command named 'mkdir'.
- Since we call `system', certain characters and commands below are
+ Since we call 'system', certain characters and commands below are
actually not specific to COMMAND.COM, but to the DJGPP implementation
- of `system'. In particular:
+ of 'system'. In particular:
The shell wildcard characters are in DOS_CHARS because they will
- not be expanded if we call the child via `spawnXX'.
+ not be expanded if we call the child via 'spawnXX'.
- The `;' is in DOS_CHARS, because our `system' knows how to run
+ The ';' is in DOS_CHARS, because our 'system' knows how to run
multiple commands on a single line.
DOS_CHARS also include characters special to 4DOS/NDOS, so we
@@ -2313,19 +2548,19 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
commands and special characters. */
static char sh_chars_dos[] = "*?[];|<>%^&()";
static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
- "copy", "ctty", "date", "del", "dir", "echo",
- "erase", "exit", "for", "goto", "if", "md",
- "mkdir", "path", "pause", "prompt", "rd",
- "rmdir", "rem", "ren", "rename", "set",
- "shift", "time", "type", "ver", "verify",
- "vol", ":", 0 };
+ "copy", "ctty", "date", "del", "dir", "echo",
+ "erase", "exit", "for", "goto", "if", "md",
+ "mkdir", "path", "pause", "prompt", "rd",
+ "rmdir", "rem", "ren", "rename", "set",
+ "shift", "time", "type", "ver", "verify",
+ "vol", ":", 0 };
static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
static char *sh_cmds_sh[] = { "cd", "echo", "eval", "exec", "exit", "login",
- "logout", "set", "umask", "wait", "while",
- "for", "case", "if", ":", ".", "break",
- "continue", "export", "read", "readonly",
- "shift", "times", "trap", "switch", "unset",
+ "logout", "set", "umask", "wait", "while",
+ "for", "case", "if", ":", ".", "break",
+ "continue", "export", "read", "readonly",
+ "shift", "times", "trap", "switch", "unset",
"ulimit", 0 };
char *sh_chars;
@@ -2333,28 +2568,28 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
#elif defined (__EMX__)
static char sh_chars_dos[] = "*?[];|<>%^&()";
static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
- "copy", "ctty", "date", "del", "dir", "echo",
- "erase", "exit", "for", "goto", "if", "md",
- "mkdir", "path", "pause", "prompt", "rd",
- "rmdir", "rem", "ren", "rename", "set",
- "shift", "time", "type", "ver", "verify",
- "vol", ":", 0 };
+ "copy", "ctty", "date", "del", "dir", "echo",
+ "erase", "exit", "for", "goto", "if", "md",
+ "mkdir", "path", "pause", "prompt", "rd",
+ "rmdir", "rem", "ren", "rename", "set",
+ "shift", "time", "type", "ver", "verify",
+ "vol", ":", 0 };
static char sh_chars_os2[] = "*?[];|<>%^()\"'&";
static char *sh_cmds_os2[] = { "call", "cd", "chcp", "chdir", "cls", "copy",
- "date", "del", "detach", "dir", "echo",
- "endlocal", "erase", "exit", "for", "goto", "if",
- "keys", "md", "mkdir", "move", "path", "pause",
- "prompt", "rd", "rem", "ren", "rename", "rmdir",
- "set", "setlocal", "shift", "start", "time",
+ "date", "del", "detach", "dir", "echo",
+ "endlocal", "erase", "exit", "for", "goto", "if",
+ "keys", "md", "mkdir", "move", "path", "pause",
+ "prompt", "rd", "rem", "ren", "rename", "rmdir",
+ "set", "setlocal", "shift", "start", "time",
"type", "ver", "verify", "vol", ":", 0 };
static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~'";
static char *sh_cmds_sh[] = { "echo", "cd", "eval", "exec", "exit", "login",
- "logout", "set", "umask", "wait", "while",
- "for", "case", "if", ":", ".", "break",
- "continue", "export", "read", "readonly",
- "shift", "times", "trap", "switch", "unset",
+ "logout", "set", "umask", "wait", "while",
+ "for", "case", "if", ":", ".", "break",
+ "continue", "export", "read", "readonly",
+ "shift", "times", "trap", "switch", "unset",
0 };
char *sh_chars;
char **sh_cmds;
@@ -2362,27 +2597,34 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
#elif defined (_AMIGA)
static char sh_chars[] = "#;\"|<>()?*$`";
static char *sh_cmds[] = { "cd", "eval", "if", "delete", "echo", "copy",
- "rename", "set", "setenv", "date", "makedir",
- "skip", "else", "endif", "path", "prompt",
- "unset", "unsetenv", "version",
- 0 };
+ "rename", "set", "setenv", "date", "makedir",
+ "skip", "else", "endif", "path", "prompt",
+ "unset", "unsetenv", "version",
+ 0 };
#elif defined (WINDOWS32)
- static char sh_chars_dos[] = "\"|&<>";
+ /* We used to have a double quote (") in sh_chars_dos[] below, but
+ that caused any command line with quoted file names be run
+ through a temporary batch file, which introduces command-line
+ limit of 4K charcaters imposed by cmd.exe. Since CreateProcess
+ can handle quoted file names just fine, removing the quote lifts
+ the limit from a very frequent use case, because using quoted
+ file names is commonplace on MS-Windows. */
+ static char sh_chars_dos[] = "|&<>";
static char *sh_cmds_dos[] = { "assoc", "break", "call", "cd", "chcp",
- "chdir", "cls", "color", "copy", "ctty",
- "date", "del", "dir", "echo", "echo.",
- "endlocal", "erase", "exit", "for", "ftype",
- "goto", "if", "if", "md", "mkdir", "path",
- "pause", "prompt", "rd", "rem", "ren",
- "rename", "rmdir", "set", "setlocal",
- "shift", "time", "title", "type", "ver",
- "verify", "vol", ":", 0 };
+ "chdir", "cls", "color", "copy", "ctty",
+ "date", "del", "dir", "echo", "echo.",
+ "endlocal", "erase", "exit", "for", "ftype",
+ "goto", "if", "if", "md", "mkdir", "move",
+ "path", "pause", "prompt", "rd", "rem", "ren",
+ "rename", "rmdir", "set", "setlocal",
+ "shift", "time", "title", "type", "ver",
+ "verify", "vol", ":", 0 };
static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
static char *sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login",
- "logout", "set", "umask", "wait", "while", "for",
- "case", "if", ":", ".", "break", "continue",
- "export", "read", "readonly", "shift", "times",
- "trap", "switch", "test",
+ "logout", "set", "umask", "wait", "while", "for",
+ "case", "if", ":", ".", "break", "continue",
+ "export", "read", "readonly", "shift", "times",
+ "trap", "switch", "test",
#ifdef BATCH_MODE_ONLY_SHELL
"echo",
#endif
@@ -2404,7 +2646,7 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
WINDOWS32) are compiled with HAVE_DOS_PATHS defined, which uses
sh_chars_sh[] directly (see below). */
static char *sh_chars_sh = sh_chars;
-# endif /* HAVE_DOS_PATHS */
+# endif /* HAVE_DOS_PATHS */
#endif
int i;
char *p;
@@ -2416,13 +2658,16 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
#ifdef WINDOWS32
int slow_flag = 0;
- if (!unixy_shell) {
- sh_cmds = sh_cmds_dos;
- sh_chars = sh_chars_dos;
- } else {
- sh_cmds = sh_cmds_sh;
- sh_chars = sh_chars_sh;
- }
+ if (!unixy_shell)
+ {
+ sh_cmds = sh_cmds_dos;
+ sh_chars = sh_chars_dos;
+ }
+ else
+ {
+ sh_cmds = sh_cmds_sh;
+ sh_chars = sh_chars_sh;
+ }
#endif /* WINDOWS32 */
if (restp != NULL)
@@ -2434,6 +2679,9 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
if (*line == '\0')
return 0;
+ if (shellflags == 0)
+ shellflags = posix_pedantic ? "-ec" : "-c";
+
/* See if it is safe to parse commands internally. */
if (shell == 0)
shell = default_shell;
@@ -2458,7 +2706,7 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
{
extern int _is_unixy_shell (const char *_path);
- DB (DB_BASIC, (_("$SHELL changed (was `%s', now `%s')\n"),
+ DB (DB_BASIC, (_("$SHELL changed (was '%s', now '%s')\n"),
default_shell, shell));
unixy_shell = _is_unixy_shell (shell);
/* we must allocate a copy of shell: construct_command_argv() will free
@@ -2491,7 +2739,7 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
if (ifs != 0)
for (ap = ifs; *ap != '\0'; ++ap)
if (*ap != ' ' && *ap != '\t' && *ap != '\n')
- goto slow;
+ goto slow;
if (shellflags != 0)
if (shellflags[0] != '-'
@@ -2516,28 +2764,27 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
assert (ap <= end);
if (instring)
- {
- /* Inside a string, just copy any char except a closing quote
- or a backslash-newline combination. */
- if (*p == instring)
- {
- instring = 0;
- if (ap == new_argv[0] || *(ap-1) == '\0')
- last_argument_was_empty = 1;
- }
- else if (*p == '\\' && p[1] == '\n')
+ {
+ /* Inside a string, just copy any char except a closing quote
+ or a backslash-newline combination. */
+ if (*p == instring)
+ {
+ instring = 0;
+ if (ap == new_argv[0] || *(ap-1) == '\0')
+ last_argument_was_empty = 1;
+ }
+ else if (*p == '\\' && p[1] == '\n')
{
/* Backslash-newline is handled differently depending on what
kind of string we're in: inside single-quoted strings you
- keep them; in double-quoted strings they disappear.
- For DOS/Windows/OS2, if we don't have a POSIX shell,
- we keep the pre-POSIX behavior of removing the
- backslash-newline. */
+ keep them; in double-quoted strings they disappear. For
+ DOS/Windows/OS2, if we don't have a POSIX shell, we keep the
+ pre-POSIX behavior of removing the backslash-newline. */
if (instring == '"'
#if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
- || !unixy_shell
+ || !unixy_shell
#endif
- )
+ )
++p;
else
{
@@ -2545,61 +2792,74 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
*(ap++) = *p;
}
}
- else if (*p == '\n' && restp != NULL)
- {
- /* End of the command line. */
- *restp = p;
- goto end_of_line;
- }
- /* Backslash, $, and ` are special inside double quotes.
- If we see any of those, punt.
- But on MSDOS, if we use COMMAND.COM, double and single
- quotes have the same effect. */
- else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
- goto slow;
- else
- *ap++ = *p;
- }
+ else if (*p == '\n' && restp != NULL)
+ {
+ /* End of the command line. */
+ *restp = p;
+ goto end_of_line;
+ }
+ /* Backslash, $, and ` are special inside double quotes.
+ If we see any of those, punt.
+ But on MSDOS, if we use COMMAND.COM, double and single
+ quotes have the same effect. */
+ else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
+ goto slow;
+#ifdef WINDOWS32
+ else if (instring == '"' && strncmp (p, "\\\"", 2) == 0)
+ *ap++ = *++p;
+#endif
+ else
+ *ap++ = *p;
+ }
else if (strchr (sh_chars, *p) != 0)
- /* Not inside a string, but it's a special char. */
- goto slow;
+ /* Not inside a string, but it's a special char. */
+ goto slow;
else if (one_shell && *p == '\n')
- /* In .ONESHELL mode \n is a separator like ; or && */
- goto slow;
+ /* In .ONESHELL mode \n is a separator like ; or && */
+ goto slow;
#ifdef __MSDOS__
else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
- /* `...' is a wildcard in DJGPP. */
- goto slow;
+ /* '...' is a wildcard in DJGPP. */
+ goto slow;
#endif
else
- /* Not a special char. */
- switch (*p)
- {
- case '=':
- /* Equals is a special character in leading words before the
- first word with no equals sign in it. This is not the case
- with sh -k, but we never get here when using nonstandard
- shell flags. */
- if (! seen_nonequals && unixy_shell)
- goto slow;
- word_has_equals = 1;
- *ap++ = '=';
- break;
-
- case '\\':
- /* Backslash-newline has special case handling, ref POSIX.
+ /* Not a special char. */
+ switch (*p)
+ {
+ case '=':
+ /* Equals is a special character in leading words before the
+ first word with no equals sign in it. This is not the case
+ with sh -k, but we never get here when using nonstandard
+ shell flags. */
+ if (! seen_nonequals && unixy_shell)
+ goto slow;
+ word_has_equals = 1;
+ *ap++ = '=';
+ break;
+
+ case '\\':
+ /* Backslash-newline has special case handling, ref POSIX.
We're in the fastpath, so emulate what the shell would do. */
- if (p[1] == '\n')
- {
- /* Throw out the backslash and newline. */
+ if (p[1] == '\n')
+ {
+ /* Throw out the backslash and newline. */
++p;
/* If there's nothing in this argument yet, skip any
whitespace before the start of the next word. */
if (ap == new_argv[i])
p = next_token (p + 1) - 1;
- }
- else if (p[1] != '\0')
+ }
+#ifdef WINDOWS32
+ /* Backslash before whitespace is not special if our shell
+ is not Unixy. */
+ else if (isspace (p[1]) && !unixy_shell)
+ {
+ *ap++ = *p;
+ break;
+ }
+#endif
+ else if (p[1] != '\0')
{
#ifdef HAVE_DOS_PATHS
/* Only remove backslashes before characters special to Unixy
@@ -2629,69 +2889,69 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
/* Copy and skip the following char. */
*ap++ = *++p;
}
- break;
-
- case '\'':
- case '"':
- instring = *p;
- break;
-
- case '\n':
- if (restp != NULL)
- {
- /* End of the command line. */
- *restp = p;
- goto end_of_line;
- }
- else
- /* Newlines are not special. */
- *ap++ = '\n';
- break;
-
- case ' ':
- case '\t':
- /* We have the end of an argument.
- Terminate the text of the argument. */
- *ap++ = '\0';
- new_argv[++i] = ap;
- last_argument_was_empty = 0;
-
- /* Update SEEN_NONEQUALS, which tells us if every word
- heretofore has contained an `='. */
- seen_nonequals |= ! word_has_equals;
- if (word_has_equals && ! seen_nonequals)
- /* An `=' in a word before the first
- word without one is magical. */
- goto slow;
- word_has_equals = 0; /* Prepare for the next word. */
-
- /* If this argument is the command name,
- see if it is a built-in shell command.
- If so, have the shell handle it. */
- if (i == 1)
- {
- register int j;
- for (j = 0; sh_cmds[j] != 0; ++j)
+ break;
+
+ case '\'':
+ case '"':
+ instring = *p;
+ break;
+
+ case '\n':
+ if (restp != NULL)
+ {
+ /* End of the command line. */
+ *restp = p;
+ goto end_of_line;
+ }
+ else
+ /* Newlines are not special. */
+ *ap++ = '\n';
+ break;
+
+ case ' ':
+ case '\t':
+ /* We have the end of an argument.
+ Terminate the text of the argument. */
+ *ap++ = '\0';
+ new_argv[++i] = ap;
+ last_argument_was_empty = 0;
+
+ /* Update SEEN_NONEQUALS, which tells us if every word
+ heretofore has contained an '='. */
+ seen_nonequals |= ! word_has_equals;
+ if (word_has_equals && ! seen_nonequals)
+ /* An '=' in a word before the first
+ word without one is magical. */
+ goto slow;
+ word_has_equals = 0; /* Prepare for the next word. */
+
+ /* If this argument is the command name,
+ see if it is a built-in shell command.
+ If so, have the shell handle it. */
+ if (i == 1)
+ {
+ register int j;
+ for (j = 0; sh_cmds[j] != 0; ++j)
{
if (streq (sh_cmds[j], new_argv[0]))
goto slow;
-# ifdef __EMX__
+#if defined(__EMX__) || defined(WINDOWS32)
/* Non-Unix shells are case insensitive. */
if (!unixy_shell
&& strcasecmp (sh_cmds[j], new_argv[0]) == 0)
goto slow;
-# endif
+#endif
}
- }
+ }
- /* Ignore multiple whitespace chars. */
- p = next_token (p) - 1;
- break;
+ /* Ignore multiple whitespace chars. */
+ p = next_token (p) - 1;
+ break;
- default:
- *ap++ = *p;
- break;
- }
+ default:
+ *ap++ = *p;
+ break;
+ }
}
end_of_line:
@@ -2710,8 +2970,8 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
{
register int j;
for (j = 0; sh_cmds[j] != 0; ++j)
- if (streq (sh_cmds[j], new_argv[0]))
- goto slow;
+ if (streq (sh_cmds[j], new_argv[0]))
+ goto slow;
}
if (new_argv[0] == 0)
@@ -2735,7 +2995,7 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
}
#ifdef __MSDOS__
- execute_by_shell = 1; /* actually, call `system' if shell isn't unixy */
+ execute_by_shell = 1; /* actually, call 'system' if shell isn't unixy */
#endif
#ifdef _AMIGA
@@ -2750,14 +3010,14 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
for (dptr=buffer; *ptr; )
{
if (*ptr == '\\' && ptr[1] == '\n')
- ptr += 2;
+ ptr += 2;
else if (*ptr == '@') /* Kludge: multiline commands */
{
- ptr += 2;
- *dptr++ = '\n';
+ ptr += 2;
+ *dptr++ = '\n';
}
else
- *dptr++ = *ptr++;
+ *dptr++ = *ptr++;
}
*dptr = 0;
@@ -2765,7 +3025,7 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
new_argv[0] = buffer;
new_argv[1] = 0;
}
-#else /* Not Amiga */
+#else /* Not Amiga */
#ifdef WINDOWS32
/*
* Not eating this whitespace caused things like
@@ -2790,37 +3050,44 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
Then recurse, expanding this command line to get the final
argument list. */
+ char *new_line;
unsigned int shell_len = strlen (shell);
unsigned int line_len = strlen (line);
- unsigned int sflags_len = strlen (shellflags);
+ unsigned int sflags_len = shellflags ? strlen (shellflags) : 0;
+#ifdef WINDOWS32
char *command_ptr = NULL; /* used for batch_mode_shell mode */
- char *new_line;
+#endif
# ifdef __EMX__ /* is this necessary? */
- if (!unixy_shell)
+ if (!unixy_shell && shellflags)
shellflags[0] = '/'; /* "/c" */
# endif
/* In .ONESHELL mode we are allowed to throw the entire current
- recipe string at a single shell and trust that the user
- has configured the shell and shell flags, and formatted
- the string, appropriately. */
+ recipe string at a single shell and trust that the user
+ has configured the shell and shell flags, and formatted
+ the string, appropriately. */
if (one_shell)
{
- /* If the shell is Bourne compatible, we must remove and ignore
- interior special chars [@+-] because they're meaningless to
- the shell itself. If, however, we're in .ONESHELL mode and
- have changed SHELL to something non-standard, we should
- leave those alone because they could be part of the
- script. In this case we must also leave in place
- any leading [@+-] for the same reason. */
-
- /* Remove and ignore interior prefix chars [@+-] because they're
- meaningless given a single shell. */
+ /* If the shell is Bourne compatible, we must remove and ignore
+ interior special chars [@+-] because they're meaningless to
+ the shell itself. If, however, we're in .ONESHELL mode and
+ have changed SHELL to something non-standard, we should
+ leave those alone because they could be part of the
+ script. In this case we must also leave in place
+ any leading [@+-] for the same reason. */
+
+ /* Remove and ignore interior prefix chars [@+-] because they're
+ meaningless given a single shell. */
#if defined __MSDOS__ || defined (__EMX__)
- if (unixy_shell) /* the test is complicated and we already did it */
+ if (unixy_shell) /* the test is complicated and we already did it */
#else
- if (is_bourne_compatible_shell(shell))
+ if (is_bourne_compatible_shell (shell)
+#ifdef WINDOWS32
+ /* If we didn't find any sh.exe, don't behave is if we did! */
+ && !no_default_sh_exe
+#endif
+ )
#endif
{
const char *f = line;
@@ -2856,35 +3123,136 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
}
*t = '\0';
}
+#ifdef WINDOWS32
+ else /* non-Posix shell (cmd.exe etc.) */
+ {
+ const char *f = line;
+ char *t = line;
+ char *tstart = t;
+ int temp_fd;
+ FILE* batch = NULL;
+ int id = GetCurrentProcessId ();
+ PATH_VAR(fbuf);
+
+ /* Generate a file name for the temporary batch file. */
+ sprintf (fbuf, "make%d", id);
+ *batch_filename = create_batch_file (fbuf, 0, &temp_fd);
+ DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
+ *batch_filename));
+
+ /* Create a FILE object for the batch file, and write to it the
+ commands to be executed. Put the batch file in TEXT mode. */
+ _setmode (temp_fd, _O_TEXT);
+ batch = _fdopen (temp_fd, "wt");
+ fputs ("@echo off\n", batch);
+ DB (DB_JOBS, (_("Batch file contents:\n\t@echo off\n")));
+
+ /* Copy the recipe, removing and ignoring interior prefix chars
+ [@+-]: they're meaningless in .ONESHELL mode. */
+ while (*f != '\0')
+ {
+ /* This is the start of a new recipe line.
+ Skip whitespace and prefix characters. */
+ while (isblank (*f) || *f == '-' || *f == '@' || *f == '+')
+ ++f;
+
+ /* Copy until we get to the next logical recipe line. */
+ while (*f != '\0')
+ {
+ /* Remove the escaped newlines in the command, and
+ the whitespace that follows them. Windows
+ shells cannot handle escaped newlines. */
+ if (*f == '\\' && f[1] == '\n')
+ {
+ f += 2;
+ while (isblank (*f))
+ ++f;
+ }
+ *(t++) = *(f++);
+ /* On an unescaped newline, we're done with this
+ line. */
+ if (f[-1] == '\n')
+ break;
+ }
+ /* Write another line into the batch file. */
+ if (t > tstart)
+ {
+ int c = *t;
+ *t = '\0';
+ fputs (tstart, batch);
+ DB (DB_JOBS, ("\t%s", tstart));
+ tstart = t;
+ *t = c;
+ }
+ }
+ DB (DB_JOBS, ("\n"));
+ fclose (batch);
+
+ /* Create an argv list for the shell command line that
+ will run the batch file. */
+ new_argv = xmalloc (2 * sizeof (char *));
+ new_argv[0] = xstrdup (*batch_filename);
+ new_argv[1] = NULL;
+ return new_argv;
+ }
+#endif /* WINDOWS32 */
+ /* Create an argv list for the shell command line. */
+ {
+ int n = 0;
- new_argv = xmalloc (4 * sizeof (char *));
- new_argv[0] = xstrdup(shell);
- new_argv[1] = xstrdup(shellflags);
- new_argv[2] = line;
- new_argv[3] = NULL;
- return new_argv;
+ new_argv = xmalloc ((4 + sflags_len/2) * sizeof (char *));
+ new_argv[n++] = xstrdup (shell);
+
+ /* Chop up the shellflags (if any) and assign them. */
+ if (! shellflags)
+ new_argv[n++] = xstrdup ("");
+ else
+ {
+ const char *s = shellflags;
+ char *t;
+ unsigned int len;
+ while ((t = find_next_token (&s, &len)) != 0)
+ new_argv[n++] = xstrndup (t, len);
+ }
+
+ /* Set the command to invoke. */
+ new_argv[n++] = line;
+ new_argv[n++] = NULL;
+ }
+ return new_argv;
}
- new_line = alloca (shell_len + 1 + sflags_len + 1
- + (line_len*2) + 1);
+ new_line = xmalloc ((shell_len*2) + 1 + sflags_len + 1
+ + (line_len*2) + 1);
ap = new_line;
- memcpy (ap, shell, shell_len);
- ap += shell_len;
+ /* Copy SHELL, escaping any characters special to the shell. If
+ we don't escape them, construct_command_argv_internal will
+ recursively call itself ad nauseam, or until stack overflow,
+ whichever happens first. */
+ for (p = shell; *p != '\0'; ++p)
+ {
+ if (strchr (sh_chars, *p) != 0)
+ *(ap++) = '\\';
+ *(ap++) = *p;
+ }
*(ap++) = ' ';
- memcpy (ap, shellflags, sflags_len);
+ if (shellflags)
+ memcpy (ap, shellflags, sflags_len);
ap += sflags_len;
*(ap++) = ' ';
+#ifdef WINDOWS32
command_ptr = ap;
+#endif
for (p = line; *p != '\0'; ++p)
{
- if (restp != NULL && *p == '\n')
- {
- *restp = p;
- break;
- }
- else if (*p == '\\' && p[1] == '\n')
- {
- /* POSIX says we keep the backslash-newline. If we don't have a
+ if (restp != NULL && *p == '\n')
+ {
+ *restp = p;
+ break;
+ }
+ else if (*p == '\\' && p[1] == '\n')
+ {
+ /* POSIX says we keep the backslash-newline. If we don't have a
POSIX shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
and remove the backslash/newline. */
#if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
@@ -2892,97 +3260,108 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
#else
# define PRESERVE_BSNL 1
#endif
- if (PRESERVE_BSNL)
- {
- *(ap++) = '\\';
- /* Only non-batch execution needs another backslash,
- because it will be passed through a recursive
- invocation of this function. */
- if (!batch_mode_shell)
- *(ap++) = '\\';
- *(ap++) = '\n';
- }
- ++p;
- continue;
- }
+ if (PRESERVE_BSNL)
+ {
+ *(ap++) = '\\';
+ /* Only non-batch execution needs another backslash,
+ because it will be passed through a recursive
+ invocation of this function. */
+ if (!batch_mode_shell)
+ *(ap++) = '\\';
+ *(ap++) = '\n';
+ }
+ ++p;
+ continue;
+ }
/* DOS shells don't know about backslash-escaping. */
- if (unixy_shell && !batch_mode_shell &&
+ if (unixy_shell && !batch_mode_shell &&
(*p == '\\' || *p == '\'' || *p == '"'
|| isspace ((unsigned char)*p)
|| strchr (sh_chars, *p) != 0))
- *ap++ = '\\';
+ *ap++ = '\\';
#ifdef __MSDOS__
else if (unixy_shell && strneq (p, "...", 3))
{
- /* The case of `...' wildcard again. */
+ /* The case of '...' wildcard again. */
strcpy (ap, "\\.\\.\\");
ap += 5;
p += 2;
}
#endif
- *ap++ = *p;
+ *ap++ = *p;
}
if (ap == new_line + shell_len + sflags_len + 2)
- /* Line was empty. */
- return 0;
+ {
+ /* Line was empty. */
+ free (new_line);
+ return 0;
+ }
*ap = '\0';
#ifdef WINDOWS32
/* Some shells do not work well when invoked as 'sh -c xxx' to run a
command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
cases, run commands via a script file. */
- if (just_print_flag && !(flags & COMMANDS_RECURSE)) {
- /* Need to allocate new_argv, although it's unused, because
- start_job_command will want to free it and its 0'th element. */
- new_argv = xmalloc(2 * sizeof (char *));
- new_argv[0] = xstrdup ("");
- new_argv[1] = NULL;
- } else if ((no_default_sh_exe || batch_mode_shell) && batch_filename_ptr) {
- int temp_fd;
- FILE* batch = NULL;
- int id = GetCurrentProcessId();
- PATH_VAR(fbuf);
-
- /* create a file name */
- sprintf(fbuf, "make%d", id);
- *batch_filename_ptr = create_batch_file (fbuf, unixy_shell, &temp_fd);
-
- DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
- *batch_filename_ptr));
-
- /* Create a FILE object for the batch file, and write to it the
- commands to be executed. Put the batch file in TEXT mode. */
- _setmode (temp_fd, _O_TEXT);
- batch = _fdopen (temp_fd, "wt");
- if (!unixy_shell)
- fputs ("@echo off\n", batch);
- fputs (command_ptr, batch);
- fputc ('\n', batch);
- fclose (batch);
- DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"),
- !unixy_shell ? "\n\t@echo off" : "", command_ptr));
-
- /* create argv */
- new_argv = xmalloc(3 * sizeof (char *));
- if (unixy_shell) {
- new_argv[0] = xstrdup (shell);
- new_argv[1] = *batch_filename_ptr; /* only argv[0] gets freed later */
- } else {
- new_argv[0] = xstrdup (*batch_filename_ptr);
+ if (just_print_flag && !(flags & COMMANDS_RECURSE))
+ {
+ /* Need to allocate new_argv, although it's unused, because
+ start_job_command will want to free it and its 0'th element. */
+ new_argv = xmalloc (2 * sizeof (char *));
+ new_argv[0] = xstrdup ("");
new_argv[1] = NULL;
}
- new_argv[2] = NULL;
- } else
+ else if ((no_default_sh_exe || batch_mode_shell) && batch_filename)
+ {
+ int temp_fd;
+ FILE* batch = NULL;
+ int id = GetCurrentProcessId ();
+ PATH_VAR (fbuf);
+
+ /* create a file name */
+ sprintf (fbuf, "make%d", id);
+ *batch_filename = create_batch_file (fbuf, unixy_shell, &temp_fd);
+
+ DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
+ *batch_filename));
+
+ /* Create a FILE object for the batch file, and write to it the
+ commands to be executed. Put the batch file in TEXT mode. */
+ _setmode (temp_fd, _O_TEXT);
+ batch = _fdopen (temp_fd, "wt");
+ if (!unixy_shell)
+ fputs ("@echo off\n", batch);
+ fputs (command_ptr, batch);
+ fputc ('\n', batch);
+ fclose (batch);
+ DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"),
+ !unixy_shell ? "\n\t@echo off" : "", command_ptr));
+
+ /* create argv */
+ new_argv = xmalloc (3 * sizeof (char *));
+ if (unixy_shell)
+ {
+ new_argv[0] = xstrdup (shell);
+ new_argv[1] = *batch_filename; /* only argv[0] gets freed later */
+ }
+ else
+ {
+ new_argv[0] = xstrdup (*batch_filename);
+ new_argv[1] = NULL;
+ }
+ new_argv[2] = NULL;
+ }
+ else
#endif /* WINDOWS32 */
if (unixy_shell)
- new_argv = construct_command_argv_internal (new_line, 0, 0, 0, 0, flags, 0);
+ new_argv = construct_command_argv_internal (new_line, 0, 0, 0, 0,
+ flags, 0);
#ifdef __EMX__
else if (!unixy_shell)
{
- /* new_line is local, must not be freed therefore
+ /* new_line is local, must not be freed therefore
We use line here instead of new_line because we run the shell
manually. */
size_t line_len = strlen (line);
@@ -3065,8 +3444,10 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
fatal (NILF, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
__FILE__, __LINE__);
#endif
+
+ free (new_line);
}
-#endif /* ! AMIGA */
+#endif /* ! AMIGA */
return new_argv;
}
@@ -3074,7 +3455,7 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
/* Figure out the argument list necessary to run LINE as a command. Try to
avoid using a shell. This routine handles only ' quoting, and " quoting
- when no backslash, $ or ` characters are seen in the quotes. Starting
+ when no backslash, $ or ' characters are seen in the quotes. Starting
quotes may be escaped with a backslash. If any of the characters in
sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
is the first word of a line, the shell is used.
@@ -3087,7 +3468,7 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
char **
construct_command_argv (char *line, char **restp, struct file *file,
- int cmd_flags, char **batch_filename_ptr)
+ int cmd_flags, char **batch_filename)
{
char *shell, *ifs, *shellflags;
char **argv;
@@ -3101,13 +3482,13 @@ construct_command_argv (char *line, char **restp, struct file *file,
for (;;)
{
while ((*cptr != 0)
- && (isspace ((unsigned char)*cptr)))
- cptr++;
+ && (isspace ((unsigned char)*cptr)))
+ cptr++;
if (*cptr == 0)
- break;
+ break;
while ((*cptr != 0)
- && (!isspace((unsigned char)*cptr)))
- cptr++;
+ && (!isspace ((unsigned char)*cptr)))
+ cptr++;
argc++;
}
@@ -3120,17 +3501,17 @@ construct_command_argv (char *line, char **restp, struct file *file,
for (;;)
{
while ((*cptr != 0)
- && (isspace ((unsigned char)*cptr)))
- cptr++;
+ && (isspace ((unsigned char)*cptr)))
+ cptr++;
if (*cptr == 0)
- break;
+ break;
DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
argv[argc++] = cptr;
while ((*cptr != 0)
- && (!isspace((unsigned char)*cptr)))
- cptr++;
+ && (!isspace ((unsigned char)*cptr)))
+ cptr++;
if (*cptr != 0)
- *cptr++ = 0;
+ *cptr++ = 0;
}
#else
{
@@ -3144,10 +3525,11 @@ construct_command_argv (char *line, char **restp, struct file *file,
* Convert to forward slashes so that construct_command_argv_internal()
* is not confused.
*/
- if (shell) {
- char *p = w32ify (shell, 0);
- strcpy (shell, p);
- }
+ if (shell)
+ {
+ char *p = w32ify (shell, 0);
+ strcpy (shell, p);
+ }
#endif
#ifdef __EMX__
{
@@ -3155,12 +3537,12 @@ construct_command_argv (char *line, char **restp, struct file *file,
static const char *last_shell = "";
static int init = 0;
if (init == 0)
- {
- unixroot = getenv ("UNIXROOT");
- /* unixroot must be NULL or not empty */
- if (unixroot && unixroot[0] == '\0') unixroot = NULL;
- init = 1;
- }
+ {
+ unixroot = getenv ("UNIXROOT");
+ /* unixroot must be NULL or not empty */
+ if (unixroot && unixroot[0] == '\0') unixroot = NULL;
+ init = 1;
+ }
/* if we have an unixroot drive and if shell is not default_shell
(which means it's either cmd.exe or the test has already been
@@ -3168,29 +3550,29 @@ construct_command_argv (char *line, char **restp, struct file *file,
try whether it exists e.g.: if "/bin/sh" does not exist use
"$UNIXROOT/bin/sh" instead. */
if (unixroot && shell && strcmp (shell, last_shell) != 0
- && (shell[0] == '/' || shell[0] == '\\'))
- {
- /* trying a new shell, check whether it exists */
- size_t size = strlen (shell);
- char *buf = xmalloc (size + 7);
- memcpy (buf, shell, size);
- memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
+ && (shell[0] == '/' || shell[0] == '\\'))
+ {
+ /* trying a new shell, check whether it exists */
+ size_t size = strlen (shell);
+ char *buf = xmalloc (size + 7);
+ memcpy (buf, shell, size);
+ memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
- {
- /* try the same for the unixroot drive */
- memmove (buf + 2, buf, size + 5);
- buf[0] = unixroot[0];
- buf[1] = unixroot[1];
- if (access (buf, F_OK) == 0)
- /* we have found a shell! */
- /* free(shell); */
- shell = buf;
- else
- free (buf);
- }
- else
+ {
+ /* try the same for the unixroot drive */
+ memmove (buf + 2, buf, size + 5);
+ buf[0] = unixroot[0];
+ buf[1] = unixroot[1];
+ if (access (buf, F_OK) == 0)
+ /* we have found a shell! */
+ /* free(shell); */
+ shell = buf;
+ else
+ free (buf);
+ }
+ else
free (buf);
- }
+ }
}
#endif /* __EMX__ */
@@ -3201,7 +3583,7 @@ construct_command_argv (char *line, char **restp, struct file *file,
}
argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs,
- cmd_flags, batch_filename_ptr);
+ cmd_flags, batch_filename);
free (shell);
free (shellflags);