2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1989 by Berkeley Softworks
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)parse.c 8.3 (Berkeley) 3/19/94
39 * $FreeBSD: src/usr.bin/make/parse.c,v 1.75 2005/02/07 11:27:47 harti Exp $
40 * $DragonFly: src/usr.bin/make/parse.c,v 1.49 2005/03/02 19:39:51 okumoto Exp $
45 * Functions to parse a makefile.
47 * One function, Parse_Init, must be called before any functions
48 * in this module are used. After that, the function Parse_File is the
49 * main entry point and controls most of the other functions in this
52 * Most important structures are kept in Lsts. Directories for
53 * the #include "..." function are kept in the 'parseIncPath' Lst, while
54 * those for the #include <...> are kept in the 'sysIncPath' Lst. The
55 * targets currently being defined are kept in the 'targets' Lst.
57 * The variables 'curFile.fname' and 'curFile.lineno' are used to track
58 * the name of the current file and the line number in that file so that
59 * error messages can be more meaningful.
62 * Parse_Init Initialization function which must be
63 * called before anything else in this module
66 * Parse_File Function used to parse a makefile. It must
67 * be given the name of the file, which should
68 * already have been opened, and a function
69 * to call to read a character from the file.
71 * Parse_IsVar Returns TRUE if the given line is a
72 * variable assignment. Used by MainParseArgs
73 * to determine if an argument is a target
74 * or a variable assignment. Used internally
75 * for pretty much the same thing...
77 * Parse_Error Function called when an error occurs in
78 * parsing. Used by the variable and
79 * conditional modules.
80 * Parse_MainName Returns a Lst of the main target to create.
101 #include "pathnames.h"
109 * These values are returned by ParseEOF to tell Parse_File whether to
110 * CONTINUE parsing, i.e. it had only reached the end of an include file,
116 /* targets we're working on */
117 static Lst targets = Lst_Initializer(targets);
119 static Boolean inLine; /* true if currently in a dependency
120 * line or its commands */
121 static int fatals = 0;
123 static GNode *mainNode; /* The main target to create. This is the
124 * first target on the first dependency
125 * line in the first makefile */
127 IFile curFile; /* current makefile */
129 /* stack of IFiles generated by * #includes */
130 static Lst includes = Lst_Initializer(includes);
132 /* list of directories for "..." includes */
133 Lst parseIncPath = Lst_Initializer(parseIncPath);
135 /* list of directories for <...> includes */
136 Lst sysIncPath = Lst_Initializer(sysIncPath);
139 * specType contains the SPECial TYPE of the current target. It is
140 * Not if the target is unspecial. If it *is* special, however, the children
141 * are linked as children of the parent but not vice versa. This variable is
142 * set in ParseDoDependency
146 Default, /* .DEFAULT */
148 Ignore, /* .IGNORE */
149 Includes, /* .INCLUDES */
150 Interrupt, /* .INTERRUPT */
152 MFlags, /* .MFLAGS or .MAKEFLAGS */
153 Main, /* .MAIN and we don't have anything user-specified to
155 NoExport, /* .NOEXPORT */
156 Not, /* Not special */
157 NotParallel, /* .NOTPARALELL */
160 Parallel, /* .PARALLEL */
164 Precious, /* .PRECIOUS */
165 ExShell, /* .SHELL */
166 Silent, /* .SILENT */
167 SingleShell, /* .SINGLESHELL */
168 Suffixes, /* .SUFFIXES */
170 Attribute /* Generic attribute */
173 static ParseSpecial specType;
177 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
178 * seen, then set to each successive source on the line.
180 static GNode *predecessor;
183 * The parseKeywords table is searched using binary search when deciding
184 * if a target or source is special. The 'spec' field is the ParseSpecial
185 * type of the keyword ("Not" if the keyword isn't special as a target) while
186 * the 'op' field is the operator to apply to the list of targets if the
187 * keyword is used as a source ("0" if the keyword isn't special as a source)
190 const char *name; /* Name of keyword */
191 ParseSpecial spec; /* Type when used as a target */
192 int op; /* Operator when used as a source */
193 } parseKeywords[] = {
194 { ".BEGIN", Begin, 0 },
195 { ".DEFAULT", Default, 0 },
197 { ".EXEC", Attribute, OP_EXEC },
198 { ".IGNORE", Ignore, OP_IGNORE },
199 { ".INCLUDES", Includes, 0 },
200 { ".INTERRUPT", Interrupt, 0 },
201 { ".INVISIBLE", Attribute, OP_INVISIBLE },
202 { ".JOIN", Attribute, OP_JOIN },
203 { ".LIBS", Libs, 0 },
204 { ".MAIN", Main, 0 },
205 { ".MAKE", Attribute, OP_MAKE },
206 { ".MAKEFLAGS", MFlags, 0 },
207 { ".MFLAGS", MFlags, 0 },
208 { ".NOTMAIN", Attribute, OP_NOTMAIN },
209 { ".NOTPARALLEL", NotParallel, 0 },
210 { ".NO_PARALLEL", NotParallel, 0 },
211 { ".NULL", Null, 0 },
212 { ".OPTIONAL", Attribute, OP_OPTIONAL },
213 { ".ORDER", Order, 0 },
214 { ".PARALLEL", Parallel, 0 },
215 { ".PATH", ExPath, 0 },
216 { ".PHONY", Phony, OP_PHONY },
217 { ".POSIX", Posix, 0 },
218 { ".PRECIOUS", Precious, OP_PRECIOUS },
219 { ".RECURSIVE", Attribute, OP_MAKE },
220 { ".SHELL", ExShell, 0 },
221 { ".SILENT", Silent, OP_SILENT },
222 { ".SINGLESHELL", SingleShell, 0 },
223 { ".SUFFIXES", Suffixes, 0 },
224 { ".USE", Attribute, OP_USE },
225 { ".WAIT", Wait, 0 },
228 static int ParseFindKeyword(char *);
229 static int ParseDoOp(void *, void *);
230 static int ParseAddDep(void *, void *);
231 static void ParseDoSrc(int, char *, Lst *);
232 static int ParseFindMain(void *, void *);
233 static int ParseAddDir(void *, void *);
234 static int ParseClearPath(void *, void *);
235 static void ParseDoDependency(char *);
236 static int ParseAddCmd(void *, void *);
237 static int ParseReadc(void);
238 static void ParseUnreadc(int);
239 static void ParseHasCommands(void *);
240 static void ParseDoInclude(char *);
241 static void ParseDoError(char *);
242 static void ParseDoWarning(char *);
244 static void ParseTraditionalInclude(char *);
246 static int ParseEOF(int);
247 static char *ParseReadLine(void);
248 static char *ParseSkipLine(int, int);
249 static void ParseFinishLine(void);
252 *----------------------------------------------------------------------
253 * ParseFindKeyword --
254 * Look in the table of keywords for one matching the given string.
257 * The index of the keyword, or -1 if it isn't there.
261 *----------------------------------------------------------------------
264 ParseFindKeyword(char *str)
272 end = (sizeof(parseKeywords) / sizeof(parseKeywords[0])) - 1;
275 cur = start + ((end - start) / 2);
276 diff = strcmp(str, parseKeywords[cur].name);
280 } else if (diff < 0) {
285 } while (start <= end);
291 * Error message abort function for parsing. Prints out the context
292 * of the error (line number and file) as well as the message with
293 * two optional arguments.
299 * "fatals" is incremented if the level is PARSE_FATAL.
303 Parse_Error(int type, const char *fmt, ...)
308 fprintf(stderr, "\"%s\", line %d: ",
309 curFile.fname, curFile.lineno);
310 if (type == PARSE_WARNING)
311 fprintf(stderr, "warning: ");
312 vfprintf(stderr, fmt, ap);
314 fprintf(stderr, "\n");
316 if (type == PARSE_FATAL)
321 *---------------------------------------------------------------------
323 * Link the parent node to its new child. Used in a Lst_ForEach by
324 * ParseDoDependency. If the specType isn't 'Not', the parent
325 * isn't linked as a parent of the child.
331 * New elements are added to the parents list of cgn and the
332 * children list of cgn. the unmade field of pgn is updated
333 * to reflect the additional child.
334 *---------------------------------------------------------------------
337 ParseLinkSrc(Lst *parents, GNode *cgn)
342 LST_FOREACH(ln, parents) {
344 if (Lst_Member(&pgn->children, cgn) == NULL) {
345 Lst_AtEnd(&pgn->children, cgn);
346 if (specType == Not) {
347 Lst_AtEnd(&cgn->parents, pgn);
355 *---------------------------------------------------------------------
357 * Apply the parsed operator to the given target node. Used in a
358 * Lst_ForEach call by ParseDoDependency once all targets have
359 * been found and their operator parsed. If the previous and new
360 * operators are incompatible, a major error is taken.
366 * The type field of the node is altered to reflect any new bits in
368 *---------------------------------------------------------------------
371 ParseDoOp(void *gnp, void *opp)
374 int op = *(int *)opp;
377 * If the dependency mask of the operator and the node don't match and
378 * the node has actually had an operator applied to it before, and
379 * the operator actually has some dependency information in it, complain.
381 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
382 !OP_NOP(gn->type) && !OP_NOP(op))
384 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
388 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
390 * If the node was the object of a :: operator, we need to create a
391 * new instance of it for the children and commands on this dependency
392 * line. The new instance is placed on the 'cohorts' list of the
393 * initial one (note the initial one is not on its own cohorts list)
394 * and the new instance is linked to all parents of the initial
400 cohort = Targ_NewGN(gn->name);
402 * Duplicate links to parents so graph traversal is simple. Perhaps
403 * some type bits should be duplicated?
405 * Make the cohort invisible as well to avoid duplicating it into
406 * other variables. True, parents of this target won't tend to do
407 * anything with their local variables, but better safe than
410 ParseLinkSrc(&gn->parents, cohort);
411 cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
412 Lst_AtEnd(&gn->cohorts, cohort);
415 * Replace the node in the targets list with the new copy
417 ln = Lst_Member(&targets, gn);
418 Lst_Replace(ln, cohort);
422 * We don't want to nuke any previous flags (whatever they were) so we
423 * just OR the new operator into the old
431 *---------------------------------------------------------------------
433 * Check if the pair of GNodes given needs to be synchronized.
434 * This has to be when two nodes are on different sides of a
438 * Returns 1 if the two targets need to be ordered, 0 otherwise.
439 * If it returns 1, the search can stop
442 * A dependency can be added between the two nodes.
444 *---------------------------------------------------------------------
447 ParseAddDep(void *pp, void *sp)
452 if (p->order < s->order) {
454 * XXX: This can cause loops, and loops can cause unmade targets,
455 * but checking is tedious, and the debugging output can show the
458 Lst_AtEnd(&p->successors, s);
459 Lst_AtEnd(&s->preds, p);
468 *---------------------------------------------------------------------
470 * Given the name of a source, figure out if it is an attribute
471 * and apply it to the targets if it is. Else decide if there is
472 * some attribute which should be applied *to* the source because
473 * of some special target and apply it if so. Otherwise, make the
474 * source be a child of the targets in the list 'targets'
480 * Operator bits may be added to the list of targets or to the source.
481 * The targets may have a new source added to their lists of children.
482 *---------------------------------------------------------------------
485 ParseDoSrc(int tOp, char *src, Lst *allsrc)
489 if (*src == '.' && isupper ((unsigned char) src[1])) {
490 int keywd = ParseFindKeyword(src);
492 int op = parseKeywords[keywd].op;
494 Lst_ForEach(&targets, ParseDoOp, &op);
497 if (parseKeywords[keywd].spec == Wait) {
507 * If we have noted the existence of a .MAIN, it means we need
508 * to add the sources of said target to the list of things
509 * to create. The string 'src' is likely to be free, so we
510 * must make a new copy of it. Note that this will only be
511 * invoked if the user didn't specify a target on the command
512 * line. This is to allow #ifmake's to succeed, or something...
514 Lst_AtEnd(&create, estrdup(src));
516 * Add the name to the .TARGETS variable as well, so the user cna
517 * employ that, if desired.
519 Var_Append(".TARGETS", src, VAR_GLOBAL);
524 * Create proper predecessor/successor links between the previous
525 * source and the current one.
527 gn = Targ_FindNode(src, TARG_CREATE);
528 if (predecessor != NULL) {
529 Lst_AtEnd(&predecessor->successors, gn);
530 Lst_AtEnd(&gn->preds, predecessor);
533 * The current source now becomes the predecessor for the next one.
540 * If the source is not an attribute, we need to find/create
541 * a node for it. After that we can apply any operator to it
542 * from a special target or link it to its parents, as
545 * In the case of a source that was the object of a :: operator,
546 * the attribute is applied to all of its instances (as kept in
547 * the 'cohorts' list of the node) or all the cohorts are linked
548 * to all the targets.
550 gn = Targ_FindNode(src, TARG_CREATE);
554 ParseLinkSrc(&targets, gn);
556 if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
560 for (ln = Lst_First(&gn->cohorts); ln != NULL; ln = Lst_Succ(ln)) {
561 cohort = Lst_Datum(ln);
565 ParseLinkSrc(&targets, cohort);
573 Lst_AtEnd(allsrc, gn);
575 Lst_ForEach(allsrc, ParseAddDep, gn);
580 *-----------------------------------------------------------------------
582 * Find a real target in the list and set it to be the main one.
583 * Called by ParseDoDependency when a main target hasn't been found
587 * 0 if main not found yet, 1 if it is.
590 * mainNode is changed and Targ_SetMain is called.
592 *-----------------------------------------------------------------------
595 ParseFindMain(void *gnp, void *dummy __unused)
599 if ((gn->type & (OP_NOTMAIN | OP_USE | OP_EXEC | OP_TRANSFORM)) == 0) {
609 *-----------------------------------------------------------------------
611 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
619 *-----------------------------------------------------------------------
622 ParseAddDir(void *path, void *name)
625 Dir_AddDir(path, name);
630 *-----------------------------------------------------------------------
632 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
640 *-----------------------------------------------------------------------
643 ParseClearPath(void *path, void *dummy __unused)
651 *---------------------------------------------------------------------
652 * ParseDoDependency --
653 * Parse the dependency line in line.
659 * The nodes of the sources are linked as children to the nodes of the
660 * targets. Some nodes may be created.
662 * We parse a dependency line by first extracting words from the line and
663 * finding nodes in the list of all targets with that name. This is done
664 * until a character is encountered which is an operator character. Currently
665 * these are only ! and :. At this point the operator is parsed and the
666 * pointer into the line advanced until the first source is encountered.
667 * The parsed operator is applied to each node in the 'targets' list,
668 * which is where the nodes found for the targets are kept, by means of
669 * the ParseDoOp function.
670 * The sources are read in much the same way as the targets were except
671 * that now they are expanded using the wildcarding scheme of the C-Shell
672 * and all instances of the resulting words in the list of all targets
673 * are found. Each of the resulting nodes is then linked to each of the
674 * targets as one of its children.
675 * Certain targets are handled specially. These are the ones detailed
676 * by the specType variable.
677 * The storing of transformation rules is also taken care of here.
678 * A target is recognized as a transformation rule by calling
679 * Suff_IsTransform. If it is a transformation rule, its node is gotten
680 * from the suffix module via Suff_AddTransform rather than the standard
681 * Targ_FindNode in the target module.
682 *---------------------------------------------------------------------
685 ParseDoDependency(char *line)
687 char *cp; /* our current position */
688 GNode *gn; /* a general purpose temporary node */
689 int op; /* the operator on the line */
690 char savec; /* a place to save a character */
691 Lst paths; /* Search paths to alter when parsing a list of .PATH targets */
692 int tOp; /* operator from special target */
702 *cp && !isspace((unsigned char)*cp) && *cp != '(';
707 * Must be a dynamic source (would have been expanded
708 * otherwise), so call the Var module to parse the puppy
709 * so we can safely advance beyond it...There should be
710 * no errors in this, as they would have been discovered
711 * in the initial Var_Subst and we wouldn't be here.
717 result = Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
723 } else if (*cp == '!' || *cp == ':') {
725 * We don't want to end a word on ':' or '!' if there is a
726 * better match later on in the string (greedy matching).
727 * This allows the user to have targets like:
730 * where "fie::fi:fo" and "foo::bar" are the targets. In
731 * real life this is used for perl5 library man pages where
732 * "::" separates an object from its class.
733 * Ie: "File::Spec::Unix". This behaviour is also consistent
734 * with other versions of make.
738 if (*cp == ':' && *p == ':')
741 /* Found the best match already. */
742 if (*p == '\0' || isspace(*p))
745 p += strcspn(p, "!:");
747 /* No better match later on... */
755 * Archives must be handled specially to make sure the OP_ARCHV
756 * flag is set in their 'type' field, for one thing, and because
757 * things like "archive(file1.o file2.o file3.o)" are permissible.
758 * Arch_ParseArchive will set 'line' to be the first non-blank
759 * after the archive-spec. It creates/finds nodes for the members
760 * and places them on the given list, returning SUCCESS if all
761 * went well and FAILURE if there was an error in the
762 * specification. On error, line should remain untouched.
764 if (Arch_ParseArchive(&line, &targets, VAR_CMD) != SUCCESS) {
765 Parse_Error(PARSE_FATAL,
766 "Error in archive specification: \"%s\"", line);
776 * Ending a dependency line without an operator is a Bozo
777 * no-no. As a heuristic, this is also often triggered by
778 * undetected conflicts from cvs/rcs merges.
780 if ((strncmp(line, "<<<<<<", 6) == 0) ||
781 (strncmp(line, "======", 6) == 0) ||
782 (strncmp(line, ">>>>>>", 6) == 0))
783 Parse_Error(PARSE_FATAL,
784 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
786 Parse_Error(PARSE_FATAL, "Need an operator");
791 * Have a word in line. See if it's a special target and set
792 * specType to match it.
794 if (*line == '.' && isupper((unsigned char)line[1])) {
796 * See if the target is a special target that must have it
797 * or its sources handled specially.
799 int keywd = ParseFindKeyword(line);
801 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
802 Parse_Error(PARSE_FATAL, "Mismatched special targets");
806 specType = parseKeywords[keywd].spec;
807 tOp = parseKeywords[keywd].op;
810 * Certain special targets have special semantics:
811 * .PATH Have to set the dirSearchPath
813 * .MAIN Its sources are only used if
814 * nothing has been specified to
816 * .DEFAULT Need to create a node to hang
817 * commands on, but we don't want
818 * it in the graph, nor do we want
819 * it to be the Main Target, so we
820 * create it, set OP_NOTMAIN and
821 * add it to the list, setting
822 * DEFAULT to the new node for
823 * later use. We claim the node is
824 * A transformation rule to make
825 * life easier later, when we'll
826 * use Make_HandleUse to actually
827 * apply the .DEFAULT commands.
828 * .PHONY The list of targets
831 * .INTERRUPT Are not to be considered the
833 * .NOTPARALLEL Make only one target at a time.
834 * .SINGLESHELL Create a shell for each command.
835 * .ORDER Must set initial predecessor to NULL
839 Lst_AtEnd(&paths, &dirSearchPath);
842 if (!Lst_IsEmpty(&create)) {
849 gn = Targ_FindNode(line, TARG_CREATE);
850 gn->type |= OP_NOTMAIN;
851 Lst_AtEnd(&targets, gn);
854 gn = Targ_NewGN(".DEFAULT");
855 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
856 Lst_AtEnd(&targets, gn);
873 } else if (strncmp(line, ".PATH", 5) == 0) {
875 * .PATH<suffix> has to be handled specially.
876 * Call on the suffix module to give us a path to
882 path = Suff_GetPath(&line[5]);
884 Parse_Error(PARSE_FATAL,
885 "Suffix '%s' not defined (yet)",
889 Lst_AtEnd(&paths, path);
894 * Have word in line. Get or create its node and stick it at
895 * the end of the targets list
897 if ((specType == Not) && (*line != '\0')) {
898 /* target names to be found and added to targets list */
899 Lst curTargs = Lst_Initializer(curTargs);
901 if (Dir_HasWildcards(line)) {
903 * Targets are to be sought only in the current directory,
904 * so create an empty path for the thing. Note we need to
905 * use Dir_Destroy in the destruction of the path as the
906 * Dir module could have added a directory to the path...
908 Lst emptyPath = Lst_Initializer(emptyPath);
910 Dir_Expand(line, &emptyPath, &curTargs);
912 Lst_Destroy(&emptyPath, Dir_Destroy);
915 * No wildcards, but we want to avoid code duplication,
916 * so create a list with the word on it.
918 Lst_AtEnd(&curTargs, line);
921 while (!Lst_IsEmpty(&curTargs)) {
922 char *targName = Lst_DeQueue(&curTargs);
924 if (!Suff_IsTransform (targName)) {
925 gn = Targ_FindNode(targName, TARG_CREATE);
927 gn = Suff_AddTransform(targName);
930 Lst_AtEnd(&targets, gn);
932 } else if (specType == ExPath && *line != '.' && *line != '\0') {
933 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
938 * If it is a special type and not .PATH, it's the only target we
939 * allow on this line...
941 if (specType != Not && specType != ExPath) {
942 Boolean warnFlag = FALSE;
944 while ((*cp != '!') && (*cp != ':') && *cp) {
945 if (*cp != ' ' && *cp != '\t') {
951 Parse_Error(PARSE_WARNING, "Extra target ignored");
954 while (*cp && isspace((unsigned char)*cp)) {
959 } while ((*line != '!') && (*line != ':') && *line);
961 if (!Lst_IsEmpty(&targets)) {
964 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
971 * These four create nodes on which to hang commands, so
972 * targets shouldn't be empty...
976 * Nothing special here -- targets can be empty if it wants.
983 * Have now parsed all the target names. Must parse the operator next. The
984 * result is left in op .
988 } else if (*cp == ':') {
996 Parse_Error(PARSE_FATAL, "Missing dependency operator");
1000 cp++; /* Advance beyond operator */
1002 Lst_ForEach(&targets, ParseDoOp, &op);
1005 * Get to the first source
1007 while (*cp && isspace((unsigned char)*cp)) {
1013 * Several special targets take different actions if present with no
1015 * a .SUFFIXES line with no sources clears out all old suffixes
1016 * a .PRECIOUS line makes all targets precious
1017 * a .IGNORE line ignores errors for all targets
1018 * a .SILENT line creates silence when making all targets
1019 * a .PATH removes all directories from the search path(s).
1024 Suff_ClearSuffixes();
1030 ignoreErrors = TRUE;
1036 Lst_ForEach(&paths, ParseClearPath, NULL);
1039 Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1044 } else if (specType == MFlags) {
1046 * Call on functions in main.c to deal with these arguments and
1047 * set the initial character to a null-character so the loop to
1048 * get sources won't get anything
1050 Main_ParseArgLine(line, 0);
1052 } else if (specType == ExShell) {
1053 if (Job_ParseShell(line) != SUCCESS) {
1054 Parse_Error(PARSE_FATAL, "improper shell specification");
1058 } else if ((specType == NotParallel) || (specType == SingleShell)) {
1063 * NOW GO FOR THE SOURCES
1065 if ((specType == Suffixes) || (specType == ExPath) ||
1066 (specType == Includes) || (specType == Libs) ||
1071 * If the target was one that doesn't take files as its sources
1072 * but takes something like suffixes, we take each
1073 * space-separated word on the line as a something and deal
1074 * with it accordingly.
1076 * If the target was .SUFFIXES, we take each source as a
1077 * suffix and add it to the list of suffixes maintained by the
1080 * If the target was a .PATH, we add the source as a directory
1081 * to search on the search path.
1083 * If it was .INCLUDES, the source is taken to be the suffix of
1084 * files which will be #included and whose search path should
1085 * be present in the .INCLUDES variable.
1087 * If it was .LIBS, the source is taken to be the suffix of
1088 * files which are considered libraries and whose search path
1089 * should be present in the .LIBS variable.
1091 * If it was .NULL, the source is the suffix to use when a file
1092 * has no valid suffix.
1095 while (*cp && !isspace((unsigned char)*cp)) {
1102 Suff_AddSuffix(line);
1105 Lst_ForEach(&paths, ParseAddDir, line);
1108 Suff_AddInclude(line);
1120 if (savech != '\0') {
1123 while (*cp && isspace((unsigned char)*cp)) {
1128 Lst_Destroy(&paths, NOFREE);
1131 Lst curSrcs = Lst_Initializer(curSrc); /* list of sources in order */
1135 * The targets take real sources, so we must beware of archive
1136 * specifications (i.e. things with left parentheses in them)
1137 * and handle them accordingly.
1139 while (*cp && !isspace((unsigned char)*cp)) {
1140 if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) {
1142 * Only stop for a left parenthesis if it isn't at the
1143 * start of a word (that'll be for variable changes
1144 * later) and isn't preceded by a dollar sign (a dynamic
1155 /* list of archive source names after expansion */
1156 Lst sources = Lst_Initializer(sources);
1158 if (Arch_ParseArchive(&line, &sources, VAR_CMD) != SUCCESS) {
1159 Parse_Error(PARSE_FATAL,
1160 "Error in source archive spec \"%s\"", line);
1164 while (!Lst_IsEmpty(&sources)) {
1165 gnp = Lst_DeQueue(&sources);
1166 ParseDoSrc(tOp, gnp->name, &curSrcs);
1175 ParseDoSrc(tOp, line, &curSrcs);
1177 while (*cp && isspace((unsigned char)*cp)) {
1182 Lst_Destroy(&curSrcs, NOFREE);
1185 if (mainNode == NULL) {
1187 * If we have yet to decide on a main target to make, in the
1188 * absence of any user input, we want the first target on
1189 * the first dependency line that is actually a real target
1190 * (i.e. isn't a .USE or .EXEC rule) to be made.
1192 Lst_ForEach(&targets, ParseFindMain, NULL);
1197 *---------------------------------------------------------------------
1199 * Return TRUE if the passed line is a variable assignment. A variable
1200 * assignment consists of a single word followed by optional whitespace
1201 * followed by either a += or an = operator.
1202 * This function is used both by the Parse_File function and main when
1203 * parsing the command-line arguments.
1206 * TRUE if it is. FALSE if it ain't
1210 *---------------------------------------------------------------------
1213 Parse_IsVar(char *line)
1215 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1216 Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1219 #define ISEQOPERATOR(c) \
1220 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1223 * Skip to variable name
1225 for (; (*line == ' ') || (*line == '\t'); line++)
1228 for (; *line != '=' || level != 0; line++)
1232 * end-of-line -- can't be a variable assignment.
1239 * there can be as much white space as desired so long as there is
1240 * only one word before the operator
1256 if (wasSpace && haveName) {
1257 if (ISEQOPERATOR(*line)) {
1259 * We must have a finished word
1265 * When an = operator [+?!:] is found, the next
1266 * character must be an = or it ain't a valid
1273 * This is a shell command
1275 if (strncmp(line, ":sh", 3) == 0)
1280 * This is the start of another word, so not assignment.
1295 *---------------------------------------------------------------------
1297 * Take the variable assignment in the passed line and do it in the
1300 * Note: There is a lexical ambiguity with assignment modifier characters
1301 * in variable names. This routine interprets the character before the =
1302 * as a modifier. Therefore, an assignment like
1304 * is interpreted as "C+ +=" instead of "C++ =".
1310 * the variable structure of the given variable name is altered in the
1312 *---------------------------------------------------------------------
1315 Parse_DoVar(char *line, GNode *ctxt)
1317 char *cp; /* pointer into line */
1319 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1320 } type; /* Type of assignment */
1321 char *opc; /* ptr to operator character to
1322 * null-terminate the variable name */
1326 * Avoid clobbered variable warnings by forcing the compiler
1327 * to ``unregister'' variables
1335 * Skip to variable name
1337 while ((*line == ' ') || (*line == '\t')) {
1342 * Skip to operator character, nulling out whitespace as we go
1344 for (cp = line + 1; *cp != '='; cp++) {
1345 if (isspace((unsigned char)*cp)) {
1349 opc = cp - 1; /* operator is the previous character */
1350 *cp++ = '\0'; /* nuke the = */
1353 * Check operator type
1363 * If the variable already has a value, we don't do anything.
1366 if (Var_Exists(line, ctxt)) {
1391 if (strncmp(opc, ":sh", 3) == 0) {
1401 while (isspace((unsigned char)*cp)) {
1405 if (type == VAR_APPEND) {
1406 Var_Append(line, cp, ctxt);
1407 } else if (type == VAR_SUBST) {
1409 * Allow variables in the old value to be undefined, but leave their
1410 * invocation alone -- this is done by forcing oldVars to be false.
1411 * XXX: This can cause recursive variables, but that's not hard to do,
1412 * and this allows someone to do something like
1414 * CFLAGS = $(.INCLUDES)
1415 * CFLAGS := -I.. $(CFLAGS)
1417 * And not get an error.
1419 Boolean oldOldVars = oldVars;
1424 * make sure that we set the variable the first time to nothing
1425 * so that it gets substituted!
1427 if (!Var_Exists(line, ctxt))
1428 Var_Set(line, "", ctxt);
1430 buf = Var_Subst(NULL, cp, ctxt, FALSE);
1431 cp = Buf_GetAll(buf, NULL);
1432 Buf_Destroy(buf, FALSE);
1434 oldVars = oldOldVars;
1436 Var_Set(line, cp, ctxt);
1438 } else if (type == VAR_SHELL) {
1439 Boolean freeCmd = FALSE; /* TRUE if the command needs to be freed, i.e.
1440 * if any variable expansion was performed */
1444 if (strchr(cp, '$') != NULL) {
1446 * There's a dollar sign in the command, so perform variable
1447 * expansion on the whole thing. The resulting string will need
1448 * freeing when we're done, so set freeCmd to TRUE.
1450 buf = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1451 cp = Buf_GetAll(buf, NULL);
1452 Buf_Destroy(buf, FALSE);
1456 buf = Cmd_Exec(cp, &error);
1457 Var_Set(line, Buf_GetAll(buf, NULL), ctxt);
1458 Buf_Destroy(buf, TRUE);
1461 Parse_Error(PARSE_WARNING, error, cp);
1467 * Normal assignment -- just do it.
1469 Var_Set(line, cp, ctxt);
1475 * Lst_ForEach function to add a command line to all targets
1481 * A new element is added to the commands list of the node.
1484 ParseAddCmd(void *gnp, void *cmd)
1488 /* if target already supplied, ignore commands */
1489 if (!(gn->type & OP_HAS_COMMANDS))
1490 Lst_AtEnd(&gn->commands, cmd);
1492 Parse_Error(PARSE_WARNING,
1493 "duplicate script for target \"%s\" ignored",
1499 *-----------------------------------------------------------------------
1500 * ParseHasCommands --
1501 * Callback procedure for Parse_File when destroying the list of
1502 * targets on the last dependency line. Marks a target as already
1503 * having commands if it does, to keep from having shell commands
1504 * on multiple dependency lines.
1510 * OP_HAS_COMMANDS may be set for the target.
1512 *-----------------------------------------------------------------------
1515 ParseHasCommands(void *gnp)
1519 if (!Lst_IsEmpty(&gn->commands)) {
1520 gn->type |= OP_HAS_COMMANDS;
1525 *-----------------------------------------------------------------------
1526 * Parse_AddIncludeDir --
1527 * Add a directory to the path searched for included makefiles
1528 * bracketed by double-quotes. Used by functions in main.c
1534 * The directory is appended to the list.
1536 *-----------------------------------------------------------------------
1539 Parse_AddIncludeDir(char *dir)
1542 Dir_AddDir(&parseIncPath, dir);
1545 /*---------------------------------------------------------------------
1547 * Handle error directive
1549 * The input is the line minus the ".error". We substitute variables,
1550 * print the message and exit(1) or just print a warning if the ".error"
1551 * directive is malformed.
1553 *---------------------------------------------------------------------
1556 ParseDoError(char *errmsg)
1560 if (!isspace((unsigned char)*errmsg)) {
1561 Parse_Error(PARSE_WARNING, "invalid syntax: .error%s", errmsg);
1565 while (isspace((unsigned char)*errmsg))
1568 buf = Var_Subst(NULL, errmsg, VAR_GLOBAL, FALSE);
1569 errmsg = Buf_GetAll(buf, NULL);
1571 Parse_Error(PARSE_FATAL, "%s", errmsg);
1572 Buf_Destroy(buf, TRUE);
1574 /* Terminate immediately. */
1578 /*---------------------------------------------------------------------
1580 * Handle warning directive
1582 * The input is the line minus the ".warning". We substitute variables
1583 * and print the message or just print a warning if the ".warning"
1584 * directive is malformed.
1586 *---------------------------------------------------------------------
1589 ParseDoWarning(char *warnmsg)
1593 if (!isspace((unsigned char)*warnmsg)) {
1594 Parse_Error(PARSE_WARNING, "invalid syntax: .warning%s",
1599 while (isspace((unsigned char)*warnmsg))
1602 buf = Var_Subst(NULL, warnmsg, VAR_GLOBAL, FALSE);
1603 warnmsg = Buf_GetAll(buf, NULL);
1605 Parse_Error(PARSE_WARNING, "%s", warnmsg);
1606 Buf_Destroy(buf, TRUE);
1610 *---------------------------------------------------------------------
1612 * Push to another file.
1614 * The input is the line minus the #include. A file spec is a string
1615 * enclosed in <> or "". The former is looked for only in sysIncPath.
1616 * The latter in . and the directories specified by -I command line
1623 * A structure is added to the includes Lst and readProc, curFile.lineno,
1624 * curFile.fname and curFile.F are altered for the new file
1625 *---------------------------------------------------------------------
1628 ParseDoInclude(char *file)
1630 char *fullname; /* full pathname of file */
1631 IFile *oldFile; /* state associated with current file */
1632 char endc; /* the character which ends the file spec */
1633 char *cp; /* current position in file spec */
1634 Boolean isSystem; /* TRUE if makefile is a system makefile */
1638 * Skip to delimiter character so we know where to look
1640 while ((*file == ' ') || (*file == '\t')) {
1644 if ((*file != '"') && (*file != '<')) {
1645 Parse_Error(PARSE_FATAL,
1646 ".include filename must be delimited by '\"' or '<'");
1651 * Set the search path on which to find the include file based on the
1652 * characters which bracket its name. Angle-brackets imply it's
1653 * a system Makefile while double-quotes imply it's a user makefile
1664 * Skip to matching delimiter
1666 for (cp = ++file; *cp && *cp != endc; cp++) {
1671 Parse_Error(PARSE_FATAL,
1672 "Unclosed %cinclude filename. '%c' expected",
1679 * Substitute for any variables in the file name before trying to
1682 buf = Var_Subst(NULL, file, VAR_CMD, FALSE);
1683 file = Buf_GetAll(buf, NULL);
1684 Buf_Destroy(buf, FALSE);
1687 * Now we know the file's name and its search path, we attempt to
1688 * find the durn thing. A return of NULL indicates the file don't
1693 * Include files contained in double-quotes are first searched for
1694 * relative to the including file's location. We don't want to
1695 * cd there, of course, so we just tack on the old file's
1696 * leading path components and call Dir_FindFile to see if
1697 * we can locate the beast.
1699 char *prefEnd, *Fname;
1701 /* Make a temporary copy of this, to be safe. */
1702 Fname = estrdup(curFile.fname);
1704 prefEnd = strrchr(Fname, '/');
1705 if (prefEnd != (char *)NULL) {
1710 newName = estrdup(file);
1712 newName = str_concat(Fname, file, STR_ADDSLASH);
1713 fullname = Dir_FindFile(newName, &parseIncPath);
1714 if (fullname == NULL) {
1715 fullname = Dir_FindFile(newName, &dirSearchPath);
1727 if (fullname == NULL) {
1729 * System makefile or makefile wasn't found in same directory as
1730 * included makefile. Search for it first on the -I search path,
1731 * then on the .PATH search path, if not found in a -I directory.
1732 * XXX: Suffix specific?
1734 fullname = Dir_FindFile(file, &parseIncPath);
1735 if (fullname == NULL) {
1736 fullname = Dir_FindFile(file, &dirSearchPath);
1740 if (fullname == NULL) {
1742 * Still haven't found the makefile. Look for it on the system
1743 * path as a last resort.
1745 fullname = Dir_FindFile(file, &sysIncPath);
1748 if (fullname == NULL) {
1750 Parse_Error(PARSE_FATAL, "Could not find %s", file);
1751 /* XXXHB free(file) */
1758 * Once we find the absolute path to the file, we get to save all the
1759 * state from the current file before we can start reading this
1760 * include file. The state is stored in an IFile structure which
1761 * is placed on a list with other IFile structures. The list makes
1762 * a very nice stack to track how we got here...
1764 oldFile = emalloc(sizeof(IFile));
1765 memcpy(oldFile, &curFile, sizeof(IFile));
1767 Lst_AtFront(&includes, oldFile);
1770 * Once the previous state has been saved, we can get down to reading
1771 * the new file. We set up the name of the file to be the absolute
1772 * name of the include file so error messages refer to the right
1773 * place. Naturally enough, we start reading at line number 0.
1775 curFile.fname = fullname;
1778 curFile.F = fopen(fullname, "r");
1780 if (curFile.F == NULL) {
1781 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
1783 * Pop to previous file
1787 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
1792 *---------------------------------------------------------------------
1793 * Parse_FromString --
1794 * Start Parsing from the given string
1800 * A structure is added to the includes Lst and readProc, curFile.lineno,
1801 * curFile.fname and curFile.F are altered for the new file
1802 *---------------------------------------------------------------------
1805 Parse_FromString(char *str, int lineno)
1807 IFile *oldFile; /* state associated with this file */
1809 DEBUGF(FOR, ("%s\n---- at line %d\n", str, lineno));
1811 oldFile = emalloc(sizeof(IFile));
1812 memcpy(oldFile, &curFile, sizeof(IFile));
1814 Lst_AtFront(&includes, oldFile);
1817 curFile.p = emalloc(sizeof(PTR));
1818 curFile.p->str = curFile.p->ptr = str;
1819 curFile.lineno = lineno;
1820 curFile.fname = estrdup(curFile.fname);
1825 *---------------------------------------------------------------------
1826 * ParseTraditionalInclude --
1827 * Push to another file.
1829 * The input is the line minus the "include". The file name is
1830 * the string following the "include".
1836 * A structure is added to the includes Lst and readProc, curFile.lineno,
1837 * curFile.fname and curFile.F are altered for the new file
1838 *---------------------------------------------------------------------
1841 ParseTraditionalInclude(char *file)
1843 char *fullname; /* full pathname of file */
1844 IFile *oldFile; /* state associated with current file */
1845 char *cp; /* current position in file spec */
1849 * Skip over whitespace
1851 while ((*file == ' ') || (*file == '\t')) {
1855 if (*file == '\0') {
1856 Parse_Error(PARSE_FATAL,
1857 "Filename missing from \"include\"");
1862 * Skip to end of line or next whitespace
1864 for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1871 * Substitute for any variables in the file name before trying to
1874 buf = Var_Subst(NULL, file, VAR_CMD, FALSE);
1875 file = Buf_GetAll(buf, NULL);
1876 Buf_Destroy(buf, FALSE);
1879 * Now we know the file's name, we attempt to find the durn thing.
1880 * Search for it first on the -I search path, then on the .PATH
1881 * search path, if not found in a -I directory.
1883 fullname = Dir_FindFile(file, &parseIncPath);
1884 if (fullname == NULL) {
1885 fullname = Dir_FindFile(file, &dirSearchPath);
1888 if (fullname == NULL) {
1890 * Still haven't found the makefile. Look for it on the system
1891 * path as a last resort.
1893 fullname = Dir_FindFile(file, &sysIncPath);
1896 if (fullname == NULL) {
1897 Parse_Error(PARSE_FATAL, "Could not find %s", file);
1898 /* XXXHB free(file) */
1902 /* XXXHB free(file) */
1905 * Once we find the absolute path to the file, we get to save all the
1906 * state from the current file before we can start reading this
1907 * include file. The state is stored in an IFile structure which
1908 * is placed on a list with other IFile structures. The list makes
1909 * a very nice stack to track how we got here...
1911 oldFile = emalloc(sizeof(IFile));
1912 memcpy(oldFile, &curFile, sizeof(IFile));
1914 Lst_AtFront(&includes, oldFile);
1917 * Once the previous state has been saved, we can get down to reading
1918 * the new file. We set up the name of the file to be the absolute
1919 * name of the include file so error messages refer to the right
1920 * place. Naturally enough, we start reading at line number 0.
1922 curFile.fname = fullname;
1925 curFile.F = fopen(fullname, "r");
1927 if (curFile.F == NULL) {
1928 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
1930 * Pop to previous file
1934 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
1940 *---------------------------------------------------------------------
1942 * Called when EOF is reached in the current file. If we were reading
1943 * an include file, the includes stack is popped and things set up
1944 * to go back to reading the previous file at the previous location.
1947 * CONTINUE if there's more to do. DONE if not.
1950 * The old curFile.F is closed. The includes list is shortened.
1951 * curFile.lineno, curFile.F, and curFile.fname are changed if
1952 * CONTINUE is returned.
1953 *---------------------------------------------------------------------
1956 ParseEOF(int opened)
1958 IFile *ifile; /* the state on the top of the includes stack */
1960 if (Lst_IsEmpty(&includes)) {
1961 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
1965 ifile = Lst_DeQueue(&includes);
1966 free(curFile.fname);
1967 if (opened && curFile.F) {
1969 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
1972 free(curFile.p->str);
1975 memcpy(&curFile, ifile, sizeof(IFile));
1981 *---------------------------------------------------------------------
1983 * Read a character from the current file
1986 * The character that was read
1989 *---------------------------------------------------------------------
1996 return (fgetc(curFile.F));
1998 if (curFile.p && *curFile.p->ptr)
1999 return (*curFile.p->ptr++);
2005 *---------------------------------------------------------------------
2007 * Put back a character to the current file
2013 *---------------------------------------------------------------------
2020 ungetc(c, curFile.F);
2024 *--(curFile.p->ptr) = c;
2031 * Grab the next line unless it begins with a dot (`.') and we're told to
2032 * ignore such lines.
2035 ParseSkipLine(int skip, int keep_newline)
2041 buf = Buf_Init(MAKE_BSIZE);
2047 while (((c = ParseReadc()) != '\n' || lastc == '\\')
2049 if (skip && c == '#' && lastc != '\\') {
2050 /* let a comment be terminated even by an escaped \n.
2051 * This is consistent to comment handling in ParseReadLine */
2052 while ((c = ParseReadc()) != '\n' && c != EOF)
2058 Buf_AddByte(buf, (Byte)c);
2060 Buf_ReplaceLastByte(buf, (Byte)' ');
2063 while ((c = ParseReadc()) == ' ' || c == '\t')
2070 Buf_AddByte(buf, (Byte)c);
2075 Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
2076 Buf_Destroy(buf, TRUE);
2081 Buf_AddByte(buf, (Byte)'\0');
2082 line = (char *)Buf_GetAll(buf, NULL);
2083 } while (skip == 1 && line[0] != '.');
2085 Buf_Destroy(buf, FALSE);
2091 *---------------------------------------------------------------------
2093 * Read an entire line from the input file. Called only by Parse_File.
2094 * To facilitate escaped newlines and what have you, a character is
2095 * buffered in 'lastc', which is '\0' when no characters have been
2096 * read. When we break out of the loop, c holds the terminating
2097 * character and lastc holds a character that should be added to
2098 * the line (unless we don't read anything but a terminator).
2101 * A line w/o its newline
2104 * Only those associated with reading a character
2105 *---------------------------------------------------------------------
2110 Buffer *buf; /* Buffer for current line */
2111 int c; /* the current character */
2112 int lastc; /* The most-recent character */
2113 Boolean semiNL; /* treat semi-colons as newlines */
2114 Boolean ignDepOp; /* TRUE if should ignore dependency operators
2115 * for the purposes of setting semiNL */
2116 Boolean ignComment; /* TRUE if should ignore comments (in a
2118 char *line; /* Result */
2119 char *ep; /* to strip trailing blanks */
2120 int lineno; /* Saved line # */
2127 * Handle special-characters at the beginning of the line. Either a
2128 * leading tab (shell command) or pound-sign (possible conditional)
2129 * forces us to ignore comments and dependency operators and treat
2130 * semi-colons as semi-colons (by leaving semiNL FALSE). This also
2131 * discards completely blank lines.
2137 ignComment = ignDepOp = TRUE;
2139 } else if (c == '\n') {
2141 } else if (c == '#') {
2146 * Anything else breaks out without doing anything
2154 buf = Buf_Init(MAKE_BSIZE);
2156 while (((c = ParseReadc()) != '\n' || (lastc == '\\')) &&
2163 * Escaped newline: read characters until a non-space or an
2164 * unescaped newline and replace them all by a single space.
2165 * This is done by storing the space over the backslash and
2166 * dropping through with the next nonspace. If it is a
2167 * semi-colon and semiNL is TRUE, it will be recognized as a
2168 * newline in the code below this...
2172 while ((c = ParseReadc()) == ' ' || c == '\t') {
2175 if (c == EOF || c == '\n') {
2179 * Check for comments, semiNL's, etc. -- easier than
2180 * ParseUnreadc(c); continue;
2189 * Semi-colon: Need to see if it should be interpreted as a
2194 * To make sure the command that may be following this
2195 * semi-colon begins with a tab, we push one back into the
2196 * input stream. This will overwrite the semi-colon in the
2197 * buffer. If there is no command following, this does no
2198 * harm, since the newline remains in the buffer and the
2199 * whole line is ignored.
2208 * Haven't seen a dependency operator before this, so this
2209 * must be a variable assignment -- don't pay attention to
2210 * dependency operators after this.
2213 } else if (lastc == ':' || lastc == '!') {
2215 * Well, we've seen a dependency operator already, but it
2216 * was the previous character, so this is really just an
2217 * expanded variable assignment. Revert semi-colons to
2218 * being just semi-colons again and ignore any more
2219 * dependency operators.
2221 * XXX: Note that a line like "foo : a:=b" will blow up,
2222 * but who'd write a line like that anyway?
2230 if (lastc != '\\') {
2232 * If the character is a hash mark and it isn't escaped
2233 * (or we're being compatible), the thing is a comment.
2234 * Skip to the end of the line.
2238 } while ((c != '\n') && (c != EOF));
2242 * Don't add the backslash. Just let the # get copied
2252 if (!ignDepOp && (c == ':' || c == '!')) {
2254 * A semi-colon is recognized as a newline only on
2255 * dependency lines. Dependency lines are lines with a
2256 * colon or an exclamation point. Ergo...
2265 * Copy in the previous character and save this one in lastc.
2267 Buf_AddByte(buf, (Byte)lastc);
2274 if (lastc != '\0') {
2275 Buf_AddByte(buf, (Byte)lastc);
2277 Buf_AddByte(buf, (Byte)'\0');
2278 line = (char *)Buf_GetAll(buf, NULL);
2279 Buf_Destroy(buf, FALSE);
2282 * Strip trailing blanks and tabs from the line.
2283 * Do not strip a blank or tab that is preceded by
2289 while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
2290 if (ep > line + 1 && ep[-2] == '\\')
2296 if (line[0] == '.') {
2298 * The line might be a conditional. Ask the conditional module
2299 * about it and act accordingly
2301 switch (Cond_Eval(line)) {
2304 * Skip to next conditional that evaluates to COND_PARSE.
2308 line = ParseSkipLine(1, 0);
2309 } while (line && Cond_Eval(line) != COND_PARSE);
2315 line = ParseReadLine();
2318 if (For_Eval(line)) {
2321 lineno = curFile.lineno;
2324 * Skip after the matching end
2326 line = ParseSkipLine(0, 1);
2328 Parse_Error(PARSE_FATAL,
2329 "Unexpected end of file in for loop.\n");
2332 ok = For_Eval(line);
2338 line = ParseReadLine();
2349 * Hit end-of-file, so return a NULL line to indicate this.
2356 *-----------------------------------------------------------------------
2357 * ParseFinishLine --
2358 * Handle the end of a dependency group.
2364 * inLine set FALSE. 'targets' list destroyed.
2366 *-----------------------------------------------------------------------
2369 ParseFinishLine(void)
2373 Lst_ForEach(&targets, Suff_EndTransform, NULL);
2374 Lst_Destroy(&targets, ParseHasCommands);
2380 stripvarname(char *cp)
2384 while (isspace((unsigned char)*cp))
2387 while (*cp2 && !isspace((unsigned char)*cp2))
2395 *---------------------------------------------------------------------
2397 * Parse a file into its component parts, incorporating it into the
2398 * current dependency graph. This is the main function and controls
2399 * almost every other function in this module
2405 * Loads. Nodes are added to the list of all targets, nodes and links
2406 * are added to the dependency graph. etc. etc. etc.
2407 *---------------------------------------------------------------------
2410 Parse_File(char *name, FILE *stream)
2412 char *cp, /* pointer into the line */
2413 *line; /* the line we're working on */
2417 curFile.fname = name;
2422 Var_Append(".MAKEFILE_LIST", name, VAR_GLOBAL);
2425 while ((line = ParseReadLine()) != NULL) {
2428 * Lines that begin with the special character are either
2429 * include or undef directives.
2431 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2434 if (strncmp(cp, "include", 7) == 0) {
2435 ParseDoInclude(cp + 7);
2437 } else if (strncmp(cp, "error", 5) == 0) {
2438 ParseDoError(cp + 5);
2440 } else if (strncmp(cp, "warning", 7) == 0) {
2441 ParseDoWarning(cp + 7);
2443 } else if (strncmp(cp, "undef", 5) == 0) {
2444 cp = stripvarname(cp + 5);
2445 buf = Var_Subst(NULL, cp, VAR_CMD, FALSE);
2446 cp = Buf_GetAll(buf, NULL);
2447 Buf_Destroy(buf, FALSE);
2449 Var_Delete(cp, VAR_GLOBAL);
2451 } else if (strncmp(cp, "makeenv", 7) == 0) {
2452 cp = stripvarname(cp + 7);
2453 Var_SetEnv(cp, VAR_GLOBAL);
2458 /* If we're this far, the line must be a comment. */
2462 if (*line == '\t') {
2464 * If a line starts with a tab, it can only hope to be
2465 * a creation command.
2467 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2473 * So long as it's not a blank line and we're actually
2474 * in a dependency spec, add the command to the list of
2475 * commands of all targets in the dependency spec
2477 Lst_ForEach(&targets, ParseAddCmd, cp);
2480 Parse_Error(PARSE_FATAL,
2481 "Unassociated shell command \"%s\"",
2486 } else if (strncmp(line, "include", 7) == 0 &&
2487 isspace((unsigned char)line[7]) &&
2488 strchr(line, ':') == NULL) {
2490 * It's an S3/S5-style "include".
2492 ParseTraditionalInclude(line + 7);
2495 } else if (Parse_IsVar(line)) {
2497 Parse_DoVar(line, VAR_GLOBAL);
2500 * We now know it's a dependency line so it needs to have all
2501 * variables expanded before being parsed. Tell the variable
2502 * module to complain if some variable is undefined...
2503 * To make life easier on novices, if the line is indented we
2504 * first make sure the line has a dependency operator in it.
2505 * If it doesn't have an operator and we're in a dependency
2506 * line's script, we assume it's actually a shell command
2507 * and add it to the current list of targets.
2510 if (isspace((unsigned char)line[0])) {
2511 while ((*cp != '\0') && isspace((unsigned char)*cp)) {
2521 buf = Var_Subst(NULL, line, VAR_CMD, TRUE);
2522 cp = Buf_GetAll(buf, NULL);
2523 Buf_Destroy(buf, FALSE);
2529 * Need a non-circular list for the target nodes
2531 Lst_Destroy(&targets, NOFREE);
2534 ParseDoDependency(line);
2542 * Reached EOF, but it may be just EOF of an include file...
2544 } while (ParseEOF(1) == CONTINUE);
2547 * Make sure conditionals are clean
2552 errx(1, "fatal errors encountered -- cannot continue");
2556 *---------------------------------------------------------------------
2558 * initialize the parsing module
2564 * the parseIncPath list is initialized...
2565 *---------------------------------------------------------------------
2575 *-----------------------------------------------------------------------
2577 * Return a Lst of the main target to create for main()'s sake. If
2578 * no such target exists, we Punt with an obnoxious error message.
2581 * A Lst of the single node to create.
2586 *-----------------------------------------------------------------------
2589 Parse_MainName(Lst *listmain)
2592 if (mainNode == NULL) {
2593 Punt("no target to make.");
2595 } else if (mainNode->type & OP_DOUBLEDEP) {
2596 Lst_AtEnd(listmain, mainNode);
2597 Lst_Concat(listmain, &mainNode->cohorts, LST_CONCNEW);
2600 Lst_AtEnd(listmain, mainNode);