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