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