1 /* Job execution and handling for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 95, 96
3 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 /* Default shell to use. */
29 char *default_shell
= "sh.exe";
30 int no_default_sh_exe
= 1;
33 char default_shell
[] = "/bin/sh";
35 char default_shell
[] = "";
36 extern int MyExecute (char **);
42 static int dos_pid
= 123;
43 static int dos_status
;
44 static char *dos_bname
;
45 static char *dos_bename
;
46 static int dos_batch_file
;
50 #include <proto/dos.h>
51 static int amiga_pid
= 123;
52 static int amiga_status
;
53 static char amiga_bname
[32];
54 static int amiga_batch_file
;
59 #include <processes.h>
61 #include <lib$routines.h>
70 #include "pathstuff.h"
72 /* this stuff used if no sh.exe is around */
73 static char *dos_bname
;
74 static char *dos_bename
;
75 static int dos_batch_file
;
84 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
89 #define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
90 #else /* Don't have waitpid. */
95 #define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
96 #endif /* Have wait3. */
97 #endif /* Have waitpid. */
99 #if !defined (wait) && !defined (POSIX)
103 #ifndef HAVE_UNION_WAIT
108 #define WTERMSIG(x) ((x) & 0x7f)
111 #define WCOREDUMP(x) ((x) & 0x80)
114 #define WEXITSTATUS(x) (((x) >> 8) & 0xff)
117 #define WIFSIGNALED(x) (WTERMSIG (x) != 0)
120 #define WIFEXITED(x) (WTERMSIG (x) == 0)
123 #else /* Have `union wait'. */
125 #define WAIT_T union wait
127 #define WTERMSIG(x) ((x).w_termsig)
130 #define WCOREDUMP(x) ((x).w_coredump)
133 #define WEXITSTATUS(x) ((x).w_retcode)
136 #define WIFSIGNALED(x) (WTERMSIG(x) != 0)
139 #define WIFEXITED(x) (WTERMSIG(x) == 0)
142 #endif /* Don't have `union wait'. */
145 static int vms_jobsefnmask
=0;
148 #ifndef HAVE_UNISTD_H
150 extern int execve ();
151 extern void _exit ();
153 extern int geteuid ();
154 extern int getegid ();
155 extern int setgid ();
156 extern int getgid ();
160 extern char *allocated_variable_expand_for_file
PARAMS ((char *line
, struct file
*file
));
162 extern int getloadavg
PARAMS ((double loadavg
[], int nelem
));
163 extern int start_remote_job
PARAMS ((char **argv
, char **envp
, int stdin_fd
,
164 int *is_remote
, int *id_ptr
, int *used_stdin
));
165 extern int start_remote_job_p
PARAMS ((void));
166 extern int remote_status
PARAMS ((int *exit_code_ptr
, int *signal_ptr
,
167 int *coredump_ptr
, int block
));
169 RETSIGTYPE child_handler
PARAMS ((int));
170 static void free_child
PARAMS ((struct child
*));
171 static void start_job_command
PARAMS ((struct child
*child
));
172 static int load_too_high
PARAMS ((void));
173 static int job_next_command
PARAMS ((struct child
*));
174 static int start_waiting_job
PARAMS ((struct child
*));
176 static void vmsWaitForChildren
PARAMS ((int *));
179 /* Chain of all live (or recently deceased) children. */
181 struct child
*children
= 0;
183 /* Number of children currently running. */
185 unsigned int job_slots_used
= 0;
187 /* Nonzero if the `good' standard input is in use. */
189 static int good_stdin_used
= 0;
191 /* Chain of children waiting to run until the load average goes down. */
193 static struct child
*waiting_jobs
= 0;
197 * The macro which references this function is defined in make.h.
199 int w32_kill(int pid
, int sig
)
201 return ((process_kill(pid
, sig
) == TRUE
) ? 0 : -1);
205 /* Write an error message describing the exit status given in
206 EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
207 Append "(ignored)" if IGNORED is nonzero. */
210 child_error (target_name
, exit_code
, exit_sig
, coredump
, ignored
)
212 int exit_code
, exit_sig
, coredump
;
215 if (ignored
&& silent_flag
)
219 if (!(exit_code
& 1))
220 error("*** [%s] Error 0x%x%s", target_name
, exit_code
, ((ignored
)? " (ignored)" : ""));
223 error (ignored
? "[%s] Error %d (ignored)" :
225 target_name
, exit_code
);
227 error ("*** [%s] %s%s",
228 target_name
, strsignal (exit_sig
),
229 coredump
? " (core dumped)" : "");
233 static unsigned int dead_children
= 0;
236 /* Wait for nchildren children to terminate */
238 vmsWaitForChildren(int *status
)
242 if (!vms_jobsefnmask
)
248 *status
= sys$
wflor (32, vms_jobsefnmask
);
255 /* Notice that a child died.
256 reap_children should be called when convenient. */
264 printf ("Got a SIGCHLD; %d unreaped children.\n", dead_children
);
267 extern int shell_function_pid
, shell_function_completed
;
269 /* Reap dead children, storing the returned status and the new command
270 state (`cs_finished') in the `file' member of the `struct child' for the
271 dead child, and removing the child from the chain. If BLOCK nonzero,
272 reap at least one child, waiting for it to die if necessary. If ERR is
273 nonzero, print an error message first. */
276 reap_children (block
, err
)
281 while ((children
!= 0 || shell_function_pid
!= 0) &&
282 (block
|| dead_children
> 0))
286 int exit_code
, exit_sig
, coredump
;
287 register struct child
*lastc
, *c
;
289 int any_remote
, any_local
;
291 if (err
&& dead_children
== 0)
293 /* We might block for a while, so let the user know why. */
295 error ("*** Waiting for unfinished jobs....");
298 /* We have one less dead child to reap.
299 The test and decrement are not atomic; if it is compiled into:
300 register = dead_children - 1;
301 dead_children = register;
302 a SIGCHLD could come between the two instructions.
303 child_handler increments dead_children.
304 The second instruction here would lose that increment. But the
305 only effect of dead_children being wrong is that we might wait
306 longer than necessary to reap a child, and lose some parallelism;
307 and we might print the "Waiting for unfinished jobs" message above
308 when not necessary. */
310 if (dead_children
> 0)
314 any_local
= shell_function_pid
!= 0;
315 for (c
= children
; c
!= 0; c
= c
->next
)
317 any_remote
|= c
->remote
;
318 any_local
|= ! c
->remote
;
320 printf ("Live child 0x%08lx PID %d%s\n",
321 (unsigned long int) c
,
322 c
->pid
, c
->remote
? " (remote)" : "");
328 /* First, check for remote children. */
330 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 0);
341 pfatal_with_name ("remote_status");
345 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WIN32)
346 /* No remote children. Check for local children. */
351 vmsWaitForChildren (&status
);
356 pid
= WAIT_NOHANG (&status
);
359 pid
= wait (&status
);
371 pfatal_with_name ("wait");
375 /* No local children. */
376 if (block
&& any_remote
)
378 /* Now try a blocking wait for a remote child. */
379 pid
= remote_status (&exit_code
, &exit_sig
, &coredump
, 1);
381 goto remote_status_lose
;
383 /* No remote children either. Finally give up. */
386 /* We got a remote child. */
394 /* Chop the status word up. */
395 exit_code
= WEXITSTATUS (status
);
396 exit_sig
= WIFSIGNALED (status
) ? WTERMSIG (status
) : 0;
397 coredump
= WCOREDUMP (status
);
399 #else /* MSDOS, Amiga, WIN32. */
401 /* Life is very different on MSDOS. */
404 exit_code
= dos_status
;
407 #endif /* __MSDOS__ */
411 status
= amiga_status
;
412 exit_code
= amiga_status
;
421 /* wait for anything to finish */
422 if (hPID
= process_wait_for_any()) {
424 /* was an error found on this process? */
425 err
= process_last_err(hPID
);
428 exit_code
= process_exit_code(hPID
);
431 fprintf(stderr
, "make (e=%d): %s",
432 exit_code
, map_win32_error_to_string(exit_code
));
434 exit_sig
= process_signal(hPID
);
436 /* cleanup process */
437 process_cleanup(hPID
);
439 if (dos_batch_file
) {
450 #endif /* Not MSDOS. */
453 /* We got a remote child. */
456 /* Check if this is the child of the `shell' function. */
457 if (!remote
&& pid
== shell_function_pid
)
459 /* It is. Leave an indicator for the `shell' function. */
460 if (exit_sig
== 0 && exit_code
== 127)
461 shell_function_completed
= -1;
463 shell_function_completed
= 1;
467 child_failed
= exit_sig
!= 0 || exit_code
!= 0;
469 /* Search for a child matching the deceased one. */
471 for (c
= children
; c
!= 0; lastc
= c
, c
= c
->next
)
472 if (c
->remote
== remote
&& c
->pid
== pid
)
477 /* An unknown child died. */
479 sprintf (buf
, "Unknown%s job %d", remote
? " remote" : "", pid
);
481 child_error (buf
, exit_code
, exit_sig
, coredump
,
484 error ("%s finished.", buf
);
489 printf ("Reaping %s child 0x%08lx PID %d%s\n",
490 child_failed
? "losing" : "winning",
491 (unsigned long int) c
,
492 c
->pid
, c
->remote
? " (remote)" : "");
494 /* If this child had the good stdin, say it is now free. */
498 if (child_failed
&& !c
->noerror
&& !ignore_errors_flag
)
500 /* The commands failed. Write an error message,
501 delete non-precious targets, and abort. */
502 static int delete_on_error
= -1;
503 child_error (c
->file
->name
, exit_code
, exit_sig
, coredump
, 0);
504 c
->file
->update_status
= 2;
505 if (delete_on_error
== -1)
507 struct file
*f
= lookup_file (".DELETE_ON_ERROR");
508 delete_on_error
= f
!= 0 && f
->is_target
;
510 if (exit_sig
!= 0 || delete_on_error
)
511 delete_child_targets (c
);
517 /* The commands failed, but we don't care. */
518 child_error (c
->file
->name
,
519 exit_code
, exit_sig
, coredump
, 1);
523 /* If there are more commands to run, try to start them. */
524 if (job_next_command (c
))
526 if (handling_fatal_signal
)
528 /* Never start new commands while we are dying.
529 Since there are more commands that wanted to be run,
530 the target was not completely remade. So we treat
531 this as if a command had failed. */
532 c
->file
->update_status
= 2;
536 /* Check again whether to start remotely.
537 Whether or not we want to changes over time.
538 Also, start_remote_job may need state set up
539 by start_remote_job_p. */
540 c
->remote
= start_remote_job_p ();
541 start_job_command (c
);
542 /* Fatal signals are left blocked in case we were
543 about to put that child on the chain. But it is
544 already there, so it is safe for a fatal signal to
545 arrive now; it will clean up this child's targets. */
547 if (c
->file
->command_state
== cs_running
)
548 /* We successfully started the new command.
549 Loop to reap more children. */
553 if (c
->file
->update_status
!= 0)
554 /* We failed to start the commands. */
555 delete_child_targets (c
);
558 /* There are no more commands. We got through them all
559 without an unignored error. Now the target has been
560 successfully updated. */
561 c
->file
->update_status
= 0;
564 /* When we get here, all the commands for C->file are finished
565 (or aborted) and C->file->update_status contains 0 or 2. But
566 C->file->command_state is still cs_running if all the commands
567 ran; notice_finish_file looks for cs_running to tell it that
568 it's interesting to check the file's modtime again now. */
570 if (! handling_fatal_signal
)
571 /* Notice if the target of the commands has been changed.
572 This also propagates its values for command_state and
573 update_status to its also_make files. */
574 notice_finished_file (c
->file
);
577 printf ("Removing child 0x%08lx PID %d%s from chain.\n",
578 (unsigned long int) c
,
579 c
->pid
, c
->remote
? " (remote)" : "");
581 /* Block fatal signals while frobnicating the list, so that
582 children and job_slots_used are always consistent. Otherwise
583 a fatal signal arriving after the child is off the chain and
584 before job_slots_used is decremented would believe a child was
585 live and call reap_children again. */
588 /* Remove the child from the chain and free it. */
592 lastc
->next
= c
->next
;
593 if (! handling_fatal_signal
) /* Don't bother if about to die. */
596 /* There is now another slot open. */
597 if (job_slots_used
> 0)
602 /* If the job failed, and the -k flag was not given, die,
603 unless we are already in the process of dying. */
604 if (!err
&& child_failed
&& !keep_going_flag
&&
605 /* fatal_error_signal will die with the right signal. */
606 !handling_fatal_signal
)
610 /* Only block for one child. */
616 /* Free the storage allocated for CHILD. */
620 register struct child
*child
;
622 if (child
->command_lines
!= 0)
624 register unsigned int i
;
625 for (i
= 0; i
< child
->file
->cmds
->ncommand_lines
; ++i
)
626 free (child
->command_lines
[i
]);
627 free ((char *) child
->command_lines
);
630 if (child
->environment
!= 0)
632 register char **ep
= child
->environment
;
635 free ((char *) child
->environment
);
638 free ((char *) child
);
642 extern sigset_t fatal_signal_set
;
649 (void) sigprocmask (SIG_BLOCK
, &fatal_signal_set
, (sigset_t
*) 0);
651 #ifdef HAVE_SIGSETMASK
652 (void) sigblock (fatal_signal_mask
);
669 sigemptyset (&empty
);
670 sigprocmask (SIG_SETMASK
, &empty
, (sigset_t
*) 0);
675 /* Start a job to run the commands specified in CHILD.
676 CHILD is updated to reflect the commands and ID of the child process.
678 NOTE: On return fatal signals are blocked! The caller is responsible
679 for calling `unblock_sigs', once the new child is safely on the chain so
680 it can be cleaned up in the event of a fatal signal. */
683 start_job_command (child
)
684 register struct child
*child
;
687 static int bad_stdin
= -1;
697 /* Combine the flags parsed for the line itself with
698 the flags specified globally for this target. */
699 flags
= (child
->file
->command_flags
700 | child
->file
->cmds
->lines_flags
[child
->command_line
- 1]);
702 p
= child
->command_ptr
;
703 child
->noerror
= flags
& COMMANDS_NOERROR
;
708 flags
|= COMMANDS_SILENT
;
710 flags
|= COMMANDS_RECURSE
;
713 else if (!isblank (*p
) && *p
!= '+')
718 /* If -q was given, just say that updating `failed'. The exit status of
719 1 tells the user that -q is saying `something to do'; the exit status
720 for a random error is 2. */
721 if (question_flag
&& !(flags
& COMMANDS_RECURSE
))
723 child
->file
->update_status
= 1;
724 notice_finished_file (child
->file
);
728 /* There may be some preceding whitespace left if there
729 was nothing but a backslash on the first line. */
732 /* Figure out an argument list from this command line. */
739 argv
= construct_command_argv (p
, &end
, child
->file
);
742 child
->command_ptr
= NULL
;
746 child
->command_ptr
= end
;
750 if (touch_flag
&& !(flags
& COMMANDS_RECURSE
))
752 /* Go on to the next command. It might be the recursive one.
753 We construct ARGV only to find the end of the command line. */
756 free ((char *) argv
);
764 /* This line has no commands. Go to the next. */
765 if (job_next_command (child
))
766 start_job_command (child
);
769 /* No more commands. All done. */
770 child
->file
->update_status
= 0;
771 notice_finished_file (child
->file
);
776 /* Print out the command. If silent, we call `message' with null so it
777 can log the working directory before the command's own error messages
780 message (0, (just_print_flag
|| (!(flags
& COMMANDS_SILENT
) && !silent_flag
))
781 ? "%s" : (char *) 0, p
);
783 /* Tell update_goal_chain that a command has been started on behalf of
784 this target. It is important that this happens here and not in
785 reap_children (where we used to do it), because reap_children might be
786 reaping children from a different target. We want this increment to
787 guaranteedly indicate that a command was started for the dependency
788 chain (i.e., update_file recursion chain) we are processing. */
792 /* If -n was given, recurse to get the next line in the sequence. */
794 if (just_print_flag
&& !(flags
& COMMANDS_RECURSE
))
798 free ((char *) argv
);
803 /* Flush the output streams so they won't have things written twice. */
812 /* Set up a bad standard input that reads from a broken pipe. */
816 /* Make a file descriptor that is the read end of a broken pipe.
817 This will be used for some children's standard inputs. */
821 /* Close the write side. */
822 (void) close (pd
[1]);
823 /* Save the read side. */
826 /* Set the descriptor to close on exec, so it does not litter any
827 child's descriptor table. When it is dup2'd onto descriptor 0,
828 that descriptor will not close on exec. */
833 (void) fcntl (bad_stdin
, F_SETFD
, FD_CLOEXEC
);
841 /* Decide whether to give this child the `good' standard input
842 (one that points to the terminal or whatever), or the `bad' one
843 that points to the read side of a broken pipe. */
845 child
->good_stdin
= !good_stdin_used
;
846 if (child
->good_stdin
)
854 /* Set up the environment for the child. */
855 if (child
->environment
== 0)
856 child
->environment
= target_environment (child
->file
);
859 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WIN32)
862 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
865 int is_remote
, id
, used_stdin
;
866 if (start_remote_job (argv
, child
->environment
,
867 child
->good_stdin
? 0 : bad_stdin
,
868 &is_remote
, &id
, &used_stdin
))
872 if (child
->good_stdin
&& !used_stdin
)
874 child
->good_stdin
= 0;
877 child
->remote
= is_remote
;
884 /* Fork the child process. */
886 char **parent_environ
;
894 if (!child_execute_job (argv
, child
)) {
896 perror_with_name ("vfork", "");
902 parent_environ
= environ
;
903 child
->pid
= vfork ();
904 environ
= parent_environ
; /* Restore value child may have clobbered. */
907 /* We are the child side. */
909 child_execute_job (child
->good_stdin
? 0 : bad_stdin
, 1,
910 argv
, child
->environment
);
912 else if (child
->pid
< 0)
916 perror_with_name ("vfork", "");
922 #else /* MSDOS or Amiga. */
924 dos_status
= spawnvpe (P_WAIT
, argv
[0], argv
, child
->environment
);
926 child
->pid
= dos_pid
++;
930 remove (dos_bname
); /* Ignore errors. */
931 if (access (dos_bename
, 0))
937 #endif /* __MSDOS__ */
939 amiga_status
= MyExecute (argv
);
942 child
->pid
= amiga_pid
++;
943 if (amiga_batch_file
)
945 amiga_batch_file
= 0;
946 DeleteFile (amiga_bname
); /* Ignore errors. */
948 #endif /* Not Amiga */
954 /* make UNC paths safe for CreateProcess -- backslash format */
956 if (arg0
&& arg0
[0] == '/' && arg0
[1] == '/')
957 for ( ; arg0
&& *arg0
; arg0
++)
961 /* make sure CreateProcess() has Path it needs */
962 sync_Path_environment();
964 hPID
= process_easy(argv
, child
->environment
);
966 if (hPID
!= INVALID_HANDLE_VALUE
)
967 child
->pid
= (int) hPID
;
972 "process_easy() failed failed to launch process (e=%d)\n",
973 process_last_err(hPID
));
974 for (i
= 0; argv
[i
]; i
++)
975 fprintf(stderr
, "%s ", argv
[i
]);
976 fprintf(stderr
, "\nCounted %d args in failed launch\n", i
);
980 #endif /* Not MSDOS. */
982 /* We are the parent side. Set the state to
983 say the commands are running and return. */
985 set_command_state (child
->file
, cs_running
);
987 /* Free the storage used by the child's argument list. */
990 free ((char *) argv
);
996 child
->file
->update_status
= 2;
997 notice_finished_file (child
->file
);
1001 /* Try to start a child running.
1002 Returns nonzero if the child was started (and maybe finished), or zero if
1003 the load was too high and the child was put on the `waiting_jobs' chain. */
1006 start_waiting_job (c
)
1009 /* If we can start a job remotely, we always want to, and don't care about
1010 the local load average. We record that the job should be started
1011 remotely in C->remote for start_job_command to test. */
1013 c
->remote
= start_remote_job_p ();
1015 /* If this job is to be started locally, and we are already running
1016 some jobs, make this one wait if the load average is too high. */
1017 if (!c
->remote
&& job_slots_used
> 0 && load_too_high ())
1019 /* Put this child on the chain of children waiting
1020 for the load average to go down. */
1021 set_command_state (c
->file
, cs_running
);
1022 c
->next
= waiting_jobs
;
1027 /* Start the first command; reap_children will run later command lines. */
1028 start_job_command (c
);
1030 switch (c
->file
->command_state
)
1035 printf ("Putting child 0x%08lx PID %05d%s on the chain.\n",
1036 (unsigned long int) c
,
1037 c
->pid
, c
->remote
? " (remote)" : "");
1039 /* One more job slot is in use. */
1044 case cs_not_started
:
1045 /* All the command lines turned out to be empty. */
1046 c
->file
->update_status
= 0;
1050 notice_finished_file (c
->file
);
1055 assert (c
->file
->command_state
== cs_finished
);
1062 /* Create a `struct child' for FILE and start its commands running. */
1066 register struct file
*file
;
1068 register struct commands
*cmds
= file
->cmds
;
1069 register struct child
*c
;
1071 register unsigned int i
;
1073 /* Let any previously decided-upon jobs that are waiting
1074 for the load to go down start before this new one. */
1075 start_waiting_jobs ();
1077 /* Reap any children that might have finished recently. */
1078 reap_children (0, 0);
1080 /* Chop the commands up into lines if they aren't already. */
1081 chop_commands (cmds
);
1084 /* Wait for a job slot to be freed up. */
1085 while (job_slots_used
== job_slots
)
1086 reap_children (1, 0);
1088 /* Expand the command lines and store the results in LINES. */
1089 lines
= (char **) xmalloc (cmds
->ncommand_lines
* sizeof (char *));
1090 for (i
= 0; i
< cmds
->ncommand_lines
; ++i
)
1092 /* Collapse backslash-newline combinations that are inside variable
1093 or function references. These are left alone by the parser so
1094 that they will appear in the echoing of commands (where they look
1095 nice); and collapsed by construct_command_argv when it tokenizes.
1096 But letting them survive inside function invocations loses because
1097 we don't want the functions to see them as part of the text. */
1099 char *in
, *out
, *ref
;
1101 /* IN points to where in the line we are scanning.
1102 OUT points to where in the line we are writing.
1103 When we collapse a backslash-newline combination,
1104 IN gets ahead out OUT. */
1106 in
= out
= cmds
->command_lines
[i
];
1107 while ((ref
= index (in
, '$')) != 0)
1109 ++ref
; /* Move past the $. */
1112 /* Copy the text between the end of the last chunk
1113 we processed (where IN points) and the new chunk
1114 we are about to process (where REF points). */
1115 bcopy (in
, out
, ref
- in
);
1117 /* Move both pointers past the boring stuff. */
1121 if (*ref
== '(' || *ref
== '{')
1123 char openparen
= *ref
;
1124 char closeparen
= openparen
== '(' ? ')' : '}';
1128 *out
++ = *in
++; /* Copy OPENPAREN. */
1129 /* IN now points past the opening paren or brace.
1130 Count parens or braces until it is matched. */
1134 if (*in
== closeparen
&& --count
< 0)
1136 else if (*in
== '\\' && in
[1] == '\n')
1138 /* We have found a backslash-newline inside a
1139 variable or function reference. Eat it and
1140 any following whitespace. */
1143 for (p
= in
- 1; p
> ref
&& *p
== '\\'; --p
)
1147 /* There were two or more backslashes, so this is
1148 not really a continuation line. We don't collapse
1149 the quoting backslashes here as is done in
1150 collapse_continuations, because the line will
1151 be collapsed again after expansion. */
1155 /* Skip the backslash, newline and
1156 any following whitespace. */
1157 in
= next_token (in
+ 2);
1159 /* Discard any preceding whitespace that has
1160 already been written to the output. */
1161 while (out
> ref
&& isblank (out
[-1]))
1164 /* Replace it all with a single space. */
1170 if (*in
== openparen
)
1179 /* There are no more references in this line to worry about.
1180 Copy the remaining uninteresting text to the output. */
1184 /* Finally, expand the line. */
1185 lines
[i
] = allocated_variable_expand_for_file (cmds
->command_lines
[i
],
1189 /* Start the command sequence, record it in a new
1190 `struct child', and add that to the chain. */
1192 c
= (struct child
*) xmalloc (sizeof (struct child
));
1194 c
->command_lines
= lines
;
1195 c
->command_line
= 0;
1199 /* Fetch the first command line to be run. */
1200 job_next_command (c
);
1202 /* The job is now primed. Start it running.
1203 (This will notice if there are in fact no commands.) */
1204 (void)start_waiting_job (c
);
1207 /* Since there is only one job slot, make things run linearly.
1208 Wait for the child to die, setting the state to `cs_finished'. */
1209 while (file
->command_state
== cs_running
)
1210 reap_children (1, 0);
1215 /* Move CHILD's pointers to the next command for it to execute.
1216 Returns nonzero if there is another command. */
1219 job_next_command (child
)
1220 struct child
*child
;
1222 while (child
->command_ptr
== 0 || *child
->command_ptr
== '\0')
1224 /* There are no more lines in the expansion of this line. */
1225 if (child
->command_line
== child
->file
->cmds
->ncommand_lines
)
1227 /* There are no more lines to be expanded. */
1228 child
->command_ptr
= 0;
1232 /* Get the next line to run. */
1233 child
->command_ptr
= child
->command_lines
[child
->command_line
++];
1241 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA)
1246 if (max_load_average
< 0)
1250 if (getloadavg (&load
, 1) != 1)
1252 static int lossage
= -1;
1253 /* Complain only once for the same error. */
1254 if (lossage
== -1 || errno
!= lossage
)
1257 /* An errno value of zero means getloadavg is just unsupported. */
1258 error ("cannot enforce load limits on this operating system");
1260 perror_with_name ("cannot enforce load limit: ", "getloadavg");
1267 return load
>= max_load_average
;
1271 /* Start jobs that are waiting for the load to be lower. */
1274 start_waiting_jobs ()
1278 if (waiting_jobs
== 0)
1283 /* Check for recently deceased descendants. */
1284 reap_children (0, 0);
1286 /* Take a job off the waiting list. */
1288 waiting_jobs
= job
->next
;
1290 /* Try to start that job. We break out of the loop as soon
1291 as start_waiting_job puts one back on the waiting list. */
1293 while (start_waiting_job (job
) && waiting_jobs
!= 0);
1300 #include <descrip.h>
1303 /* This is called as an AST when a child process dies (it won't get
1304 interrupted by anything except a higher level AST).
1306 int vmsHandleChildTerm(struct child
*child
)
1309 register struct child
*lastc
, *c
;
1312 vms_jobsefnmask
&= ~(1 << (child
->efn
- 32));
1314 lib$
free_ef(&child
->efn
);
1316 (void) sigblock (fatal_signal_mask
);
1318 child_failed
= !(child
->cstatus
& 1 || ((child
->cstatus
& 7) == 0));
1320 /* Search for a child matching the deceased one. */
1322 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
1323 for (c
= children
; c
!= 0 && c
!= child
; lastc
= c
, c
= c
->next
);
1328 if (child_failed
&& !c
->noerror
&& !ignore_errors_flag
)
1330 /* The commands failed. Write an error message,
1331 delete non-precious targets, and abort. */
1332 child_error (c
->file
->name
, c
->cstatus
, 0, 0, 0);
1333 c
->file
->update_status
= 1;
1334 delete_child_targets (c
);
1340 /* The commands failed, but we don't care. */
1341 child_error (c
->file
->name
, c
->cstatus
, 0, 0, 1);
1345 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
1346 /* If there are more commands to run, try to start them. */
1349 switch (c
->file
->command_state
)
1352 /* Successfully started. */
1356 if (c
->file
->update_status
!= 0) {
1357 /* We failed to start the commands. */
1358 delete_child_targets (c
);
1363 error ("internal error: `%s' command_state \
1364 %d in child_handler", c
->file
->name
);
1368 #endif /* RECURSIVEJOBS */
1371 /* Set the state flag to say the commands have finished. */
1372 c
->file
->command_state
= cs_finished
;
1373 notice_finished_file (c
->file
);
1375 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
1376 /* Remove the child from the chain and free it. */
1380 lastc
->next
= c
->next
;
1382 #endif /* RECURSIVEJOBS */
1384 /* There is now another slot open. */
1385 if (job_slots_used
> 0)
1388 /* If the job failed, and the -k flag was not given, die. */
1389 if (child_failed
&& !keep_going_flag
)
1392 (void) sigsetmask (sigblock (0) & ~(fatal_signal_mask
));
1398 Spawn a process executing the command in ARGV and return its pid. */
1400 #define MAXCMDLEN 200
1403 child_execute_job (argv
, child
)
1405 struct child
*child
;
1408 static struct dsc$descriptor_s cmddsc
;
1409 #ifndef DONTWAITFORCHILD
1412 int spflags
= CLI$M_NOWAIT
;
1415 char cmd
[4096],*p
,*c
;
1418 /* Remove backslashes */
1419 for (p
= argv
, c
= cmd
; *p
; p
++,c
++)
1421 if (*p
== '\\') p
++;
1426 /* check for maximum dcl length and create *.com file if neccesary */
1430 if (strlen (cmd
) > MAXCMDLEN
)
1435 strcpy (comname
, "sys$scratch:CMDXXXXXX.COM");
1436 (void) mktemp (comname
);
1438 outfile
= fopen (comname
, "w");
1440 pfatal_with_name (comname
);
1442 fprintf (outfile
, "$ ");
1447 p
= strchr (c
, ',');
1448 if ((p
== NULL
) || (p
-c
> MAXCMDLEN
))
1449 p
= strchr (c
, ' ');
1458 fprintf (outfile
, "%s%s\n", c
, (tmp
== '\0')?"":" -");
1466 sprintf (cmd
, "$ @%s", comname
);
1469 printf ("Executing %s instead\n", cmd
);
1472 cmddsc
.dsc$w_length
= strlen(cmd
);
1473 cmddsc
.dsc$a_pointer
= cmd
;
1474 cmddsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
1475 cmddsc
.dsc$b_class
= DSC$K_CLASS_S
;
1478 while (child
->efn
< 32 || child
->efn
> 63)
1480 status
= lib$
get_ef(&child
->efn
);
1485 sys$
clref(child
->efn
);
1487 vms_jobsefnmask
|= (1 << (child
->efn
- 32));
1489 #ifndef DONTWAITFORCHILD
1490 status
= lib$
spawn(&cmddsc
,0,0,&spflags
,0,&child
->pid
,&child
->cstatus
,
1492 vmsHandleChildTerm(child
);
1494 status
= lib$
spawn(&cmddsc
,0,0,&spflags
,0,&child
->pid
,&child
->cstatus
,
1495 &child
->efn
,vmsHandleChildTerm
,child
);
1500 printf("Error spawning, %d\n",status
);
1506 return (status
& 1);
1513 Replace the current process with one executing the command in ARGV.
1514 STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
1515 the environment of the new program. This function does not return. */
1518 child_execute_job (stdin_fd
, stdout_fd
, argv
, envp
)
1519 int stdin_fd
, stdout_fd
;
1520 char **argv
, **envp
;
1523 (void) dup2 (stdin_fd
, 0);
1525 (void) dup2 (stdout_fd
, 1);
1527 (void) close (stdin_fd
);
1529 (void) close (stdout_fd
);
1531 /* Run the command. */
1532 exec_command (argv
, envp
);
1539 /* Replace the current process with one running the command in ARGV,
1540 with environment ENVP. This function does not return. */
1543 exec_command (argv
, envp
)
1544 char **argv
, **envp
;
1547 /* Run the program. */
1548 execve (argv
[0], argv
, envp
);
1549 perror_with_name ("execve: ", argv
[0]);
1550 _exit (EXIT_FAILURE
);
1552 /* Be the user, permanently. */
1555 /* Run the program. */
1557 execvp (argv
[0], argv
);
1562 error ("%s: Command not found", argv
[0]);
1566 /* The file is not executable. Try it as a shell script. */
1567 extern char *getenv ();
1572 shell
= getenv ("SHELL");
1574 shell
= default_shell
;
1577 while (argv
[argc
] != 0)
1580 new_argv
= (char **) alloca ((1 + argc
+ 1) * sizeof (char *));
1581 new_argv
[0] = shell
;
1582 new_argv
[1] = argv
[0];
1585 new_argv
[1 + argc
] = argv
[argc
];
1589 execvp (shell
, new_argv
);
1590 if (errno
== ENOENT
)
1591 error ("%s: Shell program not found", shell
);
1593 perror_with_name ("execvp: ", shell
);
1598 perror_with_name ("execvp: ", argv
[0]);
1605 #else /* On Amiga */
1606 void exec_command (argv
)
1612 void clean_tmp (void)
1614 DeleteFile (amiga_bname
);
1617 #endif /* An Amiga */
1620 /* Figure out the argument list necessary to run LINE as a command. Try to
1621 avoid using a shell. This routine handles only ' quoting, and " quoting
1622 when no backslash, $ or ` characters are seen in the quotes. Starting
1623 quotes may be escaped with a backslash. If any of the characters in
1624 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
1625 is the first word of a line, the shell is used.
1627 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
1628 If *RESTP is NULL, newlines will be ignored.
1630 SHELL is the shell to use, or nil to use the default shell.
1631 IFS is the value of $IFS, or nil (meaning the default). */
1634 construct_command_argv_internal (line
, restp
, shell
, ifs
)
1635 char *line
, **restp
;
1639 static char sh_chars
[] = "\"|<>";
1640 static char *sh_cmds
[] = { "break", "call", "cd", "chcp", "chdir", "cls",
1641 "copy", "ctty", "date", "del", "dir", "echo",
1642 "erase", "exit", "for", "goto", "if", "if", "md",
1643 "mkdir", "path", "pause", "prompt", "rem", "ren",
1644 "rename", "set", "shift", "time", "type",
1645 "ver", "verify", "vol", ":", 0 };
1648 static char sh_chars
[] = "#;\"|<>()?*$`";
1649 static char *sh_cmds
[] = { "cd", "eval", "if", "delete", "echo", "copy",
1650 "rename", "set", "setenv", "date", "makedir",
1651 "skip", "else", "endif", "path", "prompt",
1652 "unset", "unsetenv", "version",
1656 static char sh_chars_dos
[] = "\"|<>";
1657 static char *sh_cmds_dos
[] = { "break", "call", "cd", "chcp", "chdir", "cls",
1658 "copy", "ctty", "date", "del", "dir", "echo",
1659 "erase", "exit", "for", "goto", "if", "if", "md",
1660 "mkdir", "path", "pause", "prompt", "rem", "ren",
1661 "rename", "set", "shift", "time", "type",
1662 "ver", "verify", "vol", ":", 0 };
1663 static char sh_chars_sh
[] = "#;\"*?[]&|<>(){}$`^";
1664 static char *sh_cmds_sh
[] = { "cd", "eval", "exec", "exit", "login",
1665 "logout", "set", "umask", "wait", "while", "for",
1666 "case", "if", ":", ".", "break", "continue",
1667 "export", "read", "readonly", "shift", "times",
1668 "trap", "switch", "test", 0 };
1672 static char sh_chars
[] = "#;\"*?[]&|<>(){}$`^";
1673 static char *sh_cmds
[] = { "cd", "eval", "exec", "exit", "login",
1674 "logout", "set", "umask", "wait", "while", "for",
1675 "case", "if", ":", ".", "break", "continue",
1676 "export", "read", "readonly", "shift", "times",
1677 "trap", "switch", 0 };
1685 int instring
, word_has_equals
, seen_nonequals
;
1686 char **new_argv
= 0;
1690 if (no_default_sh_exe
) {
1691 sh_cmds
= sh_cmds_dos
;
1692 sh_chars
= sh_chars_dos
;
1694 sh_cmds
= sh_cmds_sh
;
1695 sh_chars
= sh_chars_sh
;
1702 /* Make sure not to bother processing an empty line. */
1703 while (isblank (*line
))
1708 /* See if it is safe to parse commands internally. */
1710 shell
= default_shell
;
1711 else if (strcmp (shell
, default_shell
))
1714 char *s1
= _fullpath(NULL
, shell
, 0);
1715 char *s2
= _fullpath(NULL
, default_shell
, 0);
1717 slow_flag
= strcmp((s1
? s1
: ""), (s2
? s2
: ""));
1729 for (ap
= ifs
; *ap
!= '\0'; ++ap
)
1730 if (*ap
!= ' ' && *ap
!= '\t' && *ap
!= '\n')
1733 i
= strlen (line
) + 1;
1735 /* More than 1 arg per character is impossible. */
1736 new_argv
= (char **) xmalloc (i
* sizeof (char *));
1738 /* All the args can fit in a buffer as big as LINE is. */
1739 ap
= new_argv
[0] = (char *) xmalloc (i
);
1742 /* I is how many complete arguments have been found. */
1744 instring
= word_has_equals
= seen_nonequals
= 0;
1745 for (p
= line
; *p
!= '\0'; ++p
)
1753 /* Inside a string, just copy any char except a closing quote
1754 or a backslash-newline combination. */
1757 else if (*p
== '\\' && p
[1] == '\n')
1758 goto swallow_escaped_newline
;
1759 else if (*p
== '\n' && restp
!= NULL
)
1761 /* End of the command line. */
1765 /* Backslash, $, and ` are special inside double quotes.
1766 If we see any of those, punt. */
1767 else if (instring
== '"' && index ("\\$`", *p
) != 0)
1772 else if (index (sh_chars
, *p
) != 0)
1773 /* Not inside a string, but it's a special char. */
1776 /* Not a special char. */
1780 /* Equals is a special character in leading words before the
1781 first word with no equals sign in it. This is not the case
1782 with sh -k, but we never get here when using nonstandard
1784 if (! seen_nonequals
)
1786 word_has_equals
= 1;
1791 /* Backslash-newline combinations are eaten. */
1794 swallow_escaped_newline
:
1796 /* Eat the backslash, the newline, and following whitespace,
1797 replacing it all with a single space. */
1800 /* If there is a tab after a backslash-newline,
1801 remove it from the source line which will be echoed,
1802 since it was most likely used to line
1803 up the continued line with the previous one. */
1811 if (ap
!= new_argv
[i
])
1812 /* Treat this as a space, ending the arg.
1813 But if it's at the beginning of the arg, it should
1814 just get eaten, rather than becoming an empty arg. */
1817 p
= next_token (p
) - 1;
1820 else if (p
[1] != '\0')
1821 /* Copy and skip the following char. */
1833 /* End of the command line. */
1838 /* Newlines are not special. */
1845 /* We have the end of an argument.
1846 Terminate the text of the argument. */
1850 /* Update SEEN_NONEQUALS, which tells us if every word
1851 heretofore has contained an `='. */
1852 seen_nonequals
|= ! word_has_equals
;
1853 if (word_has_equals
&& ! seen_nonequals
)
1854 /* An `=' in a word before the first
1855 word without one is magical. */
1857 word_has_equals
= 0; /* Prepare for the next word. */
1859 /* If this argument is the command name,
1860 see if it is a built-in shell command.
1861 If so, have the shell handle it. */
1865 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
1866 if (streq (sh_cmds
[j
], new_argv
[0]))
1870 /* Ignore multiple whitespace chars. */
1872 /* Next iteration should examine the first nonwhite char. */
1884 /* Let the shell deal with an unterminated quote. */
1887 /* Terminate the last argument and the argument list. */
1890 if (new_argv
[i
][0] != '\0')
1897 for (j
= 0; sh_cmds
[j
] != 0; ++j
)
1898 if (streq (sh_cmds
[j
], new_argv
[0]))
1902 if (new_argv
[0] == 0)
1903 /* Line was empty. */
1909 /* We must use the shell. */
1913 /* Free the old argument list we were working on. */
1915 free ((void *)new_argv
);
1919 * Not eating this whitespace caused things like
1923 * which gave the shell fits. I think we have to eat
1924 * whitespace here, but this code should be considered
1925 * suspicious if things start failing....
1928 /* Make sure not to bother processing an empty line. */
1929 while (isspace (*line
))
1935 #if defined(__MSDOS__) || defined(WIN32)
1938 * only come here if no sh.exe command
1940 if (no_default_sh_exe
)
1947 dos_bname
= tempnam (".", "mk");
1948 for (i
= 0; dos_bname
[i
] != '\0'; ++i
)
1949 if (dos_bname
[i
] == '/')
1950 dos_bname
[i
] = '\\';
1951 dos_bename
= (char *) xmalloc (strlen (dos_bname
) + 5);
1952 strcpy (dos_bename
, dos_bname
);
1953 strcat (dos_bname
, ".bat");
1954 strcat (dos_bename
, ".err");
1956 batch
= fopen (dos_bename
, "w"); /* Create a file. */
1959 batch
= fopen (dos_bname
, "w");
1960 fputs ("@echo off\n", batch
);
1961 fputs (line
, batch
);
1962 fprintf (batch
, "\nif errorlevel 1 del %s\n", dos_bename
);
1964 new_argv
= (char **) xmalloc(2 * sizeof(char *));
1965 new_argv
[0] = strdup (dos_bname
);
1975 buffer
= (char *)xmalloc (strlen (line
)+1);
1978 for (dptr
=buffer
; *ptr
; )
1980 if (*ptr
== '\\' && ptr
[1] == '\n')
1982 else if (*ptr
== '@') /* Kludge: multiline commands */
1992 new_argv
= (char **) xmalloc(2 * sizeof(char *));
1993 new_argv
[0] = buffer
;
1996 #else /* Not MSDOS or Amiga */
1999 * This is technically an else to the above 'if (no_default_sh_exe)',
2000 * but (IMHO) coding if-else across ifdef is dangerous.
2002 if (!no_default_sh_exe
)
2005 /* SHELL may be a multi-word command. Construct a command line
2006 "SHELL -c LINE", with all special chars in LINE escaped.
2007 Then recurse, expanding this command line to get the final
2010 unsigned int shell_len
= strlen (shell
);
2011 static char minus_c
[] = " -c ";
2012 unsigned int line_len
= strlen (line
);
2014 char *new_line
= (char *) alloca (shell_len
+ (sizeof (minus_c
) - 1)
2015 + (line_len
* 2) + 1);
2018 bcopy (shell
, ap
, shell_len
);
2020 bcopy (minus_c
, ap
, sizeof (minus_c
) - 1);
2021 ap
+= sizeof (minus_c
) - 1;
2022 for (p
= line
; *p
!= '\0'; ++p
)
2024 if (restp
!= NULL
&& *p
== '\n')
2029 else if (*p
== '\\' && p
[1] == '\n')
2031 /* Eat the backslash, the newline, and following whitespace,
2032 replacing it all with a single space (which is escaped
2036 /* If there is a tab after a backslash-newline,
2037 remove it from the source line which will be echoed,
2038 since it was most likely used to line
2039 up the continued line with the previous one. */
2050 if (*p
== '\\' || *p
== '\'' || *p
== '"'
2052 || index (sh_chars
, *p
) != 0)
2058 new_argv
= construct_command_argv_internal (new_line
, (char **) NULL
,
2059 (char *) 0, (char *) 0);
2061 #endif /* Not MSDOS nor Amiga. */
2066 /* Figure out the argument list necessary to run LINE as a command. Try to
2067 avoid using a shell. This routine handles only ' quoting, and " quoting
2068 when no backslash, $ or ` characters are seen in the quotes. Starting
2069 quotes may be escaped with a backslash. If any of the characters in
2070 sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
2071 is the first word of a line, the shell is used.
2073 If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
2074 If *RESTP is NULL, newlines will be ignored.
2076 FILE is the target whose commands these are. It is used for
2077 variable expansion for $(SHELL) and $(IFS). */
2080 construct_command_argv (line
, restp
, file
)
2081 char *line
, **restp
;
2088 /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
2089 int save
= warn_undefined_variables_flag
;
2090 warn_undefined_variables_flag
= 0;
2092 shell
= allocated_variable_expand_for_file ("$(SHELL)", file
);
2095 * Convert to forward slashes so that construct_command_argv_internal()
2099 char *p
= w32ify(shell
, 0);
2103 ifs
= allocated_variable_expand_for_file ("$(IFS)", file
);
2105 warn_undefined_variables_flag
= save
;
2108 argv
= construct_command_argv_internal (line
, restp
, shell
, ifs
);
2117 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
2135 #endif /* !HAPE_DUP2 && !_AMIGA */