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, 2010 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 static int ctrlYPressed= 0;
114 /* This is called at main or AST level. It is at AST level for DONTWAITFORCHILD
115 and at main level otherwise. In any case it is called when a child process
116 terminated. At AST level it won't get interrupted by anything except a
117 inner mode level AST.
120 vmsHandleChildTerm(struct child *child)
123 register struct child *lastc, *c;
126 vms_jobsefnmask &= ~(1 << (child->efn - 32));
128 lib$free_ef(&child->efn);
131 if (!ISDB (DB_JOBS)&&!ctrlYPressed)
132 unlink (child->comname);
133 free (child->comname);
136 (void) sigblock (fatal_signal_mask);
138 child_failed = !(child->cstatus & 1 || ((child->cstatus & 7) == 0));
140 /* Search for a child matching the deceased one. */
142 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
143 for (c = children; c != 0 && c != child; lastc = c, c = c->next)
149 if (child_failed && !c->noerror && !ignore_errors_flag)
151 /* The commands failed. Write an error message,
152 delete non-precious targets, and abort. */
153 child_error (c->file->name, c->cstatus, 0, 0, 0);
154 c->file->update_status = 1;
155 delete_child_targets (c);
161 /* The commands failed, but we don't care. */
162 child_error (c->file->name, c->cstatus, 0, 0, 1);
166 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
167 /* If there are more commands to run, try to start them. */
170 switch (c->file->command_state)
173 /* Successfully started. */
177 if (c->file->update_status != 0) {
178 /* We failed to start the commands. */
179 delete_child_targets (c);
184 error (NILF, _("internal error: `%s' command_state"),
189 #endif /* RECURSIVEJOBS */
192 /* Set the state flag to say the commands have finished. */
193 c->file->command_state = cs_finished;
194 notice_finished_file (c->file);
196 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
197 /* Remove the child from the chain and free it. */
201 lastc->next = c->next;
203 #endif /* RECURSIVEJOBS */
205 /* There is now another slot open. */
206 if (job_slots_used > 0)
209 /* If the job failed, and the -k flag was not given, die. */
210 if (child_failed && !keep_going_flag)
213 (void) sigsetmask (sigblock (0) & ~(fatal_signal_mask));
219 Spawn a process executing the command in ARGV and return its pid. */
221 #define MAXCMDLEN 200
223 /* local helpers to make ctrl+c and ctrl+y working, see below */
225 #include <libclidef.h>
228 static int ctrlMask= LIB$M_CLI_CTRLY;
229 static int oldCtrlMask;
230 static int setupYAstTried= 0;
231 static unsigned short int chan= 0;
236 lib$enable_ctrl (&oldCtrlMask,0);
243 for (c = children; c != 0; c = c->next)
244 sys$delprc (&c->pid, 0, 0);
246 kill (getpid(),SIGQUIT);
253 $DESCRIPTOR(inputDsc,"SYS$COMMAND");
256 short int status, count;
259 unsigned short int loc_chan;
266 status= sys$assign(&inputDsc,&loc_chan,0,0);
267 if (!(status&SS$_NORMAL)) {
272 status= sys$qiow (0, loc_chan, IO$_SETMODE|IO$M_CTRLYAST,&iosb,0,0,
273 astYHandler,0,0,0,0,0);
274 if (status==SS$_NORMAL)
276 if (status!=SS$_NORMAL) {
278 sys$dassgn(loc_chan);
279 if (status!=SS$_ILLIOFUNC && status!=SS$_NOPRIV)
284 /* called from AST handler ? */
285 if (setupYAstTried>1)
287 if (atexit(reEnableAst))
289 _("-warning, you may have to re-enable CTRL-Y handling from DCL.\n"));
290 status= lib$disable_ctrl (&ctrlMask, &oldCtrlMask);
291 if (!(status&SS$_NORMAL)) {
300 child_execute_job (char *argv, struct child *child)
303 static struct dsc$descriptor_s cmddsc;
304 static struct dsc$descriptor_s pnamedsc;
305 static struct dsc$descriptor_s ifiledsc;
306 static struct dsc$descriptor_s ofiledsc;
307 static struct dsc$descriptor_s efiledsc;
308 int have_redirection = 0;
310 int have_newline = 0;
312 int spflags = CLI$M_NOWAIT;
314 char *cmd = alloca (strlen (argv) + 512), *p, *q;
315 char ifile[256], ofile[256], efile[256];
320 /* Parse IO redirection. */
325 child->comname = NULL;
327 DB (DB_JOBS, ("child_execute_job (%s)\n", argv));
329 while (isspace ((unsigned char)*argv))
335 sprintf (procname, "GMAKE_%05x", getpid () & 0xfffff);
336 pnamedsc.dsc$w_length = strlen(procname);
337 pnamedsc.dsc$a_pointer = procname;
338 pnamedsc.dsc$b_dtype = DSC$K_DTYPE_T;
339 pnamedsc.dsc$b_class = DSC$K_CLASS_S;
342 /* Handle comments and redirection. */
343 for (p = argv, q = cmd; *p; p++, q++)
346 in_string = !in_string;
362 if (isspace ((unsigned char)*p))
364 do { p++; } while (isspace ((unsigned char)*p));
370 p = vms_redirect (&ifiledsc, ifile, p);
372 have_redirection = 1;
375 have_redirection = 1;
379 if (strncmp (p, ">&1", 3) == 0)
382 strcpy (efile, "sys$output");
383 efiledsc.dsc$w_length = strlen(efile);
384 efiledsc.dsc$a_pointer = efile;
385 efiledsc.dsc$b_dtype = DSC$K_DTYPE_T;
386 efiledsc.dsc$b_class = DSC$K_CLASS_S;
390 p = vms_redirect (&efiledsc, efile, p);
400 p = vms_redirect (&ofiledsc, ofile, p);
412 while (isspace ((unsigned char)*--q))
415 if (strncmp (cmd, "builtin_", 8) == 0)
421 DB (DB_JOBS, (_("BUILTIN [%s][%s]\n"), cmd, cmd+8));
427 && ((*(p+2) == ' ') || (*(p+2) == '\t')))
430 while ((*p == ' ') || (*p == '\t'))
432 DB (DB_JOBS, (_("BUILTIN CD %s\n"), p));
438 else if ((*(p) == 'r')
440 && ((*(p+2) == ' ') || (*(p+2) == '\t')))
446 while ((*p == ' ') || (*p == '\t'))
450 DB (DB_JOBS, (_("BUILTIN RM %s\n"), p));
471 printf(_("Unknown builtin command '%s'\n"), cmd);
477 /* Create a *.com file if either the command is too long for
478 lib$spawn, or the command contains a newline, or if redirection
479 is desired. Forcing commands with newlines into DCLs allows to
480 store search lists on user mode logicals. */
482 if (strlen (cmd) > MAXCMDLEN
483 || (have_redirection != 0)
484 || (have_newline != 0))
489 int alevel = 0; /* apostrophe level */
491 if (strlen (cmd) == 0)
493 printf (_("Error, empty command\n"));
498 outfile = open_tmpfile (&child->comname, "sys$scratch:CMDXXXXXX.COM");
500 pfatal_with_name (_("fopen (temporary file)"));
501 comnamelen = strlen (child->comname);
505 fprintf (outfile, "$ assign/user %s sys$input\n", ifile);
506 DB (DB_JOBS, (_("Redirected input from %s\n"), ifile));
507 ifiledsc.dsc$w_length = 0;
512 fprintf (outfile, "$ define sys$error %s\n", efile);
513 DB (DB_JOBS, (_("Redirected error to %s\n"), efile));
514 efiledsc.dsc$w_length = 0;
521 fprintf (outfile, "$ set noon\n");
522 fprintf (outfile, "$ define sys$output %.*s\n", comnamelen-3, child->comname);
523 DB (DB_JOBS, (_("Append output to %s\n"), ofile));
524 ofiledsc.dsc$w_length = 0;
528 fprintf (outfile, "$ define sys$output %s\n", ofile);
529 DB (DB_JOBS, (_("Redirected output to %s\n"), ofile));
530 ofiledsc.dsc$w_length = 0;
535 for (c = '\n'; c; c = *q++)
540 /* At a newline, skip any whitespace around a leading $
541 from the command and issue exactly one $ into the DCL. */
542 while (isspace ((unsigned char)*p))
546 while (isspace ((unsigned char)*p))
548 fwrite (p, 1, q - p, outfile);
549 fputc ('$', outfile);
550 fputc (' ', outfile);
551 /* Reset variables. */
555 /* Nice places for line breaks are after strings, after
556 comma or space and before slash. */
558 q = vms_handle_apos (q);
574 /* Enough stuff for a line. */
575 fwrite (p, 1, sep - p, outfile);
579 /* The command continues. */
580 fputc ('-', outfile);
582 fputc ('\n', outfile);
588 fwrite (p, 1, --q - p, outfile);
589 fputc ('\n', outfile);
594 fprintf (outfile, "$ deassign sys$output ! 'f$verify(0)\n");
595 fprintf (outfile, "$ append:=append\n");
596 fprintf (outfile, "$ delete:=delete\n");
597 fprintf (outfile, "$ append/new %.*s %s\n", comnamelen-3, child->comname, ofile);
598 fprintf (outfile, "$ delete %.*s;*\n", comnamelen-3, child->comname);
599 DB (DB_JOBS, (_("Append %.*s and cleanup\n"), comnamelen-3, child->comname));
604 sprintf (cmd, "$ @%s", child->comname);
606 DB (DB_JOBS, (_("Executing %s instead\n"), cmd));
609 cmddsc.dsc$w_length = strlen(cmd);
610 cmddsc.dsc$a_pointer = cmd;
611 cmddsc.dsc$b_dtype = DSC$K_DTYPE_T;
612 cmddsc.dsc$b_class = DSC$K_CLASS_S;
615 while (child->efn < 32 || child->efn > 63)
617 status = lib$get_ef ((unsigned long *)&child->efn);
623 unlink (child->comname);
624 free (child->comname);
630 sys$clref (child->efn);
632 vms_jobsefnmask |= (1 << (child->efn - 32));
635 LIB$SPAWN [command-string]
640 [,process-id] [,completion-status-address] [,byte-integer-event-flag-num]
641 [,AST-address] [,varying-AST-argument]
642 [,prompt-string] [,cli] [,table]
645 #ifndef DONTWAITFORCHILD
647 * Code to make ctrl+c and ctrl+y working.
648 * The problem starts with the synchronous case where after lib$spawn is
649 * called any input will go to the child. But with input re-directed,
650 * both control characters won't make it to any of the programs, neither
651 * the spawning nor to the spawned one. Hence the caller needs to spawn
652 * with CLI$M_NOWAIT to NOT give up the input focus. A sys$waitfr
653 * has to follow to simulate the wanted synchronous behaviour.
654 * The next problem is ctrl+y which isn't caught by the crtl and
655 * therefore isn't converted to SIGQUIT (for a signal handler which is
656 * already established). The only way to catch ctrl+y, is an AST
657 * assigned to the input channel. But ctrl+y handling of DCL needs to be
658 * disabled, otherwise it will handle it. Not to mention the previous
659 * ctrl+y handling of DCL needs to be re-established before make exits.
660 * One more: At the time of LIB$SPAWN signals are blocked. SIGQUIT will
661 * make it to the signal handler after the child "normally" terminates.
662 * This isn't enough. It seems reasonable for simple command lines like
663 * a 'cc foobar.c' spawned in a subprocess but it is unacceptable for
664 * spawning make. Therefore we need to abort the process in the AST.
666 * Prior to the spawn it is checked if an AST is already set up for
667 * ctrl+y, if not one is set up for a channel to SYS$COMMAND. In general
668 * this will work except if make is run in a batch environment, but there
669 * nobody can press ctrl+y. During the setup the DCL handling of ctrl+y
670 * is disabled and an exit handler is established to re-enable it.
671 * If the user interrupts with ctrl+y, the assigned AST will fire, force
672 * an abort to the subprocess and signal SIGQUIT, which will be caught by
673 * the already established handler and will bring us back to common code.
674 * After the spawn (now /nowait) a sys$waitfr simulates the /wait and
675 * enables the ctrl+y be delivered to this code. And the ctrl+c too,
676 * which the crtl converts to SIGINT and which is caught by the common
677 * signal handler. Because signals were blocked before entering this code
678 * sys$waitfr will always complete and the SIGQUIT will be processed after
679 * it (after termination of the current block, somewhere in common code).
680 * And SIGINT too will be delayed. That is ctrl+c can only abort when the
681 * current command completes. Anyway it's better than nothing :-)
686 status = lib$spawn (&cmddsc, /* cmd-string */
687 (ifiledsc.dsc$w_length == 0)?0:&ifiledsc, /* input-file */
688 (ofiledsc.dsc$w_length == 0)?0:&ofiledsc, /* output-file */
689 &spflags, /* flags */
690 &pnamedsc, /* proc name */
691 &child->pid, &child->cstatus, &child->efn,
696 status= sys$waitfr (child->efn);
697 vmsHandleChildTerm(child);
700 status = lib$spawn (&cmddsc,
701 (ifiledsc.dsc$w_length == 0)?0:&ifiledsc,
702 (ofiledsc.dsc$w_length == 0)?0:&ofiledsc,
705 &child->pid, &child->cstatus, &child->efn,
706 vmsHandleChildTerm, child,
712 printf (_("Error spawning, %d\n") ,status);