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