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.74 2005/04/22 16:01:03 okumoto Exp $
45 * Functions to parse a makefile.
47 * Most important structures are kept in Lsts. Directories for
48 * the #include "..." function are kept in the 'parseIncPath' Lst, while
49 * those for the #include <...> are kept in the 'sysIncPath' Lst. The
50 * targets currently being defined are kept in the 'targets' Lst.
54 * Parse_File Function used to parse a makefile. It must
55 * be given the name of the file, which should
56 * already have been opened, and a function
57 * to call to read a character from the file.
59 * Parse_IsVar Returns TRUE if the given line is a
60 * variable assignment. Used by MainParseArgs
61 * to determine if an argument is a target
62 * or a variable assignment. Used internally
63 * for pretty much the same thing...
65 * Parse_Error Function called when an error occurs in
66 * parsing. Used by the variable and
67 * conditional modules.
69 * Parse_MainName Returns a Lst of the main target to create.
84 #include "directive_hash.h"
91 #include "pathnames.h"
99 * These values are returned by ParsePopInput to tell Parse_File whether to
100 * CONTINUE parsing, i.e. it had only reached the end of an include file,
106 /* targets we're working on */
107 static Lst targets = Lst_Initializer(targets);
109 /* true if currently in a dependency line or its commands */
110 static Boolean inLine;
112 static int fatals = 0;
115 * The main target to create. This is the first target on the
116 * first dependency line in the first makefile.
118 static GNode *mainNode;
121 * Definitions for handling #include specifications
124 char *fname; /* name of previous file */
125 int lineno; /* saved line number */
126 FILE *F; /* the open stream */
127 char *str; /* the string when parsing a string */
128 char *ptr; /* the current pointer when parsing a string */
129 TAILQ_ENTRY(IFile) link;/* stack the files */
132 /* stack of IFiles generated by * #includes */
133 static TAILQ_HEAD(, IFile) includes = TAILQ_HEAD_INITIALIZER(includes);
135 /* access current file */
136 #define CURFILE (TAILQ_FIRST(&includes))
138 /* list of directories for "..." includes */
139 struct Path parseIncPath = TAILQ_HEAD_INITIALIZER(parseIncPath);
141 /* list of directories for <...> includes */
142 struct Path sysIncPath = TAILQ_HEAD_INITIALIZER(sysIncPath);
145 * specType contains the SPECial TYPE of the current target. It is
146 * Not if the target is unspecial. If it *is* special, however, the children
147 * are linked as children of the parent but not vice versa. This variable is
148 * set in ParseDoDependency
152 Default, /* .DEFAULT */
154 ExportVar, /* .EXPORTVAR */
155 Ignore, /* .IGNORE */
156 Includes, /* .INCLUDES */
157 Interrupt, /* .INTERRUPT */
159 MFlags, /* .MFLAGS or .MAKEFLAGS */
160 Main, /* .MAIN and we don't have anyth. user-spec. to make */
161 Not, /* Not special */
162 NotParallel, /* .NOTPARALELL */
165 Parallel, /* .PARALLEL */
169 Precious, /* .PRECIOUS */
170 ExShell, /* .SHELL */
171 Silent, /* .SILENT */
172 SingleShell, /* .SINGLESHELL */
173 Suffixes, /* .SUFFIXES */
175 Attribute /* Generic attribute */
178 static ParseSpecial specType;
182 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
183 * seen, then set to each successive source on the line.
185 static GNode *predecessor;
188 * The parseKeywords table is searched using binary search when deciding
189 * if a target or source is special. The 'spec' field is the ParseSpecial
190 * type of the keyword ("Not" if the keyword isn't special as a target) while
191 * the 'op' field is the operator to apply to the list of targets if the
192 * keyword is used as a source ("0" if the keyword isn't special as a source)
195 const char *name; /* Name of keyword */
196 ParseSpecial spec; /* Type when used as a target */
197 int op; /* Operator when used as a source */
198 } parseKeywords[] = {
199 { ".BEGIN", Begin, 0 },
200 { ".DEFAULT", Default, 0 },
202 { ".EXEC", Attribute, OP_EXEC },
203 { ".EXPORTVAR", ExportVar, 0 },
204 { ".IGNORE", Ignore, OP_IGNORE },
205 { ".INCLUDES", Includes, 0 },
206 { ".INTERRUPT", Interrupt, 0 },
207 { ".INVISIBLE", Attribute, OP_INVISIBLE },
208 { ".JOIN", Attribute, OP_JOIN },
209 { ".LIBS", Libs, 0 },
210 { ".MAIN", Main, 0 },
211 { ".MAKE", Attribute, OP_MAKE },
212 { ".MAKEFLAGS", MFlags, 0 },
213 { ".MFLAGS", MFlags, 0 },
214 { ".NOTMAIN", Attribute, OP_NOTMAIN },
215 { ".NOTPARALLEL", NotParallel, 0 },
216 { ".NO_PARALLEL", NotParallel, 0 },
217 { ".NULL", Null, 0 },
218 { ".OPTIONAL", Attribute, OP_OPTIONAL },
219 { ".ORDER", Order, 0 },
220 { ".PARALLEL", Parallel, 0 },
221 { ".PATH", ExPath, 0 },
222 { ".PHONY", Phony, OP_PHONY },
223 { ".POSIX", Posix, 0 },
224 { ".PRECIOUS", Precious, OP_PRECIOUS },
225 { ".RECURSIVE", Attribute, OP_MAKE },
226 { ".SHELL", ExShell, 0 },
227 { ".SILENT", Silent, OP_SILENT },
228 { ".SINGLESHELL", SingleShell, 0 },
229 { ".SUFFIXES", Suffixes, 0 },
230 { ".USE", Attribute, OP_USE },
231 { ".WAIT", Wait, 0 },
234 static void parse_include(char *, int, int);
235 static void parse_message(char *, int, int);
236 static void parse_makeenv(char *, int, int);
237 static void parse_undef(char *, int, int);
238 static void parse_for(char *, int, int);
239 static void parse_endfor(char *, int, int);
241 static const struct directive {
244 Boolean skip_flag; /* execute even when skipped */
245 void (*func)(char *, int, int);
247 /* DIRECTIVES-START-TAG */
248 { "elif", COND_ELIF, TRUE, Cond_If },
249 { "elifdef", COND_ELIFDEF, TRUE, Cond_If },
250 { "elifmake", COND_ELIFMAKE, TRUE, Cond_If },
251 { "elifndef", COND_ELIFNDEF, TRUE, Cond_If },
252 { "elifnmake", COND_ELIFNMAKE, TRUE, Cond_If },
253 { "else", COND_ELSE, TRUE, Cond_Else },
254 { "endfor", 0, FALSE, parse_endfor },
255 { "endif", COND_ENDIF, TRUE, Cond_Endif },
256 { "error", 1, FALSE, parse_message },
257 { "for", 0, FALSE, parse_for },
258 { "if", COND_IF, TRUE, Cond_If },
259 { "ifdef", COND_IFDEF, TRUE, Cond_If },
260 { "ifmake", COND_IFMAKE, TRUE, Cond_If },
261 { "ifndef", COND_IFNDEF, TRUE, Cond_If },
262 { "ifnmake", COND_IFNMAKE, TRUE, Cond_If },
263 { "include", 0, FALSE, parse_include },
264 { "makeenv", 0, FALSE, parse_makeenv },
265 { "undef", 0, FALSE, parse_undef },
266 { "warning", 0, FALSE, parse_message },
267 /* DIRECTIVES-END-TAG */
269 #define NDIRECTS (sizeof(directives) / sizeof(directives[0]))
272 *----------------------------------------------------------------------
273 * ParseFindKeyword --
274 * Look in the table of keywords for one matching the given string.
277 * The index of the keyword, or -1 if it isn't there.
281 *----------------------------------------------------------------------
284 ParseFindKeyword(char *str)
292 end = (sizeof(parseKeywords) / sizeof(parseKeywords[0])) - 1;
295 cur = start + (end - start) / 2;
296 diff = strcmp(str, parseKeywords[cur].name);
299 } else if (diff < 0) {
304 } while (start <= end);
311 * Error message abort function for parsing. Prints out the context
312 * of the error (line number and file) as well as the message with
313 * two optional arguments.
319 * "fatals" is incremented if the level is PARSE_FATAL.
323 Parse_Error(int type, const char *fmt, ...)
329 fprintf(stderr, "\"%s\", line %d: ",
330 CURFILE->fname, CURFILE->lineno);
331 if (type == PARSE_WARNING)
332 fprintf(stderr, "warning: ");
333 vfprintf(stderr, fmt, ap);
335 fprintf(stderr, "\n");
337 if (type == PARSE_FATAL)
344 * Push a new input source onto the input stack. If ptr is NULL
345 * the fullname is used to fopen the file. If it is not NULL,
346 * ptr is assumed to point to the string to be parsed. If opening the
347 * file fails, the fullname is freed.
350 ParsePushInput(char *fullname, FILE *fp, char *ptr, int lineno)
354 nf = emalloc(sizeof(*nf));
355 nf->fname = fullname;
359 /* the input source is a file */
360 if ((nf->F = fp) == NULL) {
361 nf->F = fopen(fullname, "r");
363 Parse_Error(PARSE_FATAL, "Cannot open %s",
370 nf->str = nf->ptr = NULL;
371 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
373 nf->str = nf->ptr = ptr;
376 TAILQ_INSERT_HEAD(&includes, nf, link);
381 * Called when EOF is reached in the current file. If we were reading
382 * an include file, the includes stack is popped and things set up
383 * to go back to reading the previous file at the previous location.
386 * CONTINUE if there's more to do. DONE if not.
389 * The old curFile.F is closed. The includes list is shortened.
390 * curFile.lineno, curFile.F, and curFile.fname are changed if
391 * CONTINUE is returned.
396 struct IFile *ifile; /* the state on the top of the includes stack */
398 assert(!TAILQ_EMPTY(&includes));
400 ifile = TAILQ_FIRST(&includes);
401 TAILQ_REMOVE(&includes, ifile, link);
404 if (ifile->F != NULL) {
406 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
408 if (ifile->str != NULL) {
413 return (TAILQ_EMPTY(&includes) ? DONE : CONTINUE);
417 *---------------------------------------------------------------------
419 * Link the parent nodes to their new child. Used by
420 * ParseDoDependency. If the specType isn't 'Not', the parent
421 * isn't linked as a parent of the child.
424 * New elements are added to the parents lists of cgn and the
425 * children list of cgn. the unmade field of pgn is updated
426 * to reflect the additional child.
427 *---------------------------------------------------------------------
430 ParseLinkSrc(Lst *parents, GNode *cgn)
435 LST_FOREACH(ln, parents) {
437 if (Lst_Member(&pgn->children, cgn) == NULL) {
438 Lst_AtEnd(&pgn->children, cgn);
439 if (specType == Not) {
440 Lst_AtEnd(&cgn->parents, pgn);
448 *---------------------------------------------------------------------
450 * Apply the parsed operator to all target nodes. Used in
451 * ParseDoDependency once all targets have been found and their
452 * operator parsed. If the previous and new operators are incompatible,
453 * a major error is taken.
456 * The type field of the node is altered to reflect any new bits in
458 *---------------------------------------------------------------------
467 LST_FOREACH(ln, &targets) {
471 * If the dependency mask of the operator and the node don't
472 * match and the node has actually had an operator applied to
473 * it before, and the operator actually has some dependency
474 * information in it, complain.
476 if ((op & OP_OPMASK) != (gn->type & OP_OPMASK) &&
477 !OP_NOP(gn->type) && !OP_NOP(op)) {
478 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s",
483 if (op == OP_DOUBLEDEP &&
484 (gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
486 * If the node was the object of a :: operator, we need
487 * to create a new instance of it for the children and
488 * commands on this dependency line. The new instance
489 * is placed on the 'cohorts' list of the initial one
490 * (note the initial one is not on its own cohorts list)
491 * and the new instance is linked to all parents of the
494 cohort = Targ_NewGN(gn->name);
497 * Duplicate links to parents so graph traversal is
498 * simple. Perhaps some type bits should be duplicated?
500 * Make the cohort invisible as well to avoid
501 * duplicating it into other variables. True, parents
502 * of this target won't tend to do anything with their
503 * local variables, but better safe than sorry.
505 ParseLinkSrc(&gn->parents, cohort);
506 cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
507 Lst_AtEnd(&gn->cohorts, cohort);
510 * Replace the node in the targets list with the
513 Lst_Replace(ln, cohort);
517 * We don't want to nuke any previous flags (whatever they were)
518 * so we just OR the new operator into the old
525 *---------------------------------------------------------------------
527 * Given the name of a source, figure out if it is an attribute
528 * and apply it to the targets if it is. Else decide if there is
529 * some attribute which should be applied *to* the source because
530 * of some special target and apply it if so. Otherwise, make the
531 * source be a child of the targets in the list 'targets'
537 * Operator bits may be added to the list of targets or to the source.
538 * The targets may have a new source added to their lists of children.
539 *---------------------------------------------------------------------
542 ParseDoSrc(int tOp, char *src, Lst *allsrc)
546 if (*src == '.' && isupper ((unsigned char) src[1])) {
547 int keywd = ParseFindKeyword(src);
549 if(parseKeywords[keywd].op != 0) {
550 ParseDoOp(parseKeywords[keywd].op);
553 if (parseKeywords[keywd].spec == Wait) {
563 * If we have noted the existence of a .MAIN, it means we need
564 * to add the sources of said target to the list of things
565 * to create. The string 'src' is likely to be free, so we
566 * must make a new copy of it. Note that this will only be
567 * invoked if the user didn't specify a target on the command
568 * line. This is to allow #ifmake's to succeed, or something...
570 Lst_AtEnd(&create, estrdup(src));
572 * Add the name to the .TARGETS variable as well, so the user
573 * can employ that, if desired.
575 Var_Append(".TARGETS", src, VAR_GLOBAL);
580 * Create proper predecessor/successor links between the
581 * previous source and the current one.
583 gn = Targ_FindNode(src, TARG_CREATE);
584 if (predecessor != NULL) {
585 Lst_AtEnd(&predecessor->successors, gn);
586 Lst_AtEnd(&gn->preds, predecessor);
589 * The current source now becomes the predecessor for the next
597 * If the source is not an attribute, we need to find/create
598 * a node for it. After that we can apply any operator to it
599 * from a special target or link it to its parents, as
602 * In the case of a source that was the object of a :: operator,
603 * the attribute is applied to all of its instances (as kept in
604 * the 'cohorts' list of the node) or all the cohorts are linked
605 * to all the targets.
607 gn = Targ_FindNode(src, TARG_CREATE);
611 ParseLinkSrc(&targets, gn);
613 if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
617 for (ln = Lst_First(&gn->cohorts); ln != NULL;
619 cohort = Lst_Datum(ln);
623 ParseLinkSrc(&targets, cohort);
631 Lst_AtEnd(allsrc, gn);
637 * Check if GNodes needs to be synchronized.
638 * This has to be when two nodes are on different sides of a
641 LST_FOREACH(ln, allsrc) {
644 if (p->order >= gn->order)
647 * XXX: This can cause loops, and loops can cause
648 * unmade targets, but checking is tedious, and the
649 * debugging output can show the problem
651 Lst_AtEnd(&p->successors, gn);
652 Lst_AtEnd(&gn->preds, p);
659 *---------------------------------------------------------------------
660 * ParseDoDependency --
661 * Parse the dependency line in line.
667 * The nodes of the sources are linked as children to the nodes of the
668 * targets. Some nodes may be created.
670 * We parse a dependency line by first extracting words from the line and
671 * finding nodes in the list of all targets with that name. This is done
672 * until a character is encountered which is an operator character. Currently
673 * these are only ! and :. At this point the operator is parsed and the
674 * pointer into the line advanced until the first source is encountered.
675 * The parsed operator is applied to each node in the 'targets' list,
676 * which is where the nodes found for the targets are kept, by means of
677 * the ParseDoOp function.
678 * The sources are read in much the same way as the targets were except
679 * that now they are expanded using the wildcarding scheme of the C-Shell
680 * and all instances of the resulting words in the list of all targets
681 * are found. Each of the resulting nodes is then linked to each of the
682 * targets as one of its children.
683 * Certain targets are handled specially. These are the ones detailed
684 * by the specType variable.
685 * The storing of transformation rules is also taken care of here.
686 * A target is recognized as a transformation rule by calling
687 * Suff_IsTransform. If it is a transformation rule, its node is gotten
688 * from the suffix module via Suff_AddTransform rather than the standard
689 * Targ_FindNode in the target module.
690 *---------------------------------------------------------------------
693 ParseDoDependency(char *line)
695 char *cp; /* our current position */
696 GNode *gn; /* a general purpose temporary node */
697 int op; /* the operator on the line */
698 char savec; /* a place to save a character */
699 Lst paths; /* Search paths to alter when parsing .PATH targets */
700 int tOp; /* operator from special target */
711 *cp && !isspace((unsigned char)*cp) && *cp != '(';
715 * Must be a dynamic source (would have been
716 * expanded otherwise), so call the Var module
717 * to parse the puppy so we can safely advance
718 * beyond it...There should be no errors in this
719 * as they would have been discovered in the
720 * initial Var_Subst and we wouldn't be here.
726 result = Var_Parse(cp, VAR_CMD, TRUE,
734 } else if (*cp == '!' || *cp == ':') {
736 * We don't want to end a word on ':' or '!' if
737 * there is a better match later on in the
738 * string (greedy matching).
739 * This allows the user to have targets like:
742 * where "fie::fi:fo" and "foo::bar" are the
743 * targets. In real life this is used for perl5
744 * library man pages where "::" separates an
745 * object from its class. Ie:
746 * "File::Spec::Unix". This behaviour is also
747 * consistent with other versions of make.
751 if (*cp == ':' && *p == ':')
754 /* Found the best match already. */
755 if (*p == '\0' || isspace(*p))
758 p += strcspn(p, "!:");
760 /* No better match later on... */
768 * Archives must be handled specially to make sure the
769 * OP_ARCHV flag is set in their 'type' field, for one
770 * thing, and because things like "archive(file1.o
771 * file2.o file3.o)" are permissible. Arch_ParseArchive
772 * will set 'line' to be the first non-blank after the
773 * archive-spec. It creates/finds nodes for the members
774 * and places them on the given list, returning SUCCESS
775 * if all went well and FAILURE if there was an error in
776 * the specification. On error, line should remain
779 if (Arch_ParseArchive(&line, &targets, VAR_CMD) !=
781 Parse_Error(PARSE_FATAL,
782 "Error in archive specification: \"%s\"",
794 * Ending a dependency line without an operator is a * Bozo no-no. As a heuristic, this is also often
795 * triggered by undetected conflicts from cvs/rcs
798 if (strncmp(line, "<<<<<<", 6) == 0 ||
799 strncmp(line, "======", 6) == 0 ||
800 strncmp(line, ">>>>>>", 6) == 0) {
801 Parse_Error(PARSE_FATAL, "Makefile appears to "
802 "contain unresolved cvs/rcs/??? merge "
805 Parse_Error(PARSE_FATAL, "Need an operator");
810 * Have a word in line. See if it's a special target and set
811 * specType to match it.
813 if (*line == '.' && isupper((unsigned char)line[1])) {
815 * See if the target is a special target that must have
816 * it or its sources handled specially.
818 int keywd = ParseFindKeyword(line);
821 if (specType == ExPath &&
822 parseKeywords[keywd].spec != ExPath) {
823 Parse_Error(PARSE_FATAL,
824 "Mismatched special targets");
828 specType = parseKeywords[keywd].spec;
829 tOp = parseKeywords[keywd].op;
832 * Certain special targets have special
834 * .PATH Have to set the dirSearchPath
836 * .MAIN Its sources are only used if
837 * nothing has been specified to
839 * .DEFAULT Need to create a node to hang
840 * commands on, but we don't want
841 * it in the graph, nor do we want
842 * it to be the Main Target, so we
843 * create it, set OP_NOTMAIN and
844 * add it to the list, setting
845 * DEFAULT to the new node for
846 * later use. We claim the node is
847 * A transformation rule to make
848 * life easier later, when we'll
849 * use Make_HandleUse to actually
850 * apply the .DEFAULT commands.
851 * .PHONY The list of targets
854 * .INTERRUPT Are not to be considered the
856 * .NOTPARALLEL Make only one target at a time.
857 * .SINGLESHELL Create a shell for each
859 * .ORDER Must set initial predecessor
864 Lst_AtEnd(&paths, &dirSearchPath);
867 if (!Lst_IsEmpty(&create)) {
874 gn = Targ_FindNode(line, TARG_CREATE);
875 gn->type |= OP_NOTMAIN;
876 Lst_AtEnd(&targets, gn);
879 gn = Targ_NewGN(".DEFAULT");
880 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
881 Lst_AtEnd(&targets, gn);
897 } else if (strncmp(line, ".PATH", 5) == 0) {
899 * .PATH<suffix> has to be handled specially.
900 * Call on the suffix module to give us a path
906 path = Suff_GetPath(&line[5]);
908 Parse_Error(PARSE_FATAL, "Suffix '%s' "
909 "not defined (yet)", &line[5]);
912 Lst_AtEnd(&paths, path);
917 * Have word in line. Get or create its node and stick it at
918 * the end of the targets list
920 if (specType == Not && *line != '\0') {
922 /* target names to be found and added to targets list */
923 Lst curTargs = Lst_Initializer(curTargs);
925 if (Dir_HasWildcards(line)) {
927 * Targets are to be sought only in the current
928 * directory, so create an empty path for the
929 * thing. Note we need to use Path_Clear in the
930 * destruction of the path as the Dir module
931 * could have added a directory to the path...
933 struct Path emptyPath =
934 TAILQ_HEAD_INITIALIZER(emptyPath);
936 Path_Expand(line, &emptyPath, &curTargs);
937 Path_Clear(&emptyPath);
941 * No wildcards, but we want to avoid code
942 * duplication, so create a list with the word
945 Lst_AtEnd(&curTargs, line);
948 while (!Lst_IsEmpty(&curTargs)) {
949 char *targName = Lst_DeQueue(&curTargs);
951 if (!Suff_IsTransform (targName)) {
952 gn = Targ_FindNode(targName,
955 gn = Suff_AddTransform(targName);
958 Lst_AtEnd(&targets, gn);
960 } else if (specType == ExPath && *line != '.' && *line != '\0'){
961 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored",
967 * If it is a special type and not .PATH, it's the only
968 * target we allow on this line...
970 if (specType != Not && specType != ExPath) {
971 Boolean warnFlag = FALSE;
973 while (*cp != '!' && *cp != ':' && *cp) {
974 if (*cp != ' ' && *cp != '\t') {
980 Parse_Error(PARSE_WARNING,
981 "Extra target ignored");
984 while (*cp && isspace((unsigned char)*cp)) {
989 } while (*line != '!' && *line != ':' && *line);
991 if (!Lst_IsEmpty(&targets)) {
994 Parse_Error(PARSE_WARNING, "Special and mundane "
995 "targets don't mix. Mundane ones ignored");
1002 * These four create nodes on which to hang commands, so
1003 * targets shouldn't be empty...
1007 * Nothing special here -- targets can be empty if it
1015 * Have now parsed all the target names. Must parse the operator next.
1016 * The result is left in op.
1020 } else if (*cp == ':') {
1028 Parse_Error(PARSE_FATAL, "Missing dependency operator");
1032 cp++; /* Advance beyond operator */
1037 * Get to the first source
1039 while (*cp && isspace((unsigned char)*cp)) {
1045 * Several special targets take different actions if present with no
1047 * a .SUFFIXES line with no sources clears out all old suffixes
1048 * a .PRECIOUS line makes all targets precious
1049 * a .IGNORE line ignores errors for all targets
1050 * a .SILENT line creates silence when making all targets
1051 * a .PATH removes all directories from the search path(s).
1056 Suff_ClearSuffixes();
1062 ignoreErrors = TRUE;
1068 LST_FOREACH(ln, &paths)
1069 Path_Clear(Lst_Datum(ln));
1072 Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1078 } else if (specType == MFlags) {
1080 * Call on functions in main.c to deal with these arguments and
1081 * set the initial character to a null-character so the loop to
1082 * get sources won't get anything
1084 Main_ParseArgLine(line, 0);
1087 } else if (specType == ExShell) {
1088 if (Job_ParseShell(line) != SUCCESS) {
1089 Parse_Error(PARSE_FATAL,
1090 "improper shell specification");
1095 } else if (specType == NotParallel || specType == SingleShell) {
1100 * NOW GO FOR THE SOURCES
1102 if (specType == Suffixes || specType == ExPath ||
1103 specType == Includes || specType == Libs ||
1107 * If the target was one that doesn't take files as its
1108 * sources but takes something like suffixes, we take
1109 * each space-separated word on the line as a something
1110 * and deal with it accordingly.
1112 * If the target was .SUFFIXES, we take each source as
1113 * a suffix and add it to the list of suffixes
1114 * maintained by the Suff module.
1116 * If the target was a .PATH, we add the source as a
1117 * directory to search on the search path.
1119 * If it was .INCLUDES, the source is taken to be the
1120 * suffix of files which will be #included and whose
1121 * search path should be present in the .INCLUDES
1124 * If it was .LIBS, the source is taken to be the
1125 * suffix of files which are considered libraries and
1126 * whose search path should be present in the .LIBS
1129 * If it was .NULL, the source is the suffix to use
1130 * when a file has no valid suffix.
1133 while (*cp && !isspace((unsigned char)*cp)) {
1140 Suff_AddSuffix(line);
1143 LST_FOREACH(ln, &paths)
1144 Path_AddDir(Lst_Datum(ln), line);
1147 Suff_AddInclude(line);
1159 if (savech != '\0') {
1162 while (*cp && isspace((unsigned char)*cp)) {
1167 Lst_Destroy(&paths, NOFREE);
1169 } else if (specType == ExportVar) {
1170 Var_SetEnv(line, VAR_GLOBAL);
1173 /* list of sources in order */
1174 Lst curSrcs = Lst_Initializer(curSrc);
1178 * The targets take real sources, so we must beware of
1179 * archive specifications (i.e. things with left
1180 * parentheses in them) and handle them accordingly.
1182 while (*cp && !isspace((unsigned char)*cp)) {
1183 if (*cp == '(' && cp > line && cp[-1] != '$') {
1185 * Only stop for a left parenthesis if
1186 * it isn't at the start of a word
1187 * (that'll be for variable changes
1188 * later) and isn't preceded by a dollar
1189 * sign (a dynamic source).
1200 /* list of archive source names after exp. */
1201 Lst sources = Lst_Initializer(sources);
1203 if (Arch_ParseArchive(&line, &sources,
1204 VAR_CMD) != SUCCESS) {
1205 Parse_Error(PARSE_FATAL, "Error in "
1206 "source archive spec \"%s\"", line);
1210 while (!Lst_IsEmpty(&sources)) {
1211 gnp = Lst_DeQueue(&sources);
1212 ParseDoSrc(tOp, gnp->name, &curSrcs);
1221 ParseDoSrc(tOp, line, &curSrcs);
1223 while (*cp && isspace((unsigned char)*cp)) {
1228 Lst_Destroy(&curSrcs, NOFREE);
1231 if (mainNode == NULL) {
1233 * If we have yet to decide on a main target to make, in the
1234 * absence of any user input, we want the first target on
1235 * the first dependency line that is actually a real target
1236 * (i.e. isn't a .USE or .EXEC rule) to be made.
1238 LST_FOREACH(ln, &targets) {
1240 if ((gn->type & (OP_NOTMAIN | OP_USE |
1241 OP_EXEC | OP_TRANSFORM)) == 0) {
1251 *---------------------------------------------------------------------
1253 * Return TRUE if the passed line is a variable assignment. A variable
1254 * assignment consists of a single word followed by optional whitespace
1255 * followed by either a += or an = operator.
1256 * This function is used both by the Parse_File function and main when
1257 * parsing the command-line arguments.
1260 * TRUE if it is. FALSE if it ain't
1264 *---------------------------------------------------------------------
1267 Parse_IsVar(char *line)
1269 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1270 Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1273 #define ISEQOPERATOR(c) \
1274 ((c) == '+' || (c) == ':' || (c) == '?' || (c) == '!')
1277 * Skip to variable name
1279 for (; *line == ' ' || *line == '\t'; line++)
1282 for (; *line != '=' || level != 0; line++) {
1286 * end-of-line -- can't be a variable assignment.
1293 * there can be as much white space as desired so long
1294 * as there is only one word before the operator
1310 if (wasSpace && haveName) {
1311 if (ISEQOPERATOR(*line)) {
1313 * We must have a finished word
1319 * When an = operator [+?!:] is found,
1320 * the next character must be an = or
1321 * it ain't a valid assignment.
1327 * This is a shell command
1329 if (strncmp(line, ":sh", 3) == 0)
1334 * This is the start of another word, so not
1351 *---------------------------------------------------------------------
1353 * Take the variable assignment in the passed line and do it in the
1356 * Note: There is a lexical ambiguity with assignment modifier characters
1357 * in variable names. This routine interprets the character before the =
1358 * as a modifier. Therefore, an assignment like
1360 * is interpreted as "C+ +=" instead of "C++ =".
1366 * the variable structure of the given variable name is altered in the
1368 *---------------------------------------------------------------------
1371 Parse_DoVar(char *line, GNode *ctxt)
1373 char *cp; /* pointer into line */
1379 } type; /* Type of assignment */
1380 char *opc; /* ptr to operator character to
1381 * null-terminate the variable name */
1384 * Avoid clobbered variable warnings by forcing the compiler
1385 * to ``unregister'' variables
1393 * Skip to variable name
1395 while (*line == ' ' || *line == '\t') {
1400 * Skip to operator character, nulling out whitespace as we go
1402 for (cp = line + 1; *cp != '='; cp++) {
1403 if (isspace((unsigned char)*cp)) {
1407 opc = cp - 1; /* operator is the previous character */
1408 *cp++ = '\0'; /* nuke the = */
1411 * Check operator type
1421 * If the variable already has a value, we don't do anything.
1424 if (Var_Exists(line, ctxt)) {
1443 while (*opc != ':') {
1450 if (strncmp(opc, ":sh", 3) == 0) {
1460 while (isspace((unsigned char)*cp)) {
1464 if (type == VAR_APPEND) {
1465 Var_Append(line, cp, ctxt);
1467 } else if (type == VAR_SUBST) {
1469 * Allow variables in the old value to be undefined, but leave
1470 * their invocation alone -- this is done by forcing oldVars
1472 * XXX: This can cause recursive variables, but that's not
1473 * hard to do, and this allows someone to do something like
1475 * CFLAGS = $(.INCLUDES)
1476 * CFLAGS := -I.. $(CFLAGS)
1478 * And not get an error.
1480 Boolean oldOldVars = oldVars;
1485 * make sure that we set the variable the first time to nothing
1486 * so that it gets substituted!
1488 if (!Var_Exists(line, ctxt))
1489 Var_Set(line, "", ctxt);
1491 cp = Buf_Peel(Var_Subst(cp, ctxt, FALSE));
1493 oldVars = oldOldVars;
1495 Var_Set(line, cp, ctxt);
1498 } else if (type == VAR_SHELL) {
1500 * TRUE if the command needs to be freed, i.e.
1501 * if any variable expansion was performed
1503 Boolean freeCmd = FALSE;
1507 if (strchr(cp, '$') != NULL) {
1509 * There's a dollar sign in the command, so perform
1510 * variable expansion on the whole thing. The
1511 * resulting string will need freeing when we're done,
1512 * so set freeCmd to TRUE.
1514 cp = Buf_Peel(Var_Subst(cp, VAR_CMD, TRUE));
1518 buf = Cmd_Exec(cp, &error);
1519 Var_Set(line, Buf_Data(buf), ctxt);
1520 Buf_Destroy(buf, TRUE);
1523 Parse_Error(PARSE_WARNING, error, cp);
1530 * Normal assignment -- just do it.
1532 Var_Set(line, cp, ctxt);
1537 *-----------------------------------------------------------------------
1538 * ParseHasCommands --
1539 * Callback procedure for Parse_File when destroying the list of
1540 * targets on the last dependency line. Marks a target as already
1541 * having commands if it does, to keep from having shell commands
1542 * on multiple dependency lines.
1548 * OP_HAS_COMMANDS may be set for the target.
1550 *-----------------------------------------------------------------------
1553 ParseHasCommands(void *gnp)
1557 if (!Lst_IsEmpty(&gn->commands)) {
1558 gn->type |= OP_HAS_COMMANDS;
1563 *-----------------------------------------------------------------------
1564 * Parse_AddIncludeDir --
1565 * Add a directory to the path searched for included makefiles
1566 * bracketed by double-quotes. Used by functions in main.c
1572 * The directory is appended to the list.
1574 *-----------------------------------------------------------------------
1577 Parse_AddIncludeDir(char *dir)
1580 Path_AddDir(&parseIncPath, dir);
1584 *---------------------------------------------------------------------
1585 * Parse_FromString --
1586 * Start Parsing from the given string
1592 * A structure is added to the includes Lst and readProc, curFile.lineno,
1593 * curFile.fname and curFile.F are altered for the new file
1594 *---------------------------------------------------------------------
1597 Parse_FromString(char *str, int lineno)
1600 DEBUGF(FOR, ("%s\n---- at line %d\n", str, lineno));
1602 ParsePushInput(estrdup(CURFILE->fname), NULL, str, lineno);
1607 *---------------------------------------------------------------------
1608 * ParseTraditionalInclude --
1609 * Push to another file.
1611 * The input is the line minus the "include". The file name is
1612 * the string following the "include".
1618 * A structure is added to the includes Lst and readProc, curFile.lineno,
1619 * curFile.fname and curFile.F are altered for the new file
1620 *---------------------------------------------------------------------
1623 ParseTraditionalInclude(char *file)
1625 char *fullname; /* full pathname of file */
1626 char *cp; /* current position in file spec */
1629 * Skip over whitespace
1631 while (*file == ' ' || *file == '\t') {
1635 if (*file == '\0') {
1636 Parse_Error(PARSE_FATAL, "Filename missing from \"include\"");
1641 * Skip to end of line or next whitespace
1643 for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1650 * Substitute for any variables in the file name before trying to
1653 file = Buf_Peel(Var_Subst(file, VAR_CMD, FALSE));
1656 * Now we know the file's name, we attempt to find the durn thing.
1657 * Search for it first on the -I search path, then on the .PATH
1658 * search path, if not found in a -I directory.
1660 fullname = Path_FindFile(file, &parseIncPath);
1661 if (fullname == NULL) {
1662 fullname = Path_FindFile(file, &dirSearchPath);
1665 if (fullname == NULL) {
1667 * Still haven't found the makefile. Look for it on the system
1668 * path as a last resort.
1670 fullname = Path_FindFile(file, &sysIncPath);
1673 if (fullname == NULL) {
1674 Parse_Error(PARSE_FATAL, "Could not find %s", file);
1675 /* XXXHB free(file) */
1679 /* XXXHB free(file) */
1682 * We set up the name of the file to be the absolute
1683 * name of the include file so error messages refer to the right
1686 ParsePushInput(fullname, NULL, NULL, 0);
1691 *---------------------------------------------------------------------
1693 * Read a character from the current file
1696 * The character that was read
1699 *---------------------------------------------------------------------
1705 if (CURFILE->F != NULL)
1706 return (fgetc(CURFILE->F));
1708 if (CURFILE->str != NULL && *CURFILE->ptr != '\0')
1709 return (*CURFILE->ptr++);
1716 *---------------------------------------------------------------------
1718 * Put back a character to the current file
1724 *---------------------------------------------------------------------
1730 if (CURFILE->F != NULL) {
1731 ungetc(c, CURFILE->F);
1734 if (CURFILE->str != NULL) {
1735 *--(CURFILE->ptr) = c;
1741 * Grab the next line unless it begins with a dot (`.') and we're told to
1742 * ignore such lines.
1745 ParseSkipLine(int skip, int keep_newline)
1751 buf = Buf_Init(MAKE_BSIZE);
1757 while (((c = ParseReadc()) != '\n' || lastc == '\\')
1759 if (skip && c == '#' && lastc != '\\') {
1761 * let a comment be terminated even by an
1762 * escaped \n. This is consistent to comment
1763 * handling in ParseReadLine
1765 while ((c = ParseReadc()) != '\n' && c != EOF)
1771 Buf_AddByte(buf, (Byte)c);
1773 Buf_ReplaceLastByte(buf, (Byte)' ');
1776 while ((c = ParseReadc()) == ' ' || c == '\t')
1783 Buf_AddByte(buf, (Byte)c);
1788 Parse_Error(PARSE_FATAL,
1789 "Unclosed conditional/for loop");
1790 Buf_Destroy(buf, TRUE);
1795 Buf_AddByte(buf, (Byte)'\0');
1796 line = Buf_Data(buf);
1797 } while (skip == 1 && line[0] != '.');
1799 Buf_Destroy(buf, FALSE);
1804 *---------------------------------------------------------------------
1806 * Read an entire line from the input file. Called only by Parse_File.
1807 * To facilitate escaped newlines and what have you, a character is
1808 * buffered in 'lastc', which is '\0' when no characters have been
1809 * read. When we break out of the loop, c holds the terminating
1810 * character and lastc holds a character that should be added to
1811 * the line (unless we don't read anything but a terminator).
1814 * A line w/o its newline
1817 * Only those associated with reading a character
1818 *---------------------------------------------------------------------
1823 Buffer *buf; /* Buffer for current line */
1824 int c; /* the current character */
1825 int lastc; /* The most-recent character */
1826 Boolean semiNL; /* treat semi-colons as newlines */
1827 Boolean ignDepOp; /* TRUE if should ignore dependency operators
1828 * for the purposes of setting semiNL */
1829 Boolean ignComment; /* TRUE if should ignore comments (in a
1831 char *line; /* Result */
1832 char *ep; /* to strip trailing blanks */
1842 * Handle tab at the beginning of the line. A leading tab (shell
1843 * command) forces us to ignore comments and dependency operators and
1844 * treat semi-colons as semi-colons (by leaving semiNL FALSE).
1845 * This also discards completely blank lines.
1850 if (ParsePopInput() == DONE) {
1851 /* End of all inputs - return NULL */
1858 ignComment = ignDepOp = TRUE;
1869 buf = Buf_Init(MAKE_BSIZE);
1871 while (((c = ParseReadc()) != '\n' || lastc == '\\') && c != EOF) {
1876 * Escaped newline: read characters until a
1877 * non-space or an unescaped newline and
1878 * replace them all by a single space. This is
1879 * done by storing the space over the backslash
1880 * and dropping through with the next nonspace.
1881 * If it is a semi-colon and semiNL is TRUE,
1882 * it will be recognized as a newline in the
1883 * code below this...
1887 while ((c = ParseReadc()) == ' ' || c == '\t') {
1890 if (c == EOF || c == '\n') {
1894 * Check for comments, semiNL's, etc. --
1895 * easier than ParseUnreadc(c);
1905 * Semi-colon: Need to see if it should be
1906 * interpreted as a newline
1910 * To make sure the command that may
1911 * be following this semi-colon begins
1912 * with a tab, we push one back into the
1913 * input stream. This will overwrite the
1914 * semi-colon in the buffer. If there is
1915 * no command following, this does no
1916 * harm, since the newline remains in
1917 * the buffer and the
1918 * whole line is ignored.
1927 * Haven't seen a dependency operator
1928 * before this, so this must be a
1929 * variable assignment -- don't pay
1930 * attention to dependency operators
1934 } else if (lastc == ':' || lastc == '!') {
1936 * Well, we've seen a dependency
1937 * operator already, but it was the
1938 * previous character, so this is really
1939 * just an expanded variable assignment.
1940 * Revert semi-colons to being just
1941 * semi-colons again and ignore any more
1942 * dependency operators.
1944 * XXX: Note that a line like
1945 * "foo : a:=b" will blow up, but who'd
1946 * write a line like that anyway?
1954 if (lastc != '\\') {
1956 * If the character is a hash
1957 * mark and it isn't escaped
1958 * (or we're being compatible),
1959 * the thing is a comment.
1960 * Skip to the end of the line.
1964 } while (c != '\n' && c != EOF);
1968 * Don't add the backslash.
1969 * Just let the # get copied
1982 * A semi-colon is recognized as a
1983 * newline only on dependency lines.
1984 * Dependency lines are lines with a
1985 * colon or an exclamation point.
1996 * Copy in the previous character (there may be none if this
1997 * was the first character) and save this one in
2001 Buf_AddByte(buf, (Byte)lastc);
2007 if (lastc != '\0') {
2008 Buf_AddByte(buf, (Byte)lastc);
2010 Buf_AddByte(buf, (Byte)'\0');
2011 line = Buf_Peel(buf);
2014 * Strip trailing blanks and tabs from the line.
2015 * Do not strip a blank or tab that is preceded by
2021 while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
2022 if (ep > line + 1 && ep[-2] == '\\')
2028 if (line[0] == '\0') {
2029 /* empty line - just ignore */
2038 *-----------------------------------------------------------------------
2039 * ParseFinishLine --
2040 * Handle the end of a dependency group.
2046 * inLine set FALSE. 'targets' list destroyed.
2048 *-----------------------------------------------------------------------
2051 ParseFinishLine(void)
2056 LST_FOREACH(ln, &targets) {
2057 if (((const GNode *)Lst_Datum(ln))->type & OP_TRANSFORM)
2058 Suff_EndTransform(Lst_Datum(ln));
2060 Lst_Destroy(&targets, ParseHasCommands);
2067 * Parse an .include directive and push the file onto the input stack.
2068 * The input is the line minus the .include. A file spec is a string
2069 * enclosed in <> or "". The former is looked for only in sysIncPath.
2070 * The latter in . and the directories specified by -I command line
2074 parse_include(char *file, int code __unused, int lineno __unused)
2076 char *fullname; /* full pathname of file */
2077 char endc; /* the character which ends the file spec */
2078 char *cp; /* current position in file spec */
2079 Boolean isSystem; /* TRUE if makefile is a system makefile */
2080 char *prefEnd, *Fname;
2084 * Skip to delimiter character so we know where to look
2086 while (*file == ' ' || *file == '\t') {
2090 if (*file != '"' && *file != '<') {
2091 Parse_Error(PARSE_FATAL,
2092 ".include filename must be delimited by '\"' or '<'");
2097 * Set the search path on which to find the include file based on the
2098 * characters which bracket its name. Angle-brackets imply it's
2099 * a system Makefile while double-quotes imply it's a user makefile
2110 * Skip to matching delimiter
2112 for (cp = ++file; *cp != endc; cp++) {
2114 Parse_Error(PARSE_FATAL,
2115 "Unclosed .include filename. '%c' expected", endc);
2122 * Substitute for any variables in the file name before trying to
2125 file = Buf_Peel(Var_Subst(file, VAR_CMD, FALSE));
2128 * Now we know the file's name and its search path, we attempt to
2129 * find the durn thing. A return of NULL indicates the file don't
2134 * Include files contained in double-quotes are first searched
2135 * for relative to the including file's location. We don't want
2136 * to cd there, of course, so we just tack on the old file's
2137 * leading path components and call Dir_FindFile to see if
2138 * we can locate the beast.
2141 /* Make a temporary copy of this, to be safe. */
2142 Fname = estrdup(CURFILE->fname);
2144 prefEnd = strrchr(Fname, '/');
2145 if (prefEnd != NULL) {
2148 newName = estrdup(file);
2150 newName = str_concat(Fname, file, STR_ADDSLASH);
2151 fullname = Path_FindFile(newName, &parseIncPath);
2152 if (fullname == NULL) {
2153 fullname = Path_FindFile(newName,
2166 if (fullname == NULL) {
2168 * System makefile or makefile wasn't found in same directory as
2169 * included makefile. Search for it first on the -I search path,
2170 * then on the .PATH search path, if not found in a -I
2172 * XXX: Suffix specific?
2174 fullname = Path_FindFile(file, &parseIncPath);
2175 if (fullname == NULL) {
2176 fullname = Path_FindFile(file, &dirSearchPath);
2180 if (fullname == NULL) {
2182 * Still haven't found the makefile. Look for it on the system
2183 * path as a last resort.
2185 fullname = Path_FindFile(file, &sysIncPath);
2188 if (fullname == NULL) {
2190 Parse_Error(PARSE_FATAL, "Could not find %s", file);
2197 * We set up the name of the file to be the absolute
2198 * name of the include file so error messages refer to the right
2201 ParsePushInput(fullname, NULL, NULL, 0);
2206 * Parse a .warning or .error directive
2208 * The input is the line minus the ".error"/".warning". We substitute
2209 * variables, print the message and exit(1) (for .error) or just print
2210 * a warning if the directive is malformed.
2213 parse_message(char *line, int iserror, int lineno __unused)
2216 if (!isspace((u_char)*line)) {
2217 Parse_Error(PARSE_WARNING, "invalid syntax: .%s%s",
2218 iserror ? "error" : "warning", line);
2222 while (isspace((u_char)*line))
2225 line = Buf_Peel(Var_Subst(line, VAR_GLOBAL, FALSE));
2226 Parse_Error(iserror ? PARSE_FATAL : PARSE_WARNING, "%s", line);
2230 /* Terminate immediately. */
2237 * Parse an .undef directive.
2240 parse_undef(char *line, int code __unused, int lineno __unused)
2244 while (isspace((u_char)*line))
2247 for (cp = line; !isspace((u_char)*cp) && *cp != '\0'; cp++) {
2252 cp = Buf_Peel(Var_Subst(line, VAR_CMD, FALSE));
2253 Var_Delete(cp, VAR_GLOBAL);
2259 * Parse an .makeenv directive.
2262 parse_makeenv(char *line, int code __unused, int lineno __unused)
2266 while (isspace((u_char)*line))
2269 for (cp = line; !isspace((u_char)*cp) && *cp != '\0'; cp++) {
2274 cp = Buf_Peel(Var_Subst(line, VAR_CMD, FALSE));
2275 Var_SetEnv(cp, VAR_GLOBAL);
2281 * Parse a .for directive.
2284 parse_for(char *line, int code __unused, int lineno)
2287 if (!For_For(line)) {
2294 * Skip after the matching endfor.
2298 line = ParseSkipLine(0, 1);
2300 Parse_Error(PARSE_FATAL,
2301 "Unexpected end of file in for loop.\n");
2304 } while (For_Eval(line));
2313 * Parse endfor. This may only happen if there was no matching .for.
2316 parse_endfor(char *line __unused, int code __unused, int lineno __unused)
2319 Parse_Error(PARSE_FATAL, "for-less endfor");
2324 * Got a line starting with a '.'. Check if this is a directive
2328 * TRUE if line was a directive, FALSE otherwise.
2331 parse_directive(char *line)
2339 * .[[:space:]]*\([[:alpha:]][[:alnum:]_]*\).*
2340 * \1 is the keyword.
2342 for (start = line; isspace((u_char)*start); start++) {
2346 if (!isalpha((u_char)*start)) {
2351 while (isalnum((u_char)*cp) || *cp == '_') {
2355 dir = directive_hash(start, cp - start);
2356 if (dir < 0 || dir >= (int)NDIRECTS ||
2357 (size_t)(cp - start) != strlen(directives[dir].name) ||
2358 strncmp(start, directives[dir].name, cp - start) != 0) {
2359 /* not actually matched */
2363 if (!skipLine || directives[dir].skip_flag)
2364 (*directives[dir].func)(cp, directives[dir].code,
2370 *---------------------------------------------------------------------
2372 * Parse a file into its component parts, incorporating it into the
2373 * current dependency graph. This is the main function and controls
2374 * almost every other function in this module
2380 * Loads. Nodes are added to the list of all targets, nodes and links
2381 * are added to the dependency graph. etc. etc. etc.
2382 *---------------------------------------------------------------------
2385 Parse_File(const char *name, FILE *stream)
2387 char *cp; /* pointer into the line */
2388 char *line; /* the line we're working on */
2393 ParsePushInput(estrdup(name), stream, NULL, 0);
2395 while ((line = ParseReadLine()) != NULL) {
2396 if (*line == '.' && parse_directive(line + 1)) {
2397 /* directive consumed */
2400 if (skipLine || *line == '#') {
2401 /* Skipping .if block or comment. */
2405 if (*line == '\t') {
2407 * If a line starts with a tab, it can only
2408 * hope to be a creation command.
2410 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2419 * So long as it's not a blank
2420 * line and we're actually in a
2421 * dependency spec, add the
2422 * command to the list of
2423 * commands of all targets in
2424 * the dependency spec.
2426 LST_FOREACH(ln, &targets) {
2434 if (!(gn->type & OP_HAS_COMMANDS))
2435 Lst_AtEnd(&gn->commands, cp);
2437 Parse_Error(PARSE_WARNING, "duplicate script "
2438 "for target \"%s\" ignored", gn->name);
2442 Parse_Error(PARSE_FATAL,
2443 "Unassociated shell command \"%s\"",
2448 } else if (strncmp(line, "include", 7) == 0 &&
2449 isspace((unsigned char)line[7]) &&
2450 strchr(line, ':') == NULL) {
2452 * It's an S3/S5-style "include".
2454 ParseTraditionalInclude(line + 7);
2457 } else if (Parse_IsVar(line)) {
2459 Parse_DoVar(line, VAR_GLOBAL);
2463 * We now know it's a dependency line so it
2464 * needs to have all variables expanded before
2465 * being parsed. Tell the variable module to
2466 * complain if some variable is undefined...
2467 * To make life easier on novices, if the line
2468 * is indented we first make sure the line has
2469 * a dependency operator in it. If it doesn't
2470 * have an operator and we're in a dependency
2471 * line's script, we assume it's actually a
2472 * shell command and add it to the current
2473 * list of targets. XXX this comment seems wrong.
2476 if (isspace((unsigned char)line[0])) {
2477 while (*cp != '\0' &&
2478 isspace((unsigned char)*cp)) {
2488 cp = Buf_Peel(Var_Subst(line, VAR_CMD, TRUE));
2494 * Need a non-circular list for the target nodes
2496 Lst_Destroy(&targets, NOFREE);
2499 ParseDoDependency(line);
2509 * Make sure conditionals are clean
2514 errx(1, "fatal errors encountered -- cannot continue");
2518 *-----------------------------------------------------------------------
2520 * Return a Lst of the main target to create for main()'s sake. If
2521 * no such target exists, we Punt with an obnoxious error message.
2524 * A Lst of the single node to create.
2529 *-----------------------------------------------------------------------
2532 Parse_MainName(Lst *listmain)
2535 if (mainNode == NULL) {
2536 Punt("no target to make.");
2538 } else if (mainNode->type & OP_DOUBLEDEP) {
2539 Lst_AtEnd(listmain, mainNode);
2540 Lst_Concat(listmain, &mainNode->cohorts, LST_CONCNEW);
2542 Lst_AtEnd(listmain, mainNode);