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