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