1 /* --------------- Moved here from job.c ---------------
2 This file must be #included in job.c, as it accesses static functions.
4 Copyright (C) 1996-2012 Free Software Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
23 char *vmsify (char *name
, int type
);
25 static int vms_jobsefnmask
= 0;
27 /* Wait for nchildren children to terminate */
29 vmsWaitForChildren(int *status
)
39 *status
= sys$
wflor (32, vms_jobsefnmask
);
44 /* Set up IO redirection. */
47 vms_redirect (struct dsc$descriptor_s
*desc
, char *fname
, char *ibuf
)
52 while (isspace ((unsigned char)*ibuf
))
55 while (*ibuf
&& !isspace ((unsigned char)*ibuf
))
58 if (strcmp (fptr
, "/dev/null") != 0)
60 strcpy (fname
, vmsify (fptr
, 0));
61 if (strchr (fname
, '.') == 0)
64 desc
->dsc$w_length
= strlen(fname
);
65 desc
->dsc$a_pointer
= fname
;
66 desc
->dsc$b_dtype
= DSC$K_DTYPE_T
;
67 desc
->dsc$b_class
= DSC$K_CLASS_S
;
70 printf (_("Warning: Empty redirection\n"));
75 /* found apostrophe at (p-1)
76 inc p until after closing apostrophe.
80 vms_handle_apos (char *p
)
84 #define SEPCHARS ",/()= "
100 if (strchr (SEPCHARS
, *p
))
112 static int ctrlYPressed
= 0;
113 /* This is called at main or AST level. It is at AST level for DONTWAITFORCHILD
114 and at main level otherwise. In any case it is called when a child process
115 terminated. At AST level it won't get interrupted by anything except a
116 inner mode level AST.
119 vmsHandleChildTerm(struct child
*child
)
122 register struct child
*lastc
, *c
;
125 vms_jobsefnmask
&= ~(1 << (child
->efn
- 32));
127 lib$
free_ef(&child
->efn
);
130 if (!ISDB (DB_JOBS
)&&!ctrlYPressed
)
131 unlink (child
->comname
);
132 free (child
->comname
);
135 (void) sigblock (fatal_signal_mask
);
137 child_failed
= !(child
->cstatus
& 1 || ((child
->cstatus
& 7) == 0));
139 /* Search for a child matching the deceased one. */
141 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
142 for (c
= children
; c
!= 0 && c
!= child
; lastc
= c
, c
= c
->next
)
148 if (child_failed
&& !c
->noerror
&& !ignore_errors_flag
)
150 /* The commands failed. Write an error message,
151 delete non-precious targets, and abort. */
152 child_error (c
->file
->name
, c
->cstatus
, 0, 0, 0);
153 c
->file
->update_status
= 1;
154 delete_child_targets (c
);
160 /* The commands failed, but we don't care. */
161 child_error (c
->file
->name
, c
->cstatus
, 0, 0, 1);
165 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
166 /* If there are more commands to run, try to start them. */
169 switch (c
->file
->command_state
)
172 /* Successfully started. */
176 if (c
->file
->update_status
!= 0) {
177 /* We failed to start the commands. */
178 delete_child_targets (c
);
183 error (NILF
, _("internal error: '%s' command_state"),
188 #endif /* RECURSIVEJOBS */
191 /* Set the state flag to say the commands have finished. */
192 c
->file
->command_state
= cs_finished
;
193 notice_finished_file (c
->file
);
195 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
196 /* Remove the child from the chain and free it. */
200 lastc
->next
= c
->next
;
202 #endif /* RECURSIVEJOBS */
204 /* There is now another slot open. */
205 if (job_slots_used
> 0)
208 /* If the job failed, and the -k flag was not given, die. */
209 if (child_failed
&& !keep_going_flag
)
212 (void) sigsetmask (sigblock (0) & ~(fatal_signal_mask
));
218 Spawn a process executing the command in ARGV and return its pid. */
220 #define MAXCMDLEN 200
222 /* local helpers to make ctrl+c and ctrl+y working, see below */
224 #include <libclidef.h>
227 static int ctrlMask
= LIB$M_CLI_CTRLY
;
228 static int oldCtrlMask
;
229 static int setupYAstTried
= 0;
230 static unsigned short int chan
= 0;
235 lib$
enable_ctrl (&oldCtrlMask
,0);
242 for (c
= children
; c
!= 0; c
= c
->next
)
243 sys$
delprc (&c
->pid
, 0, 0);
245 kill (getpid(),SIGQUIT
);
252 $
DESCRIPTOR(inputDsc
,"SYS$COMMAND");
255 short int status
, count
;
258 unsigned short int loc_chan
;
265 status
= sys$
assign(&inputDsc
,&loc_chan
,0,0);
266 if (!(status
&SS$_NORMAL
)) {
271 status
= sys$
qiow (0, loc_chan
, IO$_SETMODE
|IO$M_CTRLYAST
,&iosb
,0,0,
272 astYHandler
,0,0,0,0,0);
273 if (status
==SS$_NORMAL
)
275 if (status
!=SS$_NORMAL
) {
277 sys$
dassgn(loc_chan
);
278 if (status
!=SS$_ILLIOFUNC
&& status
!=SS$_NOPRIV
)
283 /* called from AST handler ? */
284 if (setupYAstTried
>1)
286 if (atexit(reEnableAst
))
288 _("-warning, you may have to re-enable CTRL-Y handling from DCL.\n"));
289 status
= lib$
disable_ctrl (&ctrlMask
, &oldCtrlMask
);
290 if (!(status
&SS$_NORMAL
)) {
299 child_execute_job (char *argv
, struct child
*child
)
302 static struct dsc$descriptor_s cmddsc
;
303 static struct dsc$descriptor_s pnamedsc
;
304 static struct dsc$descriptor_s ifiledsc
;
305 static struct dsc$descriptor_s ofiledsc
;
306 static struct dsc$descriptor_s efiledsc
;
307 int have_redirection
= 0;
309 int have_newline
= 0;
311 int spflags
= CLI$M_NOWAIT
;
313 char *cmd
= alloca (strlen (argv
) + 512), *p
, *q
;
314 char ifile
[256], ofile
[256], efile
[256];
319 /* Parse IO redirection. */
324 child
->comname
= NULL
;
326 DB (DB_JOBS
, ("child_execute_job (%s)\n", argv
));
328 while (isspace ((unsigned char)*argv
))
334 sprintf (procname
, "GMAKE_%05x", getpid () & 0xfffff);
335 pnamedsc
.dsc$w_length
= strlen(procname
);
336 pnamedsc
.dsc$a_pointer
= procname
;
337 pnamedsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
338 pnamedsc
.dsc$b_class
= DSC$K_CLASS_S
;
341 /* Handle comments and redirection. */
342 for (p
= argv
, q
= cmd
; *p
; p
++, q
++)
345 in_string
= !in_string
;
361 if (isspace ((unsigned char)*p
))
363 do { p
++; } while (isspace ((unsigned char)*p
));
369 p
= vms_redirect (&ifiledsc
, ifile
, p
);
371 have_redirection
= 1;
374 have_redirection
= 1;
378 if (strncmp (p
, ">&1", 3) == 0)
381 strcpy (efile
, "sys$output");
382 efiledsc
.dsc$w_length
= strlen(efile
);
383 efiledsc
.dsc$a_pointer
= efile
;
384 efiledsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
385 efiledsc
.dsc$b_class
= DSC$K_CLASS_S
;
389 p
= vms_redirect (&efiledsc
, efile
, p
);
399 p
= vms_redirect (&ofiledsc
, ofile
, p
);
411 while (isspace ((unsigned char)*--q
))
414 if (strncmp (cmd
, "builtin_", 8) == 0)
420 DB (DB_JOBS
, (_("BUILTIN [%s][%s]\n"), cmd
, cmd
+8));
426 && ((*(p
+2) == ' ') || (*(p
+2) == '\t')))
429 while ((*p
== ' ') || (*p
== '\t'))
431 DB (DB_JOBS
, (_("BUILTIN CD %s\n"), p
));
437 else if ((*(p
) == 'r')
439 && ((*(p
+2) == ' ') || (*(p
+2) == '\t')))
445 while ((*p
== ' ') || (*p
== '\t'))
449 DB (DB_JOBS
, (_("BUILTIN RM %s\n"), p
));
470 printf(_("Unknown builtin command '%s'\n"), cmd
);
476 /* Create a *.com file if either the command is too long for
477 lib$spawn, or the command contains a newline, or if redirection
478 is desired. Forcing commands with newlines into DCLs allows to
479 store search lists on user mode logicals. */
481 if (strlen (cmd
) > MAXCMDLEN
482 || (have_redirection
!= 0)
483 || (have_newline
!= 0))
488 int alevel
= 0; /* apostrophe level */
490 if (strlen (cmd
) == 0)
492 printf (_("Error, empty command\n"));
497 outfile
= open_tmpfile (&child
->comname
, "sys$scratch:CMDXXXXXX.COM");
499 pfatal_with_name (_("fopen (temporary file)"));
500 comnamelen
= strlen (child
->comname
);
504 fprintf (outfile
, "$ assign/user %s sys$input\n", ifile
);
505 DB (DB_JOBS
, (_("Redirected input from %s\n"), ifile
));
506 ifiledsc
.dsc$w_length
= 0;
511 fprintf (outfile
, "$ define sys$error %s\n", efile
);
512 DB (DB_JOBS
, (_("Redirected error to %s\n"), efile
));
513 efiledsc
.dsc$w_length
= 0;
520 fprintf (outfile
, "$ set noon\n");
521 fprintf (outfile
, "$ define sys$output %.*s\n", comnamelen
-3, child
->comname
);
522 DB (DB_JOBS
, (_("Append output to %s\n"), ofile
));
523 ofiledsc
.dsc$w_length
= 0;
527 fprintf (outfile
, "$ define sys$output %s\n", ofile
);
528 DB (DB_JOBS
, (_("Redirected output to %s\n"), ofile
));
529 ofiledsc
.dsc$w_length
= 0;
534 for (c
= '\n'; c
; c
= *q
++)
539 /* At a newline, skip any whitespace around a leading $
540 from the command and issue exactly one $ into the DCL. */
541 while (isspace ((unsigned char)*p
))
545 while (isspace ((unsigned char)*p
))
547 fwrite (p
, 1, q
- p
, outfile
);
548 fputc ('$', outfile
);
549 fputc (' ', outfile
);
550 /* Reset variables. */
554 /* Nice places for line breaks are after strings, after
555 comma or space and before slash. */
557 q
= vms_handle_apos (q
);
573 /* Enough stuff for a line. */
574 fwrite (p
, 1, sep
- p
, outfile
);
578 /* The command continues. */
579 fputc ('-', outfile
);
581 fputc ('\n', outfile
);
587 fwrite (p
, 1, --q
- p
, outfile
);
588 fputc ('\n', outfile
);
593 fprintf (outfile
, "$ deassign sys$output ! 'f$verify(0)\n");
594 fprintf (outfile
, "$ append:=append\n");
595 fprintf (outfile
, "$ delete:=delete\n");
596 fprintf (outfile
, "$ append/new %.*s %s\n", comnamelen
-3, child
->comname
, ofile
);
597 fprintf (outfile
, "$ delete %.*s;*\n", comnamelen
-3, child
->comname
);
598 DB (DB_JOBS
, (_("Append %.*s and cleanup\n"), comnamelen
-3, child
->comname
));
603 sprintf (cmd
, "$ @%s", child
->comname
);
605 DB (DB_JOBS
, (_("Executing %s instead\n"), cmd
));
608 cmddsc
.dsc$w_length
= strlen(cmd
);
609 cmddsc
.dsc$a_pointer
= cmd
;
610 cmddsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
611 cmddsc
.dsc$b_class
= DSC$K_CLASS_S
;
614 while (child
->efn
< 32 || child
->efn
> 63)
616 status
= lib$
get_ef ((unsigned long *)&child
->efn
);
622 unlink (child
->comname
);
623 free (child
->comname
);
629 sys$
clref (child
->efn
);
631 vms_jobsefnmask
|= (1 << (child
->efn
- 32));
634 LIB$SPAWN [command-string]
639 [,process-id] [,completion-status-address] [,byte-integer-event-flag-num]
640 [,AST-address] [,varying-AST-argument]
641 [,prompt-string] [,cli] [,table]
644 #ifndef DONTWAITFORCHILD
646 * Code to make ctrl+c and ctrl+y working.
647 * The problem starts with the synchronous case where after lib$spawn is
648 * called any input will go to the child. But with input re-directed,
649 * both control characters won't make it to any of the programs, neither
650 * the spawning nor to the spawned one. Hence the caller needs to spawn
651 * with CLI$M_NOWAIT to NOT give up the input focus. A sys$waitfr
652 * has to follow to simulate the wanted synchronous behaviour.
653 * The next problem is ctrl+y which isn't caught by the crtl and
654 * therefore isn't converted to SIGQUIT (for a signal handler which is
655 * already established). The only way to catch ctrl+y, is an AST
656 * assigned to the input channel. But ctrl+y handling of DCL needs to be
657 * disabled, otherwise it will handle it. Not to mention the previous
658 * ctrl+y handling of DCL needs to be re-established before make exits.
659 * One more: At the time of LIB$SPAWN signals are blocked. SIGQUIT will
660 * make it to the signal handler after the child "normally" terminates.
661 * This isn't enough. It seems reasonable for simple command lines like
662 * a 'cc foobar.c' spawned in a subprocess but it is unacceptable for
663 * spawning make. Therefore we need to abort the process in the AST.
665 * Prior to the spawn it is checked if an AST is already set up for
666 * ctrl+y, if not one is set up for a channel to SYS$COMMAND. In general
667 * this will work except if make is run in a batch environment, but there
668 * nobody can press ctrl+y. During the setup the DCL handling of ctrl+y
669 * is disabled and an exit handler is established to re-enable it.
670 * If the user interrupts with ctrl+y, the assigned AST will fire, force
671 * an abort to the subprocess and signal SIGQUIT, which will be caught by
672 * the already established handler and will bring us back to common code.
673 * After the spawn (now /nowait) a sys$waitfr simulates the /wait and
674 * enables the ctrl+y be delivered to this code. And the ctrl+c too,
675 * which the crtl converts to SIGINT and which is caught by the common
676 * signal handler. Because signals were blocked before entering this code
677 * sys$waitfr will always complete and the SIGQUIT will be processed after
678 * it (after termination of the current block, somewhere in common code).
679 * And SIGINT too will be delayed. That is ctrl+c can only abort when the
680 * current command completes. Anyway it's better than nothing :-)
685 status
= lib$
spawn (&cmddsc
, /* cmd-string */
686 (ifiledsc
.dsc$w_length
== 0)?0:&ifiledsc
, /* input-file */
687 (ofiledsc
.dsc$w_length
== 0)?0:&ofiledsc
, /* output-file */
688 &spflags
, /* flags */
689 &pnamedsc
, /* proc name */
690 &child
->pid
, &child
->cstatus
, &child
->efn
,
695 status
= sys$
waitfr (child
->efn
);
696 vmsHandleChildTerm(child
);
699 status
= lib$
spawn (&cmddsc
,
700 (ifiledsc
.dsc$w_length
== 0)?0:&ifiledsc
,
701 (ofiledsc
.dsc$w_length
== 0)?0:&ofiledsc
,
704 &child
->pid
, &child
->cstatus
, &child
->efn
,
705 vmsHandleChildTerm
, child
,
711 printf (_("Error spawning, %d\n") ,status
);