Home | History | Annotate | Line # | Download | only in make
parse.c revision 1.624
      1 /*	$NetBSD: parse.c,v 1.624 2022/01/07 09:28:35 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  * Parsing of makefiles.
     73  *
     74  * Parse_File is the main entry point and controls most of the other
     75  * functions in this module.
     76  *
     77  * Interface:
     78  *	Parse_Init	Initialize the module
     79  *
     80  *	Parse_End	Clean up the module
     81  *
     82  *	Parse_File	Parse a top-level makefile.  Included files are
     83  *			handled by IncludeFile instead.
     84  *
     85  *	Parse_VarAssign
     86  *			Try to parse the given line as a variable assignment.
     87  *			Used by MainParseArgs to determine if an argument is
     88  *			a target or a variable assignment.  Used internally
     89  *			for pretty much the same thing.
     90  *
     91  *	Parse_Error	Report a parse error, a warning or an informational
     92  *			message.
     93  *
     94  *	Parse_MainName	Returns a list of the single main target to create.
     95  */
     96 
     97 #include <sys/types.h>
     98 #include <sys/stat.h>
     99 #include <errno.h>
    100 #include <stdarg.h>
    101 #include <stdint.h>
    102 
    103 #include "make.h"
    104 #include "dir.h"
    105 #include "job.h"
    106 #include "pathnames.h"
    107 
    108 /*	"@(#)parse.c	8.3 (Berkeley) 3/19/94"	*/
    109 MAKE_RCSID("$NetBSD: parse.c,v 1.624 2022/01/07 09:28:35 rillig Exp $");
    110 
    111 /*
    112  * Structure for a file being read ("included file")
    113  */
    114 typedef struct IFile {
    115 	FStr name;		/* absolute or relative to the cwd */
    116 	int lineno;		/* current line number in file */
    117 	int forBodyLineno;	/* start of the .for loop body, 0-based */
    118 	unsigned int cond_depth; /* 'if' nesting when file opened */
    119 	bool depending;		/* state of doing_depend on EOF */
    120 
    121 	Buffer buf;		/* the file's content or the body of the .for
    122 				 * loop; always ends with '\n' */
    123 	char *buf_ptr;		/* next char to be read */
    124 	char *buf_end;		/* buf_end[-1] == '\n' */
    125 
    126 	struct ForLoop *forLoop;
    127 } IFile;
    128 
    129 /*
    130  * Tokens for target attributes
    131  */
    132 typedef enum ParseSpecial {
    133 	SP_ATTRIBUTE,	/* Generic attribute */
    134 	SP_BEGIN,	/* .BEGIN */
    135 	SP_DEFAULT,	/* .DEFAULT */
    136 	SP_DELETE_ON_ERROR, /* .DELETE_ON_ERROR */
    137 	SP_END,		/* .END */
    138 	SP_ERROR,	/* .ERROR */
    139 	SP_IGNORE,	/* .IGNORE */
    140 	SP_INCLUDES,	/* .INCLUDES; not mentioned in the manual page */
    141 	SP_INTERRUPT,	/* .INTERRUPT */
    142 	SP_LIBS,	/* .LIBS; not mentioned in the manual page */
    143 	SP_MAIN,	/* .MAIN and no user-specified targets to make */
    144 	SP_META,	/* .META */
    145 	SP_MFLAGS,	/* .MFLAGS or .MAKEFLAGS */
    146 	SP_NOMETA,	/* .NOMETA */
    147 	SP_NOMETA_CMP,	/* .NOMETA_CMP */
    148 	SP_NOPATH,	/* .NOPATH */
    149 	SP_NOT,		/* Not special */
    150 	SP_NOTPARALLEL,	/* .NOTPARALLEL or .NO_PARALLEL */
    151 	SP_NULL,	/* .NULL; not mentioned in the manual page */
    152 	SP_OBJDIR,	/* .OBJDIR */
    153 	SP_ORDER,	/* .ORDER */
    154 	SP_PARALLEL,	/* .PARALLEL; not mentioned in the manual page */
    155 	SP_PATH,	/* .PATH or .PATH.suffix */
    156 	SP_PHONY,	/* .PHONY */
    157 #ifdef POSIX
    158 	SP_POSIX,	/* .POSIX; not mentioned in the manual page */
    159 #endif
    160 	SP_PRECIOUS,	/* .PRECIOUS */
    161 	SP_SHELL,	/* .SHELL */
    162 	SP_SILENT,	/* .SILENT */
    163 	SP_SINGLESHELL,	/* .SINGLESHELL; not mentioned in the manual page */
    164 	SP_STALE,	/* .STALE */
    165 	SP_SUFFIXES,	/* .SUFFIXES */
    166 	SP_WAIT		/* .WAIT */
    167 } ParseSpecial;
    168 
    169 typedef List SearchPathList;
    170 typedef ListNode SearchPathListNode;
    171 
    172 /*
    173  * The main target to create. This is the first target defined in any of the
    174  * makefiles.
    175  */
    176 static GNode *mainNode;
    177 
    178 /*
    179  * During parsing, the targets from the left-hand side of the currently
    180  * active dependency line, or NULL if the current line does not belong to a
    181  * dependency line, for example because it is a variable assignment.
    182  *
    183  * See unit-tests/deptgt.mk, keyword "parse.c:targets".
    184  */
    185 static GNodeList *targets;
    186 
    187 #ifdef CLEANUP
    188 /*
    189  * All shell commands for all targets, in no particular order and possibly
    190  * with duplicates.  Kept in a separate list since the commands from .USE or
    191  * .USEBEFORE nodes are shared with other GNodes, thereby giving up the
    192  * easily understandable ownership over the allocated strings.
    193  */
    194 static StringList targCmds = LST_INIT;
    195 #endif
    196 
    197 /*
    198  * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
    199  * seen, then set to each successive source on the line.
    200  */
    201 static GNode *order_pred;
    202 
    203 /* number of fatal errors */
    204 static int parseErrors = 0;
    205 
    206 /*
    207  * The include chain of makefiles.  At index 0 is the top-level makefile from
    208  * the command line, followed by the included files or .for loops, up to and
    209  * including the current file.
    210  *
    211  * See PrintStackTrace for how to interpret the data.
    212  */
    213 static Vector /* of IFile */ includes;
    214 
    215 SearchPath *parseIncPath;	/* directories for "..." includes */
    216 SearchPath *sysIncPath;		/* directories for <...> includes */
    217 SearchPath *defSysIncPath;	/* default for sysIncPath */
    218 
    219 /*
    220  * The parseKeywords table is searched using binary search when deciding
    221  * if a target or source is special. The 'spec' field is the ParseSpecial
    222  * type of the keyword (SP_NOT if the keyword isn't special as a target) while
    223  * the 'op' field is the operator to apply to the list of targets if the
    224  * keyword is used as a source ("0" if the keyword isn't special as a source)
    225  */
    226 static const struct {
    227 	const char name[17];
    228 	ParseSpecial special;	/* when used as a target */
    229 	GNodeType targetAttr;	/* when used as a source */
    230 } parseKeywords[] = {
    231     { ".BEGIN",		SP_BEGIN,	OP_NONE },
    232     { ".DEFAULT",	SP_DEFAULT,	OP_NONE },
    233     { ".DELETE_ON_ERROR", SP_DELETE_ON_ERROR, OP_NONE },
    234     { ".END",		SP_END,		OP_NONE },
    235     { ".ERROR",		SP_ERROR,	OP_NONE },
    236     { ".EXEC",		SP_ATTRIBUTE,	OP_EXEC },
    237     { ".IGNORE",	SP_IGNORE,	OP_IGNORE },
    238     { ".INCLUDES",	SP_INCLUDES,	OP_NONE },
    239     { ".INTERRUPT",	SP_INTERRUPT,	OP_NONE },
    240     { ".INVISIBLE",	SP_ATTRIBUTE,	OP_INVISIBLE },
    241     { ".JOIN",		SP_ATTRIBUTE,	OP_JOIN },
    242     { ".LIBS",		SP_LIBS,	OP_NONE },
    243     { ".MADE",		SP_ATTRIBUTE,	OP_MADE },
    244     { ".MAIN",		SP_MAIN,	OP_NONE },
    245     { ".MAKE",		SP_ATTRIBUTE,	OP_MAKE },
    246     { ".MAKEFLAGS",	SP_MFLAGS,	OP_NONE },
    247     { ".META",		SP_META,	OP_META },
    248     { ".MFLAGS",	SP_MFLAGS,	OP_NONE },
    249     { ".NOMETA",	SP_NOMETA,	OP_NOMETA },
    250     { ".NOMETA_CMP",	SP_NOMETA_CMP,	OP_NOMETA_CMP },
    251     { ".NOPATH",	SP_NOPATH,	OP_NOPATH },
    252     { ".NOTMAIN",	SP_ATTRIBUTE,	OP_NOTMAIN },
    253     { ".NOTPARALLEL",	SP_NOTPARALLEL,	OP_NONE },
    254     { ".NO_PARALLEL",	SP_NOTPARALLEL,	OP_NONE },
    255     { ".NULL",		SP_NULL,	OP_NONE },
    256     { ".OBJDIR",	SP_OBJDIR,	OP_NONE },
    257     { ".OPTIONAL",	SP_ATTRIBUTE,	OP_OPTIONAL },
    258     { ".ORDER",		SP_ORDER,	OP_NONE },
    259     { ".PARALLEL",	SP_PARALLEL,	OP_NONE },
    260     { ".PATH",		SP_PATH,	OP_NONE },
    261     { ".PHONY",		SP_PHONY,	OP_PHONY },
    262 #ifdef POSIX
    263     { ".POSIX",		SP_POSIX,	OP_NONE },
    264 #endif
    265     { ".PRECIOUS",	SP_PRECIOUS,	OP_PRECIOUS },
    266     { ".RECURSIVE",	SP_ATTRIBUTE,	OP_MAKE },
    267     { ".SHELL",		SP_SHELL,	OP_NONE },
    268     { ".SILENT",	SP_SILENT,	OP_SILENT },
    269     { ".SINGLESHELL",	SP_SINGLESHELL,	OP_NONE },
    270     { ".STALE",		SP_STALE,	OP_NONE },
    271     { ".SUFFIXES",	SP_SUFFIXES,	OP_NONE },
    272     { ".USE",		SP_ATTRIBUTE,	OP_USE },
    273     { ".USEBEFORE",	SP_ATTRIBUTE,	OP_USEBEFORE },
    274     { ".WAIT",		SP_WAIT,	OP_NONE },
    275 };
    276 
    277 
    278 static IFile *
    279 GetInclude(size_t i)
    280 {
    281 	return Vector_Get(&includes, i);
    282 }
    283 
    284 /* The file that is currently being read. */
    285 static IFile *
    286 CurFile(void)
    287 {
    288 	return GetInclude(includes.len - 1);
    289 }
    290 
    291 static Buffer
    292 loadfile(const char *path, int fd)
    293 {
    294 	ssize_t n;
    295 	Buffer buf;
    296 	size_t bufSize;
    297 	struct stat st;
    298 
    299 	bufSize = fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
    300 		  st.st_size >= 1 && st.st_size <= 0x3fffffff
    301 	    ? (size_t)st.st_size : 1024;
    302 	Buf_InitSize(&buf, bufSize);
    303 
    304 	for (;;) {
    305 		if (buf.len == buf.cap) {
    306 			if (buf.cap > 0x1fffffff) {
    307 				errno = EFBIG;
    308 				Error("%s: file too large", path);
    309 				exit(2); /* Not 1 so -q can distinguish error */
    310 			}
    311 			Buf_Expand(&buf);
    312 		}
    313 		assert(buf.len < buf.cap);
    314 		n = read(fd, buf.data + buf.len, buf.cap - buf.len);
    315 		if (n < 0) {
    316 			Error("%s: read error: %s", path, strerror(errno));
    317 			exit(2);	/* Not 1 so -q can distinguish error */
    318 		}
    319 		if (n == 0)
    320 			break;
    321 
    322 		buf.len += (size_t)n;
    323 	}
    324 	assert(buf.len <= buf.cap);
    325 
    326 	if (!Buf_EndsWith(&buf, '\n'))
    327 		Buf_AddByte(&buf, '\n');
    328 
    329 	return buf;
    330 }
    331 
    332 static void
    333 PrintStackTrace(void)
    334 {
    335 	const IFile *entries;
    336 	size_t i, n;
    337 
    338 	if (!DEBUG(PARSE))
    339 		return;
    340 
    341 	entries = GetInclude(0);
    342 	n = includes.len;
    343 	if (n == 0)
    344 		return;
    345 	n--;			/* This entry is already in the diagnostic. */
    346 
    347 	/*
    348 	 * For the IFiles with forLoop, lineno is the number of completely
    349 	 * parsed lines, which is right after the corresponding .endfor.  The
    350 	 * intuitive line number comes from first_lineno instead, which
    351 	 * points at the start of the .for loop.
    352 	 *
    353 	 * To make the stack trace intuitive, the entry below each chain of
    354 	 * .for loop entries must be ignored completely since neither its
    355 	 * lineno nor its first_lineno is useful.  Instead, the topmost of
    356 	 * each chain of .for loop entries needs to be printed twice, once
    357 	 * with its first_lineno and once with its lineno.
    358 	 */
    359 
    360 	for (i = n; i-- > 0;) {
    361 		const IFile *entry = entries + i;
    362 		const char *fname = entry->name.str;
    363 		char dirbuf[MAXPATHLEN + 1];
    364 
    365 		if (fname[0] != '/' && strcmp(fname, "(stdin)") != 0)
    366 			fname = realpath(fname, dirbuf);
    367 
    368 		if (entries[i + 1 < n ? i + 1 : i].forLoop == NULL)
    369 			debug_printf("\tin .include from %s:%d\n",
    370 			    fname, entry->lineno);
    371 		if (entry->forLoop != NULL)
    372 			debug_printf("\tin .for loop from %s:%d\n",
    373 			    fname, entry->forBodyLineno - 1 + 1);
    374 	}
    375 }
    376 
    377 /* Check if the current character is escaped on the current line. */
    378 static bool
    379 IsEscaped(const char *line, const char *p)
    380 {
    381 	bool active = false;
    382 	while (p > line && *--p == '\\')
    383 		active = !active;
    384 	return active;
    385 }
    386 
    387 /*
    388  * Add the filename and lineno to the GNode so that we remember where it
    389  * was first defined.
    390  */
    391 static void
    392 RememberLocation(GNode *gn)
    393 {
    394 	IFile *curFile = CurFile();
    395 	gn->fname = Str_Intern(curFile->name.str);
    396 	gn->lineno = curFile->lineno;
    397 }
    398 
    399 /*
    400  * Look in the table of keywords for one matching the given string.
    401  * Return the index of the keyword, or -1 if it isn't there.
    402  */
    403 static int
    404 FindKeyword(const char *str)
    405 {
    406 	int start = 0;
    407 	int end = sizeof parseKeywords / sizeof parseKeywords[0] - 1;
    408 
    409 	while (start <= end) {
    410 		int curr = start + (end - start) / 2;
    411 		int diff = strcmp(str, parseKeywords[curr].name);
    412 
    413 		if (diff == 0)
    414 			return curr;
    415 		if (diff < 0)
    416 			end = curr - 1;
    417 		else
    418 			start = curr + 1;
    419 	}
    420 
    421 	return -1;
    422 }
    423 
    424 static void
    425 PrintLocation(FILE *f, const char *fname, size_t lineno)
    426 {
    427 	char dirbuf[MAXPATHLEN + 1];
    428 	FStr dir, base;
    429 
    430 	if (fname[0] == '/' || strcmp(fname, "(stdin)") == 0) {
    431 		(void)fprintf(f, "\"%s\" line %u: ", fname, (unsigned)lineno);
    432 		return;
    433 	}
    434 
    435 	dir = Var_Value(SCOPE_GLOBAL, ".PARSEDIR");
    436 	if (dir.str == NULL)
    437 		dir.str = ".";
    438 	if (dir.str[0] != '/')
    439 		dir.str = realpath(dir.str, dirbuf);
    440 
    441 	base = Var_Value(SCOPE_GLOBAL, ".PARSEFILE");
    442 	if (base.str == NULL)
    443 		base.str = str_basename(fname);
    444 
    445 	(void)fprintf(f, "\"%s/%s\" line %u: ",
    446 	    dir.str, base.str, (unsigned)lineno);
    447 
    448 	FStr_Done(&base);
    449 	FStr_Done(&dir);
    450 }
    451 
    452 static void
    453 ParseVErrorInternal(FILE *f, const char *fname, size_t lineno,
    454 		    ParseErrorLevel type, const char *fmt, va_list ap)
    455 {
    456 	static bool fatal_warning_error_printed = false;
    457 
    458 	(void)fprintf(f, "%s: ", progname);
    459 
    460 	if (fname != NULL)
    461 		PrintLocation(f, fname, lineno);
    462 	if (type == PARSE_WARNING)
    463 		(void)fprintf(f, "warning: ");
    464 	(void)vfprintf(f, fmt, ap);
    465 	(void)fprintf(f, "\n");
    466 	(void)fflush(f);
    467 
    468 	if (type == PARSE_FATAL)
    469 		parseErrors++;
    470 	if (type == PARSE_WARNING && opts.parseWarnFatal) {
    471 		if (!fatal_warning_error_printed) {
    472 			Error("parsing warnings being treated as errors");
    473 			fatal_warning_error_printed = true;
    474 		}
    475 		parseErrors++;
    476 	}
    477 
    478 	PrintStackTrace();
    479 }
    480 
    481 static void
    482 ParseErrorInternal(const char *fname, size_t lineno,
    483 		   ParseErrorLevel type, const char *fmt, ...)
    484 {
    485 	va_list ap;
    486 
    487 	(void)fflush(stdout);
    488 	va_start(ap, fmt);
    489 	ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
    490 	va_end(ap);
    491 
    492 	if (opts.debug_file != stdout && opts.debug_file != stderr) {
    493 		va_start(ap, fmt);
    494 		ParseVErrorInternal(opts.debug_file, fname, lineno, type,
    495 		    fmt, ap);
    496 		va_end(ap);
    497 	}
    498 }
    499 
    500 /*
    501  * Print a parse error message, including location information.
    502  *
    503  * If the level is PARSE_FATAL, continue parsing until the end of the
    504  * current top-level makefile, then exit (see Parse_File).
    505  *
    506  * Fmt is given without a trailing newline.
    507  */
    508 void
    509 Parse_Error(ParseErrorLevel type, const char *fmt, ...)
    510 {
    511 	va_list ap;
    512 	const char *fname;
    513 	size_t lineno;
    514 
    515 	if (includes.len == 0) {
    516 		fname = NULL;
    517 		lineno = 0;
    518 	} else {
    519 		IFile *curFile = CurFile();
    520 		fname = curFile->name.str;
    521 		lineno = (size_t)curFile->lineno;
    522 	}
    523 
    524 	va_start(ap, fmt);
    525 	(void)fflush(stdout);
    526 	ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
    527 	va_end(ap);
    528 
    529 	if (opts.debug_file != stdout && opts.debug_file != stderr) {
    530 		va_start(ap, fmt);
    531 		ParseVErrorInternal(opts.debug_file, fname, lineno, type,
    532 		    fmt, ap);
    533 		va_end(ap);
    534 	}
    535 }
    536 
    537 
    538 /*
    539  * Handle an .info, .warning or .error directive.  For an .error directive,
    540  * exit immediately.
    541  */
    542 static void
    543 HandleMessage(ParseErrorLevel level, const char *levelName, const char *umsg)
    544 {
    545 	char *xmsg;
    546 
    547 	if (umsg[0] == '\0') {
    548 		Parse_Error(PARSE_FATAL, "Missing argument for \".%s\"",
    549 		    levelName);
    550 		return;
    551 	}
    552 
    553 	(void)Var_Subst(umsg, SCOPE_CMDLINE, VARE_WANTRES, &xmsg);
    554 	/* TODO: handle errors */
    555 
    556 	Parse_Error(level, "%s", xmsg);
    557 	free(xmsg);
    558 
    559 	if (level == PARSE_FATAL) {
    560 		PrintOnError(NULL, NULL);
    561 		exit(1);
    562 	}
    563 }
    564 
    565 /*
    566  * Add the child to the parent's children.
    567  *
    568  * Additionally, add the parent to the child's parents, but only if the
    569  * target is not special.  An example for such a special target is .END,
    570  * which does not need to be informed once the child target has been made.
    571  */
    572 static void
    573 LinkSource(GNode *pgn, GNode *cgn, bool isSpecial)
    574 {
    575 	if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(&pgn->cohorts))
    576 		pgn = pgn->cohorts.last->datum;
    577 
    578 	Lst_Append(&pgn->children, cgn);
    579 	pgn->unmade++;
    580 
    581 	/* Special targets like .END don't need any children. */
    582 	if (!isSpecial)
    583 		Lst_Append(&cgn->parents, pgn);
    584 
    585 	if (DEBUG(PARSE)) {
    586 		debug_printf("# LinkSource: added child %s - %s\n",
    587 		    pgn->name, cgn->name);
    588 		Targ_PrintNode(pgn, 0);
    589 		Targ_PrintNode(cgn, 0);
    590 	}
    591 }
    592 
    593 /* Add the node to each target from the current dependency group. */
    594 static void
    595 LinkToTargets(GNode *gn, bool isSpecial)
    596 {
    597 	GNodeListNode *ln;
    598 
    599 	for (ln = targets->first; ln != NULL; ln = ln->next)
    600 		LinkSource(ln->datum, gn, isSpecial);
    601 }
    602 
    603 static bool
    604 TryApplyDependencyOperator(GNode *gn, GNodeType op)
    605 {
    606 	/*
    607 	 * If the node occurred on the left-hand side of a dependency and the
    608 	 * operator also defines a dependency, they must match.
    609 	 */
    610 	if ((op & OP_OPMASK) && (gn->type & OP_OPMASK) &&
    611 	    ((op & OP_OPMASK) != (gn->type & OP_OPMASK))) {
    612 		Parse_Error(PARSE_FATAL, "Inconsistent operator for %s",
    613 		    gn->name);
    614 		return false;
    615 	}
    616 
    617 	if (op == OP_DOUBLEDEP && (gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
    618 		/*
    619 		 * If the node was of the left-hand side of a '::' operator,
    620 		 * we need to create a new instance of it for the children
    621 		 * and commands on this dependency line since each of these
    622 		 * dependency groups has its own attributes and commands,
    623 		 * separate from the others.
    624 		 *
    625 		 * The new instance is placed on the 'cohorts' list of the
    626 		 * initial one (note the initial one is not on its own
    627 		 * cohorts list) and the new instance is linked to all
    628 		 * parents of the initial instance.
    629 		 */
    630 		GNode *cohort;
    631 
    632 		/*
    633 		 * Propagate copied bits to the initial node.  They'll be
    634 		 * propagated back to the rest of the cohorts later.
    635 		 */
    636 		gn->type |= op & ~OP_OPMASK;
    637 
    638 		cohort = Targ_NewInternalNode(gn->name);
    639 		if (doing_depend)
    640 			RememberLocation(cohort);
    641 		/*
    642 		 * Make the cohort invisible as well to avoid duplicating it
    643 		 * into other variables. True, parents of this target won't
    644 		 * tend to do anything with their local variables, but better
    645 		 * safe than sorry.
    646 		 *
    647 		 * (I think this is pointless now, since the relevant list
    648 		 * traversals will no longer see this node anyway. -mycroft)
    649 		 */
    650 		cohort->type = op | OP_INVISIBLE;
    651 		Lst_Append(&gn->cohorts, cohort);
    652 		cohort->centurion = gn;
    653 		gn->unmade_cohorts++;
    654 		snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
    655 		    (unsigned int)gn->unmade_cohorts % 1000000);
    656 	} else {
    657 		/*
    658 		 * We don't want to nuke any previous flags (whatever they
    659 		 * were) so we just OR the new operator into the old.
    660 		 */
    661 		gn->type |= op;
    662 	}
    663 
    664 	return true;
    665 }
    666 
    667 static void
    668 ApplyDependencyOperator(GNodeType op)
    669 {
    670 	GNodeListNode *ln;
    671 
    672 	for (ln = targets->first; ln != NULL; ln = ln->next)
    673 		if (!TryApplyDependencyOperator(ln->datum, op))
    674 			break;
    675 }
    676 
    677 /*
    678  * We add a .WAIT node in the dependency list. After any dynamic dependencies
    679  * (and filename globbing) have happened, it is given a dependency on each
    680  * previous child, back until the previous .WAIT node. The next child won't
    681  * be scheduled until the .WAIT node is built.
    682  *
    683  * We give each .WAIT node a unique name (mainly for diagnostics).
    684  */
    685 static void
    686 ApplyDependencySourceWait(bool isSpecial)
    687 {
    688 	static int wait_number = 0;
    689 	char wait_src[16];
    690 	GNode *gn;
    691 
    692 	snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
    693 	gn = Targ_NewInternalNode(wait_src);
    694 	if (doing_depend)
    695 		RememberLocation(gn);
    696 	gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
    697 	LinkToTargets(gn, isSpecial);
    698 
    699 }
    700 
    701 static bool
    702 ApplyDependencySourceKeyword(const char *src, ParseSpecial special)
    703 {
    704 	int keywd;
    705 	GNodeType targetAttr;
    706 
    707 	if (*src != '.' || !ch_isupper(src[1]))
    708 		return false;
    709 
    710 	keywd = FindKeyword(src);
    711 	if (keywd == -1)
    712 		return false;
    713 
    714 	targetAttr = parseKeywords[keywd].targetAttr;
    715 	if (targetAttr != OP_NONE) {
    716 		ApplyDependencyOperator(targetAttr);
    717 		return true;
    718 	}
    719 	if (parseKeywords[keywd].special == SP_WAIT) {
    720 		ApplyDependencySourceWait(special != SP_NOT);
    721 		return true;
    722 	}
    723 	return false;
    724 }
    725 
    726 static void
    727 ApplyDependencySourceMain(const char *src)
    728 {
    729 	/*
    730 	 * In a line like ".MAIN: source1 source2", add all sources to the
    731 	 * list of things to create, but only if the user didn't specify a
    732 	 * target on the command line and .MAIN occurs for the first time.
    733 	 *
    734 	 * See HandleDependencyTargetSpecial, branch SP_MAIN.
    735 	 * See unit-tests/cond-func-make-main.mk.
    736 	 */
    737 	Lst_Append(&opts.create, bmake_strdup(src));
    738 	/*
    739 	 * Add the name to the .TARGETS variable as well, so the user can
    740 	 * employ that, if desired.
    741 	 */
    742 	Global_Append(".TARGETS", src);
    743 }
    744 
    745 static void
    746 ApplyDependencySourceOrder(const char *src)
    747 {
    748 	GNode *gn;
    749 	/*
    750 	 * Create proper predecessor/successor links between the previous
    751 	 * source and the current one.
    752 	 */
    753 	gn = Targ_GetNode(src);
    754 	if (doing_depend)
    755 		RememberLocation(gn);
    756 	if (order_pred != NULL) {
    757 		Lst_Append(&order_pred->order_succ, gn);
    758 		Lst_Append(&gn->order_pred, order_pred);
    759 		if (DEBUG(PARSE)) {
    760 			debug_printf(
    761 			    "# .ORDER forces '%s' to be made before '%s'\n",
    762 			    order_pred->name, gn->name);
    763 			Targ_PrintNode(order_pred, 0);
    764 			Targ_PrintNode(gn, 0);
    765 		}
    766 	}
    767 	/*
    768 	 * The current source now becomes the predecessor for the next one.
    769 	 */
    770 	order_pred = gn;
    771 }
    772 
    773 static void
    774 ApplyDependencySourceOther(const char *src, GNodeType targetAttr,
    775 			   ParseSpecial special)
    776 {
    777 	GNode *gn;
    778 
    779 	/*
    780 	 * The source is not an attribute, so find/create a node for it.
    781 	 * After that, apply any operator to it from a special target or
    782 	 * link it to its parents, as appropriate.
    783 	 *
    784 	 * In the case of a source that was the object of a '::' operator,
    785 	 * the attribute is applied to all of its instances (as kept in
    786 	 * the 'cohorts' list of the node) or all the cohorts are linked
    787 	 * to all the targets.
    788 	 */
    789 
    790 	/* Find/create the 'src' node and attach to all targets */
    791 	gn = Targ_GetNode(src);
    792 	if (doing_depend)
    793 		RememberLocation(gn);
    794 	if (targetAttr != OP_NONE)
    795 		gn->type |= targetAttr;
    796 	else
    797 		LinkToTargets(gn, special != SP_NOT);
    798 }
    799 
    800 /*
    801  * Given the name of a source in a dependency line, figure out if it is an
    802  * attribute (such as .SILENT) and if so, apply it to all targets. Otherwise
    803  * decide if there is some attribute which should be applied *to* the source
    804  * because of some special target (such as .PHONY) and apply it if so.
    805  * Otherwise, make the source a child of the targets.
    806  */
    807 static void
    808 ApplyDependencySource(GNodeType targetAttr, const char *src,
    809 		      ParseSpecial special)
    810 {
    811 	if (ApplyDependencySourceKeyword(src, special))
    812 		return;
    813 
    814 	if (special == SP_MAIN)
    815 		ApplyDependencySourceMain(src);
    816 	else if (special == SP_ORDER)
    817 		ApplyDependencySourceOrder(src);
    818 	else
    819 		ApplyDependencySourceOther(src, targetAttr, special);
    820 }
    821 
    822 /*
    823  * If we have yet to decide on a main target to make, in the absence of any
    824  * user input, we want the first target on the first dependency line that is
    825  * actually a real target (i.e. isn't a .USE or .EXEC rule) to be made.
    826  */
    827 static void
    828 FindMainTarget(void)
    829 {
    830 	GNodeListNode *ln;
    831 
    832 	if (mainNode != NULL)
    833 		return;
    834 
    835 	for (ln = targets->first; ln != NULL; ln = ln->next) {
    836 		GNode *gn = ln->datum;
    837 		if (GNode_IsMainCandidate(gn)) {
    838 			DEBUG1(MAKE, "Setting main node to \"%s\"\n", gn->name);
    839 			mainNode = gn;
    840 			Targ_SetMain(gn);
    841 			return;
    842 		}
    843 	}
    844 }
    845 
    846 static void
    847 InvalidLineType(const char *lstart)
    848 {
    849 	if ((strncmp(lstart, "<<<<<<", 6) == 0) ||
    850 	    (strncmp(lstart, "======", 6) == 0) ||
    851 	    (strncmp(lstart, ">>>>>>", 6) == 0))
    852 		Parse_Error(PARSE_FATAL,
    853 		    "Makefile appears to contain unresolved CVS/RCS/??? merge conflicts");
    854 	else if (lstart[0] == '.') {
    855 		const char *dirstart = lstart + 1;
    856 		const char *dirend;
    857 		cpp_skip_whitespace(&dirstart);
    858 		dirend = dirstart;
    859 		while (ch_isalnum(*dirend) || *dirend == '-')
    860 			dirend++;
    861 		Parse_Error(PARSE_FATAL, "Unknown directive \"%.*s\"",
    862 		    (int)(dirend - dirstart), dirstart);
    863 	} else
    864 		Parse_Error(PARSE_FATAL, "Invalid line type");
    865 }
    866 
    867 static void
    868 ParseDependencyTargetWord(char **pp, const char *lstart)
    869 {
    870 	const char *cp = *pp;
    871 
    872 	while (*cp != '\0') {
    873 		if ((ch_isspace(*cp) || *cp == '!' || *cp == ':' ||
    874 		     *cp == '(') &&
    875 		    !IsEscaped(lstart, cp))
    876 			break;
    877 
    878 		if (*cp == '$') {
    879 			/*
    880 			 * Must be a dynamic source (would have been expanded
    881 			 * otherwise).
    882 			 *
    883 			 * There should be no errors in this, as they would
    884 			 * have been discovered in the initial Var_Subst and
    885 			 * we wouldn't be here.
    886 			 */
    887 			FStr val;
    888 
    889 			(void)Var_Parse(&cp, SCOPE_CMDLINE,
    890 			    VARE_PARSE_ONLY, &val);
    891 			FStr_Done(&val);
    892 		} else
    893 			cp++;
    894 	}
    895 
    896 	*pp += cp - *pp;
    897 }
    898 
    899 /*
    900  * Handle special targets like .PATH, .DEFAULT, .BEGIN, .ORDER.
    901  *
    902  * See the tests deptgt-*.mk.
    903  */
    904 static void
    905 HandleDependencyTargetSpecial(const char *targetName,
    906 			      ParseSpecial *inout_special,
    907 			      SearchPathList **inout_paths)
    908 {
    909 	switch (*inout_special) {
    910 	case SP_PATH:
    911 		if (*inout_paths == NULL)
    912 			*inout_paths = Lst_New();
    913 		Lst_Append(*inout_paths, &dirSearchPath);
    914 		break;
    915 	case SP_MAIN:
    916 		/*
    917 		 * Allow targets from the command line to override the
    918 		 * .MAIN node.
    919 		 */
    920 		if (!Lst_IsEmpty(&opts.create))
    921 			*inout_special = SP_NOT;
    922 		break;
    923 	case SP_BEGIN:
    924 	case SP_END:
    925 	case SP_STALE:
    926 	case SP_ERROR:
    927 	case SP_INTERRUPT: {
    928 		GNode *gn = Targ_GetNode(targetName);
    929 		if (doing_depend)
    930 			RememberLocation(gn);
    931 		gn->type |= OP_NOTMAIN | OP_SPECIAL;
    932 		Lst_Append(targets, gn);
    933 		break;
    934 	}
    935 	case SP_DEFAULT: {
    936 		/*
    937 		 * Need to create a node to hang commands on, but we don't
    938 		 * want it in the graph, nor do we want it to be the Main
    939 		 * Target. We claim the node is a transformation rule to make
    940 		 * life easier later, when we'll use Make_HandleUse to
    941 		 * actually apply the .DEFAULT commands.
    942 		 */
    943 		GNode *gn = GNode_New(".DEFAULT");
    944 		gn->type |= OP_NOTMAIN | OP_TRANSFORM;
    945 		Lst_Append(targets, gn);
    946 		defaultNode = gn;
    947 		break;
    948 	}
    949 	case SP_DELETE_ON_ERROR:
    950 		deleteOnError = true;
    951 		break;
    952 	case SP_NOTPARALLEL:
    953 		opts.maxJobs = 1;
    954 		break;
    955 	case SP_SINGLESHELL:
    956 		opts.compatMake = true;
    957 		break;
    958 	case SP_ORDER:
    959 		order_pred = NULL;
    960 		break;
    961 	default:
    962 		break;
    963 	}
    964 }
    965 
    966 static bool
    967 HandleDependencyTargetPath(const char *suffixName,
    968 			   SearchPathList **inout_paths)
    969 {
    970 	SearchPath *path;
    971 
    972 	path = Suff_GetPath(suffixName);
    973 	if (path == NULL) {
    974 		Parse_Error(PARSE_FATAL,
    975 		    "Suffix '%s' not defined (yet)", suffixName);
    976 		return false;
    977 	}
    978 
    979 	if (*inout_paths == NULL)
    980 		*inout_paths = Lst_New();
    981 	Lst_Append(*inout_paths, path);
    982 
    983 	return true;
    984 }
    985 
    986 /*
    987  * See if it's a special target and if so set inout_special to match it.
    988  */
    989 static bool
    990 HandleDependencyTarget(const char *targetName,
    991 		       ParseSpecial *inout_special,
    992 		       GNodeType *inout_targetAttr,
    993 		       SearchPathList **inout_paths)
    994 {
    995 	int keywd;
    996 
    997 	if (!(targetName[0] == '.' && ch_isupper(targetName[1])))
    998 		return true;
    999 
   1000 	/*
   1001 	 * See if the target is a special target that must have it
   1002 	 * or its sources handled specially.
   1003 	 */
   1004 	keywd = FindKeyword(targetName);
   1005 	if (keywd != -1) {
   1006 		if (*inout_special == SP_PATH &&
   1007 		    parseKeywords[keywd].special != SP_PATH) {
   1008 			Parse_Error(PARSE_FATAL, "Mismatched special targets");
   1009 			return false;
   1010 		}
   1011 
   1012 		*inout_special = parseKeywords[keywd].special;
   1013 		*inout_targetAttr = parseKeywords[keywd].targetAttr;
   1014 
   1015 		HandleDependencyTargetSpecial(targetName, inout_special,
   1016 		    inout_paths);
   1017 
   1018 	} else if (strncmp(targetName, ".PATH", 5) == 0) {
   1019 		*inout_special = SP_PATH;
   1020 		if (!HandleDependencyTargetPath(targetName + 5, inout_paths))
   1021 			return false;
   1022 	}
   1023 	return true;
   1024 }
   1025 
   1026 static void
   1027 HandleDependencyTargetMundane(char *targetName)
   1028 {
   1029 	StringList targetNames = LST_INIT;
   1030 
   1031 	if (Dir_HasWildcards(targetName)) {
   1032 		SearchPath *emptyPath = SearchPath_New();
   1033 		SearchPath_Expand(emptyPath, targetName, &targetNames);
   1034 		SearchPath_Free(emptyPath);
   1035 	} else
   1036 		Lst_Append(&targetNames, targetName);
   1037 
   1038 	while (!Lst_IsEmpty(&targetNames)) {
   1039 		char *targName = Lst_Dequeue(&targetNames);
   1040 		GNode *gn = Suff_IsTransform(targName)
   1041 		    ? Suff_AddTransform(targName)
   1042 		    : Targ_GetNode(targName);
   1043 		if (doing_depend)
   1044 			RememberLocation(gn);
   1045 
   1046 		Lst_Append(targets, gn);
   1047 	}
   1048 }
   1049 
   1050 static void
   1051 SkipExtraTargets(char **pp, const char *lstart)
   1052 {
   1053 	bool warning = false;
   1054 	const char *cp = *pp;
   1055 
   1056 	while (*cp != '\0') {
   1057 		if (!IsEscaped(lstart, cp) && (*cp == '!' || *cp == ':'))
   1058 			break;
   1059 		if (IsEscaped(lstart, cp) || (*cp != ' ' && *cp != '\t'))
   1060 			warning = true;
   1061 		cp++;
   1062 	}
   1063 	if (warning)
   1064 		Parse_Error(PARSE_WARNING, "Extra target ignored");
   1065 
   1066 	*pp += cp - *pp;
   1067 }
   1068 
   1069 static void
   1070 ParseDependencyCheckSpecial(ParseSpecial special)
   1071 {
   1072 	switch (special) {
   1073 	case SP_DEFAULT:
   1074 	case SP_STALE:
   1075 	case SP_BEGIN:
   1076 	case SP_END:
   1077 	case SP_ERROR:
   1078 	case SP_INTERRUPT:
   1079 		/*
   1080 		 * These create nodes on which to hang commands, so targets
   1081 		 * shouldn't be empty.
   1082 		 */
   1083 	case SP_NOT:
   1084 		/*
   1085 		 * Nothing special here -- targets can be empty if it wants.
   1086 		 */
   1087 		break;
   1088 	default:
   1089 		Parse_Error(PARSE_WARNING,
   1090 		    "Special and mundane targets don't mix. "
   1091 		    "Mundane ones ignored");
   1092 		break;
   1093 	}
   1094 }
   1095 
   1096 /*
   1097  * In a dependency line like 'targets: sources' or 'targets! sources', parse
   1098  * the operator ':', '::' or '!' from between the targets and the sources.
   1099  */
   1100 static GNodeType
   1101 ParseDependencyOp(char **pp)
   1102 {
   1103 	if (**pp == '!')
   1104 		return (*pp)++, OP_FORCE;
   1105 	if ((*pp)[1] == ':')
   1106 		return (*pp) += 2, OP_DOUBLEDEP;
   1107 	else
   1108 		return (*pp)++, OP_DEPENDS;
   1109 }
   1110 
   1111 static void
   1112 ClearPaths(SearchPathList *paths)
   1113 {
   1114 	if (paths != NULL) {
   1115 		SearchPathListNode *ln;
   1116 		for (ln = paths->first; ln != NULL; ln = ln->next)
   1117 			SearchPath_Clear(ln->datum);
   1118 	}
   1119 
   1120 	Dir_SetPATH();
   1121 }
   1122 
   1123 /*
   1124  * Several special targets take different actions if present with no
   1125  * sources:
   1126  *	a .SUFFIXES line with no sources clears out all old suffixes
   1127  *	a .PRECIOUS line makes all targets precious
   1128  *	a .IGNORE line ignores errors for all targets
   1129  *	a .SILENT line creates silence when making all targets
   1130  *	a .PATH removes all directories from the search path(s).
   1131  */
   1132 static void
   1133 ParseDependencySourcesEmpty(ParseSpecial special, SearchPathList *paths)
   1134 {
   1135 	switch (special) {
   1136 	case SP_SUFFIXES:
   1137 		Suff_ClearSuffixes();
   1138 		break;
   1139 	case SP_PRECIOUS:
   1140 		allPrecious = true;
   1141 		break;
   1142 	case SP_IGNORE:
   1143 		opts.ignoreErrors = true;
   1144 		break;
   1145 	case SP_SILENT:
   1146 		opts.silent = true;
   1147 		break;
   1148 	case SP_PATH:
   1149 		ClearPaths(paths);
   1150 		break;
   1151 #ifdef POSIX
   1152 	case SP_POSIX:
   1153 		Global_Set("%POSIX", "1003.2");
   1154 		break;
   1155 #endif
   1156 	default:
   1157 		break;
   1158 	}
   1159 }
   1160 
   1161 static void
   1162 AddToPaths(const char *dir, SearchPathList *paths)
   1163 {
   1164 	if (paths != NULL) {
   1165 		SearchPathListNode *ln;
   1166 		for (ln = paths->first; ln != NULL; ln = ln->next)
   1167 			(void)SearchPath_Add(ln->datum, dir);
   1168 	}
   1169 }
   1170 
   1171 /*
   1172  * If the target was one that doesn't take files as its sources but takes
   1173  * something like suffixes, we take each space-separated word on the line as
   1174  * a something and deal with it accordingly.
   1175  */
   1176 static void
   1177 ParseDependencySourceSpecial(ParseSpecial special, const char *word,
   1178 			     SearchPathList *paths)
   1179 {
   1180 	switch (special) {
   1181 	case SP_SUFFIXES:
   1182 		Suff_AddSuffix(word, &mainNode);
   1183 		break;
   1184 	case SP_PATH:
   1185 		AddToPaths(word, paths);
   1186 		break;
   1187 	case SP_INCLUDES:
   1188 		Suff_AddInclude(word);
   1189 		break;
   1190 	case SP_LIBS:
   1191 		Suff_AddLib(word);
   1192 		break;
   1193 	case SP_NULL:
   1194 		Suff_SetNull(word);
   1195 		break;
   1196 	case SP_OBJDIR:
   1197 		Main_SetObjdir(false, "%s", word);
   1198 		break;
   1199 	default:
   1200 		break;
   1201 	}
   1202 }
   1203 
   1204 static bool
   1205 ApplyDependencyTarget(char *name, char *nameEnd, ParseSpecial *inout_special,
   1206 		      GNodeType *inout_targetAttr,
   1207 		      SearchPathList **inout_paths)
   1208 {
   1209 	char savec = *nameEnd;
   1210 	*nameEnd = '\0';
   1211 
   1212 	if (!HandleDependencyTarget(name, inout_special,
   1213 	    inout_targetAttr, inout_paths))
   1214 		return false;
   1215 
   1216 	if (*inout_special == SP_NOT && *name != '\0')
   1217 		HandleDependencyTargetMundane(name);
   1218 	else if (*inout_special == SP_PATH && *name != '.' && *name != '\0')
   1219 		Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", name);
   1220 
   1221 	*nameEnd = savec;
   1222 	return true;
   1223 }
   1224 
   1225 static bool
   1226 ParseDependencyTargets(char **inout_cp,
   1227 		       const char *lstart,
   1228 		       ParseSpecial *inout_special,
   1229 		       GNodeType *inout_targetAttr,
   1230 		       SearchPathList **inout_paths)
   1231 {
   1232 	char *cp;
   1233 	char *tgt = *inout_cp;
   1234 
   1235 	for (;;) {
   1236 		/* Find the end of the next word. */
   1237 		cp = tgt;
   1238 		ParseDependencyTargetWord(&cp, lstart);
   1239 
   1240 		/*
   1241 		 * If the word is followed by a left parenthesis, it's the
   1242 		 * name of one or more files inside an archive.
   1243 		 */
   1244 		if (!IsEscaped(lstart, cp) && *cp == '(') {
   1245 			if (!Arch_ParseArchive(&tgt, targets, SCOPE_CMDLINE)) {
   1246 				Parse_Error(PARSE_FATAL,
   1247 				    "Error in archive specification: \"%s\"",
   1248 				    tgt);
   1249 				return false;
   1250 			}
   1251 
   1252 			cp = tgt;
   1253 			continue;
   1254 		}
   1255 
   1256 		if (*cp == '\0') {
   1257 			InvalidLineType(lstart);
   1258 			return false;
   1259 		}
   1260 
   1261 		if (!ApplyDependencyTarget(tgt, cp, inout_special,
   1262 		    inout_targetAttr, inout_paths))
   1263 			return false;
   1264 
   1265 		if (*inout_special != SP_NOT && *inout_special != SP_PATH)
   1266 			SkipExtraTargets(&cp, lstart);
   1267 		else
   1268 			pp_skip_whitespace(&cp);
   1269 
   1270 		tgt = cp;
   1271 		if (*tgt == '\0')
   1272 			break;
   1273 		if ((*tgt == '!' || *tgt == ':') && !IsEscaped(lstart, tgt))
   1274 			break;
   1275 	}
   1276 
   1277 	*inout_cp = cp;
   1278 	return true;
   1279 }
   1280 
   1281 static void
   1282 ParseDependencySourcesSpecial(char *start,
   1283 			      ParseSpecial special, SearchPathList *paths)
   1284 {
   1285 	char savec;
   1286 
   1287 	while (*start != '\0') {
   1288 		char *end = start;
   1289 		while (*end != '\0' && !ch_isspace(*end))
   1290 			end++;
   1291 		savec = *end;
   1292 		*end = '\0';
   1293 		ParseDependencySourceSpecial(special, start, paths);
   1294 		*end = savec;
   1295 		if (savec != '\0')
   1296 			end++;
   1297 		pp_skip_whitespace(&end);
   1298 		start = end;
   1299 	}
   1300 }
   1301 
   1302 static bool
   1303 ParseDependencySourcesMundane(char *start,
   1304 			      ParseSpecial special, GNodeType targetAttr)
   1305 {
   1306 	while (*start != '\0') {
   1307 		char *end = start;
   1308 		/*
   1309 		 * The targets take real sources, so we must beware of archive
   1310 		 * specifications (i.e. things with left parentheses in them)
   1311 		 * and handle them accordingly.
   1312 		 */
   1313 		for (; *end != '\0' && !ch_isspace(*end); end++) {
   1314 			if (*end == '(' && end > start && end[-1] != '$') {
   1315 				/*
   1316 				 * Only stop for a left parenthesis if it
   1317 				 * isn't at the start of a word (that'll be
   1318 				 * for variable changes later) and isn't
   1319 				 * preceded by a dollar sign (a dynamic
   1320 				 * source).
   1321 				 */
   1322 				break;
   1323 			}
   1324 		}
   1325 
   1326 		if (*end == '(') {
   1327 			GNodeList sources = LST_INIT;
   1328 			if (!Arch_ParseArchive(&start, &sources,
   1329 			    SCOPE_CMDLINE)) {
   1330 				Parse_Error(PARSE_FATAL,
   1331 				    "Error in source archive spec \"%s\"",
   1332 				    start);
   1333 				return false;
   1334 			}
   1335 
   1336 			while (!Lst_IsEmpty(&sources)) {
   1337 				GNode *gn = Lst_Dequeue(&sources);
   1338 				ApplyDependencySource(targetAttr, gn->name,
   1339 				    special);
   1340 			}
   1341 			Lst_Done(&sources);
   1342 			end = start;
   1343 		} else {
   1344 			if (*end != '\0') {
   1345 				*end = '\0';
   1346 				end++;
   1347 			}
   1348 
   1349 			ApplyDependencySource(targetAttr, start, special);
   1350 		}
   1351 		pp_skip_whitespace(&end);
   1352 		start = end;
   1353 	}
   1354 	return true;
   1355 }
   1356 
   1357 /*
   1358  * In a dependency line like 'targets: sources', parse the sources.
   1359  *
   1360  * See the tests depsrc-*.mk.
   1361  */
   1362 static void
   1363 ParseDependencySources(char *p, GNodeType targetAttr,
   1364 		       ParseSpecial special, SearchPathList **inout_paths)
   1365 {
   1366 	if (*p == '\0') {
   1367 		ParseDependencySourcesEmpty(special, *inout_paths);
   1368 	} else if (special == SP_MFLAGS) {
   1369 		Main_ParseArgLine(p);
   1370 		/*
   1371 		 * Set the initial character to a null-character so the loop
   1372 		 * to get sources won't get anything.
   1373 		 */
   1374 		*p = '\0';
   1375 	} else if (special == SP_SHELL) {
   1376 		if (!Job_ParseShell(p)) {
   1377 			Parse_Error(PARSE_FATAL,
   1378 			    "improper shell specification");
   1379 			return;
   1380 		}
   1381 		*p = '\0';
   1382 	} else if (special == SP_NOTPARALLEL || special == SP_SINGLESHELL ||
   1383 		   special == SP_DELETE_ON_ERROR) {
   1384 		*p = '\0';
   1385 	}
   1386 
   1387 	/* Now go for the sources. */
   1388 	if (special == SP_SUFFIXES || special == SP_PATH ||
   1389 	    special == SP_INCLUDES || special == SP_LIBS ||
   1390 	    special == SP_NULL || special == SP_OBJDIR) {
   1391 		ParseDependencySourcesSpecial(p, special, *inout_paths);
   1392 		if (*inout_paths != NULL) {
   1393 			Lst_Free(*inout_paths);
   1394 			*inout_paths = NULL;
   1395 		}
   1396 		if (special == SP_PATH)
   1397 			Dir_SetPATH();
   1398 	} else {
   1399 		assert(*inout_paths == NULL);
   1400 		if (!ParseDependencySourcesMundane(p, special, targetAttr))
   1401 			return;
   1402 	}
   1403 
   1404 	FindMainTarget();
   1405 }
   1406 
   1407 /*
   1408  * Parse a dependency line consisting of targets, followed by a dependency
   1409  * operator, optionally followed by sources.
   1410  *
   1411  * The nodes of the sources are linked as children to the nodes of the
   1412  * targets. Nodes are created as necessary.
   1413  *
   1414  * The operator is applied to each node in the global 'targets' list,
   1415  * which is where the nodes found for the targets are kept.
   1416  *
   1417  * The sources are parsed in much the same way as the targets, except
   1418  * that they are expanded using the wildcarding scheme of the C-Shell,
   1419  * and a target is created for each expanded word. Each of the resulting
   1420  * nodes is then linked to each of the targets as one of its children.
   1421  *
   1422  * Certain targets and sources such as .PHONY or .PRECIOUS are handled
   1423  * specially, see ParseSpecial.
   1424  *
   1425  * Transformation rules such as '.c.o' are also handled here, see
   1426  * Suff_AddTransform.
   1427  *
   1428  * Upon return, the value of the line is unspecified.
   1429  */
   1430 static void
   1431 ParseDependency(char *line)
   1432 {
   1433 	char *p;
   1434 	SearchPathList *paths;	/* search paths to alter when parsing a list
   1435 				 * of .PATH targets */
   1436 	GNodeType targetAttr;	/* from special sources */
   1437 	ParseSpecial special;	/* in special targets, the children are
   1438 				 * linked as children of the parent but not
   1439 				 * vice versa */
   1440 
   1441 	DEBUG1(PARSE, "ParseDependency(%s)\n", line);
   1442 	p = line;
   1443 	paths = NULL;
   1444 	targetAttr = OP_NONE;
   1445 	special = SP_NOT;
   1446 
   1447 	if (!ParseDependencyTargets(&p, line, &special, &targetAttr, &paths))
   1448 		goto out;
   1449 
   1450 	if (!Lst_IsEmpty(targets))
   1451 		ParseDependencyCheckSpecial(special);
   1452 
   1453 	ApplyDependencyOperator(ParseDependencyOp(&p));
   1454 
   1455 	pp_skip_whitespace(&p);
   1456 
   1457 	ParseDependencySources(p, targetAttr, special, &paths);
   1458 
   1459 out:
   1460 	if (paths != NULL)
   1461 		Lst_Free(paths);
   1462 }
   1463 
   1464 typedef enum VarAssignOp {
   1465 	VAR_NORMAL,		/* = */
   1466 	VAR_APPEND,		/* += */
   1467 	VAR_DEFAULT,		/* ?= */
   1468 	VAR_SUBST,		/* := */
   1469 	VAR_SHELL		/* != or :sh= */
   1470 } VarAssignOp;
   1471 
   1472 typedef struct VarAssign {
   1473 	char *varname;		/* unexpanded */
   1474 	VarAssignOp op;
   1475 	const char *value;	/* unexpanded */
   1476 } VarAssign;
   1477 
   1478 /*
   1479  * Determine the assignment operator and adjust the end of the variable
   1480  * name accordingly.
   1481  */
   1482 static VarAssign
   1483 AdjustVarassignOp(const char *name, const char *nameEnd, const char *op,
   1484 		  const char *value)
   1485 {
   1486 	VarAssignOp type;
   1487 	VarAssign va;
   1488 
   1489 	if (op > name && op[-1] == '+') {
   1490 		op--;
   1491 		type = VAR_APPEND;
   1492 
   1493 	} else if (op > name && op[-1] == '?') {
   1494 		op--;
   1495 		type = VAR_DEFAULT;
   1496 
   1497 	} else if (op > name && op[-1] == ':') {
   1498 		op--;
   1499 		type = VAR_SUBST;
   1500 
   1501 	} else if (op > name && op[-1] == '!') {
   1502 		op--;
   1503 		type = VAR_SHELL;
   1504 
   1505 	} else {
   1506 		type = VAR_NORMAL;
   1507 #ifdef SUNSHCMD
   1508 		while (op > name && ch_isspace(op[-1]))
   1509 			op--;
   1510 
   1511 		if (op - name >= 3 && memcmp(op - 3, ":sh", 3) == 0) {
   1512 			op -= 3;
   1513 			type = VAR_SHELL;
   1514 		}
   1515 #endif
   1516 	}
   1517 
   1518 	va.varname = bmake_strsedup(name, nameEnd < op ? nameEnd : op);
   1519 	va.op = type;
   1520 	va.value = value;
   1521 	return va;
   1522 }
   1523 
   1524 /*
   1525  * Parse a variable assignment, consisting of a single-word variable name,
   1526  * optional whitespace, an assignment operator, optional whitespace and the
   1527  * variable value.
   1528  *
   1529  * Note: There is a lexical ambiguity with assignment modifier characters
   1530  * in variable names. This routine interprets the character before the =
   1531  * as a modifier. Therefore, an assignment like
   1532  *	C++=/usr/bin/CC
   1533  * is interpreted as "C+ +=" instead of "C++ =".
   1534  *
   1535  * Used for both lines in a file and command line arguments.
   1536  */
   1537 static bool
   1538 Parse_IsVar(const char *p, VarAssign *out_var)
   1539 {
   1540 	const char *nameStart;
   1541 	const char *nameEnd;
   1542 	const char *eq;
   1543 	const char *firstSpace = NULL;
   1544 	int level = 0;
   1545 
   1546 	cpp_skip_hspace(&p);	/* Skip to variable name */
   1547 
   1548 	/*
   1549 	 * During parsing, the '+' of the '+=' operator is initially parsed
   1550 	 * as part of the variable name.  It is later corrected, as is the
   1551 	 * ':sh' modifier. Of these two (nameEnd and op), the earlier one
   1552 	 * determines the actual end of the variable name.
   1553 	 */
   1554 	nameStart = p;
   1555 #ifdef CLEANUP
   1556 	nameEnd = NULL;
   1557 	eq = NULL;
   1558 #endif
   1559 
   1560 	/*
   1561 	 * Scan for one of the assignment operators outside a variable
   1562 	 * expansion.
   1563 	 */
   1564 	while (*p != '\0') {
   1565 		char ch = *p++;
   1566 		if (ch == '(' || ch == '{') {
   1567 			level++;
   1568 			continue;
   1569 		}
   1570 		if (ch == ')' || ch == '}') {
   1571 			level--;
   1572 			continue;
   1573 		}
   1574 
   1575 		if (level != 0)
   1576 			continue;
   1577 
   1578 		if (ch == ' ' || ch == '\t')
   1579 			if (firstSpace == NULL)
   1580 				firstSpace = p - 1;
   1581 		while (ch == ' ' || ch == '\t')
   1582 			ch = *p++;
   1583 
   1584 #ifdef SUNSHCMD
   1585 		if (ch == ':' && p[0] == 's' && p[1] == 'h') {
   1586 			p += 2;
   1587 			continue;
   1588 		}
   1589 #endif
   1590 		if (ch == '=')
   1591 			eq = p - 1;
   1592 		else if (*p == '=' &&
   1593 		    (ch == '+' || ch == ':' || ch == '?' || ch == '!'))
   1594 			eq = p;
   1595 		else if (firstSpace != NULL)
   1596 			return false;
   1597 		else
   1598 			continue;
   1599 
   1600 		nameEnd = firstSpace != NULL ? firstSpace : eq;
   1601 		p = eq + 1;
   1602 		cpp_skip_whitespace(&p);
   1603 		*out_var = AdjustVarassignOp(nameStart, nameEnd, eq, p);
   1604 		return true;
   1605 	}
   1606 
   1607 	return false;
   1608 }
   1609 
   1610 /*
   1611  * Check for syntax errors such as unclosed expressions or unknown modifiers.
   1612  */
   1613 static void
   1614 VarCheckSyntax(VarAssignOp type, const char *uvalue, GNode *scope)
   1615 {
   1616 	if (opts.strict) {
   1617 		if (type != VAR_SUBST && strchr(uvalue, '$') != NULL) {
   1618 			char *expandedValue;
   1619 
   1620 			(void)Var_Subst(uvalue, scope, VARE_PARSE_ONLY,
   1621 			    &expandedValue);
   1622 			/* TODO: handle errors */
   1623 			free(expandedValue);
   1624 		}
   1625 	}
   1626 }
   1627 
   1628 static void
   1629 VarAssign_EvalSubst(GNode *scope, const char *name, const char *uvalue,
   1630 		    FStr *out_avalue)
   1631 {
   1632 	char *evalue;
   1633 
   1634 	/*
   1635 	 * make sure that we set the variable the first time to nothing
   1636 	 * so that it gets substituted.
   1637 	 *
   1638 	 * TODO: Add a test that demonstrates why this code is needed,
   1639 	 *  apart from making the debug log longer.
   1640 	 */
   1641 	if (!Var_ExistsExpand(scope, name))
   1642 		Var_SetExpand(scope, name, "");
   1643 
   1644 	(void)Var_Subst(uvalue, scope, VARE_KEEP_DOLLAR_UNDEF, &evalue);
   1645 	/* TODO: handle errors */
   1646 
   1647 	Var_SetExpand(scope, name, evalue);
   1648 
   1649 	*out_avalue = FStr_InitOwn(evalue);
   1650 }
   1651 
   1652 static void
   1653 VarAssign_EvalShell(const char *name, const char *uvalue, GNode *scope,
   1654 		    FStr *out_avalue)
   1655 {
   1656 	FStr cmd;
   1657 	const char *errfmt;
   1658 	char *cmdOut;
   1659 
   1660 	cmd = FStr_InitRefer(uvalue);
   1661 	if (strchr(cmd.str, '$') != NULL) {
   1662 		char *expanded;
   1663 		(void)Var_Subst(cmd.str, SCOPE_CMDLINE, VARE_UNDEFERR,
   1664 		    &expanded);
   1665 		/* TODO: handle errors */
   1666 		cmd = FStr_InitOwn(expanded);
   1667 	}
   1668 
   1669 	cmdOut = Cmd_Exec(cmd.str, &errfmt);
   1670 	Var_SetExpand(scope, name, cmdOut);
   1671 	*out_avalue = FStr_InitOwn(cmdOut);
   1672 
   1673 	if (errfmt != NULL)
   1674 		Parse_Error(PARSE_WARNING, errfmt, cmd.str);
   1675 
   1676 	FStr_Done(&cmd);
   1677 }
   1678 
   1679 /*
   1680  * Perform a variable assignment.
   1681  *
   1682  * The actual value of the variable is returned in *out_true_avalue.
   1683  * Especially for VAR_SUBST and VAR_SHELL this can differ from the literal
   1684  * value.
   1685  *
   1686  * Return whether the assignment was actually performed, which is usually
   1687  * the case.  It is only skipped if the operator is '?=' and the variable
   1688  * already exists.
   1689  */
   1690 static bool
   1691 VarAssign_Eval(const char *name, VarAssignOp op, const char *uvalue,
   1692 	       GNode *scope, FStr *out_true_avalue)
   1693 {
   1694 	FStr avalue = FStr_InitRefer(uvalue);
   1695 
   1696 	if (op == VAR_APPEND)
   1697 		Var_AppendExpand(scope, name, uvalue);
   1698 	else if (op == VAR_SUBST)
   1699 		VarAssign_EvalSubst(scope, name, uvalue, &avalue);
   1700 	else if (op == VAR_SHELL)
   1701 		VarAssign_EvalShell(name, uvalue, scope, &avalue);
   1702 	else {
   1703 		if (op == VAR_DEFAULT && Var_ExistsExpand(scope, name))
   1704 			return false;
   1705 
   1706 		/* Normal assignment -- just do it. */
   1707 		Var_SetExpand(scope, name, uvalue);
   1708 	}
   1709 
   1710 	*out_true_avalue = avalue;
   1711 	return true;
   1712 }
   1713 
   1714 static void
   1715 VarAssignSpecial(const char *name, const char *avalue)
   1716 {
   1717 	if (strcmp(name, MAKEOVERRIDES) == 0)
   1718 		Main_ExportMAKEFLAGS(false);	/* re-export MAKEFLAGS */
   1719 	else if (strcmp(name, ".CURDIR") == 0) {
   1720 		/*
   1721 		 * Someone is being (too?) clever...
   1722 		 * Let's pretend they know what they are doing and
   1723 		 * re-initialize the 'cur' CachedDir.
   1724 		 */
   1725 		Dir_InitCur(avalue);
   1726 		Dir_SetPATH();
   1727 	} else if (strcmp(name, MAKE_JOB_PREFIX) == 0)
   1728 		Job_SetPrefix();
   1729 	else if (strcmp(name, MAKE_EXPORTED) == 0)
   1730 		Var_ExportVars(avalue);
   1731 }
   1732 
   1733 /* Perform the variable assignment in the given scope. */
   1734 static void
   1735 Parse_Var(VarAssign *var, GNode *scope)
   1736 {
   1737 	FStr avalue;		/* actual value (maybe expanded) */
   1738 
   1739 	VarCheckSyntax(var->op, var->value, scope);
   1740 	if (VarAssign_Eval(var->varname, var->op, var->value, scope, &avalue)) {
   1741 		VarAssignSpecial(var->varname, avalue.str);
   1742 		FStr_Done(&avalue);
   1743 	}
   1744 
   1745 	free(var->varname);
   1746 }
   1747 
   1748 
   1749 /*
   1750  * See if the command possibly calls a sub-make by using the variable
   1751  * expressions ${.MAKE}, ${MAKE} or the plain word "make".
   1752  */
   1753 static bool
   1754 MaybeSubMake(const char *cmd)
   1755 {
   1756 	const char *start;
   1757 
   1758 	for (start = cmd; *start != '\0'; start++) {
   1759 		const char *p = start;
   1760 		char endc;
   1761 
   1762 		/* XXX: What if progname != "make"? */
   1763 		if (strncmp(p, "make", 4) == 0)
   1764 			if (start == cmd || !ch_isalnum(p[-1]))
   1765 				if (!ch_isalnum(p[4]))
   1766 					return true;
   1767 
   1768 		if (*p != '$')
   1769 			continue;
   1770 		p++;
   1771 
   1772 		if (*p == '{')
   1773 			endc = '}';
   1774 		else if (*p == '(')
   1775 			endc = ')';
   1776 		else
   1777 			continue;
   1778 		p++;
   1779 
   1780 		if (*p == '.')	/* Accept either ${.MAKE} or ${MAKE}. */
   1781 			p++;
   1782 
   1783 		if (strncmp(p, "MAKE", 4) == 0 && p[4] == endc)
   1784 			return true;
   1785 	}
   1786 	return false;
   1787 }
   1788 
   1789 /*
   1790  * Append the command to the target node.
   1791  *
   1792  * The node may be marked as a submake node if the command is determined to
   1793  * be that.
   1794  */
   1795 static void
   1796 GNode_AddCommand(GNode *gn, char *cmd)
   1797 {
   1798 	/* Add to last (ie current) cohort for :: targets */
   1799 	if ((gn->type & OP_DOUBLEDEP) && gn->cohorts.last != NULL)
   1800 		gn = gn->cohorts.last->datum;
   1801 
   1802 	/* if target already supplied, ignore commands */
   1803 	if (!(gn->type & OP_HAS_COMMANDS)) {
   1804 		Lst_Append(&gn->commands, cmd);
   1805 		if (MaybeSubMake(cmd))
   1806 			gn->type |= OP_SUBMAKE;
   1807 		RememberLocation(gn);
   1808 	} else {
   1809 #if 0
   1810 		/* XXX: We cannot do this until we fix the tree */
   1811 		Lst_Append(&gn->commands, cmd);
   1812 		Parse_Error(PARSE_WARNING,
   1813 		    "overriding commands for target \"%s\"; "
   1814 		    "previous commands defined at %s: %d ignored",
   1815 		    gn->name, gn->fname, gn->lineno);
   1816 #else
   1817 		Parse_Error(PARSE_WARNING,
   1818 		    "duplicate script for target \"%s\" ignored",
   1819 		    gn->name);
   1820 		ParseErrorInternal(gn->fname, (size_t)gn->lineno, PARSE_WARNING,
   1821 		    "using previous script for \"%s\" defined here",
   1822 		    gn->name);
   1823 #endif
   1824 	}
   1825 }
   1826 
   1827 /*
   1828  * Add a directory to the path searched for included makefiles bracketed
   1829  * by double-quotes.
   1830  */
   1831 void
   1832 Parse_AddIncludeDir(const char *dir)
   1833 {
   1834 	(void)SearchPath_Add(parseIncPath, dir);
   1835 }
   1836 
   1837 /*
   1838  * Handle one of the .[-ds]include directives by remembering the current file
   1839  * and pushing the included file on the stack.  After the included file has
   1840  * finished, parsing continues with the including file; see Parse_PushInput
   1841  * and ParseEOF.
   1842  *
   1843  * System includes are looked up in sysIncPath, any other includes are looked
   1844  * up in the parsedir and then in the directories specified by the -I command
   1845  * line options.
   1846  */
   1847 static void
   1848 IncludeFile(const char *file, bool isSystem, bool depinc, bool silent)
   1849 {
   1850 	Buffer buf;
   1851 	char *fullname;		/* full pathname of file */
   1852 	char *newName;
   1853 	char *slash, *incdir;
   1854 	int fd;
   1855 	int i;
   1856 
   1857 	fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
   1858 
   1859 	if (fullname == NULL && !isSystem) {
   1860 		/*
   1861 		 * Include files contained in double-quotes are first searched
   1862 		 * relative to the including file's location. We don't want to
   1863 		 * cd there, of course, so we just tack on the old file's
   1864 		 * leading path components and call Dir_FindFile to see if
   1865 		 * we can locate the file.
   1866 		 */
   1867 
   1868 		incdir = bmake_strdup(CurFile()->name.str);
   1869 		slash = strrchr(incdir, '/');
   1870 		if (slash != NULL) {
   1871 			*slash = '\0';
   1872 			/*
   1873 			 * Now do lexical processing of leading "../" on the
   1874 			 * filename.
   1875 			 */
   1876 			for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
   1877 				slash = strrchr(incdir + 1, '/');
   1878 				if (slash == NULL || strcmp(slash, "/..") == 0)
   1879 					break;
   1880 				*slash = '\0';
   1881 			}
   1882 			newName = str_concat3(incdir, "/", file + i);
   1883 			fullname = Dir_FindFile(newName, parseIncPath);
   1884 			if (fullname == NULL)
   1885 				fullname = Dir_FindFile(newName,
   1886 				    &dirSearchPath);
   1887 			free(newName);
   1888 		}
   1889 		free(incdir);
   1890 
   1891 		if (fullname == NULL) {
   1892 			/*
   1893 			 * Makefile wasn't found in same directory as included
   1894 			 * makefile.
   1895 			 *
   1896 			 * Search for it first on the -I search path, then on
   1897 			 * the .PATH search path, if not found in a -I
   1898 			 * directory. If we have a suffix-specific path, we
   1899 			 * should use that.
   1900 			 */
   1901 			const char *suff;
   1902 			SearchPath *suffPath = NULL;
   1903 
   1904 			if ((suff = strrchr(file, '.')) != NULL) {
   1905 				suffPath = Suff_GetPath(suff);
   1906 				if (suffPath != NULL)
   1907 					fullname = Dir_FindFile(file, suffPath);
   1908 			}
   1909 			if (fullname == NULL) {
   1910 				fullname = Dir_FindFile(file, parseIncPath);
   1911 				if (fullname == NULL)
   1912 					fullname = Dir_FindFile(file,
   1913 					    &dirSearchPath);
   1914 			}
   1915 		}
   1916 	}
   1917 
   1918 	/* Looking for a system file or file still not found */
   1919 	if (fullname == NULL) {
   1920 		/*
   1921 		 * Look for it on the system path
   1922 		 */
   1923 		SearchPath *path = Lst_IsEmpty(&sysIncPath->dirs)
   1924 		    ? defSysIncPath : sysIncPath;
   1925 		fullname = Dir_FindFile(file, path);
   1926 	}
   1927 
   1928 	if (fullname == NULL) {
   1929 		if (!silent)
   1930 			Parse_Error(PARSE_FATAL, "Could not find %s", file);
   1931 		return;
   1932 	}
   1933 
   1934 	/* Actually open the file... */
   1935 	fd = open(fullname, O_RDONLY);
   1936 	if (fd == -1) {
   1937 		if (!silent)
   1938 			Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
   1939 		free(fullname);
   1940 		return;
   1941 	}
   1942 
   1943 	buf = loadfile(fullname, fd);
   1944 	(void)close(fd);
   1945 
   1946 	Parse_PushInput(fullname, 0, buf, NULL);
   1947 	if (depinc)
   1948 		doing_depend = depinc;	/* only turn it on */
   1949 	free(fullname);
   1950 }
   1951 
   1952 /*
   1953  * Parse a directive like '.include' or '.-include'.
   1954  *
   1955  * .include "user-makefile.mk"
   1956  * .include <system-makefile.mk>
   1957  */
   1958 static void
   1959 ParseInclude(char *directive)
   1960 {
   1961 	char endc;		/* '>' or '"' */
   1962 	char *p;
   1963 	bool silent = directive[0] != 'i';
   1964 	FStr file;
   1965 
   1966 	p = directive + (silent ? 8 : 7);
   1967 	pp_skip_hspace(&p);
   1968 
   1969 	if (*p != '"' && *p != '<') {
   1970 		Parse_Error(PARSE_FATAL,
   1971 		    ".include filename must be delimited by '\"' or '<'");
   1972 		return;
   1973 	}
   1974 
   1975 	if (*p++ == '<')
   1976 		endc = '>';
   1977 	else
   1978 		endc = '"';
   1979 	file = FStr_InitRefer(p);
   1980 
   1981 	/* Skip to matching delimiter */
   1982 	while (*p != '\0' && *p != endc)
   1983 		p++;
   1984 
   1985 	if (*p != endc) {
   1986 		Parse_Error(PARSE_FATAL,
   1987 		    "Unclosed .include filename. '%c' expected", endc);
   1988 		return;
   1989 	}
   1990 
   1991 	*p = '\0';
   1992 
   1993 	if (strchr(file.str, '$') != NULL) {
   1994 		char *xfile;
   1995 		Var_Subst(file.str, SCOPE_CMDLINE, VARE_WANTRES, &xfile);
   1996 		/* TODO: handle errors */
   1997 		file = FStr_InitOwn(xfile);
   1998 	}
   1999 
   2000 	IncludeFile(file.str, endc == '>', directive[0] == 'd', silent);
   2001 	FStr_Done(&file);
   2002 }
   2003 
   2004 /*
   2005  * Split filename into dirname + basename, then assign these to the
   2006  * given variables.
   2007  */
   2008 static void
   2009 SetFilenameVars(const char *filename, const char *dirvar, const char *filevar)
   2010 {
   2011 	const char *slash, *basename;
   2012 	FStr dirname;
   2013 
   2014 	slash = strrchr(filename, '/');
   2015 	if (slash == NULL) {
   2016 		dirname = FStr_InitRefer(curdir);
   2017 		basename = filename;
   2018 	} else {
   2019 		dirname = FStr_InitOwn(bmake_strsedup(filename, slash));
   2020 		basename = slash + 1;
   2021 	}
   2022 
   2023 	Global_Set(dirvar, dirname.str);
   2024 	Global_Set(filevar, basename);
   2025 
   2026 	DEBUG4(PARSE, "SetFilenameVars: ${%s} = `%s' ${%s} = `%s'\n",
   2027 	    dirvar, dirname.str, filevar, basename);
   2028 	FStr_Done(&dirname);
   2029 }
   2030 
   2031 /*
   2032  * Return the immediately including file.
   2033  *
   2034  * This is made complicated since the .for loop is implemented as a special
   2035  * kind of .include; see For_Run.
   2036  */
   2037 static const char *
   2038 GetActuallyIncludingFile(void)
   2039 {
   2040 	size_t i;
   2041 	const IFile *incs = GetInclude(0);
   2042 
   2043 	for (i = includes.len; i >= 2; i--)
   2044 		if (incs[i - 1].forLoop == NULL)
   2045 			return incs[i - 2].name.str;
   2046 	return NULL;
   2047 }
   2048 
   2049 /* Set .PARSEDIR, .PARSEFILE, .INCLUDEDFROMDIR and .INCLUDEDFROMFILE. */
   2050 static void
   2051 SetParseFile(const char *filename)
   2052 {
   2053 	const char *including;
   2054 
   2055 	SetFilenameVars(filename, ".PARSEDIR", ".PARSEFILE");
   2056 
   2057 	including = GetActuallyIncludingFile();
   2058 	if (including != NULL) {
   2059 		SetFilenameVars(including,
   2060 		    ".INCLUDEDFROMDIR", ".INCLUDEDFROMFILE");
   2061 	} else {
   2062 		Global_Delete(".INCLUDEDFROMDIR");
   2063 		Global_Delete(".INCLUDEDFROMFILE");
   2064 	}
   2065 }
   2066 
   2067 static bool
   2068 StrContainsWord(const char *str, const char *word)
   2069 {
   2070 	size_t strLen = strlen(str);
   2071 	size_t wordLen = strlen(word);
   2072 	const char *p;
   2073 
   2074 	if (strLen < wordLen)
   2075 		return false;
   2076 
   2077 	for (p = str; p != NULL; p = strchr(p, ' ')) {
   2078 		if (*p == ' ')
   2079 			p++;
   2080 		if (p > str + strLen - wordLen)
   2081 			return false;
   2082 
   2083 		if (memcmp(p, word, wordLen) == 0 &&
   2084 		    (p[wordLen] == '\0' || p[wordLen] == ' '))
   2085 			return true;
   2086 	}
   2087 	return false;
   2088 }
   2089 
   2090 /*
   2091  * XXX: Searching through a set of words with this linear search is
   2092  * inefficient for variables that contain thousands of words.
   2093  *
   2094  * XXX: The paths in this list don't seem to be normalized in any way.
   2095  */
   2096 static bool
   2097 VarContainsWord(const char *varname, const char *word)
   2098 {
   2099 	FStr val = Var_Value(SCOPE_GLOBAL, varname);
   2100 	bool found = val.str != NULL && StrContainsWord(val.str, word);
   2101 	FStr_Done(&val);
   2102 	return found;
   2103 }
   2104 
   2105 /*
   2106  * Track the makefiles we read - so makefiles can set dependencies on them.
   2107  * Avoid adding anything more than once.
   2108  *
   2109  * Time complexity: O(n) per call, in total O(n^2), where n is the number
   2110  * of makefiles that have been loaded.
   2111  */
   2112 static void
   2113 TrackInput(const char *name)
   2114 {
   2115 	if (!VarContainsWord(MAKE_MAKEFILES, name))
   2116 		Global_Append(MAKE_MAKEFILES, name);
   2117 }
   2118 
   2119 
   2120 /*
   2121  * Start parsing from the given source.
   2122  *
   2123  * The given file is added to the includes stack.
   2124  */
   2125 void
   2126 Parse_PushInput(const char *name, int lineno, Buffer buf,
   2127 		struct ForLoop *forLoop)
   2128 {
   2129 	IFile *curFile;
   2130 
   2131 	if (forLoop != NULL)
   2132 		name = CurFile()->name.str;
   2133 	else
   2134 		TrackInput(name);
   2135 
   2136 	DEBUG3(PARSE, "Parse_PushInput: %s %s, line %d\n",
   2137 	    forLoop != NULL ? ".for loop in": "file", name, lineno);
   2138 
   2139 	curFile = Vector_Push(&includes);
   2140 	curFile->name = FStr_InitOwn(bmake_strdup(name));
   2141 	curFile->lineno = lineno;
   2142 	curFile->forBodyLineno = lineno;
   2143 	curFile->buf = buf;
   2144 	curFile->depending = doing_depend;	/* restore this on EOF */
   2145 	curFile->forLoop = forLoop;
   2146 
   2147 	if (forLoop != NULL && !For_NextIteration(forLoop, &curFile->buf))
   2148 		abort();	/* see For_Run */
   2149 
   2150 	curFile->buf_ptr = curFile->buf.data;
   2151 	curFile->buf_end = curFile->buf.data + curFile->buf.len;
   2152 	curFile->cond_depth = Cond_save_depth();
   2153 	SetParseFile(name);
   2154 }
   2155 
   2156 /* Check if the directive is an include directive. */
   2157 static bool
   2158 IsInclude(const char *dir, bool sysv)
   2159 {
   2160 	if (dir[0] == 's' || dir[0] == '-' || (dir[0] == 'd' && !sysv))
   2161 		dir++;
   2162 
   2163 	if (strncmp(dir, "include", 7) != 0)
   2164 		return false;
   2165 
   2166 	/* Space is not mandatory for BSD .include */
   2167 	return !sysv || ch_isspace(dir[7]);
   2168 }
   2169 
   2170 
   2171 #ifdef SYSVINCLUDE
   2172 /* Check if the line is a SYSV include directive. */
   2173 static bool
   2174 IsSysVInclude(const char *line)
   2175 {
   2176 	const char *p;
   2177 
   2178 	if (!IsInclude(line, true))
   2179 		return false;
   2180 
   2181 	/* Avoid interpreting a dependency line as an include */
   2182 	for (p = line; (p = strchr(p, ':')) != NULL;) {
   2183 
   2184 		/* end of line -> it's a dependency */
   2185 		if (*++p == '\0')
   2186 			return false;
   2187 
   2188 		/* '::' operator or ': ' -> it's a dependency */
   2189 		if (*p == ':' || ch_isspace(*p))
   2190 			return false;
   2191 	}
   2192 	return true;
   2193 }
   2194 
   2195 /* Push to another file.  The line points to the word "include". */
   2196 static void
   2197 ParseTraditionalInclude(char *line)
   2198 {
   2199 	char *cp;		/* current position in file spec */
   2200 	bool done = false;
   2201 	bool silent = line[0] != 'i';
   2202 	char *file = line + (silent ? 8 : 7);
   2203 	char *all_files;
   2204 
   2205 	DEBUG1(PARSE, "ParseTraditionalInclude: %s\n", file);
   2206 
   2207 	pp_skip_whitespace(&file);
   2208 
   2209 	(void)Var_Subst(file, SCOPE_CMDLINE, VARE_WANTRES, &all_files);
   2210 	/* TODO: handle errors */
   2211 
   2212 	for (file = all_files; !done; file = cp + 1) {
   2213 		/* Skip to end of line or next whitespace */
   2214 		for (cp = file; *cp != '\0' && !ch_isspace(*cp); cp++)
   2215 			continue;
   2216 
   2217 		if (*cp != '\0')
   2218 			*cp = '\0';
   2219 		else
   2220 			done = true;
   2221 
   2222 		IncludeFile(file, false, false, silent);
   2223 	}
   2224 
   2225 	free(all_files);
   2226 }
   2227 #endif
   2228 
   2229 #ifdef GMAKEEXPORT
   2230 /* Parse "export <variable>=<value>", and actually export it. */
   2231 static void
   2232 ParseGmakeExport(char *line)
   2233 {
   2234 	char *variable = line + 6;
   2235 	char *value;
   2236 
   2237 	DEBUG1(PARSE, "ParseGmakeExport: %s\n", variable);
   2238 
   2239 	pp_skip_whitespace(&variable);
   2240 
   2241 	for (value = variable; *value != '\0' && *value != '='; value++)
   2242 		continue;
   2243 
   2244 	if (*value != '=') {
   2245 		Parse_Error(PARSE_FATAL,
   2246 		    "Variable/Value missing from \"export\"");
   2247 		return;
   2248 	}
   2249 	*value++ = '\0';	/* terminate variable */
   2250 
   2251 	/*
   2252 	 * Expand the value before putting it in the environment.
   2253 	 */
   2254 	(void)Var_Subst(value, SCOPE_CMDLINE, VARE_WANTRES, &value);
   2255 	/* TODO: handle errors */
   2256 
   2257 	setenv(variable, value, 1);
   2258 	free(value);
   2259 }
   2260 #endif
   2261 
   2262 /*
   2263  * Called when EOF is reached in the current file. If we were reading an
   2264  * include file or a .for loop, the includes stack is popped and things set
   2265  * up to go back to reading the previous file at the previous location.
   2266  *
   2267  * Results:
   2268  *	true to continue parsing, i.e. it had only reached the end of an
   2269  *	included file, false if the main file has been parsed completely.
   2270  */
   2271 static bool
   2272 ParseEOF(void)
   2273 {
   2274 	IFile *curFile = CurFile();
   2275 
   2276 	doing_depend = curFile->depending;
   2277 	if (curFile->forLoop != NULL &&
   2278 	    For_NextIteration(curFile->forLoop, &curFile->buf)) {
   2279 		curFile->buf_ptr = curFile->buf.data;
   2280 		curFile->buf_end = curFile->buf.data + curFile->buf.len;
   2281 		curFile->lineno = curFile->forBodyLineno;
   2282 		return true;
   2283 	}
   2284 
   2285 	/*
   2286 	 * Ensure the makefile (or .for loop) didn't have mismatched
   2287 	 * conditionals.
   2288 	 */
   2289 	Cond_restore_depth(curFile->cond_depth);
   2290 
   2291 	FStr_Done(&curFile->name);
   2292 	Buf_Done(&curFile->buf);
   2293 	Vector_Pop(&includes);
   2294 
   2295 	if (includes.len == 0) {
   2296 		/* We've run out of input */
   2297 		Global_Delete(".PARSEDIR");
   2298 		Global_Delete(".PARSEFILE");
   2299 		Global_Delete(".INCLUDEDFROMDIR");
   2300 		Global_Delete(".INCLUDEDFROMFILE");
   2301 		return false;
   2302 	}
   2303 
   2304 	curFile = CurFile();
   2305 	DEBUG2(PARSE, "ParseEOF: returning to file %s, line %d\n",
   2306 	    curFile->name.str, curFile->lineno);
   2307 
   2308 	SetParseFile(curFile->name.str);
   2309 	return true;
   2310 }
   2311 
   2312 typedef enum ParseRawLineResult {
   2313 	PRLR_LINE,
   2314 	PRLR_EOF,
   2315 	PRLR_ERROR
   2316 } ParseRawLineResult;
   2317 
   2318 /*
   2319  * Parse until the end of a line, taking into account lines that end with
   2320  * backslash-newline.  The resulting line goes from out_line to out_line_end;
   2321  * the line is not null-terminated.
   2322  */
   2323 static ParseRawLineResult
   2324 ParseRawLine(IFile *curFile, char **out_line, char **out_line_end,
   2325 	     char **out_firstBackslash, char **out_firstComment)
   2326 {
   2327 	char *line = curFile->buf_ptr;
   2328 	char *buf_end = curFile->buf_end;
   2329 	char *p = line;
   2330 	char *line_end = line;
   2331 	char *firstBackslash = NULL;
   2332 	char *firstComment = NULL;
   2333 	ParseRawLineResult res = PRLR_LINE;
   2334 
   2335 	curFile->lineno++;
   2336 
   2337 	for (;;) {
   2338 		char ch;
   2339 
   2340 		if (p == buf_end) {
   2341 			res = PRLR_EOF;
   2342 			break;
   2343 		}
   2344 
   2345 		ch = *p;
   2346 		if (ch == '\0' ||
   2347 		    (ch == '\\' && p + 1 < buf_end && p[1] == '\0')) {
   2348 			Parse_Error(PARSE_FATAL, "Zero byte read from file");
   2349 			return PRLR_ERROR;
   2350 		}
   2351 
   2352 		/* Treat next character after '\' as literal. */
   2353 		if (ch == '\\') {
   2354 			if (firstBackslash == NULL)
   2355 				firstBackslash = p;
   2356 			if (p[1] == '\n') {
   2357 				curFile->lineno++;
   2358 				if (p + 2 == buf_end) {
   2359 					line_end = p;
   2360 					*line_end = '\n';
   2361 					p += 2;
   2362 					continue;
   2363 				}
   2364 			}
   2365 			p += 2;
   2366 			line_end = p;
   2367 			assert(p <= buf_end);
   2368 			continue;
   2369 		}
   2370 
   2371 		/*
   2372 		 * Remember the first '#' for comment stripping, unless
   2373 		 * the previous char was '[', as in the modifier ':[#]'.
   2374 		 */
   2375 		if (ch == '#' && firstComment == NULL &&
   2376 		    !(p > line && p[-1] == '['))
   2377 			firstComment = line_end;
   2378 
   2379 		p++;
   2380 		if (ch == '\n')
   2381 			break;
   2382 
   2383 		/* We are not interested in trailing whitespace. */
   2384 		if (!ch_isspace(ch))
   2385 			line_end = p;
   2386 	}
   2387 
   2388 	*out_line = line;
   2389 	curFile->buf_ptr = p;
   2390 	*out_line_end = line_end;
   2391 	*out_firstBackslash = firstBackslash;
   2392 	*out_firstComment = firstComment;
   2393 	return res;
   2394 }
   2395 
   2396 /*
   2397  * Beginning at start, unescape '\#' to '#' and replace backslash-newline
   2398  * with a single space.
   2399  */
   2400 static void
   2401 UnescapeBackslash(char *line, char *start)
   2402 {
   2403 	char *src = start;
   2404 	char *dst = start;
   2405 	char *spaceStart = line;
   2406 
   2407 	for (;;) {
   2408 		char ch = *src++;
   2409 		if (ch != '\\') {
   2410 			if (ch == '\0')
   2411 				break;
   2412 			*dst++ = ch;
   2413 			continue;
   2414 		}
   2415 
   2416 		ch = *src++;
   2417 		if (ch == '\0') {
   2418 			/* Delete '\\' at end of buffer */
   2419 			dst--;
   2420 			break;
   2421 		}
   2422 
   2423 		/* Delete '\\' from before '#' on non-command lines */
   2424 		if (ch == '#' && line[0] != '\t') {
   2425 			*dst++ = ch;
   2426 			continue;
   2427 		}
   2428 
   2429 		if (ch != '\n') {
   2430 			/* Leave '\\' in buffer for later */
   2431 			*dst++ = '\\';
   2432 			/*
   2433 			 * Make sure we don't delete an escaped ' ' from the
   2434 			 * line end.
   2435 			 */
   2436 			spaceStart = dst + 1;
   2437 			*dst++ = ch;
   2438 			continue;
   2439 		}
   2440 
   2441 		/*
   2442 		 * Escaped '\n' -- replace following whitespace with a single
   2443 		 * ' '.
   2444 		 */
   2445 		pp_skip_hspace(&src);
   2446 		*dst++ = ' ';
   2447 	}
   2448 
   2449 	/* Delete any trailing spaces - eg from empty continuations */
   2450 	while (dst > spaceStart && ch_isspace(dst[-1]))
   2451 		dst--;
   2452 	*dst = '\0';
   2453 }
   2454 
   2455 typedef enum LineKind {
   2456 	/*
   2457 	 * Return the next line that is neither empty nor a comment.
   2458 	 * Backslash line continuations are folded into a single space.
   2459 	 * A trailing comment, if any, is discarded.
   2460 	 */
   2461 	LK_NONEMPTY,
   2462 
   2463 	/*
   2464 	 * Return the next line, even if it is empty or a comment.
   2465 	 * Preserve backslash-newline to keep the line numbers correct.
   2466 	 *
   2467 	 * Used in .for loops to collect the body of the loop while waiting
   2468 	 * for the corresponding .endfor.
   2469 	 */
   2470 	LK_FOR_BODY,
   2471 
   2472 	/*
   2473 	 * Return the next line that starts with a dot.
   2474 	 * Backslash line continuations are folded into a single space.
   2475 	 * A trailing comment, if any, is discarded.
   2476 	 *
   2477 	 * Used in .if directives to skip over irrelevant branches while
   2478 	 * waiting for the corresponding .endif.
   2479 	 */
   2480 	LK_DOT
   2481 } LineKind;
   2482 
   2483 /* Return the next "interesting" logical line from the current file. */
   2484 static char *
   2485 ReadLowLevelLine(LineKind kind)
   2486 {
   2487 	IFile *curFile = CurFile();
   2488 	char *line;
   2489 	char *line_end;
   2490 	char *firstBackslash;
   2491 	char *firstComment;
   2492 
   2493 	for (;;) {
   2494 		ParseRawLineResult res = ParseRawLine(curFile,
   2495 		    &line, &line_end, &firstBackslash, &firstComment);
   2496 		if (res == PRLR_ERROR)
   2497 			return NULL;
   2498 
   2499 		if (line_end == line || firstComment == line) {
   2500 			if (res == PRLR_EOF)
   2501 				return NULL;
   2502 			if (kind != LK_FOR_BODY)
   2503 				continue;
   2504 		}
   2505 
   2506 		/* We now have a line of data */
   2507 		assert(ch_isspace(*line_end));
   2508 		*line_end = '\0';
   2509 
   2510 		if (kind == LK_FOR_BODY)
   2511 			return line;	/* Don't join the physical lines. */
   2512 
   2513 		if (kind == LK_DOT && line[0] != '.')
   2514 			continue;
   2515 		break;
   2516 	}
   2517 
   2518 	/* Ignore anything after a non-escaped '#' in non-commands. */
   2519 	if (firstComment != NULL && line[0] != '\t')
   2520 		*firstComment = '\0';
   2521 
   2522 	/* If we didn't see a '\\' then the in-situ data is fine. */
   2523 	if (firstBackslash == NULL)
   2524 		return line;
   2525 
   2526 	/* Remove escapes from '\n' and '#' */
   2527 	UnescapeBackslash(line, firstBackslash);
   2528 
   2529 	return line;
   2530 }
   2531 
   2532 static bool
   2533 SkipIrrelevantBranches(void)
   2534 {
   2535 	char *line;
   2536 
   2537 	while ((line = ReadLowLevelLine(LK_DOT)) != NULL) {
   2538 		if (Cond_EvalLine(line) == CR_TRUE)
   2539 			break;
   2540 		/*
   2541 		 * TODO: Check for typos in .elif directives
   2542 		 * such as .elsif or .elseif.
   2543 		 *
   2544 		 * This check will probably duplicate some of
   2545 		 * the code in ParseLine.  Most of the code
   2546 		 * there cannot apply, only ParseVarassign and
   2547 		 * ParseDependencyLine can, and to prevent code
   2548 		 * duplication, these would need to be called
   2549 		 * with a flag called onlyCheckSyntax.
   2550 		 *
   2551 		 * See directive-elif.mk for details.
   2552 		 */
   2553 	}
   2554 
   2555 	return line != NULL;
   2556 }
   2557 
   2558 static bool
   2559 ParseForLoop(const char *line)
   2560 {
   2561 	int rval;
   2562 	int firstLineno;
   2563 
   2564 	rval = For_Eval(line);
   2565 	if (rval == 0)
   2566 		return false;	/* Not a .for line */
   2567 	if (rval < 0)
   2568 		return true;	/* Syntax error - error printed, ignore line */
   2569 
   2570 	firstLineno = CurFile()->lineno;
   2571 
   2572 	/* Accumulate the loop body until the matching '.endfor'. */
   2573 	do {
   2574 		line = ReadLowLevelLine(LK_FOR_BODY);
   2575 		if (line == NULL) {
   2576 			Parse_Error(PARSE_FATAL,
   2577 			    "Unexpected end of file in .for loop");
   2578 			break;
   2579 		}
   2580 	} while (For_Accum(line));
   2581 
   2582 	For_Run(firstLineno);
   2583 	return true;
   2584 }
   2585 
   2586 /*
   2587  * Read an entire line from the input file.
   2588  *
   2589  * Empty lines, .if and .for are completely handled by this function,
   2590  * leaving only variable assignments, other directives, dependency lines
   2591  * and shell commands to the caller.
   2592  *
   2593  * Results:
   2594  *	A line without its newline and without any trailing whitespace,
   2595  *	or NULL.
   2596  */
   2597 static char *
   2598 ReadHighLevelLine(void)
   2599 {
   2600 	char *line;
   2601 
   2602 	for (;;) {
   2603 		line = ReadLowLevelLine(LK_NONEMPTY);
   2604 		if (line == NULL)
   2605 			return NULL;
   2606 
   2607 		if (line[0] != '.')
   2608 			return line;
   2609 
   2610 		switch (Cond_EvalLine(line)) {
   2611 		case CR_FALSE:	/* May also mean a syntax error. */
   2612 			if (!SkipIrrelevantBranches())
   2613 				return NULL;
   2614 			continue;
   2615 		case CR_TRUE:
   2616 			continue;
   2617 		case CR_ERROR:	/* Not a conditional line */
   2618 			if (ParseForLoop(line))
   2619 				continue;
   2620 			break;
   2621 		}
   2622 		return line;
   2623 	}
   2624 }
   2625 
   2626 static void
   2627 FinishDependencyGroup(void)
   2628 {
   2629 	GNodeListNode *ln;
   2630 
   2631 	if (targets == NULL)
   2632 		return;
   2633 
   2634 	for (ln = targets->first; ln != NULL; ln = ln->next) {
   2635 		GNode *gn = ln->datum;
   2636 
   2637 		Suff_EndTransform(gn);
   2638 
   2639 		/*
   2640 		 * Mark the target as already having commands if it does, to
   2641 		 * keep from having shell commands on multiple dependency
   2642 		 * lines.
   2643 		 */
   2644 		if (!Lst_IsEmpty(&gn->commands))
   2645 			gn->type |= OP_HAS_COMMANDS;
   2646 	}
   2647 
   2648 	Lst_Free(targets);
   2649 	targets = NULL;
   2650 }
   2651 
   2652 /* Add the command to each target from the current dependency spec. */
   2653 static void
   2654 ParseLine_ShellCommand(const char *p)
   2655 {
   2656 	cpp_skip_whitespace(&p);
   2657 	if (*p == '\0')
   2658 		return;		/* skip empty commands */
   2659 
   2660 	if (targets == NULL) {
   2661 		Parse_Error(PARSE_FATAL,
   2662 		    "Unassociated shell command \"%s\"", p);
   2663 		return;
   2664 	}
   2665 
   2666 	{
   2667 		char *cmd = bmake_strdup(p);
   2668 		GNodeListNode *ln;
   2669 
   2670 		for (ln = targets->first; ln != NULL; ln = ln->next) {
   2671 			GNode *gn = ln->datum;
   2672 			GNode_AddCommand(gn, cmd);
   2673 		}
   2674 #ifdef CLEANUP
   2675 		Lst_Append(&targCmds, cmd);
   2676 #endif
   2677 	}
   2678 }
   2679 
   2680 /*
   2681  * See if the line starts with one of the known directives, and if so, handle
   2682  * the directive.
   2683  */
   2684 static bool
   2685 ParseDirective(char *line)
   2686 {
   2687 	char *cp = line + 1;
   2688 	const char *arg;
   2689 	Substring dir;
   2690 
   2691 	pp_skip_whitespace(&cp);
   2692 	if (IsInclude(cp, false)) {
   2693 		ParseInclude(cp);
   2694 		return true;
   2695 	}
   2696 
   2697 	dir.start = cp;
   2698 	while (ch_isalpha(*cp) || *cp == '-')
   2699 		cp++;
   2700 	dir.end = cp;
   2701 
   2702 	if (*cp != '\0' && !ch_isspace(*cp))
   2703 		return false;
   2704 
   2705 	pp_skip_whitespace(&cp);
   2706 	arg = cp;
   2707 
   2708 	if (Substring_Equals(dir, "undef"))
   2709 		Var_Undef(arg);
   2710 	else if (Substring_Equals(dir, "export"))
   2711 		Var_Export(VEM_PLAIN, arg);
   2712 	else if (Substring_Equals(dir, "export-env"))
   2713 		Var_Export(VEM_ENV, arg);
   2714 	else if (Substring_Equals(dir, "export-literal"))
   2715 		Var_Export(VEM_LITERAL, arg);
   2716 	else if (Substring_Equals(dir, "unexport"))
   2717 		Var_UnExport(false, arg);
   2718 	else if (Substring_Equals(dir, "unexport-env"))
   2719 		Var_UnExport(true, arg);
   2720 	else if (Substring_Equals(dir, "info"))
   2721 		HandleMessage(PARSE_INFO, "info", arg);
   2722 	else if (Substring_Equals(dir, "warning"))
   2723 		HandleMessage(PARSE_WARNING, "warning", arg);
   2724 	else if (Substring_Equals(dir, "error"))
   2725 		HandleMessage(PARSE_FATAL, "error", arg);
   2726 	else
   2727 		return false;
   2728 	return true;
   2729 }
   2730 
   2731 bool
   2732 Parse_VarAssign(const char *line, bool finishDependencyGroup, GNode *scope)
   2733 {
   2734 	VarAssign var;
   2735 
   2736 	if (!Parse_IsVar(line, &var))
   2737 		return false;
   2738 	if (finishDependencyGroup)
   2739 		FinishDependencyGroup();
   2740 	Parse_Var(&var, scope);
   2741 	return true;
   2742 }
   2743 
   2744 static char *
   2745 FindSemicolon(char *p)
   2746 {
   2747 	int level = 0;
   2748 
   2749 	for (; *p != '\0'; p++) {
   2750 		if (*p == '\\' && p[1] != '\0') {
   2751 			p++;
   2752 			continue;
   2753 		}
   2754 
   2755 		if (*p == '$' && (p[1] == '(' || p[1] == '{'))
   2756 			level++;
   2757 		else if (level > 0 && (*p == ')' || *p == '}'))
   2758 			level--;
   2759 		else if (level == 0 && *p == ';')
   2760 			break;
   2761 	}
   2762 	return p;
   2763 }
   2764 
   2765 /*
   2766  * dependency	-> target... op [source...] [';' command]
   2767  * op		-> ':' | '::' | '!'
   2768  */
   2769 static void
   2770 ParseDependencyLine(char *line)
   2771 {
   2772 	VarEvalMode emode;
   2773 	char *expanded_line;
   2774 	const char *shellcmd = NULL;
   2775 
   2776 	/*
   2777 	 * For some reason - probably to make the parser impossible -
   2778 	 * a ';' can be used to separate commands from dependencies.
   2779 	 * Attempt to avoid ';' inside substitution patterns.
   2780 	 */
   2781 	{
   2782 		char *semicolon = FindSemicolon(line);
   2783 		if (*semicolon != '\0') {
   2784 			/* Terminate the dependency list at the ';' */
   2785 			*semicolon = '\0';
   2786 			shellcmd = semicolon + 1;
   2787 		}
   2788 	}
   2789 
   2790 	/*
   2791 	 * We now know it's a dependency line so it needs to have all
   2792 	 * variables expanded before being parsed.
   2793 	 *
   2794 	 * XXX: Ideally the dependency line would first be split into
   2795 	 * its left-hand side, dependency operator and right-hand side,
   2796 	 * and then each side would be expanded on its own.  This would
   2797 	 * allow for the left-hand side to allow only defined variables
   2798 	 * and to allow variables on the right-hand side to be undefined
   2799 	 * as well.
   2800 	 *
   2801 	 * Parsing the line first would also prevent that targets
   2802 	 * generated from variable expressions are interpreted as the
   2803 	 * dependency operator, such as in "target${:U\:} middle: source",
   2804 	 * in which the middle is interpreted as a source, not a target.
   2805 	 */
   2806 
   2807 	/*
   2808 	 * In lint mode, allow undefined variables to appear in dependency
   2809 	 * lines.
   2810 	 *
   2811 	 * Ideally, only the right-hand side would allow undefined variables
   2812 	 * since it is common to have optional dependencies. Having undefined
   2813 	 * variables on the left-hand side is more unusual though.  Since
   2814 	 * both sides are expanded in a single pass, there is not much choice
   2815 	 * what to do here.
   2816 	 *
   2817 	 * In normal mode, it does not matter whether undefined variables are
   2818 	 * allowed or not since as of 2020-09-14, Var_Parse does not print
   2819 	 * any parse errors in such a case. It simply returns the special
   2820 	 * empty string var_Error, which cannot be detected in the result of
   2821 	 * Var_Subst.
   2822 	 */
   2823 	emode = opts.strict ? VARE_WANTRES : VARE_UNDEFERR;
   2824 	(void)Var_Subst(line, SCOPE_CMDLINE, emode, &expanded_line);
   2825 	/* TODO: handle errors */
   2826 
   2827 	/* Need a fresh list for the target nodes */
   2828 	if (targets != NULL)
   2829 		Lst_Free(targets);
   2830 	targets = Lst_New();
   2831 
   2832 	ParseDependency(expanded_line);
   2833 	free(expanded_line);
   2834 
   2835 	if (shellcmd != NULL)
   2836 		ParseLine_ShellCommand(shellcmd);
   2837 }
   2838 
   2839 static void
   2840 ParseLine(char *line)
   2841 {
   2842 	/*
   2843 	 * Lines that begin with '.' can be pretty much anything:
   2844 	 *	- directives like '.include' or '.if',
   2845 	 *	- suffix rules like '.c.o:',
   2846 	 *	- dependencies for filenames that start with '.',
   2847 	 *	- variable assignments like '.tmp=value'.
   2848 	 */
   2849 	if (line[0] == '.' && ParseDirective(line))
   2850 		return;
   2851 
   2852 	if (line[0] == '\t') {
   2853 		ParseLine_ShellCommand(line + 1);
   2854 		return;
   2855 	}
   2856 
   2857 #ifdef SYSVINCLUDE
   2858 	if (IsSysVInclude(line)) {
   2859 		/*
   2860 		 * It's an S3/S5-style "include".
   2861 		 */
   2862 		ParseTraditionalInclude(line);
   2863 		return;
   2864 	}
   2865 #endif
   2866 
   2867 #ifdef GMAKEEXPORT
   2868 	if (strncmp(line, "export", 6) == 0 && ch_isspace(line[6]) &&
   2869 	    strchr(line, ':') == NULL) {
   2870 		/*
   2871 		 * It's a Gmake "export".
   2872 		 */
   2873 		ParseGmakeExport(line);
   2874 		return;
   2875 	}
   2876 #endif
   2877 
   2878 	if (Parse_VarAssign(line, true, SCOPE_GLOBAL))
   2879 		return;
   2880 
   2881 	FinishDependencyGroup();
   2882 
   2883 	ParseDependencyLine(line);
   2884 }
   2885 
   2886 /*
   2887  * Parse a top-level makefile, incorporating its content into the global
   2888  * dependency graph.
   2889  */
   2890 void
   2891 Parse_File(const char *name, int fd)
   2892 {
   2893 	char *line;
   2894 	Buffer buf;
   2895 
   2896 	buf = loadfile(name, fd != -1 ? fd : STDIN_FILENO);
   2897 	if (fd != -1)
   2898 		(void)close(fd);
   2899 
   2900 	assert(targets == NULL);
   2901 
   2902 	Parse_PushInput(name, 0, buf, NULL);
   2903 
   2904 	do {
   2905 		while ((line = ReadHighLevelLine()) != NULL) {
   2906 			DEBUG2(PARSE, "Parsing line %d: %s\n",
   2907 			    CurFile()->lineno, line);
   2908 			ParseLine(line);
   2909 		}
   2910 		/* Reached EOF, but it may be just EOF of an include file. */
   2911 	} while (ParseEOF());
   2912 
   2913 	FinishDependencyGroup();
   2914 
   2915 	if (parseErrors != 0) {
   2916 		(void)fflush(stdout);
   2917 		(void)fprintf(stderr,
   2918 		    "%s: Fatal errors encountered -- cannot continue",
   2919 		    progname);
   2920 		PrintOnError(NULL, NULL);
   2921 		exit(1);
   2922 	}
   2923 }
   2924 
   2925 /* Initialize the parsing module. */
   2926 void
   2927 Parse_Init(void)
   2928 {
   2929 	mainNode = NULL;
   2930 	parseIncPath = SearchPath_New();
   2931 	sysIncPath = SearchPath_New();
   2932 	defSysIncPath = SearchPath_New();
   2933 	Vector_Init(&includes, sizeof(IFile));
   2934 }
   2935 
   2936 /* Clean up the parsing module. */
   2937 void
   2938 Parse_End(void)
   2939 {
   2940 #ifdef CLEANUP
   2941 	Lst_DoneCall(&targCmds, free);
   2942 	assert(targets == NULL);
   2943 	SearchPath_Free(defSysIncPath);
   2944 	SearchPath_Free(sysIncPath);
   2945 	SearchPath_Free(parseIncPath);
   2946 	assert(includes.len == 0);
   2947 	Vector_Done(&includes);
   2948 #endif
   2949 }
   2950 
   2951 
   2952 /*
   2953  * Return a list containing the single main target to create.
   2954  * If no such target exists, we Punt with an obnoxious error message.
   2955  */
   2956 void
   2957 Parse_MainName(GNodeList *mainList)
   2958 {
   2959 	if (mainNode == NULL)
   2960 		Punt("no target to make.");
   2961 
   2962 	Lst_Append(mainList, mainNode);
   2963 	if (mainNode->type & OP_DOUBLEDEP)
   2964 		Lst_AppendAll(mainList, &mainNode->cohorts);
   2965 
   2966 	Global_Append(".TARGETS", mainNode->name);
   2967 }
   2968 
   2969 int
   2970 Parse_NumErrors(void)
   2971 {
   2972 	return parseErrors;
   2973 }
   2974