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