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