xref: /csrg-svn/usr.bin/make/parse.c (revision 60557)
140395Sbostic /*
240395Sbostic  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
340395Sbostic  * Copyright (c) 1988, 1989 by Adam de Boor
440395Sbostic  * Copyright (c) 1989 by Berkeley Softworks
540395Sbostic  * All rights reserved.
640395Sbostic  *
740395Sbostic  * This code is derived from software contributed to Berkeley by
840395Sbostic  * Adam de Boor.
940395Sbostic  *
1042752Sbostic  * %sccs.include.redist.c%
1140395Sbostic  */
1240395Sbostic 
1340395Sbostic #ifndef lint
14*60557Sbostic static char sccsid[] = "@(#)parse.c	5.20 (Berkeley) 05/28/93";
1540395Sbostic #endif /* not lint */
1640395Sbostic 
1740393Sbostic /*-
1840393Sbostic  * parse.c --
1940393Sbostic  *	Functions to parse a makefile.
2040393Sbostic  *
2140393Sbostic  *	One function, Parse_Init, must be called before any functions
2240393Sbostic  *	in this module are used. After that, the function Parse_File is the
2340393Sbostic  *	main entry point and controls most of the other functions in this
2440393Sbostic  *	module.
2540393Sbostic  *
2640393Sbostic  *	Most important structures are kept in Lsts. Directories for
2740393Sbostic  *	the #include "..." function are kept in the 'parseIncPath' Lst, while
2840393Sbostic  *	those for the #include <...> are kept in the 'sysIncPath' Lst. The
2940393Sbostic  *	targets currently being defined are kept in the 'targets' Lst.
3040393Sbostic  *
3140393Sbostic  *	The variables 'fname' and 'lineno' are used to track the name
3240393Sbostic  *	of the current file and the line number in that file so that error
3340393Sbostic  *	messages can be more meaningful.
3440393Sbostic  *
3540393Sbostic  * Interface:
3640393Sbostic  *	Parse_Init	    	    Initialization function which must be
3740393Sbostic  *	    	  	    	    called before anything else in this module
3840393Sbostic  *	    	  	    	    is used.
3940393Sbostic  *
4040393Sbostic  *	Parse_File	    	    Function used to parse a makefile. It must
4140393Sbostic  *	    	  	    	    be given the name of the file, which should
4240393Sbostic  *	    	  	    	    already have been opened, and a function
4340393Sbostic  *	    	  	    	    to call to read a character from the file.
4440393Sbostic  *
4540393Sbostic  *	Parse_IsVar	    	    Returns TRUE if the given line is a
4640393Sbostic  *	    	  	    	    variable assignment. Used by MainParseArgs
4740393Sbostic  *	    	  	    	    to determine if an argument is a target
4840393Sbostic  *	    	  	    	    or a variable assignment. Used internally
4940393Sbostic  *	    	  	    	    for pretty much the same thing...
5040393Sbostic  *
5140393Sbostic  *	Parse_Error	    	    Function called when an error occurs in
5240393Sbostic  *	    	  	    	    parsing. Used by the variable and
5340393Sbostic  *	    	  	    	    conditional modules.
5440393Sbostic  *	Parse_MainName	    	    Returns a Lst of the main target to create.
5540393Sbostic  */
5640393Sbostic 
5760285Sbostic #if __STDC__
5860285Sbostic #include <stdarg.h>
5960285Sbostic #else
6040444Sbostic #include <varargs.h>
6160285Sbostic #endif
6240444Sbostic #include <stdio.h>
6340444Sbostic #include <ctype.h>
6460285Sbostic #include <errno.h>
6560285Sbostic #include <sys/wait.h>
6640444Sbostic #include "make.h"
6760285Sbostic #include "hash.h"
6860285Sbostic #include "dir.h"
6960285Sbostic #include "job.h"
7040444Sbostic #include "buf.h"
7140515Sbostic #include "pathnames.h"
7240393Sbostic 
7340393Sbostic /*
7440393Sbostic  * These values are returned by ParseEOF to tell Parse_File whether to
7540393Sbostic  * CONTINUE parsing, i.e. it had only reached the end of an include file,
7640393Sbostic  * or if it's DONE.
7740393Sbostic  */
7840393Sbostic #define	CONTINUE	1
7940393Sbostic #define	DONE		0
8040393Sbostic static Lst     	    targets;	/* targets we're working on */
8140393Sbostic static Boolean	    inLine;	/* true if currently in a dependency
8240393Sbostic 				 * line or its commands */
8360285Sbostic typedef struct {
8460285Sbostic     char *str;
8560285Sbostic     char *ptr;
8660285Sbostic } PTR;
8740393Sbostic 
8840393Sbostic static char    	    *fname;	/* name of current file (for errors) */
8940393Sbostic static int          lineno;	/* line number in current file */
9060285Sbostic static FILE   	    *curFILE = NULL; 	/* current makefile */
9140393Sbostic 
9260285Sbostic static PTR 	    *curPTR = NULL; 	/* current makefile */
9360285Sbostic 
9440393Sbostic static int	    fatals = 0;
9540393Sbostic 
9640393Sbostic static GNode	    *mainNode;	/* The main target to create. This is the
9740393Sbostic 				 * first target on the first dependency
9840393Sbostic 				 * line in the first makefile */
9940393Sbostic /*
10040393Sbostic  * Definitions for handling #include specifications
10140393Sbostic  */
10240393Sbostic typedef struct IFile {
10340393Sbostic     char           *fname;	    /* name of previous file */
10440393Sbostic     int             lineno;	    /* saved line number */
10560285Sbostic     FILE *          F;		    /* the open stream */
10660285Sbostic     PTR *	    p;	    	    /* the char pointer */
10760285Sbostic } IFile;
10840393Sbostic 
10940393Sbostic static Lst      includes;  	/* stack of IFiles generated by
11040393Sbostic 				 * #includes */
11140393Sbostic Lst         	parseIncPath;	/* list of directories for "..." includes */
11240393Sbostic Lst         	sysIncPath;	/* list of directories for <...> includes */
11340393Sbostic 
11440393Sbostic /*-
11540393Sbostic  * specType contains the SPECial TYPE of the current target. It is
11640393Sbostic  * Not if the target is unspecial. If it *is* special, however, the children
11740393Sbostic  * are linked as children of the parent but not vice versa. This variable is
11840393Sbostic  * set in ParseDoDependency
11940393Sbostic  */
12040393Sbostic typedef enum {
12140393Sbostic     Begin,  	    /* .BEGIN */
12240393Sbostic     Default,	    /* .DEFAULT */
12340393Sbostic     End,    	    /* .END */
12440393Sbostic     Ignore,	    /* .IGNORE */
12540393Sbostic     Includes,	    /* .INCLUDES */
12640393Sbostic     Interrupt,	    /* .INTERRUPT */
12740393Sbostic     Libs,	    /* .LIBS */
12840393Sbostic     MFlags,	    /* .MFLAGS or .MAKEFLAGS */
12940393Sbostic     Main,	    /* .MAIN and we don't have anything user-specified to
13040393Sbostic 		     * make */
13160285Sbostic     NoExport,	    /* .NOEXPORT */
13240393Sbostic     Not,	    /* Not special */
13340393Sbostic     NotParallel,    /* .NOTPARALELL */
13440393Sbostic     Null,   	    /* .NULL */
13540393Sbostic     Order,  	    /* .ORDER */
13660285Sbostic     ExPath,	    /* .PATH */
13740393Sbostic     Precious,	    /* .PRECIOUS */
13860285Sbostic     ExShell,	    /* .SHELL */
13940393Sbostic     Silent,	    /* .SILENT */
14040393Sbostic     SingleShell,    /* .SINGLESHELL */
14140393Sbostic     Suffixes,	    /* .SUFFIXES */
14260285Sbostic     Attribute	    /* Generic attribute */
14340393Sbostic } ParseSpecial;
14440393Sbostic 
14560285Sbostic static ParseSpecial specType;
14640393Sbostic 
14740393Sbostic /*
14840393Sbostic  * Predecessor node for handling .ORDER. Initialized to NILGNODE when .ORDER
14940393Sbostic  * seen, then set to each successive source on the line.
15040393Sbostic  */
15140393Sbostic static GNode	*predecessor;
15240393Sbostic 
15340393Sbostic /*
15440393Sbostic  * The parseKeywords table is searched using binary search when deciding
15540393Sbostic  * if a target or source is special. The 'spec' field is the ParseSpecial
15640393Sbostic  * type of the keyword ("Not" if the keyword isn't special as a target) while
15740393Sbostic  * the 'op' field is the operator to apply to the list of targets if the
15840393Sbostic  * keyword is used as a source ("0" if the keyword isn't special as a source)
15940393Sbostic  */
16040393Sbostic static struct {
16140393Sbostic     char    	  *name;    	/* Name of keyword */
16240393Sbostic     ParseSpecial  spec;	    	/* Type when used as a target */
16340393Sbostic     int	    	  op;	    	/* Operator when used as a source */
16440393Sbostic } parseKeywords[] = {
16540393Sbostic { ".BEGIN", 	  Begin,    	0 },
16640393Sbostic { ".DEFAULT",	  Default,  	0 },
16740546Sbostic { ".OPTIONAL",	  Attribute,   	OP_OPTIONAL },
16840393Sbostic { ".END",   	  End,	    	0 },
16940393Sbostic { ".EXEC",	  Attribute,   	OP_EXEC },
17040393Sbostic { ".IGNORE",	  Ignore,   	OP_IGNORE },
17140393Sbostic { ".INCLUDES",	  Includes, 	0 },
17240393Sbostic { ".INTERRUPT",	  Interrupt,	0 },
17340393Sbostic { ".INVISIBLE",	  Attribute,   	OP_INVISIBLE },
17440393Sbostic { ".JOIN",  	  Attribute,   	OP_JOIN },
17540393Sbostic { ".LIBS",  	  Libs,	    	0 },
17640393Sbostic { ".MAIN",	  Main,		0 },
17740393Sbostic { ".MAKE",  	  Attribute,   	OP_MAKE },
17840393Sbostic { ".MAKEFLAGS",	  MFlags,   	0 },
17940393Sbostic { ".MFLAGS",	  MFlags,   	0 },
18040393Sbostic { ".NOTMAIN",	  Attribute,   	OP_NOTMAIN },
18140393Sbostic { ".NOTPARALLEL", NotParallel,	0 },
18240393Sbostic { ".NULL",  	  Null,	    	0 },
18340393Sbostic { ".ORDER", 	  Order,    	0 },
18460285Sbostic { ".PATH",	  ExPath,	0 },
18540393Sbostic { ".PRECIOUS",	  Precious, 	OP_PRECIOUS },
18640393Sbostic { ".RECURSIVE",	  Attribute,	OP_MAKE },
18760285Sbostic { ".SHELL", 	  ExShell,    	0 },
18840393Sbostic { ".SILENT",	  Silent,   	OP_SILENT },
18940393Sbostic { ".SINGLESHELL", SingleShell,	0 },
19040393Sbostic { ".SUFFIXES",	  Suffixes, 	0 },
19140393Sbostic { ".USE",   	  Attribute,   	OP_USE },
19240393Sbostic };
19340424Sbostic 
19460285Sbostic static int ParseFindKeyword __P((char *));
19560285Sbostic static int ParseLinkSrc __P((GNode *, GNode *));
19660285Sbostic static int ParseDoOp __P((GNode *, int));
19760285Sbostic static void ParseDoSrc __P((int, char *));
19860285Sbostic static int ParseFindMain __P((GNode *));
19960285Sbostic static int ParseAddDir __P((Lst, char *));
20060285Sbostic static int ParseClearPath __P((Lst));
20160285Sbostic static void ParseDoDependency __P((char *));
20260285Sbostic static int ParseAddCmd __P((GNode *, char *));
20360285Sbostic static int ParseReadc __P((void));
204*60557Sbostic static void ParseUnreadc __P((int));
20560285Sbostic static int ParseHasCommands __P((GNode *));
20660285Sbostic static void ParseDoInclude __P((char *));
20760285Sbostic #ifdef SYSVINCLUDE
20860285Sbostic static void ParseTraditionalInclude __P((char *));
20960285Sbostic #endif
21060285Sbostic static int ParseEOF __P((int));
21160285Sbostic static char *ParseReadLine __P((void));
21260285Sbostic static char *ParseSkipLine __P((int));
21360285Sbostic static void ParseFinishLine __P((void));
21460285Sbostic 
21540393Sbostic /*-
21640393Sbostic  *----------------------------------------------------------------------
21740393Sbostic  * ParseFindKeyword --
21840393Sbostic  *	Look in the table of keywords for one matching the given string.
21940393Sbostic  *
22040393Sbostic  * Results:
22140393Sbostic  *	The index of the keyword, or -1 if it isn't there.
22240393Sbostic  *
22340393Sbostic  * Side Effects:
22440393Sbostic  *	None
22540393Sbostic  *----------------------------------------------------------------------
22640393Sbostic  */
22740393Sbostic static int
22840393Sbostic ParseFindKeyword (str)
22940393Sbostic     char	    *str;		/* String to find */
23040393Sbostic {
23140393Sbostic     register int    start,
23240393Sbostic 		    end,
23340393Sbostic 		    cur;
23440393Sbostic     register int    diff;
23540393Sbostic 
23640393Sbostic     start = 0;
23740393Sbostic     end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
23840393Sbostic 
23940393Sbostic     do {
24040393Sbostic 	cur = start + ((end - start) / 2);
24140393Sbostic 	diff = strcmp (str, parseKeywords[cur].name);
24240393Sbostic 
24340393Sbostic 	if (diff == 0) {
24440393Sbostic 	    return (cur);
24540393Sbostic 	} else if (diff < 0) {
24640393Sbostic 	    end = cur - 1;
24740393Sbostic 	} else {
24840393Sbostic 	    start = cur + 1;
24940393Sbostic 	}
25040393Sbostic     } while (start <= end);
25140393Sbostic     return (-1);
25240393Sbostic }
25340424Sbostic 
25440393Sbostic /*-
25540393Sbostic  * Parse_Error  --
25640393Sbostic  *	Error message abort function for parsing. Prints out the context
25740393Sbostic  *	of the error (line number and file) as well as the message with
25840393Sbostic  *	two optional arguments.
25940393Sbostic  *
26040393Sbostic  * Results:
26140393Sbostic  *	None
26240393Sbostic  *
26340393Sbostic  * Side Effects:
26440393Sbostic  *	"fatals" is incremented if the level is PARSE_FATAL.
26540393Sbostic  */
26640444Sbostic /* VARARGS */
26740393Sbostic void
26860285Sbostic #if __STDC__
26960285Sbostic Parse_Error(int type, const char *fmt, ...)
27060285Sbostic #else
27160285Sbostic Parse_Error(type, fmt, va_alist)
27260285Sbostic 	int type;
27360285Sbostic 	char *fmt;
27440444Sbostic 	va_dcl
27560285Sbostic #endif
27640393Sbostic {
27740444Sbostic 	va_list ap;
27860285Sbostic #if __STDC__
27960285Sbostic 	va_start(ap, fmt);
28060285Sbostic #else
28160285Sbostic 	va_start(ap);
28260285Sbostic #endif
28340444Sbostic 	(void)fprintf(stderr, "\"%s\", line %d: ", fname, lineno);
28440444Sbostic 	if (type == PARSE_WARNING)
28540540Sbostic 		(void)fprintf(stderr, "warning: ");
28640444Sbostic 	(void)vfprintf(stderr, fmt, ap);
28740444Sbostic 	va_end(ap);
28840444Sbostic 	(void)fprintf(stderr, "\n");
28940444Sbostic 	(void)fflush(stderr);
29040444Sbostic 	if (type == PARSE_FATAL)
29140444Sbostic 		fatals += 1;
29240393Sbostic }
29340424Sbostic 
29440393Sbostic /*-
29540393Sbostic  *---------------------------------------------------------------------
29640393Sbostic  * ParseLinkSrc  --
29740393Sbostic  *	Link the parent node to its new child. Used in a Lst_ForEach by
29840393Sbostic  *	ParseDoDependency. If the specType isn't 'Not', the parent
29940393Sbostic  *	isn't linked as a parent of the child.
30040393Sbostic  *
30140393Sbostic  * Results:
30240393Sbostic  *	Always = 0
30340393Sbostic  *
30440393Sbostic  * Side Effects:
30540393Sbostic  *	New elements are added to the parents list of cgn and the
30640393Sbostic  *	children list of cgn. the unmade field of pgn is updated
30740393Sbostic  *	to reflect the additional child.
30840393Sbostic  *---------------------------------------------------------------------
30940393Sbostic  */
31040393Sbostic static int
31140393Sbostic ParseLinkSrc (pgn, cgn)
31240393Sbostic     GNode          *pgn;	/* The parent node */
31340393Sbostic     GNode          *cgn;	/* The child node */
31440393Sbostic {
31540393Sbostic     if (Lst_Member (pgn->children, (ClientData)cgn) == NILLNODE) {
31640393Sbostic 	(void)Lst_AtEnd (pgn->children, (ClientData)cgn);
31740393Sbostic 	if (specType == Not) {
31840393Sbostic 	    (void)Lst_AtEnd (cgn->parents, (ClientData)pgn);
31940393Sbostic 	}
32040393Sbostic 	pgn->unmade += 1;
32140393Sbostic     }
32240393Sbostic     return (0);
32340393Sbostic }
32440424Sbostic 
32540393Sbostic /*-
32640393Sbostic  *---------------------------------------------------------------------
32740393Sbostic  * ParseDoOp  --
32840393Sbostic  *	Apply the parsed operator to the given target node. Used in a
32940393Sbostic  *	Lst_ForEach call by ParseDoDependency once all targets have
33040393Sbostic  *	been found and their operator parsed. If the previous and new
33140393Sbostic  *	operators are incompatible, a major error is taken.
33240393Sbostic  *
33340393Sbostic  * Results:
33440393Sbostic  *	Always 0
33540393Sbostic  *
33640393Sbostic  * Side Effects:
33740393Sbostic  *	The type field of the node is altered to reflect any new bits in
33840393Sbostic  *	the op.
33940393Sbostic  *---------------------------------------------------------------------
34040393Sbostic  */
34140393Sbostic static int
34240393Sbostic ParseDoOp (gn, op)
34340393Sbostic     GNode          *gn;		/* The node to which the operator is to be
34440393Sbostic 				 * applied */
34540393Sbostic     int             op;		/* The operator to apply */
34640393Sbostic {
34740393Sbostic     /*
34840393Sbostic      * If the dependency mask of the operator and the node don't match and
34940393Sbostic      * the node has actually had an operator applied to it before, and
35040393Sbostic      * the operator actually has some dependency information in it, complain.
35140393Sbostic      */
35240393Sbostic     if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
35340393Sbostic 	!OP_NOP(gn->type) && !OP_NOP(op))
35440393Sbostic     {
35540393Sbostic 	Parse_Error (PARSE_FATAL, "Inconsistent operator for %s", gn->name);
35640393Sbostic 	return (1);
35740393Sbostic     }
35840393Sbostic 
35940393Sbostic     if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
36040393Sbostic 	/*
36140393Sbostic 	 * If the node was the object of a :: operator, we need to create a
36240393Sbostic 	 * new instance of it for the children and commands on this dependency
36340393Sbostic 	 * line. The new instance is placed on the 'cohorts' list of the
36440393Sbostic 	 * initial one (note the initial one is not on its own cohorts list)
36540393Sbostic 	 * and the new instance is linked to all parents of the initial
36640393Sbostic 	 * instance.
36740393Sbostic 	 */
36840393Sbostic 	register GNode	*cohort;
36940393Sbostic 	LstNode	    	ln;
37040393Sbostic 
37140393Sbostic 	cohort = Targ_NewGN(gn->name);
37240393Sbostic 	/*
37340393Sbostic 	 * Duplicate links to parents so graph traversal is simple. Perhaps
37440393Sbostic 	 * some type bits should be duplicated?
37540393Sbostic 	 *
37640393Sbostic 	 * Make the cohort invisible as well to avoid duplicating it into
37740393Sbostic 	 * other variables. True, parents of this target won't tend to do
37840393Sbostic 	 * anything with their local variables, but better safe than
37940393Sbostic 	 * sorry.
38040393Sbostic 	 */
38140393Sbostic 	Lst_ForEach(gn->parents, ParseLinkSrc, (ClientData)cohort);
38240393Sbostic 	cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
38340393Sbostic 	(void)Lst_AtEnd(gn->cohorts, (ClientData)cohort);
38440393Sbostic 
38540393Sbostic 	/*
38640393Sbostic 	 * Replace the node in the targets list with the new copy
38740393Sbostic 	 */
38840393Sbostic 	ln = Lst_Member(targets, (ClientData)gn);
38940393Sbostic 	Lst_Replace(ln, (ClientData)cohort);
39040393Sbostic 	gn = cohort;
39140393Sbostic     }
39240393Sbostic     /*
39340393Sbostic      * We don't want to nuke any previous flags (whatever they were) so we
39440393Sbostic      * just OR the new operator into the old
39540393Sbostic      */
39640393Sbostic     gn->type |= op;
39740393Sbostic 
39840393Sbostic     return (0);
39940393Sbostic }
40040424Sbostic 
40140393Sbostic /*-
40240393Sbostic  *---------------------------------------------------------------------
40340393Sbostic  * ParseDoSrc  --
40440393Sbostic  *	Given the name of a source, figure out if it is an attribute
40540393Sbostic  *	and apply it to the targets if it is. Else decide if there is
40640393Sbostic  *	some attribute which should be applied *to* the source because
40740393Sbostic  *	of some special target and apply it if so. Otherwise, make the
40840393Sbostic  *	source be a child of the targets in the list 'targets'
40940393Sbostic  *
41040393Sbostic  * Results:
41140393Sbostic  *	None
41240393Sbostic  *
41340393Sbostic  * Side Effects:
41440393Sbostic  *	Operator bits may be added to the list of targets or to the source.
41540393Sbostic  *	The targets may have a new source added to their lists of children.
41640393Sbostic  *---------------------------------------------------------------------
41740393Sbostic  */
41840393Sbostic static void
41940393Sbostic ParseDoSrc (tOp, src)
42040393Sbostic     int		tOp;	/* operator (if any) from special targets */
42140393Sbostic     char	*src;	/* name of the source to handle */
42240393Sbostic {
42340393Sbostic     int		op;	/* operator (if any) from special source */
42440393Sbostic     GNode	*gn;
42540393Sbostic 
42640393Sbostic     op = 0;
42740393Sbostic     if (*src == '.' && isupper (src[1])) {
42840393Sbostic 	int keywd = ParseFindKeyword(src);
42940393Sbostic 	if (keywd != -1) {
43040393Sbostic 	    op = parseKeywords[keywd].op;
43140393Sbostic 	}
43240393Sbostic     }
43340393Sbostic     if (op != 0) {
43440393Sbostic 	Lst_ForEach (targets, ParseDoOp, (ClientData)op);
43540393Sbostic     } else if (specType == Main) {
43640393Sbostic 	/*
43740393Sbostic 	 * If we have noted the existence of a .MAIN, it means we need
43840393Sbostic 	 * to add the sources of said target to the list of things
43940393Sbostic 	 * to create. The string 'src' is likely to be free, so we
44040393Sbostic 	 * must make a new copy of it. Note that this will only be
44140393Sbostic 	 * invoked if the user didn't specify a target on the command
44240393Sbostic 	 * line. This is to allow #ifmake's to succeed, or something...
44340393Sbostic 	 */
44440424Sbostic 	(void) Lst_AtEnd (create, (ClientData)strdup(src));
44540393Sbostic 	/*
44640393Sbostic 	 * Add the name to the .TARGETS variable as well, so the user cna
44740393Sbostic 	 * employ that, if desired.
44840393Sbostic 	 */
44940393Sbostic 	Var_Append(".TARGETS", src, VAR_GLOBAL);
45040393Sbostic     } else if (specType == Order) {
45140393Sbostic 	/*
45240393Sbostic 	 * Create proper predecessor/successor links between the previous
45340393Sbostic 	 * source and the current one.
45440393Sbostic 	 */
45540393Sbostic 	gn = Targ_FindNode(src, TARG_CREATE);
45640393Sbostic 	if (predecessor != NILGNODE) {
45740393Sbostic 	    (void)Lst_AtEnd(predecessor->successors, (ClientData)gn);
45840393Sbostic 	    (void)Lst_AtEnd(gn->preds, (ClientData)predecessor);
45940393Sbostic 	}
46040393Sbostic 	/*
46140393Sbostic 	 * The current source now becomes the predecessor for the next one.
46240393Sbostic 	 */
46340393Sbostic 	predecessor = gn;
46440393Sbostic     } else {
46540393Sbostic 	/*
46640393Sbostic 	 * If the source is not an attribute, we need to find/create
46740393Sbostic 	 * a node for it. After that we can apply any operator to it
46840393Sbostic 	 * from a special target or link it to its parents, as
46940393Sbostic 	 * appropriate.
47040393Sbostic 	 *
47140393Sbostic 	 * In the case of a source that was the object of a :: operator,
47240393Sbostic 	 * the attribute is applied to all of its instances (as kept in
47340393Sbostic 	 * the 'cohorts' list of the node) or all the cohorts are linked
47440393Sbostic 	 * to all the targets.
47540393Sbostic 	 */
47640393Sbostic 	gn = Targ_FindNode (src, TARG_CREATE);
47740393Sbostic 	if (tOp) {
47840393Sbostic 	    gn->type |= tOp;
47940393Sbostic 	} else {
48040393Sbostic 	    Lst_ForEach (targets, ParseLinkSrc, (ClientData)gn);
48140393Sbostic 	}
48240393Sbostic 	if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
48340393Sbostic 	    register GNode  	*cohort;
48440393Sbostic 	    register LstNode	ln;
48540393Sbostic 
48640393Sbostic 	    for (ln=Lst_First(gn->cohorts); ln != NILLNODE; ln = Lst_Succ(ln)){
48740393Sbostic 		cohort = (GNode *)Lst_Datum(ln);
48840393Sbostic 		if (tOp) {
48940393Sbostic 		    cohort->type |= tOp;
49040393Sbostic 		} else {
49140393Sbostic 		    Lst_ForEach(targets, ParseLinkSrc, (ClientData)cohort);
49240393Sbostic 		}
49340393Sbostic 	    }
49440393Sbostic 	}
49540393Sbostic     }
49640393Sbostic }
49740424Sbostic 
49840393Sbostic /*-
49940393Sbostic  *-----------------------------------------------------------------------
50040393Sbostic  * ParseFindMain --
50140393Sbostic  *	Find a real target in the list and set it to be the main one.
50240393Sbostic  *	Called by ParseDoDependency when a main target hasn't been found
50340393Sbostic  *	yet.
50440393Sbostic  *
50540393Sbostic  * Results:
50640393Sbostic  *	0 if main not found yet, 1 if it is.
50740393Sbostic  *
50840393Sbostic  * Side Effects:
50940393Sbostic  *	mainNode is changed and Targ_SetMain is called.
51040393Sbostic  *
51140393Sbostic  *-----------------------------------------------------------------------
51240393Sbostic  */
51340393Sbostic static int
51440393Sbostic ParseFindMain(gn)
51540393Sbostic     GNode   	  *gn;	    /* Node to examine */
51640393Sbostic {
51740393Sbostic     if ((gn->type & (OP_NOTMAIN|OP_USE|OP_EXEC|OP_TRANSFORM)) == 0) {
51840393Sbostic 	mainNode = gn;
51940393Sbostic 	Targ_SetMain(gn);
52040393Sbostic 	return (1);
52140393Sbostic     } else {
52240393Sbostic 	return (0);
52340393Sbostic     }
52440393Sbostic }
52540424Sbostic 
52640393Sbostic /*-
52740393Sbostic  *-----------------------------------------------------------------------
52840393Sbostic  * ParseAddDir --
52940393Sbostic  *	Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
53040393Sbostic  *
53140393Sbostic  * Results:
53240393Sbostic  *	=== 0
53340393Sbostic  *
53440393Sbostic  * Side Effects:
53540393Sbostic  *	See Dir_AddDir.
53640393Sbostic  *
53740393Sbostic  *-----------------------------------------------------------------------
53840393Sbostic  */
53940393Sbostic static int
54040393Sbostic ParseAddDir(path, name)
54140393Sbostic     Lst	    path;
54240393Sbostic     char    *name;
54340393Sbostic {
54440393Sbostic     Dir_AddDir(path, name);
54540393Sbostic     return(0);
54640393Sbostic }
54740424Sbostic 
54840393Sbostic /*-
54940393Sbostic  *-----------------------------------------------------------------------
55040393Sbostic  * ParseClearPath --
55140393Sbostic  *	Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
55240393Sbostic  *
55340393Sbostic  * Results:
55440393Sbostic  *	=== 0
55540393Sbostic  *
55640393Sbostic  * Side Effects:
55740393Sbostic  *	See Dir_ClearPath
55840393Sbostic  *
55940393Sbostic  *-----------------------------------------------------------------------
56040393Sbostic  */
56140393Sbostic static int
56240393Sbostic ParseClearPath(path)
56340393Sbostic     Lst	    path;
56440393Sbostic {
56540393Sbostic     Dir_ClearPath(path);
56640393Sbostic     return(0);
56740393Sbostic }
56840424Sbostic 
56940393Sbostic /*-
57040393Sbostic  *---------------------------------------------------------------------
57140393Sbostic  * ParseDoDependency  --
57240393Sbostic  *	Parse the dependency line in line.
57340393Sbostic  *
57440393Sbostic  * Results:
57540393Sbostic  *	None
57640393Sbostic  *
57740393Sbostic  * Side Effects:
57840393Sbostic  *	The nodes of the sources are linked as children to the nodes of the
57940393Sbostic  *	targets. Some nodes may be created.
58040393Sbostic  *
58140393Sbostic  *	We parse a dependency line by first extracting words from the line and
58240393Sbostic  * finding nodes in the list of all targets with that name. This is done
58340393Sbostic  * until a character is encountered which is an operator character. Currently
58440393Sbostic  * these are only ! and :. At this point the operator is parsed and the
58540393Sbostic  * pointer into the line advanced until the first source is encountered.
58640393Sbostic  * 	The parsed operator is applied to each node in the 'targets' list,
58740393Sbostic  * which is where the nodes found for the targets are kept, by means of
58840393Sbostic  * the ParseDoOp function.
58940393Sbostic  *	The sources are read in much the same way as the targets were except
59040393Sbostic  * that now they are expanded using the wildcarding scheme of the C-Shell
59140393Sbostic  * and all instances of the resulting words in the list of all targets
59240393Sbostic  * are found. Each of the resulting nodes is then linked to each of the
59340393Sbostic  * targets as one of its children.
59440393Sbostic  *	Certain targets are handled specially. These are the ones detailed
59540393Sbostic  * by the specType variable.
59640393Sbostic  *	The storing of transformation rules is also taken care of here.
59740393Sbostic  * A target is recognized as a transformation rule by calling
59840393Sbostic  * Suff_IsTransform. If it is a transformation rule, its node is gotten
59940393Sbostic  * from the suffix module via Suff_AddTransform rather than the standard
60040393Sbostic  * Targ_FindNode in the target module.
60140393Sbostic  *---------------------------------------------------------------------
60240393Sbostic  */
60340393Sbostic static void
60440393Sbostic ParseDoDependency (line)
60540393Sbostic     char           *line;	/* the line to parse */
60640393Sbostic {
60740393Sbostic     register char  *cp;		/* our current position */
60840393Sbostic     register GNode *gn;		/* a general purpose temporary node */
60940393Sbostic     register int    op;		/* the operator on the line */
61040393Sbostic     char            savec;	/* a place to save a character */
61140393Sbostic     Lst    	    paths;   	/* List of search paths to alter when parsing
61240393Sbostic 				 * a list of .PATH targets */
61340393Sbostic     int	    	    tOp;    	/* operator from special target */
61440393Sbostic     Lst	    	    sources;	/* list of source names after expansion */
61540393Sbostic     Lst 	    curTargs;	/* list of target names to be found and added
61640393Sbostic 				 * to the targets list */
61740393Sbostic 
61840393Sbostic     tOp = 0;
61940393Sbostic 
62040393Sbostic     specType = Not;
62140393Sbostic     paths = (Lst)NULL;
62240393Sbostic 
62340393Sbostic     curTargs = Lst_Init(FALSE);
62440393Sbostic 
62540393Sbostic     do {
62640393Sbostic 	for (cp = line;
62740393Sbostic 	     *cp && !isspace (*cp) &&
62840393Sbostic 	     (*cp != '!') && (*cp != ':') && (*cp != '(');
62940393Sbostic 	     cp++)
63040393Sbostic 	{
63140393Sbostic 	    if (*cp == '$') {
63240393Sbostic 		/*
63340393Sbostic 		 * Must be a dynamic source (would have been expanded
63440393Sbostic 		 * otherwise), so call the Var module to parse the puppy
63540393Sbostic 		 * so we can safely advance beyond it...There should be
63640393Sbostic 		 * no errors in this, as they would have been discovered
63740393Sbostic 		 * in the initial Var_Subst and we wouldn't be here.
63840393Sbostic 		 */
63940393Sbostic 		int 	length;
64040393Sbostic 		Boolean	freeIt;
64140393Sbostic 		char	*result;
64240393Sbostic 
64340393Sbostic 		result=Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
64440393Sbostic 
64540393Sbostic 		if (freeIt) {
64640393Sbostic 		    free(result);
64740393Sbostic 		}
64840393Sbostic 		cp += length-1;
64940393Sbostic 	    }
65040393Sbostic 	    continue;
65140393Sbostic 	}
65240393Sbostic 	if (*cp == '(') {
65340393Sbostic 	    /*
65440393Sbostic 	     * Archives must be handled specially to make sure the OP_ARCHV
65540393Sbostic 	     * flag is set in their 'type' field, for one thing, and because
65640393Sbostic 	     * things like "archive(file1.o file2.o file3.o)" are permissible.
65740393Sbostic 	     * Arch_ParseArchive will set 'line' to be the first non-blank
65840393Sbostic 	     * after the archive-spec. It creates/finds nodes for the members
65940393Sbostic 	     * and places them on the given list, returning SUCCESS if all
66040393Sbostic 	     * went well and FAILURE if there was an error in the
66140393Sbostic 	     * specification. On error, line should remain untouched.
66240393Sbostic 	     */
66340393Sbostic 	    if (Arch_ParseArchive (&line, targets, VAR_CMD) != SUCCESS) {
66440393Sbostic 		Parse_Error (PARSE_FATAL,
66540393Sbostic 			     "Error in archive specification: \"%s\"", line);
66640393Sbostic 		return;
66740393Sbostic 	    } else {
66840393Sbostic 		continue;
66940393Sbostic 	    }
67040393Sbostic 	}
67140393Sbostic 	savec = *cp;
67240393Sbostic 
67340393Sbostic 	if (!*cp) {
67440393Sbostic 	    /*
67540393Sbostic 	     * Ending a dependency line without an operator is a Bozo
67640393Sbostic 	     * no-no
67740393Sbostic 	     */
67840393Sbostic 	    Parse_Error (PARSE_FATAL, "Need an operator");
67940393Sbostic 	    return;
68040393Sbostic 	}
68140393Sbostic 	*cp = '\0';
68240393Sbostic 	/*
68340393Sbostic 	 * Have a word in line. See if it's a special target and set
68440393Sbostic 	 * specType to match it.
68540393Sbostic 	 */
68640393Sbostic 	if (*line == '.' && isupper (line[1])) {
68740393Sbostic 	    /*
68840393Sbostic 	     * See if the target is a special target that must have it
68940393Sbostic 	     * or its sources handled specially.
69040393Sbostic 	     */
69140393Sbostic 	    int keywd = ParseFindKeyword(line);
69240393Sbostic 	    if (keywd != -1) {
69360285Sbostic 		if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
69440393Sbostic 		    Parse_Error(PARSE_FATAL, "Mismatched special targets");
69540393Sbostic 		    return;
69640393Sbostic 		}
69740393Sbostic 
69840393Sbostic 		specType = parseKeywords[keywd].spec;
69940393Sbostic 		tOp = parseKeywords[keywd].op;
70040393Sbostic 
70140393Sbostic 		/*
70240393Sbostic 		 * Certain special targets have special semantics:
70340393Sbostic 		 *	.PATH		Have to set the dirSearchPath
70440393Sbostic 		 *			variable too
70540393Sbostic 		 *	.MAIN		Its sources are only used if
70640393Sbostic 		 *			nothing has been specified to
70740393Sbostic 		 *			create.
70840393Sbostic 		 *	.DEFAULT    	Need to create a node to hang
70940393Sbostic 		 *			commands on, but we don't want
71040393Sbostic 		 *			it in the graph, nor do we want
71140393Sbostic 		 *			it to be the Main Target, so we
71240393Sbostic 		 *			create it, set OP_NOTMAIN and
71340393Sbostic 		 *			add it to the list, setting
71440393Sbostic 		 *			DEFAULT to the new node for
71540393Sbostic 		 *			later use. We claim the node is
71640393Sbostic 		 *	    	    	A transformation rule to make
71740393Sbostic 		 *	    	    	life easier later, when we'll
71840393Sbostic 		 *	    	    	use Make_HandleUse to actually
71940393Sbostic 		 *	    	    	apply the .DEFAULT commands.
72040393Sbostic 		 *	.BEGIN
72140393Sbostic 		 *	.END
72240393Sbostic 		 *	.INTERRUPT  	Are not to be considered the
72340393Sbostic 		 *			main target.
72440393Sbostic 		 *  	.NOTPARALLEL	Make only one target at a time.
72540393Sbostic 		 *  	.SINGLESHELL	Create a shell for each command.
72640393Sbostic 		 *  	.ORDER	    	Must set initial predecessor to NIL
72740393Sbostic 		 */
72840393Sbostic 		switch (specType) {
72960285Sbostic 		    case ExPath:
73040393Sbostic 			if (paths == NULL) {
73140393Sbostic 			    paths = Lst_Init(FALSE);
73240393Sbostic 			}
73340393Sbostic 			(void)Lst_AtEnd(paths, (ClientData)dirSearchPath);
73440393Sbostic 			break;
73540393Sbostic 		    case Main:
73640393Sbostic 			if (!Lst_IsEmpty(create)) {
73740393Sbostic 			    specType = Not;
73840393Sbostic 			}
73940393Sbostic 			break;
74040393Sbostic 		    case Begin:
74140393Sbostic 		    case End:
74240393Sbostic 		    case Interrupt:
74340393Sbostic 			gn = Targ_FindNode(line, TARG_CREATE);
74440393Sbostic 			gn->type |= OP_NOTMAIN;
74540393Sbostic 			(void)Lst_AtEnd(targets, (ClientData)gn);
74640393Sbostic 			break;
74740393Sbostic 		    case Default:
74840393Sbostic 			gn = Targ_NewGN(".DEFAULT");
74940393Sbostic 			gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
75040393Sbostic 			(void)Lst_AtEnd(targets, (ClientData)gn);
75140393Sbostic 			DEFAULT = gn;
75240393Sbostic 			break;
75340393Sbostic 		    case NotParallel:
75440393Sbostic 		    {
75540393Sbostic 			extern int  maxJobs;
75640393Sbostic 
75740393Sbostic 			maxJobs = 1;
75840393Sbostic 			break;
75940393Sbostic 		    }
76040393Sbostic 		    case SingleShell:
76160285Sbostic 			compatMake = 1;
76240393Sbostic 			break;
76340393Sbostic 		    case Order:
76440393Sbostic 			predecessor = NILGNODE;
76540393Sbostic 			break;
76660285Sbostic 		    default:
76760285Sbostic 			break;
76840393Sbostic 		}
76940393Sbostic 	    } else if (strncmp (line, ".PATH", 5) == 0) {
77040393Sbostic 		/*
77140393Sbostic 		 * .PATH<suffix> has to be handled specially.
77240393Sbostic 		 * Call on the suffix module to give us a path to
77340393Sbostic 		 * modify.
77440393Sbostic 		 */
77540393Sbostic 		Lst 	path;
77640393Sbostic 
77760285Sbostic 		specType = ExPath;
77840393Sbostic 		path = Suff_GetPath (&line[5]);
77940393Sbostic 		if (path == NILLST) {
78040393Sbostic 		    Parse_Error (PARSE_FATAL,
78140393Sbostic 				 "Suffix '%s' not defined (yet)",
78240393Sbostic 				 &line[5]);
78340393Sbostic 		    return;
78440393Sbostic 		} else {
78540393Sbostic 		    if (paths == (Lst)NULL) {
78640393Sbostic 			paths = Lst_Init(FALSE);
78740393Sbostic 		    }
78840393Sbostic 		    (void)Lst_AtEnd(paths, (ClientData)path);
78940393Sbostic 		}
79040393Sbostic 	    }
79140393Sbostic 	}
79240393Sbostic 
79340393Sbostic 	/*
79440393Sbostic 	 * Have word in line. Get or create its node and stick it at
79540393Sbostic 	 * the end of the targets list
79640393Sbostic 	 */
79740393Sbostic 	if ((specType == Not) && (*line != '\0')) {
79840393Sbostic 	    if (Dir_HasWildcards(line)) {
79940393Sbostic 		/*
80040393Sbostic 		 * Targets are to be sought only in the current directory,
80140393Sbostic 		 * so create an empty path for the thing. Note we need to
80240393Sbostic 		 * use Dir_Destroy in the destruction of the path as the
80340393Sbostic 		 * Dir module could have added a directory to the path...
80440393Sbostic 		 */
80540393Sbostic 		Lst	    emptyPath = Lst_Init(FALSE);
80640393Sbostic 
80740393Sbostic 		Dir_Expand(line, emptyPath, curTargs);
80840393Sbostic 
80940393Sbostic 		Lst_Destroy(emptyPath, Dir_Destroy);
81040393Sbostic 	    } else {
81140393Sbostic 		/*
81240393Sbostic 		 * No wildcards, but we want to avoid code duplication,
81340393Sbostic 		 * so create a list with the word on it.
81440393Sbostic 		 */
81540393Sbostic 		(void)Lst_AtEnd(curTargs, (ClientData)line);
81640393Sbostic 	    }
81740393Sbostic 
81840393Sbostic 	    while(!Lst_IsEmpty(curTargs)) {
81940393Sbostic 		char	*targName = (char *)Lst_DeQueue(curTargs);
82040393Sbostic 
82140393Sbostic 		if (!Suff_IsTransform (targName)) {
82240393Sbostic 		    gn = Targ_FindNode (targName, TARG_CREATE);
82340393Sbostic 		} else {
82440393Sbostic 		    gn = Suff_AddTransform (targName);
82540393Sbostic 		}
82640393Sbostic 
82740393Sbostic 		(void)Lst_AtEnd (targets, (ClientData)gn);
82840393Sbostic 	    }
82960285Sbostic 	} else if (specType == ExPath && *line != '.' && *line != '\0') {
83040393Sbostic 	    Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
83140393Sbostic 	}
83240393Sbostic 
83340393Sbostic 	*cp = savec;
83440393Sbostic 	/*
83540393Sbostic 	 * If it is a special type and not .PATH, it's the only target we
83640393Sbostic 	 * allow on this line...
83740393Sbostic 	 */
83860285Sbostic 	if (specType != Not && specType != ExPath) {
83940393Sbostic 	    Boolean warn = FALSE;
84040393Sbostic 
84140393Sbostic 	    while ((*cp != '!') && (*cp != ':') && *cp) {
84240393Sbostic 		if (*cp != ' ' && *cp != '\t') {
84340393Sbostic 		    warn = TRUE;
84440393Sbostic 		}
84540393Sbostic 		cp++;
84640393Sbostic 	    }
84740393Sbostic 	    if (warn) {
84840393Sbostic 		Parse_Error(PARSE_WARNING, "Extra target ignored");
84940393Sbostic 	    }
85040393Sbostic 	} else {
85140393Sbostic 	    while (*cp && isspace (*cp)) {
85240393Sbostic 		cp++;
85340393Sbostic 	    }
85440393Sbostic 	}
85540393Sbostic 	line = cp;
85640393Sbostic     } while ((*line != '!') && (*line != ':') && *line);
85740393Sbostic 
85840393Sbostic     /*
85940393Sbostic      * Don't need the list of target names anymore...
86040393Sbostic      */
86140393Sbostic     Lst_Destroy(curTargs, NOFREE);
86240393Sbostic 
86340393Sbostic     if (!Lst_IsEmpty(targets)) {
86440393Sbostic 	switch(specType) {
86540393Sbostic 	    default:
86640393Sbostic 		Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
86740393Sbostic 		break;
86840393Sbostic 	    case Default:
86940393Sbostic 	    case Begin:
87040393Sbostic 	    case End:
87140393Sbostic 	    case Interrupt:
87240393Sbostic 		/*
87340393Sbostic 		 * These four create nodes on which to hang commands, so
87440393Sbostic 		 * targets shouldn't be empty...
87540393Sbostic 		 */
87640393Sbostic 	    case Not:
87740393Sbostic 		/*
87840393Sbostic 		 * Nothing special here -- targets can be empty if it wants.
87940393Sbostic 		 */
88040393Sbostic 		break;
88140393Sbostic 	}
88240393Sbostic     }
88340393Sbostic 
88440393Sbostic     /*
88540393Sbostic      * Have now parsed all the target names. Must parse the operator next. The
88640393Sbostic      * result is left in  op .
88740393Sbostic      */
88840393Sbostic     if (*cp == '!') {
88940393Sbostic 	op = OP_FORCE;
89040393Sbostic     } else if (*cp == ':') {
89140393Sbostic 	if (cp[1] == ':') {
89240393Sbostic 	    op = OP_DOUBLEDEP;
89340393Sbostic 	    cp++;
89440393Sbostic 	} else {
89540393Sbostic 	    op = OP_DEPENDS;
89640393Sbostic 	}
89740393Sbostic     } else {
89840393Sbostic 	Parse_Error (PARSE_FATAL, "Missing dependency operator");
89940393Sbostic 	return;
90040393Sbostic     }
90140393Sbostic 
90240393Sbostic     cp++;			/* Advance beyond operator */
90340393Sbostic 
90440393Sbostic     Lst_ForEach (targets, ParseDoOp, (ClientData)op);
90540393Sbostic 
90640393Sbostic     /*
90740393Sbostic      * Get to the first source
90840393Sbostic      */
90940393Sbostic     while (*cp && isspace (*cp)) {
91040393Sbostic 	cp++;
91140393Sbostic     }
91240393Sbostic     line = cp;
91340393Sbostic 
91440393Sbostic     /*
91540393Sbostic      * Several special targets take different actions if present with no
91640393Sbostic      * sources:
91740393Sbostic      *	a .SUFFIXES line with no sources clears out all old suffixes
91840393Sbostic      *	a .PRECIOUS line makes all targets precious
91940393Sbostic      *	a .IGNORE line ignores errors for all targets
92040393Sbostic      *	a .SILENT line creates silence when making all targets
92140393Sbostic      *	a .PATH removes all directories from the search path(s).
92240393Sbostic      */
92340393Sbostic     if (!*line) {
92440393Sbostic 	switch (specType) {
92540393Sbostic 	    case Suffixes:
92640393Sbostic 		Suff_ClearSuffixes ();
92740393Sbostic 		break;
92840393Sbostic 	    case Precious:
92940393Sbostic 		allPrecious = TRUE;
93040393Sbostic 		break;
93140393Sbostic 	    case Ignore:
93240393Sbostic 		ignoreErrors = TRUE;
93340393Sbostic 		break;
93440393Sbostic 	    case Silent:
93540393Sbostic 		beSilent = TRUE;
93640393Sbostic 		break;
93760285Sbostic 	    case ExPath:
93840393Sbostic 		Lst_ForEach(paths, ParseClearPath, (ClientData)NULL);
93940393Sbostic 		break;
94060285Sbostic 	    default:
94160285Sbostic 		break;
94240393Sbostic 	}
94340393Sbostic     } else if (specType == MFlags) {
94440393Sbostic 	/*
94540393Sbostic 	 * Call on functions in main.c to deal with these arguments and
94640393Sbostic 	 * set the initial character to a null-character so the loop to
94740393Sbostic 	 * get sources won't get anything
94840393Sbostic 	 */
94940393Sbostic 	Main_ParseArgLine (line);
95040393Sbostic 	*line = '\0';
95160285Sbostic     } else if (specType == ExShell) {
95240393Sbostic 	if (Job_ParseShell (line) != SUCCESS) {
95340393Sbostic 	    Parse_Error (PARSE_FATAL, "improper shell specification");
95440393Sbostic 	    return;
95540393Sbostic 	}
95640393Sbostic 	*line = '\0';
95740393Sbostic     } else if ((specType == NotParallel) || (specType == SingleShell)) {
95840393Sbostic 	*line = '\0';
95940393Sbostic     }
96040393Sbostic 
96140393Sbostic     /*
96240393Sbostic      * NOW GO FOR THE SOURCES
96340393Sbostic      */
96460285Sbostic     if ((specType == Suffixes) || (specType == ExPath) ||
96540393Sbostic 	(specType == Includes) || (specType == Libs) ||
96640439Sbostic 	(specType == Null))
96740393Sbostic     {
96840393Sbostic 	while (*line) {
96940393Sbostic 	    /*
97040393Sbostic 	     * If the target was one that doesn't take files as its sources
97140393Sbostic 	     * but takes something like suffixes, we take each
97240393Sbostic 	     * space-separated word on the line as a something and deal
97340393Sbostic 	     * with it accordingly.
97440393Sbostic 	     *
97540393Sbostic 	     * If the target was .SUFFIXES, we take each source as a
97640393Sbostic 	     * suffix and add it to the list of suffixes maintained by the
97740393Sbostic 	     * Suff module.
97840393Sbostic 	     *
97940393Sbostic 	     * If the target was a .PATH, we add the source as a directory
98040393Sbostic 	     * to search on the search path.
98140393Sbostic 	     *
98240393Sbostic 	     * If it was .INCLUDES, the source is taken to be the suffix of
98340393Sbostic 	     * files which will be #included and whose search path should
98440393Sbostic 	     * be present in the .INCLUDES variable.
98540393Sbostic 	     *
98640393Sbostic 	     * If it was .LIBS, the source is taken to be the suffix of
98740393Sbostic 	     * files which are considered libraries and whose search path
98840393Sbostic 	     * should be present in the .LIBS variable.
98940393Sbostic 	     *
99040393Sbostic 	     * If it was .NULL, the source is the suffix to use when a file
99140393Sbostic 	     * has no valid suffix.
99240393Sbostic 	     */
99340393Sbostic 	    char  savec;
99440393Sbostic 	    while (*cp && !isspace (*cp)) {
99540393Sbostic 		cp++;
99640393Sbostic 	    }
99740393Sbostic 	    savec = *cp;
99840393Sbostic 	    *cp = '\0';
99940393Sbostic 	    switch (specType) {
100040393Sbostic 		case Suffixes:
100140393Sbostic 		    Suff_AddSuffix (line);
100240393Sbostic 		    break;
100360285Sbostic 		case ExPath:
100440393Sbostic 		    Lst_ForEach(paths, ParseAddDir, (ClientData)line);
100540393Sbostic 		    break;
100640393Sbostic 		case Includes:
100740393Sbostic 		    Suff_AddInclude (line);
100840393Sbostic 		    break;
100940393Sbostic 		case Libs:
101040393Sbostic 		    Suff_AddLib (line);
101140393Sbostic 		    break;
101240393Sbostic 		case Null:
101340393Sbostic 		    Suff_SetNull (line);
101440393Sbostic 		    break;
101560285Sbostic 		default:
101660285Sbostic 		    break;
101740393Sbostic 	    }
101840393Sbostic 	    *cp = savec;
101940393Sbostic 	    if (savec != '\0') {
102040393Sbostic 		cp++;
102140393Sbostic 	    }
102240393Sbostic 	    while (*cp && isspace (*cp)) {
102340393Sbostic 		cp++;
102440393Sbostic 	    }
102540393Sbostic 	    line = cp;
102640393Sbostic 	}
102740393Sbostic 	if (paths) {
102840393Sbostic 	    Lst_Destroy(paths, NOFREE);
102940393Sbostic 	}
103040393Sbostic     } else {
103140393Sbostic 	while (*line) {
103240393Sbostic 	    /*
103340393Sbostic 	     * The targets take real sources, so we must beware of archive
103440393Sbostic 	     * specifications (i.e. things with left parentheses in them)
103540393Sbostic 	     * and handle them accordingly.
103640393Sbostic 	     */
103740393Sbostic 	    while (*cp && !isspace (*cp)) {
103840393Sbostic 		if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) {
103940393Sbostic 		    /*
104040393Sbostic 		     * Only stop for a left parenthesis if it isn't at the
104140393Sbostic 		     * start of a word (that'll be for variable changes
104240393Sbostic 		     * later) and isn't preceded by a dollar sign (a dynamic
104340393Sbostic 		     * source).
104440393Sbostic 		     */
104540393Sbostic 		    break;
104640393Sbostic 		} else {
104740393Sbostic 		    cp++;
104840393Sbostic 		}
104940393Sbostic 	    }
105040393Sbostic 
105140393Sbostic 	    if (*cp == '(') {
105240393Sbostic 		GNode	  *gn;
105340393Sbostic 
105440393Sbostic 		sources = Lst_Init (FALSE);
105540393Sbostic 		if (Arch_ParseArchive (&line, sources, VAR_CMD) != SUCCESS) {
105640393Sbostic 		    Parse_Error (PARSE_FATAL,
105740393Sbostic 				 "Error in source archive spec \"%s\"", line);
105840393Sbostic 		    return;
105940393Sbostic 		}
106040393Sbostic 
106140393Sbostic 		while (!Lst_IsEmpty (sources)) {
106240393Sbostic 		    gn = (GNode *) Lst_DeQueue (sources);
106340393Sbostic 		    ParseDoSrc (tOp, gn->name);
106440393Sbostic 		}
106540393Sbostic 		Lst_Destroy (sources, NOFREE);
106640393Sbostic 		cp = line;
106740393Sbostic 	    } else {
106840393Sbostic 		if (*cp) {
106940393Sbostic 		    *cp = '\0';
107040393Sbostic 		    cp += 1;
107140393Sbostic 		}
107240393Sbostic 
107340393Sbostic 		ParseDoSrc (tOp, line);
107440393Sbostic 	    }
107540393Sbostic 	    while (*cp && isspace (*cp)) {
107640393Sbostic 		cp++;
107740393Sbostic 	    }
107840393Sbostic 	    line = cp;
107940393Sbostic 	}
108040393Sbostic     }
108140393Sbostic 
108240393Sbostic     if (mainNode == NILGNODE) {
108340393Sbostic 	/*
108440393Sbostic 	 * If we have yet to decide on a main target to make, in the
108540393Sbostic 	 * absence of any user input, we want the first target on
108640393Sbostic 	 * the first dependency line that is actually a real target
108740393Sbostic 	 * (i.e. isn't a .USE or .EXEC rule) to be made.
108840393Sbostic 	 */
108940393Sbostic 	Lst_ForEach (targets, ParseFindMain, (ClientData)0);
109040393Sbostic     }
109140393Sbostic 
109240393Sbostic }
109340424Sbostic 
109440393Sbostic /*-
109540393Sbostic  *---------------------------------------------------------------------
109640393Sbostic  * Parse_IsVar  --
109740393Sbostic  *	Return TRUE if the passed line is a variable assignment. A variable
109840393Sbostic  *	assignment consists of a single word followed by optional whitespace
109940393Sbostic  *	followed by either a += or an = operator.
110040393Sbostic  *	This function is used both by the Parse_File function and main when
110140393Sbostic  *	parsing the command-line arguments.
110240393Sbostic  *
110340393Sbostic  * Results:
110440393Sbostic  *	TRUE if it is. FALSE if it ain't
110540393Sbostic  *
110640393Sbostic  * Side Effects:
110740393Sbostic  *	none
110840393Sbostic  *---------------------------------------------------------------------
110940393Sbostic  */
111040393Sbostic Boolean
111140393Sbostic Parse_IsVar (line)
111240393Sbostic     register char  *line;	/* the line to check */
111340393Sbostic {
111440393Sbostic     register Boolean wasSpace = FALSE;	/* set TRUE if found a space */
111540393Sbostic     register Boolean haveName = FALSE;	/* Set TRUE if have a variable name */
111640393Sbostic 
111740393Sbostic     /*
111840393Sbostic      * Skip to variable name
111940393Sbostic      */
112040393Sbostic     while ((*line == ' ') || (*line == '\t')) {
112140393Sbostic 	line++;
112240393Sbostic     }
112340393Sbostic 
112440393Sbostic     while (*line != '=') {
112540393Sbostic 	if (*line == '\0') {
112640393Sbostic 	    /*
112740393Sbostic 	     * end-of-line -- can't be a variable assignment.
112840393Sbostic 	     */
112940393Sbostic 	    return (FALSE);
113040393Sbostic 	} else if ((*line == ' ') || (*line == '\t')) {
113140393Sbostic 	    /*
113240393Sbostic 	     * there can be as much white space as desired so long as there is
113340393Sbostic 	     * only one word before the operator
113440393Sbostic 	     */
113540393Sbostic 	    wasSpace = TRUE;
113640393Sbostic 	} else if (wasSpace && haveName) {
113740393Sbostic 	    /*
113840393Sbostic 	     * Stop when an = operator is found.
113940393Sbostic 	     */
114040393Sbostic 	    if ((*line == '+') || (*line == ':') || (*line == '?') ||
114140393Sbostic 		(*line == '!')) {
114240393Sbostic 		break;
114340393Sbostic 	    }
114440393Sbostic 
114540393Sbostic 	    /*
114640393Sbostic 	     * This is the start of another word, so not assignment.
114740393Sbostic 	     */
114840393Sbostic 	    return (FALSE);
114940393Sbostic 	} else {
115040393Sbostic 	    haveName = TRUE;
115140393Sbostic 	    wasSpace = FALSE;
115240393Sbostic 	}
115340393Sbostic 	line++;
115440393Sbostic     }
115540393Sbostic 
115640393Sbostic     /*
115740393Sbostic      * A final check: if we stopped on a +, ?, ! or :, the next character must
115840393Sbostic      * be an = or it ain't a valid assignment
115940393Sbostic      */
116040393Sbostic     if (((*line == '+') ||
116140393Sbostic 	 (*line == '?') ||
116240393Sbostic 	 (*line == ':') ||
116340393Sbostic 	 (*line == '!')) &&
116440393Sbostic 	(line[1] != '='))
116540393Sbostic     {
116640393Sbostic 	return (FALSE);
116740393Sbostic     } else {
116840393Sbostic 	return (haveName);
116940393Sbostic     }
117040393Sbostic }
117140424Sbostic 
117240393Sbostic /*-
117340393Sbostic  *---------------------------------------------------------------------
117440393Sbostic  * Parse_DoVar  --
117540393Sbostic  *	Take the variable assignment in the passed line and do it in the
117640393Sbostic  *	global context.
117740393Sbostic  *
117840393Sbostic  *	Note: There is a lexical ambiguity with assignment modifier characters
117940393Sbostic  *	in variable names. This routine interprets the character before the =
118040393Sbostic  *	as a modifier. Therefore, an assignment like
118140393Sbostic  *	    C++=/usr/bin/CC
118240393Sbostic  *	is interpreted as "C+ +=" instead of "C++ =".
118340393Sbostic  *
118440393Sbostic  * Results:
118540393Sbostic  *	none
118640393Sbostic  *
118740393Sbostic  * Side Effects:
118840393Sbostic  *	the variable structure of the given variable name is altered in the
118940393Sbostic  *	global context.
119040393Sbostic  *---------------------------------------------------------------------
119140393Sbostic  */
119240393Sbostic void
119340393Sbostic Parse_DoVar (line, ctxt)
119440393Sbostic     char            *line;	/* a line guaranteed to be a variable
119540393Sbostic 				 * assignment. This reduces error checks */
119640393Sbostic     GNode   	    *ctxt;    	/* Context in which to do the assignment */
119740393Sbostic {
119840393Sbostic     register char   *cp;	/* pointer into line */
119940393Sbostic     enum {
120040393Sbostic 	VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
120140393Sbostic     }	    	    type;   	/* Type of assignment */
120240393Sbostic     char            *opc;	/* ptr to operator character to
120340393Sbostic 				 * null-terminate the variable name */
120440393Sbostic 
120540393Sbostic     /*
120640393Sbostic      * Skip to variable name
120740393Sbostic      */
120840393Sbostic     while ((*line == ' ') || (*line == '\t')) {
120940393Sbostic 	line++;
121040393Sbostic     }
121140393Sbostic 
121240393Sbostic     /*
121340393Sbostic      * Skip to operator character, nulling out whitespace as we go
121440393Sbostic      */
121540393Sbostic     for (cp = line + 1; *cp != '='; cp++) {
121640393Sbostic 	if (isspace (*cp)) {
121740393Sbostic 	    *cp = '\0';
121840393Sbostic 	}
121940393Sbostic     }
122040393Sbostic     opc = cp-1;		/* operator is the previous character */
122140393Sbostic     *cp++ = '\0';	/* nuke the = */
122240393Sbostic 
122340393Sbostic     /*
122440393Sbostic      * Check operator type
122540393Sbostic      */
122640393Sbostic     switch (*opc) {
122740393Sbostic 	case '+':
122840393Sbostic 	    type = VAR_APPEND;
122940393Sbostic 	    *opc = '\0';
123040393Sbostic 	    break;
123140393Sbostic 
123240393Sbostic 	case '?':
123340393Sbostic 	    /*
123440393Sbostic 	     * If the variable already has a value, we don't do anything.
123540393Sbostic 	     */
123640393Sbostic 	    *opc = '\0';
123740393Sbostic 	    if (Var_Exists(line, ctxt)) {
123840393Sbostic 		return;
123940393Sbostic 	    } else {
124040393Sbostic 		type = VAR_NORMAL;
124140393Sbostic 	    }
124240393Sbostic 	    break;
124340393Sbostic 
124440393Sbostic 	case ':':
124540393Sbostic 	    type = VAR_SUBST;
124640393Sbostic 	    *opc = '\0';
124740393Sbostic 	    break;
124840393Sbostic 
124940393Sbostic 	case '!':
125040393Sbostic 	    type = VAR_SHELL;
125140393Sbostic 	    *opc = '\0';
125240393Sbostic 	    break;
125340393Sbostic 
125440393Sbostic 	default:
125540393Sbostic 	    type = VAR_NORMAL;
125640393Sbostic 	    break;
125740393Sbostic     }
125840393Sbostic 
125940393Sbostic     while (isspace (*cp)) {
126040393Sbostic 	cp++;
126140393Sbostic     }
126240393Sbostic 
126340393Sbostic     if (type == VAR_APPEND) {
126440393Sbostic 	Var_Append (line, cp, ctxt);
126540393Sbostic     } else if (type == VAR_SUBST) {
126640393Sbostic 	/*
126740393Sbostic 	 * Allow variables in the old value to be undefined, but leave their
126840393Sbostic 	 * invocation alone -- this is done by forcing oldVars to be false.
126940393Sbostic 	 * XXX: This can cause recursive variables, but that's not hard to do,
127040393Sbostic 	 * and this allows someone to do something like
127140393Sbostic 	 *
127240393Sbostic 	 *  CFLAGS = $(.INCLUDES)
127340393Sbostic 	 *  CFLAGS := -I.. $(CFLAGS)
127440393Sbostic 	 *
127540393Sbostic 	 * And not get an error.
127640393Sbostic 	 */
127740393Sbostic 	Boolean	  oldOldVars = oldVars;
127840393Sbostic 
127940393Sbostic 	oldVars = FALSE;
128060285Sbostic 	cp = Var_Subst(NULL, cp, ctxt, FALSE);
128140393Sbostic 	oldVars = oldOldVars;
128240393Sbostic 
128340393Sbostic 	Var_Set(line, cp, ctxt);
128440393Sbostic 	free(cp);
128540393Sbostic     } else if (type == VAR_SHELL) {
128640393Sbostic 	char	*args[4];   	/* Args for invoking the shell */
128740393Sbostic 	int 	fds[2];	    	/* Pipe streams */
128840393Sbostic 	int 	cpid;	    	/* Child PID */
128940393Sbostic 	int 	pid;	    	/* PID from wait() */
129040393Sbostic 	Boolean	freeCmd;    	/* TRUE if the command needs to be freed, i.e.
129140393Sbostic 				 * if any variable expansion was performed */
129240393Sbostic 
129360285Sbostic 
129440393Sbostic 	/*
129540393Sbostic 	 * Set up arguments for shell
129640393Sbostic 	 */
129740393Sbostic 	args[0] = "sh";
129840393Sbostic 	args[1] = "-c";
129960285Sbostic 	if (strchr(cp, '$') != (char *)NULL) {
130040393Sbostic 	    /*
130140393Sbostic 	     * There's a dollar sign in the command, so perform variable
130240393Sbostic 	     * expansion on the whole thing. The resulting string will need
130340393Sbostic 	     * freeing when we're done, so set freeCmd to TRUE.
130440393Sbostic 	     */
130560285Sbostic 	    args[2] = Var_Subst(NULL, cp, VAR_CMD, TRUE);
130640393Sbostic 	    freeCmd = TRUE;
130740393Sbostic 	} else {
130840393Sbostic 	    args[2] = cp;
130940393Sbostic 	    freeCmd = FALSE;
131040393Sbostic 	}
131140393Sbostic 	args[3] = (char *)NULL;
131240393Sbostic 
131340393Sbostic 	/*
131440393Sbostic 	 * Open a pipe for fetching its output
131540393Sbostic 	 */
131640393Sbostic 	pipe(fds);
131740393Sbostic 
131840393Sbostic 	/*
131940393Sbostic 	 * Fork
132040393Sbostic 	 */
132140393Sbostic 	cpid = vfork();
132240393Sbostic 	if (cpid == 0) {
132340393Sbostic 	    /*
132440393Sbostic 	     * Close input side of pipe
132540393Sbostic 	     */
132640393Sbostic 	    close(fds[0]);
132740393Sbostic 
132840393Sbostic 	    /*
132940393Sbostic 	     * Duplicate the output stream to the shell's output, then
133040393Sbostic 	     * shut the extra thing down. Note we don't fetch the error
133140393Sbostic 	     * stream...why not? Why?
133240393Sbostic 	     */
133340393Sbostic 	    dup2(fds[1], 1);
133440393Sbostic 	    close(fds[1]);
133540393Sbostic 
133640393Sbostic 	    execv("/bin/sh", args);
133740393Sbostic 	    _exit(1);
133840393Sbostic 	} else if (cpid < 0) {
133940393Sbostic 	    /*
134040393Sbostic 	     * Couldn't fork -- tell the user and make the variable null
134140393Sbostic 	     */
134240393Sbostic 	    Parse_Error(PARSE_WARNING, "Couldn't exec \"%s\"", cp);
134340393Sbostic 	    Var_Set(line, "", ctxt);
134440393Sbostic 	} else {
134540393Sbostic 	    int	status;
134640393Sbostic 	    int cc;
134760285Sbostic 	    Buffer buf;
134860285Sbostic 	    char *res;
134940393Sbostic 
135040393Sbostic 	    /*
135140393Sbostic 	     * No need for the writing half
135240393Sbostic 	     */
135340393Sbostic 	    close(fds[1]);
135440393Sbostic 
135560285Sbostic 	    buf = Buf_Init (MAKE_BSIZE);
135660285Sbostic 
135760285Sbostic 	    do {
135860285Sbostic 		char   result[BUFSIZ];
135960285Sbostic 		cc = read(fds[0], result, sizeof(result));
136060285Sbostic 		if (cc > 0)
136160285Sbostic 		    Buf_AddBytes(buf, cc, (unsigned char *) result);
136260285Sbostic 	    }
136360285Sbostic 	    while (cc > 0 || (cc == -1 && errno == EINTR));
136460285Sbostic 
136540393Sbostic 	    /*
136660285Sbostic 	     * Close the input side of the pipe.
136740393Sbostic 	     */
136860285Sbostic 	    close(fds[0]);
136940393Sbostic 
137040393Sbostic 	    /*
137160285Sbostic 	     * Wait for the process to exit.
137240393Sbostic 	     */
137360285Sbostic 	    while(((pid = wait(&status)) != cpid) && (pid >= 0))
137460285Sbostic 		continue;
137540393Sbostic 
137660285Sbostic 	    res = (char *)Buf_GetAll (buf, &cc);
137760285Sbostic 	    Buf_Destroy (buf, FALSE);
137860285Sbostic 
137960285Sbostic 	    if (cc == 0) {
138040393Sbostic 		/*
138140393Sbostic 		 * Couldn't read the child's output -- tell the user and
138240393Sbostic 		 * set the variable to null
138340393Sbostic 		 */
138440393Sbostic 		Parse_Error(PARSE_WARNING, "Couldn't read shell's output");
138540393Sbostic 	    }
138640393Sbostic 
138740393Sbostic 	    if (status) {
138840393Sbostic 		/*
138940393Sbostic 		 * Child returned an error -- tell the user but still use
139040393Sbostic 		 * the result.
139140393Sbostic 		 */
139240393Sbostic 		Parse_Error(PARSE_WARNING, "\"%s\" returned non-zero", cp);
139340393Sbostic 	    }
139460285Sbostic 
139540393Sbostic 	    /*
139640393Sbostic 	     * Null-terminate the result, convert newlines to spaces and
139740393Sbostic 	     * install it in the variable.
139840393Sbostic 	     */
139960285Sbostic 	    res[cc] = '\0';
140060285Sbostic 	    cp = &res[cc] - 1;
140140393Sbostic 
140240393Sbostic 	    if (*cp == '\n') {
140340393Sbostic 		/*
140440393Sbostic 		 * A final newline is just stripped
140540393Sbostic 		 */
140640393Sbostic 		*cp-- = '\0';
140740393Sbostic 	    }
140860285Sbostic 	    while (cp >= res) {
140940393Sbostic 		if (*cp == '\n') {
141040393Sbostic 		    *cp = ' ';
141140393Sbostic 		}
141240393Sbostic 		cp--;
141340393Sbostic 	    }
141460285Sbostic 	    Var_Set(line, res, ctxt);
141560285Sbostic 	    free(res);
141640393Sbostic 
141740393Sbostic 	}
141840393Sbostic 	if (freeCmd) {
141940393Sbostic 	    free(args[2]);
142040393Sbostic 	}
142140393Sbostic     } else {
142240393Sbostic 	/*
142340393Sbostic 	 * Normal assignment -- just do it.
142440393Sbostic 	 */
142540393Sbostic 	Var_Set (line, cp, ctxt);
142640393Sbostic     }
142740393Sbostic }
142840424Sbostic 
142940393Sbostic /*-
143040393Sbostic  * ParseAddCmd  --
143140393Sbostic  *	Lst_ForEach function to add a command line to all targets
143240393Sbostic  *
143340393Sbostic  * Results:
143440393Sbostic  *	Always 0
143540393Sbostic  *
143640393Sbostic  * Side Effects:
143740393Sbostic  *	A new element is added to the commands list of the node.
143840393Sbostic  */
143960285Sbostic static int
144040476Sbostic ParseAddCmd(gn, cmd)
144140476Sbostic 	GNode *gn;	/* the node to which the command is to be added */
144240476Sbostic 	char *cmd;	/* the command to add */
144340393Sbostic {
144440476Sbostic 	/* if target already supplied, ignore commands */
144540476Sbostic 	if (!(gn->type & OP_HAS_COMMANDS))
144640476Sbostic 		(void)Lst_AtEnd(gn->commands, (ClientData)cmd);
144740476Sbostic 	return(0);
144840393Sbostic }
144940424Sbostic 
145040393Sbostic /*-
145140393Sbostic  *-----------------------------------------------------------------------
145240393Sbostic  * ParseHasCommands --
145340393Sbostic  *	Callback procedure for Parse_File when destroying the list of
145440393Sbostic  *	targets on the last dependency line. Marks a target as already
145540393Sbostic  *	having commands if it does, to keep from having shell commands
145640393Sbostic  *	on multiple dependency lines.
145740393Sbostic  *
145840393Sbostic  * Results:
145940393Sbostic  *	Always 0.
146040393Sbostic  *
146140393Sbostic  * Side Effects:
146240393Sbostic  *	OP_HAS_COMMANDS may be set for the target.
146340393Sbostic  *
146440393Sbostic  *-----------------------------------------------------------------------
146540393Sbostic  */
146640393Sbostic static int
146740393Sbostic ParseHasCommands(gn)
146840393Sbostic     GNode   	  *gn;	    /* Node to examine */
146940393Sbostic {
147040393Sbostic     if (!Lst_IsEmpty(gn->commands)) {
147140393Sbostic 	gn->type |= OP_HAS_COMMANDS;
147240393Sbostic     }
147340393Sbostic     return(0);
147440393Sbostic }
147540424Sbostic 
147640393Sbostic /*-
147740393Sbostic  *-----------------------------------------------------------------------
147840393Sbostic  * Parse_AddIncludeDir --
147940393Sbostic  *	Add a directory to the path searched for included makefiles
148040393Sbostic  *	bracketed by double-quotes. Used by functions in main.c
148140393Sbostic  *
148240393Sbostic  * Results:
148340393Sbostic  *	None.
148440393Sbostic  *
148540393Sbostic  * Side Effects:
148640393Sbostic  *	The directory is appended to the list.
148740393Sbostic  *
148840393Sbostic  *-----------------------------------------------------------------------
148940393Sbostic  */
149040393Sbostic void
149140393Sbostic Parse_AddIncludeDir (dir)
149240393Sbostic     char    	  *dir;	    /* The name of the directory to add */
149340393Sbostic {
149440393Sbostic     Dir_AddDir (parseIncPath, dir);
149540393Sbostic }
149640424Sbostic 
149740393Sbostic /*-
149840393Sbostic  *---------------------------------------------------------------------
149940393Sbostic  * ParseDoInclude  --
150040393Sbostic  *	Push to another file.
150140393Sbostic  *
150240393Sbostic  *	The input is the line minus the #include. A file spec is a string
150340393Sbostic  *	enclosed in <> or "". The former is looked for only in sysIncPath.
150440393Sbostic  *	The latter in . and the directories specified by -I command line
150540393Sbostic  *	options
150640393Sbostic  *
150740393Sbostic  * Results:
150840393Sbostic  *	None
150940393Sbostic  *
151040393Sbostic  * Side Effects:
151140393Sbostic  *	A structure is added to the includes Lst and readProc, lineno,
151240393Sbostic  *	fname and curFILE are altered for the new file
151340393Sbostic  *---------------------------------------------------------------------
151440393Sbostic  */
151540393Sbostic static void
151640393Sbostic ParseDoInclude (file)
151740393Sbostic     char          *file;	/* file specification */
151840393Sbostic {
151940393Sbostic     char          *fullname;	/* full pathname of file */
152040393Sbostic     IFile         *oldFile;	/* state associated with current file */
152140393Sbostic     char          endc;	    	/* the character which ends the file spec */
152240393Sbostic     char          *cp;		/* current position in file spec */
152340393Sbostic     Boolean 	  isSystem; 	/* TRUE if makefile is a system makefile */
152440393Sbostic 
152540393Sbostic     /*
152640393Sbostic      * Skip to delimiter character so we know where to look
152740393Sbostic      */
152840393Sbostic     while ((*file == ' ') || (*file == '\t')) {
152940393Sbostic 	file++;
153040393Sbostic     }
153140393Sbostic 
153240393Sbostic     if ((*file != '"') && (*file != '<')) {
153342443Sbostic 	Parse_Error (PARSE_FATAL,
153442443Sbostic 	    ".include filename must be delimited by '\"' or '<'");
153540393Sbostic 	return;
153640393Sbostic     }
153740393Sbostic 
153840393Sbostic     /*
153940393Sbostic      * Set the search path on which to find the include file based on the
154040393Sbostic      * characters which bracket its name. Angle-brackets imply it's
154140393Sbostic      * a system Makefile while double-quotes imply it's a user makefile
154240393Sbostic      */
154340393Sbostic     if (*file == '<') {
154440393Sbostic 	isSystem = TRUE;
154540393Sbostic 	endc = '>';
154640393Sbostic     } else {
154740393Sbostic 	isSystem = FALSE;
154840393Sbostic 	endc = '"';
154940393Sbostic     }
155040393Sbostic 
155140393Sbostic     /*
155240393Sbostic      * Skip to matching delimiter
155340393Sbostic      */
155440393Sbostic     for (cp = ++file; *cp && *cp != endc; cp++) {
155540393Sbostic 	continue;
155640393Sbostic     }
155740393Sbostic 
155840393Sbostic     if (*cp != endc) {
155940393Sbostic 	Parse_Error (PARSE_FATAL,
156060285Sbostic 		     "Unclosed %cinclude filename. '%c' expected",
156160285Sbostic 		     '.', endc);
156240393Sbostic 	return;
156340393Sbostic     }
156440393Sbostic     *cp = '\0';
156540393Sbostic 
156640393Sbostic     /*
156740393Sbostic      * Substitute for any variables in the file name before trying to
156840393Sbostic      * find the thing.
156940393Sbostic      */
157060285Sbostic     file = Var_Subst (NULL, file, VAR_CMD, FALSE);
157140393Sbostic 
157240393Sbostic     /*
157340393Sbostic      * Now we know the file's name and its search path, we attempt to
157440393Sbostic      * find the durn thing. A return of NULL indicates the file don't
157540393Sbostic      * exist.
157640393Sbostic      */
157740393Sbostic     if (!isSystem) {
157840393Sbostic 	/*
157940393Sbostic 	 * Include files contained in double-quotes are first searched for
158040393Sbostic 	 * relative to the including file's location. We don't want to
158140393Sbostic 	 * cd there, of course, so we just tack on the old file's
158240393Sbostic 	 * leading path components and call Dir_FindFile to see if
158340393Sbostic 	 * we can locate the beast.
158440393Sbostic 	 */
158540393Sbostic 	char	  *prefEnd;
158640393Sbostic 
158760285Sbostic 	prefEnd = strrchr (fname, '/');
158840393Sbostic 	if (prefEnd != (char *)NULL) {
158940393Sbostic 	    char  	*newName;
159040393Sbostic 
159140393Sbostic 	    *prefEnd = '\0';
159240534Sbostic 	    newName = str_concat (fname, file, STR_ADDSLASH);
159340393Sbostic 	    fullname = Dir_FindFile (newName, parseIncPath);
159440393Sbostic 	    if (fullname == (char *)NULL) {
159540393Sbostic 		fullname = Dir_FindFile(newName, dirSearchPath);
159640393Sbostic 	    }
159740393Sbostic 	    free (newName);
159840393Sbostic 	    *prefEnd = '/';
159940393Sbostic 	} else {
160040393Sbostic 	    fullname = (char *)NULL;
160140393Sbostic 	}
160240393Sbostic     } else {
160340393Sbostic 	fullname = (char *)NULL;
160440393Sbostic     }
160540393Sbostic 
160640393Sbostic     if (fullname == (char *)NULL) {
160740393Sbostic 	/*
160840393Sbostic 	 * System makefile or makefile wasn't found in same directory as
160940393Sbostic 	 * included makefile. Search for it first on the -I search path,
161040393Sbostic 	 * then on the .PATH search path, if not found in a -I directory.
161140393Sbostic 	 * XXX: Suffix specific?
161240393Sbostic 	 */
161340393Sbostic 	fullname = Dir_FindFile (file, parseIncPath);
161440393Sbostic 	if (fullname == (char *)NULL) {
161540393Sbostic 	    fullname = Dir_FindFile(file, dirSearchPath);
161640393Sbostic 	}
161740393Sbostic     }
161840393Sbostic 
161940393Sbostic     if (fullname == (char *)NULL) {
162040393Sbostic 	/*
162140393Sbostic 	 * Still haven't found the makefile. Look for it on the system
162240393Sbostic 	 * path as a last resort.
162340393Sbostic 	 */
162440393Sbostic 	fullname = Dir_FindFile(file, sysIncPath);
162540393Sbostic     }
162640393Sbostic 
162740393Sbostic     if (fullname == (char *) NULL) {
162840393Sbostic 	*cp = endc;
162940393Sbostic 	Parse_Error (PARSE_FATAL, "Could not find %s", file);
163040393Sbostic 	return;
163140393Sbostic     }
163240393Sbostic 
163340393Sbostic     /*
163440393Sbostic      * Once we find the absolute path to the file, we get to save all the
163540393Sbostic      * state from the current file before we can start reading this
163640393Sbostic      * include file. The state is stored in an IFile structure which
163740393Sbostic      * is placed on a list with other IFile structures. The list makes
163840393Sbostic      * a very nice stack to track how we got here...
163940393Sbostic      */
164040534Sbostic     oldFile = (IFile *) emalloc (sizeof (IFile));
164140393Sbostic     oldFile->fname = fname;
164240393Sbostic 
164340393Sbostic     oldFile->F = curFILE;
164460285Sbostic     oldFile->p = curPTR;
164540393Sbostic     oldFile->lineno = lineno;
164640393Sbostic 
164740393Sbostic     (void) Lst_AtFront (includes, (ClientData)oldFile);
164840393Sbostic 
164940393Sbostic     /*
165040393Sbostic      * Once the previous state has been saved, we can get down to reading
165140393Sbostic      * the new file. We set up the name of the file to be the absolute
165240393Sbostic      * name of the include file so error messages refer to the right
165340393Sbostic      * place. Naturally enough, we start reading at line number 0.
165440393Sbostic      */
165540393Sbostic     fname = fullname;
165640393Sbostic     lineno = 0;
165740393Sbostic 
165840393Sbostic     curFILE = fopen (fullname, "r");
165960285Sbostic     curPTR = NULL;
166040393Sbostic     if (curFILE == (FILE * ) NULL) {
166140393Sbostic 	Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
166240393Sbostic 	/*
166340393Sbostic 	 * Pop to previous file
166440393Sbostic 	 */
166546462Sbostic 	(void) ParseEOF(0);
166640393Sbostic     }
166740393Sbostic }
166840424Sbostic 
166960285Sbostic 
167040393Sbostic /*-
167140393Sbostic  *---------------------------------------------------------------------
167260285Sbostic  * Parse_FromString  --
167360285Sbostic  *	Start Parsing from the given string
167460285Sbostic  *
167560285Sbostic  * Results:
167660285Sbostic  *	None
167760285Sbostic  *
167860285Sbostic  * Side Effects:
167960285Sbostic  *	A structure is added to the includes Lst and readProc, lineno,
168060285Sbostic  *	fname and curFILE are altered for the new file
168160285Sbostic  *---------------------------------------------------------------------
168260285Sbostic  */
168360285Sbostic void
168460285Sbostic Parse_FromString(str)
168560285Sbostic     char *str;
168660285Sbostic {
168760285Sbostic     IFile         *oldFile;	/* state associated with this file */
168860285Sbostic 
168960285Sbostic     if (DEBUG(FOR))
169060285Sbostic 	(void) fprintf(stderr, "%s\n----\n", str);
169160285Sbostic 
169260285Sbostic     oldFile = (IFile *) emalloc (sizeof (IFile));
169360285Sbostic     oldFile->lineno = lineno;
169460285Sbostic     oldFile->fname = fname;
169560285Sbostic     oldFile->F = curFILE;
169660285Sbostic     oldFile->p = curPTR;
169760285Sbostic 
169860285Sbostic     (void) Lst_AtFront (includes, (ClientData)oldFile);
169960285Sbostic 
170060285Sbostic     curFILE = NULL;
170160285Sbostic     curPTR = (PTR *) emalloc (sizeof (PTR));
170260285Sbostic     curPTR->str = curPTR->ptr = str;
170360285Sbostic     lineno = 0;
170460285Sbostic     fname = strdup(fname);
170560285Sbostic }
170660285Sbostic 
170760285Sbostic 
170860285Sbostic #ifdef SYSVINCLUDE
170960285Sbostic /*-
171060285Sbostic  *---------------------------------------------------------------------
171160285Sbostic  * ParseTraditionalInclude  --
171260285Sbostic  *	Push to another file.
171360285Sbostic  *
171460285Sbostic  *	The input is the line minus the "include".  The file name is
171560285Sbostic  *	the string following the "include".
171660285Sbostic  *
171760285Sbostic  * Results:
171860285Sbostic  *	None
171960285Sbostic  *
172060285Sbostic  * Side Effects:
172160285Sbostic  *	A structure is added to the includes Lst and readProc, lineno,
172260285Sbostic  *	fname and curFILE are altered for the new file
172360285Sbostic  *---------------------------------------------------------------------
172460285Sbostic  */
172560285Sbostic static void
172660285Sbostic ParseTraditionalInclude (file)
172760285Sbostic     char          *file;	/* file specification */
172860285Sbostic {
172960285Sbostic     char          *fullname;	/* full pathname of file */
173060285Sbostic     IFile         *oldFile;	/* state associated with current file */
173160285Sbostic     char          *cp;		/* current position in file spec */
173260285Sbostic     char	  *prefEnd;
173360285Sbostic 
173460285Sbostic     /*
173560285Sbostic      * Skip over whitespace
173660285Sbostic      */
173760285Sbostic     while ((*file == ' ') || (*file == '\t')) {
173860285Sbostic 	file++;
173960285Sbostic     }
174060285Sbostic 
174160285Sbostic     if (*file == '\0') {
174260285Sbostic 	Parse_Error (PARSE_FATAL,
174360285Sbostic 		     "Filename missing from \"include\"");
174460285Sbostic 	return;
174560285Sbostic     }
174660285Sbostic 
174760285Sbostic     /*
174860285Sbostic      * Skip to end of line or next whitespace
174960285Sbostic      */
175060285Sbostic     for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
175160285Sbostic 	continue;
175260285Sbostic     }
175360285Sbostic 
175460285Sbostic     *cp = '\0';
175560285Sbostic 
175660285Sbostic     /*
175760285Sbostic      * Substitute for any variables in the file name before trying to
175860285Sbostic      * find the thing.
175960285Sbostic      */
176060285Sbostic     file = Var_Subst (NULL, file, VAR_CMD, FALSE);
176160285Sbostic 
176260285Sbostic     /*
176360285Sbostic      * Now we know the file's name, we attempt to find the durn thing.
176460285Sbostic      * A return of NULL indicates the file don't exist.
176560285Sbostic      *
176660285Sbostic      * Include files are first searched for relative to the including
176760285Sbostic      * file's location. We don't want to cd there, of course, so we
176860285Sbostic      * just tack on the old file's leading path components and call
176960285Sbostic      * Dir_FindFile to see if we can locate the beast.
177060285Sbostic      * XXX - this *does* search in the current directory, right?
177160285Sbostic      */
177260285Sbostic 
177360285Sbostic     prefEnd = strrchr (fname, '/');
177460285Sbostic     if (prefEnd != (char *)NULL) {
177560285Sbostic 	char  	*newName;
177660285Sbostic 
177760285Sbostic 	*prefEnd = '\0';
177860285Sbostic 	newName = str_concat (fname, file, STR_ADDSLASH);
177960285Sbostic 	fullname = Dir_FindFile (newName, parseIncPath);
178060285Sbostic 	if (fullname == (char *)NULL) {
178160285Sbostic 	    fullname = Dir_FindFile(newName, dirSearchPath);
178260285Sbostic 	}
178360285Sbostic 	free (newName);
178460285Sbostic 	*prefEnd = '/';
178560285Sbostic     } else {
178660285Sbostic 	fullname = (char *)NULL;
178760285Sbostic     }
178860285Sbostic 
178960285Sbostic     if (fullname == (char *)NULL) {
179060285Sbostic 	/*
179160285Sbostic 	 * System makefile or makefile wasn't found in same directory as
179260285Sbostic 	 * included makefile. Search for it first on the -I search path,
179360285Sbostic 	 * then on the .PATH search path, if not found in a -I directory.
179460285Sbostic 	 * XXX: Suffix specific?
179560285Sbostic 	 */
179660285Sbostic 	fullname = Dir_FindFile (file, parseIncPath);
179760285Sbostic 	if (fullname == (char *)NULL) {
179860285Sbostic 	    fullname = Dir_FindFile(file, dirSearchPath);
179960285Sbostic 	}
180060285Sbostic     }
180160285Sbostic 
180260285Sbostic     if (fullname == (char *)NULL) {
180360285Sbostic 	/*
180460285Sbostic 	 * Still haven't found the makefile. Look for it on the system
180560285Sbostic 	 * path as a last resort.
180660285Sbostic 	 */
180760285Sbostic 	fullname = Dir_FindFile(file, sysIncPath);
180860285Sbostic     }
180960285Sbostic 
181060285Sbostic     if (fullname == (char *) NULL) {
181160285Sbostic 	Parse_Error (PARSE_FATAL, "Could not find %s", file);
181260285Sbostic 	return;
181360285Sbostic     }
181460285Sbostic 
181560285Sbostic     /*
181660285Sbostic      * Once we find the absolute path to the file, we get to save all the
181760285Sbostic      * state from the current file before we can start reading this
181860285Sbostic      * include file. The state is stored in an IFile structure which
181960285Sbostic      * is placed on a list with other IFile structures. The list makes
182060285Sbostic      * a very nice stack to track how we got here...
182160285Sbostic      */
182260285Sbostic     oldFile = (IFile *) emalloc (sizeof (IFile));
182360285Sbostic     oldFile->fname = fname;
182460285Sbostic 
182560285Sbostic     oldFile->F = curFILE;
182660285Sbostic     oldFile->p = curPTR;
182760285Sbostic     oldFile->lineno = lineno;
182860285Sbostic 
182960285Sbostic     (void) Lst_AtFront (includes, (ClientData)oldFile);
183060285Sbostic 
183160285Sbostic     /*
183260285Sbostic      * Once the previous state has been saved, we can get down to reading
183360285Sbostic      * the new file. We set up the name of the file to be the absolute
183460285Sbostic      * name of the include file so error messages refer to the right
183560285Sbostic      * place. Naturally enough, we start reading at line number 0.
183660285Sbostic      */
183760285Sbostic     fname = fullname;
183860285Sbostic     lineno = 0;
183960285Sbostic 
184060285Sbostic     curFILE = fopen (fullname, "r");
184160285Sbostic     curPTR = NULL;
184260285Sbostic     if (curFILE == (FILE * ) NULL) {
184360285Sbostic 	Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
184460285Sbostic 	/*
184560285Sbostic 	 * Pop to previous file
184660285Sbostic 	 */
184760285Sbostic 	(void) ParseEOF(1);
184860285Sbostic     }
184960285Sbostic }
185060285Sbostic #endif
185160285Sbostic 
185260285Sbostic /*-
185360285Sbostic  *---------------------------------------------------------------------
185440393Sbostic  * ParseEOF  --
185540393Sbostic  *	Called when EOF is reached in the current file. If we were reading
185640393Sbostic  *	an include file, the includes stack is popped and things set up
185740393Sbostic  *	to go back to reading the previous file at the previous location.
185840393Sbostic  *
185940393Sbostic  * Results:
186040393Sbostic  *	CONTINUE if there's more to do. DONE if not.
186140393Sbostic  *
186240393Sbostic  * Side Effects:
186340393Sbostic  *	The old curFILE, is closed. The includes list is shortened.
186440393Sbostic  *	lineno, curFILE, and fname are changed if CONTINUE is returned.
186540393Sbostic  *---------------------------------------------------------------------
186640393Sbostic  */
186740393Sbostic static int
186846462Sbostic ParseEOF (opened)
186946462Sbostic     int opened;
187040393Sbostic {
187140393Sbostic     IFile     *ifile;	/* the state on the top of the includes stack */
187240393Sbostic 
187340393Sbostic     if (Lst_IsEmpty (includes)) {
187440393Sbostic 	return (DONE);
187540393Sbostic     }
187640393Sbostic 
187740393Sbostic     ifile = (IFile *) Lst_DeQueue (includes);
187860285Sbostic     free ((Address) fname);
187940393Sbostic     fname = ifile->fname;
188040393Sbostic     lineno = ifile->lineno;
188160285Sbostic     if (opened && curFILE)
188246462Sbostic 	(void) fclose (curFILE);
188360285Sbostic     if (curPTR) {
188460285Sbostic 	free((Address) curPTR->str);
188560285Sbostic 	free((Address) curPTR);
188660285Sbostic     }
188740393Sbostic     curFILE = ifile->F;
188860285Sbostic     curPTR = ifile->p;
188940393Sbostic     free ((Address)ifile);
189040393Sbostic     return (CONTINUE);
189140393Sbostic }
189240424Sbostic 
189340393Sbostic /*-
189440393Sbostic  *---------------------------------------------------------------------
189540393Sbostic  * ParseReadc  --
189660285Sbostic  *	Read a character from the current file
189740393Sbostic  *
189840393Sbostic  * Results:
189940393Sbostic  *	The character that was read
190040393Sbostic  *
190140393Sbostic  * Side Effects:
190240393Sbostic  *---------------------------------------------------------------------
190340393Sbostic  */
190460285Sbostic static int
190560285Sbostic ParseReadc()
190660285Sbostic {
190760285Sbostic     if (curFILE)
190860285Sbostic 	return fgetc(curFILE);
190960285Sbostic 
191060285Sbostic     if (curPTR && *curPTR->ptr)
191160285Sbostic 	return *curPTR->ptr++;
191260285Sbostic     return EOF;
191360285Sbostic }
191440393Sbostic 
1915*60557Sbostic 
1916*60557Sbostic /*-
1917*60557Sbostic  *---------------------------------------------------------------------
1918*60557Sbostic  * ParseUnreadc  --
1919*60557Sbostic  *	Put back a character to the current file
1920*60557Sbostic  *
1921*60557Sbostic  * Results:
1922*60557Sbostic  *	None.
1923*60557Sbostic  *
1924*60557Sbostic  * Side Effects:
1925*60557Sbostic  *---------------------------------------------------------------------
1926*60557Sbostic  */
1927*60557Sbostic static void
1928*60557Sbostic ParseUnreadc(c)
1929*60557Sbostic     int c;
1930*60557Sbostic {
1931*60557Sbostic     if (curFILE) {
1932*60557Sbostic 	ungetc(c, curFILE);
1933*60557Sbostic 	return;
1934*60557Sbostic     }
1935*60557Sbostic     if (curPTR) {
1936*60557Sbostic 	*--(curPTR->ptr) = c;
1937*60557Sbostic 	return;
1938*60557Sbostic     }
1939*60557Sbostic }
1940*60557Sbostic 
1941*60557Sbostic 
194260285Sbostic /* ParseSkipLine():
194360285Sbostic  *	Grab the next line
194460285Sbostic  */
194560285Sbostic static char *
194660285Sbostic ParseSkipLine(skip)
194760285Sbostic     int skip; 		/* Skip lines that don't start with . */
194860285Sbostic {
194960285Sbostic     char *line;
195060285Sbostic     int c, lastc = '\0', lineLength;
195160285Sbostic     Buffer buf;
195240393Sbostic 
195360285Sbostic     c = ParseReadc();
195440424Sbostic 
195560285Sbostic     if (skip) {
195660285Sbostic 	/*
195760285Sbostic 	 * Skip lines until get to one that begins with a
195860285Sbostic 	 * special char.
195960285Sbostic 	 */
196060285Sbostic 	while ((c != '.') && (c != EOF)) {
196160285Sbostic 	    while (((c != '\n') || (lastc == '\\')) && (c != EOF))
196260285Sbostic 	    {
196360285Sbostic 		/*
196460285Sbostic 		 * Advance to next unescaped newline
196560285Sbostic 		 */
196660285Sbostic 		if ((lastc = c) == '\n') {
196760285Sbostic 		    lineno++;
196860285Sbostic 		}
196960285Sbostic 		c = ParseReadc();
197060285Sbostic 	    }
197160285Sbostic 	    lineno++;
197260285Sbostic 
197360285Sbostic 	    lastc = c;
197460285Sbostic 	    c = ParseReadc ();
197560285Sbostic 	}
197660285Sbostic     }
197760285Sbostic 
197860285Sbostic     if (c == EOF) {
197960285Sbostic 	Parse_Error (PARSE_FATAL, "Unclosed conditional/for loop");
198060285Sbostic 	return ((char *)NULL);
198160285Sbostic     }
198260285Sbostic 
198360285Sbostic     /*
198460285Sbostic      * Read the entire line into buf
198560285Sbostic      */
198660285Sbostic     buf = Buf_Init (MAKE_BSIZE);
198760285Sbostic     if (c != '\n') {
198860285Sbostic 	do {
198960285Sbostic 	    Buf_AddByte (buf, (Byte)c);
199060285Sbostic 	    c = ParseReadc();
199160285Sbostic 	} while ((c != '\n') && (c != EOF));
199260285Sbostic     }
199360285Sbostic     lineno++;
199460285Sbostic 
199560285Sbostic     Buf_AddByte (buf, (Byte)'\0');
199660285Sbostic     line = (char *)Buf_GetAll (buf, &lineLength);
199760285Sbostic     Buf_Destroy (buf, FALSE);
199860285Sbostic     return line;
199960285Sbostic }
200060285Sbostic 
200160285Sbostic 
200240393Sbostic /*-
200340393Sbostic  *---------------------------------------------------------------------
200440393Sbostic  * ParseReadLine --
200540393Sbostic  *	Read an entire line from the input file. Called only by Parse_File.
200640393Sbostic  *	To facilitate escaped newlines and what have you, a character is
200740393Sbostic  *	buffered in 'lastc', which is '\0' when no characters have been
200840393Sbostic  *	read. When we break out of the loop, c holds the terminating
200940393Sbostic  *	character and lastc holds a character that should be added to
201040393Sbostic  *	the line (unless we don't read anything but a terminator).
201140393Sbostic  *
201240393Sbostic  * Results:
201340393Sbostic  *	A line w/o its newline
201440393Sbostic  *
201540393Sbostic  * Side Effects:
201640393Sbostic  *	Only those associated with reading a character
201740393Sbostic  *---------------------------------------------------------------------
201840393Sbostic  */
201940393Sbostic static char *
202040393Sbostic ParseReadLine ()
202140393Sbostic {
202240393Sbostic     Buffer  	  buf;	    	/* Buffer for current line */
202340393Sbostic     register int  c;	      	/* the current character */
202440393Sbostic     register int  lastc;    	/* The most-recent character */
202540393Sbostic     Boolean	  semiNL;     	/* treat semi-colons as newlines */
202640393Sbostic     Boolean	  ignDepOp;   	/* TRUE if should ignore dependency operators
202740393Sbostic 				 * for the purposes of setting semiNL */
202840393Sbostic     Boolean 	  ignComment;	/* TRUE if should ignore comments (in a
202940393Sbostic 				 * shell command */
203040393Sbostic     char    	  *line;    	/* Result */
203140393Sbostic     int	    	  lineLength;	/* Length of result */
203240393Sbostic 
203340393Sbostic     semiNL = FALSE;
203440393Sbostic     ignDepOp = FALSE;
203540393Sbostic     ignComment = FALSE;
203640393Sbostic 
203740393Sbostic     /*
203840393Sbostic      * Handle special-characters at the beginning of the line. Either a
203940393Sbostic      * leading tab (shell command) or pound-sign (possible conditional)
204040393Sbostic      * forces us to ignore comments and dependency operators and treat
204140393Sbostic      * semi-colons as semi-colons (by leaving semiNL FALSE). This also
204240393Sbostic      * discards completely blank lines.
204340393Sbostic      */
204460285Sbostic     for (;;) {
204540393Sbostic 	c = ParseReadc();
204640393Sbostic 
2047*60557Sbostic 	if (c == '\t') {
204844599Sbostic 	    ignComment = ignDepOp = TRUE;
204944599Sbostic 	    break;
205040393Sbostic 	} else if (c == '\n') {
205140393Sbostic 	    lineno++;
205240393Sbostic 	} else {
205340393Sbostic 	    /*
205440393Sbostic 	     * Anything else breaks out without doing anything
205540393Sbostic 	     */
205640393Sbostic 	    break;
205740393Sbostic 	}
205840393Sbostic     }
205940393Sbostic 
206040393Sbostic     if (c != EOF) {
206140393Sbostic 	lastc = c;
206260285Sbostic 	buf = Buf_Init(MAKE_BSIZE);
206340393Sbostic 
206440393Sbostic 	while (((c = ParseReadc ()) != '\n' || (lastc == '\\')) &&
206540393Sbostic 	       (c != EOF))
206640393Sbostic 	{
206740393Sbostic test_char:
206840393Sbostic 	    switch(c) {
206940393Sbostic 	    case '\n':
207040393Sbostic 		/*
207140393Sbostic 		 * Escaped newline: read characters until a non-space or an
207240393Sbostic 		 * unescaped newline and replace them all by a single space.
207340393Sbostic 		 * This is done by storing the space over the backslash and
207440393Sbostic 		 * dropping through with the next nonspace. If it is a
207540393Sbostic 		 * semi-colon and semiNL is TRUE, it will be recognized as a
207640393Sbostic 		 * newline in the code below this...
207740393Sbostic 		 */
207840393Sbostic 		lineno++;
207940393Sbostic 		lastc = ' ';
208040393Sbostic 		while ((c = ParseReadc ()) == ' ' || c == '\t') {
208140393Sbostic 		    continue;
208240393Sbostic 		}
208340393Sbostic 		if (c == EOF || c == '\n') {
208440393Sbostic 		    goto line_read;
208540393Sbostic 		} else {
208640393Sbostic 		    /*
208740393Sbostic 		     * Check for comments, semiNL's, etc. -- easier than
2088*60557Sbostic 		     * ParseUnreadc(c); continue;
208940393Sbostic 		     */
209040393Sbostic 		    goto test_char;
209140393Sbostic 		}
209260285Sbostic 		/*NOTREACHED*/
209340393Sbostic 		break;
209460285Sbostic 
209540393Sbostic 	    case ';':
209640393Sbostic 		/*
209740393Sbostic 		 * Semi-colon: Need to see if it should be interpreted as a
209840393Sbostic 		 * newline
209940393Sbostic 		 */
210040393Sbostic 		if (semiNL) {
210140393Sbostic 		    /*
210240393Sbostic 		     * To make sure the command that may be following this
210340393Sbostic 		     * semi-colon begins with a tab, we push one back into the
210440393Sbostic 		     * input stream. This will overwrite the semi-colon in the
210540393Sbostic 		     * buffer. If there is no command following, this does no
210640393Sbostic 		     * harm, since the newline remains in the buffer and the
210740393Sbostic 		     * whole line is ignored.
210840393Sbostic 		     */
2109*60557Sbostic 		    ParseUnreadc('\t');
211040393Sbostic 		    goto line_read;
211140393Sbostic 		}
211240393Sbostic 		break;
211340393Sbostic 	    case '=':
211440393Sbostic 		if (!semiNL) {
211540393Sbostic 		    /*
211640393Sbostic 		     * Haven't seen a dependency operator before this, so this
211740393Sbostic 		     * must be a variable assignment -- don't pay attention to
211840393Sbostic 		     * dependency operators after this.
211940393Sbostic 		     */
212040393Sbostic 		    ignDepOp = TRUE;
212140393Sbostic 		} else if (lastc == ':' || lastc == '!') {
212240393Sbostic 		    /*
212340393Sbostic 		     * Well, we've seen a dependency operator already, but it
212440393Sbostic 		     * was the previous character, so this is really just an
212540393Sbostic 		     * expanded variable assignment. Revert semi-colons to
212640393Sbostic 		     * being just semi-colons again and ignore any more
212740393Sbostic 		     * dependency operators.
212840393Sbostic 		     *
212940393Sbostic 		     * XXX: Note that a line like "foo : a:=b" will blow up,
213040393Sbostic 		     * but who'd write a line like that anyway?
213140393Sbostic 		     */
213240393Sbostic 		    ignDepOp = TRUE; semiNL = FALSE;
213340393Sbostic 		}
213440393Sbostic 		break;
213540393Sbostic 	    case '#':
213640393Sbostic 		if (!ignComment) {
213760285Sbostic 		    if (compatMake || (lastc != '\\')) {
213840393Sbostic 			/*
213940393Sbostic 			 * If the character is a hash mark and it isn't escaped
214040393Sbostic 			 * (or we're being compatible), the thing is a comment.
214140393Sbostic 			 * Skip to the end of the line.
214240393Sbostic 			 */
214340393Sbostic 			do {
214440393Sbostic 			    c = ParseReadc();
214540393Sbostic 			} while ((c != '\n') && (c != EOF));
214640393Sbostic 			goto line_read;
214760285Sbostic 		    } else {
214860285Sbostic 			/*
214960285Sbostic 			 * Don't add the backslash. Just let the # get copied
215060285Sbostic 			 * over.
215160285Sbostic 			 */
215260285Sbostic 			lastc = c;
215360285Sbostic 			continue;
215460285Sbostic 		    }
215540393Sbostic 		}
215640393Sbostic 		break;
215740393Sbostic 	    case ':':
215840393Sbostic 	    case '!':
215940393Sbostic 		if (!ignDepOp && (c == ':' || c == '!')) {
216040393Sbostic 		    /*
216140393Sbostic 		     * A semi-colon is recognized as a newline only on
216240393Sbostic 		     * dependency lines. Dependency lines are lines with a
216340393Sbostic 		     * colon or an exclamation point. Ergo...
216440393Sbostic 		     */
216540393Sbostic 		    semiNL = TRUE;
216640393Sbostic 		}
216740393Sbostic 		break;
216840393Sbostic 	    }
216940393Sbostic 	    /*
217040393Sbostic 	     * Copy in the previous character and save this one in lastc.
217140393Sbostic 	     */
217240393Sbostic 	    Buf_AddByte (buf, (Byte)lastc);
217340393Sbostic 	    lastc = c;
217440393Sbostic 
217540393Sbostic 	}
217640393Sbostic     line_read:
217740393Sbostic 	lineno++;
217840393Sbostic 
217940393Sbostic 	if (lastc != '\0') {
218040393Sbostic 	    Buf_AddByte (buf, (Byte)lastc);
218140393Sbostic 	}
218240393Sbostic 	Buf_AddByte (buf, (Byte)'\0');
218340393Sbostic 	line = (char *)Buf_GetAll (buf, &lineLength);
218440393Sbostic 	Buf_Destroy (buf, FALSE);
218540393Sbostic 
218640435Sbostic 	if (line[0] == '.') {
218740393Sbostic 	    /*
218840393Sbostic 	     * The line might be a conditional. Ask the conditional module
218940393Sbostic 	     * about it and act accordingly
219040393Sbostic 	     */
219140393Sbostic 	    switch (Cond_Eval (line)) {
219240393Sbostic 	    case COND_SKIP:
219360285Sbostic 		/*
219460285Sbostic 		 * Skip to next conditional that evaluates to COND_PARSE.
219560285Sbostic 		 */
219640393Sbostic 		do {
219740393Sbostic 		    free (line);
219860285Sbostic 		    line = ParseSkipLine(1);
219960285Sbostic 		} while (line && Cond_Eval(line) != COND_PARSE);
220060285Sbostic 		if (line == NULL)
220160285Sbostic 		    break;
220240393Sbostic 		/*FALLTHRU*/
220340393Sbostic 	    case COND_PARSE:
220460285Sbostic 		free ((Address) line);
220540393Sbostic 		line = ParseReadLine();
220640393Sbostic 		break;
220760285Sbostic 	    case COND_INVALID:
220860285Sbostic 		if (For_Eval(line)) {
220960285Sbostic 		    int ok;
221060285Sbostic 		    free(line);
221160285Sbostic 		    do {
221260285Sbostic 			/*
221360285Sbostic 			 * Skip after the matching end
221460285Sbostic 			 */
221560285Sbostic 			line = ParseSkipLine(0);
221660285Sbostic 			if (line == NULL) {
221760285Sbostic 			    Parse_Error (PARSE_FATAL,
221860285Sbostic 				     "Unexpected end of file in for loop.\n");
221960285Sbostic 			    break;
222060285Sbostic 			}
222160285Sbostic 			ok = For_Eval(line);
222260285Sbostic 			free(line);
222360285Sbostic 		    }
222460285Sbostic 		    while (ok);
222560285Sbostic 		    if (line != NULL)
222660285Sbostic 			For_Run();
222760285Sbostic 		    line = ParseReadLine();
222860285Sbostic 		}
222960285Sbostic 		break;
223040393Sbostic 	    }
223140393Sbostic 	}
223240393Sbostic 	return (line);
223360285Sbostic 
223440393Sbostic     } else {
223540393Sbostic 	/*
223640393Sbostic 	 * Hit end-of-file, so return a NULL line to indicate this.
223740393Sbostic 	 */
223840393Sbostic 	return((char *)NULL);
223940393Sbostic     }
224040393Sbostic }
224140424Sbostic 
224240393Sbostic /*-
224340393Sbostic  *-----------------------------------------------------------------------
224440393Sbostic  * ParseFinishLine --
224540393Sbostic  *	Handle the end of a dependency group.
224640393Sbostic  *
224740393Sbostic  * Results:
224840393Sbostic  *	Nothing.
224940393Sbostic  *
225040393Sbostic  * Side Effects:
225140393Sbostic  *	inLine set FALSE. 'targets' list destroyed.
225240393Sbostic  *
225340393Sbostic  *-----------------------------------------------------------------------
225440393Sbostic  */
225540393Sbostic static void
225640393Sbostic ParseFinishLine()
225740393Sbostic {
225840393Sbostic     extern int Suff_EndTransform();
225940393Sbostic 
226040393Sbostic     if (inLine) {
226140393Sbostic 	Lst_ForEach(targets, Suff_EndTransform, (ClientData)NULL);
226240393Sbostic 	Lst_Destroy (targets, ParseHasCommands);
226340393Sbostic 	inLine = FALSE;
226440393Sbostic     }
226540393Sbostic }
226640393Sbostic 
226740424Sbostic 
226840393Sbostic /*-
226940393Sbostic  *---------------------------------------------------------------------
227040393Sbostic  * Parse_File --
227140393Sbostic  *	Parse a file into its component parts, incorporating it into the
227240393Sbostic  *	current dependency graph. This is the main function and controls
227340393Sbostic  *	almost every other function in this module
227440393Sbostic  *
227540393Sbostic  * Results:
227640393Sbostic  *	None
227740393Sbostic  *
227840393Sbostic  * Side Effects:
227940393Sbostic  *	Loads. Nodes are added to the list of all targets, nodes and links
228040393Sbostic  *	are added to the dependency graph. etc. etc. etc.
228140393Sbostic  *---------------------------------------------------------------------
228240393Sbostic  */
228340393Sbostic void
228440393Sbostic Parse_File(name, stream)
228540393Sbostic     char          *name;	/* the name of the file being read */
228640393Sbostic     FILE *	  stream;   	/* Stream open to makefile to parse */
228740393Sbostic {
228840393Sbostic     register char *cp,		/* pointer into the line */
228940393Sbostic                   *line;	/* the line we're working on */
229040393Sbostic 
229140393Sbostic     inLine = FALSE;
229240393Sbostic     fname = name;
229340393Sbostic     curFILE = stream;
229440393Sbostic     lineno = 0;
229540393Sbostic     fatals = 0;
229640393Sbostic 
229740393Sbostic     do {
229860285Sbostic 	while ((line = ParseReadLine ()) != NULL) {
229940435Sbostic 	    if (*line == '.') {
230040393Sbostic 		/*
230140393Sbostic 		 * Lines that begin with the special character are either
230240393Sbostic 		 * include or undef directives.
230340393Sbostic 		 */
230440393Sbostic 		for (cp = line + 1; isspace (*cp); cp++) {
230540393Sbostic 		    continue;
230640393Sbostic 		}
230740393Sbostic 		if (strncmp (cp, "include", 7) == 0) {
230840393Sbostic 		    ParseDoInclude (cp + 7);
230940393Sbostic 		    goto nextLine;
231040393Sbostic 		} else if (strncmp(cp, "undef", 5) == 0) {
231140393Sbostic 		    char *cp2;
231240393Sbostic 		    for (cp += 5; isspace(*cp); cp++) {
231340393Sbostic 			continue;
231440393Sbostic 		    }
231540393Sbostic 
231640393Sbostic 		    for (cp2 = cp; !isspace(*cp2) && (*cp2 != '\0'); cp2++) {
231740393Sbostic 			continue;
231840393Sbostic 		    }
231940393Sbostic 
232040393Sbostic 		    *cp2 = '\0';
232140393Sbostic 
232240393Sbostic 		    Var_Delete(cp, VAR_GLOBAL);
232340393Sbostic 		    goto nextLine;
232440393Sbostic 		}
232540393Sbostic 	    }
232640393Sbostic 	    if (*line == '#') {
232740435Sbostic 		/* If we're this far, the line must be a comment. */
232840393Sbostic 		goto nextLine;
232940393Sbostic 	    }
233040393Sbostic 
233140393Sbostic 	    if (*line == '\t'
233240393Sbostic #ifdef POSIX
233340393Sbostic 		       || *line == ' '
233440393Sbostic #endif
233540393Sbostic 		       )
233640393Sbostic 	    {
233740393Sbostic 		/*
233840393Sbostic 		 * If a line starts with a tab (or space in POSIX-land), it
233940393Sbostic 		 * can only hope to be a creation command.
234040393Sbostic 		 */
234140393Sbostic 	    shellCommand:
234240393Sbostic 		for (cp = line + 1; isspace (*cp); cp++) {
234340393Sbostic 		    continue;
234440393Sbostic 		}
234540393Sbostic 		if (*cp) {
234640393Sbostic 		    if (inLine) {
234740393Sbostic 			/*
234840393Sbostic 			 * So long as it's not a blank line and we're actually
234940393Sbostic 			 * in a dependency spec, add the command to the list of
235040393Sbostic 			 * commands of all targets in the dependency spec
235140393Sbostic 			 */
235240393Sbostic 			Lst_ForEach (targets, ParseAddCmd, (ClientData)cp);
235340393Sbostic 			continue;
235440393Sbostic 		    } else {
235540393Sbostic 			Parse_Error (PARSE_FATAL,
235640393Sbostic 				     "Unassociated shell command \"%.20s\"",
235740393Sbostic 				     cp);
235840393Sbostic 		    }
235940393Sbostic 		}
236060285Sbostic #ifdef SYSVINCLUDE
236160285Sbostic 	    } else if (strncmp (line, "include", 7) == 0 &&
236260285Sbostic 		       strchr(line, ':') == NULL) {
236360285Sbostic 		/*
236460285Sbostic 		 * It's an S3/S5-style "include".
236560285Sbostic 		 */
236660285Sbostic 		ParseTraditionalInclude (line + 7);
236760285Sbostic 		goto nextLine;
236860285Sbostic #endif
236940393Sbostic 	    } else if (Parse_IsVar (line)) {
237040393Sbostic 		ParseFinishLine();
237140393Sbostic 		Parse_DoVar (line, VAR_GLOBAL);
237240393Sbostic 	    } else {
237340393Sbostic 		/*
237440393Sbostic 		 * We now know it's a dependency line so it needs to have all
237540393Sbostic 		 * variables expanded before being parsed. Tell the variable
237640393Sbostic 		 * module to complain if some variable is undefined...
237740393Sbostic 		 * To make life easier on novices, if the line is indented we
237840393Sbostic 		 * first make sure the line has a dependency operator in it.
237940393Sbostic 		 * If it doesn't have an operator and we're in a dependency
238040393Sbostic 		 * line's script, we assume it's actually a shell command
238140393Sbostic 		 * and add it to the current list of targets.
238240393Sbostic 		 *
238340393Sbostic 		 * Note that POSIX declares all lines that start with
238440393Sbostic 		 * whitespace are shell commands, so there's no need to check
238540393Sbostic 		 * here...
238640393Sbostic 		 */
238740393Sbostic 		Boolean	nonSpace = FALSE;
238840393Sbostic 
238940393Sbostic 		cp = line;
239040393Sbostic #ifndef POSIX
239140393Sbostic 		if (line[0] == ' ') {
239240393Sbostic 		    while ((*cp != ':') && (*cp != '!') && (*cp != '\0')) {
239340393Sbostic 			if (!isspace(*cp)) {
239440393Sbostic 			    nonSpace = TRUE;
239540393Sbostic 			}
239640393Sbostic 			cp++;
239740393Sbostic 		    }
239840393Sbostic 		}
239940393Sbostic 
240040393Sbostic 		if (*cp == '\0') {
240140393Sbostic 		    if (inLine) {
240240393Sbostic 			Parse_Error (PARSE_WARNING,
240340393Sbostic 				     "Shell command needs a leading tab");
240440393Sbostic 			goto shellCommand;
240540393Sbostic 		    } else if (nonSpace) {
240640393Sbostic 			Parse_Error (PARSE_FATAL, "Missing operator");
240740393Sbostic 		    }
240840393Sbostic 		} else {
240940393Sbostic #endif
241040393Sbostic 		    ParseFinishLine();
241140393Sbostic 
241260285Sbostic 		    cp = Var_Subst (NULL, line, VAR_CMD, TRUE);
241340393Sbostic 		    free (line);
241440393Sbostic 		    line = cp;
241540393Sbostic 
241640393Sbostic 		    /*
241740393Sbostic 		     * Need a non-circular list for the target nodes
241840393Sbostic 		     */
241940393Sbostic 		    targets = Lst_Init (FALSE);
242040393Sbostic 		    inLine = TRUE;
242140393Sbostic 
242240393Sbostic 		    ParseDoDependency (line);
242340393Sbostic #ifndef POSIX
242440393Sbostic 		}
242540393Sbostic #endif
242640393Sbostic 	    }
242740393Sbostic 
242840393Sbostic 	    nextLine:
242940393Sbostic 
243040393Sbostic 	    free (line);
243140393Sbostic 	}
243240393Sbostic 	/*
243340393Sbostic 	 * Reached EOF, but it may be just EOF of an include file...
243440393Sbostic 	 */
243546462Sbostic     } while (ParseEOF(1) == CONTINUE);
243640393Sbostic 
243740393Sbostic     /*
243840393Sbostic      * Make sure conditionals are clean
243940393Sbostic      */
244040393Sbostic     Cond_End();
244140393Sbostic 
244240393Sbostic     if (fatals) {
244340393Sbostic 	fprintf (stderr, "Fatal errors encountered -- cannot continue\n");
244440393Sbostic 	exit (1);
244540393Sbostic     }
244640393Sbostic }
244740424Sbostic 
244840393Sbostic /*-
244940393Sbostic  *---------------------------------------------------------------------
245040393Sbostic  * Parse_Init --
245140393Sbostic  *	initialize the parsing module
245240393Sbostic  *
245340393Sbostic  * Results:
245440393Sbostic  *	none
245540393Sbostic  *
245640393Sbostic  * Side Effects:
245740393Sbostic  *	the parseIncPath list is initialized...
245840393Sbostic  *---------------------------------------------------------------------
245940393Sbostic  */
246060285Sbostic void
246140393Sbostic Parse_Init ()
246240393Sbostic {
246360285Sbostic 	char *cp = NULL, *start;
246440515Sbostic 					/* avoid faults on read-only strings */
246540515Sbostic 	static char syspath[] = _PATH_DEFSYSPATH;
246640393Sbostic 
246740393Sbostic     mainNode = NILGNODE;
246840393Sbostic     parseIncPath = Lst_Init (FALSE);
246940393Sbostic     sysIncPath = Lst_Init (FALSE);
247040393Sbostic     includes = Lst_Init (FALSE);
247140393Sbostic 
247240393Sbostic     /*
247340393Sbostic      * Add the directories from the DEFSYSPATH (more than one may be given
247440393Sbostic      * as dir1:...:dirn) to the system include path.
247540393Sbostic      */
247640393Sbostic     for (start = syspath; *start != '\0'; start = cp) {
247760285Sbostic 	for (cp = start; *cp != '\0' && *cp != ':'; cp++)
247860285Sbostic 	    continue;
247940393Sbostic 	if (*cp == '\0') {
248040393Sbostic 	    Dir_AddDir(sysIncPath, start);
248140393Sbostic 	} else {
248240393Sbostic 	    *cp++ = '\0';
248340393Sbostic 	    Dir_AddDir(sysIncPath, start);
248440393Sbostic 	}
248540393Sbostic     }
248640393Sbostic }
248740424Sbostic 
248840393Sbostic /*-
248940393Sbostic  *-----------------------------------------------------------------------
249040393Sbostic  * Parse_MainName --
249140393Sbostic  *	Return a Lst of the main target to create for main()'s sake. If
249240393Sbostic  *	no such target exists, we Punt with an obnoxious error message.
249340393Sbostic  *
249440393Sbostic  * Results:
249540393Sbostic  *	A Lst of the single node to create.
249640393Sbostic  *
249740393Sbostic  * Side Effects:
249840393Sbostic  *	None.
249940393Sbostic  *
250040393Sbostic  *-----------------------------------------------------------------------
250140393Sbostic  */
250240393Sbostic Lst
250340393Sbostic Parse_MainName()
250440393Sbostic {
250540393Sbostic     Lst           main;	/* result list */
250640393Sbostic 
250740393Sbostic     main = Lst_Init (FALSE);
250840393Sbostic 
250940393Sbostic     if (mainNode == NILGNODE) {
251045906Sbostic 	Punt ("make: no target to make.\n");
251140393Sbostic     	/*NOTREACHED*/
251240393Sbostic     } else if (mainNode->type & OP_DOUBLEDEP) {
251340393Sbostic 	Lst_Concat(main, mainNode->cohorts, LST_CONCNEW);
251440393Sbostic     }
251540393Sbostic     (void) Lst_AtEnd (main, (ClientData)mainNode);
251640393Sbostic     return (main);
251740393Sbostic }
2518