| 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.
|
|---|
| 4 |
|
|---|
| 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.
|
|---|
| 9 |
|
|---|
| 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.
|
|---|
| 14 |
|
|---|
| 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. */
|
|---|
| 19 |
|
|---|
| 20 | #include "make.h"
|
|---|
| 21 |
|
|---|
| 22 | #include <assert.h>
|
|---|
| 23 |
|
|---|
| 24 | #include "job.h"
|
|---|
| 25 | #include "debug.h"
|
|---|
| 26 | #include "filedef.h"
|
|---|
| 27 | #include "commands.h"
|
|---|
| 28 | #include "variable.h"
|
|---|
| 29 | #include "debug.h"
|
|---|
| 30 |
|
|---|
| 31 | #include <string.h>
|
|---|
| 32 |
|
|---|
| 33 | #ifdef MAKE_DLLSHELL
|
|---|
| 34 | #include <dlfcn.h>
|
|---|
| 35 | #endif
|
|---|
| 36 |
|
|---|
| 37 | /* Default shell to use. */
|
|---|
| 38 | #ifdef WINDOWS32
|
|---|
| 39 |
|
|---|
| 40 | char *default_shell = "sh.exe";
|
|---|
| 41 | int no_default_sh_exe = 1;
|
|---|
| 42 | int batch_mode_shell = 1;
|
|---|
| 43 |
|
|---|
| 44 | #elif defined (_AMIGA)
|
|---|
| 45 |
|
|---|
| 46 | char default_shell[] = "";
|
|---|
| 47 | extern int MyExecute (char **);
|
|---|
| 48 | int batch_mode_shell = 0;
|
|---|
| 49 |
|
|---|
| 50 | #elif defined (__MSDOS__)
|
|---|
| 51 |
|
|---|
| 52 | /* The default shell is a pointer so we can change it if Makefile
|
|---|
| 53 | says so. It is without an explicit path so we get a chance
|
|---|
| 54 | to search the $PATH for it (since MSDOS doesn't have standard
|
|---|
| 55 | directories we could trust). */
|
|---|
| 56 | char *default_shell = "command.com";
|
|---|
| 57 | int batch_mode_shell = 0;
|
|---|
| 58 |
|
|---|
| 59 | #elif defined (__EMX__)
|
|---|
| 60 |
|
|---|
| 61 | char *default_shell = "sh.exe";
|
|---|
| 62 | int batch_mode_shell = 0;
|
|---|
| 63 |
|
|---|
| 64 | #elif defined (VMS)
|
|---|
| 65 |
|
|---|
| 66 | # include <descrip.h>
|
|---|
| 67 | char default_shell[] = "";
|
|---|
| 68 | int batch_mode_shell = 0;
|
|---|
| 69 |
|
|---|
| 70 | #else
|
|---|
| 71 |
|
|---|
| 72 | char default_shell[] = "/bin/sh";
|
|---|
| 73 | int batch_mode_shell = 0;
|
|---|
| 74 |
|
|---|
| 75 | #endif
|
|---|
| 76 |
|
|---|
| 77 | #ifdef __MSDOS__
|
|---|
| 78 | # include <process.h>
|
|---|
| 79 | static int execute_by_shell;
|
|---|
| 80 | static int dos_pid = 123;
|
|---|
| 81 | int dos_status;
|
|---|
| 82 | int dos_command_running;
|
|---|
| 83 | #endif /* __MSDOS__ */
|
|---|
| 84 |
|
|---|
| 85 | #ifdef _AMIGA
|
|---|
| 86 | # include <proto/dos.h>
|
|---|
| 87 | static int amiga_pid = 123;
|
|---|
| 88 | static int amiga_status;
|
|---|
| 89 | static char amiga_bname[32];
|
|---|
| 90 | static int amiga_batch_file;
|
|---|
| 91 | #endif /* Amiga. */
|
|---|
| 92 |
|
|---|
| 93 | #ifdef VMS
|
|---|
| 94 | # ifndef __GNUC__
|
|---|
| 95 | # include <processes.h>
|
|---|
| 96 | # endif
|
|---|
| 97 | # include <starlet.h>
|
|---|
| 98 | # include <lib$routines.h>
|
|---|
| 99 | #endif
|
|---|
| 100 |
|
|---|
| 101 | #ifdef WINDOWS32
|
|---|
| 102 | # include <windows.h>
|
|---|
| 103 | # include <io.h>
|
|---|
| 104 | # include <process.h>
|
|---|
| 105 | # include "sub_proc.h"
|
|---|
| 106 | # include "w32err.h"
|
|---|
| 107 | # include "pathstuff.h"
|
|---|
| 108 | #endif /* WINDOWS32 */
|
|---|
| 109 |
|
|---|
| 110 | #ifdef __EMX__
|
|---|
| 111 | # include <process.h>
|
|---|
| 112 | #endif
|
|---|
| 113 |
|
|---|
| 114 | #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
|
|---|
| 115 | # include <sys/wait.h>
|
|---|
| 116 | #endif
|
|---|
| 117 |
|
|---|
| 118 | #ifdef HAVE_WAITPID
|
|---|
| 119 | # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
|
|---|
| 120 | #else /* Don't have waitpid. */
|
|---|
| 121 | # ifdef HAVE_WAIT3
|
|---|
| 122 | # ifndef wait3
|
|---|
| 123 | extern int wait3 ();
|
|---|
| 124 | # endif
|
|---|
| 125 | # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
|
|---|
| 126 | # endif /* Have wait3. */
|
|---|
| 127 | #endif /* Have waitpid. */
|
|---|
| 128 |
|
|---|
| 129 | #if !defined (wait) && !defined (POSIX)
|
|---|
| 130 | extern int wait ();
|
|---|
| 131 | #endif
|
|---|
| 132 |
|
|---|
| 133 | #ifndef HAVE_UNION_WAIT
|
|---|
| 134 |
|
|---|
| 135 | # define WAIT_T int
|
|---|
| 136 |
|
|---|
| 137 | # ifndef WTERMSIG
|
|---|
| 138 | # define WTERMSIG(x) ((x) & 0x7f)
|
|---|
| 139 | # endif
|
|---|
| 140 | # ifndef WCOREDUMP
|
|---|
| 141 | # define WCOREDUMP(x) ((x) & 0x80)
|
|---|
| 142 | # endif
|
|---|
| 143 | # ifndef WEXITSTATUS
|
|---|
| 144 | # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
|
|---|
| 145 | # endif
|
|---|
| 146 | # ifndef WIFSIGNALED
|
|---|
| 147 | # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
|
|---|
| 148 | # endif
|
|---|
| 149 | # ifndef WIFEXITED
|
|---|
| 150 | # define WIFEXITED(x) (WTERMSIG (x) == 0)
|
|---|
| 151 | # endif
|
|---|
| 152 |
|
|---|
| 153 | #else /* Have `union wait'. */
|
|---|
| 154 |
|
|---|
| 155 | # define WAIT_T union wait
|
|---|
| 156 | # ifndef WTERMSIG
|
|---|
| 157 | # define WTERMSIG(x) ((x).w_termsig)
|
|---|
| 158 | # endif
|
|---|
| 159 | # ifndef WCOREDUMP
|
|---|
| 160 | # define WCOREDUMP(x) ((x).w_coredump)
|
|---|
| 161 | # endif
|
|---|
| 162 | # ifndef WEXITSTATUS
|
|---|
| 163 | # define WEXITSTATUS(x) ((x).w_retcode)
|
|---|
| 164 | # endif
|
|---|
| 165 | # ifndef WIFSIGNALED
|
|---|
| 166 | # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
|
|---|
| 167 | # endif
|
|---|
| 168 | # ifndef WIFEXITED
|
|---|
| 169 | # define WIFEXITED(x) (WTERMSIG(x) == 0)
|
|---|
| 170 | # endif
|
|---|
| 171 |
|
|---|
| 172 | #endif /* Don't have `union wait'. */
|
|---|
| 173 |
|
|---|
| 174 | #ifdef VMS
|
|---|
| 175 | static int vms_jobsefnmask = 0;
|
|---|
| 176 | #endif /* !VMS */
|
|---|
| 177 |
|
|---|
| 178 | #ifndef HAVE_UNISTD_H
|
|---|
| 179 | extern int dup2 ();
|
|---|
| 180 | extern int execve ();
|
|---|
| 181 | extern void _exit ();
|
|---|
| 182 | # ifndef VMS
|
|---|
| 183 | extern int geteuid ();
|
|---|
| 184 | extern int getegid ();
|
|---|
| 185 | extern int setgid ();
|
|---|
| 186 | extern int getgid ();
|
|---|
| 187 | # endif
|
|---|
| 188 | #endif
|
|---|
| 189 |
|
|---|
| 190 | extern char *allocated_variable_expand_for_file PARAMS ((char *line, struct file *file));
|
|---|
| 191 |
|
|---|
| 192 | extern int getloadavg PARAMS ((double loadavg[], int nelem));
|
|---|
| 193 | extern int start_remote_job PARAMS ((char **argv, char **envp, int stdin_fd,
|
|---|
| 194 | int *is_remote, int *id_ptr, int *used_stdin));
|
|---|
| 195 | extern int start_remote_job_p PARAMS ((int));
|
|---|
| 196 | extern int remote_status PARAMS ((int *exit_code_ptr, int *signal_ptr,
|
|---|
| 197 | int *coredump_ptr, int block));
|
|---|
| 198 |
|
|---|
| 199 | RETSIGTYPE child_handler PARAMS ((int));
|
|---|
| 200 | static void free_child PARAMS ((struct child *));
|
|---|
| 201 | static void start_job_command PARAMS ((struct child *child));
|
|---|
| 202 | static int load_too_high PARAMS ((void));
|
|---|
| 203 | static int job_next_command PARAMS ((struct child *));
|
|---|
| 204 | static int start_waiting_job PARAMS ((struct child *));
|
|---|
| 205 | #ifdef VMS
|
|---|
| 206 | static void vmsWaitForChildren PARAMS ((int *));
|
|---|
| 207 | #endif
|
|---|
| 208 | #ifdef MAKE_DLLSHELL
|
|---|
| 209 | static int spawn_command PARAMS ((char **argv, char **envp, struct child *child));
|
|---|
| 210 | #endif
|
|---|
| 211 | |
|---|
| 212 |
|
|---|
| 213 | /* Chain of all live (or recently deceased) children. */
|
|---|
| 214 |
|
|---|
| 215 | struct child *children = 0;
|
|---|
| 216 |
|
|---|
| 217 | /* Number of children currently running. */
|
|---|
| 218 |
|
|---|
| 219 | unsigned int job_slots_used = 0;
|
|---|
| 220 |
|
|---|
| 221 | /* Nonzero if the `good' standard input is in use. */
|
|---|
| 222 |
|
|---|
| 223 | static int good_stdin_used = 0;
|
|---|
| 224 |
|
|---|
| 225 | /* Chain of children waiting to run until the load average goes down. */
|
|---|
| 226 |
|
|---|
| 227 | static struct child *waiting_jobs = 0;
|
|---|
| 228 |
|
|---|
| 229 | /* Non-zero if we use a *real* shell (always so on Unix). */
|
|---|
| 230 |
|
|---|
| 231 | int unixy_shell = 1;
|
|---|
| 232 |
|
|---|
| 233 | /* Number of jobs started in the current second. */
|
|---|
| 234 |
|
|---|
| 235 | unsigned long job_counter = 0;
|
|---|
| 236 |
|
|---|
| 237 | |
|---|
| 238 |
|
|---|
| 239 | #ifdef WINDOWS32
|
|---|
| 240 | /*
|
|---|
| 241 | * The macro which references this function is defined in make.h.
|
|---|
| 242 | */
|
|---|
| 243 | int
|
|---|
| 244 | w32_kill(int pid, int sig)
|
|---|
| 245 | {
|
|---|
| 246 | return ((process_kill(pid, sig) == TRUE) ? 0 : -1);
|
|---|
| 247 | }
|
|---|
| 248 | #endif /* WINDOWS32 */
|
|---|
| 249 |
|
|---|
| 250 | #ifdef __EMX__
|
|---|
| 251 | /* returns whether path is assumed to be a unix like shell. */
|
|---|
| 252 | int
|
|---|
| 253 | _is_unixy_shell (const char *path)
|
|---|
| 254 | {
|
|---|
| 255 | /* list of non unix shells */
|
|---|
| 256 | const char *known_os2shells[] = {
|
|---|
| 257 | "cmd.exe",
|
|---|
| 258 | "cmd",
|
|---|
| 259 | "4os2.exe",
|
|---|
| 260 | "4os2",
|
|---|
| 261 | "4dos.exe",
|
|---|
| 262 | "4dos",
|
|---|
| 263 | "command.com",
|
|---|
| 264 | "command",
|
|---|
| 265 | NULL
|
|---|
| 266 | };
|
|---|
| 267 |
|
|---|
| 268 | /* find the rightmost '/' or '\\' */
|
|---|
| 269 | const char *name = strrchr (path, '/');
|
|---|
| 270 | const char *p = strrchr (path, '\\');
|
|---|
| 271 | unsigned i;
|
|---|
| 272 |
|
|---|
| 273 | if (name && p) /* take the max */
|
|---|
| 274 | name = (name > p) ? name : p;
|
|---|
| 275 | else if (p) /* name must be 0 */
|
|---|
| 276 | name = p;
|
|---|
| 277 | else if (!name) /* name and p must be 0 */
|
|---|
| 278 | name = path;
|
|---|
| 279 |
|
|---|
| 280 | if (*name == '/' || *name == '\\') name++;
|
|---|
| 281 |
|
|---|
| 282 | i = 0;
|
|---|
| 283 | while (known_os2shells[i] != NULL) {
|
|---|
| 284 | if (stricmp (name, known_os2shells[i]) == 0) /* strcasecmp() */
|
|---|
| 285 | return 0; /* not a unix shell */
|
|---|
| 286 | i++;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /* in doubt assume a unix like shell */
|
|---|
| 290 | return 1;
|
|---|
| 291 | }
|
|---|
| 292 | #endif /* __EMX__ */
|
|---|
| 293 |
|
|---|
| 294 | |
|---|
| 295 |
|
|---|
| 296 | /* Write an error message describing the exit status given in
|
|---|
| 297 | EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
|
|---|
| 298 | Append "(ignored)" if IGNORED is nonzero. */
|
|---|
| 299 |
|
|---|
| 300 | static void
|
|---|
| 301 | child_error (char *target_name, int exit_code, int exit_sig, int coredump,
|
|---|
| 302 | int ignored)
|
|---|
| 303 | {
|
|---|
| 304 | if (ignored && silent_flag)
|
|---|
| 305 | return;
|
|---|
| 306 |
|
|---|
| 307 | #ifdef VMS
|
|---|
| 308 | if (!(exit_code & 1))
|
|---|
| 309 | error (NILF,
|
|---|
| 310 | (ignored ? _("*** [%s] Error 0x%x (ignored)")
|
|---|
| 311 | : _("*** [%s] Error 0x%x")),
|
|---|
| 312 | target_name, exit_code);
|
|---|
| 313 | #else
|
|---|
| 314 | if (exit_sig == 0)
|
|---|
| 315 | error (NILF, ignored ? _("[%s] Error %d (ignored)") :
|
|---|
| 316 | _("*** [%s] Error %d"),
|
|---|
| 317 | target_name, exit_code);
|
|---|
| 318 | else
|
|---|
| 319 | error (NILF, "*** [%s] %s%s",
|
|---|
| 320 | target_name, strsignal (exit_sig),
|
|---|
| 321 | coredump ? _(" (core dumped)") : "");
|
|---|
| 322 | #endif /* VMS */
|
|---|
| 323 | }
|
|---|
| 324 | |
|---|
| 325 |
|
|---|
| 326 | #ifdef VMS
|
|---|
| 327 | /* Wait for nchildren children to terminate */
|
|---|
| 328 | static void
|
|---|
| 329 | vmsWaitForChildren(int *status)
|
|---|
| 330 | {
|
|---|
| 331 | while (1)
|
|---|
| 332 | {
|
|---|
| 333 | if (!vms_jobsefnmask)
|
|---|
| 334 | {
|
|---|
| 335 | *status = 0;
|
|---|
| 336 | return;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | *status = sys$wflor (32, vms_jobsefnmask);
|
|---|
| 340 | }
|
|---|
| 341 | return;
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | /* Set up IO redirection. */
|
|---|
| 345 |
|
|---|
| 346 | char *
|
|---|
| 347 | vms_redirect (struct dsc$descriptor_s *desc, char *fname, char *ibuf)
|
|---|
| 348 | {
|
|---|
| 349 | char *fptr;
|
|---|
| 350 | extern char *vmsify ();
|
|---|
| 351 |
|
|---|
| 352 | ibuf++;
|
|---|
| 353 | while (isspace ((unsigned char)*ibuf))
|
|---|
| 354 | ibuf++;
|
|---|
| 355 | fptr = ibuf;
|
|---|
| 356 | while (*ibuf && !isspace ((unsigned char)*ibuf))
|
|---|
| 357 | ibuf++;
|
|---|
| 358 | *ibuf = 0;
|
|---|
| 359 | if (strcmp (fptr, "/dev/null") != 0)
|
|---|
| 360 | {
|
|---|
| 361 | strcpy (fname, vmsify (fptr, 0));
|
|---|
| 362 | if (strchr (fname, '.') == 0)
|
|---|
| 363 | strcat (fname, ".");
|
|---|
| 364 | }
|
|---|
| 365 | desc->dsc$w_length = strlen(fname);
|
|---|
| 366 | desc->dsc$a_pointer = fname;
|
|---|
| 367 | desc->dsc$b_dtype = DSC$K_DTYPE_T;
|
|---|
| 368 | desc->dsc$b_class = DSC$K_CLASS_S;
|
|---|
| 369 |
|
|---|
| 370 | if (*fname == 0)
|
|---|
| 371 | printf (_("Warning: Empty redirection\n"));
|
|---|
| 372 | return ibuf;
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 | /* found apostrophe at (p-1)
|
|---|
| 377 | inc p until after closing apostrophe.
|
|---|
| 378 | */
|
|---|
| 379 |
|
|---|
| 380 | static char *
|
|---|
| 381 | vms_handle_apos (char *p)
|
|---|
| 382 | {
|
|---|
| 383 | int alast;
|
|---|
| 384 |
|
|---|
| 385 | #define SEPCHARS ",/()= "
|
|---|
| 386 |
|
|---|
| 387 | alast = 0;
|
|---|
| 388 |
|
|---|
| 389 | while (*p != 0)
|
|---|
| 390 | {
|
|---|
| 391 | if (*p == '"')
|
|---|
| 392 | {
|
|---|
| 393 | if (alast)
|
|---|
| 394 | {
|
|---|
| 395 | alast = 0;
|
|---|
| 396 | p++;
|
|---|
| 397 | }
|
|---|
| 398 | else
|
|---|
| 399 | {
|
|---|
| 400 | p++;
|
|---|
| 401 | if (strchr (SEPCHARS, *p))
|
|---|
| 402 | break;
|
|---|
| 403 | alast = 1;
|
|---|
| 404 | }
|
|---|
| 405 | }
|
|---|
| 406 | else
|
|---|
| 407 | p++;
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | return p;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | #endif
|
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 | /* Handle a dead child. This handler may or may not ever be installed.
|
|---|
| 417 |
|
|---|
| 418 | If we're using the jobserver feature, we need it. First, installing it
|
|---|
| 419 | ensures the read will interrupt on SIGCHLD. Second, we close the dup'd
|
|---|
| 420 | read FD to ensure we don't enter another blocking read without reaping all
|
|---|
| 421 | the dead children. In this case we don't need the dead_children count.
|
|---|
| 422 |
|
|---|
| 423 | If we don't have either waitpid or wait3, then make is unreliable, but we
|
|---|
| 424 | use the dead_children count to reap children as best we can. */
|
|---|
| 425 |
|
|---|
| 426 | static unsigned int dead_children = 0;
|
|---|
| 427 |
|
|---|
| 428 | RETSIGTYPE
|
|---|
| 429 | child_handler (int sig UNUSED)
|
|---|
| 430 | {
|
|---|
| 431 | ++dead_children;
|
|---|
| 432 |
|
|---|
| 433 | if (job_rfd >= 0)
|
|---|
| 434 | {
|
|---|
| 435 | close (job_rfd);
|
|---|
| 436 | job_rfd = -1;
|
|---|
| 437 | }
|
|---|
| 438 |
|
|---|
| 439 | #ifdef __EMX__
|
|---|
| 440 | /* The signal handler must called only once! */
|
|---|
| 441 | signal (SIGCHLD, SIG_DFL);
|
|---|
| 442 | #endif
|
|---|
| 443 |
|
|---|
| 444 | /* This causes problems if the SIGCHLD interrupts a printf().
|
|---|
| 445 | DB (DB_JOBS, (_("Got a SIGCHLD; %u unreaped children.\n"), dead_children));
|
|---|
| 446 | */
|
|---|
| 447 | }
|
|---|
| 448 |
|
|---|
| 449 | extern int shell_function_pid, shell_function_completed;
|
|---|
| 450 |
|
|---|
| 451 | /* Reap all dead children, storing the returned status and the new command
|
|---|
| 452 | state (`cs_finished') in the `file' member of the `struct child' for the
|
|---|
| 453 | dead child, and removing the child from the chain. In addition, if BLOCK
|
|---|
| 454 | nonzero, we block in this function until we've reaped at least one
|
|---|
| 455 | complete child, waiting for it to die if necessary. If ERR is nonzero,
|
|---|
| 456 | print an error message first. */
|
|---|
| 457 |
|
|---|
| 458 | void
|
|---|
| 459 | reap_children (int block, int err)
|
|---|
| 460 | {
|
|---|
| 461 | WAIT_T status;
|
|---|
| 462 | /* Initially, assume we have some. */
|
|---|
| 463 | int reap_more = 1;
|
|---|
| 464 |
|
|---|
| 465 | #ifdef WAIT_NOHANG
|
|---|
| 466 | # define REAP_MORE reap_more
|
|---|
| 467 | #else
|
|---|
| 468 | # define REAP_MORE dead_children
|
|---|
| 469 | #endif
|
|---|
| 470 |
|
|---|
| 471 | /* As long as:
|
|---|
| 472 |
|
|---|
| 473 | We have at least one child outstanding OR a shell function in progress,
|
|---|
| 474 | AND
|
|---|
| 475 | We're blocking for a complete child OR there are more children to reap
|
|---|
| 476 |
|
|---|
| 477 | we'll keep reaping children. */
|
|---|
| 478 |
|
|---|
| 479 | while ((children != 0 || shell_function_pid != 0)
|
|---|
| 480 | && (block || REAP_MORE))
|
|---|
| 481 | {
|
|---|
| 482 | int remote = 0;
|
|---|
| 483 | register int pid;
|
|---|
| 484 | int exit_code, exit_sig, coredump;
|
|---|
| 485 | register struct child *lastc, *c;
|
|---|
| 486 | int child_failed;
|
|---|
| 487 | int any_remote, any_local;
|
|---|
| 488 | #ifdef MAKE_DLLSHELL
|
|---|
| 489 | struct child *dllshelled_child = 0;
|
|---|
| 490 | #endif
|
|---|
| 491 |
|
|---|
| 492 | if (err && block)
|
|---|
| 493 | {
|
|---|
| 494 | /* We might block for a while, so let the user know why. */
|
|---|
| 495 | fflush (stdout);
|
|---|
| 496 | error (NILF, _("*** Waiting for unfinished jobs...."));
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | /* We have one less dead child to reap. As noted in
|
|---|
| 500 | child_handler() above, this count is completely unimportant for
|
|---|
| 501 | all modern, POSIX-y systems that support wait3() or waitpid().
|
|---|
| 502 | The rest of this comment below applies only to early, broken
|
|---|
| 503 | pre-POSIX systems. We keep the count only because... it's there...
|
|---|
| 504 |
|
|---|
| 505 | The test and decrement are not atomic; if it is compiled into:
|
|---|
| 506 | register = dead_children - 1;
|
|---|
| 507 | dead_children = register;
|
|---|
| 508 | a SIGCHLD could come between the two instructions.
|
|---|
| 509 | child_handler increments dead_children.
|
|---|
| 510 | The second instruction here would lose that increment. But the
|
|---|
| 511 | only effect of dead_children being wrong is that we might wait
|
|---|
| 512 | longer than necessary to reap a child, and lose some parallelism;
|
|---|
| 513 | and we might print the "Waiting for unfinished jobs" message above
|
|---|
| 514 | when not necessary. */
|
|---|
| 515 |
|
|---|
| 516 | if (dead_children > 0)
|
|---|
| 517 | --dead_children;
|
|---|
| 518 |
|
|---|
| 519 | any_remote = 0;
|
|---|
| 520 | any_local = shell_function_pid != 0;
|
|---|
| 521 | for (c = children; c != 0; c = c->next)
|
|---|
| 522 | {
|
|---|
| 523 | any_remote |= c->remote;
|
|---|
| 524 | any_local |= ! c->remote;
|
|---|
| 525 | #ifdef MAKE_DLLSHELL
|
|---|
| 526 | if (c->dllshell_done)
|
|---|
| 527 | dllshelled_child = c;
|
|---|
| 528 | #endif
|
|---|
| 529 | DB (DB_JOBS, (_("Live child 0x%08lx (%s) PID %ld %s\n"),
|
|---|
| 530 | (unsigned long int) c, c->file->name,
|
|---|
| 531 | (long) c->pid, c->remote ? _(" (remote)") : ""));
|
|---|
| 532 | #ifdef VMS
|
|---|
| 533 | break;
|
|---|
| 534 | #endif
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | /* First, check for remote children. */
|
|---|
| 538 | if (any_remote)
|
|---|
| 539 | pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
|
|---|
| 540 | else
|
|---|
| 541 | pid = 0;
|
|---|
| 542 |
|
|---|
| 543 | if (pid > 0)
|
|---|
| 544 | /* We got a remote child. */
|
|---|
| 545 | remote = 1;
|
|---|
| 546 | else if (pid < 0)
|
|---|
| 547 | {
|
|---|
| 548 | /* A remote status command failed miserably. Punt. */
|
|---|
| 549 | remote_status_lose:
|
|---|
| 550 | pfatal_with_name ("remote_status");
|
|---|
| 551 | }
|
|---|
| 552 | else
|
|---|
| 553 | {
|
|---|
| 554 | /* No remote children. Check for local children. */
|
|---|
| 555 | #ifdef MAKE_DLLSHELL
|
|---|
| 556 | if (dllshelled_child)
|
|---|
| 557 | {
|
|---|
| 558 | pid = dllshelled_child->pid;
|
|---|
| 559 | status = (WAIT_T)dllshelled_child->status;
|
|---|
| 560 | }
|
|---|
| 561 | else
|
|---|
| 562 | #endif
|
|---|
| 563 | #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
|
|---|
| 564 | if (any_local)
|
|---|
| 565 | {
|
|---|
| 566 | #ifdef VMS
|
|---|
| 567 | vmsWaitForChildren (&status);
|
|---|
| 568 | pid = c->pid;
|
|---|
| 569 | #elif MAKE_DLLSHELL
|
|---|
| 570 | pid = wait_jobs((int*)&status, block);
|
|---|
| 571 | #else
|
|---|
| 572 | #ifdef WAIT_NOHANG
|
|---|
| 573 | if (!block)
|
|---|
| 574 | pid = WAIT_NOHANG (&status);
|
|---|
| 575 | else
|
|---|
| 576 | #endif
|
|---|
| 577 | pid = wait (&status);
|
|---|
| 578 | #endif /* !VMS */
|
|---|
| 579 | }
|
|---|
| 580 | else
|
|---|
| 581 | pid = 0;
|
|---|
| 582 |
|
|---|
| 583 | if (pid < 0)
|
|---|
| 584 | {
|
|---|
| 585 | /* The wait*() failed miserably. Punt. */
|
|---|
| 586 | pfatal_with_name ("wait");
|
|---|
| 587 | }
|
|---|
| 588 | else if (pid > 0)
|
|---|
| 589 | {
|
|---|
| 590 | /* We got a child exit; chop the status word up. */
|
|---|
| 591 | exit_code = WEXITSTATUS (status);
|
|---|
| 592 | exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
|
|---|
| 593 | coredump = WCOREDUMP (status);
|
|---|
| 594 |
|
|---|
| 595 | /* If we have started jobs in this second, remove one. */
|
|---|
| 596 | if (job_counter)
|
|---|
| 597 | --job_counter;
|
|---|
| 598 | }
|
|---|
| 599 | else
|
|---|
| 600 | {
|
|---|
| 601 | /* No local children are dead. */
|
|---|
| 602 | reap_more = 0;
|
|---|
| 603 |
|
|---|
| 604 | if (!block || !any_remote)
|
|---|
| 605 | break;
|
|---|
| 606 |
|
|---|
| 607 | /* Now try a blocking wait for a remote child. */
|
|---|
| 608 | pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
|
|---|
| 609 | if (pid < 0)
|
|---|
| 610 | goto remote_status_lose;
|
|---|
| 611 | else if (pid == 0)
|
|---|
| 612 | /* No remote children either. Finally give up. */
|
|---|
| 613 | break;
|
|---|
| 614 |
|
|---|
| 615 | /* We got a remote child. */
|
|---|
| 616 | remote = 1;
|
|---|
| 617 | }
|
|---|
| 618 | #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
|
|---|
| 619 |
|
|---|
| 620 | #ifdef __MSDOS__
|
|---|
| 621 | /* Life is very different on MSDOS. */
|
|---|
| 622 | pid = dos_pid - 1;
|
|---|
| 623 | status = dos_status;
|
|---|
| 624 | exit_code = WEXITSTATUS (status);
|
|---|
| 625 | if (exit_code == 0xff)
|
|---|
| 626 | exit_code = -1;
|
|---|
| 627 | exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
|
|---|
| 628 | coredump = 0;
|
|---|
| 629 | #endif /* __MSDOS__ */
|
|---|
| 630 | #ifdef _AMIGA
|
|---|
| 631 | /* Same on Amiga */
|
|---|
| 632 | pid = amiga_pid - 1;
|
|---|
| 633 | status = amiga_status;
|
|---|
| 634 | exit_code = amiga_status;
|
|---|
| 635 | exit_sig = 0;
|
|---|
| 636 | coredump = 0;
|
|---|
| 637 | #endif /* _AMIGA */
|
|---|
| 638 | #ifdef WINDOWS32
|
|---|
| 639 | {
|
|---|
| 640 | HANDLE hPID;
|
|---|
| 641 | int err;
|
|---|
| 642 |
|
|---|
| 643 | /* wait for anything to finish */
|
|---|
| 644 | if (hPID = process_wait_for_any()) {
|
|---|
| 645 |
|
|---|
| 646 | /* was an error found on this process? */
|
|---|
| 647 | err = process_last_err(hPID);
|
|---|
| 648 |
|
|---|
| 649 | /* get exit data */
|
|---|
| 650 | exit_code = process_exit_code(hPID);
|
|---|
| 651 |
|
|---|
| 652 | if (err)
|
|---|
| 653 | fprintf(stderr, "make (e=%d): %s",
|
|---|
| 654 | exit_code, map_windows32_error_to_string(exit_code));
|
|---|
| 655 |
|
|---|
| 656 | /* signal */
|
|---|
| 657 | exit_sig = process_signal(hPID);
|
|---|
| 658 |
|
|---|
| 659 | /* cleanup process */
|
|---|
| 660 | process_cleanup(hPID);
|
|---|
| 661 |
|
|---|
| 662 | coredump = 0;
|
|---|
| 663 | }
|
|---|
| 664 | pid = (int) hPID;
|
|---|
| 665 | }
|
|---|
| 666 | #endif /* WINDOWS32 */
|
|---|
| 667 | }
|
|---|
| 668 |
|
|---|
| 669 | /* Check if this is the child of the `shell' function. */
|
|---|
| 670 | if (!remote && pid == shell_function_pid)
|
|---|
| 671 | {
|
|---|
| 672 | /* It is. Leave an indicator for the `shell' function. */
|
|---|
| 673 | if (exit_sig == 0 && exit_code == 127)
|
|---|
| 674 | shell_function_completed = -1;
|
|---|
| 675 | else
|
|---|
| 676 | shell_function_completed = 1;
|
|---|
| 677 | break;
|
|---|
| 678 | }
|
|---|
| 679 |
|
|---|
| 680 | child_failed = exit_sig != 0 || exit_code != 0;
|
|---|
| 681 |
|
|---|
| 682 | /* Search for a child matching the deceased one. */
|
|---|
| 683 | lastc = 0;
|
|---|
| 684 | for (c = children; c != 0; lastc = c, c = c->next)
|
|---|
| 685 | if (c->remote == remote && c->pid == pid)
|
|---|
| 686 | break;
|
|---|
| 687 |
|
|---|
| 688 | if (c == 0)
|
|---|
| 689 | /* An unknown child died.
|
|---|
| 690 | Ignore it; it was inherited from our invoker. */
|
|---|
| 691 | continue;
|
|---|
| 692 |
|
|---|
| 693 | DB (DB_JOBS, (child_failed
|
|---|
| 694 | ? _("Reaping losing child 0x%08lx PID %ld %s\n")
|
|---|
| 695 | : _("Reaping winning child 0x%08lx PID %ld %s\n"),
|
|---|
| 696 | (unsigned long int) c, (long) c->pid,
|
|---|
| 697 | c->remote ? _(" (remote)") : ""));
|
|---|
| 698 |
|
|---|
| 699 | if (c->sh_batch_file) {
|
|---|
| 700 | DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
|
|---|
| 701 | c->sh_batch_file));
|
|---|
| 702 |
|
|---|
| 703 | /* just try and remove, don't care if this fails */
|
|---|
| 704 | remove (c->sh_batch_file);
|
|---|
| 705 |
|
|---|
| 706 | /* all done with memory */
|
|---|
| 707 | free (c->sh_batch_file);
|
|---|
| 708 | c->sh_batch_file = NULL;
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 | /* If this child had the good stdin, say it is now free. */
|
|---|
| 712 | if (c->good_stdin)
|
|---|
| 713 | good_stdin_used = 0;
|
|---|
| 714 |
|
|---|
| 715 | if (child_failed && !c->noerror && !ignore_errors_flag)
|
|---|
| 716 | {
|
|---|
| 717 | /* The commands failed. Write an error message,
|
|---|
| 718 | delete non-precious targets, and abort. */
|
|---|
| 719 | static int delete_on_error = -1;
|
|---|
| 720 | child_error (c->file->name, exit_code, exit_sig, coredump, 0);
|
|---|
| 721 | c->file->update_status = 2;
|
|---|
| 722 | if (delete_on_error == -1)
|
|---|
| 723 | {
|
|---|
| 724 | struct file *f = lookup_file (".DELETE_ON_ERROR");
|
|---|
| 725 | delete_on_error = f != 0 && f->is_target;
|
|---|
| 726 | }
|
|---|
| 727 | if (exit_sig != 0 || delete_on_error)
|
|---|
| 728 | delete_child_targets (c);
|
|---|
| 729 | }
|
|---|
| 730 | else
|
|---|
| 731 | {
|
|---|
| 732 | if (child_failed)
|
|---|
| 733 | {
|
|---|
| 734 | /* The commands failed, but we don't care. */
|
|---|
| 735 | child_error (c->file->name,
|
|---|
| 736 | exit_code, exit_sig, coredump, 1);
|
|---|
| 737 | child_failed = 0;
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|
| 740 | /* If there are more commands to run, try to start them. */
|
|---|
| 741 | if (job_next_command (c))
|
|---|
| 742 | {
|
|---|
| 743 | if (handling_fatal_signal)
|
|---|
| 744 | {
|
|---|
| 745 | /* Never start new commands while we are dying.
|
|---|
| 746 | Since there are more commands that wanted to be run,
|
|---|
| 747 | the target was not completely remade. So we treat
|
|---|
| 748 | this as if a command had failed. */
|
|---|
| 749 | c->file->update_status = 2;
|
|---|
| 750 | }
|
|---|
| 751 | else
|
|---|
| 752 | {
|
|---|
| 753 | /* Check again whether to start remotely.
|
|---|
| 754 | Whether or not we want to changes over time.
|
|---|
| 755 | Also, start_remote_job may need state set up
|
|---|
| 756 | by start_remote_job_p. */
|
|---|
| 757 | c->remote = start_remote_job_p (0);
|
|---|
| 758 | start_job_command (c);
|
|---|
| 759 | /* Fatal signals are left blocked in case we were
|
|---|
| 760 | about to put that child on the chain. But it is
|
|---|
| 761 | already there, so it is safe for a fatal signal to
|
|---|
| 762 | arrive now; it will clean up this child's targets. */
|
|---|
| 763 | unblock_sigs ();
|
|---|
| 764 | if (c->file->command_state == cs_running)
|
|---|
| 765 | /* We successfully started the new command.
|
|---|
| 766 | Loop to reap more children. */
|
|---|
| 767 | continue;
|
|---|
| 768 | }
|
|---|
| 769 |
|
|---|
| 770 | if (c->file->update_status != 0)
|
|---|
| 771 | /* We failed to start the commands. */
|
|---|
| 772 | delete_child_targets (c);
|
|---|
| 773 | }
|
|---|
| 774 | else
|
|---|
| 775 | /* There are no more commands. We got through them all
|
|---|
| 776 | without an unignored error. Now the target has been
|
|---|
| 777 | successfully updated. */
|
|---|
| 778 | c->file->update_status = 0;
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | /* When we get here, all the commands for C->file are finished
|
|---|
| 782 | (or aborted) and C->file->update_status contains 0 or 2. But
|
|---|
| 783 | C->file->command_state is still cs_running if all the commands
|
|---|
| 784 | ran; notice_finish_file looks for cs_running to tell it that
|
|---|
| 785 | it's interesting to check the file's modtime again now. */
|
|---|
| 786 |
|
|---|
| 787 | if (! handling_fatal_signal)
|
|---|
| 788 | /* Notice if the target of the commands has been changed.
|
|---|
| 789 | This also propagates its values for command_state and
|
|---|
| 790 | update_status to its also_make files. */
|
|---|
| 791 | notice_finished_file (c->file);
|
|---|
| 792 |
|
|---|
| 793 | DB (DB_JOBS, (_("Removing child 0x%08lx PID %ld%s from chain.\n"),
|
|---|
| 794 | (unsigned long int) c, (long) c->pid,
|
|---|
| 795 | c->remote ? _(" (remote)") : ""));
|
|---|
| 796 |
|
|---|
| 797 | /* Block fatal signals while frobnicating the list, so that
|
|---|
| 798 | children and job_slots_used are always consistent. Otherwise
|
|---|
| 799 | a fatal signal arriving after the child is off the chain and
|
|---|
| 800 | before job_slots_used is decremented would believe a child was
|
|---|
| 801 | live and call reap_children again. */
|
|---|
| 802 | block_sigs ();
|
|---|
| 803 |
|
|---|
| 804 | /* There is now another slot open. */
|
|---|
| 805 | if (job_slots_used > 0)
|
|---|
| 806 | --job_slots_used;
|
|---|
| 807 |
|
|---|
| 808 | /* Remove the child from the chain and free it. */
|
|---|
| 809 | if (lastc == 0)
|
|---|
| 810 | children = c->next;
|
|---|
| 811 | else
|
|---|
| 812 | lastc->next = c->next;
|
|---|
| 813 |
|
|---|
| 814 | free_child (c);
|
|---|
| 815 |
|
|---|
| 816 | unblock_sigs ();
|
|---|
| 817 |
|
|---|
| 818 | /* If the job failed, and the -k flag was not given, die,
|
|---|
| 819 | unless we are already in the process of dying. */
|
|---|
| 820 | if (!err && child_failed && !keep_going_flag &&
|
|---|
| 821 | /* fatal_error_signal will die with the right signal. */
|
|---|
| 822 | !handling_fatal_signal)
|
|---|
| 823 | die (2);
|
|---|
| 824 |
|
|---|
| 825 | /* Only block for one child. */
|
|---|
| 826 | block = 0;
|
|---|
| 827 | }
|
|---|
| 828 |
|
|---|
| 829 | return;
|
|---|
| 830 | }
|
|---|
| 831 | |
|---|
| 832 |
|
|---|
| 833 | /* Free the storage allocated for CHILD. */
|
|---|
| 834 |
|
|---|
| 835 | static void
|
|---|
| 836 | free_child (struct child *child)
|
|---|
| 837 | {
|
|---|
| 838 | /* If this child is the only one it was our "free" job, so don't put a
|
|---|
| 839 | token back for it. This child has already been removed from the list,
|
|---|
| 840 | so if there any left this wasn't the last one. */
|
|---|
| 841 |
|
|---|
| 842 | if (job_fds[1] >= 0 && children)
|
|---|
| 843 | {
|
|---|
| 844 | char token = '+';
|
|---|
| 845 | int r;
|
|---|
| 846 |
|
|---|
| 847 | /* Write a job token back to the pipe. */
|
|---|
| 848 |
|
|---|
| 849 | EINTRLOOP (r, write (job_fds[1], &token, 1));
|
|---|
| 850 | if (r != 1)
|
|---|
| 851 | pfatal_with_name (_("write jobserver"));
|
|---|
| 852 |
|
|---|
| 853 | DB (DB_JOBS, (_("Released token for child 0x%08lx (%s).\n"),
|
|---|
| 854 | (unsigned long int) child, child->file->name));
|
|---|
| 855 | }
|
|---|
| 856 |
|
|---|
| 857 | if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
|
|---|
| 858 | return;
|
|---|
| 859 |
|
|---|
| 860 | if (child->command_lines != 0)
|
|---|
| 861 | {
|
|---|
| 862 | register unsigned int i;
|
|---|
| 863 | for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
|
|---|
| 864 | free (child->command_lines[i]);
|
|---|
| 865 | free ((char *) child->command_lines);
|
|---|
| 866 | }
|
|---|
| 867 |
|
|---|
| 868 | if (child->environment != 0)
|
|---|
| 869 | {
|
|---|
| 870 | register char **ep = child->environment;
|
|---|
| 871 | while (*ep != 0)
|
|---|
| 872 | free (*ep++);
|
|---|
| 873 | free ((char *) child->environment);
|
|---|
| 874 | }
|
|---|
| 875 |
|
|---|
| 876 | free ((char *) child);
|
|---|
| 877 | }
|
|---|
| 878 | |
|---|
| 879 |
|
|---|
| 880 | #ifdef POSIX
|
|---|
| 881 | extern sigset_t fatal_signal_set;
|
|---|
| 882 | #endif
|
|---|
| 883 |
|
|---|
| 884 | void
|
|---|
| 885 | block_sigs (void)
|
|---|
| 886 | {
|
|---|
| 887 | #ifdef POSIX
|
|---|
| 888 | (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
|
|---|
| 889 | #else
|
|---|
| 890 | # ifdef HAVE_SIGSETMASK
|
|---|
| 891 | (void) sigblock (fatal_signal_mask);
|
|---|
| 892 | # endif
|
|---|
| 893 | #endif
|
|---|
| 894 | }
|
|---|
| 895 |
|
|---|
| 896 | #ifdef POSIX
|
|---|
| 897 | void
|
|---|
| 898 | unblock_sigs (void)
|
|---|
| 899 | {
|
|---|
| 900 | sigset_t empty;
|
|---|
| 901 | sigemptyset (&empty);
|
|---|
| 902 | sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
|
|---|
| 903 | }
|
|---|
| 904 | #endif
|
|---|
| 905 |
|
|---|
| 906 | #ifdef MAKE_JOBSERVER
|
|---|
| 907 | /* Set the child handler action flags to FLAGS. */
|
|---|
| 908 | static void
|
|---|
| 909 | set_child_handler_action_flags (int flags)
|
|---|
| 910 | {
|
|---|
| 911 | struct sigaction sa;
|
|---|
| 912 | bzero ((char *) &sa, sizeof sa);
|
|---|
| 913 | sa.sa_handler = child_handler;
|
|---|
| 914 | sa.sa_flags = flags;
|
|---|
| 915 | #if defined SIGCHLD
|
|---|
| 916 | sigaction (SIGCHLD, &sa, NULL);
|
|---|
| 917 | #endif
|
|---|
| 918 | #if defined SIGCLD && SIGCLD != SIGCHLD
|
|---|
| 919 | sigaction (SIGCLD, &sa, NULL);
|
|---|
| 920 | #endif
|
|---|
| 921 | }
|
|---|
| 922 | #endif
|
|---|
| 923 |
|
|---|
| 924 |
|
|---|
| 925 | /* Start a job to run the commands specified in CHILD.
|
|---|
| 926 | CHILD is updated to reflect the commands and ID of the child process.
|
|---|
| 927 |
|
|---|
| 928 | NOTE: On return fatal signals are blocked! The caller is responsible
|
|---|
| 929 | for calling `unblock_sigs', once the new child is safely on the chain so
|
|---|
| 930 | it can be cleaned up in the event of a fatal signal. */
|
|---|
| 931 |
|
|---|
| 932 | static void
|
|---|
| 933 | start_job_command (struct child *child)
|
|---|
| 934 | {
|
|---|
| 935 | #ifndef _AMIGA
|
|---|
| 936 | static int bad_stdin = -1;
|
|---|
| 937 | #endif
|
|---|
| 938 | register char *p;
|
|---|
| 939 | int flags;
|
|---|
| 940 | #ifdef VMS
|
|---|
| 941 | char *argv;
|
|---|
| 942 | #else
|
|---|
| 943 | char **argv;
|
|---|
| 944 | #endif
|
|---|
| 945 |
|
|---|
| 946 | /* If we have a completely empty commandset, stop now. */
|
|---|
| 947 | if (!child->command_ptr)
|
|---|
| 948 | goto next_command;
|
|---|
| 949 |
|
|---|
| 950 | /* Combine the flags parsed for the line itself with
|
|---|
| 951 | the flags specified globally for this target. */
|
|---|
| 952 | flags = (child->file->command_flags
|
|---|
| 953 | | child->file->cmds->lines_flags[child->command_line - 1]);
|
|---|
| 954 |
|
|---|
| 955 | p = child->command_ptr;
|
|---|
| 956 | child->noerror = flags & COMMANDS_NOERROR;
|
|---|
| 957 |
|
|---|
| 958 | while (*p != '\0')
|
|---|
| 959 | {
|
|---|
| 960 | if (*p == '@')
|
|---|
| 961 | flags |= COMMANDS_SILENT;
|
|---|
| 962 | else if (*p == '+')
|
|---|
| 963 | flags |= COMMANDS_RECURSE;
|
|---|
| 964 | else if (*p == '-')
|
|---|
| 965 | child->noerror = 1;
|
|---|
| 966 | else if (!isblank ((unsigned char)*p))
|
|---|
| 967 | break;
|
|---|
| 968 | ++p;
|
|---|
| 969 | }
|
|---|
| 970 |
|
|---|
| 971 | /* Update the file's command flags with any new ones we found. We only
|
|---|
| 972 | keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
|
|---|
| 973 | now marking more commands recursive than should be in the case of
|
|---|
| 974 | multiline define/endef scripts where only one line is marked "+". In
|
|---|
| 975 | order to really fix this, we'll have to keep a lines_flags for every
|
|---|
| 976 | actual line, after expansion. */
|
|---|
| 977 | child->file->cmds->lines_flags[child->command_line - 1]
|
|---|
| 978 | |= flags & COMMANDS_RECURSE;
|
|---|
| 979 |
|
|---|
| 980 | /* Figure out an argument list from this command line. */
|
|---|
| 981 |
|
|---|
| 982 | {
|
|---|
| 983 | char *end = 0;
|
|---|
| 984 | #ifdef VMS
|
|---|
| 985 | argv = p;
|
|---|
| 986 | #else
|
|---|
| 987 | argv = construct_command_argv (p, &end, child->file, &child->sh_batch_file);
|
|---|
| 988 | #endif
|
|---|
| 989 | if (end == NULL)
|
|---|
| 990 | child->command_ptr = NULL;
|
|---|
| 991 | else
|
|---|
| 992 | {
|
|---|
| 993 | *end++ = '\0';
|
|---|
| 994 | child->command_ptr = end;
|
|---|
| 995 | }
|
|---|
| 996 | }
|
|---|
| 997 |
|
|---|
| 998 | /* If -q was given, say that updating `failed' if there was any text on the
|
|---|
| 999 | command line, or `succeeded' otherwise. The exit status of 1 tells the
|
|---|
| 1000 | user that -q is saying `something to do'; the exit status for a random
|
|---|
| 1001 | error is 2. */
|
|---|
| 1002 | if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
|
|---|
| 1003 | {
|
|---|
| 1004 | #ifndef VMS
|
|---|
| 1005 | free (argv[0]);
|
|---|
| 1006 | free ((char *) argv);
|
|---|
| 1007 | #endif
|
|---|
| 1008 | child->file->update_status = 1;
|
|---|
| 1009 | notice_finished_file (child->file);
|
|---|
| 1010 | return;
|
|---|
| 1011 | }
|
|---|
| 1012 |
|
|---|
| 1013 | if (touch_flag && !(flags & COMMANDS_RECURSE))
|
|---|
| 1014 | {
|
|---|
| 1015 | /* Go on to the next command. It might be the recursive one.
|
|---|
| 1016 | We construct ARGV only to find the end of the command line. */
|
|---|
| 1017 | #ifndef VMS
|
|---|
| 1018 | if (argv)
|
|---|
| 1019 | {
|
|---|
| 1020 | free (argv[0]);
|
|---|
| 1021 | free ((char *) argv);
|
|---|
| 1022 | }
|
|---|
| 1023 | #endif
|
|---|
| 1024 | argv = 0;
|
|---|
| 1025 | }
|
|---|
| 1026 |
|
|---|
| 1027 | if (argv == 0)
|
|---|
| 1028 | {
|
|---|
| 1029 | next_command:
|
|---|
| 1030 | #ifdef __MSDOS__
|
|---|
| 1031 | execute_by_shell = 0; /* in case construct_command_argv sets it */
|
|---|
| 1032 | #endif
|
|---|
| 1033 | /* This line has no commands. Go to the next. */
|
|---|
| 1034 | if (job_next_command (child))
|
|---|
| 1035 | start_job_command (child);
|
|---|
| 1036 | else
|
|---|
| 1037 | {
|
|---|
| 1038 | /* No more commands. Make sure we're "running"; we might not be if
|
|---|
| 1039 | (e.g.) all commands were skipped due to -n. */
|
|---|
| 1040 | set_command_state (child->file, cs_running);
|
|---|
| 1041 | child->file->update_status = 0;
|
|---|
| 1042 | notice_finished_file (child->file);
|
|---|
| 1043 | }
|
|---|
| 1044 | return;
|
|---|
| 1045 | }
|
|---|
| 1046 |
|
|---|
| 1047 | /* Print out the command. If silent, we call `message' with null so it
|
|---|
| 1048 | can log the working directory before the command's own error messages
|
|---|
| 1049 | appear. */
|
|---|
| 1050 |
|
|---|
| 1051 | message (0, (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag))
|
|---|
| 1052 | ? "%s" : (char *) 0, p);
|
|---|
| 1053 |
|
|---|
| 1054 | /* Tell update_goal_chain that a command has been started on behalf of
|
|---|
| 1055 | this target. It is important that this happens here and not in
|
|---|
| 1056 | reap_children (where we used to do it), because reap_children might be
|
|---|
| 1057 | reaping children from a different target. We want this increment to
|
|---|
| 1058 | guaranteedly indicate that a command was started for the dependency
|
|---|
| 1059 | chain (i.e., update_file recursion chain) we are processing. */
|
|---|
| 1060 |
|
|---|
| 1061 | ++commands_started;
|
|---|
| 1062 |
|
|---|
| 1063 | /* Optimize an empty command. People use this for timestamp rules,
|
|---|
| 1064 | so avoid forking a useless shell. Do this after we increment
|
|---|
| 1065 | commands_started so make still treats this special case as if it
|
|---|
| 1066 | performed some action (makes a difference as to what messages are
|
|---|
| 1067 | printed, etc. */
|
|---|
| 1068 |
|
|---|
| 1069 | #if !defined(VMS) && !defined(_AMIGA)
|
|---|
| 1070 | if (
|
|---|
| 1071 | #if defined __MSDOS__ || defined (__EMX__)
|
|---|
| 1072 | unixy_shell /* the test is complicated and we already did it */
|
|---|
| 1073 | #else
|
|---|
| 1074 | (argv[0] && !strcmp (argv[0], "/bin/sh"))
|
|---|
| 1075 | #endif
|
|---|
| 1076 | && (argv[1]
|
|---|
| 1077 | && argv[1][0] == '-' && argv[1][1] == 'c' && argv[1][2] == '\0')
|
|---|
| 1078 | && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
|
|---|
| 1079 | && argv[3] == NULL)
|
|---|
| 1080 | {
|
|---|
| 1081 | free (argv[0]);
|
|---|
| 1082 | free ((char *) argv);
|
|---|
| 1083 | goto next_command;
|
|---|
| 1084 | }
|
|---|
| 1085 | #endif /* !VMS && !_AMIGA */
|
|---|
| 1086 |
|
|---|
| 1087 | /* If -n was given, recurse to get the next line in the sequence. */
|
|---|
| 1088 |
|
|---|
| 1089 | if (just_print_flag && !(flags & COMMANDS_RECURSE))
|
|---|
| 1090 | {
|
|---|
| 1091 | #ifndef VMS
|
|---|
| 1092 | free (argv[0]);
|
|---|
| 1093 | free ((char *) argv);
|
|---|
| 1094 | #endif
|
|---|
| 1095 | goto next_command;
|
|---|
| 1096 | }
|
|---|
| 1097 |
|
|---|
| 1098 | /* Flush the output streams so they won't have things written twice. */
|
|---|
| 1099 |
|
|---|
| 1100 | fflush (stdout);
|
|---|
| 1101 | fflush (stderr);
|
|---|
| 1102 |
|
|---|
| 1103 | #ifndef VMS
|
|---|
| 1104 | #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
|
|---|
| 1105 |
|
|---|
| 1106 | /* Set up a bad standard input that reads from a broken pipe. */
|
|---|
| 1107 |
|
|---|
| 1108 | if (bad_stdin == -1)
|
|---|
| 1109 | {
|
|---|
| 1110 | /* Make a file descriptor that is the read end of a broken pipe.
|
|---|
| 1111 | This will be used for some children's standard inputs. */
|
|---|
| 1112 | int pd[2];
|
|---|
| 1113 | if (pipe (pd) == 0)
|
|---|
| 1114 | {
|
|---|
| 1115 | /* Close the write side. */
|
|---|
| 1116 | (void) close (pd[1]);
|
|---|
| 1117 | /* Save the read side. */
|
|---|
| 1118 | bad_stdin = pd[0];
|
|---|
| 1119 |
|
|---|
| 1120 | /* Set the descriptor to close on exec, so it does not litter any
|
|---|
| 1121 | child's descriptor table. When it is dup2'd onto descriptor 0,
|
|---|
| 1122 | that descriptor will not close on exec. */
|
|---|
| 1123 | CLOSE_ON_EXEC (bad_stdin);
|
|---|
| 1124 | }
|
|---|
| 1125 | }
|
|---|
| 1126 |
|
|---|
| 1127 | #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
|
|---|
| 1128 |
|
|---|
| 1129 | /* Decide whether to give this child the `good' standard input
|
|---|
| 1130 | (one that points to the terminal or whatever), or the `bad' one
|
|---|
| 1131 | that points to the read side of a broken pipe. */
|
|---|
| 1132 |
|
|---|
| 1133 | child->good_stdin = !good_stdin_used;
|
|---|
| 1134 | if (child->good_stdin)
|
|---|
| 1135 | good_stdin_used = 1;
|
|---|
| 1136 |
|
|---|
| 1137 | #endif /* !VMS */
|
|---|
| 1138 |
|
|---|
| 1139 | child->deleted = 0;
|
|---|
| 1140 |
|
|---|
| 1141 | #ifndef _AMIGA
|
|---|
| 1142 | /* Set up the environment for the child. */
|
|---|
| 1143 | if (child->environment == 0)
|
|---|
| 1144 | child->environment = target_environment (child->file);
|
|---|
| 1145 | #endif
|
|---|
| 1146 |
|
|---|
| 1147 | #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
|
|---|
| 1148 |
|
|---|
| 1149 | #ifndef VMS
|
|---|
| 1150 | /* start_waiting_job has set CHILD->remote if we can start a remote job. */
|
|---|
| 1151 | if (child->remote)
|
|---|
| 1152 | {
|
|---|
| 1153 | int is_remote, id, used_stdin;
|
|---|
| 1154 | if (start_remote_job (argv, child->environment,
|
|---|
| 1155 | child->good_stdin ? 0 : bad_stdin,
|
|---|
| 1156 | &is_remote, &id, &used_stdin))
|
|---|
| 1157 | /* Don't give up; remote execution may fail for various reasons. If
|
|---|
| 1158 | so, simply run the job locally. */
|
|---|
| 1159 | goto run_local;
|
|---|
| 1160 | else
|
|---|
| 1161 | {
|
|---|
| 1162 | if (child->good_stdin && !used_stdin)
|
|---|
| 1163 | {
|
|---|
| 1164 | child->good_stdin = 0;
|
|---|
| 1165 | good_stdin_used = 0;
|
|---|
| 1166 | }
|
|---|
| 1167 | child->remote = is_remote;
|
|---|
| 1168 | child->pid = id;
|
|---|
| 1169 | }
|
|---|
| 1170 | }
|
|---|
| 1171 | else
|
|---|
| 1172 | #endif /* !VMS */
|
|---|
| 1173 | {
|
|---|
| 1174 | /* Fork the child process. */
|
|---|
| 1175 |
|
|---|
| 1176 | char **parent_environ;
|
|---|
| 1177 |
|
|---|
| 1178 | run_local:
|
|---|
| 1179 | block_sigs ();
|
|---|
| 1180 |
|
|---|
| 1181 | child->remote = 0;
|
|---|
| 1182 |
|
|---|
| 1183 | #ifdef VMS
|
|---|
| 1184 |
|
|---|
| 1185 | if (!child_execute_job (argv, child)) {
|
|---|
| 1186 | /* Fork failed! */
|
|---|
| 1187 | perror_with_name ("vfork", "");
|
|---|
| 1188 | goto error;
|
|---|
| 1189 | }
|
|---|
| 1190 |
|
|---|
| 1191 | #else
|
|---|
| 1192 |
|
|---|
| 1193 | parent_environ = environ;
|
|---|
| 1194 |
|
|---|
| 1195 | # ifdef __EMX__
|
|---|
| 1196 | /* If we aren't running a recursive command and we have a jobserver
|
|---|
| 1197 | pipe, close it before exec'ing. */
|
|---|
| 1198 | if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
|
|---|
| 1199 | {
|
|---|
| 1200 | CLOSE_ON_EXEC (job_fds[0]);
|
|---|
| 1201 | CLOSE_ON_EXEC (job_fds[1]);
|
|---|
| 1202 | }
|
|---|
| 1203 | if (job_rfd >= 0)
|
|---|
| 1204 | CLOSE_ON_EXEC (job_rfd);
|
|---|
| 1205 |
|
|---|
| 1206 | /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
|
|---|
| 1207 | child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
|
|---|
| 1208 | argv, child->environment, child);
|
|---|
| 1209 | if (child->pid < 0)
|
|---|
| 1210 | {
|
|---|
| 1211 | /* spawn failed! */
|
|---|
| 1212 | unblock_sigs ();
|
|---|
| 1213 | perror_with_name ("spawn", "");
|
|---|
| 1214 | goto error;
|
|---|
| 1215 | }
|
|---|
| 1216 |
|
|---|
| 1217 | /* undo CLOSE_ON_EXEC() after the child process has been started */
|
|---|
| 1218 | if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
|
|---|
| 1219 | {
|
|---|
| 1220 | fcntl (job_fds[0], F_SETFD, 0);
|
|---|
| 1221 | fcntl (job_fds[1], F_SETFD, 0);
|
|---|
| 1222 | }
|
|---|
| 1223 | if (job_rfd >= 0)
|
|---|
| 1224 | fcntl (job_rfd, F_SETFD, 0);
|
|---|
| 1225 |
|
|---|
| 1226 | #else /* !__EMX__ */
|
|---|
| 1227 |
|
|---|
| 1228 | child->pid = vfork ();
|
|---|
| 1229 | environ = parent_environ; /* Restore value child may have clobbered. */
|
|---|
| 1230 | if (child->pid == 0)
|
|---|
| 1231 | {
|
|---|
| 1232 | /* We are the child side. */
|
|---|
| 1233 | unblock_sigs ();
|
|---|
| 1234 |
|
|---|
| 1235 | /* If we aren't running a recursive command and we have a jobserver
|
|---|
| 1236 | pipe, close it before exec'ing. */
|
|---|
| 1237 | if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
|
|---|
| 1238 | {
|
|---|
| 1239 | close (job_fds[0]);
|
|---|
| 1240 | close (job_fds[1]);
|
|---|
| 1241 | }
|
|---|
| 1242 | if (job_rfd >= 0)
|
|---|
| 1243 | close (job_rfd);
|
|---|
| 1244 |
|
|---|
| 1245 | child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
|
|---|
| 1246 | argv, child->environment);
|
|---|
| 1247 | }
|
|---|
| 1248 | else if (child->pid < 0)
|
|---|
| 1249 | {
|
|---|
| 1250 | /* Fork failed! */
|
|---|
| 1251 | unblock_sigs ();
|
|---|
| 1252 | perror_with_name ("vfork", "");
|
|---|
| 1253 | goto error;
|
|---|
| 1254 | }
|
|---|
| 1255 | # endif /* !__EMX__ */
|
|---|
| 1256 | #endif /* !VMS */
|
|---|
| 1257 | }
|
|---|
| 1258 |
|
|---|
| 1259 | #else /* __MSDOS__ or Amiga or WINDOWS32 */
|
|---|
| 1260 | #ifdef __MSDOS__
|
|---|
| 1261 | {
|
|---|
| 1262 | int proc_return;
|
|---|
| 1263 |
|
|---|
| 1264 | block_sigs ();
|
|---|
| 1265 | dos_status = 0;
|
|---|
| 1266 |
|
|---|
| 1267 | /* We call `system' to do the job of the SHELL, since stock DOS
|
|---|
| 1268 | shell is too dumb. Our `system' knows how to handle long
|
|---|
| 1269 | command lines even if pipes/redirection is needed; it will only
|
|---|
| 1270 | call COMMAND.COM when its internal commands are used. */
|
|---|
| 1271 | if (execute_by_shell)
|
|---|
| 1272 | {
|
|---|
| 1273 | char *cmdline = argv[0];
|
|---|
| 1274 | /* We don't have a way to pass environment to `system',
|
|---|
| 1275 | so we need to save and restore ours, sigh... */
|
|---|
| 1276 | char **parent_environ = environ;
|
|---|
| 1277 |
|
|---|
| 1278 | environ = child->environment;
|
|---|
| 1279 |
|
|---|
| 1280 | /* If we have a *real* shell, tell `system' to call
|
|---|
| 1281 | it to do everything for us. */
|
|---|
| 1282 | if (unixy_shell)
|
|---|
| 1283 | {
|
|---|
| 1284 | /* A *real* shell on MSDOS may not support long
|
|---|
| 1285 | command lines the DJGPP way, so we must use `system'. */
|
|---|
| 1286 | cmdline = argv[2]; /* get past "shell -c" */
|
|---|
| 1287 | }
|
|---|
| 1288 |
|
|---|
| 1289 | dos_command_running = 1;
|
|---|
| 1290 | proc_return = system (cmdline);
|
|---|
| 1291 | environ = parent_environ;
|
|---|
| 1292 | execute_by_shell = 0; /* for the next time */
|
|---|
| 1293 | }
|
|---|
| 1294 | else
|
|---|
| 1295 | {
|
|---|
| 1296 | dos_command_running = 1;
|
|---|
| 1297 | proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
|
|---|
| 1298 | }
|
|---|
| 1299 |
|
|---|
| 1300 | /* Need to unblock signals before turning off
|
|---|
| 1301 | dos_command_running, so that child's signals
|
|---|
| 1302 | will be treated as such (see fatal_error_signal). */
|
|---|
| 1303 | unblock_sigs ();
|
|---|
| 1304 | dos_command_running = 0;
|
|---|
| 1305 |
|
|---|
| 1306 | /* If the child got a signal, dos_status has its
|
|---|
| 1307 | high 8 bits set, so be careful not to alter them. */
|
|---|
| 1308 | if (proc_return == -1)
|
|---|
| 1309 | dos_status |= 0xff;
|
|---|
| 1310 | else
|
|---|
| 1311 | dos_status |= (proc_return & 0xff);
|
|---|
| 1312 | ++dead_children;
|
|---|
| 1313 | child->pid = dos_pid++;
|
|---|
| 1314 | }
|
|---|
| 1315 | #endif /* __MSDOS__ */
|
|---|
| 1316 | #ifdef _AMIGA
|
|---|
| 1317 | amiga_status = MyExecute (argv);
|
|---|
| 1318 |
|
|---|
| 1319 | ++dead_children;
|
|---|
| 1320 | child->pid = amiga_pid++;
|
|---|
| 1321 | if (amiga_batch_file)
|
|---|
| 1322 | {
|
|---|
| 1323 | amiga_batch_file = 0;
|
|---|
| 1324 | DeleteFile (amiga_bname); /* Ignore errors. */
|
|---|
| 1325 | }
|
|---|
| 1326 | #endif /* Amiga */
|
|---|
| 1327 | #ifdef WINDOWS32
|
|---|
| 1328 | {
|
|---|
| 1329 | HANDLE hPID;
|
|---|
| 1330 | char* arg0;
|
|---|
| 1331 |
|
|---|
| 1332 | /* make UNC paths safe for CreateProcess -- backslash format */
|
|---|
| 1333 | arg0 = argv[0];
|
|---|
| 1334 | if (arg0 && arg0[0] == '/' && arg0[1] == '/')
|
|---|
| 1335 | for ( ; arg0 && *arg0; arg0++)
|
|---|
| 1336 | if (*arg0 == '/')
|
|---|
| 1337 | *arg0 = '\\';
|
|---|
| 1338 |
|
|---|
| 1339 | /* make sure CreateProcess() has Path it needs */
|
|---|
| 1340 | sync_Path_environment();
|
|---|
| 1341 |
|
|---|
| 1342 | hPID = process_easy(argv, child->environment);
|
|---|
| 1343 |
|
|---|
| 1344 | if (hPID != INVALID_HANDLE_VALUE)
|
|---|
| 1345 | child->pid = (int) hPID;
|
|---|
| 1346 | else {
|
|---|
| 1347 | int i;
|
|---|
| 1348 | unblock_sigs();
|
|---|
| 1349 | fprintf(stderr,
|
|---|
| 1350 | _("process_easy() failed failed to launch process (e=%d)\n"),
|
|---|
| 1351 | process_last_err(hPID));
|
|---|
| 1352 | for (i = 0; argv[i]; i++)
|
|---|
| 1353 | fprintf(stderr, "%s ", argv[i]);
|
|---|
| 1354 | fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
|
|---|
| 1355 | }
|
|---|
| 1356 | }
|
|---|
| 1357 | #endif /* WINDOWS32 */
|
|---|
| 1358 | #endif /* __MSDOS__ or Amiga or WINDOWS32 */
|
|---|
| 1359 |
|
|---|
| 1360 | /* Bump the number of jobs started in this second. */
|
|---|
| 1361 | ++job_counter;
|
|---|
| 1362 |
|
|---|
| 1363 | /* We are the parent side. Set the state to
|
|---|
| 1364 | say the commands are running and return. */
|
|---|
| 1365 |
|
|---|
| 1366 | set_command_state (child->file, cs_running);
|
|---|
| 1367 |
|
|---|
| 1368 | /* Free the storage used by the child's argument list. */
|
|---|
| 1369 | #ifndef VMS
|
|---|
| 1370 | free (argv[0]);
|
|---|
| 1371 | free ((char *) argv);
|
|---|
| 1372 | #endif
|
|---|
| 1373 |
|
|---|
| 1374 | return;
|
|---|
| 1375 |
|
|---|
| 1376 | error:
|
|---|
| 1377 | child->file->update_status = 2;
|
|---|
| 1378 | notice_finished_file (child->file);
|
|---|
| 1379 | return;
|
|---|
| 1380 | }
|
|---|
| 1381 |
|
|---|
| 1382 | /* Try to start a child running.
|
|---|
| 1383 | Returns nonzero if the child was started (and maybe finished), or zero if
|
|---|
| 1384 | the load was too high and the child was put on the `waiting_jobs' chain. */
|
|---|
| 1385 |
|
|---|
| 1386 | static int
|
|---|
| 1387 | start_waiting_job (struct child *c)
|
|---|
| 1388 | {
|
|---|
| 1389 | struct file *f = c->file;
|
|---|
| 1390 |
|
|---|
| 1391 | /* If we can start a job remotely, we always want to, and don't care about
|
|---|
| 1392 | the local load average. We record that the job should be started
|
|---|
| 1393 | remotely in C->remote for start_job_command to test. */
|
|---|
| 1394 |
|
|---|
| 1395 | c->remote = start_remote_job_p (1);
|
|---|
| 1396 |
|
|---|
| 1397 | /* If we are running at least one job already and the load average
|
|---|
| 1398 | is too high, make this one wait. */
|
|---|
| 1399 | if (!c->remote && job_slots_used > 0 && load_too_high ())
|
|---|
| 1400 | {
|
|---|
| 1401 | /* Put this child on the chain of children waiting for the load average
|
|---|
| 1402 | to go down. */
|
|---|
| 1403 | set_command_state (f, cs_running);
|
|---|
| 1404 | c->next = waiting_jobs;
|
|---|
| 1405 | waiting_jobs = c;
|
|---|
| 1406 | return 0;
|
|---|
| 1407 | }
|
|---|
| 1408 |
|
|---|
| 1409 | /* Start the first command; reap_children will run later command lines. */
|
|---|
| 1410 | start_job_command (c);
|
|---|
| 1411 |
|
|---|
| 1412 | switch (f->command_state)
|
|---|
| 1413 | {
|
|---|
| 1414 | case cs_running:
|
|---|
| 1415 | c->next = children;
|
|---|
| 1416 | DB (DB_JOBS, (_("Putting child 0x%08lx (%s) PID %ld%s on the chain.\n"),
|
|---|
| 1417 | (unsigned long int) c, c->file->name,
|
|---|
| 1418 | (long) c->pid, c->remote ? _(" (remote)") : ""));
|
|---|
| 1419 | children = c;
|
|---|
| 1420 | /* One more job slot is in use. */
|
|---|
| 1421 | ++job_slots_used;
|
|---|
| 1422 | unblock_sigs ();
|
|---|
| 1423 | break;
|
|---|
| 1424 |
|
|---|
| 1425 | case cs_not_started:
|
|---|
| 1426 | /* All the command lines turned out to be empty. */
|
|---|
| 1427 | f->update_status = 0;
|
|---|
| 1428 | /* FALLTHROUGH */
|
|---|
| 1429 |
|
|---|
| 1430 | case cs_finished:
|
|---|
| 1431 | notice_finished_file (f);
|
|---|
| 1432 | free_child (c);
|
|---|
| 1433 | break;
|
|---|
| 1434 |
|
|---|
| 1435 | default:
|
|---|
| 1436 | assert (f->command_state == cs_finished);
|
|---|
| 1437 | break;
|
|---|
| 1438 | }
|
|---|
| 1439 |
|
|---|
| 1440 | return 1;
|
|---|
| 1441 | }
|
|---|
| 1442 |
|
|---|
| 1443 | /* Create a `struct child' for FILE and start its commands running. */
|
|---|
| 1444 |
|
|---|
| 1445 | void
|
|---|
| 1446 | new_job (struct file *file)
|
|---|
| 1447 | {
|
|---|
| 1448 | register struct commands *cmds = file->cmds;
|
|---|
| 1449 | register struct child *c;
|
|---|
| 1450 | char **lines;
|
|---|
| 1451 | register unsigned int i;
|
|---|
| 1452 |
|
|---|
| 1453 | /* Let any previously decided-upon jobs that are waiting
|
|---|
| 1454 | for the load to go down start before this new one. */
|
|---|
| 1455 | start_waiting_jobs ();
|
|---|
| 1456 |
|
|---|
| 1457 | /* Reap any children that might have finished recently. */
|
|---|
| 1458 | reap_children (0, 0);
|
|---|
| 1459 |
|
|---|
| 1460 | /* Chop the commands up into lines if they aren't already. */
|
|---|
| 1461 | chop_commands (cmds);
|
|---|
| 1462 |
|
|---|
| 1463 | /* Expand the command lines and store the results in LINES. */
|
|---|
| 1464 | lines = (char **) xmalloc (cmds->ncommand_lines * sizeof (char *));
|
|---|
| 1465 | for (i = 0; i < cmds->ncommand_lines; ++i)
|
|---|
| 1466 | {
|
|---|
| 1467 | /* Collapse backslash-newline combinations that are inside variable
|
|---|
| 1468 | or function references. These are left alone by the parser so
|
|---|
| 1469 | that they will appear in the echoing of commands (where they look
|
|---|
| 1470 | nice); and collapsed by construct_command_argv when it tokenizes.
|
|---|
| 1471 | But letting them survive inside function invocations loses because
|
|---|
| 1472 | we don't want the functions to see them as part of the text. */
|
|---|
| 1473 |
|
|---|
| 1474 | char *in, *out, *ref;
|
|---|
| 1475 |
|
|---|
| 1476 | /* IN points to where in the line we are scanning.
|
|---|
| 1477 | OUT points to where in the line we are writing.
|
|---|
| 1478 | When we collapse a backslash-newline combination,
|
|---|
| 1479 | IN gets ahead of OUT. */
|
|---|
| 1480 |
|
|---|
| 1481 | in = out = cmds->command_lines[i];
|
|---|
| 1482 | while ((ref = strchr (in, '$')) != 0)
|
|---|
| 1483 | {
|
|---|
| 1484 | ++ref; /* Move past the $. */
|
|---|
| 1485 |
|
|---|
| 1486 | if (out != in)
|
|---|
| 1487 | /* Copy the text between the end of the last chunk
|
|---|
| 1488 | we processed (where IN points) and the new chunk
|
|---|
| 1489 | we are about to process (where REF points). */
|
|---|
| 1490 | bcopy (in, out, ref - in);
|
|---|
| 1491 |
|
|---|
| 1492 | /* Move both pointers past the boring stuff. */
|
|---|
| 1493 | out += ref - in;
|
|---|
| 1494 | in = ref;
|
|---|
| 1495 |
|
|---|
| 1496 | if (*ref == '(' || *ref == '{')
|
|---|
| 1497 | {
|
|---|
| 1498 | char openparen = *ref;
|
|---|
| 1499 | char closeparen = openparen == '(' ? ')' : '}';
|
|---|
| 1500 | int count;
|
|---|
| 1501 | char *p;
|
|---|
| 1502 |
|
|---|
| 1503 | *out++ = *in++; /* Copy OPENPAREN. */
|
|---|
| 1504 | /* IN now points past the opening paren or brace.
|
|---|
| 1505 | Count parens or braces until it is matched. */
|
|---|
| 1506 | count = 0;
|
|---|
| 1507 | while (*in != '\0')
|
|---|
| 1508 | {
|
|---|
| 1509 | if (*in == closeparen && --count < 0)
|
|---|
| 1510 | break;
|
|---|
| 1511 | else if (*in == '\\' && in[1] == '\n')
|
|---|
| 1512 | {
|
|---|
| 1513 | /* We have found a backslash-newline inside a
|
|---|
| 1514 | variable or function reference. Eat it and
|
|---|
| 1515 | any following whitespace. */
|
|---|
| 1516 |
|
|---|
| 1517 | int quoted = 0;
|
|---|
| 1518 | for (p = in - 1; p > ref && *p == '\\'; --p)
|
|---|
| 1519 | quoted = !quoted;
|
|---|
| 1520 |
|
|---|
| 1521 | if (quoted)
|
|---|
| 1522 | /* There were two or more backslashes, so this is
|
|---|
| 1523 | not really a continuation line. We don't collapse
|
|---|
| 1524 | the quoting backslashes here as is done in
|
|---|
| 1525 | collapse_continuations, because the line will
|
|---|
| 1526 | be collapsed again after expansion. */
|
|---|
| 1527 | *out++ = *in++;
|
|---|
| 1528 | else
|
|---|
| 1529 | {
|
|---|
| 1530 | /* Skip the backslash, newline and
|
|---|
| 1531 | any following whitespace. */
|
|---|
| 1532 | in = next_token (in + 2);
|
|---|
| 1533 |
|
|---|
| 1534 | /* Discard any preceding whitespace that has
|
|---|
| 1535 | already been written to the output. */
|
|---|
| 1536 | while (out > ref
|
|---|
| 1537 | && isblank ((unsigned char)out[-1]))
|
|---|
| 1538 | --out;
|
|---|
| 1539 |
|
|---|
| 1540 | /* Replace it all with a single space. */
|
|---|
| 1541 | *out++ = ' ';
|
|---|
| 1542 | }
|
|---|
| 1543 | }
|
|---|
| 1544 | else
|
|---|
| 1545 | {
|
|---|
| 1546 | if (*in == openparen)
|
|---|
| 1547 | ++count;
|
|---|
| 1548 |
|
|---|
| 1549 | *out++ = *in++;
|
|---|
| 1550 | }
|
|---|
| 1551 | }
|
|---|
| 1552 | }
|
|---|
| 1553 | }
|
|---|
| 1554 |
|
|---|
| 1555 | /* There are no more references in this line to worry about.
|
|---|
| 1556 | Copy the remaining uninteresting text to the output. */
|
|---|
| 1557 | if (out != in)
|
|---|
| 1558 | strcpy (out, in);
|
|---|
| 1559 |
|
|---|
| 1560 | /* Finally, expand the line. */
|
|---|
| 1561 | lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
|
|---|
| 1562 | file);
|
|---|
| 1563 | }
|
|---|
| 1564 |
|
|---|
| 1565 | /* Start the command sequence, record it in a new
|
|---|
| 1566 | `struct child', and add that to the chain. */
|
|---|
| 1567 |
|
|---|
| 1568 | c = (struct child *) xmalloc (sizeof (struct child));
|
|---|
| 1569 | bzero ((char *)c, sizeof (struct child));
|
|---|
| 1570 | c->file = file;
|
|---|
| 1571 | c->command_lines = lines;
|
|---|
| 1572 | c->sh_batch_file = NULL;
|
|---|
| 1573 |
|
|---|
| 1574 | /* Fetch the first command line to be run. */
|
|---|
| 1575 | job_next_command (c);
|
|---|
| 1576 |
|
|---|
| 1577 | /* Wait for a job slot to be freed up. If we allow an infinite number
|
|---|
| 1578 | don't bother; also job_slots will == 0 if we're using the jobserver. */
|
|---|
| 1579 |
|
|---|
| 1580 | if (job_slots != 0)
|
|---|
| 1581 | while (job_slots_used == job_slots)
|
|---|
| 1582 | reap_children (1, 0);
|
|---|
| 1583 |
|
|---|
| 1584 | #ifdef MAKE_JOBSERVER
|
|---|
| 1585 | /* If we are controlling multiple jobs make sure we have a token before
|
|---|
| 1586 | starting the child. */
|
|---|
| 1587 |
|
|---|
| 1588 | /* This can be inefficient. There's a decent chance that this job won't
|
|---|
| 1589 | actually have to run any subprocesses: the command script may be empty
|
|---|
| 1590 | or otherwise optimized away. It would be nice if we could defer
|
|---|
| 1591 | obtaining a token until just before we need it, in start_job_command.
|
|---|
| 1592 | To do that we'd need to keep track of whether we'd already obtained a
|
|---|
| 1593 | token (since start_job_command is called for each line of the job, not
|
|---|
| 1594 | just once). Also more thought needs to go into the entire algorithm;
|
|---|
| 1595 | this is where the old parallel job code waits, so... */
|
|---|
| 1596 |
|
|---|
| 1597 | else if (job_fds[0] >= 0)
|
|---|
| 1598 | while (1)
|
|---|
| 1599 | {
|
|---|
| 1600 | char token;
|
|---|
| 1601 | int got_token;
|
|---|
| 1602 | int saved_errno;
|
|---|
| 1603 |
|
|---|
| 1604 | DB (DB_JOBS, ("Need a job token; we %shave children\n",
|
|---|
| 1605 | children ? "" : "don't "));
|
|---|
| 1606 |
|
|---|
| 1607 | /* If we don't already have a job started, use our "free" token. */
|
|---|
| 1608 | if (!children)
|
|---|
| 1609 | break;
|
|---|
| 1610 |
|
|---|
| 1611 | /* Read a token. As long as there's no token available we'll block.
|
|---|
| 1612 | We enable interruptible system calls before the read(2) so that if
|
|---|
| 1613 | we get a SIGCHLD while we're waiting, we'll return with EINTR and
|
|---|
| 1614 | we can process the death(s) and return tokens to the free pool.
|
|---|
| 1615 |
|
|---|
| 1616 | Once we return from the read, we immediately reinstate restartable
|
|---|
| 1617 | system calls. This allows us to not worry about checking for
|
|---|
| 1618 | EINTR on all the other system calls in the program.
|
|---|
| 1619 |
|
|---|
| 1620 | There is one other twist: there is a span between the time
|
|---|
| 1621 | reap_children() does its last check for dead children and the time
|
|---|
| 1622 | the read(2) call is entered, below, where if a child dies we won't
|
|---|
| 1623 | notice. This is extremely serious as it could cause us to
|
|---|
| 1624 | deadlock, given the right set of events.
|
|---|
| 1625 |
|
|---|
| 1626 | To avoid this, we do the following: before we reap_children(), we
|
|---|
| 1627 | dup(2) the read FD on the jobserver pipe. The read(2) call below
|
|---|
| 1628 | uses that new FD. In the signal handler, we close that FD. That
|
|---|
| 1629 | way, if a child dies during the section mentioned above, the
|
|---|
| 1630 | read(2) will be invoked with an invalid FD and will return
|
|---|
| 1631 | immediately with EBADF. */
|
|---|
| 1632 |
|
|---|
| 1633 | /* Make sure we have a dup'd FD. */
|
|---|
| 1634 | if (job_rfd < 0)
|
|---|
| 1635 | {
|
|---|
| 1636 | DB (DB_JOBS, ("Duplicate the job FD\n"));
|
|---|
| 1637 | job_rfd = dup (job_fds[0]);
|
|---|
| 1638 | }
|
|---|
| 1639 |
|
|---|
| 1640 | /* Reap anything that's currently waiting. */
|
|---|
| 1641 | reap_children (0, 0);
|
|---|
| 1642 |
|
|---|
| 1643 | /* If our "free" token has become available, use it. */
|
|---|
| 1644 | if (!children)
|
|---|
| 1645 | break;
|
|---|
| 1646 |
|
|---|
| 1647 | /* Set interruptible system calls, and read() for a job token. */
|
|---|
| 1648 | set_child_handler_action_flags (0);
|
|---|
| 1649 | got_token = read (job_rfd, &token, 1);
|
|---|
| 1650 | saved_errno = errno;
|
|---|
| 1651 | #ifdef __EMX__
|
|---|
| 1652 | /* The child handler must be turned off here. */
|
|---|
| 1653 | signal (SIGCHLD, SIG_DFL);
|
|---|
| 1654 | #endif
|
|---|
| 1655 | set_child_handler_action_flags (SA_RESTART);
|
|---|
| 1656 |
|
|---|
| 1657 | /* If we got one, we're done here. */
|
|---|
| 1658 | if (got_token == 1)
|
|---|
| 1659 | {
|
|---|
| 1660 | DB (DB_JOBS, (_("Obtained token for child 0x%08lx (%s).\n"),
|
|---|
| 1661 | (unsigned long int) c, c->file->name));
|
|---|
| 1662 | break;
|
|---|
| 1663 | }
|
|---|
| 1664 |
|
|---|
| 1665 | /* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,
|
|---|
| 1666 | go back and reap_children(), and try again. */
|
|---|
| 1667 | errno = saved_errno;
|
|---|
| 1668 | if (errno != EINTR && errno != EBADF)
|
|---|
| 1669 | pfatal_with_name (_("read jobs pipe"));
|
|---|
| 1670 | if (errno == EBADF)
|
|---|
| 1671 | DB (DB_JOBS, ("Read returned EBADF.\n"));
|
|---|
| 1672 | }
|
|---|
| 1673 | #endif
|
|---|
| 1674 |
|
|---|
| 1675 | /* The job is now primed. Start it running.
|
|---|
| 1676 | (This will notice if there are in fact no commands.) */
|
|---|
| 1677 | (void) start_waiting_job (c);
|
|---|
| 1678 |
|
|---|
| 1679 | if (job_slots == 1 || not_parallel)
|
|---|
| 1680 | /* Since there is only one job slot, make things run linearly.
|
|---|
| 1681 | Wait for the child to die, setting the state to `cs_finished'. */
|
|---|
| 1682 | while (file->command_state == cs_running)
|
|---|
| 1683 | reap_children (1, 0);
|
|---|
| 1684 |
|
|---|
| 1685 | return;
|
|---|
| 1686 | }
|
|---|
| 1687 | |
|---|
| 1688 |
|
|---|
| 1689 | /* Move CHILD's pointers to the next command for it to execute.
|
|---|
| 1690 | Returns nonzero if there is another command. */
|
|---|
| 1691 |
|
|---|
| 1692 | static int
|
|---|
| 1693 | job_next_command (struct child *child)
|
|---|
| 1694 | {
|
|---|
| 1695 | while (child->command_ptr == 0 || *child->command_ptr == '\0')
|
|---|
| 1696 | {
|
|---|
| 1697 | /* There are no more lines in the expansion of this line. */
|
|---|
| 1698 | if (child->command_line == child->file->cmds->ncommand_lines)
|
|---|
| 1699 | {
|
|---|
| 1700 | /* There are no more lines to be expanded. */
|
|---|
| 1701 | child->command_ptr = 0;
|
|---|
| 1702 | return 0;
|
|---|
| 1703 | }
|
|---|
| 1704 | else
|
|---|
| 1705 | /* Get the next line to run. */
|
|---|
| 1706 | child->command_ptr = child->command_lines[child->command_line++];
|
|---|
| 1707 | }
|
|---|
| 1708 | return 1;
|
|---|
| 1709 | }
|
|---|
| 1710 |
|
|---|
| 1711 | /* Determine if the load average on the system is too high to start a new job.
|
|---|
| 1712 | The real system load average is only recomputed once a second. However, a
|
|---|
| 1713 | very parallel make can easily start tens or even hundreds of jobs in a
|
|---|
| 1714 | second, which brings the system to its knees for a while until that first
|
|---|
| 1715 | batch of jobs clears out.
|
|---|
| 1716 |
|
|---|
| 1717 | To avoid this we use a weighted algorithm to try to account for jobs which
|
|---|
| 1718 | have been started since the last second, and guess what the load average
|
|---|
| 1719 | would be now if it were computed.
|
|---|
| 1720 |
|
|---|
| 1721 | This algorithm was provided by Thomas Riedl <thomas.riedl@siemens.com>,
|
|---|
| 1722 | who writes:
|
|---|
| 1723 |
|
|---|
| 1724 | ! calculate something load-oid and add to the observed sys.load,
|
|---|
| 1725 | ! so that latter can catch up:
|
|---|
| 1726 | ! - every job started increases jobctr;
|
|---|
| 1727 | ! - every dying job decreases a positive jobctr;
|
|---|
| 1728 | ! - the jobctr value gets zeroed every change of seconds,
|
|---|
| 1729 | ! after its value*weight_b is stored into the 'backlog' value last_sec
|
|---|
| 1730 | ! - weight_a times the sum of jobctr and last_sec gets
|
|---|
| 1731 | ! added to the observed sys.load.
|
|---|
| 1732 | !
|
|---|
| 1733 | ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
|
|---|
| 1734 | ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
|
|---|
| 1735 | ! sub-shelled commands (rm, echo, sed...) for tests.
|
|---|
| 1736 | ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
|
|---|
| 1737 | ! resulted in significant excession of the load limit, raising it
|
|---|
| 1738 | ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
|
|---|
| 1739 | ! reach the limit in most test cases.
|
|---|
| 1740 | !
|
|---|
| 1741 | ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
|
|---|
| 1742 | ! exceeding the limit for longer-running stuff (compile jobs in
|
|---|
| 1743 | ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
|
|---|
| 1744 | ! small jobs' effects.
|
|---|
| 1745 |
|
|---|
| 1746 | */
|
|---|
| 1747 |
|
|---|
| 1748 | #define LOAD_WEIGHT_A 0.25
|
|---|
| 1749 | #define LOAD_WEIGHT_B 0.25
|
|---|
| 1750 |
|
|---|
| 1751 | static int
|
|---|
| 1752 | load_too_high (void)
|
|---|
| 1753 | {
|
|---|
| 1754 | #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA)
|
|---|
| 1755 | return 1;
|
|---|
| 1756 | #else
|
|---|
| 1757 | static double last_sec;
|
|---|
| 1758 | static time_t last_now;
|
|---|
| 1759 | double load, guess;
|
|---|
| 1760 | time_t now;
|
|---|
| 1761 |
|
|---|
| 1762 | if (max_load_average < 0)
|
|---|
| 1763 | return 0;
|
|---|
| 1764 |
|
|---|
| 1765 | /* Find the real system load average. */
|
|---|
| 1766 | make_access ();
|
|---|
| 1767 | if (getloadavg (&load, 1) != 1)
|
|---|
| 1768 | {
|
|---|
| 1769 | static int lossage = -1;
|
|---|
| 1770 | /* Complain only once for the same error. */
|
|---|
| 1771 | if (lossage == -1 || errno != lossage)
|
|---|
| 1772 | {
|
|---|
| 1773 | if (errno == 0)
|
|---|
| 1774 | /* An errno value of zero means getloadavg is just unsupported. */
|
|---|
| 1775 | error (NILF,
|
|---|
| 1776 | _("cannot enforce load limits on this operating system"));
|
|---|
| 1777 | else
|
|---|
| 1778 | perror_with_name (_("cannot enforce load limit: "), "getloadavg");
|
|---|
| 1779 | }
|
|---|
| 1780 | lossage = errno;
|
|---|
| 1781 | load = 0;
|
|---|
| 1782 | }
|
|---|
| 1783 | user_access ();
|
|---|
| 1784 |
|
|---|
| 1785 | /* If we're in a new second zero the counter and correct the backlog
|
|---|
| 1786 | value. Only keep the backlog for one extra second; after that it's 0. */
|
|---|
| 1787 | now = time (NULL);
|
|---|
| 1788 | if (last_now < now)
|
|---|
| 1789 | {
|
|---|
| 1790 | if (last_now == now - 1)
|
|---|
| 1791 | last_sec = LOAD_WEIGHT_B * job_counter;
|
|---|
| 1792 | else
|
|---|
| 1793 | last_sec = 0.0;
|
|---|
| 1794 |
|
|---|
| 1795 | job_counter = 0;
|
|---|
| 1796 | last_now = now;
|
|---|
| 1797 | }
|
|---|
| 1798 |
|
|---|
| 1799 | /* Try to guess what the load would be right now. */
|
|---|
| 1800 | guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
|
|---|
| 1801 |
|
|---|
| 1802 | DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
|
|---|
| 1803 | guess, load, max_load_average));
|
|---|
| 1804 |
|
|---|
| 1805 | return guess >= max_load_average;
|
|---|
| 1806 | #endif
|
|---|
| 1807 | }
|
|---|
| 1808 |
|
|---|
| 1809 | /* Start jobs that are waiting for the load to be lower. */
|
|---|
| 1810 |
|
|---|
| 1811 | void
|
|---|
| 1812 | start_waiting_jobs (void)
|
|---|
| 1813 | {
|
|---|
| 1814 | struct child *job;
|
|---|
| 1815 |
|
|---|
| 1816 | if (waiting_jobs == 0)
|
|---|
| 1817 | return;
|
|---|
| 1818 |
|
|---|
| 1819 | do
|
|---|
| 1820 | {
|
|---|
| 1821 | /* Check for recently deceased descendants. */
|
|---|
| 1822 | reap_children (0, 0);
|
|---|
| 1823 |
|
|---|
| 1824 | /* Take a job off the waiting list. */
|
|---|
| 1825 | job = waiting_jobs;
|
|---|
| 1826 | waiting_jobs = job->next;
|
|---|
| 1827 |
|
|---|
| 1828 | /* Try to start that job. We break out of the loop as soon
|
|---|
| 1829 | as start_waiting_job puts one back on the waiting list. */
|
|---|
| 1830 | }
|
|---|
| 1831 | while (start_waiting_job (job) && waiting_jobs != 0);
|
|---|
| 1832 |
|
|---|
| 1833 | return;
|
|---|
| 1834 | }
|
|---|
| 1835 | |
|---|
| 1836 |
|
|---|
| 1837 | #ifndef WINDOWS32
|
|---|
| 1838 | #ifdef VMS
|
|---|
| 1839 | #include <descrip.h>
|
|---|
| 1840 | #include <clidef.h>
|
|---|
| 1841 |
|
|---|
| 1842 | /* This is called as an AST when a child process dies (it won't get
|
|---|
| 1843 | interrupted by anything except a higher level AST).
|
|---|
| 1844 | */
|
|---|
| 1845 | int vmsHandleChildTerm(struct child *child)
|
|---|
| 1846 | {
|
|---|
| 1847 | int status;
|
|---|
| 1848 | register struct child *lastc, *c;
|
|---|
| 1849 | int child_failed;
|
|---|
| 1850 |
|
|---|
| 1851 | vms_jobsefnmask &= ~(1 << (child->efn - 32));
|
|---|
| 1852 |
|
|---|
| 1853 | lib$free_ef(&child->efn);
|
|---|
| 1854 |
|
|---|
| 1855 | (void) sigblock (fatal_signal_mask);
|
|---|
| 1856 |
|
|---|
| 1857 | child_failed = !(child->cstatus & 1 || ((child->cstatus & 7) == 0));
|
|---|
| 1858 |
|
|---|
| 1859 | /* Search for a child matching the deceased one. */
|
|---|
| 1860 | lastc = 0;
|
|---|
| 1861 | #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
|
|---|
| 1862 | for (c = children; c != 0 && c != child; lastc = c, c = c->next);
|
|---|
| 1863 | #else
|
|---|
| 1864 | c = child;
|
|---|
| 1865 | #endif
|
|---|
| 1866 |
|
|---|
| 1867 | if (child_failed && !c->noerror && !ignore_errors_flag)
|
|---|
| 1868 | {
|
|---|
| 1869 | /* The commands failed. Write an error message,
|
|---|
| 1870 | delete non-precious targets, and abort. */
|
|---|
| 1871 | child_error (c->file->name, c->cstatus, 0, 0, 0);
|
|---|
| 1872 | c->file->update_status = 1;
|
|---|
| 1873 | delete_child_targets (c);
|
|---|
| 1874 | }
|
|---|
| 1875 | else
|
|---|
| 1876 | {
|
|---|
| 1877 | if (child_failed)
|
|---|
| 1878 | {
|
|---|
| 1879 | /* The commands failed, but we don't care. */
|
|---|
| 1880 | child_error (c->file->name, c->cstatus, 0, 0, 1);
|
|---|
| 1881 | child_failed = 0;
|
|---|
| 1882 | }
|
|---|
| 1883 |
|
|---|
| 1884 | #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
|
|---|
| 1885 | /* If there are more commands to run, try to start them. */
|
|---|
| 1886 | start_job (c);
|
|---|
| 1887 |
|
|---|
| 1888 | switch (c->file->command_state)
|
|---|
| 1889 | {
|
|---|
| 1890 | case cs_running:
|
|---|
| 1891 | /* Successfully started. */
|
|---|
| 1892 | break;
|
|---|
| 1893 |
|
|---|
| 1894 | case cs_finished:
|
|---|
| 1895 | if (c->file->update_status != 0) {
|
|---|
| 1896 | /* We failed to start the commands. */
|
|---|
| 1897 | delete_child_targets (c);
|
|---|
| 1898 | }
|
|---|
| 1899 | break;
|
|---|
| 1900 |
|
|---|
| 1901 | default:
|
|---|
| 1902 | error (NILF, _("internal error: `%s' command_state"),
|
|---|
| 1903 | c->file->name);
|
|---|
| 1904 | abort ();
|
|---|
| 1905 | break;
|
|---|
| 1906 | }
|
|---|
| 1907 | #endif /* RECURSIVEJOBS */
|
|---|
| 1908 | }
|
|---|
| 1909 |
|
|---|
| 1910 | /* Set the state flag to say the commands have finished. */
|
|---|
| 1911 | c->file->command_state = cs_finished;
|
|---|
| 1912 | notice_finished_file (c->file);
|
|---|
| 1913 |
|
|---|
| 1914 | #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
|
|---|
| 1915 | /* Remove the child from the chain and free it. */
|
|---|
| 1916 | if (lastc == 0)
|
|---|
| 1917 | children = c->next;
|
|---|
| 1918 | else
|
|---|
| 1919 | lastc->next = c->next;
|
|---|
| 1920 | free_child (c);
|
|---|
| 1921 | #endif /* RECURSIVEJOBS */
|
|---|
| 1922 |
|
|---|
| 1923 | /* There is now another slot open. */
|
|---|
| 1924 | if (job_slots_used > 0)
|
|---|
| 1925 | --job_slots_used;
|
|---|
| 1926 |
|
|---|
| 1927 | /* If the job failed, and the -k flag was not given, die. */
|
|---|
| 1928 | if (child_failed && !keep_going_flag)
|
|---|
| 1929 | die (EXIT_FAILURE);
|
|---|
| 1930 |
|
|---|
| 1931 | (void) sigsetmask (sigblock (0) & ~(fatal_signal_mask));
|
|---|
| 1932 |
|
|---|
| 1933 | return 1;
|
|---|
| 1934 | }
|
|---|
| 1935 |
|
|---|
| 1936 | /* VMS:
|
|---|
| 1937 | Spawn a process executing the command in ARGV and return its pid. */
|
|---|
| 1938 |
|
|---|
| 1939 | #define MAXCMDLEN 200
|
|---|
| 1940 |
|
|---|
| 1941 | /* local helpers to make ctrl+c and ctrl+y working, see below */
|
|---|
| 1942 | #include <iodef.h>
|
|---|
| 1943 | #include <libclidef.h>
|
|---|
| 1944 | #include <ssdef.h>
|
|---|
| 1945 |
|
|---|
| 1946 | static int ctrlMask= LIB$M_CLI_CTRLY;
|
|---|
| 1947 | static int oldCtrlMask;
|
|---|
| 1948 | static int setupYAstTried= 0;
|
|---|
| 1949 | static int pidToAbort= 0;
|
|---|
| 1950 | static int chan= 0;
|
|---|
| 1951 |
|
|---|
| 1952 | static void reEnableAst(void) {
|
|---|
| 1953 | lib$enable_ctrl (&oldCtrlMask,0);
|
|---|
| 1954 | }
|
|---|
| 1955 |
|
|---|
| 1956 | static astHandler (void) {
|
|---|
| 1957 | if (pidToAbort) {
|
|---|
| 1958 | sys$forcex (&pidToAbort, 0, SS$_ABORT);
|
|---|
| 1959 | pidToAbort= 0;
|
|---|
| 1960 | }
|
|---|
| 1961 | kill (getpid(),SIGQUIT);
|
|---|
| 1962 | }
|
|---|
| 1963 |
|
|---|
| 1964 | static void tryToSetupYAst(void) {
|
|---|
| 1965 | $DESCRIPTOR(inputDsc,"SYS$COMMAND");
|
|---|
| 1966 | int status;
|
|---|
| 1967 | struct {
|
|---|
| 1968 | short int status, count;
|
|---|
| 1969 | int dvi;
|
|---|
| 1970 | } iosb;
|
|---|
| 1971 |
|
|---|
| 1972 | setupYAstTried++;
|
|---|
| 1973 |
|
|---|
| 1974 | if (!chan) {
|
|---|
| 1975 | status= sys$assign(&inputDsc,&chan,0,0);
|
|---|
| 1976 | if (!(status&SS$_NORMAL)) {
|
|---|
| 1977 | lib$signal(status);
|
|---|
| 1978 | return;
|
|---|
| 1979 | }
|
|---|
| 1980 | }
|
|---|
| 1981 | status= sys$qiow (0, chan, IO$_SETMODE|IO$M_CTRLYAST,&iosb,0,0,
|
|---|
| 1982 | astHandler,0,0,0,0,0);
|
|---|
| 1983 | if (status==SS$_ILLIOFUNC) {
|
|---|
| 1984 | sys$dassgn(chan);
|
|---|
| 1985 | #ifdef CTRLY_ENABLED_ANYWAY
|
|---|
| 1986 | fprintf (stderr,
|
|---|
| 1987 | _("-warning, CTRL-Y will leave sub-process(es) around.\n"));
|
|---|
| 1988 | #else
|
|---|
| 1989 | return;
|
|---|
| 1990 | #endif
|
|---|
| 1991 | }
|
|---|
| 1992 | if (status==SS$_NORMAL)
|
|---|
| 1993 | status= iosb.status;
|
|---|
| 1994 | if (!(status&SS$_NORMAL)) {
|
|---|
| 1995 | lib$signal(status);
|
|---|
| 1996 | return;
|
|---|
| 1997 | }
|
|---|
| 1998 |
|
|---|
| 1999 | /* called from AST handler ? */
|
|---|
| 2000 | if (setupYAstTried>1)
|
|---|
| 2001 | return;
|
|---|
| 2002 | if (atexit(reEnableAst))
|
|---|
| 2003 | fprintf (stderr,
|
|---|
| 2004 | _("-warning, you may have to re-enable CTRL-Y handling from DCL.\n"));
|
|---|
| 2005 | status= lib$disable_ctrl (&ctrlMask, &oldCtrlMask);
|
|---|
| 2006 | if (!(status&SS$_NORMAL)) {
|
|---|
| 2007 | lib$signal(status);
|
|---|
| 2008 | return;
|
|---|
| 2009 | }
|
|---|
| 2010 | }
|
|---|
| 2011 | int
|
|---|
| 2012 | child_execute_job (char *argv, struct child *child)
|
|---|
| 2013 | {
|
|---|
| 2014 | int i;
|
|---|
| 2015 | static struct dsc$descriptor_s cmddsc;
|
|---|
| 2016 | static struct dsc$descriptor_s pnamedsc;
|
|---|
| 2017 | static struct dsc$descriptor_s ifiledsc;
|
|---|
| 2018 | static struct dsc$descriptor_s ofiledsc;
|
|---|
| 2019 | static struct dsc$descriptor_s efiledsc;
|
|---|
| 2020 | int have_redirection = 0;
|
|---|
| 2021 | int have_newline = 0;
|
|---|
| 2022 |
|
|---|
| 2023 | int spflags = CLI$M_NOWAIT;
|
|---|
| 2024 | int status;
|
|---|
| 2025 | char *cmd = alloca (strlen (argv) + 512), *p, *q;
|
|---|
| 2026 | char ifile[256], ofile[256], efile[256];
|
|---|
| 2027 | char *comname = 0;
|
|---|
| 2028 | char procname[100];
|
|---|
| 2029 |
|
|---|
| 2030 | /* Parse IO redirection. */
|
|---|
| 2031 |
|
|---|
| 2032 | ifile[0] = 0;
|
|---|
| 2033 | ofile[0] = 0;
|
|---|
| 2034 | efile[0] = 0;
|
|---|
| 2035 |
|
|---|
| 2036 | DB (DB_JOBS, ("child_execute_job (%s)\n", argv));
|
|---|
| 2037 |
|
|---|
| 2038 | while (isspace ((unsigned char)*argv))
|
|---|
| 2039 | argv++;
|
|---|
| 2040 |
|
|---|
| 2041 | if (*argv == 0)
|
|---|
| 2042 | return 0;
|
|---|
| 2043 |
|
|---|
| 2044 | sprintf (procname, "GMAKE_%05x", getpid () & 0xfffff);
|
|---|
| 2045 | pnamedsc.dsc$w_length = strlen(procname);
|
|---|
| 2046 | pnamedsc.dsc$a_pointer = procname;
|
|---|
| 2047 | pnamedsc.dsc$b_dtype = DSC$K_DTYPE_T;
|
|---|
| 2048 | pnamedsc.dsc$b_class = DSC$K_CLASS_S;
|
|---|
| 2049 |
|
|---|
| 2050 | /* Handle comments and redirection. */
|
|---|
| 2051 | for (p = argv, q = cmd; *p; p++, q++)
|
|---|
| 2052 | {
|
|---|
| 2053 | switch (*p)
|
|---|
| 2054 | {
|
|---|
| 2055 | case '#':
|
|---|
| 2056 | *p-- = 0;
|
|---|
| 2057 | *q-- = 0;
|
|---|
| 2058 | break;
|
|---|
| 2059 | case '\\':
|
|---|
| 2060 | p++;
|
|---|
| 2061 | if (*p == '\n')
|
|---|
| 2062 | p++;
|
|---|
| 2063 | if (isspace ((unsigned char)*p))
|
|---|
| 2064 | {
|
|---|
| 2065 | do { p++; } while (isspace ((unsigned char)*p));
|
|---|
| 2066 | p--;
|
|---|
| 2067 | }
|
|---|
| 2068 | *q = *p;
|
|---|
| 2069 | break;
|
|---|
| 2070 | case '<':
|
|---|
| 2071 | p = vms_redirect (&ifiledsc, ifile, p);
|
|---|
| 2072 | *q = ' ';
|
|---|
| 2073 | have_redirection = 1;
|
|---|
| 2074 | break;
|
|---|
| 2075 | case '>':
|
|---|
| 2076 | have_redirection = 1;
|
|---|
| 2077 | if (*(p-1) == '2')
|
|---|
| 2078 | {
|
|---|
| 2079 | q--;
|
|---|
| 2080 | if (strncmp (p, ">&1", 3) == 0)
|
|---|
| 2081 | {
|
|---|
| 2082 | p += 3;
|
|---|
| 2083 | strcpy (efile, "sys$output");
|
|---|
| 2084 | efiledsc.dsc$w_length = strlen(efile);
|
|---|
| 2085 | efiledsc.dsc$a_pointer = efile;
|
|---|
| 2086 | efiledsc.dsc$b_dtype = DSC$K_DTYPE_T;
|
|---|
| 2087 | efiledsc.dsc$b_class = DSC$K_CLASS_S;
|
|---|
| 2088 | }
|
|---|
| 2089 | else
|
|---|
| 2090 | {
|
|---|
| 2091 | p = vms_redirect (&efiledsc, efile, p);
|
|---|
| 2092 | }
|
|---|
| 2093 | }
|
|---|
| 2094 | else
|
|---|
| 2095 | {
|
|---|
| 2096 | p = vms_redirect (&ofiledsc, ofile, p);
|
|---|
| 2097 | }
|
|---|
| 2098 | *q = ' ';
|
|---|
| 2099 | break;
|
|---|
| 2100 | case '\n':
|
|---|
| 2101 | have_newline = 1;
|
|---|
| 2102 | default:
|
|---|
| 2103 | *q = *p;
|
|---|
| 2104 | break;
|
|---|
| 2105 | }
|
|---|
| 2106 | }
|
|---|
| 2107 | *q = *p;
|
|---|
| 2108 |
|
|---|
| 2109 | if (strncmp (cmd, "builtin_", 8) == 0)
|
|---|
| 2110 | {
|
|---|
| 2111 | child->pid = 270163;
|
|---|
| 2112 | child->efn = 0;
|
|---|
| 2113 | child->cstatus = 1;
|
|---|
| 2114 |
|
|---|
| 2115 | DB (DB_JOBS, (_("BUILTIN [%s][%s]\n"), cmd, cmd+8));
|
|---|
| 2116 |
|
|---|
| 2117 | p = cmd + 8;
|
|---|
| 2118 |
|
|---|
| 2119 | if ((*(p) == 'c')
|
|---|
| 2120 | && (*(p+1) == 'd')
|
|---|
| 2121 | && ((*(p+2) == ' ') || (*(p+2) == '\t')))
|
|---|
| 2122 | {
|
|---|
| 2123 | p += 3;
|
|---|
| 2124 | while ((*p == ' ') || (*p == '\t'))
|
|---|
| 2125 | p++;
|
|---|
| 2126 | DB (DB_JOBS, (_("BUILTIN CD %s\n"), p));
|
|---|
| 2127 | if (chdir (p))
|
|---|
| 2128 | return 0;
|
|---|
| 2129 | else
|
|---|
| 2130 | return 1;
|
|---|
| 2131 | }
|
|---|
| 2132 | else if ((*(p) == 'r')
|
|---|
| 2133 | && (*(p+1) == 'm')
|
|---|
| 2134 | && ((*(p+2) == ' ') || (*(p+2) == '\t')))
|
|---|
| 2135 | {
|
|---|
| 2136 | int in_arg;
|
|---|
| 2137 |
|
|---|
| 2138 | /* rm */
|
|---|
| 2139 | p += 3;
|
|---|
| 2140 | while ((*p == ' ') || (*p == '\t'))
|
|---|
| 2141 | p++;
|
|---|
| 2142 | in_arg = 1;
|
|---|
| 2143 |
|
|---|
| 2144 | DB (DB_JOBS, (_("BUILTIN RM %s\n"), p));
|
|---|
| 2145 | while (*p)
|
|---|
| 2146 | {
|
|---|
| 2147 | switch (*p)
|
|---|
| 2148 | {
|
|---|
| 2149 | case ' ':
|
|---|
| 2150 | case '\t':
|
|---|
| 2151 | if (in_arg)
|
|---|
| 2152 | {
|
|---|
| 2153 | *p++ = ';';
|
|---|
| 2154 | in_arg = 0;
|
|---|
| 2155 | }
|
|---|
| 2156 | break;
|
|---|
| 2157 | default:
|
|---|
| 2158 | break;
|
|---|
| 2159 | }
|
|---|
| 2160 | p++;
|
|---|
| 2161 | }
|
|---|
| 2162 | }
|
|---|
| 2163 | else
|
|---|
| 2164 | {
|
|---|
| 2165 | printf(_("Unknown builtin command '%s'\n"), cmd);
|
|---|
| 2166 | fflush(stdout);
|
|---|
| 2167 | return 0;
|
|---|
| 2168 | }
|
|---|
| 2169 | }
|
|---|
| 2170 |
|
|---|
| 2171 | /* Create a *.com file if either the command is too long for
|
|---|
| 2172 | lib$spawn, or the command contains a newline, or if redirection
|
|---|
| 2173 | is desired. Forcing commands with newlines into DCLs allows to
|
|---|
| 2174 | store search lists on user mode logicals. */
|
|---|
| 2175 |
|
|---|
| 2176 | if (strlen (cmd) > MAXCMDLEN
|
|---|
| 2177 | || (have_redirection != 0)
|
|---|
| 2178 | || (have_newline != 0))
|
|---|
| 2179 | {
|
|---|
| 2180 | FILE *outfile;
|
|---|
| 2181 | char c;
|
|---|
| 2182 | char *sep;
|
|---|
| 2183 | int alevel = 0; /* apostrophe level */
|
|---|
| 2184 |
|
|---|
| 2185 | if (strlen (cmd) == 0)
|
|---|
| 2186 | {
|
|---|
| 2187 | printf (_("Error, empty command\n"));
|
|---|
| 2188 | fflush (stdout);
|
|---|
| 2189 | return 0;
|
|---|
| 2190 | }
|
|---|
| 2191 |
|
|---|
| 2192 | outfile = open_tmpfile (&comname, "sys$scratch:CMDXXXXXX.COM");
|
|---|
| 2193 | if (outfile == 0)
|
|---|
| 2194 | pfatal_with_name (_("fopen (temporary file)"));
|
|---|
| 2195 |
|
|---|
| 2196 | if (ifile[0])
|
|---|
| 2197 | {
|
|---|
| 2198 | fprintf (outfile, "$ assign/user %s sys$input\n", ifile);
|
|---|
| 2199 | DB (DB_JOBS, (_("Redirected input from %s\n"), ifile));
|
|---|
| 2200 | ifiledsc.dsc$w_length = 0;
|
|---|
| 2201 | }
|
|---|
| 2202 |
|
|---|
| 2203 | if (efile[0])
|
|---|
| 2204 | {
|
|---|
| 2205 | fprintf (outfile, "$ define sys$error %s\n", efile);
|
|---|
| 2206 | DB (DB_JOBS, (_("Redirected error to %s\n"), efile));
|
|---|
| 2207 | efiledsc.dsc$w_length = 0;
|
|---|
| 2208 | }
|
|---|
| 2209 |
|
|---|
| 2210 | if (ofile[0])
|
|---|
| 2211 | {
|
|---|
| 2212 | fprintf (outfile, "$ define sys$output %s\n", ofile);
|
|---|
| 2213 | DB (DB_JOBS, (_("Redirected output to %s\n"), ofile));
|
|---|
| 2214 | ofiledsc.dsc$w_length = 0;
|
|---|
| 2215 | }
|
|---|
| 2216 |
|
|---|
| 2217 | p = sep = q = cmd;
|
|---|
| 2218 | for (c = '\n'; c; c = *q++)
|
|---|
| 2219 | {
|
|---|
| 2220 | switch (c)
|
|---|
| 2221 | {
|
|---|
| 2222 | case '\n':
|
|---|
| 2223 | /* At a newline, skip any whitespace around a leading $
|
|---|
| 2224 | from the command and issue exactly one $ into the DCL. */
|
|---|
| 2225 | while (isspace ((unsigned char)*p))
|
|---|
| 2226 | p++;
|
|---|
| 2227 | if (*p == '$')
|
|---|
| 2228 | p++;
|
|---|
| 2229 | while (isspace ((unsigned char)*p))
|
|---|
| 2230 | p++;
|
|---|
| 2231 | fwrite (p, 1, q - p, outfile);
|
|---|
| 2232 | fputc ('$', outfile);
|
|---|
| 2233 | fputc (' ', outfile);
|
|---|
| 2234 | /* Reset variables. */
|
|---|
| 2235 | p = sep = q;
|
|---|
| 2236 | break;
|
|---|
| 2237 |
|
|---|
| 2238 | /* Nice places for line breaks are after strings, after
|
|---|
| 2239 | comma or space and before slash. */
|
|---|
| 2240 | case '"':
|
|---|
| 2241 | q = vms_handle_apos (q);
|
|---|
| 2242 | sep = q;
|
|---|
| 2243 | break;
|
|---|
| 2244 | case ',':
|
|---|
| 2245 | case ' ':
|
|---|
| 2246 | sep = q;
|
|---|
| 2247 | break;
|
|---|
| 2248 | case '/':
|
|---|
| 2249 | case '\0':
|
|---|
| 2250 | sep = q - 1;
|
|---|
| 2251 | break;
|
|---|
| 2252 | default:
|
|---|
| 2253 | break;
|
|---|
| 2254 | }
|
|---|
| 2255 | if (sep - p > 78)
|
|---|
| 2256 | {
|
|---|
| 2257 | /* Enough stuff for a line. */
|
|---|
| 2258 | fwrite (p, 1, sep - p, outfile);
|
|---|
| 2259 | p = sep;
|
|---|
| 2260 | if (*sep)
|
|---|
| 2261 | {
|
|---|
| 2262 | /* The command continues. */
|
|---|
| 2263 | fputc ('-', outfile);
|
|---|
| 2264 | }
|
|---|
| 2265 | fputc ('\n', outfile);
|
|---|
| 2266 | }
|
|---|
| 2267 | }
|
|---|
| 2268 |
|
|---|
| 2269 | fwrite (p, 1, q - p, outfile);
|
|---|
| 2270 | fputc ('\n', outfile);
|
|---|
| 2271 |
|
|---|
| 2272 | fclose (outfile);
|
|---|
| 2273 |
|
|---|
| 2274 | sprintf (cmd, "$ @%s", comname);
|
|---|
| 2275 |
|
|---|
| 2276 | DB (DB_JOBS, (_("Executing %s instead\n"), cmd));
|
|---|
| 2277 | }
|
|---|
| 2278 |
|
|---|
| 2279 | cmddsc.dsc$w_length = strlen(cmd);
|
|---|
| 2280 | cmddsc.dsc$a_pointer = cmd;
|
|---|
| 2281 | cmddsc.dsc$b_dtype = DSC$K_DTYPE_T;
|
|---|
| 2282 | cmddsc.dsc$b_class = DSC$K_CLASS_S;
|
|---|
| 2283 |
|
|---|
| 2284 | child->efn = 0;
|
|---|
| 2285 | while (child->efn < 32 || child->efn > 63)
|
|---|
| 2286 | {
|
|---|
| 2287 | status = lib$get_ef ((unsigned long *)&child->efn);
|
|---|
| 2288 | if (!(status & 1))
|
|---|
| 2289 | return 0;
|
|---|
| 2290 | }
|
|---|
| 2291 |
|
|---|
| 2292 | sys$clref (child->efn);
|
|---|
| 2293 |
|
|---|
| 2294 | vms_jobsefnmask |= (1 << (child->efn - 32));
|
|---|
| 2295 |
|
|---|
| 2296 | /*
|
|---|
| 2297 | LIB$SPAWN [command-string]
|
|---|
| 2298 | [,input-file]
|
|---|
| 2299 | [,output-file]
|
|---|
| 2300 | [,flags]
|
|---|
| 2301 | [,process-name]
|
|---|
| 2302 | [,process-id] [,completion-status-address] [,byte-integer-event-flag-num]
|
|---|
| 2303 | [,AST-address] [,varying-AST-argument]
|
|---|
| 2304 | [,prompt-string] [,cli] [,table]
|
|---|
| 2305 | */
|
|---|
| 2306 |
|
|---|
| 2307 | #ifndef DONTWAITFORCHILD
|
|---|
| 2308 | /*
|
|---|
| 2309 | * Code to make ctrl+c and ctrl+y working.
|
|---|
| 2310 | * The problem starts with the synchronous case where after lib$spawn is
|
|---|
| 2311 | * called any input will go to the child. But with input re-directed,
|
|---|
| 2312 | * both control characters won't make it to any of the programs, neither
|
|---|
| 2313 | * the spawning nor to the spawned one. Hence the caller needs to spawn
|
|---|
| 2314 | * with CLI$M_NOWAIT to NOT give up the input focus. A sys$waitfr
|
|---|
| 2315 | * has to follow to simulate the wanted synchronous behaviour.
|
|---|
| 2316 | * The next problem is ctrl+y which isn't caught by the crtl and
|
|---|
| 2317 | * therefore isn't converted to SIGQUIT (for a signal handler which is
|
|---|
| 2318 | * already established). The only way to catch ctrl+y, is an AST
|
|---|
| 2319 | * assigned to the input channel. But ctrl+y handling of DCL needs to be
|
|---|
| 2320 | * disabled, otherwise it will handle it. Not to mention the previous
|
|---|
| 2321 | * ctrl+y handling of DCL needs to be re-established before make exits.
|
|---|
| 2322 | * One more: At the time of LIB$SPAWN signals are blocked. SIGQUIT will
|
|---|
| 2323 | * make it to the signal handler after the child "normally" terminates.
|
|---|
| 2324 | * This isn't enough. It seems reasonable for simple command lines like
|
|---|
| 2325 | * a 'cc foobar.c' spawned in a subprocess but it is unacceptable for
|
|---|
| 2326 | * spawning make. Therefore we need to abort the process in the AST.
|
|---|
| 2327 | *
|
|---|
| 2328 | * Prior to the spawn it is checked if an AST is already set up for
|
|---|
| 2329 | * ctrl+y, if not one is set up for a channel to SYS$COMMAND. In general
|
|---|
| 2330 | * this will work except if make is run in a batch environment, but there
|
|---|
| 2331 | * nobody can press ctrl+y. During the setup the DCL handling of ctrl+y
|
|---|
| 2332 | * is disabled and an exit handler is established to re-enable it.
|
|---|
| 2333 | * If the user interrupts with ctrl+y, the assigned AST will fire, force
|
|---|
| 2334 | * an abort to the subprocess and signal SIGQUIT, which will be caught by
|
|---|
| 2335 | * the already established handler and will bring us back to common code.
|
|---|
| 2336 | * After the spawn (now /nowait) a sys$waitfr simulates the /wait and
|
|---|
| 2337 | * enables the ctrl+y be delivered to this code. And the ctrl+c too,
|
|---|
| 2338 | * which the crtl converts to SIGINT and which is caught by the common
|
|---|
| 2339 | * signal handler. Because signals were blocked before entering this code
|
|---|
| 2340 | * sys$waitfr will always complete and the SIGQUIT will be processed after
|
|---|
| 2341 | * it (after termination of the current block, somewhere in common code).
|
|---|
| 2342 | * And SIGINT too will be delayed. That is ctrl+c can only abort when the
|
|---|
| 2343 | * current command completes. Anyway it's better than nothing :-)
|
|---|
| 2344 | */
|
|---|
| 2345 |
|
|---|
| 2346 | if (!setupYAstTried)
|
|---|
| 2347 | tryToSetupYAst();
|
|---|
| 2348 | status = lib$spawn (&cmddsc, /* cmd-string */
|
|---|
| 2349 | (ifiledsc.dsc$w_length == 0)?0:&ifiledsc, /* input-file */
|
|---|
| 2350 | (ofiledsc.dsc$w_length == 0)?0:&ofiledsc, /* output-file */
|
|---|
| 2351 | &spflags, /* flags */
|
|---|
| 2352 | &pnamedsc, /* proc name */
|
|---|
| 2353 | &child->pid, &child->cstatus, &child->efn,
|
|---|
| 2354 | 0, 0,
|
|---|
| 2355 | 0, 0, 0);
|
|---|
| 2356 | if (status & 1)
|
|---|
| 2357 | {
|
|---|
| 2358 | pidToAbort= child->pid;
|
|---|
| 2359 | status= sys$waitfr (child->efn);
|
|---|
| 2360 | pidToAbort= 0;
|
|---|
| 2361 | vmsHandleChildTerm(child);
|
|---|
| 2362 | }
|
|---|
| 2363 | #else
|
|---|
| 2364 | status = lib$spawn (&cmddsc,
|
|---|
| 2365 | (ifiledsc.dsc$w_length == 0)?0:&ifiledsc,
|
|---|
| 2366 | (ofiledsc.dsc$w_length == 0)?0:&ofiledsc,
|
|---|
| 2367 | &spflags,
|
|---|
| 2368 | &pnamedsc,
|
|---|
| 2369 | &child->pid, &child->cstatus, &child->efn,
|
|---|
| 2370 | vmsHandleChildTerm, child,
|
|---|
| 2371 | 0, 0, 0);
|
|---|
| 2372 | #endif
|
|---|
| 2373 |
|
|---|
| 2374 | if (!(status & 1))
|
|---|
| 2375 | {
|
|---|
| 2376 | printf (_("Error spawning, %d\n") ,status);
|
|---|
| 2377 | fflush (stdout);
|
|---|
| 2378 | switch (status)
|
|---|
| 2379 | {
|
|---|
| 2380 | case 0x1c:
|
|---|
| 2381 | errno = EPROCLIM;
|
|---|
| 2382 | break;
|
|---|
| 2383 | default:
|
|---|
| 2384 | errno = EFAIL;
|
|---|
| 2385 | }
|
|---|
| 2386 | }
|
|---|
| 2387 |
|
|---|
| 2388 | if (comname && !ISDB (DB_JOBS))
|
|---|
| 2389 | unlink (comname);
|
|---|
| 2390 |
|
|---|
| 2391 | return (status & 1);
|
|---|
| 2392 | }
|
|---|
| 2393 |
|
|---|
| 2394 | #else /* !VMS */
|
|---|
| 2395 |
|
|---|
| 2396 | /* EMX: Start a child process. This function returns the new pid. */
|
|---|
| 2397 | # if defined __MSDOS__ || defined __EMX__
|
|---|
| 2398 | /* The child argument can be NULL (that's why we return the pid), if it is
|
|---|
| 2399 | and the shell is a dllshell:// a child structure is created and inserted
|
|---|
| 2400 | into the child list so reap_children can do its job.
|
|---|
| 2401 |
|
|---|
| 2402 | BTW. the name of this function in this port is very misleading, spawn_job
|
|---|
| 2403 | would perhaps be more appropriate. */
|
|---|
| 2404 |
|
|---|
| 2405 | int
|
|---|
| 2406 | child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp,
|
|---|
| 2407 | struct child *child)
|
|---|
| 2408 | {
|
|---|
| 2409 | int pid;
|
|---|
| 2410 | /* stdin_fd == 0 means: nothing to do for stdin;
|
|---|
| 2411 | stdout_fd == 1 means: nothing to do for stdout */
|
|---|
| 2412 | int save_stdin = (stdin_fd != 0) ? dup (0) : 0;
|
|---|
| 2413 | int save_stdout = (stdout_fd != 1) ? dup (1): 1;
|
|---|
| 2414 |
|
|---|
| 2415 | /* < 0 only if dup() failed */
|
|---|
| 2416 | if (save_stdin < 0)
|
|---|
| 2417 | fatal (NILF, _("no more file handles: could not duplicate stdin\n"));
|
|---|
| 2418 | if (save_stdout < 0)
|
|---|
| 2419 | fatal (NILF, _("no more file handles: could not duplicate stdout\n"));
|
|---|
| 2420 |
|
|---|
| 2421 | /* Close unnecessary file handles for the child. */
|
|---|
| 2422 | if (save_stdin != 0)
|
|---|
| 2423 | CLOSE_ON_EXEC (save_stdin);
|
|---|
| 2424 | if (save_stdout != 1)
|
|---|
| 2425 | CLOSE_ON_EXEC (save_stdout);
|
|---|
| 2426 |
|
|---|
| 2427 | /* Connect the pipes to the child process. */
|
|---|
| 2428 | if (stdin_fd != 0)
|
|---|
| 2429 | (void) dup2 (stdin_fd, 0);
|
|---|
| 2430 | if (stdout_fd != 1)
|
|---|
| 2431 | (void) dup2 (stdout_fd, 1);
|
|---|
| 2432 |
|
|---|
| 2433 | /* stdin_fd and stdout_fd must be closed on exit because we are
|
|---|
| 2434 | still in the parent process */
|
|---|
| 2435 | if (stdin_fd != 0)
|
|---|
| 2436 | CLOSE_ON_EXEC (stdin_fd);
|
|---|
| 2437 | if (stdout_fd != 1)
|
|---|
| 2438 | CLOSE_ON_EXEC (stdout_fd);
|
|---|
| 2439 |
|
|---|
| 2440 | #ifdef MAKE_DLLSHELL
|
|---|
| 2441 | pid = spawn_command(argv, envp, child);
|
|---|
| 2442 | #else
|
|---|
| 2443 | /* Run the command. */
|
|---|
| 2444 | pid = exec_command (argv, envp);
|
|---|
| 2445 | #endif
|
|---|
| 2446 |
|
|---|
| 2447 | /* Restore stdout/stdin of the parent process. */
|
|---|
| 2448 | if (stdin_fd != 0 && dup2 (save_stdin, 0) != 0)
|
|---|
| 2449 | fatal (NILF, _("restoring of stdin failed\n"));
|
|---|
| 2450 | if (stdout_fd != 1 && dup2 (save_stdout, 1) != 1)
|
|---|
| 2451 | fatal (NILF, _("restoring of stdout failed\n"));
|
|---|
| 2452 |
|
|---|
| 2453 | /* Cleanup handles */
|
|---|
| 2454 | if (stdin_fd != 0)
|
|---|
| 2455 | close (save_stdin);
|
|---|
| 2456 | if (stdout_fd != 1)
|
|---|
| 2457 | close (save_stdout);
|
|---|
| 2458 |
|
|---|
| 2459 | return pid;
|
|---|
| 2460 | }
|
|---|
| 2461 |
|
|---|
| 2462 | #elif !defined (_AMIGA) && !defined (__MSDOS__)
|
|---|
| 2463 |
|
|---|
| 2464 | /* UNIX:
|
|---|
| 2465 | Replace the current process with one executing the command in ARGV.
|
|---|
| 2466 | STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
|
|---|
| 2467 | the environment of the new program. This function does not return. */
|
|---|
| 2468 | void
|
|---|
| 2469 | child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
|
|---|
| 2470 | {
|
|---|
| 2471 | if (stdin_fd != 0)
|
|---|
| 2472 | (void) dup2 (stdin_fd, 0);
|
|---|
| 2473 | if (stdout_fd != 1)
|
|---|
| 2474 | (void) dup2 (stdout_fd, 1);
|
|---|
| 2475 | if (stdin_fd != 0)
|
|---|
| 2476 | (void) close (stdin_fd);
|
|---|
| 2477 | if (stdout_fd != 1)
|
|---|
| 2478 | (void) close (stdout_fd);
|
|---|
| 2479 |
|
|---|
| 2480 | /* Run the command. */
|
|---|
| 2481 | exec_command (argv, envp);
|
|---|
| 2482 | }
|
|---|
| 2483 | #endif /* !AMIGA && !__MSDOS__ */
|
|---|
| 2484 | #endif /* !VMS */
|
|---|
| 2485 | #endif /* !WINDOWS32 */
|
|---|
| 2486 | |
|---|
| 2487 |
|
|---|
| 2488 | #ifdef MAKE_DLLSHELL
|
|---|
| 2489 | /* Globals for the currently loaded dllshell. */
|
|---|
| 2490 | char *dllshell_spec;
|
|---|
| 2491 | void *dllshell_dl;
|
|---|
| 2492 | void *dllshell_instance;
|
|---|
| 2493 | void *(*dllshell_init) PARAMS ((const char *spec));
|
|---|
| 2494 | pid_t (*dllshell_spawn) PARAMS ((void *instance, char **argv, char **envp, int *status, char *done));
|
|---|
| 2495 | pid_t (*dllshell_wait) PARAMS ((void *instance, int *status, int block));
|
|---|
| 2496 |
|
|---|
| 2497 | /* This is called when all pipes and such are configured for the
|
|---|
| 2498 | child process. The child argument may be null, see child_execute_job. */
|
|---|
| 2499 | static int spawn_command (char **argv, char **envp, struct child *c)
|
|---|
| 2500 | {
|
|---|
| 2501 | /* Now let's see if there is a DLLSHELL specifier in the
|
|---|
| 2502 | first argument. */
|
|---|
| 2503 | if (!strncmp(argv[0], "dllshell://", 11))
|
|---|
| 2504 | {
|
|---|
| 2505 | /* dllshell://<dllname>[!<realshell>[!whatever]] */
|
|---|
| 2506 | char *name, *name_end;
|
|---|
| 2507 | int insert_child = 0;
|
|---|
| 2508 |
|
|---|
| 2509 | /* parse it */
|
|---|
| 2510 | name = argv[0] + 11;
|
|---|
| 2511 | name_end = strchr (name, '!');
|
|---|
| 2512 | if (!name_end)
|
|---|
| 2513 | name_end = strchr (name, '\0');
|
|---|
| 2514 | if (name_end == name)
|
|---|
| 2515 | fatal (NILF, _("%s : malformed specifier!\n"), argv[0]);
|
|---|
| 2516 |
|
|---|
| 2517 | /* need loading? */
|
|---|
| 2518 | if (!dllshell_spec || strcmp (argv[0], dllshell_spec))
|
|---|
| 2519 | {
|
|---|
| 2520 | if (dllshell_spec)
|
|---|
| 2521 | fatal (NILF, _("cannot change the dllshell!!!\n"));
|
|---|
| 2522 |
|
|---|
| 2523 | dllshell_spec = strdup (argv[0]);
|
|---|
| 2524 | dllshell_spec[name_end - argv[0]] = '\0';
|
|---|
| 2525 | dllshell_dl = dlopen (dllshell_spec + (name - argv[0]), RTLD_LOCAL);
|
|---|
| 2526 | if (!dllshell_dl)
|
|---|
| 2527 | fatal (NILF, _("%s : failed to load! dlerror: '%s'\n"), argv[0], dlerror());
|
|---|
| 2528 | dllshell_spec[name_end - name] = '!';
|
|---|
| 2529 |
|
|---|
| 2530 | /* get symbols */
|
|---|
| 2531 | dllshell_init = dlsym (dllshell_dl, "dllshell_init");
|
|---|
| 2532 | if (!dllshell_init)
|
|---|
| 2533 | fatal (NILF, _("%s : failed to find symbols 'dllshell_init' dlerror: %s\n"), argv[0], dlerror());
|
|---|
| 2534 | dllshell_spawn = dlsym (dllshell_dl, "dllshell_spawn");
|
|---|
| 2535 | if (!dllshell_spawn)
|
|---|
| 2536 | fatal (NILF, _("%s : failed to find symbols 'dllshell_spawn' dlerror: %s\n"), argv[0], dlerror());
|
|---|
| 2537 | dllshell_wait = dlsym (dllshell_dl, "dllshell_wait");
|
|---|
| 2538 | if (!dllshell_wait)
|
|---|
| 2539 | fatal (NILF, _("%s : failed to find symbols 'dllshell_wait' dlerror: %s\n"), argv[0], dlerror());
|
|---|
| 2540 |
|
|---|
| 2541 | /* init */
|
|---|
| 2542 | dllshell_instance = dllshell_init(dllshell_spec);
|
|---|
| 2543 | if (!dllshell_instance)
|
|---|
| 2544 | fatal (NILF, _("%s : init failed!!!\n"), argv[0]);
|
|---|
| 2545 | }
|
|---|
| 2546 |
|
|---|
| 2547 | /* make child struct? */
|
|---|
| 2548 | if (!c)
|
|---|
| 2549 | {
|
|---|
| 2550 | c = (struct child *) xmalloc (sizeof (struct child));
|
|---|
| 2551 | bzero ((char *)c, sizeof (struct child));
|
|---|
| 2552 | insert_child = 1;
|
|---|
| 2553 | }
|
|---|
| 2554 |
|
|---|
| 2555 | /* call it. return value is 0 on succes, -1 on failure. */
|
|---|
| 2556 | c->pid = dllshell_spawn (dllshell_instance, argv, envp, &c->status, &c->dllshell_done);
|
|---|
| 2557 | DB (DB_JOBS, (_("dllshell pid=%x\n"), c->pid));
|
|---|
| 2558 |
|
|---|
| 2559 | if (insert_child && c->pid > 0)
|
|---|
| 2560 | {
|
|---|
| 2561 | c->next = children;
|
|---|
| 2562 | DB (DB_JOBS, (_("Putting child 0x%08lx (-) PID %ld on the chain.\n"),
|
|---|
| 2563 | (unsigned long int) c, (long) c->pid));
|
|---|
| 2564 | children = c;
|
|---|
| 2565 | /* One more job slot is in use. */
|
|---|
| 2566 | ++job_slots_used;
|
|---|
| 2567 | }
|
|---|
| 2568 | }
|
|---|
| 2569 | else
|
|---|
| 2570 | {
|
|---|
| 2571 | /* Run the command. */
|
|---|
| 2572 | #ifdef __EMX__
|
|---|
| 2573 | c->pid =
|
|---|
| 2574 | exec_command (argv, envp);
|
|---|
| 2575 | #else
|
|---|
| 2576 | # error MAKE_DLLSHELL is not ported to your platform yet.
|
|---|
| 2577 | #endif
|
|---|
| 2578 | DB (DB_JOBS, (_("spawn pid=%x\n"), c->pid));
|
|---|
| 2579 | }
|
|---|
| 2580 |
|
|---|
| 2581 | return c->pid;
|
|---|
| 2582 | }
|
|---|
| 2583 |
|
|---|
| 2584 | /* Waits or pools for a job to finish.
|
|---|
| 2585 | If the block argument the the function will not return
|
|---|
| 2586 | till a job is completed (if there are any jobs).
|
|---|
| 2587 | Returns pid of completed job.
|
|---|
| 2588 | Returns 0 if no jobs are finished.
|
|---|
| 2589 | Returns -1 if no jobs are running. */
|
|---|
| 2590 | pid_t wait_jobs (int *status, int block)
|
|---|
| 2591 | {
|
|---|
| 2592 | pid_t pid;
|
|---|
| 2593 | if (dllshell_wait)
|
|---|
| 2594 | pid = dllshell_wait(dllshell_instance, status, block);
|
|---|
| 2595 | else
|
|---|
| 2596 | {
|
|---|
| 2597 | if (block)
|
|---|
| 2598 | pid = WAIT_NOHANG(status);
|
|---|
| 2599 | else
|
|---|
| 2600 | pid = wait(status);
|
|---|
| 2601 | }
|
|---|
| 2602 | return pid;
|
|---|
| 2603 | }
|
|---|
| 2604 |
|
|---|
| 2605 | #endif /* MAKE_DLLSHELL */
|
|---|
| 2606 | |
|---|
| 2607 |
|
|---|
| 2608 | #ifndef _AMIGA
|
|---|
| 2609 | /* Replace the current process with one running the command in ARGV,
|
|---|
| 2610 | with environment ENVP. This function does not return. */
|
|---|
| 2611 |
|
|---|
| 2612 | /* EMX: This function returns the pid of the child process. */
|
|---|
| 2613 | # ifdef __EMX__
|
|---|
| 2614 | int
|
|---|
| 2615 | # else
|
|---|
| 2616 | void
|
|---|
| 2617 | # endif
|
|---|
| 2618 | exec_command (char **argv, char **envp)
|
|---|
| 2619 | {
|
|---|
| 2620 | #ifdef VMS
|
|---|
| 2621 | /* to work around a problem with signals and execve: ignore them */
|
|---|
| 2622 | #ifdef SIGCHLD
|
|---|
| 2623 | signal (SIGCHLD,SIG_IGN);
|
|---|
| 2624 | #endif
|
|---|
| 2625 | /* Run the program. */
|
|---|
| 2626 | execve (argv[0], argv, envp);
|
|---|
| 2627 | perror_with_name ("execve: ", argv[0]);
|
|---|
| 2628 | _exit (EXIT_FAILURE);
|
|---|
| 2629 | #else
|
|---|
| 2630 | #ifdef WINDOWS32
|
|---|
| 2631 | HANDLE hPID;
|
|---|
| 2632 | HANDLE hWaitPID;
|
|---|
| 2633 | int err = 0;
|
|---|
| 2634 | int exit_code = EXIT_FAILURE;
|
|---|
| 2635 |
|
|---|
| 2636 | /* make sure CreateProcess() has Path it needs */
|
|---|
| 2637 | sync_Path_environment();
|
|---|
| 2638 |
|
|---|
| 2639 | /* launch command */
|
|---|
| 2640 | hPID = process_easy(argv, envp);
|
|---|
| 2641 |
|
|---|
| 2642 | /* make sure launch ok */
|
|---|
| 2643 | if (hPID == INVALID_HANDLE_VALUE)
|
|---|
| 2644 | {
|
|---|
| 2645 | int i;
|
|---|
| 2646 | fprintf(stderr,
|
|---|
| 2647 | _("process_easy() failed failed to launch process (e=%d)\n"),
|
|---|
| 2648 | process_last_err(hPID));
|
|---|
| 2649 | for (i = 0; argv[i]; i++)
|
|---|
| 2650 | fprintf(stderr, "%s ", argv[i]);
|
|---|
| 2651 | fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
|
|---|
| 2652 | exit(EXIT_FAILURE);
|
|---|
| 2653 | }
|
|---|
| 2654 |
|
|---|
| 2655 | /* wait and reap last child */
|
|---|
| 2656 | while (hWaitPID = process_wait_for_any())
|
|---|
| 2657 | {
|
|---|
| 2658 | /* was an error found on this process? */
|
|---|
| 2659 | err = process_last_err(hWaitPID);
|
|---|
| 2660 |
|
|---|
| 2661 | /* get exit data */
|
|---|
| 2662 | exit_code = process_exit_code(hWaitPID);
|
|---|
| 2663 |
|
|---|
| 2664 | if (err)
|
|---|
| 2665 | fprintf(stderr, "make (e=%d, rc=%d): %s",
|
|---|
| 2666 | err, exit_code, map_windows32_error_to_string(err));
|
|---|
| 2667 |
|
|---|
| 2668 | /* cleanup process */
|
|---|
| 2669 | process_cleanup(hWaitPID);
|
|---|
| 2670 |
|
|---|
| 2671 | /* expect to find only last pid, warn about other pids reaped */
|
|---|
| 2672 | if (hWaitPID == hPID)
|
|---|
| 2673 | break;
|
|---|
| 2674 | else
|
|---|
| 2675 | fprintf(stderr,
|
|---|
| 2676 | _("make reaped child pid %d, still waiting for pid %d\n"),
|
|---|
| 2677 | hWaitPID, hPID);
|
|---|
| 2678 | }
|
|---|
| 2679 |
|
|---|
| 2680 | /* return child's exit code as our exit code */
|
|---|
| 2681 | exit(exit_code);
|
|---|
| 2682 |
|
|---|
| 2683 | #else /* !WINDOWS32 */
|
|---|
| 2684 |
|
|---|
| 2685 | # ifdef __EMX__
|
|---|
| 2686 | int pid;
|
|---|
| 2687 | # endif
|
|---|
| 2688 |
|
|---|
| 2689 | /* Be the user, permanently. */
|
|---|
| 2690 | child_access ();
|
|---|
| 2691 |
|
|---|
| 2692 | # ifdef __EMX__
|
|---|
| 2693 |
|
|---|
| 2694 | /* Run the program. */
|
|---|
| 2695 | pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
|
|---|
| 2696 |
|
|---|
| 2697 | if (pid >= 0)
|
|---|
| 2698 | return pid;
|
|---|
| 2699 |
|
|---|
| 2700 | /* the file might have a strange shell extension */
|
|---|
| 2701 | if (errno == ENOENT)
|
|---|
| 2702 | errno = ENOEXEC;
|
|---|
| 2703 |
|
|---|
| 2704 | # else
|
|---|
| 2705 |
|
|---|
| 2706 | /* Run the program. */
|
|---|
| 2707 | environ = envp;
|
|---|
| 2708 | execvp (argv[0], argv);
|
|---|
| 2709 |
|
|---|
| 2710 | # endif /* !__EMX__ */
|
|---|
| 2711 |
|
|---|
| 2712 | switch (errno)
|
|---|
| 2713 | {
|
|---|
| 2714 | case ENOENT:
|
|---|
| 2715 | error (NILF, _("%s: Command not found"), argv[0]);
|
|---|
| 2716 | break;
|
|---|
| 2717 | case ENOEXEC:
|
|---|
| 2718 | {
|
|---|
| 2719 | /* The file is not executable. Try it as a shell script. */
|
|---|
| 2720 | extern char *getenv ();
|
|---|
| 2721 | char *shell;
|
|---|
| 2722 | char **new_argv;
|
|---|
| 2723 | int argc;
|
|---|
| 2724 |
|
|---|
| 2725 | # ifdef __EMX__
|
|---|
| 2726 | /* Do not use $SHELL from the environment */
|
|---|
| 2727 | struct variable *p = lookup_variable ("SHELL", 5);
|
|---|
| 2728 | if (p)
|
|---|
| 2729 | shell = p->value;
|
|---|
| 2730 | else
|
|---|
| 2731 | shell = 0;
|
|---|
| 2732 | # else
|
|---|
| 2733 | shell = getenv ("SHELL");
|
|---|
| 2734 | # endif
|
|---|
| 2735 | if (shell == 0)
|
|---|
| 2736 | shell = default_shell;
|
|---|
| 2737 |
|
|---|
| 2738 | argc = 1;
|
|---|
| 2739 | while (argv[argc] != 0)
|
|---|
| 2740 | ++argc;
|
|---|
| 2741 |
|
|---|
| 2742 | new_argv = (char **) alloca ((1 + argc + 1) * sizeof (char *));
|
|---|
| 2743 | new_argv[0] = shell;
|
|---|
| 2744 | new_argv[1] = argv[0];
|
|---|
| 2745 | while (argc > 0)
|
|---|
| 2746 | {
|
|---|
| 2747 | new_argv[1 + argc] = argv[argc];
|
|---|
| 2748 | --argc;
|
|---|
| 2749 | }
|
|---|
| 2750 |
|
|---|
| 2751 | # ifdef __EMX__
|
|---|
| 2752 | pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
|
|---|
| 2753 | if (pid >= 0)
|
|---|
| 2754 | break;
|
|---|
| 2755 | # else
|
|---|
| 2756 | execvp (shell, new_argv);
|
|---|
| 2757 | # endif
|
|---|
| 2758 | if (errno == ENOENT)
|
|---|
| 2759 | error (NILF, _("%s: Shell program not found"), shell);
|
|---|
| 2760 | else
|
|---|
| 2761 | perror_with_name ("execvp: ", shell);
|
|---|
| 2762 | break;
|
|---|
| 2763 | }
|
|---|
| 2764 |
|
|---|
| 2765 | # ifdef __EMX__
|
|---|
| 2766 | case EINVAL:
|
|---|
| 2767 | /* this nasty error was driving me nuts :-( */
|
|---|
| 2768 | error (NILF, _("spawnvpe: environment space might be exhausted"));
|
|---|
| 2769 | /* FALLTHROUGH */
|
|---|
| 2770 | # endif
|
|---|
| 2771 |
|
|---|
| 2772 | default:
|
|---|
| 2773 | perror_with_name ("execvp: ", argv[0]);
|
|---|
| 2774 | break;
|
|---|
| 2775 | }
|
|---|
| 2776 |
|
|---|
| 2777 | # ifdef __EMX__
|
|---|
| 2778 | return pid;
|
|---|
| 2779 | # else
|
|---|
| 2780 | _exit (127);
|
|---|
| 2781 | # endif
|
|---|
| 2782 | #endif /* !WINDOWS32 */
|
|---|
| 2783 | #endif /* !VMS */
|
|---|
| 2784 | }
|
|---|
| 2785 | #else /* On Amiga */
|
|---|
| 2786 | void exec_command (char **argv)
|
|---|
| 2787 | {
|
|---|
| 2788 | MyExecute (argv);
|
|---|
| 2789 | }
|
|---|
| 2790 |
|
|---|
| 2791 | void clean_tmp (void)
|
|---|
| 2792 | {
|
|---|
| 2793 | DeleteFile (amiga_bname);
|
|---|
| 2794 | }
|
|---|
| 2795 |
|
|---|
| 2796 | #endif /* On Amiga */
|
|---|
| 2797 | |
|---|
| 2798 |
|
|---|
| 2799 | #ifndef VMS
|
|---|
| 2800 | /* Figure out the argument list necessary to run LINE as a command. Try to
|
|---|
| 2801 | avoid using a shell. This routine handles only ' quoting, and " quoting
|
|---|
| 2802 | when no backslash, $ or ` characters are seen in the quotes. Starting
|
|---|
| 2803 | quotes may be escaped with a backslash. If any of the characters in
|
|---|
| 2804 | sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
|
|---|
| 2805 | is the first word of a line, the shell is used.
|
|---|
| 2806 |
|
|---|
| 2807 | If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
|
|---|
| 2808 | If *RESTP is NULL, newlines will be ignored.
|
|---|
| 2809 |
|
|---|
| 2810 | SHELL is the shell to use, or nil to use the default shell.
|
|---|
| 2811 | IFS is the value of $IFS, or nil (meaning the default). */
|
|---|
| 2812 |
|
|---|
| 2813 | static char **
|
|---|
| 2814 | construct_command_argv_internal (char *line, char **restp, char *shell,
|
|---|
| 2815 | char *ifs, char **batch_filename_ptr)
|
|---|
| 2816 | {
|
|---|
| 2817 | #ifdef __MSDOS__
|
|---|
| 2818 | /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
|
|---|
| 2819 | We call `system' for anything that requires ``slow'' processing,
|
|---|
| 2820 | because DOS shells are too dumb. When $SHELL points to a real
|
|---|
| 2821 | (unix-style) shell, `system' just calls it to do everything. When
|
|---|
| 2822 | $SHELL points to a DOS shell, `system' does most of the work
|
|---|
| 2823 | internally, calling the shell only for its internal commands.
|
|---|
| 2824 | However, it looks on the $PATH first, so you can e.g. have an
|
|---|
| 2825 | external command named `mkdir'.
|
|---|
| 2826 |
|
|---|
| 2827 | Since we call `system', certain characters and commands below are
|
|---|
| 2828 | actually not specific to COMMAND.COM, but to the DJGPP implementation
|
|---|
| 2829 | of `system'. In particular:
|
|---|
| 2830 |
|
|---|
| 2831 | The shell wildcard characters are in DOS_CHARS because they will
|
|---|
| 2832 | not be expanded if we call the child via `spawnXX'.
|
|---|
| 2833 |
|
|---|
| 2834 | The `;' is in DOS_CHARS, because our `system' knows how to run
|
|---|
| 2835 | multiple commands on a single line.
|
|---|
| 2836 |
|
|---|
| 2837 | DOS_CHARS also include characters special to 4DOS/NDOS, so we
|
|---|
| 2838 | won't have to tell one from another and have one more set of
|
|---|
| 2839 | commands and special characters. */
|
|---|
| 2840 | static char sh_chars_dos[] = "*?[];|<>%^&()";
|
|---|
| 2841 | static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
|
|---|
| 2842 | "copy", "ctty", "date", "del", "dir", "echo",
|
|---|
| 2843 | "erase", "exit", "for", "goto", "if", "md",
|
|---|
| 2844 | "mkdir", "path", "pause", "prompt", "rd",
|
|---|
| 2845 | "rmdir", "rem", "ren", "rename", "set",
|
|---|
| 2846 | "shift", "time", "type", "ver", "verify",
|
|---|
| 2847 | "vol", ":", 0 };
|
|---|
| 2848 |
|
|---|
| 2849 | static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
|
|---|
| 2850 | static char *sh_cmds_sh[] = { "cd", "echo", "eval", "exec", "exit", "login",
|
|---|
| 2851 | "logout", "set", "umask", "wait", "while",
|
|---|
| 2852 | "for", "case", "if", ":", ".", "break",
|
|---|
| 2853 | "continue", "export", "read", "readonly",
|
|---|
| 2854 | "shift", "times", "trap", "switch", "unset",
|
|---|
| 2855 | 0 };
|
|---|
| 2856 |
|
|---|
| 2857 | char *sh_chars;
|
|---|
| 2858 | char **sh_cmds;
|
|---|
| 2859 | #elif defined (__EMX__)
|
|---|
| 2860 | static char sh_chars_dos[] = "*?[];|<>%^&()";
|
|---|
| 2861 | static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
|
|---|
| 2862 | "copy", "ctty", "date", "del", "dir", "echo",
|
|---|
| 2863 | "erase", "exit", "for", "goto", "if", "md",
|
|---|
| 2864 | "mkdir", "path", "pause", "prompt", "rd",
|
|---|
| 2865 | "rmdir", "rem", "ren", "rename", "set",
|
|---|
| 2866 | "shift", "time", "type", "ver", "verify",
|
|---|
| 2867 | "vol", ":", 0 };
|
|---|
| 2868 |
|
|---|
| 2869 | static char sh_chars_os2[] = "*?[];|<>%^()\"'&";
|
|---|
| 2870 | static char *sh_cmds_os2[] = { "call", "cd", "chcp", "chdir", "cls", "copy",
|
|---|
| 2871 | "date", "del", "detach", "dir", "echo",
|
|---|
| 2872 | "endlocal", "erase", "exit", "for", "goto", "if",
|
|---|
| 2873 | "keys", "md", "mkdir", "move", "path", "pause",
|
|---|
| 2874 | "prompt", "rd", "rem", "ren", "rename", "rmdir",
|
|---|
| 2875 | "set", "setlocal", "shift", "start", "time",
|
|---|
| 2876 | "type", "ver", "verify", "vol", ":", 0 };
|
|---|
| 2877 |
|
|---|
| 2878 | static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~'";
|
|---|
| 2879 | static char *sh_cmds_sh[] = { "echo", "cd", "eval", "exec", "exit", "login",
|
|---|
| 2880 | "logout", "set", "umask", "wait", "while",
|
|---|
| 2881 | "for", "case", "if", ":", ".", "break",
|
|---|
| 2882 | "continue", "export", "read", "readonly",
|
|---|
| 2883 | "shift", "times", "trap", "switch", "unset",
|
|---|
| 2884 | 0 };
|
|---|
| 2885 | char *sh_chars;
|
|---|
| 2886 | char **sh_cmds;
|
|---|
| 2887 |
|
|---|
| 2888 | #elif defined (_AMIGA)
|
|---|
| 2889 | static char sh_chars[] = "#;\"|<>()?*$`";
|
|---|
| 2890 | static char *sh_cmds[] = { "cd", "eval", "if", "delete", "echo", "copy",
|
|---|
| 2891 | "rename", "set", "setenv", "date", "makedir",
|
|---|
| 2892 | "skip", "else", "endif", "path", "prompt",
|
|---|
| 2893 | "unset", "unsetenv", "version",
|
|---|
| 2894 | 0 };
|
|---|
| 2895 | #elif defined (WINDOWS32)
|
|---|
| 2896 | static char sh_chars_dos[] = "\"|&<>";
|
|---|
| 2897 | static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
|
|---|
| 2898 | "copy", "ctty", "date", "del", "dir", "echo",
|
|---|
| 2899 | "erase", "exit", "for", "goto", "if", "if", "md",
|
|---|
| 2900 | "mkdir", "path", "pause", "prompt", "rd", "rem",
|
|---|
| 2901 | "ren", "rename", "rmdir", "set", "shift", "time",
|
|---|
| 2902 | "type", "ver", "verify", "vol", ":", 0 };
|
|---|
| 2903 | static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
|
|---|
| 2904 | static char *sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login",
|
|---|
| 2905 | "logout", "set", "umask", "wait", "while", "for",
|
|---|
| 2906 | "case", "if", ":", ".", "break", "continue",
|
|---|
| 2907 | "export", "read", "readonly", "shift", "times",
|
|---|
| 2908 | "trap", "switch", "test",
|
|---|
| 2909 | #ifdef BATCH_MODE_ONLY_SHELL
|
|---|
| 2910 | "echo",
|
|---|
| 2911 | #endif
|
|---|
| 2912 | 0 };
|
|---|
| 2913 | char* sh_chars;
|
|---|
| 2914 | char** sh_cmds;
|
|---|
| 2915 | #else /* must be UNIX-ish */
|
|---|
| 2916 | static char sh_chars[] = "#;\"*?[]&|<>(){}$`^~!";
|
|---|
| 2917 | static char *sh_cmds[] = { ".", ":", "break", "case", "cd", "continue",
|
|---|
| 2918 | "eval", "exec", "exit", "export", "for", "if",
|
|---|
| 2919 | "login", "logout", "read", "readonly", "set",
|
|---|
| 2920 | "shift", "switch", "test", "times", "trap",
|
|---|
| 2921 | "umask", "wait", "while", 0 };
|
|---|
| 2922 | #endif
|
|---|
| 2923 | register int i;
|
|---|
| 2924 | register char *p;
|
|---|
| 2925 | register char *ap;
|
|---|
| 2926 | char *end;
|
|---|
| 2927 | int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
|
|---|
| 2928 | char **new_argv = 0;
|
|---|
| 2929 | #ifdef WINDOWS32
|
|---|
| 2930 | int slow_flag = 0;
|
|---|
| 2931 |
|
|---|
| 2932 | if (no_default_sh_exe) {
|
|---|
| 2933 | sh_cmds = sh_cmds_dos;
|
|---|
| 2934 | sh_chars = sh_chars_dos;
|
|---|
| 2935 | } else {
|
|---|
| 2936 | sh_cmds = sh_cmds_sh;
|
|---|
| 2937 | sh_chars = sh_chars_sh;
|
|---|
| 2938 | }
|
|---|
| 2939 | #endif /* WINDOWS32 */
|
|---|
| 2940 |
|
|---|
| 2941 | if (restp != NULL)
|
|---|
| 2942 | *restp = NULL;
|
|---|
| 2943 |
|
|---|
| 2944 | /* Make sure not to bother processing an empty line. */
|
|---|
| 2945 | while (isblank ((unsigned char)*line))
|
|---|
| 2946 | ++line;
|
|---|
| 2947 | if (*line == '\0')
|
|---|
| 2948 | return 0;
|
|---|
| 2949 |
|
|---|
| 2950 | /* See if it is safe to parse commands internally. */
|
|---|
| 2951 | if (shell == 0)
|
|---|
| 2952 | shell = default_shell;
|
|---|
| 2953 | #ifdef WINDOWS32
|
|---|
| 2954 | else if (strcmp (shell, default_shell))
|
|---|
| 2955 | {
|
|---|
| 2956 | char *s1 = _fullpath(NULL, shell, 0);
|
|---|
| 2957 | char *s2 = _fullpath(NULL, default_shell, 0);
|
|---|
| 2958 |
|
|---|
| 2959 | slow_flag = strcmp((s1 ? s1 : ""), (s2 ? s2 : ""));
|
|---|
| 2960 |
|
|---|
| 2961 | if (s1)
|
|---|
| 2962 | free (s1);
|
|---|
| 2963 | if (s2)
|
|---|
| 2964 | free (s2);
|
|---|
| 2965 | }
|
|---|
| 2966 | if (slow_flag)
|
|---|
| 2967 | goto slow;
|
|---|
| 2968 | #else /* not WINDOWS32 */
|
|---|
| 2969 | #if defined (__MSDOS__) || defined (__EMX__)
|
|---|
| 2970 | else if (stricmp (shell, default_shell))
|
|---|
| 2971 | {
|
|---|
| 2972 | extern int _is_unixy_shell (const char *_path);
|
|---|
| 2973 |
|
|---|
| 2974 | DB (DB_BASIC, (_("$SHELL changed (was `%s', now `%s')\n"),
|
|---|
| 2975 | default_shell, shell));
|
|---|
| 2976 | unixy_shell = _is_unixy_shell (shell);
|
|---|
| 2977 | /* we must allocate a copy of shell: construct_command_argv() will free
|
|---|
| 2978 | * shell after this function returns. */
|
|---|
| 2979 | default_shell = xstrdup (shell);
|
|---|
| 2980 | }
|
|---|
| 2981 | if (unixy_shell)
|
|---|
| 2982 | {
|
|---|
| 2983 | sh_chars = sh_chars_sh;
|
|---|
| 2984 | sh_cmds = sh_cmds_sh;
|
|---|
| 2985 | }
|
|---|
| 2986 | else
|
|---|
| 2987 | {
|
|---|
| 2988 | sh_chars = sh_chars_dos;
|
|---|
| 2989 | sh_cmds = sh_cmds_dos;
|
|---|
| 2990 | # ifdef __EMX__
|
|---|
| 2991 | if (_osmode == OS2_MODE)
|
|---|
| 2992 | {
|
|---|
| 2993 | sh_chars = sh_chars_os2;
|
|---|
| 2994 | sh_cmds = sh_cmds_os2;
|
|---|
| 2995 | }
|
|---|
| 2996 | # endif
|
|---|
| 2997 | }
|
|---|
| 2998 | #else /* !__MSDOS__ */
|
|---|
| 2999 | else if (strcmp (shell, default_shell))
|
|---|
| 3000 | goto slow;
|
|---|
| 3001 | #endif /* !__MSDOS__ && !__EMX__ */
|
|---|
| 3002 | #endif /* not WINDOWS32 */
|
|---|
| 3003 |
|
|---|
| 3004 | if (ifs != 0)
|
|---|
| 3005 | for (ap = ifs; *ap != '\0'; ++ap)
|
|---|
| 3006 | if (*ap != ' ' && *ap != '\t' && *ap != '\n')
|
|---|
| 3007 | goto slow;
|
|---|
| 3008 |
|
|---|
| 3009 | i = strlen (line) + 1;
|
|---|
| 3010 |
|
|---|
| 3011 | /* More than 1 arg per character is impossible. */
|
|---|
| 3012 | new_argv = (char **) xmalloc (i * sizeof (char *));
|
|---|
| 3013 |
|
|---|
| 3014 | /* All the args can fit in a buffer as big as LINE is. */
|
|---|
| 3015 | ap = new_argv[0] = (char *) xmalloc (i);
|
|---|
| 3016 | end = ap + i;
|
|---|
| 3017 |
|
|---|
| 3018 | /* I is how many complete arguments have been found. */
|
|---|
| 3019 | i = 0;
|
|---|
| 3020 | instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
|
|---|
| 3021 | for (p = line; *p != '\0'; ++p)
|
|---|
| 3022 | {
|
|---|
| 3023 | if (ap > end)
|
|---|
| 3024 | abort ();
|
|---|
| 3025 |
|
|---|
| 3026 | if (instring)
|
|---|
| 3027 | {
|
|---|
| 3028 | string_char:
|
|---|
| 3029 | /* Inside a string, just copy any char except a closing quote
|
|---|
| 3030 | or a backslash-newline combination. */
|
|---|
| 3031 | if (*p == instring)
|
|---|
| 3032 | {
|
|---|
| 3033 | instring = 0;
|
|---|
| 3034 | if (ap == new_argv[0] || *(ap-1) == '\0')
|
|---|
| 3035 | last_argument_was_empty = 1;
|
|---|
| 3036 | }
|
|---|
| 3037 | else if (*p == '\\' && p[1] == '\n')
|
|---|
| 3038 | goto swallow_escaped_newline;
|
|---|
| 3039 | else if (*p == '\n' && restp != NULL)
|
|---|
| 3040 | {
|
|---|
| 3041 | /* End of the command line. */
|
|---|
| 3042 | *restp = p;
|
|---|
| 3043 | goto end_of_line;
|
|---|
| 3044 | }
|
|---|
| 3045 | /* Backslash, $, and ` are special inside double quotes.
|
|---|
| 3046 | If we see any of those, punt.
|
|---|
| 3047 | But on MSDOS, if we use COMMAND.COM, double and single
|
|---|
| 3048 | quotes have the same effect. */
|
|---|
| 3049 | else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
|
|---|
| 3050 | goto slow;
|
|---|
| 3051 | else
|
|---|
| 3052 | *ap++ = *p;
|
|---|
| 3053 | }
|
|---|
| 3054 | else if (strchr (sh_chars, *p) != 0)
|
|---|
| 3055 | /* Not inside a string, but it's a special char. */
|
|---|
| 3056 | goto slow;
|
|---|
| 3057 | #ifdef __MSDOS__
|
|---|
| 3058 | else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
|
|---|
| 3059 | /* `...' is a wildcard in DJGPP. */
|
|---|
| 3060 | goto slow;
|
|---|
| 3061 | #endif
|
|---|
| 3062 | else
|
|---|
| 3063 | /* Not a special char. */
|
|---|
| 3064 | switch (*p)
|
|---|
| 3065 | {
|
|---|
| 3066 | case '=':
|
|---|
| 3067 | /* Equals is a special character in leading words before the
|
|---|
| 3068 | first word with no equals sign in it. This is not the case
|
|---|
| 3069 | with sh -k, but we never get here when using nonstandard
|
|---|
| 3070 | shell flags. */
|
|---|
| 3071 | if (! seen_nonequals && unixy_shell)
|
|---|
| 3072 | goto slow;
|
|---|
| 3073 | word_has_equals = 1;
|
|---|
| 3074 | *ap++ = '=';
|
|---|
| 3075 | break;
|
|---|
| 3076 |
|
|---|
| 3077 | case '\\':
|
|---|
| 3078 | /* Backslash-newline combinations are eaten. */
|
|---|
| 3079 | if (p[1] == '\n')
|
|---|
| 3080 | {
|
|---|
| 3081 | swallow_escaped_newline:
|
|---|
| 3082 |
|
|---|
| 3083 | /* Eat the backslash, the newline, and following whitespace,
|
|---|
| 3084 | replacing it all with a single space. */
|
|---|
| 3085 | p += 2;
|
|---|
| 3086 |
|
|---|
| 3087 | /* If there is a tab after a backslash-newline,
|
|---|
| 3088 | remove it from the source line which will be echoed,
|
|---|
| 3089 | since it was most likely used to line
|
|---|
| 3090 | up the continued line with the previous one. */
|
|---|
| 3091 | if (*p == '\t')
|
|---|
| 3092 | /* Note these overlap and strcpy() is undefined for
|
|---|
| 3093 | overlapping objects in ANSI C. The strlen() _IS_ right,
|
|---|
| 3094 | since we need to copy the nul byte too. */
|
|---|
| 3095 | bcopy (p + 1, p, strlen (p));
|
|---|
| 3096 |
|
|---|
| 3097 | if (instring)
|
|---|
| 3098 | goto string_char;
|
|---|
| 3099 | else
|
|---|
| 3100 | {
|
|---|
| 3101 | if (ap != new_argv[i])
|
|---|
| 3102 | /* Treat this as a space, ending the arg.
|
|---|
| 3103 | But if it's at the beginning of the arg, it should
|
|---|
| 3104 | just get eaten, rather than becoming an empty arg. */
|
|---|
| 3105 | goto end_of_arg;
|
|---|
| 3106 | else
|
|---|
| 3107 | p = next_token (p) - 1;
|
|---|
| 3108 | }
|
|---|
| 3109 | }
|
|---|
| 3110 | else if (p[1] != '\0')
|
|---|
| 3111 | {
|
|---|
| 3112 | #ifdef HAVE_DOS_PATHS
|
|---|
| 3113 | /* Only remove backslashes before characters special
|
|---|
| 3114 | to Unixy shells. All other backslashes are copied
|
|---|
| 3115 | verbatim, since they are probably DOS-style
|
|---|
| 3116 | directory separators. This still leaves a small
|
|---|
| 3117 | window for problems, but at least it should work
|
|---|
| 3118 | for the vast majority of naive users. */
|
|---|
| 3119 |
|
|---|
| 3120 | #ifdef __MSDOS__
|
|---|
| 3121 | /* A dot is only special as part of the "..."
|
|---|
| 3122 | wildcard. */
|
|---|
| 3123 | if (strneq (p + 1, ".\\.\\.", 5))
|
|---|
| 3124 | {
|
|---|
| 3125 | *ap++ = '.';
|
|---|
| 3126 | *ap++ = '.';
|
|---|
| 3127 | p += 4;
|
|---|
| 3128 | }
|
|---|
| 3129 | else
|
|---|
| 3130 | #endif
|
|---|
| 3131 | if (p[1] != '\\' && p[1] != '\''
|
|---|
| 3132 | && !isspace ((unsigned char)p[1])
|
|---|
| 3133 | && (strchr (sh_chars_sh, p[1]) == 0))
|
|---|
| 3134 | /* back up one notch, to copy the backslash */
|
|---|
| 3135 | --p;
|
|---|
| 3136 | #endif /* HAVE_DOS_PATHS */
|
|---|
| 3137 |
|
|---|
| 3138 | /* Copy and skip the following char. */
|
|---|
| 3139 | *ap++ = *++p;
|
|---|
| 3140 | }
|
|---|
| 3141 | break;
|
|---|
| 3142 |
|
|---|
| 3143 | case '\'':
|
|---|
| 3144 | case '"':
|
|---|
| 3145 | instring = *p;
|
|---|
| 3146 | break;
|
|---|
| 3147 |
|
|---|
| 3148 | case '\n':
|
|---|
| 3149 | if (restp != NULL)
|
|---|
| 3150 | {
|
|---|
| 3151 | /* End of the command line. */
|
|---|
| 3152 | *restp = p;
|
|---|
| 3153 | goto end_of_line;
|
|---|
| 3154 | }
|
|---|
| 3155 | else
|
|---|
| 3156 | /* Newlines are not special. */
|
|---|
| 3157 | *ap++ = '\n';
|
|---|
| 3158 | break;
|
|---|
| 3159 |
|
|---|
| 3160 | case ' ':
|
|---|
| 3161 | case '\t':
|
|---|
| 3162 | end_of_arg:
|
|---|
| 3163 | /* We have the end of an argument.
|
|---|
| 3164 | Terminate the text of the argument. */
|
|---|
| 3165 | *ap++ = '\0';
|
|---|
| 3166 | new_argv[++i] = ap;
|
|---|
| 3167 | last_argument_was_empty = 0;
|
|---|
| 3168 |
|
|---|
| 3169 | /* Update SEEN_NONEQUALS, which tells us if every word
|
|---|
| 3170 | heretofore has contained an `='. */
|
|---|
| 3171 | seen_nonequals |= ! word_has_equals;
|
|---|
| 3172 | if (word_has_equals && ! seen_nonequals)
|
|---|
| 3173 | /* An `=' in a word before the first
|
|---|
| 3174 | word without one is magical. */
|
|---|
| 3175 | goto slow;
|
|---|
| 3176 | word_has_equals = 0; /* Prepare for the next word. */
|
|---|
| 3177 |
|
|---|
| 3178 | /* If this argument is the command name,
|
|---|
| 3179 | see if it is a built-in shell command.
|
|---|
| 3180 | If so, have the shell handle it. */
|
|---|
| 3181 | if (i == 1)
|
|---|
| 3182 | {
|
|---|
| 3183 | register int j;
|
|---|
| 3184 | for (j = 0; sh_cmds[j] != 0; ++j)
|
|---|
| 3185 | if (streq (sh_cmds[j], new_argv[0]))
|
|---|
| 3186 | goto slow;
|
|---|
| 3187 | }
|
|---|
| 3188 |
|
|---|
| 3189 | /* Ignore multiple whitespace chars. */
|
|---|
| 3190 | p = next_token (p);
|
|---|
| 3191 | /* Next iteration should examine the first nonwhite char. */
|
|---|
| 3192 | --p;
|
|---|
| 3193 | break;
|
|---|
| 3194 |
|
|---|
| 3195 | default:
|
|---|
| 3196 | *ap++ = *p;
|
|---|
| 3197 | break;
|
|---|
| 3198 | }
|
|---|
| 3199 | }
|
|---|
| 3200 | end_of_line:
|
|---|
| 3201 |
|
|---|
| 3202 | if (instring)
|
|---|
| 3203 | /* Let the shell deal with an unterminated quote. */
|
|---|
| 3204 | goto slow;
|
|---|
| 3205 |
|
|---|
| 3206 | /* Terminate the last argument and the argument list. */
|
|---|
| 3207 |
|
|---|
| 3208 | *ap = '\0';
|
|---|
| 3209 | if (new_argv[i][0] != '\0' || last_argument_was_empty)
|
|---|
| 3210 | ++i;
|
|---|
| 3211 | new_argv[i] = 0;
|
|---|
| 3212 |
|
|---|
| 3213 | if (i == 1)
|
|---|
| 3214 | {
|
|---|
| 3215 | register int j;
|
|---|
| 3216 | for (j = 0; sh_cmds[j] != 0; ++j)
|
|---|
| 3217 | if (streq (sh_cmds[j], new_argv[0]))
|
|---|
| 3218 | goto slow;
|
|---|
| 3219 | }
|
|---|
| 3220 |
|
|---|
| 3221 | if (new_argv[0] == 0)
|
|---|
| 3222 | /* Line was empty. */
|
|---|
| 3223 | return 0;
|
|---|
| 3224 | else
|
|---|
| 3225 | return new_argv;
|
|---|
| 3226 |
|
|---|
| 3227 | slow:;
|
|---|
| 3228 | /* We must use the shell. */
|
|---|
| 3229 |
|
|---|
| 3230 | if (new_argv != 0)
|
|---|
| 3231 | {
|
|---|
| 3232 | /* Free the old argument list we were working on. */
|
|---|
| 3233 | free (new_argv[0]);
|
|---|
| 3234 | free ((void *)new_argv);
|
|---|
| 3235 | }
|
|---|
| 3236 |
|
|---|
| 3237 | #ifdef __MSDOS__
|
|---|
| 3238 | execute_by_shell = 1; /* actually, call `system' if shell isn't unixy */
|
|---|
| 3239 | #endif
|
|---|
| 3240 |
|
|---|
| 3241 | #ifdef _AMIGA
|
|---|
| 3242 | {
|
|---|
| 3243 | char *ptr;
|
|---|
| 3244 | char *buffer;
|
|---|
| 3245 | char *dptr;
|
|---|
| 3246 |
|
|---|
| 3247 | buffer = (char *)xmalloc (strlen (line)+1);
|
|---|
| 3248 |
|
|---|
| 3249 | ptr = line;
|
|---|
| 3250 | for (dptr=buffer; *ptr; )
|
|---|
| 3251 | {
|
|---|
| 3252 | if (*ptr == '\\' && ptr[1] == '\n')
|
|---|
| 3253 | ptr += 2;
|
|---|
| 3254 | else if (*ptr == '@') /* Kludge: multiline commands */
|
|---|
| 3255 | {
|
|---|
| 3256 | ptr += 2;
|
|---|
| 3257 | *dptr++ = '\n';
|
|---|
| 3258 | }
|
|---|
| 3259 | else
|
|---|
| 3260 | *dptr++ = *ptr++;
|
|---|
| 3261 | }
|
|---|
| 3262 | *dptr = 0;
|
|---|
| 3263 |
|
|---|
| 3264 | new_argv = (char **) xmalloc (2 * sizeof (char *));
|
|---|
| 3265 | new_argv[0] = buffer;
|
|---|
| 3266 | new_argv[1] = 0;
|
|---|
| 3267 | }
|
|---|
| 3268 | #else /* Not Amiga */
|
|---|
| 3269 | #ifdef WINDOWS32
|
|---|
| 3270 | /*
|
|---|
| 3271 | * Not eating this whitespace caused things like
|
|---|
| 3272 | *
|
|---|
| 3273 | * sh -c "\n"
|
|---|
| 3274 | *
|
|---|
| 3275 | * which gave the shell fits. I think we have to eat
|
|---|
| 3276 | * whitespace here, but this code should be considered
|
|---|
| 3277 | * suspicious if things start failing....
|
|---|
| 3278 | */
|
|---|
| 3279 |
|
|---|
| 3280 | /* Make sure not to bother processing an empty line. */
|
|---|
| 3281 | while (isspace ((unsigned char)*line))
|
|---|
| 3282 | ++line;
|
|---|
| 3283 | if (*line == '\0')
|
|---|
| 3284 | return 0;
|
|---|
| 3285 | #endif /* WINDOWS32 */
|
|---|
| 3286 | {
|
|---|
| 3287 | /* SHELL may be a multi-word command. Construct a command line
|
|---|
| 3288 | "SHELL -c LINE", with all special chars in LINE escaped.
|
|---|
| 3289 | Then recurse, expanding this command line to get the final
|
|---|
| 3290 | argument list. */
|
|---|
| 3291 |
|
|---|
| 3292 | unsigned int shell_len = strlen (shell);
|
|---|
| 3293 | #ifndef VMS
|
|---|
| 3294 | static char minus_c[] = " -c ";
|
|---|
| 3295 | #else
|
|---|
| 3296 | static char minus_c[] = "";
|
|---|
| 3297 | #endif
|
|---|
| 3298 | unsigned int line_len = strlen (line);
|
|---|
| 3299 |
|
|---|
| 3300 | char *new_line = (char *) alloca (shell_len + (sizeof (minus_c) - 1)
|
|---|
| 3301 | + (line_len * 2) + 1);
|
|---|
| 3302 | char *command_ptr = NULL; /* used for batch_mode_shell mode */
|
|---|
| 3303 |
|
|---|
| 3304 | # ifdef __EMX__ /* is this necessary? */
|
|---|
| 3305 | if (!unixy_shell)
|
|---|
| 3306 | minus_c[1] = '/'; /* " /c " */
|
|---|
| 3307 | # endif
|
|---|
| 3308 |
|
|---|
| 3309 | ap = new_line;
|
|---|
| 3310 | bcopy (shell, ap, shell_len);
|
|---|
| 3311 | ap += shell_len;
|
|---|
| 3312 | bcopy (minus_c, ap, sizeof (minus_c) - 1);
|
|---|
| 3313 | ap += sizeof (minus_c) - 1;
|
|---|
| 3314 | command_ptr = ap;
|
|---|
| 3315 | for (p = line; *p != '\0'; ++p)
|
|---|
| 3316 | {
|
|---|
| 3317 | if (restp != NULL && *p == '\n')
|
|---|
| 3318 | {
|
|---|
| 3319 | *restp = p;
|
|---|
| 3320 | break;
|
|---|
| 3321 | }
|
|---|
| 3322 | else if (*p == '\\' && p[1] == '\n')
|
|---|
| 3323 | {
|
|---|
| 3324 | /* Eat the backslash, the newline, and following whitespace,
|
|---|
| 3325 | replacing it all with a single space (which is escaped
|
|---|
| 3326 | from the shell). */
|
|---|
| 3327 | p += 2;
|
|---|
| 3328 |
|
|---|
| 3329 | /* If there is a tab after a backslash-newline,
|
|---|
| 3330 | remove it from the source line which will be echoed,
|
|---|
| 3331 | since it was most likely used to line
|
|---|
| 3332 | up the continued line with the previous one. */
|
|---|
| 3333 | if (*p == '\t')
|
|---|
| 3334 | bcopy (p + 1, p, strlen (p));
|
|---|
| 3335 |
|
|---|
| 3336 | p = next_token (p);
|
|---|
| 3337 | --p;
|
|---|
| 3338 | if (unixy_shell && !batch_mode_shell)
|
|---|
| 3339 | *ap++ = '\\';
|
|---|
| 3340 | *ap++ = ' ';
|
|---|
| 3341 | continue;
|
|---|
| 3342 | }
|
|---|
| 3343 |
|
|---|
| 3344 | /* DOS shells don't know about backslash-escaping. */
|
|---|
| 3345 | if (unixy_shell && !batch_mode_shell &&
|
|---|
| 3346 | (*p == '\\' || *p == '\'' || *p == '"'
|
|---|
| 3347 | || isspace ((unsigned char)*p)
|
|---|
| 3348 | || strchr (sh_chars, *p) != 0))
|
|---|
| 3349 | *ap++ = '\\';
|
|---|
| 3350 | #ifdef __MSDOS__
|
|---|
| 3351 | else if (unixy_shell && strneq (p, "...", 3))
|
|---|
| 3352 | {
|
|---|
| 3353 | /* The case of `...' wildcard again. */
|
|---|
| 3354 | strcpy (ap, "\\.\\.\\");
|
|---|
| 3355 | ap += 5;
|
|---|
| 3356 | p += 2;
|
|---|
| 3357 | }
|
|---|
| 3358 | #endif
|
|---|
| 3359 | *ap++ = *p;
|
|---|
| 3360 | }
|
|---|
| 3361 | if (ap == new_line + shell_len + sizeof (minus_c) - 1)
|
|---|
| 3362 | /* Line was empty. */
|
|---|
| 3363 | return 0;
|
|---|
| 3364 | *ap = '\0';
|
|---|
| 3365 |
|
|---|
| 3366 | #ifdef WINDOWS32
|
|---|
| 3367 | /* Some shells do not work well when invoked as 'sh -c xxx' to run a
|
|---|
| 3368 | command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
|
|---|
| 3369 | cases, run commands via a script file. */
|
|---|
| 3370 | if ((no_default_sh_exe || batch_mode_shell) && batch_filename_ptr) {
|
|---|
| 3371 | FILE* batch = NULL;
|
|---|
| 3372 | int id = GetCurrentProcessId();
|
|---|
| 3373 | PATH_VAR(fbuf);
|
|---|
| 3374 | char* fname = NULL;
|
|---|
| 3375 |
|
|---|
| 3376 | /* create a file name */
|
|---|
| 3377 | sprintf(fbuf, "make%d", id);
|
|---|
| 3378 | fname = tempnam(".", fbuf);
|
|---|
| 3379 |
|
|---|
| 3380 | /* create batch file name */
|
|---|
| 3381 | *batch_filename_ptr = xmalloc(strlen(fname) + 5);
|
|---|
| 3382 | strcpy(*batch_filename_ptr, fname);
|
|---|
| 3383 |
|
|---|
| 3384 | /* make sure path name is in DOS backslash format */
|
|---|
| 3385 | if (!unixy_shell) {
|
|---|
| 3386 | fname = *batch_filename_ptr;
|
|---|
| 3387 | for (i = 0; fname[i] != '\0'; ++i)
|
|---|
| 3388 | if (fname[i] == '/')
|
|---|
| 3389 | fname[i] = '\\';
|
|---|
| 3390 | strcat(*batch_filename_ptr, ".bat");
|
|---|
| 3391 | } else {
|
|---|
| 3392 | strcat(*batch_filename_ptr, ".sh");
|
|---|
| 3393 | }
|
|---|
| 3394 |
|
|---|
| 3395 | DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
|
|---|
| 3396 | *batch_filename_ptr));
|
|---|
| 3397 |
|
|---|
| 3398 | /* create batch file to execute command */
|
|---|
| 3399 | batch = fopen (*batch_filename_ptr, "w");
|
|---|
| 3400 | if (!unixy_shell)
|
|---|
| 3401 | fputs ("@echo off\n", batch);
|
|---|
| 3402 | fputs (command_ptr, batch);
|
|---|
| 3403 | fputc ('\n', batch);
|
|---|
| 3404 | fclose (batch);
|
|---|
| 3405 |
|
|---|
| 3406 | /* create argv */
|
|---|
| 3407 | new_argv = (char **) xmalloc(3 * sizeof (char *));
|
|---|
| 3408 | if (unixy_shell) {
|
|---|
| 3409 | new_argv[0] = xstrdup (shell);
|
|---|
| 3410 | new_argv[1] = *batch_filename_ptr; /* only argv[0] gets freed later */
|
|---|
| 3411 | } else {
|
|---|
| 3412 | new_argv[0] = xstrdup (*batch_filename_ptr);
|
|---|
| 3413 | new_argv[1] = NULL;
|
|---|
| 3414 | }
|
|---|
| 3415 | new_argv[2] = NULL;
|
|---|
| 3416 | } else
|
|---|
| 3417 | #endif /* WINDOWS32 */
|
|---|
| 3418 | if (unixy_shell)
|
|---|
| 3419 | new_argv = construct_command_argv_internal (new_line, (char **) NULL,
|
|---|
| 3420 | (char *) 0, (char *) 0,
|
|---|
| 3421 | (char **) 0);
|
|---|
| 3422 | # ifdef __EMX__
|
|---|
| 3423 | else if (!unixy_shell)
|
|---|
| 3424 | {
|
|---|
| 3425 | /* new_line is local, must not be freed therefore */
|
|---|
| 3426 | char *p, *q;
|
|---|
| 3427 | int quote;
|
|---|
| 3428 | size_t index;
|
|---|
| 3429 | size_t len;
|
|---|
| 3430 |
|
|---|
| 3431 | /* handle quotes
|
|---|
| 3432 | We have to remove all double quotes and to split the line
|
|---|
| 3433 | into distinct arguments because of the strange handling
|
|---|
| 3434 | of builtin commands by cmd: 'echo "bla"' prints "bla"
|
|---|
| 3435 | (with quotes) while 'c:\bin\echo.exe "bla"' prints bla
|
|---|
| 3436 | (without quotes). Some programs like autoconf rely
|
|---|
| 3437 | on the second behaviour. */
|
|---|
| 3438 |
|
|---|
| 3439 | len = strlen (new_line) + 1;
|
|---|
| 3440 |
|
|---|
| 3441 | /* More than 1 arg per character is impossible. */
|
|---|
| 3442 | new_argv = (char **) xmalloc (len * sizeof (char *));
|
|---|
| 3443 |
|
|---|
| 3444 | /* All the args can fit in a buffer as big as new_line is. */
|
|---|
| 3445 | new_argv[0] = (char *) xmalloc (len);
|
|---|
| 3446 |
|
|---|
| 3447 | index = 0;
|
|---|
| 3448 | quote = 0;
|
|---|
| 3449 | q = new_line;
|
|---|
| 3450 | p = new_argv[index];
|
|---|
| 3451 | while(*q != '\0')
|
|---|
| 3452 | {
|
|---|
| 3453 | /* searching for closing quote */
|
|---|
| 3454 | if (quote)
|
|---|
| 3455 | {
|
|---|
| 3456 | if (*q == quote)
|
|---|
| 3457 | {
|
|---|
| 3458 | /* remove the quote */
|
|---|
| 3459 | q++;
|
|---|
| 3460 | quote = 0;
|
|---|
| 3461 | }
|
|---|
| 3462 | else /* normal character: copy it */
|
|---|
| 3463 | *p++ = *q++;
|
|---|
| 3464 | }
|
|---|
| 3465 |
|
|---|
| 3466 | /* searching for opening quote */
|
|---|
| 3467 | else if (*q == '\"'
|
|---|
| 3468 | # ifndef NO_CMD_DEFAULT
|
|---|
| 3469 | || *q == '\''
|
|---|
| 3470 | # endif
|
|---|
| 3471 | )
|
|---|
| 3472 | {
|
|---|
| 3473 | /* remove opening quote */
|
|---|
| 3474 | quote = *q;
|
|---|
| 3475 | q++;
|
|---|
| 3476 | }
|
|---|
| 3477 |
|
|---|
| 3478 | /* spaces outside of a quoted string: remove them
|
|---|
| 3479 | and start a new argument */
|
|---|
| 3480 | else if (*q == ' ' || *q == '\t')
|
|---|
| 3481 | {
|
|---|
| 3482 | *p++ = '\0'; /* trailing '\0' for last argument */
|
|---|
| 3483 |
|
|---|
| 3484 | /* remove all successive spaces */
|
|---|
| 3485 | do
|
|---|
| 3486 | {
|
|---|
| 3487 | q++;
|
|---|
| 3488 | }
|
|---|
| 3489 | while(*q == ' ' || *q == '\t');
|
|---|
| 3490 |
|
|---|
| 3491 | /* start new argument */
|
|---|
| 3492 | index++;
|
|---|
| 3493 | new_argv[index] = p;
|
|---|
| 3494 | }
|
|---|
| 3495 |
|
|---|
| 3496 | /* normal character (no space) outside a quoted string*/
|
|---|
| 3497 | else
|
|---|
| 3498 | *p++ = *q++;
|
|---|
| 3499 | } /* end while() */
|
|---|
| 3500 |
|
|---|
| 3501 | *p = '\0'; /* trailing '\0' for the last argument */
|
|---|
| 3502 | new_argv[index + 1] = NULL;
|
|---|
| 3503 |
|
|---|
| 3504 | # ifndef NO_CMD_DEFAULT
|
|---|
| 3505 | /* special case: echo x="y"
|
|---|
| 3506 | (e.g. autoconf uses this to determine whether make works)
|
|---|
| 3507 | this is pure idioty but cmd works this way:
|
|---|
| 3508 | if 'echo' and 'x="y"' are two different arguments cmd
|
|---|
| 3509 | will print '"x="y""' but if they are only one argument
|
|---|
| 3510 | cmd will print 'bla="blurb"' as it should be
|
|---|
| 3511 | note: if we do not allow cmd to be the default shell
|
|---|
| 3512 | we do not need this kind of voodoo */
|
|---|
| 3513 | if (index == 3 && strcasecmp(new_argv[2], "echo") == 0)
|
|---|
| 3514 | {
|
|---|
| 3515 | new_argv[2][4] = ' ';
|
|---|
| 3516 | new_argv[3] = NULL;
|
|---|
| 3517 | }
|
|---|
| 3518 | # endif
|
|---|
| 3519 | }
|
|---|
| 3520 | #elif defined(__MSDOS__)
|
|---|
| 3521 | else
|
|---|
| 3522 | {
|
|---|
| 3523 | /* With MSDOS shells, we must construct the command line here
|
|---|
| 3524 | instead of recursively calling ourselves, because we
|
|---|
| 3525 | cannot backslash-escape the special characters (see above). */
|
|---|
| 3526 | new_argv = (char **) xmalloc (sizeof (char *));
|
|---|
| 3527 | line_len = strlen (new_line) - shell_len - sizeof (minus_c) + 1;
|
|---|
| 3528 | new_argv[0] = xmalloc (line_len + 1);
|
|---|
| 3529 | strncpy (new_argv[0],
|
|---|
| 3530 | new_line + shell_len + sizeof (minus_c) - 1, line_len);
|
|---|
| 3531 | new_argv[0][line_len] = '\0';
|
|---|
| 3532 | }
|
|---|
| 3533 | #else
|
|---|
| 3534 | else
|
|---|
| 3535 | fatal (NILF, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
|
|---|
| 3536 | __FILE__, __LINE__);
|
|---|
| 3537 | #endif
|
|---|
| 3538 | }
|
|---|
| 3539 | #endif /* ! AMIGA */
|
|---|
| 3540 |
|
|---|
| 3541 | return new_argv;
|
|---|
| 3542 | }
|
|---|
| 3543 | #endif /* !VMS */
|
|---|
| 3544 |
|
|---|
| 3545 | /* Figure out the argument list necessary to run LINE as a command. Try to
|
|---|
| 3546 | avoid using a shell. This routine handles only ' quoting, and " quoting
|
|---|
| 3547 | when no backslash, $ or ` characters are seen in the quotes. Starting
|
|---|
| 3548 | quotes may be escaped with a backslash. If any of the characters in
|
|---|
| 3549 | sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
|
|---|
| 3550 | is the first word of a line, the shell is used.
|
|---|
| 3551 |
|
|---|
| 3552 | If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
|
|---|
| 3553 | If *RESTP is NULL, newlines will be ignored.
|
|---|
| 3554 |
|
|---|
| 3555 | FILE is the target whose commands these are. It is used for
|
|---|
| 3556 | variable expansion for $(SHELL) and $(IFS). */
|
|---|
| 3557 |
|
|---|
| 3558 | char **
|
|---|
| 3559 | construct_command_argv (char *line, char **restp, struct file *file,
|
|---|
| 3560 | char **batch_filename_ptr)
|
|---|
| 3561 | {
|
|---|
| 3562 | char *shell, *ifs;
|
|---|
| 3563 | char **argv;
|
|---|
| 3564 |
|
|---|
| 3565 | #ifdef VMS
|
|---|
| 3566 | char *cptr;
|
|---|
| 3567 | int argc;
|
|---|
| 3568 |
|
|---|
| 3569 | argc = 0;
|
|---|
| 3570 | cptr = line;
|
|---|
| 3571 | for (;;)
|
|---|
| 3572 | {
|
|---|
| 3573 | while ((*cptr != 0)
|
|---|
| 3574 | && (isspace ((unsigned char)*cptr)))
|
|---|
| 3575 | cptr++;
|
|---|
| 3576 | if (*cptr == 0)
|
|---|
| 3577 | break;
|
|---|
| 3578 | while ((*cptr != 0)
|
|---|
| 3579 | && (!isspace((unsigned char)*cptr)))
|
|---|
| 3580 | cptr++;
|
|---|
| 3581 | argc++;
|
|---|
| 3582 | }
|
|---|
| 3583 |
|
|---|
| 3584 | argv = (char **)malloc (argc * sizeof (char *));
|
|---|
| 3585 | if (argv == 0)
|
|---|
| 3586 | abort ();
|
|---|
| 3587 |
|
|---|
| 3588 | cptr = line;
|
|---|
| 3589 | argc = 0;
|
|---|
| 3590 | for (;;)
|
|---|
| 3591 | {
|
|---|
| 3592 | while ((*cptr != 0)
|
|---|
| 3593 | && (isspace ((unsigned char)*cptr)))
|
|---|
| 3594 | cptr++;
|
|---|
| 3595 | if (*cptr == 0)
|
|---|
| 3596 | break;
|
|---|
| 3597 | DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
|
|---|
| 3598 | argv[argc++] = cptr;
|
|---|
| 3599 | while ((*cptr != 0)
|
|---|
| 3600 | && (!isspace((unsigned char)*cptr)))
|
|---|
| 3601 | cptr++;
|
|---|
| 3602 | if (*cptr != 0)
|
|---|
| 3603 | *cptr++ = 0;
|
|---|
| 3604 | }
|
|---|
| 3605 | #else
|
|---|
| 3606 | {
|
|---|
| 3607 | /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
|
|---|
| 3608 | int save = warn_undefined_variables_flag;
|
|---|
| 3609 | warn_undefined_variables_flag = 0;
|
|---|
| 3610 |
|
|---|
| 3611 | shell = allocated_variable_expand_for_file ("$(SHELL)", file);
|
|---|
| 3612 | #ifdef WINDOWS32
|
|---|
| 3613 | /*
|
|---|
| 3614 | * Convert to forward slashes so that construct_command_argv_internal()
|
|---|
| 3615 | * is not confused.
|
|---|
| 3616 | */
|
|---|
| 3617 | if (shell) {
|
|---|
| 3618 | char *p = w32ify (shell, 0);
|
|---|
| 3619 | strcpy (shell, p);
|
|---|
| 3620 | }
|
|---|
| 3621 | #endif
|
|---|
| 3622 | #ifdef __EMX__
|
|---|
| 3623 | {
|
|---|
| 3624 | static const char *unixroot = NULL;
|
|---|
| 3625 | static const char *last_shell = "";
|
|---|
| 3626 | static int init = 0;
|
|---|
| 3627 | if (init == 0)
|
|---|
| 3628 | {
|
|---|
| 3629 | unixroot = getenv ("UNIXROOT");
|
|---|
| 3630 | /* unixroot must be NULL or not empty */
|
|---|
| 3631 | if (unixroot && unixroot[0] == '\0') unixroot = NULL;
|
|---|
| 3632 | init = 1;
|
|---|
| 3633 | }
|
|---|
| 3634 |
|
|---|
| 3635 | /* if we have an unixroot drive and if shell is not default_shell
|
|---|
| 3636 | (which means it's either cmd.exe or the test has already been
|
|---|
| 3637 | performed) and if shell is an absolute path without drive letter,
|
|---|
| 3638 | try whether it exists e.g.: if "/bin/sh" does not exist use
|
|---|
| 3639 | "$UNIXROOT/bin/sh" instead. */
|
|---|
| 3640 | if (unixroot && shell && strcmp (shell, last_shell) != 0
|
|---|
| 3641 | && (shell[0] == '/' || shell[0] == '\\'))
|
|---|
| 3642 | {
|
|---|
| 3643 | /* trying a new shell, check whether it exists */
|
|---|
| 3644 | size_t size = strlen (shell);
|
|---|
| 3645 | char *buf = xmalloc (size + 7);
|
|---|
| 3646 | memcpy (buf, shell, size);
|
|---|
| 3647 | memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
|
|---|
| 3648 | if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
|
|---|
| 3649 | {
|
|---|
| 3650 | /* try the same for the unixroot drive */
|
|---|
| 3651 | memmove (buf + 2, buf, size + 5);
|
|---|
| 3652 | buf[0] = unixroot[0];
|
|---|
| 3653 | buf[1] = unixroot[1];
|
|---|
| 3654 | if (access (buf, F_OK) == 0)
|
|---|
| 3655 | /* we have found a shell! */
|
|---|
| 3656 | /* free(shell); */
|
|---|
| 3657 | shell = buf;
|
|---|
| 3658 | else
|
|---|
| 3659 | free (buf);
|
|---|
| 3660 | }
|
|---|
| 3661 | else
|
|---|
| 3662 | free (buf);
|
|---|
| 3663 | }
|
|---|
| 3664 | }
|
|---|
| 3665 | #endif /* __EMX__ */
|
|---|
| 3666 |
|
|---|
| 3667 | ifs = allocated_variable_expand_for_file ("$(IFS)", file);
|
|---|
| 3668 |
|
|---|
| 3669 | warn_undefined_variables_flag = save;
|
|---|
| 3670 | }
|
|---|
| 3671 |
|
|---|
| 3672 | argv = construct_command_argv_internal (line, restp, shell, ifs, batch_filename_ptr);
|
|---|
| 3673 |
|
|---|
| 3674 | free (shell);
|
|---|
| 3675 | free (ifs);
|
|---|
| 3676 | #endif /* !VMS */
|
|---|
| 3677 | return argv;
|
|---|
| 3678 | }
|
|---|
| 3679 | |
|---|
| 3680 |
|
|---|
| 3681 | #if !defined(HAVE_DUP2) && !defined(_AMIGA)
|
|---|
| 3682 | int
|
|---|
| 3683 | dup2 (int old, int new)
|
|---|
| 3684 | {
|
|---|
| 3685 | int fd;
|
|---|
| 3686 |
|
|---|
| 3687 | (void) close (new);
|
|---|
| 3688 | fd = dup (old);
|
|---|
| 3689 | if (fd != new)
|
|---|
| 3690 | {
|
|---|
| 3691 | (void) close (fd);
|
|---|
| 3692 | errno = EMFILE;
|
|---|
| 3693 | return -1;
|
|---|
| 3694 | }
|
|---|
| 3695 |
|
|---|
| 3696 | return fd;
|
|---|
| 3697 | }
|
|---|
| 3698 | #endif /* !HAPE_DUP2 && !_AMIGA */
|
|---|