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