Home | History | Annotate | Line # | Download | only in make
job.c revision 1.110
      1 /*	$NetBSD: job.c,v 1.110 2006/03/15 20:33:19 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1988, 1989 by Adam de Boor
     37  * Copyright (c) 1989 by Berkeley Softworks
     38  * All rights reserved.
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Adam de Boor.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  */
     71 
     72 #ifndef MAKE_NATIVE
     73 static char rcsid[] = "$NetBSD: job.c,v 1.110 2006/03/15 20:33:19 dsl Exp $";
     74 #else
     75 #include <sys/cdefs.h>
     76 #ifndef lint
     77 #if 0
     78 static char sccsid[] = "@(#)job.c	8.2 (Berkeley) 3/19/94";
     79 #else
     80 __RCSID("$NetBSD: job.c,v 1.110 2006/03/15 20:33:19 dsl Exp $");
     81 #endif
     82 #endif /* not lint */
     83 #endif
     84 
     85 /*-
     86  * job.c --
     87  *	handle the creation etc. of our child processes.
     88  *
     89  * Interface:
     90  *	Job_Make  	    	Start the creation of the given target.
     91  *
     92  *	Job_CatchChildren   	Check for and handle the termination of any
     93  *	    	  	    	children. This must be called reasonably
     94  *	    	  	    	frequently to keep the whole make going at
     95  *	    	  	    	a decent clip, since job table entries aren't
     96  *	    	  	    	removed until their process is caught this way.
     97  *	    	  	    	Its single argument is TRUE if the function
     98  *	    	  	    	should block waiting for a child to terminate.
     99  *
    100  *	Job_CatchOutput	    	Print any output our children have produced.
    101  *	    	  	    	Should also be called fairly frequently to
    102  *	    	  	    	keep the user informed of what's going on.
    103  *	    	  	    	If no output is waiting, it will block for
    104  *	    	  	    	a time given by the SEL_* constants, below,
    105  *	    	  	    	or until output is ready.
    106  *
    107  *	Job_Init  	    	Called to intialize this module. in addition,
    108  *	    	  	    	any commands attached to the .BEGIN target
    109  *	    	  	    	are executed before this function returns.
    110  *	    	  	    	Hence, the makefile must have been parsed
    111  *	    	  	    	before this function is called.
    112  *
    113  *	Job_End  	    	Cleanup any memory used.
    114  *
    115  *	Job_Empty 	    	Return TRUE if the job table is completely
    116  *	    	  	    	empty.
    117  *
    118  *	Job_ParseShell	    	Given the line following a .SHELL target, parse
    119  *	    	  	    	the line as a shell specification. Returns
    120  *	    	  	    	FAILURE if the spec was incorrect.
    121  *
    122  *	Job_Finish	    	Perform any final processing which needs doing.
    123  *	    	  	    	This includes the execution of any commands
    124  *	    	  	    	which have been/were attached to the .END
    125  *	    	  	    	target. It should only be called when the
    126  *	    	  	    	job table is empty.
    127  *
    128  *	Job_AbortAll	    	Abort all currently running jobs. It doesn't
    129  *	    	  	    	handle output or do anything for the jobs,
    130  *	    	  	    	just kills them. It should only be called in
    131  *	    	  	    	an emergency, as it were.
    132  *
    133  *	Job_CheckCommands   	Verify that the commands for a target are
    134  *	    	  	    	ok. Provide them if necessary and possible.
    135  *
    136  *	Job_Touch 	    	Update a target without really updating it.
    137  *
    138  *	Job_Wait  	    	Wait for all currently-running jobs to finish.
    139  */
    140 
    141 #include <sys/types.h>
    142 #include <sys/stat.h>
    143 #include <sys/file.h>
    144 #include <sys/time.h>
    145 #include <sys/wait.h>
    146 
    147 #include <errno.h>
    148 #include <fcntl.h>
    149 #ifndef USE_SELECT
    150 #include <poll.h>
    151 #endif
    152 #include <signal.h>
    153 #include <stdio.h>
    154 #include <string.h>
    155 #include <utime.h>
    156 
    157 #include "make.h"
    158 #include "hash.h"
    159 #include "dir.h"
    160 #include "job.h"
    161 #include "pathnames.h"
    162 #include "trace.h"
    163 # define STATIC static
    164 
    165 /*
    166  * error handling variables
    167  */
    168 static int     	errors = 0;	    /* number of errors reported */
    169 static int    	aborting = 0;	    /* why is the make aborting? */
    170 #define ABORT_ERROR	1   	    /* Because of an error */
    171 #define ABORT_INTERRUPT	2   	    /* Because it was interrupted */
    172 #define ABORT_WAIT	3   	    /* Waiting for jobs to finish */
    173 #define JOB_TOKENS	"+EI+"	    /* Token to requeue for each abort state */
    174 
    175 /*
    176  * this tracks the number of tokens currently "out" to build jobs.
    177  */
    178 int jobTokensRunning = 0;
    179 int not_parallel = 0;		    /* set if .NOT_PARALLEL */
    180 
    181 /*
    182  * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file
    183  * is a char! So when we go above 127 we turn negative!
    184  */
    185 #define FILENO(a) ((unsigned) fileno(a))
    186 
    187 /*
    188  * post-make command processing. The node postCommands is really just the
    189  * .END target but we keep it around to avoid having to search for it
    190  * all the time.
    191  */
    192 static GNode   	  *postCommands = NILGNODE;
    193 				    /* node containing commands to execute when
    194 				     * everything else is done */
    195 static int     	  numCommands; 	    /* The number of commands actually printed
    196 				     * for a target. Should this number be
    197 				     * 0, no shell will be executed. */
    198 
    199 /*
    200  * Return values from JobStart.
    201  */
    202 #define JOB_RUNNING	0   	/* Job is running */
    203 #define JOB_ERROR 	1   	/* Error in starting the job */
    204 #define JOB_FINISHED	2   	/* The job is already finished */
    205 #define JOB_STOPPED	3   	/* The job is stopped */
    206 
    207 
    208 
    209 /*
    210  * Descriptions for various shells.
    211  */
    212 static Shell    shells[] = {
    213     /*
    214      * CSH description. The csh can do echo control by playing
    215      * with the setting of the 'echo' shell variable. Sadly,
    216      * however, it is unable to do error control nicely.
    217      */
    218 {
    219     "csh",
    220     TRUE, "unset verbose", "set verbose", "unset verbose", 10,
    221     FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"\n", "", '#',
    222     "v", "e",
    223 },
    224     /*
    225      * SH description. Echo control is also possible and, under
    226      * sun UNIX anyway, one can even control error checking.
    227      */
    228 {
    229     "sh",
    230     FALSE, "", "", "", 0,
    231     FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", '#',
    232 #ifdef __NetBSD__
    233     "q",
    234 #else
    235     "",
    236 #endif
    237     "",
    238 },
    239     /*
    240      * KSH description.
    241      */
    242 {
    243     "ksh",
    244     TRUE, "set +v", "set -v", "set +v", 6,
    245     FALSE, "echo \"%s\"\n", "%s\n", "{ %s \n} || exit $?\n", '#',
    246     "v",
    247     "",
    248 },
    249     /*
    250      * UNKNOWN.
    251      */
    252 {
    253     NULL,
    254     FALSE, NULL, NULL, NULL, 0,
    255     FALSE, NULL, NULL, NULL, 0,
    256     NULL, NULL,
    257 }
    258 };
    259 static Shell 	*commandShell = &shells[DEFSHELL];/* this is the shell to
    260 						   * which we pass all
    261 						   * commands in the Makefile.
    262 						   * It is set by the
    263 						   * Job_ParseShell function */
    264 const char *shellPath = NULL,		  	  /* full pathname of
    265 						   * executable image */
    266            *shellName = NULL;		      	  /* last component of shell */
    267 static const char *shellArgv = NULL;		  /* Custom shell args */
    268 
    269 
    270 static int  	maxJobs;    	/* The most children we can run at once */
    271 STATIC Lst     	jobs;		/* The structures that describe them */
    272 static Boolean	wantToken;	/* we want a token */
    273 
    274 /*
    275  * Set of descriptors of pipes connected to
    276  * the output channels of children
    277  */
    278 static struct pollfd *fds = NULL;
    279 static Job **jobfds = NULL;
    280 static int nfds = 0;
    281 static int maxfds = 0;
    282 static void watchfd(Job *);
    283 static void clearfd(Job *);
    284 static int readyfd(Job *);
    285 #define JBSTART 256
    286 #define JBFACTOR 2
    287 
    288 STATIC GNode   	*lastNode;	/* The node for which output was most recently
    289 				 * produced. */
    290 STATIC const char *targFmt;   	/* Format string to use to head output from a
    291 				 * job when it's not the most-recent job heard
    292 				 * from */
    293 static Job tokenWaitJob;	/* token wait pseudo-job */
    294 int	job_pipe[2] = { -1, -1 }; /* job server pipes. */
    295 
    296 static Job childExitJob;	/* child exit pseudo-job */
    297 int	exit_pipe[2] = { -1, -1 }; /* child exit signal pipe. */
    298 #define	CHILD_EXIT	"."
    299 #define	DO_JOB_RESUME	"R"
    300 
    301 #define TARG_FMT  "--- %s ---\n" /* Default format */
    302 #define MESSAGE(fp, gn) \
    303 	(void)fprintf(fp, targFmt, gn->name)
    304 
    305 /*
    306  * When make is stopped by SIGTSTP (ie ^Z types) the jobs are sent SIGTSTP.
    307  * When they report themselves stopped they are moved to syoppedJobs.
    308  * They all get moved back after SIGCONT.
    309  * This is all a waste of cpu cycles...
    310  */
    311 STATIC Lst	stoppedJobs;	/* Lst of Job structures describing
    312 				 * jobs that were stopped due to concurrency
    313 				 * limits or migration home */
    314 
    315 
    316 static sigset_t caught_signals;	/* Set of signals we handle */
    317 #if defined(USE_PGRP) && defined(SYSV)
    318 # define KILL(pid, sig)		kill(-(pid), (sig))
    319 #else
    320 # if defined(USE_PGRP)
    321 #  define KILL(pid, sig)	killpg((pid), (sig))
    322 # else
    323 #  define KILL(pid, sig)	kill((pid), (sig))
    324 # endif
    325 #endif
    326 
    327 /*
    328  * Grmpf... There is no way to set bits of the wait structure
    329  * anymore with the stupid W*() macros. I liked the union wait
    330  * stuff much more. So, we devise our own macros... This is
    331  * really ugly, use dramamine sparingly. You have been warned.
    332  */
    333 #ifndef W_STOPCODE
    334 #define W_STOPCODE(sig) (((sig) << 8) | 0177)
    335 #endif
    336 #ifndef W_EXITCODE
    337 #define W_EXITCODE(ret, sig) ((ret << 8) | (sig))
    338 #endif
    339 
    340 static int JobCondPassSig(ClientData, ClientData);
    341 static void JobPassSig(int);
    342 static void JobChildSig(int);
    343 #ifdef USE_PGRP
    344 static void JobContinueSig(int);
    345 #endif
    346 static int JobCmpPid(ClientData, ClientData);
    347 static int JobPrintCommand(ClientData, ClientData);
    348 static int JobSaveCommand(ClientData, ClientData);
    349 static void JobClose(Job *);
    350 static void JobFinish(Job *, int *);
    351 static void JobExec(Job *, char **);
    352 static void JobMakeArgv(Job *, char **);
    353 static int JobRestart(Job *);
    354 static int JobStart(GNode *, int);
    355 static char *JobOutput(Job *, char *, char *, int);
    356 static void JobDoOutput(Job *, Boolean);
    357 static Shell *JobMatchShell(const char *);
    358 static void JobInterrupt(int, int);
    359 static void JobRestartJobs(void);
    360 static void JobTokenAdd(void);
    361 static void JobSigLock(sigset_t *);
    362 static void JobSigUnlock(sigset_t *);
    363 static void JobSigReset(void);
    364 
    365 
    366 
    367 /*
    368  * JobSigLock/JobSigUnlock
    369  *
    370  * Signal lock routines to get exclusive access. Currently used to
    371  * protect `jobs' and `stoppedJobs' list manipulations.
    372  */
    373 static void JobSigLock(sigset_t *omaskp)
    374 {
    375 	if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) {
    376 		Punt("JobSigLock: sigprocmask: %s", strerror(errno));
    377 		sigemptyset(omaskp);
    378 	}
    379 }
    380 
    381 static void JobSigUnlock(sigset_t *omaskp)
    382 {
    383 	(void)sigprocmask(SIG_SETMASK, omaskp, NULL);
    384 }
    385 
    386 /*-
    387  *-----------------------------------------------------------------------
    388  * JobCondPassSig --
    389  *	Pass a signal to a job if the job is remote or if USE_PGRP
    390  *	is defined.
    391  *
    392  * Input:
    393  *	jobp		Job to biff
    394  *	signop		Signal to send it
    395  *
    396  * Results:
    397  *	=== 0
    398  *
    399  * Side Effects:
    400  *	None, except the job may bite it.
    401  *
    402  *-----------------------------------------------------------------------
    403  */
    404 static int
    405 JobCondPassSig(ClientData jobp, ClientData signop)
    406 {
    407     Job	*job = (Job *)jobp;
    408     int	signo = *(int *)signop;
    409 
    410     if (DEBUG(JOB)) {
    411 	(void)fprintf(stdout,
    412 		       "JobCondPassSig passing signal %d to child %d.\n",
    413 		       signo, job->pid);
    414 	(void)fflush(stdout);
    415     }
    416     KILL(job->pid, signo);
    417     return 0;
    418 }
    419 
    420 /*-
    421  *-----------------------------------------------------------------------
    422  * JobChldSig --
    423  *	SIGCHLD handler.
    424  *
    425  * Input:
    426  *	signo		The signal number we've received
    427  *
    428  * Results:
    429  *	None.
    430  *
    431  * Side Effects:
    432  *	Sends a token on the child exit pipe to wake us up from
    433  *	select()/poll().
    434  *
    435  *-----------------------------------------------------------------------
    436  */
    437 static void
    438 JobChildSig(int signo __unused)
    439 {
    440     write(exit_pipe[1], CHILD_EXIT, 1);
    441 }
    442 
    443 
    444 #ifdef USE_PGRP
    445 /*-
    446  *-----------------------------------------------------------------------
    447  * JobContinueSig --
    448  *	Resume all stopped jobs.
    449  *
    450  * Input:
    451  *	signo		The signal number we've received
    452  *
    453  * Results:
    454  *	None.
    455  *
    456  * Side Effects:
    457  *	Jobs start running again.
    458  *
    459  *-----------------------------------------------------------------------
    460  */
    461 static void
    462 JobContinueSig(int signo __unused)
    463 {
    464     write(exit_pipe[1], DO_JOB_RESUME, 1);
    465 }
    466 #endif
    467 
    468 /*-
    469  *-----------------------------------------------------------------------
    470  * JobPassSig --
    471  *	Pass a signal on to all remote jobs and to all local jobs if
    472  *	USE_PGRP is defined, then die ourselves.
    473  *
    474  * Input:
    475  *	signo		The signal number we've received
    476  *
    477  * Results:
    478  *	None.
    479  *
    480  * Side Effects:
    481  *	We die by the same signal.
    482  *
    483  *-----------------------------------------------------------------------
    484  */
    485 static void
    486 JobPassSig(int signo)
    487 {
    488     sigset_t nmask, omask;
    489     struct sigaction act;
    490     int sigcont;
    491 
    492     if (DEBUG(JOB)) {
    493 	(void)fprintf(stdout, "JobPassSig(%d) called.\n", signo);
    494 	(void)fflush(stdout);
    495     }
    496     Lst_ForEach(jobs, JobCondPassSig, (ClientData) &signo);
    497 
    498     /*
    499      * Deal with proper cleanup based on the signal received. We only run
    500      * the .INTERRUPT target if the signal was in fact an interrupt. The other
    501      * three termination signals are more of a "get out *now*" command.
    502      */
    503     if (signo == SIGINT) {
    504 	JobInterrupt(TRUE, signo);
    505     } else if ((signo == SIGHUP) || (signo == SIGTERM) || (signo == SIGQUIT)) {
    506 	JobInterrupt(FALSE, signo);
    507     }
    508 
    509     /*
    510      * Leave gracefully if SIGQUIT, rather than core dumping.
    511      */
    512     if (signo == SIGQUIT) {
    513 	Finish(0);
    514     }
    515 
    516     if (signo == SIGTSTP) {
    517 	Job_CatchChildren(FALSE);
    518     }
    519     /*
    520      * Send ourselves the signal now we've given the message to everyone else.
    521      * Note we block everything else possible while we're getting the signal.
    522      * This ensures that all our jobs get continued when we wake up before
    523      * we take any other signal.
    524      */
    525     sigfillset(&nmask);
    526     sigdelset(&nmask, signo);
    527     (void)sigprocmask(SIG_SETMASK, &nmask, &omask);
    528 
    529     act.sa_handler = SIG_DFL;
    530     sigemptyset(&act.sa_mask);
    531     act.sa_flags = 0;
    532     (void)sigaction(signo, &act, NULL);
    533 
    534     if (DEBUG(JOB)) {
    535 	(void)fprintf(stdout,
    536 		       "JobPassSig passing signal %d to self.\n", signo);
    537 	(void)fflush(stdout);
    538     }
    539 
    540     (void)kill(getpid(), signo);
    541     if (signo != SIGTSTP) {
    542 	sigcont = SIGCONT;
    543 	Lst_ForEach(jobs, JobCondPassSig, (ClientData) &sigcont);
    544     }
    545 
    546     /* Restore handler and signal mask */
    547     act.sa_handler = JobPassSig;
    548     (void)sigaction(signo, &act, NULL);
    549     (void)sigprocmask(SIG_SETMASK, &omask, NULL);
    550 }
    551 
    552 /*-
    553  *-----------------------------------------------------------------------
    554  * JobCmpPid  --
    555  *	Compare the pid of the job with the given pid and return 0 if they
    556  *	are equal. This function is called from Job_CatchChildren via
    557  *	Lst_Find to find the job descriptor of the finished job.
    558  *
    559  * Input:
    560  *	job		job to examine
    561  *	pid		process id desired
    562  *
    563  * Results:
    564  *	0 if the pid's match
    565  *
    566  * Side Effects:
    567  *	None
    568  *-----------------------------------------------------------------------
    569  */
    570 static int
    571 JobCmpPid(ClientData job, ClientData pid)
    572 {
    573     return *(int *)pid - ((Job *)job)->pid;
    574 }
    575 
    576 /*-
    577  *-----------------------------------------------------------------------
    578  * JobPrintCommand  --
    579  *	Put out another command for the given job. If the command starts
    580  *	with an @ or a - we process it specially. In the former case,
    581  *	so long as the -s and -n flags weren't given to make, we stick
    582  *	a shell-specific echoOff command in the script. In the latter,
    583  *	we ignore errors for the entire job, unless the shell has error
    584  *	control.
    585  *	If the command is just "..." we take all future commands for this
    586  *	job to be commands to be executed once the entire graph has been
    587  *	made and return non-zero to signal that the end of the commands
    588  *	was reached. These commands are later attached to the postCommands
    589  *	node and executed by Job_End when all things are done.
    590  *	This function is called from JobStart via Lst_ForEach.
    591  *
    592  * Input:
    593  *	cmdp		command string to print
    594  *	jobp		job for which to print it
    595  *
    596  * Results:
    597  *	Always 0, unless the command was "..."
    598  *
    599  * Side Effects:
    600  *	If the command begins with a '-' and the shell has no error control,
    601  *	the JOB_IGNERR flag is set in the job descriptor.
    602  *	If the command is "..." and we're not ignoring such things,
    603  *	tailCmds is set to the successor node of the cmd.
    604  *	numCommands is incremented if the command is actually printed.
    605  *-----------------------------------------------------------------------
    606  */
    607 static int
    608 JobPrintCommand(ClientData cmdp, ClientData jobp)
    609 {
    610     Boolean	  noSpecials;	    /* true if we shouldn't worry about
    611 				     * inserting special commands into
    612 				     * the input stream. */
    613     Boolean       shutUp = FALSE;   /* true if we put a no echo command
    614 				     * into the command file */
    615     Boolean	  errOff = FALSE;   /* true if we turned error checking
    616 				     * off before printing the command
    617 				     * and need to turn it back on */
    618     const char    *cmdTemplate;	    /* Template to use when printing the
    619 				     * command */
    620     char    	  *cmdStart;	    /* Start of expanded command */
    621     char	  *escCmd = NULL;    /* Command with quotes/backticks escaped */
    622     char     	  *cmd = (char *)cmdp;
    623     Job           *job = (Job *)jobp;
    624     char	  *cp, *tmp;
    625     int           i, j;
    626 
    627     noSpecials = NoExecute(job->node);
    628 
    629     if (strcmp(cmd, "...") == 0) {
    630 	job->node->type |= OP_SAVE_CMDS;
    631 	if ((job->flags & JOB_IGNDOTS) == 0) {
    632 	    job->tailCmds = Lst_Succ(Lst_Member(job->node->commands,
    633 						(ClientData)cmd));
    634 	    return 1;
    635 	}
    636 	return 0;
    637     }
    638 
    639 #define DBPRINTF(fmt, arg) if (DEBUG(JOB)) {	\
    640 	(void)fprintf(stdout, fmt, arg); 	\
    641 	(void)fflush(stdout); 			\
    642     }						\
    643    (void)fprintf(job->cmdFILE, fmt, arg);	\
    644    (void)fflush(job->cmdFILE);
    645 
    646     numCommands += 1;
    647 
    648     cmdStart = cmd = Var_Subst(NULL, cmd, job->node, FALSE);
    649 
    650     cmdTemplate = "%s\n";
    651 
    652     /*
    653      * Check for leading @' and -'s to control echoing and error checking.
    654      */
    655     while (*cmd == '@' || *cmd == '-' || (*cmd == '+')) {
    656 	switch (*cmd) {
    657 	case '@':
    658 	    shutUp = TRUE;
    659 	    break;
    660 	case '-':
    661 	    errOff = TRUE;
    662 	    break;
    663 	case '+':
    664 	    if (noSpecials) {
    665 		/*
    666 		 * We're not actually executing anything...
    667 		 * but this one needs to be - use compat mode just for it.
    668 		 */
    669 		CompatRunCommand(cmdp, (ClientData)job->node);
    670 		return 0;
    671 	    }
    672 	    break;
    673 	}
    674 	cmd++;
    675     }
    676 
    677     while (isspace((unsigned char) *cmd))
    678 	cmd++;
    679 
    680     /*
    681      * If the shell doesn't have error control the alternate echo'ing will
    682      * be done (to avoid showing additional error checking code)
    683      * and this will need the characters '$ ` \ "' escaped
    684      */
    685 
    686     if (!commandShell->hasErrCtl) {
    687 	/* Worst that could happen is every char needs escaping. */
    688 	escCmd = emalloc((strlen(cmd) * 2) + 1);
    689 	for (i = 0, j= 0; cmd[i] != '\0'; i++, j++) {
    690 		if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' ||
    691 			cmd[i] == '"')
    692 			escCmd[j++] = '\\';
    693 		escCmd[j] = cmd[i];
    694 	}
    695 	escCmd[j] = 0;
    696     }
    697 
    698     if (shutUp) {
    699 	if (!(job->flags & JOB_SILENT) && !noSpecials &&
    700 	    commandShell->hasEchoCtl) {
    701 		DBPRINTF("%s\n", commandShell->echoOff);
    702 	} else {
    703 	    if (commandShell->hasErrCtl)
    704 		shutUp = FALSE;
    705 	}
    706     }
    707 
    708     if (errOff) {
    709 	if ( !(job->flags & JOB_IGNERR) && !noSpecials) {
    710 	    if (commandShell->hasErrCtl) {
    711 		/*
    712 		 * we don't want the error-control commands showing
    713 		 * up either, so we turn off echoing while executing
    714 		 * them. We could put another field in the shell
    715 		 * structure to tell JobDoOutput to look for this
    716 		 * string too, but why make it any more complex than
    717 		 * it already is?
    718 		 */
    719 		if (!(job->flags & JOB_SILENT) && !shutUp &&
    720 		    commandShell->hasEchoCtl) {
    721 			DBPRINTF("%s\n", commandShell->echoOff);
    722 			DBPRINTF("%s\n", commandShell->ignErr);
    723 			DBPRINTF("%s\n", commandShell->echoOn);
    724 		} else {
    725 			DBPRINTF("%s\n", commandShell->ignErr);
    726 		}
    727 	    } else if (commandShell->ignErr &&
    728 		      (*commandShell->ignErr != '\0'))
    729 	    {
    730 		/*
    731 		 * The shell has no error control, so we need to be
    732 		 * weird to get it to ignore any errors from the command.
    733 		 * If echoing is turned on, we turn it off and use the
    734 		 * errCheck template to echo the command. Leave echoing
    735 		 * off so the user doesn't see the weirdness we go through
    736 		 * to ignore errors. Set cmdTemplate to use the weirdness
    737 		 * instead of the simple "%s\n" template.
    738 		 */
    739 		if (!(job->flags & JOB_SILENT) && !shutUp) {
    740 			if (commandShell->hasEchoCtl) {
    741 				DBPRINTF("%s\n", commandShell->echoOff);
    742 			}
    743 			DBPRINTF(commandShell->errCheck, escCmd);
    744 			shutUp = TRUE;
    745 		} else {
    746 			if (!shutUp) {
    747 				DBPRINTF(commandShell->errCheck, escCmd);
    748 			}
    749 		}
    750 		cmdTemplate = commandShell->ignErr;
    751 		/*
    752 		 * The error ignoration (hee hee) is already taken care
    753 		 * of by the ignErr template, so pretend error checking
    754 		 * is still on.
    755 		 */
    756 		errOff = FALSE;
    757 	    } else {
    758 		errOff = FALSE;
    759 	    }
    760 	} else {
    761 	    errOff = FALSE;
    762 	}
    763     } else {
    764 
    765 	/*
    766 	 * If errors are being checked and the shell doesn't have error control
    767 	 * but does supply an errOut template, then setup commands to run
    768 	 * through it.
    769 	 */
    770 
    771 	if (!commandShell->hasErrCtl && commandShell->errOut &&
    772 	    (*commandShell->errOut != '\0')) {
    773 		if (!(job->flags & JOB_SILENT) && !shutUp) {
    774 			if (commandShell->hasEchoCtl) {
    775 				DBPRINTF("%s\n", commandShell->echoOff);
    776 			}
    777 			DBPRINTF(commandShell->errCheck, escCmd);
    778 			shutUp = TRUE;
    779 		}
    780 		/* If it's a comment line or blank, treat as an ignored error */
    781 		if ((escCmd[0] == commandShell->commentChar) ||
    782 		    (escCmd[0] == 0))
    783 			cmdTemplate = commandShell->ignErr;
    784 		else
    785 			cmdTemplate = commandShell->errOut;
    786 		errOff = FALSE;
    787 	}
    788     }
    789 
    790     if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0 &&
    791 	(job->flags & JOB_TRACED) == 0) {
    792 	    DBPRINTF("set -%s\n", "x");
    793 	    job->flags |= JOB_TRACED;
    794     }
    795 
    796     if ((cp = Check_Cwd_Cmd(cmd)) != NULL) {
    797 	    DBPRINTF("test -d %s && ", cp);
    798 	    DBPRINTF("cd %s\n", cp);
    799     }
    800 
    801     DBPRINTF(cmdTemplate, cmd);
    802     free(cmdStart);
    803     if (escCmd)
    804         free(escCmd);
    805     if (errOff) {
    806 	/*
    807 	 * If echoing is already off, there's no point in issuing the
    808 	 * echoOff command. Otherwise we issue it and pretend it was on
    809 	 * for the whole command...
    810 	 */
    811 	if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl){
    812 	    DBPRINTF("%s\n", commandShell->echoOff);
    813 	    shutUp = TRUE;
    814 	}
    815 	DBPRINTF("%s\n", commandShell->errCheck);
    816     }
    817     if (shutUp && commandShell->hasEchoCtl) {
    818 	DBPRINTF("%s\n", commandShell->echoOn);
    819     }
    820     if (cp != NULL) {
    821 	    DBPRINTF("test -d %s && ", cp);
    822 	    DBPRINTF("cd %s\n", Var_Value(".OBJDIR", VAR_GLOBAL, &tmp));
    823     }
    824     return 0;
    825 }
    826 
    827 /*-
    828  *-----------------------------------------------------------------------
    829  * JobSaveCommand --
    830  *	Save a command to be executed when everything else is done.
    831  *	Callback function for JobFinish...
    832  *
    833  * Results:
    834  *	Always returns 0
    835  *
    836  * Side Effects:
    837  *	The command is tacked onto the end of postCommands's commands list.
    838  *
    839  *-----------------------------------------------------------------------
    840  */
    841 static int
    842 JobSaveCommand(ClientData cmd, ClientData gn)
    843 {
    844     cmd = (ClientData)Var_Subst(NULL, (char *)cmd, (GNode *)gn, FALSE);
    845     (void)Lst_AtEnd(postCommands->commands, cmd);
    846     return(0);
    847 }
    848 
    849 
    850 /*-
    851  *-----------------------------------------------------------------------
    852  * JobClose --
    853  *	Called to close both input and output pipes when a job is finished.
    854  *
    855  * Results:
    856  *	Nada
    857  *
    858  * Side Effects:
    859  *	The file descriptors associated with the job are closed.
    860  *
    861  *-----------------------------------------------------------------------
    862  */
    863 static void
    864 JobClose(Job *job)
    865 {
    866     if (usePipes) {
    867 	clearfd(job);
    868 	if (job->outPipe != job->inPipe) {
    869 	   (void)close(job->outPipe);
    870 	}
    871 	JobDoOutput(job, TRUE);
    872 	(void)close(job->inPipe);
    873     } else {
    874 	(void)close(job->outFd);
    875 	JobDoOutput(job, TRUE);
    876     }
    877 }
    878 
    879 /*-
    880  *-----------------------------------------------------------------------
    881  * JobFinish  --
    882  *	Do final processing for the given job including updating
    883  *	parents and starting new jobs as available/necessary. Note
    884  *	that we pay no attention to the JOB_IGNERR flag here.
    885  *	This is because when we're called because of a noexecute flag
    886  *	or something, jstat.w_status is 0 and when called from
    887  *	Job_CatchChildren, the status is zeroed if it s/b ignored.
    888  *
    889  * Input:
    890  *	job		job to finish
    891  *	status		sub-why job went away
    892  *
    893  * Results:
    894  *	None
    895  *
    896  * Side Effects:
    897  *	Final commands for the job are placed on postCommands.
    898  *
    899  *	If we got an error and are aborting (aborting == ABORT_ERROR) and
    900  *	the job list is now empty, we are done for the day.
    901  *	If we recognized an error (errors !=0), we set the aborting flag
    902  *	to ABORT_ERROR so no more jobs will be started.
    903  *-----------------------------------------------------------------------
    904  */
    905 /*ARGSUSED*/
    906 static void
    907 JobFinish(Job *job, int *status)
    908 {
    909     Boolean 	 done, return_job_token;
    910 
    911     if ((WIFEXITED(*status) &&
    912 	 (((WEXITSTATUS(*status) != 0) && !(job->flags & JOB_IGNERR)))) ||
    913 	WIFSIGNALED(*status))
    914     {
    915 	/*
    916 	 * If it exited non-zero and either we're doing things our
    917 	 * way or we're not ignoring errors, the job is finished.
    918 	 * Similarly, if the shell died because of a signal
    919 	 * the job is also finished. In these
    920 	 * cases, finish out the job's output before printing the exit
    921 	 * status...
    922 	 */
    923 	JobClose(job);
    924 	if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
    925 	   (void)fclose(job->cmdFILE);
    926 	   job->cmdFILE = NULL;
    927 	}
    928 	done = TRUE;
    929     } else if (WIFEXITED(*status)) {
    930 	/*
    931 	 * Deal with ignored errors in -B mode. We need to print a message
    932 	 * telling of the ignored error as well as setting status.w_status
    933 	 * to 0 so the next command gets run. To do this, we set done to be
    934 	 * TRUE if in -B mode and the job exited non-zero.
    935 	 */
    936 	done = WEXITSTATUS(*status) != 0;
    937 	/*
    938 	 * Old comment said: "Note we don't
    939 	 * want to close down any of the streams until we know we're at the
    940 	 * end."
    941 	 * But we do. Otherwise when are we going to print the rest of the
    942 	 * stuff?
    943 	 */
    944 	JobClose(job);
    945     } else {
    946 	/*
    947 	 * No need to close things down or anything.
    948 	 */
    949 	done = FALSE;
    950     }
    951 
    952     if (done || WIFSTOPPED(*status) ||
    953 	    (WIFSIGNALED(*status) && (WTERMSIG(*status) == SIGCONT))) {
    954 
    955 	if (WIFEXITED(*status)) {
    956 	    if (DEBUG(JOB)) {
    957 		(void)fprintf(stdout, "Process %d [%s] exited.\n",
    958 				job->pid, job->node->name);
    959 		(void)fflush(stdout);
    960 	    }
    961 	    if (WEXITSTATUS(*status) != 0) {
    962 		if (usePipes && job->node != lastNode) {
    963 		    MESSAGE(stdout, job->node);
    964 		    lastNode = job->node;
    965 		}
    966 		(void)printf("*** [%s] Error code %d%s\n",
    967 				job->node->name,
    968 			       WEXITSTATUS(*status),
    969 			       (job->flags & JOB_IGNERR) ? "(ignored)" : "");
    970 
    971 		if (job->flags & JOB_IGNERR) {
    972 		    *status = 0;
    973 		}
    974 	    } else if (DEBUG(JOB)) {
    975 		if (usePipes && job->node != lastNode) {
    976 		    MESSAGE(stdout, job->node);
    977 		    lastNode = job->node;
    978 		}
    979 		(void)printf("*** [%s] Completed successfully\n",
    980 				job->node->name);
    981 	    }
    982 	} else if (WIFSTOPPED(*status) && WSTOPSIG(*status) != SIGCONT) {
    983 	    if (DEBUG(JOB)) {
    984 		(void)fprintf(stdout, "Process %d (%s) stopped.\n",
    985 				job->pid, job->node->name);
    986 		(void)fflush(stdout);
    987 	    }
    988 	    if (usePipes && job->node != lastNode) {
    989 		MESSAGE(stdout, job->node);
    990 		lastNode = job->node;
    991 	    }
    992 	    switch (WSTOPSIG(*status)) {
    993 	    case SIGTSTP:
    994 		(void)printf("*** [%s] Suspended\n",
    995 			    job->node->name);
    996 		break;
    997 	    case SIGSTOP:
    998 		(void)printf("*** [%s] Stopped\n",
    999 			    job->node->name);
   1000 		break;
   1001 	    default:
   1002 		(void)printf("*** [%s] Stopped -- signal %d\n",
   1003 		    job->node->name, WSTOPSIG(*status));
   1004 	    }
   1005 	    job->flags |= JOB_RESUME;
   1006 	    (void)Lst_AtEnd(stoppedJobs, (ClientData)job);
   1007 	    (void)fflush(stdout);
   1008 	    return;
   1009 	} else if (WIFSTOPPED(*status) && WSTOPSIG(*status) == SIGCONT) {
   1010 	    /*
   1011 	     * If the beastie has continued, shift the Job from the stopped
   1012 	     * list to the running one.
   1013 	     */
   1014 	    if (job->flags & JOB_RESUME) {
   1015 		if (usePipes && job->node != lastNode) {
   1016 		    MESSAGE(stdout, job->node);
   1017 		    lastNode = job->node;
   1018 		}
   1019 		(void)printf("*** [%s] Continued\n", job->node->name);
   1020 	    }
   1021 	    if (!(job->flags & JOB_CONTINUING)) {
   1022 		if (DEBUG(JOB)) {
   1023 		    (void)fprintf(stdout,
   1024 			   "Warning: process %d [%s] was not continuing.\n",
   1025 			   job->pid, job->node->name);
   1026 		    (void)fflush(stdout);
   1027 		}
   1028 	    }
   1029 	    job->flags &= ~JOB_CONTINUING;
   1030  	    Lst_AtEnd(jobs, (ClientData)job);
   1031 	    if (DEBUG(JOB)) {
   1032 		(void)fprintf(stdout, "Process %d is continuing.\n",
   1033 			       job->pid);
   1034 		(void)fflush(stdout);
   1035 	    }
   1036 	    (void)fflush(stdout);
   1037   	    return;
   1038 	} else {
   1039 	    if (usePipes && job->node != lastNode) {
   1040 		MESSAGE(stdout, job->node);
   1041 		lastNode = job->node;
   1042 	    }
   1043 	    (void)printf("*** [%s] Signal %d\n",
   1044 			job->node->name, WTERMSIG(*status));
   1045 	}
   1046 
   1047 	(void)fflush(stdout);
   1048     }
   1049 
   1050     return_job_token = FALSE;
   1051 
   1052     Trace_Log(JOBEND, job);
   1053     if (!(job->flags & JOB_SPECIAL)) {
   1054 	if ((*status != 0) ||
   1055 		(aborting == ABORT_ERROR) ||
   1056 		(aborting == ABORT_INTERRUPT))
   1057 	    return_job_token = TRUE;
   1058     }
   1059 
   1060     if ((aborting != ABORT_ERROR) && (aborting != ABORT_INTERRUPT) && (*status == 0)) {
   1061 	/*
   1062 	 * As long as we aren't aborting and the job didn't return a non-zero
   1063 	 * status that we shouldn't ignore, we call Make_Update to update
   1064 	 * the parents. In addition, any saved commands for the node are placed
   1065 	 * on the .END target.
   1066 	 */
   1067 	if (job->tailCmds != NILLNODE) {
   1068 	    Lst_ForEachFrom(job->node->commands, job->tailCmds,
   1069 			     JobSaveCommand,
   1070 			    (ClientData)job->node);
   1071 	}
   1072 	job->node->made = MADE;
   1073 	if (!(job->flags & JOB_SPECIAL))
   1074 	    return_job_token = TRUE;
   1075 	Make_Update(job->node);
   1076 	free(job);
   1077     } else if (*status != 0) {
   1078 	errors += 1;
   1079 	free(job);
   1080     }
   1081     JobRestartJobs();
   1082 
   1083     /*
   1084      * Set aborting if any error.
   1085      */
   1086     if (errors && !keepgoing && (aborting != ABORT_INTERRUPT)) {
   1087 	/*
   1088 	 * If we found any errors in this batch of children and the -k flag
   1089 	 * wasn't given, we set the aborting flag so no more jobs get
   1090 	 * started.
   1091 	 */
   1092 	aborting = ABORT_ERROR;
   1093     }
   1094 
   1095     if (return_job_token)
   1096 	Job_TokenReturn();
   1097 
   1098     if ((aborting == ABORT_ERROR) && Job_Empty()) {
   1099 	/*
   1100 	 * If we are aborting and the job table is now empty, we finish.
   1101 	 */
   1102 	Finish(errors);
   1103     }
   1104 }
   1105 
   1106 /*-
   1107  *-----------------------------------------------------------------------
   1108  * Job_Touch --
   1109  *	Touch the given target. Called by JobStart when the -t flag was
   1110  *	given
   1111  *
   1112  * Input:
   1113  *	gn		the node of the file to touch
   1114  *	silent		TRUE if should not print message
   1115  *
   1116  * Results:
   1117  *	None
   1118  *
   1119  * Side Effects:
   1120  *	The data modification of the file is changed. In addition, if the
   1121  *	file did not exist, it is created.
   1122  *-----------------------------------------------------------------------
   1123  */
   1124 void
   1125 Job_Touch(GNode *gn, Boolean silent)
   1126 {
   1127     int		  streamID;   	/* ID of stream opened to do the touch */
   1128     struct utimbuf times;	/* Times for utime() call */
   1129 
   1130     if (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC|OP_OPTIONAL|OP_PHONY)) {
   1131 	/*
   1132 	 * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" targets
   1133 	 * and, as such, shouldn't really be created.
   1134 	 */
   1135 	return;
   1136     }
   1137 
   1138     if (!silent || NoExecute(gn)) {
   1139 	(void)fprintf(stdout, "touch %s\n", gn->name);
   1140 	(void)fflush(stdout);
   1141     }
   1142 
   1143     if (NoExecute(gn)) {
   1144 	return;
   1145     }
   1146 
   1147     if (gn->type & OP_ARCHV) {
   1148 	Arch_Touch(gn);
   1149     } else if (gn->type & OP_LIB) {
   1150 	Arch_TouchLib(gn);
   1151     } else {
   1152 	char	*file = gn->path ? gn->path : gn->name;
   1153 
   1154 	times.actime = times.modtime = now;
   1155 	if (utime(file, &times) < 0){
   1156 	    streamID = open(file, O_RDWR | O_CREAT, 0666);
   1157 
   1158 	    if (streamID >= 0) {
   1159 		char	c;
   1160 
   1161 		/*
   1162 		 * Read and write a byte to the file to change the
   1163 		 * modification time, then close the file.
   1164 		 */
   1165 		if (read(streamID, &c, 1) == 1) {
   1166 		    (void)lseek(streamID, (off_t)0, SEEK_SET);
   1167 		    (void)write(streamID, &c, 1);
   1168 		}
   1169 
   1170 		(void)close(streamID);
   1171 	    } else {
   1172 		(void)fprintf(stdout, "*** couldn't touch %s: %s",
   1173 			       file, strerror(errno));
   1174 		(void)fflush(stdout);
   1175 	    }
   1176 	}
   1177     }
   1178 }
   1179 
   1180 /*-
   1181  *-----------------------------------------------------------------------
   1182  * Job_CheckCommands --
   1183  *	Make sure the given node has all the commands it needs.
   1184  *
   1185  * Input:
   1186  *	gn		The target whose commands need verifying
   1187  *	abortProc	Function to abort with message
   1188  *
   1189  * Results:
   1190  *	TRUE if the commands list is/was ok.
   1191  *
   1192  * Side Effects:
   1193  *	The node will have commands from the .DEFAULT rule added to it
   1194  *	if it needs them.
   1195  *-----------------------------------------------------------------------
   1196  */
   1197 Boolean
   1198 Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
   1199 {
   1200     if (OP_NOP(gn->type) && Lst_IsEmpty(gn->commands) &&
   1201 	((gn->type & OP_LIB) == 0 || Lst_IsEmpty(gn->children))) {
   1202 	/*
   1203 	 * No commands. Look for .DEFAULT rule from which we might infer
   1204 	 * commands
   1205 	 */
   1206 	if ((DEFAULT != NILGNODE) && !Lst_IsEmpty(DEFAULT->commands) &&
   1207 		(gn->type & OP_SPECIAL) == 0) {
   1208 	    char *p1;
   1209 	    /*
   1210 	     * Make only looks for a .DEFAULT if the node was never the
   1211 	     * target of an operator, so that's what we do too. If
   1212 	     * a .DEFAULT was given, we substitute its commands for gn's
   1213 	     * commands and set the IMPSRC variable to be the target's name
   1214 	     * The DEFAULT node acts like a transformation rule, in that
   1215 	     * gn also inherits any attributes or sources attached to
   1216 	     * .DEFAULT itself.
   1217 	     */
   1218 	    Make_HandleUse(DEFAULT, gn);
   1219 	    Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), gn, 0);
   1220 	    if (p1)
   1221 		free(p1);
   1222 	} else if (Dir_MTime(gn) == 0 && (gn->type & OP_SPECIAL) == 0) {
   1223 	    /*
   1224 	     * The node wasn't the target of an operator we have no .DEFAULT
   1225 	     * rule to go on and the target doesn't already exist. There's
   1226 	     * nothing more we can do for this branch. If the -k flag wasn't
   1227 	     * given, we stop in our tracks, otherwise we just don't update
   1228 	     * this node's parents so they never get examined.
   1229 	     */
   1230 	    static const char msg[] = ": don't know how to make";
   1231 
   1232 	    if (gn->type & OP_OPTIONAL) {
   1233 		(void)fprintf(stdout, "%s%s %s(ignored)\n", progname,
   1234 		    msg, gn->name);
   1235 		(void)fflush(stdout);
   1236 	    } else if (keepgoing) {
   1237 		(void)fprintf(stdout, "%s%s %s(continuing)\n", progname,
   1238 		    msg, gn->name);
   1239 		(void)fflush(stdout);
   1240   		return FALSE;
   1241 	    } else {
   1242 		(*abortProc)("%s%s %s. Stop", progname, msg, gn->name);
   1243 		return FALSE;
   1244 	    }
   1245 	}
   1246     }
   1247     return TRUE;
   1248 }
   1249 
   1250 /*-
   1251  *-----------------------------------------------------------------------
   1252  * JobExec --
   1253  *	Execute the shell for the given job. Called from JobStart and
   1254  *	JobRestart.
   1255  *
   1256  * Input:
   1257  *	job		Job to execute
   1258  *
   1259  * Results:
   1260  *	None.
   1261  *
   1262  * Side Effects:
   1263  *	A shell is executed, outputs is altered and the Job structure added
   1264  *	to the job table.
   1265  *
   1266  *-----------------------------------------------------------------------
   1267  */
   1268 static void
   1269 JobExec(Job *job, char **argv)
   1270 {
   1271     int	    	  cpid;	    	/* ID of new child */
   1272     sigset_t	  mask;
   1273 
   1274     job->flags &= ~JOB_TRACED;
   1275 
   1276     if (DEBUG(JOB)) {
   1277 	int 	  i;
   1278 
   1279 	(void)fprintf(stdout, "Running %s %sly\n", job->node->name, "local");
   1280 	(void)fprintf(stdout, "\tCommand: ");
   1281 	for (i = 0; argv[i] != NULL; i++) {
   1282 	    (void)fprintf(stdout, "%s ", argv[i]);
   1283 	}
   1284  	(void)fprintf(stdout, "\n");
   1285  	(void)fflush(stdout);
   1286     }
   1287 
   1288     /*
   1289      * Some jobs produce no output and it's disconcerting to have
   1290      * no feedback of their running (since they produce no output, the
   1291      * banner with their name in it never appears). This is an attempt to
   1292      * provide that feedback, even if nothing follows it.
   1293      */
   1294     if ((lastNode != job->node) && !(job->flags & JOB_SILENT)) {
   1295 	MESSAGE(stdout, job->node);
   1296 	lastNode = job->node;
   1297     }
   1298 
   1299     /* No interruptions until this job is on the `jobs' list */
   1300     JobSigLock(&mask);
   1301 
   1302     if ((cpid = vfork()) == -1) {
   1303 	Punt("Cannot vfork: %s", strerror(errno));
   1304     } else if (cpid == 0) {
   1305 
   1306 	/*
   1307 	 * Reset all signal handlers; this is necessary because we also
   1308 	 * need to unblock signals before we exec(2).
   1309 	 */
   1310 	JobSigReset();
   1311 
   1312 	/* Now unblock signals */
   1313 	sigemptyset(&mask);
   1314 	JobSigUnlock(&mask);
   1315 
   1316 	/*
   1317 	 * Must duplicate the input stream down to the child's input and
   1318 	 * reset it to the beginning (again). Since the stream was marked
   1319 	 * close-on-exec, we must clear that bit in the new input.
   1320 	 */
   1321 	if (dup2(FILENO(job->cmdFILE), 0) == -1) {
   1322 	    execError("dup2", "job->cmdFILE");
   1323 	    _exit(1);
   1324 	}
   1325 	(void)fcntl(0, F_SETFD, 0);
   1326 	(void)lseek(0, (off_t)0, SEEK_SET);
   1327 
   1328 	if (job->node->type & OP_MAKE) {
   1329 		/*
   1330 		 * Pass job token pipe to submakes.
   1331 		 */
   1332 		fcntl(job_pipe[0], F_SETFD, 0);
   1333 		fcntl(job_pipe[1], F_SETFD, 0);
   1334 	}
   1335 
   1336 	if (usePipes) {
   1337 	    /*
   1338 	     * Set up the child's output to be routed through the pipe
   1339 	     * we've created for it.
   1340 	     */
   1341 	    if (dup2(job->outPipe, 1) == -1) {
   1342 		execError("dup2", "job->outPipe");
   1343 		_exit(1);
   1344 	    }
   1345 	} else {
   1346 	    /*
   1347 	     * We're capturing output in a file, so we duplicate the
   1348 	     * descriptor to the temporary file into the standard
   1349 	     * output.
   1350 	     */
   1351 	    if (dup2(job->outFd, 1) == -1) {
   1352 		execError("dup2", "job->outFd");
   1353 		_exit(1);
   1354 	    }
   1355 	}
   1356 	/*
   1357 	 * The output channels are marked close on exec. This bit was
   1358 	 * duplicated by the dup2(on some systems), so we have to clear
   1359 	 * it before routing the shell's error output to the same place as
   1360 	 * its standard output.
   1361 	 */
   1362 	(void)fcntl(1, F_SETFD, 0);
   1363 	if (dup2(1, 2) == -1) {
   1364 	    execError("dup2", "1, 2");
   1365 	    _exit(1);
   1366 	}
   1367 
   1368 #ifdef USE_PGRP
   1369 	/*
   1370 	 * We want to switch the child into a different process family so
   1371 	 * we can kill it and all its descendants in one fell swoop,
   1372 	 * by killing its process family, but not commit suicide.
   1373 	 */
   1374 # if defined(SYSV)
   1375 	(void)setsid();
   1376 # else
   1377 	(void)setpgid(0, getpid());
   1378 # endif
   1379 #endif /* USE_PGRP */
   1380 
   1381 	(void)execv(shellPath, argv);
   1382 	execError("exec", shellPath);
   1383 	_exit(1);
   1384     } else {
   1385 	job->pid = cpid;
   1386 
   1387 	Trace_Log(JOBSTART, job);
   1388 
   1389 	if (usePipes) {
   1390 	    /*
   1391 	     * Set the current position in the buffer to the beginning
   1392 	     * and mark another stream to watch in the outputs mask
   1393 	     */
   1394 	    job->curPos = 0;
   1395 
   1396 	    watchfd(job);
   1397 	}
   1398 
   1399 	if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
   1400 	    (void)fclose(job->cmdFILE);
   1401 	    job->cmdFILE = NULL;
   1402 	}
   1403     }
   1404 
   1405     /*
   1406      * Now the job is actually running, add it to the table.
   1407      */
   1408     if (DEBUG(JOB)) {
   1409 	printf("JobExec(%s): pid %d added to jobs table\n",
   1410 		job->node->name, job->pid);
   1411     }
   1412     (void)Lst_AtEnd(jobs, (ClientData)job);
   1413     JobSigUnlock(&mask);
   1414 }
   1415 
   1416 /*-
   1417  *-----------------------------------------------------------------------
   1418  * JobMakeArgv --
   1419  *	Create the argv needed to execute the shell for a given job.
   1420  *
   1421  *
   1422  * Results:
   1423  *
   1424  * Side Effects:
   1425  *
   1426  *-----------------------------------------------------------------------
   1427  */
   1428 static void
   1429 JobMakeArgv(Job *job, char **argv)
   1430 {
   1431     int	    	  argc;
   1432     static char args[10]; 	/* For merged arguments */
   1433 
   1434     argv[0] = UNCONST(shellName);
   1435     argc = 1;
   1436 
   1437     if ((commandShell->exit && (*commandShell->exit != '-')) ||
   1438 	(commandShell->echo && (*commandShell->echo != '-')))
   1439     {
   1440 	/*
   1441 	 * At least one of the flags doesn't have a minus before it, so
   1442 	 * merge them together. Have to do this because the *(&(@*#*&#$#
   1443 	 * Bourne shell thinks its second argument is a file to source.
   1444 	 * Grrrr. Note the ten-character limitation on the combined arguments.
   1445 	 */
   1446 	(void)snprintf(args, sizeof(args), "-%s%s",
   1447 		      ((job->flags & JOB_IGNERR) ? "" :
   1448 		       (commandShell->exit ? commandShell->exit : "")),
   1449 		      ((job->flags & JOB_SILENT) ? "" :
   1450 		       (commandShell->echo ? commandShell->echo : "")));
   1451 
   1452 	if (args[1]) {
   1453 	    argv[argc] = args;
   1454 	    argc++;
   1455 	}
   1456     } else {
   1457 	if (!(job->flags & JOB_IGNERR) && commandShell->exit) {
   1458 	    argv[argc] = UNCONST(commandShell->exit);
   1459 	    argc++;
   1460 	}
   1461 	if (!(job->flags & JOB_SILENT) && commandShell->echo) {
   1462 	    argv[argc] = UNCONST(commandShell->echo);
   1463 	    argc++;
   1464 	}
   1465     }
   1466     argv[argc] = NULL;
   1467 }
   1468 
   1469 /*-
   1470  *-----------------------------------------------------------------------
   1471  * JobRestart --
   1472  *	Restart a job that stopped for some reason.
   1473  *
   1474  * Input:
   1475  *	job		Job to restart
   1476  *
   1477  * Results:
   1478  *	1 if max number of running jobs has been reached, 0 otherwise.
   1479  *
   1480  *-----------------------------------------------------------------------
   1481  */
   1482 static int
   1483 JobRestart(Job *job)
   1484 {
   1485     Boolean error;
   1486     int status;
   1487 
   1488     /*
   1489      * The job has stopped and needs to be restarted. Why it stopped,
   1490      * we don't know...
   1491      */
   1492     if (DEBUG(JOB)) {
   1493        (void)fprintf(stdout, "Resuming %s...", job->node->name);
   1494        (void)fflush(stdout);
   1495     }
   1496 
   1497     error = (KILL(job->pid, SIGCONT) != 0);
   1498 
   1499     if (!error) {
   1500 	/*
   1501 	 * Make sure the user knows we've continued the beast and
   1502 	 * actually put the thing in the job table.
   1503 	 */
   1504 	job->flags |= JOB_CONTINUING;
   1505 	status = W_STOPCODE(SIGCONT);
   1506 	JobFinish(job, &status);
   1507 
   1508 	job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
   1509 	if (DEBUG(JOB)) {
   1510 	   (void)fprintf(stdout, "done\n");
   1511 	   (void)fflush(stdout);
   1512 	}
   1513     } else {
   1514 	Error("couldn't resume %s: %s",
   1515 	    job->node->name, strerror(errno));
   1516 	status = W_EXITCODE(1, 0);
   1517 	JobFinish(job, &status);
   1518     }
   1519     return 0;
   1520 }
   1521 
   1522 /*-
   1523  *-----------------------------------------------------------------------
   1524  * JobStart  --
   1525  *	Start a target-creation process going for the target described
   1526  *	by the graph node gn.
   1527  *
   1528  * Input:
   1529  *	gn		target to create
   1530  *	flags		flags for the job to override normal ones.
   1531  *			e.g. JOB_SPECIAL or JOB_IGNDOTS
   1532  *	previous	The previous Job structure for this node, if any.
   1533  *
   1534  * Results:
   1535  *	JOB_ERROR if there was an error in the commands, JOB_FINISHED
   1536  *	if there isn't actually anything left to do for the job and
   1537  *	JOB_RUNNING if the job has been started.
   1538  *
   1539  * Side Effects:
   1540  *	A new Job node is created and added to the list of running
   1541  *	jobs. PMake is forked and a child shell created.
   1542  *
   1543  * NB: I'm fairly sure that this code is never called with JOB_SPECIAL set
   1544  *     JOB_IGNDOTS is never set (dsl)
   1545  *-----------------------------------------------------------------------
   1546  */
   1547 static int
   1548 JobStart(GNode *gn, int flags)
   1549 {
   1550     Job		  *job;       /* new job descriptor */
   1551     char	  *argv[10];  /* Argument vector to shell */
   1552     Boolean	  cmdsOK;     /* true if the nodes commands were all right */
   1553     Boolean 	  noExec;     /* Set true if we decide not to run the job */
   1554     int		  tfd;	      /* File descriptor to the temp file */
   1555 
   1556     job = emalloc(sizeof(Job));
   1557     if (job == NULL)
   1558 	Punt("JobStart out of memory");
   1559     if (gn->type & OP_SPECIAL)
   1560 	flags |= JOB_SPECIAL;
   1561 
   1562     job->node = gn;
   1563     job->tailCmds = NILLNODE;
   1564 
   1565     /*
   1566      * Set the initial value of the flags for this job based on the global
   1567      * ones and the node's attributes... Any flags supplied by the caller
   1568      * are also added to the field.
   1569      */
   1570     job->flags = 0;
   1571     if (Targ_Ignore(gn)) {
   1572 	job->flags |= JOB_IGNERR;
   1573     }
   1574     if (Targ_Silent(gn)) {
   1575 	job->flags |= JOB_SILENT;
   1576     }
   1577     job->flags |= flags;
   1578 
   1579     /*
   1580      * Check the commands now so any attributes from .DEFAULT have a chance
   1581      * to migrate to the node
   1582      */
   1583     cmdsOK = Job_CheckCommands(gn, Error);
   1584 
   1585     job->inPollfd = NULL;
   1586     /*
   1587      * If the -n flag wasn't given, we open up OUR (not the child's)
   1588      * temporary file to stuff commands in it. The thing is rd/wr so we don't
   1589      * need to reopen it to feed it to the shell. If the -n flag *was* given,
   1590      * we just set the file to be stdout. Cute, huh?
   1591      */
   1592     if (((gn->type & OP_MAKE) && !(noRecursiveExecute)) ||
   1593 	(!noExecute && !touchFlag)) {
   1594 	/*
   1595 	 * tfile is the name of a file into which all shell commands are
   1596 	 * put. It is used over by removing it before the child shell is
   1597 	 * executed. The XXXXXX in the string are replaced by the pid of
   1598 	 * the make process in a 6-character field with leading zeroes.
   1599 	 */
   1600 	char     tfile[sizeof(TMPPAT)];
   1601 	sigset_t mask;
   1602 	/*
   1603 	 * We're serious here, but if the commands were bogus, we're
   1604 	 * also dead...
   1605 	 */
   1606 	if (!cmdsOK) {
   1607 	    DieHorribly();
   1608 	}
   1609 
   1610 	JobSigLock(&mask);
   1611 	(void)strcpy(tfile, TMPPAT);
   1612 	if ((tfd = mkstemp(tfile)) == -1)
   1613 	    Punt("Could not create temporary file %s", strerror(errno));
   1614 	if (!DEBUG(SCRIPT))
   1615 		(void)eunlink(tfile);
   1616 	JobSigUnlock(&mask);
   1617 
   1618 	job->cmdFILE = fdopen(tfd, "w+");
   1619 	if (job->cmdFILE == NULL) {
   1620 	    Punt("Could not fdopen %s", tfile);
   1621 	}
   1622 	(void)fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
   1623 	/*
   1624 	 * Send the commands to the command file, flush all its buffers then
   1625 	 * rewind and remove the thing.
   1626 	 */
   1627 	noExec = FALSE;
   1628 
   1629 	/*
   1630 	 * We can do all the commands at once. hooray for sanity
   1631 	 */
   1632 	numCommands = 0;
   1633 	Lst_ForEach(gn->commands, JobPrintCommand, (ClientData)job);
   1634 
   1635 	/*
   1636 	 * If we didn't print out any commands to the shell script,
   1637 	 * there's not much point in executing the shell, is there?
   1638 	 */
   1639 	if (numCommands == 0) {
   1640 	    noExec = TRUE;
   1641 	}
   1642     } else if (NoExecute(gn)) {
   1643 	/*
   1644 	 * Not executing anything -- just print all the commands to stdout
   1645 	 * in one fell swoop. This will still set up job->tailCmds correctly.
   1646 	 */
   1647 	if (lastNode != gn) {
   1648 	    MESSAGE(stdout, gn);
   1649 	    lastNode = gn;
   1650 	}
   1651 	job->cmdFILE = stdout;
   1652 	/*
   1653 	 * Only print the commands if they're ok, but don't die if they're
   1654 	 * not -- just let the user know they're bad and keep going. It
   1655 	 * doesn't do any harm in this case and may do some good.
   1656 	 */
   1657 	if (cmdsOK) {
   1658 	    Lst_ForEach(gn->commands, JobPrintCommand, (ClientData)job);
   1659 	}
   1660 	/*
   1661 	 * Don't execute the shell, thank you.
   1662 	 */
   1663 	noExec = TRUE;
   1664     } else {
   1665 	/*
   1666 	 * Just touch the target and note that no shell should be executed.
   1667 	 * Set cmdFILE to stdout to make life easier. Check the commands, too,
   1668 	 * but don't die if they're no good -- it does no harm to keep working
   1669 	 * up the graph.
   1670 	 */
   1671 	job->cmdFILE = stdout;
   1672     	Job_Touch(gn, job->flags&JOB_SILENT);
   1673 	noExec = TRUE;
   1674     }
   1675 
   1676     /*
   1677      * If we're not supposed to execute a shell, don't.
   1678      */
   1679     if (noExec) {
   1680 	if (!(job->flags & JOB_SPECIAL))
   1681 	    Job_TokenReturn();
   1682 	/*
   1683 	 * Unlink and close the command file if we opened one
   1684 	 */
   1685 	if (job->cmdFILE != stdout) {
   1686 	    if (job->cmdFILE != NULL) {
   1687 		(void)fclose(job->cmdFILE);
   1688 		job->cmdFILE = NULL;
   1689 	    }
   1690 	} else {
   1691 	     (void)fflush(stdout);
   1692 	}
   1693 
   1694 	/*
   1695 	 * We only want to work our way up the graph if we aren't here because
   1696 	 * the commands for the job were no good.
   1697 	 */
   1698 	if (cmdsOK) {
   1699 	    if (aborting == 0) {
   1700 		if (job->tailCmds != NILLNODE) {
   1701 		    Lst_ForEachFrom(job->node->commands, job->tailCmds,
   1702 				    JobSaveCommand,
   1703 				   (ClientData)job->node);
   1704 		}
   1705 		job->node->made = MADE;
   1706 		Make_Update(job->node);
   1707 	    }
   1708 	    free(job);
   1709 	    return(JOB_FINISHED);
   1710 	} else {
   1711 	    free(job);
   1712 	    return(JOB_ERROR);
   1713 	}
   1714     } else {
   1715 	(void)fflush(job->cmdFILE);
   1716     }
   1717 
   1718     /*
   1719      * Set up the control arguments to the shell. This is based on the flags
   1720      * set earlier for this job.
   1721      */
   1722     JobMakeArgv(job, argv);
   1723 
   1724     /*
   1725      * If we're using pipes to catch output, create the pipe by which we'll
   1726      * get the shell's output. If we're using files, print out that we're
   1727      * starting a job and then set up its temporary-file name.
   1728      */
   1729     if (usePipes) {
   1730 	int fd[2];
   1731 	if (pipe(fd) == -1)
   1732 	    Punt("Cannot create pipe: %s", strerror(errno));
   1733 	job->inPipe = fd[0];
   1734 	job->outPipe = fd[1];
   1735 	(void)fcntl(job->inPipe, F_SETFD, 1);
   1736 	(void)fcntl(job->outPipe, F_SETFD, 1);
   1737     } else {
   1738 	(void)fprintf(stdout, "Remaking `%s'\n", gn->name);
   1739 	(void)fflush(stdout);
   1740 	(void)strcpy(job->outFile, TMPPAT);
   1741 	job->outFd = mkstemp(job->outFile);
   1742 	(void)fcntl(job->outFd, F_SETFD, 1);
   1743     }
   1744 
   1745     JobExec(job, argv);
   1746     return(JOB_RUNNING);
   1747 }
   1748 
   1749 static char *
   1750 JobOutput(Job *job, char *cp, char *endp, int msg)
   1751 {
   1752     char *ecp;
   1753 
   1754     if (commandShell->noPrint) {
   1755 	ecp = Str_FindSubstring(cp, commandShell->noPrint);
   1756 	while (ecp != NULL) {
   1757 	    if (cp != ecp) {
   1758 		*ecp = '\0';
   1759 		if (!beSilent && msg && job->node != lastNode) {
   1760 		    MESSAGE(stdout, job->node);
   1761 		    lastNode = job->node;
   1762 		}
   1763 		/*
   1764 		 * The only way there wouldn't be a newline after
   1765 		 * this line is if it were the last in the buffer.
   1766 		 * however, since the non-printable comes after it,
   1767 		 * there must be a newline, so we don't print one.
   1768 		 */
   1769 		(void)fprintf(stdout, "%s", cp);
   1770 		(void)fflush(stdout);
   1771 	    }
   1772 	    cp = ecp + commandShell->noPLen;
   1773 	    if (cp != endp) {
   1774 		/*
   1775 		 * Still more to print, look again after skipping
   1776 		 * the whitespace following the non-printable
   1777 		 * command....
   1778 		 */
   1779 		cp++;
   1780 		while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
   1781 		    cp++;
   1782 		}
   1783 		ecp = Str_FindSubstring(cp, commandShell->noPrint);
   1784 	    } else {
   1785 		return cp;
   1786 	    }
   1787 	}
   1788     }
   1789     return cp;
   1790 }
   1791 
   1792 /*-
   1793  *-----------------------------------------------------------------------
   1794  * JobDoOutput  --
   1795  *	This function is called at different times depending on
   1796  *	whether the user has specified that output is to be collected
   1797  *	via pipes or temporary files. In the former case, we are called
   1798  *	whenever there is something to read on the pipe. We collect more
   1799  *	output from the given job and store it in the job's outBuf. If
   1800  *	this makes up a line, we print it tagged by the job's identifier,
   1801  *	as necessary.
   1802  *	If output has been collected in a temporary file, we open the
   1803  *	file and read it line by line, transfering it to our own
   1804  *	output channel until the file is empty. At which point we
   1805  *	remove the temporary file.
   1806  *	In both cases, however, we keep our figurative eye out for the
   1807  *	'noPrint' line for the shell from which the output came. If
   1808  *	we recognize a line, we don't print it. If the command is not
   1809  *	alone on the line (the character after it is not \0 or \n), we
   1810  *	do print whatever follows it.
   1811  *
   1812  * Input:
   1813  *	job		the job whose output needs printing
   1814  *	finish		TRUE if this is the last time we'll be called
   1815  *			for this job
   1816  *
   1817  * Results:
   1818  *	None
   1819  *
   1820  * Side Effects:
   1821  *	curPos may be shifted as may the contents of outBuf.
   1822  *-----------------------------------------------------------------------
   1823  */
   1824 STATIC void
   1825 JobDoOutput(Job *job, Boolean finish)
   1826 {
   1827     Boolean       gotNL = FALSE;  /* true if got a newline */
   1828     Boolean       fbuf;  	  /* true if our buffer filled up */
   1829     int		  nr;	      	  /* number of bytes read */
   1830     int		  i;	      	  /* auxiliary index into outBuf */
   1831     int		  max;	      	  /* limit for i (end of current data) */
   1832     int		  nRead;      	  /* (Temporary) number of bytes read */
   1833 
   1834     FILE      	  *oFILE;	  /* Stream pointer to shell's output file */
   1835     char          inLine[132];
   1836 
   1837 
   1838     if (usePipes) {
   1839 	/*
   1840 	 * Read as many bytes as will fit in the buffer.
   1841 	 */
   1842 end_loop:
   1843 	gotNL = FALSE;
   1844 	fbuf = FALSE;
   1845 
   1846 	nRead = read(job->inPipe, &job->outBuf[job->curPos],
   1847 			 JOB_BUFSIZE - job->curPos);
   1848 	if (nRead < 0) {
   1849 	    if (DEBUG(JOB)) {
   1850 		perror("JobDoOutput(piperead)");
   1851 	    }
   1852 	    nr = 0;
   1853 	} else {
   1854 	    nr = nRead;
   1855 	}
   1856 
   1857 	/*
   1858 	 * If we hit the end-of-file (the job is dead), we must flush its
   1859 	 * remaining output, so pretend we read a newline if there's any
   1860 	 * output remaining in the buffer.
   1861 	 * Also clear the 'finish' flag so we stop looping.
   1862 	 */
   1863 	if ((nr == 0) && (job->curPos != 0)) {
   1864 	    job->outBuf[job->curPos] = '\n';
   1865 	    nr = 1;
   1866 	    finish = FALSE;
   1867 	} else if (nr == 0) {
   1868 	    finish = FALSE;
   1869 	}
   1870 
   1871 	/*
   1872 	 * Look for the last newline in the bytes we just got. If there is
   1873 	 * one, break out of the loop with 'i' as its index and gotNL set
   1874 	 * TRUE.
   1875 	 */
   1876 	max = job->curPos + nr;
   1877 	for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
   1878 	    if (job->outBuf[i] == '\n') {
   1879 		gotNL = TRUE;
   1880 		break;
   1881 	    } else if (job->outBuf[i] == '\0') {
   1882 		/*
   1883 		 * Why?
   1884 		 */
   1885 		job->outBuf[i] = ' ';
   1886 	    }
   1887 	}
   1888 
   1889 	if (!gotNL) {
   1890 	    job->curPos += nr;
   1891 	    if (job->curPos == JOB_BUFSIZE) {
   1892 		/*
   1893 		 * If we've run out of buffer space, we have no choice
   1894 		 * but to print the stuff. sigh.
   1895 		 */
   1896 		fbuf = TRUE;
   1897 		i = job->curPos;
   1898 	    }
   1899 	}
   1900 	if (gotNL || fbuf) {
   1901 	    /*
   1902 	     * Need to send the output to the screen. Null terminate it
   1903 	     * first, overwriting the newline character if there was one.
   1904 	     * So long as the line isn't one we should filter (according
   1905 	     * to the shell description), we print the line, preceded
   1906 	     * by a target banner if this target isn't the same as the
   1907 	     * one for which we last printed something.
   1908 	     * The rest of the data in the buffer are then shifted down
   1909 	     * to the start of the buffer and curPos is set accordingly.
   1910 	     */
   1911 	    job->outBuf[i] = '\0';
   1912 	    if (i >= job->curPos) {
   1913 		char *cp;
   1914 
   1915 		cp = JobOutput(job, job->outBuf, &job->outBuf[i], FALSE);
   1916 
   1917 		/*
   1918 		 * There's still more in that thar buffer. This time, though,
   1919 		 * we know there's no newline at the end, so we add one of
   1920 		 * our own free will.
   1921 		 */
   1922 		if (*cp != '\0') {
   1923 		    if (!beSilent && job->node != lastNode) {
   1924 			MESSAGE(stdout, job->node);
   1925 			lastNode = job->node;
   1926 		    }
   1927 		    (void)fprintf(stdout, "%s%s", cp, gotNL ? "\n" : "");
   1928 		    (void)fflush(stdout);
   1929 		}
   1930 	    }
   1931 	    if (i < max - 1) {
   1932 		/* shift the remaining characters down */
   1933 		(void)memcpy(job->outBuf, &job->outBuf[i + 1], max - (i + 1));
   1934 		job->curPos = max - (i + 1);
   1935 
   1936 	    } else {
   1937 		/*
   1938 		 * We have written everything out, so we just start over
   1939 		 * from the start of the buffer. No copying. No nothing.
   1940 		 */
   1941 		job->curPos = 0;
   1942 	    }
   1943 	}
   1944 	if (finish) {
   1945 	    /*
   1946 	     * If the finish flag is true, we must loop until we hit
   1947 	     * end-of-file on the pipe. This is guaranteed to happen
   1948 	     * eventually since the other end of the pipe is now closed
   1949 	     * (we closed it explicitly and the child has exited). When
   1950 	     * we do get an EOF, finish will be set FALSE and we'll fall
   1951 	     * through and out.
   1952 	     */
   1953 	    goto end_loop;
   1954 	}
   1955     } else {
   1956 	/*
   1957 	 * We've been called to retrieve the output of the job from the
   1958 	 * temporary file where it's been squirreled away. This consists of
   1959 	 * opening the file, reading the output line by line, being sure not
   1960 	 * to print the noPrint line for the shell we used, then close and
   1961 	 * remove the temporary file. Very simple.
   1962 	 *
   1963 	 * Change to read in blocks and do FindSubString type things as for
   1964 	 * pipes? That would allow for "@echo -n..."
   1965 	 */
   1966 	oFILE = fopen(job->outFile, "r");
   1967 	if (oFILE != NULL) {
   1968 	    (void)fprintf(stdout, "Results of making %s:\n", job->node->name);
   1969 	    (void)fflush(stdout);
   1970 	    while (fgets(inLine, sizeof(inLine), oFILE) != NULL) {
   1971 		char	*cp, *endp, *oendp;
   1972 
   1973 		cp = inLine;
   1974 		oendp = endp = inLine + strlen(inLine);
   1975 		if (endp[-1] == '\n') {
   1976 		    *--endp = '\0';
   1977 		}
   1978 		cp = JobOutput(job, inLine, endp, FALSE);
   1979 
   1980 		/*
   1981 		 * There's still more in that thar buffer. This time, though,
   1982 		 * we know there's no newline at the end, so we add one of
   1983 		 * our own free will.
   1984 		 */
   1985 		(void)fprintf(stdout, "%s", cp);
   1986 		(void)fflush(stdout);
   1987 		if (endp != oendp) {
   1988 		    (void)fprintf(stdout, "\n");
   1989 		    (void)fflush(stdout);
   1990 		}
   1991 	    }
   1992 	    (void)fclose(oFILE);
   1993 	    (void)eunlink(job->outFile);
   1994 	} else {
   1995 	    Punt("Cannot open `%s'", job->outFile);
   1996 	}
   1997     }
   1998 }
   1999 
   2000 static void
   2001 JobRun(GNode *targ)
   2002 {
   2003 #ifdef notyet
   2004     /*
   2005      * Unfortunately it is too complicated to run .BEGIN, .END,
   2006      * and .INTERRUPT job in the parallel job module. This has
   2007      * the nice side effect that it avoids a lot of other problems.
   2008      */
   2009     Lst lst = Lst_Init(FALSE);
   2010     Lst_AtEnd(lst, targ);
   2011     (void)Make_Run(lst);
   2012     Lst_Destroy(lst, NOFREE);
   2013     JobStart(targ, JOB_SPECIAL);
   2014     while (jobTokensRunning) {
   2015 	Job_CatchOutput();
   2016 	Job_CatchChildren(!usePipes);
   2017     }
   2018 #else
   2019     Compat_Make(targ, targ);
   2020     if (targ->made == ERROR) {
   2021 	PrintOnError("\n\nStop.");
   2022 	exit(1);
   2023     }
   2024 #endif
   2025 }
   2026 
   2027 /*-
   2028  *-----------------------------------------------------------------------
   2029  * Job_CatchChildren --
   2030  *	Handle the exit of a child. Called from Make_Make.
   2031  *
   2032  * Input:
   2033  *	block		TRUE if should block on the wait
   2034  *
   2035  * Results:
   2036  *	none.
   2037  *
   2038  * Side Effects:
   2039  *	The job descriptor is removed from the list of children.
   2040  *
   2041  * Notes:
   2042  *	We do waits, blocking or not, according to the wisdom of our
   2043  *	caller, until there are no more children to report. For each
   2044  *	job, call JobFinish to finish things off. This will take care of
   2045  *	putting jobs on the stoppedJobs queue.
   2046  *
   2047  *-----------------------------------------------------------------------
   2048  */
   2049 void
   2050 Job_CatchChildren(Boolean block)
   2051 {
   2052     int    	  pid;	    	/* pid of dead child */
   2053     Job		  *job;	    	/* job descriptor for dead child */
   2054     LstNode       jnode;    	/* list element for finding job */
   2055     int	  	  status;   	/* Exit/termination status */
   2056 
   2057     /*
   2058      * Don't even bother if we know there's no one around.
   2059      */
   2060     if (jobTokensRunning == 0) {
   2061 	return;
   2062     }
   2063 
   2064     while ((pid = waitpid((pid_t) -1, &status,
   2065 			  (block?0:WNOHANG)|WUNTRACED)) > 0)
   2066     {
   2067 	if (DEBUG(JOB)) {
   2068 	    (void)fprintf(stdout, "Process %d exited or stopped %x.\n", pid,
   2069 	      status);
   2070 	    (void)fflush(stdout);
   2071 	}
   2072 
   2073 	jnode = Lst_Find(jobs, (ClientData)&pid, JobCmpPid);
   2074 	if (jnode == NILLNODE) {
   2075 	    if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGCONT)) {
   2076 		jnode = Lst_Find(stoppedJobs, (ClientData) &pid, JobCmpPid);
   2077 		if (jnode == NILLNODE) {
   2078 		    Error("Resumed child (%d) not in table", pid);
   2079 		    continue;
   2080 		}
   2081 		job = (Job *)Lst_Datum(jnode);
   2082 		(void)Lst_Remove(stoppedJobs, jnode);
   2083 	    } else {
   2084 		Error("Child (%d) not in table?", pid);
   2085 		continue;
   2086 	    }
   2087 	} else {
   2088 	    job = (Job *)Lst_Datum(jnode);
   2089 	    (void)Lst_Remove(jobs, jnode);
   2090 	}
   2091 
   2092 	JobFinish(job, &status);
   2093     }
   2094 }
   2095 
   2096 /*-
   2097  *-----------------------------------------------------------------------
   2098  * Job_CatchOutput --
   2099  *	Catch the output from our children, if we're using
   2100  *	pipes do so. Otherwise just block time until we get a
   2101  *	signal(most likely a SIGCHLD) since there's no point in
   2102  *	just spinning when there's nothing to do and the reaping
   2103  *	of a child can wait for a while.
   2104  *
   2105  * Results:
   2106  *	None
   2107  *
   2108  * Side Effects:
   2109  *	Output is read from pipes if we're piping.
   2110  * -----------------------------------------------------------------------
   2111  */
   2112 void
   2113 Job_CatchOutput(void)
   2114 {
   2115     int           	  nready;
   2116     LstNode		  ln;
   2117     Job  	 	  *job;
   2118 
   2119     (void)fflush(stdout);
   2120     if (usePipes) {
   2121 	if ((nready = poll((wantToken ? fds : (fds + 1)),
   2122 	  		   (wantToken ? nfds : (nfds - 1)), POLL_MSEC)) <= 0) {
   2123 	    return;
   2124 	} else {
   2125 	    sigset_t	mask;
   2126 
   2127 	    if (readyfd(&childExitJob)) {
   2128 		char token;
   2129 		(void)read(childExitJob.inPipe, &token, 1);
   2130 		nready -= 1;
   2131 		if (token == DO_JOB_RESUME[0])
   2132 		    JobRestartJobs();
   2133 	    }
   2134 
   2135 	    JobSigLock(&mask);
   2136 	    if (Lst_Open(jobs) == FAILURE) {
   2137 		Punt("Cannot open job table");
   2138 	    }
   2139 
   2140 	    while (nready && (ln = Lst_Next(jobs)) != NILLNODE) {
   2141 		job = (Job *)Lst_Datum(ln);
   2142 		if (readyfd(job)) {
   2143 		    JobDoOutput(job, FALSE);
   2144 		    nready -= 1;
   2145 		}
   2146 	    }
   2147 	    Lst_Close(jobs);
   2148 	    JobSigUnlock(&mask);
   2149 	}
   2150     }
   2151 }
   2152 
   2153 /*-
   2154  *-----------------------------------------------------------------------
   2155  * Job_Make --
   2156  *	Start the creation of a target. Basically a front-end for
   2157  *	JobStart used by the Make module.
   2158  *
   2159  * Results:
   2160  *	None.
   2161  *
   2162  * Side Effects:
   2163  *	Another job is started.
   2164  *
   2165  *-----------------------------------------------------------------------
   2166  */
   2167 void
   2168 Job_Make(GNode *gn)
   2169 {
   2170     (void)JobStart(gn, 0);
   2171 }
   2172 
   2173 void
   2174 Shell_Init()
   2175 {
   2176     if (shellPath == NULL) {
   2177 	/*
   2178 	 * The user didn't specify a shell to use, so we are using the
   2179 	 * default one... Both the absolute path and the last component
   2180 	 * must be set. The last component is taken from the 'name' field
   2181 	 * of the default shell description pointed-to by commandShell.
   2182 	 * All default shells are located in _PATH_DEFSHELLDIR.
   2183 	 */
   2184 	shellName = commandShell->name;
   2185 	shellPath = str_concat(_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH);
   2186     }
   2187     if (commandShell->exit == NULL) {
   2188 	commandShell->exit = "";
   2189     }
   2190     if (commandShell->echo == NULL) {
   2191 	commandShell->echo = "";
   2192     }
   2193 }
   2194 
   2195 /*-
   2196  *-----------------------------------------------------------------------
   2197  * Job_Init --
   2198  *	Initialize the process module
   2199  *
   2200  * Input:
   2201  *	maxproc		the greatest number of jobs which may be running
   2202  *			at one time
   2203  *	maxlocal	the greatest number of jobs which may be running
   2204  *			at once
   2205  *
   2206  * Results:
   2207  *	none
   2208  *
   2209  * Side Effects:
   2210  *	lists and counters are initialized
   2211  *-----------------------------------------------------------------------
   2212  */
   2213 void
   2214 Job_Init(int maxproc)
   2215 {
   2216     GNode         *begin;     /* node for commands to do at the very start */
   2217 
   2218     jobs =  	  Lst_Init(FALSE);
   2219     stoppedJobs = Lst_Init(FALSE);
   2220     maxJobs = 	  maxproc;
   2221     wantToken =	  FALSE;
   2222 
   2223     aborting = 	  0;
   2224     errors = 	  0;
   2225 
   2226     lastNode =	  NILGNODE;
   2227 
   2228     if (maxJobs == 1) {
   2229 	/*
   2230 	 * If only one job can run at a time, there's no need for a banner,
   2231 	 * is there?
   2232 	 */
   2233 	targFmt = "";
   2234     } else {
   2235 	targFmt = TARG_FMT;
   2236     }
   2237 
   2238     Shell_Init();
   2239 
   2240     if (pipe(exit_pipe) < 0)
   2241 	Fatal("error in pipe: %s", strerror(errno));
   2242     fcntl(exit_pipe[0], F_SETFD, 1);
   2243     fcntl(exit_pipe[1], F_SETFD, 1);
   2244 
   2245     childExitJob.inPipe = exit_pipe[0];
   2246 
   2247     sigemptyset(&caught_signals);
   2248     /*
   2249      * Install a SIGCHLD handler.
   2250      */
   2251     (void)signal(SIGCHLD, JobChildSig);
   2252     sigaddset(&caught_signals, SIGCHLD);
   2253 
   2254 #define ADDSIG(s,h)				\
   2255     if (signal(s, SIG_IGN) != SIG_IGN) {	\
   2256 	sigaddset(&caught_signals, s);		\
   2257 	(void)signal(s, h);			\
   2258     }
   2259 
   2260     /*
   2261      * Catch the four signals that POSIX specifies if they aren't ignored.
   2262      * JobPassSig will take care of calling JobInterrupt if appropriate.
   2263      */
   2264     ADDSIG(SIGINT, JobPassSig)
   2265     ADDSIG(SIGHUP, JobPassSig)
   2266     ADDSIG(SIGTERM, JobPassSig)
   2267     ADDSIG(SIGQUIT, JobPassSig)
   2268 
   2269     /*
   2270      * There are additional signals that need to be caught and passed if
   2271      * either the export system wants to be told directly of signals or if
   2272      * we're giving each job its own process group (since then it won't get
   2273      * signals from the terminal driver as we own the terminal)
   2274      */
   2275 #if defined(USE_PGRP)
   2276     ADDSIG(SIGTSTP, JobPassSig)
   2277     ADDSIG(SIGTTOU, JobPassSig)
   2278     ADDSIG(SIGTTIN, JobPassSig)
   2279     ADDSIG(SIGWINCH, JobPassSig)
   2280     ADDSIG(SIGCONT, JobContinueSig)
   2281 #endif
   2282 #undef ADDSIG
   2283 
   2284     begin = Targ_FindNode(".BEGIN", TARG_NOCREATE);
   2285 
   2286     if (begin != NILGNODE) {
   2287 	JobRun(begin);
   2288 	if (begin->made == ERROR) {
   2289 	    PrintOnError("\n\nStop.");
   2290 	    exit(1);
   2291 	}
   2292     }
   2293     postCommands = Targ_FindNode(".END", TARG_CREATE);
   2294 }
   2295 
   2296 static void JobSigReset(void)
   2297 {
   2298 #define DELSIG(s)					\
   2299     if (sigismember(&caught_signals, s)) {		\
   2300 	(void)signal(s, SIG_DFL);			\
   2301     }
   2302 
   2303     DELSIG(SIGINT)
   2304     DELSIG(SIGHUP)
   2305     DELSIG(SIGQUIT)
   2306     DELSIG(SIGTERM)
   2307 #if defined(USE_PGRP)
   2308     DELSIG(SIGTSTP)
   2309     DELSIG(SIGTTOU)
   2310     DELSIG(SIGTTIN)
   2311     DELSIG(SIGWINCH)
   2312     DELSIG(SIGCONT)
   2313 #endif
   2314 #undef DELSIG
   2315     (void)signal(SIGCHLD, SIG_DFL);
   2316 }
   2317 
   2318 /*-
   2319  *-----------------------------------------------------------------------
   2320  * Job_Empty --
   2321  *	See if the job table is empty.  Because the local concurrency may
   2322  *	be set to 0, it is possible for the job table to become empty,
   2323  *	while the list of stoppedJobs remains non-empty. In such a case,
   2324  *	we want to restart as many jobs as we can.
   2325  *
   2326  * Results:
   2327  *	TRUE if it is. FALSE if it ain't.
   2328  *
   2329  * Side Effects:
   2330  *	None.
   2331  *
   2332  * -----------------------------------------------------------------------
   2333  */
   2334 Boolean
   2335 Job_Empty(void)
   2336 {
   2337     if (jobTokensRunning != 0)
   2338 	return FALSE;
   2339 
   2340     if (Lst_IsEmpty(stoppedJobs) || aborting)
   2341 	return TRUE;
   2342 
   2343     /*
   2344      * The job table is obviously not full if it has no jobs in
   2345      * it...Try and restart the stopped jobs.
   2346      */
   2347     JobRestartJobs();
   2348     return FALSE;
   2349 }
   2350 
   2351 /*-
   2352  *-----------------------------------------------------------------------
   2353  * JobMatchShell --
   2354  *	Find a shell in 'shells' given its name.
   2355  *
   2356  * Results:
   2357  *	A pointer to the Shell structure.
   2358  *
   2359  * Side Effects:
   2360  *	None.
   2361  *
   2362  *-----------------------------------------------------------------------
   2363  */
   2364 static Shell *
   2365 JobMatchShell(const char *name)
   2366 {
   2367     Shell	*sh;
   2368 
   2369     for (sh = shells; sh->name != NULL; sh++) {
   2370 	if (strcmp(name, sh->name) == 0)
   2371 		return (sh);
   2372     }
   2373     return (NULL);
   2374 }
   2375 
   2376 /*-
   2377  *-----------------------------------------------------------------------
   2378  * Job_ParseShell --
   2379  *	Parse a shell specification and set up commandShell, shellPath
   2380  *	and shellName appropriately.
   2381  *
   2382  * Input:
   2383  *	line		The shell spec
   2384  *
   2385  * Results:
   2386  *	FAILURE if the specification was incorrect.
   2387  *
   2388  * Side Effects:
   2389  *	commandShell points to a Shell structure (either predefined or
   2390  *	created from the shell spec), shellPath is the full path of the
   2391  *	shell described by commandShell, while shellName is just the
   2392  *	final component of shellPath.
   2393  *
   2394  * Notes:
   2395  *	A shell specification consists of a .SHELL target, with dependency
   2396  *	operator, followed by a series of blank-separated words. Double
   2397  *	quotes can be used to use blanks in words. A backslash escapes
   2398  *	anything (most notably a double-quote and a space) and
   2399  *	provides the functionality it does in C. Each word consists of
   2400  *	keyword and value separated by an equal sign. There should be no
   2401  *	unnecessary spaces in the word. The keywords are as follows:
   2402  *	    name  	    Name of shell.
   2403  *	    path  	    Location of shell.
   2404  *	    quiet 	    Command to turn off echoing.
   2405  *	    echo  	    Command to turn echoing on
   2406  *	    filter	    Result of turning off echoing that shouldn't be
   2407  *	    	  	    printed.
   2408  *	    echoFlag	    Flag to turn echoing on at the start
   2409  *	    errFlag	    Flag to turn error checking on at the start
   2410  *	    hasErrCtl	    True if shell has error checking control
   2411  *	    check 	    Command to turn on error checking if hasErrCtl
   2412  *	    	  	    is TRUE or template of command to echo a command
   2413  *	    	  	    for which error checking is off if hasErrCtl is
   2414  *	    	  	    FALSE.
   2415  *	    ignore	    Command to turn off error checking if hasErrCtl
   2416  *	    	  	    is TRUE or template of command to execute a
   2417  *	    	  	    command so as to ignore any errors it returns if
   2418  *	    	  	    hasErrCtl is FALSE.
   2419  *
   2420  *-----------------------------------------------------------------------
   2421  */
   2422 ReturnStatus
   2423 Job_ParseShell(char *line)
   2424 {
   2425     char	**words;
   2426     char	**argv;
   2427     int		argc;
   2428     char	*path;
   2429     Shell	newShell;
   2430     Boolean	fullSpec = FALSE;
   2431     Shell	*sh;
   2432 
   2433     while (isspace((unsigned char)*line)) {
   2434 	line++;
   2435     }
   2436 
   2437     if (shellArgv)
   2438 	free(UNCONST(shellArgv));
   2439 
   2440     memset(&newShell, 0, sizeof(newShell));
   2441 
   2442     /*
   2443      * Parse the specification by keyword
   2444      */
   2445     words = brk_string(line, &argc, TRUE, &path);
   2446     shellArgv = path;
   2447 
   2448     for (path = NULL, argv = words; argc != 0; argc--, argv++) {
   2449 	    if (strncmp(*argv, "path=", 5) == 0) {
   2450 		path = &argv[0][5];
   2451 	    } else if (strncmp(*argv, "name=", 5) == 0) {
   2452 		newShell.name = &argv[0][5];
   2453 	    } else {
   2454 		if (strncmp(*argv, "quiet=", 6) == 0) {
   2455 		    newShell.echoOff = &argv[0][6];
   2456 		} else if (strncmp(*argv, "echo=", 5) == 0) {
   2457 		    newShell.echoOn = &argv[0][5];
   2458 		} else if (strncmp(*argv, "filter=", 7) == 0) {
   2459 		    newShell.noPrint = &argv[0][7];
   2460 		    newShell.noPLen = strlen(newShell.noPrint);
   2461 		} else if (strncmp(*argv, "echoFlag=", 9) == 0) {
   2462 		    newShell.echo = &argv[0][9];
   2463 		} else if (strncmp(*argv, "errFlag=", 8) == 0) {
   2464 		    newShell.exit = &argv[0][8];
   2465 		} else if (strncmp(*argv, "hasErrCtl=", 10) == 0) {
   2466 		    char c = argv[0][10];
   2467 		    newShell.hasErrCtl = !((c != 'Y') && (c != 'y') &&
   2468 					   (c != 'T') && (c != 't'));
   2469 		} else if (strncmp(*argv, "check=", 6) == 0) {
   2470 		    newShell.errCheck = &argv[0][6];
   2471 		} else if (strncmp(*argv, "ignore=", 7) == 0) {
   2472 		    newShell.ignErr = &argv[0][7];
   2473 		} else if (strncmp(*argv, "errout=", 7) == 0) {
   2474 		    newShell.errOut = &argv[0][7];
   2475 		} else if (strncmp(*argv, "comment=", 8) == 0) {
   2476 		    newShell.commentChar = argv[0][8];
   2477 		} else {
   2478 		    Parse_Error(PARSE_FATAL, "Unknown keyword \"%s\"",
   2479 				*argv);
   2480 		    free(words);
   2481 		    return(FAILURE);
   2482 		}
   2483 		fullSpec = TRUE;
   2484 	    }
   2485     }
   2486 
   2487     if (path == NULL) {
   2488 	/*
   2489 	 * If no path was given, the user wants one of the pre-defined shells,
   2490 	 * yes? So we find the one s/he wants with the help of JobMatchShell
   2491 	 * and set things up the right way. shellPath will be set up by
   2492 	 * Job_Init.
   2493 	 */
   2494 	if (newShell.name == NULL) {
   2495 	    Parse_Error(PARSE_FATAL, "Neither path nor name specified");
   2496 	    free(words);
   2497 	    return(FAILURE);
   2498 	} else {
   2499 	    if ((sh = JobMatchShell(newShell.name)) == NULL) {
   2500 		    Parse_Error(PARSE_WARNING, "%s: No matching shell",
   2501 				newShell.name);
   2502 		    free(words);
   2503 		    return(FAILURE);
   2504 	    }
   2505 	    commandShell = sh;
   2506 	    shellName = newShell.name;
   2507 	}
   2508     } else {
   2509 	/*
   2510 	 * The user provided a path. If s/he gave nothing else (fullSpec is
   2511 	 * FALSE), try and find a matching shell in the ones we know of.
   2512 	 * Else we just take the specification at its word and copy it
   2513 	 * to a new location. In either case, we need to record the
   2514 	 * path the user gave for the shell.
   2515 	 */
   2516 	shellPath = path;
   2517 	path = strrchr(path, '/');
   2518 	if (path == NULL) {
   2519 	    path = UNCONST(shellPath);
   2520 	} else {
   2521 	    path += 1;
   2522 	}
   2523 	if (newShell.name != NULL) {
   2524 	    shellName = newShell.name;
   2525 	} else {
   2526 	    shellName = path;
   2527 	}
   2528 	if (!fullSpec) {
   2529 	    if ((sh = JobMatchShell(shellName)) == NULL) {
   2530 		    Parse_Error(PARSE_WARNING, "%s: No matching shell",
   2531 				shellName);
   2532 		    free(words);
   2533 		    return(FAILURE);
   2534 	    }
   2535 	    commandShell = sh;
   2536 	} else {
   2537 	    commandShell = emalloc(sizeof(Shell));
   2538 	    *commandShell = newShell;
   2539 	}
   2540     }
   2541 
   2542     if (commandShell->echoOn && commandShell->echoOff) {
   2543 	commandShell->hasEchoCtl = TRUE;
   2544     }
   2545 
   2546     if (!commandShell->hasErrCtl) {
   2547 	if (commandShell->errCheck == NULL) {
   2548 	    commandShell->errCheck = "";
   2549 	}
   2550 	if (commandShell->ignErr == NULL) {
   2551 	    commandShell->ignErr = "%s\n";
   2552 	}
   2553     }
   2554 
   2555     /*
   2556      * Do not free up the words themselves, since they might be in use by the
   2557      * shell specification.
   2558      */
   2559     free(words);
   2560     return SUCCESS;
   2561 }
   2562 
   2563 /*-
   2564  *-----------------------------------------------------------------------
   2565  * JobInterrupt --
   2566  *	Handle the receipt of an interrupt.
   2567  *
   2568  * Input:
   2569  *	runINTERRUPT	Non-zero if commands for the .INTERRUPT target
   2570  *			should be executed
   2571  *	signo		signal received
   2572  *
   2573  * Results:
   2574  *	None
   2575  *
   2576  * Side Effects:
   2577  *	All children are killed. Another job will be started if the
   2578  *	.INTERRUPT target was given.
   2579  *-----------------------------------------------------------------------
   2580  */
   2581 static void
   2582 JobInterrupt(int runINTERRUPT, int signo)
   2583 {
   2584     LstNode	ln;		/* element in job table */
   2585     Job		*job;		/* job descriptor in that element */
   2586     GNode	*interrupt;	/* the node describing the .INTERRUPT target */
   2587     sigset_t	mask;
   2588 
   2589     aborting = ABORT_INTERRUPT;
   2590 
   2591     JobSigLock(&mask);
   2592 
   2593     (void)Lst_Open(jobs);
   2594     while ((ln = Lst_Next(jobs)) != NILLNODE) {
   2595 	GNode *gn;
   2596 
   2597 	job = (Job *)Lst_Datum(ln);
   2598 	gn = job->node;
   2599 
   2600 	if ((gn->type & (OP_JOIN|OP_PHONY)) == 0 && !Targ_Precious(gn)) {
   2601 	    char *file = (gn->path == NULL ? gn->name : gn->path);
   2602 	    if (!noExecute && eunlink(file) != -1) {
   2603 		Error("*** %s removed", file);
   2604 	    }
   2605 	}
   2606 	if (job->pid) {
   2607 	    if (DEBUG(JOB)) {
   2608 		(void)fprintf(stdout,
   2609 			   "JobInterrupt passing signal %d to child %d.\n",
   2610 			   signo, job->pid);
   2611 		(void)fflush(stdout);
   2612 	    }
   2613 	    KILL(job->pid, signo);
   2614 	}
   2615     }
   2616     Lst_Close(jobs);
   2617 
   2618     JobSigUnlock(&mask);
   2619 
   2620     if (runINTERRUPT && !touchFlag) {
   2621 	interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
   2622 	if (interrupt != NILGNODE) {
   2623 	    ignoreErrors = FALSE;
   2624 	    JobRun(interrupt);
   2625 	}
   2626     }
   2627     Trace_Log(MAKEINTR, 0);
   2628     exit(signo);
   2629 }
   2630 
   2631 /*
   2632  *-----------------------------------------------------------------------
   2633  * Job_Finish --
   2634  *	Do final processing such as the running of the commands
   2635  *	attached to the .END target.
   2636  *
   2637  * Results:
   2638  *	Number of errors reported.
   2639  *
   2640  * Side Effects:
   2641  *	None.
   2642  *-----------------------------------------------------------------------
   2643  */
   2644 int
   2645 Job_Finish(void)
   2646 {
   2647     if (postCommands != NILGNODE && !Lst_IsEmpty(postCommands->commands)) {
   2648 	if (errors) {
   2649 	    Error("Errors reported so .END ignored");
   2650 	} else {
   2651 	    JobRun(postCommands);
   2652 	}
   2653     }
   2654     return(errors);
   2655 }
   2656 
   2657 /*-
   2658  *-----------------------------------------------------------------------
   2659  * Job_End --
   2660  *	Cleanup any memory used by the jobs module
   2661  *
   2662  * Results:
   2663  *	None.
   2664  *
   2665  * Side Effects:
   2666  *	Memory is freed
   2667  *-----------------------------------------------------------------------
   2668  */
   2669 void
   2670 Job_End(void)
   2671 {
   2672 #ifdef CLEANUP
   2673     if (shellArgv)
   2674 	free(shellArgv);
   2675 #endif
   2676 }
   2677 
   2678 /*-
   2679  *-----------------------------------------------------------------------
   2680  * Job_Wait --
   2681  *	Waits for all running jobs to finish and returns. Sets 'aborting'
   2682  *	to ABORT_WAIT to prevent other jobs from starting.
   2683  *
   2684  * Results:
   2685  *	None.
   2686  *
   2687  * Side Effects:
   2688  *	Currently running jobs finish.
   2689  *
   2690  *-----------------------------------------------------------------------
   2691  */
   2692 void
   2693 Job_Wait(void)
   2694 {
   2695     aborting = ABORT_WAIT;
   2696     while (jobTokensRunning != 0) {
   2697 	Job_CatchOutput();
   2698 	Job_CatchChildren(!usePipes);
   2699     }
   2700     aborting = 0;
   2701 }
   2702 
   2703 /*-
   2704  *-----------------------------------------------------------------------
   2705  * Job_AbortAll --
   2706  *	Abort all currently running jobs without handling output or anything.
   2707  *	This function is to be called only in the event of a major
   2708  *	error. Most definitely NOT to be called from JobInterrupt.
   2709  *
   2710  * Results:
   2711  *	None
   2712  *
   2713  * Side Effects:
   2714  *	All children are killed, not just the firstborn
   2715  *-----------------------------------------------------------------------
   2716  */
   2717 void
   2718 Job_AbortAll(void)
   2719 {
   2720     LstNode	ln;	/* element in job table */
   2721     Job		*job;	/* the job descriptor in that element */
   2722     int		foo;
   2723     sigset_t	mask;
   2724 
   2725     aborting = ABORT_ERROR;
   2726 
   2727     if (jobTokensRunning) {
   2728 
   2729 	JobSigLock(&mask);
   2730 	(void)Lst_Open(jobs);
   2731 	while ((ln = Lst_Next(jobs)) != NILLNODE) {
   2732 	    job = (Job *)Lst_Datum(ln);
   2733 
   2734 	    /*
   2735 	     * kill the child process with increasingly drastic signals to make
   2736 	     * darn sure it's dead.
   2737 	     */
   2738 	    KILL(job->pid, SIGINT);
   2739 	    KILL(job->pid, SIGKILL);
   2740 	}
   2741 	Lst_Close(jobs);
   2742 	JobSigUnlock(&mask);
   2743     }
   2744 
   2745     /*
   2746      * Catch as many children as want to report in at first, then give up
   2747      */
   2748     while (waitpid((pid_t) -1, &foo, WNOHANG) > 0)
   2749 	continue;
   2750 }
   2751 
   2752 
   2753 /*-
   2755  *-----------------------------------------------------------------------
   2756  * JobRestartJobs --
   2757  *	Tries to restart stopped jobs if there are slots available.
   2758  *	Note that this tries to restart them regardless of pending errors.
   2759  *	It's not good to leave stopped jobs lying around!
   2760  *
   2761  * Results:
   2762  *	None.
   2763  *
   2764  * Side Effects:
   2765  *	Resumes(and possibly migrates) jobs.
   2766  *
   2767  *-----------------------------------------------------------------------
   2768  */
   2769 static void
   2770 JobRestartJobs(void)
   2771 {
   2772     sigset_t	mask;
   2773 
   2774     JobSigLock(&mask);
   2775     while (!Lst_IsEmpty(stoppedJobs)) {
   2776 	if (DEBUG(JOB)) {
   2777 	    (void)fprintf(stdout, "Restarting a stopped job.\n");
   2778 	    (void)fflush(stdout);
   2779 	}
   2780 	if (JobRestart((Job *)Lst_DeQueue(stoppedJobs)) != 0)
   2781 		break;
   2782     }
   2783     JobSigUnlock(&mask);
   2784 }
   2785 
   2786 static void
   2787 watchfd(Job *job)
   2788 {
   2789     int i;
   2790     if (job->inPollfd != NULL)
   2791 	Punt("Watching watched job");
   2792     if (fds == NULL) {
   2793 	maxfds = JBSTART;
   2794 	fds = emalloc(sizeof(struct pollfd) * maxfds);
   2795 	jobfds = emalloc(sizeof(Job **) * maxfds);
   2796 
   2797 	fds[0].fd = job_pipe[0];
   2798 	fds[0].events = POLLIN;
   2799 	jobfds[0] = &tokenWaitJob;
   2800 	tokenWaitJob.inPollfd = &fds[0];
   2801 	nfds++;
   2802 
   2803 	fds[1].fd = exit_pipe[0];
   2804 	fds[1].events = POLLIN;
   2805 	jobfds[1] = &childExitJob;
   2806 	childExitJob.inPollfd = &fds[1];
   2807 	nfds++;
   2808     } else if (nfds == maxfds) {
   2809 	maxfds *= JBFACTOR;
   2810 	fds = erealloc(fds, sizeof(struct pollfd) * maxfds);
   2811 	jobfds = erealloc(jobfds, sizeof(Job **) * maxfds);
   2812 	for (i = 0; i < nfds; i++)
   2813 	    jobfds[i]->inPollfd = &fds[i];
   2814     }
   2815 
   2816     fds[nfds].fd = job->inPipe;
   2817     fds[nfds].events = POLLIN;
   2818     jobfds[nfds] = job;
   2819     job->inPollfd = &fds[nfds];
   2820     nfds++;
   2821 }
   2822 
   2823 static void
   2824 clearfd(Job *job)
   2825 {
   2826     int i;
   2827     if (job->inPollfd == NULL)
   2828 	Punt("Unwatching unwatched job");
   2829     i = job->inPollfd - fds;
   2830     nfds--;
   2831     /*
   2832      * Move last job in table into hole made by dead job.
   2833      */
   2834     if (nfds != i) {
   2835 	fds[i] = fds[nfds];
   2836 	jobfds[i] = jobfds[nfds];
   2837 	jobfds[i]->inPollfd = &fds[i];
   2838     }
   2839     job->inPollfd = NULL;
   2840 }
   2841 
   2842 static int
   2843 readyfd(Job *job)
   2844 {
   2845     if (job->inPollfd == NULL)
   2846 	Punt("Polling unwatched job");
   2847     return (job->inPollfd->revents & POLLIN) != 0;
   2848 }
   2849 
   2850 /*-
   2851  *-----------------------------------------------------------------------
   2852  * JobTokenAdd --
   2853  *	Put a token into the job pipe so that some make process can start
   2854  *	another job.
   2855  *
   2856  * Side Effects:
   2857  *	Allows more build jobs to be spawned somewhere.
   2858  *
   2859  *-----------------------------------------------------------------------
   2860  */
   2861 
   2862 static void
   2863 JobTokenAdd(void)
   2864 {
   2865     char tok = JOB_TOKENS[aborting], tok1;
   2866 
   2867     /* If we are depositing an error token flush everything else */
   2868     while (tok != '+' && read(job_pipe[0], &tok1, 1) == 1)
   2869 	continue;
   2870 
   2871     if (DEBUG(JOB))
   2872 	printf("(%d) aborting %d, deposit token %c\n",
   2873 	    getpid(), aborting, JOB_TOKENS[aborting]);
   2874     write(job_pipe[1], &tok, 1);
   2875 }
   2876 
   2877 /*-
   2878  *-----------------------------------------------------------------------
   2879  * Job_ServerStartTokenAdd --
   2880  *	Prep the job token pipe in the root make process.
   2881  *
   2882  *-----------------------------------------------------------------------
   2883  */
   2884 
   2885 void
   2886 Job_ServerStart(int maxproc)
   2887 {
   2888     int i, fd, flags;
   2889     char jobarg[64];
   2890 
   2891     if (pipe(job_pipe) < 0)
   2892 	Fatal("error in pipe: %s", strerror(errno));
   2893 
   2894     for (i = 0; i < 2; i++) {
   2895        /* Avoid using low numbered fds */
   2896        fd = fcntl(job_pipe[i], F_DUPFD, 15);
   2897        if (fd != -1) {
   2898 	   close(job_pipe[i]);
   2899 	   job_pipe[i] = fd;
   2900        }
   2901     }
   2902 
   2903     /*
   2904      * We mark the input side of the pipe non-blocking; we poll(2) the
   2905      * pipe when we're waiting for a job token, but we might lose the
   2906      * race for the token when a new one becomes available, so the read
   2907      * from the pipe should not block.
   2908      */
   2909     flags = fcntl(job_pipe[0], F_GETFL, 0);
   2910     flags |= O_NONBLOCK;
   2911     fcntl(job_pipe[0], F_SETFL, flags);
   2912 
   2913     /*
   2914      * Mark job pipes as close-on-exec.
   2915      * Note that we will clear this when executing submakes.
   2916      */
   2917     fcntl(job_pipe[0], F_SETFD, 1);
   2918     fcntl(job_pipe[1], F_SETFD, 1);
   2919 
   2920     snprintf(jobarg, sizeof(jobarg), "%d,%d", job_pipe[0], job_pipe[1]);
   2921 
   2922     Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL);
   2923     Var_Append(MAKEFLAGS, jobarg, VAR_GLOBAL);
   2924 
   2925     /*
   2926      * Preload job_pipe with one token per job, save the one
   2927      * "extra" token for the primary job.
   2928      *
   2929      * XXX should clip maxJobs against PIPE_BUF -- if maxJobs is
   2930      * larger than the write buffer size of the pipe, we will
   2931      * deadlock here.
   2932      */
   2933     for (i=1; i < maxproc; i++)
   2934 	JobTokenAdd();
   2935 }
   2936 
   2937 /*-
   2938  *-----------------------------------------------------------------------
   2939  * Job_TokenReturn --
   2940  *	Return a withdrawn token to the pool.
   2941  *
   2942  *-----------------------------------------------------------------------
   2943  */
   2944 
   2945 void
   2946 Job_TokenReturn(void)
   2947 {
   2948     jobTokensRunning--;
   2949     if (jobTokensRunning < 0)
   2950 	Punt("token botch");
   2951     if (jobTokensRunning || JOB_TOKENS[aborting] != '+')
   2952 	JobTokenAdd();
   2953 }
   2954 
   2955 /*-
   2956  *-----------------------------------------------------------------------
   2957  * Job_TokenWithdraw --
   2958  *	Attempt to withdraw a token from the pool.
   2959  *
   2960  * Results:
   2961  *	Returns TRUE if a token was withdrawn, and FALSE if the pool
   2962  *	is currently empty.
   2963  *
   2964  * Side Effects:
   2965  * 	If pool is empty, set wantToken so that we wake up
   2966  *	when a token is released.
   2967  *
   2968  *-----------------------------------------------------------------------
   2969  */
   2970 
   2971 
   2972 Boolean
   2973 Job_TokenWithdraw(void)
   2974 {
   2975     char tok, tok1;
   2976     int count;
   2977 
   2978     wantToken = FALSE;
   2979     if (DEBUG(JOB))
   2980 	printf("Job_TokenWithdraw(%d): aborting %d, running %d\n",
   2981 		getpid(), aborting, jobTokensRunning);
   2982 
   2983     if (aborting || (jobTokensRunning && not_parallel))
   2984 	return FALSE;
   2985 
   2986     count = read(job_pipe[0], &tok, 1);
   2987     if (count == 0)
   2988 	Fatal("eof on job pipe!");
   2989     if (count < 0 && jobTokensRunning != 0) {
   2990 	if (errno != EAGAIN) {
   2991 	    Fatal("job pipe read: %s", strerror(errno));
   2992 	}
   2993 	if (DEBUG(JOB))
   2994 	    printf("(%d) blocked for token\n", getpid());
   2995 	wantToken = TRUE;
   2996 	return FALSE;
   2997     }
   2998 
   2999     if (count == 1 && tok != '+') {
   3000 	/* Remove any other job tokens */
   3001 	if (DEBUG(JOB))
   3002 	    printf("(%d) aborted by token %c\n", getpid(), tok);
   3003 	while (read(job_pipe[0], &tok1, 1) == 1)
   3004 	    continue;
   3005 	/* And put the stopper back */
   3006 	write(job_pipe[1], &tok, 1);
   3007 	Fatal("A failure has been detected in another branch of the parallel make");
   3008     }
   3009 
   3010     if (count == 1 && jobTokensRunning == 0)
   3011 	/* We didn't want the token really */
   3012 	write(job_pipe[1], &tok, 1);
   3013 
   3014     jobTokensRunning++;
   3015     if (DEBUG(JOB))
   3016 	printf("(%d) withdrew token\n", getpid());
   3017     return TRUE;
   3018 }
   3019 
   3020 #ifdef USE_SELECT
   3021 int
   3022 emul_poll(struct pollfd *fd, int nfd, int timeout)
   3023 {
   3024     fd_set rfds, wfds;
   3025     int i, maxfd, nselect, npoll;
   3026     struct timeval tv, *tvp;
   3027     long usecs;
   3028 
   3029     FD_ZERO(&rfds);
   3030     FD_ZERO(&wfds);
   3031 
   3032     maxfd = -1;
   3033     for (i = 0; i < nfd; i++) {
   3034 	fd[i].revents = 0;
   3035 
   3036 	if (fd[i].events & POLLIN)
   3037 	    FD_SET(fd[i].fd, &rfds);
   3038 
   3039 	if (fd[i].events & POLLOUT)
   3040 	    FD_SET(fd[i].fd, &wfds);
   3041 
   3042 	if (fd[i].fd > maxfd)
   3043 	    maxfd = fd[i].fd;
   3044     }
   3045 
   3046     if (maxfd >= FD_SETSIZE) {
   3047 	Punt("Ran out of fd_set slots; "
   3048 	     "recompile with a larger FD_SETSIZE.");
   3049     }
   3050 
   3051     if (timeout < 0) {
   3052 	tvp = NULL;
   3053     } else {
   3054 	usecs = timeout * 1000;
   3055 	tv.tv_sec = usecs / 1000000;
   3056 	tv.tv_usec = usecs % 1000000;
   3057         tvp = &tv;
   3058     }
   3059 
   3060     nselect = select(maxfd + 1, &rfds, &wfds, 0, tvp);
   3061 
   3062     if (nselect <= 0)
   3063 	return nselect;
   3064 
   3065     npoll = 0;
   3066     for (i = 0; i < nfd; i++) {
   3067 	if (FD_ISSET(fd[i].fd, &rfds))
   3068 	    fd[i].revents |= POLLIN;
   3069 
   3070 	if (FD_ISSET(fd[i].fd, &wfds))
   3071 	    fd[i].revents |= POLLOUT;
   3072 
   3073 	if (fd[i].revents)
   3074 	    npoll++;
   3075     }
   3076 
   3077     return npoll;
   3078 }
   3079 #endif /* USE_SELECT */
   3080