Home | History | Annotate | Line # | Download | only in make
parse.c revision 1.276
      1 /*	$NetBSD: parse.c,v 1.276 2020/09/04 17:59:36 rillig 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.276 2020/09/04 17:59:36 rillig 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.276 2020/09/04 17:59:36 rillig 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 <sys/types.h>
    127 #include <sys/mman.h>
    128 #include <sys/stat.h>
    129 #include <errno.h>
    130 #include <stdarg.h>
    131 #include <stdio.h>
    132 #include <stdint.h>
    133 
    134 #ifndef MAP_FILE
    135 #define MAP_FILE 0
    136 #endif
    137 #ifndef MAP_COPY
    138 #define MAP_COPY MAP_PRIVATE
    139 #endif
    140 
    141 #include "make.h"
    142 #include "dir.h"
    143 #include "job.h"
    144 #include "pathnames.h"
    145 
    146 /* types and constants */
    147 
    148 /*
    149  * Structure for a file being read ("included file")
    150  */
    151 typedef struct IFile {
    152     char      	    *fname;         /* name of file */
    153     int             lineno;         /* current line number in file */
    154     int             first_lineno;   /* line number of start of text */
    155     int             cond_depth;     /* 'if' nesting when file opened */
    156     Boolean         depending;      /* state of doing_depend on EOF */
    157     char            *P_str;         /* point to base of string buffer */
    158     char            *P_ptr;         /* point to next char of string buffer */
    159     char            *P_end;         /* point to the end of string buffer */
    160     char            *(*nextbuf)(void *, size_t *); /* Function to get more data */
    161     void            *nextbuf_arg;   /* Opaque arg for nextbuf() */
    162     struct loadedfile *lf;          /* loadedfile object, if any */
    163 } IFile;
    164 
    165 
    166 /*
    167  * These values are returned by ParseEOF to tell Parse_File whether to
    168  * CONTINUE parsing, i.e. it had only reached the end of an include file,
    169  * or if it's DONE.
    170  */
    171 #define CONTINUE	1
    172 #define DONE		0
    173 
    174 /*
    175  * Tokens for target attributes
    176  */
    177 typedef enum {
    178     Begin,  	    /* .BEGIN */
    179     Default,	    /* .DEFAULT */
    180     DeleteOnError,  /* .DELETE_ON_ERROR */
    181     End,    	    /* .END */
    182     dotError,	    /* .ERROR */
    183     Ignore,	    /* .IGNORE */
    184     Includes,	    /* .INCLUDES */
    185     Interrupt,	    /* .INTERRUPT */
    186     Libs,	    /* .LIBS */
    187     Meta,	    /* .META */
    188     MFlags,	    /* .MFLAGS or .MAKEFLAGS */
    189     Main,	    /* .MAIN and we don't have anything user-specified to
    190 		     * make */
    191     NoExport,	    /* .NOEXPORT */
    192     NoMeta,	    /* .NOMETA */
    193     NoMetaCmp,	    /* .NOMETA_CMP */
    194     NoPath,	    /* .NOPATH */
    195     Not,	    /* Not special */
    196     NotParallel,    /* .NOTPARALLEL */
    197     Null,   	    /* .NULL */
    198     ExObjdir,	    /* .OBJDIR */
    199     Order,  	    /* .ORDER */
    200     Parallel,	    /* .PARALLEL */
    201     ExPath,	    /* .PATH */
    202     Phony,	    /* .PHONY */
    203 #ifdef POSIX
    204     Posix,	    /* .POSIX */
    205 #endif
    206     Precious,	    /* .PRECIOUS */
    207     ExShell,	    /* .SHELL */
    208     Silent,	    /* .SILENT */
    209     SingleShell,    /* .SINGLESHELL */
    210     Stale,	    /* .STALE */
    211     Suffixes,	    /* .SUFFIXES */
    212     Wait,	    /* .WAIT */
    213     Attribute	    /* Generic attribute */
    214 } ParseSpecial;
    215 
    216 /*
    217  * Other tokens
    218  */
    219 #define LPAREN	'('
    220 #define RPAREN	')'
    221 
    222 
    223 /* result data */
    224 
    225 /*
    226  * The main target to create. This is the first target on the first
    227  * dependency line in the first makefile.
    228  */
    229 static GNode *mainNode;
    230 
    231 /* eval state */
    232 
    233 /* targets we're working on */
    234 static Lst targets;
    235 
    236 #ifdef CLEANUP
    237 /* command lines for targets */
    238 static Lst targCmds;
    239 #endif
    240 
    241 /*
    242  * specType contains the SPECial TYPE of the current target. It is
    243  * Not if the target is unspecial. If it *is* special, however, the children
    244  * are linked as children of the parent but not vice versa. This variable is
    245  * set in ParseDoDependency
    246  */
    247 static ParseSpecial specType;
    248 
    249 /*
    250  * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
    251  * seen, then set to each successive source on the line.
    252  */
    253 static GNode	*predecessor;
    254 
    255 /* parser state */
    256 
    257 /* true if currently in a dependency line or its commands */
    258 static Boolean inLine;
    259 
    260 /* number of fatal errors */
    261 static int fatals = 0;
    262 
    263 /*
    264  * Variables for doing includes
    265  */
    266 
    267 /* current file being read */
    268 static IFile *curFile;
    269 
    270 /* The current file from the command line (at the bottom of the stack) and
    271  * further up all the files that are currently being read due to nested
    272  * .include directives. */
    273 static Stack /* of *IFile */ includes;
    274 
    275 /* include paths (lists of directories) */
    276 Lst parseIncPath;	/* dirs for "..." includes */
    277 Lst sysIncPath;		/* dirs for <...> includes */
    278 Lst defIncPath;		/* default for sysIncPath */
    279 
    280 /* parser tables */
    281 
    282 /*
    283  * The parseKeywords table is searched using binary search when deciding
    284  * if a target or source is special. The 'spec' field is the ParseSpecial
    285  * type of the keyword ("Not" if the keyword isn't special as a target) while
    286  * the 'op' field is the operator to apply to the list of targets if the
    287  * keyword is used as a source ("0" if the keyword isn't special as a source)
    288  */
    289 static const struct {
    290     const char   *name;    	/* Name of keyword */
    291     ParseSpecial  spec;	    	/* Type when used as a target */
    292     int	    	  op;	    	/* Operator when used as a source */
    293 } parseKeywords[] = {
    294 { ".BEGIN", 	  Begin,    	0 },
    295 { ".DEFAULT",	  Default,  	0 },
    296 { ".DELETE_ON_ERROR", DeleteOnError, 0 },
    297 { ".END",   	  End,	    	0 },
    298 { ".ERROR",   	  dotError,    	0 },
    299 { ".EXEC",	  Attribute,   	OP_EXEC },
    300 { ".IGNORE",	  Ignore,   	OP_IGNORE },
    301 { ".INCLUDES",	  Includes, 	0 },
    302 { ".INTERRUPT",	  Interrupt,	0 },
    303 { ".INVISIBLE",	  Attribute,   	OP_INVISIBLE },
    304 { ".JOIN",  	  Attribute,   	OP_JOIN },
    305 { ".LIBS",  	  Libs,	    	0 },
    306 { ".MADE",	  Attribute,	OP_MADE },
    307 { ".MAIN",	  Main,		0 },
    308 { ".MAKE",  	  Attribute,   	OP_MAKE },
    309 { ".MAKEFLAGS",	  MFlags,   	0 },
    310 { ".META",	  Meta,		OP_META },
    311 { ".MFLAGS",	  MFlags,   	0 },
    312 { ".NOMETA",	  NoMeta,	OP_NOMETA },
    313 { ".NOMETA_CMP",  NoMetaCmp,	OP_NOMETA_CMP },
    314 { ".NOPATH",	  NoPath,	OP_NOPATH },
    315 { ".NOTMAIN",	  Attribute,   	OP_NOTMAIN },
    316 { ".NOTPARALLEL", NotParallel,	0 },
    317 { ".NO_PARALLEL", NotParallel,	0 },
    318 { ".NULL",  	  Null,	    	0 },
    319 { ".OBJDIR",	  ExObjdir,	0 },
    320 { ".OPTIONAL",	  Attribute,   	OP_OPTIONAL },
    321 { ".ORDER", 	  Order,    	0 },
    322 { ".PARALLEL",	  Parallel,	0 },
    323 { ".PATH",	  ExPath,	0 },
    324 { ".PHONY",	  Phony,	OP_PHONY },
    325 #ifdef POSIX
    326 { ".POSIX",	  Posix,	0 },
    327 #endif
    328 { ".PRECIOUS",	  Precious, 	OP_PRECIOUS },
    329 { ".RECURSIVE",	  Attribute,	OP_MAKE },
    330 { ".SHELL", 	  ExShell,    	0 },
    331 { ".SILENT",	  Silent,   	OP_SILENT },
    332 { ".SINGLESHELL", SingleShell,	0 },
    333 { ".STALE",	  Stale,	0 },
    334 { ".SUFFIXES",	  Suffixes, 	0 },
    335 { ".USE",   	  Attribute,   	OP_USE },
    336 { ".USEBEFORE",   Attribute,   	OP_USEBEFORE },
    337 { ".WAIT",	  Wait, 	0 },
    338 };
    339 
    340 /* local functions */
    341 
    342 static int ParseIsEscaped(const char *, const char *);
    343 static void ParseErrorInternal(const char *, size_t, int, const char *, ...)
    344     MAKE_ATTR_PRINTFLIKE(4,5);
    345 static void ParseVErrorInternal(FILE *, const char *, size_t, int, const char *, va_list)
    346     MAKE_ATTR_PRINTFLIKE(5, 0);
    347 static int ParseFindKeyword(const char *);
    348 static int ParseLinkSrc(void *, void *);
    349 static int ParseDoOp(void *, void *);
    350 static void ParseDoSrc(int, const char *);
    351 static int ParseFindMain(void *, void *);
    352 static int ParseAddDir(void *, void *);
    353 static int ParseClearPath(void *, void *);
    354 static void ParseDoDependency(char *);
    355 static int ParseAddCmd(void *, void *);
    356 static void ParseHasCommands(void *);
    357 static void ParseDoInclude(char *);
    358 static void ParseSetParseFile(const char *);
    359 static void ParseSetIncludedFile(void);
    360 #ifdef GMAKEEXPORT
    361 static void ParseGmakeExport(char *);
    362 #endif
    363 static int ParseEOF(void);
    364 static char *ParseReadLine(void);
    365 static void ParseFinishLine(void);
    366 static void ParseMark(GNode *);
    367 
    368 /* file loader */
    369 
    370 struct loadedfile {
    371 	const char *path;		/* name, for error reports */
    372 	char *buf;			/* contents buffer */
    373 	size_t len;			/* length of contents */
    374 	size_t maplen;			/* length of mmap area, or 0 */
    375 	Boolean used;			/* XXX: have we used the data yet */
    376 };
    377 
    378 /*
    379  * Constructor/destructor for loadedfile
    380  */
    381 static struct loadedfile *
    382 loadedfile_create(const char *path)
    383 {
    384 	struct loadedfile *lf;
    385 
    386 	lf = bmake_malloc(sizeof(*lf));
    387 	lf->path = path == NULL ? "(stdin)" : path;
    388 	lf->buf = NULL;
    389 	lf->len = 0;
    390 	lf->maplen = 0;
    391 	lf->used = FALSE;
    392 	return lf;
    393 }
    394 
    395 static void
    396 loadedfile_destroy(struct loadedfile *lf)
    397 {
    398 	if (lf->buf != NULL) {
    399 		if (lf->maplen > 0) {
    400 			munmap(lf->buf, lf->maplen);
    401 		} else {
    402 			free(lf->buf);
    403 		}
    404 	}
    405 	free(lf);
    406 }
    407 
    408 /*
    409  * nextbuf() operation for loadedfile, as needed by the weird and twisted
    410  * logic below. Once that's cleaned up, we can get rid of lf->used...
    411  */
    412 static char *
    413 loadedfile_nextbuf(void *x, size_t *len)
    414 {
    415 	struct loadedfile *lf = x;
    416 
    417 	if (lf->used) {
    418 		return NULL;
    419 	}
    420 	lf->used = TRUE;
    421 	*len = lf->len;
    422 	return lf->buf;
    423 }
    424 
    425 /*
    426  * Try to get the size of a file.
    427  */
    428 static Boolean
    429 load_getsize(int fd, size_t *ret)
    430 {
    431 	struct stat st;
    432 
    433 	if (fstat(fd, &st) < 0) {
    434 		return FALSE;
    435 	}
    436 
    437 	if (!S_ISREG(st.st_mode)) {
    438 		return FALSE;
    439 	}
    440 
    441 	/*
    442 	 * st_size is an off_t, which is 64 bits signed; *ret is
    443 	 * size_t, which might be 32 bits unsigned or 64 bits
    444 	 * unsigned. Rather than being elaborate, just punt on
    445 	 * files that are more than 2^31 bytes. We should never
    446 	 * see a makefile that size in practice...
    447 	 *
    448 	 * While we're at it reject negative sizes too, just in case.
    449 	 */
    450 	if (st.st_size < 0 || st.st_size > 0x7fffffff) {
    451 		return FALSE;
    452 	}
    453 
    454 	*ret = (size_t) st.st_size;
    455 	return TRUE;
    456 }
    457 
    458 /*
    459  * Read in a file.
    460  *
    461  * Until the path search logic can be moved under here instead of
    462  * being in the caller in another source file, we need to have the fd
    463  * passed in already open. Bleh.
    464  *
    465  * If the path is NULL use stdin and (to insure against fd leaks)
    466  * assert that the caller passed in -1.
    467  */
    468 static struct loadedfile *
    469 loadfile(const char *path, int fd)
    470 {
    471 	struct loadedfile *lf;
    472 	static long pagesize = 0;
    473 	ssize_t result;
    474 	size_t bufpos;
    475 
    476 	lf = loadedfile_create(path);
    477 
    478 	if (path == NULL) {
    479 		assert(fd == -1);
    480 		fd = STDIN_FILENO;
    481 	} else {
    482 #if 0 /* notyet */
    483 		fd = open(path, O_RDONLY);
    484 		if (fd < 0) {
    485 			...
    486 			Error("%s: %s", path, strerror(errno));
    487 			exit(1);
    488 		}
    489 #endif
    490 	}
    491 
    492 	if (load_getsize(fd, &lf->len)) {
    493 		/* found a size, try mmap */
    494 		if (pagesize == 0)
    495 			pagesize = sysconf(_SC_PAGESIZE);
    496 		if (pagesize <= 0) {
    497 			pagesize = 0x1000;
    498 		}
    499 		/* round size up to a page */
    500 		lf->maplen = pagesize * ((lf->len + pagesize - 1)/pagesize);
    501 
    502 		/*
    503 		 * XXX hack for dealing with empty files; remove when
    504 		 * we're no longer limited by interfacing to the old
    505 		 * logic elsewhere in this file.
    506 		 */
    507 		if (lf->maplen == 0) {
    508 			lf->maplen = pagesize;
    509 		}
    510 
    511 		/*
    512 		 * FUTURE: remove PROT_WRITE when the parser no longer
    513 		 * needs to scribble on the input.
    514 		 */
    515 		lf->buf = mmap(NULL, lf->maplen, PROT_READ|PROT_WRITE,
    516 			       MAP_FILE|MAP_COPY, fd, 0);
    517 		if (lf->buf != MAP_FAILED) {
    518 			/* succeeded */
    519 			if (lf->len == lf->maplen && lf->buf[lf->len - 1] != '\n') {
    520 				char *b = bmake_malloc(lf->len + 1);
    521 				b[lf->len] = '\n';
    522 				memcpy(b, lf->buf, lf->len++);
    523 				munmap(lf->buf, lf->maplen);
    524 				lf->maplen = 0;
    525 				lf->buf = b;
    526 			}
    527 			goto done;
    528 		}
    529 	}
    530 
    531 	/* cannot mmap; load the traditional way */
    532 
    533 	lf->maplen = 0;
    534 	lf->len = 1024;
    535 	lf->buf = bmake_malloc(lf->len);
    536 
    537 	bufpos = 0;
    538 	while (1) {
    539 		assert(bufpos <= lf->len);
    540 		if (bufpos == lf->len) {
    541 			if (lf->len > SIZE_MAX/2) {
    542 				errno = EFBIG;
    543 				Error("%s: file too large", path);
    544 				exit(1);
    545 			}
    546 			lf->len *= 2;
    547 			lf->buf = bmake_realloc(lf->buf, lf->len);
    548 		}
    549 		assert(bufpos < lf->len);
    550 		result = read(fd, lf->buf + bufpos, lf->len - bufpos);
    551 		if (result < 0) {
    552 			Error("%s: read error: %s", path, strerror(errno));
    553 			exit(1);
    554 		}
    555 		if (result == 0) {
    556 			break;
    557 		}
    558 		bufpos += result;
    559 	}
    560 	assert(bufpos <= lf->len);
    561 	lf->len = bufpos;
    562 
    563 	/* truncate malloc region to actual length (maybe not useful) */
    564 	if (lf->len > 0) {
    565 		/* as for mmap case, ensure trailing \n */
    566 		if (lf->buf[lf->len - 1] != '\n')
    567 			lf->len++;
    568 		lf->buf = bmake_realloc(lf->buf, lf->len);
    569 		lf->buf[lf->len - 1] = '\n';
    570 	}
    571 
    572 done:
    573 	if (path != NULL) {
    574 		close(fd);
    575 	}
    576 	return lf;
    577 }
    578 
    579 /* old code */
    580 
    581 /*-
    582  *----------------------------------------------------------------------
    583  * ParseIsEscaped --
    584  *	Check if the current character is escaped on the current line
    585  *
    586  * Results:
    587  *	0 if the character is not backslash escaped, 1 otherwise
    588  *
    589  * Side Effects:
    590  *	None
    591  *----------------------------------------------------------------------
    592  */
    593 static int
    594 ParseIsEscaped(const char *line, const char *c)
    595 {
    596     int active = 0;
    597     for (;;) {
    598 	if (line == c)
    599 	    return active;
    600 	if (*--c != '\\')
    601 	    return active;
    602 	active = !active;
    603     }
    604 }
    605 
    606 /*-
    607  *----------------------------------------------------------------------
    608  * ParseFindKeyword --
    609  *	Look in the table of keywords for one matching the given string.
    610  *
    611  * Input:
    612  *	str		String to find
    613  *
    614  * Results:
    615  *	The index of the keyword, or -1 if it isn't there.
    616  *
    617  * Side Effects:
    618  *	None
    619  *----------------------------------------------------------------------
    620  */
    621 static int
    622 ParseFindKeyword(const char *str)
    623 {
    624     int    start, end, cur;
    625     int    diff;
    626 
    627     start = 0;
    628     end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
    629 
    630     do {
    631 	cur = start + ((end - start) / 2);
    632 	diff = strcmp(str, parseKeywords[cur].name);
    633 
    634 	if (diff == 0) {
    635 	    return cur;
    636 	} else if (diff < 0) {
    637 	    end = cur - 1;
    638 	} else {
    639 	    start = cur + 1;
    640 	}
    641     } while (start <= end);
    642     return -1;
    643 }
    644 
    645 /*-
    646  * ParseVErrorInternal  --
    647  *	Error message abort function for parsing. Prints out the context
    648  *	of the error (line number and file) as well as the message with
    649  *	two optional arguments.
    650  *
    651  * Results:
    652  *	None
    653  *
    654  * Side Effects:
    655  *	"fatals" is incremented if the level is PARSE_FATAL.
    656  */
    657 /* VARARGS */
    658 static void
    659 ParseVErrorInternal(FILE *f, const char *cfname, size_t clineno, int type,
    660     const char *fmt, va_list ap)
    661 {
    662 	static Boolean fatal_warning_error_printed = FALSE;
    663 	char dirbuf[MAXPATHLEN+1];
    664 
    665 	(void)fprintf(f, "%s: ", progname);
    666 
    667 	if (cfname != NULL) {
    668 		(void)fprintf(f, "\"");
    669 		if (*cfname != '/' && strcmp(cfname, "(stdin)") != 0) {
    670 			char *cp, *cp2;
    671 			const char *dir, *fname;
    672 
    673 			/*
    674 			 * Nothing is more annoying than not knowing
    675 			 * which Makefile is the culprit; we try ${.PARSEDIR}
    676 			 * and apply realpath(3) if not absolute.
    677 			 */
    678 			dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &cp);
    679 			if (dir == NULL)
    680 				dir = ".";
    681 			if (*dir != '/') {
    682 				dir = realpath(dir, dirbuf);
    683 			}
    684 			fname = Var_Value(".PARSEFILE", VAR_GLOBAL, &cp2);
    685 			if (fname == NULL) {
    686 				if ((fname = strrchr(cfname, '/')))
    687 					fname++;
    688 				else
    689 					fname = cfname;
    690 			}
    691 			(void)fprintf(f, "%s/%s", dir, fname);
    692 			bmake_free(cp2);
    693 			bmake_free(cp);
    694 		} else
    695 			(void)fprintf(f, "%s", cfname);
    696 
    697 		(void)fprintf(f, "\" line %d: ", (int)clineno);
    698 	}
    699 	if (type == PARSE_WARNING)
    700 		(void)fprintf(f, "warning: ");
    701 	(void)vfprintf(f, fmt, ap);
    702 	(void)fprintf(f, "\n");
    703 	(void)fflush(f);
    704 	if (type == PARSE_INFO)
    705 		return;
    706 	if (type == PARSE_FATAL || parseWarnFatal)
    707 		fatals += 1;
    708 	if (parseWarnFatal && !fatal_warning_error_printed) {
    709 		Error("parsing warnings being treated as errors");
    710 		fatal_warning_error_printed = TRUE;
    711 	}
    712 }
    713 
    714 /*-
    715  * ParseErrorInternal  --
    716  *	Error function
    717  *
    718  * Results:
    719  *	None
    720  *
    721  * Side Effects:
    722  *	None
    723  */
    724 /* VARARGS */
    725 static void
    726 ParseErrorInternal(const char *cfname, size_t clineno, int type,
    727     const char *fmt, ...)
    728 {
    729 	va_list ap;
    730 
    731 	va_start(ap, fmt);
    732 	(void)fflush(stdout);
    733 	ParseVErrorInternal(stderr, cfname, clineno, type, fmt, ap);
    734 	va_end(ap);
    735 
    736 	if (debug_file != stderr && debug_file != stdout) {
    737 		va_start(ap, fmt);
    738 		ParseVErrorInternal(debug_file, cfname, clineno, type, fmt, ap);
    739 		va_end(ap);
    740 	}
    741 }
    742 
    743 /*-
    744  * Parse_Error  --
    745  *	External interface to ParseErrorInternal; uses the default filename
    746  *	Line number.
    747  *
    748  * Results:
    749  *	None
    750  *
    751  * Side Effects:
    752  *	None
    753  */
    754 /* VARARGS */
    755 void
    756 Parse_Error(int type, const char *fmt, ...)
    757 {
    758 	va_list ap;
    759 	const char *fname;
    760 	size_t lineno;
    761 
    762 	if (curFile == NULL) {
    763 		fname = NULL;
    764 		lineno = 0;
    765 	} else {
    766 		fname = curFile->fname;
    767 		lineno = curFile->lineno;
    768 	}
    769 
    770 	va_start(ap, fmt);
    771 	(void)fflush(stdout);
    772 	ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
    773 	va_end(ap);
    774 
    775 	if (debug_file != stderr && debug_file != stdout) {
    776 		va_start(ap, fmt);
    777 		ParseVErrorInternal(debug_file, fname, lineno, type, fmt, ap);
    778 		va_end(ap);
    779 	}
    780 }
    781 
    782 
    783 /*
    784  * ParseMessage
    785  *	Parse a .info .warning or .error directive
    786  *
    787  *	The input is the line minus the ".".  We substitute
    788  *	variables, print the message and exit(1) (for .error) or just print
    789  *	a warning if the directive is malformed.
    790  */
    791 static Boolean
    792 ParseMessage(char *line)
    793 {
    794     int mtype;
    795 
    796     switch(*line) {
    797     case 'i':
    798 	mtype = PARSE_INFO;
    799 	break;
    800     case 'w':
    801 	mtype = PARSE_WARNING;
    802 	break;
    803     case 'e':
    804 	mtype = PARSE_FATAL;
    805 	break;
    806     default:
    807 	Parse_Error(PARSE_WARNING, "invalid syntax: \".%s\"", line);
    808 	return FALSE;
    809     }
    810 
    811     while (isalpha((unsigned char)*line))
    812 	line++;
    813     if (!isspace((unsigned char)*line))
    814 	return FALSE;			/* not for us */
    815     while (isspace((unsigned char)*line))
    816 	line++;
    817 
    818     line = Var_Subst(line, VAR_CMD, VARE_WANTRES);
    819     Parse_Error(mtype, "%s", line);
    820     free(line);
    821 
    822     if (mtype == PARSE_FATAL) {
    823 	/* Terminate immediately. */
    824 	exit(1);
    825     }
    826     return TRUE;
    827 }
    828 
    829 /*-
    830  *---------------------------------------------------------------------
    831  * ParseLinkSrc  --
    832  *	Link the parent node to its new child. Used in a Lst_ForEach by
    833  *	ParseDoDependency. If the specType isn't 'Not', the parent
    834  *	isn't linked as a parent of the child.
    835  *
    836  * Input:
    837  *	pgnp		The parent node
    838  *	cgpn		The child node
    839  *
    840  * Results:
    841  *	Always = 0
    842  *
    843  * Side Effects:
    844  *	New elements are added to the parents list of cgn and the
    845  *	children list of cgn. the unmade field of pgn is updated
    846  *	to reflect the additional child.
    847  *---------------------------------------------------------------------
    848  */
    849 static int
    850 ParseLinkSrc(void *pgnp, void *cgnp)
    851 {
    852     GNode          *pgn = (GNode *)pgnp;
    853     GNode          *cgn = (GNode *)cgnp;
    854 
    855     if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(pgn->cohorts))
    856 	pgn = LstNode_Datum(Lst_Last(pgn->cohorts));
    857     Lst_Append(pgn->children, cgn);
    858     if (specType == Not)
    859 	Lst_Append(cgn->parents, pgn);
    860     pgn->unmade += 1;
    861     if (DEBUG(PARSE)) {
    862 	fprintf(debug_file, "# %s: added child %s - %s\n", __func__,
    863 	    pgn->name, cgn->name);
    864 	Targ_PrintNode(pgn, 0);
    865 	Targ_PrintNode(cgn, 0);
    866     }
    867     return 0;
    868 }
    869 
    870 /*-
    871  *---------------------------------------------------------------------
    872  * ParseDoOp  --
    873  *	Apply the parsed operator to the given target node. Used in a
    874  *	Lst_ForEach call by ParseDoDependency once all targets have
    875  *	been found and their operator parsed. If the previous and new
    876  *	operators are incompatible, a major error is taken.
    877  *
    878  * Input:
    879  *	gnp		The node to which the operator is to be applied
    880  *	opp		The operator to apply
    881  *
    882  * Results:
    883  *	Always 0
    884  *
    885  * Side Effects:
    886  *	The type field of the node is altered to reflect any new bits in
    887  *	the op.
    888  *---------------------------------------------------------------------
    889  */
    890 static int
    891 ParseDoOp(void *gnp, void *opp)
    892 {
    893     GNode          *gn = (GNode *)gnp;
    894     int             op = *(int *)opp;
    895     /*
    896      * If the dependency mask of the operator and the node don't match and
    897      * the node has actually had an operator applied to it before, and
    898      * the operator actually has some dependency information in it, complain.
    899      */
    900     if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
    901 	!OP_NOP(gn->type) && !OP_NOP(op))
    902     {
    903 	Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
    904 	return 1;
    905     }
    906 
    907     if (op == OP_DOUBLEDEP && (gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
    908 	/*
    909 	 * If the node was the object of a :: operator, we need to create a
    910 	 * new instance of it for the children and commands on this dependency
    911 	 * line. The new instance is placed on the 'cohorts' list of the
    912 	 * initial one (note the initial one is not on its own cohorts list)
    913 	 * and the new instance is linked to all parents of the initial
    914 	 * instance.
    915 	 */
    916 	GNode	*cohort;
    917 
    918 	/*
    919 	 * Propagate copied bits to the initial node.  They'll be propagated
    920 	 * back to the rest of the cohorts later.
    921 	 */
    922 	gn->type |= op & ~OP_OPMASK;
    923 
    924 	cohort = Targ_FindNode(gn->name, TARG_NOHASH);
    925 	if (doing_depend)
    926 	    ParseMark(cohort);
    927 	/*
    928 	 * Make the cohort invisible as well to avoid duplicating it into
    929 	 * other variables. True, parents of this target won't tend to do
    930 	 * anything with their local variables, but better safe than
    931 	 * sorry. (I think this is pointless now, since the relevant list
    932 	 * traversals will no longer see this node anyway. -mycroft)
    933 	 */
    934 	cohort->type = op | OP_INVISIBLE;
    935 	Lst_Append(gn->cohorts, cohort);
    936 	cohort->centurion = gn;
    937 	gn->unmade_cohorts += 1;
    938 	snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
    939 		gn->unmade_cohorts);
    940     } else {
    941 	/*
    942 	 * We don't want to nuke any previous flags (whatever they were) so we
    943 	 * just OR the new operator into the old
    944 	 */
    945 	gn->type |= op;
    946     }
    947 
    948     return 0;
    949 }
    950 
    951 /*-
    952  *---------------------------------------------------------------------
    953  * ParseDoSrc  --
    954  *	Given the name of a source, figure out if it is an attribute
    955  *	and apply it to the targets if it is. Else decide if there is
    956  *	some attribute which should be applied *to* the source because
    957  *	of some special target and apply it if so. Otherwise, make the
    958  *	source be a child of the targets in the list 'targets'
    959  *
    960  * Input:
    961  *	tOp		operator (if any) from special targets
    962  *	src		name of the source to handle
    963  *
    964  * Results:
    965  *	None
    966  *
    967  * Side Effects:
    968  *	Operator bits may be added to the list of targets or to the source.
    969  *	The targets may have a new source added to their lists of children.
    970  *---------------------------------------------------------------------
    971  */
    972 static void
    973 ParseDoSrc(int tOp, const char *src)
    974 {
    975     GNode	*gn = NULL;
    976     static int wait_number = 0;
    977     char wait_src[16];
    978 
    979     if (*src == '.' && isupper ((unsigned char)src[1])) {
    980 	int keywd = ParseFindKeyword(src);
    981 	if (keywd != -1) {
    982 	    int op = parseKeywords[keywd].op;
    983 	    if (op != 0) {
    984 		if (targets != NULL)
    985 		    Lst_ForEach(targets, ParseDoOp, &op);
    986 		return;
    987 	    }
    988 	    if (parseKeywords[keywd].spec == Wait) {
    989 		/*
    990 		 * We add a .WAIT node in the dependency list.
    991 		 * After any dynamic dependencies (and filename globbing)
    992 		 * have happened, it is given a dependency on the each
    993 		 * previous child back to and previous .WAIT node.
    994 		 * The next child won't be scheduled until the .WAIT node
    995 		 * is built.
    996 		 * We give each .WAIT node a unique name (mainly for diag).
    997 		 */
    998 		snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
    999 		gn = Targ_FindNode(wait_src, TARG_NOHASH);
   1000 		if (doing_depend)
   1001 		    ParseMark(gn);
   1002 		gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
   1003 		if (targets != NULL)
   1004 		    Lst_ForEach(targets, ParseLinkSrc, gn);
   1005 		return;
   1006 	    }
   1007 	}
   1008     }
   1009 
   1010     switch (specType) {
   1011     case Main:
   1012 	/*
   1013 	 * If we have noted the existence of a .MAIN, it means we need
   1014 	 * to add the sources of said target to the list of things
   1015 	 * to create. The string 'src' is likely to be free, so we
   1016 	 * must make a new copy of it. Note that this will only be
   1017 	 * invoked if the user didn't specify a target on the command
   1018 	 * line. This is to allow #ifmake's to succeed, or something...
   1019 	 */
   1020 	Lst_Append(create, bmake_strdup(src));
   1021 	/*
   1022 	 * Add the name to the .TARGETS variable as well, so the user can
   1023 	 * employ that, if desired.
   1024 	 */
   1025 	Var_Append(".TARGETS", src, VAR_GLOBAL);
   1026 	return;
   1027 
   1028     case Order:
   1029 	/*
   1030 	 * Create proper predecessor/successor links between the previous
   1031 	 * source and the current one.
   1032 	 */
   1033 	gn = Targ_FindNode(src, TARG_CREATE);
   1034 	if (doing_depend)
   1035 	    ParseMark(gn);
   1036 	if (predecessor != NULL) {
   1037 	    Lst_Append(predecessor->order_succ, gn);
   1038 	    Lst_Append(gn->order_pred, predecessor);
   1039 	    if (DEBUG(PARSE)) {
   1040 		fprintf(debug_file, "# %s: added Order dependency %s - %s\n",
   1041 		    __func__, predecessor->name, gn->name);
   1042 		Targ_PrintNode(predecessor, 0);
   1043 		Targ_PrintNode(gn, 0);
   1044 	    }
   1045 	}
   1046 	/*
   1047 	 * The current source now becomes the predecessor for the next one.
   1048 	 */
   1049 	predecessor = gn;
   1050 	break;
   1051 
   1052     default:
   1053 	/*
   1054 	 * If the source is not an attribute, we need to find/create
   1055 	 * a node for it. After that we can apply any operator to it
   1056 	 * from a special target or link it to its parents, as
   1057 	 * appropriate.
   1058 	 *
   1059 	 * In the case of a source that was the object of a :: operator,
   1060 	 * the attribute is applied to all of its instances (as kept in
   1061 	 * the 'cohorts' list of the node) or all the cohorts are linked
   1062 	 * to all the targets.
   1063 	 */
   1064 
   1065 	/* Find/create the 'src' node and attach to all targets */
   1066 	gn = Targ_FindNode(src, TARG_CREATE);
   1067 	if (doing_depend)
   1068 	    ParseMark(gn);
   1069 	if (tOp) {
   1070 	    gn->type |= tOp;
   1071 	} else {
   1072 	    if (targets != NULL)
   1073 		Lst_ForEach(targets, ParseLinkSrc, gn);
   1074 	}
   1075 	break;
   1076     }
   1077 }
   1078 
   1079 /*-
   1080  *-----------------------------------------------------------------------
   1081  * ParseFindMain --
   1082  *	Find a real target in the list and set it to be the main one.
   1083  *	Called by ParseDoDependency when a main target hasn't been found
   1084  *	yet.
   1085  *
   1086  * Input:
   1087  *	gnp		Node to examine
   1088  *
   1089  * Results:
   1090  *	0 if main not found yet, 1 if it is.
   1091  *
   1092  * Side Effects:
   1093  *	mainNode is changed and Targ_SetMain is called.
   1094  *
   1095  *-----------------------------------------------------------------------
   1096  */
   1097 static int
   1098 ParseFindMain(void *gnp, void *dummy MAKE_ATTR_UNUSED)
   1099 {
   1100     GNode   	  *gn = (GNode *)gnp;
   1101     if (!(gn->type & OP_NOTARGET)) {
   1102 	mainNode = gn;
   1103 	Targ_SetMain(gn);
   1104 	return 1;
   1105     } else {
   1106 	return 0;
   1107     }
   1108 }
   1109 
   1110 /*-
   1111  *-----------------------------------------------------------------------
   1112  * ParseAddDir --
   1113  *	Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
   1114  *
   1115  * Results:
   1116  *	=== 0
   1117  *
   1118  * Side Effects:
   1119  *	See Dir_AddDir.
   1120  *
   1121  *-----------------------------------------------------------------------
   1122  */
   1123 static int
   1124 ParseAddDir(void *path, void *name)
   1125 {
   1126     (void)Dir_AddDir((Lst) path, (char *)name);
   1127     return 0;
   1128 }
   1129 
   1130 /*-
   1131  *-----------------------------------------------------------------------
   1132  * ParseClearPath --
   1133  *	Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
   1134  *
   1135  * Results:
   1136  *	=== 0
   1137  *
   1138  * Side Effects:
   1139  *	See Dir_ClearPath
   1140  *
   1141  *-----------------------------------------------------------------------
   1142  */
   1143 static int
   1144 ParseClearPath(void *path, void *dummy MAKE_ATTR_UNUSED)
   1145 {
   1146     Dir_ClearPath((Lst) path);
   1147     return 0;
   1148 }
   1149 
   1150 /*-
   1151  *---------------------------------------------------------------------
   1152  * ParseDoDependency  --
   1153  *	Parse the dependency line in line.
   1154  *
   1155  * Input:
   1156  *	line		the line to parse
   1157  *
   1158  * Results:
   1159  *	None
   1160  *
   1161  * Side Effects:
   1162  *	The nodes of the sources are linked as children to the nodes of the
   1163  *	targets. Some nodes may be created.
   1164  *
   1165  *	We parse a dependency line by first extracting words from the line and
   1166  * finding nodes in the list of all targets with that name. This is done
   1167  * until a character is encountered which is an operator character. Currently
   1168  * these are only ! and :. At this point the operator is parsed and the
   1169  * pointer into the line advanced until the first source is encountered.
   1170  * 	The parsed operator is applied to each node in the 'targets' list,
   1171  * which is where the nodes found for the targets are kept, by means of
   1172  * the ParseDoOp function.
   1173  *	The sources are read in much the same way as the targets were except
   1174  * that now they are expanded using the wildcarding scheme of the C-Shell
   1175  * and all instances of the resulting words in the list of all targets
   1176  * are found. Each of the resulting nodes is then linked to each of the
   1177  * targets as one of its children.
   1178  *	Certain targets are handled specially. These are the ones detailed
   1179  * by the specType variable.
   1180  *	The storing of transformation rules is also taken care of here.
   1181  * A target is recognized as a transformation rule by calling
   1182  * Suff_IsTransform. If it is a transformation rule, its node is gotten
   1183  * from the suffix module via Suff_AddTransform rather than the standard
   1184  * Targ_FindNode in the target module.
   1185  *---------------------------------------------------------------------
   1186  */
   1187 static void
   1188 ParseDoDependency(char *line)
   1189 {
   1190     char  	   *cp;		/* our current position */
   1191     GNode 	   *gn = NULL;	/* a general purpose temporary node */
   1192     int             op;		/* the operator on the line */
   1193     char            savec;	/* a place to save a character */
   1194     Lst    	    paths;   	/* List of search paths to alter when parsing
   1195 				 * a list of .PATH targets */
   1196     int	    	    tOp;    	/* operator from special target */
   1197     Lst	    	    sources;	/* list of archive source names after
   1198 				 * expansion */
   1199     Lst 	    curTargs;	/* list of target names to be found and added
   1200 				 * to the targets list */
   1201     char	   *lstart = line;
   1202 
   1203     if (DEBUG(PARSE))
   1204 	fprintf(debug_file, "ParseDoDependency(%s)\n", line);
   1205     tOp = 0;
   1206 
   1207     specType = Not;
   1208     paths = NULL;
   1209 
   1210     curTargs = Lst_Init();
   1211 
   1212     /*
   1213      * First, grind through the targets.
   1214      */
   1215 
   1216     do {
   1217 	/*
   1218 	 * Here LINE points to the beginning of the next word, and
   1219 	 * LSTART points to the actual beginning of the line.
   1220 	 */
   1221 
   1222 	/* Find the end of the next word. */
   1223 	for (cp = line; *cp && (ParseIsEscaped(lstart, cp) ||
   1224 		     !(isspace((unsigned char)*cp) ||
   1225 			 *cp == '!' || *cp == ':' || *cp == LPAREN));
   1226 		 cp++) {
   1227 	    if (*cp == '$') {
   1228 		/*
   1229 		 * Must be a dynamic source (would have been expanded
   1230 		 * otherwise), so call the Var module to parse the puppy
   1231 		 * so we can safely advance beyond it...There should be
   1232 		 * no errors in this, as they would have been discovered
   1233 		 * in the initial Var_Subst and we wouldn't be here.
   1234 		 */
   1235 		int 	length;
   1236 		void    *freeIt;
   1237 
   1238 		(void)Var_Parse(cp, VAR_CMD, VARE_UNDEFERR|VARE_WANTRES,
   1239 				&length, &freeIt);
   1240 		free(freeIt);
   1241 		cp += length - 1;
   1242 	    }
   1243 	}
   1244 
   1245 	/*
   1246 	 * If the word is followed by a left parenthesis, it's the
   1247 	 * name of an object file inside an archive (ar file).
   1248 	 */
   1249 	if (!ParseIsEscaped(lstart, cp) && *cp == LPAREN) {
   1250 	    /*
   1251 	     * Archives must be handled specially to make sure the OP_ARCHV
   1252 	     * flag is set in their 'type' field, for one thing, and because
   1253 	     * things like "archive(file1.o file2.o file3.o)" are permissible.
   1254 	     * Arch_ParseArchive will set 'line' to be the first non-blank
   1255 	     * after the archive-spec. It creates/finds nodes for the members
   1256 	     * and places them on the given list, returning TRUE if all
   1257 	     * went well and FALSE if there was an error in the
   1258 	     * specification. On error, line should remain untouched.
   1259 	     */
   1260 	    if (!Arch_ParseArchive(&line, targets, VAR_CMD)) {
   1261 		Parse_Error(PARSE_FATAL,
   1262 			     "Error in archive specification: \"%s\"", line);
   1263 		goto out;
   1264 	    } else {
   1265 		/* Done with this word; on to the next. */
   1266 		cp = line;
   1267 		continue;
   1268 	    }
   1269 	}
   1270 
   1271 	if (!*cp) {
   1272 	    /*
   1273 	     * We got to the end of the line while we were still
   1274 	     * looking at targets.
   1275 	     *
   1276 	     * Ending a dependency line without an operator is a Bozo
   1277 	     * no-no.  As a heuristic, this is also often triggered by
   1278 	     * undetected conflicts from cvs/rcs merges.
   1279 	     */
   1280 	    if ((strncmp(line, "<<<<<<", 6) == 0) ||
   1281 		(strncmp(line, "======", 6) == 0) ||
   1282 		(strncmp(line, ">>>>>>", 6) == 0))
   1283 		Parse_Error(PARSE_FATAL,
   1284 		    "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
   1285 	    else if (lstart[0] == '.') {
   1286 		const char *dirstart = lstart + 1;
   1287 		const char *dirend;
   1288 		while (isspace((unsigned char)*dirstart))
   1289 		    dirstart++;
   1290 		dirend = dirstart;
   1291 		while (isalnum((unsigned char)*dirend) || *dirend == '-')
   1292 		    dirend++;
   1293 		Parse_Error(PARSE_FATAL, "Unknown directive \"%.*s\"",
   1294 			    (int)(dirend - dirstart), dirstart);
   1295 	    } else
   1296 		Parse_Error(PARSE_FATAL, "Need an operator");
   1297 	    goto out;
   1298 	}
   1299 
   1300 	/* Insert a null terminator. */
   1301 	savec = *cp;
   1302 	*cp = '\0';
   1303 
   1304 	/*
   1305 	 * Got the word. See if it's a special target and if so set
   1306 	 * specType to match it.
   1307 	 */
   1308 	if (*line == '.' && isupper ((unsigned char)line[1])) {
   1309 	    /*
   1310 	     * See if the target is a special target that must have it
   1311 	     * or its sources handled specially.
   1312 	     */
   1313 	    int keywd = ParseFindKeyword(line);
   1314 	    if (keywd != -1) {
   1315 		if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
   1316 		    Parse_Error(PARSE_FATAL, "Mismatched special targets");
   1317 		    goto out;
   1318 		}
   1319 
   1320 		specType = parseKeywords[keywd].spec;
   1321 		tOp = parseKeywords[keywd].op;
   1322 
   1323 		/*
   1324 		 * Certain special targets have special semantics:
   1325 		 *	.PATH		Have to set the dirSearchPath
   1326 		 *			variable too
   1327 		 *	.MAIN		Its sources are only used if
   1328 		 *			nothing has been specified to
   1329 		 *			create.
   1330 		 *	.DEFAULT    	Need to create a node to hang
   1331 		 *			commands on, but we don't want
   1332 		 *			it in the graph, nor do we want
   1333 		 *			it to be the Main Target, so we
   1334 		 *			create it, set OP_NOTMAIN and
   1335 		 *			add it to the list, setting
   1336 		 *			DEFAULT to the new node for
   1337 		 *			later use. We claim the node is
   1338 		 *	    	    	A transformation rule to make
   1339 		 *	    	    	life easier later, when we'll
   1340 		 *	    	    	use Make_HandleUse to actually
   1341 		 *	    	    	apply the .DEFAULT commands.
   1342 		 *	.PHONY		The list of targets
   1343 		 *	.NOPATH		Don't search for file in the path
   1344 		 *	.STALE
   1345 		 *	.BEGIN
   1346 		 *	.END
   1347 		 *	.ERROR
   1348 		 *	.DELETE_ON_ERROR
   1349 		 *	.INTERRUPT  	Are not to be considered the
   1350 		 *			main target.
   1351 		 *  	.NOTPARALLEL	Make only one target at a time.
   1352 		 *  	.SINGLESHELL	Create a shell for each command.
   1353 		 *  	.ORDER	    	Must set initial predecessor to NULL
   1354 		 */
   1355 		switch (specType) {
   1356 		case ExPath:
   1357 		    if (paths == NULL) {
   1358 			paths = Lst_Init();
   1359 		    }
   1360 		    Lst_Append(paths, dirSearchPath);
   1361 		    break;
   1362 		case Main:
   1363 		    if (!Lst_IsEmpty(create)) {
   1364 			specType = Not;
   1365 		    }
   1366 		    break;
   1367 		case Begin:
   1368 		case End:
   1369 		case Stale:
   1370 		case dotError:
   1371 		case Interrupt:
   1372 		    gn = Targ_FindNode(line, TARG_CREATE);
   1373 		    if (doing_depend)
   1374 			ParseMark(gn);
   1375 		    gn->type |= OP_NOTMAIN|OP_SPECIAL;
   1376 		    Lst_Append(targets, gn);
   1377 		    break;
   1378 		case Default:
   1379 		    gn = Targ_NewGN(".DEFAULT");
   1380 		    gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
   1381 		    Lst_Append(targets, gn);
   1382 		    DEFAULT = gn;
   1383 		    break;
   1384 		case DeleteOnError:
   1385 		    deleteOnError = TRUE;
   1386 		    break;
   1387 		case NotParallel:
   1388 		    maxJobs = 1;
   1389 		    break;
   1390 		case SingleShell:
   1391 		    compatMake = TRUE;
   1392 		    break;
   1393 		case Order:
   1394 		    predecessor = NULL;
   1395 		    break;
   1396 		default:
   1397 		    break;
   1398 		}
   1399 	    } else if (strncmp(line, ".PATH", 5) == 0) {
   1400 		/*
   1401 		 * .PATH<suffix> has to be handled specially.
   1402 		 * Call on the suffix module to give us a path to
   1403 		 * modify.
   1404 		 */
   1405 		Lst 	path;
   1406 
   1407 		specType = ExPath;
   1408 		path = Suff_GetPath(&line[5]);
   1409 		if (path == NULL) {
   1410 		    Parse_Error(PARSE_FATAL,
   1411 				 "Suffix '%s' not defined (yet)",
   1412 				 &line[5]);
   1413 		    goto out;
   1414 		} else {
   1415 		    if (paths == NULL) {
   1416 			paths = Lst_Init();
   1417 		    }
   1418 		    Lst_Append(paths, path);
   1419 		}
   1420 	    }
   1421 	}
   1422 
   1423 	/*
   1424 	 * Have word in line. Get or create its node and stick it at
   1425 	 * the end of the targets list
   1426 	 */
   1427 	if (specType == Not && *line != '\0') {
   1428 	    if (Dir_HasWildcards(line)) {
   1429 		/*
   1430 		 * Targets are to be sought only in the current directory,
   1431 		 * so create an empty path for the thing. Note we need to
   1432 		 * use Dir_Destroy in the destruction of the path as the
   1433 		 * Dir module could have added a directory to the path...
   1434 		 */
   1435 		Lst	    emptyPath = Lst_Init();
   1436 
   1437 		Dir_Expand(line, emptyPath, curTargs);
   1438 
   1439 		Lst_Destroy(emptyPath, Dir_Destroy);
   1440 	    } else {
   1441 		/*
   1442 		 * No wildcards, but we want to avoid code duplication,
   1443 		 * so create a list with the word on it.
   1444 		 */
   1445 		Lst_Append(curTargs, line);
   1446 	    }
   1447 
   1448 	    /* Apply the targets. */
   1449 
   1450 	    while(!Lst_IsEmpty(curTargs)) {
   1451 		char *targName = Lst_Dequeue(curTargs);
   1452 
   1453 		if (!Suff_IsTransform (targName)) {
   1454 		    gn = Targ_FindNode(targName, TARG_CREATE);
   1455 		} else {
   1456 		    gn = Suff_AddTransform(targName);
   1457 		}
   1458 		if (doing_depend)
   1459 		    ParseMark(gn);
   1460 
   1461 		Lst_Append(targets, gn);
   1462 	    }
   1463 	} else if (specType == ExPath && *line != '.' && *line != '\0') {
   1464 	    Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
   1465 	}
   1466 
   1467 	/* Don't need the inserted null terminator any more. */
   1468 	*cp = savec;
   1469 
   1470 	/*
   1471 	 * If it is a special type and not .PATH, it's the only target we
   1472 	 * allow on this line...
   1473 	 */
   1474 	if (specType != Not && specType != ExPath) {
   1475 	    Boolean warning = FALSE;
   1476 
   1477 	    while (*cp && (ParseIsEscaped(lstart, cp) ||
   1478 		(*cp != '!' && *cp != ':'))) {
   1479 		if (ParseIsEscaped(lstart, cp) ||
   1480 		    (*cp != ' ' && *cp != '\t')) {
   1481 		    warning = TRUE;
   1482 		}
   1483 		cp++;
   1484 	    }
   1485 	    if (warning) {
   1486 		Parse_Error(PARSE_WARNING, "Extra target ignored");
   1487 	    }
   1488 	} else {
   1489 	    while (*cp && isspace ((unsigned char)*cp)) {
   1490 		cp++;
   1491 	    }
   1492 	}
   1493 	line = cp;
   1494     } while (*line && (ParseIsEscaped(lstart, line) ||
   1495 	(*line != '!' && *line != ':')));
   1496 
   1497     /*
   1498      * Don't need the list of target names anymore...
   1499      */
   1500     Lst_Free(curTargs);
   1501     curTargs = NULL;
   1502 
   1503     if (targets != NULL && !Lst_IsEmpty(targets)) {
   1504 	switch(specType) {
   1505 	    default:
   1506 		Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
   1507 		break;
   1508 	    case Default:
   1509 	    case Stale:
   1510 	    case Begin:
   1511 	    case End:
   1512 	    case dotError:
   1513 	    case Interrupt:
   1514 		/*
   1515 		 * These four create nodes on which to hang commands, so
   1516 		 * targets shouldn't be empty...
   1517 		 */
   1518 	    case Not:
   1519 		/*
   1520 		 * Nothing special here -- targets can be empty if it wants.
   1521 		 */
   1522 		break;
   1523 	}
   1524     }
   1525 
   1526     /*
   1527      * Have now parsed all the target names. Must parse the operator next. The
   1528      * result is left in  op .
   1529      */
   1530     if (*cp == '!') {
   1531 	op = OP_FORCE;
   1532     } else if (*cp == ':') {
   1533 	if (cp[1] == ':') {
   1534 	    op = OP_DOUBLEDEP;
   1535 	    cp++;
   1536 	} else {
   1537 	    op = OP_DEPENDS;
   1538 	}
   1539     } else {
   1540 	Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive"
   1541 		    : "Missing dependency operator");
   1542 	goto out;
   1543     }
   1544 
   1545     /* Advance beyond the operator */
   1546     cp++;
   1547 
   1548     /*
   1549      * Apply the operator to the target. This is how we remember which
   1550      * operator a target was defined with. It fails if the operator
   1551      * used isn't consistent across all references.
   1552      */
   1553     if (targets != NULL)
   1554 	Lst_ForEach(targets, ParseDoOp, &op);
   1555 
   1556     /*
   1557      * Onward to the sources.
   1558      *
   1559      * LINE will now point to the first source word, if any, or the
   1560      * end of the string if not.
   1561      */
   1562     while (*cp && isspace ((unsigned char)*cp)) {
   1563 	cp++;
   1564     }
   1565     line = cp;
   1566 
   1567     /*
   1568      * Several special targets take different actions if present with no
   1569      * sources:
   1570      *	a .SUFFIXES line with no sources clears out all old suffixes
   1571      *	a .PRECIOUS line makes all targets precious
   1572      *	a .IGNORE line ignores errors for all targets
   1573      *	a .SILENT line creates silence when making all targets
   1574      *	a .PATH removes all directories from the search path(s).
   1575      */
   1576     if (!*line) {
   1577 	switch (specType) {
   1578 	    case Suffixes:
   1579 		Suff_ClearSuffixes();
   1580 		break;
   1581 	    case Precious:
   1582 		allPrecious = TRUE;
   1583 		break;
   1584 	    case Ignore:
   1585 		ignoreErrors = TRUE;
   1586 		break;
   1587 	    case Silent:
   1588 		beSilent = TRUE;
   1589 		break;
   1590 	    case ExPath:
   1591 		if (paths != NULL)
   1592 		    Lst_ForEach(paths, ParseClearPath, NULL);
   1593 		Dir_SetPATH();
   1594 		break;
   1595 #ifdef POSIX
   1596 	    case Posix:
   1597 		Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
   1598 		break;
   1599 #endif
   1600 	    default:
   1601 		break;
   1602 	}
   1603     } else if (specType == MFlags) {
   1604 	/*
   1605 	 * Call on functions in main.c to deal with these arguments and
   1606 	 * set the initial character to a null-character so the loop to
   1607 	 * get sources won't get anything
   1608 	 */
   1609 	Main_ParseArgLine(line);
   1610 	*line = '\0';
   1611     } else if (specType == ExShell) {
   1612 	if (!Job_ParseShell(line)) {
   1613 	    Parse_Error(PARSE_FATAL, "improper shell specification");
   1614 	    goto out;
   1615 	}
   1616 	*line = '\0';
   1617     } else if (specType == NotParallel || specType == SingleShell ||
   1618 	    specType == DeleteOnError) {
   1619 	*line = '\0';
   1620     }
   1621 
   1622     /*
   1623      * NOW GO FOR THE SOURCES
   1624      */
   1625     if (specType == Suffixes || specType == ExPath ||
   1626 	specType == Includes || specType == Libs ||
   1627 	specType == Null || specType == ExObjdir)
   1628     {
   1629 	while (*line) {
   1630 	    /*
   1631 	     * If the target was one that doesn't take files as its sources
   1632 	     * but takes something like suffixes, we take each
   1633 	     * space-separated word on the line as a something and deal
   1634 	     * with it accordingly.
   1635 	     *
   1636 	     * If the target was .SUFFIXES, we take each source as a
   1637 	     * suffix and add it to the list of suffixes maintained by the
   1638 	     * Suff module.
   1639 	     *
   1640 	     * If the target was a .PATH, we add the source as a directory
   1641 	     * to search on the search path.
   1642 	     *
   1643 	     * If it was .INCLUDES, the source is taken to be the suffix of
   1644 	     * files which will be #included and whose search path should
   1645 	     * be present in the .INCLUDES variable.
   1646 	     *
   1647 	     * If it was .LIBS, the source is taken to be the suffix of
   1648 	     * files which are considered libraries and whose search path
   1649 	     * should be present in the .LIBS variable.
   1650 	     *
   1651 	     * If it was .NULL, the source is the suffix to use when a file
   1652 	     * has no valid suffix.
   1653 	     *
   1654 	     * If it was .OBJDIR, the source is a new definition for .OBJDIR,
   1655 	     * and will cause make to do a new chdir to that path.
   1656 	     */
   1657 	    while (*cp && !isspace ((unsigned char)*cp)) {
   1658 		cp++;
   1659 	    }
   1660 	    savec = *cp;
   1661 	    *cp = '\0';
   1662 	    switch (specType) {
   1663 		case Suffixes:
   1664 		    Suff_AddSuffix(line, &mainNode);
   1665 		    break;
   1666 		case ExPath:
   1667 		    if (paths != NULL)
   1668 			Lst_ForEach(paths, ParseAddDir, line);
   1669 		    break;
   1670 		case Includes:
   1671 		    Suff_AddInclude(line);
   1672 		    break;
   1673 		case Libs:
   1674 		    Suff_AddLib(line);
   1675 		    break;
   1676 		case Null:
   1677 		    Suff_SetNull(line);
   1678 		    break;
   1679 		case ExObjdir:
   1680 		    Main_SetObjdir("%s", line);
   1681 		    break;
   1682 		default:
   1683 		    break;
   1684 	    }
   1685 	    *cp = savec;
   1686 	    if (savec != '\0') {
   1687 		cp++;
   1688 	    }
   1689 	    while (*cp && isspace ((unsigned char)*cp)) {
   1690 		cp++;
   1691 	    }
   1692 	    line = cp;
   1693 	}
   1694 	if (paths) {
   1695 	    Lst_Free(paths);
   1696 	    paths = NULL;
   1697 	}
   1698 	if (specType == ExPath)
   1699 	    Dir_SetPATH();
   1700     } else {
   1701 	assert(paths == NULL);
   1702 	while (*line) {
   1703 	    /*
   1704 	     * The targets take real sources, so we must beware of archive
   1705 	     * specifications (i.e. things with left parentheses in them)
   1706 	     * and handle them accordingly.
   1707 	     */
   1708 	    for (; *cp && !isspace ((unsigned char)*cp); cp++) {
   1709 		if (*cp == LPAREN && cp > line && cp[-1] != '$') {
   1710 		    /*
   1711 		     * Only stop for a left parenthesis if it isn't at the
   1712 		     * start of a word (that'll be for variable changes
   1713 		     * later) and isn't preceded by a dollar sign (a dynamic
   1714 		     * source).
   1715 		     */
   1716 		    break;
   1717 		}
   1718 	    }
   1719 
   1720 	    if (*cp == LPAREN) {
   1721 		sources = Lst_Init();
   1722 		if (!Arch_ParseArchive(&line, sources, VAR_CMD)) {
   1723 		    Parse_Error(PARSE_FATAL,
   1724 				 "Error in source archive spec \"%s\"", line);
   1725 		    goto out;
   1726 		}
   1727 
   1728 		while (!Lst_IsEmpty(sources)) {
   1729 		    gn = Lst_Dequeue(sources);
   1730 		    ParseDoSrc(tOp, gn->name);
   1731 		}
   1732 		Lst_Free(sources);
   1733 		cp = line;
   1734 	    } else {
   1735 		if (*cp) {
   1736 		    *cp = '\0';
   1737 		    cp += 1;
   1738 		}
   1739 
   1740 		ParseDoSrc(tOp, line);
   1741 	    }
   1742 	    while (*cp && isspace ((unsigned char)*cp)) {
   1743 		cp++;
   1744 	    }
   1745 	    line = cp;
   1746 	}
   1747     }
   1748 
   1749     if (mainNode == NULL && targets != NULL) {
   1750 	/*
   1751 	 * If we have yet to decide on a main target to make, in the
   1752 	 * absence of any user input, we want the first target on
   1753 	 * the first dependency line that is actually a real target
   1754 	 * (i.e. isn't a .USE or .EXEC rule) to be made.
   1755 	 */
   1756 	Lst_ForEach(targets, ParseFindMain, NULL);
   1757     }
   1758 
   1759 out:
   1760     if (paths != NULL)
   1761 	Lst_Free(paths);
   1762     if (curTargs != NULL)
   1763 	Lst_Free(curTargs);
   1764 }
   1765 
   1766 /*-
   1767  *---------------------------------------------------------------------
   1768  * Parse_IsVar  --
   1769  *	Return TRUE if the passed line is a variable assignment. A variable
   1770  *	assignment consists of a single word followed by optional whitespace
   1771  *	followed by either a += or an = operator.
   1772  *	This function is used both by the Parse_File function and main when
   1773  *	parsing the command-line arguments.
   1774  *
   1775  * Input:
   1776  *	line		the line to check
   1777  *
   1778  * Results:
   1779  *	TRUE if it is. FALSE if it ain't
   1780  *
   1781  * Side Effects:
   1782  *	none
   1783  *---------------------------------------------------------------------
   1784  */
   1785 Boolean
   1786 Parse_IsVar(char *line)
   1787 {
   1788     Boolean wasSpace = FALSE;	/* set TRUE if found a space */
   1789     char ch;
   1790     int level = 0;
   1791 #define ISEQOPERATOR(c) \
   1792 	(((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
   1793 
   1794     /*
   1795      * Skip to variable name
   1796      */
   1797     while (*line == ' ' || *line == '\t')
   1798 	line++;
   1799 
   1800     /* Scan for one of the assignment operators outside a variable expansion */
   1801     while ((ch = *line++) != 0) {
   1802 	if (ch == '(' || ch == '{') {
   1803 	    level++;
   1804 	    continue;
   1805 	}
   1806 	if (ch == ')' || ch == '}') {
   1807 	    level--;
   1808 	    continue;
   1809 	}
   1810 	if (level != 0)
   1811 	    continue;
   1812 	while (ch == ' ' || ch == '\t') {
   1813 	    ch = *line++;
   1814 	    wasSpace = TRUE;
   1815 	}
   1816 #ifdef SUNSHCMD
   1817 	if (ch == ':' && strncmp(line, "sh", 2) == 0) {
   1818 	    line += 2;
   1819 	    continue;
   1820 	}
   1821 #endif
   1822 	if (ch == '=')
   1823 	    return TRUE;
   1824 	if (*line == '=' && ISEQOPERATOR(ch))
   1825 	    return TRUE;
   1826 	if (wasSpace)
   1827 	    return FALSE;
   1828     }
   1829 
   1830     return FALSE;
   1831 }
   1832 
   1833 /*-
   1834  *---------------------------------------------------------------------
   1835  * Parse_DoVar  --
   1836  *	Take the variable assignment in the passed line and do it in the
   1837  *	global context.
   1838  *
   1839  *	Note: There is a lexical ambiguity with assignment modifier characters
   1840  *	in variable names. This routine interprets the character before the =
   1841  *	as a modifier. Therefore, an assignment like
   1842  *	    C++=/usr/bin/CC
   1843  *	is interpreted as "C+ +=" instead of "C++ =".
   1844  *
   1845  * Input:
   1846  *	line		a line guaranteed to be a variable assignment.
   1847  *			This reduces error checks
   1848  *	ctxt		Context in which to do the assignment
   1849  *
   1850  * Results:
   1851  *	none
   1852  *
   1853  * Side Effects:
   1854  *	the variable structure of the given variable name is altered in the
   1855  *	global context.
   1856  *---------------------------------------------------------------------
   1857  */
   1858 void
   1859 Parse_DoVar(char *line, GNode *ctxt)
   1860 {
   1861     char	   *cp;	/* pointer into line */
   1862     enum {
   1863 	VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
   1864     }	    	    type;   	/* Type of assignment */
   1865     char            *opc;	/* ptr to operator character to
   1866 				 * null-terminate the variable name */
   1867     Boolean	   freeCp = FALSE; /* TRUE if cp needs to be freed,
   1868 				    * i.e. if any variable expansion was
   1869 				    * performed */
   1870     int depth;
   1871 
   1872     /*
   1873      * Skip to variable name
   1874      */
   1875     while (*line == ' ' || *line == '\t')
   1876 	line++;
   1877 
   1878     /*
   1879      * Skip to operator character, nulling out whitespace as we go
   1880      * XXX Rather than counting () and {} we should look for $ and
   1881      * then expand the variable.
   1882      */
   1883     for (depth = 0, cp = line; depth > 0 || *cp != '='; cp++) {
   1884 	if (*cp == '(' || *cp == '{') {
   1885 	    depth++;
   1886 	    continue;
   1887 	}
   1888 	if (*cp == ')' || *cp == '}') {
   1889 	    depth--;
   1890 	    continue;
   1891 	}
   1892 	if (depth == 0 && isspace ((unsigned char)*cp)) {
   1893 	    *cp = '\0';
   1894 	}
   1895     }
   1896     opc = cp-1;		/* operator is the previous character */
   1897     *cp++ = '\0';	/* nuke the = */
   1898 
   1899     /*
   1900      * Check operator type
   1901      */
   1902     switch (*opc) {
   1903 	case '+':
   1904 	    type = VAR_APPEND;
   1905 	    *opc = '\0';
   1906 	    break;
   1907 
   1908 	case '?':
   1909 	    /*
   1910 	     * If the variable already has a value, we don't do anything.
   1911 	     */
   1912 	    *opc = '\0';
   1913 	    if (Var_Exists(line, ctxt)) {
   1914 		return;
   1915 	    } else {
   1916 		type = VAR_NORMAL;
   1917 	    }
   1918 	    break;
   1919 
   1920 	case ':':
   1921 	    type = VAR_SUBST;
   1922 	    *opc = '\0';
   1923 	    break;
   1924 
   1925 	case '!':
   1926 	    type = VAR_SHELL;
   1927 	    *opc = '\0';
   1928 	    break;
   1929 
   1930 	default:
   1931 #ifdef SUNSHCMD
   1932 	    while (opc > line && *opc != ':')
   1933 		opc--;
   1934 
   1935 	    if (strncmp(opc, ":sh", 3) == 0) {
   1936 		type = VAR_SHELL;
   1937 		*opc = '\0';
   1938 		break;
   1939 	    }
   1940 #endif
   1941 	    type = VAR_NORMAL;
   1942 	    break;
   1943     }
   1944 
   1945     while (isspace((unsigned char)*cp))
   1946 	cp++;
   1947 
   1948     if (DEBUG(LINT)) {
   1949 	if (type != VAR_SUBST && strchr(cp, '$') != NULL) {
   1950 	    /* sanity check now */
   1951 	    char *cp2;
   1952 
   1953 	    cp2 = Var_Subst(cp, ctxt, VARE_ASSIGN);
   1954 	    free(cp2);
   1955 	}
   1956     }
   1957 
   1958     if (type == VAR_APPEND) {
   1959 	Var_Append(line, cp, ctxt);
   1960     } else if (type == VAR_SUBST) {
   1961 	/*
   1962 	 * Allow variables in the old value to be undefined, but leave their
   1963 	 * invocation alone -- this is done by forcing oldVars to be false.
   1964 	 * XXX: This can cause recursive variables, but that's not hard to do,
   1965 	 * and this allows someone to do something like
   1966 	 *
   1967 	 *  CFLAGS = $(.INCLUDES)
   1968 	 *  CFLAGS := -I.. $(CFLAGS)
   1969 	 *
   1970 	 * And not get an error.
   1971 	 */
   1972 	Boolean	  oldOldVars = oldVars;
   1973 
   1974 	oldVars = FALSE;
   1975 
   1976 	/*
   1977 	 * make sure that we set the variable the first time to nothing
   1978 	 * so that it gets substituted!
   1979 	 */
   1980 	if (!Var_Exists(line, ctxt))
   1981 	    Var_Set(line, "", ctxt);
   1982 
   1983 	cp = Var_Subst(cp, ctxt, VARE_WANTRES|VARE_ASSIGN);
   1984 	oldVars = oldOldVars;
   1985 	freeCp = TRUE;
   1986 
   1987 	Var_Set(line, cp, ctxt);
   1988     } else if (type == VAR_SHELL) {
   1989 	char *res;
   1990 	const char *error;
   1991 
   1992 	if (strchr(cp, '$') != NULL) {
   1993 	    /*
   1994 	     * There's a dollar sign in the command, so perform variable
   1995 	     * expansion on the whole thing. The resulting string will need
   1996 	     * freeing when we're done.
   1997 	     */
   1998 	    cp = Var_Subst(cp, VAR_CMD, VARE_UNDEFERR|VARE_WANTRES);
   1999 	    freeCp = TRUE;
   2000 	}
   2001 
   2002 	res = Cmd_Exec(cp, &error);
   2003 	Var_Set(line, res, ctxt);
   2004 	free(res);
   2005 
   2006 	if (error)
   2007 	    Parse_Error(PARSE_WARNING, error, cp);
   2008     } else {
   2009 	/*
   2010 	 * Normal assignment -- just do it.
   2011 	 */
   2012 	Var_Set(line, cp, ctxt);
   2013     }
   2014     if (strcmp(line, MAKEOVERRIDES) == 0)
   2015 	Main_ExportMAKEFLAGS(FALSE);	/* re-export MAKEFLAGS */
   2016     else if (strcmp(line, ".CURDIR") == 0) {
   2017 	/*
   2018 	 * Somone is being (too?) clever...
   2019 	 * Let's pretend they know what they are doing and
   2020 	 * re-initialize the 'cur' Path.
   2021 	 */
   2022 	Dir_InitCur(cp);
   2023 	Dir_SetPATH();
   2024     } else if (strcmp(line, MAKE_JOB_PREFIX) == 0) {
   2025 	Job_SetPrefix();
   2026     } else if (strcmp(line, MAKE_EXPORTED) == 0) {
   2027 	Var_Export(cp, FALSE);
   2028     }
   2029     if (freeCp)
   2030 	free(cp);
   2031 }
   2032 
   2033 
   2034 /*
   2035  * ParseMaybeSubMake --
   2036  * 	Scan the command string to see if it a possible submake node
   2037  * Input:
   2038  *	cmd		the command to scan
   2039  * Results:
   2040  *	TRUE if the command is possibly a submake, FALSE if not.
   2041  */
   2042 static Boolean
   2043 ParseMaybeSubMake(const char *cmd)
   2044 {
   2045     size_t i;
   2046     static struct {
   2047 	const char *name;
   2048 	size_t len;
   2049     } vals[] = {
   2050 #define MKV(A)	{	A, sizeof(A) - 1	}
   2051 	MKV("${MAKE}"),
   2052 	MKV("${.MAKE}"),
   2053 	MKV("$(MAKE)"),
   2054 	MKV("$(.MAKE)"),
   2055 	MKV("make"),
   2056     };
   2057     for (i = 0; i < sizeof(vals)/sizeof(vals[0]); i++) {
   2058 	char *ptr;
   2059 	if ((ptr = strstr(cmd, vals[i].name)) == NULL)
   2060 	    continue;
   2061 	if ((ptr == cmd || !isalnum((unsigned char)ptr[-1]))
   2062 	    && !isalnum((unsigned char)ptr[vals[i].len]))
   2063 	    return TRUE;
   2064     }
   2065     return FALSE;
   2066 }
   2067 
   2068 /*-
   2069  * ParseAddCmd  --
   2070  *	Lst_ForEach function to add a command line to all targets
   2071  *
   2072  * Input:
   2073  *	gnp		the node to which the command is to be added
   2074  *	cmd		the command to add
   2075  *
   2076  * Results:
   2077  *	Always 0
   2078  *
   2079  * Side Effects:
   2080  *	A new element is added to the commands list of the node,
   2081  *	and the node can be marked as a submake node if the command is
   2082  *	determined to be that.
   2083  */
   2084 static int
   2085 ParseAddCmd(void *gnp, void *cmd)
   2086 {
   2087     GNode *gn = (GNode *)gnp;
   2088 
   2089     /* Add to last (ie current) cohort for :: targets */
   2090     if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(gn->cohorts))
   2091 	gn = LstNode_Datum(Lst_Last(gn->cohorts));
   2092 
   2093     /* if target already supplied, ignore commands */
   2094     if (!(gn->type & OP_HAS_COMMANDS)) {
   2095 	Lst_Append(gn->commands, cmd);
   2096 	if (ParseMaybeSubMake(cmd))
   2097 	    gn->type |= OP_SUBMAKE;
   2098 	ParseMark(gn);
   2099     } else {
   2100 #ifdef notyet
   2101 	/* XXX: We cannot do this until we fix the tree */
   2102 	Lst_Append(gn->commands, cmd);
   2103 	Parse_Error(PARSE_WARNING,
   2104 		     "overriding commands for target \"%s\"; "
   2105 		     "previous commands defined at %s: %d ignored",
   2106 		     gn->name, gn->fname, gn->lineno);
   2107 #else
   2108 	Parse_Error(PARSE_WARNING,
   2109 		     "duplicate script for target \"%s\" ignored",
   2110 		     gn->name);
   2111 	ParseErrorInternal(gn->fname, gn->lineno, PARSE_WARNING,
   2112 			    "using previous script for \"%s\" defined here",
   2113 			    gn->name);
   2114 #endif
   2115     }
   2116     return 0;
   2117 }
   2118 
   2119 /*-
   2120  *-----------------------------------------------------------------------
   2121  * ParseHasCommands --
   2122  *	Callback procedure for Parse_File when destroying the list of
   2123  *	targets on the last dependency line. Marks a target as already
   2124  *	having commands if it does, to keep from having shell commands
   2125  *	on multiple dependency lines.
   2126  *
   2127  * Input:
   2128  *	gnp		Node to examine
   2129  *
   2130  * Results:
   2131  *	None
   2132  *
   2133  * Side Effects:
   2134  *	OP_HAS_COMMANDS may be set for the target.
   2135  *
   2136  *-----------------------------------------------------------------------
   2137  */
   2138 static void
   2139 ParseHasCommands(void *gnp)
   2140 {
   2141     GNode *gn = (GNode *)gnp;
   2142     if (!Lst_IsEmpty(gn->commands)) {
   2143 	gn->type |= OP_HAS_COMMANDS;
   2144     }
   2145 }
   2146 
   2147 /*-
   2148  *-----------------------------------------------------------------------
   2149  * Parse_AddIncludeDir --
   2150  *	Add a directory to the path searched for included makefiles
   2151  *	bracketed by double-quotes. Used by functions in main.c
   2152  *
   2153  * Input:
   2154  *	dir		The name of the directory to add
   2155  *
   2156  * Results:
   2157  *	None.
   2158  *
   2159  * Side Effects:
   2160  *	The directory is appended to the list.
   2161  *
   2162  *-----------------------------------------------------------------------
   2163  */
   2164 void
   2165 Parse_AddIncludeDir(char *dir)
   2166 {
   2167     (void)Dir_AddDir(parseIncPath, dir);
   2168 }
   2169 
   2170 /*-
   2171  *---------------------------------------------------------------------
   2172  * ParseDoInclude  --
   2173  *	Push to another file.
   2174  *
   2175  *	The input is the line minus the `.'. A file spec is a string
   2176  *	enclosed in <> or "". The former is looked for only in sysIncPath.
   2177  *	The latter in . and the directories specified by -I command line
   2178  *	options
   2179  *
   2180  * Results:
   2181  *	None
   2182  *
   2183  * Side Effects:
   2184  *	A structure is added to the includes Lst and readProc, lineno,
   2185  *	fname and curFILE are altered for the new file
   2186  *---------------------------------------------------------------------
   2187  */
   2188 
   2189 static void
   2190 Parse_include_file(char *file, Boolean isSystem, Boolean depinc, int silent)
   2191 {
   2192     struct loadedfile *lf;
   2193     char          *fullname;	/* full pathname of file */
   2194     char          *newName;
   2195     char          *prefEnd, *incdir;
   2196     int           fd;
   2197     int           i;
   2198 
   2199     /*
   2200      * Now we know the file's name and its search path, we attempt to
   2201      * find the durn thing. A return of NULL indicates the file don't
   2202      * exist.
   2203      */
   2204     fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
   2205 
   2206     if (fullname == NULL && !isSystem) {
   2207 	/*
   2208 	 * Include files contained in double-quotes are first searched for
   2209 	 * relative to the including file's location. We don't want to
   2210 	 * cd there, of course, so we just tack on the old file's
   2211 	 * leading path components and call Dir_FindFile to see if
   2212 	 * we can locate the beast.
   2213 	 */
   2214 
   2215 	incdir = bmake_strdup(curFile->fname);
   2216 	prefEnd = strrchr(incdir, '/');
   2217 	if (prefEnd != NULL) {
   2218 	    *prefEnd = '\0';
   2219 	    /* Now do lexical processing of leading "../" on the filename */
   2220 	    for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
   2221 		prefEnd = strrchr(incdir + 1, '/');
   2222 		if (prefEnd == NULL || strcmp(prefEnd, "/..") == 0)
   2223 		    break;
   2224 		*prefEnd = '\0';
   2225 	    }
   2226 	    newName = str_concat3(incdir, "/", file + i);
   2227 	    fullname = Dir_FindFile(newName, parseIncPath);
   2228 	    if (fullname == NULL)
   2229 		fullname = Dir_FindFile(newName, dirSearchPath);
   2230 	    free(newName);
   2231 	}
   2232 	free(incdir);
   2233 
   2234 	if (fullname == NULL) {
   2235 	    /*
   2236 	     * Makefile wasn't found in same directory as included makefile.
   2237 	     * Search for it first on the -I search path,
   2238 	     * then on the .PATH search path, if not found in a -I directory.
   2239 	     * If we have a suffix specific path we should use that.
   2240 	     */
   2241 	    char *suff;
   2242 	    Lst	suffPath = NULL;
   2243 
   2244 	    if ((suff = strrchr(file, '.'))) {
   2245 		suffPath = Suff_GetPath(suff);
   2246 		if (suffPath != NULL) {
   2247 		    fullname = Dir_FindFile(file, suffPath);
   2248 		}
   2249 	    }
   2250 	    if (fullname == NULL) {
   2251 		fullname = Dir_FindFile(file, parseIncPath);
   2252 		if (fullname == NULL) {
   2253 		    fullname = Dir_FindFile(file, dirSearchPath);
   2254 		}
   2255 	    }
   2256 	}
   2257     }
   2258 
   2259     /* Looking for a system file or file still not found */
   2260     if (fullname == NULL) {
   2261 	/*
   2262 	 * Look for it on the system path
   2263 	 */
   2264 	fullname = Dir_FindFile(file,
   2265 		    Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
   2266     }
   2267 
   2268     if (fullname == NULL) {
   2269 	if (!silent)
   2270 	    Parse_Error(PARSE_FATAL, "Could not find %s", file);
   2271 	return;
   2272     }
   2273 
   2274     /* Actually open the file... */
   2275     fd = open(fullname, O_RDONLY);
   2276     if (fd == -1) {
   2277 	if (!silent)
   2278 	    Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
   2279 	free(fullname);
   2280 	return;
   2281     }
   2282 
   2283     /* load it */
   2284     lf = loadfile(fullname, fd);
   2285 
   2286     ParseSetIncludedFile();
   2287     /* Start reading from this file next */
   2288     Parse_SetInput(fullname, 0, -1, loadedfile_nextbuf, lf);
   2289     curFile->lf = lf;
   2290     if (depinc)
   2291 	doing_depend = depinc;		/* only turn it on */
   2292 }
   2293 
   2294 static void
   2295 ParseDoInclude(char *line)
   2296 {
   2297     char          endc;	    	/* the character which ends the file spec */
   2298     char          *cp;		/* current position in file spec */
   2299     int		  silent = *line != 'i';
   2300     char	  *file = &line[7 + silent];
   2301 
   2302     /* Skip to delimiter character so we know where to look */
   2303     while (*file == ' ' || *file == '\t')
   2304 	file++;
   2305 
   2306     if (*file != '"' && *file != '<') {
   2307 	Parse_Error(PARSE_FATAL,
   2308 	    ".include filename must be delimited by '\"' or '<'");
   2309 	return;
   2310     }
   2311 
   2312     /*
   2313      * Set the search path on which to find the include file based on the
   2314      * characters which bracket its name. Angle-brackets imply it's
   2315      * a system Makefile while double-quotes imply it's a user makefile
   2316      */
   2317     if (*file == '<') {
   2318 	endc = '>';
   2319     } else {
   2320 	endc = '"';
   2321     }
   2322 
   2323     /* Skip to matching delimiter */
   2324     for (cp = ++file; *cp && *cp != endc; cp++)
   2325 	continue;
   2326 
   2327     if (*cp != endc) {
   2328 	Parse_Error(PARSE_FATAL,
   2329 		     "Unclosed %cinclude filename. '%c' expected",
   2330 		     '.', endc);
   2331 	return;
   2332     }
   2333     *cp = '\0';
   2334 
   2335     /*
   2336      * Substitute for any variables in the file name before trying to
   2337      * find the thing.
   2338      */
   2339     file = Var_Subst(file, VAR_CMD, VARE_WANTRES);
   2340 
   2341     Parse_include_file(file, endc == '>', *line == 'd', silent);
   2342     free(file);
   2343 }
   2344 
   2345 
   2346 /*-
   2347  *---------------------------------------------------------------------
   2348  * ParseSetIncludedFile  --
   2349  *	Set the .INCLUDEDFROMFILE variable to the contents of .PARSEFILE
   2350  *	and the .INCLUDEDFROMDIR variable to the contents of .PARSEDIR
   2351  *
   2352  * Results:
   2353  *	None
   2354  *
   2355  * Side Effects:
   2356  *	The .INCLUDEDFROMFILE variable is overwritten by the contents
   2357  *	of .PARSEFILE and the .INCLUDEDFROMDIR variable is overwriten
   2358  *	by the contents of .PARSEDIR
   2359  *---------------------------------------------------------------------
   2360  */
   2361 static void
   2362 ParseSetIncludedFile(void)
   2363 {
   2364     const char *pf, *pd;
   2365     char *pf_freeIt, *pd_freeIt;
   2366 
   2367     pf = Var_Value(".PARSEFILE", VAR_GLOBAL, &pf_freeIt);
   2368     Var_Set(".INCLUDEDFROMFILE", pf, VAR_GLOBAL);
   2369     pd = Var_Value(".PARSEDIR", VAR_GLOBAL, &pd_freeIt);
   2370     Var_Set(".INCLUDEDFROMDIR", pd, VAR_GLOBAL);
   2371 
   2372     if (DEBUG(PARSE))
   2373 	fprintf(debug_file, "%s: ${.INCLUDEDFROMDIR} = `%s' "
   2374 	    "${.INCLUDEDFROMFILE} = `%s'\n", __func__, pd, pf);
   2375 
   2376     bmake_free(pf_freeIt);
   2377     bmake_free(pd_freeIt);
   2378 }
   2379 /*-
   2380  *---------------------------------------------------------------------
   2381  * ParseSetParseFile  --
   2382  *	Set the .PARSEDIR and .PARSEFILE variables to the dirname and
   2383  *	basename of the given filename
   2384  *
   2385  * Results:
   2386  *	None
   2387  *
   2388  * Side Effects:
   2389  *	The .PARSEDIR and .PARSEFILE variables are overwritten by the
   2390  *	dirname and basename of the given filename.
   2391  *---------------------------------------------------------------------
   2392  */
   2393 static void
   2394 ParseSetParseFile(const char *filename)
   2395 {
   2396     char *slash, *dirname;
   2397     const char *pd, *pf;
   2398 
   2399     slash = strrchr(filename, '/');
   2400     if (slash == NULL) {
   2401 	Var_Set(".PARSEDIR", pd = curdir, VAR_GLOBAL);
   2402 	Var_Set(".PARSEFILE", pf = filename, VAR_GLOBAL);
   2403 	dirname = NULL;
   2404     } else {
   2405 	dirname = bmake_strsedup(filename, slash);
   2406 	Var_Set(".PARSEDIR", pd = dirname, VAR_GLOBAL);
   2407 	Var_Set(".PARSEFILE", pf = slash + 1, VAR_GLOBAL);
   2408     }
   2409     if (DEBUG(PARSE))
   2410 	fprintf(debug_file, "%s: ${.PARSEDIR} = `%s' ${.PARSEFILE} = `%s'\n",
   2411 	    __func__, pd, pf);
   2412     free(dirname);
   2413 }
   2414 
   2415 /*
   2416  * Track the makefiles we read - so makefiles can
   2417  * set dependencies on them.
   2418  * Avoid adding anything more than once.
   2419  */
   2420 
   2421 static void
   2422 ParseTrackInput(const char *name)
   2423 {
   2424     char *fp = NULL;
   2425 
   2426     const char *old = Var_Value(MAKE_MAKEFILES, VAR_GLOBAL, &fp);
   2427     if (old) {
   2428 	size_t name_len = strlen(name);
   2429 	const char *ep = old + strlen(old) - name_len;
   2430 	/* does it contain name? */
   2431 	for (; old != NULL; old = strchr(old, ' ')) {
   2432 	    if (*old == ' ')
   2433 		old++;
   2434 	    if (old >= ep)
   2435 		break;			/* cannot contain name */
   2436 	    if (memcmp(old, name, name_len) == 0
   2437 		    && (old[name_len] == 0 || old[name_len] == ' '))
   2438 		goto cleanup;
   2439 	}
   2440     }
   2441     Var_Append (MAKE_MAKEFILES, name, VAR_GLOBAL);
   2442  cleanup:
   2443     bmake_free(fp);
   2444 }
   2445 
   2446 
   2447 /* Start Parsing from the given source.
   2448  *
   2449  * The given file is added to the includes stack. */
   2450 void
   2451 Parse_SetInput(const char *name, int line, int fd,
   2452 	char *(*nextbuf)(void *, size_t *), void *arg)
   2453 {
   2454     char *buf;
   2455     size_t len;
   2456 
   2457     if (name == NULL)
   2458 	name = curFile->fname;
   2459     else
   2460 	ParseTrackInput(name);
   2461 
   2462     if (DEBUG(PARSE))
   2463 	fprintf(debug_file, "%s: file %s, line %d, fd %d, nextbuf %p, arg %p\n",
   2464 	    __func__, name, line, fd, nextbuf, arg);
   2465 
   2466     if (fd == -1 && nextbuf == NULL)
   2467 	/* sanity */
   2468 	return;
   2469 
   2470     if (curFile != NULL)
   2471 	/* Save exiting file info */
   2472 	Stack_Push(&includes, curFile);
   2473 
   2474     /* Allocate and fill in new structure */
   2475     curFile = bmake_malloc(sizeof *curFile);
   2476 
   2477     /*
   2478      * Once the previous state has been saved, we can get down to reading
   2479      * the new file. We set up the name of the file to be the absolute
   2480      * name of the include file so error messages refer to the right
   2481      * place.
   2482      */
   2483     curFile->fname = bmake_strdup(name);
   2484     curFile->lineno = line;
   2485     curFile->first_lineno = line;
   2486     curFile->nextbuf = nextbuf;
   2487     curFile->nextbuf_arg = arg;
   2488     curFile->lf = NULL;
   2489     curFile->depending = doing_depend;	/* restore this on EOF */
   2490 
   2491     assert(nextbuf != NULL);
   2492 
   2493     /* Get first block of input data */
   2494     buf = curFile->nextbuf(curFile->nextbuf_arg, &len);
   2495     if (buf == NULL) {
   2496 	/* Was all a waste of time ... */
   2497 	if (curFile->fname)
   2498 	    free(curFile->fname);
   2499 	free(curFile);
   2500 	return;
   2501     }
   2502     curFile->P_str = buf;
   2503     curFile->P_ptr = buf;
   2504     curFile->P_end = buf+len;
   2505 
   2506     curFile->cond_depth = Cond_save_depth();
   2507     ParseSetParseFile(name);
   2508 }
   2509 
   2510 /*-
   2511  *-----------------------------------------------------------------------
   2512  * IsInclude --
   2513  *	Check if the line is an include directive
   2514  *
   2515  * Results:
   2516  *	TRUE if it is.
   2517  *
   2518  * Side Effects:
   2519  *	None
   2520  *
   2521  *-----------------------------------------------------------------------
   2522  */
   2523 static Boolean
   2524 IsInclude(const char *line, Boolean sysv)
   2525 {
   2526 	static const char inc[] = "include";
   2527 	static const size_t inclen = sizeof(inc) - 1;
   2528 
   2529 	/* 'd' is not valid for sysv */
   2530 	int o = strchr(sysv ? "s-" : "ds-", *line) != NULL;
   2531 
   2532 	if (strncmp(line + o, inc, inclen) != 0)
   2533 		return FALSE;
   2534 
   2535 	/* Space is not mandatory for BSD .include */
   2536 	return !sysv || isspace((unsigned char)line[inclen + o]);
   2537 }
   2538 
   2539 
   2540 #ifdef SYSVINCLUDE
   2541 /*-
   2542  *-----------------------------------------------------------------------
   2543  * IsSysVInclude --
   2544  *	Check if the line is a SYSV include directive
   2545  *
   2546  * Results:
   2547  *	TRUE if it is.
   2548  *
   2549  * Side Effects:
   2550  *	None
   2551  *
   2552  *-----------------------------------------------------------------------
   2553  */
   2554 static Boolean
   2555 IsSysVInclude(const char *line)
   2556 {
   2557 	const char *p;
   2558 
   2559 	if (!IsInclude(line, TRUE))
   2560 		return FALSE;
   2561 
   2562 	/* Avoid interpeting a dependency line as an include */
   2563 	for (p = line; (p = strchr(p, ':')) != NULL;) {
   2564 		if (*++p == '\0') {
   2565 			/* end of line -> dependency */
   2566 			return FALSE;
   2567 		}
   2568 		if (*p == ':' || isspace((unsigned char)*p)) {
   2569 			/* :: operator or ': ' -> dependency */
   2570 			return FALSE;
   2571 		}
   2572 	}
   2573 	return TRUE;
   2574 }
   2575 
   2576 /*-
   2577  *---------------------------------------------------------------------
   2578  * ParseTraditionalInclude  --
   2579  *	Push to another file.
   2580  *
   2581  *	The input is the current line. The file name(s) are
   2582  *	following the "include".
   2583  *
   2584  * Results:
   2585  *	None
   2586  *
   2587  * Side Effects:
   2588  *	A structure is added to the includes Lst and readProc, lineno,
   2589  *	fname and curFILE are altered for the new file
   2590  *---------------------------------------------------------------------
   2591  */
   2592 static void
   2593 ParseTraditionalInclude(char *line)
   2594 {
   2595     char          *cp;		/* current position in file spec */
   2596     int		   done = 0;
   2597     int		   silent = line[0] != 'i';
   2598     char	  *file = &line[silent + 7];
   2599     char	  *all_files;
   2600 
   2601     if (DEBUG(PARSE)) {
   2602 	    fprintf(debug_file, "%s: %s\n", __func__, file);
   2603     }
   2604 
   2605     /*
   2606      * Skip over whitespace
   2607      */
   2608     while (isspace((unsigned char)*file))
   2609 	file++;
   2610 
   2611     /*
   2612      * Substitute for any variables in the file name before trying to
   2613      * find the thing.
   2614      */
   2615     all_files = Var_Subst(file, VAR_CMD, VARE_WANTRES);
   2616 
   2617     if (*file == '\0') {
   2618 	Parse_Error(PARSE_FATAL,
   2619 		     "Filename missing from \"include\"");
   2620 	goto out;
   2621     }
   2622 
   2623     for (file = all_files; !done; file = cp + 1) {
   2624 	/* Skip to end of line or next whitespace */
   2625 	for (cp = file; *cp && !isspace((unsigned char) *cp); cp++)
   2626 	    continue;
   2627 
   2628 	if (*cp)
   2629 	    *cp = '\0';
   2630 	else
   2631 	    done = 1;
   2632 
   2633 	Parse_include_file(file, FALSE, FALSE, silent);
   2634     }
   2635 out:
   2636     free(all_files);
   2637 }
   2638 #endif
   2639 
   2640 #ifdef GMAKEEXPORT
   2641 /*-
   2642  *---------------------------------------------------------------------
   2643  * ParseGmakeExport  --
   2644  *	Parse export <variable>=<value>
   2645  *
   2646  *	And set the environment with it.
   2647  *
   2648  * Results:
   2649  *	None
   2650  *
   2651  * Side Effects:
   2652  *	None
   2653  *---------------------------------------------------------------------
   2654  */
   2655 static void
   2656 ParseGmakeExport(char *line)
   2657 {
   2658     char	  *variable = &line[6];
   2659     char	  *value;
   2660 
   2661     if (DEBUG(PARSE)) {
   2662 	    fprintf(debug_file, "%s: %s\n", __func__, variable);
   2663     }
   2664 
   2665     /*
   2666      * Skip over whitespace
   2667      */
   2668     while (isspace((unsigned char)*variable))
   2669 	variable++;
   2670 
   2671     for (value = variable; *value && *value != '='; value++)
   2672 	continue;
   2673 
   2674     if (*value != '=') {
   2675 	Parse_Error(PARSE_FATAL,
   2676 		     "Variable/Value missing from \"export\"");
   2677 	return;
   2678     }
   2679     *value++ = '\0';			/* terminate variable */
   2680 
   2681     /*
   2682      * Expand the value before putting it in the environment.
   2683      */
   2684     value = Var_Subst(value, VAR_CMD, VARE_WANTRES);
   2685     setenv(variable, value, 1);
   2686     free(value);
   2687 }
   2688 #endif
   2689 
   2690 /*-
   2691  *---------------------------------------------------------------------
   2692  * ParseEOF  --
   2693  *	Called when EOF is reached in the current file. If we were reading
   2694  *	an include file, the includes stack is popped and things set up
   2695  *	to go back to reading the previous file at the previous location.
   2696  *
   2697  * Results:
   2698  *	CONTINUE if there's more to do. DONE if not.
   2699  *
   2700  * Side Effects:
   2701  *	The old curFILE, is closed. The includes list is shortened.
   2702  *	lineno, curFILE, and fname are changed if CONTINUE is returned.
   2703  *---------------------------------------------------------------------
   2704  */
   2705 static int
   2706 ParseEOF(void)
   2707 {
   2708     char *ptr;
   2709     size_t len;
   2710 
   2711     assert(curFile->nextbuf != NULL);
   2712 
   2713     doing_depend = curFile->depending;	/* restore this */
   2714     /* get next input buffer, if any */
   2715     ptr = curFile->nextbuf(curFile->nextbuf_arg, &len);
   2716     curFile->P_ptr = ptr;
   2717     curFile->P_str = ptr;
   2718     curFile->P_end = ptr + len;
   2719     curFile->lineno = curFile->first_lineno;
   2720     if (ptr != NULL) {
   2721 	/* Iterate again */
   2722 	return CONTINUE;
   2723     }
   2724 
   2725     /* Ensure the makefile (or loop) didn't have mismatched conditionals */
   2726     Cond_restore_depth(curFile->cond_depth);
   2727 
   2728     if (curFile->lf != NULL) {
   2729 	    loadedfile_destroy(curFile->lf);
   2730 	    curFile->lf = NULL;
   2731     }
   2732 
   2733     /* Dispose of curFile info */
   2734     /* Leak curFile->fname because all the gnodes have pointers to it */
   2735     free(curFile->P_str);
   2736     free(curFile);
   2737 
   2738     if (Stack_IsEmpty(&includes)) {
   2739 	curFile = NULL;
   2740 	/* We've run out of input */
   2741 	Var_Delete(".PARSEDIR", VAR_GLOBAL);
   2742 	Var_Delete(".PARSEFILE", VAR_GLOBAL);
   2743 	Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL);
   2744 	Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL);
   2745 	return DONE;
   2746     }
   2747 
   2748     curFile = Stack_Pop(&includes);
   2749     if (DEBUG(PARSE))
   2750 	fprintf(debug_file, "ParseEOF: returning to file %s, line %d\n",
   2751 	    curFile->fname, curFile->lineno);
   2752 
   2753     /* Restore the PARSEDIR/PARSEFILE variables */
   2754     ParseSetParseFile(curFile->fname);
   2755     return CONTINUE;
   2756 }
   2757 
   2758 #define PARSE_RAW 1
   2759 #define PARSE_SKIP 2
   2760 
   2761 static char *
   2762 ParseGetLine(int flags, int *length)
   2763 {
   2764     IFile *cf = curFile;
   2765     char *ptr;
   2766     char ch;
   2767     char *line;
   2768     char *line_end;
   2769     char *escaped;
   2770     char *comment;
   2771     char *tp;
   2772 
   2773     /* Loop through blank lines and comment lines */
   2774     for (;;) {
   2775 	cf->lineno++;
   2776 	line = cf->P_ptr;
   2777 	ptr = line;
   2778 	line_end = line;
   2779 	escaped = NULL;
   2780 	comment = NULL;
   2781 	for (;;) {
   2782 	    if (cf->P_end != NULL && ptr == cf->P_end) {
   2783 		/* end of buffer */
   2784 		ch = 0;
   2785 		break;
   2786 	    }
   2787 	    ch = *ptr;
   2788 	    if (ch == 0 || (ch == '\\' && ptr[1] == 0)) {
   2789 		if (cf->P_end == NULL)
   2790 		    /* End of string (aka for loop) data */
   2791 		    break;
   2792 		/* see if there is more we can parse */
   2793 		while (ptr++ < cf->P_end) {
   2794 		    if ((ch = *ptr) == '\n') {
   2795 			if (ptr > line && ptr[-1] == '\\')
   2796 			    continue;
   2797 			Parse_Error(PARSE_WARNING,
   2798 			    "Zero byte read from file, skipping rest of line.");
   2799 			break;
   2800 		    }
   2801 		}
   2802 		if (cf->nextbuf != NULL) {
   2803 		    /*
   2804 		     * End of this buffer; return EOF and outer logic
   2805 		     * will get the next one. (eww)
   2806 		     */
   2807 		    break;
   2808 		}
   2809 		Parse_Error(PARSE_FATAL, "Zero byte read from file");
   2810 		return NULL;
   2811 	    }
   2812 
   2813 	    if (ch == '\\') {
   2814 		/* Don't treat next character as special, remember first one */
   2815 		if (escaped == NULL)
   2816 		    escaped = ptr;
   2817 		if (ptr[1] == '\n')
   2818 		    cf->lineno++;
   2819 		ptr += 2;
   2820 		line_end = ptr;
   2821 		continue;
   2822 	    }
   2823 	    if (ch == '#' && comment == NULL) {
   2824 		/* Remember first '#' for comment stripping */
   2825 		/* Unless previous char was '[', as in modifier :[#] */
   2826 		if (!(ptr > line && ptr[-1] == '['))
   2827 		    comment = line_end;
   2828 	    }
   2829 	    ptr++;
   2830 	    if (ch == '\n')
   2831 		break;
   2832 	    if (!isspace((unsigned char)ch))
   2833 		/* We are not interested in trailing whitespace */
   2834 		line_end = ptr;
   2835 	}
   2836 
   2837 	/* Save next 'to be processed' location */
   2838 	cf->P_ptr = ptr;
   2839 
   2840 	/* Check we have a non-comment, non-blank line */
   2841 	if (line_end == line || comment == line) {
   2842 	    if (ch == 0)
   2843 		/* At end of file */
   2844 		return NULL;
   2845 	    /* Parse another line */
   2846 	    continue;
   2847 	}
   2848 
   2849 	/* We now have a line of data */
   2850 	*line_end = 0;
   2851 
   2852 	if (flags & PARSE_RAW) {
   2853 	    /* Leave '\' (etc) in line buffer (eg 'for' lines) */
   2854 	    *length = line_end - line;
   2855 	    return line;
   2856 	}
   2857 
   2858 	if (flags & PARSE_SKIP) {
   2859 	    /* Completely ignore non-directives */
   2860 	    if (line[0] != '.')
   2861 		continue;
   2862 	    /* We could do more of the .else/.elif/.endif checks here */
   2863 	}
   2864 	break;
   2865     }
   2866 
   2867     /* Brutally ignore anything after a non-escaped '#' in non-commands */
   2868     if (comment != NULL && line[0] != '\t') {
   2869 	line_end = comment;
   2870 	*line_end = 0;
   2871     }
   2872 
   2873     /* If we didn't see a '\\' then the in-situ data is fine */
   2874     if (escaped == NULL) {
   2875 	*length = line_end - line;
   2876 	return line;
   2877     }
   2878 
   2879     /* Remove escapes from '\n' and '#' */
   2880     tp = ptr = escaped;
   2881     escaped = line;
   2882     for (; ; *tp++ = ch) {
   2883 	ch = *ptr++;
   2884 	if (ch != '\\') {
   2885 	    if (ch == 0)
   2886 		break;
   2887 	    continue;
   2888 	}
   2889 
   2890 	ch = *ptr++;
   2891 	if (ch == 0) {
   2892 	    /* Delete '\\' at end of buffer */
   2893 	    tp--;
   2894 	    break;
   2895 	}
   2896 
   2897 	if (ch == '#' && line[0] != '\t')
   2898 	    /* Delete '\\' from before '#' on non-command lines */
   2899 	    continue;
   2900 
   2901 	if (ch != '\n') {
   2902 	    /* Leave '\\' in buffer for later */
   2903 	    *tp++ = '\\';
   2904 	    /* Make sure we don't delete an escaped ' ' from the line end */
   2905 	    escaped = tp + 1;
   2906 	    continue;
   2907 	}
   2908 
   2909 	/* Escaped '\n' replace following whitespace with a single ' ' */
   2910 	while (ptr[0] == ' ' || ptr[0] == '\t')
   2911 	    ptr++;
   2912 	ch = ' ';
   2913     }
   2914 
   2915     /* Delete any trailing spaces - eg from empty continuations */
   2916     while (tp > escaped && isspace((unsigned char)tp[-1]))
   2917 	tp--;
   2918 
   2919     *tp = 0;
   2920     *length = tp - line;
   2921     return line;
   2922 }
   2923 
   2924 /*-
   2925  *---------------------------------------------------------------------
   2926  * ParseReadLine --
   2927  *	Read an entire line from the input file. Called only by Parse_File.
   2928  *
   2929  * Results:
   2930  *	A line w/o its newline
   2931  *
   2932  * Side Effects:
   2933  *	Only those associated with reading a character
   2934  *---------------------------------------------------------------------
   2935  */
   2936 static char *
   2937 ParseReadLine(void)
   2938 {
   2939     char 	  *line;    	/* Result */
   2940     int	    	  lineLength;	/* Length of result */
   2941     int	    	  lineno;	/* Saved line # */
   2942     int	    	  rval;
   2943 
   2944     for (;;) {
   2945 	line = ParseGetLine(0, &lineLength);
   2946 	if (line == NULL)
   2947 	    return NULL;
   2948 
   2949 	if (line[0] != '.')
   2950 	    return line;
   2951 
   2952 	/*
   2953 	 * The line might be a conditional. Ask the conditional module
   2954 	 * about it and act accordingly
   2955 	 */
   2956 	switch (Cond_Eval(line)) {
   2957 	case COND_SKIP:
   2958 	    /* Skip to next conditional that evaluates to COND_PARSE.  */
   2959 	    do {
   2960 		line = ParseGetLine(PARSE_SKIP, &lineLength);
   2961 	    } while (line && Cond_Eval(line) != COND_PARSE);
   2962 	    if (line == NULL)
   2963 		break;
   2964 	    continue;
   2965 	case COND_PARSE:
   2966 	    continue;
   2967 	case COND_INVALID:    /* Not a conditional line */
   2968 	    /* Check for .for loops */
   2969 	    rval = For_Eval(line);
   2970 	    if (rval == 0)
   2971 		/* Not a .for line */
   2972 		break;
   2973 	    if (rval < 0)
   2974 		/* Syntax error - error printed, ignore line */
   2975 		continue;
   2976 	    /* Start of a .for loop */
   2977 	    lineno = curFile->lineno;
   2978 	    /* Accumulate loop lines until matching .endfor */
   2979 	    do {
   2980 		line = ParseGetLine(PARSE_RAW, &lineLength);
   2981 		if (line == NULL) {
   2982 		    Parse_Error(PARSE_FATAL,
   2983 			     "Unexpected end of file in for loop.");
   2984 		    break;
   2985 		}
   2986 	    } while (For_Accum(line));
   2987 	    /* Stash each iteration as a new 'input file' */
   2988 	    For_Run(lineno);
   2989 	    /* Read next line from for-loop buffer */
   2990 	    continue;
   2991 	}
   2992 	return line;
   2993     }
   2994 }
   2995 
   2996 /*-
   2997  *-----------------------------------------------------------------------
   2998  * ParseFinishLine --
   2999  *	Handle the end of a dependency group.
   3000  *
   3001  * Results:
   3002  *	Nothing.
   3003  *
   3004  * Side Effects:
   3005  *	inLine set FALSE. 'targets' list destroyed.
   3006  *
   3007  *-----------------------------------------------------------------------
   3008  */
   3009 static void
   3010 ParseFinishLine(void)
   3011 {
   3012     if (inLine) {
   3013         if (targets != NULL) {
   3014 	    Lst_ForEach(targets, Suff_EndTransform, NULL);
   3015 	    Lst_Destroy(targets, ParseHasCommands);
   3016 	}
   3017 	targets = NULL;
   3018 	inLine = FALSE;
   3019     }
   3020 }
   3021 
   3022 
   3023 /*-
   3024  *---------------------------------------------------------------------
   3025  * Parse_File --
   3026  *	Parse a file into its component parts, incorporating it into the
   3027  *	current dependency graph. This is the main function and controls
   3028  *	almost every other function in this module
   3029  *
   3030  * Input:
   3031  *	name		the name of the file being read
   3032  *	fd		Open file to makefile to parse
   3033  *
   3034  * Results:
   3035  *	None
   3036  *
   3037  * Side Effects:
   3038  *	closes fd.
   3039  *	Loads. Nodes are added to the list of all targets, nodes and links
   3040  *	are added to the dependency graph. etc. etc. etc.
   3041  *---------------------------------------------------------------------
   3042  */
   3043 void
   3044 Parse_File(const char *name, int fd)
   3045 {
   3046     char	  *cp;		/* pointer into the line */
   3047     char          *line;	/* the line we're working on */
   3048     struct loadedfile *lf;
   3049 
   3050     lf = loadfile(name, fd);
   3051 
   3052     inLine = FALSE;
   3053     fatals = 0;
   3054 
   3055     if (name == NULL)
   3056 	name = "(stdin)";
   3057 
   3058     Parse_SetInput(name, 0, -1, loadedfile_nextbuf, lf);
   3059     curFile->lf = lf;
   3060 
   3061     do {
   3062 	for (; (line = ParseReadLine()) != NULL; ) {
   3063 	    if (DEBUG(PARSE))
   3064 		fprintf(debug_file, "ParseReadLine (%d): '%s'\n",
   3065 			curFile->lineno, line);
   3066 	    if (*line == '.') {
   3067 		/*
   3068 		 * Lines that begin with the special character may be
   3069 		 * include or undef directives.
   3070 		 * On the other hand they can be suffix rules (.c.o: ...)
   3071 		 * or just dependencies for filenames that start '.'.
   3072 		 */
   3073 		for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
   3074 		    continue;
   3075 		}
   3076 		if (IsInclude(cp, FALSE)) {
   3077 		    ParseDoInclude(cp);
   3078 		    continue;
   3079 		}
   3080 		if (strncmp(cp, "undef", 5) == 0) {
   3081 		    char *cp2;
   3082 		    for (cp += 5; isspace((unsigned char) *cp); cp++)
   3083 			continue;
   3084 		    for (cp2 = cp; !isspace((unsigned char) *cp2) &&
   3085 				   *cp2 != '\0'; cp2++)
   3086 			continue;
   3087 		    *cp2 = '\0';
   3088 		    Var_Delete(cp, VAR_GLOBAL);
   3089 		    continue;
   3090 		} else if (strncmp(cp, "export", 6) == 0) {
   3091 		    for (cp += 6; isspace((unsigned char) *cp); cp++)
   3092 			continue;
   3093 		    Var_Export(cp, TRUE);
   3094 		    continue;
   3095 		} else if (strncmp(cp, "unexport", 8) == 0) {
   3096 		    Var_UnExport(cp);
   3097 		    continue;
   3098 		} else if (strncmp(cp, "info", 4) == 0 ||
   3099 			   strncmp(cp, "error", 5) == 0 ||
   3100 			   strncmp(cp, "warning", 7) == 0) {
   3101 		    if (ParseMessage(cp))
   3102 			continue;
   3103 		}
   3104 	    }
   3105 
   3106 	    if (*line == '\t') {
   3107 		/*
   3108 		 * If a line starts with a tab, it can only hope to be
   3109 		 * a creation command.
   3110 		 */
   3111 		cp = line + 1;
   3112 	      shellCommand:
   3113 		for (; isspace ((unsigned char)*cp); cp++) {
   3114 		    continue;
   3115 		}
   3116 		if (*cp) {
   3117 		    if (!inLine)
   3118 			Parse_Error(PARSE_FATAL,
   3119 				     "Unassociated shell command \"%s\"",
   3120 				     cp);
   3121 		    /*
   3122 		     * So long as it's not a blank line and we're actually
   3123 		     * in a dependency spec, add the command to the list of
   3124 		     * commands of all targets in the dependency spec
   3125 		     */
   3126 		    if (targets) {
   3127 			cp = bmake_strdup(cp);
   3128 			Lst_ForEach(targets, ParseAddCmd, cp);
   3129 #ifdef CLEANUP
   3130 			Lst_Append(targCmds, cp);
   3131 #endif
   3132 		    }
   3133 		}
   3134 		continue;
   3135 	    }
   3136 
   3137 #ifdef SYSVINCLUDE
   3138 	    if (IsSysVInclude(line)) {
   3139 		/*
   3140 		 * It's an S3/S5-style "include".
   3141 		 */
   3142 		ParseTraditionalInclude(line);
   3143 		continue;
   3144 	    }
   3145 #endif
   3146 #ifdef GMAKEEXPORT
   3147 	    if (strncmp(line, "export", 6) == 0 &&
   3148 		isspace((unsigned char) line[6]) &&
   3149 		strchr(line, ':') == NULL) {
   3150 		/*
   3151 		 * It's a Gmake "export".
   3152 		 */
   3153 		ParseGmakeExport(line);
   3154 		continue;
   3155 	    }
   3156 #endif
   3157 	    if (Parse_IsVar(line)) {
   3158 		ParseFinishLine();
   3159 		Parse_DoVar(line, VAR_GLOBAL);
   3160 		continue;
   3161 	    }
   3162 
   3163 #ifndef POSIX
   3164 	    /*
   3165 	     * To make life easier on novices, if the line is indented we
   3166 	     * first make sure the line has a dependency operator in it.
   3167 	     * If it doesn't have an operator and we're in a dependency
   3168 	     * line's script, we assume it's actually a shell command
   3169 	     * and add it to the current list of targets.
   3170 	     */
   3171 	    cp = line;
   3172 	    if (isspace((unsigned char) line[0])) {
   3173 		while (isspace((unsigned char) *cp))
   3174 		    cp++;
   3175 		while (*cp && (ParseIsEscaped(line, cp) ||
   3176 			*cp != ':' && *cp != '!')) {
   3177 		    cp++;
   3178 		}
   3179 		if (*cp == '\0') {
   3180 		    if (inLine) {
   3181 			Parse_Error(PARSE_WARNING,
   3182 				     "Shell command needs a leading tab");
   3183 			goto shellCommand;
   3184 		    }
   3185 		}
   3186 	    }
   3187 #endif
   3188 	    ParseFinishLine();
   3189 
   3190 	    /*
   3191 	     * For some reason - probably to make the parser impossible -
   3192 	     * a ';' can be used to separate commands from dependencies.
   3193 	     * Attempt to avoid ';' inside substitution patterns.
   3194 	     */
   3195 	    {
   3196 		int level = 0;
   3197 
   3198 		for (cp = line; *cp != 0; cp++) {
   3199 		    if (*cp == '\\' && cp[1] != 0) {
   3200 			cp++;
   3201 			continue;
   3202 		    }
   3203 		    if (*cp == '$' &&
   3204 			(cp[1] == '(' || cp[1] == '{')) {
   3205 			level++;
   3206 			continue;
   3207 		    }
   3208 		    if (level > 0) {
   3209 			if (*cp == ')' || *cp == '}') {
   3210 			    level--;
   3211 			    continue;
   3212 			}
   3213 		    } else if (*cp == ';') {
   3214 			break;
   3215 		    }
   3216 		}
   3217 	    }
   3218 	    if (*cp != 0)
   3219 		/* Terminate the dependency list at the ';' */
   3220 		*cp++ = 0;
   3221 	    else
   3222 		cp = NULL;
   3223 
   3224 	    /*
   3225 	     * We now know it's a dependency line so it needs to have all
   3226 	     * variables expanded before being parsed. Tell the variable
   3227 	     * module to complain if some variable is undefined...
   3228 	     */
   3229 	    line = Var_Subst(line, VAR_CMD, VARE_UNDEFERR|VARE_WANTRES);
   3230 
   3231 	    /*
   3232 	     * Need a non-circular list for the target nodes
   3233 	     */
   3234 	    if (targets != NULL)
   3235 		Lst_Free(targets);
   3236 
   3237 	    targets = Lst_Init();
   3238 	    inLine = TRUE;
   3239 
   3240 	    ParseDoDependency(line);
   3241 	    free(line);
   3242 
   3243 	    /* If there were commands after a ';', add them now */
   3244 	    if (cp != NULL) {
   3245 		goto shellCommand;
   3246 	    }
   3247 	}
   3248 	/*
   3249 	 * Reached EOF, but it may be just EOF of an include file...
   3250 	 */
   3251     } while (ParseEOF() == CONTINUE);
   3252 
   3253     if (fatals) {
   3254 	(void)fflush(stdout);
   3255 	(void)fprintf(stderr,
   3256 	    "%s: Fatal errors encountered -- cannot continue",
   3257 	    progname);
   3258 	PrintOnError(NULL, NULL);
   3259 	exit(1);
   3260     }
   3261 }
   3262 
   3263 /*-
   3264  *---------------------------------------------------------------------
   3265  * Parse_Init --
   3266  *	initialize the parsing module
   3267  *
   3268  * Results:
   3269  *	none
   3270  *
   3271  * Side Effects:
   3272  *	the parseIncPath list is initialized...
   3273  *---------------------------------------------------------------------
   3274  */
   3275 void
   3276 Parse_Init(void)
   3277 {
   3278     mainNode = NULL;
   3279     parseIncPath = Lst_Init();
   3280     sysIncPath = Lst_Init();
   3281     defIncPath = Lst_Init();
   3282     Stack_Init(&includes);
   3283 #ifdef CLEANUP
   3284     targCmds = Lst_Init();
   3285 #endif
   3286 }
   3287 
   3288 void
   3289 Parse_End(void)
   3290 {
   3291 #ifdef CLEANUP
   3292     Lst_Destroy(targCmds, free);
   3293     if (targets)
   3294 	Lst_Free(targets);
   3295     Lst_Destroy(defIncPath, Dir_Destroy);
   3296     Lst_Destroy(sysIncPath, Dir_Destroy);
   3297     Lst_Destroy(parseIncPath, Dir_Destroy);
   3298     assert(Stack_IsEmpty(&includes));
   3299     Stack_Done(&includes);
   3300 #endif
   3301 }
   3302 
   3303 
   3304 /*-
   3305  *-----------------------------------------------------------------------
   3306  * Parse_MainName --
   3307  *	Return a Lst of the main target to create for main()'s sake. If
   3308  *	no such target exists, we Punt with an obnoxious error message.
   3309  *
   3310  * Results:
   3311  *	A Lst of the single node to create.
   3312  *
   3313  * Side Effects:
   3314  *	None.
   3315  *
   3316  *-----------------------------------------------------------------------
   3317  */
   3318 Lst
   3319 Parse_MainName(void)
   3320 {
   3321     Lst           mainList;	/* result list */
   3322 
   3323     mainList = Lst_Init();
   3324 
   3325     if (mainNode == NULL) {
   3326 	Punt("no target to make.");
   3327 	/*NOTREACHED*/
   3328     } else if (mainNode->type & OP_DOUBLEDEP) {
   3329 	Lst_Append(mainList, mainNode);
   3330 	Lst_AppendAll(mainList, mainNode->cohorts);
   3331     }
   3332     else
   3333 	Lst_Append(mainList, mainNode);
   3334     Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL);
   3335     return mainList;
   3336 }
   3337 
   3338 /*-
   3339  *-----------------------------------------------------------------------
   3340  * ParseMark --
   3341  *	Add the filename and lineno to the GNode so that we remember
   3342  *	where it was first defined.
   3343  *
   3344  * Side Effects:
   3345  *	None.
   3346  *
   3347  *-----------------------------------------------------------------------
   3348  */
   3349 static void
   3350 ParseMark(GNode *gn)
   3351 {
   3352     gn->fname = curFile->fname;
   3353     gn->lineno = curFile->lineno;
   3354 }
   3355