Fix some bugs in variable pattern substitution (e.g. $(VAR:A=B)),
[make.git] / job.c
blobc45889331ff72efc6e5e92949c0afb6ab8a9d83e
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)
8 any later version.
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. */
20 #include "make.h"
22 #include <assert.h>
24 #include "job.h"
25 #include "debug.h"
26 #include "filedef.h"
27 #include "commands.h"
28 #include "variable.h"
29 #include "debug.h"
31 #include <string.h>
33 /* Default shell to use. */
34 #ifdef WINDOWS32
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;
60 #elif defined (VMS)
62 # include <descrip.h>
63 char default_shell[] = "";
64 int batch_mode_shell = 0;
66 #elif defined (__riscos__)
68 char default_shell[] = "";
69 int batch_mode_shell = 0;
71 #else
73 char default_shell[] = "/bin/sh";
74 int batch_mode_shell = 0;
76 #endif
78 #ifdef __MSDOS__
79 # include <process.h>
80 static int execute_by_shell;
81 static int dos_pid = 123;
82 int dos_status;
83 int dos_command_running;
84 #endif /* __MSDOS__ */
86 #ifdef _AMIGA
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;
92 #endif /* Amiga. */
94 #ifdef VMS
95 # ifndef __GNUC__
96 # include <processes.h>
97 # endif
98 # include <starlet.h>
99 # include <lib$routines.h>
100 #endif
102 #ifdef WINDOWS32
103 # include <windows.h>
104 # include <io.h>
105 # include <process.h>
106 # include "sub_proc.h"
107 # include "w32err.h"
108 # include "pathstuff.h"
109 #endif /* WINDOWS32 */
111 #ifdef __EMX__
112 # include <process.h>
113 #endif
115 #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
116 # include <sys/wait.h>
117 #endif
119 #ifdef HAVE_WAITPID
120 # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
121 #else /* Don't have waitpid. */
122 # ifdef HAVE_WAIT3
123 # ifndef wait3
124 extern int wait3 ();
125 # endif
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)
131 extern int wait ();
132 #endif
134 #ifndef HAVE_UNION_WAIT
136 # define WAIT_T int
138 # ifndef WTERMSIG
139 # define WTERMSIG(x) ((x) & 0x7f)
140 # endif
141 # ifndef WCOREDUMP
142 # define WCOREDUMP(x) ((x) & 0x80)
143 # endif
144 # ifndef WEXITSTATUS
145 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
146 # endif
147 # ifndef WIFSIGNALED
148 # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
149 # endif
150 # ifndef WIFEXITED
151 # define WIFEXITED(x) (WTERMSIG (x) == 0)
152 # endif
154 #else /* Have `union wait'. */
156 # define WAIT_T union wait
157 # ifndef WTERMSIG
158 # define WTERMSIG(x) ((x).w_termsig)
159 # endif
160 # ifndef WCOREDUMP
161 # define WCOREDUMP(x) ((x).w_coredump)
162 # endif
163 # ifndef WEXITSTATUS
164 # define WEXITSTATUS(x) ((x).w_retcode)
165 # endif
166 # ifndef WIFSIGNALED
167 # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
168 # endif
169 # ifndef WIFEXITED
170 # define WIFEXITED(x) (WTERMSIG(x) == 0)
171 # endif
173 #endif /* Don't have `union wait'. */
175 #ifdef VMS
176 static int vms_jobsefnmask = 0;
177 #endif /* !VMS */
179 #ifndef HAVE_UNISTD_H
180 extern int dup2 ();
181 extern int execve ();
182 extern void _exit ();
183 # ifndef VMS
184 extern int geteuid ();
185 extern int getegid ();
186 extern int setgid ();
187 extern int getgid ();
188 # endif
189 #endif
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 *));
206 #ifdef VMS
207 static void vmsWaitForChildren PARAMS ((int *));
208 #endif
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). */
228 int unixy_shell = 1;
230 /* Number of jobs started in the current second. */
232 unsigned long job_counter = 0;
235 #ifdef WINDOWS32
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 */
246 #ifdef __EMX__
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[] = {
253 "cmd.exe",
254 "cmd",
255 "4os2.exe",
256 "4os2",
257 "4dos.exe",
258 "4dos",
259 "command.com",
260 "command",
261 NULL
264 /* find the rightmost '/' or '\\' */
265 const char *name = strrchr (path, '/');
266 const char *p = strrchr (path, '\\');
267 unsigned i;
269 if (name && p) /* take the max */
270 name = (name > p) ? name : p;
271 else if (p) /* name must be 0 */
272 name = p;
273 else if (!name) /* name and p must be 0 */
274 name = path;
276 if (*name == '/' || *name == '\\') name++;
278 i = 0;
279 while (known_os2shells[i] != NULL) {
280 if (stricmp (name, known_os2shells[i]) == 0) /* strcasecmp() */
281 return 0; /* not a unix shell */
282 i++;
285 /* in doubt assume a unix like shell */
286 return 1;
288 #endif /* __EMX__ */
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. */
295 static void
296 child_error (char *target_name, int exit_code, int exit_sig, int coredump,
297 int ignored)
299 if (ignored && silent_flag)
300 return;
302 #ifdef VMS
303 if (!(exit_code & 1))
304 error (NILF,
305 (ignored ? _("*** [%s] Error 0x%x (ignored)")
306 : _("*** [%s] Error 0x%x")),
307 target_name, exit_code);
308 #else
309 if (exit_sig == 0)
310 error (NILF, ignored ? _("[%s] Error %d (ignored)") :
311 _("*** [%s] Error %d"),
312 target_name, exit_code);
313 else
314 error (NILF, "*** [%s] %s%s",
315 target_name, strsignal (exit_sig),
316 coredump ? _(" (core dumped)") : "");
317 #endif /* VMS */
320 #ifdef VMS
321 /* Wait for nchildren children to terminate */
322 static void
323 vmsWaitForChildren(int *status)
325 while (1)
327 if (!vms_jobsefnmask)
329 *status = 0;
330 return;
333 *status = sys$wflor (32, vms_jobsefnmask);
335 return;
338 /* Set up IO redirection. */
340 char *
341 vms_redirect (struct dsc$descriptor_s *desc, char *fname, char *ibuf)
343 char *fptr;
344 extern char *vmsify ();
346 ibuf++;
347 while (isspace ((unsigned char)*ibuf))
348 ibuf++;
349 fptr = ibuf;
350 while (*ibuf && !isspace ((unsigned char)*ibuf))
351 ibuf++;
352 *ibuf = 0;
353 if (strcmp (fptr, "/dev/null") != 0)
355 strcpy (fname, vmsify (fptr, 0));
356 if (strchr (fname, '.') == 0)
357 strcat (fname, ".");
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;
364 if (*fname == 0)
365 printf (_("Warning: Empty redirection\n"));
366 return ibuf;
370 /* found apostrophe at (p-1)
371 inc p until after closing apostrophe.
374 static char *
375 vms_handle_apos (char *p)
377 int alast;
379 #define SEPCHARS ",/()= "
381 alast = 0;
383 while (*p != 0)
385 if (*p == '"')
387 if (alast)
389 alast = 0;
390 p++;
392 else
394 p++;
395 if (strchr (SEPCHARS, *p))
396 break;
397 alast = 1;
400 else
401 p++;
404 return p;
407 #endif
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;
422 RETSIGTYPE
423 child_handler (int sig UNUSED)
425 ++dead_children;
427 if (job_rfd >= 0)
429 close (job_rfd);
430 job_rfd = -1;
433 #ifdef __EMX__
434 /* The signal handler must called only once! */
435 signal (SIGCHLD, SIG_DFL);
436 #endif
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. */
452 void
453 reap_children (int block, int err)
455 WAIT_T status;
456 /* Initially, assume we have some. */
457 int reap_more = 1;
459 #ifdef WAIT_NOHANG
460 # define REAP_MORE reap_more
461 #else
462 # define REAP_MORE dead_children
463 #endif
465 /* As long as:
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))
476 int remote = 0;
477 pid_t pid;
478 int exit_code, exit_sig, coredump;
479 register struct child *lastc, *c;
480 int child_failed;
481 int any_remote, any_local;
483 if (err && block)
485 /* We might block for a while, so let the user know why. */
486 fflush (stdout);
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)
508 --dead_children;
510 any_remote = 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)") : ""));
519 #ifdef VMS
520 break;
521 #endif
524 /* First, check for remote children. */
525 if (any_remote)
526 pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
527 else
528 pid = 0;
530 if (pid > 0)
531 /* We got a remote child. */
532 remote = 1;
533 else if (pid < 0)
535 /* A remote status command failed miserably. Punt. */
536 remote_status_lose:
537 pfatal_with_name ("remote_status");
539 else
541 /* No remote children. Check for local children. */
542 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
543 if (any_local)
545 #ifdef VMS
546 vmsWaitForChildren (&status);
547 pid = c->pid;
548 #else
549 #ifdef WAIT_NOHANG
550 if (!block)
551 pid = WAIT_NOHANG (&status);
552 else
553 #endif
554 pid = wait (&status);
555 #endif /* !VMS */
557 else
558 pid = 0;
560 if (pid < 0)
562 /* The wait*() failed miserably. Punt. */
563 pfatal_with_name ("wait");
565 else if (pid > 0)
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. */
573 if (job_counter)
574 --job_counter;
576 else
578 /* No local children are dead. */
579 reap_more = 0;
581 if (!block || !any_remote)
582 break;
584 /* Now try a blocking wait for a remote child. */
585 pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
586 if (pid < 0)
587 goto remote_status_lose;
588 else if (pid == 0)
589 /* No remote children either. Finally give up. */
590 break;
592 /* We got a remote child. */
593 remote = 1;
595 #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
597 #ifdef __MSDOS__
598 /* Life is very different on MSDOS. */
599 pid = dos_pid - 1;
600 status = dos_status;
601 exit_code = WEXITSTATUS (status);
602 if (exit_code == 0xff)
603 exit_code = -1;
604 exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
605 coredump = 0;
606 #endif /* __MSDOS__ */
607 #ifdef _AMIGA
608 /* Same on Amiga */
609 pid = amiga_pid - 1;
610 status = amiga_status;
611 exit_code = amiga_status;
612 exit_sig = 0;
613 coredump = 0;
614 #endif /* _AMIGA */
615 #ifdef WINDOWS32
617 HANDLE hPID;
618 int err;
619 exit_code = 0;
620 exit_sig = 0;
621 coredump = 0;
623 /* wait for anything to finish */
624 hPID = process_wait_for_any();
625 if (hPID)
628 /* was an error found on this process? */
629 err = process_last_err(hPID);
631 /* get exit data */
632 exit_code = process_exit_code(hPID);
634 if (err)
635 fprintf(stderr, "make (e=%d): %s",
636 exit_code, map_windows32_error_to_string(exit_code));
638 /* signal */
639 exit_sig = process_signal(hPID);
641 /* cleanup process */
642 process_cleanup(hPID);
644 coredump = 0;
646 pid = (pid_t) 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;
657 else
658 shell_function_completed = 1;
659 break;
662 child_failed = exit_sig != 0 || exit_code != 0;
664 /* Search for a child matching the deceased one. */
665 lastc = 0;
666 for (c = children; c != 0; lastc = c, c = c->next)
667 if (c->remote == remote && c->pid == pid)
668 break;
670 if (c == 0)
671 /* An unknown child died.
672 Ignore it; it was inherited from our invoker. */
673 continue;
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"),
683 c->sh_batch_file));
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. */
694 if (c->good_stdin)
695 good_stdin_used = 0;
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);
712 else
714 if (child_failed)
716 /* The commands failed, but we don't care. */
717 child_error (c->file->name,
718 exit_code, exit_sig, coredump, 1);
719 child_failed = 0;
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;
733 else
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. */
745 unblock_sigs ();
746 if (c->file->command_state == cs_running)
747 /* We successfully started the new command.
748 Loop to reap more children. */
749 continue;
752 if (c->file->update_status != 0)
753 /* We failed to start the commands. */
754 delete_child_targets (c);
756 else
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. */
784 block_sigs ();
786 /* There is now another slot open. */
787 if (job_slots_used > 0)
788 --job_slots_used;
790 /* Remove the child from the chain and free it. */
791 if (lastc == 0)
792 children = c->next;
793 else
794 lastc->next = c->next;
796 free_child (c);
798 unblock_sigs ();
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)
805 die (2);
807 /* Only block for one child. */
808 block = 0;
811 return;
814 /* Free the storage allocated for CHILD. */
816 static void
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)
825 char token = '+';
826 int r;
828 /* Write a job token back to the pipe. */
830 EINTRLOOP (r, write (job_fds[1], &token, 1));
831 if (r != 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. */
839 return;
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;
852 while (*ep != 0)
853 free (*ep++);
854 free ((char *) child->environment);
857 free ((char *) child);
860 #ifdef POSIX
861 extern sigset_t fatal_signal_set;
862 #endif
864 void
865 block_sigs (void)
867 #ifdef POSIX
868 (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
869 #else
870 # ifdef HAVE_SIGSETMASK
871 (void) sigblock (fatal_signal_mask);
872 # endif
873 #endif
876 #ifdef POSIX
877 void
878 unblock_sigs (void)
880 sigset_t empty;
881 sigemptyset (&empty);
882 sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
884 #endif
886 #ifdef MAKE_JOBSERVER
887 /* Set the child handler action flags to FLAGS. */
888 static void
889 set_child_handler_action_flags (int flags)
891 struct sigaction sa;
892 bzero ((char *) &sa, sizeof sa);
893 sa.sa_handler = child_handler;
894 sa.sa_flags = flags;
895 #if defined SIGCHLD
896 sigaction (SIGCHLD, &sa, NULL);
897 #endif
898 #if defined SIGCLD && SIGCLD != SIGCHLD
899 sigaction (SIGCLD, &sa, NULL);
900 #endif
902 #endif
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. */
912 static void
913 start_job_command (struct child *child)
915 #ifndef _AMIGA
916 static int bad_stdin = -1;
917 #endif
918 register char *p;
919 int flags;
920 #ifdef VMS
921 char *argv;
922 #else
923 char **argv;
924 #endif
926 /* If we have a completely empty commandset, stop now. */
927 if (!child->command_ptr)
928 goto next_command;
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;
938 while (*p != '\0')
940 if (*p == '@')
941 flags |= COMMANDS_SILENT;
942 else if (*p == '+')
943 flags |= COMMANDS_RECURSE;
944 else if (*p == '-')
945 child->noerror = 1;
946 else if (!isblank ((unsigned char)*p))
947 break;
948 ++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. */
963 char *end = 0;
964 #ifdef VMS
965 argv = p;
966 #else
967 argv = construct_command_argv (p, &end, child->file, &child->sh_batch_file);
968 #endif
969 if (end == NULL)
970 child->command_ptr = NULL;
971 else
973 *end++ = '\0';
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
981 error is 2. */
982 if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
984 #ifndef VMS
985 free (argv[0]);
986 free ((char *) argv);
987 #endif
988 child->file->update_status = 1;
989 notice_finished_file (child->file);
990 return;
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. */
997 #ifndef VMS
998 if (argv)
1000 free (argv[0]);
1001 free ((char *) argv);
1003 #endif
1004 argv = 0;
1007 if (argv == 0)
1009 next_command:
1010 #ifdef __MSDOS__
1011 execute_by_shell = 0; /* in case construct_command_argv sets it */
1012 #endif
1013 /* This line has no commands. Go to the next. */
1014 if (job_next_command (child))
1015 start_job_command (child);
1016 else
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);
1024 return;
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
1029 appear. */
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. */
1041 ++commands_started;
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
1047 printed, etc. */
1049 #if !defined(VMS) && !defined(_AMIGA)
1050 if (
1051 #if defined __MSDOS__ || defined (__EMX__)
1052 unixy_shell /* the test is complicated and we already did it */
1053 #else
1054 (argv[0] && !strcmp (argv[0], "/bin/sh"))
1055 #endif
1056 && (argv[1]
1057 && argv[1][0] == '-' && argv[1][1] == 'c' && argv[1][2] == '\0')
1058 && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
1059 && argv[3] == NULL)
1061 free (argv[0]);
1062 free ((char *) argv);
1063 goto next_command;
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))
1071 #ifndef VMS
1072 free (argv[0]);
1073 free ((char *) argv);
1074 #endif
1075 goto next_command;
1078 /* Flush the output streams so they won't have things written twice. */
1080 fflush (stdout);
1081 fflush (stderr);
1083 #ifndef VMS
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. */
1092 int pd[2];
1093 if (pipe (pd) == 0)
1095 /* Close the write side. */
1096 (void) close (pd[1]);
1097 /* Save the read side. */
1098 bad_stdin = pd[0];
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;
1117 #endif /* !VMS */
1119 child->deleted = 0;
1121 #ifndef _AMIGA
1122 /* Set up the environment for the child. */
1123 if (child->environment == 0)
1124 child->environment = target_environment (child->file);
1125 #endif
1127 #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
1129 #ifndef VMS
1130 /* start_waiting_job has set CHILD->remote if we can start a remote job. */
1131 if (child->remote)
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. */
1139 goto run_local;
1140 else
1142 if (child->good_stdin && !used_stdin)
1144 child->good_stdin = 0;
1145 good_stdin_used = 0;
1147 child->remote = is_remote;
1148 child->pid = id;
1151 else
1152 #endif /* !VMS */
1154 /* Fork the child process. */
1156 char **parent_environ;
1158 run_local:
1159 block_sigs ();
1161 child->remote = 0;
1163 #ifdef VMS
1165 if (!child_execute_job (argv, child)) {
1166 /* Fork failed! */
1167 perror_with_name ("vfork", "");
1168 goto error;
1171 #else
1173 parent_environ = environ;
1175 # ifdef __EMX__
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]);
1183 if (job_rfd >= 0)
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);
1189 if (child->pid < 0)
1191 /* spawn failed! */
1192 unblock_sigs ();
1193 perror_with_name ("spawn", "");
1194 goto error;
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);
1203 if (job_rfd >= 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. */
1213 unblock_sigs ();
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)
1219 close (job_fds[0]);
1220 close (job_fds[1]);
1222 if (job_rfd >= 0)
1223 close (job_rfd);
1225 child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
1226 argv, child->environment);
1228 else if (child->pid < 0)
1230 /* Fork failed! */
1231 unblock_sigs ();
1232 perror_with_name ("vfork", "");
1233 goto error;
1235 # endif /* !__EMX__ */
1236 #endif /* !VMS */
1239 #else /* __MSDOS__ or Amiga or WINDOWS32 */
1240 #ifdef __MSDOS__
1242 int proc_return;
1244 block_sigs ();
1245 dos_status = 0;
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. */
1262 if (unixy_shell)
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 */
1274 else
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). */
1283 unblock_sigs ();
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)
1289 dos_status |= 0xff;
1290 else
1291 dos_status |= (proc_return & 0xff);
1292 ++dead_children;
1293 child->pid = dos_pid++;
1295 #endif /* __MSDOS__ */
1296 #ifdef _AMIGA
1297 amiga_status = MyExecute (argv);
1299 ++dead_children;
1300 child->pid = amiga_pid++;
1301 if (amiga_batch_file)
1303 amiga_batch_file = 0;
1304 DeleteFile (amiga_bname); /* Ignore errors. */
1306 #endif /* Amiga */
1307 #ifdef WINDOWS32
1309 HANDLE hPID;
1310 char* arg0;
1312 /* make UNC paths safe for CreateProcess -- backslash format */
1313 arg0 = argv[0];
1314 if (arg0 && arg0[0] == '/' && arg0[1] == '/')
1315 for ( ; arg0 && *arg0; arg0++)
1316 if (*arg0 == '/')
1317 *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;
1326 else {
1327 int i;
1328 unblock_sigs();
1329 fprintf(stderr,
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. */
1341 ++job_counter;
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. */
1349 #ifndef VMS
1350 free (argv[0]);
1351 free ((char *) argv);
1352 #endif
1354 return;
1356 error:
1357 child->file->update_status = 2;
1358 notice_finished_file (child->file);
1359 return;
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. */
1366 static int
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
1382 to go down. */
1383 set_command_state (f, cs_running);
1384 c->next = waiting_jobs;
1385 waiting_jobs = c;
1386 return 0;
1389 /* Start the first command; reap_children will run later command lines. */
1390 start_job_command (c);
1392 switch (f->command_state)
1394 case cs_running:
1395 c->next = children;
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)") : ""));
1399 children = c;
1400 /* One more job slot is in use. */
1401 ++job_slots_used;
1402 unblock_sigs ();
1403 break;
1405 case cs_not_started:
1406 /* All the command lines turned out to be empty. */
1407 f->update_status = 0;
1408 /* FALLTHROUGH */
1410 case cs_finished:
1411 notice_finished_file (f);
1412 free_child (c);
1413 break;
1415 default:
1416 assert (f->command_state == cs_finished);
1417 break;
1420 return 1;
1423 /* Create a `struct child' for FILE and start its commands running. */
1425 void
1426 new_job (struct file *file)
1428 register struct commands *cmds = file->cmds;
1429 register struct child *c;
1430 char **lines;
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 $. */
1466 if (out != in)
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. */
1473 out += ref - in;
1474 in = ref;
1476 if (*ref == '(' || *ref == '{')
1478 char openparen = *ref;
1479 char closeparen = openparen == '(' ? ')' : '}';
1480 int count;
1481 char *p;
1483 *out++ = *in++; /* Copy OPENPAREN. */
1484 /* IN now points past the opening paren or brace.
1485 Count parens or braces until it is matched. */
1486 count = 0;
1487 while (*in != '\0')
1489 if (*in == closeparen && --count < 0)
1490 break;
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. */
1497 int quoted = 0;
1498 for (p = in - 1; p > ref && *p == '\\'; --p)
1499 quoted = !quoted;
1501 if (quoted)
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. */
1507 *out++ = *in++;
1508 else
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. */
1516 while (out > ref
1517 && isblank ((unsigned char)out[-1]))
1518 --out;
1520 /* Replace it all with a single space. */
1521 *out++ = ' ';
1524 else
1526 if (*in == openparen)
1527 ++count;
1529 *out++ = *in++;
1535 /* There are no more references in this line to worry about.
1536 Copy the remaining uninteresting text to the output. */
1537 if (out != in)
1538 strcpy (out, in);
1540 /* Finally, expand the line. */
1541 lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
1542 file);
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));
1550 c->file = file;
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. */
1560 if (job_slots != 0)
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)
1578 while (1)
1580 char token;
1581 int got_token;
1582 int saved_errno;
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. */
1588 if (!children)
1589 break;
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. */
1614 if (job_rfd < 0)
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. */
1624 if (!children)
1625 break;
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;
1631 #ifdef __EMX__
1632 /* The child handler must be turned off here. */
1633 signal (SIGCHLD, SIG_DFL);
1634 #endif
1635 set_child_handler_action_flags (SA_RESTART);
1637 /* If we got one, we're done here. */
1638 if (got_token == 1)
1640 DB (DB_JOBS, (_("Obtained token for child 0x%08lx (%s).\n"),
1641 (unsigned long int) c, c->file->name));
1642 break;
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"));
1650 if (errno == EBADF)
1651 DB (DB_JOBS, ("Read returned EBADF.\n"));
1653 #endif
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);
1665 return;
1668 /* Move CHILD's pointers to the next command for it to execute.
1669 Returns nonzero if there is another command. */
1671 static int
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;
1681 return 0;
1683 else
1684 /* Get the next line to run. */
1685 child->command_ptr = child->command_lines[child->command_line++];
1687 return 1;
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>,
1701 who writes:
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
1730 static int
1731 load_too_high (void)
1733 #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__)
1734 return 1;
1735 #else
1736 static double last_sec;
1737 static time_t last_now;
1738 double load, guess;
1739 time_t now;
1741 if (max_load_average < 0)
1742 return 0;
1744 /* Find the real system load average. */
1745 make_access ();
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)
1752 if (errno == 0)
1753 /* An errno value of zero means getloadavg is just unsupported. */
1754 error (NILF,
1755 _("cannot enforce load limits on this operating system"));
1756 else
1757 perror_with_name (_("cannot enforce load limit: "), "getloadavg");
1759 lossage = errno;
1760 load = 0;
1762 user_access ();
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. */
1766 now = time (NULL);
1767 if (last_now < now)
1769 if (last_now == now - 1)
1770 last_sec = LOAD_WEIGHT_B * job_counter;
1771 else
1772 last_sec = 0.0;
1774 job_counter = 0;
1775 last_now = now;
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;
1785 #endif
1788 /* Start jobs that are waiting for the load to be lower. */
1790 void
1791 start_waiting_jobs (void)
1793 struct child *job;
1795 if (waiting_jobs == 0)
1796 return;
1800 /* Check for recently deceased descendants. */
1801 reap_children (0, 0);
1803 /* Take a job off the waiting list. */
1804 job = waiting_jobs;
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);
1812 return;
1815 #ifndef WINDOWS32
1816 #ifdef VMS
1817 #include <descrip.h>
1818 #include <clidef.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)
1825 int status;
1826 register struct child *lastc, *c;
1827 int child_failed;
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. */
1838 lastc = 0;
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);
1841 #else
1842 c = child;
1843 #endif
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);
1853 else
1855 if (child_failed)
1857 /* The commands failed, but we don't care. */
1858 child_error (c->file->name, c->cstatus, 0, 0, 1);
1859 child_failed = 0;
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. */
1864 start_job (c);
1866 switch (c->file->command_state)
1868 case cs_running:
1869 /* Successfully started. */
1870 break;
1872 case cs_finished:
1873 if (c->file->update_status != 0) {
1874 /* We failed to start the commands. */
1875 delete_child_targets (c);
1877 break;
1879 default:
1880 error (NILF, _("internal error: `%s' command_state"),
1881 c->file->name);
1882 abort ();
1883 break;
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. */
1894 if (lastc == 0)
1895 children = c->next;
1896 else
1897 lastc->next = c->next;
1898 free_child (c);
1899 #endif /* RECURSIVEJOBS */
1901 /* There is now another slot open. */
1902 if (job_slots_used > 0)
1903 --job_slots_used;
1905 /* If the job failed, and the -k flag was not given, die. */
1906 if (child_failed && !keep_going_flag)
1907 die (EXIT_FAILURE);
1909 (void) sigsetmask (sigblock (0) & ~(fatal_signal_mask));
1911 return 1;
1914 /* VMS:
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 */
1920 #include <iodef.h>
1921 #include <libclidef.h>
1922 #include <ssdef.h>
1924 static int ctrlMask= LIB$M_CLI_CTRLY;
1925 static int oldCtrlMask;
1926 static int setupYAstTried= 0;
1927 static int pidToAbort= 0;
1928 static int chan= 0;
1930 static void reEnableAst(void) {
1931 lib$enable_ctrl (&oldCtrlMask,0);
1934 static astHandler (void) {
1935 if (pidToAbort) {
1936 sys$forcex (&pidToAbort, 0, SS$_ABORT);
1937 pidToAbort= 0;
1939 kill (getpid(),SIGQUIT);
1942 static void tryToSetupYAst(void) {
1943 $DESCRIPTOR(inputDsc,"SYS$COMMAND");
1944 int status;
1945 struct {
1946 short int status, count;
1947 int dvi;
1948 } iosb;
1950 setupYAstTried++;
1952 if (!chan) {
1953 status= sys$assign(&inputDsc,&chan,0,0);
1954 if (!(status&SS$_NORMAL)) {
1955 lib$signal(status);
1956 return;
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) {
1964 sys$dassgn(chan);
1965 #ifdef CTRLY_ENABLED_ANYWAY
1966 fprintf (stderr,
1967 _("-warning, CTRL-Y will leave sub-process(es) around.\n"));
1968 #else
1969 return;
1970 #endif
1972 else if (!(status&SS$_NORMAL)) {
1973 sys$dassgn(chan);
1974 lib$signal(status);
1975 return;
1978 /* called from AST handler ? */
1979 if (setupYAstTried>1)
1980 return;
1981 if (atexit(reEnableAst))
1982 fprintf (stderr,
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)) {
1986 lib$signal(status);
1987 return;
1991 child_execute_job (char *argv, struct child *child)
1993 int i;
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;
2003 int status;
2004 char *cmd = alloca (strlen (argv) + 512), *p, *q;
2005 char ifile[256], ofile[256], efile[256];
2006 char *comname = 0;
2007 char procname[100];
2009 /* Parse IO redirection. */
2011 ifile[0] = 0;
2012 ofile[0] = 0;
2013 efile[0] = 0;
2015 DB (DB_JOBS, ("child_execute_job (%s)\n", argv));
2017 while (isspace ((unsigned char)*argv))
2018 argv++;
2020 if (*argv == 0)
2021 return 0;
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++)
2032 switch (*p)
2034 case '#':
2035 *p-- = 0;
2036 *q-- = 0;
2037 break;
2038 case '\\':
2039 p++;
2040 if (*p == '\n')
2041 p++;
2042 if (isspace ((unsigned char)*p))
2044 do { p++; } while (isspace ((unsigned char)*p));
2045 p--;
2047 *q = *p;
2048 break;
2049 case '<':
2050 p = vms_redirect (&ifiledsc, ifile, p);
2051 *q = ' ';
2052 have_redirection = 1;
2053 break;
2054 case '>':
2055 have_redirection = 1;
2056 if (*(p-1) == '2')
2058 q--;
2059 if (strncmp (p, ">&1", 3) == 0)
2061 p += 3;
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;
2068 else
2070 p = vms_redirect (&efiledsc, efile, p);
2073 else
2075 p = vms_redirect (&ofiledsc, ofile, p);
2077 *q = ' ';
2078 break;
2079 case '\n':
2080 have_newline = 1;
2081 default:
2082 *q = *p;
2083 break;
2086 *q = *p;
2088 if (strncmp (cmd, "builtin_", 8) == 0)
2090 child->pid = 270163;
2091 child->efn = 0;
2092 child->cstatus = 1;
2094 DB (DB_JOBS, (_("BUILTIN [%s][%s]\n"), cmd, cmd+8));
2096 p = cmd + 8;
2098 if ((*(p) == 'c')
2099 && (*(p+1) == 'd')
2100 && ((*(p+2) == ' ') || (*(p+2) == '\t')))
2102 p += 3;
2103 while ((*p == ' ') || (*p == '\t'))
2104 p++;
2105 DB (DB_JOBS, (_("BUILTIN CD %s\n"), p));
2106 if (chdir (p))
2107 return 0;
2108 else
2109 return 1;
2111 else if ((*(p) == 'r')
2112 && (*(p+1) == 'm')
2113 && ((*(p+2) == ' ') || (*(p+2) == '\t')))
2115 int in_arg;
2117 /* rm */
2118 p += 3;
2119 while ((*p == ' ') || (*p == '\t'))
2120 p++;
2121 in_arg = 1;
2123 DB (DB_JOBS, (_("BUILTIN RM %s\n"), p));
2124 while (*p)
2126 switch (*p)
2128 case ' ':
2129 case '\t':
2130 if (in_arg)
2132 *p++ = ';';
2133 in_arg = 0;
2135 break;
2136 default:
2137 break;
2139 p++;
2142 else
2144 printf(_("Unknown builtin command '%s'\n"), cmd);
2145 fflush(stdout);
2146 return 0;
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))
2159 FILE *outfile;
2160 char c;
2161 char *sep;
2162 int alevel = 0; /* apostrophe level */
2164 if (strlen (cmd) == 0)
2166 printf (_("Error, empty command\n"));
2167 fflush (stdout);
2168 return 0;
2171 outfile = open_tmpfile (&comname, "sys$scratch:CMDXXXXXX.COM");
2172 if (outfile == 0)
2173 pfatal_with_name (_("fopen (temporary file)"));
2175 if (ifile[0])
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;
2182 if (efile[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;
2189 if (ofile[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;
2196 p = sep = q = cmd;
2197 for (c = '\n'; c; c = *q++)
2199 switch (c)
2201 case '\n':
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))
2205 p++;
2206 if (*p == '$')
2207 p++;
2208 while (isspace ((unsigned char)*p))
2209 p++;
2210 fwrite (p, 1, q - p, outfile);
2211 fputc ('$', outfile);
2212 fputc (' ', outfile);
2213 /* Reset variables. */
2214 p = sep = q;
2215 break;
2217 /* Nice places for line breaks are after strings, after
2218 comma or space and before slash. */
2219 case '"':
2220 q = vms_handle_apos (q);
2221 sep = q;
2222 break;
2223 case ',':
2224 case ' ':
2225 sep = q;
2226 break;
2227 case '/':
2228 case '\0':
2229 sep = q - 1;
2230 break;
2231 default:
2232 break;
2234 if (sep - p > 78)
2236 /* Enough stuff for a line. */
2237 fwrite (p, 1, sep - p, outfile);
2238 p = sep;
2239 if (*sep)
2241 /* The command continues. */
2242 fputc ('-', outfile);
2244 fputc ('\n', outfile);
2248 fwrite (p, 1, q - p, outfile);
2249 fputc ('\n', outfile);
2251 fclose (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;
2263 child->efn = 0;
2264 while (child->efn < 32 || child->efn > 63)
2266 status = lib$get_ef ((unsigned long *)&child->efn);
2267 if (!(status & 1))
2268 return 0;
2271 sys$clref (child->efn);
2273 vms_jobsefnmask |= (1 << (child->efn - 32));
2276 LIB$SPAWN [command-string]
2277 [,input-file]
2278 [,output-file]
2279 [,flags]
2280 [,process-name]
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)
2326 tryToSetupYAst();
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,
2333 0, 0,
2334 0, 0, 0);
2335 if (status & 1)
2337 pidToAbort= child->pid;
2338 status= sys$waitfr (child->efn);
2339 pidToAbort= 0;
2340 vmsHandleChildTerm(child);
2342 #else
2343 status = lib$spawn (&cmddsc,
2344 (ifiledsc.dsc$w_length == 0)?0:&ifiledsc,
2345 (ofiledsc.dsc$w_length == 0)?0:&ofiledsc,
2346 &spflags,
2347 &pnamedsc,
2348 &child->pid, &child->cstatus, &child->efn,
2349 vmsHandleChildTerm, child,
2350 0, 0, 0);
2351 #endif
2353 if (!(status & 1))
2355 printf (_("Error spawning, %d\n") ,status);
2356 fflush (stdout);
2357 switch (status)
2359 case 0x1c:
2360 errno = EPROCLIM;
2361 break;
2362 default:
2363 errno = EFAIL;
2367 if (comname && !ISDB (DB_JOBS))
2368 unlink (comname);
2370 return (status & 1);
2373 #else /* !VMS */
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)
2380 int pid;
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 */
2387 if (save_stdin < 0)
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. */
2399 if (stdin_fd != 0)
2400 (void) dup2 (stdin_fd, 0);
2401 if (stdout_fd != 1)
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 */
2406 if (stdin_fd != 0)
2407 CLOSE_ON_EXEC (stdin_fd);
2408 if (stdout_fd != 1)
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. */
2415 if (stdin_fd != 0)
2417 if (dup2 (save_stdin, 0) != 0)
2418 fatal (NILF, _("Could not restore stdin\n"));
2419 else
2420 close (save_stdin);
2423 if (stdout_fd != 1)
2425 if (dup2 (save_stdout, 1) != 1)
2426 fatal (NILF, _("Could not restore stdout\n"));
2427 else
2428 close (save_stdout);
2431 return pid;
2434 #elif !defined (_AMIGA) && !defined (__MSDOS__)
2436 /* UNIX:
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. */
2440 void
2441 child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
2443 if (stdin_fd != 0)
2444 (void) dup2 (stdin_fd, 0);
2445 if (stdout_fd != 1)
2446 (void) dup2 (stdout_fd, 1);
2447 if (stdin_fd != 0)
2448 (void) close (stdin_fd);
2449 if (stdout_fd != 1)
2450 (void) close (stdout_fd);
2452 /* Run the command. */
2453 exec_command (argv, envp);
2455 #endif /* !AMIGA && !__MSDOS__ */
2456 #endif /* !VMS */
2457 #endif /* !WINDOWS32 */
2459 #ifndef _AMIGA
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. */
2464 # ifdef __EMX__
2466 # else
2467 void
2468 # endif
2469 exec_command (char **argv, char **envp)
2471 #ifdef VMS
2472 /* to work around a problem with signals and execve: ignore them */
2473 #ifdef SIGCHLD
2474 signal (SIGCHLD,SIG_IGN);
2475 #endif
2476 /* Run the program. */
2477 execve (argv[0], argv, envp);
2478 perror_with_name ("execve: ", argv[0]);
2479 _exit (EXIT_FAILURE);
2480 #else
2481 #ifdef WINDOWS32
2482 HANDLE hPID;
2483 HANDLE hWaitPID;
2484 int err = 0;
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)
2496 int i;
2497 fprintf(stderr,
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);
2503 exit(EXIT_FAILURE);
2506 /* wait and reap last child */
2507 hWaitPID = process_wait_for_any();
2508 while (hWaitPID)
2510 /* was an error found on this process? */
2511 err = process_last_err(hWaitPID);
2513 /* get exit data */
2514 exit_code = process_exit_code(hWaitPID);
2516 if (err)
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)
2525 break;
2526 else
2527 fprintf(stderr,
2528 _("make reaped child pid %d, still waiting for pid %d\n"),
2529 hWaitPID, hPID);
2532 /* return child's exit code as our exit code */
2533 exit(exit_code);
2535 #else /* !WINDOWS32 */
2537 # ifdef __EMX__
2538 int pid;
2539 # endif
2541 /* Be the user, permanently. */
2542 child_access ();
2544 # ifdef __EMX__
2546 /* Run the program. */
2547 pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
2549 if (pid >= 0)
2550 return pid;
2552 /* the file might have a strange shell extension */
2553 if (errno == ENOENT)
2554 errno = ENOEXEC;
2556 # else
2558 /* Run the program. */
2559 environ = envp;
2560 execvp (argv[0], argv);
2562 # endif /* !__EMX__ */
2564 switch (errno)
2566 case ENOENT:
2567 error (NILF, _("%s: Command not found"), argv[0]);
2568 break;
2569 case ENOEXEC:
2571 /* The file is not executable. Try it as a shell script. */
2572 extern char *getenv ();
2573 char *shell;
2574 char **new_argv;
2575 int argc;
2576 int i=1;
2578 # ifdef __EMX__
2579 /* Do not use $SHELL from the environment */
2580 struct variable *p = lookup_variable ("SHELL", 5);
2581 if (p)
2582 shell = p->value;
2583 else
2584 shell = 0;
2585 # else
2586 shell = getenv ("SHELL");
2587 # endif
2588 if (shell == 0)
2589 shell = default_shell;
2591 argc = 1;
2592 while (argv[argc] != 0)
2593 ++argc;
2595 # ifdef __EMX__
2596 if (!unixy_shell)
2597 ++argc;
2598 # endif
2600 new_argv = (char **) alloca ((1 + argc + 1) * sizeof (char *));
2601 new_argv[0] = shell;
2603 # ifdef __EMX__
2604 if (!unixy_shell)
2606 new_argv[1] = "/c";
2607 ++i;
2608 --argc;
2610 # endif
2612 new_argv[i] = argv[0];
2613 while (argc > 0)
2615 new_argv[i + argc] = argv[argc];
2616 --argc;
2619 # ifdef __EMX__
2620 pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
2621 if (pid >= 0)
2622 break;
2623 # else
2624 execvp (shell, new_argv);
2625 # endif
2626 if (errno == ENOENT)
2627 error (NILF, _("%s: Shell program not found"), shell);
2628 else
2629 perror_with_name ("execvp: ", shell);
2630 break;
2633 # ifdef __EMX__
2634 case EINVAL:
2635 /* this nasty error was driving me nuts :-( */
2636 error (NILF, _("spawnvpe: environment space might be exhausted"));
2637 /* FALLTHROUGH */
2638 # endif
2640 default:
2641 perror_with_name ("execvp: ", argv[0]);
2642 break;
2645 # ifdef __EMX__
2646 return pid;
2647 # else
2648 _exit (127);
2649 # endif
2650 #endif /* !WINDOWS32 */
2651 #endif /* !VMS */
2653 #else /* On Amiga */
2654 void exec_command (char **argv)
2656 MyExecute (argv);
2659 void clean_tmp (void)
2661 DeleteFile (amiga_bname);
2664 #endif /* On Amiga */
2666 #ifndef VMS
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). */
2680 static char **
2681 construct_command_argv_internal (char *line, char **restp, char *shell,
2682 char *ifs, char **batch_filename_ptr)
2684 #ifdef __MSDOS__
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",
2714 "vol", ":", 0 };
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",
2722 0 };
2724 char *sh_chars;
2725 char **sh_cmds;
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",
2734 "vol", ":", 0 };
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",
2751 0 };
2752 char *sh_chars;
2753 char **sh_cmds;
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",
2761 0 };
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
2777 "echo",
2778 #endif
2779 0 };
2780 char* sh_chars;
2781 char** sh_cmds;
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 };
2792 #endif
2793 register int i;
2794 register char *p;
2795 register char *ap;
2796 char *end;
2797 int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
2798 char **new_argv = 0;
2799 #ifdef WINDOWS32
2800 int slow_flag = 0;
2802 if (no_default_sh_exe) {
2803 sh_cmds = sh_cmds_dos;
2804 sh_chars = sh_chars_dos;
2805 } else {
2806 sh_cmds = sh_cmds_sh;
2807 sh_chars = sh_chars_sh;
2809 #endif /* WINDOWS32 */
2811 if (restp != NULL)
2812 *restp = NULL;
2814 /* Make sure not to bother processing an empty line. */
2815 while (isblank ((unsigned char)*line))
2816 ++line;
2817 if (*line == '\0')
2818 return 0;
2820 /* See if it is safe to parse commands internally. */
2821 if (shell == 0)
2822 shell = default_shell;
2823 #ifdef WINDOWS32
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 : ""));
2831 if (s1)
2832 free (s1);
2833 if (s2)
2834 free (s2);
2836 if (slow_flag)
2837 goto slow;
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);
2851 if (unixy_shell)
2853 sh_chars = sh_chars_sh;
2854 sh_cmds = sh_cmds_sh;
2856 else
2858 sh_chars = sh_chars_dos;
2859 sh_cmds = sh_cmds_dos;
2860 # ifdef __EMX__
2861 if (_osmode == OS2_MODE)
2863 sh_chars = sh_chars_os2;
2864 sh_cmds = sh_cmds_os2;
2866 # endif
2868 #else /* !__MSDOS__ */
2869 else if (strcmp (shell, default_shell))
2870 goto slow;
2871 #endif /* !__MSDOS__ && !__EMX__ */
2872 #endif /* not WINDOWS32 */
2874 if (ifs != 0)
2875 for (ap = ifs; *ap != '\0'; ++ap)
2876 if (*ap != ' ' && *ap != '\t' && *ap != '\n')
2877 goto slow;
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);
2886 end = ap + i;
2888 /* I is how many complete arguments have been found. */
2889 i = 0;
2890 instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
2891 for (p = line; *p != '\0'; ++p)
2893 if (ap > end)
2894 abort ();
2896 if (instring)
2898 string_char:
2899 /* Inside a string, just copy any char except a closing quote
2900 or a backslash-newline combination. */
2901 if (*p == instring)
2903 instring = 0;
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. */
2912 *restp = p;
2913 goto end_of_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)
2920 goto slow;
2921 else
2922 *ap++ = *p;
2924 else if (strchr (sh_chars, *p) != 0)
2925 /* Not inside a string, but it's a special char. */
2926 goto slow;
2927 #ifdef __MSDOS__
2928 else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
2929 /* `...' is a wildcard in DJGPP. */
2930 goto slow;
2931 #endif
2932 else
2933 /* Not a special char. */
2934 switch (*p)
2936 case '=':
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
2940 shell flags. */
2941 if (! seen_nonequals && unixy_shell)
2942 goto slow;
2943 word_has_equals = 1;
2944 *ap++ = '=';
2945 break;
2947 case '\\':
2948 /* Backslash-newline combinations are eaten. */
2949 if (p[1] == '\n')
2951 swallow_escaped_newline:
2953 /* Eat the backslash, the newline, and following whitespace,
2954 replacing it all with a single space. */
2955 p += 2;
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. */
2961 if (*p == '\t')
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));
2967 if (instring)
2968 goto string_char;
2969 else
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. */
2975 goto end_of_arg;
2976 else
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. */
2990 #ifdef __MSDOS__
2991 /* A dot is only special as part of the "..."
2992 wildcard. */
2993 if (strneq (p + 1, ".\\.\\.", 5))
2995 *ap++ = '.';
2996 *ap++ = '.';
2997 p += 4;
2999 else
3000 #endif
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 */
3005 --p;
3006 #endif /* HAVE_DOS_PATHS */
3008 /* Copy and skip the following char. */
3009 *ap++ = *++p;
3011 break;
3013 case '\'':
3014 case '"':
3015 instring = *p;
3016 break;
3018 case '\n':
3019 if (restp != NULL)
3021 /* End of the command line. */
3022 *restp = p;
3023 goto end_of_line;
3025 else
3026 /* Newlines are not special. */
3027 *ap++ = '\n';
3028 break;
3030 case ' ':
3031 case '\t':
3032 end_of_arg:
3033 /* We have the end of an argument.
3034 Terminate the text of the argument. */
3035 *ap++ = '\0';
3036 new_argv[++i] = ap;
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. */
3045 goto slow;
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. */
3051 if (i == 1)
3053 register int j;
3054 for (j = 0; sh_cmds[j] != 0; ++j)
3056 if (streq (sh_cmds[j], new_argv[0]))
3057 goto slow;
3058 # ifdef __EMX__
3059 /* Non-Unix shells are case insensitive. */
3060 if (!unixy_shell
3061 && strcasecmp (sh_cmds[j], new_argv[0]) == 0)
3062 goto slow;
3063 # endif
3067 /* Ignore multiple whitespace chars. */
3068 p = next_token (p);
3069 /* Next iteration should examine the first nonwhite char. */
3070 --p;
3071 break;
3073 default:
3074 *ap++ = *p;
3075 break;
3078 end_of_line:
3080 if (instring)
3081 /* Let the shell deal with an unterminated quote. */
3082 goto slow;
3084 /* Terminate the last argument and the argument list. */
3086 *ap = '\0';
3087 if (new_argv[i][0] != '\0' || last_argument_was_empty)
3088 ++i;
3089 new_argv[i] = 0;
3091 if (i == 1)
3093 register int j;
3094 for (j = 0; sh_cmds[j] != 0; ++j)
3095 if (streq (sh_cmds[j], new_argv[0]))
3096 goto slow;
3099 if (new_argv[0] == 0)
3100 /* Line was empty. */
3101 return 0;
3103 return new_argv;
3105 slow:;
3106 /* We must use the shell. */
3108 if (new_argv != 0)
3110 /* Free the old argument list we were working on. */
3111 free (new_argv[0]);
3112 free ((void *)new_argv);
3115 #ifdef __MSDOS__
3116 execute_by_shell = 1; /* actually, call `system' if shell isn't unixy */
3117 #endif
3119 #ifdef _AMIGA
3121 char *ptr;
3122 char *buffer;
3123 char *dptr;
3125 buffer = (char *)xmalloc (strlen (line)+1);
3127 ptr = line;
3128 for (dptr=buffer; *ptr; )
3130 if (*ptr == '\\' && ptr[1] == '\n')
3131 ptr += 2;
3132 else if (*ptr == '@') /* Kludge: multiline commands */
3134 ptr += 2;
3135 *dptr++ = '\n';
3137 else
3138 *dptr++ = *ptr++;
3140 *dptr = 0;
3142 new_argv = (char **) xmalloc (2 * sizeof (char *));
3143 new_argv[0] = buffer;
3144 new_argv[1] = 0;
3146 #else /* Not Amiga */
3147 #ifdef WINDOWS32
3149 * Not eating this whitespace caused things like
3151 * sh -c "\n"
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))
3160 ++line;
3161 if (*line == '\0')
3162 return 0;
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
3168 argument list. */
3170 unsigned int shell_len = strlen (shell);
3171 #ifndef VMS
3172 static char minus_c[] = " -c ";
3173 #else
3174 static char minus_c[] = "";
3175 #endif
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? */
3183 if (!unixy_shell)
3184 minus_c[1] = '/'; /* " /c " */
3185 # endif
3187 ap = new_line;
3188 bcopy (shell, ap, shell_len);
3189 ap += shell_len;
3190 bcopy (minus_c, ap, sizeof (minus_c) - 1);
3191 ap += sizeof (minus_c) - 1;
3192 command_ptr = ap;
3193 for (p = line; *p != '\0'; ++p)
3195 if (restp != NULL && *p == '\n')
3197 *restp = p;
3198 break;
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
3204 from the shell). */
3205 p += 2;
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. */
3211 if (*p == '\t')
3212 bcopy (p + 1, p, strlen (p));
3214 p = next_token (p);
3215 --p;
3216 if (unixy_shell && !batch_mode_shell)
3217 *ap++ = '\\';
3218 *ap++ = ' ';
3219 continue;
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))
3227 *ap++ = '\\';
3228 #ifdef __MSDOS__
3229 else if (unixy_shell && strneq (p, "...", 3))
3231 /* The case of `...' wildcard again. */
3232 strcpy (ap, "\\.\\.\\");
3233 ap += 5;
3234 p += 2;
3236 #endif
3237 *ap++ = *p;
3239 if (ap == new_line + shell_len + sizeof (minus_c) - 1)
3240 /* Line was empty. */
3241 return 0;
3242 *ap = '\0';
3244 #ifdef WINDOWS32
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) {
3249 FILE* batch = NULL;
3250 int id = GetCurrentProcessId();
3251 PATH_VAR(fbuf);
3252 char* fname = NULL;
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 */
3263 if (!unixy_shell) {
3264 fname = *batch_filename_ptr;
3265 for (i = 0; fname[i] != '\0'; ++i)
3266 if (fname[i] == '/')
3267 fname[i] = '\\';
3268 strcat(*batch_filename_ptr, ".bat");
3269 } else {
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");
3278 if (!unixy_shell)
3279 fputs ("@echo off\n", batch);
3280 fputs (command_ptr, batch);
3281 fputc ('\n', batch);
3282 fclose (batch);
3284 /* create argv */
3285 new_argv = (char **) xmalloc(3 * sizeof (char *));
3286 if (unixy_shell) {
3287 new_argv[0] = xstrdup (shell);
3288 new_argv[1] = *batch_filename_ptr; /* only argv[0] gets freed later */
3289 } else {
3290 new_argv[0] = xstrdup (*batch_filename_ptr);
3291 new_argv[1] = NULL;
3293 new_argv[2] = NULL;
3294 } else
3295 #endif /* WINDOWS32 */
3296 if (unixy_shell)
3297 new_argv = construct_command_argv_internal (new_line, (char **) NULL,
3298 (char *) 0, (char *) 0,
3299 (char **) 0);
3300 #ifdef __EMX__
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
3305 manually. */
3306 size_t line_len = strlen (line);
3307 char *p = new_line;
3308 char *q = new_line;
3309 memcpy (new_line, line, line_len + 1);
3310 /* replace all backslash-newline combination and also following tabs */
3311 while (*q != '\0')
3313 if (q[0] == '\\' && q[1] == '\n')
3315 q += 2; /* remove '\\' and '\n' */
3316 if (q[0] == '\t')
3317 q++; /* remove 1st tab in the next line */
3319 else
3320 *p++ = *q++;
3322 *p = '\0';
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';
3349 # endif
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);
3366 new_argv[3] = NULL;
3369 #elif defined(__MSDOS__)
3370 else
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';
3382 #else
3383 else
3384 fatal (NILF, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
3385 __FILE__, __LINE__);
3386 #endif
3388 #endif /* ! AMIGA */
3390 return new_argv;
3392 #endif /* !VMS */
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). */
3407 char **
3408 construct_command_argv (char *line, char **restp, struct file *file,
3409 char **batch_filename_ptr)
3411 char *shell, *ifs;
3412 char **argv;
3414 #ifdef VMS
3415 char *cptr;
3416 int argc;
3418 argc = 0;
3419 cptr = line;
3420 for (;;)
3422 while ((*cptr != 0)
3423 && (isspace ((unsigned char)*cptr)))
3424 cptr++;
3425 if (*cptr == 0)
3426 break;
3427 while ((*cptr != 0)
3428 && (!isspace((unsigned char)*cptr)))
3429 cptr++;
3430 argc++;
3433 argv = (char **)malloc (argc * sizeof (char *));
3434 if (argv == 0)
3435 abort ();
3437 cptr = line;
3438 argc = 0;
3439 for (;;)
3441 while ((*cptr != 0)
3442 && (isspace ((unsigned char)*cptr)))
3443 cptr++;
3444 if (*cptr == 0)
3445 break;
3446 DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
3447 argv[argc++] = cptr;
3448 while ((*cptr != 0)
3449 && (!isspace((unsigned char)*cptr)))
3450 cptr++;
3451 if (*cptr != 0)
3452 *cptr++ = 0;
3454 #else
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);
3461 #ifdef WINDOWS32
3463 * Convert to forward slashes so that construct_command_argv_internal()
3464 * is not confused.
3466 if (shell) {
3467 char *p = w32ify (shell, 0);
3468 strcpy (shell, p);
3470 #endif
3471 #ifdef __EMX__
3473 static const char *unixroot = NULL;
3474 static const char *last_shell = "";
3475 static int init = 0;
3476 if (init == 0)
3478 unixroot = getenv ("UNIXROOT");
3479 /* unixroot must be NULL or not empty */
3480 if (unixroot && unixroot[0] == '\0') unixroot = NULL;
3481 init = 1;
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! */
3505 /* free(shell); */
3506 shell = buf;
3507 else
3508 free (buf);
3510 else
3511 free (buf);
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);
3523 free (shell);
3524 free (ifs);
3525 #endif /* !VMS */
3526 return argv;
3529 #if !defined(HAVE_DUP2) && !defined(_AMIGA)
3531 dup2 (int old, int new)
3533 int fd;
3535 (void) close (new);
3536 fd = dup (old);
3537 if (fd != new)
3539 (void) close (fd);
3540 errno = EMFILE;
3541 return -1;
3544 return fd;
3546 #endif /* !HAPE_DUP2 && !_AMIGA */