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