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