Implement new "if... else if... endif" semantics.
[make.git] / vmsjobs.c
blobbc15276c67741f23938ced57cb698457b26635d8
1 \f
3 /* --------------- Moved here from job.c ---------------
4 This file must be #included in job.c, as it accesses static functions.
5 */
7 static int vms_jobsefnmask = 0;
9 /* Wait for nchildren children to terminate */
10 void
11 vmsWaitForChildren(int *status)
13 while (1)
15 if (!vms_jobsefnmask)
17 *status = 0;
18 return;
21 *status = sys$wflor (32, vms_jobsefnmask);
23 return;
26 /* Set up IO redirection. */
28 char *
29 vms_redirect (struct dsc$descriptor_s *desc, char *fname, char *ibuf)
31 char *fptr;
33 ibuf++;
34 while (isspace ((unsigned char)*ibuf))
35 ibuf++;
36 fptr = ibuf;
37 while (*ibuf && !isspace ((unsigned char)*ibuf))
38 ibuf++;
39 *ibuf = 0;
40 if (strcmp (fptr, "/dev/null") != 0)
42 strcpy (fname, vmsify (fptr, 0));
43 if (strchr (fname, '.') == 0)
44 strcat (fname, ".");
46 desc->dsc$w_length = strlen(fname);
47 desc->dsc$a_pointer = fname;
48 desc->dsc$b_dtype = DSC$K_DTYPE_T;
49 desc->dsc$b_class = DSC$K_CLASS_S;
51 if (*fname == 0)
52 printf (_("Warning: Empty redirection\n"));
53 return ibuf;
57 /* found apostrophe at (p-1)
58 inc p until after closing apostrophe.
61 char *
62 vms_handle_apos (char *p)
64 int alast;
66 #define SEPCHARS ",/()= "
68 alast = 0;
70 while (*p != 0)
72 if (*p == '"')
74 if (alast)
76 alast = 0;
77 p++;
79 else
81 p++;
82 if (strchr (SEPCHARS, *p))
83 break;
84 alast = 1;
87 else
88 p++;
91 return p;
94 #include <descrip.h>
95 #include <clidef.h>
97 /* This is called as an AST when a child process dies (it won't get
98 interrupted by anything except a higher level AST).
101 vmsHandleChildTerm(struct child *child)
103 int status;
104 register struct child *lastc, *c;
105 int child_failed;
107 vms_jobsefnmask &= ~(1 << (child->efn - 32));
109 lib$free_ef(&child->efn);
111 (void) sigblock (fatal_signal_mask);
113 child_failed = !(child->cstatus & 1 || ((child->cstatus & 7) == 0));
115 /* Search for a child matching the deceased one. */
116 lastc = 0;
117 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
118 for (c = children; c != 0 && c != child; lastc = c, c = c->next)
120 #else
121 c = child;
122 #endif
124 if (child_failed && !c->noerror && !ignore_errors_flag)
126 /* The commands failed. Write an error message,
127 delete non-precious targets, and abort. */
128 child_error (c->file->name, c->cstatus, 0, 0, 0);
129 c->file->update_status = 1;
130 delete_child_targets (c);
132 else
134 if (child_failed)
136 /* The commands failed, but we don't care. */
137 child_error (c->file->name, c->cstatus, 0, 0, 1);
138 child_failed = 0;
141 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
142 /* If there are more commands to run, try to start them. */
143 start_job (c);
145 switch (c->file->command_state)
147 case cs_running:
148 /* Successfully started. */
149 break;
151 case cs_finished:
152 if (c->file->update_status != 0) {
153 /* We failed to start the commands. */
154 delete_child_targets (c);
156 break;
158 default:
159 error (NILF, _("internal error: `%s' command_state"),
160 c->file->name);
161 abort ();
162 break;
164 #endif /* RECURSIVEJOBS */
167 /* Set the state flag to say the commands have finished. */
168 c->file->command_state = cs_finished;
169 notice_finished_file (c->file);
171 #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
172 /* Remove the child from the chain and free it. */
173 if (lastc == 0)
174 children = c->next;
175 else
176 lastc->next = c->next;
177 free_child (c);
178 #endif /* RECURSIVEJOBS */
180 /* There is now another slot open. */
181 if (job_slots_used > 0)
182 --job_slots_used;
184 /* If the job failed, and the -k flag was not given, die. */
185 if (child_failed && !keep_going_flag)
186 die (EXIT_FAILURE);
188 (void) sigsetmask (sigblock (0) & ~(fatal_signal_mask));
190 return 1;
193 /* VMS:
194 Spawn a process executing the command in ARGV and return its pid. */
196 #define MAXCMDLEN 200
198 /* local helpers to make ctrl+c and ctrl+y working, see below */
199 #include <libclidef.h>
200 #include <ssdef.h>
202 static int ctrlMask= LIB$M_CLI_CTRLY;
203 static int oldCtrlMask;
204 static int setupYAstTried= 0;
205 static int pidToAbort= 0;
206 static int chan= 0;
208 static void
209 reEnableAst(void)
211 lib$enable_ctrl (&oldCtrlMask,0);
214 static void
215 astHandler (void)
217 if (pidToAbort) {
218 sys$forcex (&pidToAbort, 0, SS$_ABORT);
219 pidToAbort= 0;
221 kill (getpid(),SIGQUIT);
224 static void
225 tryToSetupYAst(void)
227 $DESCRIPTOR(inputDsc,"SYS$COMMAND");
228 int status;
229 struct {
230 short int status, count;
231 int dvi;
232 } iosb;
234 setupYAstTried++;
236 if (!chan) {
237 status= sys$assign(&inputDsc,&chan,0,0);
238 if (!(status&SS$_NORMAL)) {
239 lib$signal(status);
240 return;
243 status= sys$qiow (0, chan, IO$_SETMODE|IO$M_CTRLYAST,&iosb,0,0,
244 astHandler,0,0,0,0,0);
245 if (status==SS$_NORMAL)
246 status= iosb.status;
247 if (status==SS$_ILLIOFUNC || status==SS$_NOPRIV) {
248 sys$dassgn(chan);
249 #ifdef CTRLY_ENABLED_ANYWAY
250 fprintf (stderr,
251 _("-warning, CTRL-Y will leave sub-process(es) around.\n"));
252 #else
253 return;
254 #endif
256 else if (!(status&SS$_NORMAL)) {
257 sys$dassgn(chan);
258 lib$signal(status);
259 return;
262 /* called from AST handler ? */
263 if (setupYAstTried>1)
264 return;
265 if (atexit(reEnableAst))
266 fprintf (stderr,
267 _("-warning, you may have to re-enable CTRL-Y handling from DCL.\n"));
268 status= lib$disable_ctrl (&ctrlMask, &oldCtrlMask);
269 if (!(status&SS$_NORMAL)) {
270 lib$signal(status);
271 return;
276 child_execute_job (char *argv, struct child *child)
278 int i;
279 static struct dsc$descriptor_s cmddsc;
280 static struct dsc$descriptor_s pnamedsc;
281 static struct dsc$descriptor_s ifiledsc;
282 static struct dsc$descriptor_s ofiledsc;
283 static struct dsc$descriptor_s efiledsc;
284 int have_redirection = 0;
285 int have_newline = 0;
287 int spflags = CLI$M_NOWAIT;
288 int status;
289 char *cmd = alloca (strlen (argv) + 512), *p, *q;
290 char ifile[256], ofile[256], efile[256];
291 char *comname = 0;
292 char procname[100];
293 int in_string;
295 /* Parse IO redirection. */
297 ifile[0] = 0;
298 ofile[0] = 0;
299 efile[0] = 0;
301 DB (DB_JOBS, ("child_execute_job (%s)\n", argv));
303 while (isspace ((unsigned char)*argv))
304 argv++;
306 if (*argv == 0)
307 return 0;
309 sprintf (procname, "GMAKE_%05x", getpid () & 0xfffff);
310 pnamedsc.dsc$w_length = strlen(procname);
311 pnamedsc.dsc$a_pointer = procname;
312 pnamedsc.dsc$b_dtype = DSC$K_DTYPE_T;
313 pnamedsc.dsc$b_class = DSC$K_CLASS_S;
315 in_string = 0;
316 /* Handle comments and redirection. */
317 for (p = argv, q = cmd; *p; p++, q++)
319 if (*p == '"')
320 in_string = !in_string;
321 if (in_string)
323 *q = *p;
324 continue;
326 switch (*p)
328 case '#':
329 *p-- = 0;
330 *q-- = 0;
331 break;
332 case '\\':
333 p++;
334 if (*p == '\n')
335 p++;
336 if (isspace ((unsigned char)*p))
338 do { p++; } while (isspace ((unsigned char)*p));
339 p--;
341 *q = *p;
342 break;
343 case '<':
344 p = vms_redirect (&ifiledsc, ifile, p);
345 *q = ' ';
346 have_redirection = 1;
347 break;
348 case '>':
349 have_redirection = 1;
350 if (*(p-1) == '2')
352 q--;
353 if (strncmp (p, ">&1", 3) == 0)
355 p += 3;
356 strcpy (efile, "sys$output");
357 efiledsc.dsc$w_length = strlen(efile);
358 efiledsc.dsc$a_pointer = efile;
359 efiledsc.dsc$b_dtype = DSC$K_DTYPE_T;
360 efiledsc.dsc$b_class = DSC$K_CLASS_S;
362 else
364 p = vms_redirect (&efiledsc, efile, p);
367 else
369 p = vms_redirect (&ofiledsc, ofile, p);
371 *q = ' ';
372 break;
373 case '\n':
374 have_newline = 1;
375 default:
376 *q = *p;
377 break;
380 *q = *p;
381 while (isspace ((unsigned char)*--q))
382 *q = '\0';
384 if (strncmp (cmd, "builtin_", 8) == 0)
386 child->pid = 270163;
387 child->efn = 0;
388 child->cstatus = 1;
390 DB (DB_JOBS, (_("BUILTIN [%s][%s]\n"), cmd, cmd+8));
392 p = cmd + 8;
394 if ((*(p) == 'c')
395 && (*(p+1) == 'd')
396 && ((*(p+2) == ' ') || (*(p+2) == '\t')))
398 p += 3;
399 while ((*p == ' ') || (*p == '\t'))
400 p++;
401 DB (DB_JOBS, (_("BUILTIN CD %s\n"), p));
402 if (chdir (p))
403 return 0;
404 else
405 return 1;
407 else if ((*(p) == 'r')
408 && (*(p+1) == 'm')
409 && ((*(p+2) == ' ') || (*(p+2) == '\t')))
411 int in_arg;
413 /* rm */
414 p += 3;
415 while ((*p == ' ') || (*p == '\t'))
416 p++;
417 in_arg = 1;
419 DB (DB_JOBS, (_("BUILTIN RM %s\n"), p));
420 while (*p)
422 switch (*p)
424 case ' ':
425 case '\t':
426 if (in_arg)
428 *p++ = ';';
429 in_arg = 0;
431 break;
432 default:
433 break;
435 p++;
438 else
440 printf(_("Unknown builtin command '%s'\n"), cmd);
441 fflush(stdout);
442 return 0;
446 /* Create a *.com file if either the command is too long for
447 lib$spawn, or the command contains a newline, or if redirection
448 is desired. Forcing commands with newlines into DCLs allows to
449 store search lists on user mode logicals. */
451 if (strlen (cmd) > MAXCMDLEN
452 || (have_redirection != 0)
453 || (have_newline != 0))
455 FILE *outfile;
456 char c;
457 char *sep;
458 int alevel = 0; /* apostrophe level */
460 if (strlen (cmd) == 0)
462 printf (_("Error, empty command\n"));
463 fflush (stdout);
464 return 0;
467 outfile = open_tmpfile (&comname, "sys$scratch:CMDXXXXXX.COM");
468 if (outfile == 0)
469 pfatal_with_name (_("fopen (temporary file)"));
471 if (ifile[0])
473 fprintf (outfile, "$ assign/user %s sys$input\n", ifile);
474 DB (DB_JOBS, (_("Redirected input from %s\n"), ifile));
475 ifiledsc.dsc$w_length = 0;
478 if (efile[0])
480 fprintf (outfile, "$ define sys$error %s\n", efile);
481 DB (DB_JOBS, (_("Redirected error to %s\n"), efile));
482 efiledsc.dsc$w_length = 0;
485 if (ofile[0])
487 fprintf (outfile, "$ define sys$output %s\n", ofile);
488 DB (DB_JOBS, (_("Redirected output to %s\n"), ofile));
489 ofiledsc.dsc$w_length = 0;
492 p = sep = q = cmd;
493 for (c = '\n'; c; c = *q++)
495 switch (c)
497 case '\n':
498 /* At a newline, skip any whitespace around a leading $
499 from the command and issue exactly one $ into the DCL. */
500 while (isspace ((unsigned char)*p))
501 p++;
502 if (*p == '$')
503 p++;
504 while (isspace ((unsigned char)*p))
505 p++;
506 fwrite (p, 1, q - p, outfile);
507 fputc ('$', outfile);
508 fputc (' ', outfile);
509 /* Reset variables. */
510 p = sep = q;
511 break;
513 /* Nice places for line breaks are after strings, after
514 comma or space and before slash. */
515 case '"':
516 q = vms_handle_apos (q);
517 sep = q;
518 break;
519 case ',':
520 case ' ':
521 sep = q;
522 break;
523 case '/':
524 case '\0':
525 sep = q - 1;
526 break;
527 default:
528 break;
530 if (sep - p > 78)
532 /* Enough stuff for a line. */
533 fwrite (p, 1, sep - p, outfile);
534 p = sep;
535 if (*sep)
537 /* The command continues. */
538 fputc ('-', outfile);
540 fputc ('\n', outfile);
544 fwrite (p, 1, q - p, outfile);
545 fputc ('\n', outfile);
547 fclose (outfile);
549 sprintf (cmd, "$ @%s", comname);
551 DB (DB_JOBS, (_("Executing %s instead\n"), cmd));
554 cmddsc.dsc$w_length = strlen(cmd);
555 cmddsc.dsc$a_pointer = cmd;
556 cmddsc.dsc$b_dtype = DSC$K_DTYPE_T;
557 cmddsc.dsc$b_class = DSC$K_CLASS_S;
559 child->efn = 0;
560 while (child->efn < 32 || child->efn > 63)
562 status = lib$get_ef ((unsigned long *)&child->efn);
563 if (!(status & 1))
564 return 0;
567 sys$clref (child->efn);
569 vms_jobsefnmask |= (1 << (child->efn - 32));
572 LIB$SPAWN [command-string]
573 [,input-file]
574 [,output-file]
575 [,flags]
576 [,process-name]
577 [,process-id] [,completion-status-address] [,byte-integer-event-flag-num]
578 [,AST-address] [,varying-AST-argument]
579 [,prompt-string] [,cli] [,table]
582 #ifndef DONTWAITFORCHILD
584 * Code to make ctrl+c and ctrl+y working.
585 * The problem starts with the synchronous case where after lib$spawn is
586 * called any input will go to the child. But with input re-directed,
587 * both control characters won't make it to any of the programs, neither
588 * the spawning nor to the spawned one. Hence the caller needs to spawn
589 * with CLI$M_NOWAIT to NOT give up the input focus. A sys$waitfr
590 * has to follow to simulate the wanted synchronous behaviour.
591 * The next problem is ctrl+y which isn't caught by the crtl and
592 * therefore isn't converted to SIGQUIT (for a signal handler which is
593 * already established). The only way to catch ctrl+y, is an AST
594 * assigned to the input channel. But ctrl+y handling of DCL needs to be
595 * disabled, otherwise it will handle it. Not to mention the previous
596 * ctrl+y handling of DCL needs to be re-established before make exits.
597 * One more: At the time of LIB$SPAWN signals are blocked. SIGQUIT will
598 * make it to the signal handler after the child "normally" terminates.
599 * This isn't enough. It seems reasonable for simple command lines like
600 * a 'cc foobar.c' spawned in a subprocess but it is unacceptable for
601 * spawning make. Therefore we need to abort the process in the AST.
603 * Prior to the spawn it is checked if an AST is already set up for
604 * ctrl+y, if not one is set up for a channel to SYS$COMMAND. In general
605 * this will work except if make is run in a batch environment, but there
606 * nobody can press ctrl+y. During the setup the DCL handling of ctrl+y
607 * is disabled and an exit handler is established to re-enable it.
608 * If the user interrupts with ctrl+y, the assigned AST will fire, force
609 * an abort to the subprocess and signal SIGQUIT, which will be caught by
610 * the already established handler and will bring us back to common code.
611 * After the spawn (now /nowait) a sys$waitfr simulates the /wait and
612 * enables the ctrl+y be delivered to this code. And the ctrl+c too,
613 * which the crtl converts to SIGINT and which is caught by the common
614 * signal handler. Because signals were blocked before entering this code
615 * sys$waitfr will always complete and the SIGQUIT will be processed after
616 * it (after termination of the current block, somewhere in common code).
617 * And SIGINT too will be delayed. That is ctrl+c can only abort when the
618 * current command completes. Anyway it's better than nothing :-)
621 if (!setupYAstTried)
622 tryToSetupYAst();
623 status = lib$spawn (&cmddsc, /* cmd-string */
624 (ifiledsc.dsc$w_length == 0)?0:&ifiledsc, /* input-file */
625 (ofiledsc.dsc$w_length == 0)?0:&ofiledsc, /* output-file */
626 &spflags, /* flags */
627 &pnamedsc, /* proc name */
628 &child->pid, &child->cstatus, &child->efn,
629 0, 0,
630 0, 0, 0);
631 if (status & 1)
633 pidToAbort= child->pid;
634 status= sys$waitfr (child->efn);
635 pidToAbort= 0;
636 vmsHandleChildTerm(child);
638 #else
639 status = lib$spawn (&cmddsc,
640 (ifiledsc.dsc$w_length == 0)?0:&ifiledsc,
641 (ofiledsc.dsc$w_length == 0)?0:&ofiledsc,
642 &spflags,
643 &pnamedsc,
644 &child->pid, &child->cstatus, &child->efn,
645 vmsHandleChildTerm, child,
646 0, 0, 0);
647 #endif
649 if (!(status & 1))
651 printf (_("Error spawning, %d\n") ,status);
652 fflush (stdout);
653 switch (status)
655 case 0x1c:
656 errno = EPROCLIM;
657 break;
658 default:
659 errno = EFAIL;
663 if (comname && !ISDB (DB_JOBS))
664 unlink (comname);
666 return (status & 1);