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