Home | History | Annotate | Line # | Download | only in make
parse.c revision 1.122
      1 /*	$NetBSD: parse.c,v 1.122 2006/12/04 21:34:47 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990, 1993
      5  *	The Regents of the University of California.  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) 1989 by Berkeley Softworks
     37  * All rights reserved.
     38  *
     39  * This code is derived from software contributed to Berkeley by
     40  * Adam de Boor.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  */
     70 
     71 #ifndef MAKE_NATIVE
     72 static char rcsid[] = "$NetBSD: parse.c,v 1.122 2006/12/04 21:34:47 dsl Exp $";
     73 #else
     74 #include <sys/cdefs.h>
     75 #ifndef lint
     76 #if 0
     77 static char sccsid[] = "@(#)parse.c	8.3 (Berkeley) 3/19/94";
     78 #else
     79 __RCSID("$NetBSD: parse.c,v 1.122 2006/12/04 21:34:47 dsl Exp $");
     80 #endif
     81 #endif /* not lint */
     82 #endif
     83 
     84 /*-
     85  * parse.c --
     86  *	Functions to parse a makefile.
     87  *
     88  *	One function, Parse_Init, must be called before any functions
     89  *	in this module are used. After that, the function Parse_File is the
     90  *	main entry point and controls most of the other functions in this
     91  *	module.
     92  *
     93  *	Most important structures are kept in Lsts. Directories for
     94  *	the #include "..." function are kept in the 'parseIncPath' Lst, while
     95  *	those for the #include <...> are kept in the 'sysIncPath' Lst. The
     96  *	targets currently being defined are kept in the 'targets' Lst.
     97  *
     98  *	The variables 'fname' and 'lineno' are used to track the name
     99  *	of the current file and the line number in that file so that error
    100  *	messages can be more meaningful.
    101  *
    102  * Interface:
    103  *	Parse_Init	    	    Initialization function which must be
    104  *	    	  	    	    called before anything else in this module
    105  *	    	  	    	    is used.
    106  *
    107  *	Parse_End		    Cleanup the module
    108  *
    109  *	Parse_File	    	    Function used to parse a makefile. It must
    110  *	    	  	    	    be given the name of the file, which should
    111  *	    	  	    	    already have been opened, and a function
    112  *	    	  	    	    to call to read a character from the file.
    113  *
    114  *	Parse_IsVar	    	    Returns TRUE if the given line is a
    115  *	    	  	    	    variable assignment. Used by MainParseArgs
    116  *	    	  	    	    to determine if an argument is a target
    117  *	    	  	    	    or a variable assignment. Used internally
    118  *	    	  	    	    for pretty much the same thing...
    119  *
    120  *	Parse_Error	    	    Function called when an error occurs in
    121  *	    	  	    	    parsing. Used by the variable and
    122  *	    	  	    	    conditional modules.
    123  *	Parse_MainName	    	    Returns a Lst of the main target to create.
    124  */
    125 
    126 #include <ctype.h>
    127 #include <errno.h>
    128 #include <stdarg.h>
    129 #include <stdio.h>
    130 
    131 #include "make.h"
    132 #include "hash.h"
    133 #include "dir.h"
    134 #include "job.h"
    135 #include "buf.h"
    136 #include "pathnames.h"
    137 
    138 /*
    139  * These values are returned by ParseEOF to tell Parse_File whether to
    140  * CONTINUE parsing, i.e. it had only reached the end of an include file,
    141  * or if it's DONE.
    142  */
    143 #define	CONTINUE	1
    144 #define	DONE		0
    145 static Lst     	    targets;	/* targets we're working on */
    146 #ifdef CLEANUP
    147 static Lst     	    targCmds;	/* command lines for targets */
    148 #endif
    149 static Boolean	    inLine;	/* true if currently in a dependency
    150 				 * line or its commands */
    151 typedef struct {
    152     char *str;
    153     char *ptr;
    154 } PTR;
    155 
    156 static int	    fatals = 0;
    157 
    158 static GNode	    *mainNode;	/* The main target to create. This is the
    159 				 * first target on the first dependency
    160 				 * line in the first makefile */
    161 typedef struct IFile {
    162     const char      *fname;	    /* name of file */
    163     int             lineno;	    /* line number in file */
    164     FILE *          F;		    /* the open stream */
    165     PTR *	    P;	    	    /* the char pointer */
    166 } IFile;
    167 
    168 static IFile	    curFile;
    169 
    170 
    171 /*
    172  * Definitions for handling #include specifications
    173  */
    174 
    175 static Lst      includes;  	/* stack of IFiles generated by
    176 				 * #includes */
    177 Lst         	parseIncPath;	/* list of directories for "..." includes */
    178 Lst         	sysIncPath;	/* list of directories for <...> includes */
    179 Lst         	defIncPath;	/* default directories for <...> includes */
    180 
    181 /*-
    182  * specType contains the SPECial TYPE of the current target. It is
    183  * Not if the target is unspecial. If it *is* special, however, the children
    184  * are linked as children of the parent but not vice versa. This variable is
    185  * set in ParseDoDependency
    186  */
    187 typedef enum {
    188     Begin,  	    /* .BEGIN */
    189     Default,	    /* .DEFAULT */
    190     End,    	    /* .END */
    191     Ignore,	    /* .IGNORE */
    192     Includes,	    /* .INCLUDES */
    193     Interrupt,	    /* .INTERRUPT */
    194     Libs,	    /* .LIBS */
    195     MFlags,	    /* .MFLAGS or .MAKEFLAGS */
    196     Main,	    /* .MAIN and we don't have anything user-specified to
    197 		     * make */
    198     NoExport,	    /* .NOEXPORT */
    199     NoPath,	    /* .NOPATH */
    200     Not,	    /* Not special */
    201     NotParallel,    /* .NOTPARALLEL */
    202     Null,   	    /* .NULL */
    203     ExObjdir,	    /* .OBJDIR */
    204     Order,  	    /* .ORDER */
    205     Parallel,	    /* .PARALLEL */
    206     ExPath,	    /* .PATH */
    207     Phony,	    /* .PHONY */
    208 #ifdef POSIX
    209     Posix,	    /* .POSIX */
    210 #endif
    211     Precious,	    /* .PRECIOUS */
    212     ExShell,	    /* .SHELL */
    213     Silent,	    /* .SILENT */
    214     SingleShell,    /* .SINGLESHELL */
    215     Suffixes,	    /* .SUFFIXES */
    216     Wait,	    /* .WAIT */
    217     Attribute	    /* Generic attribute */
    218 } ParseSpecial;
    219 
    220 static ParseSpecial specType;
    221 
    222 #define	LPAREN	'('
    223 #define	RPAREN	')'
    224 /*
    225  * Predecessor node for handling .ORDER. Initialized to NILGNODE when .ORDER
    226  * seen, then set to each successive source on the line.
    227  */
    228 static GNode	*predecessor;
    229 
    230 /*
    231  * The parseKeywords table is searched using binary search when deciding
    232  * if a target or source is special. The 'spec' field is the ParseSpecial
    233  * type of the keyword ("Not" if the keyword isn't special as a target) while
    234  * the 'op' field is the operator to apply to the list of targets if the
    235  * keyword is used as a source ("0" if the keyword isn't special as a source)
    236  */
    237 static struct {
    238     const char   *name;    	/* Name of keyword */
    239     ParseSpecial  spec;	    	/* Type when used as a target */
    240     int	    	  op;	    	/* Operator when used as a source */
    241 } parseKeywords[] = {
    242 { ".BEGIN", 	  Begin,    	0 },
    243 { ".DEFAULT",	  Default,  	0 },
    244 { ".END",   	  End,	    	0 },
    245 { ".EXEC",	  Attribute,   	OP_EXEC },
    246 { ".IGNORE",	  Ignore,   	OP_IGNORE },
    247 { ".INCLUDES",	  Includes, 	0 },
    248 { ".INTERRUPT",	  Interrupt,	0 },
    249 { ".INVISIBLE",	  Attribute,   	OP_INVISIBLE },
    250 { ".JOIN",  	  Attribute,   	OP_JOIN },
    251 { ".LIBS",  	  Libs,	    	0 },
    252 { ".MADE",	  Attribute,	OP_MADE },
    253 { ".MAIN",	  Main,		0 },
    254 { ".MAKE",  	  Attribute,   	OP_MAKE },
    255 { ".MAKEFLAGS",	  MFlags,   	0 },
    256 { ".MFLAGS",	  MFlags,   	0 },
    257 { ".NOPATH",	  NoPath,	OP_NOPATH },
    258 { ".NOTMAIN",	  Attribute,   	OP_NOTMAIN },
    259 { ".NOTPARALLEL", NotParallel,	0 },
    260 { ".NO_PARALLEL", NotParallel,	0 },
    261 { ".NULL",  	  Null,	    	0 },
    262 { ".OBJDIR",	  ExObjdir,	0 },
    263 { ".OPTIONAL",	  Attribute,   	OP_OPTIONAL },
    264 { ".ORDER", 	  Order,    	0 },
    265 { ".PARALLEL",	  Parallel,	0 },
    266 { ".PATH",	  ExPath,	0 },
    267 { ".PHONY",	  Phony,	OP_PHONY },
    268 #ifdef POSIX
    269 { ".POSIX",	  Posix,	0 },
    270 #endif
    271 { ".PRECIOUS",	  Precious, 	OP_PRECIOUS },
    272 { ".RECURSIVE",	  Attribute,	OP_MAKE },
    273 { ".SHELL", 	  ExShell,    	0 },
    274 { ".SILENT",	  Silent,   	OP_SILENT },
    275 { ".SINGLESHELL", SingleShell,	0 },
    276 { ".SUFFIXES",	  Suffixes, 	0 },
    277 { ".USE",   	  Attribute,   	OP_USE },
    278 { ".USEBEFORE",   Attribute,   	OP_USEBEFORE },
    279 { ".WAIT",	  Wait, 	0 },
    280 };
    281 
    282 static int ParseIsEscaped(const char *, const char *);
    283 static void ParseErrorInternal(const char *, size_t, int, const char *, ...)
    284      __attribute__((__format__(__printf__, 4, 5)));
    285 static void ParseVErrorInternal(const char *, size_t, int, const char *, va_list)
    286      __attribute__((__format__(__printf__, 4, 0)));
    287 static int ParseFindKeyword(const char *);
    288 static int ParseLinkSrc(ClientData, ClientData);
    289 static int ParseDoOp(ClientData, ClientData);
    290 static void ParseDoSrc(int, const char *);
    291 static int ParseFindMain(ClientData, ClientData);
    292 static int ParseAddDir(ClientData, ClientData);
    293 static int ParseClearPath(ClientData, ClientData);
    294 static void ParseDoDependency(char *);
    295 static int ParseAddCmd(ClientData, ClientData);
    296 static inline int ParseReadc(void);
    297 static void ParseUnreadc(int);
    298 static void ParseHasCommands(ClientData);
    299 static void ParseDoInclude(char *);
    300 static void ParseSetParseFile(const char *);
    301 #ifdef SYSVINCLUDE
    302 static void ParseTraditionalInclude(char *);
    303 #endif
    304 static int ParseEOF(int);
    305 static char *ParseReadLine(void);
    306 static char *ParseSkipLine(int, int);
    307 static void ParseFinishLine(void);
    308 static void ParseMark(GNode *);
    309 
    310 extern int  maxJobs;
    311 
    312 
    313 /*-
    314  *----------------------------------------------------------------------
    315  * ParseIsEscaped --
    316  *	Check if the current character is escaped on the current line
    317  *
    318  * Results:
    319  *	0 if the character is not backslash escaped, 1 otherwise
    320  *
    321  * Side Effects:
    322  *	None
    323  *----------------------------------------------------------------------
    324  */
    325 static int
    326 ParseIsEscaped(const char *line, const char *c)
    327 {
    328     int active = 0;
    329     for (;;) {
    330 	if (line == c)
    331 	    return active;
    332 	if (*--c != '\\')
    333 	    return active;
    334 	active = !active;
    335     }
    336 }
    337 
    338 /*-
    339  *----------------------------------------------------------------------
    340  * ParseFindKeyword --
    341  *	Look in the table of keywords for one matching the given string.
    342  *
    343  * Input:
    344  *	str		String to find
    345  *
    346  * Results:
    347  *	The index of the keyword, or -1 if it isn't there.
    348  *
    349  * Side Effects:
    350  *	None
    351  *----------------------------------------------------------------------
    352  */
    353 static int
    354 ParseFindKeyword(const char *str)
    355 {
    356     int    start, end, cur;
    357     int    diff;
    358 
    359     start = 0;
    360     end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
    361 
    362     do {
    363 	cur = start + ((end - start) / 2);
    364 	diff = strcmp(str, parseKeywords[cur].name);
    365 
    366 	if (diff == 0) {
    367 	    return (cur);
    368 	} else if (diff < 0) {
    369 	    end = cur - 1;
    370 	} else {
    371 	    start = cur + 1;
    372 	}
    373     } while (start <= end);
    374     return (-1);
    375 }
    376 
    377 /*-
    378  * ParseVErrorInternal  --
    379  *	Error message abort function for parsing. Prints out the context
    380  *	of the error (line number and file) as well as the message with
    381  *	two optional arguments.
    382  *
    383  * Results:
    384  *	None
    385  *
    386  * Side Effects:
    387  *	"fatals" is incremented if the level is PARSE_FATAL.
    388  */
    389 /* VARARGS */
    390 static void
    391 ParseVErrorInternal(const char *cfname, size_t clineno, int type,
    392     const char *fmt, va_list ap)
    393 {
    394 	static Boolean fatal_warning_error_printed = FALSE;
    395 
    396 	(void)fprintf(stderr, "%s: \"", progname);
    397 
    398 	if (*cfname != '/') {
    399 		char *cp;
    400 		const char *dir;
    401 
    402 		/*
    403 		 * Nothing is more anoying than not knowing which Makefile
    404 		 * is the culprit.
    405 		 */
    406 		dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &cp);
    407 		if (dir == NULL || *dir == '\0' ||
    408 		    (*dir == '.' && dir[1] == '\0'))
    409 			dir = Var_Value(".CURDIR", VAR_GLOBAL, &cp);
    410 		if (dir == NULL)
    411 			dir = ".";
    412 
    413 		(void)fprintf(stderr, "%s/%s", dir, cfname);
    414 	} else
    415 		(void)fprintf(stderr, "%s", cfname);
    416 
    417 	(void)fprintf(stderr, "\" line %d: ", (int)clineno);
    418 	if (type == PARSE_WARNING)
    419 		(void)fprintf(stderr, "warning: ");
    420 	(void)vfprintf(stderr, fmt, ap);
    421 	(void)fprintf(stderr, "\n");
    422 	(void)fflush(stderr);
    423 	if (type == PARSE_FATAL || parseWarnFatal)
    424 		fatals += 1;
    425 	if (parseWarnFatal && !fatal_warning_error_printed) {
    426 		Error("parsing warnings being treated as errors");
    427 		fatal_warning_error_printed = TRUE;
    428 	}
    429 }
    430 
    431 /*-
    432  * ParseErrorInternal  --
    433  *	Error function
    434  *
    435  * Results:
    436  *	None
    437  *
    438  * Side Effects:
    439  *	None
    440  */
    441 /* VARARGS */
    442 static void
    443 ParseErrorInternal(const char *cfname, size_t clineno, int type,
    444     const char *fmt, ...)
    445 {
    446 	va_list ap;
    447 
    448 	va_start(ap, fmt);
    449 	ParseVErrorInternal(cfname, clineno, type, fmt, ap);
    450 	va_end(ap);
    451 }
    452 
    453 /*-
    454  * Parse_Error  --
    455  *	External interface to ParseErrorInternal; uses the default filename
    456  *	Line number.
    457  *
    458  * Results:
    459  *	None
    460  *
    461  * Side Effects:
    462  *	None
    463  */
    464 /* VARARGS */
    465 void
    466 Parse_Error(int type, const char *fmt, ...)
    467 {
    468 	va_list ap;
    469 
    470 	va_start(ap, fmt);
    471 	ParseVErrorInternal(curFile.fname, curFile.lineno, type, fmt, ap);
    472 	va_end(ap);
    473 }
    474 
    475 /*-
    476  *---------------------------------------------------------------------
    477  * ParseLinkSrc  --
    478  *	Link the parent node to its new child. Used in a Lst_ForEach by
    479  *	ParseDoDependency. If the specType isn't 'Not', the parent
    480  *	isn't linked as a parent of the child.
    481  *
    482  * Input:
    483  *	pgnp		The parent node
    484  *	cgpn		The child node
    485  *
    486  * Results:
    487  *	Always = 0
    488  *
    489  * Side Effects:
    490  *	New elements are added to the parents list of cgn and the
    491  *	children list of cgn. the unmade field of pgn is updated
    492  *	to reflect the additional child.
    493  *---------------------------------------------------------------------
    494  */
    495 static int
    496 ParseLinkSrc(ClientData pgnp, ClientData cgnp)
    497 {
    498     GNode          *pgn = (GNode *)pgnp;
    499     GNode          *cgn = (GNode *)cgnp;
    500 
    501     if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (pgn->cohorts))
    502 	pgn = (GNode *)Lst_Datum(Lst_Last(pgn->cohorts));
    503     (void)Lst_AtEnd(pgn->children, cgn);
    504     if (specType == Not)
    505 	    (void)Lst_AtEnd(cgn->parents, pgn);
    506     pgn->unmade += 1;
    507     if (DEBUG(PARSE)) {
    508 	fprintf(debug_file, "# ParseLinkSrc: added child %s - %s\n", pgn->name, cgn->name);
    509 	Targ_PrintNode(pgn, 0);
    510 	Targ_PrintNode(cgn, 0);
    511     }
    512     return (0);
    513 }
    514 
    515 /*-
    516  *---------------------------------------------------------------------
    517  * ParseDoOp  --
    518  *	Apply the parsed operator to the given target node. Used in a
    519  *	Lst_ForEach call by ParseDoDependency once all targets have
    520  *	been found and their operator parsed. If the previous and new
    521  *	operators are incompatible, a major error is taken.
    522  *
    523  * Input:
    524  *	gnp		The node to which the operator is to be applied
    525  *	opp		The operator to apply
    526  *
    527  * Results:
    528  *	Always 0
    529  *
    530  * Side Effects:
    531  *	The type field of the node is altered to reflect any new bits in
    532  *	the op.
    533  *---------------------------------------------------------------------
    534  */
    535 static int
    536 ParseDoOp(ClientData gnp, ClientData opp)
    537 {
    538     GNode          *gn = (GNode *)gnp;
    539     int             op = *(int *)opp;
    540     /*
    541      * If the dependency mask of the operator and the node don't match and
    542      * the node has actually had an operator applied to it before, and
    543      * the operator actually has some dependency information in it, complain.
    544      */
    545     if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
    546 	!OP_NOP(gn->type) && !OP_NOP(op))
    547     {
    548 	Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
    549 	return (1);
    550     }
    551 
    552     if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
    553 	/*
    554 	 * If the node was the object of a :: operator, we need to create a
    555 	 * new instance of it for the children and commands on this dependency
    556 	 * line. The new instance is placed on the 'cohorts' list of the
    557 	 * initial one (note the initial one is not on its own cohorts list)
    558 	 * and the new instance is linked to all parents of the initial
    559 	 * instance.
    560 	 */
    561 	GNode	*cohort;
    562 
    563 	/*
    564 	 * Propagate copied bits to the initial node.  They'll be propagated
    565 	 * back to the rest of the cohorts later.
    566 	 */
    567 	gn->type |= op & ~OP_OPMASK;
    568 
    569 	cohort = Targ_FindNode(gn->name, TARG_NOHASH);
    570 	/*
    571 	 * Make the cohort invisible as well to avoid duplicating it into
    572 	 * other variables. True, parents of this target won't tend to do
    573 	 * anything with their local variables, but better safe than
    574 	 * sorry. (I think this is pointless now, since the relevant list
    575 	 * traversals will no longer see this node anyway. -mycroft)
    576 	 */
    577 	cohort->type = op | OP_INVISIBLE;
    578 	(void)Lst_AtEnd(gn->cohorts, cohort);
    579 	cohort->centurion = gn;
    580 	gn->unmade_cohorts += 1;
    581 	snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
    582 		gn->unmade_cohorts);
    583     } else {
    584 	/*
    585 	 * We don't want to nuke any previous flags (whatever they were) so we
    586 	 * just OR the new operator into the old
    587 	 */
    588 	gn->type |= op;
    589     }
    590 
    591     return (0);
    592 }
    593 
    594 /*-
    595  *---------------------------------------------------------------------
    596  * ParseDoSrc  --
    597  *	Given the name of a source, figure out if it is an attribute
    598  *	and apply it to the targets if it is. Else decide if there is
    599  *	some attribute which should be applied *to* the source because
    600  *	of some special target and apply it if so. Otherwise, make the
    601  *	source be a child of the targets in the list 'targets'
    602  *
    603  * Input:
    604  *	tOp		operator (if any) from special targets
    605  *	src		name of the source to handle
    606  *
    607  * Results:
    608  *	None
    609  *
    610  * Side Effects:
    611  *	Operator bits may be added to the list of targets or to the source.
    612  *	The targets may have a new source added to their lists of children.
    613  *---------------------------------------------------------------------
    614  */
    615 static void
    616 ParseDoSrc(int tOp, const char *src)
    617 {
    618     GNode	*gn = NULL;
    619     static int wait_number = 0;
    620     char wait_src[16];
    621 
    622     if (*src == '.' && isupper ((unsigned char)src[1])) {
    623 	int keywd = ParseFindKeyword(src);
    624 	if (keywd != -1) {
    625 	    int op = parseKeywords[keywd].op;
    626 	    if (op != 0) {
    627 		Lst_ForEach(targets, ParseDoOp, &op);
    628 		return;
    629 	    }
    630 	    if (parseKeywords[keywd].spec == Wait) {
    631 		/*
    632 		 * We add a .WAIT node in the dependency list.
    633 		 * After any dynamic dependencies (and filename globbing)
    634 		 * have happened, it is given a dependency on the each
    635 		 * previous child back to and previous .WAIT node.
    636 		 * The next child won't be scheduled until the .WAIT node
    637 		 * is built.
    638 		 * We give each .WAIT node a unique name (mainly for diag).
    639 		 */
    640 		snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
    641 		gn = Targ_FindNode(wait_src, TARG_NOHASH);
    642 		gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
    643 		Lst_ForEach(targets, ParseLinkSrc, gn);
    644 		return;
    645 	    }
    646 	}
    647     }
    648 
    649     switch (specType) {
    650     case Main:
    651 	/*
    652 	 * If we have noted the existence of a .MAIN, it means we need
    653 	 * to add the sources of said target to the list of things
    654 	 * to create. The string 'src' is likely to be free, so we
    655 	 * must make a new copy of it. Note that this will only be
    656 	 * invoked if the user didn't specify a target on the command
    657 	 * line. This is to allow #ifmake's to succeed, or something...
    658 	 */
    659 	(void)Lst_AtEnd(create, estrdup(src));
    660 	/*
    661 	 * Add the name to the .TARGETS variable as well, so the user can
    662 	 * employ that, if desired.
    663 	 */
    664 	Var_Append(".TARGETS", src, VAR_GLOBAL);
    665 	return;
    666 
    667     case Order:
    668 	/*
    669 	 * Create proper predecessor/successor links between the previous
    670 	 * source and the current one.
    671 	 */
    672 	gn = Targ_FindNode(src, TARG_CREATE);
    673 	if (predecessor != NILGNODE) {
    674 	    (void)Lst_AtEnd(predecessor->order_succ, gn);
    675 	    (void)Lst_AtEnd(gn->order_pred, predecessor);
    676 	    if (DEBUG(PARSE)) {
    677 		fprintf(debug_file, "# ParseDoSrc: added Order dependency %s - %s\n",
    678 			predecessor->name, gn->name);
    679 		Targ_PrintNode(predecessor, 0);
    680 		Targ_PrintNode(gn, 0);
    681 	    }
    682 	}
    683 	/*
    684 	 * The current source now becomes the predecessor for the next one.
    685 	 */
    686 	predecessor = gn;
    687 	break;
    688 
    689     default:
    690 	/*
    691 	 * If the source is not an attribute, we need to find/create
    692 	 * a node for it. After that we can apply any operator to it
    693 	 * from a special target or link it to its parents, as
    694 	 * appropriate.
    695 	 *
    696 	 * In the case of a source that was the object of a :: operator,
    697 	 * the attribute is applied to all of its instances (as kept in
    698 	 * the 'cohorts' list of the node) or all the cohorts are linked
    699 	 * to all the targets.
    700 	 */
    701 
    702 	/* Find/create the 'src' node and attach to all targets */
    703 	gn = Targ_FindNode(src, TARG_CREATE);
    704 	if (tOp) {
    705 	    gn->type |= tOp;
    706 	} else {
    707 	    Lst_ForEach(targets, ParseLinkSrc, gn);
    708 	}
    709 	break;
    710     }
    711 }
    712 
    713 /*-
    714  *-----------------------------------------------------------------------
    715  * ParseFindMain --
    716  *	Find a real target in the list and set it to be the main one.
    717  *	Called by ParseDoDependency when a main target hasn't been found
    718  *	yet.
    719  *
    720  * Input:
    721  *	gnp		Node to examine
    722  *
    723  * Results:
    724  *	0 if main not found yet, 1 if it is.
    725  *
    726  * Side Effects:
    727  *	mainNode is changed and Targ_SetMain is called.
    728  *
    729  *-----------------------------------------------------------------------
    730  */
    731 static int
    732 ParseFindMain(ClientData gnp, ClientData dummy)
    733 {
    734     GNode   	  *gn = (GNode *)gnp;
    735     if ((gn->type & OP_NOTARGET) == 0) {
    736 	mainNode = gn;
    737 	Targ_SetMain(gn);
    738 	return (dummy ? 1 : 1);
    739     } else {
    740 	return (dummy ? 0 : 0);
    741     }
    742 }
    743 
    744 /*-
    745  *-----------------------------------------------------------------------
    746  * ParseAddDir --
    747  *	Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
    748  *
    749  * Results:
    750  *	=== 0
    751  *
    752  * Side Effects:
    753  *	See Dir_AddDir.
    754  *
    755  *-----------------------------------------------------------------------
    756  */
    757 static int
    758 ParseAddDir(ClientData path, ClientData name)
    759 {
    760     (void)Dir_AddDir((Lst) path, (char *)name);
    761     return(0);
    762 }
    763 
    764 /*-
    765  *-----------------------------------------------------------------------
    766  * ParseClearPath --
    767  *	Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
    768  *
    769  * Results:
    770  *	=== 0
    771  *
    772  * Side Effects:
    773  *	See Dir_ClearPath
    774  *
    775  *-----------------------------------------------------------------------
    776  */
    777 static int
    778 ParseClearPath(ClientData path, ClientData dummy)
    779 {
    780     Dir_ClearPath((Lst) path);
    781     return(dummy ? 0 : 0);
    782 }
    783 
    784 /*-
    785  *---------------------------------------------------------------------
    786  * ParseDoDependency  --
    787  *	Parse the dependency line in line.
    788  *
    789  * Input:
    790  *	line		the line to parse
    791  *
    792  * Results:
    793  *	None
    794  *
    795  * Side Effects:
    796  *	The nodes of the sources are linked as children to the nodes of the
    797  *	targets. Some nodes may be created.
    798  *
    799  *	We parse a dependency line by first extracting words from the line and
    800  * finding nodes in the list of all targets with that name. This is done
    801  * until a character is encountered which is an operator character. Currently
    802  * these are only ! and :. At this point the operator is parsed and the
    803  * pointer into the line advanced until the first source is encountered.
    804  * 	The parsed operator is applied to each node in the 'targets' list,
    805  * which is where the nodes found for the targets are kept, by means of
    806  * the ParseDoOp function.
    807  *	The sources are read in much the same way as the targets were except
    808  * that now they are expanded using the wildcarding scheme of the C-Shell
    809  * and all instances of the resulting words in the list of all targets
    810  * are found. Each of the resulting nodes is then linked to each of the
    811  * targets as one of its children.
    812  *	Certain targets are handled specially. These are the ones detailed
    813  * by the specType variable.
    814  *	The storing of transformation rules is also taken care of here.
    815  * A target is recognized as a transformation rule by calling
    816  * Suff_IsTransform. If it is a transformation rule, its node is gotten
    817  * from the suffix module via Suff_AddTransform rather than the standard
    818  * Targ_FindNode in the target module.
    819  *---------------------------------------------------------------------
    820  */
    821 static void
    822 ParseDoDependency(char *line)
    823 {
    824     char  	   *cp;		/* our current position */
    825     GNode 	   *gn = NULL;	/* a general purpose temporary node */
    826     int             op;		/* the operator on the line */
    827     char            savec;	/* a place to save a character */
    828     Lst    	    paths;   	/* List of search paths to alter when parsing
    829 				 * a list of .PATH targets */
    830     int	    	    tOp;    	/* operator from special target */
    831     Lst	    	    sources;	/* list of archive source names after
    832 				 * expansion */
    833     Lst 	    curTargs;	/* list of target names to be found and added
    834 				 * to the targets list */
    835     char	   *lstart = line;
    836 
    837     if (DEBUG(PARSE))
    838 	fprintf(debug_file, "ParseDoDependency(%s)\n", line);
    839     tOp = 0;
    840 
    841     specType = Not;
    842     paths = (Lst)NULL;
    843 
    844     curTargs = Lst_Init(FALSE);
    845 
    846     do {
    847 	for (cp = line; *cp && (ParseIsEscaped(lstart, cp) ||
    848 		     !(isspace((unsigned char)*cp) ||
    849 			 *cp == '!' || *cp == ':' || *cp == LPAREN));
    850 		 cp++) {
    851 	    if (*cp == '$') {
    852 		/*
    853 		 * Must be a dynamic source (would have been expanded
    854 		 * otherwise), so call the Var module to parse the puppy
    855 		 * so we can safely advance beyond it...There should be
    856 		 * no errors in this, as they would have been discovered
    857 		 * in the initial Var_Subst and we wouldn't be here.
    858 		 */
    859 		int 	length;
    860 		void    *freeIt;
    861 		char	*result;
    862 
    863 		result = Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
    864 		if (freeIt)
    865 		    free(freeIt);
    866 		cp += length-1;
    867 	    }
    868 	}
    869 
    870 	if (!ParseIsEscaped(lstart, cp) && *cp == LPAREN) {
    871 	    /*
    872 	     * Archives must be handled specially to make sure the OP_ARCHV
    873 	     * flag is set in their 'type' field, for one thing, and because
    874 	     * things like "archive(file1.o file2.o file3.o)" are permissible.
    875 	     * Arch_ParseArchive will set 'line' to be the first non-blank
    876 	     * after the archive-spec. It creates/finds nodes for the members
    877 	     * and places them on the given list, returning SUCCESS if all
    878 	     * went well and FAILURE if there was an error in the
    879 	     * specification. On error, line should remain untouched.
    880 	     */
    881 	    if (Arch_ParseArchive(&line, targets, VAR_CMD) != SUCCESS) {
    882 		Parse_Error(PARSE_FATAL,
    883 			     "Error in archive specification: \"%s\"", line);
    884 		goto out;
    885 	    } else {
    886 		continue;
    887 	    }
    888 	}
    889 	savec = *cp;
    890 
    891 	if (!*cp) {
    892 	    /*
    893 	     * Ending a dependency line without an operator is a Bozo
    894 	     * no-no.  As a heuristic, this is also often triggered by
    895 	     * undetected conflicts from cvs/rcs merges.
    896 	     */
    897 	    if ((strncmp(line, "<<<<<<", 6) == 0) ||
    898 		(strncmp(line, "======", 6) == 0) ||
    899 		(strncmp(line, ">>>>>>", 6) == 0))
    900 		Parse_Error(PARSE_FATAL,
    901 		    "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
    902 	    else
    903 		Parse_Error(PARSE_FATAL, "Need an operator");
    904 	    goto out;
    905 	}
    906 	*cp = '\0';
    907 
    908 	/*
    909 	 * Have a word in line. See if it's a special target and set
    910 	 * specType to match it.
    911 	 */
    912 	if (*line == '.' && isupper ((unsigned char)line[1])) {
    913 	    /*
    914 	     * See if the target is a special target that must have it
    915 	     * or its sources handled specially.
    916 	     */
    917 	    int keywd = ParseFindKeyword(line);
    918 	    if (keywd != -1) {
    919 		if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
    920 		    Parse_Error(PARSE_FATAL, "Mismatched special targets");
    921 		    goto out;
    922 		}
    923 
    924 		specType = parseKeywords[keywd].spec;
    925 		tOp = parseKeywords[keywd].op;
    926 
    927 		/*
    928 		 * Certain special targets have special semantics:
    929 		 *	.PATH		Have to set the dirSearchPath
    930 		 *			variable too
    931 		 *	.MAIN		Its sources are only used if
    932 		 *			nothing has been specified to
    933 		 *			create.
    934 		 *	.DEFAULT    	Need to create a node to hang
    935 		 *			commands on, but we don't want
    936 		 *			it in the graph, nor do we want
    937 		 *			it to be the Main Target, so we
    938 		 *			create it, set OP_NOTMAIN and
    939 		 *			add it to the list, setting
    940 		 *			DEFAULT to the new node for
    941 		 *			later use. We claim the node is
    942 		 *	    	    	A transformation rule to make
    943 		 *	    	    	life easier later, when we'll
    944 		 *	    	    	use Make_HandleUse to actually
    945 		 *	    	    	apply the .DEFAULT commands.
    946 		 *	.PHONY		The list of targets
    947 		 *	.NOPATH		Don't search for file in the path
    948 		 *	.BEGIN
    949 		 *	.END
    950 		 *	.INTERRUPT  	Are not to be considered the
    951 		 *			main target.
    952 		 *  	.NOTPARALLEL	Make only one target at a time.
    953 		 *  	.SINGLESHELL	Create a shell for each command.
    954 		 *  	.ORDER	    	Must set initial predecessor to NIL
    955 		 */
    956 		switch (specType) {
    957 		    case ExPath:
    958 			if (paths == NULL) {
    959 			    paths = Lst_Init(FALSE);
    960 			}
    961 			(void)Lst_AtEnd(paths, dirSearchPath);
    962 			break;
    963 		    case Main:
    964 			if (!Lst_IsEmpty(create)) {
    965 			    specType = Not;
    966 			}
    967 			break;
    968 		    case Begin:
    969 		    case End:
    970 		    case Interrupt:
    971 			gn = Targ_FindNode(line, TARG_CREATE);
    972 			gn->type |= OP_NOTMAIN|OP_SPECIAL;
    973 			(void)Lst_AtEnd(targets, gn);
    974 			break;
    975 		    case Default:
    976 			gn = Targ_NewGN(".DEFAULT");
    977 			gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
    978 			(void)Lst_AtEnd(targets, gn);
    979 			DEFAULT = gn;
    980 			break;
    981 		    case NotParallel:
    982 			maxJobs = 1;
    983 			break;
    984 		    case SingleShell:
    985 			compatMake = TRUE;
    986 			break;
    987 		    case Order:
    988 			predecessor = NILGNODE;
    989 			break;
    990 		    default:
    991 			break;
    992 		}
    993 	    } else if (strncmp(line, ".PATH", 5) == 0) {
    994 		/*
    995 		 * .PATH<suffix> has to be handled specially.
    996 		 * Call on the suffix module to give us a path to
    997 		 * modify.
    998 		 */
    999 		Lst 	path;
   1000 
   1001 		specType = ExPath;
   1002 		path = Suff_GetPath(&line[5]);
   1003 		if (path == NILLST) {
   1004 		    Parse_Error(PARSE_FATAL,
   1005 				 "Suffix '%s' not defined (yet)",
   1006 				 &line[5]);
   1007 		    goto out;
   1008 		} else {
   1009 		    if (paths == (Lst)NULL) {
   1010 			paths = Lst_Init(FALSE);
   1011 		    }
   1012 		    (void)Lst_AtEnd(paths, path);
   1013 		}
   1014 	    }
   1015 	}
   1016 
   1017 	/*
   1018 	 * Have word in line. Get or create its node and stick it at
   1019 	 * the end of the targets list
   1020 	 */
   1021 	if ((specType == Not) && (*line != '\0')) {
   1022 	    if (Dir_HasWildcards(line)) {
   1023 		/*
   1024 		 * Targets are to be sought only in the current directory,
   1025 		 * so create an empty path for the thing. Note we need to
   1026 		 * use Dir_Destroy in the destruction of the path as the
   1027 		 * Dir module could have added a directory to the path...
   1028 		 */
   1029 		Lst	    emptyPath = Lst_Init(FALSE);
   1030 
   1031 		Dir_Expand(line, emptyPath, curTargs);
   1032 
   1033 		Lst_Destroy(emptyPath, Dir_Destroy);
   1034 	    } else {
   1035 		/*
   1036 		 * No wildcards, but we want to avoid code duplication,
   1037 		 * so create a list with the word on it.
   1038 		 */
   1039 		(void)Lst_AtEnd(curTargs, line);
   1040 	    }
   1041 
   1042 	    while(!Lst_IsEmpty(curTargs)) {
   1043 		char	*targName = (char *)Lst_DeQueue(curTargs);
   1044 
   1045 		if (!Suff_IsTransform (targName)) {
   1046 		    gn = Targ_FindNode(targName, TARG_CREATE);
   1047 		} else {
   1048 		    gn = Suff_AddTransform(targName);
   1049 		}
   1050 
   1051 		(void)Lst_AtEnd(targets, gn);
   1052 	    }
   1053 	} else if (specType == ExPath && *line != '.' && *line != '\0') {
   1054 	    Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
   1055 	}
   1056 
   1057 	*cp = savec;
   1058 	/*
   1059 	 * If it is a special type and not .PATH, it's the only target we
   1060 	 * allow on this line...
   1061 	 */
   1062 	if (specType != Not && specType != ExPath) {
   1063 	    Boolean warning = FALSE;
   1064 
   1065 	    while (*cp && (ParseIsEscaped(lstart, cp) ||
   1066 		((*cp != '!') && (*cp != ':')))) {
   1067 		if (ParseIsEscaped(lstart, cp) ||
   1068 		    (*cp != ' ' && *cp != '\t')) {
   1069 		    warning = TRUE;
   1070 		}
   1071 		cp++;
   1072 	    }
   1073 	    if (warning) {
   1074 		Parse_Error(PARSE_WARNING, "Extra target ignored");
   1075 	    }
   1076 	} else {
   1077 	    while (*cp && isspace ((unsigned char)*cp)) {
   1078 		cp++;
   1079 	    }
   1080 	}
   1081 	line = cp;
   1082     } while (*line && (ParseIsEscaped(lstart, line) ||
   1083 	((*line != '!') && (*line != ':'))));
   1084 
   1085     /*
   1086      * Don't need the list of target names anymore...
   1087      */
   1088     Lst_Destroy(curTargs, NOFREE);
   1089     curTargs = NULL;
   1090 
   1091     if (!Lst_IsEmpty(targets)) {
   1092 	switch(specType) {
   1093 	    default:
   1094 		Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
   1095 		break;
   1096 	    case Default:
   1097 	    case Begin:
   1098 	    case End:
   1099 	    case Interrupt:
   1100 		/*
   1101 		 * These four create nodes on which to hang commands, so
   1102 		 * targets shouldn't be empty...
   1103 		 */
   1104 	    case Not:
   1105 		/*
   1106 		 * Nothing special here -- targets can be empty if it wants.
   1107 		 */
   1108 		break;
   1109 	}
   1110     }
   1111 
   1112     /*
   1113      * Have now parsed all the target names. Must parse the operator next. The
   1114      * result is left in  op .
   1115      */
   1116     if (*cp == '!') {
   1117 	op = OP_FORCE;
   1118     } else if (*cp == ':') {
   1119 	if (cp[1] == ':') {
   1120 	    op = OP_DOUBLEDEP;
   1121 	    cp++;
   1122 	} else {
   1123 	    op = OP_DEPENDS;
   1124 	}
   1125     } else {
   1126 	Parse_Error(PARSE_FATAL, "Missing dependency operator");
   1127 	goto out;
   1128     }
   1129 
   1130     cp++;			/* Advance beyond operator */
   1131 
   1132     Lst_ForEach(targets, ParseDoOp, &op);
   1133 
   1134     /*
   1135      * Get to the first source
   1136      */
   1137     while (*cp && isspace ((unsigned char)*cp)) {
   1138 	cp++;
   1139     }
   1140     line = cp;
   1141 
   1142     /*
   1143      * Several special targets take different actions if present with no
   1144      * sources:
   1145      *	a .SUFFIXES line with no sources clears out all old suffixes
   1146      *	a .PRECIOUS line makes all targets precious
   1147      *	a .IGNORE line ignores errors for all targets
   1148      *	a .SILENT line creates silence when making all targets
   1149      *	a .PATH removes all directories from the search path(s).
   1150      */
   1151     if (!*line) {
   1152 	switch (specType) {
   1153 	    case Suffixes:
   1154 		Suff_ClearSuffixes();
   1155 		break;
   1156 	    case Precious:
   1157 		allPrecious = TRUE;
   1158 		break;
   1159 	    case Ignore:
   1160 		ignoreErrors = TRUE;
   1161 		break;
   1162 	    case Silent:
   1163 		beSilent = TRUE;
   1164 		break;
   1165 	    case ExPath:
   1166 		Lst_ForEach(paths, ParseClearPath, NULL);
   1167 		Dir_SetPATH();
   1168 		break;
   1169 #ifdef POSIX
   1170             case Posix:
   1171                 Var_Set("%POSIX", "1003.2", VAR_GLOBAL, 0);
   1172                 break;
   1173 #endif
   1174 	    default:
   1175 		break;
   1176 	}
   1177     } else if (specType == MFlags) {
   1178 	/*
   1179 	 * Call on functions in main.c to deal with these arguments and
   1180 	 * set the initial character to a null-character so the loop to
   1181 	 * get sources won't get anything
   1182 	 */
   1183 	Main_ParseArgLine(line);
   1184 	*line = '\0';
   1185     } else if (specType == ExShell) {
   1186 	if (Job_ParseShell(line) != SUCCESS) {
   1187 	    Parse_Error(PARSE_FATAL, "improper shell specification");
   1188 	    goto out;
   1189 	}
   1190 	*line = '\0';
   1191     } else if ((specType == NotParallel) || (specType == SingleShell)) {
   1192 	*line = '\0';
   1193     }
   1194 
   1195     /*
   1196      * NOW GO FOR THE SOURCES
   1197      */
   1198     if ((specType == Suffixes) || (specType == ExPath) ||
   1199 	(specType == Includes) || (specType == Libs) ||
   1200 	(specType == Null) || (specType == ExObjdir))
   1201     {
   1202 	while (*line) {
   1203 	    /*
   1204 	     * If the target was one that doesn't take files as its sources
   1205 	     * but takes something like suffixes, we take each
   1206 	     * space-separated word on the line as a something and deal
   1207 	     * with it accordingly.
   1208 	     *
   1209 	     * If the target was .SUFFIXES, we take each source as a
   1210 	     * suffix and add it to the list of suffixes maintained by the
   1211 	     * Suff module.
   1212 	     *
   1213 	     * If the target was a .PATH, we add the source as a directory
   1214 	     * to search on the search path.
   1215 	     *
   1216 	     * If it was .INCLUDES, the source is taken to be the suffix of
   1217 	     * files which will be #included and whose search path should
   1218 	     * be present in the .INCLUDES variable.
   1219 	     *
   1220 	     * If it was .LIBS, the source is taken to be the suffix of
   1221 	     * files which are considered libraries and whose search path
   1222 	     * should be present in the .LIBS variable.
   1223 	     *
   1224 	     * If it was .NULL, the source is the suffix to use when a file
   1225 	     * has no valid suffix.
   1226 	     *
   1227 	     * If it was .OBJDIR, the source is a new definition for .OBJDIR,
   1228 	     * and will cause make to do a new chdir to that path.
   1229 	     */
   1230 	    while (*cp && !isspace ((unsigned char)*cp)) {
   1231 		cp++;
   1232 	    }
   1233 	    savec = *cp;
   1234 	    *cp = '\0';
   1235 	    switch (specType) {
   1236 		case Suffixes:
   1237 		    Suff_AddSuffix(line, &mainNode);
   1238 		    break;
   1239 		case ExPath:
   1240 		    Lst_ForEach(paths, ParseAddDir, line);
   1241 		    break;
   1242 		case Includes:
   1243 		    Suff_AddInclude(line);
   1244 		    break;
   1245 		case Libs:
   1246 		    Suff_AddLib(line);
   1247 		    break;
   1248 		case Null:
   1249 		    Suff_SetNull(line);
   1250 		    break;
   1251 		case ExObjdir:
   1252 		    Main_SetObjdir(line);
   1253 		    break;
   1254 		default:
   1255 		    break;
   1256 	    }
   1257 	    *cp = savec;
   1258 	    if (savec != '\0') {
   1259 		cp++;
   1260 	    }
   1261 	    while (*cp && isspace ((unsigned char)*cp)) {
   1262 		cp++;
   1263 	    }
   1264 	    line = cp;
   1265 	}
   1266 	if (paths) {
   1267 	    Lst_Destroy(paths, NOFREE);
   1268 	}
   1269 	if (specType == ExPath)
   1270 	    Dir_SetPATH();
   1271     } else {
   1272 	while (*line) {
   1273 	    /*
   1274 	     * The targets take real sources, so we must beware of archive
   1275 	     * specifications (i.e. things with left parentheses in them)
   1276 	     * and handle them accordingly.
   1277 	     */
   1278 	    for (; *cp && !isspace ((unsigned char)*cp); cp++) {
   1279 		if ((*cp == LPAREN) && (cp > line) && (cp[-1] != '$')) {
   1280 		    /*
   1281 		     * Only stop for a left parenthesis if it isn't at the
   1282 		     * start of a word (that'll be for variable changes
   1283 		     * later) and isn't preceded by a dollar sign (a dynamic
   1284 		     * source).
   1285 		     */
   1286 		    break;
   1287 		}
   1288 	    }
   1289 
   1290 	    if (*cp == LPAREN) {
   1291 		sources = Lst_Init(FALSE);
   1292 		if (Arch_ParseArchive(&line, sources, VAR_CMD) != SUCCESS) {
   1293 		    Parse_Error(PARSE_FATAL,
   1294 				 "Error in source archive spec \"%s\"", line);
   1295 		    goto out;
   1296 		}
   1297 
   1298 		while (!Lst_IsEmpty (sources)) {
   1299 		    gn = (GNode *)Lst_DeQueue(sources);
   1300 		    ParseDoSrc(tOp, gn->name);
   1301 		}
   1302 		Lst_Destroy(sources, NOFREE);
   1303 		cp = line;
   1304 	    } else {
   1305 		if (*cp) {
   1306 		    *cp = '\0';
   1307 		    cp += 1;
   1308 		}
   1309 
   1310 		ParseDoSrc(tOp, line);
   1311 	    }
   1312 	    while (*cp && isspace ((unsigned char)*cp)) {
   1313 		cp++;
   1314 	    }
   1315 	    line = cp;
   1316 	}
   1317     }
   1318 
   1319     if (mainNode == NILGNODE) {
   1320 	/*
   1321 	 * If we have yet to decide on a main target to make, in the
   1322 	 * absence of any user input, we want the first target on
   1323 	 * the first dependency line that is actually a real target
   1324 	 * (i.e. isn't a .USE or .EXEC rule) to be made.
   1325 	 */
   1326 	Lst_ForEach(targets, ParseFindMain, NULL);
   1327     }
   1328 
   1329 out:
   1330     if (curTargs)
   1331 	    Lst_Destroy(curTargs, NOFREE);
   1332 }
   1333 
   1334 /*-
   1335  *---------------------------------------------------------------------
   1336  * Parse_IsVar  --
   1337  *	Return TRUE if the passed line is a variable assignment. A variable
   1338  *	assignment consists of a single word followed by optional whitespace
   1339  *	followed by either a += or an = operator.
   1340  *	This function is used both by the Parse_File function and main when
   1341  *	parsing the command-line arguments.
   1342  *
   1343  * Input:
   1344  *	line		the line to check
   1345  *
   1346  * Results:
   1347  *	TRUE if it is. FALSE if it ain't
   1348  *
   1349  * Side Effects:
   1350  *	none
   1351  *---------------------------------------------------------------------
   1352  */
   1353 Boolean
   1354 Parse_IsVar(char *line)
   1355 {
   1356     Boolean wasSpace = FALSE;	/* set TRUE if found a space */
   1357     Boolean haveName = FALSE;	/* Set TRUE if have a variable name */
   1358     int level = 0;
   1359 #define ISEQOPERATOR(c) \
   1360 	(((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
   1361 
   1362     /*
   1363      * Skip to variable name
   1364      */
   1365     for (;(*line == ' ') || (*line == '\t'); line++)
   1366 	continue;
   1367 
   1368     for (; *line != '=' || level != 0; line++)
   1369 	switch (*line) {
   1370 	case '\0':
   1371 	    /*
   1372 	     * end-of-line -- can't be a variable assignment.
   1373 	     */
   1374 	    return FALSE;
   1375 
   1376 	case ' ':
   1377 	case '\t':
   1378 	    /*
   1379 	     * there can be as much white space as desired so long as there is
   1380 	     * only one word before the operator
   1381 	     */
   1382 	    wasSpace = TRUE;
   1383 	    break;
   1384 
   1385 	case LPAREN:
   1386 	case '{':
   1387 	    level++;
   1388 	    break;
   1389 
   1390 	case '}':
   1391 	case RPAREN:
   1392 	    level--;
   1393 	    break;
   1394 
   1395 	default:
   1396 	    if (wasSpace && haveName) {
   1397 		    if (ISEQOPERATOR(*line)) {
   1398 			/*
   1399 			 * We must have a finished word
   1400 			 */
   1401 			if (level != 0)
   1402 			    return FALSE;
   1403 
   1404 			/*
   1405 			 * When an = operator [+?!:] is found, the next
   1406 			 * character must be an = or it ain't a valid
   1407 			 * assignment.
   1408 			 */
   1409 			if (line[1] == '=')
   1410 			    return haveName;
   1411 #ifdef SUNSHCMD
   1412 			/*
   1413 			 * This is a shell command
   1414 			 */
   1415 			if (strncmp(line, ":sh", 3) == 0)
   1416 			    return haveName;
   1417 #endif
   1418 		    }
   1419 		    /*
   1420 		     * This is the start of another word, so not assignment.
   1421 		     */
   1422 		    return FALSE;
   1423 	    }
   1424 	    else {
   1425 		haveName = TRUE;
   1426 		wasSpace = FALSE;
   1427 	    }
   1428 	    break;
   1429 	}
   1430 
   1431     return haveName;
   1432 }
   1433 
   1434 /*-
   1435  *---------------------------------------------------------------------
   1436  * Parse_DoVar  --
   1437  *	Take the variable assignment in the passed line and do it in the
   1438  *	global context.
   1439  *
   1440  *	Note: There is a lexical ambiguity with assignment modifier characters
   1441  *	in variable names. This routine interprets the character before the =
   1442  *	as a modifier. Therefore, an assignment like
   1443  *	    C++=/usr/bin/CC
   1444  *	is interpreted as "C+ +=" instead of "C++ =".
   1445  *
   1446  * Input:
   1447  *	line		a line guaranteed to be a variable assignment.
   1448  *			This reduces error checks
   1449  *	ctxt		Context in which to do the assignment
   1450  *
   1451  * Results:
   1452  *	none
   1453  *
   1454  * Side Effects:
   1455  *	the variable structure of the given variable name is altered in the
   1456  *	global context.
   1457  *---------------------------------------------------------------------
   1458  */
   1459 void
   1460 Parse_DoVar(char *line, GNode *ctxt)
   1461 {
   1462     char	   *cp;	/* pointer into line */
   1463     enum {
   1464 	VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
   1465     }	    	    type;   	/* Type of assignment */
   1466     char            *opc;	/* ptr to operator character to
   1467 				 * null-terminate the variable name */
   1468     Boolean	   freeCp = FALSE; /* TRUE if cp needs to be freed,
   1469 				    * i.e. if any variable expansion was
   1470 				    * performed */
   1471     /*
   1472      * Avoid clobbered variable warnings by forcing the compiler
   1473      * to ``unregister'' variables
   1474      */
   1475 #if __GNUC__
   1476     (void) &cp;
   1477     (void) &line;
   1478 #endif
   1479 
   1480     /*
   1481      * Skip to variable name
   1482      */
   1483     while ((*line == ' ') || (*line == '\t')) {
   1484 	line++;
   1485     }
   1486 
   1487     /*
   1488      * Skip to operator character, nulling out whitespace as we go
   1489      */
   1490     for (cp = line + 1; *cp != '='; cp++) {
   1491 	if (isspace ((unsigned char)*cp)) {
   1492 	    *cp = '\0';
   1493 	}
   1494     }
   1495     opc = cp-1;		/* operator is the previous character */
   1496     *cp++ = '\0';	/* nuke the = */
   1497 
   1498     /*
   1499      * Check operator type
   1500      */
   1501     switch (*opc) {
   1502 	case '+':
   1503 	    type = VAR_APPEND;
   1504 	    *opc = '\0';
   1505 	    break;
   1506 
   1507 	case '?':
   1508 	    /*
   1509 	     * If the variable already has a value, we don't do anything.
   1510 	     */
   1511 	    *opc = '\0';
   1512 	    if (Var_Exists(line, ctxt)) {
   1513 		return;
   1514 	    } else {
   1515 		type = VAR_NORMAL;
   1516 	    }
   1517 	    break;
   1518 
   1519 	case ':':
   1520 	    type = VAR_SUBST;
   1521 	    *opc = '\0';
   1522 	    break;
   1523 
   1524 	case '!':
   1525 	    type = VAR_SHELL;
   1526 	    *opc = '\0';
   1527 	    break;
   1528 
   1529 	default:
   1530 #ifdef SUNSHCMD
   1531 	    while (opc > line && *opc != ':')
   1532 		opc--;
   1533 
   1534 	    if (strncmp(opc, ":sh", 3) == 0) {
   1535 		type = VAR_SHELL;
   1536 		*opc = '\0';
   1537 		break;
   1538 	    }
   1539 #endif
   1540 	    type = VAR_NORMAL;
   1541 	    break;
   1542     }
   1543 
   1544     while (isspace ((unsigned char)*cp)) {
   1545 	cp++;
   1546     }
   1547 
   1548     if (type == VAR_APPEND) {
   1549 	Var_Append(line, cp, ctxt);
   1550     } else if (type == VAR_SUBST) {
   1551 	/*
   1552 	 * Allow variables in the old value to be undefined, but leave their
   1553 	 * invocation alone -- this is done by forcing oldVars to be false.
   1554 	 * XXX: This can cause recursive variables, but that's not hard to do,
   1555 	 * and this allows someone to do something like
   1556 	 *
   1557 	 *  CFLAGS = $(.INCLUDES)
   1558 	 *  CFLAGS := -I.. $(CFLAGS)
   1559 	 *
   1560 	 * And not get an error.
   1561 	 */
   1562 	Boolean	  oldOldVars = oldVars;
   1563 
   1564 	oldVars = FALSE;
   1565 
   1566 	/*
   1567 	 * make sure that we set the variable the first time to nothing
   1568 	 * so that it gets substituted!
   1569 	 */
   1570 	if (!Var_Exists(line, ctxt))
   1571 	    Var_Set(line, "", ctxt, 0);
   1572 
   1573 	cp = Var_Subst(NULL, cp, ctxt, FALSE);
   1574 	oldVars = oldOldVars;
   1575 	freeCp = TRUE;
   1576 
   1577 	Var_Set(line, cp, ctxt, 0);
   1578     } else if (type == VAR_SHELL) {
   1579 	char *res;
   1580 	const char *error;
   1581 
   1582 	if (strchr(cp, '$') != NULL) {
   1583 	    /*
   1584 	     * There's a dollar sign in the command, so perform variable
   1585 	     * expansion on the whole thing. The resulting string will need
   1586 	     * freeing when we're done, so set freeCmd to TRUE.
   1587 	     */
   1588 	    cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
   1589 	    freeCp = TRUE;
   1590 	}
   1591 
   1592 	res = Cmd_Exec(cp, &error);
   1593 	Var_Set(line, res, ctxt, 0);
   1594 	free(res);
   1595 
   1596 	if (error)
   1597 	    Parse_Error(PARSE_WARNING, error, cp);
   1598     } else {
   1599 	/*
   1600 	 * Normal assignment -- just do it.
   1601 	 */
   1602 	Var_Set(line, cp, ctxt, 0);
   1603     }
   1604     if (strcmp(line, MAKEOVERRIDES) == 0)
   1605 	Main_ExportMAKEFLAGS(FALSE);	/* re-export MAKEFLAGS */
   1606     else if (strcmp(line, ".CURDIR") == 0) {
   1607 	/*
   1608 	 * Somone is being (too?) clever...
   1609 	 * Let's pretend they know what they are doing and
   1610 	 * re-initialize the 'cur' Path.
   1611 	 */
   1612 	Dir_InitCur(cp);
   1613 	Dir_SetPATH();
   1614     }
   1615     if (freeCp)
   1616 	free(cp);
   1617 }
   1618 
   1619 
   1620 /*-
   1621  * ParseAddCmd  --
   1622  *	Lst_ForEach function to add a command line to all targets
   1623  *
   1624  * Input:
   1625  *	gnp		the node to which the command is to be added
   1626  *	cmd		the command to add
   1627  *
   1628  * Results:
   1629  *	Always 0
   1630  *
   1631  * Side Effects:
   1632  *	A new element is added to the commands list of the node.
   1633  */
   1634 static int
   1635 ParseAddCmd(ClientData gnp, ClientData cmd)
   1636 {
   1637     GNode *gn = (GNode *)gnp;
   1638 
   1639     /* Add to last (ie current) cohort for :: targets */
   1640     if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts))
   1641 	gn = (GNode *)Lst_Datum(Lst_Last(gn->cohorts));
   1642 
   1643     /* if target already supplied, ignore commands */
   1644     if (!(gn->type & OP_HAS_COMMANDS)) {
   1645 	(void)Lst_AtEnd(gn->commands, cmd);
   1646 	ParseMark(gn);
   1647     } else {
   1648 #ifdef notyet
   1649 	/* XXX: We cannot do this until we fix the tree */
   1650 	(void)Lst_AtEnd(gn->commands, cmd);
   1651 	Parse_Error(PARSE_WARNING,
   1652 		     "overriding commands for target \"%s\"; "
   1653 		     "previous commands defined at %s: %d ignored",
   1654 		     gn->name, gn->fname, gn->lineno);
   1655 #else
   1656 	Parse_Error(PARSE_WARNING,
   1657 		     "duplicate script for target \"%s\" ignored",
   1658 		     gn->name);
   1659 	ParseErrorInternal(gn->fname, gn->lineno, PARSE_WARNING,
   1660 			    "using previous script for \"%s\" defined here",
   1661 			    gn->name);
   1662 #endif
   1663     }
   1664     return(0);
   1665 }
   1666 
   1667 /*-
   1668  *-----------------------------------------------------------------------
   1669  * ParseHasCommands --
   1670  *	Callback procedure for Parse_File when destroying the list of
   1671  *	targets on the last dependency line. Marks a target as already
   1672  *	having commands if it does, to keep from having shell commands
   1673  *	on multiple dependency lines.
   1674  *
   1675  * Input:
   1676  *	gnp		Node to examine
   1677  *
   1678  * Results:
   1679  *	None
   1680  *
   1681  * Side Effects:
   1682  *	OP_HAS_COMMANDS may be set for the target.
   1683  *
   1684  *-----------------------------------------------------------------------
   1685  */
   1686 static void
   1687 ParseHasCommands(ClientData gnp)
   1688 {
   1689     GNode *gn = (GNode *)gnp;
   1690     if (!Lst_IsEmpty(gn->commands)) {
   1691 	gn->type |= OP_HAS_COMMANDS;
   1692     }
   1693 }
   1694 
   1695 /*-
   1696  *-----------------------------------------------------------------------
   1697  * Parse_AddIncludeDir --
   1698  *	Add a directory to the path searched for included makefiles
   1699  *	bracketed by double-quotes. Used by functions in main.c
   1700  *
   1701  * Input:
   1702  *	dir		The name of the directory to add
   1703  *
   1704  * Results:
   1705  *	None.
   1706  *
   1707  * Side Effects:
   1708  *	The directory is appended to the list.
   1709  *
   1710  *-----------------------------------------------------------------------
   1711  */
   1712 void
   1713 Parse_AddIncludeDir(char *dir)
   1714 {
   1715     (void)Dir_AddDir(parseIncPath, dir);
   1716 }
   1717 
   1718 /*-
   1719  *---------------------------------------------------------------------
   1720  * ParseDoInclude  --
   1721  *	Push to another file.
   1722  *
   1723  *	The input is the line minus the `.'. A file spec is a string
   1724  *	enclosed in <> or "". The former is looked for only in sysIncPath.
   1725  *	The latter in . and the directories specified by -I command line
   1726  *	options
   1727  *
   1728  * Results:
   1729  *	None
   1730  *
   1731  * Side Effects:
   1732  *	A structure is added to the includes Lst and readProc, lineno,
   1733  *	fname and curFILE are altered for the new file
   1734  *---------------------------------------------------------------------
   1735  */
   1736 static void
   1737 ParseDoInclude(char *line)
   1738 {
   1739     char          *fullname;	/* full pathname of file */
   1740     IFile         *oldFile;	/* state associated with current file */
   1741     char          endc;	    	/* the character which ends the file spec */
   1742     char          *cp;		/* current position in file spec */
   1743     Boolean 	  isSystem; 	/* TRUE if makefile is a system makefile */
   1744     int		  silent = (*line != 'i') ? 1 : 0;
   1745     char	  *file = &line[7 + silent];
   1746 
   1747     /*
   1748      * Skip to delimiter character so we know where to look
   1749      */
   1750     while ((*file == ' ') || (*file == '\t')) {
   1751 	file++;
   1752     }
   1753 
   1754     if ((*file != '"') && (*file != '<')) {
   1755 	Parse_Error(PARSE_FATAL,
   1756 	    ".include filename must be delimited by '\"' or '<'");
   1757 	return;
   1758     }
   1759 
   1760     /*
   1761      * Set the search path on which to find the include file based on the
   1762      * characters which bracket its name. Angle-brackets imply it's
   1763      * a system Makefile while double-quotes imply it's a user makefile
   1764      */
   1765     if (*file == '<') {
   1766 	isSystem = TRUE;
   1767 	endc = '>';
   1768     } else {
   1769 	isSystem = FALSE;
   1770 	endc = '"';
   1771     }
   1772 
   1773     /*
   1774      * Skip to matching delimiter
   1775      */
   1776     for (cp = ++file; *cp && *cp != endc; cp++) {
   1777 	continue;
   1778     }
   1779 
   1780     if (*cp != endc) {
   1781 	Parse_Error(PARSE_FATAL,
   1782 		     "Unclosed %cinclude filename. '%c' expected",
   1783 		     '.', endc);
   1784 	return;
   1785     }
   1786     *cp = '\0';
   1787 
   1788     /*
   1789      * Substitute for any variables in the file name before trying to
   1790      * find the thing.
   1791      */
   1792     file = Var_Subst(NULL, file, VAR_CMD, FALSE);
   1793 
   1794     /*
   1795      * Now we know the file's name and its search path, we attempt to
   1796      * find the durn thing. A return of NULL indicates the file don't
   1797      * exist.
   1798      */
   1799     fullname = NULL;
   1800 
   1801     if (!isSystem) {
   1802 	/*
   1803 	 * Include files contained in double-quotes are first searched for
   1804 	 * relative to the including file's location. We don't want to
   1805 	 * cd there, of course, so we just tack on the old file's
   1806 	 * leading path components and call Dir_FindFile to see if
   1807 	 * we can locate the beast.
   1808 	 */
   1809 	char	  *prefEnd, *Fname;
   1810 
   1811 	/* Make a temporary copy of this, to be safe. */
   1812 	Fname = estrdup(curFile.fname);
   1813 
   1814 	prefEnd = strrchr(Fname, '/');
   1815 	if (prefEnd != NULL) {
   1816 	    char  	*newName;
   1817 
   1818 	    *prefEnd = '\0';
   1819 	    if (file[0] == '/')
   1820 		newName = estrdup(file);
   1821 	    else
   1822 		newName = str_concat(Fname, file, STR_ADDSLASH);
   1823 	    fullname = Dir_FindFile(newName, parseIncPath);
   1824 	    if (fullname == NULL) {
   1825 		fullname = Dir_FindFile(newName, dirSearchPath);
   1826 	    }
   1827 	    free(newName);
   1828 	    *prefEnd = '/';
   1829 	} else {
   1830 	    fullname = NULL;
   1831 	}
   1832 	free(Fname);
   1833         if (fullname == NULL) {
   1834 	    /*
   1835     	     * Makefile wasn't found in same directory as included makefile.
   1836 	     * Search for it first on the -I search path,
   1837 	     * then on the .PATH search path, if not found in a -I directory.
   1838 	     * XXX: Suffix specific?
   1839 	     */
   1840 	    fullname = Dir_FindFile(file, parseIncPath);
   1841 	    if (fullname == NULL) {
   1842 	        fullname = Dir_FindFile(file, dirSearchPath);
   1843 	    }
   1844         }
   1845     }
   1846 
   1847     /* Looking for a system file or file still not found */
   1848     if (fullname == NULL) {
   1849 	/*
   1850 	 * Look for it on the system path
   1851 	 */
   1852 	fullname = Dir_FindFile(file, Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
   1853     }
   1854 
   1855     if (fullname == NULL) {
   1856 	*cp = endc;
   1857 	if (!silent)
   1858 	    Parse_Error(PARSE_FATAL, "Could not find %s", file);
   1859 	return;
   1860     }
   1861 
   1862     free(file);
   1863 
   1864     /*
   1865      * Once we find the absolute path to the file, we get to save all the
   1866      * state from the current file before we can start reading this
   1867      * include file. The state is stored in an IFile structure which
   1868      * is placed on a list with other IFile structures. The list makes
   1869      * a very nice stack to track how we got here...
   1870      */
   1871     oldFile = emalloc(sizeof(IFile));
   1872 
   1873     memcpy(oldFile, &curFile, sizeof(IFile));
   1874 
   1875     (void)Lst_AtFront(includes, oldFile);
   1876 
   1877     /*
   1878      * Once the previous state has been saved, we can get down to reading
   1879      * the new file. We set up the name of the file to be the absolute
   1880      * name of the include file so error messages refer to the right
   1881      * place. Naturally enough, we start reading at line number 0.
   1882      */
   1883     curFile.fname = fullname;
   1884     curFile.lineno = 0;
   1885 
   1886     ParseSetParseFile(curFile.fname);
   1887 
   1888     curFile.F = fopen(fullname, "r");
   1889     curFile.P = NULL;
   1890 
   1891     if (curFile.F == (FILE * ) NULL) {
   1892 	if (!silent)
   1893 	    Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
   1894 	/*
   1895 	 * Pop to previous file
   1896 	 */
   1897 	(void)ParseEOF(0);
   1898     }
   1899 }
   1900 
   1901 
   1902 /*-
   1903  *---------------------------------------------------------------------
   1904  * ParseSetParseFile  --
   1905  *	Set the .PARSEDIR and .PARSEFILE variables to the dirname and
   1906  *	basename of the given filename
   1907  *
   1908  * Results:
   1909  *	None
   1910  *
   1911  * Side Effects:
   1912  *	The .PARSEDIR and .PARSEFILE variables are overwritten by the
   1913  *	dirname and basename of the given filename.
   1914  *---------------------------------------------------------------------
   1915  */
   1916 static void
   1917 ParseSetParseFile(const char *filename)
   1918 {
   1919     char *slash;
   1920     char *dirname;
   1921     int len;
   1922 
   1923     slash = strrchr(filename, '/');
   1924     if (slash == NULL) {
   1925 	Var_Set(".PARSEDIR", ".", VAR_GLOBAL, 0);
   1926 	Var_Set(".PARSEFILE", filename, VAR_GLOBAL, 0);
   1927     } else {
   1928 	len = slash - filename;
   1929 	dirname = emalloc(len + 1);
   1930 	memcpy(dirname, filename, len);
   1931 	dirname[len] = 0;
   1932 	Var_Set(".PARSEDIR", dirname, VAR_GLOBAL, 0);
   1933 	Var_Set(".PARSEFILE", slash+1, VAR_GLOBAL, 0);
   1934 	free(dirname);
   1935     }
   1936 }
   1937 
   1938 
   1939 /*-
   1940  *---------------------------------------------------------------------
   1941  * Parse_FromString  --
   1942  *	Start Parsing from the given string
   1943  *
   1944  * Results:
   1945  *	None
   1946  *
   1947  * Side Effects:
   1948  *	A structure is added to the includes Lst and readProc, lineno,
   1949  *	fname and curFILE are altered for the new file
   1950  *---------------------------------------------------------------------
   1951  */
   1952 void
   1953 Parse_FromString(char *str, int lineno)
   1954 {
   1955     IFile         *oldFile;	/* state associated with this file */
   1956 
   1957     if (DEBUG(FOR))
   1958 	(void)fprintf(debug_file, "%s\n---- at line %d\n", str, lineno);
   1959 
   1960     oldFile = emalloc(sizeof(IFile));
   1961     memcpy(oldFile, &curFile, sizeof(IFile));
   1962 
   1963     (void)Lst_AtFront(includes, oldFile);
   1964 
   1965     curFile.F = NULL;
   1966     curFile.P = emalloc(sizeof(PTR));
   1967     curFile.P->str = curFile.P->ptr = str;
   1968     curFile.lineno = lineno;
   1969     curFile.fname = curFile.fname;
   1970 }
   1971 
   1972 
   1973 #ifdef SYSVINCLUDE
   1974 /*-
   1975  *---------------------------------------------------------------------
   1976  * ParseTraditionalInclude  --
   1977  *	Push to another file.
   1978  *
   1979  *	The input is the current line. The file name(s) are
   1980  *	following the "include".
   1981  *
   1982  * Results:
   1983  *	None
   1984  *
   1985  * Side Effects:
   1986  *	A structure is added to the includes Lst and readProc, lineno,
   1987  *	fname and curFILE are altered for the new file
   1988  *---------------------------------------------------------------------
   1989  */
   1990 static void
   1991 ParseTraditionalInclude(char *line)
   1992 {
   1993     char          *fullname;	/* full pathname of file */
   1994     IFile         *oldFile;	/* state associated with current file */
   1995     char          *cp;		/* current position in file spec */
   1996     char	  *prefEnd;
   1997     int		   done = 0;
   1998     int		   silent = (line[0] != 'i') ? 1 : 0;
   1999     char	  *file = &line[silent + 7];
   2000     const char	  *cfname;
   2001     size_t	   clineno;
   2002 
   2003     cfname  = curFile.fname;
   2004     clineno = curFile.lineno;
   2005 
   2006     if (DEBUG(PARSE)) {
   2007 	    fprintf(debug_file, "ParseTraditionalInclude: %s\n", file);
   2008     }
   2009 
   2010     /*
   2011      * Skip over whitespace
   2012      */
   2013     while (isspace((unsigned char)*file))
   2014 	file++;
   2015 
   2016     /*
   2017      * Substitute for any variables in the file name before trying to
   2018      * find the thing.
   2019      */
   2020     file = Var_Subst(NULL, file, VAR_CMD, FALSE);
   2021 
   2022     if (*file == '\0') {
   2023 	Parse_Error(PARSE_FATAL,
   2024 		     "Filename missing from \"include\"");
   2025 	return;
   2026     }
   2027 
   2028     for (; !done; file = cp + 1) {
   2029 	/*
   2030 	 * Skip to end of line or next whitespace
   2031 	 */
   2032 	for (cp = file; *cp && !isspace((unsigned char) *cp); cp++)
   2033 	    continue;
   2034 
   2035 	if (*cp)
   2036 	    *cp = '\0';
   2037 	else
   2038 	    done = 1;
   2039 
   2040 	/*
   2041 	 * Now we know the file's name, we attempt to find the durn thing.
   2042 	 * A return of NULL indicates the file don't exist.
   2043 	 *
   2044 	 * Include files are first searched for relative to the including
   2045 	 * file's location. We don't want to cd there, of course, so we
   2046 	 * just tack on the old file's leading path components and call
   2047 	 * Dir_FindFile to see if we can locate the beast.
   2048 	 * XXX - this *does* search in the current directory, right?
   2049 	 */
   2050 
   2051 	prefEnd = strrchr(cfname, '/');
   2052 	if (prefEnd != NULL) {
   2053 	    char  	*newName;
   2054 
   2055 	    *prefEnd = '\0';
   2056 	    newName = str_concat(cfname, file, STR_ADDSLASH);
   2057 	    fullname = Dir_FindFile(newName, parseIncPath);
   2058 	    if (fullname == NULL) {
   2059 		fullname = Dir_FindFile(newName, dirSearchPath);
   2060 	    }
   2061 	    free(newName);
   2062 	    *prefEnd = '/';
   2063 	} else {
   2064 	    fullname = NULL;
   2065 	}
   2066 
   2067 	if (fullname == NULL) {
   2068 	    /*
   2069 	     * System makefile or makefile wasn't found in same directory as
   2070 	     * included makefile. Search for it first on the -I search path,
   2071 	     * then on the .PATH search path, if not found in a
   2072 	     * -I directory. XXX: Suffix specific?
   2073 	     */
   2074 	    fullname = Dir_FindFile(file, parseIncPath);
   2075 	    if (fullname == NULL) {
   2076 		fullname = Dir_FindFile(file, dirSearchPath);
   2077 	    }
   2078 	}
   2079 
   2080 	if (fullname == NULL) {
   2081 	    /*
   2082 	     * Still haven't found the makefile. Look for it on the system
   2083 	     * path as a last resort.
   2084 	     */
   2085 	    fullname = Dir_FindFile(file,
   2086 			Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
   2087 	}
   2088 
   2089 	if (fullname == NULL) {
   2090 	    if (!silent)
   2091 		ParseErrorInternal(cfname, clineno, PARSE_FATAL,
   2092 		    "Could not find %s", file);
   2093 	    free(file);
   2094 	    continue;
   2095 	}
   2096 
   2097 	free(file);
   2098 
   2099 	/*
   2100 	 * Once we find the absolute path to the file, we get to save all
   2101 	 * the state from the current file before we can start reading this
   2102 	 * include file. The state is stored in an IFile structure which
   2103 	 * is placed on a list with other IFile structures. The list makes
   2104 	 * a very nice stack to track how we got here...
   2105 	 */
   2106 	oldFile = emalloc(sizeof(IFile));
   2107 	memcpy(oldFile, &curFile, sizeof(IFile));
   2108 
   2109 	(void)Lst_AtFront(includes, oldFile);
   2110 
   2111 	/*
   2112 	 * Once the previous state has been saved, we can get down to
   2113 	 * reading the new file. We set up the name of the file to be the
   2114 	 * absolute name of the include file so error messages refer to the
   2115 	 * right place. Naturally enough, we start reading at line number 0.
   2116 	 */
   2117 	curFile.fname = fullname;
   2118 	curFile.lineno = 0;
   2119 
   2120 	curFile.F = fopen(fullname, "r");
   2121 	curFile.P = NULL;
   2122 
   2123 	if (curFile.F == NULL) {
   2124 	    if (!silent)
   2125 		ParseErrorInternal(cfname, clineno, PARSE_FATAL,
   2126 		    "Cannot open %s", fullname);
   2127 	    /*
   2128 	     * Pop to previous file
   2129 	     */
   2130 	    (void)ParseEOF(1);
   2131 	}
   2132     }
   2133 }
   2134 #endif
   2135 
   2136 /*-
   2137  *---------------------------------------------------------------------
   2138  * ParseEOF  --
   2139  *	Called when EOF is reached in the current file. If we were reading
   2140  *	an include file, the includes stack is popped and things set up
   2141  *	to go back to reading the previous file at the previous location.
   2142  *
   2143  * Results:
   2144  *	CONTINUE if there's more to do. DONE if not.
   2145  *
   2146  * Side Effects:
   2147  *	The old curFILE, is closed. The includes list is shortened.
   2148  *	lineno, curFILE, and fname are changed if CONTINUE is returned.
   2149  *---------------------------------------------------------------------
   2150  */
   2151 static int
   2152 ParseEOF(int opened)
   2153 {
   2154     IFile     *ifile;	/* the state on the top of the includes stack */
   2155 
   2156     if (Lst_IsEmpty(includes)) {
   2157 	Var_Delete(".PARSEDIR", VAR_GLOBAL);
   2158 	Var_Delete(".PARSEFILE", VAR_GLOBAL);
   2159 	return (DONE);
   2160     }
   2161 
   2162     ifile = (IFile *)Lst_DeQueue(includes);
   2163 
   2164     /* XXX dispose of curFile info */
   2165     /* Leak curFile.fname because all the gnodes have pointers to it */
   2166     if (opened && curFile.F)
   2167 	(void)fclose(curFile.F);
   2168     if (curFile.P) {
   2169 	free(curFile.P->str);
   2170 	free(curFile.P);
   2171     }
   2172 
   2173     memcpy(&curFile, ifile, sizeof(IFile));
   2174 
   2175     free(ifile);
   2176 
   2177     /* pop the PARSEDIR/PARSEFILE variables */
   2178     ParseSetParseFile(curFile.fname);
   2179     return (CONTINUE);
   2180 }
   2181 
   2182 /*-
   2183  *---------------------------------------------------------------------
   2184  * ParseReadc  --
   2185  *	Read a character from the current file
   2186  *
   2187  * Results:
   2188  *	The character that was read
   2189  *
   2190  * Side Effects:
   2191  *---------------------------------------------------------------------
   2192  */
   2193 static inline int
   2194 ParseReadc(void)
   2195 {
   2196     if (curFile.F)
   2197 	return fgetc(curFile.F);
   2198 
   2199     if (curFile.P && *curFile.P->ptr)
   2200 	return *curFile.P->ptr++;
   2201     return EOF;
   2202 }
   2203 
   2204 
   2205 /*-
   2206  *---------------------------------------------------------------------
   2207  * ParseUnreadc  --
   2208  *	Put back a character to the current file
   2209  *
   2210  * Results:
   2211  *	None.
   2212  *
   2213  * Side Effects:
   2214  *---------------------------------------------------------------------
   2215  */
   2216 static void
   2217 ParseUnreadc(int c)
   2218 {
   2219     if (curFile.F) {
   2220 	ungetc(c, curFile.F);
   2221 	return;
   2222     }
   2223     if (curFile.P) {
   2224 	*--(curFile.P->ptr) = c;
   2225 	return;
   2226     }
   2227 }
   2228 
   2229 
   2230 /* ParseSkipLine():
   2231  *	Grab the next line
   2232  *
   2233  * Input:
   2234  *	skip		Skip lines that don't start with .
   2235  *	keep_newline	Keep newline character as is.
   2236  *
   2237  */
   2238 static char *
   2239 ParseSkipLine(int skip, int keep_newline)
   2240 {
   2241     char *line;
   2242     int c, lastc, lineLength = 0;
   2243     Buffer buf;
   2244 
   2245     buf = Buf_Init(MAKE_BSIZE);
   2246 
   2247     do {
   2248         Buf_Discard(buf, lineLength);
   2249         lastc = '\0';
   2250 
   2251         while (((c = ParseReadc()) != '\n' || lastc == '\\')
   2252                && c != EOF) {
   2253             if (c == '\n') {
   2254                 if (keep_newline)
   2255                     Buf_AddByte(buf, (Byte)c);
   2256                 else
   2257                     Buf_ReplaceLastByte(buf, (Byte)' ');
   2258                 curFile.lineno++;
   2259 
   2260                 while ((c = ParseReadc()) == ' ' || c == '\t');
   2261 
   2262                 if (c == EOF)
   2263                     break;
   2264             }
   2265 
   2266             Buf_AddByte(buf, (Byte)c);
   2267             lastc = c;
   2268         }
   2269 
   2270         if (c == EOF) {
   2271             Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
   2272             Buf_Destroy(buf, TRUE);
   2273             return(NULL);
   2274         }
   2275 
   2276         curFile.lineno++;
   2277         Buf_AddByte(buf, (Byte)'\0');
   2278         line = (char *)Buf_GetAll(buf, &lineLength);
   2279     } while (skip == 1 && line[0] != '.');
   2280 
   2281     Buf_Destroy(buf, FALSE);
   2282     return line;
   2283 }
   2284 
   2285 
   2286 /*-
   2287  *---------------------------------------------------------------------
   2288  * ParseReadLine --
   2289  *	Read an entire line from the input file. Called only by Parse_File.
   2290  *	To facilitate escaped newlines and what have you, a character is
   2291  *	buffered in 'lastc', which is '\0' when no characters have been
   2292  *	read. When we break out of the loop, c holds the terminating
   2293  *	character and lastc holds a character that should be added to
   2294  *	the line (unless we don't read anything but a terminator).
   2295  *
   2296  * Results:
   2297  *	A line w/o its newline
   2298  *
   2299  * Side Effects:
   2300  *	Only those associated with reading a character
   2301  *---------------------------------------------------------------------
   2302  */
   2303 static char *
   2304 ParseReadLine(void)
   2305 {
   2306     Buffer  	  buf;	    	/* Buffer for current line */
   2307     int		  c;	      	/* the current character */
   2308     int		  lastc;    	/* The most-recent character */
   2309     Boolean	  semiNL;     	/* treat semi-colons as newlines */
   2310     Boolean	  ignDepOp;   	/* TRUE if should ignore dependency operators
   2311 				 * for the purposes of setting semiNL */
   2312     Boolean 	  ignComment;	/* TRUE if should ignore comments (in a
   2313 				 * shell command */
   2314     char 	  *line;    	/* Result */
   2315     char          *ep;		/* to strip trailing blanks */
   2316     int	    	  lineLength;	/* Length of result */
   2317     int	    	  lineno;	/* Saved line # */
   2318 
   2319     semiNL = FALSE;
   2320     ignDepOp = FALSE;
   2321     ignComment = FALSE;
   2322 
   2323     /*
   2324      * Handle special-characters at the beginning of the line.
   2325      * A leading tab (shell command) forces us to ignore comments and
   2326      * dependency operators and treat semi-colons as semi-colons
   2327      * (by leaving semiNL FALSE).
   2328      * This also discards completely blank lines.
   2329      */
   2330     for (;;) {
   2331 	c = ParseReadc();
   2332 	if (c == '\n') {
   2333 	    curFile.lineno++;
   2334 	    continue;
   2335 	}
   2336 
   2337 	if (c == '\t')
   2338 	    ignComment = ignDepOp = TRUE;
   2339 	else if (c == '#')
   2340 	    ParseUnreadc(c);
   2341 	break;
   2342     }
   2343 
   2344     if (c == EOF)
   2345 	/*
   2346 	 * Hit end-of-file, so return a NULL line to indicate this.
   2347 	 */
   2348 	return(NULL);
   2349 
   2350     lastc = c;
   2351     buf = Buf_Init(MAKE_BSIZE);
   2352 
   2353     while (((c = ParseReadc()) != '\n' || (lastc == '\\')) && (c != EOF)) {
   2354 test_char:
   2355 	switch (c) {
   2356 	case '\n':
   2357 	    /*
   2358 	     * Escaped newline: read characters until a non-space or an
   2359 	     * unescaped newline and replace them all by a single space.
   2360 	     * This is done by storing the space over the backslash and
   2361 	     * dropping through with the next nonspace. If it is a
   2362 	     * semi-colon and semiNL is TRUE, it will be recognized as a
   2363 	     * newline in the code below this...
   2364 	     */
   2365 	    curFile.lineno++;
   2366 	    lastc = ' ';
   2367 	    while ((c = ParseReadc()) == ' ' || c == '\t')
   2368 		continue;
   2369 	    if (c == EOF || c == '\n')
   2370 		goto line_read;
   2371 	    /*
   2372 	     * Check for comments, semiNL's, etc. -- easier than
   2373 	     * ParseUnreadc(c); continue;
   2374 	     */
   2375 	    goto test_char;
   2376 
   2377 	case ';':
   2378 	    /*
   2379 	     * Semi-colon: Need to see if it should be interpreted as a
   2380 	     * newline
   2381 	     */
   2382 	    if (!semiNL)
   2383 		break;
   2384 	    /*
   2385 	     * To make sure the command that may be following this
   2386 	     * semi-colon begins with a tab, we push one back into the
   2387 	     * input stream. This will overwrite the semi-colon in the
   2388 	     * buffer. If there is no command following, this does no
   2389 	     * harm, since the newline remains in the buffer and the
   2390 	     * whole line is ignored.
   2391 	     */
   2392 	    ParseUnreadc('\t');
   2393 	    goto line_read;
   2394 
   2395 	case '=':
   2396 	    if (!semiNL) {
   2397 		/*
   2398 		 * Haven't seen a dependency operator before this, so this
   2399 		 * must be a variable assignment -- don't pay attention to
   2400 		 * dependency operators after this.
   2401 		 */
   2402 		ignDepOp = TRUE;
   2403 	    } else if (lastc == ':' || lastc == '!') {
   2404 		/*
   2405 		 * Well, we've seen a dependency operator already, but it
   2406 		 * was the previous character, so this is really just an
   2407 		 * expanded variable assignment. Revert semi-colons to
   2408 		 * being just semi-colons again and ignore any more
   2409 		 * dependency operators.
   2410 		 *
   2411 		 * XXX: Note that a line like "foo : a:=b" will blow up,
   2412 		 * but who'd write a line like that anyway?
   2413 		 */
   2414 		ignDepOp = TRUE;
   2415 		semiNL = FALSE;
   2416 	    }
   2417 	    break;
   2418 
   2419 	case '#':
   2420 	    if (ignComment)
   2421 		break;
   2422 	    if (
   2423 #if 0
   2424 		    !compatMake ||
   2425 #endif
   2426 		    (lastc == '\\')) {
   2427 		/* Don't add the backslash. Just let the # get copied over. */
   2428 		lastc = c;
   2429 		continue;
   2430 	    }
   2431 	    /*
   2432 	     * If the character is a hash mark and it isn't escaped
   2433 	     * (or we're being compatible), the thing is a comment.
   2434 	     * Skip to the end of the line.
   2435 	     */
   2436 	    do {
   2437 		c = ParseReadc();
   2438 		/*
   2439 		 * If we found a backslash not escaped itself it means
   2440 		 * that the comment is going to continue in the next line.
   2441 		 */
   2442 		if (c == '\\')
   2443 		    /* Discard the (escaped) character after the '\\' */
   2444 		    ParseReadc();
   2445 	    } while ((c != '\n') && (c != EOF));
   2446 	    goto line_read;
   2447 
   2448 	case ':':
   2449 	case '!':
   2450 	    if (!ignDepOp) {
   2451 		/*
   2452 		 * A semi-colon is recognized as a newline only on
   2453 		 * dependency lines. Dependency lines are lines with a
   2454 		 * colon or an exclamation point. Ergo...
   2455 		 */
   2456 		semiNL = TRUE;
   2457 	    }
   2458 	    break;
   2459 	}
   2460 
   2461 	/* Copy in the previous character and save this one in lastc. */
   2462 	Buf_AddByte(buf, (Byte)lastc);
   2463 	lastc = c;
   2464     }
   2465 
   2466 line_read:
   2467     curFile.lineno++;
   2468 
   2469     if (lastc != '\0') {
   2470 	Buf_AddByte(buf, (Byte)lastc);
   2471     }
   2472     Buf_AddByte(buf, (Byte)'\0');
   2473     line = (char *)Buf_GetAll(buf, &lineLength);
   2474     Buf_Destroy(buf, FALSE);
   2475 
   2476     /*
   2477      * Strip trailing blanks and tabs from the line.
   2478      * Do not strip a blank or tab that is preceded by a '\'
   2479      */
   2480     ep = line + lineLength - 1;
   2481     while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
   2482 	if (ep > line + 1 && ep[-2] == '\\')
   2483 	    break;
   2484 	--ep;
   2485     }
   2486     *ep = 0;
   2487 
   2488     if (line[0] != '.')
   2489 	return line;
   2490 
   2491     /*
   2492      * The line might be a conditional. Ask the conditional module
   2493      * about it and act accordingly
   2494      */
   2495     switch (Cond_Eval(line)) {
   2496     case COND_SKIP:
   2497 	/*
   2498 	 * Skip to next conditional that evaluates to COND_PARSE.
   2499 	 */
   2500 	do {
   2501 	    free(line);
   2502 	    line = ParseSkipLine(1, 0);
   2503 	} while (line && Cond_Eval(line) != COND_PARSE);
   2504 	if (line == NULL)
   2505 	    break;
   2506 	/*FALLTHRU*/
   2507     case COND_PARSE:
   2508 	free(line);
   2509 	line = ParseReadLine();
   2510 	break;
   2511     case COND_INVALID:
   2512 	lineno = curFile.lineno;
   2513 	if (For_Eval(line)) {
   2514 	    int ok;
   2515 	    free(line);
   2516 	    do {
   2517 		/* Skip after the matching end */
   2518 		line = ParseSkipLine(0, 1);
   2519 		if (line == NULL) {
   2520 		    Parse_Error(PARSE_FATAL,
   2521 			     "Unexpected end of file in for loop.\n");
   2522 		    break;
   2523 		}
   2524 		ok = For_Eval(line);
   2525 		free(line);
   2526 	    }
   2527 	    while (ok);
   2528 	    if (line != NULL)
   2529 		For_Run(lineno);
   2530 	    line = ParseReadLine();
   2531 	}
   2532 	break;
   2533     }
   2534     return (line);
   2535 }
   2536 
   2537 /*-
   2538  *-----------------------------------------------------------------------
   2539  * ParseFinishLine --
   2540  *	Handle the end of a dependency group.
   2541  *
   2542  * Results:
   2543  *	Nothing.
   2544  *
   2545  * Side Effects:
   2546  *	inLine set FALSE. 'targets' list destroyed.
   2547  *
   2548  *-----------------------------------------------------------------------
   2549  */
   2550 static void
   2551 ParseFinishLine(void)
   2552 {
   2553     if (inLine) {
   2554 	Lst_ForEach(targets, Suff_EndTransform, NULL);
   2555 	Lst_Destroy(targets, ParseHasCommands);
   2556 	targets = NULL;
   2557 	inLine = FALSE;
   2558     }
   2559 }
   2560 
   2561 
   2562 /*-
   2563  *---------------------------------------------------------------------
   2564  * Parse_File --
   2565  *	Parse a file into its component parts, incorporating it into the
   2566  *	current dependency graph. This is the main function and controls
   2567  *	almost every other function in this module
   2568  *
   2569  * Input:
   2570  *	name		the name of the file being read
   2571  *	stream		Stream open to makefile to parse
   2572  *
   2573  * Results:
   2574  *	None
   2575  *
   2576  * Side Effects:
   2577  *	Loads. Nodes are added to the list of all targets, nodes and links
   2578  *	are added to the dependency graph. etc. etc. etc.
   2579  *---------------------------------------------------------------------
   2580  */
   2581 void
   2582 Parse_File(const char *name, FILE *stream)
   2583 {
   2584     char	  *cp;		/* pointer into the line */
   2585     char          *line;	/* the line we're working on */
   2586 #ifndef POSIX
   2587     Boolean	nonSpace;
   2588 #endif
   2589 
   2590     inLine = FALSE;
   2591     fatals = 0;
   2592 
   2593     curFile.fname = name;
   2594     curFile.F = stream;
   2595     curFile.lineno = 0;
   2596 
   2597     ParseSetParseFile(curFile.fname);
   2598 
   2599     do {
   2600 	for (; (line = ParseReadLine()) != NULL; free(line)) {
   2601 	    if (DEBUG(PARSE))
   2602 		fprintf(debug_file, "ParseReadLine: %s\n", line);
   2603 	    if (*line == '.') {
   2604 		/*
   2605 		 * Lines that begin with the special character are either
   2606 		 * include or undef directives.
   2607 		 */
   2608 		for (cp = line + 1; isspace ((unsigned char)*cp); cp++) {
   2609 		    continue;
   2610 		}
   2611 		if (strncmp(cp, "include", 7) == 0 ||
   2612 	    	    ((cp[0] == 's' || cp[0] == '-') &&
   2613 		    strncmp(&cp[1], "include", 7) == 0)) {
   2614 		    ParseDoInclude(cp);
   2615 		    continue;
   2616 		}
   2617 		if (strncmp(cp, "undef", 5) == 0) {
   2618 		    char *cp2;
   2619 		    for (cp += 5; isspace((unsigned char) *cp); cp++) {
   2620 			continue;
   2621 		    }
   2622 
   2623 		    for (cp2 = cp; !isspace((unsigned char) *cp2) &&
   2624 				   (*cp2 != '\0'); cp2++) {
   2625 			continue;
   2626 		    }
   2627 
   2628 		    *cp2 = '\0';
   2629 
   2630 		    Var_Delete(cp, VAR_GLOBAL);
   2631 		    continue;
   2632 		}
   2633 	    }
   2634 	    if (*line == '#') {
   2635 		/* If we're this far, the line must be a comment. */
   2636 		continue;
   2637 	    }
   2638 
   2639 	    if (*line == '\t') {
   2640 		/*
   2641 		 * If a line starts with a tab, it can only hope to be
   2642 		 * a creation command.
   2643 		 */
   2644 #ifndef POSIX
   2645 	    shellCommand:
   2646 #endif
   2647 		for (cp = line + 1; isspace ((unsigned char)*cp); cp++) {
   2648 		    continue;
   2649 		}
   2650 		if (*cp) {
   2651 		    if (!inLine)
   2652 			Parse_Error(PARSE_FATAL,
   2653 				     "Unassociated shell command \"%s\"",
   2654 				     cp);
   2655 		    /*
   2656 		     * So long as it's not a blank line and we're actually
   2657 		     * in a dependency spec, add the command to the list of
   2658 		     * commands of all targets in the dependency spec
   2659 		     */
   2660 		    Lst_ForEach(targets, ParseAddCmd, cp);
   2661 #ifdef CLEANUP
   2662 		    Lst_AtEnd(targCmds, line);
   2663 #endif
   2664 		    line = 0;
   2665 		}
   2666 		continue;
   2667 	    }
   2668 
   2669 #ifdef SYSVINCLUDE
   2670 	    if (((strncmp(line, "include", 7) == 0 &&
   2671 		    isspace((unsigned char) line[7])) ||
   2672 			((line[0] == 's' || line[0] == '-') &&
   2673 			    strncmp(&line[1], "include", 7) == 0 &&
   2674 			    isspace((unsigned char) line[8]))) &&
   2675 		    strchr(line, ':') == NULL) {
   2676 		/*
   2677 		 * It's an S3/S5-style "include".
   2678 		 */
   2679 		ParseTraditionalInclude(line);
   2680 		continue;
   2681 	    }
   2682 #endif
   2683 	    if (Parse_IsVar(line)) {
   2684 		ParseFinishLine();
   2685 		Parse_DoVar(line, VAR_GLOBAL);
   2686 		continue;
   2687 	    }
   2688 
   2689 	    /*
   2690 	     * We now know it's a dependency line so it needs to have all
   2691 	     * variables expanded before being parsed. Tell the variable
   2692 	     * module to complain if some variable is undefined...
   2693 	     * To make life easier on novices, if the line is indented we
   2694 	     * first make sure the line has a dependency operator in it.
   2695 	     * If it doesn't have an operator and we're in a dependency
   2696 	     * line's script, we assume it's actually a shell command
   2697 	     * and add it to the current list of targets.
   2698 	     */
   2699 #ifndef POSIX
   2700 	    nonSpace = FALSE;
   2701 #endif
   2702 
   2703 	    cp = line;
   2704 	    if (isspace((unsigned char) line[0])) {
   2705 		while ((*cp != '\0') && isspace((unsigned char) *cp)) {
   2706 		    cp++;
   2707 		}
   2708 		if (*cp == '\0') {
   2709 		    /* Ignore blank line in commands */
   2710 		    continue;
   2711 		}
   2712 #ifndef POSIX
   2713 		while (*cp && (ParseIsEscaped(line, cp) ||
   2714 			(*cp != ':') && (*cp != '!'))) {
   2715 		    nonSpace = TRUE;
   2716 		    cp++;
   2717 		}
   2718 #endif
   2719 	    }
   2720 
   2721 #ifndef POSIX
   2722 	    if (*cp == '\0') {
   2723 		if (inLine) {
   2724 		    Parse_Error(PARSE_WARNING,
   2725 				 "Shell command needs a leading tab");
   2726 		    goto shellCommand;
   2727 		} else if (nonSpace) {
   2728 		    Parse_Error(PARSE_FATAL, "Missing operator");
   2729 		}
   2730 		continue;
   2731 	    }
   2732 #endif
   2733 	    ParseFinishLine();
   2734 
   2735 	    cp = Var_Subst(NULL, line, VAR_CMD, TRUE);
   2736 	    free(line);
   2737 	    line = cp;
   2738 
   2739 	    /*
   2740 	     * Need a non-circular list for the target nodes
   2741 	     */
   2742 	    if (targets)
   2743 		Lst_Destroy(targets, NOFREE);
   2744 
   2745 	    targets = Lst_Init(FALSE);
   2746 	    inLine = TRUE;
   2747 
   2748 	    ParseDoDependency(line);
   2749 	}
   2750 	/*
   2751 	 * Reached EOF, but it may be just EOF of an include file...
   2752 	 */
   2753     } while (ParseEOF(1) == CONTINUE);
   2754 
   2755     /*
   2756      * Make sure conditionals are clean
   2757      */
   2758     Cond_End();
   2759 
   2760     if (fatals) {
   2761 	(void)fprintf(stderr,
   2762 	    "%s: Fatal errors encountered -- cannot continue\n",
   2763 	    progname);
   2764 	PrintOnError(NULL);
   2765 	exit(1);
   2766     }
   2767 }
   2768 
   2769 /*-
   2770  *---------------------------------------------------------------------
   2771  * Parse_Init --
   2772  *	initialize the parsing module
   2773  *
   2774  * Results:
   2775  *	none
   2776  *
   2777  * Side Effects:
   2778  *	the parseIncPath list is initialized...
   2779  *---------------------------------------------------------------------
   2780  */
   2781 void
   2782 Parse_Init(void)
   2783 {
   2784     mainNode = NILGNODE;
   2785     parseIncPath = Lst_Init(FALSE);
   2786     sysIncPath = Lst_Init(FALSE);
   2787     defIncPath = Lst_Init(FALSE);
   2788     includes = Lst_Init(FALSE);
   2789 #ifdef CLEANUP
   2790     targCmds = Lst_Init(FALSE);
   2791 #endif
   2792 }
   2793 
   2794 void
   2795 Parse_End(void)
   2796 {
   2797 #ifdef CLEANUP
   2798     Lst_Destroy(targCmds, (FreeProc *)free);
   2799     if (targets)
   2800 	Lst_Destroy(targets, NOFREE);
   2801     Lst_Destroy(defIncPath, Dir_Destroy);
   2802     Lst_Destroy(sysIncPath, Dir_Destroy);
   2803     Lst_Destroy(parseIncPath, Dir_Destroy);
   2804     Lst_Destroy(includes, NOFREE);	/* Should be empty now */
   2805 #endif
   2806 }
   2807 
   2808 
   2809 /*-
   2810  *-----------------------------------------------------------------------
   2811  * Parse_MainName --
   2812  *	Return a Lst of the main target to create for main()'s sake. If
   2813  *	no such target exists, we Punt with an obnoxious error message.
   2814  *
   2815  * Results:
   2816  *	A Lst of the single node to create.
   2817  *
   2818  * Side Effects:
   2819  *	None.
   2820  *
   2821  *-----------------------------------------------------------------------
   2822  */
   2823 Lst
   2824 Parse_MainName(void)
   2825 {
   2826     Lst           mainList;	/* result list */
   2827 
   2828     mainList = Lst_Init(FALSE);
   2829 
   2830     if (mainNode == NILGNODE) {
   2831 	Punt("no target to make.");
   2832     	/*NOTREACHED*/
   2833     } else if (mainNode->type & OP_DOUBLEDEP) {
   2834 	(void)Lst_AtEnd(mainList, mainNode);
   2835 	Lst_Concat(mainList, mainNode->cohorts, LST_CONCNEW);
   2836     }
   2837     else
   2838 	(void)Lst_AtEnd(mainList, mainNode);
   2839     Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL);
   2840     return (mainList);
   2841 }
   2842 
   2843 /*-
   2844  *-----------------------------------------------------------------------
   2845  * ParseMark --
   2846  *	Add the filename and lineno to the GNode so that we remember
   2847  *	where it was first defined.
   2848  *
   2849  * Side Effects:
   2850  *	None.
   2851  *
   2852  *-----------------------------------------------------------------------
   2853  */
   2854 static void
   2855 ParseMark(GNode *gn)
   2856 {
   2857     gn->fname = curFile.fname;
   2858     gn->lineno = curFile.lineno;
   2859 }
   2860