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