xref: /original-bsd/usr.bin/make/job.c (revision 2ca53284)
1 /*
2  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3  * Copyright (c) 1988, 1989 by Adam de Boor
4  * Copyright (c) 1989 by Berkeley Softworks
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * %sccs.include.redist.c%
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)job.c	5.14 (Berkeley) 01/10/91";
15 #endif /* not lint */
16 
17 /*-
18  * job.c --
19  *	handle the creation etc. of our child processes.
20  *
21  * Interface:
22  *	Job_Make  	    	Start the creation of the given target.
23  *
24  *	Job_CatchChildren   	Check for and handle the termination of any
25  *	    	  	    	children. This must be called reasonably
26  *	    	  	    	frequently to keep the whole make going at
27  *	    	  	    	a decent clip, since job table entries aren't
28  *	    	  	    	removed until their process is caught this way.
29  *	    	  	    	Its single argument is TRUE if the function
30  *	    	  	    	should block waiting for a child to terminate.
31  *
32  *	Job_CatchOutput	    	Print any output our children have produced.
33  *	    	  	    	Should also be called fairly frequently to
34  *	    	  	    	keep the user informed of what's going on.
35  *	    	  	    	If no output is waiting, it will block for
36  *	    	  	    	a time given by the SEL_* constants, below,
37  *	    	  	    	or until output is ready.
38  *
39  *	Job_Init  	    	Called to intialize this module. in addition,
40  *	    	  	    	any commands attached to the .BEGIN target
41  *	    	  	    	are executed before this function returns.
42  *	    	  	    	Hence, the makefile must have been parsed
43  *	    	  	    	before this function is called.
44  *
45  *	Job_Full  	    	Return TRUE if the job table is filled.
46  *
47  *	Job_Empty 	    	Return TRUE if the job table is completely
48  *	    	  	    	empty.
49  *
50  *	Job_ParseShell	    	Given the line following a .SHELL target, parse
51  *	    	  	    	the line as a shell specification. Returns
52  *	    	  	    	FAILURE if the spec was incorrect.
53  *
54  *	Job_End	  	    	Perform any final processing which needs doing.
55  *	    	  	    	This includes the execution of any commands
56  *	    	  	    	which have been/were attached to the .END
57  *	    	  	    	target. It should only be called when the
58  *	    	  	    	job table is empty.
59  *
60  *	Job_AbortAll	    	Abort all currently running jobs. It doesn't
61  *	    	  	    	handle output or do anything for the jobs,
62  *	    	  	    	just kills them. It should only be called in
63  *	    	  	    	an emergency, as it were.
64  *
65  *	Job_CheckCommands   	Verify that the commands for a target are
66  *	    	  	    	ok. Provide them if necessary and possible.
67  *
68  *	Job_Touch 	    	Update a target without really updating it.
69  *
70  *	Job_Wait  	    	Wait for all currently-running jobs to finish.
71  */
72 
73 #include "make.h"
74 #include <sys/signal.h>
75 #include <sys/stat.h>
76 #include <sys/file.h>
77 #include <sys/time.h>
78 #include <sys/wait.h>
79 #include <fcntl.h>
80 #include <errno.h>
81 #include <stdio.h>
82 #include <string.h>
83 #include "job.h"
84 #include "pathnames.h"
85 
86 extern int  errno;
87 
88 /*
89  * error handling variables
90  */
91 int         	errors = 0;	    /* number of errors reported */
92 int  	    	aborting = 0;	    /* why is the make aborting? */
93 #define ABORT_ERROR	1   	    /* Because of an error */
94 #define ABORT_INTERRUPT	2   	    /* Because it was interrupted */
95 #define ABORT_WAIT	3   	    /* Waiting for jobs to finish */
96 
97 
98 /*
99  * post-make command processing. The node postCommands is really just the
100  * .END target but we keep it around to avoid having to search for it
101  * all the time.
102  */
103 static GNode   	  *postCommands;    /* node containing commands to execute when
104 				     * everything else is done */
105 static int     	  numCommands; 	    /* The number of commands actually printed
106 				     * for a target. Should this number be
107 				     * 0, no shell will be executed. */
108 
109 
110 /*
111  * Return values from JobStart.
112  */
113 #define JOB_RUNNING	0   	/* Job is running */
114 #define JOB_ERROR 	1   	/* Error in starting the job */
115 #define JOB_FINISHED	2   	/* The job is already finished */
116 #define JOB_STOPPED	3   	/* The job is stopped */
117 
118 /*
119  * tfile is the name of a file into which all shell commands are put. It is
120  * used over by removing it before the child shell is executed. The XXXXX in
121  * the string are replaced by the pid of the make process in a 5-character
122  * field with leading zeroes.
123  */
124 static char     tfile[] = TMPPAT;
125 
126 
127 /*
128  * Descriptions for various shells.
129  */
130 static Shell    shells[] = {
131     /*
132      * CSH description. The csh can do echo control by playing
133      * with the setting of the 'echo' shell variable. Sadly,
134      * however, it is unable to do error control nicely.
135      */
136 {
137     "csh",
138     TRUE, "unset verbose", "set verbose", "unset verbose", 10,
139     FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"",
140     "v", "e",
141 },
142     /*
143      * SH description. Echo control is also possible and, under
144      * sun UNIX anyway, one can even control error checking.
145      */
146 {
147     "sh",
148     TRUE, "set -", "set -v", "set -", 5,
149     FALSE, "echo \"%s\"\n", "sh -c '%s || exit 0'\n",
150     "v", "e",
151 },
152     /*
153      * UNKNOWN.
154      */
155 {
156     (char *)0,
157     FALSE, (char *)0, (char *)0, (char *)0, 0,
158     FALSE, (char *)0, (char *)0,
159     (char *)0, (char *)0,
160 }
161 };
162 Shell       	*commandShell = &shells[DEFSHELL]; /* this is the shell to
163 						   * which we pass all
164 						   * commands in the Makefile.
165 						   * It is set by the
166 						   * Job_ParseShell function */
167 char        	*shellPath = (char *) NULL,	  /* full pathname of
168 						   * executable image */
169                	*shellName;	      	      	  /* last component of shell */
170 
171 
172 static int  	maxJobs;    	/* The most children we can run at once */
173 static int  	maxLocal;    	/* The most local ones we can have */
174 int  	    	nJobs;	    	/* The number of children currently running */
175 int  	    	nLocal;    	/* The number of local children */
176 Lst  	    	jobs;		/* The structures that describe them */
177 Boolean	    	jobFull;    	/* Flag to tell when the job table is full. It
178 				 * is set TRUE when (1) the total number of
179 				 * running jobs equals the maximum allowed or
180 				 * (2) a job can only be run locally, but
181 				 * nLocal equals maxLocal */
182 #ifndef RMT_WILL_WATCH
183 static fd_set  	outputs;    	/* Set of descriptors of pipes connected to
184 				 * the output channels of children */
185 #endif
186 
187 GNode 	    	*lastNode;	/* The node for which output was most recently
188 				 * produced. */
189 char 	    	*targFmt;   	/* Format string to use to head output from a
190 				 * job when it's not the most-recent job heard
191 				 * from */
192 #define TARG_FMT  "--- %s ---\n" /* Default format */
193 
194 /*
195  * When JobStart attempts to run a job remotely but can't, and isn't allowed
196  * to run the job locally, or when Job_CatchChildren detects a job that has
197  * been migrated home, the job is placed on the stoppedJobs queue to be run
198  * when the next job finishes.
199  */
200 Lst  	    stoppedJobs;	/* Lst of Job structures describing
201 				 * jobs that were stopped due to concurrency
202 				 * limits or migration home */
203 
204 
205 # if defined(USE_PGRP)
206 #define KILL(pid,sig)	killpg((pid),(sig))
207 # else
208 #define KILL(pid,sig)	kill((pid),(sig))
209 # endif
210 
211 static void JobRestart();
212 static int  JobStart();
213 static void JobInterrupt();
214 
215 /*-
216  *-----------------------------------------------------------------------
217  * JobCondPassSig --
218  *	Pass a signal to a job if the job is remote or if USE_PGRP
219  *	is defined.
220  *
221  * Results:
222  *	=== 0
223  *
224  * Side Effects:
225  *	None, except the job may bite it.
226  *
227  *-----------------------------------------------------------------------
228  */
229 static int
230 JobCondPassSig(job, signo)
231     Job	    	*job;	    /* Job to biff */
232     int	    	signo;	    /* Signal to send it */
233 {
234 #ifdef RMT_WANTS_SIGNALS
235     if (job->flags & JOB_REMOTE) {
236 	(void)Rmt_Signal(job, signo);
237     } else {
238 	KILL(job->pid, signo);
239     }
240 #else
241     /*
242      * Assume that sending the signal to job->pid will signal any remote
243      * job as well.
244      */
245     KILL(job->pid, signo);
246 #endif
247     return(0);
248 }
249 
250 /*-
251  *-----------------------------------------------------------------------
252  * JobPassSig --
253  *	Pass a signal on to all remote jobs and to all local jobs if
254  *	USE_PGRP is defined, then die ourselves.
255  *
256  * Results:
257  *	None.
258  *
259  * Side Effects:
260  *	We die by the same signal.
261  *
262  *-----------------------------------------------------------------------
263  */
264 static void
265 JobPassSig(signo)
266     int	    signo;	/* The signal number we've received */
267 {
268     int	    mask;
269 
270     Lst_ForEach(jobs, JobCondPassSig, (ClientData)signo);
271 
272     /*
273      * Deal with proper cleanup based on the signal received. We only run
274      * the .INTERRUPT target if the signal was in fact an interrupt. The other
275      * three termination signals are more of a "get out *now*" command.
276      */
277     if (signo == SIGINT) {
278 	JobInterrupt(TRUE);
279     } else if ((signo == SIGHUP) || (signo == SIGTERM) || (signo == SIGQUIT)) {
280 	JobInterrupt(FALSE);
281     }
282 
283     /*
284      * Leave gracefully if SIGQUIT, rather than core dumping.
285      */
286     if (signo == SIGQUIT) {
287 	Finish();
288     }
289 
290     /*
291      * Send ourselves the signal now we've given the message to everyone else.
292      * Note we block everything else possible while we're getting the signal.
293      * This ensures that all our jobs get continued when we wake up before
294      * we take any other signal.
295      */
296     mask = sigblock(0);
297     (void) sigsetmask(~0 & ~(1 << (signo-1)));
298     signal(signo, SIG_DFL);
299 
300     kill(getpid(), signo);
301 
302     Lst_ForEach(jobs, JobCondPassSig, (ClientData)SIGCONT);
303 
304     sigsetmask(mask);
305     signal(signo, JobPassSig);
306 
307 }
308 
309 /*-
310  *-----------------------------------------------------------------------
311  * JobCmpPid  --
312  *	Compare the pid of the job with the given pid and return 0 if they
313  *	are equal. This function is called from Job_CatchChildren via
314  *	Lst_Find to find the job descriptor of the finished job.
315  *
316  * Results:
317  *	0 if the pid's match
318  *
319  * Side Effects:
320  *	None
321  *-----------------------------------------------------------------------
322  */
323 static int
324 JobCmpPid (job, pid)
325     int             pid;	/* process id desired */
326     Job            *job;	/* job to examine */
327 {
328     return (pid - job->pid);
329 }
330 
331 /*-
332  *-----------------------------------------------------------------------
333  * JobPrintCommand  --
334  *	Put out another command for the given job. If the command starts
335  *	with an @ or a - we process it specially. In the former case,
336  *	so long as the -s and -n flags weren't given to make, we stick
337  *	a shell-specific echoOff command in the script. In the latter,
338  *	we ignore errors for the entire job, unless the shell has error
339  *	control.
340  *	If the command is just "..." we take all future commands for this
341  *	job to be commands to be executed once the entire graph has been
342  *	made and return non-zero to signal that the end of the commands
343  *	was reached. These commands are later attached to the postCommands
344  *	node and executed by Job_End when all things are done.
345  *	This function is called from JobStart via Lst_ForEach.
346  *
347  * Results:
348  *	Always 0, unless the command was "..."
349  *
350  * Side Effects:
351  *	If the command begins with a '-' and the shell has no error control,
352  *	the JOB_IGNERR flag is set in the job descriptor.
353  *	If the command is "..." and we're not ignoring such things,
354  *	tailCmds is set to the successor node of the cmd.
355  *	numCommands is incremented if the command is actually printed.
356  *-----------------------------------------------------------------------
357  */
358 static int
359 JobPrintCommand (cmd, job)
360     char     	  *cmd;	    	    /* command string to print */
361     Job           *job;	    	    /* job for which to print it */
362 {
363     Boolean	  noSpecials;	    /* true if we shouldn't worry about
364 				     * inserting special commands into
365 				     * the input stream. */
366     Boolean       shutUp = FALSE;   /* true if we put a no echo command
367 				     * into the command file */
368     Boolean	  errOff = FALSE;   /* true if we turned error checking
369 				     * off before printing the command
370 				     * and need to turn it back on */
371     char       	  *cmdTemplate;	    /* Template to use when printing the
372 				     * command */
373     char    	  *cmdStart;	    /* Start of expanded command */
374     LstNode 	  cmdNode;  	    /* Node for replacing the command */
375 
376     noSpecials = (noExecute && ! (job->node->type & OP_MAKE));
377 
378     if (strcmp (cmd, "...") == 0) {
379 	if ((job->flags & JOB_IGNDOTS) == 0) {
380 	    job->tailCmds = Lst_Succ (Lst_Member (job->node->commands,
381 						  (ClientData)cmd));
382 	    return (1);
383 	}
384 	return (0);
385     }
386 
387 #define DBPRINTF(fmt, arg) if (DEBUG(JOB)) printf (fmt, arg); fprintf (job->cmdFILE, fmt, arg)
388 
389     numCommands += 1;
390 
391     /*
392      * For debugging, we replace each command with the result of expanding
393      * the variables in the command.
394      */
395     cmdNode = Lst_Member (job->node->commands, (ClientData)cmd);
396     cmdStart = cmd = Var_Subst (cmd, job->node, FALSE);
397     Lst_Replace (cmdNode, (ClientData)cmdStart);
398 
399     cmdTemplate = "%s\n";
400 
401     /*
402      * Check for leading @' and -'s to control echoing and error checking.
403      */
404     while (*cmd == '@' || *cmd == '-') {
405 	if (*cmd == '@') {
406 	    shutUp = TRUE;
407 	} else {
408 	    errOff = TRUE;
409 	}
410 	cmd++;
411     }
412 
413     if (shutUp) {
414 	if (! (job->flags & JOB_SILENT) && !noSpecials &&
415 	    commandShell->hasEchoCtl) {
416 		DBPRINTF ("%s\n", commandShell->echoOff);
417 	} else {
418 	    shutUp = FALSE;
419 	}
420     }
421 
422     if (errOff) {
423 	if ( ! (job->flags & JOB_IGNERR) && !noSpecials) {
424 	    if (commandShell->hasErrCtl) {
425 		/*
426 		 * we don't want the error-control commands showing
427 		 * up either, so we turn off echoing while executing
428 		 * them. We could put another field in the shell
429 		 * structure to tell JobDoOutput to look for this
430 		 * string too, but why make it any more complex than
431 		 * it already is?
432 		 */
433 		if (! (job->flags & JOB_SILENT) && !shutUp &&
434 		    commandShell->hasEchoCtl) {
435 			DBPRINTF ("%s\n", commandShell->echoOff);
436 			DBPRINTF ("%s\n", commandShell->ignErr);
437 			DBPRINTF ("%s\n", commandShell->echoOn);
438 		} else {
439 		    DBPRINTF ("%s\n", commandShell->ignErr);
440 		}
441 	    } else if (commandShell->ignErr &&
442 		       (*commandShell->ignErr != '\0'))
443 	    {
444 		/*
445 		 * The shell has no error control, so we need to be
446 		 * weird to get it to ignore any errors from the command.
447 		 * If echoing is turned on, we turn it off and use the
448 		 * errCheck template to echo the command. Leave echoing
449 		 * off so the user doesn't see the weirdness we go through
450 		 * to ignore errors. Set cmdTemplate to use the weirdness
451 		 * instead of the simple "%s\n" template.
452 		 */
453 		if (! (job->flags & JOB_SILENT) && !shutUp &&
454 		    commandShell->hasEchoCtl) {
455 			DBPRINTF ("%s\n", commandShell->echoOff);
456 			DBPRINTF (commandShell->errCheck, cmd);
457 			shutUp = TRUE;
458 		}
459 		cmdTemplate = commandShell->ignErr;
460 		/*
461 		 * The error ignoration (hee hee) is already taken care
462 		 * of by the ignErr template, so pretend error checking
463 		 * is still on.
464 		 */
465 		errOff = FALSE;
466 	    } else {
467 		errOff = FALSE;
468 	    }
469 	} else {
470 	    errOff = FALSE;
471 	}
472     }
473 
474     DBPRINTF (cmdTemplate, cmd);
475 
476     if (errOff) {
477 	/*
478 	 * If echoing is already off, there's no point in issuing the
479 	 * echoOff command. Otherwise we issue it and pretend it was on
480 	 * for the whole command...
481 	 */
482 	if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl){
483 	    DBPRINTF ("%s\n", commandShell->echoOff);
484 	    shutUp = TRUE;
485 	}
486 	DBPRINTF ("%s\n", commandShell->errCheck);
487     }
488     if (shutUp) {
489 	DBPRINTF ("%s\n", commandShell->echoOn);
490     }
491     return (0);
492 }
493 
494 /*-
495  *-----------------------------------------------------------------------
496  * JobSaveCommand --
497  *	Save a command to be executed when everything else is done.
498  *	Callback function for JobFinish...
499  *
500  * Results:
501  *	Always returns 0
502  *
503  * Side Effects:
504  *	The command is tacked onto the end of postCommands's commands list.
505  *
506  *-----------------------------------------------------------------------
507  */
508 static int
509 JobSaveCommand (cmd, gn)
510     char    *cmd;
511     GNode   *gn;
512 {
513     cmd = Var_Subst (cmd, gn, FALSE);
514     (void)Lst_AtEnd (postCommands->commands, (ClientData)cmd);
515     return (0);
516 }
517 
518 /*-
519  *-----------------------------------------------------------------------
520  * JobFinish  --
521  *	Do final processing for the given job including updating
522  *	parents and starting new jobs as available/necessary. Note
523  *	that we pay no attention to the JOB_IGNERR flag here.
524  *	This is because when we're called because of a noexecute flag
525  *	or something, jstat.w_status is 0 and when called from
526  *	Job_CatchChildren, the status is zeroed if it s/b ignored.
527  *
528  * Results:
529  *	None
530  *
531  * Side Effects:
532  *	Some nodes may be put on the toBeMade queue.
533  *	Final commands for the job are placed on postCommands.
534  *
535  *	If we got an error and are aborting (aborting == ABORT_ERROR) and
536  *	the job list is now empty, we are done for the day.
537  *	If we recognized an error (errors !=0), we set the aborting flag
538  *	to ABORT_ERROR so no more jobs will be started.
539  *-----------------------------------------------------------------------
540  */
541 /*ARGSUSED*/
542 void
543 JobFinish (job, status)
544     Job           *job;	      	  /* job to finish */
545     union wait	  status;     	  /* sub-why job went away */
546 {
547     Boolean 	  done;
548 
549     if ((WIFEXITED(status) &&
550 	  (((status.w_retcode != 0) && !(job->flags & JOB_IGNERR)))) ||
551 	(WIFSIGNALED(status) && (status.w_termsig != SIGCONT)))
552     {
553 	/*
554 	 * If it exited non-zero and either we're doing things our
555 	 * way or we're not ignoring errors, the job is finished.
556 	 * Similarly, if the shell died because of a signal
557 	 * the job is also finished. In these
558 	 * cases, finish out the job's output before printing the exit
559 	 * status...
560 	 */
561 	if (usePipes) {
562 #ifdef RMT_WILL_WATCH
563 	    Rmt_Ignore(job->inPipe);
564 #else
565 	    FD_CLR(job->inPipe, &outputs);
566 #endif /* RMT_WILL_WATCH */
567 	    if (job->outPipe != job->inPipe) {
568 		(void)close (job->outPipe);
569 	    }
570 	    JobDoOutput (job, TRUE);
571 	    (void)close (job->inPipe);
572 	} else {
573 	    (void)close (job->outFd);
574 	    JobDoOutput (job, TRUE);
575 	}
576 
577 	if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
578 	    fclose(job->cmdFILE);
579 	}
580 	done = TRUE;
581     } else if (WIFEXITED(status) && status.w_retcode != 0) {
582 	/*
583 	 * Deal with ignored errors in -B mode. We need to print a message
584 	 * telling of the ignored error as well as setting status.w_status
585 	 * to 0 so the next command gets run. To do this, we set done to be
586 	 * TRUE if in -B mode and the job exited non-zero. Note we don't
587 	 * want to close down any of the streams until we know we're at the
588 	 * end.
589 	 */
590 	done = TRUE;
591     } else {
592 	/*
593 	 * No need to close things down or anything.
594 	 */
595 	done = FALSE;
596     }
597 
598     if (done ||
599 	WIFSTOPPED(status) ||
600 	(WIFSIGNALED(status) && (status.w_termsig == SIGCONT)) ||
601 	DEBUG(JOB))
602     {
603 	FILE	  *out;
604 
605 	if (!usePipes && (job->flags & JOB_IGNERR)) {
606 	    /*
607 	     * If output is going to a file and this job is ignoring
608 	     * errors, arrange to have the exit status sent to the
609 	     * output file as well.
610 	     */
611 	    out = fdopen (job->outFd, "w");
612 	} else {
613 	    out = stdout;
614 	}
615 
616 	if (WIFEXITED(status)) {
617 	    if (status.w_retcode != 0) {
618 		if (usePipes && job->node != lastNode) {
619 		    fprintf (out, targFmt, job->node->name);
620 		    lastNode = job->node;
621 		}
622 		fprintf (out, "*** Error code %d%s\n", status.w_retcode,
623 			 (job->flags & JOB_IGNERR) ? " (ignored)" : "");
624 
625 		if (job->flags & JOB_IGNERR) {
626 		    status.w_status = 0;
627 		}
628 	    } else if (DEBUG(JOB)) {
629 		if (usePipes && job->node != lastNode) {
630 		    fprintf (out, targFmt, job->node->name);
631 		    lastNode = job->node;
632 		}
633 		fprintf (out, "*** Completed successfully\n");
634 	    }
635 	} else if (WIFSTOPPED(status)) {
636 	    if (usePipes && job->node != lastNode) {
637 		fprintf (out, targFmt, job->node->name);
638 		lastNode = job->node;
639 	    }
640 	    if (! (job->flags & JOB_REMIGRATE)) {
641 		fprintf (out, "*** Stopped -- signal %d\n", status.w_stopsig);
642 	    }
643 	    job->flags |= JOB_RESUME;
644 	    (void)Lst_AtEnd(stoppedJobs, (ClientData)job);
645 	    fflush(out);
646 	    return;
647 	} else if (status.w_termsig == SIGCONT) {
648 	    /*
649 	     * If the beastie has continued, shift the Job from the stopped
650 	     * list to the running one (or re-stop it if concurrency is
651 	     * exceeded) and go and get another child.
652 	     */
653 	    if (job->flags & (JOB_RESUME|JOB_REMIGRATE|JOB_RESTART)) {
654 		if (usePipes && job->node != lastNode) {
655 		    fprintf (out, targFmt, job->node->name);
656 		    lastNode = job->node;
657 		}
658 		fprintf (out, "*** Continued\n");
659 	    }
660 	    if (! (job->flags & JOB_CONTINUING)) {
661 		JobRestart(job);
662 	    } else {
663 		Lst_AtEnd(jobs, (ClientData)job);
664 		nJobs += 1;
665 		if (! (job->flags & JOB_REMOTE)) {
666 		    nLocal += 1;
667 		}
668 		if (nJobs == maxJobs) {
669 		    jobFull = TRUE;
670 		    if (DEBUG(JOB)) {
671 			printf("Job queue is full.\n");
672 		    }
673 		}
674 	    }
675 	    fflush(out);
676 	    return;
677 	} else {
678 	    if (usePipes && job->node != lastNode) {
679 		fprintf (out, targFmt, job->node->name);
680 		lastNode = job->node;
681 	    }
682 	    fprintf (out, "*** Signal %d\n", status.w_termsig);
683 	}
684 
685 	fflush (out);
686     }
687 
688     /*
689      * Now handle the -B-mode stuff. If the beast still isn't finished,
690      * try and restart the job on the next command. If JobStart says it's
691      * ok, it's ok. If there's an error, this puppy is done.
692      */
693     if ((status.w_status == 0) &&
694 	!Lst_IsAtEnd (job->node->commands))
695     {
696 	switch (JobStart (job->node,
697 			  job->flags & JOB_IGNDOTS,
698 			  job))
699 	{
700 	    case JOB_RUNNING:
701 		done = FALSE;
702 		break;
703 	    case JOB_ERROR:
704 		done = TRUE;
705 		status.w_retcode = 1;
706 		break;
707 	    case JOB_FINISHED:
708 		/*
709 		 * If we got back a JOB_FINISHED code, JobStart has already
710 		 * called Make_Update and freed the job descriptor. We set
711 		 * done to false here to avoid fake cycles and double frees.
712 		 * JobStart needs to do the update so we can proceed up the
713 		 * graph when given the -n flag..
714 		 */
715 		done = FALSE;
716 		break;
717 	}
718     } else {
719 	done = TRUE;
720     }
721 
722 
723     if (done &&
724 	(aborting != ABORT_ERROR) &&
725 	(aborting != ABORT_INTERRUPT) &&
726 	(status.w_status == 0))
727     {
728 	/*
729 	 * As long as we aren't aborting and the job didn't return a non-zero
730 	 * status that we shouldn't ignore, we call Make_Update to update
731 	 * the parents. In addition, any saved commands for the node are placed
732 	 * on the .END target.
733 	 */
734 	if (job->tailCmds != NILLNODE) {
735 	    Lst_ForEachFrom (job->node->commands, job->tailCmds,
736 			     JobSaveCommand,
737 			     (ClientData)job->node);
738 	}
739 	job->node->made = MADE;
740 	Make_Update (job->node);
741 	free((Address)job);
742     } else if (status.w_status) {
743 	errors += 1;
744 	free((Address)job);
745     }
746 
747     while (!errors && !jobFull && !Lst_IsEmpty(stoppedJobs)) {
748 	JobRestart((Job *)Lst_DeQueue(stoppedJobs));
749     }
750 
751     /*
752      * Set aborting if any error.
753      */
754     if (errors && !keepgoing && (aborting != ABORT_INTERRUPT)) {
755 	/*
756 	 * If we found any errors in this batch of children and the -k flag
757 	 * wasn't given, we set the aborting flag so no more jobs get
758 	 * started.
759 	 */
760 	aborting = ABORT_ERROR;
761     }
762 
763     if ((aborting == ABORT_ERROR) && Job_Empty()) {
764 	/*
765 	 * If we are aborting and the job table is now empty, we finish.
766 	 */
767 	(void) unlink (tfile);
768 	Finish (errors);
769     }
770 }
771 
772 /*-
773  *-----------------------------------------------------------------------
774  * Job_Touch --
775  *	Touch the given target. Called by JobStart when the -t flag was
776  *	given
777  *
778  * Results:
779  *	None
780  *
781  * Side Effects:
782  *	The data modification of the file is changed. In addition, if the
783  *	file did not exist, it is created.
784  *-----------------------------------------------------------------------
785  */
786 void
787 Job_Touch (gn, silent)
788     GNode         *gn;	      	/* the node of the file to touch */
789     Boolean 	  silent;   	/* TRUE if should not print messages */
790 {
791     int		  streamID;   	/* ID of stream opened to do the touch */
792     struct timeval times[2];	/* Times for utimes() call */
793     struct stat attr;        /* Attributes of the file */
794 
795     if (gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_OPTIONAL)) {
796 	/*
797 	 * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" targets
798 	 * and, as such, shouldn't really be created.
799 	 */
800 	return;
801     }
802 
803     if (!silent) {
804 	printf ("touch %s\n", gn->name);
805     }
806 
807     if (noExecute) {
808 	return;
809     }
810 
811     if (gn->type & OP_ARCHV) {
812 	Arch_Touch (gn);
813     } else if (gn->type & OP_LIB) {
814 	Arch_TouchLib (gn);
815     } else {
816 	char	*file = gn->path ? gn->path : gn->name;
817 
818 	times[0].tv_sec = times[1].tv_sec = now;
819 	times[0].tv_usec = times[1].tv_usec = 0;
820 	if (utimes(file, times) < 0){
821 	    streamID = open (file, O_RDWR | O_CREAT, 0666);
822 
823 	    if (streamID >= 0) {
824 		char	c;
825 
826 		/*
827 		 * Read and write a byte to the file to change the
828 		 * modification time, then close the file.
829 		 */
830 		if (read(streamID, &c, 1) == 1) {
831 		    lseek(streamID, 0L, L_SET);
832 		    write(streamID, &c, 1);
833 		}
834 
835 		(void)close (streamID);
836 	    } else
837 		printf("*** couldn't touch %s: %s", file, strerror(errno));
838 	}
839     }
840 }
841 
842 /*-
843  *-----------------------------------------------------------------------
844  * Job_CheckCommands --
845  *	Make sure the given node has all the commands it needs.
846  *
847  * Results:
848  *	TRUE if the commands list is/was ok.
849  *
850  * Side Effects:
851  *	The node will have commands from the .DEFAULT rule added to it
852  *	if it needs them.
853  *-----------------------------------------------------------------------
854  */
855 Boolean
856 Job_CheckCommands (gn, abortProc)
857     GNode          *gn;	    	    /* The target whose commands need
858 				     * verifying */
859     void    	  (*abortProc)();   /* Function to abort with message */
860 {
861     if (OP_NOP(gn->type) && Lst_IsEmpty (gn->commands) &&
862 	(gn->type & OP_LIB) == 0) {
863 	/*
864 	 * No commands. Look for .DEFAULT rule from which we might infer
865 	 * commands
866 	 */
867 	if ((DEFAULT != NILGNODE) && !Lst_IsEmpty(DEFAULT->commands)) {
868 	    /*
869 	     * Make only looks for a .DEFAULT if the node was never the
870 	     * target of an operator, so that's what we do too. If
871 	     * a .DEFAULT was given, we substitute its commands for gn's
872 	     * commands and set the IMPSRC variable to be the target's name
873 	     * The DEFAULT node acts like a transformation rule, in that
874 	     * gn also inherits any attributes or sources attached to
875 	     * .DEFAULT itself.
876 	     */
877 	    Make_HandleUse(DEFAULT, gn);
878 	    Var_Set (IMPSRC, Var_Value (TARGET, gn), gn);
879 	} else if (Dir_MTime (gn) == 0) {
880 	    /*
881 	     * The node wasn't the target of an operator we have no .DEFAULT
882 	     * rule to go on and the target doesn't already exist. There's
883 	     * nothing more we can do for this branch. If the -k flag wasn't
884 	     * given, we stop in our tracks, otherwise we just don't update
885 	     * this node's parents so they never get examined.
886 	     */
887 	    if (gn->type & OP_OPTIONAL) {
888 		printf ("make: don't know how to make %s (ignored)\n",
889 			gn->name);
890 	    } else if (keepgoing) {
891 		printf ("make: don't know how to make %s (continuing)\n",
892 			gn->name);
893 		return (FALSE);
894 	    } else {
895 		(*abortProc) ("make: don't know how to make %s. Stop",
896 			     gn->name);
897 		return(FALSE);
898 	    }
899 	}
900     }
901     return (TRUE);
902 }
903 #ifdef RMT_WILL_WATCH
904 /*-
905  *-----------------------------------------------------------------------
906  * JobLocalInput --
907  *	Handle a pipe becoming readable. Callback function for Rmt_Watch
908  *
909  * Results:
910  *	None
911  *
912  * Side Effects:
913  *	JobDoOutput is called.
914  *
915  *-----------------------------------------------------------------------
916  */
917 /*ARGSUSED*/
918 static void
919 JobLocalInput(stream, job)
920     int	    stream; 	/* Stream that's ready (ignored) */
921     Job	    *job;   	/* Job to which the stream belongs */
922 {
923     JobDoOutput(job, FALSE);
924 }
925 #endif /* RMT_WILL_WATCH */
926 
927 /*-
928  *-----------------------------------------------------------------------
929  * JobExec --
930  *	Execute the shell for the given job. Called from JobStart and
931  *	JobRestart.
932  *
933  * Results:
934  *	None.
935  *
936  * Side Effects:
937  *	A shell is executed, outputs is altered and the Job structure added
938  *	to the job table.
939  *
940  *-----------------------------------------------------------------------
941  */
942 static void
943 JobExec(job, argv)
944     Job	    	  *job; 	/* Job to execute */
945     char    	  **argv;
946 {
947     int	    	  cpid;	    	/* ID of new child */
948 
949     if (DEBUG(JOB)) {
950 	int 	  i;
951 
952 	printf("Running %s %sly\n", job->node->name,
953 	       job->flags&JOB_REMOTE?"remote":"local");
954 	printf("\tCommand: ");
955 	for (i = 0; argv[i] != (char *)NULL; i++) {
956 	    printf("%s ", argv[i]);
957 	}
958 	printf("\n");
959     }
960 
961     /*
962      * Some jobs produce no output and it's disconcerting to have
963      * no feedback of their running (since they produce no output, the
964      * banner with their name in it never appears). This is an attempt to
965      * provide that feedback, even if nothing follows it.
966      */
967     if ((lastNode != job->node) && (job->flags & JOB_FIRST) &&
968 	!(job->flags & JOB_SILENT))
969     {
970 	printf(targFmt, job->node->name);
971 	lastNode = job->node;
972     }
973 
974 #ifdef RMT_NO_EXEC
975     if (job->flags & JOB_REMOTE) {
976 	goto jobExecFinish;
977     }
978 #endif /* RMT_NO_EXEC */
979 
980     if ((cpid =  vfork()) == -1) {
981 	Punt ("Cannot fork");
982     } else if (cpid == 0) {
983 
984 	/*
985 	 * Must duplicate the input stream down to the child's input and
986 	 * reset it to the beginning (again). Since the stream was marked
987 	 * close-on-exec, we must clear that bit in the new input.
988 	 */
989 	(void) dup2(fileno(job->cmdFILE), 0);
990 	fcntl(0, F_SETFD, 0);
991 	lseek(0, 0, L_SET);
992 
993 	if (usePipes) {
994 	    /*
995 	     * Set up the child's output to be routed through the pipe
996 	     * we've created for it.
997 	     */
998 	    (void) dup2 (job->outPipe, 1);
999 	} else {
1000 	    /*
1001 	     * We're capturing output in a file, so we duplicate the
1002 	     * descriptor to the temporary file into the standard
1003 	     * output.
1004 	     */
1005 	    (void) dup2 (job->outFd, 1);
1006 	}
1007 	/*
1008 	 * The output channels are marked close on exec. This bit was
1009 	 * duplicated by the dup2 (on some systems), so we have to clear
1010 	 * it before routing the shell's error output to the same place as
1011 	 * its standard output.
1012 	 */
1013 	fcntl(1, F_SETFD, 0);
1014 	(void) dup2 (1, 2);
1015 
1016 #ifdef USE_PGRP
1017 	/*
1018 	 * We want to switch the child into a different process family so
1019 	 * we can kill it and all its descendants in one fell swoop,
1020 	 * by killing its process family, but not commit suicide.
1021 	 */
1022 
1023 	(void) setpgrp(0, getpid());
1024 #endif USE_PGRP
1025 
1026 	(void) execv (shellPath, argv);
1027 	(void) write (2, "Could not execute shell\n",
1028 		 sizeof ("Could not execute shell"));
1029 	_exit (1);
1030     } else {
1031 	job->pid = cpid;
1032 
1033 	if (usePipes && (job->flags & JOB_FIRST) ) {
1034 	    /*
1035 	     * The first time a job is run for a node, we set the current
1036 	     * position in the buffer to the beginning and mark another
1037 	     * stream to watch in the outputs mask
1038 	     */
1039 	    job->curPos = 0;
1040 
1041 #ifdef RMT_WILL_WATCH
1042 	    Rmt_Watch(job->inPipe, JobLocalInput, job);
1043 #else
1044 	    FD_SET(job->inPipe, &outputs);
1045 #endif /* RMT_WILL_WATCH */
1046 	}
1047 
1048 	if (job->flags & JOB_REMOTE) {
1049 	    job->rmtID = (char *)0;
1050 	} else {
1051 	    nLocal += 1;
1052 	    /*
1053 	     * XXX: Used to not happen if CUSTOMS. Why?
1054 	     */
1055 	    if (job->cmdFILE != stdout) {
1056 		fclose(job->cmdFILE);
1057 		job->cmdFILE = NULL;
1058 	    }
1059 	}
1060     }
1061 
1062 jobExecFinish:
1063     /*
1064      * Now the job is actually running, add it to the table.
1065      */
1066     nJobs += 1;
1067     (void)Lst_AtEnd (jobs, (ClientData)job);
1068     if (nJobs == maxJobs) {
1069 	jobFull = TRUE;
1070     }
1071 }
1072 
1073 /*-
1074  *-----------------------------------------------------------------------
1075  * JobMakeArgv --
1076  *	Create the argv needed to execute the shell for a given job.
1077  *
1078  *
1079  * Results:
1080  *
1081  * Side Effects:
1082  *
1083  *-----------------------------------------------------------------------
1084  */
1085 static void
1086 JobMakeArgv(job, argv)
1087     Job	    	  *job;
1088     char	  **argv;
1089 {
1090     int	    	  argc;
1091     static char	  args[10]; 	/* For merged arguments */
1092 
1093     argv[0] = shellName;
1094     argc = 1;
1095 
1096     if ((commandShell->exit && (*commandShell->exit != '-')) ||
1097 	(commandShell->echo && (*commandShell->echo != '-')))
1098     {
1099 	/*
1100 	 * At least one of the flags doesn't have a minus before it, so
1101 	 * merge them together. Have to do this because the *(&(@*#*&#$#
1102 	 * Bourne shell thinks its second argument is a file to source.
1103 	 * Grrrr. Note the ten-character limitation on the combined arguments.
1104 	 */
1105 	(void)sprintf(args, "-%s%s",
1106 		      ((job->flags & JOB_IGNERR) ? "" :
1107 		       (commandShell->exit ? commandShell->exit : "")),
1108 		      ((job->flags & JOB_SILENT) ? "" :
1109 		       (commandShell->echo ? commandShell->echo : "")));
1110 
1111 	if (args[1]) {
1112 	    argv[argc] = args;
1113 	    argc++;
1114 	}
1115     } else {
1116 	if (!(job->flags & JOB_IGNERR) && commandShell->exit) {
1117 	    argv[argc] = commandShell->exit;
1118 	    argc++;
1119 	}
1120 	if (!(job->flags & JOB_SILENT) && commandShell->echo) {
1121 	    argv[argc] = commandShell->echo;
1122 	    argc++;
1123 	}
1124     }
1125     argv[argc] = (char *)NULL;
1126 }
1127 
1128 /*-
1129  *-----------------------------------------------------------------------
1130  * JobRestart --
1131  *	Restart a job that stopped for some reason.
1132  *
1133  * Results:
1134  *	None.
1135  *
1136  * Side Effects:
1137  *	jobFull will be set if the job couldn't be run.
1138  *
1139  *-----------------------------------------------------------------------
1140  */
1141 static void
1142 JobRestart(job)
1143     Job 	  *job;    	/* Job to restart */
1144 {
1145     if (job->flags & JOB_REMIGRATE) {
1146 	if (DEBUG(JOB)) {
1147 	    printf("Remigrating %x\n", job->pid);
1148 	}
1149 	if (nLocal != maxLocal) {
1150 		/*
1151 		 * Job cannot be remigrated, but there's room on the local
1152 		 * machine, so resume the job and note that another
1153 		 * local job has started.
1154 		 */
1155 		if (DEBUG(JOB)) {
1156 		    printf("resuming on local machine\n");
1157 	        }
1158 		KILL(job->pid, SIGCONT);
1159 		nLocal +=1;
1160 		job->flags &= ~(JOB_REMIGRATE|JOB_RESUME);
1161 	} else {
1162 		/*
1163 		 * Job cannot be restarted. Mark the table as full and
1164 		 * place the job back on the list of stopped jobs.
1165 		 */
1166 		if (DEBUG(JOB)) {
1167 		    printf("holding\n");
1168 		}
1169 		(void)Lst_AtFront(stoppedJobs, (ClientData)job);
1170 		jobFull = TRUE;
1171 		if (DEBUG(JOB)) {
1172 		    printf("Job queue is full.\n");
1173 		}
1174 		return;
1175 	}
1176 
1177 	(void)Lst_AtEnd(jobs, (ClientData)job);
1178 	nJobs += 1;
1179 	if (nJobs == maxJobs) {
1180 	    jobFull = TRUE;
1181 	    if (DEBUG(JOB)) {
1182 		printf("Job queue is full.\n");
1183 	    }
1184 	}
1185     } else if (job->flags & JOB_RESTART) {
1186 	/*
1187 	 * Set up the control arguments to the shell. This is based on the
1188 	 * flags set earlier for this job. If the JOB_IGNERR flag is clear,
1189 	 * the 'exit' flag of the commandShell is used to cause it to exit
1190 	 * upon receiving an error. If the JOB_SILENT flag is clear, the
1191 	 * 'echo' flag of the commandShell is used to get it to start echoing
1192 	 * as soon as it starts processing commands.
1193 	 */
1194 	char	  *argv[4];
1195 
1196 	JobMakeArgv(job, argv);
1197 
1198 	if (DEBUG(JOB)) {
1199 	    printf("Restarting %s...", job->node->name);
1200 	}
1201 	if (((nLocal >= maxLocal) && ! (job->flags & JOB_SPECIAL))) {
1202 		/*
1203 		 * Can't be exported and not allowed to run locally -- put it
1204 		 * back on the hold queue and mark the table full
1205 		 */
1206 		if (DEBUG(JOB)) {
1207 		    printf("holding\n");
1208 		}
1209 		(void)Lst_AtFront(stoppedJobs, (ClientData)job);
1210 		jobFull = TRUE;
1211 		if (DEBUG(JOB)) {
1212 		    printf("Job queue is full.\n");
1213 		}
1214 		return;
1215 	} else {
1216 		/*
1217 		 * Job may be run locally.
1218 		 */
1219 		if (DEBUG(JOB)) {
1220 		    printf("running locally\n");
1221 		}
1222 		job->flags &= ~JOB_REMOTE;
1223 	}
1224 	JobExec(job, argv);
1225     } else {
1226 	/*
1227 	 * The job has stopped and needs to be restarted. Why it stopped,
1228 	 * we don't know...
1229 	 */
1230 	if (DEBUG(JOB)) {
1231 	    printf("Resuming %s...", job->node->name);
1232 	}
1233 	if (((job->flags & JOB_REMOTE) ||
1234 	     (nLocal < maxLocal) ||
1235 	     (((job->flags & JOB_SPECIAL)) &&
1236 	      (maxLocal == 0))) &&
1237 	    (nJobs != maxJobs))
1238 	{
1239 	    /*
1240 	     * If the job is remote, it's ok to resume it as long as the
1241 	     * maximum concurrency won't be exceeded. If it's local and
1242 	     * we haven't reached the local concurrency limit already (or the
1243 	     * job must be run locally and maxLocal is 0), it's also ok to
1244 	     * resume it.
1245 	     */
1246 	    Boolean error;
1247 	    extern int errno;
1248 	    union wait status;
1249 
1250 #ifdef RMT_WANTS_SIGNALS
1251 	    if (job->flags & JOB_REMOTE) {
1252 		error = !Rmt_Signal(job, SIGCONT);
1253 	    } else
1254 #endif	/* RMT_WANTS_SIGNALS */
1255 		error = (KILL(job->pid, SIGCONT) != 0);
1256 
1257 	    if (!error) {
1258 		/*
1259 		 * Make sure the user knows we've continued the beast and
1260 		 * actually put the thing in the job table.
1261 		 */
1262 		job->flags |= JOB_CONTINUING;
1263 		status.w_termsig = SIGCONT;
1264 		JobFinish(job, status);
1265 
1266 		job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1267 		if (DEBUG(JOB)) {
1268 		    printf("done\n");
1269 		}
1270 	    } else {
1271 		Error("couldn't resume %s: %s",
1272 		    job->node->name, strerror(errno));
1273 		status.w_status = 0;
1274 		status.w_retcode = 1;
1275 		JobFinish(job, status);
1276 	    }
1277 	} else {
1278 	    /*
1279 	     * Job cannot be restarted. Mark the table as full and
1280 	     * place the job back on the list of stopped jobs.
1281 	     */
1282 	    if (DEBUG(JOB)) {
1283 		printf("table full\n");
1284 	    }
1285 	    (void)Lst_AtFront(stoppedJobs, (ClientData)job);
1286 	    jobFull = TRUE;
1287 	    if (DEBUG(JOB)) {
1288 		printf("Job queue is full.\n");
1289 	    }
1290 	}
1291     }
1292 }
1293 
1294 /*-
1295  *-----------------------------------------------------------------------
1296  * JobStart  --
1297  *	Start a target-creation process going for the target described
1298  *	by the graph node gn.
1299  *
1300  * Results:
1301  *	JOB_ERROR if there was an error in the commands, JOB_FINISHED
1302  *	if there isn't actually anything left to do for the job and
1303  *	JOB_RUNNING if the job has been started.
1304  *
1305  * Side Effects:
1306  *	A new Job node is created and added to the list of running
1307  *	jobs. PMake is forked and a child shell created.
1308  *-----------------------------------------------------------------------
1309  */
1310 static int
1311 JobStart (gn, flags, previous)
1312     GNode         *gn;	      /* target to create */
1313     short	  flags;      /* flags for the job to override normal ones.
1314 			       * e.g. JOB_SPECIAL or JOB_IGNDOTS */
1315     Job 	  *previous;  /* The previous Job structure for this node,
1316 			       * if any. */
1317 {
1318     register Job  *job;       /* new job descriptor */
1319     char	  *argv[4];   /* Argument vector to shell */
1320     char          args[5];    /* arguments to shell */
1321     static int    jobno = 0;  /* job number of catching output in a file */
1322     Boolean	  cmdsOK;     /* true if the nodes commands were all right */
1323     Boolean 	  local;      /* Set true if the job was run locally */
1324     Boolean 	  noExec;     /* Set true if we decide not to run the job */
1325 
1326     if (previous != (Job *)NULL) {
1327 	previous->flags &= ~ (JOB_FIRST|JOB_IGNERR|JOB_SILENT|JOB_REMOTE);
1328 	job = previous;
1329     } else {
1330 	job = (Job *) emalloc (sizeof (Job));
1331 	if (job == (Job *)NULL) {
1332 	    Punt("JobStart out of memory");
1333 	}
1334 	flags |= JOB_FIRST;
1335     }
1336 
1337     job->node = gn;
1338     job->tailCmds = NILLNODE;
1339 
1340     /*
1341      * Set the initial value of the flags for this job based on the global
1342      * ones and the node's attributes... Any flags supplied by the caller
1343      * are also added to the field.
1344      */
1345     job->flags = 0;
1346     if (Targ_Ignore (gn)) {
1347 	job->flags |= JOB_IGNERR;
1348     }
1349     if (Targ_Silent (gn)) {
1350 	job->flags |= JOB_SILENT;
1351     }
1352     job->flags |= flags;
1353 
1354     /*
1355      * Check the commands now so any attributes from .DEFAULT have a chance
1356      * to migrate to the node
1357      */
1358     if (job->flags & JOB_FIRST) {
1359 	cmdsOK = Job_CheckCommands(gn, Error);
1360     } else {
1361 	cmdsOK = TRUE;
1362     }
1363 
1364     /*
1365      * If the -n flag wasn't given, we open up OUR (not the child's)
1366      * temporary file to stuff commands in it. The thing is rd/wr so we don't
1367      * need to reopen it to feed it to the shell. If the -n flag *was* given,
1368      * we just set the file to be stdout. Cute, huh?
1369      */
1370     if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
1371 	/*
1372 	 * We're serious here, but if the commands were bogus, we're
1373 	 * also dead...
1374 	 */
1375 	if (!cmdsOK) {
1376 	    DieHorribly();
1377 	}
1378 
1379 	job->cmdFILE = fopen (tfile, "w+");
1380 	if (job->cmdFILE == (FILE *) NULL) {
1381 	    Punt ("Could not open %s", tfile);
1382 	}
1383 	fcntl(fileno(job->cmdFILE), F_SETFD, 1);
1384 	/*
1385 	 * Send the commands to the command file, flush all its buffers then
1386 	 * rewind and remove the thing.
1387 	 */
1388 	noExec = FALSE;
1389 
1390 	/*
1391 	 * used to be backwards; replace when start doing multiple commands
1392 	 * per shell.
1393 	 */
1394 	if (1) {
1395 	    /*
1396 	     * Be compatible: If this is the first time for this node,
1397 	     * verify its commands are ok and open the commands list for
1398 	     * sequential access by later invocations of JobStart.
1399 	     * Once that is done, we take the next command off the list
1400 	     * and print it to the command file. If the command was an
1401 	     * ellipsis, note that there's nothing more to execute.
1402 	     */
1403 	    if ((job->flags&JOB_FIRST) && (Lst_Open(gn->commands) != SUCCESS)){
1404 		cmdsOK = FALSE;
1405 	    } else {
1406 		LstNode	ln = Lst_Next (gn->commands);
1407 
1408 		if ((ln == NILLNODE) ||
1409 		    JobPrintCommand ((char *)Lst_Datum (ln), job))
1410 		{
1411 		    noExec = TRUE;
1412 		    Lst_Close (gn->commands);
1413 		}
1414 		if (noExec && !(job->flags & JOB_FIRST)) {
1415 		    /*
1416 		     * If we're not going to execute anything, the job
1417 		     * is done and we need to close down the various
1418 		     * file descriptors we've opened for output, then
1419 		     * call JobDoOutput to catch the final characters or
1420 		     * send the file to the screen... Note that the i/o streams
1421 		     * are only open if this isn't the first job.
1422 		     * Note also that this could not be done in
1423 		     * Job_CatchChildren b/c it wasn't clear if there were
1424 		     * more commands to execute or not...
1425 		     */
1426 		    if (usePipes) {
1427 #ifdef RMT_WILL_WATCH
1428 			Rmt_Ignore(job->inPipe);
1429 #else
1430 			FD_CLR(job->inPipe, &outputs);
1431 #endif
1432 			if (job->outPipe != job->inPipe) {
1433 			    (void)close (job->outPipe);
1434 			}
1435 			JobDoOutput (job, TRUE);
1436 			(void)close (job->inPipe);
1437 		    } else {
1438 			(void)close (job->outFd);
1439 			JobDoOutput (job, TRUE);
1440 		    }
1441 		}
1442 	    }
1443 	} else {
1444 	    /*
1445 	     * We can do all the commands at once. hooray for sanity
1446 	     */
1447 	    numCommands = 0;
1448 	    Lst_ForEach (gn->commands, JobPrintCommand, (ClientData)job);
1449 
1450 	    /*
1451 	     * If we didn't print out any commands to the shell script,
1452 	     * there's not much point in executing the shell, is there?
1453 	     */
1454 	    if (numCommands == 0) {
1455 		noExec = TRUE;
1456 	    }
1457 	}
1458     } else if (noExecute) {
1459 	/*
1460 	 * Not executing anything -- just print all the commands to stdout
1461 	 * in one fell swoop. This will still set up job->tailCmds correctly.
1462 	 */
1463 	if (lastNode != gn) {
1464 	    printf (targFmt, gn->name);
1465 	    lastNode = gn;
1466 	}
1467 	job->cmdFILE = stdout;
1468 	/*
1469 	 * Only print the commands if they're ok, but don't die if they're
1470 	 * not -- just let the user know they're bad and keep going. It
1471 	 * doesn't do any harm in this case and may do some good.
1472 	 */
1473 	if (cmdsOK) {
1474 	    Lst_ForEach(gn->commands, JobPrintCommand, (ClientData)job);
1475 	}
1476 	/*
1477 	 * Don't execute the shell, thank you.
1478 	 */
1479 	noExec = TRUE;
1480     } else {
1481 	/*
1482 	 * Just touch the target and note that no shell should be executed.
1483 	 * Set cmdFILE to stdout to make life easier. Check the commands, too,
1484 	 * but don't die if they're no good -- it does no harm to keep working
1485 	 * up the graph.
1486 	 */
1487 	job->cmdFILE = stdout;
1488     	Job_Touch (gn, job->flags&JOB_SILENT);
1489 	noExec = TRUE;
1490     }
1491 
1492     /*
1493      * If we're not supposed to execute a shell, don't.
1494      */
1495     if (noExec) {
1496 	/*
1497 	 * Unlink and close the command file if we opened one
1498 	 */
1499 	if (job->cmdFILE != stdout) {
1500 	    (void) unlink (tfile);
1501 	    fclose(job->cmdFILE);
1502 	} else {
1503 	    fflush (stdout);
1504 	}
1505 
1506 	/*
1507 	 * We only want to work our way up the graph if we aren't here because
1508 	 * the commands for the job were no good.
1509 	 */
1510 	if (cmdsOK) {
1511 	    if (aborting == 0) {
1512 		if (job->tailCmds != NILLNODE) {
1513 		    Lst_ForEachFrom(job->node->commands, job->tailCmds,
1514 				    JobSaveCommand,
1515 				    (ClientData)job->node);
1516 		}
1517 		Make_Update(job->node);
1518 	    }
1519 	    free((Address)job);
1520 	    return(JOB_FINISHED);
1521 	} else {
1522 	    free((Address)job);
1523 	    return(JOB_ERROR);
1524 	}
1525     } else {
1526 	fflush (job->cmdFILE);
1527 	(void) unlink (tfile);
1528     }
1529 
1530     /*
1531      * Set up the control arguments to the shell. This is based on the flags
1532      * set earlier for this job.
1533      */
1534     JobMakeArgv(job, argv);
1535 
1536     /*
1537      * If we're using pipes to catch output, create the pipe by which we'll
1538      * get the shell's output. If we're using files, print out that we're
1539      * starting a job and then set up its temporary-file name. This is just
1540      * tfile with two extra digits tacked on -- jobno.
1541      */
1542     if (job->flags & JOB_FIRST) {
1543 	if (usePipes) {
1544 	    int fd[2];
1545 	    (void) pipe(fd);
1546 	    job->inPipe = fd[0];
1547 	    job->outPipe = fd[1];
1548 	    (void)fcntl (job->inPipe, F_SETFD, 1);
1549 	    (void)fcntl (job->outPipe, F_SETFD, 1);
1550 	} else {
1551 	    printf ("Remaking `%s'\n", gn->name);
1552 	    fflush (stdout);
1553 	    sprintf (job->outFile, "%s%02d", tfile, jobno);
1554 	    jobno = (jobno + 1) % 100;
1555 	    job->outFd = open(job->outFile,O_WRONLY|O_CREAT|O_APPEND,0600);
1556 	    (void)fcntl (job->outFd, F_SETFD, 1);
1557 	}
1558     }
1559 
1560     local = TRUE;
1561 
1562     if (local && (((nLocal >= maxLocal) &&
1563 	 !(job->flags & JOB_SPECIAL) &&
1564 	 (maxLocal != 0))))
1565     {
1566 	/*
1567 	 * The job can only be run locally, but we've hit the limit of
1568 	 * local concurrency, so put the job on hold until some other job
1569 	 * finishes. Note that the special jobs (.BEGIN, .INTERRUPT and .END)
1570 	 * may be run locally even when the local limit has been reached
1571 	 * (e.g. when maxLocal == 0), though they will be exported if at
1572 	 * all possible.
1573 	 */
1574 	jobFull = TRUE;
1575 
1576 	if (DEBUG(JOB)) {
1577 	    printf("Can only run job locally.\n");
1578 	}
1579 	job->flags |= JOB_RESTART;
1580 	(void)Lst_AtEnd(stoppedJobs, (ClientData)job);
1581     } else {
1582 	if ((nLocal >= maxLocal) && local) {
1583 	    /*
1584 	     * If we're running this job locally as a special case (see above),
1585 	     * at least say the table is full.
1586 	     */
1587 	    jobFull = TRUE;
1588 	    if (DEBUG(JOB)) {
1589 		printf("Local job queue is full.\n");
1590 	    }
1591 	}
1592 	JobExec(job, argv);
1593     }
1594     return(JOB_RUNNING);
1595 }
1596 
1597 /*-
1598  *-----------------------------------------------------------------------
1599  * JobDoOutput  --
1600  *	This function is called at different times depending on
1601  *	whether the user has specified that output is to be collected
1602  *	via pipes or temporary files. In the former case, we are called
1603  *	whenever there is something to read on the pipe. We collect more
1604  *	output from the given job and store it in the job's outBuf. If
1605  *	this makes up a line, we print it tagged by the job's identifier,
1606  *	as necessary.
1607  *	If output has been collected in a temporary file, we open the
1608  *	file and read it line by line, transfering it to our own
1609  *	output channel until the file is empty. At which point we
1610  *	remove the temporary file.
1611  *	In both cases, however, we keep our figurative eye out for the
1612  *	'noPrint' line for the shell from which the output came. If
1613  *	we recognize a line, we don't print it. If the command is not
1614  *	alone on the line (the character after it is not \0 or \n), we
1615  *	do print whatever follows it.
1616  *
1617  * Results:
1618  *	None
1619  *
1620  * Side Effects:
1621  *	curPos may be shifted as may the contents of outBuf.
1622  *-----------------------------------------------------------------------
1623  */
1624 void
1625 JobDoOutput (job, finish)
1626     register Job   *job;	  /* the job whose output needs printing */
1627     Boolean	   finish;	  /* TRUE if this is the last time we'll be
1628 				   * called for this job */
1629 {
1630     Boolean       gotNL = FALSE;  /* true if got a newline */
1631     register int  nr;	      	  /* number of bytes read */
1632     register int  i;	      	  /* auxiliary index into outBuf */
1633     register int  max;	      	  /* limit for i (end of current data) */
1634     int		  nRead;      	  /* (Temporary) number of bytes read */
1635 
1636     char          c;		  /* character after noPrint string */
1637     FILE      	  *oFILE;	  /* Stream pointer to shell's output file */
1638     char          inLine[132];
1639 
1640 
1641     if (usePipes) {
1642 	/*
1643 	 * Read as many bytes as will fit in the buffer.
1644 	 */
1645 end_loop:
1646 
1647 	nRead = read (job->inPipe, &job->outBuf[job->curPos],
1648 			 JOB_BUFSIZE - job->curPos);
1649 	if (nRead < 0) {
1650 	    if (DEBUG(JOB)) {
1651 		perror("JobDoOutput(piperead)");
1652 	    }
1653 	    nr = 0;
1654 	} else {
1655 	    nr = nRead;
1656 	}
1657 
1658 	/*
1659 	 * If we hit the end-of-file (the job is dead), we must flush its
1660 	 * remaining output, so pretend we read a newline if there's any
1661 	 * output remaining in the buffer.
1662 	 * Also clear the 'finish' flag so we stop looping.
1663 	 */
1664 	if ((nr == 0) && (job->curPos != 0)) {
1665 	    job->outBuf[job->curPos] = '\n';
1666 	    nr = 1;
1667 	    finish = FALSE;
1668 	} else if (nr == 0) {
1669 	    finish = FALSE;
1670 	}
1671 
1672 	/*
1673 	 * Look for the last newline in the bytes we just got. If there is
1674 	 * one, break out of the loop with 'i' as its index and gotNL set
1675 	 * TRUE.
1676 	 */
1677 	max = job->curPos + nr;
1678 	for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
1679 	    if (job->outBuf[i] == '\n') {
1680 		gotNL = TRUE;
1681 		break;
1682 	    } else if (job->outBuf[i] == '\0') {
1683 		/*
1684 		 * Why?
1685 		 */
1686 		job->outBuf[i] = ' ';
1687 	    }
1688 	}
1689 
1690 	if (!gotNL) {
1691 	    job->curPos += nr;
1692 	    if (job->curPos == JOB_BUFSIZE) {
1693 		/*
1694 		 * If we've run out of buffer space, we have no choice
1695 		 * but to print the stuff. sigh.
1696 		 */
1697 		gotNL = TRUE;
1698 		i = job->curPos;
1699 	    }
1700 	}
1701 	if (gotNL) {
1702 	    /*
1703 	     * Need to send the output to the screen. Null terminate it
1704 	     * first, overwriting the newline character if there was one.
1705 	     * So long as the line isn't one we should filter (according
1706 	     * to the shell description), we print the line, preceeded
1707 	     * by a target banner if this target isn't the same as the
1708 	     * one for which we last printed something.
1709 	     * The rest of the data in the buffer are then shifted down
1710 	     * to the start of the buffer and curPos is set accordingly.
1711 	     */
1712 	    job->outBuf[i] = '\0';
1713 	    if (i >= job->curPos) {
1714 		register char	*cp, *ecp;
1715 
1716 		cp = job->outBuf;
1717 		if (commandShell->noPrint) {
1718 		    ecp = Str_FindSubstring(job->outBuf,
1719 					    commandShell->noPrint);
1720 		    while (ecp != (char *)NULL) {
1721 			if (cp != ecp) {
1722 			    *ecp = '\0';
1723 			    if (job->node != lastNode) {
1724 				printf (targFmt, job->node->name);
1725 				lastNode = job->node;
1726 			    }
1727 			    /*
1728 			     * The only way there wouldn't be a newline after
1729 			     * this line is if it were the last in the buffer.
1730 			     * however, since the non-printable comes after it,
1731 			     * there must be a newline, so we don't print one.
1732 			     */
1733 			    printf ("%s", cp);
1734 			}
1735 			cp = ecp + commandShell->noPLen;
1736 			if (cp != &job->outBuf[i]) {
1737 			    /*
1738 			     * Still more to print, look again after skipping
1739 			     * the whitespace following the non-printable
1740 			     * command....
1741 			     */
1742 			    cp++;
1743 			    while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
1744 				cp++;
1745 			    }
1746 			    ecp = Str_FindSubstring (cp,
1747 						     commandShell->noPrint);
1748 			} else {
1749 			    break;
1750 			}
1751 		    }
1752 		}
1753 
1754 		/*
1755 		 * There's still more in that thar buffer. This time, though,
1756 		 * we know there's no newline at the end, so we add one of
1757 		 * our own free will.
1758 		 */
1759 		if (*cp != '\0') {
1760 		    if (job->node != lastNode) {
1761 			printf (targFmt, job->node->name);
1762 			lastNode = job->node;
1763 		    }
1764 		    printf ("%s\n", cp);
1765 		}
1766 
1767 		fflush (stdout);
1768 	    }
1769 	    if (i < max - 1) {
1770 		bcopy (&job->outBuf[i + 1], /* shift the remaining */
1771 		       job->outBuf,        /* characters down */
1772 		       max - (i + 1));
1773 		job->curPos = max - (i + 1);
1774 
1775 	    } else {
1776 		/*
1777 		 * We have written everything out, so we just start over
1778 		 * from the start of the buffer. No copying. No nothing.
1779 		 */
1780 		job->curPos = 0;
1781 	    }
1782 	}
1783 	if (finish) {
1784 	    /*
1785 	     * If the finish flag is true, we must loop until we hit
1786 	     * end-of-file on the pipe. This is guaranteed to happen eventually
1787 	     * since the other end of the pipe is now closed (we closed it
1788 	     * explicitly and the child has exited). When we do get an EOF,
1789 	     * finish will be set FALSE and we'll fall through and out.
1790 	     */
1791 	    goto end_loop;
1792 	}
1793     } else {
1794 	/*
1795 	 * We've been called to retrieve the output of the job from the
1796 	 * temporary file where it's been squirreled away. This consists of
1797 	 * opening the file, reading the output line by line, being sure not
1798 	 * to print the noPrint line for the shell we used, then close and
1799 	 * remove the temporary file. Very simple.
1800 	 *
1801 	 * Change to read in blocks and do FindSubString type things as for
1802 	 * pipes? That would allow for "@echo -n..."
1803 	 */
1804 	oFILE = fopen (job->outFile, "r");
1805 	if (oFILE != (FILE *) NULL) {
1806 	    printf ("Results of making %s:\n", job->node->name);
1807 	    while (fgets (inLine, sizeof(inLine), oFILE) != NULL) {
1808 		register char	*cp, *ecp, *endp;
1809 
1810 		cp = inLine;
1811 		endp = inLine + strlen(inLine);
1812 		if (endp[-1] == '\n') {
1813 		    *--endp = '\0';
1814 		}
1815 		if (commandShell->noPrint) {
1816 		    ecp = Str_FindSubstring(cp, commandShell->noPrint);
1817 		    while (ecp != (char *)NULL) {
1818 			if (cp != ecp) {
1819 			    *ecp = '\0';
1820 			    /*
1821 			     * The only way there wouldn't be a newline after
1822 			     * this line is if it were the last in the buffer.
1823 			     * however, since the non-printable comes after it,
1824 			     * there must be a newline, so we don't print one.
1825 			     */
1826 			    printf ("%s", cp);
1827 			}
1828 			cp = ecp + commandShell->noPLen;
1829 			if (cp != endp) {
1830 			    /*
1831 			     * Still more to print, look again after skipping
1832 			     * the whitespace following the non-printable
1833 			     * command....
1834 			     */
1835 			    cp++;
1836 			    while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
1837 				cp++;
1838 			    }
1839 			    ecp = Str_FindSubstring(cp, commandShell->noPrint);
1840 			} else {
1841 			    break;
1842 			}
1843 		    }
1844 		}
1845 
1846 		/*
1847 		 * There's still more in that thar buffer. This time, though,
1848 		 * we know there's no newline at the end, so we add one of
1849 		 * our own free will.
1850 		 */
1851 		if (*cp != '\0') {
1852 		    printf ("%s\n", cp);
1853 		}
1854 	    }
1855 	    fclose (oFILE);
1856 	    (void) unlink (job->outFile);
1857 	}
1858     }
1859     fflush(stdout);
1860 }
1861 
1862 /*-
1863  *-----------------------------------------------------------------------
1864  * Job_CatchChildren --
1865  *	Handle the exit of a child. Called from Make_Make.
1866  *
1867  * Results:
1868  *	none.
1869  *
1870  * Side Effects:
1871  *	The job descriptor is removed from the list of children.
1872  *
1873  * Notes:
1874  *	We do waits, blocking or not, according to the wisdom of our
1875  *	caller, until there are no more children to report. For each
1876  *	job, call JobFinish to finish things off. This will take care of
1877  *	putting jobs on the stoppedJobs queue.
1878  *
1879  *-----------------------------------------------------------------------
1880  */
1881 void
1882 Job_CatchChildren (block)
1883     Boolean	  block;    	/* TRUE if should block on the wait. */
1884 {
1885     int    	  pid;	    	/* pid of dead child */
1886     register Job  *job;	    	/* job descriptor for dead child */
1887     LstNode       jnode;    	/* list element for finding job */
1888     union wait	  status;   	/* Exit/termination status */
1889 
1890     /*
1891      * Don't even bother if we know there's no one around.
1892      */
1893     if (nLocal == 0) {
1894 	return;
1895     }
1896 
1897     while ((pid = wait3(&status, (block?0:WNOHANG)|WUNTRACED,
1898 			(struct rusage *)0)) > 0)
1899     {
1900 	if (DEBUG(JOB))
1901 	    printf("Process %d exited or stopped.\n", pid);
1902 
1903 
1904 	jnode = Lst_Find (jobs, (ClientData)pid, JobCmpPid);
1905 
1906 	if (jnode == NILLNODE) {
1907 	    if (WIFSIGNALED(status) && (status.w_termsig == SIGCONT)) {
1908 		jnode = Lst_Find(stoppedJobs, (ClientData)pid, JobCmpPid);
1909 		if (jnode == NILLNODE) {
1910 		    Error("Resumed child (%d) not in table", pid);
1911 		    continue;
1912 		}
1913 		job = (Job *)Lst_Datum(jnode);
1914 		(void)Lst_Remove(stoppedJobs, jnode);
1915 	    } else {
1916 		Error ("Child (%d) not in table?", pid);
1917 		continue;
1918 	    }
1919 	} else {
1920 	    job = (Job *) Lst_Datum (jnode);
1921 	    (void)Lst_Remove (jobs, jnode);
1922 	    nJobs -= 1;
1923 	    if (jobFull && DEBUG(JOB)) {
1924 		printf("Job queue is no longer full.\n");
1925 	    }
1926 	    jobFull = FALSE;
1927 	    nLocal -= 1;
1928 	}
1929 
1930 	JobFinish (job, status);
1931     }
1932 }
1933 
1934 /*-
1935  *-----------------------------------------------------------------------
1936  * Job_CatchOutput --
1937  *	Catch the output from our children, if we're using
1938  *	pipes do so. Otherwise just block time until we get a
1939  *	signal (most likely a SIGCHLD) since there's no point in
1940  *	just spinning when there's nothing to do and the reaping
1941  *	of a child can wait for a while.
1942  *
1943  * Results:
1944  *	None
1945  *
1946  * Side Effects:
1947  *	Output is read from pipes if we're piping.
1948  * -----------------------------------------------------------------------
1949  */
1950 void
1951 Job_CatchOutput ()
1952 {
1953     int           	  nfds;
1954     struct timeval	  timeout;
1955     fd_set           	  readfds;
1956     register LstNode	  ln;
1957     register Job   	  *job;
1958     int	    	  	  pnJobs;   	/* Previous nJobs */
1959 
1960     fflush(stdout);
1961 #ifdef RMT_WILL_WATCH
1962     pnJobs = nJobs;
1963 
1964     /*
1965      * It is possible for us to be called with nJobs equal to 0. This happens
1966      * if all the jobs finish and a job that is stopped cannot be run
1967      * locally (eg if maxLocal is 0) and cannot be exported. The job will
1968      * be placed back on the stoppedJobs queue, Job_Empty() will return false,
1969      * Make_Run will call us again when there's nothing for which to wait.
1970      * nJobs never changes, so we loop forever. Hence the check. It could
1971      * be argued that we should sleep for a bit so as not to swamp the
1972      * exportation system with requests. Perhaps we should.
1973      *
1974      * NOTE: IT IS THE RESPONSIBILITY OF Rmt_Wait TO CALL Job_CatchChildren
1975      * IN A TIMELY FASHION TO CATCH ANY LOCALLY RUNNING JOBS THAT EXIT.
1976      * It may use the variable nLocal to determine if it needs to call
1977      * Job_CatchChildren (if nLocal is 0, there's nothing for which to
1978      * wait...)
1979      */
1980     while (nJobs != 0 && pnJobs == nJobs) {
1981 	Rmt_Wait();
1982     }
1983 #else
1984     if (usePipes) {
1985 	readfds = outputs;
1986 	timeout.tv_sec = SEL_SEC;
1987 	timeout.tv_usec = SEL_USEC;
1988 
1989 	if ((nfds = select (FD_SETSIZE, &readfds, (int *) 0, (int *) 0, &timeout)) < 0)
1990 	{
1991 	    return;
1992 	} else {
1993 	    if (Lst_Open (jobs) == FAILURE) {
1994 		Punt ("Cannot open job table");
1995 	    }
1996 	    while (nfds && (ln = Lst_Next (jobs)) != NILLNODE) {
1997 		job = (Job *) Lst_Datum (ln);
1998 		if (FD_ISSET(job->inPipe, &readfds)) {
1999 		    JobDoOutput (job, FALSE);
2000 		    nfds -= 1;
2001 		}
2002 	    }
2003 	    Lst_Close (jobs);
2004 	}
2005     }
2006 #endif /* RMT_WILL_WATCH */
2007 }
2008 
2009 /*-
2010  *-----------------------------------------------------------------------
2011  * Job_Make --
2012  *	Start the creation of a target. Basically a front-end for
2013  *	JobStart used by the Make module.
2014  *
2015  * Results:
2016  *	None.
2017  *
2018  * Side Effects:
2019  *	Another job is started.
2020  *
2021  *-----------------------------------------------------------------------
2022  */
2023 void
2024 Job_Make (gn)
2025     GNode   *gn;
2026 {
2027     (void)JobStart (gn, 0, (Job *)NULL);
2028 }
2029 
2030 /*-
2031  *-----------------------------------------------------------------------
2032  * Job_Init --
2033  *	Initialize the process module
2034  *
2035  * Results:
2036  *	none
2037  *
2038  * Side Effects:
2039  *	lists and counters are initialized
2040  *-----------------------------------------------------------------------
2041  */
2042 void
2043 Job_Init (maxproc, maxlocal)
2044     int           maxproc;  /* the greatest number of jobs which may be
2045 			     * running at one time */
2046     int	    	  maxlocal; /* the greatest number of local jobs which may
2047 			     * be running at once. */
2048 {
2049     GNode         *begin;     /* node for commands to do at the very start */
2050 
2051     sprintf (tfile, "/tmp/make%05d", getpid());
2052 
2053     jobs =  	  Lst_Init (FALSE);
2054     stoppedJobs = Lst_Init(FALSE);
2055     maxJobs = 	  maxproc;
2056     maxLocal = 	  maxlocal;
2057     nJobs = 	  0;
2058     nLocal = 	  0;
2059     jobFull = 	  FALSE;
2060 
2061     aborting = 	  0;
2062     errors = 	  0;
2063 
2064     lastNode =	  NILGNODE;
2065 
2066     if (maxJobs == 1) {
2067 	/*
2068 	 * If only one job can run at a time, there's no need for a banner,
2069 	 * no is there?
2070 	 */
2071 	targFmt = "";
2072     } else {
2073 	targFmt = TARG_FMT;
2074     }
2075 
2076     if (shellPath == (char *) NULL) {
2077 	/*
2078 	 * The user didn't specify a shell to use, so we are using the
2079 	 * default one... Both the absolute path and the last component
2080 	 * must be set. The last component is taken from the 'name' field
2081 	 * of the default shell description pointed-to by commandShell.
2082 	 * All default shells are located in _PATH_DEFSHELLDIR.
2083 	 */
2084 	shellName = commandShell->name;
2085 	shellPath = str_concat (_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH);
2086     }
2087 
2088     if (commandShell->exit == (char *)NULL) {
2089 	commandShell->exit = "";
2090     }
2091     if (commandShell->echo == (char *)NULL) {
2092 	commandShell->echo = "";
2093     }
2094 
2095     /*
2096      * Catch the four signals that POSIX specifies if they aren't ignored.
2097      * JobPassSig will take care of calling JobInterrupt if appropriate.
2098      */
2099     if (signal (SIGINT, SIG_IGN) != SIG_IGN) {
2100 	signal (SIGINT, JobPassSig);
2101     }
2102     if (signal (SIGHUP, SIG_IGN) != SIG_IGN) {
2103 	signal (SIGHUP, JobPassSig);
2104     }
2105     if (signal (SIGQUIT, SIG_IGN) != SIG_IGN) {
2106 	signal (SIGQUIT, JobPassSig);
2107     }
2108     if (signal (SIGTERM, SIG_IGN) != SIG_IGN) {
2109 	signal (SIGTERM, JobPassSig);
2110     }
2111     /*
2112      * There are additional signals that need to be caught and passed if
2113      * either the export system wants to be told directly of signals or if
2114      * we're giving each job its own process group (since then it won't get
2115      * signals from the terminal driver as we own the terminal)
2116      */
2117 #if defined(RMT_WANTS_SIGNALS) || defined(USE_PGRP)
2118     if (signal (SIGTSTP, SIG_IGN) != SIG_IGN) {
2119 	signal (SIGTSTP, JobPassSig);
2120     }
2121     if (signal (SIGTTOU, SIG_IGN) != SIG_IGN) {
2122 	signal (SIGTTOU, JobPassSig);
2123     }
2124     if (signal (SIGTTIN, SIG_IGN) != SIG_IGN) {
2125 	signal (SIGTTIN, JobPassSig);
2126     }
2127     if (signal (SIGWINCH, SIG_IGN) != SIG_IGN) {
2128 	signal (SIGWINCH, JobPassSig);
2129     }
2130 #endif
2131 
2132     begin = Targ_FindNode (".BEGIN", TARG_NOCREATE);
2133 
2134     if (begin != NILGNODE) {
2135 	JobStart (begin, JOB_SPECIAL, (Job *)0);
2136 	while (nJobs) {
2137 	    Job_CatchOutput();
2138 #ifndef RMT_WILL_WATCH
2139 	    Job_CatchChildren (!usePipes);
2140 #endif /* RMT_WILL_WATCH */
2141 	}
2142     }
2143     postCommands = Targ_FindNode (".END", TARG_CREATE);
2144 }
2145 
2146 /*-
2147  *-----------------------------------------------------------------------
2148  * Job_Full --
2149  *	See if the job table is full. It is considered full if it is OR
2150  *	if we are in the process of aborting OR if we have
2151  *	reached/exceeded our local quota. This prevents any more jobs
2152  *	from starting up.
2153  *
2154  * Results:
2155  *	TRUE if the job table is full, FALSE otherwise
2156  * Side Effects:
2157  *	None.
2158  *-----------------------------------------------------------------------
2159  */
2160 Boolean
2161 Job_Full ()
2162 {
2163     return (aborting || jobFull);
2164 }
2165 
2166 /*-
2167  *-----------------------------------------------------------------------
2168  * Job_Empty --
2169  *	See if the job table is empty.  Because the local concurrency may
2170  *	be set to 0, it is possible for the job table to become empty,
2171  *	while the list of stoppedJobs remains non-empty. In such a case,
2172  *	we want to restart as many jobs as we can.
2173  *
2174  * Results:
2175  *	TRUE if it is. FALSE if it ain't.
2176  *
2177  * Side Effects:
2178  *	None.
2179  *
2180  * -----------------------------------------------------------------------
2181  */
2182 Boolean
2183 Job_Empty ()
2184 {
2185     if (nJobs == 0) {
2186 	if (!Lst_IsEmpty(stoppedJobs) && !aborting) {
2187 	    /*
2188 	     * The job table is obviously not full if it has no jobs in
2189 	     * it...Try and restart the stopped jobs.
2190 	     */
2191 	    jobFull = FALSE;
2192 	    while (!jobFull && !Lst_IsEmpty(stoppedJobs)) {
2193 		JobRestart((Job *)Lst_DeQueue(stoppedJobs));
2194 	    }
2195 	    return(FALSE);
2196 	} else {
2197 	    return(TRUE);
2198 	}
2199     } else {
2200 	return(FALSE);
2201     }
2202 }
2203 
2204 /*-
2205  *-----------------------------------------------------------------------
2206  * JobMatchShell --
2207  *	Find a matching shell in 'shells' given its final component.
2208  *
2209  * Results:
2210  *	A pointer to the Shell structure.
2211  *
2212  * Side Effects:
2213  *	None.
2214  *
2215  *-----------------------------------------------------------------------
2216  */
2217 static Shell *
2218 JobMatchShell (name)
2219     char	  *name;      /* Final component of shell path */
2220 {
2221     register Shell *sh;	      /* Pointer into shells table */
2222     Shell	   *match;    /* Longest-matching shell */
2223     register char *cp1,
2224 		  *cp2;
2225     char	  *eoname;
2226 
2227     eoname = name + strlen (name);
2228 
2229     match = (Shell *) NULL;
2230 
2231     for (sh = shells; sh->name != NULL; sh++) {
2232 	for (cp1 = eoname - strlen (sh->name), cp2 = sh->name;
2233 	     *cp1 != '\0' && *cp1 == *cp2;
2234 	     cp1++, cp2++) {
2235 		 continue;
2236 	}
2237 	if (*cp1 != *cp2) {
2238 	    continue;
2239 	} else if (match == (Shell *) NULL ||
2240 		   strlen (match->name) < strlen (sh->name)) {
2241 		       match = sh;
2242 	}
2243     }
2244     return (match == (Shell *) NULL ? sh : match);
2245 }
2246 
2247 /*-
2248  *-----------------------------------------------------------------------
2249  * Job_ParseShell --
2250  *	Parse a shell specification and set up commandShell, shellPath
2251  *	and shellName appropriately.
2252  *
2253  * Results:
2254  *	FAILURE if the specification was incorrect.
2255  *
2256  * Side Effects:
2257  *	commandShell points to a Shell structure (either predefined or
2258  *	created from the shell spec), shellPath is the full path of the
2259  *	shell described by commandShell, while shellName is just the
2260  *	final component of shellPath.
2261  *
2262  * Notes:
2263  *	A shell specification consists of a .SHELL target, with dependency
2264  *	operator, followed by a series of blank-separated words. Double
2265  *	quotes can be used to use blanks in words. A backslash escapes
2266  *	anything (most notably a double-quote and a space) and
2267  *	provides the functionality it does in C. Each word consists of
2268  *	keyword and value separated by an equal sign. There should be no
2269  *	unnecessary spaces in the word. The keywords are as follows:
2270  *	    name  	    Name of shell.
2271  *	    path  	    Location of shell. Overrides "name" if given
2272  *	    quiet 	    Command to turn off echoing.
2273  *	    echo  	    Command to turn echoing on
2274  *	    filter	    Result of turning off echoing that shouldn't be
2275  *	    	  	    printed.
2276  *	    echoFlag	    Flag to turn echoing on at the start
2277  *	    errFlag	    Flag to turn error checking on at the start
2278  *	    hasErrCtl	    True if shell has error checking control
2279  *	    check 	    Command to turn on error checking if hasErrCtl
2280  *	    	  	    is TRUE or template of command to echo a command
2281  *	    	  	    for which error checking is off if hasErrCtl is
2282  *	    	  	    FALSE.
2283  *	    ignore	    Command to turn off error checking if hasErrCtl
2284  *	    	  	    is TRUE or template of command to execute a
2285  *	    	  	    command so as to ignore any errors it returns if
2286  *	    	  	    hasErrCtl is FALSE.
2287  *
2288  *-----------------------------------------------------------------------
2289  */
2290 ReturnStatus
2291 Job_ParseShell (line)
2292     char	  *line;  /* The shell spec */
2293 {
2294     char    	  **words;
2295     int	    	  wordCount;
2296     register char **argv;
2297     register int  argc;
2298     char    	  *path;
2299     Shell   	  newShell;
2300     Boolean 	  fullSpec = FALSE;
2301 
2302     while (isspace (*line)) {
2303 	line++;
2304     }
2305     words = brk_string (line, &wordCount);
2306 
2307     bzero ((Address)&newShell, sizeof(newShell));
2308 
2309     /*
2310      * Parse the specification by keyword
2311      */
2312     for (path = (char *)NULL, argc = wordCount - 1, argv = words + 1;
2313 	 argc != 0;
2314 	 argc--, argv++) {
2315 	     if (strncmp (*argv, "path=", 5) == 0) {
2316 		 path = &argv[0][5];
2317 	     } else if (strncmp (*argv, "name=", 5) == 0) {
2318 		 newShell.name = &argv[0][5];
2319 	     } else {
2320 		 if (strncmp (*argv, "quiet=", 6) == 0) {
2321 		     newShell.echoOff = &argv[0][6];
2322 		 } else if (strncmp (*argv, "echo=", 5) == 0) {
2323 		     newShell.echoOn = &argv[0][5];
2324 		 } else if (strncmp (*argv, "filter=", 7) == 0) {
2325 		     newShell.noPrint = &argv[0][7];
2326 		     newShell.noPLen = strlen(newShell.noPrint);
2327 		 } else if (strncmp (*argv, "echoFlag=", 9) == 0) {
2328 		     newShell.echo = &argv[0][9];
2329 		 } else if (strncmp (*argv, "errFlag=", 8) == 0) {
2330 		     newShell.exit = &argv[0][8];
2331 		 } else if (strncmp (*argv, "hasErrCtl=", 10) == 0) {
2332 		     char c = argv[0][10];
2333 		     newShell.hasErrCtl = !((c != 'Y') && (c != 'y') &&
2334 					    (c != 'T') && (c != 't'));
2335 		 } else if (strncmp (*argv, "check=", 6) == 0) {
2336 		     newShell.errCheck = &argv[0][6];
2337 		 } else if (strncmp (*argv, "ignore=", 7) == 0) {
2338 		     newShell.ignErr = &argv[0][7];
2339 		 } else {
2340 		     Parse_Error (PARSE_FATAL, "Unknown keyword \"%s\"",
2341 				  *argv);
2342 		     return (FAILURE);
2343 		 }
2344 		 fullSpec = TRUE;
2345 	     }
2346     }
2347 
2348     if (path == (char *)NULL) {
2349 	/*
2350 	 * If no path was given, the user wants one of the pre-defined shells,
2351 	 * yes? So we find the one s/he wants with the help of JobMatchShell
2352 	 * and set things up the right way. shellPath will be set up by
2353 	 * Job_Init.
2354 	 */
2355 	if (newShell.name == (char *)NULL) {
2356 	    Parse_Error (PARSE_FATAL, "Neither path nor name specified");
2357 	    return (FAILURE);
2358 	} else {
2359 	    commandShell = JobMatchShell (newShell.name);
2360 	    shellName = newShell.name;
2361 	}
2362     } else {
2363 	/*
2364 	 * The user provided a path. If s/he gave nothing else (fullSpec is
2365 	 * FALSE), try and find a matching shell in the ones we know of.
2366 	 * Else we just take the specification at its word and copy it
2367 	 * to a new location. In either case, we need to record the
2368 	 * path the user gave for the shell.
2369 	 */
2370 	shellPath = path;
2371 	path = rindex (path, '/');
2372 	if (path == (char *)NULL) {
2373 	    path = shellPath;
2374 	} else {
2375 	    path += 1;
2376 	}
2377 	if (newShell.name != (char *)NULL) {
2378 	    shellName = newShell.name;
2379 	} else {
2380 	    shellName = path;
2381 	}
2382 	if (!fullSpec) {
2383 	    commandShell = JobMatchShell (shellName);
2384 	} else {
2385 	    commandShell = (Shell *) emalloc(sizeof(Shell));
2386 	    *commandShell = newShell;
2387 	}
2388     }
2389 
2390     if (commandShell->echoOn && commandShell->echoOff) {
2391 	commandShell->hasEchoCtl = TRUE;
2392     }
2393 
2394     if (!commandShell->hasErrCtl) {
2395 	if (commandShell->errCheck == (char *)NULL) {
2396 	    commandShell->errCheck = "";
2397 	}
2398 	if (commandShell->ignErr == (char *)NULL) {
2399 	    commandShell->ignErr = "%s\n";
2400 	}
2401     }
2402 
2403     /*
2404      * Do not free up the words themselves, since they might be in use by the
2405      * shell specification...
2406      */
2407     free (words);
2408     return SUCCESS;
2409 }
2410 
2411 /*-
2412  *-----------------------------------------------------------------------
2413  * JobInterrupt --
2414  *	Handle the receipt of an interrupt.
2415  *
2416  * Results:
2417  *	None
2418  *
2419  * Side Effects:
2420  *	All children are killed. Another job will be started if the
2421  *	.INTERRUPT target was given.
2422  *-----------------------------------------------------------------------
2423  */
2424 static void
2425 JobInterrupt (runINTERRUPT)
2426     int	    runINTERRUPT;   	/* Non-zero if commands for the .INTERRUPT
2427 				 * target should be executed */
2428 {
2429     LstNode 	  ln;		/* element in job table */
2430     Job           *job;	    	/* job descriptor in that element */
2431     GNode         *interrupt;	/* the node describing the .INTERRUPT target */
2432 
2433     aborting = ABORT_INTERRUPT;
2434 
2435     (void)Lst_Open (jobs);
2436     while ((ln = Lst_Next (jobs)) != NILLNODE) {
2437 	job = (Job *) Lst_Datum (ln);
2438 
2439 	if (!Targ_Precious (job->node)) {
2440 	    char  	*file = (job->node->path == (char *)NULL ?
2441 				 job->node->name :
2442 				 job->node->path);
2443 	    if (unlink (file) == 0) {
2444 		Error ("*** %s removed", file);
2445 	    }
2446 	}
2447 #ifdef RMT_WANTS_SIGNALS
2448 	if (job->flags & JOB_REMOTE) {
2449 	    /*
2450 	     * If job is remote, let the Rmt module do the killing.
2451 	     */
2452 	    if (!Rmt_Signal(job, SIGINT)) {
2453 		/*
2454 		 * If couldn't kill the thing, finish it out now with an
2455 		 * error code, since no exit report will come in likely.
2456 		 */
2457 		union wait status;
2458 
2459 		status.w_status = 0;
2460 		status.w_retcode = 1;
2461 		JobFinish(job, status);
2462 	    }
2463 	} else if (job->pid) {
2464 	    KILL(job->pid, SIGINT);
2465 	}
2466 #else
2467 	if (job->pid) {
2468 	    KILL(job->pid, SIGINT);
2469 	}
2470 #endif /* RMT_WANTS_SIGNALS */
2471     }
2472     Lst_Close (jobs);
2473 
2474     if (runINTERRUPT && !touchFlag) {
2475 	interrupt = Targ_FindNode (".INTERRUPT", TARG_NOCREATE);
2476 	if (interrupt != NILGNODE) {
2477 	    ignoreErrors = FALSE;
2478 
2479 	    JobStart (interrupt, JOB_IGNDOTS, (Job *)0);
2480 	    while (nJobs) {
2481 		Job_CatchOutput();
2482 #ifndef RMT_WILL_WATCH
2483 		Job_CatchChildren (!usePipes);
2484 #endif /* RMT_WILL_WATCH */
2485 	    }
2486 	}
2487     }
2488     (void) unlink (tfile);
2489     exit (0);
2490 }
2491 
2492 /*
2493  *-----------------------------------------------------------------------
2494  * Job_End --
2495  *	Do final processing such as the running of the commands
2496  *	attached to the .END target.
2497  *
2498  * Results:
2499  *	Number of errors reported.
2500  *
2501  * Side Effects:
2502  *	The process' temporary file (tfile) is removed if it still
2503  *	existed.
2504  *-----------------------------------------------------------------------
2505  */
2506 int
2507 Job_End ()
2508 {
2509     if (postCommands != NILGNODE && !Lst_IsEmpty (postCommands->commands)) {
2510 	if (errors) {
2511 	    Error ("Errors reported so .END ignored");
2512 	} else {
2513 	    JobStart (postCommands, JOB_SPECIAL | JOB_IGNDOTS,
2514 		       (Job *)0);
2515 
2516 	    while (nJobs) {
2517 		Job_CatchOutput();
2518 #ifndef RMT_WILL_WATCH
2519 		Job_CatchChildren (!usePipes);
2520 #endif /* RMT_WILL_WATCH */
2521 	    }
2522 	}
2523     }
2524     (void) unlink (tfile);
2525     return(errors);
2526 }
2527 
2528 /*-
2529  *-----------------------------------------------------------------------
2530  * Job_Wait --
2531  *	Waits for all running jobs to finish and returns. Sets 'aborting'
2532  *	to ABORT_WAIT to prevent other jobs from starting.
2533  *
2534  * Results:
2535  *	None.
2536  *
2537  * Side Effects:
2538  *	Currently running jobs finish.
2539  *
2540  *-----------------------------------------------------------------------
2541  */
2542 void
2543 Job_Wait()
2544 {
2545     aborting = ABORT_WAIT;
2546     while (nJobs != 0) {
2547 	Job_CatchOutput();
2548 #ifndef RMT_WILL_WATCH
2549 	Job_CatchChildren(!usePipes);
2550 #endif /* RMT_WILL_WATCH */
2551     }
2552     aborting = 0;
2553 }
2554 
2555 /*-
2556  *-----------------------------------------------------------------------
2557  * Job_AbortAll --
2558  *	Abort all currently running jobs without handling output or anything.
2559  *	This function is to be called only in the event of a major
2560  *	error. Most definitely NOT to be called from JobInterrupt.
2561  *
2562  * Results:
2563  *	None
2564  *
2565  * Side Effects:
2566  *	All children are killed, not just the firstborn
2567  *-----------------------------------------------------------------------
2568  */
2569 void
2570 Job_AbortAll ()
2571 {
2572     LstNode           	ln;		/* element in job table */
2573     Job            	*job;	/* the job descriptor in that element */
2574     int     	  	foo;
2575 
2576     aborting = ABORT_ERROR;
2577 
2578     if (nJobs) {
2579 
2580 	(void)Lst_Open (jobs);
2581 	while ((ln = Lst_Next (jobs)) != NILLNODE) {
2582 	    job = (Job *) Lst_Datum (ln);
2583 
2584 	    /*
2585 	     * kill the child process with increasingly drastic signals to make
2586 	     * darn sure it's dead.
2587 	     */
2588 #ifdef RMT_WANTS_SIGNALS
2589 	    if (job->flags & JOB_REMOTE) {
2590 		Rmt_Signal(job, SIGINT);
2591 		Rmt_Signal(job, SIGKILL);
2592 	    } else {
2593 		KILL(job->pid, SIGINT);
2594 		KILL(job->pid, SIGKILL);
2595 	    }
2596 #else
2597 	    KILL(job->pid, SIGINT);
2598 	    KILL(job->pid, SIGKILL);
2599 #endif /* RMT_WANTS_SIGNALS */
2600 	}
2601     }
2602 
2603     /*
2604      * Catch as many children as want to report in at first, then give up
2605      */
2606     while (wait3(&foo, WNOHANG, (struct rusage *)0) > 0) {
2607 	;
2608     }
2609     (void) unlink (tfile);
2610 }
2611