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