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