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