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