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