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
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
46 * Functions to parse a makefile.
48 * Most important structures are kept in Lsts. Directories for
49 * the #include "..." function are kept in the 'parseIncPath' Lst, while
50 * those for the #include <...> are kept in the 'sysIncPath' Lst. The
51 * targets currently being defined are kept in the 'targets' Lst.
55 * Parse_File Function used to parse a makefile. It must
56 * be given the name of the file, which should
57 * already have been opened, and a function
58 * to call to read a character from the file.
60 * Parse_IsVar Returns TRUE if the given line is a
61 * variable assignment. Used by MainParseArgs
62 * to determine if an argument is a target
63 * or a variable assignment. Used internally
64 * for pretty much the same thing...
66 * Parse_Error Function called when an error occurs in
67 * parsing. Used by the variable and
68 * conditional modules.
70 * Parse_MainName Returns a Lst of the main target to create.
88 #include "hash_tables.h"
92 #include "pathnames.h"
101 * These values are returned by ParsePopInput to tell Parse_File whether to
102 * CONTINUE parsing, i.e. it had only reached the end of an include file,
108 /* targets we're working on */
109 static Lst targets = Lst_Initializer(targets);
111 /* true if currently in a dependency line or its commands */
112 static Boolean inLine;
114 static int fatals = 0;
117 * The main target to create. This is the first target on the
118 * first dependency line in the first makefile.
120 static GNode *mainNode;
123 * Definitions for handling #include specifications
126 char *fname; /* name of previous file */
127 int lineno; /* saved line number */
128 FILE *F; /* the open stream */
129 char *str; /* the string when parsing a string */
130 char *ptr; /* the current pointer when parsing a string */
131 TAILQ_ENTRY(IFile) link;/* stack the files */
134 /* stack of IFiles generated by * #includes */
135 static TAILQ_HEAD(, IFile) includes = TAILQ_HEAD_INITIALIZER(includes);
137 /* access current file */
138 #define CURFILE (TAILQ_FIRST(&includes))
140 /* list of directories for "..." includes */
141 struct Path parseIncPath = TAILQ_HEAD_INITIALIZER(parseIncPath);
143 /* list of directories for <...> includes */
144 struct Path sysIncPath = TAILQ_HEAD_INITIALIZER(sysIncPath);
147 * specType contains the SPECial TYPE of the current target. It is
148 * Not if the target is unspecial. If it *is* special, however, the children
149 * are linked as children of the parent but not vice versa. This variable is
150 * set in ParseDoDependency
154 Default, /* .DEFAULT */
156 ExportVar, /* .EXPORTVAR */
157 Ignore, /* .IGNORE */
158 Includes, /* .INCLUDES */
159 Interrupt, /* .INTERRUPT */
161 MFlags, /* .MFLAGS or .MAKEFLAGS */
162 Main, /* .MAIN and we don't have anyth. user-spec. to make */
163 Not, /* Not special */
164 NotParallel, /* .NOTPARALELL */
167 Parallel, /* .PARALLEL */
171 Precious, /* .PRECIOUS */
172 ExShell, /* .SHELL */
173 Silent, /* .SILENT */
174 SingleShell, /* .SINGLESHELL */
175 Suffixes, /* .SUFFIXES */
178 Attribute /* Generic attribute */
181 static ParseSpecial specType;
185 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
186 * seen, then set to each successive source on the line.
188 static GNode *predecessor;
191 * The parseKeywords table is searched using binary search when deciding
192 * if a target or source is special. The 'spec' field is the ParseSpecial
193 * type of the keyword ("Not" if the keyword isn't special as a target) while
194 * the 'op' field is the operator to apply to the list of targets if the
195 * keyword is used as a source ("0" if the keyword isn't special as a source)
197 static const struct keyword {
198 const char *name; /* Name of keyword */
199 ParseSpecial spec; /* Type when used as a target */
200 int op; /* Operator when used as a source */
201 } parseKeywords[] = {
202 /* KEYWORD-START-TAG */
203 { ".BEGIN", Begin, 0 },
204 { ".DEFAULT", Default, 0 },
206 { ".EXEC", Attribute, OP_EXEC },
207 { ".EXPORTVAR", ExportVar, 0 },
208 { ".IGNORE", Ignore, OP_IGNORE },
209 { ".INCLUDES", Includes, 0 },
210 { ".INTERRUPT", Interrupt, 0 },
211 { ".INVISIBLE", Attribute, OP_INVISIBLE },
212 { ".JOIN", Attribute, OP_JOIN },
213 { ".LIBS", Libs, 0 },
214 { ".MAIN", Main, 0 },
215 { ".MAKE", Attribute, OP_MAKE },
216 { ".MAKEFLAGS", MFlags, 0 },
217 { ".MFLAGS", MFlags, 0 },
218 { ".NOTMAIN", Attribute, OP_NOTMAIN },
219 { ".NOTPARALLEL", NotParallel, 0 },
220 { ".NO_PARALLEL", NotParallel, 0 },
221 { ".NULL", Null, 0 },
222 { ".OPTIONAL", Attribute, OP_OPTIONAL },
223 { ".ORDER", Order, 0 },
224 { ".PARALLEL", Parallel, 0 },
225 { ".PATH", ExPath, 0 },
226 { ".PHONY", Phony, OP_PHONY },
227 { ".POSIX", Posix, 0 },
228 { ".PRECIOUS", Precious, OP_PRECIOUS },
229 { ".RECURSIVE", Attribute, OP_MAKE },
230 { ".SHELL", ExShell, 0 },
231 { ".SILENT", Silent, OP_SILENT },
232 { ".SINGLESHELL", SingleShell, 0 },
233 { ".SUFFIXES", Suffixes, 0 },
234 { ".USE", Attribute, OP_USE },
235 { ".WAIT", Wait, 0 },
236 { ".WARN", Warn, 0 },
237 /* KEYWORD-END-TAG */
239 #define NKEYWORDS (sizeof(parseKeywords) / sizeof(parseKeywords[0]))
241 static void parse_include(char *, int, int);
242 static void parse_sinclude(char *, int, int);
243 static void parse_message(char *, int, int);
244 static void parse_undef(char *, int, int);
245 static void parse_for(char *, int, int);
246 static void parse_endfor(char *, int, int);
248 static const struct directive {
251 Boolean skip_flag; /* execute even when skipped */
252 void (*func)(char *, int, int);
254 /* DIRECTIVES-START-TAG */
255 { "elif", COND_ELIF, TRUE, Cond_If },
256 { "elifdef", COND_ELIFDEF, TRUE, Cond_If },
257 { "elifmake", COND_ELIFMAKE, TRUE, Cond_If },
258 { "elifndef", COND_ELIFNDEF, TRUE, Cond_If },
259 { "elifnmake", COND_ELIFNMAKE, TRUE, Cond_If },
260 { "else", COND_ELSE, TRUE, Cond_Else },
261 { "endfor", 0, FALSE, parse_endfor },
262 { "endif", COND_ENDIF, TRUE, Cond_Endif },
263 { "error", 1, FALSE, parse_message },
264 { "for", 0, FALSE, parse_for },
265 { "if", COND_IF, TRUE, Cond_If },
266 { "ifdef", COND_IFDEF, TRUE, Cond_If },
267 { "ifmake", COND_IFMAKE, TRUE, Cond_If },
268 { "ifndef", COND_IFNDEF, TRUE, Cond_If },
269 { "ifnmake", COND_IFNMAKE, TRUE, Cond_If },
270 { "include", 0, FALSE, parse_include },
271 { "sinclude", 0, FALSE, parse_sinclude },
272 { "undef", 0, FALSE, parse_undef },
273 { "warning", 0, FALSE, parse_message },
274 /* DIRECTIVES-END-TAG */
276 #define NDIRECTS (sizeof(directives) / sizeof(directives[0]))
280 * Look in the table of keywords for one matching the given string.
283 * The pointer to keyword table entry or NULL.
285 static const struct keyword *
286 ParseFindKeyword(const char *str)
290 kw = keyword_hash(str, strlen(str));
291 if (kw < 0 || kw >= (int)NKEYWORDS ||
292 strcmp(str, parseKeywords[kw].name) != 0)
294 return (&parseKeywords[kw]);
299 * Error message abort function for parsing. Prints out the context
300 * of the error (line number and file) as well as the message with
301 * two optional arguments.
307 * "fatals" is incremented if the level is PARSE_FATAL.
311 Parse_Error(int type, const char *fmt, ...)
317 fprintf(stderr, "\"%s\", line %d: ",
318 CURFILE->fname, CURFILE->lineno);
319 if (type == PARSE_WARNING)
320 fprintf(stderr, "warning: ");
321 vfprintf(stderr, fmt, ap);
323 fprintf(stderr, "\n");
325 if (type == PARSE_FATAL)
332 * Push a new input source onto the input stack. If ptr is NULL
333 * the fullname is used to fopen the file. If it is not NULL,
334 * ptr is assumed to point to the string to be parsed. If opening the
335 * file fails, the fullname is freed.
338 ParsePushInput(char *fullname, FILE *fp, char *ptr, int lineno)
342 nf = emalloc(sizeof(*nf));
343 nf->fname = fullname;
347 /* the input source is a file */
348 if ((nf->F = fp) == NULL) {
349 nf->F = fopen(fullname, "r");
351 Parse_Error(PARSE_FATAL, "Cannot open %s",
358 nf->str = nf->ptr = NULL;
359 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
361 nf->str = nf->ptr = ptr;
364 TAILQ_INSERT_HEAD(&includes, nf, link);
369 * Called when EOF is reached in the current file. If we were reading
370 * an include file, the includes stack is popped and things set up
371 * to go back to reading the previous file at the previous location.
374 * CONTINUE if there's more to do. DONE if not.
377 * The old curFile.F is closed. The includes list is shortened.
378 * curFile.lineno, curFile.F, and curFile.fname are changed if
379 * CONTINUE is returned.
384 struct IFile *ifile; /* the state on the top of the includes stack */
386 assert(!TAILQ_EMPTY(&includes));
388 ifile = TAILQ_FIRST(&includes);
389 TAILQ_REMOVE(&includes, ifile, link);
392 if (ifile->F != NULL) {
394 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
396 if (ifile->str != NULL) {
401 return (TAILQ_EMPTY(&includes) ? DONE : CONTINUE);
406 * Parse the .WARN pseudo-target.
409 parse_warn(char *line)
414 brk_string(&aa, line, TRUE);
416 for (i = 1; i < aa.argc; i++)
417 Main_ParseWarn(aa.argv[i], 0);
421 *---------------------------------------------------------------------
423 * Link the parent nodes to their new child. Used by
424 * ParseDoDependency. If the specType isn't 'Not', the parent
425 * isn't linked as a parent of the child.
428 * New elements are added to the parents lists of cgn and the
429 * children list of cgn. the unmade field of pgn is updated
430 * to reflect the additional child.
431 *---------------------------------------------------------------------
434 ParseLinkSrc(Lst *parents, GNode *cgn)
439 LST_FOREACH(ln, parents) {
441 if (Lst_Member(&pgn->children, cgn) == NULL) {
442 Lst_AtEnd(&pgn->children, cgn);
443 if (specType == Not) {
444 Lst_AtEnd(&cgn->parents, pgn);
452 *---------------------------------------------------------------------
454 * Apply the parsed operator to all target nodes. Used in
455 * ParseDoDependency once all targets have been found and their
456 * operator parsed. If the previous and new operators are incompatible,
457 * a major error is taken.
460 * The type field of the node is altered to reflect any new bits in
462 *---------------------------------------------------------------------
471 LST_FOREACH(ln, &targets) {
475 * If the dependency mask of the operator and the node don't
476 * match and the node has actually had an operator applied to
477 * it before, and the operator actually has some dependency
478 * information in it, complain.
480 if ((op & OP_OPMASK) != (gn->type & OP_OPMASK) &&
481 !OP_NOP(gn->type) && !OP_NOP(op)) {
482 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s",
487 if (op == OP_DOUBLEDEP &&
488 (gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
490 * If the node was the object of a :: operator, we need
491 * to create a new instance of it for the children and
492 * commands on this dependency line. The new instance
493 * is placed on the 'cohorts' list of the initial one
494 * (note the initial one is not on its own cohorts list)
495 * and the new instance is linked to all parents of the
498 cohort = Targ_NewGN(gn->name);
501 * Duplicate links to parents so graph traversal is
502 * simple. Perhaps some type bits should be duplicated?
504 * Make the cohort invisible as well to avoid
505 * duplicating it into other variables. True, parents
506 * of this target won't tend to do anything with their
507 * local variables, but better safe than sorry.
509 ParseLinkSrc(&gn->parents, cohort);
510 cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
511 Lst_AtEnd(&gn->cohorts, cohort);
514 * Replace the node in the targets list with the
517 Lst_Replace(ln, cohort);
521 * We don't want to nuke any previous flags (whatever they were)
522 * so we just OR the new operator into the old
529 *---------------------------------------------------------------------
531 * Given the name of a source, figure out if it is an attribute
532 * and apply it to the targets if it is. Else decide if there is
533 * some attribute which should be applied *to* the source because
534 * of some special target and apply it if so. Otherwise, make the
535 * source be a child of the targets in the list 'targets'
541 * Operator bits may be added to the list of targets or to the source.
542 * The targets may have a new source added to their lists of children.
543 *---------------------------------------------------------------------
546 ParseDoSrc(int tOp, char *src, Lst *allsrc)
549 const struct keyword *kw;
551 if (src[0] == '.' && isupper ((unsigned char)src[1])) {
552 if ((kw = ParseFindKeyword(src)) != NULL) {
557 if (kw->spec == Wait) {
567 * If we have noted the existence of a .MAIN, it means we need
568 * to add the sources of said target to the list of things
569 * to create. The string 'src' is likely to be free, so we
570 * must make a new copy of it. Note that this will only be
571 * invoked if the user didn't specify a target on the command
572 * line. This is to allow #ifmake's to succeed, or something...
574 Lst_AtEnd(&create, estrdup(src));
576 * Add the name to the .TARGETS variable as well, so the user
577 * can employ that, if desired.
579 Var_Append(".TARGETS", src, VAR_GLOBAL);
584 * Create proper predecessor/successor links between the
585 * previous source and the current one.
587 gn = Targ_FindNode(src, TARG_CREATE);
588 if (predecessor != NULL) {
589 Lst_AtEnd(&predecessor->successors, gn);
590 Lst_AtEnd(&gn->preds, predecessor);
593 * The current source now becomes the predecessor for the next
601 * If the source is not an attribute, we need to find/create
602 * a node for it. After that we can apply any operator to it
603 * from a special target or link it to its parents, as
606 * In the case of a source that was the object of a :: operator,
607 * the attribute is applied to all of its instances (as kept in
608 * the 'cohorts' list of the node) or all the cohorts are linked
609 * to all the targets.
611 gn = Targ_FindNode(src, TARG_CREATE);
615 ParseLinkSrc(&targets, gn);
617 if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
621 for (ln = Lst_First(&gn->cohorts); ln != NULL;
623 cohort = Lst_Datum(ln);
627 ParseLinkSrc(&targets, cohort);
635 Lst_AtEnd(allsrc, gn);
641 * Check if GNodes needs to be synchronized.
642 * This has to be when two nodes are on different sides of a
645 LST_FOREACH(ln, allsrc) {
648 if (p->order >= gn->order)
651 * XXX: This can cause loops, and loops can cause
652 * unmade targets, but checking is tedious, and the
653 * debugging output can show the problem
655 Lst_AtEnd(&p->successors, gn);
656 Lst_AtEnd(&gn->preds, p);
663 *---------------------------------------------------------------------
664 * ParseDoDependency --
665 * Parse the dependency line in line.
671 * The nodes of the sources are linked as children to the nodes of the
672 * targets. Some nodes may be created.
674 * We parse a dependency line by first extracting words from the line and
675 * finding nodes in the list of all targets with that name. This is done
676 * until a character is encountered which is an operator character. Currently
677 * these are only ! and :. At this point the operator is parsed and the
678 * pointer into the line advanced until the first source is encountered.
679 * The parsed operator is applied to each node in the 'targets' list,
680 * which is where the nodes found for the targets are kept, by means of
681 * the ParseDoOp function.
682 * The sources are read in much the same way as the targets were except
683 * that now they are expanded using the wildcarding scheme of the C-Shell
684 * and all instances of the resulting words in the list of all targets
685 * are found. Each of the resulting nodes is then linked to each of the
686 * targets as one of its children.
687 * Certain targets are handled specially. These are the ones detailed
688 * by the specType variable.
689 * The storing of transformation rules is also taken care of here.
690 * A target is recognized as a transformation rule by calling
691 * Suff_IsTransform. If it is a transformation rule, its node is gotten
692 * from the suffix module via Suff_AddTransform rather than the standard
693 * Targ_FindNode in the target module.
694 *---------------------------------------------------------------------
697 ParseDoDependency(char *line)
699 char *cp; /* our current position */
700 GNode *gn; /* a general purpose temporary node */
701 int op; /* the operator on the line */
702 char savec; /* a place to save a character */
703 Lst paths; /* Search paths to alter when parsing .PATH targets */
704 int tOp; /* operator from special target */
706 const struct keyword *kw;
716 *cp && !isspace((unsigned char)*cp) && *cp != '(';
720 * Must be a dynamic source (would have been
721 * expanded otherwise), so call the Var module
722 * to parse the puppy so we can safely advance
723 * beyond it...There should be no errors in this
724 * as they would have been discovered in the
725 * initial Var_Subst and we wouldn't be here.
731 result = Var_Parse(cp, VAR_CMD, TRUE,
739 } else if (*cp == '!' || *cp == ':') {
741 * We don't want to end a word on ':' or '!' if
742 * there is a better match later on in the
743 * string (greedy matching).
744 * This allows the user to have targets like:
747 * where "fie::fi:fo" and "foo::bar" are the
748 * targets. In real life this is used for perl5
749 * library man pages where "::" separates an
750 * object from its class. Ie:
751 * "File::Spec::Unix". This behaviour is also
752 * consistent with other versions of make.
756 if (*cp == ':' && *p == ':')
759 /* Found the best match already. */
760 if (*p == '\0' || isspace(*p))
763 p += strcspn(p, "!:");
765 /* No better match later on... */
773 * Archives must be handled specially to make sure the
774 * OP_ARCHV flag is set in their 'type' field, for one
775 * thing, and because things like "archive(file1.o
776 * file2.o file3.o)" are permissible. Arch_ParseArchive
777 * will set 'line' to be the first non-blank after the
778 * archive-spec. It creates/finds nodes for the members
779 * and places them on the given list, returning TRUE
780 * if all went well and FALSE if there was an error in
781 * the specification. On error, line should remain
784 if (!Arch_ParseArchive(&line, &targets, VAR_CMD)) {
785 Parse_Error(PARSE_FATAL,
786 "Error in archive specification: \"%s\"",
798 * Ending a dependency line without an operator is a * Bozo no-no. As a heuristic, this is also often
799 * triggered by undetected conflicts from cvs/rcs
802 if (strncmp(line, "<<<<<<", 6) == 0 ||
803 strncmp(line, "======", 6) == 0 ||
804 strncmp(line, ">>>>>>", 6) == 0) {
805 Parse_Error(PARSE_FATAL, "Makefile appears to "
806 "contain unresolved cvs/rcs/??? merge "
809 Parse_Error(PARSE_FATAL, "Need an operator");
814 * Have a word in line. See if it's a special target and set
815 * specType to match it.
817 if (*line == '.' && isupper((unsigned char)line[1])) {
819 * See if the target is a special target that must have
820 * it or its sources handled specially.
822 if ((kw = ParseFindKeyword(line)) != NULL) {
823 if (specType == ExPath && kw->spec != ExPath) {
824 Parse_Error(PARSE_FATAL,
825 "Mismatched special targets");
833 * Certain special targets have special
835 * .PATH Have to set the dirSearchPath
837 * .MAIN Its sources are only used if
838 * nothing has been specified to
840 * .DEFAULT Need to create a node to hang
841 * commands on, but we don't want
842 * it in the graph, nor do we want
843 * it to be the Main Target, so we
844 * create it, set OP_NOTMAIN and
845 * add it to the list, setting
846 * DEFAULT to the new node for
847 * later use. We claim the node is
848 * A transformation rule to make
849 * life easier later, when we'll
850 * use Make_HandleUse to actually
851 * apply the .DEFAULT commands.
852 * .PHONY The list of targets
855 * .INTERRUPT Are not to be considered the
857 * .NOTPARALLEL Make only one target at a time.
858 * .SINGLESHELL Create a shell for each
860 * .ORDER Must set initial predecessor
865 Lst_AtEnd(&paths, &dirSearchPath);
868 if (!Lst_IsEmpty(&create)) {
875 gn = Targ_FindNode(line, TARG_CREATE);
876 gn->type |= OP_NOTMAIN;
877 Lst_AtEnd(&targets, gn);
880 gn = Targ_NewGN(".DEFAULT");
881 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
882 Lst_AtEnd(&targets, gn);
898 } else if (strncmp(line, ".PATH", 5) == 0) {
900 * .PATH<suffix> has to be handled specially.
901 * Call on the suffix module to give us a path
907 path = Suff_GetPath(&line[5]);
909 Parse_Error(PARSE_FATAL, "Suffix '%s' "
910 "not defined (yet)", &line[5]);
913 Lst_AtEnd(&paths, path);
918 * Have word in line. Get or create its node and stick it at
919 * the end of the targets list
921 if (specType == Not && *line != '\0') {
923 /* target names to be found and added to targets list */
924 Lst curTargs = Lst_Initializer(curTargs);
926 if (Dir_HasWildcards(line)) {
928 * Targets are to be sought only in the current
929 * directory, so create an empty path for the
930 * thing. Note we need to use Path_Clear in the
931 * destruction of the path as the Dir module
932 * could have added a directory to the path...
934 struct Path emptyPath =
935 TAILQ_HEAD_INITIALIZER(emptyPath);
937 Path_Expand(line, &emptyPath, &curTargs);
938 Path_Clear(&emptyPath);
942 * No wildcards, but we want to avoid code
943 * duplication, so create a list with the word
946 Lst_AtEnd(&curTargs, line);
949 while (!Lst_IsEmpty(&curTargs)) {
950 char *targName = Lst_DeQueue(&curTargs);
952 if (!Suff_IsTransform (targName)) {
953 gn = Targ_FindNode(targName,
956 gn = Suff_AddTransform(targName);
959 Lst_AtEnd(&targets, gn);
961 } else if (specType == ExPath && *line != '.' && *line != '\0'){
962 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored",
968 * If it is a special type and not .PATH, it's the only
969 * target we allow on this line...
971 if (specType != Not && specType != ExPath) {
972 Boolean warnFlag = FALSE;
974 while (*cp != '!' && *cp != ':' && *cp) {
975 if (*cp != ' ' && *cp != '\t') {
981 Parse_Error(PARSE_WARNING,
982 "Extra target ignored");
985 while (*cp && isspace((unsigned char)*cp)) {
990 } while (*line != '!' && *line != ':' && *line);
992 if (!Lst_IsEmpty(&targets)) {
995 Parse_Error(PARSE_WARNING, "Special and mundane "
996 "targets don't mix. Mundane ones ignored");
1003 * These four create nodes on which to hang commands, so
1004 * targets shouldn't be empty...
1008 * Nothing special here -- targets can be empty if it
1016 * Have now parsed all the target names. Must parse the operator next.
1017 * The result is left in op.
1021 } else if (*cp == ':') {
1029 Parse_Error(PARSE_FATAL, "Missing dependency operator");
1033 cp++; /* Advance beyond operator */
1038 * Get to the first source
1040 while (*cp && isspace((unsigned char)*cp)) {
1046 * Several special targets take different actions if present with no
1048 * a .SUFFIXES line with no sources clears out all old suffixes
1049 * a .PRECIOUS line makes all targets precious
1050 * a .IGNORE line ignores errors for all targets
1051 * a .SILENT line creates silence when making all targets
1052 * a .PATH removes all directories from the search path(s).
1057 Suff_ClearSuffixes();
1063 ignoreErrors = TRUE;
1069 LST_FOREACH(ln, &paths)
1070 Path_Clear(Lst_Datum(ln));
1073 Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1079 } else if (specType == MFlags) {
1081 * Call on functions in main.c to deal with these arguments and
1082 * set the initial character to a null-character so the loop to
1083 * get sources won't get anything
1085 Main_ParseArgLine(line, 0);
1088 } else if (specType == Warn) {
1092 } else if (specType == ExShell) {
1093 if (!Shell_Parse(line)) {
1094 Parse_Error(PARSE_FATAL,
1095 "improper shell specification");
1100 } else if (specType == NotParallel || specType == SingleShell) {
1105 * NOW GO FOR THE SOURCES
1107 if (specType == Suffixes || specType == ExPath ||
1108 specType == Includes || specType == Libs ||
1112 * If the target was one that doesn't take files as its
1113 * sources but takes something like suffixes, we take
1114 * each space-separated word on the line as a something
1115 * and deal with it accordingly.
1117 * If the target was .SUFFIXES, we take each source as
1118 * a suffix and add it to the list of suffixes
1119 * maintained by the Suff module.
1121 * If the target was a .PATH, we add the source as a
1122 * directory to search on the search path.
1124 * If it was .INCLUDES, the source is taken to be the
1125 * suffix of files which will be #included and whose
1126 * search path should be present in the .INCLUDES
1129 * If it was .LIBS, the source is taken to be the
1130 * suffix of files which are considered libraries and
1131 * whose search path should be present in the .LIBS
1134 * If it was .NULL, the source is the suffix to use
1135 * when a file has no valid suffix.
1138 while (*cp && !isspace((unsigned char)*cp)) {
1145 Suff_AddSuffix(line);
1148 LST_FOREACH(ln, &paths)
1149 Path_AddDir(Lst_Datum(ln), line);
1152 Suff_AddInclude(line);
1164 if (savech != '\0') {
1167 while (*cp && isspace((unsigned char)*cp)) {
1172 Lst_Destroy(&paths, NOFREE);
1174 } else if (specType == ExportVar) {
1175 Var_SetEnv(line, VAR_GLOBAL);
1178 /* list of sources in order */
1179 Lst curSrcs = Lst_Initializer(curSrc);
1183 * The targets take real sources, so we must beware of
1184 * archive specifications (i.e. things with left
1185 * parentheses in them) and handle them accordingly.
1187 while (*cp && !isspace((unsigned char)*cp)) {
1188 if (*cp == '(' && cp > line && cp[-1] != '$') {
1190 * Only stop for a left parenthesis if
1191 * it isn't at the start of a word
1192 * (that'll be for variable changes
1193 * later) and isn't preceded by a dollar
1194 * sign (a dynamic source).
1205 /* list of archive source names after exp. */
1206 Lst sources = Lst_Initializer(sources);
1208 if (!Arch_ParseArchive(&line, &sources,
1210 Parse_Error(PARSE_FATAL, "Error in "
1211 "source archive spec \"%s\"", line);
1215 while (!Lst_IsEmpty(&sources)) {
1216 gnp = Lst_DeQueue(&sources);
1217 ParseDoSrc(tOp, gnp->name, &curSrcs);
1226 ParseDoSrc(tOp, line, &curSrcs);
1228 while (*cp && isspace((unsigned char)*cp)) {
1233 Lst_Destroy(&curSrcs, NOFREE);
1236 if (mainNode == NULL) {
1238 * If we have yet to decide on a main target to make, in the
1239 * absence of any user input, we want the first target on
1240 * the first dependency line that is actually a real target
1241 * (i.e. isn't a .USE or .EXEC rule) to be made.
1243 LST_FOREACH(ln, &targets) {
1245 if ((gn->type & (OP_NOTMAIN | OP_USE |
1246 OP_EXEC | OP_TRANSFORM)) == 0) {
1256 *---------------------------------------------------------------------
1258 * Return TRUE if the passed line is a variable assignment. A variable
1259 * assignment consists of a single word followed by optional whitespace
1260 * followed by either a += or an = operator.
1261 * This function is used both by the Parse_File function and main when
1262 * parsing the command-line arguments.
1265 * TRUE if it is. FALSE if it ain't
1269 *---------------------------------------------------------------------
1272 Parse_IsVar(char *line)
1274 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1275 Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1278 #define ISEQOPERATOR(c) \
1279 ((c) == '+' || (c) == ':' || (c) == '?' || (c) == '!')
1282 * Skip to variable name
1284 for (; *line == ' ' || *line == '\t'; line++)
1287 for (; *line != '=' || level != 0; line++) {
1291 * end-of-line -- can't be a variable assignment.
1298 * there can be as much white space as desired so long
1299 * as there is only one word before the operator
1315 if (wasSpace && haveName) {
1316 if (ISEQOPERATOR(*line)) {
1318 * We must have a finished word
1324 * When an = operator [+?!:] is found,
1325 * the next character must be an = or
1326 * it ain't a valid assignment.
1332 * This is a shell command
1334 if (strncmp(line, ":sh", 3) == 0)
1339 * This is the start of another word, so not
1356 *---------------------------------------------------------------------
1358 * Take the variable assignment in the passed line and do it in the
1361 * Note: There is a lexical ambiguity with assignment modifier characters
1362 * in variable names. This routine interprets the character before the =
1363 * as a modifier. Therefore, an assignment like
1365 * is interpreted as "C+ +=" instead of "C++ =".
1371 * the variable structure of the given variable name is altered in the
1373 *---------------------------------------------------------------------
1376 Parse_DoVar(char *line, GNode *ctxt)
1378 char *cp; /* pointer into line */
1384 } type; /* Type of assignment */
1385 char *opc; /* ptr to operator character to
1386 * null-terminate the variable name */
1389 * Skip to variable name
1391 while (*line == ' ' || *line == '\t') {
1396 * Skip to operator character, nulling out whitespace as we go
1398 for (cp = line + 1; *cp != '='; cp++) {
1399 if (isspace((unsigned char)*cp)) {
1403 opc = cp - 1; /* operator is the previous character */
1404 *cp++ = '\0'; /* nuke the = */
1407 * Check operator type
1417 * If the variable already has a value, we don't do anything.
1420 if (Var_Exists(line, ctxt)) {
1439 while (*opc != ':') {
1446 if (strncmp(opc, ":sh", 3) == 0) {
1456 while (isspace((unsigned char)*cp)) {
1460 if (type == VAR_APPEND) {
1461 Var_Append(line, cp, ctxt);
1463 } else if (type == VAR_SUBST) {
1465 * Allow variables in the old value to be undefined, but leave
1466 * their invocation alone -- this is done by forcing oldVars
1468 * XXX: This can cause recursive variables, but that's not
1469 * hard to do, and this allows someone to do something like
1471 * CFLAGS = $(.INCLUDES)
1472 * CFLAGS := -I.. $(CFLAGS)
1474 * And not get an error.
1476 Boolean oldOldVars = oldVars;
1481 * make sure that we set the variable the first time to nothing
1482 * so that it gets substituted!
1484 if (!Var_Exists(line, ctxt))
1485 Var_Set(line, "", ctxt);
1487 cp = Buf_Peel(Var_Subst(cp, ctxt, FALSE));
1489 oldVars = oldOldVars;
1491 Var_Set(line, cp, ctxt);
1494 } else if (type == VAR_SHELL) {
1496 * TRUE if the command needs to be freed, i.e.
1497 * if any variable expansion was performed
1499 Boolean freeCmd = FALSE;
1503 if (strchr(cp, '$') != NULL) {
1505 * There's a dollar sign in the command, so perform
1506 * variable expansion on the whole thing. The
1507 * resulting string will need freeing when we're done,
1508 * so set freeCmd to TRUE.
1510 cp = Buf_Peel(Var_Subst(cp, VAR_CMD, TRUE));
1514 buf = Cmd_Exec(cp, &error);
1515 Var_Set(line, Buf_Data(buf), ctxt);
1516 Buf_Destroy(buf, TRUE);
1519 Parse_Error(PARSE_WARNING, error, cp);
1526 * Normal assignment -- just do it.
1528 Var_Set(line, cp, ctxt);
1533 *-----------------------------------------------------------------------
1534 * ParseHasCommands --
1535 * Callback procedure for Parse_File when destroying the list of
1536 * targets on the last dependency line. Marks a target as already
1537 * having commands if it does, to keep from having shell commands
1538 * on multiple dependency lines.
1544 * OP_HAS_COMMANDS may be set for the target.
1546 *-----------------------------------------------------------------------
1549 ParseHasCommands(void *gnp)
1553 if (!Lst_IsEmpty(&gn->commands)) {
1554 gn->type |= OP_HAS_COMMANDS;
1559 *-----------------------------------------------------------------------
1560 * Parse_AddIncludeDir --
1561 * Add a directory to the path searched for included makefiles
1562 * bracketed by double-quotes. Used by functions in main.c
1568 * The directory is appended to the list.
1570 *-----------------------------------------------------------------------
1573 Parse_AddIncludeDir(char *dir)
1576 Path_AddDir(&parseIncPath, dir);
1580 *---------------------------------------------------------------------
1581 * Parse_FromString --
1582 * Start Parsing from the given string
1588 * A structure is added to the includes Lst and readProc, curFile.lineno,
1589 * curFile.fname and curFile.F are altered for the new file
1590 *---------------------------------------------------------------------
1593 Parse_FromString(char *str, int lineno)
1596 DEBUGF(FOR, ("%s\n---- at line %d\n", str, lineno));
1598 ParsePushInput(estrdup(CURFILE->fname), NULL, str, lineno);
1603 *---------------------------------------------------------------------
1604 * ParseTraditionalInclude --
1605 * Push to another file.
1607 * The input is the line minus the "include". The file name is
1608 * the string following the "include".
1614 * A structure is added to the includes Lst and readProc, curFile.lineno,
1615 * curFile.fname and curFile.F are altered for the new file
1616 *---------------------------------------------------------------------
1619 ParseTraditionalInclude(char *file)
1621 char *fullname; /* full pathname of file */
1622 char *cp; /* current position in file spec */
1625 * Skip over whitespace
1627 while (*file == ' ' || *file == '\t') {
1631 if (*file == '\0') {
1632 Parse_Error(PARSE_FATAL, "Filename missing from \"include\"");
1637 * Skip to end of line or next whitespace
1639 for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1646 * Substitute for any variables in the file name before trying to
1649 file = Buf_Peel(Var_Subst(file, VAR_CMD, FALSE));
1652 * Now we know the file's name, we attempt to find the durn thing.
1653 * Search for it first on the -I search path, then on the .PATH
1654 * search path, if not found in a -I directory.
1656 fullname = Path_FindFile(file, &parseIncPath);
1657 if (fullname == NULL) {
1658 fullname = Path_FindFile(file, &dirSearchPath);
1661 if (fullname == NULL) {
1663 * Still haven't found the makefile. Look for it on the system
1664 * path as a last resort.
1666 fullname = Path_FindFile(file, &sysIncPath);
1669 if (fullname == NULL) {
1670 Parse_Error(PARSE_FATAL, "Could not find %s", file);
1671 /* XXXHB free(file) */
1675 /* XXXHB free(file) */
1678 * We set up the name of the file to be the absolute
1679 * name of the include file so error messages refer to the right
1682 ParsePushInput(fullname, NULL, NULL, 0);
1687 *---------------------------------------------------------------------
1689 * Read a character from the current file
1692 * The character that was read
1695 *---------------------------------------------------------------------
1701 if (CURFILE->F != NULL)
1702 return (fgetc(CURFILE->F));
1704 if (CURFILE->str != NULL && *CURFILE->ptr != '\0')
1705 return (*CURFILE->ptr++);
1712 *---------------------------------------------------------------------
1714 * Put back a character to the current file
1720 *---------------------------------------------------------------------
1726 if (CURFILE->F != NULL) {
1727 ungetc(c, CURFILE->F);
1730 if (CURFILE->str != NULL) {
1731 *--(CURFILE->ptr) = c;
1737 * Grab the next line unless it begins with a dot (`.') and we're told to
1738 * ignore such lines.
1741 ParseSkipLine(int skip, int keep_newline)
1747 buf = Buf_Init(MAKE_BSIZE);
1753 while (((c = ParseReadc()) != '\n' || lastc == '\\')
1755 if (skip && c == '#' && lastc != '\\') {
1757 * let a comment be terminated even by an
1758 * escaped \n. This is consistent to comment
1759 * handling in ParseReadLine
1761 while ((c = ParseReadc()) != '\n' && c != EOF)
1767 Buf_AddByte(buf, (Byte)c);
1769 Buf_ReplaceLastByte(buf, (Byte)' ');
1772 while ((c = ParseReadc()) == ' ' || c == '\t')
1779 Buf_AddByte(buf, (Byte)c);
1784 Parse_Error(PARSE_FATAL,
1785 "Unclosed conditional/for loop");
1786 Buf_Destroy(buf, TRUE);
1791 Buf_AddByte(buf, (Byte)'\0');
1792 line = Buf_Data(buf);
1793 } while (skip == 1 && line[0] != '.');
1795 Buf_Destroy(buf, FALSE);
1800 *---------------------------------------------------------------------
1802 * Read an entire line from the input file. Called only by Parse_File.
1803 * To facilitate escaped newlines and what have you, a character is
1804 * buffered in 'lastc', which is '\0' when no characters have been
1805 * read. When we break out of the loop, c holds the terminating
1806 * character and lastc holds a character that should be added to
1807 * the line (unless we don't read anything but a terminator).
1810 * A line w/o its newline
1813 * Only those associated with reading a character
1814 *---------------------------------------------------------------------
1819 Buffer *buf; /* Buffer for current line */
1820 int c; /* the current character */
1821 int lastc; /* The most-recent character */
1822 Boolean semiNL; /* treat semi-colons as newlines */
1823 Boolean ignDepOp; /* TRUE if should ignore dependency operators
1824 * for the purposes of setting semiNL */
1825 Boolean ignComment; /* TRUE if should ignore comments (in a
1827 char *line; /* Result */
1828 char *ep; /* to strip trailing blanks */
1838 * Handle tab at the beginning of the line. A leading tab (shell
1839 * command) forces us to ignore comments and dependency operators and
1840 * treat semi-colons as semi-colons (by leaving semiNL FALSE).
1841 * This also discards completely blank lines.
1846 if (ParsePopInput() == DONE) {
1847 /* End of all inputs - return NULL */
1854 ignComment = ignDepOp = TRUE;
1865 buf = Buf_Init(MAKE_BSIZE);
1867 while (((c = ParseReadc()) != '\n' || lastc == '\\') && c != EOF) {
1872 * Escaped newline: read characters until a
1873 * non-space or an unescaped newline and
1874 * replace them all by a single space. This is
1875 * done by storing the space over the backslash
1876 * and dropping through with the next nonspace.
1877 * If it is a semi-colon and semiNL is TRUE,
1878 * it will be recognized as a newline in the
1879 * code below this...
1883 while ((c = ParseReadc()) == ' ' || c == '\t') {
1886 if (c == EOF || c == '\n') {
1890 * Check for comments, semiNL's, etc. --
1891 * easier than ParseUnreadc(c);
1901 * Semi-colon: Need to see if it should be
1902 * interpreted as a newline
1906 * To make sure the command that may
1907 * be following this semi-colon begins
1908 * with a tab, we push one back into the
1909 * input stream. This will overwrite the
1910 * semi-colon in the buffer. If there is
1911 * no command following, this does no
1912 * harm, since the newline remains in
1913 * the buffer and the
1914 * whole line is ignored.
1923 * Haven't seen a dependency operator
1924 * before this, so this must be a
1925 * variable assignment -- don't pay
1926 * attention to dependency operators
1930 } else if (lastc == ':' || lastc == '!') {
1932 * Well, we've seen a dependency
1933 * operator already, but it was the
1934 * previous character, so this is really
1935 * just an expanded variable assignment.
1936 * Revert semi-colons to being just
1937 * semi-colons again and ignore any more
1938 * dependency operators.
1940 * XXX: Note that a line like
1941 * "foo : a:=b" will blow up, but who'd
1942 * write a line like that anyway?
1950 if (lastc != '\\') {
1952 * If the character is a hash
1953 * mark and it isn't escaped
1954 * (or we're being compatible),
1955 * the thing is a comment.
1956 * Skip to the end of the line.
1960 } while (c != '\n' && c != EOF);
1964 * Don't add the backslash.
1965 * Just let the # get copied
1978 * A semi-colon is recognized as a
1979 * newline only on dependency lines.
1980 * Dependency lines are lines with a
1981 * colon or an exclamation point.
1992 * Copy in the previous character (there may be none if this
1993 * was the first character) and save this one in
1997 Buf_AddByte(buf, (Byte)lastc);
2003 if (lastc != '\0') {
2004 Buf_AddByte(buf, (Byte)lastc);
2006 Buf_AddByte(buf, (Byte)'\0');
2007 line = Buf_Peel(buf);
2010 * Strip trailing blanks and tabs from the line.
2011 * Do not strip a blank or tab that is preceded by
2017 while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
2018 if (ep > line + 1 && ep[-2] == '\\')
2024 if (line[0] == '\0') {
2025 /* empty line - just ignore */
2034 *-----------------------------------------------------------------------
2035 * ParseFinishLine --
2036 * Handle the end of a dependency group.
2042 * inLine set FALSE. 'targets' list destroyed.
2044 *-----------------------------------------------------------------------
2047 ParseFinishLine(void)
2052 LST_FOREACH(ln, &targets) {
2053 if (((const GNode *)Lst_Datum(ln))->type & OP_TRANSFORM)
2054 Suff_EndTransform(Lst_Datum(ln));
2056 Lst_Destroy(&targets, ParseHasCommands);
2063 * Parse an .include directive and push the file onto the input stack.
2064 * The input is the line minus the .include. A file spec is a string
2065 * enclosed in <> or "". The former is looked for only in sysIncPath.
2066 * The latter in . and the directories specified by -I command line
2070 xparse_include(char *file, int sinclude)
2072 char *fullname; /* full pathname of file */
2073 char endc; /* the character which ends the file spec */
2074 char *cp; /* current position in file spec */
2075 Boolean isSystem; /* TRUE if makefile is a system makefile */
2076 char *prefEnd, *Fname;
2080 * Skip to delimiter character so we know where to look
2082 while (*file == ' ' || *file == '\t') {
2086 if (*file != '"' && *file != '<') {
2087 Parse_Error(PARSE_FATAL,
2088 ".include filename must be delimited by '\"' or '<'");
2093 * Set the search path on which to find the include file based on the
2094 * characters which bracket its name. Angle-brackets imply it's
2095 * a system Makefile while double-quotes imply it's a user makefile
2106 * Skip to matching delimiter
2108 for (cp = ++file; *cp != endc; cp++) {
2110 Parse_Error(PARSE_FATAL,
2111 "Unclosed .include filename. '%c' expected", endc);
2118 * Substitute for any variables in the file name before trying to
2121 file = Buf_Peel(Var_Subst(file, VAR_CMD, FALSE));
2124 * Now we know the file's name and its search path, we attempt to
2125 * find the durn thing. A return of NULL indicates the file don't
2130 * Include files contained in double-quotes are first searched
2131 * for relative to the including file's location. We don't want
2132 * to cd there, of course, so we just tack on the old file's
2133 * leading path components and call Path_FindFile to see if
2134 * we can locate the beast.
2137 /* Make a temporary copy of this, to be safe. */
2138 Fname = estrdup(CURFILE->fname);
2140 prefEnd = strrchr(Fname, '/');
2141 if (prefEnd != NULL) {
2144 newName = estrdup(file);
2146 newName = str_concat(Fname, file, STR_ADDSLASH);
2147 fullname = Path_FindFile(newName, &parseIncPath);
2148 if (fullname == NULL) {
2149 fullname = Path_FindFile(newName,
2158 if (fullname == NULL) {
2160 * Makefile wasn't found in same directory as included
2161 * makefile. Search for it first on the -I search path,
2162 * then on the .PATH search path, if not found in a -I
2164 * XXX: Suffix specific?
2166 fullname = Path_FindFile(file, &parseIncPath);
2167 if (fullname == NULL) {
2168 fullname = Path_FindFile(file, &dirSearchPath);
2175 if (fullname == NULL) {
2177 * System makefile or still haven't found the makefile.
2178 * Look for it on the system path.
2180 fullname = Path_FindFile(file, &sysIncPath);
2183 if (fullname == NULL) {
2186 Parse_Error(PARSE_FATAL, "Could not find %s", file);
2193 * We set up the name of the file to be the absolute
2194 * name of the include file so error messages refer to the right
2197 ParsePushInput(fullname, NULL, NULL, 0);
2198 DEBUGF(DIR, (".include %s\n", fullname));
2202 parse_include(char *file, int code __unused, int lineno __unused)
2204 xparse_include(file, 0);
2208 parse_sinclude(char *file, int code __unused, int lineno __unused)
2210 xparse_include(file, 1);
2215 * Parse a .warning or .error directive
2217 * The input is the line minus the ".error"/".warning". We substitute
2218 * variables, print the message and exit(1) (for .error) or just print
2219 * a warning if the directive is malformed.
2222 parse_message(char *line, int iserror, int lineno __unused)
2225 if (!isspace((u_char)*line)) {
2226 Parse_Error(PARSE_WARNING, "invalid syntax: .%s%s",
2227 iserror ? "error" : "warning", line);
2231 while (isspace((u_char)*line))
2234 line = Buf_Peel(Var_Subst(line, VAR_GLOBAL, FALSE));
2235 Parse_Error(iserror ? PARSE_FATAL : PARSE_WARNING, "%s", line);
2239 /* Terminate immediately. */
2246 * Parse an .undef directive.
2249 parse_undef(char *line, int code __unused, int lineno __unused)
2253 while (isspace((u_char)*line))
2256 for (cp = line; !isspace((u_char)*cp) && *cp != '\0'; cp++) {
2261 cp = Buf_Peel(Var_Subst(line, VAR_CMD, FALSE));
2262 Var_Delete(cp, VAR_GLOBAL);
2268 * Parse a .for directive.
2271 parse_for(char *line, int code __unused, int lineno)
2274 if (!For_For(line)) {
2281 * Skip after the matching endfor.
2285 line = ParseSkipLine(0, 1);
2287 Parse_Error(PARSE_FATAL,
2288 "Unexpected end of file in for loop.\n");
2291 } while (For_Eval(line));
2300 * Parse endfor. This may only happen if there was no matching .for.
2303 parse_endfor(char *line __unused, int code __unused, int lineno __unused)
2306 Parse_Error(PARSE_FATAL, "for-less endfor");
2311 * Got a line starting with a '.'. Check if this is a directive
2315 * TRUE if line was a directive, FALSE otherwise.
2318 parse_directive(char *line)
2326 * .[[:space:]]*\([[:alpha:]][[:alnum:]_]*\).*
2327 * \1 is the keyword.
2329 for (start = line; isspace((u_char)*start); start++) {
2333 if (!isalpha((u_char)*start)) {
2338 while (isalnum((u_char)*cp) || *cp == '_') {
2342 dir = directive_hash(start, cp - start);
2343 if (dir < 0 || dir >= (int)NDIRECTS ||
2344 (size_t)(cp - start) != strlen(directives[dir].name) ||
2345 strncmp(start, directives[dir].name, cp - start) != 0) {
2346 /* not actually matched */
2350 if (!skipLine || directives[dir].skip_flag)
2351 (*directives[dir].func)(cp, directives[dir].code,
2357 *---------------------------------------------------------------------
2359 * Parse a file into its component parts, incorporating it into the
2360 * current dependency graph. This is the main function and controls
2361 * almost every other function in this module
2367 * Loads. Nodes are added to the list of all targets, nodes and links
2368 * are added to the dependency graph. etc. etc. etc.
2369 *---------------------------------------------------------------------
2372 Parse_File(const char *name, FILE *stream)
2374 char *cp; /* pointer into the line */
2375 char *line; /* the line we're working on */
2380 ParsePushInput(estrdup(name), stream, NULL, 0);
2382 while ((line = ParseReadLine()) != NULL) {
2383 if (*line == '.' && parse_directive(line + 1)) {
2384 /* directive consumed */
2387 if (skipLine || *line == '#') {
2388 /* Skipping .if block or comment. */
2392 if (*line == '\t') {
2394 * If a line starts with a tab, it can only
2395 * hope to be a creation command.
2397 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2406 * So long as it's not a blank
2407 * line and we're actually in a
2408 * dependency spec, add the
2409 * command to the list of
2410 * commands of all targets in
2411 * the dependency spec.
2413 LST_FOREACH(ln, &targets) {
2421 if (!(gn->type & OP_HAS_COMMANDS))
2422 Lst_AtEnd(&gn->commands, cp);
2424 Parse_Error(PARSE_WARNING, "duplicate script "
2425 "for target \"%s\" ignored", gn->name);
2429 Parse_Error(PARSE_FATAL,
2430 "Unassociated shell command \"%s\"",
2435 } else if (strncmp(line, "include", 7) == 0 &&
2436 isspace((unsigned char)line[7]) &&
2437 strchr(line, ':') == NULL) {
2439 * It's an S3/S5-style "include".
2441 ParseTraditionalInclude(line + 7);
2444 } else if (Parse_IsVar(line)) {
2446 Parse_DoVar(line, VAR_GLOBAL);
2450 * We now know it's a dependency line so it
2451 * needs to have all variables expanded before
2452 * being parsed. Tell the variable module to
2453 * complain if some variable is undefined...
2454 * To make life easier on novices, if the line
2455 * is indented we first make sure the line has
2456 * a dependency operator in it. If it doesn't
2457 * have an operator and we're in a dependency
2458 * line's script, we assume it's actually a
2459 * shell command and add it to the current
2460 * list of targets. XXX this comment seems wrong.
2463 if (isspace((unsigned char)line[0])) {
2464 while (*cp != '\0' &&
2465 isspace((unsigned char)*cp)) {
2475 cp = Buf_Peel(Var_Subst(line, VAR_CMD, TRUE));
2481 * Need a non-circular list for the target nodes
2483 Lst_Destroy(&targets, NOFREE);
2486 ParseDoDependency(line);
2496 * Make sure conditionals are clean
2501 errx(1, "fatal errors encountered -- cannot continue");
2505 *-----------------------------------------------------------------------
2507 * Return a Lst of the main target to create for main()'s sake. If
2508 * no such target exists, we Punt with an obnoxious error message.
2511 * A Lst of the single node to create.
2516 *-----------------------------------------------------------------------
2519 Parse_MainName(Lst *listmain)
2522 if (mainNode == NULL) {
2523 Punt("no target to make.");
2525 } else if (mainNode->type & OP_DOUBLEDEP) {
2526 Lst_AtEnd(listmain, mainNode);
2527 Lst_Concat(listmain, &mainNode->cohorts, LST_CONCNEW);
2529 Lst_AtEnd(listmain, mainNode);