1 /* --------------- Moved here from job.c ---------------
2 This file must be #included in job.c, as it accesses static functions.
4 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GNU Make.
8 GNU Make is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 3 of the License, or (at your option) any later
13 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along with
18 this program. If not, see <http://www.gnu.org/licenses/>. */
24 char *vmsify (char *name, int type);
26 static int vms_jobsefnmask = 0;
28 /* Wait for nchildren children to terminate */
30 vmsWaitForChildren(int *status)
40 *status = sys$wflor (32, vms_jobsefnmask);
45 /* Set up IO redirection. */
48 vms_redirect (struct dsc$descriptor_s *desc, char *fname, char *ibuf)
53 while (isspace ((unsigned char)*ibuf))
56 while (*ibuf && !isspace ((unsigned char)*ibuf))
59 if (strcmp (fptr, "/dev/null") != 0)
61 strcpy (fname, vmsify (fptr, 0));
62 if (strchr (fname, '.') == 0)
65 desc->dsc$w_length = strlen(fname);
66 desc->dsc$a_pointer = fname;
67 desc->dsc$b_dtype = DSC$K_DTYPE_T;
68 desc->dsc$b_class = DSC$K_CLASS_S;
71 printf (_("Warning: Empty redirection\n"));
76 /* found apostrophe at (p-1)
77 inc p until after closing apostrophe.
81 vms_handle_apos (char *p)
85 #define SEPCHARS ",/()= "
101 if (strchr (SEPCHARS, *p))
113 /* This is called as an AST when a child process dies (it won't get
114 interrupted by anything except a higher level AST).
117 vmsHandleChildTerm(struct child *child)
120 register struct child *lastc, *c;
123 vms_jobsefnmask &= ~(1 << (child->efn - 32));
125 lib$free_ef(&child->efn);
127 (void) sigblock (fatal_signal_mask);
129 child_failed = !(child->cstatus & 1 || ((child->cstatus & 7) == 0));
131 /* Search for a child matching the deceased one. */
133 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
134 for (c = children; c != 0 && c != child; lastc = c, c = c->next)
140 if (child_failed && !c->noerror && !ignore_errors_flag)
142 /* The commands failed. Write an error message,
143 delete non-precious targets, and abort. */
144 child_error (c->file->name, c->cstatus, 0, 0, 0);
145 c->file->update_status = 1;
146 delete_child_targets (c);
152 /* The commands failed, but we don't care. */
153 child_error (c->file->name, c->cstatus, 0, 0, 1);
157 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
158 /* If there are more commands to run, try to start them. */
161 switch (c->file->command_state)
164 /* Successfully started. */
168 if (c->file->update_status != 0) {
169 /* We failed to start the commands. */
170 delete_child_targets (c);
175 error (NILF, _("internal error: `%s' command_state"),
180 #endif /* RECURSIVEJOBS */
183 /* Set the state flag to say the commands have finished. */
184 c->file->command_state = cs_finished;
185 notice_finished_file (c->file);
187 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
188 /* Remove the child from the chain and free it. */
192 lastc->next = c->next;
194 #endif /* RECURSIVEJOBS */
196 /* There is now another slot open. */
197 if (job_slots_used > 0)
200 /* If the job failed, and the -k flag was not given, die. */
201 if (child_failed && !keep_going_flag)
204 (void) sigsetmask (sigblock (0) & ~(fatal_signal_mask));
210 Spawn a process executing the command in ARGV and return its pid. */
212 #define MAXCMDLEN 200
214 /* local helpers to make ctrl+c and ctrl+y working, see below */
216 #include <libclidef.h>
219 static int ctrlMask= LIB$M_CLI_CTRLY;
220 static int oldCtrlMask;
221 static int setupYAstTried= 0;
222 static int pidToAbort= 0;
228 lib$enable_ctrl (&oldCtrlMask,0);
235 sys$forcex (&pidToAbort, 0, SS$_ABORT);
238 kill (getpid(),SIGQUIT);
244 $DESCRIPTOR(inputDsc,"SYS$COMMAND");
247 short int status, count;
254 status= sys$assign(&inputDsc,&chan,0,0);
255 if (!(status&SS$_NORMAL)) {
260 status= sys$qiow (0, chan, IO$_SETMODE|IO$M_CTRLYAST,&iosb,0,0,
261 astHandler,0,0,0,0,0);
262 if (status==SS$_NORMAL)
264 if (status==SS$_ILLIOFUNC || status==SS$_NOPRIV) {
266 #ifdef CTRLY_ENABLED_ANYWAY
268 _("-warning, CTRL-Y will leave sub-process(es) around.\n"));
273 else if (!(status&SS$_NORMAL)) {
279 /* called from AST handler ? */
280 if (setupYAstTried>1)
282 if (atexit(reEnableAst))
284 _("-warning, you may have to re-enable CTRL-Y handling from DCL.\n"));
285 status= lib$disable_ctrl (&ctrlMask, &oldCtrlMask);
286 if (!(status&SS$_NORMAL)) {
293 child_execute_job (char *argv, struct child *child)
296 static struct dsc$descriptor_s cmddsc;
297 static struct dsc$descriptor_s pnamedsc;
298 static struct dsc$descriptor_s ifiledsc;
299 static struct dsc$descriptor_s ofiledsc;
300 static struct dsc$descriptor_s efiledsc;
301 int have_redirection = 0;
302 int have_newline = 0;
304 int spflags = CLI$M_NOWAIT;
306 char *cmd = alloca (strlen (argv) + 512), *p, *q;
307 char ifile[256], ofile[256], efile[256];
312 /* Parse IO redirection. */
318 DB (DB_JOBS, ("child_execute_job (%s)\n", argv));
320 while (isspace ((unsigned char)*argv))
326 sprintf (procname, "GMAKE_%05x", getpid () & 0xfffff);
327 pnamedsc.dsc$w_length = strlen(procname);
328 pnamedsc.dsc$a_pointer = procname;
329 pnamedsc.dsc$b_dtype = DSC$K_DTYPE_T;
330 pnamedsc.dsc$b_class = DSC$K_CLASS_S;
333 /* Handle comments and redirection. */
334 for (p = argv, q = cmd; *p; p++, q++)
337 in_string = !in_string;
353 if (isspace ((unsigned char)*p))
355 do { p++; } while (isspace ((unsigned char)*p));
361 p = vms_redirect (&ifiledsc, ifile, p);
363 have_redirection = 1;
366 have_redirection = 1;
370 if (strncmp (p, ">&1", 3) == 0)
373 strcpy (efile, "sys$output");
374 efiledsc.dsc$w_length = strlen(efile);
375 efiledsc.dsc$a_pointer = efile;
376 efiledsc.dsc$b_dtype = DSC$K_DTYPE_T;
377 efiledsc.dsc$b_class = DSC$K_CLASS_S;
381 p = vms_redirect (&efiledsc, efile, p);
386 p = vms_redirect (&ofiledsc, ofile, p);
398 while (isspace ((unsigned char)*--q))
401 if (strncmp (cmd, "builtin_", 8) == 0)
407 DB (DB_JOBS, (_("BUILTIN [%s][%s]\n"), cmd, cmd+8));
413 && ((*(p+2) == ' ') || (*(p+2) == '\t')))
416 while ((*p == ' ') || (*p == '\t'))
418 DB (DB_JOBS, (_("BUILTIN CD %s\n"), p));
424 else if ((*(p) == 'r')
426 && ((*(p+2) == ' ') || (*(p+2) == '\t')))
432 while ((*p == ' ') || (*p == '\t'))
436 DB (DB_JOBS, (_("BUILTIN RM %s\n"), p));
457 printf(_("Unknown builtin command '%s'\n"), cmd);
463 /* Create a *.com file if either the command is too long for
464 lib$spawn, or the command contains a newline, or if redirection
465 is desired. Forcing commands with newlines into DCLs allows to
466 store search lists on user mode logicals. */
468 if (strlen (cmd) > MAXCMDLEN
469 || (have_redirection != 0)
470 || (have_newline != 0))
475 int alevel = 0; /* apostrophe level */
477 if (strlen (cmd) == 0)
479 printf (_("Error, empty command\n"));
484 outfile = open_tmpfile (&comname, "sys$scratch:CMDXXXXXX.COM");
486 pfatal_with_name (_("fopen (temporary file)"));
490 fprintf (outfile, "$ assign/user %s sys$input\n", ifile);
491 DB (DB_JOBS, (_("Redirected input from %s\n"), ifile));
492 ifiledsc.dsc$w_length = 0;
497 fprintf (outfile, "$ define sys$error %s\n", efile);
498 DB (DB_JOBS, (_("Redirected error to %s\n"), efile));
499 efiledsc.dsc$w_length = 0;
504 fprintf (outfile, "$ define sys$output %s\n", ofile);
505 DB (DB_JOBS, (_("Redirected output to %s\n"), ofile));
506 ofiledsc.dsc$w_length = 0;
510 for (c = '\n'; c; c = *q++)
515 /* At a newline, skip any whitespace around a leading $
516 from the command and issue exactly one $ into the DCL. */
517 while (isspace ((unsigned char)*p))
521 while (isspace ((unsigned char)*p))
523 fwrite (p, 1, q - p, outfile);
524 fputc ('$', outfile);
525 fputc (' ', outfile);
526 /* Reset variables. */
530 /* Nice places for line breaks are after strings, after
531 comma or space and before slash. */
533 q = vms_handle_apos (q);
549 /* Enough stuff for a line. */
550 fwrite (p, 1, sep - p, outfile);
554 /* The command continues. */
555 fputc ('-', outfile);
557 fputc ('\n', outfile);
561 fwrite (p, 1, q - p, outfile);
562 fputc ('\n', outfile);
566 sprintf (cmd, "$ @%s", comname);
568 DB (DB_JOBS, (_("Executing %s instead\n"), cmd));
571 cmddsc.dsc$w_length = strlen(cmd);
572 cmddsc.dsc$a_pointer = cmd;
573 cmddsc.dsc$b_dtype = DSC$K_DTYPE_T;
574 cmddsc.dsc$b_class = DSC$K_CLASS_S;
577 while (child->efn < 32 || child->efn > 63)
579 status = lib$get_ef ((unsigned long *)&child->efn);
584 sys$clref (child->efn);
586 vms_jobsefnmask |= (1 << (child->efn - 32));
589 LIB$SPAWN [command-string]
594 [,process-id] [,completion-status-address] [,byte-integer-event-flag-num]
595 [,AST-address] [,varying-AST-argument]
596 [,prompt-string] [,cli] [,table]
599 #ifndef DONTWAITFORCHILD
601 * Code to make ctrl+c and ctrl+y working.
602 * The problem starts with the synchronous case where after lib$spawn is
603 * called any input will go to the child. But with input re-directed,
604 * both control characters won't make it to any of the programs, neither
605 * the spawning nor to the spawned one. Hence the caller needs to spawn
606 * with CLI$M_NOWAIT to NOT give up the input focus. A sys$waitfr
607 * has to follow to simulate the wanted synchronous behaviour.
608 * The next problem is ctrl+y which isn't caught by the crtl and
609 * therefore isn't converted to SIGQUIT (for a signal handler which is
610 * already established). The only way to catch ctrl+y, is an AST
611 * assigned to the input channel. But ctrl+y handling of DCL needs to be
612 * disabled, otherwise it will handle it. Not to mention the previous
613 * ctrl+y handling of DCL needs to be re-established before make exits.
614 * One more: At the time of LIB$SPAWN signals are blocked. SIGQUIT will
615 * make it to the signal handler after the child "normally" terminates.
616 * This isn't enough. It seems reasonable for simple command lines like
617 * a 'cc foobar.c' spawned in a subprocess but it is unacceptable for
618 * spawning make. Therefore we need to abort the process in the AST.
620 * Prior to the spawn it is checked if an AST is already set up for
621 * ctrl+y, if not one is set up for a channel to SYS$COMMAND. In general
622 * this will work except if make is run in a batch environment, but there
623 * nobody can press ctrl+y. During the setup the DCL handling of ctrl+y
624 * is disabled and an exit handler is established to re-enable it.
625 * If the user interrupts with ctrl+y, the assigned AST will fire, force
626 * an abort to the subprocess and signal SIGQUIT, which will be caught by
627 * the already established handler and will bring us back to common code.
628 * After the spawn (now /nowait) a sys$waitfr simulates the /wait and
629 * enables the ctrl+y be delivered to this code. And the ctrl+c too,
630 * which the crtl converts to SIGINT and which is caught by the common
631 * signal handler. Because signals were blocked before entering this code
632 * sys$waitfr will always complete and the SIGQUIT will be processed after
633 * it (after termination of the current block, somewhere in common code).
634 * And SIGINT too will be delayed. That is ctrl+c can only abort when the
635 * current command completes. Anyway it's better than nothing :-)
640 status = lib$spawn (&cmddsc, /* cmd-string */
641 (ifiledsc.dsc$w_length == 0)?0:&ifiledsc, /* input-file */
642 (ofiledsc.dsc$w_length == 0)?0:&ofiledsc, /* output-file */
643 &spflags, /* flags */
644 &pnamedsc, /* proc name */
645 &child->pid, &child->cstatus, &child->efn,
650 pidToAbort= child->pid;
651 status= sys$waitfr (child->efn);
653 vmsHandleChildTerm(child);
656 status = lib$spawn (&cmddsc,
657 (ifiledsc.dsc$w_length == 0)?0:&ifiledsc,
658 (ofiledsc.dsc$w_length == 0)?0:&ofiledsc,
661 &child->pid, &child->cstatus, &child->efn,
662 vmsHandleChildTerm, child,
668 printf (_("Error spawning, %d\n") ,status);
680 if (comname && !ISDB (DB_JOBS))