1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 /* Default shell to use. */
28 char *default_shell
= "sh.exe";
29 int no_default_sh_exe
= 1;
32 char default_shell
[] = "";
33 extern int MyExecute (char **);
36 /* The default shell is a pointer so we can change it if Makefile
37 says so. It is without an explicit path so we get a chance
38 to search the $PATH for it (since MSDOS doesn't have standard
39 directories we could trust). */
40 char *default_shell
= "command.com";
42 char default_shell
[] = "/bin/sh";
43 #endif /* __MSDOS__ */
45 #endif /* WINDOWS32 */
49 static int execute_by_shell
;
50 static int dos_pid
= 123;
52 int dos_command_running
;
53 #endif /* __MSDOS__ */
56 #include <proto/dos.h>
57 static int amiga_pid
= 123;
58 static int amiga_status
;
59 static char amiga_bname
[32];
60 static int amiga_batch_file
;
65 #include <processes.h>
67 #include <lib$routines.h>
76 #include "pathstuff.h"
78 /* this stuff used if no sh.exe is around */
79 static char *dos_bname
;
80 static char *dos_bename
;
81 static int dos_batch_file
;
82 #endif /* WINDOWS32 */
90 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
95 #define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
96 #else /* Don't have waitpid. */
101 #define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
102 #endif /* Have wait3. */
103 #endif /* Have waitpid. */
105 #if !defined (wait) && !defined (POSIX)
109 #ifndef HAVE_UNION_WAIT
114 #define WTERMSIG(x) ((x) & 0x7f)
117 #define WCOREDUMP(x) ((x) & 0x80)
120 #define WEXITSTATUS(x) (((x) >> 8) & 0xff)
123 #define WIFSIGNALED(x) (WTERMSIG (x) != 0)
126 #define WIFEXITED(x) (WTERMSIG (x) == 0)
129 #else /* Have `union wait'. */
131 #define WAIT_T union wait
133 #define WTERMSIG(x) ((x).w_termsig)
136 #define WCOREDUMP(x) ((x).w_coredump)
139 #define WEXITSTATUS(x) ((x).w_retcode)
142 #define WIFSIGNALED(x) (WTERMSIG(x) != 0)
145 #define WIFEXITED(x) (WTERMSIG(x) == 0)
148 #endif /* Don't have `union wait'. */
151 static int vms_jobsefnmask
=0;
154 #ifndef HAVE_UNISTD_H
156 extern int execve ();
157 extern void _exit ();
159 extern int geteuid ();
160 extern int getegid ();
161 extern int setgid ();
162 extern int getgid ();
166 extern char *allocated_variable_expand_for_file
PARAMS ((char *line
, struct file
*file
));
168 extern int getloadavg
PARAMS ((double loadavg
[], int nelem
));
169 extern int start_remote_job
PARAMS ((char **argv
, char **envp
, int stdin_fd
,
170 int *is_remote
, int *id_ptr
, int *used_stdin
));
171 extern int start_remote_job_p
PARAMS ((void));
172 extern int remote_status
PARAMS ((int *exit_code_ptr
, int *signal_ptr
,
173 int *coredump_ptr
, int block
));
175 RETSIGTYPE child_handler
PARAMS ((int));
176 static void free_child
PARAMS ((struct child
*));
177 static void start_job_command
PARAMS ((struct child
*child
));
178 static int load_too_high
PARAMS ((void));
179 static int job_next_command
PARAMS ((struct child
*));
180 static int start_waiting_job
PARAMS ((struct child
*));
182 static void vmsWaitForChildren
PARAMS ((int *));
185 /* Chain of all live (or recently deceased) children. */
187 struct child
*children
= 0;
189 /* Number of children currently running. */
191 unsigned int job_slots_used
= 0;
193 /* Nonzero if the `good' standard input is in use. */
195 static int good_stdin_used
= 0;
197 /* Chain of children waiting to run until the load average goes down. */
199 static struct child
*waiting_jobs
= 0;
201 /* Non-zero if we use a *real* shell (always so on Unix). */
207 * The macro which references this function is defined in make.h.
209 int w32_kill(int pid
, int sig
)
211 return ((process_kill(pid
, sig
) == TRUE
) ? 0 : -1);
213 #endif /* WINDOWS32 */
215 /* Write an error message describing the exit status given in
216 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
217 Append "(ignored)" if IGNORED is nonzero. */
220 child_error (target_name
, exit_code
, exit_sig
, coredump
, ignored
)
222 int exit_code
, exit_sig
, coredump
;
225 if (ignored
&& silent_flag
)
229 if (!(exit_code
& 1))
230 error("*** [%s] Error 0x%x%s", target_name
, exit_code
, ((ignored
)? " (ignored)" : ""));
233 error (ignored
? "[%s] Error %d (ignored)" :
235 target_name
, exit_code
);
237 error ("*** [%s] %s%s",
238 target_name
, strsignal (exit_sig
),
239 coredump
? " (core dumped)" : "");
243 static unsigned int dead_children
= 0;
246 /* Wait for nchildren children to terminate */
248 vmsWaitForChildren(int *status
)
252 if (!vms_jobsefnmask
)
258 *status
= sys$
wflor (32, vms_jobsefnmask
);
265 /* Notice that a child died.
266 reap_children should be called when convenient. */
274 printf ("Got a SIGCHLD; %d unreaped children.\n", dead_children
);
277 extern int shell_function_pid
, shell_function_completed
;
279 /* Reap dead children, storing the returned status and the new command
280 state (`cs_finished') in the `file' member of the `struct child' for the
281 dead child, and removing the child from the chain. If BLOCK nonzero,
282 reap at least one child, waiting for it to die if necessary. If ERR is
283 nonzero, print an error message first. */
286 reap_children (block
, err
)
291 while ((children
!= 0 || shell_function_pid
!= 0) &&
292 (block
|| dead_children
> 0))
296 int exit_code
, exit_sig
, coredump
;
297 register struct child
*lastc
, *c
;
299 int any_remote
, any_local
;
301 if (err
&& dead_children
== 0)
303 /* We might block for a while, so let the user know why. */
305 error ("*** Waiting for unfinished jobs....");
308 /* We have one less dead child to reap.
309 The test and decrement are not atomic; if it is compiled into:
310 register = dead_children - 1;
311 dead_children = register;
312 a SIGCHLD could come between the two instructions.
313 child_handler increments dead_children.
314 The second instruction here would lose that increment. But the
315 only effect of dead_children being wrong is that we might wait
316 longer than necessary to reap a child, and lose some parallelism;
317 and we might print the "Waiting for unfinished jobs" message above
318 when not necessary. */
320 if (dead_children
> 0)
324 any_local
= shell_function_pid
!= 0;
325 for (c
= children
; c
!= 0; c
= c
->next
)
327 any_remote
|= c
->remote
;
328 any_local
|= ! c
->remote
;
330 printf ("Live child 0x%08lx PID %d%s\n",
331 (unsigned long int) c
,
332 c
->pid
, c
->remote
? " (remote)" : "");
338 /* First, check for remote children. */
340 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 0);
351 pfatal_with_name ("remote_status");
355 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
356 /* No remote children. Check for local children. */
361 vmsWaitForChildren (&status
);
366 pid
= WAIT_NOHANG (&status
);
369 pid
= wait (&status
);
381 pfatal_with_name ("wait");
385 /* No local children. */
386 if (block
&& any_remote
)
388 /* Now try a blocking wait for a remote child. */
389 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 1);
391 goto remote_status_lose
;
393 /* No remote children either. Finally give up. */
396 /* We got a remote child. */
404 /* Chop the status word up. */
405 exit_code
= WEXITSTATUS (status
);
406 exit_sig
= WIFSIGNALED (status
) ? WTERMSIG (status
) : 0;
407 coredump
= WCOREDUMP (status
);
409 #else /* __MSDOS__, Amiga, WINDOWS32. */
411 /* Life is very different on MSDOS. */
414 exit_code
= WEXITSTATUS (status
);
415 if (exit_code
== 0xff)
417 exit_sig
= WIFSIGNALED (status
) ? WTERMSIG (status
) : 0;
419 #endif /* __MSDOS__ */
423 status
= amiga_status
;
424 exit_code
= amiga_status
;
433 /* wait for anything to finish */
434 if (hPID
= process_wait_for_any()) {
436 /* was an error found on this process? */
437 err
= process_last_err(hPID
);
440 exit_code
= process_exit_code(hPID
);
443 fprintf(stderr
, "make (e=%d): %s",
444 exit_code
, map_windows32_error_to_string(exit_code
));
446 exit_sig
= process_signal(hPID
);
448 /* cleanup process */
449 process_cleanup(hPID
);
451 if (dos_batch_file
) {
461 #endif /* WINDOWS32 */
462 #endif /* Not __MSDOS__ */
465 /* We got a remote child. */
468 /* Check if this is the child of the `shell' function. */
469 if (!remote
&& pid
== shell_function_pid
)
471 /* It is. Leave an indicator for the `shell' function. */
472 if (exit_sig
== 0 && exit_code
== 127)
473 shell_function_completed
= -1;
475 shell_function_completed
= 1;
479 child_failed
= exit_sig
!= 0 || exit_code
!= 0;
481 /* Search for a child matching the deceased one. */
483 for (c
= children
; c
!= 0; lastc
= c
, c
= c
->next
)
484 if (c
->remote
== remote
&& c
->pid
== pid
)
489 /* An unknown child died. */
491 sprintf (buf
, "Unknown%s job %d", remote
? " remote" : "", pid
);
493 child_error (buf
, exit_code
, exit_sig
, coredump
,
496 error ("%s finished.", buf
);
501 printf ("Reaping %s child 0x%08lx PID %d%s\n",
502 child_failed
? "losing" : "winning",
503 (unsigned long int) c
,
504 c
->pid
, c
->remote
? " (remote)" : "");
506 /* If this child had the good stdin, say it is now free. */
510 if (child_failed
&& !c
->noerror
&& !ignore_errors_flag
)
512 /* The commands failed. Write an error message,
513 delete non-precious targets, and abort. */
514 static int delete_on_error
= -1;
515 child_error (c
->file
->name
, exit_code
, exit_sig
, coredump
, 0);
516 c
->file
->update_status
= 2;
517 if (delete_on_error
== -1)
519 struct file
*f
= lookup_file (".DELETE_ON_ERROR");
520 delete_on_error
= f
!= 0 && f
->is_target
;
522 if (exit_sig
!= 0 || delete_on_error
)
523 delete_child_targets (c
);
529 /* The commands failed, but we don't care. */
530 child_error (c
->file
->name
,
531 exit_code
, exit_sig
, coredump
, 1);
535 /* If there are more commands to run, try to start them. */
536 if (job_next_command (c
))
538 if (handling_fatal_signal
)
540 /* Never start new commands while we are dying.
541 Since there are more commands that wanted to be run,
542 the target was not completely remade. So we treat
543 this as if a command had failed. */
544 c
->file
->update_status
= 2;
548 /* Check again whether to start remotely.
549 Whether or not we want to changes over time.
550 Also, start_remote_job may need state set up
551 by start_remote_job_p. */
552 c
->remote
= start_remote_job_p ();
553 start_job_command (c
);
554 /* Fatal signals are left blocked in case we were
555 about to put that child on the chain. But it is
556 already there, so it is safe for a fatal signal to
557 arrive now; it will clean up this child's targets. */
559 if (c
->file
->command_state
== cs_running
)
560 /* We successfully started the new command.
561 Loop to reap more children. */
565 if (c
->file
->update_status
!= 0)
566 /* We failed to start the commands. */
567 delete_child_targets (c
);
570 /* There are no more commands. We got through them all
571 without an unignored error. Now the target has been
572 successfully updated. */
573 c
->file
->update_status
= 0;
576 /* When we get here, all the commands for C->file are finished
577 (or aborted) and C->file->update_status contains 0 or 2. But
578 C->file->command_state is still cs_running if all the commands
579 ran; notice_finish_file looks for cs_running to tell it that
580 it's interesting to check the file's modtime again now. */
582 if (! handling_fatal_signal
)
583 /* Notice if the target of the commands has been changed.
584 This also propagates its values for command_state and
585 update_status to its also_make files. */
586 notice_finished_file (c
->file
);
589 printf ("Removing child 0x%08lx PID %d%s from chain.\n",
590 (unsigned long int) c
,
591 c
->pid
, c
->remote
? " (remote)" : "");
593 /* Block fatal signals while frobnicating the list, so that
594 children and job_slots_used are always consistent. Otherwise
595 a fatal signal arriving after the child is off the chain and
596 before job_slots_used is decremented would believe a child was
597 live and call reap_children again. */
600 /* Remove the child from the chain and free it. */
604 lastc
->next
= c
->next
;
605 if (! handling_fatal_signal
) /* Don't bother if about to die. */
608 /* There is now another slot open. */
609 if (job_slots_used
> 0)
614 /* If the job failed, and the -k flag was not given, die,
615 unless we are already in the process of dying. */
616 if (!err
&& child_failed
&& !keep_going_flag
&&
617 /* fatal_error_signal will die with the right signal. */
618 !handling_fatal_signal
)
622 /* Only block for one child. */
628 /* Free the storage allocated for CHILD. */
632 register struct child
*child
;
634 if (child
->command_lines
!= 0)
636 register unsigned int i
;
637 for (i
= 0; i
< child
->file
->cmds
->ncommand_lines
; ++i
)
638 free (child
->command_lines
[i
]);
639 free ((char *) child
->command_lines
);
642 if (child
->environment
!= 0)
644 register char **ep
= child
->environment
;
647 free ((char *) child
->environment
);
650 free ((char *) child
);
654 extern sigset_t fatal_signal_set
;
661 (void) sigprocmask (SIG_BLOCK
, &fatal_signal_set
, (sigset_t
*) 0);
663 #ifdef HAVE_SIGSETMASK
664 (void) sigblock (fatal_signal_mask
);
674 sigemptyset (&empty
);
675 sigprocmask (SIG_SETMASK
, &empty
, (sigset_t
*) 0);
679 /* Start a job to run the commands specified in CHILD.
680 CHILD is updated to reflect the commands and ID of the child process.
682 NOTE: On return fatal signals are blocked! The caller is responsible
683 for calling `unblock_sigs', once the new child is safely on the chain so
684 it can be cleaned up in the event of a fatal signal. */
687 start_job_command (child
)
688 register struct child
*child
;
691 static int bad_stdin
= -1;
701 /* Combine the flags parsed for the line itself with
702 the flags specified globally for this target. */
703 flags
= (child
->file
->command_flags
704 | child
->file
->cmds
->lines_flags
[child
->command_line
- 1]);
706 p
= child
->command_ptr
;
707 child
->noerror
= flags
& COMMANDS_NOERROR
;
712 flags
|= COMMANDS_SILENT
;
714 flags
|= COMMANDS_RECURSE
;
717 else if (!isblank (*p
) && *p
!= '+')
722 /* If -q was given, just say that updating `failed'. The exit status of
723 1 tells the user that -q is saying `something to do'; the exit status
724 for a random error is 2. */
725 if (question_flag
&& !(flags
& COMMANDS_RECURSE
))
727 child
->file
->update_status
= 1;
728 notice_finished_file (child
->file
);
732 /* There may be some preceding whitespace left if there
733 was nothing but a backslash on the first line. */
736 /* Figure out an argument list from this command line. */
743 argv
= construct_command_argv (p
, &end
, child
->file
);
746 child
->command_ptr
= NULL
;
750 child
->command_ptr
= end
;
754 if (touch_flag
&& !(flags
& COMMANDS_RECURSE
))
756 /* Go on to the next command. It might be the recursive one.
757 We construct ARGV only to find the end of the command line. */
760 free ((char *) argv
);
768 /* This line has no commands. Go to the next. */
769 if (job_next_command (child
))
770 start_job_command (child
);
773 /* No more commands. All done. */
774 child
->file
->update_status
= 0;
775 notice_finished_file (child
->file
);
780 /* Print out the command. If silent, we call `message' with null so it
781 can log the working directory before the command's own error messages
784 message (0, (just_print_flag
|| (!(flags
& COMMANDS_SILENT
) && !silent_flag
))
785 ? "%s" : (char *) 0, p
);
787 /* Optimize an empty command. People use this for timestamp rules,
788 and forking a useless shell all the time leads to inefficiency. */
790 #if !defined(VMS) && !defined(_AMIGA)
793 unixy_shell
/* the test is complicated and we already did it */
795 (argv
[0] && !strcmp(argv
[0], "/bin/sh"))
797 && (argv
[1] && !strcmp(argv
[1], "-c"))
798 && (argv
[2] && !strcmp(argv
[2], ":"))
801 set_command_state (child
->file
, cs_running
);
804 #endif /* !VMS && !_AMIGA */
806 /* Tell update_goal_chain that a command has been started on behalf of
807 this target. It is important that this happens here and not in
808 reap_children (where we used to do it), because reap_children might be
809 reaping children from a different target. We want this increment to
810 guaranteedly indicate that a command was started for the dependency
811 chain (i.e., update_file recursion chain) we are processing. */
815 /* If -n was given, recurse to get the next line in the sequence. */
817 if (just_print_flag
&& !(flags
& COMMANDS_RECURSE
))
821 free ((char *) argv
);
826 /* Flush the output streams so they won't have things written twice. */
832 #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
834 /* Set up a bad standard input that reads from a broken pipe. */
838 /* Make a file descriptor that is the read end of a broken pipe.
839 This will be used for some children's standard inputs. */
843 /* Close the write side. */
844 (void) close (pd
[1]);
845 /* Save the read side. */
848 /* Set the descriptor to close on exec, so it does not litter any
849 child's descriptor table. When it is dup2'd onto descriptor 0,
850 that descriptor will not close on exec. */
855 (void) fcntl (bad_stdin
, F_SETFD
, FD_CLOEXEC
);
860 #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
862 /* Decide whether to give this child the `good' standard input
863 (one that points to the terminal or whatever), or the `bad' one
864 that points to the read side of a broken pipe. */
866 child
->good_stdin
= !good_stdin_used
;
867 if (child
->good_stdin
)
875 /* Set up the environment for the child. */
876 if (child
->environment
== 0)
877 child
->environment
= target_environment (child
->file
);
880 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
883 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
886 int is_remote
, id
, used_stdin
;
887 if (start_remote_job (argv
, child
->environment
,
888 child
->good_stdin
? 0 : bad_stdin
,
889 &is_remote
, &id
, &used_stdin
))
893 if (child
->good_stdin
&& !used_stdin
)
895 child
->good_stdin
= 0;
898 child
->remote
= is_remote
;
905 /* Fork the child process. */
907 char **parent_environ
;
915 if (!child_execute_job (argv
, child
)) {
917 perror_with_name ("vfork", "");
923 parent_environ
= environ
;
924 child
->pid
= vfork ();
925 environ
= parent_environ
; /* Restore value child may have clobbered. */
928 /* We are the child side. */
930 child_execute_job (child
->good_stdin
? 0 : bad_stdin
, 1,
931 argv
, child
->environment
);
933 else if (child
->pid
< 0)
937 perror_with_name ("vfork", "");
943 #else /* __MSDOS__ or Amiga or WINDOWS32 */
951 /* We call `system' to do the job of the SHELL, since stock DOS
952 shell is too dumb. Our `system' knows how to handle long
953 command lines even if pipes/redirection is needed; it will only
954 call COMMAND.COM when its internal commands are used. */
955 if (execute_by_shell
)
957 char *cmdline
= argv
[0];
958 /* We don't have a way to pass environment to `system',
959 so we need to save and restore ours, sigh... */
960 char **parent_environ
= environ
;
962 environ
= child
->environment
;
964 /* If we have a *real* shell, tell `system' to call
965 it to do everything for us. */
968 /* A *real* shell on MSDOS may not support long
969 command lines the DJGPP way, so we must use `system'. */
970 cmdline
= argv
[2]; /* get past "shell -c" */
973 dos_command_running
= 1;
974 proc_return
= system (cmdline
);
975 dos_command_running
= 0;
976 environ
= parent_environ
;
977 execute_by_shell
= 0; /* for the next time */
981 dos_command_running
= 1;
982 proc_return
= spawnvpe (P_WAIT
, argv
[0], argv
, child
->environment
);
983 dos_command_running
= 0;
986 if (proc_return
== -1)
989 dos_status
|= (proc_return
& 0xff);
991 child
->pid
= dos_pid
++;
993 #endif /* __MSDOS__ */
995 amiga_status
= MyExecute (argv
);
998 child
->pid
= amiga_pid
++;
999 if (amiga_batch_file
)
1001 amiga_batch_file
= 0;
1002 DeleteFile (amiga_bname
); /* Ignore errors. */
1010 /* make UNC paths safe for CreateProcess -- backslash format */
1012 if (arg0
&& arg0
[0] == '/' && arg0
[1] == '/')
1013 for ( ; arg0
&& *arg0
; arg0
++)
1017 /* make sure CreateProcess() has Path it needs */
1018 sync_Path_environment();
1020 hPID
= process_easy(argv
, child
->environment
);
1022 if (hPID
!= INVALID_HANDLE_VALUE
)
1023 child
->pid
= (int) hPID
;
1028 "process_easy() failed failed to launch process (e=%d)\n",
1029 process_last_err(hPID
));
1030 for (i
= 0; argv
[i
]; i
++)
1031 fprintf(stderr
, "%s ", argv
[i
]);
1032 fprintf(stderr
, "\nCounted %d args in failed launch\n", i
);
1035 #endif /* WINDOWS32 */
1036 #endif /* __MSDOS__ or Amiga or WINDOWS32 */
1038 /* We are the parent side. Set the state to
1039 say the commands are running and return. */
1041 set_command_state (child
->file
, cs_running
);
1043 /* Free the storage used by the child's argument list. */
1046 free ((char *) argv
);
1052 child
->file
->update_status
= 2;
1053 notice_finished_file (child
->file
);
1057 /* Try to start a child running.
1058 Returns nonzero if the child was started (and maybe finished), or zero if
1059 the load was too high and the child was put on the `waiting_jobs' chain. */
1062 start_waiting_job (c
)
1065 /* If we can start a job remotely, we always want to, and don't care about
1066 the local load average. We record that the job should be started
1067 remotely in C->remote for start_job_command to test. */
1069 c
->remote
= start_remote_job_p ();
1071 /* If this job is to be started locally, and we are already running
1072 some jobs, make this one wait if the load average is too high. */
1073 if (!c
->remote
&& job_slots_used
> 0 && load_too_high ())
1075 /* Put this child on the chain of children waiting
1076 for the load average to go down. */
1077 set_command_state (c
->file
, cs_running
);
1078 c
->next
= waiting_jobs
;
1083 /* Start the first command; reap_children will run later command lines. */
1084 start_job_command (c
);
1086 switch (c
->file
->command_state
)
1091 printf ("Putting child 0x%08lx PID %05d%s on the chain.\n",
1092 (unsigned long int) c
,
1093 c
->pid
, c
->remote
? " (remote)" : "");
1095 /* One more job slot is in use. */
1100 case cs_not_started
:
1101 /* All the command lines turned out to be empty. */
1102 c
->file
->update_status
= 0;
1106 notice_finished_file (c
->file
);
1111 assert (c
->file
->command_state
== cs_finished
);
1118 /* Create a `struct child' for FILE and start its commands running. */
1122 register struct file
*file
;
1124 register struct commands
*cmds
= file
->cmds
;
1125 register struct child
*c
;
1127 register unsigned int i
;
1129 /* Let any previously decided-upon jobs that are waiting
1130 for the load to go down start before this new one. */
1131 start_waiting_jobs ();
1133 /* Reap any children that might have finished recently. */
1134 reap_children (0, 0);
1136 /* Chop the commands up into lines if they aren't already. */
1137 chop_commands (cmds
);
1140 /* Wait for a job slot to be freed up. */
1141 while (job_slots_used
== job_slots
)
1142 reap_children (1, 0);
1144 /* Expand the command lines and store the results in LINES. */
1145 lines
= (char **) xmalloc (cmds
->ncommand_lines
* sizeof (char *));
1146 for (i
= 0; i
< cmds
->ncommand_lines
; ++i
)
1148 /* Collapse backslash-newline combinations that are inside variable
1149 or function references. These are left alone by the parser so
1150 that they will appear in the echoing of commands (where they look
1151 nice); and collapsed by construct_command_argv when it tokenizes.
1152 But letting them survive inside function invocations loses because
1153 we don't want the functions to see them as part of the text. */
1155 char *in
, *out
, *ref
;
1157 /* IN points to where in the line we are scanning.
1158 OUT points to where in the line we are writing.
1159 When we collapse a backslash-newline combination,
1160 IN gets ahead of OUT. */
1162 in
= out
= cmds
->command_lines
[i
];
1163 while ((ref
= index (in
, '$')) != 0)
1165 ++ref
; /* Move past the $. */
1168 /* Copy the text between the end of the last chunk
1169 we processed (where IN points) and the new chunk
1170 we are about to process (where REF points). */
1171 bcopy (in
, out
, ref
- in
);
1173 /* Move both pointers past the boring stuff. */
1177 if (*ref
== '(' || *ref
== '{')
1179 char openparen
= *ref
;
1180 char closeparen
= openparen
== '(' ? ')' : '}';
1184 *out
++ = *in
++; /* Copy OPENPAREN. */
1185 /* IN now points past the opening paren or brace.
1186 Count parens or braces until it is matched. */
1190 if (*in
== closeparen
&& --count
< 0)
1192 else if (*in
== '\\' && in
[1] == '\n')
1194 /* We have found a backslash-newline inside a
1195 variable or function reference. Eat it and
1196 any following whitespace. */
1199 for (p
= in
- 1; p
> ref
&& *p
== '\\'; --p
)
1203 /* There were two or more backslashes, so this is
1204 not really a continuation line. We don't collapse
1205 the quoting backslashes here as is done in
1206 collapse_continuations, because the line will
1207 be collapsed again after expansion. */
1211 /* Skip the backslash, newline and
1212 any following whitespace. */
1213 in
= next_token (in
+ 2);
1215 /* Discard any preceding whitespace that has
1216 already been written to the output. */
1217 while (out
> ref
&& isblank (out
[-1]))
1220 /* Replace it all with a single space. */
1226 if (*in
== openparen
)
1235 /* There are no more references in this line to worry about.
1236 Copy the remaining uninteresting text to the output. */
1240 /* Finally, expand the line. */
1241 lines
[i
] = allocated_variable_expand_for_file (cmds
->command_lines
[i
],
1245 /* Start the command sequence, record it in a new
1246 `struct child', and add that to the chain. */
1248 c
= (struct child
*) xmalloc (sizeof (struct child
));
1250 c
->command_lines
= lines
;
1251 c
->command_line
= 0;
1255 /* Fetch the first command line to be run. */
1256 job_next_command (c
);
1258 /* The job is now primed. Start it running.
1259 (This will notice if there are in fact no commands.) */
1260 (void)start_waiting_job (c
);
1263 /* Since there is only one job slot, make things run linearly.
1264 Wait for the child to die, setting the state to `cs_finished'. */
1265 while (file
->command_state
== cs_running
)
1266 reap_children (1, 0);
1271 /* Move CHILD's pointers to the next command for it to execute.
1272 Returns nonzero if there is another command. */
1275 job_next_command (child
)
1276 struct child
*child
;
1278 while (child
->command_ptr
== 0 || *child
->command_ptr
== '\0')
1280 /* There are no more lines in the expansion of this line. */
1281 if (child
->command_line
== child
->file
->cmds
->ncommand_lines
)
1283 /* There are no more lines to be expanded. */
1284 child
->command_ptr
= 0;
1288 /* Get the next line to run. */
1289 child
->command_ptr
= child
->command_lines
[child
->command_line
++];
1297 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA)
1302 if (max_load_average
< 0)
1306 if (getloadavg (&load
, 1) != 1)
1308 static int lossage
= -1;
1309 /* Complain only once for the same error. */
1310 if (lossage
== -1 || errno
!= lossage
)
1313 /* An errno value of zero means getloadavg is just unsupported. */
1314 error ("cannot enforce load limits on this operating system");
1316 perror_with_name ("cannot enforce load limit: ", "getloadavg");
1323 return load
>= max_load_average
;
1327 /* Start jobs that are waiting for the load to be lower. */
1330 start_waiting_jobs ()
1334 if (waiting_jobs
== 0)
1339 /* Check for recently deceased descendants. */
1340 reap_children (0, 0);
1342 /* Take a job off the waiting list. */
1344 waiting_jobs
= job
->next
;
1346 /* Try to start that job. We break out of the loop as soon
1347 as start_waiting_job puts one back on the waiting list. */
1349 while (start_waiting_job (job
) && waiting_jobs
!= 0);
1356 #include <descrip.h>
1359 /* This is called as an AST when a child process dies (it won't get
1360 interrupted by anything except a higher level AST).
1362 int vmsHandleChildTerm(struct child
*child
)
1365 register struct child
*lastc
, *c
;
1368 vms_jobsefnmask
&= ~(1 << (child
->efn
- 32));
1370 lib$
free_ef(&child
->efn
);
1372 (void) sigblock (fatal_signal_mask
);
1374 child_failed
= !(child
->cstatus
& 1 || ((child
->cstatus
& 7) == 0));
1376 /* Search for a child matching the deceased one. */
1378 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
1379 for (c
= children
; c
!= 0 && c
!= child
; lastc
= c
, c
= c
->next
);
1384 if (child_failed
&& !c
->noerror
&& !ignore_errors_flag
)
1386 /* The commands failed. Write an error message,
1387 delete non-precious targets, and abort. */
1388 child_error (c
->file
->name
, c
->cstatus
, 0, 0, 0);
1389 c
->file
->update_status
= 1;
1390 delete_child_targets (c
);
1396 /* The commands failed, but we don't care. */
1397 child_error (c
->file
->name
, c
->cstatus
, 0, 0, 1);
1401 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
1402 /* If there are more commands to run, try to start them. */
1405 switch (c
->file
->command_state
)
1408 /* Successfully started. */
1412 if (c
->file
->update_status
!= 0) {
1413 /* We failed to start the commands. */
1414 delete_child_targets (c
);
1419 error ("internal error: `%s' command_state \
1420 %d in child_handler", c
->file
->name
);
1424 #endif /* RECURSIVEJOBS */
1427 /* Set the state flag to say the commands have finished. */
1428 c
->file
->command_state
= cs_finished
;
1429 notice_finished_file (c
->file
);
1431 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
1432 /* Remove the child from the chain and free it. */
1436 lastc
->next
= c
->next
;
1438 #endif /* RECURSIVEJOBS */
1440 /* There is now another slot open. */
1441 if (job_slots_used
> 0)
1444 /* If the job failed, and the -k flag was not given, die. */
1445 if (child_failed
&& !keep_going_flag
)
1448 (void) sigsetmask (sigblock (0) & ~(fatal_signal_mask
));
1454 Spawn a process executing the command in ARGV and return its pid. */
1456 #define MAXCMDLEN 200
1459 child_execute_job (argv
, child
)
1461 struct child
*child
;
1464 static struct dsc$descriptor_s cmddsc
;
1465 #ifndef DONTWAITFORCHILD
1468 int spflags
= CLI$M_NOWAIT
;
1471 char cmd
[4096],*p
,*c
;
1474 /* Remove backslashes */
1475 for (p
= argv
, c
= cmd
; *p
; p
++,c
++)
1477 if (*p
== '\\') p
++;
1482 /* check for maximum dcl length and create *.com file if neccesary */
1486 if (strlen (cmd
) > MAXCMDLEN
)
1491 strcpy (comname
, "sys$scratch:CMDXXXXXX.COM");
1492 (void) mktemp (comname
);
1494 outfile
= fopen (comname
, "w");
1496 pfatal_with_name (comname
);
1498 fprintf (outfile
, "$ ");
1503 p
= strchr (c
, ',');
1504 if ((p
== NULL
) || (p
-c
> MAXCMDLEN
))
1505 p
= strchr (c
, ' ');
1514 fprintf (outfile
, "%s%s\n", c
, (tmp
== '\0')?"":" -");
1522 sprintf (cmd
, "$ @%s", comname
);
1525 printf ("Executing %s instead\n", cmd
);
1528 cmddsc
.dsc$w_length
= strlen(cmd
);
1529 cmddsc
.dsc$a_pointer
= cmd
;
1530 cmddsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
1531 cmddsc
.dsc$b_class
= DSC$K_CLASS_S
;
1534 while (child
->efn
< 32 || child
->efn
> 63)
1536 status
= lib$
get_ef(&child
->efn
);
1541 sys$
clref(child
->efn
);
1543 vms_jobsefnmask
|= (1 << (child
->efn
- 32));
1545 #ifndef DONTWAITFORCHILD
1546 status
= lib$
spawn(&cmddsc
,0,0,&spflags
,0,&child
->pid
,&child
->cstatus
,
1548 vmsHandleChildTerm(child
);
1550 status
= lib$
spawn(&cmddsc
,0,0,&spflags
,0,&child
->pid
,&child
->cstatus
,
1551 &child
->efn
,vmsHandleChildTerm
,child
);
1556 printf("Error spawning, %d\n",status
);
1562 return (status
& 1);
1567 #if !defined (_AMIGA) && !defined (__MSDOS__)
1569 Replace the current process with one executing the command in ARGV.
1570 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
1571 the environment of the new program. This function does not return. */
1574 child_execute_job (stdin_fd
, stdout_fd
, argv
, envp
)
1575 int stdin_fd
, stdout_fd
;
1576 char **argv
, **envp
;
1579 (void) dup2 (stdin_fd
, 0);
1581 (void) dup2 (stdout_fd
, 1);
1583 (void) close (stdin_fd
);
1585 (void) close (stdout_fd
);
1587 /* Run the command. */
1588 exec_command (argv
, envp
);
1590 #endif /* !AMIGA && !__MSDOS__ */
1592 #endif /* !WINDOWS32 */
1595 /* Replace the current process with one running the command in ARGV,
1596 with environment ENVP. This function does not return. */
1599 exec_command (argv
, envp
)
1600 char **argv
, **envp
;
1603 /* Run the program. */
1604 execve (argv
[0], argv
, envp
);
1605 perror_with_name ("execve: ", argv
[0]);
1606 _exit (EXIT_FAILURE
);
1612 int exit_code
= EXIT_FAILURE
;
1614 /* make sure CreateProcess() has Path it needs */
1615 sync_Path_environment();
1617 /* launch command */
1618 hPID
= process_easy(argv
, envp
);
1620 /* make sure launch ok */
1621 if (hPID
== INVALID_HANDLE_VALUE
)
1625 "process_easy() failed failed to launch process (e=%d)\n",
1626 process_last_err(hPID
));
1627 for (i
= 0; argv
[i
]; i
++)
1628 fprintf(stderr
, "%s ", argv
[i
]);
1629 fprintf(stderr
, "\nCounted %d args in failed launch\n", i
);
1633 /* wait and reap last child */
1634 while (hWaitPID
= process_wait_for_any())
1636 /* was an error found on this process? */
1637 err
= process_last_err(hWaitPID
);
1640 exit_code
= process_exit_code(hWaitPID
);
1643 fprintf(stderr
, "make (e=%d, rc=%d): %s",
1644 err
, exit_code
, map_windows32_error_to_string(err
));
1646 /* cleanup process */
1647 process_cleanup(hWaitPID
);
1649 /* expect to find only last pid, warn about other pids reaped */
1650 if (hWaitPID
== hPID
)
1654 "make reaped child pid %d, still waiting for pid %d\n",
1658 /* return child's exit code as our exit code */
1661 #else /* !WINDOWS32 */
1663 /* Be the user, permanently. */
1666 /* Run the program. */
1668 execvp (argv
[0], argv
);
1673 error ("%s: Command not found", argv
[0]);
1677 /* The file is not executable. Try it as a shell script. */
1678 extern char *getenv ();
1683 shell
= getenv ("SHELL");
1685 shell
= default_shell
;
1688 while (argv
[argc
] != 0)
1691 new_argv
= (char **) alloca ((1 + argc
+ 1) * sizeof (char *));
1692 new_argv
[0] = shell
;
1693 new_argv
[1] = argv
[0];
1696 new_argv
[1 + argc
] = argv
[argc
];
1700 execvp (shell
, new_argv
);
1701 if (errno
== ENOENT
)
1702 error ("%s: Shell program not found", shell
);
1704 perror_with_name ("execvp: ", shell
);
1709 perror_with_name ("execvp: ", argv
[0]);
1714 #endif /* !WINDOWS32 */
1717 #else /* On Amiga */
1718 void exec_command (argv
)
1724 void clean_tmp (void)
1726 DeleteFile (amiga_bname
);
1729 #endif /* On Amiga */
1732 /* Figure out the argument list necessary to run LINE as a command. Try to
1733 avoid using a shell. This routine handles only ' quoting, and " quoting
1734 when no backslash, $ or ` characters are seen in the quotes. Starting
1735 quotes may be escaped with a backslash. If any of the characters in
1736 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
1737 is the first word of a line, the shell is used.
1739 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
1740 If *RESTP is NULL, newlines will be ignored.
1742 SHELL is the shell to use, or nil to use the default shell.
1743 IFS is the value of $IFS, or nil (meaning the default). */
1746 construct_command_argv_internal (line
, restp
, shell
, ifs
)
1747 char *line
, **restp
;
1751 /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
1752 We call `system' for anything that requires ``slow'' processing,
1753 because DOS shells are too dumb. When $SHELL points to a real
1754 (unix-style) shell, `system' just calls it to do everything. When
1755 $SHELL points to a DOS shell, `system' does most of the work
1756 internally, calling the shell only for its internal commands.
1757 However, it looks on the $PATH first, so you can e.g. have an
1758 external command named `mkdir'.
1760 Since we call `system', certain characters and commands below are
1761 actually not specific to COMMAND.COM, but to the DJGPP implementation
1762 of `system'. In particular:
1764 The shell wildcard characters are in DOS_CHARS because they will
1765 not be expanded if we call the child via `spawnXX'.
1767 The `;' is in DOS_CHARS, because our `system' knows how to run
1768 multiple commands on a single line.
1770 DOS_CHARS also include characters special to 4DOS/NDOS, so we
1771 won't have to tell one from another and have one more set of
1772 commands and special characters. */
1773 static char sh_chars_dos
[] = "*?[];|<>%^&()";
1774 static char *sh_cmds_dos
[] = { "break", "call", "cd", "chcp", "chdir", "cls",
1775 "copy", "ctty", "date", "del", "dir", "echo",
1776 "erase", "exit", "for", "goto", "if", "md",
1777 "mkdir", "path", "pause", "prompt", "rd",
1778 "rmdir", "rem", "ren", "rename", "set",
1779 "shift", "time", "type", "ver", "verify",
1782 static char sh_chars_sh
[] = "#;\"*?[]&|<>(){}$`^";
1783 static char *sh_cmds_sh
[] = { "cd", "eval", "exec", "exit", "login",
1784 "logout", "set", "umask", "wait", "while",
1785 "for", "case", "if", ":", ".", "break",
1786 "continue", "export", "read", "readonly",
1787 "shift", "times", "trap", "switch", 0 };
1793 static char sh_chars
[] = "#;\"|<>()?*$`";
1794 static char *sh_cmds
[] = { "cd", "eval", "if", "delete", "echo", "copy",
1795 "rename", "set", "setenv", "date", "makedir",
1796 "skip", "else", "endif", "path", "prompt",
1797 "unset", "unsetenv", "version",
1801 static char sh_chars_dos
[] = "\"|<>";
1802 static char *sh_cmds_dos
[] = { "break", "call", "cd", "chcp", "chdir", "cls",
1803 "copy", "ctty", "date", "del", "dir", "echo",
1804 "erase", "exit", "for", "goto", "if", "if", "md",
1805 "mkdir", "path", "pause", "prompt", "rem", "ren",
1806 "rename", "set", "shift", "time", "type",
1807 "ver", "verify", "vol", ":", 0 };
1808 static char sh_chars_sh
[] = "#;\"*?[]&|<>(){}$`^";
1809 static char *sh_cmds_sh
[] = { "cd", "eval", "exec", "exit", "login",
1810 "logout", "set", "umask", "wait", "while", "for",
1811 "case", "if", ":", ".", "break", "continue",
1812 "export", "read", "readonly", "shift", "times",
1813 "trap", "switch", "test", 0 };
1816 #else /* WINDOWS32 */
1817 static char sh_chars
[] = "#;\"*?[]&|<>(){}$`^";
1818 static char *sh_cmds
[] = { "cd", "eval", "exec", "exit", "login",
1819 "logout", "set", "umask", "wait", "while", "for",
1820 "case", "if", ":", ".", "break", "continue",
1821 "export", "read", "readonly", "shift", "times",
1822 "trap", "switch", 0 };
1823 #endif /* WINDOWS32 */
1825 #endif /* __MSDOS__ */
1830 int instring
, word_has_equals
, seen_nonequals
, last_argument_was_empty
;
1831 char **new_argv
= 0;
1835 if (no_default_sh_exe
) {
1836 sh_cmds
= sh_cmds_dos
;
1837 sh_chars
= sh_chars_dos
;
1839 sh_cmds
= sh_cmds_sh
;
1840 sh_chars
= sh_chars_sh
;
1842 #endif /* WINDOWS32 */
1847 /* Make sure not to bother processing an empty line. */
1848 while (isblank (*line
))
1853 /* See if it is safe to parse commands internally. */
1855 shell
= default_shell
;
1857 else if (strcmp (shell
, default_shell
))
1859 char *s1
= _fullpath(NULL
, shell
, 0);
1860 char *s2
= _fullpath(NULL
, default_shell
, 0);
1862 slow_flag
= strcmp((s1
? s1
: ""), (s2
? s2
: ""));
1871 #else /* not WINDOWS32 */
1873 else if (stricmp (shell
, default_shell
))
1875 extern int _is_unixy_shell (const char *_path
);
1877 message (1, "$SHELL changed (was `%s', now `%s')", default_shell
, shell
);
1878 unixy_shell
= _is_unixy_shell (shell
);
1879 default_shell
= shell
;
1883 sh_chars
= sh_chars_sh
;
1884 sh_cmds
= sh_cmds_sh
;
1888 sh_chars
= sh_chars_dos
;
1889 sh_cmds
= sh_cmds_dos
;
1891 #else /* not __MSDOS__ */
1892 else if (strcmp (shell
, default_shell
))
1894 #endif /* not __MSDOS__ */
1895 #endif /* not WINDOWS32 */
1898 for (ap
= ifs
; *ap
!= '\0'; ++ap
)
1899 if (*ap
!= ' ' && *ap
!= '\t' && *ap
!= '\n')
1902 i
= strlen (line
) + 1;
1904 /* More than 1 arg per character is impossible. */
1905 new_argv
= (char **) xmalloc (i
* sizeof (char *));
1907 /* All the args can fit in a buffer as big as LINE is. */
1908 ap
= new_argv
[0] = (char *) xmalloc (i
);
1911 /* I is how many complete arguments have been found. */
1913 instring
= word_has_equals
= seen_nonequals
= last_argument_was_empty
= 0;
1914 for (p
= line
; *p
!= '\0'; ++p
)
1922 /* Inside a string, just copy any char except a closing quote
1923 or a backslash-newline combination. */
1927 if (ap
== new_argv
[0] || *(ap
-1) == '\0')
1928 last_argument_was_empty
= 1;
1930 else if (*p
== '\\' && p
[1] == '\n')
1931 goto swallow_escaped_newline
;
1932 else if (*p
== '\n' && restp
!= NULL
)
1934 /* End of the command line. */
1938 /* Backslash, $, and ` are special inside double quotes.
1939 If we see any of those, punt.
1940 But on MSDOS, if we use COMMAND.COM, double and single
1941 quotes have the same effect. */
1942 else if (instring
== '"' && index ("\\$`", *p
) != 0 && unixy_shell
)
1947 else if (index (sh_chars
, *p
) != 0)
1948 /* Not inside a string, but it's a special char. */
1951 else if (*p
== '.' && p
[1] == '.' && p
[2] == '.' && p
[3] != '.')
1952 /* `...' is a wildcard in DJGPP. */
1956 /* Not a special char. */
1960 /* Equals is a special character in leading words before the
1961 first word with no equals sign in it. This is not the case
1962 with sh -k, but we never get here when using nonstandard
1964 if (! seen_nonequals
&& unixy_shell
)
1966 word_has_equals
= 1;
1971 /* Backslash-newline combinations are eaten. */
1974 swallow_escaped_newline
:
1976 /* Eat the backslash, the newline, and following whitespace,
1977 replacing it all with a single space. */
1980 /* If there is a tab after a backslash-newline,
1981 remove it from the source line which will be echoed,
1982 since it was most likely used to line
1983 up the continued line with the previous one. */
1991 if (ap
!= new_argv
[i
])
1992 /* Treat this as a space, ending the arg.
1993 But if it's at the beginning of the arg, it should
1994 just get eaten, rather than becoming an empty arg. */
1997 p
= next_token (p
) - 1;
2000 else if (p
[1] != '\0')
2001 /* Copy and skip the following char. */
2013 /* End of the command line. */
2018 /* Newlines are not special. */
2025 /* We have the end of an argument.
2026 Terminate the text of the argument. */
2029 last_argument_was_empty
= 0;
2031 /* Update SEEN_NONEQUALS, which tells us if every word
2032 heretofore has contained an `='. */
2033 seen_nonequals
|= ! word_has_equals
;
2034 if (word_has_equals
&& ! seen_nonequals
)
2035 /* An `=' in a word before the first
2036 word without one is magical. */
2038 word_has_equals
= 0; /* Prepare for the next word. */
2040 /* If this argument is the command name,
2041 see if it is a built-in shell command.
2042 If so, have the shell handle it. */
2046 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
2047 if (streq (sh_cmds
[j
], new_argv
[0]))
2051 /* Ignore multiple whitespace chars. */
2053 /* Next iteration should examine the first nonwhite char. */
2065 /* Let the shell deal with an unterminated quote. */
2068 /* Terminate the last argument and the argument list. */
2071 if (new_argv
[i
][0] != '\0' || last_argument_was_empty
)
2078 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
2079 if (streq (sh_cmds
[j
], new_argv
[0]))
2083 if (new_argv
[0] == 0)
2084 /* Line was empty. */
2090 /* We must use the shell. */
2094 /* Free the old argument list we were working on. */
2096 free ((void *)new_argv
);
2100 execute_by_shell
= 1; /* actually, call `system' if shell isn't unixy */
2109 buffer
= (char *)xmalloc (strlen (line
)+1);
2112 for (dptr
=buffer
; *ptr
; )
2114 if (*ptr
== '\\' && ptr
[1] == '\n')
2116 else if (*ptr
== '@') /* Kludge: multiline commands */
2126 new_argv
= (char **) xmalloc(2 * sizeof(char *));
2127 new_argv
[0] = buffer
;
2130 #else /* Not Amiga */
2133 * Not eating this whitespace caused things like
2137 * which gave the shell fits. I think we have to eat
2138 * whitespace here, but this code should be considered
2139 * suspicious if things start failing....
2142 /* Make sure not to bother processing an empty line. */
2143 while (isspace (*line
))
2149 * only come here if no sh.exe command
2151 if (no_default_sh_exe
)
2157 dos_bname
= tempnam (".", "mk");
2158 for (i
= 0; dos_bname
[i
] != '\0'; ++i
)
2159 if (dos_bname
[i
] == '/')
2160 dos_bname
[i
] = '\\';
2161 dos_bename
= (char *) xmalloc (strlen (dos_bname
) + 5);
2162 strcpy (dos_bename
, dos_bname
);
2163 strcat (dos_bname
, ".bat");
2164 strcat (dos_bename
, ".err");
2166 batch
= fopen (dos_bename
, "w"); /* Create a file. */
2169 batch
= fopen (dos_bname
, "w");
2170 fputs ("@echo off\n", batch
);
2171 fputs (line
, batch
);
2172 fprintf (batch
, "\nif errorlevel 1 del %s\n", dos_bename
);
2174 new_argv
= (char **) xmalloc(2 * sizeof(char *));
2175 new_argv
[0] = strdup (dos_bname
);
2179 #endif /* WINDOWS32 */
2181 /* SHELL may be a multi-word command. Construct a command line
2182 "SHELL -c LINE", with all special chars in LINE escaped.
2183 Then recurse, expanding this command line to get the final
2186 unsigned int shell_len
= strlen (shell
);
2187 static char minus_c
[] = " -c ";
2188 unsigned int line_len
= strlen (line
);
2190 char *new_line
= (char *) alloca (shell_len
+ (sizeof (minus_c
) - 1)
2191 + (line_len
* 2) + 1);
2194 bcopy (shell
, ap
, shell_len
);
2196 bcopy (minus_c
, ap
, sizeof (minus_c
) - 1);
2197 ap
+= sizeof (minus_c
) - 1;
2198 for (p
= line
; *p
!= '\0'; ++p
)
2200 if (restp
!= NULL
&& *p
== '\n')
2205 else if (*p
== '\\' && p
[1] == '\n')
2207 /* Eat the backslash, the newline, and following whitespace,
2208 replacing it all with a single space (which is escaped
2212 /* If there is a tab after a backslash-newline,
2213 remove it from the source line which will be echoed,
2214 since it was most likely used to line
2215 up the continued line with the previous one. */
2217 bcopy (p
+ 1, p
, strlen (p
));
2227 /* DOS shells don't know about backslash-escaping. */
2229 (*p
== '\\' || *p
== '\'' || *p
== '"'
2231 || index (sh_chars
, *p
) != 0))
2234 else if (unixy_shell
&& strncmp (p
, "...", 3) == 0)
2236 /* The case of `...' wildcard again. */
2237 strcpy (ap
, "\\.\\.\\");
2244 if (ap
== new_line
+ shell_len
+ sizeof (minus_c
) - 1)
2245 /* Line was empty. */
2250 new_argv
= construct_command_argv_internal (new_line
, (char **) NULL
,
2251 (char *) 0, (char *) 0);
2255 /* With MSDOS shells, we must construct the command line here
2256 instead of recursively calling ourselves, because we
2257 cannot backslash-escape the special characters (see above). */
2258 new_argv
= (char **) xmalloc (sizeof (char *));
2259 line_len
= strlen (new_line
) - shell_len
- sizeof (minus_c
) + 1;
2260 new_argv
[0] = xmalloc (line_len
+ 1);
2261 strncpy (new_argv
[0],
2262 new_line
+ shell_len
+ sizeof (minus_c
) - 1, line_len
);
2263 new_argv
[0][line_len
] = '\0';
2267 #endif /* ! AMIGA */
2272 /* Figure out the argument list necessary to run LINE as a command. Try to
2273 avoid using a shell. This routine handles only ' quoting, and " quoting
2274 when no backslash, $ or ` characters are seen in the quotes. Starting
2275 quotes may be escaped with a backslash. If any of the characters in
2276 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2277 is the first word of a line, the shell is used.
2279 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2280 If *RESTP is NULL, newlines will be ignored.
2282 FILE is the target whose commands these are. It is used for
2283 variable expansion for $(SHELL) and $(IFS). */
2286 construct_command_argv (line
, restp
, file
)
2287 char *line
, **restp
;
2294 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
2295 int save
= warn_undefined_variables_flag
;
2296 warn_undefined_variables_flag
= 0;
2298 shell
= allocated_variable_expand_for_file ("$(SHELL)", file
);
2301 * Convert to forward slashes so that construct_command_argv_internal()
2305 char *p
= w32ify(shell
, 0);
2309 ifs
= allocated_variable_expand_for_file ("$(IFS)", file
);
2311 warn_undefined_variables_flag
= save
;
2314 argv
= construct_command_argv_internal (line
, restp
, shell
, ifs
);
2323 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
2341 #endif /* !HAPE_DUP2 && !_AMIGA */