Changeset 3140 for trunk/src/kmk/job.c
- Timestamp:
- Mar 14, 2018, 10:28:10 PM (7 years ago)
- Location:
- trunk/src/kmk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk
-
Property svn:mergeinfo
set to
/vendor/gnumake/current merged eligible
-
Property svn:mergeinfo
set to
-
trunk/src/kmk/job.c
r3065 r3140 1 1 /* Job execution and handling for GNU Make. 2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 4 2010 Free Software Foundation, Inc. 2 Copyright (C) 1988-2016 Free Software Foundation, Inc. 5 3 This file is part of GNU Make. 6 4 … … 17 15 this program. If not, see <http://www.gnu.org/licenses/>. */ 18 16 19 #include "make .h"17 #include "makeint.h" 20 18 21 19 #include <assert.h> … … 26 24 #include "commands.h" 27 25 #include "variable.h" 28 #include " debug.h"26 #include "os.h" 29 27 #ifdef CONFIG_WITH_KMK_BUILTIN 30 28 # include "kmkbuiltin.h" … … 41 39 #include <windows.h> 42 40 43 c har *default_shell = "sh.exe";41 const char *default_shell = "sh.exe"; 44 42 int no_default_sh_exe = 1; 45 43 int batch_mode_shell = 1; … … 48 46 #elif defined (_AMIGA) 49 47 50 c har default_shell[]= "";48 const char *default_shell = ""; 51 49 extern int MyExecute (char **); 52 50 int batch_mode_shell = 0; … … 58 56 to search the $PATH for it (since MSDOS doesn't have standard 59 57 directories we could trust). */ 60 c har *default_shell = "command.com";58 const char *default_shell = "command.com"; 61 59 int batch_mode_shell = 0; 62 60 63 61 #elif defined (__EMX__) 64 62 65 c har *default_shell = "sh.exe"; /* bird changed this from "/bin/sh" as that doesn't make sense on OS/2. */63 const char *default_shell = "sh.exe"; /* bird changed this from "/bin/sh" as that doesn't make sense on OS/2. */ 66 64 int batch_mode_shell = 0; 67 65 … … 69 67 70 68 # include <descrip.h> 71 char default_shell[] = ""; 69 # include <stsdef.h> 70 const char *default_shell = ""; 72 71 int batch_mode_shell = 0; 73 72 74 #elif defined (__riscos__) 75 76 char default_shell[] = ""; 77 int batch_mode_shell = 0; 73 #define strsignal vms_strsignal 74 char * vms_strsignal (int status); 75 76 #ifndef C_FACILITY_NO 77 # define C_FACILITY_NO 0x350000 78 #endif 79 #ifndef VMS_POSIX_EXIT_MASK 80 # define VMS_POSIX_EXIT_MASK (C_FACILITY_NO | 0xA000) 81 #endif 78 82 79 83 #else 80 84 81 c har default_shell[]= "/bin/sh";85 const char *default_shell = "/bin/sh"; 82 86 int batch_mode_shell = 0; 83 87 … … 116 120 # include "w32err.h" 117 121 # include "pathstuff.h" 122 # define WAIT_NOHANG 1 118 123 #endif /* WINDOWS32 */ 119 124 … … 127 132 128 133 #ifdef HAVE_WAITPID 129 # define WAIT_NOHANG(status) 130 #else 134 # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG) 135 #else /* Don't have waitpid. */ 131 136 # ifdef HAVE_WAIT3 132 137 # ifndef wait3 133 138 extern int wait3 (); 134 139 # endif 135 # define WAIT_NOHANG(status) 140 # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0) 136 141 # endif /* Have wait3. */ 137 142 #endif /* Have waitpid. */ … … 141 146 #endif 142 147 143 #ifndef 148 #ifndef HAVE_UNION_WAIT 144 149 145 150 # define WAIT_T int … … 161 166 # endif 162 167 163 #else /* Have `union wait'. */168 #else /* Have 'union wait'. */ 164 169 165 170 # define WAIT_T union wait … … 180 185 # endif 181 186 182 #endif /* Don't have `union wait'. */187 #endif /* Don't have 'union wait'. */ 183 188 184 189 #if !defined(HAVE_UNISTD_H) && !defined(WINDOWS32) … … 212 217 } 213 218 219 #ifndef HAVE_GETLOADAVG 214 220 int getloadavg (double loadavg[], int nelem); 215 int start_remote_job (char **argv, char **envp, int stdin_fd, int *is_remote, 216 int *id_ptr, int *used_stdin); 217 int start_remote_job_p (int); 218 int remote_status (int *exit_code_ptr, int *signal_ptr, int *coredump_ptr, 219 int block); 220 221 RETSIGTYPE child_handler (int); 221 #endif 222 222 223 static void free_child (struct child *); 223 224 static void start_job_command (struct child *child); … … 238 239 unsigned int job_slots_used = 0; 239 240 240 /* Nonzero if the `good' standard input is in use. */241 /* Nonzero if the 'good' standard input is in use. */ 241 242 242 243 static int good_stdin_used = 0; … … 257 258 258 259 unsigned int jobserver_tokens = 0; 260 259 261 260 262 261 263 #ifdef WINDOWS32 262 264 /* 263 * The macro which references this function is defined in make .h.265 * The macro which references this function is defined in makeint.h. 264 266 */ 265 267 int 266 w32_kill (pid_t pid, int sig)268 w32_kill (pid_t pid, int sig) 267 269 { 268 return ((process_kill ((HANDLE)pid, sig) == TRUE) ? 0 : -1);270 return ((process_kill ((HANDLE)pid, sig) == TRUE) ? 0 : -1); 269 271 } 270 272 … … 279 281 const char *error_string = NULL; 280 282 char temp_path[MAXPATHLEN]; /* need to know its length */ 281 unsigned path_size = GetTempPath (sizeof temp_path, temp_path);283 unsigned path_size = GetTempPath (sizeof temp_path, temp_path); 282 284 int path_is_dot = 0; 283 unsigned uniq = 1; 285 /* The following variable is static so we won't try to reuse a name 286 that was generated a little while ago, because that file might 287 not be on disk yet, since we use FILE_ATTRIBUTE_TEMPORARY below, 288 which tells the OS it doesn't need to flush the cache to disk. 289 If the file is not yet on disk, we might think the name is 290 available, while it really isn't. This happens in parallel 291 builds, where Make doesn't wait for one job to finish before it 292 launches the next one. */ 293 static unsigned uniq = 0; 294 static int second_loop = 0; 284 295 const unsigned sizemax = strlen (base) + strlen (ext) + 10; 285 296 … … 290 301 } 291 302 303 ++uniq; 304 if (uniq >= 0x10000 && !second_loop) 305 { 306 /* If we already had 64K batch files in this 307 process, make a second loop through the numbers, 308 looking for free slots, i.e. files that were 309 deleted in the meantime. */ 310 second_loop = 1; 311 uniq = 1; 312 } 292 313 while (path_size > 0 && 293 314 path_size + sizemax < sizeof temp_path && 294 uniq < 0x10000)315 !(uniq >= 0x10000 && second_loop)) 295 316 { 296 317 unsigned size = sprintf (temp_path + path_size, … … 309 330 if (h == INVALID_HANDLE_VALUE) 310 331 { 311 const DWORD er = GetLastError ();332 const DWORD er = GetLastError (); 312 333 313 334 if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS) 314 ++uniq; 335 { 336 ++uniq; 337 if (uniq == 0x10000 && !second_loop) 338 { 339 second_loop = 1; 340 uniq = 1; 341 } 342 } 315 343 316 344 /* the temporary path is not guaranteed to exist */ … … 348 376 if (error_string == NULL) 349 377 error_string = _("Cannot create a temporary file\n"); 350 fatal (NILF, error_string);378 O (fatal, NILF, error_string); 351 379 352 380 /* not reached */ … … 388 416 389 417 i = 0; 390 while (known_os2shells[i] != NULL) { 391 if (strcasecmp (name, known_os2shells[i]) == 0) 392 return 0; /* not a unix shell */ 393 i++; 394 } 418 while (known_os2shells[i] != NULL) 419 { 420 if (strcasecmp (name, known_os2shells[i]) == 0) 421 return 0; /* not a unix shell */ 422 i++; 423 } 395 424 396 425 /* in doubt assume a unix like shell */ … … 403 432 is_bourne_compatible_shell (const char *path) 404 433 { 405 /* list of known unix (Bourne-like) shells*/406 const char *unix_shells[] = {434 /* List of known POSIX (or POSIX-ish) shells. */ 435 static const char *unix_shells[] = { 407 436 "sh", 408 437 "bash", … … 414 443 NULL 415 444 }; 416 unsigned i, len;445 const char **s; 417 446 418 447 /* find the rightmost '/' or '\\' */ … … 427 456 name = path; 428 457 429 if (*name == '/' || *name == '\\') name++; 458 if (*name == '/' || *name == '\\') 459 ++name; 430 460 431 461 /* this should be able to deal with extensions on Windows-like systems */ 432 for ( i = 0; unix_shells[i] != NULL; i++) {433 len = strlen(unix_shells[i]);462 for (s = unix_shells; *s != NULL; ++s) 463 { 434 464 #if defined(WINDOWS32) || defined(__MSDOS__) 435 if ((strncasecmp (name, unix_shells[i], len) == 0) && 436 (strlen(name) >= len && (name[len] == '\0' || name[len] == '.'))) 465 unsigned int len = strlen (*s); 466 if ((strlen (name) >= len && STOP_SET (name[len], MAP_DOT|MAP_NUL)) 467 && strncasecmp (name, *s, len) == 0) 437 468 #else 438 if ((strncmp (name, unix_shells[i], len) == 0) && 439 (strlen(name) >= len && name[len] == '\0')) 440 #endif 441 return 1; /* a known unix-style shell */ 442 } 469 if (strcmp (name, *s) == 0) 470 #endif 471 return 1; /* a known unix-style shell */ 472 } 443 473 444 474 /* if not on the list, assume it's not a Bourne-like shell */ … … 453 483 454 484 static void 455 child_error ( const char *target_name,485 child_error (struct child *child, 456 486 int exit_code, int exit_sig, int coredump, int ignored) 457 487 { 488 const char *pre = "*** "; 489 const char *post = ""; 490 const char *dump = ""; 491 const struct file *f = child->file; 492 const floc *flocp = &f->cmds->fileinfo; 493 const char *nm; 494 size_t l; 495 458 496 if (ignored && silent_flag) 459 497 return; 460 498 461 #ifdef VMS 462 if (!(exit_code & 1)) 463 error (NILF, 464 (ignored ? _("*** [%s] Error 0x%x (ignored)") 465 : _("*** [%s] Error 0x%x")), 466 target_name, exit_code); 467 #else 499 if (exit_sig && coredump) 500 dump = _(" (core dumped)"); 501 502 if (ignored) 503 { 504 pre = ""; 505 post = _(" (ignored)"); 506 } 507 508 if (! flocp->filenm) 509 nm = _("<builtin>"); 510 else 511 { 512 char *a = alloca (strlen (flocp->filenm) + 1 + 11 + 1); 513 sprintf (a, "%s:%lu", flocp->filenm, flocp->lineno + flocp->offset); 514 nm = a; 515 } 516 517 l = strlen (pre) + strlen (nm) + strlen (f->name) + strlen (post); 518 519 OUTPUT_SET (&child->output); 520 521 show_goal_error (); 522 468 523 if (exit_sig == 0) 469 524 # if defined(KMK) && defined(KBUILD_OS_WINDOWS) 470 525 { 471 const char * name = NULL;526 const char *exit_name = NULL; 472 527 switch ((unsigned)exit_code) 473 528 { 474 case 0xc0000005U: name = "STATUS_ACCESS_VIOLATION"; break;475 case 0xc000013aU: name = "STATUS_CONTROL_C_EXIT"; break;476 case 0xc0000374U: name = "STATUS_HEAP_CORRUPTION"; break;477 case 0xc0000409U: name = "STATUS_STACK_BUFFER_OVERRUN"; break;478 case 0xc0000417U: name = "STATUS_INVALID_CRUNTIME_PARAMETER"; break;479 case 0x80000003U: name = "STATUS_BREAKPOINT"; break;480 case 0x40000015U: name = "STATUS_FATAL_APP_EXIT"; break;481 case 0x40010004U: name = "DBG_TERMINATE_PROCESS"; break;482 case 0x40010005U: name = "DBG_CONTROL_C"; break;483 case 0x40010008U: name = "DBG_CONTROL_BREAK"; break;529 case 0xc0000005U: exit_name = "STATUS_ACCESS_VIOLATION"; break; 530 case 0xc000013aU: exit_name = "STATUS_CONTROL_C_EXIT"; break; 531 case 0xc0000374U: exit_name = "STATUS_HEAP_CORRUPTION"; break; 532 case 0xc0000409U: exit_name = "STATUS_STACK_BUFFER_OVERRUN"; break; 533 case 0xc0000417U: exit_name = "STATUS_INVALID_CRUNTIME_PARAMETER"; break; 534 case 0x80000003U: exit_name = "STATUS_BREAKPOINT"; break; 535 case 0x40000015U: exit_name = "STATUS_FATAL_APP_EXIT"; break; 536 case 0x40010004U: exit_name = "DBG_TERMINATE_PROCESS"; break; 537 case 0x40010005U: exit_name = "DBG_CONTROL_C"; break; 538 case 0x40010008U: exit_name = "DBG_CONTROL_BREAK"; break; 484 539 } 485 if ( name)486 error (NILF, ignored ? _("[%s] Error %d (%s) (ignored)") :487 _(" *** [%s] Error %d (%s)"),488 target_name, exit_code, name);540 if (exit_name) 541 error (NILF, l + strlen (exit_name) + INTSTR_LENGTH, 542 _("%s[%s: %s] Error %d (%s)%s"), 543 pre, nm, f->name, exit_code, exit_name, post); 489 544 else 490 error (NILF, ignored ? _("[%s] Error %d (%#x) (ignored)") :491 _(" *** [%s] Error %d (%#x)"),492 target_name, exit_code, exit_code);545 error (NILF, l + INTSTR_LENGTH + INTSTR_LENGTH, 546 _("%s[%s: %s] Error %d (%#x)%s"), 547 pre, nm, f->name, exit_code, exit_code, post); 493 548 } 494 549 # else 495 error (NILF, ignored ? _("[%s] Error %d (ignored)") : 496 _("*** [%s] Error %d"), 497 target_name, exit_code); 550 error (NILF, l + INTSTR_LENGTH, 551 _("%s[%s: %s] Error %d%s"), pre, nm, f->name, exit_code, post); 498 552 # endif 499 553 else 500 error (NILF, "*** [%s] %s%s", 501 target_name, strsignal (exit_sig), 502 coredump ? _(" (core dumped)") : ""); 503 #endif /* VMS */ 554 { 555 const char *s = strsignal (exit_sig); 556 error (NILF, l + strlen (s) + strlen (dump), 557 "%s[%s: %s] %s%s%s", pre, nm, f->name, s, dump, post); 558 } 559 560 OUTPUT_UNSET (); 504 561 } 505 562 … … 508 565 /* Handle a dead child. This handler may or may not ever be installed. 509 566 510 If we're using the jobserver feature, we need it. First, installing it 511 ensures the read will interrupt on SIGCHLD. Second, we close the dup'd 512 read FD to ensure we don't enter another blocking read without reaping all 513 the dead children. In this case we don't need the dead_children count. 567 If we're using the jobserver feature without pselect(), we need it. 568 First, installing it ensures the read will interrupt on SIGCHLD. Second, 569 we close the dup'd read FD to ensure we don't enter another blocking read 570 without reaping all the dead children. In this case we don't need the 571 dead_children count. 514 572 515 573 If we don't have either waitpid or wait3, then make is unreliable, but we … … 523 581 ++dead_children; 524 582 525 if (job_rfd >= 0) 526 { 527 close (job_rfd); 528 job_rfd = -1; 529 } 583 jobserver_signal (); 530 584 531 585 #if defined __EMX__ && !defined(__INNOTEK_LIBC__) /* bird */ … … 533 587 signal (SIGCHLD, SIG_DFL); 534 588 #endif 535 536 /* This causes problems if the SIGCHLD interrupts a printf().537 DB (DB_JOBS, (_("Got a SIGCHLD; %u unreaped children.\n"), dead_children));538 */539 589 } 540 590 541 extern int shell_function_pid, shell_function_completed;591 extern pid_t shell_function_pid; 542 592 543 593 /* Reap all dead children, storing the returned status and the new command 544 state ( `cs_finished') in the `file' member of the `struct child' for the594 state ('cs_finished') in the 'file' member of the 'struct child' for the 545 595 dead child, and removing the child from the chain. In addition, if BLOCK 546 596 nonzero, we block in this function until we've reaped at least one … … 553 603 #ifndef WINDOWS32 554 604 WAIT_T status; 605 #endif 555 606 /* Initially, assume we have some. */ 556 607 int reap_more = 1; 557 #endif558 608 559 609 #ifdef WAIT_NOHANG … … 574 624 && (block || REAP_MORE)) 575 625 { 576 int remote = 0;626 unsigned int remote = 0; 577 627 pid_t pid; 578 628 int exit_code, exit_sig, coredump; 579 registerstruct child *lastc, *c;629 struct child *lastc, *c; 580 630 int child_failed; 581 631 int any_remote, any_local; … … 586 636 587 637 if (err && block) 588 638 { 589 639 static int printed = 0; 590 640 591 641 /* We might block for a while, so let the user know why. 592 642 Only print this message once no matter how many jobs are left. */ 593 643 fflush (stdout); 594 644 if (!printed) 595 error (NILF, _("*** Waiting for unfinished jobs...."));645 O (error, NILF, _("*** Waiting for unfinished jobs....")); 596 646 printed = 1; 597 647 } 598 648 599 649 /* We have one less dead child to reap. As noted in 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 650 child_handler() above, this count is completely unimportant for 651 all modern, POSIX-y systems that support wait3() or waitpid(). 652 The rest of this comment below applies only to early, broken 653 pre-POSIX systems. We keep the count only because... it's there... 654 655 The test and decrement are not atomic; if it is compiled into: 656 register = dead_children - 1; 657 dead_children = register; 658 a SIGCHLD could come between the two instructions. 659 child_handler increments dead_children. 660 The second instruction here would lose that increment. But the 661 only effect of dead_children being wrong is that we might wait 662 longer than necessary to reap a child, and lose some parallelism; 663 and we might print the "Waiting for unfinished jobs" message above 664 when not necessary. */ 615 665 616 666 if (dead_children > 0) 617 667 --dead_children; 618 668 619 669 any_remote = 0; 620 670 any_local = shell_function_pid != 0; 621 671 for (c = children; c != 0; c = c->next) 622 623 624 672 { 673 any_remote |= c->remote; 674 any_local |= ! c->remote; 625 675 #ifdef CONFIG_WITH_KMK_BUILTIN 626 676 if (c->has_status) … … 634 684 else 635 685 #endif 636 637 (void *)c, c->file->name, pid2str (c->pid),638 c->remote ? _(" (remote)") : ""));686 DB (DB_JOBS, (_("Live child %p (%s) PID %s %s\n"), 687 (void *)c, c->file->name, pid2str (c->pid), 688 c->remote ? _(" (remote)") : "")); 639 689 #ifdef VMS 640 641 #endif 642 690 break; 691 #endif 692 } 643 693 644 694 /* First, check for remote children. */ 645 695 if (any_remote) 646 696 pid = remote_status (&exit_code, &exit_sig, &coredump, 0); 647 697 else 648 698 pid = 0; 649 699 650 700 if (pid > 0) 651 652 701 /* We got a remote child. */ 702 remote = 1; 653 703 else if (pid < 0) 654 704 { 655 705 /* A remote status command failed miserably. Punt. */ 656 657 658 706 remote_status_lose: 707 pfatal_with_name ("remote_status"); 708 } 659 709 else 660 661 710 { 711 /* No remote children. Check for local children. */ 662 712 #ifdef CONFIG_WITH_KMK_BUILTIN 663 713 if (completed_child) … … 675 725 #endif /* CONFIG_WITH_KMK_BUILTIN */ 676 726 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32) 677 678 727 if (any_local) 728 { 679 729 #ifdef VMS 680 vmsWaitForChildren (&status); 681 pid = c->pid; 730 /* Todo: This needs more untangling multi-process support */ 731 /* Just do single child process support now */ 732 vmsWaitForChildren (&status); 733 pid = c->pid; 734 735 /* VMS failure status can not be fully translated */ 736 status = $VMS_STATUS_SUCCESS (c->cstatus) ? 0 : (1 << 8); 737 738 /* A Posix failure can be exactly translated */ 739 if ((c->cstatus & VMS_POSIX_EXIT_MASK) == VMS_POSIX_EXIT_MASK) 740 status = (c->cstatus >> 3 & 255) << 8; 682 741 #else 683 742 #ifdef WAIT_NOHANG 684 685 686 687 #endif 688 EINTRLOOP(pid, wait (&status));743 if (!block) 744 pid = WAIT_NOHANG (&status); 745 else 746 #endif 747 EINTRLOOP (pid, wait (&status)); 689 748 #endif /* !VMS */ 690 691 692 693 694 695 749 } 750 else 751 pid = 0; 752 753 if (pid < 0) 754 { 696 755 /* The wait*() failed miserably. Punt. */ 697 698 699 700 701 702 703 704 756 pfatal_with_name ("wait"); 757 } 758 else if (pid > 0) 759 { 760 /* We got a child exit; chop the status word up. */ 761 exit_code = WEXITSTATUS (status); 762 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0; 763 coredump = WCOREDUMP (status); 705 764 706 765 /* If we have started jobs in this second, remove one. */ 707 766 if (job_counter) 708 767 --job_counter; 709 710 711 712 768 } 769 else 770 { 771 /* No local children are dead. */ 713 772 reap_more = 0; 714 773 715 774 if (!block || !any_remote) 716 775 break; 717 776 … … 726 785 /* We got a remote child. */ 727 786 remote = 1; 728 787 } 729 788 #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */ 730 789 731 790 #ifdef __MSDOS__ 732 733 734 735 736 737 738 739 791 /* Life is very different on MSDOS. */ 792 pid = dos_pid - 1; 793 status = dos_status; 794 exit_code = WEXITSTATUS (status); 795 if (exit_code == 0xff) 796 exit_code = -1; 797 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0; 798 coredump = 0; 740 799 #endif /* __MSDOS__ */ 741 800 #ifdef _AMIGA 742 743 744 745 746 747 801 /* Same on Amiga */ 802 pid = amiga_pid - 1; 803 status = amiga_status; 804 exit_code = amiga_status; 805 exit_sig = 0; 806 coredump = 0; 748 807 #endif /* _AMIGA */ 749 808 #ifdef WINDOWS32 750 809 { 751 810 HANDLE hPID; 752 int werr;753 811 HANDLE hcTID, hcPID; 812 DWORD dwWaitStatus = 0; 754 813 exit_code = 0; 755 814 exit_sig = 0; … … 768 827 fprintf (stderr, 769 828 "Determine main thread ID (Error %ld: %s)\n", 770 e, map_windows32_error_to_string (e));829 e, map_windows32_error_to_string (e)); 771 830 } 772 831 else … … 775 834 776 835 /* wait for anything to finish */ 777 hPID = process_wait_for_any ();836 hPID = process_wait_for_any (block, &dwWaitStatus); 778 837 if (hPID) 779 838 { 780 781 839 /* was an error found on this process? */ 782 werr = process_last_err(hPID);840 int werr = process_last_err (hPID); 783 841 784 842 /* get exit data */ 785 exit_code = process_exit_code (hPID);843 exit_code = process_exit_code (hPID); 786 844 787 845 if (werr) 788 fprintf (stderr, "make (e=%d): %s",789 exit_code, map_windows32_error_to_string(exit_code));846 fprintf (stderr, "make (e=%d): %s", exit_code, 847 map_windows32_error_to_string (exit_code)); 790 848 791 849 /* signal */ 792 exit_sig = process_signal (hPID);850 exit_sig = process_signal (hPID); 793 851 794 852 /* cleanup process */ 795 process_cleanup (hPID);853 process_cleanup (hPID); 796 854 797 855 coredump = 0; 798 856 } 799 else if ( !process_used_slots())857 else if (dwWaitStatus == WAIT_FAILED) 800 858 { 801 /* The wait*() failed miserably. Punt. */ 802 errno = ECHILD; 803 pfatal_with_name ("wait"); 859 /* The WaitForMultipleObjects() failed miserably. Punt. */ 860 pfatal_with_name ("WaitForMultipleObjects"); 861 } 862 else if (dwWaitStatus == WAIT_TIMEOUT) 863 { 864 /* No child processes are finished. Give up waiting. */ 865 reap_more = 0; 866 break; 804 867 } 805 868 … … 807 870 } 808 871 #endif /* WINDOWS32 */ 809 810 811 /* Check if this is the child of the `shell' function. */872 } 873 874 /* Check if this is the child of the 'shell' function. */ 812 875 if (!remote && pid == shell_function_pid) 813 { 814 /* It is. Leave an indicator for the `shell' function. */ 815 if (exit_sig == 0 && exit_code == 127) 816 shell_function_completed = -1; 817 else 818 shell_function_completed = 1; 819 break; 820 } 821 822 child_failed = exit_sig != 0 || exit_code != 0; 876 { 877 shell_completed (exit_code, exit_sig); 878 break; 879 } 823 880 824 881 /* Search for a child matching the deceased one. */ 825 882 lastc = 0; 826 883 for (c = children; c != 0; lastc = c, c = c->next) 827 if (c->remote == remote && c->pid == pid)828 884 if (c->pid == pid && c->remote == remote) 885 break; 829 886 830 887 if (c == 0) … … 833 890 continue; 834 891 892 /* Determine the failure status: 0 for success, 1 for updating target in 893 question mode, 2 for anything else. */ 894 if (exit_sig == 0 && exit_code == 0) 895 child_failed = MAKE_SUCCESS; 896 else if (exit_sig == 0 && exit_code == 1 && question_flag && c->recursive) 897 child_failed = MAKE_TROUBLE; 898 else 899 child_failed = MAKE_FAILURE; 900 835 901 DB (DB_JOBS, (child_failed 836 902 ? _("Reaping losing child %p PID %s %s\n") … … 838 904 (void *)c, pid2str (c->pid), c->remote ? _(" (remote)") : "")); 839 905 840 if (c->sh_batch_file) { 841 DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"), 842 c->sh_batch_file)); 843 844 /* just try and remove, don't care if this fails */ 845 remove (c->sh_batch_file); 846 847 /* all done with memory */ 848 free (c->sh_batch_file); 849 c->sh_batch_file = NULL; 850 } 906 if (c->sh_batch_file) 907 { 908 int rm_status; 909 910 DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"), 911 c->sh_batch_file)); 912 913 errno = 0; 914 rm_status = remove (c->sh_batch_file); 915 if (rm_status) 916 DB (DB_JOBS, (_("Cleaning up temp batch file %s failed (%d)\n"), 917 c->sh_batch_file, errno)); 918 919 /* all done with memory */ 920 free (c->sh_batch_file); 921 c->sh_batch_file = NULL; 922 } 851 923 852 924 /* If this child had the good stdin, say it is now free. */ … … 862 934 static int delete_on_error = -1; 863 935 864 if (!dontcare )936 if (!dontcare && child_failed == MAKE_FAILURE) 865 937 #ifdef KMK 866 938 { 867 child_error (c ->file->name, exit_code, exit_sig, coredump, 0);939 child_error (c, exit_code, exit_sig, coredump, 0); 868 940 if ( ( c->file->cmds->lines_flags[c->command_line - 1] 869 941 & (COMMANDS_SILENT | COMMANDS_RECURSE)) … … 873 945 # endif 874 946 || exit_sig != 0) 875 message (0, "The failing command:\n%s", c->file->cmds->command_lines[c->command_line - 1]);947 OS (message, 0, "The failing command:\n%s", c->file->cmds->command_lines[c->command_line - 1]); 876 948 } 877 949 #else /* !KMK */ 878 child_error (c ->file->name, exit_code, exit_sig, coredump, 0);950 child_error (c, exit_code, exit_sig, coredump, 0); 879 951 #endif /* !KMK */ 880 952 881 c->file->update_status = 2;953 c->file->update_status = child_failed == MAKE_FAILURE ? us_failed : us_question; 882 954 if (delete_on_error == -1) 883 955 { … … 893 965 { 894 966 /* The commands failed, but we don't care. */ 895 child_error (c->file->name, 896 exit_code, exit_sig, coredump, 1); 967 child_error (c, exit_code, exit_sig, coredump, 1); 897 968 child_failed = 0; 898 969 } … … 907 978 the target was not completely remade. So we treat 908 979 this as if a command had failed. */ 909 c->file->update_status = 2;980 c->file->update_status = us_failed; 910 981 } 911 982 else 912 983 { 984 #ifndef NO_OUTPUT_SYNC 985 /* If we're sync'ing per line, write the previous line's 986 output before starting the next one. */ 987 if (output_sync == OUTPUT_SYNC_LINE) 988 output_dump (&c->output); 989 #endif 913 990 /* Check again whether to start remotely. 914 991 Whether or not we want to changes over time. … … 928 1005 } 929 1006 930 if (c->file->update_status != 0)1007 if (c->file->update_status != us_success) 931 1008 /* We failed to start the commands. */ 932 1009 delete_child_targets (c); … … 936 1013 without an unignored error. Now the target has been 937 1014 successfully updated. */ 938 c->file->update_status = 0;1015 c->file->update_status = us_success; 939 1016 } 940 1017 941 /* When we get here, all the commands for C->file are finished 942 (or aborted) and C->file->update_status contains 0 or 2. But 943 C->file->command_state is still cs_running if all the commands 1018 /* When we get here, all the commands for c->file are finished. */ 1019 1020 #ifndef NO_OUTPUT_SYNC 1021 /* Synchronize any remaining parallel output. */ 1022 output_dump (&c->output); 1023 #endif 1024 1025 /* At this point c->file->update_status is success or failed. But 1026 c->file->command_state is still cs_running if all the commands 944 1027 ran; notice_finish_file looks for cs_running to tell it that 945 1028 it's interesting to check the file's modtime again now. */ … … 980 1063 /* fatal_error_signal will die with the right signal. */ 981 1064 !handling_fatal_signal) 982 die ( 2);1065 die (child_failed); 983 1066 984 1067 /* Only block for one child. */ … … 998 1081 print_job_time (child); 999 1082 #endif 1083 output_close (&child->output); 1084 1000 1085 if (!jobserver_tokens) 1001 fatal (NILF, "INTERNAL: Freeing child %p (%s) but no tokens left!\n",1002 (void *)child, child->file->name);1086 ONS (fatal, NILF, "INTERNAL: Freeing child %p (%s) but no tokens left!\n", 1087 child, child->file->name); 1003 1088 1004 1089 /* If we're using the jobserver and this child is not the only outstanding 1005 1090 job, put a token back into the pipe for it. */ 1006 1091 1007 if (job_fds[1] >= 0 && jobserver_tokens > 1) 1008 { 1009 char token = '+'; 1010 int r; 1011 1012 /* Write a job token back to the pipe. */ 1013 1014 EINTRLOOP (r, write (job_fds[1], &token, 1)); 1015 if (r != 1) 1016 pfatal_with_name (_("write jobserver")); 1017 1092 if (jobserver_enabled () && jobserver_tokens > 1) 1093 { 1094 jobserver_release (1); 1018 1095 DB (DB_JOBS, (_("Released token for child %p (%s).\n"), 1019 1096 (void *)child, child->file->name)); … … 1029 1106 register unsigned int i; 1030 1107 for (i = 0; i < child->file->cmds->ncommand_lines; ++i) 1031 1108 free (child->command_lines[i]); 1032 1109 free (child->command_lines); 1033 1110 } … … 1037 1114 register char **ep = child->environment; 1038 1115 while (*ep != 0) 1039 1116 free (*ep++); 1040 1117 free (child->environment); 1041 1118 } … … 1081 1158 #endif 1082 1159 1083 #ifdef MAKE_JOBSERVER1084 RETSIGTYPE1085 job_noop (int sig UNUSED)1086 {1087 }1088 /* Set the child handler action flags to FLAGS. */1089 static void1090 set_child_handler_action_flags (int set_handler, int set_alarm)1091 {1092 struct sigaction sa;1093 int rval = 0;1094 1095 #if defined(__EMX__) && !defined(__KLIBC__) /* bird */1096 /* The child handler must be turned off here. */1097 signal (SIGCHLD, SIG_DFL);1098 #endif1099 1100 memset (&sa, '\0', sizeof sa);1101 sa.sa_handler = child_handler;1102 sa.sa_flags = set_handler ? 0 : SA_RESTART;1103 #if defined SIGCHLD1104 rval = sigaction (SIGCHLD, &sa, NULL);1105 #endif1106 #if defined SIGCLD && SIGCLD != SIGCHLD1107 rval = sigaction (SIGCLD, &sa, NULL);1108 #endif1109 if (rval != 0)1110 fprintf (stderr, "sigaction: %s (%d)\n", strerror (errno), errno);1111 #if defined SIGALRM1112 if (set_alarm)1113 {1114 /* If we're about to enter the read(), set an alarm to wake up in a1115 second so we can check if the load has dropped and we can start more1116 work. On the way out, turn off the alarm and set SIG_DFL. */1117 alarm (set_handler ? 1 : 0);1118 sa.sa_handler = set_handler ? job_noop : SIG_DFL;1119 sa.sa_flags = 0;1120 sigaction (SIGALRM, &sa, NULL);1121 }1122 #endif1123 }1124 #endif1125 1126 1127 1160 /* Start a job to run the commands specified in CHILD. 1128 1161 CHILD is updated to reflect the commands and ID of the child process. 1129 1162 1130 1163 NOTE: On return fatal signals are blocked! The caller is responsible 1131 for calling `unblock_sigs', once the new child is safely on the chain so1164 for calling 'unblock_sigs', once the new child is safely on the chain so 1132 1165 it can be cleaned up in the event of a fatal signal. */ 1133 1166 … … 1135 1168 start_job_command (struct child *child) 1136 1169 { 1137 #if !defined(_AMIGA) && !defined(WINDOWS32) 1138 static int bad_stdin = -1; 1139 #endif 1170 int flags; 1140 1171 char *p; 1141 /* Must be volatile to silence bogus GCC warning about longjmp/vfork. */1142 /*volatile*/ int flags;1143 1172 #ifdef VMS 1144 1173 char *argv; … … 1163 1192 the flags specified globally for this target. */ 1164 1193 flags = (child->file->command_flags 1165 1194 | child->file->cmds->lines_flags[child->command_line - 1]); 1166 1195 1167 1196 p = child->command_ptr; … … 1171 1200 { 1172 1201 if (*p == '@') 1173 1202 flags |= COMMANDS_SILENT; 1174 1203 else if (*p == '+') 1175 1204 flags |= COMMANDS_RECURSE; 1176 1205 else if (*p == '-') 1177 1206 child->noerror = 1; 1178 1207 #ifdef CONFIG_WITH_COMMANDS_FUNC 1179 1208 else if (*p == '%') 1180 1209 flags |= COMMAND_GETTER_SKIP_IT; 1181 1210 #endif 1182 else if (!isblank ((unsigned char)*p)) 1211 /* Don't skip newlines. */ 1212 else if (!ISBLANK (*p)) 1183 1213 #ifndef CONFIG_WITH_KMK_BUILTIN 1184 1214 break; … … 1195 1225 } 1196 1226 1227 child->recursive = ((flags & COMMANDS_RECURSE) != 0); 1228 1197 1229 /* Update the file's command flags with any new ones we found. We only 1198 1230 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are … … 1201 1233 order to really fix this, we'll have to keep a lines_flags for every 1202 1234 actual line, after expansion. */ 1203 child->file->cmds->lines_flags[child->command_line - 1] 1204 |= flags & COMMANDS_RECURSE; 1235 child->file->cmds->lines_flags[child->command_line - 1] |= flags & COMMANDS_RECURSE; 1236 1237 /* POSIX requires that a recipe prefix after a backslash-newline should 1238 be ignored. Remove it now so the output is correct. */ 1239 { 1240 char prefix = child->file->cmds->recipe_prefix; 1241 char *p1, *p2; 1242 p1 = p2 = p; 1243 while (*p1 != '\0') 1244 { 1245 *(p2++) = *p1; 1246 if (p1[0] == '\n' && p1[1] == prefix) 1247 ++p1; 1248 ++p1; 1249 } 1250 *p2 = *p1; 1251 } 1205 1252 1206 1253 /* Figure out an argument list from this command line. */ 1207 1208 1254 { 1209 1255 char *end = 0; 1210 1256 #ifdef VMS 1257 /* Skip any leading whitespace */ 1258 while (*p) 1259 { 1260 if (!ISSPACE (*p)) 1261 { 1262 if (*p != '\\') 1263 break; 1264 if ((p[1] != '\n') && (p[1] != 'n') && (p[1] != 't')) 1265 break; 1266 } 1267 p++; 1268 } 1269 1211 1270 argv = p; 1271 /* Although construct_command_argv contains some code for VMS, it was/is 1272 not called/used. Please note, for VMS argv is a string (not an array 1273 of strings) which contains the complete command line, which for 1274 multi-line variables still includes the newlines. So detect newlines 1275 and set 'end' (which is used for child->command_ptr) instead of 1276 (re-)writing construct_command_argv */ 1277 if (!one_shell) 1278 { 1279 char *s = p; 1280 int instring = 0; 1281 while (*s) 1282 { 1283 if (*s == '"') 1284 instring = !instring; 1285 else if (*s == '\\' && !instring && *(s+1) != 0) 1286 s++; 1287 else if (*s == '\n' && !instring) 1288 { 1289 end = s; 1290 break; 1291 } 1292 ++s; 1293 } 1294 } 1212 1295 #else 1213 1296 argv = construct_command_argv (p, &end, child->file, 1214 1215 1297 child->file->cmds->lines_flags[child->command_line - 1], 1298 &child->sh_batch_file); 1216 1299 #endif 1217 1300 if (end == NULL) … … 1219 1302 else 1220 1303 { 1221 1222 1304 *end++ = '\0'; 1305 child->command_ptr = end; 1223 1306 } 1224 1307 } 1225 1308 1226 /* If -q was given, say that updating `failed' if there was any text on the1227 command line, or `succeeded' otherwise. The exit status of 1 tells the1228 user that -q is saying `something to do'; the exit status for a random1309 /* If -q was given, say that updating 'failed' if there was any text on the 1310 command line, or 'succeeded' otherwise. The exit status of 1 tells the 1311 user that -q is saying 'something to do'; the exit status for a random 1229 1312 error is 2. */ 1230 1313 if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE)) … … 1234 1317 free (argv); 1235 1318 #endif 1236 child->file->update_status = 1; 1237 notice_finished_file (child->file); 1238 return; 1319 #ifdef VMS 1320 /* On VMS, argv[0] can be a null string here */ 1321 if (argv[0] != 0) 1322 { 1323 #endif 1324 child->file->update_status = us_question; 1325 notice_finished_file (child->file); 1326 return; 1327 #ifdef VMS 1328 } 1329 #endif 1239 1330 } 1240 1331 … … 1242 1333 { 1243 1334 /* Go on to the next command. It might be the recursive one. 1244 1335 We construct ARGV only to find the end of the command line. */ 1245 1336 #ifndef VMS 1246 1337 if (argv) … … 1261 1352 /* This line has no commands. Go to the next. */ 1262 1353 if (job_next_command (child)) 1263 1354 start_job_command (child); 1264 1355 else 1265 1266 1356 { 1357 /* No more commands. Make sure we're "running"; we might not be if 1267 1358 (e.g.) all commands were skipped due to -n. */ 1268 1359 set_command_state (child->file, cs_running); 1269 child->file->update_status = 0; 1270 notice_finished_file (child->file); 1271 } 1360 child->file->update_status = us_success; 1361 notice_finished_file (child->file); 1362 } 1363 1364 OUTPUT_UNSET(); 1272 1365 return; 1273 1366 } 1274 1367 1275 /* Print out the command. If silent, we call `message' with null so it 1276 can log the working directory before the command's own error messages 1277 appear. */ 1368 /* Are we going to synchronize this command's output? Do so if either we're 1369 in SYNC_RECURSE mode or this command is not recursive. We'll also check 1370 output_sync separately below in case it changes due to error. */ 1371 child->output.syncout = output_sync && (output_sync == OUTPUT_SYNC_RECURSE 1372 || !(flags & COMMANDS_RECURSE)); 1373 OUTPUT_SET (&child->output); 1374 1375 #ifndef NO_OUTPUT_SYNC 1376 if (! child->output.syncout) 1377 /* We don't want to sync this command: to avoid misordered 1378 output ensure any already-synced content is written. */ 1379 output_dump (&child->output); 1380 #endif 1381 1382 /* Print the command if appropriate. */ 1278 1383 #ifdef CONFIG_PRETTY_COMMAND_PRINTING 1279 1384 if ( pretty_command_printing … … 1283 1388 unsigned i; 1284 1389 for (i = 0; argv[i]; i++) 1285 message (0, "%s'%s'%s", i ? "\t" : "> ", argv[i], argv[i + 1] ? " \\" : "");1390 OSSS ( message, 0, "%s'%s'%s", i ? "\t" : "> ", argv[i], argv[i + 1] ? " \\" : ""); 1286 1391 } 1287 1392 else 1288 1393 #endif /* CONFIG_PRETTY_COMMAND_PRINTING */ 1289 message (0, (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag)) 1290 ? "%s" : (char *) 0, p); 1394 if (just_print_flag || trace_flag 1395 || (!(flags & COMMANDS_SILENT) && !silent_flag)) 1396 OS (message, 0, "%s", p); 1291 1397 1292 1398 /* Tell update_goal_chain that a command has been started on behalf of … … 1308 1414 if ( 1309 1415 #if defined __MSDOS__ || defined (__EMX__) 1310 unixy_shell 1416 unixy_shell /* the test is complicated and we already did it */ 1311 1417 #else 1312 (argv[0] && is_bourne_compatible_shell (argv[0]))1418 (argv[0] && is_bourne_compatible_shell (argv[0])) 1313 1419 #endif 1314 1420 && (argv[1] && argv[1][0] == '-' 1315 1316 1317 1318 1421 && 1422 ((argv[1][1] == 'c' && argv[1][2] == '\0') 1423 || 1424 (argv[1][1] == 'e' && argv[1][2] == 'c' && argv[1][3] == '\0'))) 1319 1425 && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0') 1320 1426 && argv[3] == NULL) … … 1395 1501 #endif /* CONFIG_WITH_KMK_BUILTIN */ 1396 1502 1503 /* We're sure we're going to invoke a command: set up the output. */ 1504 output_start (); 1505 1397 1506 /* Flush the output streams so they won't have things written twice. */ 1398 1507 … … 1400 1509 fflush (stderr); 1401 1510 1402 #ifndef VMS 1403 #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__) 1404 1405 /* Set up a bad standard input that reads from a broken pipe. */ 1406 1407 if (bad_stdin == -1) 1408 { 1409 /* Make a file descriptor that is the read end of a broken pipe. 1410 This will be used for some children's standard inputs. */ 1411 int pd[2]; 1412 if (pipe (pd) == 0) 1413 { 1414 /* Close the write side. */ 1415 (void) close (pd[1]); 1416 /* Save the read side. */ 1417 bad_stdin = pd[0]; 1418 1419 /* Set the descriptor to close on exec, so it does not litter any 1420 child's descriptor table. When it is dup2'd onto descriptor 0, 1421 that descriptor will not close on exec. */ 1422 CLOSE_ON_EXEC (bad_stdin); 1423 } 1424 } 1425 1426 #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */ 1427 1428 /* Decide whether to give this child the `good' standard input 1429 (one that points to the terminal or whatever), or the `bad' one 1511 /* Decide whether to give this child the 'good' standard input 1512 (one that points to the terminal or whatever), or the 'bad' one 1430 1513 that points to the read side of a broken pipe. */ 1431 1514 … … 1433 1516 if (child->good_stdin) 1434 1517 good_stdin_used = 1; 1435 1436 #endif /* !VMS */1437 1518 1438 1519 child->deleted = 0; … … 1452 1533 int is_remote, id, used_stdin; 1453 1534 if (start_remote_job (argv, child->environment, 1454 child->good_stdin ? 0 : bad_stdin,1455 1535 child->good_stdin ? 0 : get_bad_stdin (), 1536 &is_remote, &id, &used_stdin)) 1456 1537 /* Don't give up; remote execution may fail for various reasons. If 1457 1538 so, simply run the job locally. */ 1458 1539 goto run_local; 1459 1540 else 1460 1461 1462 1463 1464 1465 1466 1467 1468 1541 { 1542 if (child->good_stdin && !used_stdin) 1543 { 1544 child->good_stdin = 0; 1545 good_stdin_used = 0; 1546 } 1547 child->remote = is_remote; 1548 child->pid = id; 1549 } 1469 1550 } 1470 1551 else … … 1481 1562 1482 1563 #ifdef VMS 1483 if (!child_execute_job (argv, child)) { 1484 /* Fork failed! */ 1485 perror_with_name ("vfork", ""); 1486 goto error; 1487 } 1564 if (!child_execute_job (child, argv)) 1565 { 1566 /* Fork failed! */ 1567 perror_with_name ("fork", ""); 1568 goto error; 1569 } 1488 1570 1489 1571 #else … … 1491 1573 parent_environ = environ; 1492 1574 1493 # ifdef __EMX__ 1494 /* If we aren't running a recursive command and we have a jobserver 1495 pipe, close it before exec'ing. */ 1496 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0) 1497 { 1498 CLOSE_ON_EXEC (job_fds[0]); 1499 CLOSE_ON_EXEC (job_fds[1]); 1500 } 1501 if (job_rfd >= 0) 1502 CLOSE_ON_EXEC (job_rfd); 1503 1504 /* Never use fork()/exec() here! Use spawn() instead in exec_command() */ 1505 child->pid = child_execute_job (child->good_stdin ? 0 : bad_stdin, 1, 1506 argv, child->environment); 1575 jobserver_pre_child (flags & COMMANDS_RECURSE); 1576 1577 child->pid = child_execute_job (&child->output, child->good_stdin, argv, child->environment); 1578 1579 environ = parent_environ; /* Restore value child may have clobbered. */ 1580 jobserver_post_child (flags & COMMANDS_RECURSE); 1581 1507 1582 if (child->pid < 0) 1508 { 1509 /* spawn failed! */ 1510 unblock_sigs (); 1511 perror_with_name ("spawn", ""); 1512 goto error; 1513 } 1514 1515 /* undo CLOSE_ON_EXEC() after the child process has been started */ 1516 if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0) 1517 { 1518 fcntl (job_fds[0], F_SETFD, 0); 1519 fcntl (job_fds[1], F_SETFD, 0); 1520 } 1521 if (job_rfd >= 0) 1522 fcntl (job_rfd, F_SETFD, 0); 1523 1524 #else /* !__EMX__ */ 1525 volatile_argv = argv; /* shut up gcc */ 1526 volatile_flags = flags; /* ditto */ 1527 1528 child->pid = vfork (); 1529 environ = parent_environ; /* Restore value child may have clobbered. */ 1530 argv = volatile_argv; /* shut up gcc */ 1531 if (child->pid == 0) 1532 { 1533 /* We are the child side. */ 1534 unblock_sigs (); 1535 1536 /* If we aren't running a recursive command and we have a jobserver 1537 pipe, close it before exec'ing. */ 1538 if (!(volatile_flags & COMMANDS_RECURSE) && job_fds[0] >= 0) 1539 { 1540 close (job_fds[0]); 1541 close (job_fds[1]); 1542 } 1543 if (job_rfd >= 0) 1544 close (job_rfd); 1545 1546 #ifdef SET_STACK_SIZE 1547 /* Reset limits, if necessary. */ 1548 if (stack_limit.rlim_cur) 1549 setrlimit (RLIMIT_STACK, &stack_limit); 1550 #endif 1551 1552 child_execute_job (child->good_stdin ? 0 : bad_stdin, 1, 1553 argv, child->environment); 1554 } 1555 else if (child->pid < 0) 1556 { 1557 /* Fork failed! */ 1558 unblock_sigs (); 1559 perror_with_name ("vfork", ""); 1560 goto error; 1561 } 1562 # endif /* !__EMX__ */ 1583 { 1584 /* Fork failed! */ 1585 unblock_sigs (); 1586 perror_with_name ("fork", ""); 1587 goto error; 1588 } 1563 1589 #endif /* !VMS */ 1564 1590 } 1565 1591 1566 #else 1592 #else /* __MSDOS__ or Amiga or WINDOWS32 */ 1567 1593 #ifdef __MSDOS__ 1568 1594 { … … 1572 1598 dos_status = 0; 1573 1599 1574 /* We call `system' to do the job of the SHELL, since stock DOS1575 shell is too dumb. Our `system' knows how to handle long1600 /* We call 'system' to do the job of the SHELL, since stock DOS 1601 shell is too dumb. Our 'system' knows how to handle long 1576 1602 command lines even if pipes/redirection is needed; it will only 1577 1603 call COMMAND.COM when its internal commands are used. */ 1578 1604 if (execute_by_shell) 1579 1605 { 1580 1581 /* We don't have a way to pass environment to `system',1582 1583 1584 1585 1586 1587 /* If we have a *real* shell, tell `system' to call1588 1589 1590 1591 1592 command lines the DJGPP way, so we must use `system'. */1593 cmdline = argv[2];/* get past "shell -c" */1594 1595 1596 1597 1598 1599 execute_by_shell = 0;/* for the next time */1606 char *cmdline = argv[0]; 1607 /* We don't have a way to pass environment to 'system', 1608 so we need to save and restore ours, sigh... */ 1609 char **parent_environ = environ; 1610 1611 environ = child->environment; 1612 1613 /* If we have a *real* shell, tell 'system' to call 1614 it to do everything for us. */ 1615 if (unixy_shell) 1616 { 1617 /* A *real* shell on MSDOS may not support long 1618 command lines the DJGPP way, so we must use 'system'. */ 1619 cmdline = argv[2]; /* get past "shell -c" */ 1620 } 1621 1622 dos_command_running = 1; 1623 proc_return = system (cmdline); 1624 environ = parent_environ; 1625 execute_by_shell = 0; /* for the next time */ 1600 1626 } 1601 1627 else 1602 1628 { 1603 1604 1629 dos_command_running = 1; 1630 proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment); 1605 1631 } 1606 1632 … … 1631 1657 DeleteFile (amiga_bname); /* Ignore errors. */ 1632 1658 } 1633 #endif 1659 #endif /* Amiga */ 1634 1660 #ifdef WINDOWS32 1635 1661 { 1636 1662 HANDLE hPID; 1637 1663 char* arg0; 1664 int outfd = FD_STDOUT; 1665 int errfd = FD_STDERR; 1638 1666 1639 1667 /* make UNC paths safe for CreateProcess -- backslash format */ … … 1645 1673 1646 1674 /* make sure CreateProcess() has Path it needs */ 1647 sync_Path_environment(); 1648 1649 hPID = process_easy(argv, child->environment); 1675 sync_Path_environment (); 1676 1677 #ifndef NO_OUTPUT_SYNC 1678 /* Divert child output if output_sync in use. */ 1679 if (child->output.syncout) 1680 { 1681 if (child->output.out >= 0) 1682 outfd = child->output.out; 1683 if (child->output.err >= 0) 1684 errfd = child->output.err; 1685 } 1686 #else 1687 outfd = errfd = -1; 1688 #endif 1689 hPID = process_easy (argv, child->environment, outfd, errfd); 1650 1690 1651 1691 if (hPID != INVALID_HANDLE_VALUE) 1652 1692 child->pid = (pid_t) hPID; 1653 else { 1654 int i; 1655 unblock_sigs(); 1656 fprintf(stderr, 1657 _("process_easy() failed to launch process (e=%ld)\n"), 1658 process_last_err(hPID)); 1659 for (i = 0; argv[i]; i++) 1660 fprintf(stderr, "%s ", argv[i]); 1661 fprintf(stderr, _("\nCounted %d args in failed launch\n"), i); 1662 goto error; 1663 } 1693 else 1694 { 1695 int i; 1696 unblock_sigs (); 1697 fprintf (stderr, 1698 _("process_easy() failed to launch process (e=%ld)\n"), 1699 process_last_err (hPID)); 1700 for (i = 0; argv[i]; i++) 1701 fprintf (stderr, "%s ", argv[i]); 1702 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i); 1703 goto error; 1704 } 1664 1705 } 1665 1706 #endif /* WINDOWS32 */ 1666 #endif 1707 #endif /* __MSDOS__ or Amiga or WINDOWS32 */ 1667 1708 1668 1709 /* Bump the number of jobs started in this second. */ … … 1683 1724 #endif 1684 1725 1726 OUTPUT_UNSET(); 1685 1727 return; 1686 1728 1687 1729 error: 1688 child->file->update_status = 2;1730 child->file->update_status = us_failed; 1689 1731 notice_finished_file (child->file); 1690 1732 #ifdef KMK /* fix leak */ 1691 1733 goto cleanup_argv; 1692 1734 #else 1693 return;1735 OUTPUT_UNSET(); 1694 1736 #endif 1695 1737 } … … 1697 1739 /* Try to start a child running. 1698 1740 Returns nonzero if the child was started (and maybe finished), or zero if 1699 the load was too high and the child was put on the `waiting_jobs' chain. */1741 the load was too high and the child was put on the 'waiting_jobs' chain. */ 1700 1742 1701 1743 static int … … 1733 1775 #endif 1734 1776 #ifdef WINDOWS32 1735 1736 #endif 1737 1777 || (process_used_slots () >= MAXIMUM_WAIT_OBJECTS) 1778 #endif 1779 )) 1738 1780 { 1739 1781 #ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL … … 1783 1825 case cs_not_started: 1784 1826 /* All the command lines turned out to be empty. */ 1785 f->update_status = 0;1827 f->update_status = us_success; 1786 1828 /* FALLTHROUGH */ 1787 1829 … … 1799 1841 } 1800 1842 1801 /* Create a `struct child' for FILE and start its commands running. */1843 /* Create a 'struct child' for FILE and start its commands running. */ 1802 1844 1803 1845 void … … 1822 1864 #endif 1823 1865 1866 /* Start the command sequence, record it in a new 1867 'struct child', and add that to the chain. */ 1868 1869 c = xcalloc (sizeof (struct child)); 1870 output_init (&c->output); 1871 1872 c->file = file; 1873 c->sh_batch_file = NULL; 1874 1875 /* Cache dontcare flag because file->dontcare can be changed once we 1876 return. Check dontcare inheritance mechanism for details. */ 1877 c->dontcare = file->dontcare; 1878 1879 /* Start saving output in case the expansion uses $(info ...) etc. */ 1880 OUTPUT_SET (&c->output); 1881 1824 1882 /* Expand the command lines and store the results in LINES. */ 1825 1883 lines = xmalloc (cmds->ncommand_lines * sizeof (char *)); … … 1827 1885 { 1828 1886 /* Collapse backslash-newline combinations that are inside variable 1829 1830 1831 1832 1833 1887 or function references. These are left alone by the parser so 1888 that they will appear in the echoing of commands (where they look 1889 nice); and collapsed by construct_command_argv when it tokenizes. 1890 But letting them survive inside function invocations loses because 1891 we don't want the functions to see them as part of the text. */ 1834 1892 1835 1893 char *in, *out, *ref; 1836 1894 1837 1895 /* IN points to where in the line we are scanning. 1838 1839 1840 1896 OUT points to where in the line we are writing. 1897 When we collapse a backslash-newline combination, 1898 IN gets ahead of OUT. */ 1841 1899 1842 1900 in = out = cmds->command_lines[i]; 1843 1901 while ((ref = strchr (in, '$')) != 0) 1844 { 1845 ++ref; /* Move past the $. */ 1846 1847 if (out != in) 1848 /* Copy the text between the end of the last chunk 1849 we processed (where IN points) and the new chunk 1850 we are about to process (where REF points). */ 1851 memmove (out, in, ref - in); 1852 1853 /* Move both pointers past the boring stuff. */ 1854 out += ref - in; 1855 in = ref; 1856 1857 if (*ref == '(' || *ref == '{') 1858 { 1859 char openparen = *ref; 1860 char closeparen = openparen == '(' ? ')' : '}'; 1861 int count; 1862 char *p; 1863 1864 *out++ = *in++; /* Copy OPENPAREN. */ 1865 /* IN now points past the opening paren or brace. 1866 Count parens or braces until it is matched. */ 1867 count = 0; 1868 while (*in != '\0') 1869 { 1870 if (*in == closeparen && --count < 0) 1871 break; 1872 else if (*in == '\\' && in[1] == '\n') 1873 { 1874 /* We have found a backslash-newline inside a 1875 variable or function reference. Eat it and 1876 any following whitespace. */ 1877 1878 int quoted = 0; 1879 for (p = in - 1; p > ref && *p == '\\'; --p) 1880 quoted = !quoted; 1881 1882 if (quoted) 1883 /* There were two or more backslashes, so this is 1884 not really a continuation line. We don't collapse 1885 the quoting backslashes here as is done in 1886 collapse_continuations, because the line will 1887 be collapsed again after expansion. */ 1888 *out++ = *in++; 1889 else 1890 { 1891 /* Skip the backslash, newline and 1892 any following whitespace. */ 1893 in = next_token (in + 2); 1894 1895 /* Discard any preceding whitespace that has 1896 already been written to the output. */ 1897 while (out > ref 1898 && isblank ((unsigned char)out[-1])) 1899 --out; 1900 1901 /* Replace it all with a single space. */ 1902 *out++ = ' '; 1903 } 1904 } 1905 else 1906 { 1907 if (*in == openparen) 1908 ++count; 1909 1910 *out++ = *in++; 1911 } 1912 } 1913 } 1914 } 1902 { 1903 ++ref; /* Move past the $. */ 1904 1905 if (out != in) 1906 /* Copy the text between the end of the last chunk 1907 we processed (where IN points) and the new chunk 1908 we are about to process (where REF points). */ 1909 memmove (out, in, ref - in); 1910 1911 /* Move both pointers past the boring stuff. */ 1912 out += ref - in; 1913 in = ref; 1914 1915 if (*ref == '(' || *ref == '{') 1916 { 1917 char openparen = *ref; 1918 char closeparen = openparen == '(' ? ')' : '}'; 1919 char *outref; 1920 int count; 1921 char *p; 1922 1923 *out++ = *in++; /* Copy OPENPAREN. */ 1924 outref = out; 1925 /* IN now points past the opening paren or brace. 1926 Count parens or braces until it is matched. */ 1927 count = 0; 1928 while (*in != '\0') 1929 { 1930 if (*in == closeparen && --count < 0) 1931 break; 1932 else if (*in == '\\' && in[1] == '\n') 1933 { 1934 /* We have found a backslash-newline inside a 1935 variable or function reference. Eat it and 1936 any following whitespace. */ 1937 1938 int quoted = 0; 1939 for (p = in - 1; p > ref && *p == '\\'; --p) 1940 quoted = !quoted; 1941 1942 if (quoted) 1943 /* There were two or more backslashes, so this is 1944 not really a continuation line. We don't collapse 1945 the quoting backslashes here as is done in 1946 collapse_continuations, because the line will 1947 be collapsed again after expansion. */ 1948 *out++ = *in++; 1949 else 1950 { 1951 /* Skip the backslash, newline, and whitespace. */ 1952 in += 2; 1953 NEXT_TOKEN (in); 1954 1955 /* Discard any preceding whitespace that has 1956 already been written to the output. */ 1957 while (out > outref && ISBLANK (out[-1])) 1958 --out; 1959 1960 /* Replace it all with a single space. */ 1961 *out++ = ' '; 1962 } 1963 } 1964 else 1965 { 1966 if (*in == openparen) 1967 ++count; 1968 1969 *out++ = *in++; 1970 } 1971 } 1972 } 1973 } 1915 1974 1916 1975 /* There are no more references in this line to worry about. 1917 1976 Copy the remaining uninteresting text to the output. */ 1918 1977 if (out != in) 1919 1978 memmove (out, in, strlen (in) + 1); 1920 1979 1921 1980 /* Finally, expand the line. */ 1981 cmds->fileinfo.offset = i; 1922 1982 lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i], 1923 file); 1924 } 1925 1926 /* Start the command sequence, record it in a new 1927 `struct child', and add that to the chain. */ 1928 1929 c = xcalloc (sizeof (struct child)); 1930 c->file = file; 1983 file); 1984 } 1985 1986 cmds->fileinfo.offset = 0; 1931 1987 c->command_lines = lines; 1932 c->sh_batch_file = NULL;1933 1988 #ifdef CONFIG_WITH_PRINT_TIME_SWITCH 1934 1989 c->start_ts = -1; 1935 1990 #endif 1936 1937 /* Cache dontcare flag because file->dontcare can be changed once we1938 return. Check dontcare inheritance mechanism for details. */1939 c->dontcare = file->dontcare;1940 1991 1941 1992 /* Fetch the first command line to be run. */ … … 1962 2013 this is where the old parallel job code waits, so... */ 1963 2014 1964 else if (job _fds[0] >= 0)2015 else if (jobserver_enabled ()) 1965 2016 while (1) 1966 2017 { 1967 char token; 1968 int got_token; 1969 int saved_errno; 2018 int got_token; 1970 2019 1971 2020 DB (DB_JOBS, ("Need a job token; we %shave children\n", … … 1976 2025 break; 1977 2026 1978 /* Read a token. As long as there's no token available we'll block. 1979 We enable interruptible system calls before the read(2) so that if 1980 we get a SIGCHLD while we're waiting, we'll return with EINTR and 1981 we can process the death(s) and return tokens to the free pool. 1982 1983 Once we return from the read, we immediately reinstate restartable 1984 system calls. This allows us to not worry about checking for 1985 EINTR on all the other system calls in the program. 1986 1987 There is one other twist: there is a span between the time 1988 reap_children() does its last check for dead children and the time 1989 the read(2) call is entered, below, where if a child dies we won't 1990 notice. This is extremely serious as it could cause us to 1991 deadlock, given the right set of events. 1992 1993 To avoid this, we do the following: before we reap_children(), we 1994 dup(2) the read FD on the jobserver pipe. The read(2) call below 1995 uses that new FD. In the signal handler, we close that FD. That 1996 way, if a child dies during the section mentioned above, the 1997 read(2) will be invoked with an invalid FD and will return 1998 immediately with EBADF. */ 1999 2000 /* Make sure we have a dup'd FD. */ 2001 if (job_rfd < 0) 2002 { 2003 DB (DB_JOBS, ("Duplicate the job FD\n")); 2004 job_rfd = dup (job_fds[0]); 2005 } 2027 /* Prepare for jobserver token acquisition. */ 2028 jobserver_pre_acquire (); 2006 2029 2007 2030 /* Reap anything that's currently waiting. */ … … 2009 2032 2010 2033 /* Kick off any jobs we have waiting for an opportunity that 2011 can run now (i ewaiting for load). */2034 can run now (i.e., waiting for load). */ 2012 2035 start_waiting_jobs (); 2013 2036 2014 /* If our "free" slot has become available, use it; we don't need an 2015 actual token. */ 2037 /* If our "free" slot is available, use it; we don't need a token. */ 2016 2038 if (!jobserver_tokens) 2017 2039 break; … … 2020 2042 waiting for a token. */ 2021 2043 if (!children) 2022 fatal (NILF, "INTERNAL: no children as we go to sleep on read\n"); 2023 2024 /* Set interruptible system calls, and read() for a job token. */ 2025 set_child_handler_action_flags (1, waiting_jobs != NULL); 2026 got_token = read (job_rfd, &token, 1); 2027 saved_errno = errno; 2028 set_child_handler_action_flags (0, waiting_jobs != NULL); 2044 O (fatal, NILF, "INTERNAL: no children as we go to sleep on read\n"); 2045 2046 /* Get a token. */ 2047 got_token = jobserver_acquire (waiting_jobs != NULL); 2029 2048 2030 2049 /* If we got one, we're done here. */ 2031 2050 if (got_token == 1) 2032 2051 { 2033 2052 DB (DB_JOBS, (_("Obtained token for child %p (%s).\n"), … … 2035 2054 break; 2036 2055 } 2037 2038 /* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,2039 go back and reap_children(), and try again. */2040 errno = saved_errno;2041 if (errno != EINTR && errno != EBADF)2042 pfatal_with_name (_("read jobs pipe"));2043 if (errno == EBADF)2044 DB (DB_JOBS, ("Read returned EBADF.\n"));2045 2056 } 2046 2057 #endif 2047 2058 2048 2059 ++jobserver_tokens; 2060 2061 /* Trace the build. 2062 Use message here so that changes to working directories are logged. */ 2063 if (trace_flag) 2064 { 2065 char *newer = allocated_variable_expand_for_file ("$?", c->file); 2066 const char *nm; 2067 2068 if (! cmds->fileinfo.filenm) 2069 nm = _("<builtin>"); 2070 else 2071 { 2072 char *n = alloca (strlen (cmds->fileinfo.filenm) + 1 + 11 + 1); 2073 sprintf (n, "%s:%lu", cmds->fileinfo.filenm, cmds->fileinfo.lineno); 2074 nm = n; 2075 } 2076 2077 if (newer[0] == '\0') 2078 OSS (message, 0, 2079 _("%s: target '%s' does not exist"), nm, c->file->name); 2080 else 2081 OSSS (message, 0, 2082 _("%s: update target '%s' due to: %s"), nm, c->file->name, newer); 2083 2084 free (newer); 2085 } 2049 2086 2050 2087 /* The job is now primed. Start it running. 2051 2088 (This will notice if there is in fact no recipe.) */ 2052 if (cmds->fileinfo.filenm)2053 DB (DB_BASIC, (_("Invoking recipe from %s:%lu to update target `%s'.\n"),2054 cmds->fileinfo.filenm, cmds->fileinfo.lineno,2055 c->file->name));2056 else2057 DB (DB_BASIC, (_("Invoking builtin recipe to update target `%s'.\n"),2058 c->file->name));2059 2060 2061 2089 start_waiting_job (c); 2062 2090 … … 2064 2092 if (job_slots == 1 || not_parallel) 2065 2093 /* Since there is only one job slot, make things run linearly. 2066 Wait for the child to die, setting the state to `cs_finished'. */2094 Wait for the child to die, setting the state to 'cs_finished'. */ 2067 2095 while (file->command_state == cs_running) 2068 2096 reap_children (1, 0); … … 2088 2116 #endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */ 2089 2117 2118 OUTPUT_UNSET (); 2090 2119 return; 2091 2120 } … … 2102 2131 /* There are no more lines in the expansion of this line. */ 2103 2132 if (child->command_line == child->file->cmds->ncommand_lines) 2104 { 2105 /* There are no more lines to be expanded. */ 2106 child->command_ptr = 0; 2107 return 0; 2108 } 2133 { 2134 /* There are no more lines to be expanded. */ 2135 child->command_ptr = 0; 2136 child->file->cmds->fileinfo.offset = 0; 2137 return 0; 2138 } 2109 2139 else 2110 /* Get the next line to run. */ 2111 child->command_ptr = child->command_lines[child->command_line++]; 2112 } 2140 /* Get the next line to run. */ 2141 child->command_ptr = child->command_lines[child->command_line++]; 2142 } 2143 2144 child->file->cmds->fileinfo.offset = child->command_line - 1; 2113 2145 return 1; 2114 2146 } … … 2181 2213 /* Complain only once for the same error. */ 2182 2214 if (lossage == -1 || errno != lossage) 2183 2184 2185 2186 error (NILF,2187 2188 2189 2190 2215 { 2216 if (errno == 0) 2217 /* An errno value of zero means getloadavg is just unsupported. */ 2218 O (error, NILF, 2219 _("cannot enforce load limits on this operating system")); 2220 else 2221 perror_with_name (_("cannot enforce load limit: "), "getloadavg"); 2222 } 2191 2223 lossage = errno; 2192 2224 load = 0; … … 2251 2283 2252 2284 /* Try to start that job. We break out of the loop as soon 2253 2285 as start_waiting_job puts one back on the waiting list. */ 2254 2286 } 2255 2287 while (start_waiting_job (job) && waiting_jobs != 0); … … 2264 2296 # if defined __EMX__ 2265 2297 int 2266 child_execute_job ( int stdin_fd, int stdout_fd, char **argv, char **envp)2298 child_execute_job (struct output *out, int good_stdin, char **argv, char **envp) 2267 2299 { 2268 2300 int pid; 2269 /* stdin_fd == 0 means: nothing to do for stdin; 2270 stdout_fd == 1 means: nothing to do for stdout */ 2271 int save_stdin = (stdin_fd != 0) ? dup (0) : 0; 2272 int save_stdout = (stdout_fd != 1) ? dup (1): 1; 2273 2274 /* < 0 only if dup() failed */ 2275 if (save_stdin < 0) 2276 fatal (NILF, _("no more file handles: could not duplicate stdin\n")); 2277 if (save_stdout < 0) 2278 fatal (NILF, _("no more file handles: could not duplicate stdout\n")); 2279 2280 /* Close unnecessary file handles for the child. */ 2281 if (save_stdin != 0) 2282 CLOSE_ON_EXEC (save_stdin); 2283 if (save_stdout != 1) 2284 CLOSE_ON_EXEC (save_stdout); 2285 2286 /* Connect the pipes to the child process. */ 2287 if (stdin_fd != 0) 2288 (void) dup2 (stdin_fd, 0); 2289 if (stdout_fd != 1) 2290 (void) dup2 (stdout_fd, 1); 2291 2292 /* stdin_fd and stdout_fd must be closed on exit because we are 2293 still in the parent process */ 2294 if (stdin_fd != 0) 2295 CLOSE_ON_EXEC (stdin_fd); 2296 if (stdout_fd != 1) 2297 CLOSE_ON_EXEC (stdout_fd); 2301 int fdin = good_stdin ? FD_STDIN : get_bad_stdin (); 2302 int fdout = FD_STDOUT; 2303 int fderr = FD_STDERR; 2304 int save_fdin = -1; 2305 int save_fdout = -1; 2306 int save_fderr = -1; 2307 2308 /* Divert child output if we want to capture output. */ 2309 if (out && out->syncout) 2310 { 2311 if (out->out >= 0) 2312 fdout = out->out; 2313 if (out->err >= 0) 2314 fderr = out->err; 2315 } 2316 2317 /* For each FD which needs to be redirected first make a dup of the standard 2318 FD to save and mark it close on exec so our child won't see it. Then 2319 dup2() the standard FD to the redirect FD, and also mark the redirect FD 2320 as close on exec. */ 2321 if (fdin != FD_STDIN) 2322 { 2323 save_fdin = dup (FD_STDIN); 2324 if (save_fdin < 0) 2325 O (fatal, NILF, _("no more file handles: could not duplicate stdin\n")); 2326 CLOSE_ON_EXEC (save_fdin); 2327 2328 dup2 (fdin, FD_STDIN); 2329 CLOSE_ON_EXEC (fdin); 2330 } 2331 2332 if (fdout != FD_STDOUT) 2333 { 2334 save_fdout = dup (FD_STDOUT); 2335 if (save_fdout < 0) 2336 O (fatal, NILF, 2337 _("no more file handles: could not duplicate stdout\n")); 2338 CLOSE_ON_EXEC (save_fdout); 2339 2340 dup2 (fdout, FD_STDOUT); 2341 CLOSE_ON_EXEC (fdout); 2342 } 2343 2344 if (fderr != FD_STDERR) 2345 { 2346 if (fderr != fdout) 2347 { 2348 save_fderr = dup (FD_STDERR); 2349 if (save_fderr < 0) 2350 O (fatal, NILF, 2351 _("no more file handles: could not duplicate stderr\n")); 2352 CLOSE_ON_EXEC (save_fderr); 2353 } 2354 2355 dup2 (fderr, FD_STDERR); 2356 CLOSE_ON_EXEC (fderr); 2357 } 2298 2358 2299 2359 /* Run the command. */ 2300 2360 pid = exec_command (argv, envp); 2301 2361 2302 /* Restore stdout/stdin of the parent and close temporary FDs. */2303 if (s tdin_fd != 0)2304 { 2305 if (dup2 (save_ stdin, 0) != 0)2306 fatal (NILF, _("Could not restore stdin\n"));2362 /* Restore stdout/stdin/stderr of the parent and close temporary FDs. */ 2363 if (save_fdin >= 0) 2364 { 2365 if (dup2 (save_fdin, FD_STDIN) != FD_STDIN) 2366 O (fatal, NILF, _("Could not restore stdin\n")); 2307 2367 else 2308 close (save_ stdin);2309 } 2310 2311 if (s tdout_fd != 1)2312 { 2313 if (dup2 (save_ stdout, 1) != 1)2314 fatal (NILF, _("Could not restore stdout\n"));2368 close (save_fdin); 2369 } 2370 2371 if (save_fdout >= 0) 2372 { 2373 if (dup2 (save_fdout, FD_STDOUT) != FD_STDOUT) 2374 O (fatal, NILF, _("Could not restore stdout\n")); 2315 2375 else 2316 close (save_stdout); 2376 close (save_fdout); 2377 } 2378 2379 if (save_fderr >= 0) 2380 { 2381 if (dup2 (save_fderr, FD_STDERR) != FD_STDERR) 2382 O (fatal, NILF, _("Could not restore stderr\n")); 2383 else 2384 close (save_fderr); 2317 2385 } 2318 2386 … … 2322 2390 #elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS) 2323 2391 2324 /* UNIX: 2325 Replace the current process with one executing the command in ARGV. 2326 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is 2327 the environment of the new program. This function does not return. */ 2328 void 2329 child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp) 2392 /* POSIX: 2393 Create a child process executing the command in ARGV. 2394 ENVP is the environment of the new program. Returns the PID or -1. */ 2395 int 2396 child_execute_job (struct output *out, int good_stdin, char **argv, char **envp) 2330 2397 { 2331 if (stdin_fd != 0) 2332 (void) dup2 (stdin_fd, 0); 2333 if (stdout_fd != 1) 2334 (void) dup2 (stdout_fd, 1); 2335 if (stdin_fd != 0) 2336 (void) close (stdin_fd); 2337 if (stdout_fd != 1) 2338 (void) close (stdout_fd); 2398 int r; 2399 int pid; 2400 int fdin = good_stdin ? FD_STDIN : get_bad_stdin (); 2401 int fdout = FD_STDOUT; 2402 int fderr = FD_STDERR; 2403 2404 /* Divert child output if we want to capture it. */ 2405 if (out && out->syncout) 2406 { 2407 if (out->out >= 0) 2408 fdout = out->out; 2409 if (out->err >= 0) 2410 fderr = out->err; 2411 } 2412 2413 pid = vfork(); 2414 if (pid != 0) 2415 return pid; 2416 2417 /* We are the child. */ 2418 unblock_sigs (); 2419 2420 #ifdef SET_STACK_SIZE 2421 /* Reset limits, if necessary. */ 2422 if (stack_limit.rlim_cur) 2423 setrlimit (RLIMIT_STACK, &stack_limit); 2424 #endif 2425 2426 /* For any redirected FD, dup2() it to the standard FD. 2427 They are all marked close-on-exec already. */ 2428 if (fdin != FD_STDIN) 2429 EINTRLOOP (r, dup2 (fdin, FD_STDIN)); 2430 if (fdout != FD_STDOUT) 2431 EINTRLOOP (r, dup2 (fdout, FD_STDOUT)); 2432 if (fderr != FD_STDERR) 2433 EINTRLOOP (r, dup2 (fderr, FD_STDERR)); 2339 2434 2340 2435 /* Run the command. */ … … 2370 2465 HANDLE hPID; 2371 2466 HANDLE hWaitPID; 2372 int err = 0;2373 2467 int exit_code = EXIT_FAILURE; 2374 2468 2375 2469 /* make sure CreateProcess() has Path it needs */ 2376 sync_Path_environment ();2470 sync_Path_environment (); 2377 2471 2378 2472 /* launch command */ 2379 hPID = process_easy (argv, envp);2473 hPID = process_easy (argv, envp, -1, -1); 2380 2474 2381 2475 /* make sure launch ok */ … … 2383 2477 { 2384 2478 int i; 2385 fprintf(stderr, 2386 _("process_easy() failed to launch process (e=%ld)\n"), 2387 process_last_err(hPID)); 2479 fprintf (stderr, _("process_easy() failed to launch process (e=%ld)\n"), 2480 process_last_err (hPID)); 2388 2481 for (i = 0; argv[i]; i++) 2389 fprintf (stderr, "%s ", argv[i]);2390 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i);2391 exit (EXIT_FAILURE);2482 fprintf (stderr, "%s ", argv[i]); 2483 fprintf (stderr, _("\nCounted %d args in failed launch\n"), i); 2484 exit (EXIT_FAILURE); 2392 2485 } 2393 2486 2394 2487 /* wait and reap last child */ 2395 hWaitPID = process_wait_for_any ();2488 hWaitPID = process_wait_for_any (1, 0); 2396 2489 while (hWaitPID) 2397 2490 { 2398 2491 /* was an error found on this process? */ 2399 err = process_last_err(hWaitPID);2492 int err = process_last_err (hWaitPID); 2400 2493 2401 2494 /* get exit data */ 2402 exit_code = process_exit_code (hWaitPID);2495 exit_code = process_exit_code (hWaitPID); 2403 2496 2404 2497 if (err) 2405 fprintf (stderr, "make (e=%d, rc=%d): %s",2406 err, exit_code, map_windows32_error_to_string(err));2498 fprintf (stderr, "make (e=%d, rc=%d): %s", 2499 err, exit_code, map_windows32_error_to_string (err)); 2407 2500 2408 2501 /* cleanup process */ 2409 process_cleanup (hWaitPID);2502 process_cleanup (hWaitPID); 2410 2503 2411 2504 /* expect to find only last pid, warn about other pids reaped */ … … 2413 2506 break; 2414 2507 else 2415 2416 2417 2418 fprintf (stderr,2419 _("make reaped child pid %s, still waiting for pid %s\n"),2420 2421 2422 2508 { 2509 char *pidstr = xstrdup (pid2str ((pid_t)hWaitPID)); 2510 2511 fprintf (stderr, 2512 _("make reaped child pid %s, still waiting for pid %s\n"), 2513 pidstr, pid2str ((pid_t)hPID)); 2514 free (pidstr); 2515 } 2423 2516 } 2424 2517 2425 2518 /* return child's exit code as our exit code */ 2426 exit (exit_code);2519 exit (exit_code); 2427 2520 2428 2521 #else /* !WINDOWS32 */ … … 2436 2529 2437 2530 # ifdef __EMX__ 2438 2439 2531 /* Run the program. */ 2440 2532 pid = spawnvpe (P_NOWAIT, argv[0], argv, envp); 2441 2442 2533 if (pid >= 0) 2443 2534 return pid; … … 2448 2539 2449 2540 # else 2450 2451 2541 /* Run the program. */ 2452 2542 environ = envp; … … 2458 2548 { 2459 2549 case ENOENT: 2460 error (NILF, _("%s: Command not found"), argv[0]); 2550 /* We are in the child: don't use the output buffer. 2551 It's not right to run fprintf() here! */ 2552 if (makelevel == 0) 2553 fprintf (stderr, _("%s: %s: Command not found\n"), program, argv[0]); 2554 else 2555 fprintf (stderr, _("%s[%u]: %s: Command not found\n"), 2556 program, makelevel, argv[0]); 2461 2557 break; 2462 2558 case ENOEXEC: 2463 2559 { 2464 /* The file is not executable. Try it as a shell script. */ 2465 extern char *getenv (); 2466 char *shell; 2467 char **new_argv; 2468 int argc; 2560 /* The file is not executable. Try it as a shell script. */ 2561 const char *shell; 2562 char **new_argv; 2563 int argc; 2469 2564 int i=1; 2470 2565 2471 2566 # ifdef __EMX__ 2472 2567 /* Do not use $SHELL from the environment */ 2473 2474 2475 2568 struct variable *p = lookup_variable ("SHELL", 5); 2569 if (p) 2570 shell = p->value; 2476 2571 else 2477 2572 shell = 0; 2478 2573 # else 2479 2574 shell = getenv ("SHELL"); 2480 2575 # endif 2481 2482 2483 2484 2485 2486 2576 if (shell == 0) 2577 shell = default_shell; 2578 2579 argc = 1; 2580 while (argv[argc] != 0) 2581 ++argc; 2487 2582 2488 2583 # ifdef __EMX__ … … 2491 2586 # endif 2492 2587 2493 2494 new_argv[0] =shell;2588 new_argv = alloca ((1 + argc + 1) * sizeof (char *)); 2589 new_argv[0] = (char *)shell; 2495 2590 2496 2591 # ifdef __EMX__ … … 2504 2599 2505 2600 new_argv[i] = argv[0]; 2506 2507 2508 2509 2510 2601 while (argc > 0) 2602 { 2603 new_argv[i + argc] = argv[argc]; 2604 --argc; 2605 } 2511 2606 2512 2607 # ifdef __EMX__ 2513 2514 2608 pid = spawnvpe (P_NOWAIT, shell, new_argv, envp); 2609 if (pid >= 0) 2515 2610 break; 2516 2611 # else 2517 2612 execvp (shell, new_argv); 2518 2613 # endif 2519 2520 error (NILF, _("%s: Shell program not found"), shell);2521 2522 2523 2614 if (errno == ENOENT) 2615 OS (error, NILF, _("%s: Shell program not found"), shell); 2616 else 2617 perror_with_name ("execvp: ", shell); 2618 break; 2524 2619 } 2525 2620 … … 2527 2622 case EINVAL: 2528 2623 /* this nasty error was driving me nuts :-( */ 2529 error (NILF, _("spawnvpe: environment space might be exhausted"));2624 O (error, NILF, _("spawnvpe: environment space might be exhausted")); 2530 2625 /* FALLTHROUGH */ 2531 2626 # endif … … 2545 2640 } 2546 2641 #else /* On Amiga */ 2547 void exec_command (char **argv) 2642 void 2643 exec_command (char **argv) 2548 2644 { 2549 2645 MyExecute (argv); … … 2561 2657 /* Figure out the argument list necessary to run LINE as a command. Try to 2562 2658 avoid using a shell. This routine handles only ' quoting, and " quoting 2563 when no backslash, $ or `characters are seen in the quotes. Starting2659 when no backslash, $ or ' characters are seen in the quotes. Starting 2564 2660 quotes may be escaped with a backslash. If any of the characters in 2565 sh_chars [] is seen, or any of the builtin commands listed in sh_cmds[]2661 sh_chars is seen, or any of the builtin commands listed in sh_cmds 2566 2662 is the first word of a line, the shell is used. 2567 2663 … … 2578 2674 2579 2675 static char ** 2580 construct_command_argv_internal (char *line, char **restp, c har *shell,2581 c har *shellflags, char *ifs, int flags,2582 char **batch_filename_ptr)2676 construct_command_argv_internal (char *line, char **restp, const char *shell, 2677 const char *shellflags, const char *ifs, 2678 int flags, char **batch_filename UNUSED) 2583 2679 { 2584 2680 #ifdef __MSDOS__ 2585 2681 /* MSDOS supports both the stock DOS shell and ports of Unixy shells. 2586 We call `system' for anything that requires ``slow'' processing,2682 We call 'system' for anything that requires ''slow'' processing, 2587 2683 because DOS shells are too dumb. When $SHELL points to a real 2588 (unix-style) shell, `system' just calls it to do everything. When2589 $SHELL points to a DOS shell, `system' does most of the work2684 (unix-style) shell, 'system' just calls it to do everything. When 2685 $SHELL points to a DOS shell, 'system' does most of the work 2590 2686 internally, calling the shell only for its internal commands. 2591 2687 However, it looks on the $PATH first, so you can e.g. have an 2592 external command named `mkdir'.2593 2594 Since we call `system', certain characters and commands below are2688 external command named 'mkdir'. 2689 2690 Since we call 'system', certain characters and commands below are 2595 2691 actually not specific to COMMAND.COM, but to the DJGPP implementation 2596 of `system'. In particular:2692 of 'system'. In particular: 2597 2693 2598 2694 The shell wildcard characters are in DOS_CHARS because they will 2599 not be expanded if we call the child via `spawnXX'.2600 2601 The `;' is in DOS_CHARS, because our `system' knows how to run2695 not be expanded if we call the child via 'spawnXX'. 2696 2697 The ';' is in DOS_CHARS, because our 'system' knows how to run 2602 2698 multiple commands on a single line. 2603 2699 … … 2605 2701 won't have to tell one from another and have one more set of 2606 2702 commands and special characters. */ 2607 static char sh_chars_dos[] = "*?[];|<>%^&()"; 2608 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls", 2609 "copy", "ctty", "date", "del", "dir", "echo", 2610 "erase", "exit", "for", "goto", "if", "md", 2611 "mkdir", "path", "pause", "prompt", "rd", 2612 "rmdir", "rem", "ren", "rename", "set", 2613 "shift", "time", "type", "ver", "verify", 2614 "vol", ":", 0 }; 2615 2616 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^"; 2617 static char *sh_cmds_sh[] = { "cd", "echo", "eval", "exec", "exit", "login", 2618 "logout", "set", "umask", "wait", "while", 2619 "for", "case", "if", ":", ".", "break", 2620 "continue", "export", "read", "readonly", 2621 "shift", "times", "trap", "switch", "unset", 2622 "ulimit", 0 }; 2623 2624 char *sh_chars; 2625 char **sh_cmds; 2703 static const char *sh_chars_dos = "*?[];|<>%^&()"; 2704 static const char *sh_cmds_dos[] = 2705 { "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date", 2706 "del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md", 2707 "mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren", 2708 "rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":", 2709 0 }; 2710 2711 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^"; 2712 static const char *sh_cmds_sh[] = 2713 { "cd", "echo", "eval", "exec", "exit", "login", "logout", "set", "umask", 2714 "wait", "while", "for", "case", "if", ":", ".", "break", "continue", 2715 "export", "read", "readonly", "shift", "times", "trap", "switch", 2716 "unset", "ulimit", 0 }; 2717 2718 const char *sh_chars; 2719 const char **sh_cmds; 2720 2626 2721 #elif defined (__EMX__) 2627 static char sh_chars_dos[] = "*?[];|<>%^&()"; 2628 static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls", 2629 "copy", "ctty", "date", "del", "dir", "echo", 2630 "erase", "exit", "for", "goto", "if", "md", 2631 "mkdir", "path", "pause", "prompt", "rd", 2632 "rmdir", "rem", "ren", "rename", "set", 2633 "shift", "time", "type", "ver", "verify", 2634 "vol", ":", 0 }; 2635 2636 static char sh_chars_os2[] = "*?[];|<>%^()\"'&"; 2637 static char *sh_cmds_os2[] = { "call", "cd", "chcp", "chdir", "cls", "copy", 2638 "date", "del", "detach", "dir", "echo", 2639 "endlocal", "erase", "exit", "for", "goto", "if", 2640 "keys", "md", "mkdir", "move", "path", "pause", 2641 "prompt", "rd", "rem", "ren", "rename", "rmdir", 2642 "set", "setlocal", "shift", "start", "time", 2643 "type", "ver", "verify", "vol", ":", 0 }; 2644 2645 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~'"; 2646 static char *sh_cmds_sh[] = { "echo", "cd", "eval", "exec", "exit", "login", 2647 "logout", "set", "umask", "wait", "while", 2648 "for", "case", "if", ":", ".", "break", 2649 "continue", "export", "read", "readonly", 2650 "shift", "times", "trap", "switch", "unset", 2651 0 }; 2652 char *sh_chars; 2653 char **sh_cmds; 2722 static const char *sh_chars_dos = "*?[];|<>%^&()"; 2723 static const char *sh_cmds_dos[] = 2724 { "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date", 2725 "del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md", 2726 "mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren", 2727 "rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":", 2728 0 }; 2729 2730 static const char *sh_chars_os2 = "*?[];|<>%^()\"'&"; 2731 static const char *sh_cmds_os2[] = 2732 { "call", "cd", "chcp", "chdir", "cls", "copy", "date", "del", "detach", 2733 "dir", "echo", "endlocal", "erase", "exit", "for", "goto", "if", "keys", 2734 "md", "mkdir", "move", "path", "pause", "prompt", "rd", "rem", "ren", 2735 "rename", "rmdir", "set", "setlocal", "shift", "start", "time", "type", 2736 "ver", "verify", "vol", ":", 0 }; 2737 2738 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~'"; 2739 static const char *sh_cmds_sh[] = 2740 { "echo", "cd", "eval", "exec", "exit", "login", "logout", "set", "umask", 2741 "wait", "while", "for", "case", "if", ":", ".", "break", "continue", 2742 "export", "read", "readonly", "shift", "times", "trap", "switch", 2743 "unset", 0 }; 2744 2745 const char *sh_chars; 2746 const char **sh_cmds; 2654 2747 2655 2748 #elif defined (_AMIGA) 2656 static c har sh_chars[]= "#;\"|<>()?*$`";2657 static c har *sh_cmds[] = { "cd", "eval", "if", "delete", "echo", "copy",2658 "rename", "set", "setenv", "date", "makedir",2659 "skip", "else", "endif", "path", "prompt",2660 "unset", "unsetenv", "version", 2661 0 }; 2749 static const char *sh_chars = "#;\"|<>()?*$`"; 2750 static const char *sh_cmds[] = 2751 { "cd", "eval", "if", "delete", "echo", "copy", "rename", "set", "setenv", 2752 "date", "makedir", "skip", "else", "endif", "path", "prompt", "unset", 2753 "unsetenv", "version", 0 }; 2754 2662 2755 #elif defined (WINDOWS32) 2663 static char sh_chars_dos[] = "\"|&<>"; 2664 static char *sh_cmds_dos[] = { "assoc", "break", "call", "cd", "chcp", 2665 "chdir", "cls", "color", "copy", "ctty", 2666 "date", "del", "dir", "echo", "echo.", 2667 "endlocal", "erase", "exit", "for", "ftype", 2668 "goto", "if", "if", "md", "mkdir", "path", 2669 "pause", "prompt", "rd", "rem", "ren", 2670 "rename", "rmdir", "set", "setlocal", 2671 "shift", "time", "title", "type", "ver", 2672 "verify", "vol", ":", 0 }; 2673 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^"; 2674 static char *sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login", 2675 "logout", "set", "umask", "wait", "while", "for", 2676 "case", "if", ":", ".", "break", "continue", 2677 "export", "read", "readonly", "shift", "times", 2678 "trap", "switch", "test", 2756 /* We used to have a double quote (") in sh_chars_dos[] below, but 2757 that caused any command line with quoted file names be run 2758 through a temporary batch file, which introduces command-line 2759 limit of 4K charcaters imposed by cmd.exe. Since CreateProcess 2760 can handle quoted file names just fine, removing the quote lifts 2761 the limit from a very frequent use case, because using quoted 2762 file names is commonplace on MS-Windows. */ 2763 static const char *sh_chars_dos = "|&<>"; 2764 static const char *sh_cmds_dos[] = 2765 { "assoc", "break", "call", "cd", "chcp", "chdir", "cls", "color", "copy", 2766 "ctty", "date", "del", "dir", "echo", "echo.", "endlocal", "erase", 2767 "exit", "for", "ftype", "goto", "if", "if", "md", "mkdir", "move", 2768 "path", "pause", "prompt", "rd", "rem", "ren", "rename", "rmdir", 2769 "set", "setlocal", "shift", "time", "title", "type", "ver", "verify", 2770 "vol", ":", 0 }; 2771 2772 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^"; 2773 static const char *sh_cmds_sh[] = 2774 { "cd", "eval", "exec", "exit", "login", "logout", "set", "umask", "wait", 2775 "while", "for", "case", "if", ":", ".", "break", "continue", "export", 2776 "read", "readonly", "shift", "times", "trap", "switch", "test", 2679 2777 #ifdef BATCH_MODE_ONLY_SHELL 2680 "echo", 2681 #endif 2682 0 }; 2683 char* sh_chars; 2684 char** sh_cmds; 2778 "echo", 2779 #endif 2780 0 }; 2781 2782 const char *sh_chars; 2783 const char **sh_cmds; 2685 2784 #elif defined(__riscos__) 2686 static c har sh_chars[]= "";2687 static c har *sh_cmds[] = { 0 };2785 static const char *sh_chars = ""; 2786 static const char *sh_cmds[] = { 0 }; 2688 2787 #else /* must be UNIX-ish */ 2689 static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~!"; /* kmk: +_sh */ 2690 static char *sh_cmds_sh[] = { ".", ":", "break", "case", "cd", "continue", /* kmk: +_sh */ 2691 "eval", "exec", "exit", "export", "for", "if", 2692 "login", "logout", "read", "readonly", "set", 2693 "shift", "switch", "test", "times", "trap", 2694 "ulimit", "umask", "unset", "wait", "while", 0 }; 2695 # ifdef HAVE_DOS_PATHS 2788 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~!"; /* kmk: +_sh */ 2789 static const char *sh_cmds_sh[] = /* kmk: +_sh */ 2790 { ".", ":", "break", "case", "cd", "continue", "eval", "exec", "exit", 2791 "export", "for", "if", "login", "logout", "read", "readonly", "set", 2792 "shift", "switch", "test", "times", "trap", "ulimit", "umask", "unset", 2793 "wait", "while", 0 }; 2794 2795 # if 0 /*def HAVE_DOS_PATHS - kmk */ 2696 2796 /* This is required if the MSYS/Cygwin ports (which do not define 2697 2797 WINDOWS32) are compiled with HAVE_DOS_PATHS defined, which uses 2698 sh_chars_sh[] directly (see below). */ 2699 static char *sh_chars_sh = sh_chars; 2700 # endif /* HAVE_DOS_PATHS */ 2798 sh_chars_sh directly (see below). The value must be identical 2799 to that of sh_chars immediately above. */ 2800 static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~!"; 2801 # endif /* HAVE_DOS_PATHS */ 2701 2802 char* sh_chars = sh_chars_sh; /* kmk: +_sh */ 2702 2803 char** sh_cmds = sh_cmds_sh; /* kmk: +_sh */ … … 2705 2806 static char sh_chars_kash[] = "#;*?[]&|<>(){}$`^~!"; /* note: no \" - good idea? */ 2706 2807 static char *sh_cmds_kash[] = { 2707 ".", ":", "break", "case", "cd", "continue", 2708 "echo", "eval", "exec", "exit", "export", "for", "if", 2709 "login", "logout", "read", "readonly", "set", 2710 "shift", "switch", "test", "times", "trap", 2711 "umask", "wait", "while", 0 2808 ".", ":", "break", "case", "cd", "continue", "echo", "eval", "exec", "exit", 2809 "export", "for", "if", "login", "logout", "read", "readonly", "set", 2810 "shift", "switch", "test", "times", "trap", "umask", "wait", "while", 0 /* +echo, -ulimit, -unset */ 2712 2811 }; 2713 2812 int is_kmk_shell = 0; … … 2715 2814 int i; 2716 2815 char *p; 2717 char *ap;2718 2816 #ifndef NDEBUG 2719 2817 char *end; 2720 2818 #endif 2819 char *ap; 2820 const char *cap; 2821 const char *cp; 2721 2822 int instring, word_has_equals, seen_nonequals, last_argument_was_empty; 2722 2823 char **new_argv = 0; … … 2725 2826 int slow_flag = 0; 2726 2827 2727 if (!unixy_shell) { 2728 sh_cmds = sh_cmds_dos; 2729 sh_chars = sh_chars_dos; 2730 } else { 2731 sh_cmds = sh_cmds_sh; 2732 sh_chars = sh_chars_sh; 2733 } 2828 if (!unixy_shell) 2829 { 2830 sh_cmds = sh_cmds_dos; 2831 sh_chars = sh_chars_dos; 2832 } 2833 else 2834 { 2835 sh_cmds = sh_cmds_sh; 2836 sh_chars = sh_chars_sh; 2837 } 2734 2838 #endif /* WINDOWS32 */ 2735 2839 … … 2737 2841 *restp = NULL; 2738 2842 2739 /* Make sure not to bother processing an empty line . */2740 while ( isblank ((unsigned char)*line))2843 /* Make sure not to bother processing an empty line but stop at newline. */ 2844 while (ISBLANK (*line)) 2741 2845 ++line; 2742 2846 if (*line == '\0') 2743 2847 return 0; 2848 2849 if (shellflags == 0) 2850 shellflags = posix_pedantic ? "-ec" : "-c"; 2744 2851 2745 2852 /* See if it is safe to parse commands internally. */ … … 2786 2893 slow_flag = strcmp ((s1 ? s1 : ""), (s2 ? s2 : "")); 2787 2894 2788 if (s1) 2789 free (s1); 2790 if (s2) 2791 free (s2); 2895 free (s1); 2896 free (s2); 2792 2897 } 2793 2898 if (slow_flag) … … 2799 2904 extern int _is_unixy_shell (const char *_path); 2800 2905 2801 DB (DB_BASIC, (_("$SHELL changed (was `%s', now `%s')\n"),2906 DB (DB_BASIC, (_("$SHELL changed (was '%s', now '%s')\n"), 2802 2907 default_shell, shell)); 2803 2908 unixy_shell = _is_unixy_shell (shell); … … 2834 2939 #endif /* not WINDOWS32 */ 2835 2940 2836 if (ifs != 0)2837 for ( ap = ifs; *ap != '\0'; ++ap)2838 if (* ap != ' ' && *ap != '\t' && *ap != '\n')2839 2840 2841 if (shellflags != 0)2941 if (ifs) 2942 for (cap = ifs; *cap != '\0'; ++cap) 2943 if (*cap != ' ' && *cap != '\t' && *cap != '\n') 2944 goto slow; 2945 2946 if (shellflags) 2842 2947 if (shellflags[0] != '-' 2843 2948 || ((shellflags[1] != 'c' || shellflags[2] != '\0') … … 2864 2969 2865 2970 if (instring) 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2971 { 2972 /* Inside a string, just copy any char except a closing quote 2973 or a backslash-newline combination. */ 2974 if (*p == instring) 2975 { 2976 instring = 0; 2977 if (ap == new_argv[0] || *(ap-1) == '\0') 2978 last_argument_was_empty = 1; 2979 } 2980 else if (*p == '\\' && p[1] == '\n') 2876 2981 { 2877 2982 /* Backslash-newline is handled differently depending on what 2878 2983 kind of string we're in: inside single-quoted strings you 2879 keep them; in double-quoted strings they disappear. 2880 For DOS/Windows/OS2, if we don't have a POSIX shell, 2881 we keep the pre-POSIX behavior of removing the 2882 backslash-newline. */ 2984 keep them; in double-quoted strings they disappear. For 2985 DOS/Windows/OS2, if we don't have a POSIX shell, we keep the 2986 pre-POSIX behavior of removing the backslash-newline. */ 2883 2987 if (instring == '"' 2884 2988 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32) 2885 2886 #endif 2887 2989 || !unixy_shell 2990 #endif 2991 ) 2888 2992 ++p; 2889 2993 else … … 2893 2997 } 2894 2998 } 2895 else if (*p == '\n' && restp != NULL) 2896 { 2897 /* End of the command line. */ 2898 *restp = p; 2899 goto end_of_line; 2900 } 2901 /* Backslash, $, and ` are special inside double quotes. 2902 If we see any of those, punt. 2903 But on MSDOS, if we use COMMAND.COM, double and single 2904 quotes have the same effect. */ 2905 else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell) 2906 goto slow; 2907 else 2908 *ap++ = *p; 2909 } 2999 else if (*p == '\n' && restp != NULL) 3000 { 3001 /* End of the command line. */ 3002 *restp = p; 3003 goto end_of_line; 3004 } 3005 /* Backslash, $, and ` are special inside double quotes. 3006 If we see any of those, punt. 3007 But on MSDOS, if we use COMMAND.COM, double and single 3008 quotes have the same effect. */ 3009 else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell) 3010 goto slow; 3011 #ifdef WINDOWS32 3012 /* Quoted wildcard characters must be passed quoted to the 3013 command, so give up the fast route. */ 3014 else if (instring == '"' && strchr ("*?", *p) != 0 && !unixy_shell) 3015 goto slow; 3016 else if (instring == '"' && strncmp (p, "\\\"", 2) == 0) 3017 *ap++ = *++p; 3018 #endif 3019 else 3020 *ap++ = *p; 3021 } 2910 3022 else if (strchr (sh_chars, *p) != 0) 2911 3023 #ifdef KMK … … 2915 3027 if ( *p == '~' 2916 3028 && p > line 2917 && ! isspace(p[-1])3029 && !ISSPACE (p[-1]) 2918 3030 && p[-1] != '=' 2919 3031 && p[-1] != ':' … … 2926 3038 } 2927 3039 #else /* !KMK */ 2928 2929 3040 /* Not inside a string, but it's a special char. */ 3041 goto slow; 2930 3042 #endif /* !KMK */ 2931 3043 else if (one_shell && *p == '\n') 2932 2933 3044 /* In .ONESHELL mode \n is a separator like ; or && */ 3045 goto slow; 2934 3046 #ifdef __MSDOS__ 2935 3047 else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.') 2936 /* `...' is a wildcard in DJGPP. */2937 3048 /* '...' is a wildcard in DJGPP. */ 3049 goto slow; 2938 3050 #endif 2939 3051 else 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 3052 /* Not a special char. */ 3053 switch (*p) 3054 { 3055 case '=': 3056 /* Equals is a special character in leading words before the 3057 first word with no equals sign in it. This is not the case 3058 with sh -k, but we never get here when using nonstandard 3059 shell flags. */ 3060 if (! seen_nonequals && unixy_shell) 3061 goto slow; 3062 word_has_equals = 1; 3063 *ap++ = '='; 3064 break; 3065 3066 case '\\': 3067 /* Backslash-newline has special case handling, ref POSIX. 2956 3068 We're in the fastpath, so emulate what the shell would do. */ 2957 2958 2959 3069 if (p[1] == '\n') 3070 { 3071 /* Throw out the backslash and newline. */ 2960 3072 ++p; 2961 3073 2962 /* If there's nothing in this argument yet, skip any2963 whitespace before the start of the next word. */3074 /* At the beginning of the argument, skip any whitespace other 3075 than newline before the start of the next word. */ 2964 3076 if (ap == new_argv[i]) 2965 p = next_token (p + 1) - 1; 2966 } 2967 else if (p[1] != '\0') 3077 while (ISBLANK (p[1])) 3078 ++p; 3079 } 3080 #ifdef WINDOWS32 3081 /* Backslash before whitespace is not special if our shell 3082 is not Unixy. */ 3083 else if (ISSPACE (p[1]) && !unixy_shell) 3084 { 3085 *ap++ = *p; 3086 break; 3087 } 3088 #endif 3089 else if (p[1] != '\0') 2968 3090 { 2969 3091 #ifdef HAVE_DOS_PATHS … … 2986 3108 #endif 2987 3109 if (p[1] != '\\' && p[1] != '\'' 2988 && ! isspace ((unsigned char)p[1])3110 && !ISSPACE (p[1]) 2989 3111 # ifdef KMK 2990 3112 && strchr (sh_chars, p[1]) == 0 … … 3000 3122 *ap++ = *++p; 3001 3123 } 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 heretofore has contained an `='. */3031 3032 3033 /* An `=' in a word before the first3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3124 break; 3125 3126 case '\'': 3127 case '"': 3128 instring = *p; 3129 break; 3130 3131 case '\n': 3132 if (restp != NULL) 3133 { 3134 /* End of the command line. */ 3135 *restp = p; 3136 goto end_of_line; 3137 } 3138 else 3139 /* Newlines are not special. */ 3140 *ap++ = '\n'; 3141 break; 3142 3143 case ' ': 3144 case '\t': 3145 /* We have the end of an argument. 3146 Terminate the text of the argument. */ 3147 *ap++ = '\0'; 3148 new_argv[++i] = ap; 3149 last_argument_was_empty = 0; 3150 3151 /* Update SEEN_NONEQUALS, which tells us if every word 3152 heretofore has contained an '='. */ 3153 seen_nonequals |= ! word_has_equals; 3154 if (word_has_equals && ! seen_nonequals) 3155 /* An '=' in a word before the first 3156 word without one is magical. */ 3157 goto slow; 3158 word_has_equals = 0; /* Prepare for the next word. */ 3159 3160 /* If this argument is the command name, 3161 see if it is a built-in shell command. 3162 If so, have the shell handle it. */ 3163 if (i == 1) 3164 { 3165 register int j; 3166 for (j = 0; sh_cmds[j] != 0; ++j) 3045 3167 { 3046 3168 if (streq (sh_cmds[j], new_argv[0])) 3047 3169 goto slow; 3048 # ifdef __EMX__3170 #if defined(__EMX__) || defined(WINDOWS32) 3049 3171 /* Non-Unix shells are case insensitive. */ 3050 3172 if (!unixy_shell 3051 3173 && strcasecmp (sh_cmds[j], new_argv[0]) == 0) 3052 3174 goto slow; 3053 # 3175 #endif 3054 3176 } 3055 } 3056 3057 /* Ignore multiple whitespace chars. */ 3058 p = next_token (p) - 1; 3059 break; 3060 3061 default: 3062 *ap++ = *p; 3063 break; 3064 } 3177 } 3178 3179 /* Skip whitespace chars, but not newlines. */ 3180 while (ISBLANK (p[1])) 3181 ++p; 3182 break; 3183 3184 default: 3185 *ap++ = *p; 3186 break; 3187 } 3065 3188 } 3066 3189 end_of_line: … … 3081 3204 register int j; 3082 3205 for (j = 0; sh_cmds[j] != 0; ++j) 3083 3084 3206 if (streq (sh_cmds[j], new_argv[0])) 3207 goto slow; 3085 3208 } 3086 3209 … … 3106 3229 3107 3230 #ifdef __MSDOS__ 3108 execute_by_shell = 1; /* actually, call `system' if shell isn't unixy */3231 execute_by_shell = 1; /* actually, call 'system' if shell isn't unixy */ 3109 3232 #endif 3110 3233 … … 3121 3244 { 3122 3245 if (*ptr == '\\' && ptr[1] == '\n') 3123 3246 ptr += 2; 3124 3247 else if (*ptr == '@') /* Kludge: multiline commands */ 3125 3248 { 3126 3127 3249 ptr += 2; 3250 *dptr++ = '\n'; 3128 3251 } 3129 3252 else 3130 3253 *dptr++ = *ptr++; 3131 3254 } 3132 3255 *dptr = 0; … … 3136 3259 new_argv[1] = 0; 3137 3260 } 3138 #else 3261 #else /* Not Amiga */ 3139 3262 #ifdef WINDOWS32 3140 3263 /* … … 3149 3272 3150 3273 /* Make sure not to bother processing an empty line. */ 3151 while (isspace ((unsigned char)*line)) 3152 ++line; 3274 NEXT_TOKEN (line); 3153 3275 if (*line == '\0') 3154 3276 return 0; … … 3161 3283 argument list. */ 3162 3284 3285 char *new_line; 3163 3286 unsigned int shell_len = strlen (shell); 3164 3287 unsigned int line_len = strlen (line); 3165 unsigned int sflags_len = s trlen (shellflags);3166 # 3288 unsigned int sflags_len = shellflags ? strlen (shellflags) : 0; 3289 #ifdef WINDOWS32 3167 3290 char *command_ptr = NULL; /* used for batch_mode_shell mode */ 3168 # endif 3169 char *new_line; 3291 #endif 3170 3292 3171 3293 # ifdef __EMX__ /* is this necessary? */ 3172 if (!unixy_shell )3294 if (!unixy_shell && shellflags) 3173 3295 shellflags[0] = '/'; /* "/c" */ 3174 3296 # endif 3175 3297 3176 3298 /* In .ONESHELL mode we are allowed to throw the entire current 3177 3178 3179 3299 recipe string at a single shell and trust that the user 3300 has configured the shell and shell flags, and formatted 3301 the string, appropriately. */ 3180 3302 if (one_shell) 3181 3303 { 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3304 /* If the shell is Bourne compatible, we must remove and ignore 3305 interior special chars [@+-] because they're meaningless to 3306 the shell itself. If, however, we're in .ONESHELL mode and 3307 have changed SHELL to something non-standard, we should 3308 leave those alone because they could be part of the 3309 script. In this case we must also leave in place 3310 any leading [@+-] for the same reason. */ 3311 3312 /* Remove and ignore interior prefix chars [@+-] because they're 3313 meaningless given a single shell. */ 3192 3314 #if defined __MSDOS__ || defined (__EMX__) 3193 3315 if (unixy_shell) /* the test is complicated and we already did it */ 3194 3316 #else 3195 if (is_bourne_compatible_shell(shell)) 3317 if (is_bourne_compatible_shell (shell) 3318 #ifdef WINDOWS32 3319 /* If we didn't find any sh.exe, don't behave is if we did! */ 3320 && !no_default_sh_exe 3321 #endif 3322 ) 3196 3323 #endif 3197 3324 { … … 3205 3332 int esc = 0; 3206 3333 3207 /* This is the start of a new recipe line. 3208 Skip whitespace and prefix characters. */3209 while ( isblank(*f) || *f == '-' || *f == '@' || *f == '+')3334 /* This is the start of a new recipe line. Skip whitespace 3335 and prefix characters but not newlines. */ 3336 while (ISBLANK (*f) || *f == '-' || *f == '@' || *f == '+') 3210 3337 ++f; 3211 3338 … … 3229 3356 *t = '\0'; 3230 3357 } 3231 3232 new_argv = xmalloc (4 * sizeof (char *)); 3233 new_argv[0] = xstrdup(shell); 3234 new_argv[1] = xstrdup(shellflags); 3235 new_argv[2] = line; 3236 new_argv[3] = NULL; 3237 return new_argv; 3358 #ifdef WINDOWS32 3359 else /* non-Posix shell (cmd.exe etc.) */ 3360 { 3361 const char *f = line; 3362 char *t = line; 3363 char *tstart = t; 3364 int temp_fd; 3365 FILE* batch = NULL; 3366 int id = GetCurrentProcessId (); 3367 PATH_VAR(fbuf); 3368 3369 /* Generate a file name for the temporary batch file. */ 3370 sprintf (fbuf, "make%d", id); 3371 *batch_filename = create_batch_file (fbuf, 0, &temp_fd); 3372 DB (DB_JOBS, (_("Creating temporary batch file %s\n"), 3373 *batch_filename)); 3374 3375 /* Create a FILE object for the batch file, and write to it the 3376 commands to be executed. Put the batch file in TEXT mode. */ 3377 _setmode (temp_fd, _O_TEXT); 3378 batch = _fdopen (temp_fd, "wt"); 3379 fputs ("@echo off\n", batch); 3380 DB (DB_JOBS, (_("Batch file contents:\n\t@echo off\n"))); 3381 3382 /* Copy the recipe, removing and ignoring interior prefix chars 3383 [@+-]: they're meaningless in .ONESHELL mode. */ 3384 while (*f != '\0') 3385 { 3386 /* This is the start of a new recipe line. Skip whitespace 3387 and prefix characters but not newlines. */ 3388 while (ISBLANK (*f) || *f == '-' || *f == '@' || *f == '+') 3389 ++f; 3390 3391 /* Copy until we get to the next logical recipe line. */ 3392 while (*f != '\0') 3393 { 3394 /* Remove the escaped newlines in the command, and the 3395 blanks that follow them. Windows shells cannot handle 3396 escaped newlines. */ 3397 if (*f == '\\' && f[1] == '\n') 3398 { 3399 f += 2; 3400 while (ISBLANK (*f)) 3401 ++f; 3402 } 3403 *(t++) = *(f++); 3404 /* On an unescaped newline, we're done with this 3405 line. */ 3406 if (f[-1] == '\n') 3407 break; 3408 } 3409 /* Write another line into the batch file. */ 3410 if (t > tstart) 3411 { 3412 char c = *t; 3413 *t = '\0'; 3414 fputs (tstart, batch); 3415 DB (DB_JOBS, ("\t%s", tstart)); 3416 tstart = t; 3417 *t = c; 3418 } 3419 } 3420 DB (DB_JOBS, ("\n")); 3421 fclose (batch); 3422 3423 /* Create an argv list for the shell command line that 3424 will run the batch file. */ 3425 new_argv = xmalloc (2 * sizeof (char *)); 3426 new_argv[0] = xstrdup (*batch_filename); 3427 new_argv[1] = NULL; 3428 return new_argv; 3429 } 3430 #endif /* WINDOWS32 */ 3431 /* Create an argv list for the shell command line. */ 3432 { 3433 int n = 0; 3434 3435 new_argv = xmalloc ((4 + sflags_len/2) * sizeof (char *)); 3436 new_argv[n++] = xstrdup (shell); 3437 3438 /* Chop up the shellflags (if any) and assign them. */ 3439 if (! shellflags) 3440 new_argv[n++] = xstrdup (""); 3441 else 3442 { 3443 const char *s = shellflags; 3444 char *t; 3445 unsigned int len; 3446 while ((t = find_next_token (&s, &len)) != 0) 3447 new_argv[n++] = xstrndup (t, len); 3448 } 3449 3450 /* Set the command to invoke. */ 3451 new_argv[n++] = line; 3452 new_argv[n++] = NULL; 3453 } 3454 return new_argv; 3238 3455 } 3239 3456 3240 new_line = alloca (shell_len+ 1 + sflags_len + 13241 3457 new_line = xmalloc ((shell_len*2) + 1 + sflags_len + 1 3458 + (line_len*2) + 1); 3242 3459 ap = new_line; 3243 memcpy (ap, shell, shell_len); 3244 ap += shell_len; 3460 /* Copy SHELL, escaping any characters special to the shell. If 3461 we don't escape them, construct_command_argv_internal will 3462 recursively call itself ad nauseam, or until stack overflow, 3463 whichever happens first. */ 3464 for (cp = shell; *cp != '\0'; ++cp) 3465 { 3466 if (strchr (sh_chars, *cp) != 0) 3467 *(ap++) = '\\'; 3468 *(ap++) = *cp; 3469 } 3245 3470 *(ap++) = ' '; 3246 memcpy (ap, shellflags, sflags_len); 3471 if (shellflags) 3472 memcpy (ap, shellflags, sflags_len); 3247 3473 ap += sflags_len; 3248 3474 *(ap++) = ' '; … … 3252 3478 for (p = line; *p != '\0'; ++p) 3253 3479 { 3254 3255 3256 3257 3258 3259 3260 3261 3480 if (restp != NULL && *p == '\n') 3481 { 3482 *restp = p; 3483 break; 3484 } 3485 else if (*p == '\\' && p[1] == '\n') 3486 { 3487 /* POSIX says we keep the backslash-newline. If we don't have a 3262 3488 POSIX shell on DOS/Windows/OS2, mimic the pre-POSIX behavior 3263 3489 and remove the backslash/newline. */ … … 3267 3493 # define PRESERVE_BSNL 1 3268 3494 #endif 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3495 if (PRESERVE_BSNL) 3496 { 3497 *(ap++) = '\\'; 3498 /* Only non-batch execution needs another backslash, 3499 because it will be passed through a recursive 3500 invocation of this function. */ 3501 if (!batch_mode_shell) 3502 *(ap++) = '\\'; 3503 *(ap++) = '\n'; 3504 } 3505 ++p; 3506 continue; 3507 } 3282 3508 3283 3509 /* DOS shells don't know about backslash-escaping. */ 3284 3510 if (unixy_shell && !batch_mode_shell && 3285 3511 (*p == '\\' || *p == '\'' || *p == '"' 3286 || isspace ((unsigned char)*p)3512 || ISSPACE (*p) 3287 3513 || strchr (sh_chars, *p) != 0)) 3288 3514 *ap++ = '\\'; 3289 3515 #ifdef __MSDOS__ 3290 3516 else if (unixy_shell && strneq (p, "...", 3)) 3291 3517 { 3292 /* The case of `...' wildcard again. */3518 /* The case of '...' wildcard again. */ 3293 3519 strcpy (ap, "\\.\\.\\"); 3294 3520 ap += 5; … … 3296 3522 } 3297 3523 #endif 3298 3524 *ap++ = *p; 3299 3525 } 3300 3526 if (ap == new_line + shell_len + sflags_len + 2) 3301 /* Line was empty. */ 3302 return 0; 3527 { 3528 /* Line was empty. */ 3529 free (new_line); 3530 return 0; 3531 } 3303 3532 *ap = '\0'; 3304 3533 … … 3307 3536 command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these 3308 3537 cases, run commands via a script file. */ 3309 if (just_print_flag && !(flags & COMMANDS_RECURSE)) { 3310 /* Need to allocate new_argv, although it's unused, because 3311 start_job_command will want to free it and its 0'th element. */ 3312 new_argv = xmalloc(2 * sizeof (char *)); 3313 new_argv[0] = xstrdup (""); 3314 new_argv[1] = NULL; 3315 } else if ((no_default_sh_exe || batch_mode_shell) && batch_filename_ptr) { 3316 int temp_fd; 3317 FILE* batch = NULL; 3318 int id = GetCurrentProcessId(); 3319 PATH_VAR(fbuf); 3320 3321 /* create a file name */ 3322 sprintf(fbuf, "make%d", id); 3323 *batch_filename_ptr = create_batch_file (fbuf, unixy_shell, &temp_fd); 3324 3325 DB (DB_JOBS, (_("Creating temporary batch file %s\n"), 3326 *batch_filename_ptr)); 3327 3328 /* Create a FILE object for the batch file, and write to it the 3329 commands to be executed. Put the batch file in TEXT mode. */ 3330 _setmode (temp_fd, _O_TEXT); 3331 batch = _fdopen (temp_fd, "wt"); 3332 if (!unixy_shell) 3333 fputs ("@echo off\n", batch); 3334 fputs (command_ptr, batch); 3335 fputc ('\n', batch); 3336 fclose (batch); 3337 DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"), 3338 !unixy_shell ? "\n\t@echo off" : "", command_ptr)); 3339 3340 /* create argv */ 3341 new_argv = xmalloc(3 * sizeof (char *)); 3342 if (unixy_shell) { 3343 new_argv[0] = xstrdup (shell); 3344 new_argv[1] = *batch_filename_ptr; /* only argv[0] gets freed later */ 3345 } else { 3346 new_argv[0] = xstrdup (*batch_filename_ptr); 3538 if (just_print_flag && !(flags & COMMANDS_RECURSE)) 3539 { 3540 /* Need to allocate new_argv, although it's unused, because 3541 start_job_command will want to free it and its 0'th element. */ 3542 new_argv = xmalloc (2 * sizeof (char *)); 3543 new_argv[0] = xstrdup (""); 3347 3544 new_argv[1] = NULL; 3348 3545 } 3349 new_argv[2] = NULL; 3350 } else 3546 else if ((no_default_sh_exe || batch_mode_shell) && batch_filename) 3547 { 3548 int temp_fd; 3549 FILE* batch = NULL; 3550 int id = GetCurrentProcessId (); 3551 PATH_VAR (fbuf); 3552 3553 /* create a file name */ 3554 sprintf (fbuf, "make%d", id); 3555 *batch_filename = create_batch_file (fbuf, unixy_shell, &temp_fd); 3556 3557 DB (DB_JOBS, (_("Creating temporary batch file %s\n"), 3558 *batch_filename)); 3559 3560 /* Create a FILE object for the batch file, and write to it the 3561 commands to be executed. Put the batch file in TEXT mode. */ 3562 _setmode (temp_fd, _O_TEXT); 3563 batch = _fdopen (temp_fd, "wt"); 3564 if (!unixy_shell) 3565 fputs ("@echo off\n", batch); 3566 fputs (command_ptr, batch); 3567 fputc ('\n', batch); 3568 fclose (batch); 3569 DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"), 3570 !unixy_shell ? "\n\t@echo off" : "", command_ptr)); 3571 3572 /* create argv */ 3573 new_argv = xmalloc (3 * sizeof (char *)); 3574 if (unixy_shell) 3575 { 3576 new_argv[0] = xstrdup (shell); 3577 new_argv[1] = *batch_filename; /* only argv[0] gets freed later */ 3578 } 3579 else 3580 { 3581 new_argv[0] = xstrdup (*batch_filename); 3582 new_argv[1] = NULL; 3583 } 3584 new_argv[2] = NULL; 3585 } 3586 else 3351 3587 #endif /* WINDOWS32 */ 3352 3588 3353 3589 if (unixy_shell) 3354 new_argv = construct_command_argv_internal (new_line, 0, 0, 0, 0, flags, 0); 3590 new_argv = construct_command_argv_internal (new_line, 0, 0, 0, 0, 3591 flags, 0); 3355 3592 3356 3593 #ifdef __EMX__ 3357 3594 else if (!unixy_shell) 3358 3595 { 3359 3596 /* new_line is local, must not be freed therefore 3360 3597 We use line here instead of new_line because we run the shell 3361 3598 manually. */ … … 3437 3674 #else 3438 3675 else 3439 fatal (NILF, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"), 3676 fatal (NILF, CSTRLEN (__FILE__) + INTSTR_LENGTH, 3677 _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"), 3440 3678 __FILE__, __LINE__); 3441 3679 #endif 3680 3681 free (new_line); 3442 3682 } 3443 #endif 3683 #endif /* ! AMIGA */ 3444 3684 3445 3685 return new_argv; … … 3449 3689 /* Figure out the argument list necessary to run LINE as a command. Try to 3450 3690 avoid using a shell. This routine handles only ' quoting, and " quoting 3451 when no backslash, $ or `characters are seen in the quotes. Starting3691 when no backslash, $ or ' characters are seen in the quotes. Starting 3452 3692 quotes may be escaped with a backslash. If any of the characters in 3453 sh_chars [] is seen, or any of the builtin commands listed in sh_cmds[]3693 sh_chars is seen, or any of the builtin commands listed in sh_cmds 3454 3694 is the first word of a line, the shell is used. 3455 3695 … … 3462 3702 char ** 3463 3703 construct_command_argv (char *line, char **restp, struct file *file, 3464 int cmd_flags, char **batch_filename _ptr)3704 int cmd_flags, char **batch_filename) 3465 3705 { 3466 3706 char *shell, *ifs, *shellflags; … … 3475 3715 for (;;) 3476 3716 { 3477 while ((*cptr != 0) 3478 && (isspace ((unsigned char)*cptr))) 3479 cptr++; 3717 while ((*cptr != 0) && (ISSPACE (*cptr))) 3718 cptr++; 3480 3719 if (*cptr == 0) 3481 break; 3482 while ((*cptr != 0) 3483 && (!isspace((unsigned char)*cptr))) 3484 cptr++; 3720 break; 3721 while ((*cptr != 0) && (!ISSPACE (*cptr))) 3722 cptr++; 3485 3723 argc++; 3486 3724 } … … 3494 3732 for (;;) 3495 3733 { 3496 while ((*cptr != 0) 3497 && (isspace ((unsigned char)*cptr))) 3498 cptr++; 3734 while ((*cptr != 0) && (ISSPACE (*cptr))) 3735 cptr++; 3499 3736 if (*cptr == 0) 3500 3737 break; 3501 3738 DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr)); 3502 3739 argv[argc++] = cptr; 3503 while ((*cptr != 0) 3504 && (!isspace((unsigned char)*cptr))) 3505 cptr++; 3740 while ((*cptr != 0) && (!ISSPACE (*cptr))) 3741 cptr++; 3506 3742 if (*cptr != 0) 3507 3743 *cptr++ = 0; 3508 3744 } 3509 3745 #else … … 3519 3755 * is not confused. 3520 3756 */ 3521 if (shell) { 3522 char *p = w32ify (shell, 0); 3523 strcpy (shell, p); 3524 } 3757 if (shell) 3758 { 3759 char *p = w32ify (shell, 0); 3760 strcpy (shell, p); 3761 } 3525 3762 #endif 3526 3763 #ifdef __EMX__ … … 3530 3767 static int init = 0; 3531 3768 if (init == 0) 3532 3533 3534 3535 3536 3537 3769 { 3770 unixroot = getenv ("UNIXROOT"); 3771 /* unixroot must be NULL or not empty */ 3772 if (unixroot && unixroot[0] == '\0') unixroot = NULL; 3773 init = 1; 3774 } 3538 3775 3539 3776 /* if we have an unixroot drive and if shell is not default_shell … … 3543 3780 "$UNIXROOT/bin/sh" instead. */ 3544 3781 if (unixroot && shell && strcmp (shell, last_shell) != 0 3545 3546 3547 3548 3549 3550 3551 3782 && (shell[0] == '/' || shell[0] == '\\')) 3783 { 3784 /* trying a new shell, check whether it exists */ 3785 size_t size = strlen (shell); 3786 char *buf = xmalloc (size + 7); 3787 memcpy (buf, shell, size); 3788 memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */ 3552 3789 if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0) 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3790 { 3791 /* try the same for the unixroot drive */ 3792 memmove (buf + 2, buf, size + 5); 3793 buf[0] = unixroot[0]; 3794 buf[1] = unixroot[1]; 3795 if (access (buf, F_OK) == 0) 3796 /* we have found a shell! */ 3797 /* free(shell); */ 3798 shell = buf; 3799 else 3800 free (buf); 3801 } 3802 else 3566 3803 free (buf); 3567 3804 } 3568 3805 } 3569 3806 #endif /* __EMX__ */ … … 3575 3812 } 3576 3813 3577 # ifdef CONFIG_WITH_KMK_BUILTIN3814 # ifdef CONFIG_WITH_KMK_BUILTIN 3578 3815 /* If it's a kmk_builtin command, make sure we're treated like a 3579 3816 unix shell and and don't get batch files. */ 3580 3817 if ( ( !unixy_shell 3581 3818 || batch_mode_shell 3582 # ifdef WINDOWS323819 # ifdef WINDOWS32 3583 3820 || no_default_sh_exe 3584 # endif3821 # endif 3585 3822 ) 3586 3823 && line 3587 && !strncmp (line, "kmk_builtin_", sizeof("kmk_builtin_") - 1))3824 && !strncmp (line, "kmk_builtin_", sizeof("kmk_builtin_") - 1)) 3588 3825 { 3589 3826 int saved_batch_mode_shell = batch_mode_shell; 3590 3827 int saved_unixy_shell = unixy_shell; 3591 # ifdef WINDOWS323828 # ifdef WINDOWS32 3592 3829 int saved_no_default_sh_exe = no_default_sh_exe; 3593 3830 no_default_sh_exe = 0; 3594 # endif3831 # endif 3595 3832 unixy_shell = 1; 3596 3833 batch_mode_shell = 0; 3597 3834 argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs, 3598 cmd_flags, batch_filename _ptr);3835 cmd_flags, batch_filename); 3599 3836 batch_mode_shell = saved_batch_mode_shell; 3600 3837 unixy_shell = saved_unixy_shell; 3601 # ifdef WINDOWS323838 # ifdef WINDOWS32 3602 3839 no_default_sh_exe = saved_no_default_sh_exe; 3603 # endif3840 # endif 3604 3841 } 3605 3842 else 3606 # endif /* CONFIG_WITH_KMK_BUILTIN */3843 # endif /* CONFIG_WITH_KMK_BUILTIN */ 3607 3844 argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs, 3608 cmd_flags, batch_filename _ptr);3845 cmd_flags, batch_filename); 3609 3846 3610 3847 free (shell); … … 3623 3860 3624 3861 (void) close (new); 3625 fd = dup (old);3862 EINTRLOOP (fd, dup (old)); 3626 3863 if (fd != new) 3627 3864 { … … 3650 3887 if (len > print_time_width) 3651 3888 print_time_width = len; 3652 message (1, _("%*s - %s"), print_time_width, buf, c ->file->name); 3889 message (1, print_time_width + strlen (c->file->name), 3890 _("%*s - %s"), print_time_width, buf, c->file->name); 3653 3891 } 3654 3892 }
Note:
See TracChangeset
for help on using the changeset viewer.