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