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