xref: /netbsd-src/usr.bin/make/parse.c (revision e5d758f832e07a177fa24707c434b7ce26d0f762)
1 /*	$NetBSD: parse.c,v 1.469 2020/12/05 19:46:04 rillig Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1989 by Berkeley Softworks
37  * All rights reserved.
38  *
39  * This code is derived from software contributed to Berkeley by
40  * Adam de Boor.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *	This product includes software developed by the University of
53  *	California, Berkeley and its contributors.
54  * 4. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  */
70 
71 /*
72  * Parsing of makefiles.
73  *
74  * Parse_File is the main entry point and controls most of the other
75  * functions in this module.
76  *
77  * The directories for the .include "..." directive are kept in
78  * 'parseIncPath', while those for .include <...> are kept in 'sysIncPath'.
79  * The targets currently being defined are kept in 'targets'.
80  *
81  * Interface:
82  *	Parse_Init	Initialize the module
83  *
84  *	Parse_End	Clean up the module
85  *
86  *	Parse_File	Parse a top-level makefile.  Included files are
87  *			handled by Parse_include_file though.
88  *
89  *	Parse_IsVar	Return TRUE if the given line is a variable
90  *			assignment. Used by MainParseArgs to determine if
91  *			an argument is a target or a variable assignment.
92  *			Used internally for pretty much the same thing.
93  *
94  *	Parse_Error	Report a parse error, a warning or an informational
95  *			message.
96  *
97  *	Parse_MainName	Returns a list of the main target to create.
98  */
99 
100 #include <sys/types.h>
101 #include <sys/mman.h>
102 #include <sys/stat.h>
103 #include <errno.h>
104 #include <stdarg.h>
105 #include <stdint.h>
106 
107 #ifndef MAP_FILE
108 #define MAP_FILE 0
109 #endif
110 #ifndef MAP_COPY
111 #define MAP_COPY MAP_PRIVATE
112 #endif
113 
114 #include "make.h"
115 #include "dir.h"
116 #include "job.h"
117 #include "pathnames.h"
118 
119 /*	"@(#)parse.c	8.3 (Berkeley) 3/19/94"	*/
120 MAKE_RCSID("$NetBSD: parse.c,v 1.469 2020/12/05 19:46:04 rillig Exp $");
121 
122 /* types and constants */
123 
124 /*
125  * Structure for a file being read ("included file")
126  */
127 typedef struct IFile {
128 	char *fname;		/* name of file (relative? absolute?) */
129 	Boolean fromForLoop;	/* simulated .include by the .for loop */
130 	int lineno;		/* current line number in file */
131 	int first_lineno;	/* line number of start of text */
132 	unsigned int cond_depth; /* 'if' nesting when file opened */
133 	Boolean depending;	/* state of doing_depend on EOF */
134 
135 	/* The buffer from which the file's content is read. */
136 	char *buf_freeIt;
137 	char *buf_ptr;		/* next char to be read */
138 	char *buf_end;
139 
140 	char *(*nextbuf)(void *, size_t *); /* Function to get more data */
141 	void *nextbuf_arg;	/* Opaque arg for nextbuf() */
142 	struct loadedfile *lf;	/* loadedfile object, if any */
143 } IFile;
144 
145 /*
146  * Tokens for target attributes
147  */
148 typedef enum ParseSpecial {
149 	SP_ATTRIBUTE,	/* Generic attribute */
150 	SP_BEGIN,	/* .BEGIN */
151 	SP_DEFAULT,	/* .DEFAULT */
152 	SP_DELETE_ON_ERROR, /* .DELETE_ON_ERROR */
153 	SP_END,		/* .END */
154 	SP_ERROR,	/* .ERROR */
155 	SP_IGNORE,	/* .IGNORE */
156 	SP_INCLUDES,	/* .INCLUDES; not mentioned in the manual page */
157 	SP_INTERRUPT,	/* .INTERRUPT */
158 	SP_LIBS,	/* .LIBS; not mentioned in the manual page */
159 	/* .MAIN and we don't have anything user-specified to make */
160 	SP_MAIN,
161 	SP_META,	/* .META */
162 	SP_MFLAGS,	/* .MFLAGS or .MAKEFLAGS */
163 	SP_NOMETA,	/* .NOMETA */
164 	SP_NOMETA_CMP,	/* .NOMETA_CMP */
165 	SP_NOPATH,	/* .NOPATH */
166 	SP_NOT,		/* Not special */
167 	SP_NOTPARALLEL,	/* .NOTPARALLEL or .NO_PARALLEL */
168 	SP_NULL,	/* .NULL; not mentioned in the manual page */
169 	SP_OBJDIR,	/* .OBJDIR */
170 	SP_ORDER,	/* .ORDER */
171 	SP_PARALLEL,	/* .PARALLEL; not mentioned in the manual page */
172 	SP_PATH,	/* .PATH or .PATH.suffix */
173 	SP_PHONY,	/* .PHONY */
174 #ifdef POSIX
175 	SP_POSIX,	/* .POSIX; not mentioned in the manual page */
176 #endif
177 	SP_PRECIOUS,	/* .PRECIOUS */
178 	SP_SHELL,	/* .SHELL */
179 	SP_SILENT,	/* .SILENT */
180 	SP_SINGLESHELL,	/* .SINGLESHELL; not mentioned in the manual page */
181 	SP_STALE,	/* .STALE */
182 	SP_SUFFIXES,	/* .SUFFIXES */
183 	SP_WAIT		/* .WAIT */
184 } ParseSpecial;
185 
186 typedef List SearchPathList;
187 typedef ListNode SearchPathListNode;
188 
189 /* result data */
190 
191 /*
192  * The main target to create. This is the first target on the first
193  * dependency line in the first makefile.
194  */
195 static GNode *mainNode;
196 
197 /* eval state */
198 
199 /* During parsing, the targets from the left-hand side of the currently
200  * active dependency line, or NULL if the current line does not belong to a
201  * dependency line, for example because it is a variable assignment.
202  *
203  * See unit-tests/deptgt.mk, keyword "parse.c:targets". */
204 static GNodeList *targets;
205 
206 #ifdef CLEANUP
207 /* All shell commands for all targets, in no particular order and possibly
208  * with duplicates.  Kept in a separate list since the commands from .USE or
209  * .USEBEFORE nodes are shared with other GNodes, thereby giving up the
210  * easily understandable ownership over the allocated strings. */
211 static StringList targCmds = LST_INIT;
212 #endif
213 
214 /*
215  * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
216  * seen, then set to each successive source on the line.
217  */
218 static GNode *order_pred;
219 
220 /* parser state */
221 
222 /* number of fatal errors */
223 static int fatals = 0;
224 
225 /*
226  * Variables for doing includes
227  */
228 
229 /* The include chain of makefiles.  At the bottom is the top-level makefile
230  * from the command line, and on top of that, there are the included files or
231  * .for loops, up to and including the current file.
232  *
233  * This data could be used to print stack traces on parse errors.  As of
234  * 2020-09-14, this is not done though.  It seems quite simple to print the
235  * tuples (fname:lineno:fromForLoop), from top to bottom.  This simple idea is
236  * made complicated by the fact that the .for loops also use this stack for
237  * storing information.
238  *
239  * The lineno fields of the IFiles with fromForLoop == TRUE look confusing,
240  * which is demonstrated by the test 'include-main.mk'.  They seem sorted
241  * backwards since they tell the number of completely parsed lines, which for
242  * a .for loop is right after the terminating .endfor.  To compensate for this
243  * confusion, there is another field first_lineno pointing at the start of the
244  * .for loop, 1-based for human consumption.
245  *
246  * To make the stack trace intuitive, the entry below the first .for loop must
247  * be ignored completely since neither its lineno nor its first_lineno is
248  * useful.  Instead, the topmost of each chain of .for loop needs to be
249  * printed twice, once with its first_lineno and once with its lineno.
250  *
251  * As of 2020-10-28, using the above rules, the stack trace for the .info line
252  * in include-subsub.mk would be:
253  *
254  *	includes[5]:	include-subsub.mk:4
255  *			(lineno, from an .include)
256  *	includes[4]:	include-sub.mk:32
257  *			(lineno, from a .for loop below an .include)
258  *	includes[4]:	include-sub.mk:31
259  *			(first_lineno, from a .for loop, lineno == 32)
260  *	includes[3]:	include-sub.mk:30
261  *			(first_lineno, from a .for loop, lineno == 33)
262  *	includes[2]:	include-sub.mk:29
263  *			(first_lineno, from a .for loop, lineno == 34)
264  *	includes[1]:	include-sub.mk:35
265  *			(not printed since it is below a .for loop)
266  *	includes[0]:	include-main.mk:27
267  */
268 static Vector /* of IFile */ includes;
269 
270 static IFile *
271 GetInclude(size_t i)
272 {
273 	return Vector_Get(&includes, i);
274 }
275 
276 /* The file that is currently being read. */
277 static IFile *
278 CurFile(void)
279 {
280 	return GetInclude(includes.len - 1);
281 }
282 
283 /* include paths */
284 SearchPath *parseIncPath;	/* dirs for "..." includes */
285 SearchPath *sysIncPath;		/* dirs for <...> includes */
286 SearchPath *defSysIncPath;	/* default for sysIncPath */
287 
288 /* parser tables */
289 
290 /*
291  * The parseKeywords table is searched using binary search when deciding
292  * if a target or source is special. The 'spec' field is the ParseSpecial
293  * type of the keyword (SP_NOT if the keyword isn't special as a target) while
294  * the 'op' field is the operator to apply to the list of targets if the
295  * keyword is used as a source ("0" if the keyword isn't special as a source)
296  */
297 static const struct {
298 	const char *name;	/* Name of keyword */
299 	ParseSpecial spec;	/* Type when used as a target */
300 	GNodeType op;		/* Operator when used as a source */
301 } parseKeywords[] = {
302     { ".BEGIN",		SP_BEGIN,	OP_NONE },
303     { ".DEFAULT",	SP_DEFAULT,	OP_NONE },
304     { ".DELETE_ON_ERROR", SP_DELETE_ON_ERROR, OP_NONE },
305     { ".END",		SP_END,		OP_NONE },
306     { ".ERROR",		SP_ERROR,	OP_NONE },
307     { ".EXEC",		SP_ATTRIBUTE,	OP_EXEC },
308     { ".IGNORE",	SP_IGNORE,	OP_IGNORE },
309     { ".INCLUDES",	SP_INCLUDES,	OP_NONE },
310     { ".INTERRUPT",	SP_INTERRUPT,	OP_NONE },
311     { ".INVISIBLE",	SP_ATTRIBUTE,	OP_INVISIBLE },
312     { ".JOIN",		SP_ATTRIBUTE,	OP_JOIN },
313     { ".LIBS",		SP_LIBS,	OP_NONE },
314     { ".MADE",		SP_ATTRIBUTE,	OP_MADE },
315     { ".MAIN",		SP_MAIN,	OP_NONE },
316     { ".MAKE",		SP_ATTRIBUTE,	OP_MAKE },
317     { ".MAKEFLAGS",	SP_MFLAGS,	OP_NONE },
318     { ".META",		SP_META,	OP_META },
319     { ".MFLAGS",	SP_MFLAGS,	OP_NONE },
320     { ".NOMETA",	SP_NOMETA,	OP_NOMETA },
321     { ".NOMETA_CMP",	SP_NOMETA_CMP,	OP_NOMETA_CMP },
322     { ".NOPATH",	SP_NOPATH,	OP_NOPATH },
323     { ".NOTMAIN",	SP_ATTRIBUTE,	OP_NOTMAIN },
324     { ".NOTPARALLEL",	SP_NOTPARALLEL,	OP_NONE },
325     { ".NO_PARALLEL",	SP_NOTPARALLEL,	OP_NONE },
326     { ".NULL",		SP_NULL,	OP_NONE },
327     { ".OBJDIR",	SP_OBJDIR,	OP_NONE },
328     { ".OPTIONAL",	SP_ATTRIBUTE,	OP_OPTIONAL },
329     { ".ORDER",		SP_ORDER,	OP_NONE },
330     { ".PARALLEL",	SP_PARALLEL,	OP_NONE },
331     { ".PATH",		SP_PATH,	OP_NONE },
332     { ".PHONY",		SP_PHONY,	OP_PHONY },
333 #ifdef POSIX
334     { ".POSIX",		SP_POSIX,	OP_NONE },
335 #endif
336     { ".PRECIOUS",	SP_PRECIOUS,	OP_PRECIOUS },
337     { ".RECURSIVE",	SP_ATTRIBUTE,	OP_MAKE },
338     { ".SHELL",		SP_SHELL,	OP_NONE },
339     { ".SILENT",	SP_SILENT,	OP_SILENT },
340     { ".SINGLESHELL",	SP_SINGLESHELL,	OP_NONE },
341     { ".STALE",		SP_STALE,	OP_NONE },
342     { ".SUFFIXES",	SP_SUFFIXES,	OP_NONE },
343     { ".USE",		SP_ATTRIBUTE,	OP_USE },
344     { ".USEBEFORE",	SP_ATTRIBUTE,	OP_USEBEFORE },
345     { ".WAIT",		SP_WAIT,	OP_NONE },
346 };
347 
348 /* file loader */
349 
350 struct loadedfile {
351 	/* XXX: What is the lifetime of this path? Who manages the memory? */
352 	const char *path;	/* name, for error reports */
353 	char *buf;		/* contents buffer */
354 	size_t len;		/* length of contents */
355 	size_t maplen;		/* length of mmap area, or 0 */
356 	Boolean used;		/* XXX: have we used the data yet */
357 };
358 
359 /* XXX: What is the lifetime of the path? Who manages the memory? */
360 static struct loadedfile *
361 loadedfile_create(const char *path)
362 {
363 	struct loadedfile *lf;
364 
365 	lf = bmake_malloc(sizeof *lf);
366 	lf->path = path == NULL ? "(stdin)" : path;
367 	lf->buf = NULL;
368 	lf->len = 0;
369 	lf->maplen = 0;
370 	lf->used = FALSE;
371 	return lf;
372 }
373 
374 static void
375 loadedfile_destroy(struct loadedfile *lf)
376 {
377 	if (lf->buf != NULL) {
378 		if (lf->maplen > 0)
379 			munmap(lf->buf, lf->maplen);
380 		else
381 			free(lf->buf);
382 	}
383 	free(lf);
384 }
385 
386 /*
387  * nextbuf() operation for loadedfile, as needed by the weird and twisted
388  * logic below. Once that's cleaned up, we can get rid of lf->used...
389  */
390 static char *
391 loadedfile_nextbuf(void *x, size_t *len)
392 {
393 	struct loadedfile *lf = x;
394 
395 	if (lf->used)
396 		return NULL;
397 
398 	lf->used = TRUE;
399 	*len = lf->len;
400 	return lf->buf;
401 }
402 
403 /*
404  * Try to get the size of a file.
405  */
406 static Boolean
407 load_getsize(int fd, size_t *ret)
408 {
409 	struct stat st;
410 
411 	if (fstat(fd, &st) < 0)
412 		return FALSE;
413 
414 	if (!S_ISREG(st.st_mode))
415 		return FALSE;
416 
417 	/*
418 	 * st_size is an off_t, which is 64 bits signed; *ret is
419 	 * size_t, which might be 32 bits unsigned or 64 bits
420 	 * unsigned. Rather than being elaborate, just punt on
421 	 * files that are more than 2^31 bytes. We should never
422 	 * see a makefile that size in practice...
423 	 *
424 	 * While we're at it reject negative sizes too, just in case.
425 	 */
426 	if (st.st_size < 0 || st.st_size > 0x7fffffff)
427 		return FALSE;
428 
429 	*ret = (size_t)st.st_size;
430 	return TRUE;
431 }
432 
433 static Boolean
434 loadedfile_mmap(struct loadedfile *lf, int fd)
435 {
436 	static unsigned long pagesize = 0;
437 
438 	if (!load_getsize(fd, &lf->len))
439 		return FALSE;
440 
441 	/* found a size, try mmap */
442 	if (pagesize == 0)
443 		pagesize = (unsigned long)sysconf(_SC_PAGESIZE);
444 	if (pagesize == 0 || pagesize == (unsigned long)-1)
445 		pagesize = 0x1000;
446 
447 	/* round size up to a page */
448 	lf->maplen = pagesize * ((lf->len + pagesize - 1) / pagesize);
449 
450 	/*
451 	 * XXX hack for dealing with empty files; remove when
452 	 * we're no longer limited by interfacing to the old
453 	 * logic elsewhere in this file.
454 	 */
455 	if (lf->maplen == 0)
456 		lf->maplen = pagesize;
457 
458 	/*
459 	 * FUTURE: remove PROT_WRITE when the parser no longer
460 	 * needs to scribble on the input.
461 	 */
462 	lf->buf = mmap(NULL, lf->maplen, PROT_READ | PROT_WRITE,
463 	    MAP_FILE | MAP_COPY, fd, 0);
464 	if (lf->buf == MAP_FAILED)
465 		return FALSE;
466 
467 	if (lf->len == lf->maplen && lf->buf[lf->len - 1] != '\n') {
468 		char *b = bmake_malloc(lf->len + 1);
469 		b[lf->len] = '\n';
470 		memcpy(b, lf->buf, lf->len++);
471 		munmap(lf->buf, lf->maplen);
472 		lf->maplen = 0;
473 		lf->buf = b;
474 	}
475 
476 	return TRUE;
477 }
478 
479 /*
480  * Read in a file.
481  *
482  * Until the path search logic can be moved under here instead of
483  * being in the caller in another source file, we need to have the fd
484  * passed in already open. Bleh.
485  *
486  * If the path is NULL, use stdin.
487  */
488 static struct loadedfile *
489 loadfile(const char *path, int fd)
490 {
491 	struct loadedfile *lf;
492 	ssize_t result;
493 	size_t bufpos;
494 
495 	lf = loadedfile_create(path);
496 
497 	if (path == NULL) {
498 		assert(fd == -1);
499 		fd = STDIN_FILENO;
500 	} else {
501 #if 0 /* notyet */
502 		fd = open(path, O_RDONLY);
503 		if (fd < 0) {
504 			...
505 			Error("%s: %s", path, strerror(errno));
506 			exit(1);
507 		}
508 #endif
509 	}
510 
511 	if (loadedfile_mmap(lf, fd))
512 		goto done;
513 
514 	/* cannot mmap; load the traditional way */
515 
516 	lf->maplen = 0;
517 	lf->len = 1024;
518 	lf->buf = bmake_malloc(lf->len);
519 
520 	bufpos = 0;
521 	for (;;) {
522 		assert(bufpos <= lf->len);
523 		if (bufpos == lf->len) {
524 			if (lf->len > SIZE_MAX / 2) {
525 				errno = EFBIG;
526 				Error("%s: file too large", path);
527 				exit(1);
528 			}
529 			lf->len *= 2;
530 			lf->buf = bmake_realloc(lf->buf, lf->len);
531 		}
532 		assert(bufpos < lf->len);
533 		result = read(fd, lf->buf + bufpos, lf->len - bufpos);
534 		if (result < 0) {
535 			Error("%s: read error: %s", path, strerror(errno));
536 			exit(1);
537 		}
538 		if (result == 0)
539 			break;
540 
541 		bufpos += (size_t)result;
542 	}
543 	assert(bufpos <= lf->len);
544 	lf->len = bufpos;
545 
546 	/* truncate malloc region to actual length (maybe not useful) */
547 	if (lf->len > 0) {
548 		/* as for mmap case, ensure trailing \n */
549 		if (lf->buf[lf->len - 1] != '\n')
550 			lf->len++;
551 		lf->buf = bmake_realloc(lf->buf, lf->len);
552 		lf->buf[lf->len - 1] = '\n';
553 	}
554 
555 done:
556 	if (path != NULL)
557 		close(fd);
558 
559 	return lf;
560 }
561 
562 /* old code */
563 
564 /* Check if the current character is escaped on the current line. */
565 static Boolean
566 ParseIsEscaped(const char *line, const char *c)
567 {
568 	Boolean active = FALSE;
569 	for (;;) {
570 		if (line == c)
571 			return active;
572 		if (*--c != '\\')
573 			return active;
574 		active = !active;
575 	}
576 }
577 
578 /* Add the filename and lineno to the GNode so that we remember where it
579  * was first defined. */
580 static void
581 ParseMark(GNode *gn)
582 {
583 	IFile *curFile = CurFile();
584 	gn->fname = curFile->fname;
585 	gn->lineno = curFile->lineno;
586 }
587 
588 /* Look in the table of keywords for one matching the given string.
589  * Return the index of the keyword, or -1 if it isn't there. */
590 static int
591 ParseFindKeyword(const char *str)
592 {
593 	int start = 0;
594 	int end = sizeof parseKeywords / sizeof parseKeywords[0] - 1;
595 
596 	do {
597 		int curr = start + (end - start) / 2;
598 		int diff = strcmp(str, parseKeywords[curr].name);
599 
600 		if (diff == 0)
601 			return curr;
602 		if (diff < 0)
603 			end = curr - 1;
604 		else
605 			start = curr + 1;
606 	} while (start <= end);
607 
608 	return -1;
609 }
610 
611 static void
612 PrintLocation(FILE *f, const char *fname, size_t lineno)
613 {
614 	char dirbuf[MAXPATHLEN + 1];
615 	const char *dir, *base;
616 	void *dir_freeIt, *base_freeIt;
617 
618 	if (*fname == '/' || strcmp(fname, "(stdin)") == 0) {
619 		(void)fprintf(f, "\"%s\" line %zu: ", fname, lineno);
620 		return;
621 	}
622 
623 	/* Find out which makefile is the culprit.
624 	 * We try ${.PARSEDIR} and apply realpath(3) if not absolute. */
625 
626 	dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &dir_freeIt);
627 	if (dir == NULL)
628 		dir = ".";
629 	if (*dir != '/')
630 		dir = realpath(dir, dirbuf);
631 
632 	base = Var_Value(".PARSEFILE", VAR_GLOBAL, &base_freeIt);
633 	if (base == NULL) {
634 		const char *slash = strrchr(fname, '/');
635 		base = slash != NULL ? slash + 1 : fname;
636 	}
637 
638 	(void)fprintf(f, "\"%s/%s\" line %zu: ", dir, base, lineno);
639 	bmake_free(base_freeIt);
640 	bmake_free(dir_freeIt);
641 }
642 
643 static void
644 ParseVErrorInternal(FILE *f, const char *fname, size_t lineno,
645 		    ParseErrorLevel type, const char *fmt, va_list ap)
646 {
647 	static Boolean fatal_warning_error_printed = FALSE;
648 
649 	(void)fprintf(f, "%s: ", progname);
650 
651 	if (fname != NULL)
652 		PrintLocation(f, fname, lineno);
653 	if (type == PARSE_WARNING)
654 		(void)fprintf(f, "warning: ");
655 	(void)vfprintf(f, fmt, ap);
656 	(void)fprintf(f, "\n");
657 	(void)fflush(f);
658 
659 	if (type == PARSE_INFO)
660 		return;
661 	if (type == PARSE_FATAL || opts.parseWarnFatal)
662 		fatals++;
663 	if (opts.parseWarnFatal && !fatal_warning_error_printed) {
664 		Error("parsing warnings being treated as errors");
665 		fatal_warning_error_printed = TRUE;
666 	}
667 }
668 
669 static void
670 ParseErrorInternal(const char *fname, size_t lineno,
671 		   ParseErrorLevel type, const char *fmt, ...)
672 {
673 	va_list ap;
674 
675 	(void)fflush(stdout);
676 	va_start(ap, fmt);
677 	ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
678 	va_end(ap);
679 
680 	if (opts.debug_file != stderr && opts.debug_file != stdout) {
681 		va_start(ap, fmt);
682 		ParseVErrorInternal(opts.debug_file, fname, lineno, type,
683 		    fmt, ap);
684 		va_end(ap);
685 	}
686 }
687 
688 /* Print a parse error message, including location information.
689  *
690  * If the level is PARSE_FATAL, continue parsing until the end of the
691  * current top-level makefile, then exit (see Parse_File).
692  *
693  * Fmt is given without a trailing newline. */
694 void
695 Parse_Error(ParseErrorLevel type, const char *fmt, ...)
696 {
697 	va_list ap;
698 	const char *fname;
699 	size_t lineno;
700 
701 	if (includes.len == 0) {
702 		fname = NULL;
703 		lineno = 0;
704 	} else {
705 		IFile *curFile = CurFile();
706 		fname = curFile->fname;
707 		lineno = (size_t)curFile->lineno;
708 	}
709 
710 	va_start(ap, fmt);
711 	(void)fflush(stdout);
712 	ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
713 	va_end(ap);
714 
715 	if (opts.debug_file != stderr && opts.debug_file != stdout) {
716 		va_start(ap, fmt);
717 		ParseVErrorInternal(opts.debug_file, fname, lineno, type,
718 		    fmt, ap);
719 		va_end(ap);
720 	}
721 }
722 
723 
724 /* Parse and handle a .info, .warning or .error directive.
725  * For an .error directive, immediately exit. */
726 static Boolean
727 ParseMessage(const char *directive)
728 {
729 	const char *p = directive;
730 	ParseErrorLevel mtype = *p == 'i' ? PARSE_INFO :
731 	    *p == 'w' ? PARSE_WARNING : PARSE_FATAL;
732 	char *arg;
733 
734 	while (ch_isalpha(*p))
735 		p++;
736 	if (!ch_isspace(*p))
737 		return FALSE;	/* missing argument */
738 
739 	cpp_skip_whitespace(&p);
740 	(void)Var_Subst(p, VAR_CMDLINE, VARE_WANTRES, &arg);
741 	/* TODO: handle errors */
742 
743 	Parse_Error(mtype, "%s", arg);
744 	free(arg);
745 
746 	if (mtype == PARSE_FATAL) {
747 		PrintOnError(NULL, NULL);
748 		exit(1);
749 	}
750 	return TRUE;
751 }
752 
753 /* Add the child to the parent's children.
754  *
755  * Additionally, add the parent to the child's parents, but only if the
756  * target is not special.  An example for such a special target is .END,
757  * which does not need to be informed once the child target has been made. */
758 static void
759 LinkSource(GNode *pgn, GNode *cgn, Boolean isSpecial)
760 {
761 	if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(&pgn->cohorts))
762 		pgn = pgn->cohorts.last->datum;
763 
764 	Lst_Append(&pgn->children, cgn);
765 	pgn->unmade++;
766 
767 	/* Special targets like .END don't need any children. */
768 	if (!isSpecial)
769 		Lst_Append(&cgn->parents, pgn);
770 
771 	if (DEBUG(PARSE)) {
772 		debug_printf("# %s: added child %s - %s\n",
773 		    __func__, pgn->name, cgn->name);
774 		Targ_PrintNode(pgn, 0);
775 		Targ_PrintNode(cgn, 0);
776 	}
777 }
778 
779 /* Add the node to each target from the current dependency group. */
780 static void
781 LinkToTargets(GNode *gn, Boolean isSpecial)
782 {
783 	GNodeListNode *ln;
784 
785 	for (ln = targets->first; ln != NULL; ln = ln->next)
786 		LinkSource(ln->datum, gn, isSpecial);
787 }
788 
789 static Boolean
790 TryApplyDependencyOperator(GNode *gn, GNodeType op)
791 {
792 	/*
793 	 * If the node occurred on the left-hand side of a dependency and the
794 	 * operator also defines a dependency, they must match.
795 	 */
796 	if ((op & OP_OPMASK) && (gn->type & OP_OPMASK) &&
797 	    ((op & OP_OPMASK) != (gn->type & OP_OPMASK))) {
798 		Parse_Error(PARSE_FATAL, "Inconsistent operator for %s",
799 		    gn->name);
800 		return FALSE;
801 	}
802 
803 	if (op == OP_DOUBLEDEP && (gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
804 		/*
805 		 * If the node was of the left-hand side of a '::' operator,
806 		 * we need to create a new instance of it for the children
807 		 * and commands on this dependency line since each of these
808 		 * dependency groups has its own attributes and commands,
809 		 * separate from the others.
810 		 *
811 		 * The new instance is placed on the 'cohorts' list of the
812 		 * initial one (note the initial one is not on its own
813 		 * cohorts list) and the new instance is linked to all
814 		 * parents of the initial instance.
815 		 */
816 		GNode *cohort;
817 
818 		/*
819 		 * Propagate copied bits to the initial node.  They'll be
820 		 * propagated back to the rest of the cohorts later.
821 		 */
822 		gn->type |= op & ~OP_OPMASK;
823 
824 		cohort = Targ_NewInternalNode(gn->name);
825 		if (doing_depend)
826 			ParseMark(cohort);
827 		/*
828 		 * Make the cohort invisible as well to avoid duplicating it
829 		 * into other variables. True, parents of this target won't
830 		 * tend to do anything with their local variables, but better
831 		 * safe than sorry.
832 		 *
833 		 * (I think this is pointless now, since the relevant list
834 		 * traversals will no longer see this node anyway. -mycroft)
835 		 */
836 		cohort->type = op | OP_INVISIBLE;
837 		Lst_Append(&gn->cohorts, cohort);
838 		cohort->centurion = gn;
839 		gn->unmade_cohorts++;
840 		snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
841 		    (unsigned int)gn->unmade_cohorts % 1000000);
842 	} else {
843 		/*
844 		 * We don't want to nuke any previous flags (whatever they
845 		 * were) so we just OR the new operator into the old.
846 		 */
847 		gn->type |= op;
848 	}
849 
850 	return TRUE;
851 }
852 
853 static void
854 ApplyDependencyOperator(GNodeType op)
855 {
856 	GNodeListNode *ln;
857 
858 	for (ln = targets->first; ln != NULL; ln = ln->next)
859 		if (!TryApplyDependencyOperator(ln->datum, op))
860 			break;
861 }
862 
863 /*
864  * We add a .WAIT node in the dependency list. After any dynamic dependencies
865  * (and filename globbing) have happened, it is given a dependency on each
866  * previous child, back until the previous .WAIT node. The next child won't
867  * be scheduled until the .WAIT node is built.
868  *
869  * We give each .WAIT node a unique name (mainly for diagnostics).
870  */
871 static void
872 ParseDependencySourceWait(Boolean isSpecial)
873 {
874 	static int wait_number = 0;
875 	char wait_src[16];
876 	GNode *gn;
877 
878 	snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
879 	gn = Targ_NewInternalNode(wait_src);
880 	if (doing_depend)
881 		ParseMark(gn);
882 	gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
883 	LinkToTargets(gn, isSpecial);
884 
885 }
886 
887 static Boolean
888 ParseDependencySourceKeyword(const char *src, ParseSpecial specType)
889 {
890 	int keywd;
891 	GNodeType op;
892 
893 	if (*src != '.' || !ch_isupper(src[1]))
894 		return FALSE;
895 
896 	keywd = ParseFindKeyword(src);
897 	if (keywd == -1)
898 		return FALSE;
899 
900 	op = parseKeywords[keywd].op;
901 	if (op != OP_NONE) {
902 		ApplyDependencyOperator(op);
903 		return TRUE;
904 	}
905 	if (parseKeywords[keywd].spec == SP_WAIT) {
906 		ParseDependencySourceWait(specType != SP_NOT);
907 		return TRUE;
908 	}
909 	return FALSE;
910 }
911 
912 static void
913 ParseDependencySourceMain(const char *src)
914 {
915 	/*
916 	 * In a line like ".MAIN: source1 source2", it means we need to add
917 	 * the sources of said target to the list of things to create.
918 	 *
919 	 * Note that this will only be invoked if the user didn't specify a
920 	 * target on the command line and the .MAIN occurs for the first time.
921 	 *
922 	 * See ParseDoDependencyTargetSpecial, branch SP_MAIN.
923 	 * See unit-tests/cond-func-make-main.mk.
924 	 */
925 	Lst_Append(&opts.create, bmake_strdup(src));
926 	/*
927 	 * Add the name to the .TARGETS variable as well, so the user can
928 	 * employ that, if desired.
929 	 */
930 	Var_Append(".TARGETS", src, VAR_GLOBAL);
931 }
932 
933 static void
934 ParseDependencySourceOrder(const char *src)
935 {
936 	GNode *gn;
937 	/*
938 	 * Create proper predecessor/successor links between the previous
939 	 * source and the current one.
940 	 */
941 	gn = Targ_GetNode(src);
942 	if (doing_depend)
943 		ParseMark(gn);
944 	if (order_pred != NULL) {
945 		Lst_Append(&order_pred->order_succ, gn);
946 		Lst_Append(&gn->order_pred, order_pred);
947 		if (DEBUG(PARSE)) {
948 			debug_printf("# %s: added Order dependency %s - %s\n",
949 			    __func__, order_pred->name, gn->name);
950 			Targ_PrintNode(order_pred, 0);
951 			Targ_PrintNode(gn, 0);
952 		}
953 	}
954 	/*
955 	 * The current source now becomes the predecessor for the next one.
956 	 */
957 	order_pred = gn;
958 }
959 
960 static void
961 ParseDependencySourceOther(const char *src, GNodeType tOp,
962 			   ParseSpecial specType)
963 {
964 	GNode *gn;
965 
966 	/*
967 	 * If the source is not an attribute, we need to find/create
968 	 * a node for it. After that we can apply any operator to it
969 	 * from a special target or link it to its parents, as
970 	 * appropriate.
971 	 *
972 	 * In the case of a source that was the object of a :: operator,
973 	 * the attribute is applied to all of its instances (as kept in
974 	 * the 'cohorts' list of the node) or all the cohorts are linked
975 	 * to all the targets.
976 	 */
977 
978 	/* Find/create the 'src' node and attach to all targets */
979 	gn = Targ_GetNode(src);
980 	if (doing_depend)
981 		ParseMark(gn);
982 	if (tOp != OP_NONE)
983 		gn->type |= tOp;
984 	else
985 		LinkToTargets(gn, specType != SP_NOT);
986 }
987 
988 /*
989  * Given the name of a source in a dependency line, figure out if it is an
990  * attribute (such as .SILENT) and apply it to the targets if it is. Else
991  * decide if there is some attribute which should be applied *to* the source
992  * because of some special target (such as .PHONY) and apply it if so.
993  * Otherwise, make the source a child of the targets in the list 'targets'.
994  *
995  * Input:
996  *	tOp		operator (if any) from special targets
997  *	src		name of the source to handle
998  */
999 static void
1000 ParseDependencySource(GNodeType tOp, const char *src, ParseSpecial specType)
1001 {
1002 	if (ParseDependencySourceKeyword(src, specType))
1003 		return;
1004 
1005 	if (specType == SP_MAIN)
1006 		ParseDependencySourceMain(src);
1007 	else if (specType == SP_ORDER)
1008 		ParseDependencySourceOrder(src);
1009 	else
1010 		ParseDependencySourceOther(src, tOp, specType);
1011 }
1012 
1013 /*
1014  * If we have yet to decide on a main target to make, in the absence of any
1015  * user input, we want the first target on the first dependency line that is
1016  * actually a real target (i.e. isn't a .USE or .EXEC rule) to be made.
1017  */
1018 static void
1019 FindMainTarget(void)
1020 {
1021 	GNodeListNode *ln;
1022 
1023 	if (mainNode != NULL)
1024 		return;
1025 
1026 	for (ln = targets->first; ln != NULL; ln = ln->next) {
1027 		GNode *gn = ln->datum;
1028 		if (!(gn->type & OP_NOTARGET)) {
1029 			DEBUG1(MAKE, "Setting main node to \"%s\"\n", gn->name);
1030 			mainNode = gn;
1031 			Targ_SetMain(gn);
1032 			return;
1033 		}
1034 	}
1035 }
1036 
1037 /*
1038  * We got to the end of the line while we were still looking at targets.
1039  *
1040  * Ending a dependency line without an operator is a Bozo no-no.  As a
1041  * heuristic, this is also often triggered by undetected conflicts from
1042  * cvs/rcs merges.
1043  */
1044 static void
1045 ParseErrorNoDependency(const char *lstart)
1046 {
1047 	if ((strncmp(lstart, "<<<<<<", 6) == 0) ||
1048 	    (strncmp(lstart, "======", 6) == 0) ||
1049 	    (strncmp(lstart, ">>>>>>", 6) == 0))
1050 		Parse_Error(PARSE_FATAL,
1051 		    "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
1052 	else if (lstart[0] == '.') {
1053 		const char *dirstart = lstart + 1;
1054 		const char *dirend;
1055 		cpp_skip_whitespace(&dirstart);
1056 		dirend = dirstart;
1057 		while (ch_isalnum(*dirend) || *dirend == '-')
1058 			dirend++;
1059 		Parse_Error(PARSE_FATAL, "Unknown directive \"%.*s\"",
1060 		    (int)(dirend - dirstart), dirstart);
1061 	} else
1062 		Parse_Error(PARSE_FATAL, "Need an operator");
1063 }
1064 
1065 static void
1066 ParseDependencyTargetWord(const char **pp, const char *lstart)
1067 {
1068 	const char *cp = *pp;
1069 
1070 	while (*cp != '\0') {
1071 		if ((ch_isspace(*cp) || *cp == '!' || *cp == ':' ||
1072 		     *cp == '(') &&
1073 		    !ParseIsEscaped(lstart, cp))
1074 			break;
1075 
1076 		if (*cp == '$') {
1077 			/*
1078 			 * Must be a dynamic source (would have been expanded
1079 			 * otherwise), so call the Var module to parse the
1080 			 * puppy so we can safely advance beyond it.
1081 			 *
1082 			 * There should be no errors in this, as they would
1083 			 * have been discovered in the initial Var_Subst and
1084 			 * we wouldn't be here.
1085 			 */
1086 			const char *nested_p = cp;
1087 			const char *nested_val;
1088 			void *freeIt;
1089 
1090 			/* XXX: Why VARE_WANTRES? */
1091 			(void)Var_Parse(&nested_p, VAR_CMDLINE,
1092 			    VARE_WANTRES | VARE_UNDEFERR, &nested_val, &freeIt);
1093 			/* TODO: handle errors */
1094 			free(freeIt);
1095 			cp += nested_p - cp;
1096 		} else
1097 			cp++;
1098 	}
1099 
1100 	*pp = cp;
1101 }
1102 
1103 /* Handle special targets like .PATH, .DEFAULT, .BEGIN, .ORDER. */
1104 static void
1105 ParseDoDependencyTargetSpecial(ParseSpecial *inout_specType,
1106 			       const char *line,
1107 			       SearchPathList **inout_paths)
1108 {
1109 	switch (*inout_specType) {
1110 	case SP_PATH:
1111 		if (*inout_paths == NULL)
1112 			*inout_paths = Lst_New();
1113 		Lst_Append(*inout_paths, &dirSearchPath);
1114 		break;
1115 	case SP_MAIN:
1116 		/*
1117 		 * Allow targets from the command line to override the
1118 		 * .MAIN node.
1119 		 */
1120 		if (!Lst_IsEmpty(&opts.create))
1121 			*inout_specType = SP_NOT;
1122 		break;
1123 	case SP_BEGIN:
1124 	case SP_END:
1125 	case SP_STALE:
1126 	case SP_ERROR:
1127 	case SP_INTERRUPT: {
1128 		GNode *gn = Targ_GetNode(line);
1129 		if (doing_depend)
1130 			ParseMark(gn);
1131 		gn->type |= OP_NOTMAIN | OP_SPECIAL;
1132 		Lst_Append(targets, gn);
1133 		break;
1134 	}
1135 	case SP_DEFAULT: {
1136 		/*
1137 		 * Need to create a node to hang commands on, but we don't
1138 		 * want it in the graph, nor do we want it to be the Main
1139 		 * Target. We claim the node is a transformation rule to make
1140 		 * life easier later, when we'll use Make_HandleUse to
1141 		 * actually apply the .DEFAULT commands.
1142 		 */
1143 		GNode *gn = GNode_New(".DEFAULT");
1144 		gn->type |= OP_NOTMAIN | OP_TRANSFORM;
1145 		Lst_Append(targets, gn);
1146 		defaultNode = gn;
1147 		break;
1148 	}
1149 	case SP_DELETE_ON_ERROR:
1150 		deleteOnError = TRUE;
1151 		break;
1152 	case SP_NOTPARALLEL:
1153 		opts.maxJobs = 1;
1154 		break;
1155 	case SP_SINGLESHELL:
1156 		opts.compatMake = TRUE;
1157 		break;
1158 	case SP_ORDER:
1159 		order_pred = NULL;
1160 		break;
1161 	default:
1162 		break;
1163 	}
1164 }
1165 
1166 /*
1167  * .PATH<suffix> has to be handled specially.
1168  * Call on the suffix module to give us a path to modify.
1169  */
1170 static Boolean
1171 ParseDoDependencyTargetPath(const char *line, SearchPathList **inout_paths)
1172 {
1173 	SearchPath *path;
1174 
1175 	path = Suff_GetPath(&line[5]);
1176 	if (path == NULL) {
1177 		Parse_Error(PARSE_FATAL,
1178 		    "Suffix '%s' not defined (yet)", &line[5]);
1179 		return FALSE;
1180 	}
1181 
1182 	if (*inout_paths == NULL)
1183 		*inout_paths = Lst_New();
1184 	Lst_Append(*inout_paths, path);
1185 
1186 	return TRUE;
1187 }
1188 
1189 /*
1190  * See if it's a special target and if so set specType to match it.
1191  */
1192 static Boolean
1193 ParseDoDependencyTarget(const char *line, ParseSpecial *inout_specType,
1194 			GNodeType *out_tOp, SearchPathList **inout_paths)
1195 {
1196 	int keywd;
1197 
1198 	if (!(*line == '.' && ch_isupper(line[1])))
1199 		return TRUE;
1200 
1201 	/*
1202 	 * See if the target is a special target that must have it
1203 	 * or its sources handled specially.
1204 	 */
1205 	keywd = ParseFindKeyword(line);
1206 	if (keywd != -1) {
1207 		if (*inout_specType == SP_PATH &&
1208 		    parseKeywords[keywd].spec != SP_PATH) {
1209 			Parse_Error(PARSE_FATAL, "Mismatched special targets");
1210 			return FALSE;
1211 		}
1212 
1213 		*inout_specType = parseKeywords[keywd].spec;
1214 		*out_tOp = parseKeywords[keywd].op;
1215 
1216 		ParseDoDependencyTargetSpecial(inout_specType, line,
1217 		    inout_paths);
1218 
1219 	} else if (strncmp(line, ".PATH", 5) == 0) {
1220 		*inout_specType = SP_PATH;
1221 		if (!ParseDoDependencyTargetPath(line, inout_paths))
1222 			return FALSE;
1223 	}
1224 	return TRUE;
1225 }
1226 
1227 static void
1228 ParseDoDependencyTargetMundane(char *line, StringList *curTargs)
1229 {
1230 	if (Dir_HasWildcards(line)) {
1231 		/*
1232 		 * Targets are to be sought only in the current directory,
1233 		 * so create an empty path for the thing. Note we need to
1234 		 * use Dir_Destroy in the destruction of the path as the
1235 		 * Dir module could have added a directory to the path...
1236 		 */
1237 		SearchPath *emptyPath = SearchPath_New();
1238 
1239 		Dir_Expand(line, emptyPath, curTargs);
1240 
1241 		SearchPath_Free(emptyPath);
1242 	} else {
1243 		/*
1244 		 * No wildcards, but we want to avoid code duplication,
1245 		 * so create a list with the word on it.
1246 		 */
1247 		Lst_Append(curTargs, line);
1248 	}
1249 
1250 	/* Apply the targets. */
1251 
1252 	while (!Lst_IsEmpty(curTargs)) {
1253 		char *targName = Lst_Dequeue(curTargs);
1254 		GNode *gn = Suff_IsTransform(targName)
1255 		    ? Suff_AddTransform(targName)
1256 		    : Targ_GetNode(targName);
1257 		if (doing_depend)
1258 			ParseMark(gn);
1259 
1260 		Lst_Append(targets, gn);
1261 	}
1262 }
1263 
1264 static void
1265 ParseDoDependencyTargetExtraWarn(char **pp, const char *lstart)
1266 {
1267 	Boolean warning = FALSE;
1268 	char *cp = *pp;
1269 
1270 	while (*cp != '\0') {
1271 		if (!ParseIsEscaped(lstart, cp) && (*cp == '!' || *cp == ':'))
1272 			break;
1273 		if (ParseIsEscaped(lstart, cp) || (*cp != ' ' && *cp != '\t'))
1274 			warning = TRUE;
1275 		cp++;
1276 	}
1277 	if (warning)
1278 		Parse_Error(PARSE_WARNING, "Extra target ignored");
1279 
1280 	*pp = cp;
1281 }
1282 
1283 static void
1284 ParseDoDependencyCheckSpec(ParseSpecial specType)
1285 {
1286 	switch (specType) {
1287 	default:
1288 		Parse_Error(PARSE_WARNING,
1289 		    "Special and mundane targets don't mix. "
1290 		    "Mundane ones ignored");
1291 		break;
1292 	case SP_DEFAULT:
1293 	case SP_STALE:
1294 	case SP_BEGIN:
1295 	case SP_END:
1296 	case SP_ERROR:
1297 	case SP_INTERRUPT:
1298 		/*
1299 		 * These create nodes on which to hang commands, so targets
1300 		 * shouldn't be empty.
1301 		 */
1302 	case SP_NOT:
1303 		/* Nothing special here -- targets can be empty if it wants. */
1304 		break;
1305 	}
1306 }
1307 
1308 static Boolean
1309 ParseDoDependencyParseOp(char **pp, const char *lstart, GNodeType *out_op)
1310 {
1311 	const char *cp = *pp;
1312 
1313 	if (*cp == '!') {
1314 		*out_op = OP_FORCE;
1315 		(*pp)++;
1316 		return TRUE;
1317 	}
1318 
1319 	if (*cp == ':') {
1320 		if (cp[1] == ':') {
1321 			*out_op = OP_DOUBLEDEP;
1322 			(*pp) += 2;
1323 		} else {
1324 			*out_op = OP_DEPENDS;
1325 			(*pp)++;
1326 		}
1327 		return TRUE;
1328 	}
1329 
1330 	{
1331 		const char *msg = lstart[0] == '.'
1332 		    ? "Unknown directive" : "Missing dependency operator";
1333 		Parse_Error(PARSE_FATAL, "%s", msg);
1334 		return FALSE;
1335 	}
1336 }
1337 
1338 static void
1339 ClearPaths(SearchPathList *paths)
1340 {
1341 	if (paths != NULL) {
1342 		SearchPathListNode *ln;
1343 		for (ln = paths->first; ln != NULL; ln = ln->next)
1344 			SearchPath_Clear(ln->datum);
1345 	}
1346 
1347 	Dir_SetPATH();
1348 }
1349 
1350 static void
1351 ParseDoDependencySourcesEmpty(ParseSpecial specType, SearchPathList *paths)
1352 {
1353 	switch (specType) {
1354 	case SP_SUFFIXES:
1355 		Suff_ClearSuffixes();
1356 		break;
1357 	case SP_PRECIOUS:
1358 		allPrecious = TRUE;
1359 		break;
1360 	case SP_IGNORE:
1361 		opts.ignoreErrors = TRUE;
1362 		break;
1363 	case SP_SILENT:
1364 		opts.beSilent = TRUE;
1365 		break;
1366 	case SP_PATH:
1367 		ClearPaths(paths);
1368 		break;
1369 #ifdef POSIX
1370 	case SP_POSIX:
1371 		Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1372 		break;
1373 #endif
1374 	default:
1375 		break;
1376 	}
1377 }
1378 
1379 static void
1380 AddToPaths(const char *dir, SearchPathList *paths)
1381 {
1382 	if (paths != NULL) {
1383 		SearchPathListNode *ln;
1384 		for (ln = paths->first; ln != NULL; ln = ln->next)
1385 			(void)Dir_AddDir(ln->datum, dir);
1386 	}
1387 }
1388 
1389 /*
1390  * If the target was one that doesn't take files as its sources
1391  * but takes something like suffixes, we take each
1392  * space-separated word on the line as a something and deal
1393  * with it accordingly.
1394  *
1395  * If the target was .SUFFIXES, we take each source as a
1396  * suffix and add it to the list of suffixes maintained by the
1397  * Suff module.
1398  *
1399  * If the target was a .PATH, we add the source as a directory
1400  * to search on the search path.
1401  *
1402  * If it was .INCLUDES, the source is taken to be the suffix of
1403  * files which will be #included and whose search path should
1404  * be present in the .INCLUDES variable.
1405  *
1406  * If it was .LIBS, the source is taken to be the suffix of
1407  * files which are considered libraries and whose search path
1408  * should be present in the .LIBS variable.
1409  *
1410  * If it was .NULL, the source is the suffix to use when a file
1411  * has no valid suffix.
1412  *
1413  * If it was .OBJDIR, the source is a new definition for .OBJDIR,
1414  * and will cause make to do a new chdir to that path.
1415  */
1416 static void
1417 ParseDoDependencySourceSpecial(ParseSpecial specType, char *word,
1418 			       SearchPathList *paths)
1419 {
1420 	switch (specType) {
1421 	case SP_SUFFIXES:
1422 		Suff_AddSuffix(word, &mainNode);
1423 		break;
1424 	case SP_PATH:
1425 		AddToPaths(word, paths);
1426 		break;
1427 	case SP_INCLUDES:
1428 		Suff_AddInclude(word);
1429 		break;
1430 	case SP_LIBS:
1431 		Suff_AddLib(word);
1432 		break;
1433 	case SP_NULL:
1434 		Suff_SetNull(word);
1435 		break;
1436 	case SP_OBJDIR:
1437 		Main_SetObjdir(FALSE, "%s", word);
1438 		break;
1439 	default:
1440 		break;
1441 	}
1442 }
1443 
1444 static Boolean
1445 ParseDoDependencyTargets(char **inout_cp,
1446 			 char **inout_line,
1447 			 const char *lstart,
1448 			 ParseSpecial *inout_specType,
1449 			 GNodeType *inout_tOp,
1450 			 SearchPathList **inout_paths,
1451 			 StringList *curTargs)
1452 {
1453 	char *cp;
1454 	char *tgt = *inout_line;
1455 	char savec;
1456 	const char *p;
1457 
1458 	for (;;) {
1459 		/*
1460 		 * Here LINE points to the beginning of the next word, and
1461 		 * LSTART points to the actual beginning of the line.
1462 		 */
1463 
1464 		/* Find the end of the next word. */
1465 		cp = tgt;
1466 		p = cp;
1467 		ParseDependencyTargetWord(&p, lstart);
1468 		cp += p - cp;
1469 
1470 		/*
1471 		 * If the word is followed by a left parenthesis, it's the
1472 		 * name of an object file inside an archive (ar file).
1473 		 */
1474 		if (!ParseIsEscaped(lstart, cp) && *cp == '(') {
1475 			/*
1476 			 * Archives must be handled specially to make sure the
1477 			 * OP_ARCHV flag is set in their 'type' field, for one
1478 			 * thing, and because things like "archive(file1.o
1479 			 * file2.o file3.o)" are permissible.
1480 			 *
1481 			 * Arch_ParseArchive will set 'line' to be the first
1482 			 * non-blank after the archive-spec. It creates/finds
1483 			 * nodes for the members and places them on the given
1484 			 * list, returning TRUE if all went well and FALSE if
1485 			 * there was an error in the specification. On error,
1486 			 * line should remain untouched.
1487 			 */
1488 			if (!Arch_ParseArchive(&tgt, targets, VAR_CMDLINE)) {
1489 				Parse_Error(PARSE_FATAL,
1490 				    "Error in archive specification: \"%s\"",
1491 				    tgt);
1492 				return FALSE;
1493 			}
1494 
1495 			cp = tgt;
1496 			continue;
1497 		}
1498 
1499 		if (*cp == '\0') {
1500 			ParseErrorNoDependency(lstart);
1501 			return FALSE;
1502 		}
1503 
1504 		/* Insert a null terminator. */
1505 		savec = *cp;
1506 		*cp = '\0';
1507 
1508 		if (!ParseDoDependencyTarget(tgt, inout_specType, inout_tOp,
1509 		    inout_paths))
1510 			return FALSE;
1511 
1512 		/*
1513 		 * Have word in line. Get or create its node and stick it at
1514 		 * the end of the targets list
1515 		 */
1516 		if (*inout_specType == SP_NOT && *tgt != '\0')
1517 			ParseDoDependencyTargetMundane(tgt, curTargs);
1518 		else if (*inout_specType == SP_PATH && *tgt != '.' &&
1519 			 *tgt != '\0')
1520 			Parse_Error(PARSE_WARNING, "Extra target (%s) ignored",
1521 			    tgt);
1522 
1523 		/* Don't need the inserted null terminator any more. */
1524 		*cp = savec;
1525 
1526 		/*
1527 		 * If it is a special type and not .PATH, it's the only target
1528 		 * we allow on this line.
1529 		 */
1530 		if (*inout_specType != SP_NOT && *inout_specType != SP_PATH)
1531 			ParseDoDependencyTargetExtraWarn(&cp, lstart);
1532 		else
1533 			pp_skip_whitespace(&cp);
1534 
1535 		tgt = cp;
1536 		if (*tgt == '\0')
1537 			break;
1538 		if ((*tgt == '!' || *tgt == ':') &&
1539 		    !ParseIsEscaped(lstart, tgt))
1540 			break;
1541 	}
1542 
1543 	*inout_cp = cp;
1544 	*inout_line = tgt;
1545 	return TRUE;
1546 }
1547 
1548 static void
1549 ParseDoDependencySourcesSpecial(char *start, char *end,
1550 				ParseSpecial specType, SearchPathList *paths)
1551 {
1552 	char savec;
1553 
1554 	while (*start != '\0') {
1555 		while (*end != '\0' && !ch_isspace(*end))
1556 			end++;
1557 		savec = *end;
1558 		*end = '\0';
1559 		ParseDoDependencySourceSpecial(specType, start, paths);
1560 		*end = savec;
1561 		if (savec != '\0')
1562 			end++;
1563 		pp_skip_whitespace(&end);
1564 		start = end;
1565 	}
1566 }
1567 
1568 static Boolean
1569 ParseDoDependencySourcesMundane(char *start, char *end,
1570 				ParseSpecial specType, GNodeType tOp)
1571 {
1572 	while (*start != '\0') {
1573 		/*
1574 		 * The targets take real sources, so we must beware of archive
1575 		 * specifications (i.e. things with left parentheses in them)
1576 		 * and handle them accordingly.
1577 		 */
1578 		for (; *end != '\0' && !ch_isspace(*end); end++) {
1579 			if (*end == '(' && end > start && end[-1] != '$') {
1580 				/*
1581 				 * Only stop for a left parenthesis if it
1582 				 * isn't at the start of a word (that'll be
1583 				 * for variable changes later) and isn't
1584 				 * preceded by a dollar sign (a dynamic
1585 				 * source).
1586 				 */
1587 				break;
1588 			}
1589 		}
1590 
1591 		if (*end == '(') {
1592 			GNodeList sources = LST_INIT;
1593 			if (!Arch_ParseArchive(&start, &sources, VAR_CMDLINE)) {
1594 				Parse_Error(PARSE_FATAL,
1595 				    "Error in source archive spec \"%s\"",
1596 				    start);
1597 				return FALSE;
1598 			}
1599 
1600 			while (!Lst_IsEmpty(&sources)) {
1601 				GNode *gn = Lst_Dequeue(&sources);
1602 				ParseDependencySource(tOp, gn->name, specType);
1603 			}
1604 			Lst_Done(&sources);
1605 			end = start;
1606 		} else {
1607 			if (*end != '\0') {
1608 				*end = '\0';
1609 				end++;
1610 			}
1611 
1612 			ParseDependencySource(tOp, start, specType);
1613 		}
1614 		pp_skip_whitespace(&end);
1615 		start = end;
1616 	}
1617 	return TRUE;
1618 }
1619 
1620 /* Parse a dependency line consisting of targets, followed by a dependency
1621  * operator, optionally followed by sources.
1622  *
1623  * The nodes of the sources are linked as children to the nodes of the
1624  * targets. Nodes are created as necessary.
1625  *
1626  * The operator is applied to each node in the global 'targets' list,
1627  * which is where the nodes found for the targets are kept, by means of
1628  * the ParseDoOp function.
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. These are the ones detailed by the specType variable.
1637  *
1638  * The storing of transformation rules such as '.c.o' is also taken care of
1639  * here. A target is recognized as a transformation rule by calling
1640  * Suff_IsTransform. If it is a transformation rule, its node is gotten
1641  * from the suffix module via Suff_AddTransform rather than the standard
1642  * Targ_FindNode in the target module.
1643  *
1644  * Upon return, the value of the line is unspecified.
1645  */
1646 static void
1647 ParseDoDependency(char *line)
1648 {
1649 	char *cp;		/* our current position */
1650 	GNodeType op;		/* the operator on the line */
1651 	SearchPathList *paths;	/* search paths to alter when parsing
1652 				 * a list of .PATH targets */
1653 	GNodeType tOp;		/* operator from special target */
1654 	/* target names to be found and added to the targets list */
1655 	StringList curTargs = LST_INIT;
1656 	char *lstart = line;
1657 
1658 	/*
1659 	 * specType contains the SPECial TYPE of the current target. It is
1660 	 * SP_NOT if the target is unspecial. If it *is* special, however, the
1661 	 * children are linked as children of the parent but not vice versa.
1662 	 */
1663 	ParseSpecial specType = SP_NOT;
1664 
1665 	DEBUG1(PARSE, "ParseDoDependency(%s)\n", line);
1666 	tOp = OP_NONE;
1667 
1668 	paths = NULL;
1669 
1670 	/*
1671 	 * First, grind through the targets.
1672 	 */
1673 	if (!ParseDoDependencyTargets(&cp, &line, lstart, &specType, &tOp,
1674 	    &paths, &curTargs))
1675 		goto out;
1676 
1677 	/*
1678 	 * Don't need the list of target names anymore.
1679 	 * The targets themselves are now in the global variable 'targets'.
1680 	 */
1681 	Lst_Done(&curTargs);
1682 	Lst_Init(&curTargs);
1683 
1684 	if (!Lst_IsEmpty(targets))
1685 		ParseDoDependencyCheckSpec(specType);
1686 
1687 	/*
1688 	 * Have now parsed all the target names. Must parse the operator next.
1689 	 */
1690 	if (!ParseDoDependencyParseOp(&cp, lstart, &op))
1691 		goto out;
1692 
1693 	/*
1694 	 * Apply the operator to the target. This is how we remember which
1695 	 * operator a target was defined with. It fails if the operator
1696 	 * used isn't consistent across all references.
1697 	 */
1698 	ApplyDependencyOperator(op);
1699 
1700 	/*
1701 	 * Onward to the sources.
1702 	 *
1703 	 * LINE will now point to the first source word, if any, or the
1704 	 * end of the string if not.
1705 	 */
1706 	pp_skip_whitespace(&cp);
1707 	line = cp;		/* XXX: 'line' is an inappropriate name */
1708 
1709 	/*
1710 	 * Several special targets take different actions if present with no
1711 	 * sources:
1712 	 *	a .SUFFIXES line with no sources clears out all old suffixes
1713 	 *	a .PRECIOUS line makes all targets precious
1714 	 *	a .IGNORE line ignores errors for all targets
1715 	 *	a .SILENT line creates silence when making all targets
1716 	 *	a .PATH removes all directories from the search path(s).
1717 	 */
1718 	if (line[0] == '\0') {
1719 		ParseDoDependencySourcesEmpty(specType, paths);
1720 	} else if (specType == SP_MFLAGS) {
1721 		/*
1722 		 * Call on functions in main.c to deal with these arguments and
1723 		 * set the initial character to a null-character so the loop to
1724 		 * get sources won't get anything
1725 		 */
1726 		Main_ParseArgLine(line);
1727 		*line = '\0';
1728 	} else if (specType == SP_SHELL) {
1729 		if (!Job_ParseShell(line)) {
1730 			Parse_Error(PARSE_FATAL,
1731 			    "improper shell specification");
1732 			goto out;
1733 		}
1734 		*line = '\0';
1735 	} else if (specType == SP_NOTPARALLEL || specType == SP_SINGLESHELL ||
1736 		   specType == SP_DELETE_ON_ERROR) {
1737 		*line = '\0';
1738 	}
1739 
1740 	/* Now go for the sources. */
1741 	if (specType == SP_SUFFIXES || specType == SP_PATH ||
1742 	    specType == SP_INCLUDES || specType == SP_LIBS ||
1743 	    specType == SP_NULL || specType == SP_OBJDIR) {
1744 		ParseDoDependencySourcesSpecial(line, cp, specType, paths);
1745 		if (paths != NULL) {
1746 			Lst_Free(paths);
1747 			paths = NULL;
1748 		}
1749 		if (specType == SP_PATH)
1750 			Dir_SetPATH();
1751 	} else {
1752 		assert(paths == NULL);
1753 		if (!ParseDoDependencySourcesMundane(line, cp, specType, tOp))
1754 			goto out;
1755 	}
1756 
1757 	FindMainTarget();
1758 
1759 out:
1760 	if (paths != NULL)
1761 		Lst_Free(paths);
1762 	Lst_Done(&curTargs);
1763 }
1764 
1765 typedef struct VarAssignParsed {
1766 	const char *nameStart;	/* unexpanded */
1767 	const char *nameEnd;	/* before operator adjustment */
1768 	const char *eq;		/* the '=' of the assignment operator */
1769 } VarAssignParsed;
1770 
1771 /*
1772  * Determine the assignment operator and adjust the end of the variable
1773  * name accordingly.
1774  */
1775 static void
1776 AdjustVarassignOp(const VarAssignParsed *pvar, const char *value,
1777 		  VarAssign *out_var)
1778 {
1779 	const char *op = pvar->eq;
1780 	const char *const name = pvar->nameStart;
1781 	VarAssignOp type;
1782 
1783 	if (op > name && op[-1] == '+') {
1784 		type = VAR_APPEND;
1785 		op--;
1786 
1787 	} else if (op > name && op[-1] == '?') {
1788 		op--;
1789 		type = VAR_DEFAULT;
1790 
1791 	} else if (op > name && op[-1] == ':') {
1792 		op--;
1793 		type = VAR_SUBST;
1794 
1795 	} else if (op > name && op[-1] == '!') {
1796 		op--;
1797 		type = VAR_SHELL;
1798 
1799 	} else {
1800 		type = VAR_NORMAL;
1801 #ifdef SUNSHCMD
1802 		while (op > name && ch_isspace(op[-1]))
1803 			op--;
1804 
1805 		if (op >= name + 3 && op[-3] == ':' && op[-2] == 's' &&
1806 		    op[-1] == 'h') {
1807 			type = VAR_SHELL;
1808 			op -= 3;
1809 		}
1810 #endif
1811 	}
1812 
1813 	{
1814 		const char *nameEnd = pvar->nameEnd < op ? pvar->nameEnd : op;
1815 		out_var->varname = bmake_strsedup(pvar->nameStart, nameEnd);
1816 		out_var->op = type;
1817 		out_var->value = value;
1818 	}
1819 }
1820 
1821 /*
1822  * Parse a variable assignment, consisting of a single-word variable name,
1823  * optional whitespace, an assignment operator, optional whitespace and the
1824  * variable value.
1825  *
1826  * Note: There is a lexical ambiguity with assignment modifier characters
1827  * in variable names. This routine interprets the character before the =
1828  * as a modifier. Therefore, an assignment like
1829  *	C++=/usr/bin/CC
1830  * is interpreted as "C+ +=" instead of "C++ =".
1831  *
1832  * Used for both lines in a file and command line arguments.
1833  */
1834 Boolean
1835 Parse_IsVar(const char *p, VarAssign *out_var)
1836 {
1837 	VarAssignParsed pvar;
1838 	const char *firstSpace = NULL;
1839 	int level = 0;
1840 
1841 	cpp_skip_hspace(&p);	/* Skip to variable name */
1842 
1843 	/*
1844 	 * During parsing, the '+' of the '+=' operator is initially parsed
1845 	 * as part of the variable name.  It is later corrected, as is the
1846 	 * ':sh' modifier. Of these two (nameEnd and op), the earlier one
1847 	 * determines the actual end of the variable name.
1848 	 */
1849 	pvar.nameStart = p;
1850 #ifdef CLEANUP
1851 	pvar.nameEnd = NULL;
1852 	pvar.eq = NULL;
1853 #endif
1854 
1855 	/*
1856 	 * Scan for one of the assignment operators outside a variable
1857 	 * expansion.
1858 	 */
1859 	while (*p != '\0') {
1860 		char ch = *p++;
1861 		if (ch == '(' || ch == '{') {
1862 			level++;
1863 			continue;
1864 		}
1865 		if (ch == ')' || ch == '}') {
1866 			level--;
1867 			continue;
1868 		}
1869 
1870 		if (level != 0)
1871 			continue;
1872 
1873 		if (ch == ' ' || ch == '\t')
1874 			if (firstSpace == NULL)
1875 				firstSpace = p - 1;
1876 		while (ch == ' ' || ch == '\t')
1877 			ch = *p++;
1878 
1879 #ifdef SUNSHCMD
1880 		if (ch == ':' && p[0] == 's' && p[1] == 'h') {
1881 			p += 2;
1882 			continue;
1883 		}
1884 #endif
1885 		if (ch == '=') {
1886 			pvar.eq = p - 1;
1887 			pvar.nameEnd = firstSpace != NULL ? firstSpace : p - 1;
1888 			cpp_skip_whitespace(&p);
1889 			AdjustVarassignOp(&pvar, p, out_var);
1890 			return TRUE;
1891 		}
1892 		if (*p == '=' &&
1893 		    (ch == '+' || ch == ':' || ch == '?' || ch == '!')) {
1894 			pvar.eq = p;
1895 			pvar.nameEnd = firstSpace != NULL ? firstSpace : p;
1896 			p++;
1897 			cpp_skip_whitespace(&p);
1898 			AdjustVarassignOp(&pvar, p, out_var);
1899 			return TRUE;
1900 		}
1901 		if (firstSpace != NULL)
1902 			return FALSE;
1903 	}
1904 
1905 	return FALSE;
1906 }
1907 
1908 /*
1909  * Check for syntax errors such as unclosed expressions or unknown modifiers.
1910  */
1911 static void
1912 VarCheckSyntax(VarAssignOp type, const char *uvalue, GNode *ctxt)
1913 {
1914 	if (opts.lint) {
1915 		if (type != VAR_SUBST && strchr(uvalue, '$') != NULL) {
1916 			char *expandedValue;
1917 
1918 			(void)Var_Subst(uvalue, ctxt, VARE_NONE,
1919 			    &expandedValue);
1920 			/* TODO: handle errors */
1921 			free(expandedValue);
1922 		}
1923 	}
1924 }
1925 
1926 static void
1927 VarAssign_EvalSubst(const char *name, const char *uvalue, GNode *ctxt,
1928 		    const char **out_avalue, void **out_avalue_freeIt)
1929 {
1930 	const char *avalue;
1931 	char *evalue;
1932 	Boolean savedPreserveUndefined = preserveUndefined;
1933 
1934 	/* TODO: Can this assignment to preserveUndefined be moved further down
1935 	 * to the actually interesting Var_Subst call, without affecting any
1936 	 * edge cases?
1937 	 *
1938 	 * It might affect the implicit expansion of the variable name in the
1939 	 * Var_Exists and Var_Set calls, even though it's unlikely that anyone
1940 	 * cared about this edge case when adding this code.  In addition,
1941 	 * variable assignments should not refer to any undefined variables in
1942 	 * the variable name. */
1943 	preserveUndefined = TRUE;
1944 
1945 	/*
1946 	 * make sure that we set the variable the first time to nothing
1947 	 * so that it gets substituted!
1948 	 */
1949 	if (!Var_Exists(name, ctxt))
1950 		Var_Set(name, "", ctxt);
1951 
1952 	(void)Var_Subst(uvalue, ctxt, VARE_WANTRES | VARE_KEEP_DOLLAR, &evalue);
1953 	/* TODO: handle errors */
1954 	preserveUndefined = savedPreserveUndefined;
1955 	avalue = evalue;
1956 	Var_Set(name, avalue, ctxt);
1957 
1958 	*out_avalue = avalue;
1959 	*out_avalue_freeIt = evalue;
1960 }
1961 
1962 static void
1963 VarAssign_EvalShell(const char *name, const char *uvalue, GNode *ctxt,
1964 		    const char **out_avalue, void **out_avalue_freeIt)
1965 {
1966 	const char *cmd, *errfmt;
1967 	char *cmdOut;
1968 	void *cmd_freeIt = NULL;
1969 
1970 	cmd = uvalue;
1971 	if (strchr(cmd, '$') != NULL) {
1972 		char *ecmd;
1973 		(void)Var_Subst(cmd, VAR_CMDLINE, VARE_WANTRES | VARE_UNDEFERR,
1974 		    &ecmd);
1975 		/* TODO: handle errors */
1976 		cmd = cmd_freeIt = ecmd;
1977 	}
1978 
1979 	cmdOut = Cmd_Exec(cmd, &errfmt);
1980 	Var_Set(name, cmdOut, ctxt);
1981 	*out_avalue = *out_avalue_freeIt = cmdOut;
1982 
1983 	if (errfmt != NULL)
1984 		Parse_Error(PARSE_WARNING, errfmt, cmd);
1985 
1986 	free(cmd_freeIt);
1987 }
1988 
1989 /* Perform a variable assignment.
1990  *
1991  * The actual value of the variable is returned in *out_avalue and
1992  * *out_avalue_freeIt.  Especially for VAR_SUBST and VAR_SHELL this can differ
1993  * from the literal value.
1994  *
1995  * Return whether the assignment was actually done.  The assignment is only
1996  * skipped if the operator is '?=' and the variable already exists. */
1997 static Boolean
1998 VarAssign_Eval(const char *name, VarAssignOp op, const char *uvalue,
1999 	       GNode *ctxt, const char **out_avalue, void **out_avalue_freeIt)
2000 {
2001 	const char *avalue = uvalue;
2002 	void *avalue_freeIt = NULL;
2003 
2004 	if (op == VAR_APPEND)
2005 		Var_Append(name, uvalue, ctxt);
2006 	else if (op == VAR_SUBST)
2007 		VarAssign_EvalSubst(name, uvalue, ctxt, &avalue,
2008 		    &avalue_freeIt);
2009 	else if (op == VAR_SHELL)
2010 		VarAssign_EvalShell(name, uvalue, ctxt, &avalue,
2011 		    &avalue_freeIt);
2012 	else {
2013 		if (op == VAR_DEFAULT && Var_Exists(name, ctxt)) {
2014 			*out_avalue_freeIt = NULL;
2015 			return FALSE;
2016 		}
2017 
2018 		/* Normal assignment -- just do it. */
2019 		Var_Set(name, uvalue, ctxt);
2020 	}
2021 
2022 	*out_avalue = avalue;
2023 	*out_avalue_freeIt = avalue_freeIt;
2024 	return TRUE;
2025 }
2026 
2027 static void
2028 VarAssignSpecial(const char *name, const char *avalue)
2029 {
2030 	if (strcmp(name, MAKEOVERRIDES) == 0)
2031 		Main_ExportMAKEFLAGS(FALSE); /* re-export MAKEFLAGS */
2032 	else if (strcmp(name, ".CURDIR") == 0) {
2033 		/*
2034 		 * Someone is being (too?) clever...
2035 		 * Let's pretend they know what they are doing and
2036 		 * re-initialize the 'cur' CachedDir.
2037 		 */
2038 		Dir_InitCur(avalue);
2039 		Dir_SetPATH();
2040 	} else if (strcmp(name, MAKE_JOB_PREFIX) == 0)
2041 		Job_SetPrefix();
2042 	else if (strcmp(name, MAKE_EXPORTED) == 0)
2043 		Var_Export(avalue, FALSE);
2044 }
2045 
2046 /* Perform the variable variable assignment in the given context. */
2047 void
2048 Parse_DoVar(VarAssign *var, GNode *ctxt)
2049 {
2050 	const char *avalue;	/* actual value (maybe expanded) */
2051 	void *avalue_freeIt;
2052 
2053 	VarCheckSyntax(var->op, var->value, ctxt);
2054 	if (VarAssign_Eval(var->varname, var->op, var->value, ctxt,
2055 	    &avalue, &avalue_freeIt))
2056 		VarAssignSpecial(var->varname, avalue);
2057 
2058 	free(avalue_freeIt);
2059 	free(var->varname);
2060 }
2061 
2062 
2063 /* See if the command possibly calls a sub-make by using the variable
2064  * expressions ${.MAKE}, ${MAKE} or the plain word "make". */
2065 static Boolean
2066 MaybeSubMake(const char *cmd)
2067 {
2068 	const char *start;
2069 
2070 	for (start = cmd; *start != '\0'; start++) {
2071 		const char *p = start;
2072 		char endc;
2073 
2074 		/* XXX: What if progname != "make"? */
2075 		if (p[0] == 'm' && p[1] == 'a' && p[2] == 'k' && p[3] == 'e')
2076 			if (start == cmd || !ch_isalnum(p[-1]))
2077 				if (!ch_isalnum(p[4]))
2078 					return TRUE;
2079 
2080 		if (*p != '$')
2081 			continue;
2082 		p++;
2083 
2084 		if (*p == '{')
2085 			endc = '}';
2086 		else if (*p == '(')
2087 			endc = ')';
2088 		else
2089 			continue;
2090 		p++;
2091 
2092 		if (*p == '.')	/* Accept either ${.MAKE} or ${MAKE}. */
2093 			p++;
2094 
2095 		if (p[0] == 'M' && p[1] == 'A' && p[2] == 'K' && p[3] == 'E')
2096 			if (p[4] == endc)
2097 				return TRUE;
2098 	}
2099 	return FALSE;
2100 }
2101 
2102 /* Append the command to the target node.
2103  *
2104  * The node may be marked as a submake node if the command is determined to
2105  * be that. */
2106 static void
2107 ParseAddCmd(GNode *gn, char *cmd)
2108 {
2109 	/* Add to last (ie current) cohort for :: targets */
2110 	if ((gn->type & OP_DOUBLEDEP) && gn->cohorts.last != NULL)
2111 		gn = gn->cohorts.last->datum;
2112 
2113 	/* if target already supplied, ignore commands */
2114 	if (!(gn->type & OP_HAS_COMMANDS)) {
2115 		Lst_Append(&gn->commands, cmd);
2116 		if (MaybeSubMake(cmd))
2117 			gn->type |= OP_SUBMAKE;
2118 		ParseMark(gn);
2119 	} else {
2120 #if 0
2121 		/* XXX: We cannot do this until we fix the tree */
2122 		Lst_Append(&gn->commands, cmd);
2123 		Parse_Error(PARSE_WARNING,
2124 		    "overriding commands for target \"%s\"; "
2125 		    "previous commands defined at %s: %d ignored",
2126 		    gn->name, gn->fname, gn->lineno);
2127 #else
2128 		Parse_Error(PARSE_WARNING,
2129 		    "duplicate script for target \"%s\" ignored",
2130 		    gn->name);
2131 		ParseErrorInternal(gn->fname, (size_t)gn->lineno, PARSE_WARNING,
2132 		    "using previous script for \"%s\" defined here",
2133 		    gn->name);
2134 #endif
2135 	}
2136 }
2137 
2138 /*
2139  * Add a directory to the path searched for included makefiles bracketed
2140  * by double-quotes.
2141  */
2142 void
2143 Parse_AddIncludeDir(const char *dir)
2144 {
2145 	(void)Dir_AddDir(parseIncPath, dir);
2146 }
2147 
2148 /* Handle one of the .[-ds]include directives by remembering the current file
2149  * and pushing the included file on the stack.  After the included file has
2150  * finished, parsing continues with the including file; see Parse_SetInput
2151  * and ParseEOF.
2152  *
2153  * System includes are looked up in sysIncPath, any other includes are looked
2154  * up in the parsedir and then in the directories specified by the -I command
2155  * line options.
2156  */
2157 static void
2158 Parse_include_file(char *file, Boolean isSystem, Boolean depinc, Boolean silent)
2159 {
2160 	struct loadedfile *lf;
2161 	char *fullname;		/* full pathname of file */
2162 	char *newName;
2163 	char *slash, *incdir;
2164 	int fd;
2165 	int i;
2166 
2167 	fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
2168 
2169 	if (fullname == NULL && !isSystem) {
2170 		/*
2171 		 * Include files contained in double-quotes are first searched
2172 		 * relative to the including file's location. We don't want to
2173 		 * cd there, of course, so we just tack on the old file's
2174 		 * leading path components and call Dir_FindFile to see if
2175 		 * we can locate the file.
2176 		 */
2177 
2178 		incdir = bmake_strdup(CurFile()->fname);
2179 		slash = strrchr(incdir, '/');
2180 		if (slash != NULL) {
2181 			*slash = '\0';
2182 			/*
2183 			 * Now do lexical processing of leading "../" on the
2184 			 * filename.
2185 			 */
2186 			for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
2187 				slash = strrchr(incdir + 1, '/');
2188 				if (slash == NULL || strcmp(slash, "/..") == 0)
2189 					break;
2190 				*slash = '\0';
2191 			}
2192 			newName = str_concat3(incdir, "/", file + i);
2193 			fullname = Dir_FindFile(newName, parseIncPath);
2194 			if (fullname == NULL)
2195 				fullname = Dir_FindFile(newName,
2196 				    &dirSearchPath);
2197 			free(newName);
2198 		}
2199 		free(incdir);
2200 
2201 		if (fullname == NULL) {
2202 			/*
2203 			 * Makefile wasn't found in same directory as included
2204 			 * makefile.
2205 			 *
2206 			 * Search for it first on the -I search path, then on
2207 			 * the .PATH search path, if not found in a -I
2208 			 * directory. If we have a suffix-specific path, we
2209 			 * should use that.
2210 			 */
2211 			const char *suff;
2212 			SearchPath *suffPath = NULL;
2213 
2214 			if ((suff = strrchr(file, '.'))) {
2215 				suffPath = Suff_GetPath(suff);
2216 				if (suffPath != NULL)
2217 					fullname = Dir_FindFile(file, suffPath);
2218 			}
2219 			if (fullname == NULL) {
2220 				fullname = Dir_FindFile(file, parseIncPath);
2221 				if (fullname == NULL)
2222 					fullname = Dir_FindFile(file,
2223 					    &dirSearchPath);
2224 			}
2225 		}
2226 	}
2227 
2228 	/* Looking for a system file or file still not found */
2229 	if (fullname == NULL) {
2230 		/*
2231 		 * Look for it on the system path
2232 		 */
2233 		SearchPath *path = Lst_IsEmpty(sysIncPath) ? defSysIncPath
2234 		    : sysIncPath;
2235 		fullname = Dir_FindFile(file, path);
2236 	}
2237 
2238 	if (fullname == NULL) {
2239 		if (!silent)
2240 			Parse_Error(PARSE_FATAL, "Could not find %s", file);
2241 		return;
2242 	}
2243 
2244 	/* Actually open the file... */
2245 	fd = open(fullname, O_RDONLY);
2246 	if (fd == -1) {
2247 		if (!silent)
2248 			Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
2249 		free(fullname);
2250 		return;
2251 	}
2252 
2253 	/* load it */
2254 	lf = loadfile(fullname, fd);
2255 
2256 	/* Start reading from this file next */
2257 	Parse_SetInput(fullname, 0, -1, loadedfile_nextbuf, lf);
2258 	CurFile()->lf = lf;
2259 	if (depinc)
2260 		doing_depend = depinc;	/* only turn it on */
2261 }
2262 
2263 static void
2264 ParseDoInclude(char *line)
2265 {
2266 	char endc;		/* the character which ends the file spec */
2267 	char *cp;		/* current position in file spec */
2268 	Boolean silent = *line != 'i';
2269 	char *file = line + (silent ? 8 : 7);
2270 
2271 	/* Skip to delimiter character so we know where to look */
2272 	pp_skip_hspace(&file);
2273 
2274 	if (*file != '"' && *file != '<') {
2275 		Parse_Error(PARSE_FATAL,
2276 		    ".include filename must be delimited by '\"' or '<'");
2277 		return;
2278 	}
2279 
2280 	/*
2281 	 * Set the search path on which to find the include file based on the
2282 	 * characters which bracket its name. Angle-brackets imply it's
2283 	 * a system Makefile while double-quotes imply it's a user makefile
2284 	 */
2285 	if (*file == '<')
2286 		endc = '>';
2287 	else
2288 		endc = '"';
2289 
2290 	/* Skip to matching delimiter */
2291 	for (cp = ++file; *cp && *cp != endc; cp++)
2292 		continue;
2293 
2294 	if (*cp != endc) {
2295 		Parse_Error(PARSE_FATAL,
2296 		    "Unclosed .include filename. '%c' expected", endc);
2297 		return;
2298 	}
2299 
2300 	*cp = '\0';
2301 
2302 	/*
2303 	 * Substitute for any variables in the filename before trying to
2304 	 * find the file.
2305 	 */
2306 	(void)Var_Subst(file, VAR_CMDLINE, VARE_WANTRES, &file);
2307 	/* TODO: handle errors */
2308 
2309 	Parse_include_file(file, endc == '>', *line == 'd', silent);
2310 	free(file);
2311 }
2312 
2313 /* Split filename into dirname + basename, then assign these to the
2314  * given variables. */
2315 static void
2316 SetFilenameVars(const char *filename, const char *dirvar, const char *filevar)
2317 {
2318 	const char *slash, *dirname, *basename;
2319 	void *freeIt;
2320 
2321 	slash = strrchr(filename, '/');
2322 	if (slash == NULL) {
2323 		dirname = curdir;
2324 		basename = filename;
2325 		freeIt = NULL;
2326 	} else {
2327 		dirname = freeIt = bmake_strsedup(filename, slash);
2328 		basename = slash + 1;
2329 	}
2330 
2331 	Var_Set(dirvar, dirname, VAR_GLOBAL);
2332 	Var_Set(filevar, basename, VAR_GLOBAL);
2333 
2334 	DEBUG5(PARSE, "%s: ${%s} = `%s' ${%s} = `%s'\n",
2335 	    __func__, dirvar, dirname, filevar, basename);
2336 	free(freeIt);
2337 }
2338 
2339 /*
2340  * Return the immediately including file.
2341  *
2342  * This is made complicated since the .for loop is implemented as a special
2343  * kind of .include; see For_Run.
2344  */
2345 static const char *
2346 GetActuallyIncludingFile(void)
2347 {
2348 	size_t i;
2349 	const IFile *incs = GetInclude(0);
2350 
2351 	for (i = includes.len; i >= 2; i--)
2352 		if (!incs[i - 1].fromForLoop)
2353 			return incs[i - 2].fname;
2354 	return NULL;
2355 }
2356 
2357 /* Set .PARSEDIR, .PARSEFILE, .INCLUDEDFROMDIR and .INCLUDEDFROMFILE. */
2358 static void
2359 ParseSetParseFile(const char *filename)
2360 {
2361 	const char *including;
2362 
2363 	SetFilenameVars(filename, ".PARSEDIR", ".PARSEFILE");
2364 
2365 	including = GetActuallyIncludingFile();
2366 	if (including != NULL) {
2367 		SetFilenameVars(including,
2368 		    ".INCLUDEDFROMDIR", ".INCLUDEDFROMFILE");
2369 	} else {
2370 		Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL);
2371 		Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL);
2372 	}
2373 }
2374 
2375 static Boolean
2376 StrContainsWord(const char *str, const char *word)
2377 {
2378 	size_t strLen = strlen(str);
2379 	size_t wordLen = strlen(word);
2380 	const char *p, *end;
2381 
2382 	if (strLen < wordLen)
2383 		return FALSE;	/* str is too short to contain word */
2384 
2385 	end = str + strLen - wordLen;
2386 	for (p = str; p != NULL; p = strchr(p, ' ')) {
2387 		if (*p == ' ')
2388 			p++;
2389 		if (p > end)
2390 			return FALSE;	/* cannot contain word */
2391 
2392 		if (memcmp(p, word, wordLen) == 0 &&
2393 		    (p[wordLen] == '\0' || p[wordLen] == ' '))
2394 			return TRUE;
2395 	}
2396 	return FALSE;
2397 }
2398 
2399 /*
2400  * XXX: Searching through a set of words with this linear search is
2401  * inefficient for variables that contain thousands of words.
2402  *
2403  * XXX: The paths in this list don't seem to be normalized in any way.
2404  */
2405 static Boolean
2406 VarContainsWord(const char *varname, const char *word)
2407 {
2408 	void *val_freeIt;
2409 	const char *val = Var_Value(varname, VAR_GLOBAL, &val_freeIt);
2410 	Boolean found = val != NULL && StrContainsWord(val, word);
2411 	bmake_free(val_freeIt);
2412 	return found;
2413 }
2414 
2415 /* Track the makefiles we read - so makefiles can set dependencies on them.
2416  * Avoid adding anything more than once.
2417  *
2418  * Time complexity: O(n) per call, in total O(n^2), where n is the number
2419  * of makefiles that have been loaded. */
2420 static void
2421 ParseTrackInput(const char *name)
2422 {
2423 	if (!VarContainsWord(MAKE_MAKEFILES, name))
2424 		Var_Append(MAKE_MAKEFILES, name, VAR_GLOBAL);
2425 }
2426 
2427 
2428 /*
2429  * Start parsing from the given source.
2430  *
2431  * The given file is added to the includes stack.
2432  */
2433 void
2434 Parse_SetInput(const char *name, int line, int fd,
2435 	       char *(*nextbuf)(void *, size_t *), void *arg)
2436 {
2437 	IFile *curFile;
2438 	char *buf;
2439 	size_t len;
2440 	Boolean fromForLoop = name == NULL;
2441 
2442 	if (fromForLoop)
2443 		name = CurFile()->fname;
2444 	else
2445 		ParseTrackInput(name);
2446 
2447 	if (DEBUG(PARSE)) {
2448 		const char *caller = nextbuf == loadedfile_nextbuf
2449 		    ? "loadedfile" : "other";
2450 		debug_printf(
2451 		    "%s: file %s, line %d, fd %d, nextbuf %s, arg %p\n",
2452 		    __func__, name, line, fd, caller, arg);
2453 	}
2454 
2455 	if (fd == -1 && nextbuf == NULL)
2456 		/* sanity */
2457 		return;
2458 
2459 	curFile = Vector_Push(&includes);
2460 	curFile->fname = bmake_strdup(name);
2461 	curFile->fromForLoop = fromForLoop;
2462 	curFile->lineno = line;
2463 	curFile->first_lineno = line;
2464 	curFile->nextbuf = nextbuf;
2465 	curFile->nextbuf_arg = arg;
2466 	curFile->lf = NULL;
2467 	curFile->depending = doing_depend;	/* restore this on EOF */
2468 
2469 	assert(nextbuf != NULL);
2470 
2471 	/* Get first block of input data */
2472 	buf = curFile->nextbuf(curFile->nextbuf_arg, &len);
2473 	if (buf == NULL) {
2474 		/* Was all a waste of time ... */
2475 		if (curFile->fname)
2476 			free(curFile->fname);
2477 		free(curFile);
2478 		return;
2479 	}
2480 	curFile->buf_freeIt = buf;
2481 	curFile->buf_ptr = buf;
2482 	curFile->buf_end = buf + len;
2483 
2484 	curFile->cond_depth = Cond_save_depth();
2485 	ParseSetParseFile(name);
2486 }
2487 
2488 /* Check if the directive is an include directive. */
2489 static Boolean
2490 IsInclude(const char *dir, Boolean sysv)
2491 {
2492 	if (dir[0] == 's' || dir[0] == '-' || (dir[0] == 'd' && !sysv))
2493 		dir++;
2494 
2495 	if (strncmp(dir, "include", 7) != 0)
2496 		return FALSE;
2497 
2498 	/* Space is not mandatory for BSD .include */
2499 	return !sysv || ch_isspace(dir[7]);
2500 }
2501 
2502 
2503 #ifdef SYSVINCLUDE
2504 /* Check if the line is a SYSV include directive. */
2505 static Boolean
2506 IsSysVInclude(const char *line)
2507 {
2508 	const char *p;
2509 
2510 	if (!IsInclude(line, TRUE))
2511 		return FALSE;
2512 
2513 	/* Avoid interpreting a dependency line as an include */
2514 	for (p = line; (p = strchr(p, ':')) != NULL;) {
2515 
2516 		/* end of line -> it's a dependency */
2517 		if (*++p == '\0')
2518 			return FALSE;
2519 
2520 		/* '::' operator or ': ' -> it's a dependency */
2521 		if (*p == ':' || ch_isspace(*p))
2522 			return FALSE;
2523 	}
2524 	return TRUE;
2525 }
2526 
2527 /* Push to another file.  The line points to the word "include". */
2528 static void
2529 ParseTraditionalInclude(char *line)
2530 {
2531 	char *cp;		/* current position in file spec */
2532 	Boolean done = FALSE;
2533 	Boolean silent = line[0] != 'i';
2534 	char *file = line + (silent ? 8 : 7);
2535 	char *all_files;
2536 
2537 	DEBUG2(PARSE, "%s: %s\n", __func__, file);
2538 
2539 	pp_skip_whitespace(&file);
2540 
2541 	/*
2542 	 * Substitute for any variables in the file name before trying to
2543 	 * find the thing.
2544 	 */
2545 	(void)Var_Subst(file, VAR_CMDLINE, VARE_WANTRES, &all_files);
2546 	/* TODO: handle errors */
2547 
2548 	if (*file == '\0') {
2549 		Parse_Error(PARSE_FATAL, "Filename missing from \"include\"");
2550 		goto out;
2551 	}
2552 
2553 	for (file = all_files; !done; file = cp + 1) {
2554 		/* Skip to end of line or next whitespace */
2555 		for (cp = file; *cp != '\0' && !ch_isspace(*cp); cp++)
2556 			continue;
2557 
2558 		if (*cp != '\0')
2559 			*cp = '\0';
2560 		else
2561 			done = TRUE;
2562 
2563 		Parse_include_file(file, FALSE, FALSE, silent);
2564 	}
2565 out:
2566 	free(all_files);
2567 }
2568 #endif
2569 
2570 #ifdef GMAKEEXPORT
2571 /* Parse "export <variable>=<value>", and actually export it. */
2572 static void
2573 ParseGmakeExport(char *line)
2574 {
2575 	char *variable = line + 6;
2576 	char *value;
2577 
2578 	DEBUG2(PARSE, "%s: %s\n", __func__, variable);
2579 
2580 	pp_skip_whitespace(&variable);
2581 
2582 	for (value = variable; *value && *value != '='; value++)
2583 		continue;
2584 
2585 	if (*value != '=') {
2586 		Parse_Error(PARSE_FATAL,
2587 		    "Variable/Value missing from \"export\"");
2588 		return;
2589 	}
2590 	*value++ = '\0';	/* terminate variable */
2591 
2592 	/*
2593 	 * Expand the value before putting it in the environment.
2594 	 */
2595 	(void)Var_Subst(value, VAR_CMDLINE, VARE_WANTRES, &value);
2596 	/* TODO: handle errors */
2597 
2598 	setenv(variable, value, 1);
2599 	free(value);
2600 }
2601 #endif
2602 
2603 /*
2604  * Called when EOF is reached in the current file. If we were reading an
2605  * include file or a .for loop, the includes stack is popped and things set
2606  * up to go back to reading the previous file at the previous location.
2607  *
2608  * Results:
2609  *	TRUE to continue parsing, i.e. it had only reached the end of an
2610  *	included file, FALSE if the main file has been parsed completely.
2611  */
2612 static Boolean
2613 ParseEOF(void)
2614 {
2615 	char *ptr;
2616 	size_t len;
2617 	IFile *curFile = CurFile();
2618 
2619 	assert(curFile->nextbuf != NULL);
2620 
2621 	doing_depend = curFile->depending;	/* restore this */
2622 	/* get next input buffer, if any */
2623 	ptr = curFile->nextbuf(curFile->nextbuf_arg, &len);
2624 	curFile->buf_ptr = ptr;
2625 	curFile->buf_freeIt = ptr;
2626 	curFile->buf_end = ptr + len; /* XXX: undefined behavior if ptr == NULL */
2627 	curFile->lineno = curFile->first_lineno;
2628 	if (ptr != NULL)
2629 		return TRUE;	/* Iterate again */
2630 
2631 	/* Ensure the makefile (or loop) didn't have mismatched conditionals */
2632 	Cond_restore_depth(curFile->cond_depth);
2633 
2634 	if (curFile->lf != NULL) {
2635 		loadedfile_destroy(curFile->lf);
2636 		curFile->lf = NULL;
2637 	}
2638 
2639 	/* Dispose of curFile info */
2640 	/* Leak curFile->fname because all the gnodes have pointers to it. */
2641 	free(curFile->buf_freeIt);
2642 	Vector_Pop(&includes);
2643 
2644 	if (includes.len == 0) {
2645 		/* We've run out of input */
2646 		Var_Delete(".PARSEDIR", VAR_GLOBAL);
2647 		Var_Delete(".PARSEFILE", VAR_GLOBAL);
2648 		Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL);
2649 		Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL);
2650 		return FALSE;
2651 	}
2652 
2653 	curFile = CurFile();
2654 	DEBUG2(PARSE, "ParseEOF: returning to file %s, line %d\n",
2655 	    curFile->fname, curFile->lineno);
2656 
2657 	ParseSetParseFile(curFile->fname);
2658 	return TRUE;
2659 }
2660 
2661 typedef enum GetLineMode {
2662 	PARSE_NORMAL,
2663 	PARSE_RAW,
2664 	PARSE_SKIP
2665 } GetLineMode;
2666 
2667 static char *
2668 ParseGetLine(GetLineMode mode)
2669 {
2670 	IFile *cf = CurFile();
2671 	char *ptr;
2672 	char ch;
2673 	char *line;
2674 	char *line_end;
2675 	char *escaped;
2676 	char *comment;
2677 	char *tp;
2678 
2679 	/* Loop through blank lines and comment lines */
2680 	for (;;) {
2681 		cf->lineno++;
2682 		line = cf->buf_ptr;
2683 		ptr = line;
2684 		line_end = line;
2685 		escaped = NULL;
2686 		comment = NULL;
2687 		for (;;) {
2688 			/* XXX: can buf_end ever be null? */
2689 			if (cf->buf_end != NULL && ptr == cf->buf_end) {
2690 				/* end of buffer */
2691 				ch = '\0';
2692 				break;
2693 			}
2694 			ch = *ptr;
2695 			if (ch == '\0' || (ch == '\\' && ptr[1] == '\0')) {
2696 
2697 				/* XXX: can buf_end ever be null? */
2698 				if (cf->buf_end == NULL)
2699 					/* End of string (aka for loop) data */
2700 					break;
2701 
2702 				/* see if there is more we can parse */
2703 				while (ptr++ < cf->buf_end) {
2704 					if ((ch = *ptr) == '\n') {
2705 						if (ptr > line &&
2706 						    ptr[-1] == '\\')
2707 							continue;
2708 						Parse_Error(PARSE_WARNING,
2709 						    "Zero byte read from file, "
2710 						    "skipping rest of line.");
2711 						break;
2712 					}
2713 				}
2714 				/* XXX: Can cf->nextbuf ever be NULL? */
2715 				if (cf->nextbuf != NULL) {
2716 					/*
2717 					 * End of this buffer; return EOF and
2718 					 * outer logic will get the next one.
2719 					 * (eww)
2720 					 */
2721 					break;
2722 				}
2723 				Parse_Error(PARSE_FATAL,
2724 				    "Zero byte read from file");
2725 				return NULL;
2726 			}
2727 
2728 			if (ch == '\\') {
2729 				/*
2730 				 * Don't treat next character as special,
2731 				 * remember first one.
2732 				 */
2733 				if (escaped == NULL)
2734 					escaped = ptr;
2735 				if (ptr[1] == '\n')
2736 					cf->lineno++;
2737 				ptr += 2;
2738 				line_end = ptr;
2739 				continue;
2740 			}
2741 			if (ch == '#' && comment == NULL) {
2742 				/*
2743 				 * Remember the first '#' for comment
2744 				 * stripping, unless the previous char was
2745 				 * '[', as in the modifier ':[#]'.
2746 				 */
2747 				if (!(ptr > line && ptr[-1] == '['))
2748 					comment = line_end;
2749 			}
2750 			ptr++;
2751 			if (ch == '\n')
2752 				break;
2753 			if (!ch_isspace(ch)) {
2754 				/*
2755 				 * We are not interested in trailing
2756 				 * whitespace.
2757 				 */
2758 				line_end = ptr;
2759 			}
2760 		}
2761 
2762 		/* Save next 'to be processed' location */
2763 		cf->buf_ptr = ptr;
2764 
2765 		/* Check we have a non-comment, non-blank line */
2766 		if (line_end == line || comment == line) {
2767 			if (ch == '\0')
2768 				/* At end of file */
2769 				return NULL;
2770 			/* Parse another line */
2771 			continue;
2772 		}
2773 
2774 		/* We now have a line of data */
2775 		*line_end = '\0';
2776 
2777 		if (mode == PARSE_RAW) {
2778 			/* Leave '\' (etc) in line buffer (eg 'for' lines) */
2779 			return line;
2780 		}
2781 
2782 		if (mode == PARSE_SKIP) {
2783 			/* Completely ignore non-directives */
2784 			if (line[0] != '.')
2785 				continue;
2786 			/*
2787 			 * We could do more of the .else/.elif/.endif checks
2788 			 * here.
2789 			 */
2790 		}
2791 		break;
2792 	}
2793 
2794 	/* Brutally ignore anything after a non-escaped '#' in non-commands. */
2795 	if (comment != NULL && line[0] != '\t') {
2796 		line_end = comment;
2797 		*line_end = '\0';
2798 	}
2799 
2800 	/* If we didn't see a '\\' then the in-situ data is fine. */
2801 	if (escaped == NULL)
2802 		return line;
2803 
2804 	/* Remove escapes from '\n' and '#' */
2805 	tp = ptr = escaped;
2806 	escaped = line;
2807 	for (;; *tp++ = ch) {
2808 		ch = *ptr++;
2809 		if (ch != '\\') {
2810 			if (ch == '\0')
2811 				break;
2812 			continue;
2813 		}
2814 
2815 		ch = *ptr++;
2816 		if (ch == '\0') {
2817 			/* Delete '\\' at end of buffer */
2818 			tp--;
2819 			break;
2820 		}
2821 
2822 		if (ch == '#' && line[0] != '\t')
2823 			/* Delete '\\' from before '#' on non-command lines */
2824 			continue;
2825 
2826 		if (ch != '\n') {
2827 			/* Leave '\\' in buffer for later */
2828 			*tp++ = '\\';
2829 			/*
2830 			 * Make sure we don't delete an escaped ' ' from the
2831 			 * line end.
2832 			 */
2833 			escaped = tp + 1;
2834 			continue;
2835 		}
2836 
2837 		/*
2838 		 * Escaped '\n' -- replace following whitespace with a single
2839 		 * ' '.
2840 		 */
2841 		pp_skip_hspace(&ptr);
2842 		ch = ' ';
2843 	}
2844 
2845 	/* Delete any trailing spaces - eg from empty continuations */
2846 	while (tp > escaped && ch_isspace(tp[-1]))
2847 		tp--;
2848 
2849 	*tp = '\0';
2850 	return line;
2851 }
2852 
2853 /*
2854  * Read an entire line from the input file. Called only by Parse_File.
2855  *
2856  * Results:
2857  *	A line without its newline and without any trailing whitespace.
2858  */
2859 static char *
2860 ParseReadLine(void)
2861 {
2862 	char *line;		/* Result */
2863 	int lineno;		/* Saved line # */
2864 	int rval;
2865 
2866 	for (;;) {
2867 		line = ParseGetLine(PARSE_NORMAL);
2868 		if (line == NULL)
2869 			return NULL;
2870 
2871 		if (line[0] != '.')
2872 			return line;
2873 
2874 		/*
2875 		 * The line might be a conditional. Ask the conditional module
2876 		 * about it and act accordingly
2877 		 */
2878 		switch (Cond_EvalLine(line)) {
2879 		case COND_SKIP:
2880 			/*
2881 			 * Skip to next conditional that evaluates to
2882 			 * COND_PARSE.
2883 			 */
2884 			do {
2885 				line = ParseGetLine(PARSE_SKIP);
2886 			} while (line && Cond_EvalLine(line) != COND_PARSE);
2887 			if (line == NULL)
2888 				break;
2889 			continue;
2890 		case COND_PARSE:
2891 			continue;
2892 		case COND_INVALID:	/* Not a conditional line */
2893 			/* Check for .for loops */
2894 			rval = For_Eval(line);
2895 			if (rval == 0)
2896 				/* Not a .for line */
2897 				break;
2898 			if (rval < 0)
2899 				/* Syntax error - error printed, ignore line */
2900 				continue;
2901 			/* Start of a .for loop */
2902 			lineno = CurFile()->lineno;
2903 			/* Accumulate loop lines until matching .endfor */
2904 			do {
2905 				line = ParseGetLine(PARSE_RAW);
2906 				if (line == NULL) {
2907 					Parse_Error(PARSE_FATAL,
2908 					    "Unexpected end of file "
2909 					    "in for loop.");
2910 					break;
2911 				}
2912 			} while (For_Accum(line));
2913 			/* Stash each iteration as a new 'input file' */
2914 			For_Run(lineno);
2915 			/* Read next line from for-loop buffer */
2916 			continue;
2917 		}
2918 		return line;
2919 	}
2920 }
2921 
2922 static void
2923 FinishDependencyGroup(void)
2924 {
2925 	GNodeListNode *ln;
2926 
2927 	if (targets == NULL)
2928 		return;
2929 
2930 	for (ln = targets->first; ln != NULL; ln = ln->next) {
2931 		GNode *gn = ln->datum;
2932 
2933 		Suff_EndTransform(gn);
2934 
2935 		/*
2936 		 * Mark the target as already having commands if it does, to
2937 		 * keep from having shell commands on multiple dependency
2938 		 * lines.
2939 		 */
2940 		if (!Lst_IsEmpty(&gn->commands))
2941 			gn->type |= OP_HAS_COMMANDS;
2942 	}
2943 
2944 	Lst_Free(targets);
2945 	targets = NULL;
2946 }
2947 
2948 /* Add the command to each target from the current dependency spec. */
2949 static void
2950 ParseLine_ShellCommand(const char *p)
2951 {
2952 	cpp_skip_whitespace(&p);
2953 	if (*p == '\0')
2954 		return;		/* skip empty commands */
2955 
2956 	if (targets == NULL) {
2957 		Parse_Error(PARSE_FATAL,
2958 		    "Unassociated shell command \"%s\"", p);
2959 		return;
2960 	}
2961 
2962 	{
2963 		char *cmd = bmake_strdup(p);
2964 		GNodeListNode *ln;
2965 
2966 		for (ln = targets->first; ln != NULL; ln = ln->next) {
2967 			GNode *gn = ln->datum;
2968 			ParseAddCmd(gn, cmd);
2969 		}
2970 #ifdef CLEANUP
2971 		Lst_Append(&targCmds, cmd);
2972 #endif
2973 	}
2974 }
2975 
2976 static Boolean
2977 ParseDirective(char *line)
2978 {
2979 	char *cp;
2980 
2981 	if (*line == '.') {
2982 		/*
2983 		 * Lines that begin with '.' can be pretty much anything:
2984 		 *	- directives like '.include' or '.if',
2985 		 *	- suffix rules like '.c.o:',
2986 		 *	- dependencies for filenames that start with '.',
2987 		 *	- variable assignments like '.tmp=value'.
2988 		 */
2989 		cp = line + 1;
2990 		pp_skip_whitespace(&cp);
2991 		if (IsInclude(cp, FALSE)) {
2992 			ParseDoInclude(cp);
2993 			return TRUE;
2994 		}
2995 		if (strncmp(cp, "undef", 5) == 0) {
2996 			const char *varname;
2997 			cp += 5;
2998 			pp_skip_whitespace(&cp);
2999 			varname = cp;
3000 			for (; !ch_isspace(*cp) && *cp != '\0'; cp++)
3001 				continue;
3002 			*cp = '\0';
3003 			Var_Delete(varname, VAR_GLOBAL);
3004 			/* TODO: undefine all variables, not only the first */
3005 			/* TODO: use Str_Words, like everywhere else */
3006 			return TRUE;
3007 		} else if (strncmp(cp, "export", 6) == 0) {
3008 			cp += 6;
3009 			pp_skip_whitespace(&cp);
3010 			Var_Export(cp, TRUE);
3011 			return TRUE;
3012 		} else if (strncmp(cp, "unexport", 8) == 0) {
3013 			Var_UnExport(cp);
3014 			return TRUE;
3015 		} else if (strncmp(cp, "info", 4) == 0 ||
3016 			   strncmp(cp, "error", 5) == 0 ||
3017 			   strncmp(cp, "warning", 7) == 0) {
3018 			if (ParseMessage(cp))
3019 				return TRUE;
3020 		}
3021 	}
3022 	return FALSE;
3023 }
3024 
3025 static Boolean
3026 ParseVarassign(const char *line)
3027 {
3028 	VarAssign var;
3029 
3030 	if (!Parse_IsVar(line, &var))
3031 		return FALSE;
3032 
3033 	FinishDependencyGroup();
3034 	Parse_DoVar(&var, VAR_GLOBAL);
3035 	return TRUE;
3036 }
3037 
3038 static char *
3039 FindSemicolon(char *p)
3040 {
3041 	int level = 0;
3042 
3043 	for (; *p != '\0'; p++) {
3044 		if (*p == '\\' && p[1] != '\0') {
3045 			p++;
3046 			continue;
3047 		}
3048 
3049 		if (*p == '$' && (p[1] == '(' || p[1] == '{'))
3050 			level++;
3051 		else if (level > 0 && (*p == ')' || *p == '}'))
3052 			level--;
3053 		else if (level == 0 && *p == ';')
3054 			break;
3055 	}
3056 	return p;
3057 }
3058 
3059 /* dependency	-> target... op [source...]
3060  * op		-> ':' | '::' | '!' */
3061 static void
3062 ParseDependency(char *line)
3063 {
3064 	VarEvalFlags eflags;
3065 	char *expanded_line;
3066 	const char *shellcmd = NULL;
3067 
3068 	/*
3069 	 * For some reason - probably to make the parser impossible -
3070 	 * a ';' can be used to separate commands from dependencies.
3071 	 * Attempt to avoid ';' inside substitution patterns.
3072 	 */
3073 	{
3074 		char *semicolon = FindSemicolon(line);
3075 		if (*semicolon != '\0') {
3076 			/* Terminate the dependency list at the ';' */
3077 			*semicolon = '\0';
3078 			shellcmd = semicolon + 1;
3079 		}
3080 	}
3081 
3082 	/*
3083 	 * We now know it's a dependency line so it needs to have all
3084 	 * variables expanded before being parsed.
3085 	 *
3086 	 * XXX: Ideally the dependency line would first be split into
3087 	 * its left-hand side, dependency operator and right-hand side,
3088 	 * and then each side would be expanded on its own.  This would
3089 	 * allow for the left-hand side to allow only defined variables
3090 	 * and to allow variables on the right-hand side to be undefined
3091 	 * as well.
3092 	 *
3093 	 * Parsing the line first would also prevent that targets
3094 	 * generated from variable expressions are interpreted as the
3095 	 * dependency operator, such as in "target${:U\:} middle: source",
3096 	 * in which the middle is interpreted as a source, not a target.
3097 	 */
3098 
3099 	/* In lint mode, allow undefined variables to appear in
3100 	 * dependency lines.
3101 	 *
3102 	 * Ideally, only the right-hand side would allow undefined
3103 	 * variables since it is common to have optional dependencies.
3104 	 * Having undefined variables on the left-hand side is more
3105 	 * unusual though.  Since both sides are expanded in a single
3106 	 * pass, there is not much choice what to do here.
3107 	 *
3108 	 * In normal mode, it does not matter whether undefined
3109 	 * variables are allowed or not since as of 2020-09-14,
3110 	 * Var_Parse does not print any parse errors in such a case.
3111 	 * It simply returns the special empty string var_Error,
3112 	 * which cannot be detected in the result of Var_Subst. */
3113 	eflags = opts.lint ? VARE_WANTRES : VARE_WANTRES | VARE_UNDEFERR;
3114 	(void)Var_Subst(line, VAR_CMDLINE, eflags, &expanded_line);
3115 	/* TODO: handle errors */
3116 
3117 	/* Need a fresh list for the target nodes */
3118 	if (targets != NULL)
3119 		Lst_Free(targets);
3120 	targets = Lst_New();
3121 
3122 	ParseDoDependency(expanded_line);
3123 	free(expanded_line);
3124 
3125 	if (shellcmd != NULL)
3126 		ParseLine_ShellCommand(shellcmd);
3127 }
3128 
3129 static void
3130 ParseLine(char *line)
3131 {
3132 	if (ParseDirective(line))
3133 		return;
3134 
3135 	if (*line == '\t') {
3136 		ParseLine_ShellCommand(line + 1);
3137 		return;
3138 	}
3139 
3140 #ifdef SYSVINCLUDE
3141 	if (IsSysVInclude(line)) {
3142 		/*
3143 		 * It's an S3/S5-style "include".
3144 		 */
3145 		ParseTraditionalInclude(line);
3146 		return;
3147 	}
3148 #endif
3149 
3150 #ifdef GMAKEEXPORT
3151 	if (strncmp(line, "export", 6) == 0 && ch_isspace(line[6]) &&
3152 	    strchr(line, ':') == NULL) {
3153 		/*
3154 		 * It's a Gmake "export".
3155 		 */
3156 		ParseGmakeExport(line);
3157 		return;
3158 	}
3159 #endif
3160 
3161 	if (ParseVarassign(line))
3162 		return;
3163 
3164 	FinishDependencyGroup();
3165 
3166 	ParseDependency(line);
3167 }
3168 
3169 /*
3170  * Parse a top-level makefile, incorporating its content into the global
3171  * dependency graph.
3172  *
3173  * Input:
3174  *	name		The name of the file being read
3175  *	fd		The open file to parse; will be closed at the end
3176  */
3177 void
3178 Parse_File(const char *name, int fd)
3179 {
3180 	char *line;		/* the line we're working on */
3181 	struct loadedfile *lf;
3182 
3183 	lf = loadfile(name, fd);
3184 
3185 	assert(targets == NULL);
3186 
3187 	if (name == NULL)
3188 		name = "(stdin)";
3189 
3190 	Parse_SetInput(name, 0, -1, loadedfile_nextbuf, lf);
3191 	CurFile()->lf = lf;
3192 
3193 	do {
3194 		while ((line = ParseReadLine()) != NULL) {
3195 			DEBUG2(PARSE, "ParseReadLine (%d): '%s'\n",
3196 			    CurFile()->lineno, line);
3197 			ParseLine(line);
3198 		}
3199 		/* Reached EOF, but it may be just EOF of an include file. */
3200 	} while (ParseEOF());
3201 
3202 	FinishDependencyGroup();
3203 
3204 	if (fatals != 0) {
3205 		(void)fflush(stdout);
3206 		(void)fprintf(stderr,
3207 		    "%s: Fatal errors encountered -- cannot continue",
3208 		    progname);
3209 		PrintOnError(NULL, NULL);
3210 		exit(1);
3211 	}
3212 }
3213 
3214 /* Initialize the parsing module. */
3215 void
3216 Parse_Init(void)
3217 {
3218 	mainNode = NULL;
3219 	parseIncPath = SearchPath_New();
3220 	sysIncPath = SearchPath_New();
3221 	defSysIncPath = SearchPath_New();
3222 	Vector_Init(&includes, sizeof(IFile));
3223 }
3224 
3225 /* Clean up the parsing module. */
3226 void
3227 Parse_End(void)
3228 {
3229 #ifdef CLEANUP
3230 	Lst_DoneCall(&targCmds, free);
3231 	assert(targets == NULL);
3232 	SearchPath_Free(defSysIncPath);
3233 	SearchPath_Free(sysIncPath);
3234 	SearchPath_Free(parseIncPath);
3235 	assert(includes.len == 0);
3236 	Vector_Done(&includes);
3237 #endif
3238 }
3239 
3240 
3241 /*
3242  * Return a list containing the single main target to create.
3243  * If no such target exists, we Punt with an obnoxious error message.
3244  */
3245 void
3246 Parse_MainName(GNodeList *mainList)
3247 {
3248 	if (mainNode == NULL)
3249 		Punt("no target to make.");
3250 
3251 	if (mainNode->type & OP_DOUBLEDEP) {
3252 		Lst_Append(mainList, mainNode);
3253 		Lst_AppendAll(mainList, &mainNode->cohorts);
3254 	} else
3255 		Lst_Append(mainList, mainNode);
3256 
3257 	Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL);
3258 }
3259 
3260 int
3261 Parse_GetFatals(void)
3262 {
3263 	return fatals;
3264 }
3265