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