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.79 2005/05/05 09:08:42 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.
87 #include "hash_tables.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 */
176 Attribute /* Generic attribute */
179 static ParseSpecial specType;
183 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
184 * seen, then set to each successive source on the line.
186 static GNode *predecessor;
189 * The parseKeywords table is searched using binary search when deciding
190 * if a target or source is special. The 'spec' field is the ParseSpecial
191 * type of the keyword ("Not" if the keyword isn't special as a target) while
192 * the 'op' field is the operator to apply to the list of targets if the
193 * keyword is used as a source ("0" if the keyword isn't special as a source)
195 static const struct keyword {
196 const char *name; /* Name of keyword */
197 ParseSpecial spec; /* Type when used as a target */
198 int op; /* Operator when used as a source */
199 } parseKeywords[] = {
200 /* KEYWORD-START-TAG */
201 { ".BEGIN", Begin, 0 },
202 { ".DEFAULT", Default, 0 },
204 { ".EXEC", Attribute, OP_EXEC },
205 { ".EXPORTVAR", ExportVar, 0 },
206 { ".IGNORE", Ignore, OP_IGNORE },
207 { ".INCLUDES", Includes, 0 },
208 { ".INTERRUPT", Interrupt, 0 },
209 { ".INVISIBLE", Attribute, OP_INVISIBLE },
210 { ".JOIN", Attribute, OP_JOIN },
211 { ".LIBS", Libs, 0 },
212 { ".MAIN", Main, 0 },
213 { ".MAKE", Attribute, OP_MAKE },
214 { ".MAKEFLAGS", MFlags, 0 },
215 { ".MFLAGS", MFlags, 0 },
216 { ".NOTMAIN", Attribute, OP_NOTMAIN },
217 { ".NOTPARALLEL", NotParallel, 0 },
218 { ".NO_PARALLEL", NotParallel, 0 },
219 { ".NULL", Null, 0 },
220 { ".OPTIONAL", Attribute, OP_OPTIONAL },
221 { ".ORDER", Order, 0 },
222 { ".PARALLEL", Parallel, 0 },
223 { ".PATH", ExPath, 0 },
224 { ".PHONY", Phony, OP_PHONY },
225 { ".POSIX", Posix, 0 },
226 { ".PRECIOUS", Precious, OP_PRECIOUS },
227 { ".RECURSIVE", Attribute, OP_MAKE },
228 { ".SHELL", ExShell, 0 },
229 { ".SILENT", Silent, OP_SILENT },
230 { ".SINGLESHELL", SingleShell, 0 },
231 { ".SUFFIXES", Suffixes, 0 },
232 { ".USE", Attribute, OP_USE },
233 { ".WAIT", Wait, 0 },
234 { ".WARN", Warn, 0 },
235 /* KEYWORD-END-TAG */
237 #define NKEYWORDS (sizeof(parseKeywords) / sizeof(parseKeywords[0]))
239 static void parse_include(char *, int, int);
240 static void parse_message(char *, int, int);
241 static void parse_makeenv(char *, int, int);
242 static void parse_undef(char *, int, int);
243 static void parse_for(char *, int, int);
244 static void parse_endfor(char *, int, int);
246 static const struct directive {
249 Boolean skip_flag; /* execute even when skipped */
250 void (*func)(char *, int, int);
252 /* DIRECTIVES-START-TAG */
253 { "elif", COND_ELIF, TRUE, Cond_If },
254 { "elifdef", COND_ELIFDEF, TRUE, Cond_If },
255 { "elifmake", COND_ELIFMAKE, TRUE, Cond_If },
256 { "elifndef", COND_ELIFNDEF, TRUE, Cond_If },
257 { "elifnmake", COND_ELIFNMAKE, TRUE, Cond_If },
258 { "else", COND_ELSE, TRUE, Cond_Else },
259 { "endfor", 0, FALSE, parse_endfor },
260 { "endif", COND_ENDIF, TRUE, Cond_Endif },
261 { "error", 1, FALSE, parse_message },
262 { "for", 0, FALSE, parse_for },
263 { "if", COND_IF, TRUE, Cond_If },
264 { "ifdef", COND_IFDEF, TRUE, Cond_If },
265 { "ifmake", COND_IFMAKE, TRUE, Cond_If },
266 { "ifndef", COND_IFNDEF, TRUE, Cond_If },
267 { "ifnmake", COND_IFNMAKE, TRUE, Cond_If },
268 { "include", 0, FALSE, parse_include },
269 { "makeenv", 0, FALSE, parse_makeenv },
270 { "undef", 0, FALSE, parse_undef },
271 { "warning", 0, FALSE, parse_message },
272 /* DIRECTIVES-END-TAG */
274 #define NDIRECTS (sizeof(directives) / sizeof(directives[0]))
278 * Look in the table of keywords for one matching the given string.
281 * The pointer to keyword table entry or NULL.
283 static const struct keyword *
284 ParseFindKeyword(const char *str)
288 kw = keyword_hash(str, strlen(str));
289 if (kw < 0 || kw >= (int)NKEYWORDS ||
290 strcmp(str, parseKeywords[kw].name) != 0)
292 return (&parseKeywords[kw]);
297 * Error message abort function for parsing. Prints out the context
298 * of the error (line number and file) as well as the message with
299 * two optional arguments.
305 * "fatals" is incremented if the level is PARSE_FATAL.
309 Parse_Error(int type, const char *fmt, ...)
315 fprintf(stderr, "\"%s\", line %d: ",
316 CURFILE->fname, CURFILE->lineno);
317 if (type == PARSE_WARNING)
318 fprintf(stderr, "warning: ");
319 vfprintf(stderr, fmt, ap);
321 fprintf(stderr, "\n");
323 if (type == PARSE_FATAL)
330 * Push a new input source onto the input stack. If ptr is NULL
331 * the fullname is used to fopen the file. If it is not NULL,
332 * ptr is assumed to point to the string to be parsed. If opening the
333 * file fails, the fullname is freed.
336 ParsePushInput(char *fullname, FILE *fp, char *ptr, int lineno)
340 nf = emalloc(sizeof(*nf));
341 nf->fname = fullname;
345 /* the input source is a file */
346 if ((nf->F = fp) == NULL) {
347 nf->F = fopen(fullname, "r");
349 Parse_Error(PARSE_FATAL, "Cannot open %s",
356 nf->str = nf->ptr = NULL;
357 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
359 nf->str = nf->ptr = ptr;
362 TAILQ_INSERT_HEAD(&includes, nf, link);
367 * Called when EOF is reached in the current file. If we were reading
368 * an include file, the includes stack is popped and things set up
369 * to go back to reading the previous file at the previous location.
372 * CONTINUE if there's more to do. DONE if not.
375 * The old curFile.F is closed. The includes list is shortened.
376 * curFile.lineno, curFile.F, and curFile.fname are changed if
377 * CONTINUE is returned.
382 struct IFile *ifile; /* the state on the top of the includes stack */
384 assert(!TAILQ_EMPTY(&includes));
386 ifile = TAILQ_FIRST(&includes);
387 TAILQ_REMOVE(&includes, ifile, link);
390 if (ifile->F != NULL) {
392 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
394 if (ifile->str != NULL) {
399 return (TAILQ_EMPTY(&includes) ? DONE : CONTINUE);
404 * Parse the .WARN pseudo-target.
407 parse_warn(const char line[])
412 brk_string(&aa, line, TRUE);
414 for (i = 1; i < aa.argc; i++)
415 Main_ParseWarn(aa.argv[i], 0);
419 *---------------------------------------------------------------------
421 * Link the parent nodes to their new child. Used by
422 * ParseDoDependency. If the specType isn't 'Not', the parent
423 * isn't linked as a parent of the child.
426 * New elements are added to the parents lists of cgn and the
427 * children list of cgn. the unmade field of pgn is updated
428 * to reflect the additional child.
429 *---------------------------------------------------------------------
432 ParseLinkSrc(Lst *parents, GNode *cgn)
437 LST_FOREACH(ln, parents) {
439 if (Lst_Member(&pgn->children, cgn) == NULL) {
440 Lst_AtEnd(&pgn->children, cgn);
441 if (specType == Not) {
442 Lst_AtEnd(&cgn->parents, pgn);
450 *---------------------------------------------------------------------
452 * Apply the parsed operator to all target nodes. Used in
453 * ParseDoDependency once all targets have been found and their
454 * operator parsed. If the previous and new operators are incompatible,
455 * a major error is taken.
458 * The type field of the node is altered to reflect any new bits in
460 *---------------------------------------------------------------------
469 LST_FOREACH(ln, &targets) {
473 * If the dependency mask of the operator and the node don't
474 * match and the node has actually had an operator applied to
475 * it before, and the operator actually has some dependency
476 * information in it, complain.
478 if ((op & OP_OPMASK) != (gn->type & OP_OPMASK) &&
479 !OP_NOP(gn->type) && !OP_NOP(op)) {
480 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s",
485 if (op == OP_DOUBLEDEP &&
486 (gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
488 * If the node was the object of a :: operator, we need
489 * to create a new instance of it for the children and
490 * commands on this dependency line. The new instance
491 * is placed on the 'cohorts' list of the initial one
492 * (note the initial one is not on its own cohorts list)
493 * and the new instance is linked to all parents of the
496 cohort = Targ_NewGN(gn->name);
499 * Duplicate links to parents so graph traversal is
500 * simple. Perhaps some type bits should be duplicated?
502 * Make the cohort invisible as well to avoid
503 * duplicating it into other variables. True, parents
504 * of this target won't tend to do anything with their
505 * local variables, but better safe than sorry.
507 ParseLinkSrc(&gn->parents, cohort);
508 cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
509 Lst_AtEnd(&gn->cohorts, cohort);
512 * Replace the node in the targets list with the
515 Lst_Replace(ln, cohort);
519 * We don't want to nuke any previous flags (whatever they were)
520 * so we just OR the new operator into the old
527 *---------------------------------------------------------------------
529 * Given the name of a source, figure out if it is an attribute
530 * and apply it to the targets if it is. Else decide if there is
531 * some attribute which should be applied *to* the source because
532 * of some special target and apply it if so. Otherwise, make the
533 * source be a child of the targets in the list 'targets'
539 * Operator bits may be added to the list of targets or to the source.
540 * The targets may have a new source added to their lists of children.
541 *---------------------------------------------------------------------
544 ParseDoSrc(int tOp, char *src, Lst *allsrc)
547 const struct keyword *kw;
549 if (src[0] == '.' && isupper ((unsigned char)src[1])) {
550 if ((kw = ParseFindKeyword(src)) != NULL) {
555 if (kw->spec == Wait) {
565 * If we have noted the existence of a .MAIN, it means we need
566 * to add the sources of said target to the list of things
567 * to create. The string 'src' is likely to be free, so we
568 * must make a new copy of it. Note that this will only be
569 * invoked if the user didn't specify a target on the command
570 * line. This is to allow #ifmake's to succeed, or something...
572 Lst_AtEnd(&create, estrdup(src));
574 * Add the name to the .TARGETS variable as well, so the user
575 * can employ that, if desired.
577 Var_Append(".TARGETS", src, VAR_GLOBAL);
582 * Create proper predecessor/successor links between the
583 * previous source and the current one.
585 gn = Targ_FindNode(src, TARG_CREATE);
586 if (predecessor != NULL) {
587 Lst_AtEnd(&predecessor->successors, gn);
588 Lst_AtEnd(&gn->preds, predecessor);
591 * The current source now becomes the predecessor for the next
599 * If the source is not an attribute, we need to find/create
600 * a node for it. After that we can apply any operator to it
601 * from a special target or link it to its parents, as
604 * In the case of a source that was the object of a :: operator,
605 * the attribute is applied to all of its instances (as kept in
606 * the 'cohorts' list of the node) or all the cohorts are linked
607 * to all the targets.
609 gn = Targ_FindNode(src, TARG_CREATE);
613 ParseLinkSrc(&targets, gn);
615 if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
619 for (ln = Lst_First(&gn->cohorts); ln != NULL;
621 cohort = Lst_Datum(ln);
625 ParseLinkSrc(&targets, cohort);
633 Lst_AtEnd(allsrc, gn);
639 * Check if GNodes needs to be synchronized.
640 * This has to be when two nodes are on different sides of a
643 LST_FOREACH(ln, allsrc) {
646 if (p->order >= gn->order)
649 * XXX: This can cause loops, and loops can cause
650 * unmade targets, but checking is tedious, and the
651 * debugging output can show the problem
653 Lst_AtEnd(&p->successors, gn);
654 Lst_AtEnd(&gn->preds, p);
661 *---------------------------------------------------------------------
662 * ParseDoDependency --
663 * Parse the dependency line in line.
669 * The nodes of the sources are linked as children to the nodes of the
670 * targets. Some nodes may be created.
672 * We parse a dependency line by first extracting words from the line and
673 * finding nodes in the list of all targets with that name. This is done
674 * until a character is encountered which is an operator character. Currently
675 * these are only ! and :. At this point the operator is parsed and the
676 * pointer into the line advanced until the first source is encountered.
677 * The parsed operator is applied to each node in the 'targets' list,
678 * which is where the nodes found for the targets are kept, by means of
679 * the ParseDoOp function.
680 * The sources are read in much the same way as the targets were except
681 * that now they are expanded using the wildcarding scheme of the C-Shell
682 * and all instances of the resulting words in the list of all targets
683 * are found. Each of the resulting nodes is then linked to each of the
684 * targets as one of its children.
685 * Certain targets are handled specially. These are the ones detailed
686 * by the specType variable.
687 * The storing of transformation rules is also taken care of here.
688 * A target is recognized as a transformation rule by calling
689 * Suff_IsTransform. If it is a transformation rule, its node is gotten
690 * from the suffix module via Suff_AddTransform rather than the standard
691 * Targ_FindNode in the target module.
692 *---------------------------------------------------------------------
695 ParseDoDependency(char *line)
697 char *cp; /* our current position */
698 GNode *gn; /* a general purpose temporary node */
699 int op; /* the operator on the line */
700 char savec; /* a place to save a character */
701 Lst paths; /* Search paths to alter when parsing .PATH targets */
702 int tOp; /* operator from special target */
704 const struct keyword *kw;
714 *cp && !isspace((unsigned char)*cp) && *cp != '(';
718 * Must be a dynamic source (would have been
719 * expanded otherwise), so call the Var module
720 * to parse the puppy so we can safely advance
721 * beyond it...There should be no errors in this
722 * as they would have been discovered in the
723 * initial Var_Subst and we wouldn't be here.
729 result = Var_Parse(cp, VAR_CMD, TRUE,
737 } else if (*cp == '!' || *cp == ':') {
739 * We don't want to end a word on ':' or '!' if
740 * there is a better match later on in the
741 * string (greedy matching).
742 * This allows the user to have targets like:
745 * where "fie::fi:fo" and "foo::bar" are the
746 * targets. In real life this is used for perl5
747 * library man pages where "::" separates an
748 * object from its class. Ie:
749 * "File::Spec::Unix". This behaviour is also
750 * consistent with other versions of make.
754 if (*cp == ':' && *p == ':')
757 /* Found the best match already. */
758 if (*p == '\0' || isspace(*p))
761 p += strcspn(p, "!:");
763 /* No better match later on... */
771 * Archives must be handled specially to make sure the
772 * OP_ARCHV flag is set in their 'type' field, for one
773 * thing, and because things like "archive(file1.o
774 * file2.o file3.o)" are permissible. Arch_ParseArchive
775 * will set 'line' to be the first non-blank after the
776 * archive-spec. It creates/finds nodes for the members
777 * and places them on the given list, returning SUCCESS
778 * if all went well and FAILURE if there was an error in
779 * the specification. On error, line should remain
782 if (Arch_ParseArchive(&line, &targets, VAR_CMD) !=
784 Parse_Error(PARSE_FATAL,
785 "Error in archive specification: \"%s\"",
797 * Ending a dependency line without an operator is a * Bozo no-no. As a heuristic, this is also often
798 * triggered by undetected conflicts from cvs/rcs
801 if (strncmp(line, "<<<<<<", 6) == 0 ||
802 strncmp(line, "======", 6) == 0 ||
803 strncmp(line, ">>>>>>", 6) == 0) {
804 Parse_Error(PARSE_FATAL, "Makefile appears to "
805 "contain unresolved cvs/rcs/??? merge "
808 Parse_Error(PARSE_FATAL, "Need an operator");
813 * Have a word in line. See if it's a special target and set
814 * specType to match it.
816 if (*line == '.' && isupper((unsigned char)line[1])) {
818 * See if the target is a special target that must have
819 * it or its sources handled specially.
821 if ((kw = ParseFindKeyword(line)) != NULL) {
822 if (specType == ExPath && kw->spec != ExPath) {
823 Parse_Error(PARSE_FATAL,
824 "Mismatched special targets");
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 == Warn) {
1091 } else if (specType == ExShell) {
1092 if (Job_ParseShell(line) != SUCCESS) {
1093 Parse_Error(PARSE_FATAL,
1094 "improper shell specification");
1099 } else if (specType == NotParallel || specType == SingleShell) {
1104 * NOW GO FOR THE SOURCES
1106 if (specType == Suffixes || specType == ExPath ||
1107 specType == Includes || specType == Libs ||
1111 * If the target was one that doesn't take files as its
1112 * sources but takes something like suffixes, we take
1113 * each space-separated word on the line as a something
1114 * and deal with it accordingly.
1116 * If the target was .SUFFIXES, we take each source as
1117 * a suffix and add it to the list of suffixes
1118 * maintained by the Suff module.
1120 * If the target was a .PATH, we add the source as a
1121 * directory to search on the search path.
1123 * If it was .INCLUDES, the source is taken to be the
1124 * suffix of files which will be #included and whose
1125 * search path should be present in the .INCLUDES
1128 * If it was .LIBS, the source is taken to be the
1129 * suffix of files which are considered libraries and
1130 * whose search path should be present in the .LIBS
1133 * If it was .NULL, the source is the suffix to use
1134 * when a file has no valid suffix.
1137 while (*cp && !isspace((unsigned char)*cp)) {
1144 Suff_AddSuffix(line);
1147 LST_FOREACH(ln, &paths)
1148 Path_AddDir(Lst_Datum(ln), line);
1151 Suff_AddInclude(line);
1163 if (savech != '\0') {
1166 while (*cp && isspace((unsigned char)*cp)) {
1171 Lst_Destroy(&paths, NOFREE);
1173 } else if (specType == ExportVar) {
1174 Var_SetEnv(line, VAR_GLOBAL);
1177 /* list of sources in order */
1178 Lst curSrcs = Lst_Initializer(curSrc);
1182 * The targets take real sources, so we must beware of
1183 * archive specifications (i.e. things with left
1184 * parentheses in them) and handle them accordingly.
1186 while (*cp && !isspace((unsigned char)*cp)) {
1187 if (*cp == '(' && cp > line && cp[-1] != '$') {
1189 * Only stop for a left parenthesis if
1190 * it isn't at the start of a word
1191 * (that'll be for variable changes
1192 * later) and isn't preceded by a dollar
1193 * sign (a dynamic source).
1204 /* list of archive source names after exp. */
1205 Lst sources = Lst_Initializer(sources);
1207 if (Arch_ParseArchive(&line, &sources,
1208 VAR_CMD) != SUCCESS) {
1209 Parse_Error(PARSE_FATAL, "Error in "
1210 "source archive spec \"%s\"", line);
1214 while (!Lst_IsEmpty(&sources)) {
1215 gnp = Lst_DeQueue(&sources);
1216 ParseDoSrc(tOp, gnp->name, &curSrcs);
1225 ParseDoSrc(tOp, line, &curSrcs);
1227 while (*cp && isspace((unsigned char)*cp)) {
1232 Lst_Destroy(&curSrcs, NOFREE);
1235 if (mainNode == NULL) {
1237 * If we have yet to decide on a main target to make, in the
1238 * absence of any user input, we want the first target on
1239 * the first dependency line that is actually a real target
1240 * (i.e. isn't a .USE or .EXEC rule) to be made.
1242 LST_FOREACH(ln, &targets) {
1244 if ((gn->type & (OP_NOTMAIN | OP_USE |
1245 OP_EXEC | OP_TRANSFORM)) == 0) {
1255 *---------------------------------------------------------------------
1257 * Return TRUE if the passed line is a variable assignment. A variable
1258 * assignment consists of a single word followed by optional whitespace
1259 * followed by either a += or an = operator.
1260 * This function is used both by the Parse_File function and main when
1261 * parsing the command-line arguments.
1264 * TRUE if it is. FALSE if it ain't
1268 *---------------------------------------------------------------------
1271 Parse_IsVar(char *line)
1273 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1274 Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1277 #define ISEQOPERATOR(c) \
1278 ((c) == '+' || (c) == ':' || (c) == '?' || (c) == '!')
1281 * Skip to variable name
1283 for (; *line == ' ' || *line == '\t'; line++)
1286 for (; *line != '=' || level != 0; line++) {
1290 * end-of-line -- can't be a variable assignment.
1297 * there can be as much white space as desired so long
1298 * as there is only one word before the operator
1314 if (wasSpace && haveName) {
1315 if (ISEQOPERATOR(*line)) {
1317 * We must have a finished word
1323 * When an = operator [+?!:] is found,
1324 * the next character must be an = or
1325 * it ain't a valid assignment.
1331 * This is a shell command
1333 if (strncmp(line, ":sh", 3) == 0)
1338 * This is the start of another word, so not
1355 *---------------------------------------------------------------------
1357 * Take the variable assignment in the passed line and do it in the
1360 * Note: There is a lexical ambiguity with assignment modifier characters
1361 * in variable names. This routine interprets the character before the =
1362 * as a modifier. Therefore, an assignment like
1364 * is interpreted as "C+ +=" instead of "C++ =".
1370 * the variable structure of the given variable name is altered in the
1372 *---------------------------------------------------------------------
1375 Parse_DoVar(char *line, GNode *ctxt)
1377 char *cp; /* pointer into line */
1383 } type; /* Type of assignment */
1384 char *opc; /* ptr to operator character to
1385 * null-terminate the variable name */
1388 * Skip to variable name
1390 while (*line == ' ' || *line == '\t') {
1395 * Skip to operator character, nulling out whitespace as we go
1397 for (cp = line + 1; *cp != '='; cp++) {
1398 if (isspace((unsigned char)*cp)) {
1402 opc = cp - 1; /* operator is the previous character */
1403 *cp++ = '\0'; /* nuke the = */
1406 * Check operator type
1416 * If the variable already has a value, we don't do anything.
1419 if (Var_Exists(line, ctxt)) {
1438 while (*opc != ':') {
1445 if (strncmp(opc, ":sh", 3) == 0) {
1455 while (isspace((unsigned char)*cp)) {
1459 if (type == VAR_APPEND) {
1460 Var_Append(line, cp, ctxt);
1462 } else if (type == VAR_SUBST) {
1464 * Allow variables in the old value to be undefined, but leave
1465 * their invocation alone -- this is done by forcing oldVars
1467 * XXX: This can cause recursive variables, but that's not
1468 * hard to do, and this allows someone to do something like
1470 * CFLAGS = $(.INCLUDES)
1471 * CFLAGS := -I.. $(CFLAGS)
1473 * And not get an error.
1475 Boolean oldOldVars = oldVars;
1480 * make sure that we set the variable the first time to nothing
1481 * so that it gets substituted!
1483 if (!Var_Exists(line, ctxt))
1484 Var_Set(line, "", ctxt);
1486 cp = Buf_Peel(Var_Subst(cp, ctxt, FALSE));
1488 oldVars = oldOldVars;
1490 Var_Set(line, cp, ctxt);
1493 } else if (type == VAR_SHELL) {
1495 * TRUE if the command needs to be freed, i.e.
1496 * if any variable expansion was performed
1498 Boolean freeCmd = FALSE;
1502 if (strchr(cp, '$') != NULL) {
1504 * There's a dollar sign in the command, so perform
1505 * variable expansion on the whole thing. The
1506 * resulting string will need freeing when we're done,
1507 * so set freeCmd to TRUE.
1509 cp = Buf_Peel(Var_Subst(cp, VAR_CMD, TRUE));
1513 buf = Cmd_Exec(cp, &error);
1514 Var_Set(line, Buf_Data(buf), ctxt);
1515 Buf_Destroy(buf, TRUE);
1518 Parse_Error(PARSE_WARNING, error, cp);
1525 * Normal assignment -- just do it.
1527 Var_Set(line, cp, ctxt);
1532 *-----------------------------------------------------------------------
1533 * ParseHasCommands --
1534 * Callback procedure for Parse_File when destroying the list of
1535 * targets on the last dependency line. Marks a target as already
1536 * having commands if it does, to keep from having shell commands
1537 * on multiple dependency lines.
1543 * OP_HAS_COMMANDS may be set for the target.
1545 *-----------------------------------------------------------------------
1548 ParseHasCommands(void *gnp)
1552 if (!Lst_IsEmpty(&gn->commands)) {
1553 gn->type |= OP_HAS_COMMANDS;
1558 *-----------------------------------------------------------------------
1559 * Parse_AddIncludeDir --
1560 * Add a directory to the path searched for included makefiles
1561 * bracketed by double-quotes. Used by functions in main.c
1567 * The directory is appended to the list.
1569 *-----------------------------------------------------------------------
1572 Parse_AddIncludeDir(char *dir)
1575 Path_AddDir(&parseIncPath, dir);
1579 *---------------------------------------------------------------------
1580 * Parse_FromString --
1581 * Start Parsing from the given string
1587 * A structure is added to the includes Lst and readProc, curFile.lineno,
1588 * curFile.fname and curFile.F are altered for the new file
1589 *---------------------------------------------------------------------
1592 Parse_FromString(char *str, int lineno)
1595 DEBUGF(FOR, ("%s\n---- at line %d\n", str, lineno));
1597 ParsePushInput(estrdup(CURFILE->fname), NULL, str, lineno);
1602 *---------------------------------------------------------------------
1603 * ParseTraditionalInclude --
1604 * Push to another file.
1606 * The input is the line minus the "include". The file name is
1607 * the string following the "include".
1613 * A structure is added to the includes Lst and readProc, curFile.lineno,
1614 * curFile.fname and curFile.F are altered for the new file
1615 *---------------------------------------------------------------------
1618 ParseTraditionalInclude(char *file)
1620 char *fullname; /* full pathname of file */
1621 char *cp; /* current position in file spec */
1624 * Skip over whitespace
1626 while (*file == ' ' || *file == '\t') {
1630 if (*file == '\0') {
1631 Parse_Error(PARSE_FATAL, "Filename missing from \"include\"");
1636 * Skip to end of line or next whitespace
1638 for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1645 * Substitute for any variables in the file name before trying to
1648 file = Buf_Peel(Var_Subst(file, VAR_CMD, FALSE));
1651 * Now we know the file's name, we attempt to find the durn thing.
1652 * Search for it first on the -I search path, then on the .PATH
1653 * search path, if not found in a -I directory.
1655 fullname = Path_FindFile(file, &parseIncPath);
1656 if (fullname == NULL) {
1657 fullname = Path_FindFile(file, &dirSearchPath);
1660 if (fullname == NULL) {
1662 * Still haven't found the makefile. Look for it on the system
1663 * path as a last resort.
1665 fullname = Path_FindFile(file, &sysIncPath);
1668 if (fullname == NULL) {
1669 Parse_Error(PARSE_FATAL, "Could not find %s", file);
1670 /* XXXHB free(file) */
1674 /* XXXHB free(file) */
1677 * We set up the name of the file to be the absolute
1678 * name of the include file so error messages refer to the right
1681 ParsePushInput(fullname, NULL, NULL, 0);
1686 *---------------------------------------------------------------------
1688 * Read a character from the current file
1691 * The character that was read
1694 *---------------------------------------------------------------------
1700 if (CURFILE->F != NULL)
1701 return (fgetc(CURFILE->F));
1703 if (CURFILE->str != NULL && *CURFILE->ptr != '\0')
1704 return (*CURFILE->ptr++);
1711 *---------------------------------------------------------------------
1713 * Put back a character to the current file
1719 *---------------------------------------------------------------------
1725 if (CURFILE->F != NULL) {
1726 ungetc(c, CURFILE->F);
1729 if (CURFILE->str != NULL) {
1730 *--(CURFILE->ptr) = c;
1736 * Grab the next line unless it begins with a dot (`.') and we're told to
1737 * ignore such lines.
1740 ParseSkipLine(int skip, int keep_newline)
1746 buf = Buf_Init(MAKE_BSIZE);
1752 while (((c = ParseReadc()) != '\n' || lastc == '\\')
1754 if (skip && c == '#' && lastc != '\\') {
1756 * let a comment be terminated even by an
1757 * escaped \n. This is consistent to comment
1758 * handling in ParseReadLine
1760 while ((c = ParseReadc()) != '\n' && c != EOF)
1766 Buf_AddByte(buf, (Byte)c);
1768 Buf_ReplaceLastByte(buf, (Byte)' ');
1771 while ((c = ParseReadc()) == ' ' || c == '\t')
1778 Buf_AddByte(buf, (Byte)c);
1783 Parse_Error(PARSE_FATAL,
1784 "Unclosed conditional/for loop");
1785 Buf_Destroy(buf, TRUE);
1790 Buf_AddByte(buf, (Byte)'\0');
1791 line = Buf_Data(buf);
1792 } while (skip == 1 && line[0] != '.');
1794 Buf_Destroy(buf, FALSE);
1799 *---------------------------------------------------------------------
1801 * Read an entire line from the input file. Called only by Parse_File.
1802 * To facilitate escaped newlines and what have you, a character is
1803 * buffered in 'lastc', which is '\0' when no characters have been
1804 * read. When we break out of the loop, c holds the terminating
1805 * character and lastc holds a character that should be added to
1806 * the line (unless we don't read anything but a terminator).
1809 * A line w/o its newline
1812 * Only those associated with reading a character
1813 *---------------------------------------------------------------------
1818 Buffer *buf; /* Buffer for current line */
1819 int c; /* the current character */
1820 int lastc; /* The most-recent character */
1821 Boolean semiNL; /* treat semi-colons as newlines */
1822 Boolean ignDepOp; /* TRUE if should ignore dependency operators
1823 * for the purposes of setting semiNL */
1824 Boolean ignComment; /* TRUE if should ignore comments (in a
1826 char *line; /* Result */
1827 char *ep; /* to strip trailing blanks */
1837 * Handle tab at the beginning of the line. A leading tab (shell
1838 * command) forces us to ignore comments and dependency operators and
1839 * treat semi-colons as semi-colons (by leaving semiNL FALSE).
1840 * This also discards completely blank lines.
1845 if (ParsePopInput() == DONE) {
1846 /* End of all inputs - return NULL */
1853 ignComment = ignDepOp = TRUE;
1864 buf = Buf_Init(MAKE_BSIZE);
1866 while (((c = ParseReadc()) != '\n' || lastc == '\\') && c != EOF) {
1871 * Escaped newline: read characters until a
1872 * non-space or an unescaped newline and
1873 * replace them all by a single space. This is
1874 * done by storing the space over the backslash
1875 * and dropping through with the next nonspace.
1876 * If it is a semi-colon and semiNL is TRUE,
1877 * it will be recognized as a newline in the
1878 * code below this...
1882 while ((c = ParseReadc()) == ' ' || c == '\t') {
1885 if (c == EOF || c == '\n') {
1889 * Check for comments, semiNL's, etc. --
1890 * easier than ParseUnreadc(c);
1900 * Semi-colon: Need to see if it should be
1901 * interpreted as a newline
1905 * To make sure the command that may
1906 * be following this semi-colon begins
1907 * with a tab, we push one back into the
1908 * input stream. This will overwrite the
1909 * semi-colon in the buffer. If there is
1910 * no command following, this does no
1911 * harm, since the newline remains in
1912 * the buffer and the
1913 * whole line is ignored.
1922 * Haven't seen a dependency operator
1923 * before this, so this must be a
1924 * variable assignment -- don't pay
1925 * attention to dependency operators
1929 } else if (lastc == ':' || lastc == '!') {
1931 * Well, we've seen a dependency
1932 * operator already, but it was the
1933 * previous character, so this is really
1934 * just an expanded variable assignment.
1935 * Revert semi-colons to being just
1936 * semi-colons again and ignore any more
1937 * dependency operators.
1939 * XXX: Note that a line like
1940 * "foo : a:=b" will blow up, but who'd
1941 * write a line like that anyway?
1949 if (lastc != '\\') {
1951 * If the character is a hash
1952 * mark and it isn't escaped
1953 * (or we're being compatible),
1954 * the thing is a comment.
1955 * Skip to the end of the line.
1959 } while (c != '\n' && c != EOF);
1963 * Don't add the backslash.
1964 * Just let the # get copied
1977 * A semi-colon is recognized as a
1978 * newline only on dependency lines.
1979 * Dependency lines are lines with a
1980 * colon or an exclamation point.
1991 * Copy in the previous character (there may be none if this
1992 * was the first character) and save this one in
1996 Buf_AddByte(buf, (Byte)lastc);
2002 if (lastc != '\0') {
2003 Buf_AddByte(buf, (Byte)lastc);
2005 Buf_AddByte(buf, (Byte)'\0');
2006 line = Buf_Peel(buf);
2009 * Strip trailing blanks and tabs from the line.
2010 * Do not strip a blank or tab that is preceded by
2016 while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
2017 if (ep > line + 1 && ep[-2] == '\\')
2023 if (line[0] == '\0') {
2024 /* empty line - just ignore */
2033 *-----------------------------------------------------------------------
2034 * ParseFinishLine --
2035 * Handle the end of a dependency group.
2041 * inLine set FALSE. 'targets' list destroyed.
2043 *-----------------------------------------------------------------------
2046 ParseFinishLine(void)
2051 LST_FOREACH(ln, &targets) {
2052 if (((const GNode *)Lst_Datum(ln))->type & OP_TRANSFORM)
2053 Suff_EndTransform(Lst_Datum(ln));
2055 Lst_Destroy(&targets, ParseHasCommands);
2062 * Parse an .include directive and push the file onto the input stack.
2063 * The input is the line minus the .include. A file spec is a string
2064 * enclosed in <> or "". The former is looked for only in sysIncPath.
2065 * The latter in . and the directories specified by -I command line
2069 parse_include(char *file, int code __unused, int lineno __unused)
2071 char *fullname; /* full pathname of file */
2072 char endc; /* the character which ends the file spec */
2073 char *cp; /* current position in file spec */
2074 Boolean isSystem; /* TRUE if makefile is a system makefile */
2075 char *prefEnd, *Fname;
2079 * Skip to delimiter character so we know where to look
2081 while (*file == ' ' || *file == '\t') {
2085 if (*file != '"' && *file != '<') {
2086 Parse_Error(PARSE_FATAL,
2087 ".include filename must be delimited by '\"' or '<'");
2092 * Set the search path on which to find the include file based on the
2093 * characters which bracket its name. Angle-brackets imply it's
2094 * a system Makefile while double-quotes imply it's a user makefile
2105 * Skip to matching delimiter
2107 for (cp = ++file; *cp != endc; cp++) {
2109 Parse_Error(PARSE_FATAL,
2110 "Unclosed .include filename. '%c' expected", endc);
2117 * Substitute for any variables in the file name before trying to
2120 file = Buf_Peel(Var_Subst(file, VAR_CMD, FALSE));
2123 * Now we know the file's name and its search path, we attempt to
2124 * find the durn thing. A return of NULL indicates the file don't
2129 * Include files contained in double-quotes are first searched
2130 * for relative to the including file's location. We don't want
2131 * to cd there, of course, so we just tack on the old file's
2132 * leading path components and call Dir_FindFile to see if
2133 * we can locate the beast.
2136 /* Make a temporary copy of this, to be safe. */
2137 Fname = estrdup(CURFILE->fname);
2139 prefEnd = strrchr(Fname, '/');
2140 if (prefEnd != NULL) {
2143 newName = estrdup(file);
2145 newName = str_concat(Fname, file, STR_ADDSLASH);
2146 fullname = Path_FindFile(newName, &parseIncPath);
2147 if (fullname == NULL) {
2148 fullname = Path_FindFile(newName,
2161 if (fullname == NULL) {
2163 * System makefile or makefile wasn't found in same directory as
2164 * included makefile. Search for it first on the -I search path,
2165 * then on the .PATH search path, if not found in a -I
2167 * XXX: Suffix specific?
2169 fullname = Path_FindFile(file, &parseIncPath);
2170 if (fullname == NULL) {
2171 fullname = Path_FindFile(file, &dirSearchPath);
2175 if (fullname == NULL) {
2177 * Still haven't found the makefile. Look for it on the system
2178 * path as a last resort.
2180 fullname = Path_FindFile(file, &sysIncPath);
2183 if (fullname == NULL) {
2185 Parse_Error(PARSE_FATAL, "Could not find %s", file);
2192 * We set up the name of the file to be the absolute
2193 * name of the include file so error messages refer to the right
2196 ParsePushInput(fullname, NULL, NULL, 0);
2201 * Parse a .warning or .error directive
2203 * The input is the line minus the ".error"/".warning". We substitute
2204 * variables, print the message and exit(1) (for .error) or just print
2205 * a warning if the directive is malformed.
2208 parse_message(char *line, int iserror, int lineno __unused)
2211 if (!isspace((u_char)*line)) {
2212 Parse_Error(PARSE_WARNING, "invalid syntax: .%s%s",
2213 iserror ? "error" : "warning", line);
2217 while (isspace((u_char)*line))
2220 line = Buf_Peel(Var_Subst(line, VAR_GLOBAL, FALSE));
2221 Parse_Error(iserror ? PARSE_FATAL : PARSE_WARNING, "%s", line);
2225 /* Terminate immediately. */
2232 * Parse an .undef directive.
2235 parse_undef(char *line, int code __unused, int lineno __unused)
2239 while (isspace((u_char)*line))
2242 for (cp = line; !isspace((u_char)*cp) && *cp != '\0'; cp++) {
2247 cp = Buf_Peel(Var_Subst(line, VAR_CMD, FALSE));
2248 Var_Delete(cp, VAR_GLOBAL);
2254 * Parse an .makeenv directive.
2257 parse_makeenv(char *line, int code __unused, int lineno __unused)
2261 while (isspace((u_char)*line))
2264 for (cp = line; !isspace((u_char)*cp) && *cp != '\0'; cp++) {
2269 cp = Buf_Peel(Var_Subst(line, VAR_CMD, FALSE));
2270 Var_SetEnv(cp, VAR_GLOBAL);
2276 * Parse a .for directive.
2279 parse_for(char *line, int code __unused, int lineno)
2282 if (!For_For(line)) {
2289 * Skip after the matching endfor.
2293 line = ParseSkipLine(0, 1);
2295 Parse_Error(PARSE_FATAL,
2296 "Unexpected end of file in for loop.\n");
2299 } while (For_Eval(line));
2308 * Parse endfor. This may only happen if there was no matching .for.
2311 parse_endfor(char *line __unused, int code __unused, int lineno __unused)
2314 Parse_Error(PARSE_FATAL, "for-less endfor");
2319 * Got a line starting with a '.'. Check if this is a directive
2323 * TRUE if line was a directive, FALSE otherwise.
2326 parse_directive(char *line)
2334 * .[[:space:]]*\([[:alpha:]][[:alnum:]_]*\).*
2335 * \1 is the keyword.
2337 for (start = line; isspace((u_char)*start); start++) {
2341 if (!isalpha((u_char)*start)) {
2346 while (isalnum((u_char)*cp) || *cp == '_') {
2350 dir = directive_hash(start, cp - start);
2351 if (dir < 0 || dir >= (int)NDIRECTS ||
2352 (size_t)(cp - start) != strlen(directives[dir].name) ||
2353 strncmp(start, directives[dir].name, cp - start) != 0) {
2354 /* not actually matched */
2358 if (!skipLine || directives[dir].skip_flag)
2359 (*directives[dir].func)(cp, directives[dir].code,
2365 *---------------------------------------------------------------------
2367 * Parse a file into its component parts, incorporating it into the
2368 * current dependency graph. This is the main function and controls
2369 * almost every other function in this module
2375 * Loads. Nodes are added to the list of all targets, nodes and links
2376 * are added to the dependency graph. etc. etc. etc.
2377 *---------------------------------------------------------------------
2380 Parse_File(const char *name, FILE *stream)
2382 char *cp; /* pointer into the line */
2383 char *line; /* the line we're working on */
2388 ParsePushInput(estrdup(name), stream, NULL, 0);
2390 while ((line = ParseReadLine()) != NULL) {
2391 if (*line == '.' && parse_directive(line + 1)) {
2392 /* directive consumed */
2395 if (skipLine || *line == '#') {
2396 /* Skipping .if block or comment. */
2400 if (*line == '\t') {
2402 * If a line starts with a tab, it can only
2403 * hope to be a creation command.
2405 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2414 * So long as it's not a blank
2415 * line and we're actually in a
2416 * dependency spec, add the
2417 * command to the list of
2418 * commands of all targets in
2419 * the dependency spec.
2421 LST_FOREACH(ln, &targets) {
2429 if (!(gn->type & OP_HAS_COMMANDS))
2430 Lst_AtEnd(&gn->commands, cp);
2432 Parse_Error(PARSE_WARNING, "duplicate script "
2433 "for target \"%s\" ignored", gn->name);
2437 Parse_Error(PARSE_FATAL,
2438 "Unassociated shell command \"%s\"",
2443 } else if (strncmp(line, "include", 7) == 0 &&
2444 isspace((unsigned char)line[7]) &&
2445 strchr(line, ':') == NULL) {
2447 * It's an S3/S5-style "include".
2449 ParseTraditionalInclude(line + 7);
2452 } else if (Parse_IsVar(line)) {
2454 Parse_DoVar(line, VAR_GLOBAL);
2458 * We now know it's a dependency line so it
2459 * needs to have all variables expanded before
2460 * being parsed. Tell the variable module to
2461 * complain if some variable is undefined...
2462 * To make life easier on novices, if the line
2463 * is indented we first make sure the line has
2464 * a dependency operator in it. If it doesn't
2465 * have an operator and we're in a dependency
2466 * line's script, we assume it's actually a
2467 * shell command and add it to the current
2468 * list of targets. XXX this comment seems wrong.
2471 if (isspace((unsigned char)line[0])) {
2472 while (*cp != '\0' &&
2473 isspace((unsigned char)*cp)) {
2483 cp = Buf_Peel(Var_Subst(line, VAR_CMD, TRUE));
2489 * Need a non-circular list for the target nodes
2491 Lst_Destroy(&targets, NOFREE);
2494 ParseDoDependency(line);
2504 * Make sure conditionals are clean
2509 errx(1, "fatal errors encountered -- cannot continue");
2513 *-----------------------------------------------------------------------
2515 * Return a Lst of the main target to create for main()'s sake. If
2516 * no such target exists, we Punt with an obnoxious error message.
2519 * A Lst of the single node to create.
2524 *-----------------------------------------------------------------------
2527 Parse_MainName(Lst *listmain)
2530 if (mainNode == NULL) {
2531 Punt("no target to make.");
2533 } else if (mainNode->type & OP_DOUBLEDEP) {
2534 Lst_AtEnd(listmain, mainNode);
2535 Lst_Concat(listmain, &mainNode->cohorts, LST_CONCNEW);
2537 Lst_AtEnd(listmain, mainNode);