1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1999,
3 2000,2001,2002,2003,2004,2005 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
34 /* Default shell to use. */
38 char *default_shell
= "sh.exe";
39 int no_default_sh_exe
= 1;
40 int batch_mode_shell
= 1;
43 #elif defined (_AMIGA)
45 char default_shell
[] = "";
46 extern int MyExecute (char **);
47 int batch_mode_shell
= 0;
49 #elif defined (__MSDOS__)
51 /* The default shell is a pointer so we can change it if Makefile
52 says so. It is without an explicit path so we get a chance
53 to search the $PATH for it (since MSDOS doesn't have standard
54 directories we could trust). */
55 char *default_shell
= "command.com";
56 int batch_mode_shell
= 0;
58 #elif defined (__EMX__)
60 char *default_shell
= "/bin/sh";
61 int batch_mode_shell
= 0;
66 char default_shell
[] = "";
67 int batch_mode_shell
= 0;
69 #elif defined (__riscos__)
71 char default_shell
[] = "";
72 int batch_mode_shell
= 0;
76 char default_shell
[] = "/bin/sh";
77 int batch_mode_shell
= 0;
83 static int execute_by_shell
;
84 static int dos_pid
= 123;
86 int dos_command_running
;
87 #endif /* __MSDOS__ */
90 # include <proto/dos.h>
91 static int amiga_pid
= 123;
92 static int amiga_status
;
93 static char amiga_bname
[32];
94 static int amiga_batch_file
;
99 # include <processes.h>
101 # include <starlet.h>
102 # include <lib$routines.h>
103 static void vmsWaitForChildren
PARAMS ((int *));
107 # include <windows.h>
109 # include <process.h>
110 # include "sub_proc.h"
112 # include "pathstuff.h"
113 #endif /* WINDOWS32 */
116 # include <process.h>
119 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
120 # include <sys/wait.h>
124 # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
125 #else /* Don't have waitpid. */
130 # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
131 # endif /* Have wait3. */
132 #endif /* Have waitpid. */
134 #if !defined (wait) && !defined (POSIX)
138 #ifndef HAVE_UNION_WAIT
143 # define WTERMSIG(x) ((x) & 0x7f)
146 # define WCOREDUMP(x) ((x) & 0x80)
149 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
152 # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
155 # define WIFEXITED(x) (WTERMSIG (x) == 0)
158 #else /* Have `union wait'. */
160 # define WAIT_T union wait
162 # define WTERMSIG(x) ((x).w_termsig)
165 # define WCOREDUMP(x) ((x).w_coredump)
168 # define WEXITSTATUS(x) ((x).w_retcode)
171 # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
174 # define WIFEXITED(x) (WTERMSIG(x) == 0)
177 #endif /* Don't have `union wait'. */
179 #ifndef HAVE_UNISTD_H
181 extern int execve ();
182 extern void _exit ();
184 extern int geteuid ();
185 extern int getegid ();
186 extern int setgid ();
187 extern int getgid ();
191 extern char *allocated_variable_expand_for_file
PARAMS ((char *line
, struct file
*file
));
193 extern int getloadavg
PARAMS ((double loadavg
[], int nelem
));
194 extern int start_remote_job
PARAMS ((char **argv
, char **envp
, int stdin_fd
,
195 int *is_remote
, int *id_ptr
, int *used_stdin
));
196 extern int start_remote_job_p
PARAMS ((int));
197 extern int remote_status
PARAMS ((int *exit_code_ptr
, int *signal_ptr
,
198 int *coredump_ptr
, int block
));
200 RETSIGTYPE child_handler
PARAMS ((int));
201 static void free_child
PARAMS ((struct child
*));
202 static void start_job_command
PARAMS ((struct child
*child
));
203 static int load_too_high
PARAMS ((void));
204 static int job_next_command
PARAMS ((struct child
*));
205 static int start_waiting_job
PARAMS ((struct child
*));
207 /* Chain of all live (or recently deceased) children. */
209 struct child
*children
= 0;
211 /* Number of children currently running. */
213 unsigned int job_slots_used
= 0;
215 /* Nonzero if the `good' standard input is in use. */
217 static int good_stdin_used
= 0;
219 /* Chain of children waiting to run until the load average goes down. */
221 static struct child
*waiting_jobs
= 0;
223 /* Non-zero if we use a *real* shell (always so on Unix). */
227 /* Number of jobs started in the current second. */
229 unsigned long job_counter
= 0;
231 /* Number of jobserver tokens this instance is currently using. */
233 unsigned int jobserver_tokens
= 0;
237 * The macro which references this function is defined in make.h.
240 w32_kill(int pid
, int sig
)
242 return ((process_kill((HANDLE
)pid
, sig
) == TRUE
) ? 0 : -1);
245 /* This function creates a temporary file name with the given extension
246 * the unixy param controls both the extension and the path separator
247 * return an xmalloc'ed string of a newly created temp file or die. */
249 create_batch_filename(char const *base
, int unixy
)
251 const char *const ext
= unixy
? "sh" : "bat";
252 const char *error
= NULL
;
253 char temp_path
[MAXPATHLEN
]; /* need to know its length */
254 unsigned path_size
= GetTempPath(sizeof temp_path
, temp_path
);
257 const unsigned sizemax
= strlen (base
) + strlen (ext
) + 10;
261 path_size
= GetCurrentDirectory (sizeof temp_path
, temp_path
);
265 while (path_size
> 0 &&
266 path_size
+ sizemax
< sizeof temp_path
&&
269 unsigned size
= sprintf (temp_path
+ path_size
,
271 temp_path
[path_size
- 1] == '\\' ? "" : "\\",
273 HANDLE h
= CreateFile (temp_path
, /* file name */
274 GENERIC_READ
| GENERIC_WRITE
, /* desired access */
275 0, /* no share mode */
276 NULL
, /* default security attributes */
277 CREATE_NEW
, /* creation disposition */
278 FILE_ATTRIBUTE_NORMAL
| /* flags and attributes */
279 FILE_ATTRIBUTE_TEMPORARY
, /* we'll delete it */
280 NULL
); /* no template file */
282 if (h
== INVALID_HANDLE_VALUE
)
284 const DWORD er
= GetLastError();
286 if (er
== ERROR_FILE_EXISTS
|| er
== ERROR_ALREADY_EXISTS
)
289 /* the temporary path is not guaranteed to exist */
290 else if (path_is_dot
== 0)
292 path_size
= GetCurrentDirectory (sizeof temp_path
, temp_path
);
298 error
= map_windows32_error_to_string (er
);
304 const unsigned final_size
= path_size
+ size
+ 1;
305 char *const path
= (char *) xmalloc (final_size
);
306 memcpy (path
, temp_path
, final_size
);
312 for (p
= path
; (ch
= *p
) != 0; ++p
)
316 return path
; /* good return */
321 error
= _("Cannot create a temporary file\n");
327 #endif /* WINDOWS32 */
330 /* returns whether path is assumed to be a unix like shell. */
332 _is_unixy_shell (const char *path
)
334 /* list of non unix shells */
335 const char *known_os2shells
[] = {
347 /* find the rightmost '/' or '\\' */
348 const char *name
= strrchr (path
, '/');
349 const char *p
= strrchr (path
, '\\');
352 if (name
&& p
) /* take the max */
353 name
= (name
> p
) ? name
: p
;
354 else if (p
) /* name must be 0 */
356 else if (!name
) /* name and p must be 0 */
359 if (*name
== '/' || *name
== '\\') name
++;
362 while (known_os2shells
[i
] != NULL
) {
363 if (stricmp (name
, known_os2shells
[i
]) == 0) /* strcasecmp() */
364 return 0; /* not a unix shell */
368 /* in doubt assume a unix like shell */
374 /* Write an error message describing the exit status given in
375 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
376 Append "(ignored)" if IGNORED is nonzero. */
379 child_error (char *target_name
, int exit_code
, int exit_sig
, int coredump
,
382 if (ignored
&& silent_flag
)
386 if (!(exit_code
& 1))
388 (ignored
? _("*** [%s] Error 0x%x (ignored)")
389 : _("*** [%s] Error 0x%x")),
390 target_name
, exit_code
);
393 error (NILF
, ignored
? _("[%s] Error %d (ignored)") :
394 _("*** [%s] Error %d"),
395 target_name
, exit_code
);
397 error (NILF
, "*** [%s] %s%s",
398 target_name
, strsignal (exit_sig
),
399 coredump
? _(" (core dumped)") : "");
404 /* Handle a dead child. This handler may or may not ever be installed.
406 If we're using the jobserver feature, we need it. First, installing it
407 ensures the read will interrupt on SIGCHLD. Second, we close the dup'd
408 read FD to ensure we don't enter another blocking read without reaping all
409 the dead children. In this case we don't need the dead_children count.
411 If we don't have either waitpid or wait3, then make is unreliable, but we
412 use the dead_children count to reap children as best we can. */
414 static unsigned int dead_children
= 0;
417 child_handler (int sig UNUSED
)
428 /* The signal handler must called only once! */
429 signal (SIGCHLD
, SIG_DFL
);
432 /* This causes problems if the SIGCHLD interrupts a printf().
433 DB (DB_JOBS, (_("Got a SIGCHLD; %u unreaped children.\n"), dead_children));
437 extern int shell_function_pid
, shell_function_completed
;
439 /* Reap all dead children, storing the returned status and the new command
440 state (`cs_finished') in the `file' member of the `struct child' for the
441 dead child, and removing the child from the chain. In addition, if BLOCK
442 nonzero, we block in this function until we've reaped at least one
443 complete child, waiting for it to die if necessary. If ERR is nonzero,
444 print an error message first. */
447 reap_children (int block
, int err
)
451 /* Initially, assume we have some. */
456 # define REAP_MORE reap_more
458 # define REAP_MORE dead_children
463 We have at least one child outstanding OR a shell function in progress,
465 We're blocking for a complete child OR there are more children to reap
467 we'll keep reaping children. */
469 while ((children
!= 0 || shell_function_pid
!= 0)
470 && (block
|| REAP_MORE
))
474 int exit_code
, exit_sig
, coredump
;
475 register struct child
*lastc
, *c
;
477 int any_remote
, any_local
;
482 static int printed
= 0;
484 /* We might block for a while, so let the user know why.
485 Only print this message once no matter how many jobs are left. */
488 error (NILF
, _("*** Waiting for unfinished jobs...."));
492 /* We have one less dead child to reap. As noted in
493 child_handler() above, this count is completely unimportant for
494 all modern, POSIX-y systems that support wait3() or waitpid().
495 The rest of this comment below applies only to early, broken
496 pre-POSIX systems. We keep the count only because... it's there...
498 The test and decrement are not atomic; if it is compiled into:
499 register = dead_children - 1;
500 dead_children = register;
501 a SIGCHLD could come between the two instructions.
502 child_handler increments dead_children.
503 The second instruction here would lose that increment. But the
504 only effect of dead_children being wrong is that we might wait
505 longer than necessary to reap a child, and lose some parallelism;
506 and we might print the "Waiting for unfinished jobs" message above
507 when not necessary. */
509 if (dead_children
> 0)
513 any_local
= shell_function_pid
!= 0;
514 for (c
= children
; c
!= 0; c
= c
->next
)
516 any_remote
|= c
->remote
;
517 any_local
|= ! c
->remote
;
518 DB (DB_JOBS
, (_("Live child 0x%08lx (%s) PID %ld %s\n"),
519 (unsigned long int) c
, c
->file
->name
,
520 (long) c
->pid
, c
->remote
? _(" (remote)") : ""));
526 /* First, check for remote children. */
528 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 0);
533 /* We got a remote child. */
537 /* A remote status command failed miserably. Punt. */
539 pfatal_with_name ("remote_status");
543 /* No remote children. Check for local children. */
544 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
548 vmsWaitForChildren (&status
);
553 pid
= WAIT_NOHANG (&status
);
556 pid
= wait (&status
);
564 /* The wait*() failed miserably. Punt. */
565 pfatal_with_name ("wait");
569 /* We got a child exit; chop the status word up. */
570 exit_code
= WEXITSTATUS (status
);
571 exit_sig
= WIFSIGNALED (status
) ? WTERMSIG (status
) : 0;
572 coredump
= WCOREDUMP (status
);
574 /* If we have started jobs in this second, remove one. */
580 /* No local children are dead. */
583 if (!block
|| !any_remote
)
586 /* Now try a blocking wait for a remote child. */
587 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 1);
589 goto remote_status_lose
;
591 /* No remote children either. Finally give up. */
594 /* We got a remote child. */
597 #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
600 /* Life is very different on MSDOS. */
603 exit_code
= WEXITSTATUS (status
);
604 if (exit_code
== 0xff)
606 exit_sig
= WIFSIGNALED (status
) ? WTERMSIG (status
) : 0;
608 #endif /* __MSDOS__ */
612 status
= amiga_status
;
613 exit_code
= amiga_status
;
626 /* Record the thread ID of the main process, so that we
627 could suspend it in the signal handler. */
630 hcTID
= GetCurrentThread ();
631 hcPID
= GetCurrentProcess ();
632 if (!DuplicateHandle (hcPID
, hcTID
, hcPID
, &main_thread
, 0,
633 FALSE
, DUPLICATE_SAME_ACCESS
))
635 DWORD e
= GetLastError ();
637 "Determine main thread ID (Error %ld: %s)\n",
638 e
, map_windows32_error_to_string(e
));
641 DB (DB_VERBOSE
, ("Main thread handle = 0x%08lx\n",
642 (unsigned long)main_thread
));
645 /* wait for anything to finish */
646 hPID
= process_wait_for_any();
650 /* was an error found on this process? */
651 werr
= process_last_err(hPID
);
654 exit_code
= process_exit_code(hPID
);
657 fprintf(stderr
, "make (e=%d): %s",
658 exit_code
, map_windows32_error_to_string(exit_code
));
661 exit_sig
= process_signal(hPID
);
663 /* cleanup process */
664 process_cleanup(hPID
);
670 #endif /* WINDOWS32 */
673 /* Check if this is the child of the `shell' function. */
674 if (!remote
&& pid
== shell_function_pid
)
676 /* It is. Leave an indicator for the `shell' function. */
677 if (exit_sig
== 0 && exit_code
== 127)
678 shell_function_completed
= -1;
680 shell_function_completed
= 1;
684 child_failed
= exit_sig
!= 0 || exit_code
!= 0;
686 /* Search for a child matching the deceased one. */
688 for (c
= children
; c
!= 0; lastc
= c
, c
= c
->next
)
689 if (c
->remote
== remote
&& c
->pid
== pid
)
693 /* An unknown child died.
694 Ignore it; it was inherited from our invoker. */
697 DB (DB_JOBS
, (child_failed
698 ? _("Reaping losing child 0x%08lx PID %ld %s\n")
699 : _("Reaping winning child 0x%08lx PID %ld %s\n"),
700 (unsigned long int) c
, (long) c
->pid
,
701 c
->remote
? _(" (remote)") : ""));
703 if (c
->sh_batch_file
) {
704 DB (DB_JOBS
, (_("Cleaning up temp batch file %s\n"),
707 /* just try and remove, don't care if this fails */
708 remove (c
->sh_batch_file
);
710 /* all done with memory */
711 free (c
->sh_batch_file
);
712 c
->sh_batch_file
= NULL
;
715 /* If this child had the good stdin, say it is now free. */
719 dontcare
= c
->file
->dontcare
;
721 if (child_failed
&& !c
->noerror
&& !ignore_errors_flag
)
723 /* The commands failed. Write an error message,
724 delete non-precious targets, and abort. */
725 static int delete_on_error
= -1;
728 child_error (c
->file
->name
, exit_code
, exit_sig
, coredump
, 0);
730 c
->file
->update_status
= 2;
731 if (delete_on_error
== -1)
733 struct file
*f
= lookup_file (".DELETE_ON_ERROR");
734 delete_on_error
= f
!= 0 && f
->is_target
;
736 if (exit_sig
!= 0 || delete_on_error
)
737 delete_child_targets (c
);
743 /* The commands failed, but we don't care. */
744 child_error (c
->file
->name
,
745 exit_code
, exit_sig
, coredump
, 1);
749 /* If there are more commands to run, try to start them. */
750 if (job_next_command (c
))
752 if (handling_fatal_signal
)
754 /* Never start new commands while we are dying.
755 Since there are more commands that wanted to be run,
756 the target was not completely remade. So we treat
757 this as if a command had failed. */
758 c
->file
->update_status
= 2;
762 /* Check again whether to start remotely.
763 Whether or not we want to changes over time.
764 Also, start_remote_job may need state set up
765 by start_remote_job_p. */
766 c
->remote
= start_remote_job_p (0);
767 start_job_command (c
);
768 /* Fatal signals are left blocked in case we were
769 about to put that child on the chain. But it is
770 already there, so it is safe for a fatal signal to
771 arrive now; it will clean up this child's targets. */
773 if (c
->file
->command_state
== cs_running
)
774 /* We successfully started the new command.
775 Loop to reap more children. */
779 if (c
->file
->update_status
!= 0)
780 /* We failed to start the commands. */
781 delete_child_targets (c
);
784 /* There are no more commands. We got through them all
785 without an unignored error. Now the target has been
786 successfully updated. */
787 c
->file
->update_status
= 0;
790 /* When we get here, all the commands for C->file are finished
791 (or aborted) and C->file->update_status contains 0 or 2. But
792 C->file->command_state is still cs_running if all the commands
793 ran; notice_finish_file looks for cs_running to tell it that
794 it's interesting to check the file's modtime again now. */
796 if (! handling_fatal_signal
)
797 /* Notice if the target of the commands has been changed.
798 This also propagates its values for command_state and
799 update_status to its also_make files. */
800 notice_finished_file (c
->file
);
802 DB (DB_JOBS
, (_("Removing child 0x%08lx PID %ld%s from chain.\n"),
803 (unsigned long int) c
, (long) c
->pid
,
804 c
->remote
? _(" (remote)") : ""));
806 /* Block fatal signals while frobnicating the list, so that
807 children and job_slots_used are always consistent. Otherwise
808 a fatal signal arriving after the child is off the chain and
809 before job_slots_used is decremented would believe a child was
810 live and call reap_children again. */
813 /* There is now another slot open. */
814 if (job_slots_used
> 0)
817 /* Remove the child from the chain and free it. */
821 lastc
->next
= c
->next
;
827 /* If the job failed, and the -k flag was not given, die,
828 unless we are already in the process of dying. */
829 if (!err
&& child_failed
&& !dontcare
&& !keep_going_flag
&&
830 /* fatal_error_signal will die with the right signal. */
831 !handling_fatal_signal
)
834 /* Only block for one child. */
841 /* Free the storage allocated for CHILD. */
844 free_child (struct child
*child
)
846 if (!jobserver_tokens
)
847 fatal (NILF
, "INTERNAL: Freeing child 0x%08lx (%s) but no tokens left!\n",
848 (unsigned long int) child
, child
->file
->name
);
850 /* If we're using the jobserver and this child is not the only outstanding
851 job, put a token back into the pipe for it. */
853 if (job_fds
[1] >= 0 && jobserver_tokens
> 1)
858 /* Write a job token back to the pipe. */
860 EINTRLOOP (r
, write (job_fds
[1], &token
, 1));
862 pfatal_with_name (_("write jobserver"));
864 DB (DB_JOBS
, (_("Released token for child 0x%08lx (%s).\n"),
865 (unsigned long int) child
, child
->file
->name
));
870 if (handling_fatal_signal
) /* Don't bother free'ing if about to die. */
873 if (child
->command_lines
!= 0)
875 register unsigned int i
;
876 for (i
= 0; i
< child
->file
->cmds
->ncommand_lines
; ++i
)
877 free (child
->command_lines
[i
]);
878 free ((char *) child
->command_lines
);
881 if (child
->environment
!= 0)
883 register char **ep
= child
->environment
;
886 free ((char *) child
->environment
);
889 free ((char *) child
);
893 extern sigset_t fatal_signal_set
;
900 (void) sigprocmask (SIG_BLOCK
, &fatal_signal_set
, (sigset_t
*) 0);
902 # ifdef HAVE_SIGSETMASK
903 (void) sigblock (fatal_signal_mask
);
913 sigemptyset (&empty
);
914 sigprocmask (SIG_SETMASK
, &empty
, (sigset_t
*) 0);
918 #ifdef MAKE_JOBSERVER
920 job_noop (int sig UNUSED
)
923 /* Set the child handler action flags to FLAGS. */
925 set_child_handler_action_flags (int set_handler
, int set_alarm
)
930 /* The child handler must be turned off here. */
931 signal (SIGCHLD
, SIG_DFL
);
934 bzero ((char *) &sa
, sizeof sa
);
935 sa
.sa_handler
= child_handler
;
936 sa
.sa_flags
= set_handler
? 0 : SA_RESTART
;
938 sigaction (SIGCHLD
, &sa
, NULL
);
940 #if defined SIGCLD && SIGCLD != SIGCHLD
941 sigaction (SIGCLD
, &sa
, NULL
);
946 /* If we're about to enter the read(), set an alarm to wake up in a
947 second so we can check if the load has dropped and we can start more
948 work. On the way out, turn off the alarm and set SIG_DFL. */
949 alarm (set_handler
? 1 : 0);
950 sa
.sa_handler
= set_handler
? job_noop
: SIG_DFL
;
952 sigaction (SIGALRM
, &sa
, NULL
);
959 /* Start a job to run the commands specified in CHILD.
960 CHILD is updated to reflect the commands and ID of the child process.
962 NOTE: On return fatal signals are blocked! The caller is responsible
963 for calling `unblock_sigs', once the new child is safely on the chain so
964 it can be cleaned up in the event of a fatal signal. */
967 start_job_command (struct child
*child
)
969 #if !defined(_AMIGA) && !defined(WINDOWS32)
970 static int bad_stdin
= -1;
980 /* If we have a completely empty commandset, stop now. */
981 if (!child
->command_ptr
)
984 /* Combine the flags parsed for the line itself with
985 the flags specified globally for this target. */
986 flags
= (child
->file
->command_flags
987 | child
->file
->cmds
->lines_flags
[child
->command_line
- 1]);
989 p
= child
->command_ptr
;
990 child
->noerror
= ((flags
& COMMANDS_NOERROR
) != 0);
995 flags
|= COMMANDS_SILENT
;
997 flags
|= COMMANDS_RECURSE
;
1000 else if (!isblank ((unsigned char)*p
))
1005 /* Update the file's command flags with any new ones we found. We only
1006 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
1007 now marking more commands recursive than should be in the case of
1008 multiline define/endef scripts where only one line is marked "+". In
1009 order to really fix this, we'll have to keep a lines_flags for every
1010 actual line, after expansion. */
1011 child
->file
->cmds
->lines_flags
[child
->command_line
- 1]
1012 |= flags
& COMMANDS_RECURSE
;
1014 /* Figure out an argument list from this command line. */
1021 argv
= construct_command_argv (p
, &end
, child
->file
, &child
->sh_batch_file
);
1024 child
->command_ptr
= NULL
;
1028 child
->command_ptr
= end
;
1032 /* If -q was given, say that updating `failed' if there was any text on the
1033 command line, or `succeeded' otherwise. The exit status of 1 tells the
1034 user that -q is saying `something to do'; the exit status for a random
1036 if (argv
!= 0 && question_flag
&& !(flags
& COMMANDS_RECURSE
))
1040 free ((char *) argv
);
1042 child
->file
->update_status
= 1;
1043 notice_finished_file (child
->file
);
1047 if (touch_flag
&& !(flags
& COMMANDS_RECURSE
))
1049 /* Go on to the next command. It might be the recursive one.
1050 We construct ARGV only to find the end of the command line. */
1055 free ((char *) argv
);
1065 execute_by_shell
= 0; /* in case construct_command_argv sets it */
1067 /* This line has no commands. Go to the next. */
1068 if (job_next_command (child
))
1069 start_job_command (child
);
1072 /* No more commands. Make sure we're "running"; we might not be if
1073 (e.g.) all commands were skipped due to -n. */
1074 set_command_state (child
->file
, cs_running
);
1075 child
->file
->update_status
= 0;
1076 notice_finished_file (child
->file
);
1081 /* Print out the command. If silent, we call `message' with null so it
1082 can log the working directory before the command's own error messages
1085 message (0, (just_print_flag
|| (!(flags
& COMMANDS_SILENT
) && !silent_flag
))
1086 ? "%s" : (char *) 0, p
);
1088 /* Tell update_goal_chain that a command has been started on behalf of
1089 this target. It is important that this happens here and not in
1090 reap_children (where we used to do it), because reap_children might be
1091 reaping children from a different target. We want this increment to
1092 guaranteedly indicate that a command was started for the dependency
1093 chain (i.e., update_file recursion chain) we are processing. */
1097 /* Optimize an empty command. People use this for timestamp rules,
1098 so avoid forking a useless shell. Do this after we increment
1099 commands_started so make still treats this special case as if it
1100 performed some action (makes a difference as to what messages are
1103 #if !defined(VMS) && !defined(_AMIGA)
1105 #if defined __MSDOS__ || defined (__EMX__)
1106 unixy_shell
/* the test is complicated and we already did it */
1108 (argv
[0] && !strcmp (argv
[0], "/bin/sh"))
1111 && argv
[1][0] == '-' && argv
[1][1] == 'c' && argv
[1][2] == '\0')
1112 && (argv
[2] && argv
[2][0] == ':' && argv
[2][1] == '\0')
1116 free ((char *) argv
);
1119 #endif /* !VMS && !_AMIGA */
1121 /* If -n was given, recurse to get the next line in the sequence. */
1123 if (just_print_flag
&& !(flags
& COMMANDS_RECURSE
))
1127 free ((char *) argv
);
1132 /* Flush the output streams so they won't have things written twice. */
1138 #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
1140 /* Set up a bad standard input that reads from a broken pipe. */
1142 if (bad_stdin
== -1)
1144 /* Make a file descriptor that is the read end of a broken pipe.
1145 This will be used for some children's standard inputs. */
1149 /* Close the write side. */
1150 (void) close (pd
[1]);
1151 /* Save the read side. */
1154 /* Set the descriptor to close on exec, so it does not litter any
1155 child's descriptor table. When it is dup2'd onto descriptor 0,
1156 that descriptor will not close on exec. */
1157 CLOSE_ON_EXEC (bad_stdin
);
1161 #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
1163 /* Decide whether to give this child the `good' standard input
1164 (one that points to the terminal or whatever), or the `bad' one
1165 that points to the read side of a broken pipe. */
1167 child
->good_stdin
= !good_stdin_used
;
1168 if (child
->good_stdin
)
1169 good_stdin_used
= 1;
1176 /* Set up the environment for the child. */
1177 if (child
->environment
== 0)
1178 child
->environment
= target_environment (child
->file
);
1181 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1184 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1187 int is_remote
, id
, used_stdin
;
1188 if (start_remote_job (argv
, child
->environment
,
1189 child
->good_stdin
? 0 : bad_stdin
,
1190 &is_remote
, &id
, &used_stdin
))
1191 /* Don't give up; remote execution may fail for various reasons. If
1192 so, simply run the job locally. */
1196 if (child
->good_stdin
&& !used_stdin
)
1198 child
->good_stdin
= 0;
1199 good_stdin_used
= 0;
1201 child
->remote
= is_remote
;
1208 /* Fork the child process. */
1210 char **parent_environ
;
1218 if (!child_execute_job (argv
, child
)) {
1220 perror_with_name ("vfork", "");
1226 parent_environ
= environ
;
1229 /* If we aren't running a recursive command and we have a jobserver
1230 pipe, close it before exec'ing. */
1231 if (!(flags
& COMMANDS_RECURSE
) && job_fds
[0] >= 0)
1233 CLOSE_ON_EXEC (job_fds
[0]);
1234 CLOSE_ON_EXEC (job_fds
[1]);
1237 CLOSE_ON_EXEC (job_rfd
);
1239 /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
1240 child
->pid
= child_execute_job (child
->good_stdin
? 0 : bad_stdin
, 1,
1241 argv
, child
->environment
);
1246 perror_with_name ("spawn", "");
1250 /* undo CLOSE_ON_EXEC() after the child process has been started */
1251 if (!(flags
& COMMANDS_RECURSE
) && job_fds
[0] >= 0)
1253 fcntl (job_fds
[0], F_SETFD
, 0);
1254 fcntl (job_fds
[1], F_SETFD
, 0);
1257 fcntl (job_rfd
, F_SETFD
, 0);
1259 #else /* !__EMX__ */
1261 child
->pid
= vfork ();
1262 environ
= parent_environ
; /* Restore value child may have clobbered. */
1263 if (child
->pid
== 0)
1265 /* We are the child side. */
1268 /* If we aren't running a recursive command and we have a jobserver
1269 pipe, close it before exec'ing. */
1270 if (!(flags
& COMMANDS_RECURSE
) && job_fds
[0] >= 0)
1278 child_execute_job (child
->good_stdin
? 0 : bad_stdin
, 1,
1279 argv
, child
->environment
);
1281 else if (child
->pid
< 0)
1285 perror_with_name ("vfork", "");
1288 # endif /* !__EMX__ */
1292 #else /* __MSDOS__ or Amiga or WINDOWS32 */
1300 /* We call `system' to do the job of the SHELL, since stock DOS
1301 shell is too dumb. Our `system' knows how to handle long
1302 command lines even if pipes/redirection is needed; it will only
1303 call COMMAND.COM when its internal commands are used. */
1304 if (execute_by_shell
)
1306 char *cmdline
= argv
[0];
1307 /* We don't have a way to pass environment to `system',
1308 so we need to save and restore ours, sigh... */
1309 char **parent_environ
= environ
;
1311 environ
= child
->environment
;
1313 /* If we have a *real* shell, tell `system' to call
1314 it to do everything for us. */
1317 /* A *real* shell on MSDOS may not support long
1318 command lines the DJGPP way, so we must use `system'. */
1319 cmdline
= argv
[2]; /* get past "shell -c" */
1322 dos_command_running
= 1;
1323 proc_return
= system (cmdline
);
1324 environ
= parent_environ
;
1325 execute_by_shell
= 0; /* for the next time */
1329 dos_command_running
= 1;
1330 proc_return
= spawnvpe (P_WAIT
, argv
[0], argv
, child
->environment
);
1333 /* Need to unblock signals before turning off
1334 dos_command_running, so that child's signals
1335 will be treated as such (see fatal_error_signal). */
1337 dos_command_running
= 0;
1339 /* If the child got a signal, dos_status has its
1340 high 8 bits set, so be careful not to alter them. */
1341 if (proc_return
== -1)
1344 dos_status
|= (proc_return
& 0xff);
1346 child
->pid
= dos_pid
++;
1348 #endif /* __MSDOS__ */
1350 amiga_status
= MyExecute (argv
);
1353 child
->pid
= amiga_pid
++;
1354 if (amiga_batch_file
)
1356 amiga_batch_file
= 0;
1357 DeleteFile (amiga_bname
); /* Ignore errors. */
1365 /* make UNC paths safe for CreateProcess -- backslash format */
1367 if (arg0
&& arg0
[0] == '/' && arg0
[1] == '/')
1368 for ( ; arg0
&& *arg0
; arg0
++)
1372 /* make sure CreateProcess() has Path it needs */
1373 sync_Path_environment();
1375 hPID
= process_easy(argv
, child
->environment
);
1377 if (hPID
!= INVALID_HANDLE_VALUE
)
1378 child
->pid
= (int) hPID
;
1383 _("process_easy() failed failed to launch process (e=%ld)\n"),
1384 process_last_err(hPID
));
1385 for (i
= 0; argv
[i
]; i
++)
1386 fprintf(stderr
, "%s ", argv
[i
]);
1387 fprintf(stderr
, _("\nCounted %d args in failed launch\n"), i
);
1390 #endif /* WINDOWS32 */
1391 #endif /* __MSDOS__ or Amiga or WINDOWS32 */
1393 /* Bump the number of jobs started in this second. */
1396 /* We are the parent side. Set the state to
1397 say the commands are running and return. */
1399 set_command_state (child
->file
, cs_running
);
1401 /* Free the storage used by the child's argument list. */
1404 free ((char *) argv
);
1410 child
->file
->update_status
= 2;
1411 notice_finished_file (child
->file
);
1415 /* Try to start a child running.
1416 Returns nonzero if the child was started (and maybe finished), or zero if
1417 the load was too high and the child was put on the `waiting_jobs' chain. */
1420 start_waiting_job (struct child
*c
)
1422 struct file
*f
= c
->file
;
1424 /* If we can start a job remotely, we always want to, and don't care about
1425 the local load average. We record that the job should be started
1426 remotely in C->remote for start_job_command to test. */
1428 c
->remote
= start_remote_job_p (1);
1430 /* If we are running at least one job already and the load average
1431 is too high, make this one wait. */
1432 if (!c
->remote
&& job_slots_used
> 0 && load_too_high ())
1434 /* Put this child on the chain of children waiting for the load average
1436 set_command_state (f
, cs_running
);
1437 c
->next
= waiting_jobs
;
1442 /* Start the first command; reap_children will run later command lines. */
1443 start_job_command (c
);
1445 switch (f
->command_state
)
1449 DB (DB_JOBS
, (_("Putting child 0x%08lx (%s) PID %ld%s on the chain.\n"),
1450 (unsigned long int) c
, c
->file
->name
,
1451 (long) c
->pid
, c
->remote
? _(" (remote)") : ""));
1453 /* One more job slot is in use. */
1458 case cs_not_started
:
1459 /* All the command lines turned out to be empty. */
1460 f
->update_status
= 0;
1464 notice_finished_file (f
);
1469 assert (f
->command_state
== cs_finished
);
1476 /* Create a `struct child' for FILE and start its commands running. */
1479 new_job (struct file
*file
)
1481 register struct commands
*cmds
= file
->cmds
;
1482 register struct child
*c
;
1484 register unsigned int i
;
1486 /* Let any previously decided-upon jobs that are waiting
1487 for the load to go down start before this new one. */
1488 start_waiting_jobs ();
1490 /* Reap any children that might have finished recently. */
1491 reap_children (0, 0);
1493 /* Chop the commands up into lines if they aren't already. */
1494 chop_commands (cmds
);
1496 /* Expand the command lines and store the results in LINES. */
1497 lines
= (char **) xmalloc (cmds
->ncommand_lines
* sizeof (char *));
1498 for (i
= 0; i
< cmds
->ncommand_lines
; ++i
)
1500 /* Collapse backslash-newline combinations that are inside variable
1501 or function references. These are left alone by the parser so
1502 that they will appear in the echoing of commands (where they look
1503 nice); and collapsed by construct_command_argv when it tokenizes.
1504 But letting them survive inside function invocations loses because
1505 we don't want the functions to see them as part of the text. */
1507 char *in
, *out
, *ref
;
1509 /* IN points to where in the line we are scanning.
1510 OUT points to where in the line we are writing.
1511 When we collapse a backslash-newline combination,
1512 IN gets ahead of OUT. */
1514 in
= out
= cmds
->command_lines
[i
];
1515 while ((ref
= strchr (in
, '$')) != 0)
1517 ++ref
; /* Move past the $. */
1520 /* Copy the text between the end of the last chunk
1521 we processed (where IN points) and the new chunk
1522 we are about to process (where REF points). */
1523 bcopy (in
, out
, ref
- in
);
1525 /* Move both pointers past the boring stuff. */
1529 if (*ref
== '(' || *ref
== '{')
1531 char openparen
= *ref
;
1532 char closeparen
= openparen
== '(' ? ')' : '}';
1536 *out
++ = *in
++; /* Copy OPENPAREN. */
1537 /* IN now points past the opening paren or brace.
1538 Count parens or braces until it is matched. */
1542 if (*in
== closeparen
&& --count
< 0)
1544 else if (*in
== '\\' && in
[1] == '\n')
1546 /* We have found a backslash-newline inside a
1547 variable or function reference. Eat it and
1548 any following whitespace. */
1551 for (p
= in
- 1; p
> ref
&& *p
== '\\'; --p
)
1555 /* There were two or more backslashes, so this is
1556 not really a continuation line. We don't collapse
1557 the quoting backslashes here as is done in
1558 collapse_continuations, because the line will
1559 be collapsed again after expansion. */
1563 /* Skip the backslash, newline and
1564 any following whitespace. */
1565 in
= next_token (in
+ 2);
1567 /* Discard any preceding whitespace that has
1568 already been written to the output. */
1570 && isblank ((unsigned char)out
[-1]))
1573 /* Replace it all with a single space. */
1579 if (*in
== openparen
)
1588 /* There are no more references in this line to worry about.
1589 Copy the remaining uninteresting text to the output. */
1593 /* Finally, expand the line. */
1594 lines
[i
] = allocated_variable_expand_for_file (cmds
->command_lines
[i
],
1598 /* Start the command sequence, record it in a new
1599 `struct child', and add that to the chain. */
1601 c
= (struct child
*) xmalloc (sizeof (struct child
));
1602 bzero ((char *)c
, sizeof (struct child
));
1604 c
->command_lines
= lines
;
1605 c
->sh_batch_file
= NULL
;
1607 /* Fetch the first command line to be run. */
1608 job_next_command (c
);
1610 /* Wait for a job slot to be freed up. If we allow an infinite number
1611 don't bother; also job_slots will == 0 if we're using the jobserver. */
1614 while (job_slots_used
== job_slots
)
1615 reap_children (1, 0);
1617 #ifdef MAKE_JOBSERVER
1618 /* If we are controlling multiple jobs make sure we have a token before
1619 starting the child. */
1621 /* This can be inefficient. There's a decent chance that this job won't
1622 actually have to run any subprocesses: the command script may be empty
1623 or otherwise optimized away. It would be nice if we could defer
1624 obtaining a token until just before we need it, in start_job_command.
1625 To do that we'd need to keep track of whether we'd already obtained a
1626 token (since start_job_command is called for each line of the job, not
1627 just once). Also more thought needs to go into the entire algorithm;
1628 this is where the old parallel job code waits, so... */
1630 else if (job_fds
[0] >= 0)
1637 DB (DB_JOBS
, ("Need a job token; we %shave children\n",
1638 children
? "" : "don't "));
1640 /* If we don't already have a job started, use our "free" token. */
1641 if (!jobserver_tokens
)
1644 /* Read a token. As long as there's no token available we'll block.
1645 We enable interruptible system calls before the read(2) so that if
1646 we get a SIGCHLD while we're waiting, we'll return with EINTR and
1647 we can process the death(s) and return tokens to the free pool.
1649 Once we return from the read, we immediately reinstate restartable
1650 system calls. This allows us to not worry about checking for
1651 EINTR on all the other system calls in the program.
1653 There is one other twist: there is a span between the time
1654 reap_children() does its last check for dead children and the time
1655 the read(2) call is entered, below, where if a child dies we won't
1656 notice. This is extremely serious as it could cause us to
1657 deadlock, given the right set of events.
1659 To avoid this, we do the following: before we reap_children(), we
1660 dup(2) the read FD on the jobserver pipe. The read(2) call below
1661 uses that new FD. In the signal handler, we close that FD. That
1662 way, if a child dies during the section mentioned above, the
1663 read(2) will be invoked with an invalid FD and will return
1664 immediately with EBADF. */
1666 /* Make sure we have a dup'd FD. */
1669 DB (DB_JOBS
, ("Duplicate the job FD\n"));
1670 job_rfd
= dup (job_fds
[0]);
1673 /* Reap anything that's currently waiting. */
1674 reap_children (0, 0);
1676 /* Kick off any jobs we have waiting for an opportunity that
1677 can run now (ie waiting for load). */
1678 start_waiting_jobs ();
1680 /* If our "free" slot has become available, use it; we don't need an
1682 if (!jobserver_tokens
)
1685 /* There must be at least one child already, or we have no business
1686 waiting for a token. */
1688 fatal (NILF
, "INTERNAL: no children as we go to sleep on read\n");
1690 /* Set interruptible system calls, and read() for a job token. */
1691 set_child_handler_action_flags (1, waiting_jobs
!= NULL
);
1692 got_token
= read (job_rfd
, &token
, 1);
1693 saved_errno
= errno
;
1694 set_child_handler_action_flags (0, waiting_jobs
!= NULL
);
1696 /* If we got one, we're done here. */
1699 DB (DB_JOBS
, (_("Obtained token for child 0x%08lx (%s).\n"),
1700 (unsigned long int) c
, c
->file
->name
));
1704 /* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,
1705 go back and reap_children(), and try again. */
1706 errno
= saved_errno
;
1707 if (errno
!= EINTR
&& errno
!= EBADF
)
1708 pfatal_with_name (_("read jobs pipe"));
1710 DB (DB_JOBS
, ("Read returned EBADF.\n"));
1716 /* The job is now primed. Start it running.
1717 (This will notice if there are in fact no commands.) */
1718 (void) start_waiting_job (c
);
1720 if (job_slots
== 1 || not_parallel
)
1721 /* Since there is only one job slot, make things run linearly.
1722 Wait for the child to die, setting the state to `cs_finished'. */
1723 while (file
->command_state
== cs_running
)
1724 reap_children (1, 0);
1729 /* Move CHILD's pointers to the next command for it to execute.
1730 Returns nonzero if there is another command. */
1733 job_next_command (struct child
*child
)
1735 while (child
->command_ptr
== 0 || *child
->command_ptr
== '\0')
1737 /* There are no more lines in the expansion of this line. */
1738 if (child
->command_line
== child
->file
->cmds
->ncommand_lines
)
1740 /* There are no more lines to be expanded. */
1741 child
->command_ptr
= 0;
1745 /* Get the next line to run. */
1746 child
->command_ptr
= child
->command_lines
[child
->command_line
++];
1751 /* Determine if the load average on the system is too high to start a new job.
1752 The real system load average is only recomputed once a second. However, a
1753 very parallel make can easily start tens or even hundreds of jobs in a
1754 second, which brings the system to its knees for a while until that first
1755 batch of jobs clears out.
1757 To avoid this we use a weighted algorithm to try to account for jobs which
1758 have been started since the last second, and guess what the load average
1759 would be now if it were computed.
1761 This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
1764 ! calculate something load-oid and add to the observed sys.load,
1765 ! so that latter can catch up:
1766 ! - every job started increases jobctr;
1767 ! - every dying job decreases a positive jobctr;
1768 ! - the jobctr value gets zeroed every change of seconds,
1769 ! after its value*weight_b is stored into the 'backlog' value last_sec
1770 ! - weight_a times the sum of jobctr and last_sec gets
1771 ! added to the observed sys.load.
1773 ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
1774 ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
1775 ! sub-shelled commands (rm, echo, sed...) for tests.
1776 ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
1777 ! resulted in significant excession of the load limit, raising it
1778 ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
1779 ! reach the limit in most test cases.
1781 ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
1782 ! exceeding the limit for longer-running stuff (compile jobs in
1783 ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
1784 ! small jobs' effects.
1788 #define LOAD_WEIGHT_A 0.25
1789 #define LOAD_WEIGHT_B 0.25
1792 load_too_high (void)
1794 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__)
1797 static double last_sec
;
1798 static time_t last_now
;
1802 if (max_load_average
< 0)
1805 /* Find the real system load average. */
1807 if (getloadavg (&load
, 1) != 1)
1809 static int lossage
= -1;
1810 /* Complain only once for the same error. */
1811 if (lossage
== -1 || errno
!= lossage
)
1814 /* An errno value of zero means getloadavg is just unsupported. */
1816 _("cannot enforce load limits on this operating system"));
1818 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
1825 /* If we're in a new second zero the counter and correct the backlog
1826 value. Only keep the backlog for one extra second; after that it's 0. */
1830 if (last_now
== now
- 1)
1831 last_sec
= LOAD_WEIGHT_B
* job_counter
;
1839 /* Try to guess what the load would be right now. */
1840 guess
= load
+ (LOAD_WEIGHT_A
* (job_counter
+ last_sec
));
1842 DB (DB_JOBS
, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
1843 guess
, load
, max_load_average
));
1845 return guess
>= max_load_average
;
1849 /* Start jobs that are waiting for the load to be lower. */
1852 start_waiting_jobs (void)
1856 if (waiting_jobs
== 0)
1861 /* Check for recently deceased descendants. */
1862 reap_children (0, 0);
1864 /* Take a job off the waiting list. */
1866 waiting_jobs
= job
->next
;
1868 /* Try to start that job. We break out of the loop as soon
1869 as start_waiting_job puts one back on the waiting list. */
1871 while (start_waiting_job (job
) && waiting_jobs
!= 0);
1878 /* EMX: Start a child process. This function returns the new pid. */
1879 # if defined __MSDOS__ || defined __EMX__
1881 child_execute_job (int stdin_fd
, int stdout_fd
, char **argv
, char **envp
)
1884 /* stdin_fd == 0 means: nothing to do for stdin;
1885 stdout_fd == 1 means: nothing to do for stdout */
1886 int save_stdin
= (stdin_fd
!= 0) ? dup (0) : 0;
1887 int save_stdout
= (stdout_fd
!= 1) ? dup (1): 1;
1889 /* < 0 only if dup() failed */
1891 fatal (NILF
, _("no more file handles: could not duplicate stdin\n"));
1892 if (save_stdout
< 0)
1893 fatal (NILF
, _("no more file handles: could not duplicate stdout\n"));
1895 /* Close unnecessary file handles for the child. */
1896 if (save_stdin
!= 0)
1897 CLOSE_ON_EXEC (save_stdin
);
1898 if (save_stdout
!= 1)
1899 CLOSE_ON_EXEC (save_stdout
);
1901 /* Connect the pipes to the child process. */
1903 (void) dup2 (stdin_fd
, 0);
1905 (void) dup2 (stdout_fd
, 1);
1907 /* stdin_fd and stdout_fd must be closed on exit because we are
1908 still in the parent process */
1910 CLOSE_ON_EXEC (stdin_fd
);
1912 CLOSE_ON_EXEC (stdout_fd
);
1914 /* Run the command. */
1915 pid
= exec_command (argv
, envp
);
1917 /* Restore stdout/stdin of the parent and close temporary FDs. */
1920 if (dup2 (save_stdin
, 0) != 0)
1921 fatal (NILF
, _("Could not restore stdin\n"));
1928 if (dup2 (save_stdout
, 1) != 1)
1929 fatal (NILF
, _("Could not restore stdout\n"));
1931 close (save_stdout
);
1937 #elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS)
1940 Replace the current process with one executing the command in ARGV.
1941 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
1942 the environment of the new program. This function does not return. */
1944 child_execute_job (int stdin_fd
, int stdout_fd
, char **argv
, char **envp
)
1947 (void) dup2 (stdin_fd
, 0);
1949 (void) dup2 (stdout_fd
, 1);
1951 (void) close (stdin_fd
);
1953 (void) close (stdout_fd
);
1955 /* Run the command. */
1956 exec_command (argv
, envp
);
1958 #endif /* !AMIGA && !__MSDOS__ && !VMS */
1959 #endif /* !WINDOWS32 */
1962 /* Replace the current process with one running the command in ARGV,
1963 with environment ENVP. This function does not return. */
1965 /* EMX: This function returns the pid of the child process. */
1971 exec_command (char **argv
, char **envp
)
1974 /* to work around a problem with signals and execve: ignore them */
1976 signal (SIGCHLD
,SIG_IGN
);
1978 /* Run the program. */
1979 execve (argv
[0], argv
, envp
);
1980 perror_with_name ("execve: ", argv
[0]);
1981 _exit (EXIT_FAILURE
);
1987 int exit_code
= EXIT_FAILURE
;
1989 /* make sure CreateProcess() has Path it needs */
1990 sync_Path_environment();
1992 /* launch command */
1993 hPID
= process_easy(argv
, envp
);
1995 /* make sure launch ok */
1996 if (hPID
== INVALID_HANDLE_VALUE
)
2000 _("process_easy() failed failed to launch process (e=%ld)\n"),
2001 process_last_err(hPID
));
2002 for (i
= 0; argv
[i
]; i
++)
2003 fprintf(stderr
, "%s ", argv
[i
]);
2004 fprintf(stderr
, _("\nCounted %d args in failed launch\n"), i
);
2008 /* wait and reap last child */
2009 hWaitPID
= process_wait_for_any();
2012 /* was an error found on this process? */
2013 err
= process_last_err(hWaitPID
);
2016 exit_code
= process_exit_code(hWaitPID
);
2019 fprintf(stderr
, "make (e=%d, rc=%d): %s",
2020 err
, exit_code
, map_windows32_error_to_string(err
));
2022 /* cleanup process */
2023 process_cleanup(hWaitPID
);
2025 /* expect to find only last pid, warn about other pids reaped */
2026 if (hWaitPID
== hPID
)
2030 _("make reaped child pid %d, still waiting for pid %d\n"),
2034 /* return child's exit code as our exit code */
2037 #else /* !WINDOWS32 */
2043 /* Be the user, permanently. */
2048 /* Run the program. */
2049 pid
= spawnvpe (P_NOWAIT
, argv
[0], argv
, envp
);
2054 /* the file might have a strange shell extension */
2055 if (errno
== ENOENT
)
2060 /* Run the program. */
2062 execvp (argv
[0], argv
);
2064 # endif /* !__EMX__ */
2069 error (NILF
, _("%s: Command not found"), argv
[0]);
2073 /* The file is not executable. Try it as a shell script. */
2074 extern char *getenv ();
2081 /* Do not use $SHELL from the environment */
2082 struct variable
*p
= lookup_variable ("SHELL", 5);
2088 shell
= getenv ("SHELL");
2091 shell
= default_shell
;
2094 while (argv
[argc
] != 0)
2102 new_argv
= (char **) alloca ((1 + argc
+ 1) * sizeof (char *));
2103 new_argv
[0] = shell
;
2114 new_argv
[i
] = argv
[0];
2117 new_argv
[i
+ argc
] = argv
[argc
];
2122 pid
= spawnvpe (P_NOWAIT
, shell
, new_argv
, envp
);
2126 execvp (shell
, new_argv
);
2128 if (errno
== ENOENT
)
2129 error (NILF
, _("%s: Shell program not found"), shell
);
2131 perror_with_name ("execvp: ", shell
);
2137 /* this nasty error was driving me nuts :-( */
2138 error (NILF
, _("spawnvpe: environment space might be exhausted"));
2143 perror_with_name ("execvp: ", argv
[0]);
2152 #endif /* !WINDOWS32 */
2155 #else /* On Amiga */
2156 void exec_command (char **argv
)
2161 void clean_tmp (void)
2163 DeleteFile (amiga_bname
);
2166 #endif /* On Amiga */
2169 /* Figure out the argument list necessary to run LINE as a command. Try to
2170 avoid using a shell. This routine handles only ' quoting, and " quoting
2171 when no backslash, $ or ` characters are seen in the quotes. Starting
2172 quotes may be escaped with a backslash. If any of the characters in
2173 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2174 is the first word of a line, the shell is used.
2176 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2177 If *RESTP is NULL, newlines will be ignored.
2179 SHELL is the shell to use, or nil to use the default shell.
2180 IFS is the value of $IFS, or nil (meaning the default). */
2183 construct_command_argv_internal (char *line
, char **restp
, char *shell
,
2184 char *ifs
, char **batch_filename_ptr
)
2187 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
2188 We call `system' for anything that requires ``slow'' processing,
2189 because DOS shells are too dumb. When $SHELL points to a real
2190 (unix-style) shell, `system' just calls it to do everything. When
2191 $SHELL points to a DOS shell, `system' does most of the work
2192 internally, calling the shell only for its internal commands.
2193 However, it looks on the $PATH first, so you can e.g. have an
2194 external command named `mkdir'.
2196 Since we call `system', certain characters and commands below are
2197 actually not specific to COMMAND.COM, but to the DJGPP implementation
2198 of `system'. In particular:
2200 The shell wildcard characters are in DOS_CHARS because they will
2201 not be expanded if we call the child via `spawnXX'.
2203 The `;' is in DOS_CHARS, because our `system' knows how to run
2204 multiple commands on a single line.
2206 DOS_CHARS also include characters special to 4DOS/NDOS, so we
2207 won't have to tell one from another and have one more set of
2208 commands and special characters. */
2209 static char sh_chars_dos
[] = "*?[];|<>%^&()";
2210 static char *sh_cmds_dos
[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2211 "copy", "ctty", "date", "del", "dir", "echo",
2212 "erase", "exit", "for", "goto", "if", "md",
2213 "mkdir", "path", "pause", "prompt", "rd",
2214 "rmdir", "rem", "ren", "rename", "set",
2215 "shift", "time", "type", "ver", "verify",
2218 static char sh_chars_sh
[] = "#;\"*?[]&|<>(){}$`^";
2219 static char *sh_cmds_sh
[] = { "cd", "echo", "eval", "exec", "exit", "login",
2220 "logout", "set", "umask", "wait", "while",
2221 "for", "case", "if", ":", ".", "break",
2222 "continue", "export", "read", "readonly",
2223 "shift", "times", "trap", "switch", "unset",
2228 #elif defined (__EMX__)
2229 static char sh_chars_dos
[] = "*?[];|<>%^&()";
2230 static char *sh_cmds_dos
[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2231 "copy", "ctty", "date", "del", "dir", "echo",
2232 "erase", "exit", "for", "goto", "if", "md",
2233 "mkdir", "path", "pause", "prompt", "rd",
2234 "rmdir", "rem", "ren", "rename", "set",
2235 "shift", "time", "type", "ver", "verify",
2238 static char sh_chars_os2
[] = "*?[];|<>%^()\"'&";
2239 static char *sh_cmds_os2
[] = { "call", "cd", "chcp", "chdir", "cls", "copy",
2240 "date", "del", "detach", "dir", "echo",
2241 "endlocal", "erase", "exit", "for", "goto", "if",
2242 "keys", "md", "mkdir", "move", "path", "pause",
2243 "prompt", "rd", "rem", "ren", "rename", "rmdir",
2244 "set", "setlocal", "shift", "start", "time",
2245 "type", "ver", "verify", "vol", ":", 0 };
2247 static char sh_chars_sh
[] = "#;\"*?[]&|<>(){}$`^~'";
2248 static char *sh_cmds_sh
[] = { "echo", "cd", "eval", "exec", "exit", "login",
2249 "logout", "set", "umask", "wait", "while",
2250 "for", "case", "if", ":", ".", "break",
2251 "continue", "export", "read", "readonly",
2252 "shift", "times", "trap", "switch", "unset",
2257 #elif defined (_AMIGA)
2258 static char sh_chars
[] = "#;\"|<>()?*$`";
2259 static char *sh_cmds
[] = { "cd", "eval", "if", "delete", "echo", "copy",
2260 "rename", "set", "setenv", "date", "makedir",
2261 "skip", "else", "endif", "path", "prompt",
2262 "unset", "unsetenv", "version",
2264 #elif defined (WINDOWS32)
2265 static char sh_chars_dos
[] = "\"|&<>";
2266 static char *sh_cmds_dos
[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2267 "copy", "ctty", "date", "del", "dir", "echo",
2268 "erase", "exit", "for", "goto", "if", "if", "md",
2269 "mkdir", "path", "pause", "prompt", "rd", "rem",
2270 "ren", "rename", "rmdir", "set", "shift", "time",
2271 "type", "ver", "verify", "vol", ":", 0 };
2272 static char sh_chars_sh
[] = "#;\"*?[]&|<>(){}$`^";
2273 static char *sh_cmds_sh
[] = { "cd", "eval", "exec", "exit", "login",
2274 "logout", "set", "umask", "wait", "while", "for",
2275 "case", "if", ":", ".", "break", "continue",
2276 "export", "read", "readonly", "shift", "times",
2277 "trap", "switch", "test",
2278 #ifdef BATCH_MODE_ONLY_SHELL
2284 #elif defined(__riscos__)
2285 static char sh_chars
[] = "";
2286 static char *sh_cmds
[] = { 0 };
2287 #else /* must be UNIX-ish */
2288 static char sh_chars
[] = "#;\"*?[]&|<>(){}$`^~!";
2289 static char *sh_cmds
[] = { ".", ":", "break", "case", "cd", "continue",
2290 "eval", "exec", "exit", "export", "for", "if",
2291 "login", "logout", "read", "readonly", "set",
2292 "shift", "switch", "test", "times", "trap",
2293 "umask", "wait", "while", 0 };
2299 int instring
, word_has_equals
, seen_nonequals
, last_argument_was_empty
;
2300 char **new_argv
= 0;
2306 sh_cmds
= sh_cmds_dos
;
2307 sh_chars
= sh_chars_dos
;
2309 sh_cmds
= sh_cmds_sh
;
2310 sh_chars
= sh_chars_sh
;
2312 #endif /* WINDOWS32 */
2317 /* Make sure not to bother processing an empty line. */
2318 while (isblank ((unsigned char)*line
))
2323 /* See if it is safe to parse commands internally. */
2325 shell
= default_shell
;
2327 else if (strcmp (shell
, default_shell
))
2329 char *s1
= _fullpath(NULL
, shell
, 0);
2330 char *s2
= _fullpath(NULL
, default_shell
, 0);
2332 slow_flag
= strcmp((s1
? s1
: ""), (s2
? s2
: ""));
2341 #else /* not WINDOWS32 */
2342 #if defined (__MSDOS__) || defined (__EMX__)
2343 else if (stricmp (shell
, default_shell
))
2345 extern int _is_unixy_shell (const char *_path
);
2347 DB (DB_BASIC
, (_("$SHELL changed (was `%s', now `%s')\n"),
2348 default_shell
, shell
));
2349 unixy_shell
= _is_unixy_shell (shell
);
2350 /* we must allocate a copy of shell: construct_command_argv() will free
2351 * shell after this function returns. */
2352 default_shell
= xstrdup (shell
);
2356 sh_chars
= sh_chars_sh
;
2357 sh_cmds
= sh_cmds_sh
;
2361 sh_chars
= sh_chars_dos
;
2362 sh_cmds
= sh_cmds_dos
;
2364 if (_osmode
== OS2_MODE
)
2366 sh_chars
= sh_chars_os2
;
2367 sh_cmds
= sh_cmds_os2
;
2371 #else /* !__MSDOS__ */
2372 else if (strcmp (shell
, default_shell
))
2374 #endif /* !__MSDOS__ && !__EMX__ */
2375 #endif /* not WINDOWS32 */
2378 for (ap
= ifs
; *ap
!= '\0'; ++ap
)
2379 if (*ap
!= ' ' && *ap
!= '\t' && *ap
!= '\n')
2382 i
= strlen (line
) + 1;
2384 /* More than 1 arg per character is impossible. */
2385 new_argv
= (char **) xmalloc (i
* sizeof (char *));
2387 /* All the args can fit in a buffer as big as LINE is. */
2388 ap
= new_argv
[0] = argstr
= (char *) xmalloc (i
);
2391 /* I is how many complete arguments have been found. */
2393 instring
= word_has_equals
= seen_nonequals
= last_argument_was_empty
= 0;
2394 for (p
= line
; *p
!= '\0'; ++p
)
2400 /* Inside a string, just copy any char except a closing quote
2401 or a backslash-newline combination. */
2405 if (ap
== new_argv
[0] || *(ap
-1) == '\0')
2406 last_argument_was_empty
= 1;
2408 else if (*p
== '\\' && p
[1] == '\n')
2410 /* Backslash-newline is handled differently depending on what
2411 kind of string we're in: inside single-quoted strings you
2412 keep them; in double-quoted strings they disappear.
2413 For DOS/Windows/OS2, if we don't have a POSIX shell,
2414 we keep the pre-POSIX behavior of removing the
2415 backslash-newline. */
2417 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2427 /* If there's a TAB here, skip it. */
2431 else if (*p
== '\n' && restp
!= NULL
)
2433 /* End of the command line. */
2437 /* Backslash, $, and ` are special inside double quotes.
2438 If we see any of those, punt.
2439 But on MSDOS, if we use COMMAND.COM, double and single
2440 quotes have the same effect. */
2441 else if (instring
== '"' && strchr ("\\$`", *p
) != 0 && unixy_shell
)
2446 else if (strchr (sh_chars
, *p
) != 0)
2447 /* Not inside a string, but it's a special char. */
2450 else if (*p
== '.' && p
[1] == '.' && p
[2] == '.' && p
[3] != '.')
2451 /* `...' is a wildcard in DJGPP. */
2455 /* Not a special char. */
2459 /* Equals is a special character in leading words before the
2460 first word with no equals sign in it. This is not the case
2461 with sh -k, but we never get here when using nonstandard
2463 if (! seen_nonequals
&& unixy_shell
)
2465 word_has_equals
= 1;
2470 /* Backslash-newline has special case handling, ref POSIX.
2471 We're in the fastpath, so emulate what the shell would do. */
2474 /* Throw out the backslash and newline. */
2477 /* If there is a tab after a backslash-newline, remove it. */
2481 /* If there's nothing in this argument yet, skip any
2482 whitespace before the start of the next word. */
2483 if (ap
== new_argv
[i
])
2484 p
= next_token (p
+ 1) - 1;
2486 else if (p
[1] != '\0')
2488 #ifdef HAVE_DOS_PATHS
2489 /* Only remove backslashes before characters special to Unixy
2490 shells. All other backslashes are copied verbatim, since
2491 they are probably DOS-style directory separators. This
2492 still leaves a small window for problems, but at least it
2493 should work for the vast majority of naive users. */
2496 /* A dot is only special as part of the "..."
2498 if (strneq (p
+ 1, ".\\.\\.", 5))
2506 if (p
[1] != '\\' && p
[1] != '\''
2507 && !isspace ((unsigned char)p
[1])
2508 && strchr (sh_chars_sh
, p
[1]) == 0)
2509 /* back up one notch, to copy the backslash */
2511 #endif /* HAVE_DOS_PATHS */
2513 /* Copy and skip the following char. */
2526 /* End of the command line. */
2531 /* Newlines are not special. */
2537 /* We have the end of an argument.
2538 Terminate the text of the argument. */
2541 last_argument_was_empty
= 0;
2543 /* Update SEEN_NONEQUALS, which tells us if every word
2544 heretofore has contained an `='. */
2545 seen_nonequals
|= ! word_has_equals
;
2546 if (word_has_equals
&& ! seen_nonequals
)
2547 /* An `=' in a word before the first
2548 word without one is magical. */
2550 word_has_equals
= 0; /* Prepare for the next word. */
2552 /* If this argument is the command name,
2553 see if it is a built-in shell command.
2554 If so, have the shell handle it. */
2558 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
2560 if (streq (sh_cmds
[j
], new_argv
[0]))
2563 /* Non-Unix shells are case insensitive. */
2565 && strcasecmp (sh_cmds
[j
], new_argv
[0]) == 0)
2571 /* Ignore multiple whitespace chars. */
2572 p
= next_token (p
) - 1;
2583 /* Let the shell deal with an unterminated quote. */
2586 /* Terminate the last argument and the argument list. */
2589 if (new_argv
[i
][0] != '\0' || last_argument_was_empty
)
2596 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
2597 if (streq (sh_cmds
[j
], new_argv
[0]))
2601 if (new_argv
[0] == 0)
2603 /* Line was empty. */
2605 free ((char *)new_argv
);
2612 /* We must use the shell. */
2616 /* Free the old argument list we were working on. */
2618 free ((char *)new_argv
);
2622 execute_by_shell
= 1; /* actually, call `system' if shell isn't unixy */
2631 buffer
= (char *)xmalloc (strlen (line
)+1);
2634 for (dptr
=buffer
; *ptr
; )
2636 if (*ptr
== '\\' && ptr
[1] == '\n')
2638 else if (*ptr
== '@') /* Kludge: multiline commands */
2648 new_argv
= (char **) xmalloc (2 * sizeof (char *));
2649 new_argv
[0] = buffer
;
2652 #else /* Not Amiga */
2655 * Not eating this whitespace caused things like
2659 * which gave the shell fits. I think we have to eat
2660 * whitespace here, but this code should be considered
2661 * suspicious if things start failing....
2664 /* Make sure not to bother processing an empty line. */
2665 while (isspace ((unsigned char)*line
))
2669 #endif /* WINDOWS32 */
2671 /* SHELL may be a multi-word command. Construct a command line
2672 "SHELL -c LINE", with all special chars in LINE escaped.
2673 Then recurse, expanding this command line to get the final
2676 unsigned int shell_len
= strlen (shell
);
2678 static char minus_c
[] = " -c ";
2680 static char minus_c
[] = "";
2682 unsigned int line_len
= strlen (line
);
2684 char *new_line
= (char *) alloca (shell_len
+ (sizeof (minus_c
) - 1)
2685 + (line_len
* 2) + 1);
2686 char *command_ptr
= NULL
; /* used for batch_mode_shell mode */
2688 # ifdef __EMX__ /* is this necessary? */
2690 minus_c
[1] = '/'; /* " /c " */
2694 bcopy (shell
, ap
, shell_len
);
2696 bcopy (minus_c
, ap
, sizeof (minus_c
) - 1);
2697 ap
+= sizeof (minus_c
) - 1;
2699 for (p
= line
; *p
!= '\0'; ++p
)
2701 if (restp
!= NULL
&& *p
== '\n')
2706 else if (*p
== '\\' && p
[1] == '\n')
2708 /* POSIX says we keep the backslash-newline, but throw out
2709 the next char if it's a TAB. If we don't have a POSIX
2710 shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
2711 and remove the backslash/newline. */
2712 #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
2713 # define PRESERVE_BSNL unixy_shell
2715 # define PRESERVE_BSNL 1
2731 /* DOS shells don't know about backslash-escaping. */
2732 if (unixy_shell
&& !batch_mode_shell
&&
2733 (*p
== '\\' || *p
== '\'' || *p
== '"'
2734 || isspace ((unsigned char)*p
)
2735 || strchr (sh_chars
, *p
) != 0))
2738 else if (unixy_shell
&& strneq (p
, "...", 3))
2740 /* The case of `...' wildcard again. */
2741 strcpy (ap
, "\\.\\.\\");
2748 if (ap
== new_line
+ shell_len
+ sizeof (minus_c
) - 1)
2749 /* Line was empty. */
2754 /* Some shells do not work well when invoked as 'sh -c xxx' to run a
2755 command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
2756 cases, run commands via a script file. */
2757 if ((no_default_sh_exe
|| batch_mode_shell
) && batch_filename_ptr
) {
2759 int id
= GetCurrentProcessId();
2762 /* create a file name */
2763 sprintf(fbuf
, "make%d", id
);
2764 *batch_filename_ptr
= create_batch_filename (fbuf
, unixy_shell
);
2766 DB (DB_JOBS
, (_("Creating temporary batch file %s\n"),
2767 *batch_filename_ptr
));
2769 /* create batch file to execute command */
2770 batch
= fopen (*batch_filename_ptr
, "w");
2772 fputs ("@echo off\n", batch
);
2773 fputs (command_ptr
, batch
);
2774 fputc ('\n', batch
);
2778 new_argv
= (char **) xmalloc(3 * sizeof (char *));
2780 new_argv
[0] = xstrdup (shell
);
2781 new_argv
[1] = *batch_filename_ptr
; /* only argv[0] gets freed later */
2783 new_argv
[0] = xstrdup (*batch_filename_ptr
);
2788 #endif /* WINDOWS32 */
2790 new_argv
= construct_command_argv_internal (new_line
, (char **) NULL
,
2791 (char *) 0, (char *) 0,
2794 else if (!unixy_shell
)
2796 /* new_line is local, must not be freed therefore
2797 We use line here instead of new_line because we run the shell
2799 size_t line_len
= strlen (line
);
2802 memcpy (new_line
, line
, line_len
+ 1);
2803 /* replace all backslash-newline combination and also following tabs */
2806 if (q
[0] == '\\' && q
[1] == '\n')
2808 q
+= 2; /* remove '\\' and '\n' */
2810 q
++; /* remove 1st tab in the next line */
2817 # ifndef NO_CMD_DEFAULT
2818 if (strnicmp (new_line
, "echo", 4) == 0
2819 && (new_line
[4] == ' ' || new_line
[4] == '\t'))
2821 /* the builtin echo command: handle it separately */
2822 size_t echo_len
= line_len
- 5;
2823 char *echo_line
= new_line
+ 5;
2825 /* special case: echo 'x="y"'
2826 cmd works this way: a string is printed as is, i.e., no quotes
2827 are removed. But autoconf uses a command like echo 'x="y"' to
2828 determine whether make works. autoconf expects the output x="y"
2829 so we will do exactly that.
2830 Note: if we do not allow cmd to be the default shell
2831 we do not need this kind of voodoo */
2832 if (echo_line
[0] == '\''
2833 && echo_line
[echo_len
- 1] == '\''
2834 && strncmp (echo_line
+ 1, "ac_maketemp=",
2835 strlen ("ac_maketemp=")) == 0)
2837 /* remove the enclosing quotes */
2838 memmove (echo_line
, echo_line
+ 1, echo_len
- 2);
2839 echo_line
[echo_len
- 2] = '\0';
2845 /* Let the shell decide what to do. Put the command line into the
2846 2nd command line argument and hope for the best ;-) */
2847 size_t sh_len
= strlen (shell
);
2849 /* exactly 3 arguments + NULL */
2850 new_argv
= (char **) xmalloc (4 * sizeof (char *));
2851 /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
2852 the trailing '\0' */
2853 new_argv
[0] = (char *) malloc (sh_len
+ line_len
+ 5);
2854 memcpy (new_argv
[0], shell
, sh_len
+ 1);
2855 new_argv
[1] = new_argv
[0] + sh_len
+ 1;
2856 memcpy (new_argv
[1], "/c", 3);
2857 new_argv
[2] = new_argv
[1] + 3;
2858 memcpy (new_argv
[2], new_line
, line_len
+ 1);
2862 #elif defined(__MSDOS__)
2865 /* With MSDOS shells, we must construct the command line here
2866 instead of recursively calling ourselves, because we
2867 cannot backslash-escape the special characters (see above). */
2868 new_argv
= (char **) xmalloc (sizeof (char *));
2869 line_len
= strlen (new_line
) - shell_len
- sizeof (minus_c
) + 1;
2870 new_argv
[0] = xmalloc (line_len
+ 1);
2871 strncpy (new_argv
[0],
2872 new_line
+ shell_len
+ sizeof (minus_c
) - 1, line_len
);
2873 new_argv
[0][line_len
] = '\0';
2877 fatal (NILF
, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
2878 __FILE__
, __LINE__
);
2881 #endif /* ! AMIGA */
2887 /* Figure out the argument list necessary to run LINE as a command. Try to
2888 avoid using a shell. This routine handles only ' quoting, and " quoting
2889 when no backslash, $ or ` characters are seen in the quotes. Starting
2890 quotes may be escaped with a backslash. If any of the characters in
2891 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2892 is the first word of a line, the shell is used.
2894 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2895 If *RESTP is NULL, newlines will be ignored.
2897 FILE is the target whose commands these are. It is used for
2898 variable expansion for $(SHELL) and $(IFS). */
2901 construct_command_argv (char *line
, char **restp
, struct file
*file
,
2902 char **batch_filename_ptr
)
2916 && (isspace ((unsigned char)*cptr
)))
2921 && (!isspace((unsigned char)*cptr
)))
2926 argv
= (char **)malloc (argc
* sizeof (char *));
2935 && (isspace ((unsigned char)*cptr
)))
2939 DB (DB_JOBS
, ("argv[%d] = [%s]\n", argc
, cptr
));
2940 argv
[argc
++] = cptr
;
2942 && (!isspace((unsigned char)*cptr
)))
2949 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
2950 int save
= warn_undefined_variables_flag
;
2951 warn_undefined_variables_flag
= 0;
2953 shell
= allocated_variable_expand_for_file ("$(SHELL)", file
);
2956 * Convert to forward slashes so that construct_command_argv_internal()
2960 char *p
= w32ify (shell
, 0);
2966 static const char *unixroot
= NULL
;
2967 static const char *last_shell
= "";
2968 static int init
= 0;
2971 unixroot
= getenv ("UNIXROOT");
2972 /* unixroot must be NULL or not empty */
2973 if (unixroot
&& unixroot
[0] == '\0') unixroot
= NULL
;
2977 /* if we have an unixroot drive and if shell is not default_shell
2978 (which means it's either cmd.exe or the test has already been
2979 performed) and if shell is an absolute path without drive letter,
2980 try whether it exists e.g.: if "/bin/sh" does not exist use
2981 "$UNIXROOT/bin/sh" instead. */
2982 if (unixroot
&& shell
&& strcmp (shell
, last_shell
) != 0
2983 && (shell
[0] == '/' || shell
[0] == '\\'))
2985 /* trying a new shell, check whether it exists */
2986 size_t size
= strlen (shell
);
2987 char *buf
= xmalloc (size
+ 7);
2988 memcpy (buf
, shell
, size
);
2989 memcpy (buf
+ size
, ".exe", 5); /* including the trailing '\0' */
2990 if (access (shell
, F_OK
) != 0 && access (buf
, F_OK
) != 0)
2992 /* try the same for the unixroot drive */
2993 memmove (buf
+ 2, buf
, size
+ 5);
2994 buf
[0] = unixroot
[0];
2995 buf
[1] = unixroot
[1];
2996 if (access (buf
, F_OK
) == 0)
2997 /* we have found a shell! */
3007 #endif /* __EMX__ */
3009 ifs
= allocated_variable_expand_for_file ("$(IFS)", file
);
3011 warn_undefined_variables_flag
= save
;
3014 argv
= construct_command_argv_internal (line
, restp
, shell
, ifs
, batch_filename_ptr
);
3022 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
3024 dup2 (int old
, int new)
3039 #endif /* !HAPE_DUP2 && !_AMIGA */
3041 /* On VMS systems, include special VMS functions. */
3044 #include "vmsjobs.c"