* Copyright (c) 1988, 1989, 1990, 1993
* The Regents of the University of California. All rights reserved.
* Copyright (c) 1989 by Berkeley Softworks
* This code is derived from software contributed to Berkeley by
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)parse.c 8.6 (Berkeley) 6/13/95";
* Functions to parse a makefile.
* One function, Parse_Init, must be called before any functions
* in this module are used. After that, the function Parse_File is the
* main entry point and controls most of the other functions in this
* Most important structures are kept in Lsts. Directories for
* the #include "..." function are kept in the 'parseIncPath' Lst, while
* those for the #include <...> are kept in the 'sysIncPath' Lst. The
* targets currently being defined are kept in the 'targets' Lst.
* The variables 'fname' and 'lineno' are used to track the name
* of the current file and the line number in that file so that error
* messages can be more meaningful.
* Parse_Init Initialization function which must be
* called before anything else in this module
* Parse_End Cleanup the module
* Parse_File Function used to parse a makefile. It must
* be given the name of the file, which should
* already have been opened, and a function
* to call to read a character from the file.
* Parse_IsVar Returns TRUE if the given line is a
* variable assignment. Used by MainParseArgs
* to determine if an argument is a target
* or a variable assignment. Used internally
* for pretty much the same thing...
* Parse_Error Function called when an error occurs in
* parsing. Used by the variable and
* Parse_MainName Returns a Lst of the main target to create.
* These values are returned by ParseEOF to tell Parse_File whether to
* CONTINUE parsing, i.e. it had only reached the end of an include file,
static Lst targets
; /* targets we're working on */
static Lst targCmds
; /* command lines for targets */
static Boolean inLine
; /* true if currently in a dependency
* line or its commands */
static char *fname
; /* name of current file (for errors) */
static int lineno
; /* line number in current file */
static FILE *curFILE
= NULL
; /* current makefile */
static PTR
*curPTR
= NULL
; /* current makefile */
static GNode
*mainNode
; /* The main target to create. This is the
* first target on the first dependency
* line in the first makefile */
* Definitions for handling #include specifications
char *fname
; /* name of previous file */
int lineno
; /* saved line number */
FILE * F
; /* the open stream */
PTR
* p
; /* the char pointer */
static Lst includes
; /* stack of IFiles generated by
Lst parseIncPath
; /* list of directories for "..." includes */
Lst sysIncPath
; /* list of directories for <...> includes */
* specType contains the SPECial TYPE of the current target. It is
* Not if the target is unspecial. If it *is* special, however, the children
* are linked as children of the parent but not vice versa. This variable is
* set in ParseDoDependency
Includes
, /* .INCLUDES */
Interrupt
, /* .INTERRUPT */
MFlags
, /* .MFLAGS or .MAKEFLAGS */
Main
, /* .MAIN and we don't have anything user-specified to
NoExport
, /* .NOEXPORT */
NotParallel
, /* .NOTPARALELL */
Precious
, /* .PRECIOUS */
Reserved
, /* .RESERVED or .[A-Z]* */
SingleShell
, /* .SINGLESHELL */
Suffixes
, /* .SUFFIXES */
Attribute
/* Generic attribute */
static ParseSpecial specType
;
* Predecessor node for handling .ORDER. Initialized to NILGNODE when .ORDER
* seen, then set to each successive source on the line.
static GNode
*predecessor
;
* The parseKeywords table is searched using binary search when deciding
* if a target or source is special. The 'spec' field is the ParseSpecial
* type of the keyword ("Not" if the keyword isn't special as a target) while
* the 'op' field is the operator to apply to the list of targets if the
* keyword is used as a source ("0" if the keyword isn't special as a source)
char *name
; /* Name of keyword */
ParseSpecial spec
; /* Type when used as a target */
int op
; /* Operator when used as a source */
{ ".DEFAULT", Default
, 0 },
{ ".EXEC", Attribute
, OP_EXEC
},
{ ".IGNORE", Ignore
, OP_IGNORE
},
{ ".INCLUDES", Includes
, 0 },
{ ".INTERRUPT", Interrupt
, 0 },
{ ".INVISIBLE", Attribute
, OP_INVISIBLE
},
{ ".JOIN", Attribute
, OP_JOIN
},
{ ".MAKE", Attribute
, OP_MAKE
},
{ ".MAKEFLAGS", MFlags
, 0 },
{ ".MFLAGS", MFlags
, 0 },
{ ".NOTMAIN", Attribute
, OP_NOTMAIN
},
#define DOT_NOTPARALLEL 15
{ ".NOTPARALLEL", NotParallel
, 0 },
{ ".OPTIONAL", Attribute
, OP_OPTIONAL
},
{ ".PHONY", Attribute
, OP_PHONY
},
{ ".PRECIOUS", Precious
, OP_PRECIOUS
},
{ ".RECURSIVE", Attribute
, OP_MAKE
},
{ ".RESERVED", Reserved
, 0 },
{ ".SHELL", ExShell
, 0 },
{ ".SILENT", Silent
, OP_SILENT
},
#define DOT_SINGLESHELL 26
{ ".SINGLESHELL", SingleShell
, 0 },
{ ".SUFFIXES", Suffixes
, 0 },
{ ".USE", Attribute
, OP_USE
},
static int ParseFindKeyword
__P((char *));
static int ParseLinkSrc
__P((ClientData
, ClientData
));
static int ParseDoOp
__P((ClientData
, ClientData
));
static void ParseDoSrc
__P((int, char *));
static int ParseFindMain
__P((ClientData
, ClientData
));
static int ParseAddDir
__P((ClientData
, ClientData
));
static int ParseClearPath
__P((ClientData
, ClientData
));
static void ParseDoDependency
__P((char *));
static int ParseAddCmd
__P((ClientData
, ClientData
));
static int ParseReadc
__P((void));
static void ParseUnreadc
__P((int));
static void ParseHasCommands
__P((ClientData
));
static void ParseDoInclude
__P((char *));
static void ParseTraditionalInclude
__P((char *));
static int ParseEOF
__P((int));
static char *ParseReadLine
__P((void));
static char *ParseSkipLine
__P((int));
static void ParseFinishLine
__P((void));
*----------------------------------------------------------------------
* Look in the table of keywords for one matching the given string.
* The index of the keyword, or -1 if it isn't there.
*----------------------------------------------------------------------
char *str
; /* String to find */
end
= (sizeof(parseKeywords
)/sizeof(parseKeywords
[0])) - 1;
cur
= start
+ ((end
- start
) / 2);
diff
= strcmp (str
, parseKeywords
[cur
].name
);
if (!isupper((unsigned char) *str
))
return *str
? -1 : DOT_RESERVED
;
* Error message abort function for parsing. Prints out the context
* of the error (line number and file) as well as the message with
* two optional arguments.
* "fatals" is incremented if the level is PARSE_FATAL.
Parse_Error(int type
, char *fmt
, ...)
int type
; /* Error type (PARSE_WARNING, PARSE_FATAL) */
fmt
= va_arg(ap
, char *);
(void)fprintf(stderr
, "\"%s\", line %d: ", fname
, lineno
);
if (type
== PARSE_WARNING
)
(void)fprintf(stderr
, "warning: ");
(void)vfprintf(stderr
, fmt
, ap
);
(void)fprintf(stderr
, "\n");
*---------------------------------------------------------------------
* Link the parent node to its new child. Used in a Lst_ForEach by
* ParseDoDependency. If the specType isn't 'Not', the parent
* isn't linked as a parent of the child.
* New elements are added to the parents list of cgn and the
* children list of cgn. the unmade field of pgn is updated
* to reflect the additional child.
*---------------------------------------------------------------------
ParseLinkSrc (pgnp
, cgnp
)
ClientData pgnp
; /* The parent node */
ClientData cgnp
; /* The child node */
GNode
*pgn
= (GNode
*) pgnp
;
GNode
*cgn
= (GNode
*) cgnp
;
if (Lst_Member (pgn
->children
, (ClientData
)cgn
) == NILLNODE
) {
(void)Lst_AtEnd (pgn
->children
, (ClientData
)cgn
);
(void)Lst_AtEnd (cgn
->parents
, (ClientData
)pgn
);
*---------------------------------------------------------------------
* Apply the parsed operator to the given target node. Used in a
* Lst_ForEach call by ParseDoDependency once all targets have
* been found and their operator parsed. If the previous and new
* operators are incompatible, a major error is taken.
* The type field of the node is altered to reflect any new bits in
*---------------------------------------------------------------------
ClientData gnp
; /* The node to which the operator is to be
ClientData opp
; /* The operator to apply */
GNode
*gn
= (GNode
*) gnp
;
* If the dependency mask of the operator and the node don't match and
* the node has actually had an operator applied to it before, and
* the operator actually has some dependency information in it, complain.
if (((op
& OP_OPMASK
) != (gn
->type
& OP_OPMASK
)) &&
!OP_NOP(gn
->type
) && !OP_NOP(op
))
Parse_Error (PARSE_FATAL
, "Inconsistent operator for %s", gn
->name
);
if ((op
== OP_DOUBLEDEP
) && ((gn
->type
& OP_OPMASK
) == OP_DOUBLEDEP
)) {
* If the node was the object of a :: operator, we need to create a
* new instance of it for the children and commands on this dependency
* line. The new instance is placed on the 'cohorts' list of the
* initial one (note the initial one is not on its own cohorts list)
* and the new instance is linked to all parents of the initial
cohort
= Targ_NewGN(gn
->name
);
* Duplicate links to parents so graph traversal is simple. Perhaps
* some type bits should be duplicated?
* Make the cohort invisible as well to avoid duplicating it into
* other variables. True, parents of this target won't tend to do
* anything with their local variables, but better safe than
Lst_ForEach(gn
->parents
, ParseLinkSrc
, (ClientData
)cohort
);
cohort
->type
= OP_DOUBLEDEP
|OP_INVISIBLE
;
(void)Lst_AtEnd(gn
->cohorts
, (ClientData
)cohort
);
* Replace the node in the targets list with the new copy
ln
= Lst_Member(targets
, (ClientData
)gn
);
Lst_Replace(ln
, (ClientData
)cohort
);
* We don't want to nuke any previous flags (whatever they were) so we
* just OR the new operator into the old
*---------------------------------------------------------------------
* Given the name of a source, figure out if it is an attribute
* and apply it to the targets if it is. Else decide if there is
* some attribute which should be applied *to* the source because
* of some special target and apply it if so. Otherwise, make the
* source be a child of the targets in the list 'targets'
* Operator bits may be added to the list of targets or to the source.
* The targets may have a new source added to their lists of children.
*---------------------------------------------------------------------
int tOp
; /* operator (if any) from special targets */
char *src
; /* name of the source to handle */
int op
; /* operator (if any) from special source */
if (*src
== '.' && isupper (src
[1])) {
int keywd
= ParseFindKeyword(src
);
op
= parseKeywords
[keywd
].op
;
Lst_ForEach (targets
, ParseDoOp
, (ClientData
)&op
);
} else if (specType
== Main
) {
* If we have noted the existence of a .MAIN, it means we need
* to add the sources of said target to the list of things
* to create. The string 'src' is likely to be free, so we
* must make a new copy of it. Note that this will only be
* invoked if the user didn't specify a target on the command
* line. This is to allow #ifmake's to succeed, or something...
(void) Lst_AtEnd (create
, (ClientData
)strdup(src
));
* Add the name to the .TARGETS variable as well, so the user cna
* employ that, if desired.
Var_Append(".TARGETS", src
, VAR_GLOBAL
);
} else if (specType
== Order
) {
* Create proper predecessor/successor links between the previous
* source and the current one.
gn
= Targ_FindNode(src
, TARG_CREATE
);
if (predecessor
!= NILGNODE
) {
(void)Lst_AtEnd(predecessor
->successors
, (ClientData
)gn
);
(void)Lst_AtEnd(gn
->preds
, (ClientData
)predecessor
);
* The current source now becomes the predecessor for the next one.
* If the source is not an attribute, we need to find/create
* a node for it. After that we can apply any operator to it
* from a special target or link it to its parents, as
* In the case of a source that was the object of a :: operator,
* the attribute is applied to all of its instances (as kept in
* the 'cohorts' list of the node) or all the cohorts are linked
gn
= Targ_FindNode (src
, TARG_CREATE
);
Lst_ForEach (targets
, ParseLinkSrc
, (ClientData
)gn
);
if ((gn
->type
& OP_OPMASK
) == OP_DOUBLEDEP
) {
for (ln
=Lst_First(gn
->cohorts
); ln
!= NILLNODE
; ln
= Lst_Succ(ln
)){
cohort
= (GNode
*)Lst_Datum(ln
);
Lst_ForEach(targets
, ParseLinkSrc
, (ClientData
)cohort
);
*-----------------------------------------------------------------------
* Find a real target in the list and set it to be the main one.
* Called by ParseDoDependency when a main target hasn't been found
* 0 if main not found yet, 1 if it is.
* mainNode is changed and Targ_SetMain is called.
*-----------------------------------------------------------------------
ParseFindMain(gnp
, dummy
)
ClientData gnp
; /* Node to examine */
GNode
*gn
= (GNode
*) gnp
;
if ((gn
->type
& (OP_NOTMAIN
|OP_USE
|OP_EXEC
|OP_TRANSFORM
)) == 0) {
*-----------------------------------------------------------------------
* Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
*-----------------------------------------------------------------------
Dir_AddDir((Lst
) path
, (char *) name
);
*-----------------------------------------------------------------------
* Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
*-----------------------------------------------------------------------
ParseClearPath(path
, dummy
)
Dir_ClearPath((Lst
) path
);
*---------------------------------------------------------------------
* Parse the dependency line in line.
* The nodes of the sources are linked as children to the nodes of the
* targets. Some nodes may be created.
* We parse a dependency line by first extracting words from the line and
* finding nodes in the list of all targets with that name. This is done
* until a character is encountered which is an operator character. Currently
* these are only ! and :. At this point the operator is parsed and the
* pointer into the line advanced until the first source is encountered.
* The parsed operator is applied to each node in the 'targets' list,
* which is where the nodes found for the targets are kept, by means of
* the ParseDoOp function.
* The sources are read in much the same way as the targets were except
* that now they are expanded using the wildcarding scheme of the C-Shell
* and all instances of the resulting words in the list of all targets
* are found. Each of the resulting nodes is then linked to each of the
* targets as one of its children.
* Certain targets are handled specially. These are the ones detailed
* by the specType variable.
* The storing of transformation rules is also taken care of here.
* A target is recognized as a transformation rule by calling
* Suff_IsTransform. If it is a transformation rule, its node is gotten
* from the suffix module via Suff_AddTransform rather than the standard
* Targ_FindNode in the target module.
*---------------------------------------------------------------------
char *line
; /* the line to parse */
char *cp
; /* our current position */
GNode
*gn
; /* a general purpose temporary node */
int op
; /* the operator on the line */
char savec
; /* a place to save a character */
Lst paths
; /* List of search paths to alter when parsing
* a list of .PATH targets */
int tOp
; /* operator from special target */
Lst sources
; /* list of source names after expansion */
Lst curTargs
; /* list of target names to be found and added
curTargs
= Lst_Init(FALSE
);
(*cp
!= '!') && (*cp
!= ':') && (*cp
!= '(');
* Must be a dynamic source (would have been expanded
* otherwise), so call the Var module to parse the puppy
* so we can safely advance beyond it...There should be
* no errors in this, as they would have been discovered
* in the initial Var_Subst and we wouldn't be here.
result
=Var_Parse(cp
, VAR_CMD
, TRUE
, &length
, &freeIt
);
* Archives must be handled specially to make sure the OP_ARCHV
* flag is set in their 'type' field, for one thing, and because
* things like "archive(file1.o file2.o file3.o)" are permissible.
* Arch_ParseArchive will set 'line' to be the first non-blank
* after the archive-spec. It creates/finds nodes for the members
* and places them on the given list, returning SUCCESS if all
* went well and FAILURE if there was an error in the
* specification. On error, line should remain untouched.
if (Arch_ParseArchive (&line
, targets
, VAR_CMD
) != SUCCESS
) {
Parse_Error (PARSE_FATAL
,
"Error in archive specification: \"%s\"", line
);
* Ending a dependency line without an operator is a Bozo
Parse_Error (PARSE_FATAL
, "Need an operator");
* Have a word in line. See if it's a special target and set
if (*line
== '.' && isupper (line
[1])) {
* See if the target is a special target that must have it
* or its sources handled specially.
int keywd
= ParseFindKeyword(line
);
if (specType
== ExPath
&& parseKeywords
[keywd
].spec
!= ExPath
) {
Parse_Error(PARSE_FATAL
, "Mismatched special targets");
specType
= parseKeywords
[keywd
].spec
;
tOp
= parseKeywords
[keywd
].op
;
* Certain special targets have special semantics:
* .PATH Have to set the dirSearchPath
* .MAIN Its sources are only used if
* nothing has been specified to
* .DEFAULT Need to create a node to hang
* commands on, but we don't want
* it in the graph, nor do we want
* it to be the Main Target, so we
* create it, set OP_NOTMAIN and
* add it to the list, setting
* DEFAULT to the new node for
* later use. We claim the node is
* A transformation rule to make
* life easier later, when we'll
* use Make_HandleUse to actually
* apply the .DEFAULT commands.
* .INTERRUPT Are not to be considered the
* .NOTPARALLEL Make only one target at a time.
* .SINGLESHELL Create a shell for each command.
* .ORDER Must set initial predecessor to NIL
(void)Lst_AtEnd(paths
, (ClientData
)dirSearchPath
);
if (!Lst_IsEmpty(create
)) {
gn
= Targ_FindNode(line
, TARG_CREATE
);
(void)Lst_AtEnd(targets
, (ClientData
)gn
);
gn
= Targ_NewGN(".DEFAULT");
gn
->type
|= (OP_NOTMAIN
|OP_TRANSFORM
);
(void)Lst_AtEnd(targets
, (ClientData
)gn
);
* A posix reserved target that we don't know
} else if (strncmp (line
, ".PATH", 5) == 0) {
* .PATH<suffix> has to be handled specially.
* Call on the suffix module to give us a path to
path
= Suff_GetPath (&line
[5]);
Parse_Error (PARSE_FATAL
,
"Suffix '%s' not defined (yet)",
if (paths
== (Lst
)NULL
) {
(void)Lst_AtEnd(paths
, (ClientData
)path
);
* Have word in line. Get or create its node and stick it at
* the end of the targets list
if ((specType
== Not
) && (*line
!= '\0')) {
if (Dir_HasWildcards(line
)) {
* Targets are to be sought only in the current directory,
* so create an empty path for the thing. Note we need to
* use Dir_Destroy in the destruction of the path as the
* Dir module could have added a directory to the path...
Lst emptyPath
= Lst_Init(FALSE
);
Dir_Expand(line
, emptyPath
, curTargs
);
Lst_Destroy(emptyPath
, Dir_Destroy
);
* No wildcards, but we want to avoid code duplication,
* so create a list with the word on it.
(void)Lst_AtEnd(curTargs
, (ClientData
)line
);
while(!Lst_IsEmpty(curTargs
)) {
char *targName
= (char *)Lst_DeQueue(curTargs
);
if (!Suff_IsTransform (targName
)) {
gn
= Targ_FindNode (targName
, TARG_CREATE
);
gn
= Suff_AddTransform (targName
);
(void)Lst_AtEnd (targets
, (ClientData
)gn
);
} else if (specType
== ExPath
&& *line
!= '.' && *line
!= '\0') {
Parse_Error(PARSE_WARNING
, "Extra target (%s) ignored", line
);
* If it is a special type and not .PATH, it's the only target we
if (specType
!= Not
&& specType
!= ExPath
) {
while ((*cp
!= '!') && (*cp
!= ':') && *cp
) {
if (*cp
!= ' ' && *cp
!= '\t') {
Parse_Error(PARSE_WARNING
, "Extra target ignored");
while (*cp
&& isspace (*cp
)) {
} while ((*line
!= '!') && (*line
!= ':') && *line
);
* Don't need the list of target names anymore...
Lst_Destroy(curTargs
, NOFREE
);
if (!Lst_IsEmpty(targets
)) {
Parse_Error(PARSE_WARNING
, "Special and mundane targets don't mix. Mundane ones ignored");
* These four create nodes on which to hang commands, so
* targets shouldn't be empty...
* Nothing special here -- targets can be empty if it wants.
* Have now parsed all the target names. Must parse the operator next. The
Parse_Error (PARSE_FATAL
, "Missing dependency operator");
cp
++; /* Advance beyond operator */
Lst_ForEach (targets
, ParseDoOp
, (ClientData
)&op
);
* Get to the first source
while (*cp
&& isspace (*cp
)) {
* Several special targets take different actions if present with no
* a .SUFFIXES line with no sources clears out all old suffixes
* a .PRECIOUS line makes all targets precious
* a .IGNORE line ignores errors for all targets
* a .SILENT line creates silence when making all targets
* a .PATH removes all directories from the search path(s).
Lst_ForEach(paths
, ParseClearPath
, (ClientData
)NULL
);
} else if (specType
== MFlags
) {
* Call on functions in main.c to deal with these arguments and
* set the initial character to a null-character so the loop to
* get sources won't get anything
Main_ParseArgLine (line
);
} else if (specType
== ExShell
) {
if (Job_ParseShell (line
) != SUCCESS
) {
Parse_Error (PARSE_FATAL
, "improper shell specification");
} else if ((specType
== NotParallel
) || (specType
== SingleShell
)) {
if ((specType
== Suffixes
) || (specType
== ExPath
) ||
(specType
== Includes
) || (specType
== Libs
) ||
* If the target was one that doesn't take files as its sources
* but takes something like suffixes, we take each
* space-separated word on the line as a something and deal
* If the target was .SUFFIXES, we take each source as a
* suffix and add it to the list of suffixes maintained by the
* If the target was a .PATH, we add the source as a directory
* to search on the search path.
* If it was .INCLUDES, the source is taken to be the suffix of
* files which will be #included and whose search path should
* be present in the .INCLUDES variable.
* If it was .LIBS, the source is taken to be the suffix of
* files which are considered libraries and whose search path
* should be present in the .LIBS variable.
* If it was .NULL, the source is the suffix to use when a file
while (*cp
&& !isspace (*cp
)) {
Lst_ForEach(paths
, ParseAddDir
, (ClientData
)line
);
while (*cp
&& isspace (*cp
)) {
Lst_Destroy(paths
, NOFREE
);
* The targets take real sources, so we must beware of archive
* specifications (i.e. things with left parentheses in them)
* and handle them accordingly.
while (*cp
&& !isspace (*cp
)) {
if ((*cp
== '(') && (cp
> line
) && (cp
[-1] != '$')) {
* Only stop for a left parenthesis if it isn't at the
* start of a word (that'll be for variable changes
* later) and isn't preceded by a dollar sign (a dynamic
sources
= Lst_Init (FALSE
);
if (Arch_ParseArchive (&line
, sources
, VAR_CMD
) != SUCCESS
) {
Parse_Error (PARSE_FATAL
,
"Error in source archive spec \"%s\"", line
);
while (!Lst_IsEmpty (sources
)) {
gn
= (GNode
*) Lst_DeQueue (sources
);
ParseDoSrc (tOp
, gn
->name
);
Lst_Destroy (sources
, NOFREE
);
while (*cp
&& isspace (*cp
)) {
if (mainNode
== NILGNODE
) {
* If we have yet to decide on a main target to make, in the
* absence of any user input, we want the first target on
* the first dependency line that is actually a real target
* (i.e. isn't a .USE or .EXEC rule) to be made.
Lst_ForEach (targets
, ParseFindMain
, (ClientData
)0);
*---------------------------------------------------------------------
* Return TRUE if the passed line is a variable assignment. A variable
* assignment consists of a single word followed by optional whitespace
* followed by either a += or an = operator.
* This function is used both by the Parse_File function and main when
* parsing the command-line arguments.
* TRUE if it is. FALSE if it ain't
*---------------------------------------------------------------------
register char *line
; /* the line to check */
register Boolean wasSpace
= FALSE
; /* set TRUE if found a space */
register Boolean haveName
= FALSE
; /* Set TRUE if have a variable name */
while ((*line
== ' ') || (*line
== '\t')) {
* end-of-line -- can't be a variable assignment.
} else if ((*line
== ' ') || (*line
== '\t')) {
* there can be as much white space as desired so long as there is
* only one word before the operator
} else if (wasSpace
&& haveName
) {
* Stop when an = operator is found.
if ((*line
== '+') || (*line
== ':') || (*line
== '?') ||
* This is the start of another word, so not assignment.
* A final check: if we stopped on a +, ?, ! or :, the next character must
* be an = or it ain't a valid assignment
*---------------------------------------------------------------------
* Take the variable assignment in the passed line and do it in the
* Note: There is a lexical ambiguity with assignment modifier characters
* in variable names. This routine interprets the character before the =
* as a modifier. Therefore, an assignment like
* is interpreted as "C+ +=" instead of "C++ =".
* the variable structure of the given variable name is altered in the
*---------------------------------------------------------------------
char *line
; /* a line guaranteed to be a variable
* assignment. This reduces error checks */
GNode
*ctxt
; /* Context in which to do the assignment */
char *cp
; /* pointer into line */
VAR_SUBST
, VAR_APPEND
, VAR_SHELL
, VAR_NORMAL
} type
; /* Type of assignment */
char *opc
; /* ptr to operator character to
* null-terminate the variable name */
* Avoid clobbered variable warnings by forcing the compiler
* to ``unregister'' variables
while ((*line
== ' ') || (*line
== '\t')) {
* Skip to operator character, nulling out whitespace as we go
for (cp
= line
+ 1; *cp
!= '='; cp
++) {
opc
= cp
-1; /* operator is the previous character */
*cp
++ = '\0'; /* nuke the = */
* If the variable already has a value, we don't do anything.
if (Var_Exists(line
, ctxt
)) {
if (type
== VAR_APPEND
) {
Var_Append (line
, cp
, ctxt
);
} else if (type
== VAR_SUBST
) {
* Allow variables in the old value to be undefined, but leave their
* invocation alone -- this is done by forcing oldVars to be false.
* XXX: This can cause recursive variables, but that's not hard to do,
* and this allows someone to do something like
* CFLAGS := -I.. $(CFLAGS)
Boolean oldOldVars
= oldVars
;
cp
= Var_Subst(NULL
, cp
, ctxt
, FALSE
);
} else if (type
== VAR_SHELL
) {
char *args
[4]; /* Args for invoking the shell */
int fds
[2]; /* Pipe streams */
int cpid
; /* Child PID */
int pid
; /* PID from wait() */
Boolean freeCmd
; /* TRUE if the command needs to be freed, i.e.
* if any variable expansion was performed */
* Avoid clobbered variable warnings by forcing the compiler
* to ``unregister'' variables
* Set up arguments for shell
if (strchr(cp
, '$') != (char *)NULL
) {
* There's a dollar sign in the command, so perform variable
* expansion on the whole thing. The resulting string will need
* freeing when we're done, so set freeCmd to TRUE.
args
[2] = Var_Subst(NULL
, cp
, VAR_CMD
, TRUE
);
* Open a pipe for fetching its output
* Close input side of pipe
* Duplicate the output stream to the shell's output, then
* shut the extra thing down. Note we don't fetch the error
* Couldn't fork -- tell the user and make the variable null
Parse_Error(PARSE_WARNING
, "Couldn't exec \"%s\"", cp
);
* No need for the writing half
buf
= Buf_Init (MAKE_BSIZE
);
cc
= read(fds
[0], result
, sizeof(result
));
Buf_AddBytes(buf
, cc
, (Byte
*) result
);
while (cc
> 0 || (cc
== -1 && errno
== EINTR
));
* Close the input side of the pipe.
* Wait for the process to exit.
while(((pid
= wait(&status
)) != cpid
) && (pid
>= 0))
res
= (char *)Buf_GetAll (buf
, &cc
);
Buf_Destroy (buf
, FALSE
);
* Couldn't read the child's output -- tell the user and
* set the variable to null
Parse_Error(PARSE_WARNING
, "Couldn't read shell's output");
* Child returned an error -- tell the user but still use
Parse_Error(PARSE_WARNING
, "\"%s\" returned non-zero", cp
);
* Null-terminate the result, convert newlines to spaces and
* install it in the variable.
* A final newline is just stripped
Var_Set(line
, res
, ctxt
);
* Normal assignment -- just do it.
Var_Set (line
, cp
, ctxt
);
* Lst_ForEach function to add a command line to all targets
* A new element is added to the commands list of the node.
ClientData gnp
; /* the node to which the command is to be added */
ClientData cmd
; /* the command to add */
GNode
*gn
= (GNode
*) gnp
;
/* if target already supplied, ignore commands */
if (!(gn
->type
& OP_HAS_COMMANDS
))
(void)Lst_AtEnd(gn
->commands
, cmd
);
*-----------------------------------------------------------------------
* Callback procedure for Parse_File when destroying the list of
* targets on the last dependency line. Marks a target as already
* having commands if it does, to keep from having shell commands
* on multiple dependency lines.
* OP_HAS_COMMANDS may be set for the target.
*-----------------------------------------------------------------------
ClientData gnp
; /* Node to examine */
GNode
*gn
= (GNode
*) gnp
;
if (!Lst_IsEmpty(gn
->commands
)) {
gn
->type
|= OP_HAS_COMMANDS
;
*-----------------------------------------------------------------------
* Add a directory to the path searched for included makefiles
* bracketed by double-quotes. Used by functions in main.c
* The directory is appended to the list.
*-----------------------------------------------------------------------
Parse_AddIncludeDir (dir
)
char *dir
; /* The name of the directory to add */
Dir_AddDir (parseIncPath
, dir
);
*---------------------------------------------------------------------
* The input is the line minus the #include. A file spec is a string
* enclosed in <> or "". The former is looked for only in sysIncPath.
* The latter in . and the directories specified by -I command line
* A structure is added to the includes Lst and readProc, lineno,
* fname and curFILE are altered for the new file
*---------------------------------------------------------------------
char *file
; /* file specification */
char *fullname
; /* full pathname of file */
IFile
*oldFile
; /* state associated with current file */
char endc
; /* the character which ends the file spec */
char *cp
; /* current position in file spec */
Boolean isSystem
; /* TRUE if makefile is a system makefile */
* Skip to delimiter character so we know where to look
while ((*file
== ' ') || (*file
== '\t')) {
if ((*file
!= '"') && (*file
!= '<')) {
Parse_Error (PARSE_FATAL
,
".include filename must be delimited by '\"' or '<'");
* Set the search path on which to find the include file based on the
* characters which bracket its name. Angle-brackets imply it's
* a system Makefile while double-quotes imply it's a user makefile
* Skip to matching delimiter
for (cp
= ++file
; *cp
&& *cp
!= endc
; cp
++) {
Parse_Error (PARSE_FATAL
,
"Unclosed %cinclude filename. '%c' expected",
* Substitute for any variables in the file name before trying to
file
= Var_Subst (NULL
, file
, VAR_CMD
, FALSE
);
* Now we know the file's name and its search path, we attempt to
* find the durn thing. A return of NULL indicates the file don't
* Include files contained in double-quotes are first searched for
* relative to the including file's location. We don't want to
* cd there, of course, so we just tack on the old file's
* leading path components and call Dir_FindFile to see if
* we can locate the beast.
prefEnd
= strrchr (fname
, '/');
if (prefEnd
!= (char *)NULL
) {
newName
= str_concat (fname
, file
, STR_ADDSLASH
);
fullname
= Dir_FindFile (newName
, parseIncPath
);
if (fullname
== (char *)NULL
) {
fullname
= Dir_FindFile(newName
, dirSearchPath
);
if (fullname
== (char *)NULL
) {
* System makefile or makefile wasn't found in same directory as
* included makefile. Search for it first on the -I search path,
* then on the .PATH search path, if not found in a -I directory.
fullname
= Dir_FindFile (file
, parseIncPath
);
if (fullname
== (char *)NULL
) {
fullname
= Dir_FindFile(file
, dirSearchPath
);
if (fullname
== (char *)NULL
) {
* Still haven't found the makefile. Look for it on the system
fullname
= Dir_FindFile(file
, sysIncPath
);
if (fullname
== (char *) NULL
) {
Parse_Error (PARSE_FATAL
, "Could not find %s", file
);
* Once we find the absolute path to the file, we get to save all the
* state from the current file before we can start reading this
* include file. The state is stored in an IFile structure which
* is placed on a list with other IFile structures. The list makes
* a very nice stack to track how we got here...
oldFile
= (IFile
*) emalloc (sizeof (IFile
));
oldFile
->lineno
= lineno
;
(void) Lst_AtFront (includes
, (ClientData
)oldFile
);
* Once the previous state has been saved, we can get down to reading
* the new file. We set up the name of the file to be the absolute
* name of the include file so error messages refer to the right
* place. Naturally enough, we start reading at line number 0.
curFILE
= fopen (fullname
, "r");
if (curFILE
== (FILE * ) NULL
) {
Parse_Error (PARSE_FATAL
, "Cannot open %s", fullname
);
*---------------------------------------------------------------------
* Start Parsing from the given string
* A structure is added to the includes Lst and readProc, lineno,
* fname and curFILE are altered for the new file
*---------------------------------------------------------------------
IFile
*oldFile
; /* state associated with this file */
(void) fprintf(stderr
, "%s\n----\n", str
);
oldFile
= (IFile
*) emalloc (sizeof (IFile
));
oldFile
->lineno
= lineno
;
(void) Lst_AtFront (includes
, (ClientData
)oldFile
);
curPTR
= (PTR
*) emalloc (sizeof (PTR
));
curPTR
->str
= curPTR
->ptr
= str
;
*---------------------------------------------------------------------
* ParseTraditionalInclude --
* The input is the line minus the "include". The file name is
* the string following the "include".
* A structure is added to the includes Lst and readProc, lineno,
* fname and curFILE are altered for the new file
*---------------------------------------------------------------------
ParseTraditionalInclude (file
)
char *file
; /* file specification */
char *fullname
; /* full pathname of file */
IFile
*oldFile
; /* state associated with current file */
char *cp
; /* current position in file spec */
while ((*file
== ' ') || (*file
== '\t')) {
Parse_Error (PARSE_FATAL
,
"Filename missing from \"include\"");
* Skip to end of line or next whitespace
for (cp
= file
; *cp
&& *cp
!= '\n' && *cp
!= '\t' && *cp
!= ' '; cp
++) {
* Substitute for any variables in the file name before trying to
file
= Var_Subst (NULL
, file
, VAR_CMD
, FALSE
);
* Now we know the file's name, we attempt to find the durn thing.
* A return of NULL indicates the file don't exist.
* Include files are first searched for relative to the including
* file's location. We don't want to cd there, of course, so we
* just tack on the old file's leading path components and call
* Dir_FindFile to see if we can locate the beast.
* XXX - this *does* search in the current directory, right?
prefEnd
= strrchr (fname
, '/');
if (prefEnd
!= (char *)NULL
) {
newName
= str_concat (fname
, file
, STR_ADDSLASH
);
fullname
= Dir_FindFile (newName
, parseIncPath
);
if (fullname
== (char *)NULL
) {
fullname
= Dir_FindFile(newName
, dirSearchPath
);
if (fullname
== (char *)NULL
) {
* System makefile or makefile wasn't found in same directory as
* included makefile. Search for it first on the -I search path,
* then on the .PATH search path, if not found in a -I directory.
fullname
= Dir_FindFile (file
, parseIncPath
);
if (fullname
== (char *)NULL
) {
fullname
= Dir_FindFile(file
, dirSearchPath
);
if (fullname
== (char *)NULL
) {
* Still haven't found the makefile. Look for it on the system
fullname
= Dir_FindFile(file
, sysIncPath
);
if (fullname
== (char *) NULL
) {
Parse_Error (PARSE_FATAL
, "Could not find %s", file
);
* Once we find the absolute path to the file, we get to save all the
* state from the current file before we can start reading this
* include file. The state is stored in an IFile structure which
* is placed on a list with other IFile structures. The list makes
* a very nice stack to track how we got here...
oldFile
= (IFile
*) emalloc (sizeof (IFile
));
oldFile
->lineno
= lineno
;
(void) Lst_AtFront (includes
, (ClientData
)oldFile
);
* Once the previous state has been saved, we can get down to reading
* the new file. We set up the name of the file to be the absolute
* name of the include file so error messages refer to the right
* place. Naturally enough, we start reading at line number 0.
curFILE
= fopen (fullname
, "r");
if (curFILE
== (FILE * ) NULL
) {
Parse_Error (PARSE_FATAL
, "Cannot open %s", fullname
);
*---------------------------------------------------------------------
* Called when EOF is reached in the current file. If we were reading
* an include file, the includes stack is popped and things set up
* to go back to reading the previous file at the previous location.
* CONTINUE if there's more to do. DONE if not.
* The old curFILE, is closed. The includes list is shortened.
* lineno, curFILE, and fname are changed if CONTINUE is returned.
*---------------------------------------------------------------------
IFile
*ifile
; /* the state on the top of the includes stack */
if (Lst_IsEmpty (includes
)) {
ifile
= (IFile
*) Lst_DeQueue (includes
);
free((Address
) curPTR
->str
);
*---------------------------------------------------------------------
* Read a character from the current file
* The character that was read
*---------------------------------------------------------------------
if (curPTR
&& *curPTR
->ptr
)
*---------------------------------------------------------------------
* Put back a character to the current file
*---------------------------------------------------------------------
int skip
; /* Skip lines that don't start with . */
int c
, lastc
= '\0', lineLength
;
* Skip lines until get to one that begins with a
while ((c
!= '.') && (c
!= EOF
)) {
while (((c
!= '\n') || (lastc
== '\\')) && (c
!= EOF
))
* Advance to next unescaped newline
if ((lastc
= c
) == '\n') {
Parse_Error (PARSE_FATAL
, "Unclosed conditional/for loop");
* Read the entire line into buf
buf
= Buf_Init (MAKE_BSIZE
);
Buf_AddByte (buf
, (Byte
)c
);
} while ((c
!= '\n') && (c
!= EOF
));
Buf_AddByte (buf
, (Byte
)'\0');
line
= (char *)Buf_GetAll (buf
, &lineLength
);
Buf_Destroy (buf
, FALSE
);
*---------------------------------------------------------------------
* Read an entire line from the input file. Called only by Parse_File.
* To facilitate escaped newlines and what have you, a character is
* buffered in 'lastc', which is '\0' when no characters have been
* read. When we break out of the loop, c holds the terminating
* character and lastc holds a character that should be added to
* the line (unless we don't read anything but a terminator).
* Only those associated with reading a character
*---------------------------------------------------------------------
Buffer buf
; /* Buffer for current line */
register int c
; /* the current character */
register int lastc
; /* The most-recent character */
Boolean semiNL
; /* treat semi-colons as newlines */
Boolean ignDepOp
; /* TRUE if should ignore dependency operators
* for the purposes of setting semiNL */
Boolean ignComment
; /* TRUE if should ignore comments (in a
char *ep
; /* to strip trailing blanks */
int lineLength
; /* Length of result */
* Handle special-characters at the beginning of the line. Either a
* leading tab (shell command) or pound-sign (possible conditional)
* forces us to ignore comments and dependency operators and treat
* semi-colons as semi-colons (by leaving semiNL FALSE). This also
* discards completely blank lines.
ignComment
= ignDepOp
= TRUE
;
* Anything else breaks out without doing anything
buf
= Buf_Init(MAKE_BSIZE
);
while (((c
= ParseReadc ()) != '\n' || (lastc
== '\\')) &&
* Escaped newline: read characters until a non-space or an
* unescaped newline and replace them all by a single space.
* This is done by storing the space over the backslash and
* dropping through with the next nonspace. If it is a
* semi-colon and semiNL is TRUE, it will be recognized as a
* newline in the code below this...
while ((c
= ParseReadc ()) == ' ' || c
== '\t') {
if (c
== EOF
|| c
== '\n') {
* Check for comments, semiNL's, etc. -- easier than
* ParseUnreadc(c); continue;
* Semi-colon: Need to see if it should be interpreted as a
* To make sure the command that may be following this
* semi-colon begins with a tab, we push one back into the
* input stream. This will overwrite the semi-colon in the
* buffer. If there is no command following, this does no
* harm, since the newline remains in the buffer and the
* Haven't seen a dependency operator before this, so this
* must be a variable assignment -- don't pay attention to
* dependency operators after this.
} else if (lastc
== ':' || lastc
== '!') {
* Well, we've seen a dependency operator already, but it
* was the previous character, so this is really just an
* expanded variable assignment. Revert semi-colons to
* being just semi-colons again and ignore any more
* XXX: Note that a line like "foo : a:=b" will blow up,
* but who'd write a line like that anyway?
ignDepOp
= TRUE
; semiNL
= FALSE
;
if (compatMake
&& (lastc
!= '\\')) {
* If the character is a hash mark and it isn't escaped
* (or we're being compatible), the thing is a comment.
* Skip to the end of the line.
} while ((c
!= '\n') && (c
!= EOF
));
* Don't add the backslash. Just let the # get copied
if (!ignDepOp
&& (c
== ':' || c
== '!')) {
* A semi-colon is recognized as a newline only on
* dependency lines. Dependency lines are lines with a
* colon or an exclamation point. Ergo...
* Copy in the previous character and save this one in lastc.
Buf_AddByte (buf
, (Byte
)lastc
);
Buf_AddByte (buf
, (Byte
)lastc
);
Buf_AddByte (buf
, (Byte
)'\0');
line
= (char *)Buf_GetAll (buf
, &lineLength
);
Buf_Destroy (buf
, FALSE
);
* Strip trailing blanks and tabs from the line.
* Do not strip a blank or tab that is preceeded by
while (ep
> line
&& (ep
[-1] == ' ' || ep
[-1] == '\t')) {
if (ep
> line
+ 1 && ep
[-2] == '\\')
* The line might be a conditional. Ask the conditional module
* about it and act accordingly
switch (Cond_Eval (line
)) {
* Skip to next conditional that evaluates to COND_PARSE.
} while (line
&& Cond_Eval(line
) != COND_PARSE
);
* Skip after the matching end
Parse_Error (PARSE_FATAL
,
"Unexpected end of file in for loop.\n");
* Hit end-of-file, so return a NULL line to indicate this.
*-----------------------------------------------------------------------
* Handle the end of a dependency group.
* inLine set FALSE. 'targets' list destroyed.
*-----------------------------------------------------------------------
Lst_ForEach(targets
, Suff_EndTransform
, (ClientData
)NULL
);
Lst_Destroy (targets
, ParseHasCommands
);
*---------------------------------------------------------------------
* Parse a file into its component parts, incorporating it into the
* current dependency graph. This is the main function and controls
* almost every other function in this module
* Loads. Nodes are added to the list of all targets, nodes and links
* are added to the dependency graph. etc. etc. etc.
*---------------------------------------------------------------------
char *name
; /* the name of the file being read */
FILE * stream
; /* Stream open to makefile to parse */
register char *cp
, /* pointer into the line */
*line
; /* the line we're working on */
while ((line
= ParseReadLine ()) != NULL
) {
* Lines that begin with the special character are either
* include or undef directives.
for (cp
= line
+ 1; isspace (*cp
); cp
++) {
if (strncmp (cp
, "include", 7) == 0) {
} else if (strncmp(cp
, "undef", 5) == 0) {
for (cp
+= 5; isspace((unsigned char) *cp
); cp
++) {
for (cp2
= cp
; !isspace((unsigned char) *cp2
) &&
Var_Delete(cp
, VAR_GLOBAL
);
if (*line
== '#' || *line
== '\0') {
/* If we're this far, the line must be a comment.
(Empty lines are ignored as well) */
* If a line starts with a tab, it can only hope to be
for (cp
= line
+ 1; isspace (*cp
); cp
++) {
* So long as it's not a blank line and we're actually
* in a dependency spec, add the command to the list of
* commands of all targets in the dependency spec
Lst_ForEach (targets
, ParseAddCmd
, cp
);
Lst_AtEnd(targCmds
, (ClientData
) line
);
Parse_Error (PARSE_FATAL
,
"Unassociated shell command \"%.20s\"",
} else if (strncmp (line
, "include", 7) == 0 &&
strchr(line
, ':') == NULL
) {
* It's an S3/S5-style "include".
ParseTraditionalInclude (line
+ 7);
} else if (Parse_IsVar (line
)) {
Parse_DoVar (line
, VAR_GLOBAL
);
* We now know it's a dependency line so it needs to have all
* variables expanded before being parsed. Tell the variable
* module to complain if some variable is undefined...
* To make life easier on novices, if the line is indented we
* first make sure the line has a dependency operator in it.
* If it doesn't have an operator and we're in a dependency
* line's script, we assume it's actually a shell command
* and add it to the current list of targets.
Boolean nonSpace
= FALSE
;
if (isspace((unsigned char) line
[0])) {
while ((*cp
!= '\0') && isspace((unsigned char) *cp
)) {
while ((*cp
!= ':') && (*cp
!= '!') && (*cp
!= '\0')) {
Parse_Error (PARSE_WARNING
,
"Shell command needs a leading tab");
Parse_Error (PARSE_FATAL
, "Missing operator");
cp
= Var_Subst (NULL
, line
, VAR_CMD
, TRUE
);
* Need a non-circular list for the target nodes
Lst_Destroy(targets
, NOFREE
);
targets
= Lst_Init (FALSE
);
ParseDoDependency (line
);
* Reached EOF, but it may be just EOF of an include file...
} while (ParseEOF(1) == CONTINUE
);
* Make sure conditionals are clean
fprintf (stderr
, "Fatal errors encountered -- cannot continue\n");
*---------------------------------------------------------------------
* initialize the parsing module
* the parseIncPath list is initialized...
*---------------------------------------------------------------------
/* avoid faults on read-only strings */
static char syspath
[] = _PATH_DEFSYSPATH
;
parseIncPath
= Lst_Init (FALSE
);
sysIncPath
= Lst_Init (FALSE
);
includes
= Lst_Init (FALSE
);
targCmds
= Lst_Init (FALSE
);
* Add the directories from the DEFSYSPATH (more than one may be given
* as dir1:...:dirn) to the system include path.
for (start
= syspath
; *start
!= '\0'; start
= cp
) {
for (cp
= start
; *cp
!= '\0' && *cp
!= ':'; cp
++)
Dir_AddDir(sysIncPath
, start
);
Dir_AddDir(sysIncPath
, start
);
Lst_Destroy(targCmds
, (void (*) __P((ClientData
))) free
);
Lst_Destroy(targets
, NOFREE
);
Lst_Destroy(sysIncPath
, Dir_Destroy
);
Lst_Destroy(parseIncPath
, Dir_Destroy
);
Lst_Destroy(includes
, NOFREE
); /* Should be empty now */
*-----------------------------------------------------------------------
* Return a Lst of the main target to create for main()'s sake. If
* no such target exists, we Punt with an obnoxious error message.
* A Lst of the single node to create.
*-----------------------------------------------------------------------
Lst main
; /* result list */
if (mainNode
== NILGNODE
) {
Punt ("make: no target to make.\n");
} else if (mainNode
->type
& OP_DOUBLEDEP
) {
(void) Lst_AtEnd (main
, (ClientData
)mainNode
);
Lst_Concat(main
, mainNode
->cohorts
, LST_CONCNEW
);
(void) Lst_AtEnd (main
, (ClientData
)mainNode
);