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