1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97,99 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
33 /* Default shell to use. */
36 char *default_shell
= "sh.exe";
37 int no_default_sh_exe
= 1;
38 int batch_mode_shell
= 1;
40 #elif defined (_AMIGA)
42 char default_shell
[] = "";
43 extern int MyExecute (char **);
44 int batch_mode_shell
= 0;
46 #elif defined (__MSDOS__)
48 /* The default shell is a pointer so we can change it if Makefile
49 says so. It is without an explicit path so we get a chance
50 to search the $PATH for it (since MSDOS doesn't have standard
51 directories we could trust). */
52 char *default_shell
= "command.com";
53 int batch_mode_shell
= 0;
55 #elif defined (__EMX__)
57 char *default_shell
= "/bin/sh";
58 int batch_mode_shell
= 0;
63 char default_shell
[] = "";
64 int batch_mode_shell
= 0;
66 #elif defined (__riscos__)
68 char default_shell
[] = "";
69 int batch_mode_shell
= 0;
73 char default_shell
[] = "/bin/sh";
74 int batch_mode_shell
= 0;
80 static int execute_by_shell
;
81 static int dos_pid
= 123;
83 int dos_command_running
;
84 #endif /* __MSDOS__ */
87 # include <proto/dos.h>
88 static int amiga_pid
= 123;
89 static int amiga_status
;
90 static char amiga_bname
[32];
91 static int amiga_batch_file
;
96 # include <processes.h>
99 # include <lib$routines.h>
103 # include <windows.h>
105 # include <process.h>
106 # include "sub_proc.h"
108 # include "pathstuff.h"
109 #endif /* WINDOWS32 */
112 # include <process.h>
115 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
116 # include <sys/wait.h>
120 # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
121 #else /* Don't have waitpid. */
126 # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
127 # endif /* Have wait3. */
128 #endif /* Have waitpid. */
130 #if !defined (wait) && !defined (POSIX)
134 #ifndef HAVE_UNION_WAIT
139 # define WTERMSIG(x) ((x) & 0x7f)
142 # define WCOREDUMP(x) ((x) & 0x80)
145 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
148 # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
151 # define WIFEXITED(x) (WTERMSIG (x) == 0)
154 #else /* Have `union wait'. */
156 # define WAIT_T union wait
158 # define WTERMSIG(x) ((x).w_termsig)
161 # define WCOREDUMP(x) ((x).w_coredump)
164 # define WEXITSTATUS(x) ((x).w_retcode)
167 # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
170 # define WIFEXITED(x) (WTERMSIG(x) == 0)
173 #endif /* Don't have `union wait'. */
176 static int vms_jobsefnmask
= 0;
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 static void vmsWaitForChildren
PARAMS ((int *));
210 /* Chain of all live (or recently deceased) children. */
212 struct child
*children
= 0;
214 /* Number of children currently running. */
216 unsigned int job_slots_used
= 0;
218 /* Nonzero if the `good' standard input is in use. */
220 static int good_stdin_used
= 0;
222 /* Chain of children waiting to run until the load average goes down. */
224 static struct child
*waiting_jobs
= 0;
226 /* Non-zero if we use a *real* shell (always so on Unix). */
230 /* Number of jobs started in the current second. */
232 unsigned long job_counter
= 0;
237 * The macro which references this function is defined in make.h.
240 w32_kill(int pid
, int sig
)
242 return ((process_kill(pid
, sig
) == TRUE
) ? 0 : -1);
244 #endif /* WINDOWS32 */
247 /* returns whether path is assumed to be a unix like shell. */
249 _is_unixy_shell (const char *path
)
251 /* list of non unix shells */
252 const char *known_os2shells
[] = {
264 /* find the rightmost '/' or '\\' */
265 const char *name
= strrchr (path
, '/');
266 const char *p
= strrchr (path
, '\\');
269 if (name
&& p
) /* take the max */
270 name
= (name
> p
) ? name
: p
;
271 else if (p
) /* name must be 0 */
273 else if (!name
) /* name and p must be 0 */
276 if (*name
== '/' || *name
== '\\') name
++;
279 while (known_os2shells
[i
] != NULL
) {
280 if (stricmp (name
, known_os2shells
[i
]) == 0) /* strcasecmp() */
281 return 0; /* not a unix shell */
285 /* in doubt assume a unix like shell */
291 /* Write an error message describing the exit status given in
292 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
293 Append "(ignored)" if IGNORED is nonzero. */
296 child_error (char *target_name
, int exit_code
, int exit_sig
, int coredump
,
299 if (ignored
&& silent_flag
)
303 if (!(exit_code
& 1))
305 (ignored
? _("*** [%s] Error 0x%x (ignored)")
306 : _("*** [%s] Error 0x%x")),
307 target_name
, exit_code
);
310 error (NILF
, ignored
? _("[%s] Error %d (ignored)") :
311 _("*** [%s] Error %d"),
312 target_name
, exit_code
);
314 error (NILF
, "*** [%s] %s%s",
315 target_name
, strsignal (exit_sig
),
316 coredump
? _(" (core dumped)") : "");
321 /* Wait for nchildren children to terminate */
323 vmsWaitForChildren(int *status
)
327 if (!vms_jobsefnmask
)
333 *status
= sys$
wflor (32, vms_jobsefnmask
);
338 /* Set up IO redirection. */
341 vms_redirect (struct dsc$descriptor_s
*desc
, char *fname
, char *ibuf
)
344 extern char *vmsify ();
347 while (isspace ((unsigned char)*ibuf
))
350 while (*ibuf
&& !isspace ((unsigned char)*ibuf
))
353 if (strcmp (fptr
, "/dev/null") != 0)
355 strcpy (fname
, vmsify (fptr
, 0));
356 if (strchr (fname
, '.') == 0)
359 desc
->dsc$w_length
= strlen(fname
);
360 desc
->dsc$a_pointer
= fname
;
361 desc
->dsc$b_dtype
= DSC$K_DTYPE_T
;
362 desc
->dsc$b_class
= DSC$K_CLASS_S
;
365 printf (_("Warning: Empty redirection\n"));
370 /* found apostrophe at (p-1)
371 inc p until after closing apostrophe.
375 vms_handle_apos (char *p
)
379 #define SEPCHARS ",/()= "
395 if (strchr (SEPCHARS
, *p
))
410 /* Handle a dead child. This handler may or may not ever be installed.
412 If we're using the jobserver feature, we need it. First, installing it
413 ensures the read will interrupt on SIGCHLD. Second, we close the dup'd
414 read FD to ensure we don't enter another blocking read without reaping all
415 the dead children. In this case we don't need the dead_children count.
417 If we don't have either waitpid or wait3, then make is unreliable, but we
418 use the dead_children count to reap children as best we can. */
420 static unsigned int dead_children
= 0;
423 child_handler (int sig UNUSED
)
434 /* The signal handler must called only once! */
435 signal (SIGCHLD
, SIG_DFL
);
438 /* This causes problems if the SIGCHLD interrupts a printf().
439 DB (DB_JOBS, (_("Got a SIGCHLD; %u unreaped children.\n"), dead_children));
443 extern int shell_function_pid
, shell_function_completed
;
445 /* Reap all dead children, storing the returned status and the new command
446 state (`cs_finished') in the `file' member of the `struct child' for the
447 dead child, and removing the child from the chain. In addition, if BLOCK
448 nonzero, we block in this function until we've reaped at least one
449 complete child, waiting for it to die if necessary. If ERR is nonzero,
450 print an error message first. */
453 reap_children (int block
, int err
)
456 /* Initially, assume we have some. */
460 # define REAP_MORE reap_more
462 # define REAP_MORE dead_children
467 We have at least one child outstanding OR a shell function in progress,
469 We're blocking for a complete child OR there are more children to reap
471 we'll keep reaping children. */
473 while ((children
!= 0 || shell_function_pid
!= 0)
474 && (block
|| REAP_MORE
))
478 int exit_code
, exit_sig
, coredump
;
479 register struct child
*lastc
, *c
;
481 int any_remote
, any_local
;
485 /* We might block for a while, so let the user know why. */
487 error (NILF
, _("*** Waiting for unfinished jobs...."));
490 /* We have one less dead child to reap. As noted in
491 child_handler() above, this count is completely unimportant for
492 all modern, POSIX-y systems that support wait3() or waitpid().
493 The rest of this comment below applies only to early, broken
494 pre-POSIX systems. We keep the count only because... it's there...
496 The test and decrement are not atomic; if it is compiled into:
497 register = dead_children - 1;
498 dead_children = register;
499 a SIGCHLD could come between the two instructions.
500 child_handler increments dead_children.
501 The second instruction here would lose that increment. But the
502 only effect of dead_children being wrong is that we might wait
503 longer than necessary to reap a child, and lose some parallelism;
504 and we might print the "Waiting for unfinished jobs" message above
505 when not necessary. */
507 if (dead_children
> 0)
511 any_local
= shell_function_pid
!= 0;
512 for (c
= children
; c
!= 0; c
= c
->next
)
514 any_remote
|= c
->remote
;
515 any_local
|= ! c
->remote
;
516 DB (DB_JOBS
, (_("Live child 0x%08lx (%s) PID %ld %s\n"),
517 (unsigned long int) c
, c
->file
->name
,
518 (long) c
->pid
, c
->remote
? _(" (remote)") : ""));
524 /* First, check for remote children. */
526 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 0);
531 /* We got a remote child. */
535 /* A remote status command failed miserably. Punt. */
537 pfatal_with_name ("remote_status");
541 /* No remote children. Check for local children. */
542 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
546 vmsWaitForChildren (&status
);
551 pid
= WAIT_NOHANG (&status
);
554 pid
= wait (&status
);
562 /* The wait*() failed miserably. Punt. */
563 pfatal_with_name ("wait");
567 /* We got a child exit; chop the status word up. */
568 exit_code
= WEXITSTATUS (status
);
569 exit_sig
= WIFSIGNALED (status
) ? WTERMSIG (status
) : 0;
570 coredump
= WCOREDUMP (status
);
572 /* If we have started jobs in this second, remove one. */
578 /* No local children are dead. */
581 if (!block
|| !any_remote
)
584 /* Now try a blocking wait for a remote child. */
585 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 1);
587 goto remote_status_lose
;
589 /* No remote children either. Finally give up. */
592 /* We got a remote child. */
595 #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
598 /* Life is very different on MSDOS. */
601 exit_code
= WEXITSTATUS (status
);
602 if (exit_code
== 0xff)
604 exit_sig
= WIFSIGNALED (status
) ? WTERMSIG (status
) : 0;
606 #endif /* __MSDOS__ */
610 status
= amiga_status
;
611 exit_code
= amiga_status
;
623 /* wait for anything to finish */
624 hPID
= process_wait_for_any();
628 /* was an error found on this process? */
629 err
= process_last_err(hPID
);
632 exit_code
= process_exit_code(hPID
);
635 fprintf(stderr
, "make (e=%d): %s",
636 exit_code
, map_windows32_error_to_string(exit_code
));
639 exit_sig
= process_signal(hPID
);
641 /* cleanup process */
642 process_cleanup(hPID
);
648 #endif /* WINDOWS32 */
651 /* Check if this is the child of the `shell' function. */
652 if (!remote
&& pid
== shell_function_pid
)
654 /* It is. Leave an indicator for the `shell' function. */
655 if (exit_sig
== 0 && exit_code
== 127)
656 shell_function_completed
= -1;
658 shell_function_completed
= 1;
662 child_failed
= exit_sig
!= 0 || exit_code
!= 0;
664 /* Search for a child matching the deceased one. */
666 for (c
= children
; c
!= 0; lastc
= c
, c
= c
->next
)
667 if (c
->remote
== remote
&& c
->pid
== pid
)
671 /* An unknown child died.
672 Ignore it; it was inherited from our invoker. */
675 DB (DB_JOBS
, (child_failed
676 ? _("Reaping losing child 0x%08lx PID %ld %s\n")
677 : _("Reaping winning child 0x%08lx PID %ld %s\n"),
678 (unsigned long int) c
, (long) c
->pid
,
679 c
->remote
? _(" (remote)") : ""));
681 if (c
->sh_batch_file
) {
682 DB (DB_JOBS
, (_("Cleaning up temp batch file %s\n"),
685 /* just try and remove, don't care if this fails */
686 remove (c
->sh_batch_file
);
688 /* all done with memory */
689 free (c
->sh_batch_file
);
690 c
->sh_batch_file
= NULL
;
693 /* If this child had the good stdin, say it is now free. */
697 if (child_failed
&& !c
->noerror
&& !ignore_errors_flag
)
699 /* The commands failed. Write an error message,
700 delete non-precious targets, and abort. */
701 static int delete_on_error
= -1;
702 child_error (c
->file
->name
, exit_code
, exit_sig
, coredump
, 0);
703 c
->file
->update_status
= 2;
704 if (delete_on_error
== -1)
706 struct file
*f
= lookup_file (".DELETE_ON_ERROR");
707 delete_on_error
= f
!= 0 && f
->is_target
;
709 if (exit_sig
!= 0 || delete_on_error
)
710 delete_child_targets (c
);
716 /* The commands failed, but we don't care. */
717 child_error (c
->file
->name
,
718 exit_code
, exit_sig
, coredump
, 1);
722 /* If there are more commands to run, try to start them. */
723 if (job_next_command (c
))
725 if (handling_fatal_signal
)
727 /* Never start new commands while we are dying.
728 Since there are more commands that wanted to be run,
729 the target was not completely remade. So we treat
730 this as if a command had failed. */
731 c
->file
->update_status
= 2;
735 /* Check again whether to start remotely.
736 Whether or not we want to changes over time.
737 Also, start_remote_job may need state set up
738 by start_remote_job_p. */
739 c
->remote
= start_remote_job_p (0);
740 start_job_command (c
);
741 /* Fatal signals are left blocked in case we were
742 about to put that child on the chain. But it is
743 already there, so it is safe for a fatal signal to
744 arrive now; it will clean up this child's targets. */
746 if (c
->file
->command_state
== cs_running
)
747 /* We successfully started the new command.
748 Loop to reap more children. */
752 if (c
->file
->update_status
!= 0)
753 /* We failed to start the commands. */
754 delete_child_targets (c
);
757 /* There are no more commands. We got through them all
758 without an unignored error. Now the target has been
759 successfully updated. */
760 c
->file
->update_status
= 0;
763 /* When we get here, all the commands for C->file are finished
764 (or aborted) and C->file->update_status contains 0 or 2. But
765 C->file->command_state is still cs_running if all the commands
766 ran; notice_finish_file looks for cs_running to tell it that
767 it's interesting to check the file's modtime again now. */
769 if (! handling_fatal_signal
)
770 /* Notice if the target of the commands has been changed.
771 This also propagates its values for command_state and
772 update_status to its also_make files. */
773 notice_finished_file (c
->file
);
775 DB (DB_JOBS
, (_("Removing child 0x%08lx PID %ld%s from chain.\n"),
776 (unsigned long int) c
, (long) c
->pid
,
777 c
->remote
? _(" (remote)") : ""));
779 /* Block fatal signals while frobnicating the list, so that
780 children and job_slots_used are always consistent. Otherwise
781 a fatal signal arriving after the child is off the chain and
782 before job_slots_used is decremented would believe a child was
783 live and call reap_children again. */
786 /* There is now another slot open. */
787 if (job_slots_used
> 0)
790 /* Remove the child from the chain and free it. */
794 lastc
->next
= c
->next
;
800 /* If the job failed, and the -k flag was not given, die,
801 unless we are already in the process of dying. */
802 if (!err
&& child_failed
&& !keep_going_flag
&&
803 /* fatal_error_signal will die with the right signal. */
804 !handling_fatal_signal
)
807 /* Only block for one child. */
814 /* Free the storage allocated for CHILD. */
817 free_child (struct child
*child
)
819 /* If this child is the only one it was our "free" job, so don't put a
820 token back for it. This child has already been removed from the list,
821 so if there any left this wasn't the last one. */
823 if (job_fds
[1] >= 0 && children
)
828 /* Write a job token back to the pipe. */
830 EINTRLOOP (r
, write (job_fds
[1], &token
, 1));
832 pfatal_with_name (_("write jobserver"));
834 DB (DB_JOBS
, (_("Released token for child 0x%08lx (%s).\n"),
835 (unsigned long int) child
, child
->file
->name
));
838 if (handling_fatal_signal
) /* Don't bother free'ing if about to die. */
841 if (child
->command_lines
!= 0)
843 register unsigned int i
;
844 for (i
= 0; i
< child
->file
->cmds
->ncommand_lines
; ++i
)
845 free (child
->command_lines
[i
]);
846 free ((char *) child
->command_lines
);
849 if (child
->environment
!= 0)
851 register char **ep
= child
->environment
;
854 free ((char *) child
->environment
);
857 free ((char *) child
);
861 extern sigset_t fatal_signal_set
;
868 (void) sigprocmask (SIG_BLOCK
, &fatal_signal_set
, (sigset_t
*) 0);
870 # ifdef HAVE_SIGSETMASK
871 (void) sigblock (fatal_signal_mask
);
881 sigemptyset (&empty
);
882 sigprocmask (SIG_SETMASK
, &empty
, (sigset_t
*) 0);
886 #ifdef MAKE_JOBSERVER
887 /* Set the child handler action flags to FLAGS. */
889 set_child_handler_action_flags (int flags
)
892 bzero ((char *) &sa
, sizeof sa
);
893 sa
.sa_handler
= child_handler
;
896 sigaction (SIGCHLD
, &sa
, NULL
);
898 #if defined SIGCLD && SIGCLD != SIGCHLD
899 sigaction (SIGCLD
, &sa
, NULL
);
905 /* Start a job to run the commands specified in CHILD.
906 CHILD is updated to reflect the commands and ID of the child process.
908 NOTE: On return fatal signals are blocked! The caller is responsible
909 for calling `unblock_sigs', once the new child is safely on the chain so
910 it can be cleaned up in the event of a fatal signal. */
913 start_job_command (struct child
*child
)
916 static int bad_stdin
= -1;
926 /* If we have a completely empty commandset, stop now. */
927 if (!child
->command_ptr
)
930 /* Combine the flags parsed for the line itself with
931 the flags specified globally for this target. */
932 flags
= (child
->file
->command_flags
933 | child
->file
->cmds
->lines_flags
[child
->command_line
- 1]);
935 p
= child
->command_ptr
;
936 child
->noerror
= flags
& COMMANDS_NOERROR
;
941 flags
|= COMMANDS_SILENT
;
943 flags
|= COMMANDS_RECURSE
;
946 else if (!isblank ((unsigned char)*p
))
951 /* Update the file's command flags with any new ones we found. We only
952 keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
953 now marking more commands recursive than should be in the case of
954 multiline define/endef scripts where only one line is marked "+". In
955 order to really fix this, we'll have to keep a lines_flags for every
956 actual line, after expansion. */
957 child
->file
->cmds
->lines_flags
[child
->command_line
- 1]
958 |= flags
& COMMANDS_RECURSE
;
960 /* Figure out an argument list from this command line. */
967 argv
= construct_command_argv (p
, &end
, child
->file
, &child
->sh_batch_file
);
970 child
->command_ptr
= NULL
;
974 child
->command_ptr
= end
;
978 /* If -q was given, say that updating `failed' if there was any text on the
979 command line, or `succeeded' otherwise. The exit status of 1 tells the
980 user that -q is saying `something to do'; the exit status for a random
982 if (argv
!= 0 && question_flag
&& !(flags
& COMMANDS_RECURSE
))
986 free ((char *) argv
);
988 child
->file
->update_status
= 1;
989 notice_finished_file (child
->file
);
993 if (touch_flag
&& !(flags
& COMMANDS_RECURSE
))
995 /* Go on to the next command. It might be the recursive one.
996 We construct ARGV only to find the end of the command line. */
1001 free ((char *) argv
);
1011 execute_by_shell
= 0; /* in case construct_command_argv sets it */
1013 /* This line has no commands. Go to the next. */
1014 if (job_next_command (child
))
1015 start_job_command (child
);
1018 /* No more commands. Make sure we're "running"; we might not be if
1019 (e.g.) all commands were skipped due to -n. */
1020 set_command_state (child
->file
, cs_running
);
1021 child
->file
->update_status
= 0;
1022 notice_finished_file (child
->file
);
1027 /* Print out the command. If silent, we call `message' with null so it
1028 can log the working directory before the command's own error messages
1031 message (0, (just_print_flag
|| (!(flags
& COMMANDS_SILENT
) && !silent_flag
))
1032 ? "%s" : (char *) 0, p
);
1034 /* Tell update_goal_chain that a command has been started on behalf of
1035 this target. It is important that this happens here and not in
1036 reap_children (where we used to do it), because reap_children might be
1037 reaping children from a different target. We want this increment to
1038 guaranteedly indicate that a command was started for the dependency
1039 chain (i.e., update_file recursion chain) we are processing. */
1043 /* Optimize an empty command. People use this for timestamp rules,
1044 so avoid forking a useless shell. Do this after we increment
1045 commands_started so make still treats this special case as if it
1046 performed some action (makes a difference as to what messages are
1049 #if !defined(VMS) && !defined(_AMIGA)
1051 #if defined __MSDOS__ || defined (__EMX__)
1052 unixy_shell
/* the test is complicated and we already did it */
1054 (argv
[0] && !strcmp (argv
[0], "/bin/sh"))
1057 && argv
[1][0] == '-' && argv
[1][1] == 'c' && argv
[1][2] == '\0')
1058 && (argv
[2] && argv
[2][0] == ':' && argv
[2][1] == '\0')
1062 free ((char *) argv
);
1065 #endif /* !VMS && !_AMIGA */
1067 /* If -n was given, recurse to get the next line in the sequence. */
1069 if (just_print_flag
&& !(flags
& COMMANDS_RECURSE
))
1073 free ((char *) argv
);
1078 /* Flush the output streams so they won't have things written twice. */
1084 #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
1086 /* Set up a bad standard input that reads from a broken pipe. */
1088 if (bad_stdin
== -1)
1090 /* Make a file descriptor that is the read end of a broken pipe.
1091 This will be used for some children's standard inputs. */
1095 /* Close the write side. */
1096 (void) close (pd
[1]);
1097 /* Save the read side. */
1100 /* Set the descriptor to close on exec, so it does not litter any
1101 child's descriptor table. When it is dup2'd onto descriptor 0,
1102 that descriptor will not close on exec. */
1103 CLOSE_ON_EXEC (bad_stdin
);
1107 #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
1109 /* Decide whether to give this child the `good' standard input
1110 (one that points to the terminal or whatever), or the `bad' one
1111 that points to the read side of a broken pipe. */
1113 child
->good_stdin
= !good_stdin_used
;
1114 if (child
->good_stdin
)
1115 good_stdin_used
= 1;
1122 /* Set up the environment for the child. */
1123 if (child
->environment
== 0)
1124 child
->environment
= target_environment (child
->file
);
1127 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1130 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1133 int is_remote
, id
, used_stdin
;
1134 if (start_remote_job (argv
, child
->environment
,
1135 child
->good_stdin
? 0 : bad_stdin
,
1136 &is_remote
, &id
, &used_stdin
))
1137 /* Don't give up; remote execution may fail for various reasons. If
1138 so, simply run the job locally. */
1142 if (child
->good_stdin
&& !used_stdin
)
1144 child
->good_stdin
= 0;
1145 good_stdin_used
= 0;
1147 child
->remote
= is_remote
;
1154 /* Fork the child process. */
1156 char **parent_environ
;
1165 if (!child_execute_job (argv
, child
)) {
1167 perror_with_name ("vfork", "");
1173 parent_environ
= environ
;
1176 /* If we aren't running a recursive command and we have a jobserver
1177 pipe, close it before exec'ing. */
1178 if (!(flags
& COMMANDS_RECURSE
) && job_fds
[0] >= 0)
1180 CLOSE_ON_EXEC (job_fds
[0]);
1181 CLOSE_ON_EXEC (job_fds
[1]);
1184 CLOSE_ON_EXEC (job_rfd
);
1186 /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
1187 child
->pid
= child_execute_job (child
->good_stdin
? 0 : bad_stdin
, 1,
1188 argv
, child
->environment
);
1193 perror_with_name ("spawn", "");
1197 /* undo CLOSE_ON_EXEC() after the child process has been started */
1198 if (!(flags
& COMMANDS_RECURSE
) && job_fds
[0] >= 0)
1200 fcntl (job_fds
[0], F_SETFD
, 0);
1201 fcntl (job_fds
[1], F_SETFD
, 0);
1204 fcntl (job_rfd
, F_SETFD
, 0);
1206 #else /* !__EMX__ */
1208 child
->pid
= vfork ();
1209 environ
= parent_environ
; /* Restore value child may have clobbered. */
1210 if (child
->pid
== 0)
1212 /* We are the child side. */
1215 /* If we aren't running a recursive command and we have a jobserver
1216 pipe, close it before exec'ing. */
1217 if (!(flags
& COMMANDS_RECURSE
) && job_fds
[0] >= 0)
1225 child_execute_job (child
->good_stdin
? 0 : bad_stdin
, 1,
1226 argv
, child
->environment
);
1228 else if (child
->pid
< 0)
1232 perror_with_name ("vfork", "");
1235 # endif /* !__EMX__ */
1239 #else /* __MSDOS__ or Amiga or WINDOWS32 */
1247 /* We call `system' to do the job of the SHELL, since stock DOS
1248 shell is too dumb. Our `system' knows how to handle long
1249 command lines even if pipes/redirection is needed; it will only
1250 call COMMAND.COM when its internal commands are used. */
1251 if (execute_by_shell
)
1253 char *cmdline
= argv
[0];
1254 /* We don't have a way to pass environment to `system',
1255 so we need to save and restore ours, sigh... */
1256 char **parent_environ
= environ
;
1258 environ
= child
->environment
;
1260 /* If we have a *real* shell, tell `system' to call
1261 it to do everything for us. */
1264 /* A *real* shell on MSDOS may not support long
1265 command lines the DJGPP way, so we must use `system'. */
1266 cmdline
= argv
[2]; /* get past "shell -c" */
1269 dos_command_running
= 1;
1270 proc_return
= system (cmdline
);
1271 environ
= parent_environ
;
1272 execute_by_shell
= 0; /* for the next time */
1276 dos_command_running
= 1;
1277 proc_return
= spawnvpe (P_WAIT
, argv
[0], argv
, child
->environment
);
1280 /* Need to unblock signals before turning off
1281 dos_command_running, so that child's signals
1282 will be treated as such (see fatal_error_signal). */
1284 dos_command_running
= 0;
1286 /* If the child got a signal, dos_status has its
1287 high 8 bits set, so be careful not to alter them. */
1288 if (proc_return
== -1)
1291 dos_status
|= (proc_return
& 0xff);
1293 child
->pid
= dos_pid
++;
1295 #endif /* __MSDOS__ */
1297 amiga_status
= MyExecute (argv
);
1300 child
->pid
= amiga_pid
++;
1301 if (amiga_batch_file
)
1303 amiga_batch_file
= 0;
1304 DeleteFile (amiga_bname
); /* Ignore errors. */
1312 /* make UNC paths safe for CreateProcess -- backslash format */
1314 if (arg0
&& arg0
[0] == '/' && arg0
[1] == '/')
1315 for ( ; arg0
&& *arg0
; arg0
++)
1319 /* make sure CreateProcess() has Path it needs */
1320 sync_Path_environment();
1322 hPID
= process_easy(argv
, child
->environment
);
1324 if (hPID
!= INVALID_HANDLE_VALUE
)
1325 child
->pid
= (int) hPID
;
1330 _("process_easy() failed failed to launch process (e=%d)\n"),
1331 process_last_err(hPID
));
1332 for (i
= 0; argv
[i
]; i
++)
1333 fprintf(stderr
, "%s ", argv
[i
]);
1334 fprintf(stderr
, _("\nCounted %d args in failed launch\n"), i
);
1337 #endif /* WINDOWS32 */
1338 #endif /* __MSDOS__ or Amiga or WINDOWS32 */
1340 /* Bump the number of jobs started in this second. */
1343 /* We are the parent side. Set the state to
1344 say the commands are running and return. */
1346 set_command_state (child
->file
, cs_running
);
1348 /* Free the storage used by the child's argument list. */
1351 free ((char *) argv
);
1357 child
->file
->update_status
= 2;
1358 notice_finished_file (child
->file
);
1362 /* Try to start a child running.
1363 Returns nonzero if the child was started (and maybe finished), or zero if
1364 the load was too high and the child was put on the `waiting_jobs' chain. */
1367 start_waiting_job (struct child
*c
)
1369 struct file
*f
= c
->file
;
1371 /* If we can start a job remotely, we always want to, and don't care about
1372 the local load average. We record that the job should be started
1373 remotely in C->remote for start_job_command to test. */
1375 c
->remote
= start_remote_job_p (1);
1377 /* If we are running at least one job already and the load average
1378 is too high, make this one wait. */
1379 if (!c
->remote
&& job_slots_used
> 0 && load_too_high ())
1381 /* Put this child on the chain of children waiting for the load average
1383 set_command_state (f
, cs_running
);
1384 c
->next
= waiting_jobs
;
1389 /* Start the first command; reap_children will run later command lines. */
1390 start_job_command (c
);
1392 switch (f
->command_state
)
1396 DB (DB_JOBS
, (_("Putting child 0x%08lx (%s) PID %ld%s on the chain.\n"),
1397 (unsigned long int) c
, c
->file
->name
,
1398 (long) c
->pid
, c
->remote
? _(" (remote)") : ""));
1400 /* One more job slot is in use. */
1405 case cs_not_started
:
1406 /* All the command lines turned out to be empty. */
1407 f
->update_status
= 0;
1411 notice_finished_file (f
);
1416 assert (f
->command_state
== cs_finished
);
1423 /* Create a `struct child' for FILE and start its commands running. */
1426 new_job (struct file
*file
)
1428 register struct commands
*cmds
= file
->cmds
;
1429 register struct child
*c
;
1431 register unsigned int i
;
1433 /* Let any previously decided-upon jobs that are waiting
1434 for the load to go down start before this new one. */
1435 start_waiting_jobs ();
1437 /* Reap any children that might have finished recently. */
1438 reap_children (0, 0);
1440 /* Chop the commands up into lines if they aren't already. */
1441 chop_commands (cmds
);
1443 /* Expand the command lines and store the results in LINES. */
1444 lines
= (char **) xmalloc (cmds
->ncommand_lines
* sizeof (char *));
1445 for (i
= 0; i
< cmds
->ncommand_lines
; ++i
)
1447 /* Collapse backslash-newline combinations that are inside variable
1448 or function references. These are left alone by the parser so
1449 that they will appear in the echoing of commands (where they look
1450 nice); and collapsed by construct_command_argv when it tokenizes.
1451 But letting them survive inside function invocations loses because
1452 we don't want the functions to see them as part of the text. */
1454 char *in
, *out
, *ref
;
1456 /* IN points to where in the line we are scanning.
1457 OUT points to where in the line we are writing.
1458 When we collapse a backslash-newline combination,
1459 IN gets ahead of OUT. */
1461 in
= out
= cmds
->command_lines
[i
];
1462 while ((ref
= strchr (in
, '$')) != 0)
1464 ++ref
; /* Move past the $. */
1467 /* Copy the text between the end of the last chunk
1468 we processed (where IN points) and the new chunk
1469 we are about to process (where REF points). */
1470 bcopy (in
, out
, ref
- in
);
1472 /* Move both pointers past the boring stuff. */
1476 if (*ref
== '(' || *ref
== '{')
1478 char openparen
= *ref
;
1479 char closeparen
= openparen
== '(' ? ')' : '}';
1483 *out
++ = *in
++; /* Copy OPENPAREN. */
1484 /* IN now points past the opening paren or brace.
1485 Count parens or braces until it is matched. */
1489 if (*in
== closeparen
&& --count
< 0)
1491 else if (*in
== '\\' && in
[1] == '\n')
1493 /* We have found a backslash-newline inside a
1494 variable or function reference. Eat it and
1495 any following whitespace. */
1498 for (p
= in
- 1; p
> ref
&& *p
== '\\'; --p
)
1502 /* There were two or more backslashes, so this is
1503 not really a continuation line. We don't collapse
1504 the quoting backslashes here as is done in
1505 collapse_continuations, because the line will
1506 be collapsed again after expansion. */
1510 /* Skip the backslash, newline and
1511 any following whitespace. */
1512 in
= next_token (in
+ 2);
1514 /* Discard any preceding whitespace that has
1515 already been written to the output. */
1517 && isblank ((unsigned char)out
[-1]))
1520 /* Replace it all with a single space. */
1526 if (*in
== openparen
)
1535 /* There are no more references in this line to worry about.
1536 Copy the remaining uninteresting text to the output. */
1540 /* Finally, expand the line. */
1541 lines
[i
] = allocated_variable_expand_for_file (cmds
->command_lines
[i
],
1545 /* Start the command sequence, record it in a new
1546 `struct child', and add that to the chain. */
1548 c
= (struct child
*) xmalloc (sizeof (struct child
));
1549 bzero ((char *)c
, sizeof (struct child
));
1551 c
->command_lines
= lines
;
1552 c
->sh_batch_file
= NULL
;
1554 /* Fetch the first command line to be run. */
1555 job_next_command (c
);
1557 /* Wait for a job slot to be freed up. If we allow an infinite number
1558 don't bother; also job_slots will == 0 if we're using the jobserver. */
1561 while (job_slots_used
== job_slots
)
1562 reap_children (1, 0);
1564 #ifdef MAKE_JOBSERVER
1565 /* If we are controlling multiple jobs make sure we have a token before
1566 starting the child. */
1568 /* This can be inefficient. There's a decent chance that this job won't
1569 actually have to run any subprocesses: the command script may be empty
1570 or otherwise optimized away. It would be nice if we could defer
1571 obtaining a token until just before we need it, in start_job_command.
1572 To do that we'd need to keep track of whether we'd already obtained a
1573 token (since start_job_command is called for each line of the job, not
1574 just once). Also more thought needs to go into the entire algorithm;
1575 this is where the old parallel job code waits, so... */
1577 else if (job_fds
[0] >= 0)
1584 DB (DB_JOBS
, ("Need a job token; we %shave children\n",
1585 children
? "" : "don't "));
1587 /* If we don't already have a job started, use our "free" token. */
1591 /* Read a token. As long as there's no token available we'll block.
1592 We enable interruptible system calls before the read(2) so that if
1593 we get a SIGCHLD while we're waiting, we'll return with EINTR and
1594 we can process the death(s) and return tokens to the free pool.
1596 Once we return from the read, we immediately reinstate restartable
1597 system calls. This allows us to not worry about checking for
1598 EINTR on all the other system calls in the program.
1600 There is one other twist: there is a span between the time
1601 reap_children() does its last check for dead children and the time
1602 the read(2) call is entered, below, where if a child dies we won't
1603 notice. This is extremely serious as it could cause us to
1604 deadlock, given the right set of events.
1606 To avoid this, we do the following: before we reap_children(), we
1607 dup(2) the read FD on the jobserver pipe. The read(2) call below
1608 uses that new FD. In the signal handler, we close that FD. That
1609 way, if a child dies during the section mentioned above, the
1610 read(2) will be invoked with an invalid FD and will return
1611 immediately with EBADF. */
1613 /* Make sure we have a dup'd FD. */
1616 DB (DB_JOBS
, ("Duplicate the job FD\n"));
1617 job_rfd
= dup (job_fds
[0]);
1620 /* Reap anything that's currently waiting. */
1621 reap_children (0, 0);
1623 /* If our "free" token has become available, use it. */
1627 /* Set interruptible system calls, and read() for a job token. */
1628 set_child_handler_action_flags (0);
1629 got_token
= read (job_rfd
, &token
, 1);
1630 saved_errno
= errno
;
1632 /* The child handler must be turned off here. */
1633 signal (SIGCHLD
, SIG_DFL
);
1635 set_child_handler_action_flags (SA_RESTART
);
1637 /* If we got one, we're done here. */
1640 DB (DB_JOBS
, (_("Obtained token for child 0x%08lx (%s).\n"),
1641 (unsigned long int) c
, c
->file
->name
));
1645 /* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,
1646 go back and reap_children(), and try again. */
1647 errno
= saved_errno
;
1648 if (errno
!= EINTR
&& errno
!= EBADF
)
1649 pfatal_with_name (_("read jobs pipe"));
1651 DB (DB_JOBS
, ("Read returned EBADF.\n"));
1655 /* The job is now primed. Start it running.
1656 (This will notice if there are in fact no commands.) */
1657 (void) start_waiting_job (c
);
1659 if (job_slots
== 1 || not_parallel
)
1660 /* Since there is only one job slot, make things run linearly.
1661 Wait for the child to die, setting the state to `cs_finished'. */
1662 while (file
->command_state
== cs_running
)
1663 reap_children (1, 0);
1668 /* Move CHILD's pointers to the next command for it to execute.
1669 Returns nonzero if there is another command. */
1672 job_next_command (struct child
*child
)
1674 while (child
->command_ptr
== 0 || *child
->command_ptr
== '\0')
1676 /* There are no more lines in the expansion of this line. */
1677 if (child
->command_line
== child
->file
->cmds
->ncommand_lines
)
1679 /* There are no more lines to be expanded. */
1680 child
->command_ptr
= 0;
1684 /* Get the next line to run. */
1685 child
->command_ptr
= child
->command_lines
[child
->command_line
++];
1690 /* Determine if the load average on the system is too high to start a new job.
1691 The real system load average is only recomputed once a second. However, a
1692 very parallel make can easily start tens or even hundreds of jobs in a
1693 second, which brings the system to its knees for a while until that first
1694 batch of jobs clears out.
1696 To avoid this we use a weighted algorithm to try to account for jobs which
1697 have been started since the last second, and guess what the load average
1698 would be now if it were computed.
1700 This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
1703 ! calculate something load-oid and add to the observed sys.load,
1704 ! so that latter can catch up:
1705 ! - every job started increases jobctr;
1706 ! - every dying job decreases a positive jobctr;
1707 ! - the jobctr value gets zeroed every change of seconds,
1708 ! after its value*weight_b is stored into the 'backlog' value last_sec
1709 ! - weight_a times the sum of jobctr and last_sec gets
1710 ! added to the observed sys.load.
1712 ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
1713 ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
1714 ! sub-shelled commands (rm, echo, sed...) for tests.
1715 ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
1716 ! resulted in significant excession of the load limit, raising it
1717 ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
1718 ! reach the limit in most test cases.
1720 ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
1721 ! exceeding the limit for longer-running stuff (compile jobs in
1722 ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
1723 ! small jobs' effects.
1727 #define LOAD_WEIGHT_A 0.25
1728 #define LOAD_WEIGHT_B 0.25
1731 load_too_high (void)
1733 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__)
1736 static double last_sec
;
1737 static time_t last_now
;
1741 if (max_load_average
< 0)
1744 /* Find the real system load average. */
1746 if (getloadavg (&load
, 1) != 1)
1748 static int lossage
= -1;
1749 /* Complain only once for the same error. */
1750 if (lossage
== -1 || errno
!= lossage
)
1753 /* An errno value of zero means getloadavg is just unsupported. */
1755 _("cannot enforce load limits on this operating system"));
1757 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
1764 /* If we're in a new second zero the counter and correct the backlog
1765 value. Only keep the backlog for one extra second; after that it's 0. */
1769 if (last_now
== now
- 1)
1770 last_sec
= LOAD_WEIGHT_B
* job_counter
;
1778 /* Try to guess what the load would be right now. */
1779 guess
= load
+ (LOAD_WEIGHT_A
* (job_counter
+ last_sec
));
1781 DB (DB_JOBS
, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
1782 guess
, load
, max_load_average
));
1784 return guess
>= max_load_average
;
1788 /* Start jobs that are waiting for the load to be lower. */
1791 start_waiting_jobs (void)
1795 if (waiting_jobs
== 0)
1800 /* Check for recently deceased descendants. */
1801 reap_children (0, 0);
1803 /* Take a job off the waiting list. */
1805 waiting_jobs
= job
->next
;
1807 /* Try to start that job. We break out of the loop as soon
1808 as start_waiting_job puts one back on the waiting list. */
1810 while (start_waiting_job (job
) && waiting_jobs
!= 0);
1817 #include <descrip.h>
1820 /* This is called as an AST when a child process dies (it won't get
1821 interrupted by anything except a higher level AST).
1823 int vmsHandleChildTerm(struct child
*child
)
1826 register struct child
*lastc
, *c
;
1829 vms_jobsefnmask
&= ~(1 << (child
->efn
- 32));
1831 lib$
free_ef(&child
->efn
);
1833 (void) sigblock (fatal_signal_mask
);
1835 child_failed
= !(child
->cstatus
& 1 || ((child
->cstatus
& 7) == 0));
1837 /* Search for a child matching the deceased one. */
1839 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
1840 for (c
= children
; c
!= 0 && c
!= child
; lastc
= c
, c
= c
->next
);
1845 if (child_failed
&& !c
->noerror
&& !ignore_errors_flag
)
1847 /* The commands failed. Write an error message,
1848 delete non-precious targets, and abort. */
1849 child_error (c
->file
->name
, c
->cstatus
, 0, 0, 0);
1850 c
->file
->update_status
= 1;
1851 delete_child_targets (c
);
1857 /* The commands failed, but we don't care. */
1858 child_error (c
->file
->name
, c
->cstatus
, 0, 0, 1);
1862 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
1863 /* If there are more commands to run, try to start them. */
1866 switch (c
->file
->command_state
)
1869 /* Successfully started. */
1873 if (c
->file
->update_status
!= 0) {
1874 /* We failed to start the commands. */
1875 delete_child_targets (c
);
1880 error (NILF
, _("internal error: `%s' command_state"),
1885 #endif /* RECURSIVEJOBS */
1888 /* Set the state flag to say the commands have finished. */
1889 c
->file
->command_state
= cs_finished
;
1890 notice_finished_file (c
->file
);
1892 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
1893 /* Remove the child from the chain and free it. */
1897 lastc
->next
= c
->next
;
1899 #endif /* RECURSIVEJOBS */
1901 /* There is now another slot open. */
1902 if (job_slots_used
> 0)
1905 /* If the job failed, and the -k flag was not given, die. */
1906 if (child_failed
&& !keep_going_flag
)
1909 (void) sigsetmask (sigblock (0) & ~(fatal_signal_mask
));
1915 Spawn a process executing the command in ARGV and return its pid. */
1917 #define MAXCMDLEN 200
1919 /* local helpers to make ctrl+c and ctrl+y working, see below */
1921 #include <libclidef.h>
1924 static int ctrlMask
= LIB$M_CLI_CTRLY
;
1925 static int oldCtrlMask
;
1926 static int setupYAstTried
= 0;
1927 static int pidToAbort
= 0;
1930 static void reEnableAst(void) {
1931 lib$
enable_ctrl (&oldCtrlMask
,0);
1934 static astHandler (void) {
1936 sys$
forcex (&pidToAbort
, 0, SS$_ABORT
);
1939 kill (getpid(),SIGQUIT
);
1942 static void tryToSetupYAst(void) {
1943 $
DESCRIPTOR(inputDsc
,"SYS$COMMAND");
1946 short int status
, count
;
1953 status
= sys$
assign(&inputDsc
,&chan
,0,0);
1954 if (!(status
&SS$_NORMAL
)) {
1959 status
= sys$
qiow (0, chan
, IO$_SETMODE
|IO$M_CTRLYAST
,&iosb
,0,0,
1960 astHandler
,0,0,0,0,0);
1961 if (status
==SS$_NORMAL
)
1962 status
= iosb
.status
;
1963 if (status
==SS$_ILLIOFUNC
|| status
==SS$_NOPRIV
) {
1965 #ifdef CTRLY_ENABLED_ANYWAY
1967 _("-warning, CTRL-Y will leave sub-process(es) around.\n"));
1972 else if (!(status
&SS$_NORMAL
)) {
1978 /* called from AST handler ? */
1979 if (setupYAstTried
>1)
1981 if (atexit(reEnableAst
))
1983 _("-warning, you may have to re-enable CTRL-Y handling from DCL.\n"));
1984 status
= lib$
disable_ctrl (&ctrlMask
, &oldCtrlMask
);
1985 if (!(status
&SS$_NORMAL
)) {
1991 child_execute_job (char *argv
, struct child
*child
)
1994 static struct dsc$descriptor_s cmddsc
;
1995 static struct dsc$descriptor_s pnamedsc
;
1996 static struct dsc$descriptor_s ifiledsc
;
1997 static struct dsc$descriptor_s ofiledsc
;
1998 static struct dsc$descriptor_s efiledsc
;
1999 int have_redirection
= 0;
2000 int have_newline
= 0;
2002 int spflags
= CLI$M_NOWAIT
;
2004 char *cmd
= alloca (strlen (argv
) + 512), *p
, *q
;
2005 char ifile
[256], ofile
[256], efile
[256];
2009 /* Parse IO redirection. */
2015 DB (DB_JOBS
, ("child_execute_job (%s)\n", argv
));
2017 while (isspace ((unsigned char)*argv
))
2023 sprintf (procname
, "GMAKE_%05x", getpid () & 0xfffff);
2024 pnamedsc
.dsc$w_length
= strlen(procname
);
2025 pnamedsc
.dsc$a_pointer
= procname
;
2026 pnamedsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
2027 pnamedsc
.dsc$b_class
= DSC$K_CLASS_S
;
2029 /* Handle comments and redirection. */
2030 for (p
= argv
, q
= cmd
; *p
; p
++, q
++)
2042 if (isspace ((unsigned char)*p
))
2044 do { p
++; } while (isspace ((unsigned char)*p
));
2050 p
= vms_redirect (&ifiledsc
, ifile
, p
);
2052 have_redirection
= 1;
2055 have_redirection
= 1;
2059 if (strncmp (p
, ">&1", 3) == 0)
2062 strcpy (efile
, "sys$output");
2063 efiledsc
.dsc$w_length
= strlen(efile
);
2064 efiledsc
.dsc$a_pointer
= efile
;
2065 efiledsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
2066 efiledsc
.dsc$b_class
= DSC$K_CLASS_S
;
2070 p
= vms_redirect (&efiledsc
, efile
, p
);
2075 p
= vms_redirect (&ofiledsc
, ofile
, p
);
2088 if (strncmp (cmd
, "builtin_", 8) == 0)
2090 child
->pid
= 270163;
2094 DB (DB_JOBS
, (_("BUILTIN [%s][%s]\n"), cmd
, cmd
+8));
2100 && ((*(p
+2) == ' ') || (*(p
+2) == '\t')))
2103 while ((*p
== ' ') || (*p
== '\t'))
2105 DB (DB_JOBS
, (_("BUILTIN CD %s\n"), p
));
2111 else if ((*(p
) == 'r')
2113 && ((*(p
+2) == ' ') || (*(p
+2) == '\t')))
2119 while ((*p
== ' ') || (*p
== '\t'))
2123 DB (DB_JOBS
, (_("BUILTIN RM %s\n"), p
));
2144 printf(_("Unknown builtin command '%s'\n"), cmd
);
2150 /* Create a *.com file if either the command is too long for
2151 lib$spawn, or the command contains a newline, or if redirection
2152 is desired. Forcing commands with newlines into DCLs allows to
2153 store search lists on user mode logicals. */
2155 if (strlen (cmd
) > MAXCMDLEN
2156 || (have_redirection
!= 0)
2157 || (have_newline
!= 0))
2162 int alevel
= 0; /* apostrophe level */
2164 if (strlen (cmd
) == 0)
2166 printf (_("Error, empty command\n"));
2171 outfile
= open_tmpfile (&comname
, "sys$scratch:CMDXXXXXX.COM");
2173 pfatal_with_name (_("fopen (temporary file)"));
2177 fprintf (outfile
, "$ assign/user %s sys$input\n", ifile
);
2178 DB (DB_JOBS
, (_("Redirected input from %s\n"), ifile
));
2179 ifiledsc
.dsc$w_length
= 0;
2184 fprintf (outfile
, "$ define sys$error %s\n", efile
);
2185 DB (DB_JOBS
, (_("Redirected error to %s\n"), efile
));
2186 efiledsc
.dsc$w_length
= 0;
2191 fprintf (outfile
, "$ define sys$output %s\n", ofile
);
2192 DB (DB_JOBS
, (_("Redirected output to %s\n"), ofile
));
2193 ofiledsc
.dsc$w_length
= 0;
2197 for (c
= '\n'; c
; c
= *q
++)
2202 /* At a newline, skip any whitespace around a leading $
2203 from the command and issue exactly one $ into the DCL. */
2204 while (isspace ((unsigned char)*p
))
2208 while (isspace ((unsigned char)*p
))
2210 fwrite (p
, 1, q
- p
, outfile
);
2211 fputc ('$', outfile
);
2212 fputc (' ', outfile
);
2213 /* Reset variables. */
2217 /* Nice places for line breaks are after strings, after
2218 comma or space and before slash. */
2220 q
= vms_handle_apos (q
);
2236 /* Enough stuff for a line. */
2237 fwrite (p
, 1, sep
- p
, outfile
);
2241 /* The command continues. */
2242 fputc ('-', outfile
);
2244 fputc ('\n', outfile
);
2248 fwrite (p
, 1, q
- p
, outfile
);
2249 fputc ('\n', outfile
);
2253 sprintf (cmd
, "$ @%s", comname
);
2255 DB (DB_JOBS
, (_("Executing %s instead\n"), cmd
));
2258 cmddsc
.dsc$w_length
= strlen(cmd
);
2259 cmddsc
.dsc$a_pointer
= cmd
;
2260 cmddsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
2261 cmddsc
.dsc$b_class
= DSC$K_CLASS_S
;
2264 while (child
->efn
< 32 || child
->efn
> 63)
2266 status
= lib$
get_ef ((unsigned long *)&child
->efn
);
2271 sys$
clref (child
->efn
);
2273 vms_jobsefnmask
|= (1 << (child
->efn
- 32));
2276 LIB$SPAWN [command-string]
2281 [,process-id] [,completion-status-address] [,byte-integer-event-flag-num]
2282 [,AST-address] [,varying-AST-argument]
2283 [,prompt-string] [,cli] [,table]
2286 #ifndef DONTWAITFORCHILD
2288 * Code to make ctrl+c and ctrl+y working.
2289 * The problem starts with the synchronous case where after lib$spawn is
2290 * called any input will go to the child. But with input re-directed,
2291 * both control characters won't make it to any of the programs, neither
2292 * the spawning nor to the spawned one. Hence the caller needs to spawn
2293 * with CLI$M_NOWAIT to NOT give up the input focus. A sys$waitfr
2294 * has to follow to simulate the wanted synchronous behaviour.
2295 * The next problem is ctrl+y which isn't caught by the crtl and
2296 * therefore isn't converted to SIGQUIT (for a signal handler which is
2297 * already established). The only way to catch ctrl+y, is an AST
2298 * assigned to the input channel. But ctrl+y handling of DCL needs to be
2299 * disabled, otherwise it will handle it. Not to mention the previous
2300 * ctrl+y handling of DCL needs to be re-established before make exits.
2301 * One more: At the time of LIB$SPAWN signals are blocked. SIGQUIT will
2302 * make it to the signal handler after the child "normally" terminates.
2303 * This isn't enough. It seems reasonable for simple command lines like
2304 * a 'cc foobar.c' spawned in a subprocess but it is unacceptable for
2305 * spawning make. Therefore we need to abort the process in the AST.
2307 * Prior to the spawn it is checked if an AST is already set up for
2308 * ctrl+y, if not one is set up for a channel to SYS$COMMAND. In general
2309 * this will work except if make is run in a batch environment, but there
2310 * nobody can press ctrl+y. During the setup the DCL handling of ctrl+y
2311 * is disabled and an exit handler is established to re-enable it.
2312 * If the user interrupts with ctrl+y, the assigned AST will fire, force
2313 * an abort to the subprocess and signal SIGQUIT, which will be caught by
2314 * the already established handler and will bring us back to common code.
2315 * After the spawn (now /nowait) a sys$waitfr simulates the /wait and
2316 * enables the ctrl+y be delivered to this code. And the ctrl+c too,
2317 * which the crtl converts to SIGINT and which is caught by the common
2318 * signal handler. Because signals were blocked before entering this code
2319 * sys$waitfr will always complete and the SIGQUIT will be processed after
2320 * it (after termination of the current block, somewhere in common code).
2321 * And SIGINT too will be delayed. That is ctrl+c can only abort when the
2322 * current command completes. Anyway it's better than nothing :-)
2325 if (!setupYAstTried
)
2327 status
= lib$
spawn (&cmddsc
, /* cmd-string */
2328 (ifiledsc
.dsc$w_length
== 0)?0:&ifiledsc
, /* input-file */
2329 (ofiledsc
.dsc$w_length
== 0)?0:&ofiledsc
, /* output-file */
2330 &spflags
, /* flags */
2331 &pnamedsc
, /* proc name */
2332 &child
->pid
, &child
->cstatus
, &child
->efn
,
2337 pidToAbort
= child
->pid
;
2338 status
= sys$
waitfr (child
->efn
);
2340 vmsHandleChildTerm(child
);
2343 status
= lib$
spawn (&cmddsc
,
2344 (ifiledsc
.dsc$w_length
== 0)?0:&ifiledsc
,
2345 (ofiledsc
.dsc$w_length
== 0)?0:&ofiledsc
,
2348 &child
->pid
, &child
->cstatus
, &child
->efn
,
2349 vmsHandleChildTerm
, child
,
2355 printf (_("Error spawning, %d\n") ,status
);
2367 if (comname
&& !ISDB (DB_JOBS
))
2370 return (status
& 1);
2375 /* EMX: Start a child process. This function returns the new pid. */
2376 # if defined __MSDOS__ || defined __EMX__
2378 child_execute_job (int stdin_fd
, int stdout_fd
, char **argv
, char **envp
)
2381 /* stdin_fd == 0 means: nothing to do for stdin;
2382 stdout_fd == 1 means: nothing to do for stdout */
2383 int save_stdin
= (stdin_fd
!= 0) ? dup (0) : 0;
2384 int save_stdout
= (stdout_fd
!= 1) ? dup (1): 1;
2386 /* < 0 only if dup() failed */
2388 fatal (NILF
, _("no more file handles: could not duplicate stdin\n"));
2389 if (save_stdout
< 0)
2390 fatal (NILF
, _("no more file handles: could not duplicate stdout\n"));
2392 /* Close unnecessary file handles for the child. */
2393 if (save_stdin
!= 0)
2394 CLOSE_ON_EXEC (save_stdin
);
2395 if (save_stdout
!= 1)
2396 CLOSE_ON_EXEC (save_stdout
);
2398 /* Connect the pipes to the child process. */
2400 (void) dup2 (stdin_fd
, 0);
2402 (void) dup2 (stdout_fd
, 1);
2404 /* stdin_fd and stdout_fd must be closed on exit because we are
2405 still in the parent process */
2407 CLOSE_ON_EXEC (stdin_fd
);
2409 CLOSE_ON_EXEC (stdout_fd
);
2411 /* Run the command. */
2412 pid
= exec_command (argv
, envp
);
2414 /* Restore stdout/stdin of the parent and close temporary FDs. */
2417 if (dup2 (save_stdin
, 0) != 0)
2418 fatal (NILF
, _("Could not restore stdin\n"));
2425 if (dup2 (save_stdout
, 1) != 1)
2426 fatal (NILF
, _("Could not restore stdout\n"));
2428 close (save_stdout
);
2434 #elif !defined (_AMIGA) && !defined (__MSDOS__)
2437 Replace the current process with one executing the command in ARGV.
2438 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
2439 the environment of the new program. This function does not return. */
2441 child_execute_job (int stdin_fd
, int stdout_fd
, char **argv
, char **envp
)
2444 (void) dup2 (stdin_fd
, 0);
2446 (void) dup2 (stdout_fd
, 1);
2448 (void) close (stdin_fd
);
2450 (void) close (stdout_fd
);
2452 /* Run the command. */
2453 exec_command (argv
, envp
);
2455 #endif /* !AMIGA && !__MSDOS__ */
2457 #endif /* !WINDOWS32 */
2460 /* Replace the current process with one running the command in ARGV,
2461 with environment ENVP. This function does not return. */
2463 /* EMX: This function returns the pid of the child process. */
2469 exec_command (char **argv
, char **envp
)
2472 /* to work around a problem with signals and execve: ignore them */
2474 signal (SIGCHLD
,SIG_IGN
);
2476 /* Run the program. */
2477 execve (argv
[0], argv
, envp
);
2478 perror_with_name ("execve: ", argv
[0]);
2479 _exit (EXIT_FAILURE
);
2485 int exit_code
= EXIT_FAILURE
;
2487 /* make sure CreateProcess() has Path it needs */
2488 sync_Path_environment();
2490 /* launch command */
2491 hPID
= process_easy(argv
, envp
);
2493 /* make sure launch ok */
2494 if (hPID
== INVALID_HANDLE_VALUE
)
2498 _("process_easy() failed failed to launch process (e=%d)\n"),
2499 process_last_err(hPID
));
2500 for (i
= 0; argv
[i
]; i
++)
2501 fprintf(stderr
, "%s ", argv
[i
]);
2502 fprintf(stderr
, _("\nCounted %d args in failed launch\n"), i
);
2506 /* wait and reap last child */
2507 hWaitPID
= process_wait_for_any();
2510 /* was an error found on this process? */
2511 err
= process_last_err(hWaitPID
);
2514 exit_code
= process_exit_code(hWaitPID
);
2517 fprintf(stderr
, "make (e=%d, rc=%d): %s",
2518 err
, exit_code
, map_windows32_error_to_string(err
));
2520 /* cleanup process */
2521 process_cleanup(hWaitPID
);
2523 /* expect to find only last pid, warn about other pids reaped */
2524 if (hWaitPID
== hPID
)
2528 _("make reaped child pid %d, still waiting for pid %d\n"),
2532 /* return child's exit code as our exit code */
2535 #else /* !WINDOWS32 */
2541 /* Be the user, permanently. */
2546 /* Run the program. */
2547 pid
= spawnvpe (P_NOWAIT
, argv
[0], argv
, envp
);
2552 /* the file might have a strange shell extension */
2553 if (errno
== ENOENT
)
2558 /* Run the program. */
2560 execvp (argv
[0], argv
);
2562 # endif /* !__EMX__ */
2567 error (NILF
, _("%s: Command not found"), argv
[0]);
2571 /* The file is not executable. Try it as a shell script. */
2572 extern char *getenv ();
2579 /* Do not use $SHELL from the environment */
2580 struct variable
*p
= lookup_variable ("SHELL", 5);
2586 shell
= getenv ("SHELL");
2589 shell
= default_shell
;
2592 while (argv
[argc
] != 0)
2600 new_argv
= (char **) alloca ((1 + argc
+ 1) * sizeof (char *));
2601 new_argv
[0] = shell
;
2612 new_argv
[i
] = argv
[0];
2615 new_argv
[i
+ argc
] = argv
[argc
];
2620 pid
= spawnvpe (P_NOWAIT
, shell
, new_argv
, envp
);
2624 execvp (shell
, new_argv
);
2626 if (errno
== ENOENT
)
2627 error (NILF
, _("%s: Shell program not found"), shell
);
2629 perror_with_name ("execvp: ", shell
);
2635 /* this nasty error was driving me nuts :-( */
2636 error (NILF
, _("spawnvpe: environment space might be exhausted"));
2641 perror_with_name ("execvp: ", argv
[0]);
2650 #endif /* !WINDOWS32 */
2653 #else /* On Amiga */
2654 void exec_command (char **argv
)
2659 void clean_tmp (void)
2661 DeleteFile (amiga_bname
);
2664 #endif /* On Amiga */
2667 /* Figure out the argument list necessary to run LINE as a command. Try to
2668 avoid using a shell. This routine handles only ' quoting, and " quoting
2669 when no backslash, $ or ` characters are seen in the quotes. Starting
2670 quotes may be escaped with a backslash. If any of the characters in
2671 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2672 is the first word of a line, the shell is used.
2674 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2675 If *RESTP is NULL, newlines will be ignored.
2677 SHELL is the shell to use, or nil to use the default shell.
2678 IFS is the value of $IFS, or nil (meaning the default). */
2681 construct_command_argv_internal (char *line
, char **restp
, char *shell
,
2682 char *ifs
, char **batch_filename_ptr
)
2685 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
2686 We call `system' for anything that requires ``slow'' processing,
2687 because DOS shells are too dumb. When $SHELL points to a real
2688 (unix-style) shell, `system' just calls it to do everything. When
2689 $SHELL points to a DOS shell, `system' does most of the work
2690 internally, calling the shell only for its internal commands.
2691 However, it looks on the $PATH first, so you can e.g. have an
2692 external command named `mkdir'.
2694 Since we call `system', certain characters and commands below are
2695 actually not specific to COMMAND.COM, but to the DJGPP implementation
2696 of `system'. In particular:
2698 The shell wildcard characters are in DOS_CHARS because they will
2699 not be expanded if we call the child via `spawnXX'.
2701 The `;' is in DOS_CHARS, because our `system' knows how to run
2702 multiple commands on a single line.
2704 DOS_CHARS also include characters special to 4DOS/NDOS, so we
2705 won't have to tell one from another and have one more set of
2706 commands and special characters. */
2707 static char sh_chars_dos
[] = "*?[];|<>%^&()";
2708 static char *sh_cmds_dos
[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2709 "copy", "ctty", "date", "del", "dir", "echo",
2710 "erase", "exit", "for", "goto", "if", "md",
2711 "mkdir", "path", "pause", "prompt", "rd",
2712 "rmdir", "rem", "ren", "rename", "set",
2713 "shift", "time", "type", "ver", "verify",
2716 static char sh_chars_sh
[] = "#;\"*?[]&|<>(){}$`^";
2717 static char *sh_cmds_sh
[] = { "cd", "echo", "eval", "exec", "exit", "login",
2718 "logout", "set", "umask", "wait", "while",
2719 "for", "case", "if", ":", ".", "break",
2720 "continue", "export", "read", "readonly",
2721 "shift", "times", "trap", "switch", "unset",
2726 #elif defined (__EMX__)
2727 static char sh_chars_dos
[] = "*?[];|<>%^&()";
2728 static char *sh_cmds_dos
[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2729 "copy", "ctty", "date", "del", "dir", "echo",
2730 "erase", "exit", "for", "goto", "if", "md",
2731 "mkdir", "path", "pause", "prompt", "rd",
2732 "rmdir", "rem", "ren", "rename", "set",
2733 "shift", "time", "type", "ver", "verify",
2736 static char sh_chars_os2
[] = "*?[];|<>%^()\"'&";
2737 static char *sh_cmds_os2
[] = { "call", "cd", "chcp", "chdir", "cls", "copy",
2738 "date", "del", "detach", "dir", "echo",
2739 "endlocal", "erase", "exit", "for", "goto", "if",
2740 "keys", "md", "mkdir", "move", "path", "pause",
2741 "prompt", "rd", "rem", "ren", "rename", "rmdir",
2742 "set", "setlocal", "shift", "start", "time",
2743 "type", "ver", "verify", "vol", ":", 0 };
2745 static char sh_chars_sh
[] = "#;\"*?[]&|<>(){}$`^~'";
2746 static char *sh_cmds_sh
[] = { "echo", "cd", "eval", "exec", "exit", "login",
2747 "logout", "set", "umask", "wait", "while",
2748 "for", "case", "if", ":", ".", "break",
2749 "continue", "export", "read", "readonly",
2750 "shift", "times", "trap", "switch", "unset",
2755 #elif defined (_AMIGA)
2756 static char sh_chars
[] = "#;\"|<>()?*$`";
2757 static char *sh_cmds
[] = { "cd", "eval", "if", "delete", "echo", "copy",
2758 "rename", "set", "setenv", "date", "makedir",
2759 "skip", "else", "endif", "path", "prompt",
2760 "unset", "unsetenv", "version",
2762 #elif defined (WINDOWS32)
2763 static char sh_chars_dos
[] = "\"|&<>";
2764 static char *sh_cmds_dos
[] = { "break", "call", "cd", "chcp", "chdir", "cls",
2765 "copy", "ctty", "date", "del", "dir", "echo",
2766 "erase", "exit", "for", "goto", "if", "if", "md",
2767 "mkdir", "path", "pause", "prompt", "rd", "rem",
2768 "ren", "rename", "rmdir", "set", "shift", "time",
2769 "type", "ver", "verify", "vol", ":", 0 };
2770 static char sh_chars_sh
[] = "#;\"*?[]&|<>(){}$`^";
2771 static char *sh_cmds_sh
[] = { "cd", "eval", "exec", "exit", "login",
2772 "logout", "set", "umask", "wait", "while", "for",
2773 "case", "if", ":", ".", "break", "continue",
2774 "export", "read", "readonly", "shift", "times",
2775 "trap", "switch", "test",
2776 #ifdef BATCH_MODE_ONLY_SHELL
2782 #elif defined(__riscos__)
2783 static char sh_chars
[] = "";
2784 static char *sh_cmds
[] = { 0 };
2785 #else /* must be UNIX-ish */
2786 static char sh_chars
[] = "#;\"*?[]&|<>(){}$`^~!";
2787 static char *sh_cmds
[] = { ".", ":", "break", "case", "cd", "continue",
2788 "eval", "exec", "exit", "export", "for", "if",
2789 "login", "logout", "read", "readonly", "set",
2790 "shift", "switch", "test", "times", "trap",
2791 "umask", "wait", "while", 0 };
2797 int instring
, word_has_equals
, seen_nonequals
, last_argument_was_empty
;
2798 char **new_argv
= 0;
2802 if (no_default_sh_exe
) {
2803 sh_cmds
= sh_cmds_dos
;
2804 sh_chars
= sh_chars_dos
;
2806 sh_cmds
= sh_cmds_sh
;
2807 sh_chars
= sh_chars_sh
;
2809 #endif /* WINDOWS32 */
2814 /* Make sure not to bother processing an empty line. */
2815 while (isblank ((unsigned char)*line
))
2820 /* See if it is safe to parse commands internally. */
2822 shell
= default_shell
;
2824 else if (strcmp (shell
, default_shell
))
2826 char *s1
= _fullpath(NULL
, shell
, 0);
2827 char *s2
= _fullpath(NULL
, default_shell
, 0);
2829 slow_flag
= strcmp((s1
? s1
: ""), (s2
? s2
: ""));
2838 #else /* not WINDOWS32 */
2839 #if defined (__MSDOS__) || defined (__EMX__)
2840 else if (stricmp (shell
, default_shell
))
2842 extern int _is_unixy_shell (const char *_path
);
2844 DB (DB_BASIC
, (_("$SHELL changed (was `%s', now `%s')\n"),
2845 default_shell
, shell
));
2846 unixy_shell
= _is_unixy_shell (shell
);
2847 /* we must allocate a copy of shell: construct_command_argv() will free
2848 * shell after this function returns. */
2849 default_shell
= xstrdup (shell
);
2853 sh_chars
= sh_chars_sh
;
2854 sh_cmds
= sh_cmds_sh
;
2858 sh_chars
= sh_chars_dos
;
2859 sh_cmds
= sh_cmds_dos
;
2861 if (_osmode
== OS2_MODE
)
2863 sh_chars
= sh_chars_os2
;
2864 sh_cmds
= sh_cmds_os2
;
2868 #else /* !__MSDOS__ */
2869 else if (strcmp (shell
, default_shell
))
2871 #endif /* !__MSDOS__ && !__EMX__ */
2872 #endif /* not WINDOWS32 */
2875 for (ap
= ifs
; *ap
!= '\0'; ++ap
)
2876 if (*ap
!= ' ' && *ap
!= '\t' && *ap
!= '\n')
2879 i
= strlen (line
) + 1;
2881 /* More than 1 arg per character is impossible. */
2882 new_argv
= (char **) xmalloc (i
* sizeof (char *));
2884 /* All the args can fit in a buffer as big as LINE is. */
2885 ap
= new_argv
[0] = (char *) xmalloc (i
);
2888 /* I is how many complete arguments have been found. */
2890 instring
= word_has_equals
= seen_nonequals
= last_argument_was_empty
= 0;
2891 for (p
= line
; *p
!= '\0'; ++p
)
2899 /* Inside a string, just copy any char except a closing quote
2900 or a backslash-newline combination. */
2904 if (ap
== new_argv
[0] || *(ap
-1) == '\0')
2905 last_argument_was_empty
= 1;
2907 else if (*p
== '\\' && p
[1] == '\n')
2908 goto swallow_escaped_newline
;
2909 else if (*p
== '\n' && restp
!= NULL
)
2911 /* End of the command line. */
2915 /* Backslash, $, and ` are special inside double quotes.
2916 If we see any of those, punt.
2917 But on MSDOS, if we use COMMAND.COM, double and single
2918 quotes have the same effect. */
2919 else if (instring
== '"' && strchr ("\\$`", *p
) != 0 && unixy_shell
)
2924 else if (strchr (sh_chars
, *p
) != 0)
2925 /* Not inside a string, but it's a special char. */
2928 else if (*p
== '.' && p
[1] == '.' && p
[2] == '.' && p
[3] != '.')
2929 /* `...' is a wildcard in DJGPP. */
2933 /* Not a special char. */
2937 /* Equals is a special character in leading words before the
2938 first word with no equals sign in it. This is not the case
2939 with sh -k, but we never get here when using nonstandard
2941 if (! seen_nonequals
&& unixy_shell
)
2943 word_has_equals
= 1;
2948 /* Backslash-newline combinations are eaten. */
2951 swallow_escaped_newline
:
2953 /* Eat the backslash, the newline, and following whitespace,
2954 replacing it all with a single space. */
2957 /* If there is a tab after a backslash-newline,
2958 remove it from the source line which will be echoed,
2959 since it was most likely used to line
2960 up the continued line with the previous one. */
2962 /* Note these overlap and strcpy() is undefined for
2963 overlapping objects in ANSI C. The strlen() _IS_ right,
2964 since we need to copy the nul byte too. */
2965 bcopy (p
+ 1, p
, strlen (p
));
2971 if (ap
!= new_argv
[i
])
2972 /* Treat this as a space, ending the arg.
2973 But if it's at the beginning of the arg, it should
2974 just get eaten, rather than becoming an empty arg. */
2977 p
= next_token (p
) - 1;
2980 else if (p
[1] != '\0')
2982 #ifdef HAVE_DOS_PATHS
2983 /* Only remove backslashes before characters special
2984 to Unixy shells. All other backslashes are copied
2985 verbatim, since they are probably DOS-style
2986 directory separators. This still leaves a small
2987 window for problems, but at least it should work
2988 for the vast majority of naive users. */
2991 /* A dot is only special as part of the "..."
2993 if (strneq (p
+ 1, ".\\.\\.", 5))
3001 if (p
[1] != '\\' && p
[1] != '\''
3002 && !isspace ((unsigned char)p
[1])
3003 && (strchr (sh_chars_sh
, p
[1]) == 0))
3004 /* back up one notch, to copy the backslash */
3006 #endif /* HAVE_DOS_PATHS */
3008 /* Copy and skip the following char. */
3021 /* End of the command line. */
3026 /* Newlines are not special. */
3033 /* We have the end of an argument.
3034 Terminate the text of the argument. */
3037 last_argument_was_empty
= 0;
3039 /* Update SEEN_NONEQUALS, which tells us if every word
3040 heretofore has contained an `='. */
3041 seen_nonequals
|= ! word_has_equals
;
3042 if (word_has_equals
&& ! seen_nonequals
)
3043 /* An `=' in a word before the first
3044 word without one is magical. */
3046 word_has_equals
= 0; /* Prepare for the next word. */
3048 /* If this argument is the command name,
3049 see if it is a built-in shell command.
3050 If so, have the shell handle it. */
3054 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
3056 if (streq (sh_cmds
[j
], new_argv
[0]))
3059 /* Non-Unix shells are case insensitive. */
3061 && strcasecmp (sh_cmds
[j
], new_argv
[0]) == 0)
3067 /* Ignore multiple whitespace chars. */
3069 /* Next iteration should examine the first nonwhite char. */
3081 /* Let the shell deal with an unterminated quote. */
3084 /* Terminate the last argument and the argument list. */
3087 if (new_argv
[i
][0] != '\0' || last_argument_was_empty
)
3094 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
3095 if (streq (sh_cmds
[j
], new_argv
[0]))
3099 if (new_argv
[0] == 0)
3100 /* Line was empty. */
3106 /* We must use the shell. */
3110 /* Free the old argument list we were working on. */
3112 free ((void *)new_argv
);
3116 execute_by_shell
= 1; /* actually, call `system' if shell isn't unixy */
3125 buffer
= (char *)xmalloc (strlen (line
)+1);
3128 for (dptr
=buffer
; *ptr
; )
3130 if (*ptr
== '\\' && ptr
[1] == '\n')
3132 else if (*ptr
== '@') /* Kludge: multiline commands */
3142 new_argv
= (char **) xmalloc (2 * sizeof (char *));
3143 new_argv
[0] = buffer
;
3146 #else /* Not Amiga */
3149 * Not eating this whitespace caused things like
3153 * which gave the shell fits. I think we have to eat
3154 * whitespace here, but this code should be considered
3155 * suspicious if things start failing....
3158 /* Make sure not to bother processing an empty line. */
3159 while (isspace ((unsigned char)*line
))
3163 #endif /* WINDOWS32 */
3165 /* SHELL may be a multi-word command. Construct a command line
3166 "SHELL -c LINE", with all special chars in LINE escaped.
3167 Then recurse, expanding this command line to get the final
3170 unsigned int shell_len
= strlen (shell
);
3172 static char minus_c
[] = " -c ";
3174 static char minus_c
[] = "";
3176 unsigned int line_len
= strlen (line
);
3178 char *new_line
= (char *) alloca (shell_len
+ (sizeof (minus_c
) - 1)
3179 + (line_len
* 2) + 1);
3180 char *command_ptr
= NULL
; /* used for batch_mode_shell mode */
3182 # ifdef __EMX__ /* is this necessary? */
3184 minus_c
[1] = '/'; /* " /c " */
3188 bcopy (shell
, ap
, shell_len
);
3190 bcopy (minus_c
, ap
, sizeof (minus_c
) - 1);
3191 ap
+= sizeof (minus_c
) - 1;
3193 for (p
= line
; *p
!= '\0'; ++p
)
3195 if (restp
!= NULL
&& *p
== '\n')
3200 else if (*p
== '\\' && p
[1] == '\n')
3202 /* Eat the backslash, the newline, and following whitespace,
3203 replacing it all with a single space (which is escaped
3207 /* If there is a tab after a backslash-newline,
3208 remove it from the source line which will be echoed,
3209 since it was most likely used to line
3210 up the continued line with the previous one. */
3212 bcopy (p
+ 1, p
, strlen (p
));
3216 if (unixy_shell
&& !batch_mode_shell
)
3222 /* DOS shells don't know about backslash-escaping. */
3223 if (unixy_shell
&& !batch_mode_shell
&&
3224 (*p
== '\\' || *p
== '\'' || *p
== '"'
3225 || isspace ((unsigned char)*p
)
3226 || strchr (sh_chars
, *p
) != 0))
3229 else if (unixy_shell
&& strneq (p
, "...", 3))
3231 /* The case of `...' wildcard again. */
3232 strcpy (ap
, "\\.\\.\\");
3239 if (ap
== new_line
+ shell_len
+ sizeof (minus_c
) - 1)
3240 /* Line was empty. */
3245 /* Some shells do not work well when invoked as 'sh -c xxx' to run a
3246 command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
3247 cases, run commands via a script file. */
3248 if ((no_default_sh_exe
|| batch_mode_shell
) && batch_filename_ptr
) {
3250 int id
= GetCurrentProcessId();
3254 /* create a file name */
3255 sprintf(fbuf
, "make%d", id
);
3256 fname
= tempnam(".", fbuf
);
3258 /* create batch file name */
3259 *batch_filename_ptr
= xmalloc(strlen(fname
) + 5);
3260 strcpy(*batch_filename_ptr
, fname
);
3262 /* make sure path name is in DOS backslash format */
3264 fname
= *batch_filename_ptr
;
3265 for (i
= 0; fname
[i
] != '\0'; ++i
)
3266 if (fname
[i
] == '/')
3268 strcat(*batch_filename_ptr
, ".bat");
3270 strcat(*batch_filename_ptr
, ".sh");
3273 DB (DB_JOBS
, (_("Creating temporary batch file %s\n"),
3274 *batch_filename_ptr
));
3276 /* create batch file to execute command */
3277 batch
= fopen (*batch_filename_ptr
, "w");
3279 fputs ("@echo off\n", batch
);
3280 fputs (command_ptr
, batch
);
3281 fputc ('\n', batch
);
3285 new_argv
= (char **) xmalloc(3 * sizeof (char *));
3287 new_argv
[0] = xstrdup (shell
);
3288 new_argv
[1] = *batch_filename_ptr
; /* only argv[0] gets freed later */
3290 new_argv
[0] = xstrdup (*batch_filename_ptr
);
3295 #endif /* WINDOWS32 */
3297 new_argv
= construct_command_argv_internal (new_line
, (char **) NULL
,
3298 (char *) 0, (char *) 0,
3301 else if (!unixy_shell
)
3303 /* new_line is local, must not be freed therefore
3304 We use line here instead of new_line because we run the shell
3306 size_t line_len
= strlen (line
);
3309 memcpy (new_line
, line
, line_len
+ 1);
3310 /* replace all backslash-newline combination and also following tabs */
3313 if (q
[0] == '\\' && q
[1] == '\n')
3315 q
+= 2; /* remove '\\' and '\n' */
3317 q
++; /* remove 1st tab in the next line */
3324 # ifndef NO_CMD_DEFAULT
3325 if (strnicmp (new_line
, "echo", 4) == 0
3326 && (new_line
[4] == ' ' || new_line
[4] == '\t'))
3328 /* the builtin echo command: handle it separately */
3329 size_t echo_len
= line_len
- 5;
3330 char *echo_line
= new_line
+ 5;
3332 /* special case: echo 'x="y"'
3333 cmd works this way: a string is printed as is, i.e., no quotes
3334 are removed. But autoconf uses a command like echo 'x="y"' to
3335 determine whether make works. autoconf expects the output x="y"
3336 so we will do exactly that.
3337 Note: if we do not allow cmd to be the default shell
3338 we do not need this kind of voodoo */
3339 if (echo_line
[0] == '\''
3340 && echo_line
[echo_len
- 1] == '\''
3341 && strncmp (echo_line
+ 1, "ac_maketemp=",
3342 strlen ("ac_maketemp=")) == 0)
3344 /* remove the enclosing quotes */
3345 memmove (echo_line
, echo_line
+ 1, echo_len
- 2);
3346 echo_line
[echo_len
- 2] = '\0';
3352 /* Let the shell decide what to do. Put the command line into the
3353 2nd command line argument and hope for the best ;-) */
3354 size_t sh_len
= strlen (shell
);
3356 /* exactly 3 arguments + NULL */
3357 new_argv
= (char **) xmalloc (4 * sizeof (char *));
3358 /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
3359 the trailing '\0' */
3360 new_argv
[0] = (char *) malloc (sh_len
+ line_len
+ 5);
3361 memcpy (new_argv
[0], shell
, sh_len
+ 1);
3362 new_argv
[1] = new_argv
[0] + sh_len
+ 1;
3363 memcpy (new_argv
[1], "/c", 3);
3364 new_argv
[2] = new_argv
[1] + 3;
3365 memcpy (new_argv
[2], new_line
, line_len
+ 1);
3369 #elif defined(__MSDOS__)
3372 /* With MSDOS shells, we must construct the command line here
3373 instead of recursively calling ourselves, because we
3374 cannot backslash-escape the special characters (see above). */
3375 new_argv
= (char **) xmalloc (sizeof (char *));
3376 line_len
= strlen (new_line
) - shell_len
- sizeof (minus_c
) + 1;
3377 new_argv
[0] = xmalloc (line_len
+ 1);
3378 strncpy (new_argv
[0],
3379 new_line
+ shell_len
+ sizeof (minus_c
) - 1, line_len
);
3380 new_argv
[0][line_len
] = '\0';
3384 fatal (NILF
, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
3385 __FILE__
, __LINE__
);
3388 #endif /* ! AMIGA */
3394 /* Figure out the argument list necessary to run LINE as a command. Try to
3395 avoid using a shell. This routine handles only ' quoting, and " quoting
3396 when no backslash, $ or ` characters are seen in the quotes. Starting
3397 quotes may be escaped with a backslash. If any of the characters in
3398 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
3399 is the first word of a line, the shell is used.
3401 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
3402 If *RESTP is NULL, newlines will be ignored.
3404 FILE is the target whose commands these are. It is used for
3405 variable expansion for $(SHELL) and $(IFS). */
3408 construct_command_argv (char *line
, char **restp
, struct file
*file
,
3409 char **batch_filename_ptr
)
3423 && (isspace ((unsigned char)*cptr
)))
3428 && (!isspace((unsigned char)*cptr
)))
3433 argv
= (char **)malloc (argc
* sizeof (char *));
3442 && (isspace ((unsigned char)*cptr
)))
3446 DB (DB_JOBS
, ("argv[%d] = [%s]\n", argc
, cptr
));
3447 argv
[argc
++] = cptr
;
3449 && (!isspace((unsigned char)*cptr
)))
3456 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
3457 int save
= warn_undefined_variables_flag
;
3458 warn_undefined_variables_flag
= 0;
3460 shell
= allocated_variable_expand_for_file ("$(SHELL)", file
);
3463 * Convert to forward slashes so that construct_command_argv_internal()
3467 char *p
= w32ify (shell
, 0);
3473 static const char *unixroot
= NULL
;
3474 static const char *last_shell
= "";
3475 static int init
= 0;
3478 unixroot
= getenv ("UNIXROOT");
3479 /* unixroot must be NULL or not empty */
3480 if (unixroot
&& unixroot
[0] == '\0') unixroot
= NULL
;
3484 /* if we have an unixroot drive and if shell is not default_shell
3485 (which means it's either cmd.exe or the test has already been
3486 performed) and if shell is an absolute path without drive letter,
3487 try whether it exists e.g.: if "/bin/sh" does not exist use
3488 "$UNIXROOT/bin/sh" instead. */
3489 if (unixroot
&& shell
&& strcmp (shell
, last_shell
) != 0
3490 && (shell
[0] == '/' || shell
[0] == '\\'))
3492 /* trying a new shell, check whether it exists */
3493 size_t size
= strlen (shell
);
3494 char *buf
= xmalloc (size
+ 7);
3495 memcpy (buf
, shell
, size
);
3496 memcpy (buf
+ size
, ".exe", 5); /* including the trailing '\0' */
3497 if (access (shell
, F_OK
) != 0 && access (buf
, F_OK
) != 0)
3499 /* try the same for the unixroot drive */
3500 memmove (buf
+ 2, buf
, size
+ 5);
3501 buf
[0] = unixroot
[0];
3502 buf
[1] = unixroot
[1];
3503 if (access (buf
, F_OK
) == 0)
3504 /* we have found a shell! */
3514 #endif /* __EMX__ */
3516 ifs
= allocated_variable_expand_for_file ("$(IFS)", file
);
3518 warn_undefined_variables_flag
= save
;
3521 argv
= construct_command_argv_internal (line
, restp
, shell
, ifs
, batch_filename_ptr
);
3529 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
3531 dup2 (int old
, int new)
3546 #endif /* !HAPE_DUP2 && !_AMIGA */