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