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.22.2.2 2004/07/10 08:14:42 eik Exp $
40 * $DragonFly: src/usr.bin/make/parse.c,v 1.19 2004/12/01 15:44:20 joerg Exp $
45 * Functions to parse a makefile.
47 * One function, Parse_Init, must be called before any functions
48 * in this module are used. After that, the function Parse_File is the
49 * main entry point and controls most of the other functions in this
52 * Most important structures are kept in Lsts. Directories for
53 * the #include "..." function are kept in the 'parseIncPath' Lst, while
54 * those for the #include <...> are kept in the 'sysIncPath' Lst. The
55 * targets currently being defined are kept in the 'targets' Lst.
57 * The variables 'curFile.fname' and 'curFile.lineno' are used to track
58 * the name of the current file and the line number in that file so that
59 * error messages can be more meaningful.
62 * Parse_Init Initialization function which must be
63 * called before anything else in this module
66 * Parse_End Cleanup the module
68 * Parse_File Function used to parse a makefile. It must
69 * be given the name of the file, which should
70 * already have been opened, and a function
71 * to call to read a character from the file.
73 * Parse_IsVar Returns TRUE if the given line is a
74 * variable assignment. Used by MainParseArgs
75 * to determine if an argument is a target
76 * or a variable assignment. Used internally
77 * for pretty much the same thing...
79 * Parse_Error Function called when an error occurs in
80 * parsing. Used by the variable and
81 * conditional modules.
82 * Parse_MainName Returns a Lst of the main target to create.
94 #include "pathnames.h"
97 * These values are returned by ParseEOF to tell Parse_File whether to
98 * CONTINUE parsing, i.e. it had only reached the end of an include file,
103 static Lst targets; /* targets we're working on */
104 static Lst targCmds; /* command lines for targets */
105 static Boolean inLine; /* true if currently in a dependency
106 * line or its commands */
107 static int fatals = 0;
109 static GNode *mainNode; /* The main target to create. This is the
110 * first target on the first dependency
111 * line in the first makefile */
113 IFile curFile; /* current makefile */
115 static Lst includes; /* stack of IFiles generated by
117 Lst parseIncPath; /* list of directories for "..." includes */
118 Lst sysIncPath; /* list of directories for <...> includes */
121 * specType contains the SPECial TYPE of the current target. It is
122 * Not if the target is unspecial. If it *is* special, however, the children
123 * are linked as children of the parent but not vice versa. This variable is
124 * set in ParseDoDependency
128 Default, /* .DEFAULT */
130 Ignore, /* .IGNORE */
131 Includes, /* .INCLUDES */
132 Interrupt, /* .INTERRUPT */
134 MFlags, /* .MFLAGS or .MAKEFLAGS */
135 Main, /* .MAIN and we don't have anything user-specified to
137 NoExport, /* .NOEXPORT */
138 Not, /* Not special */
139 NotParallel, /* .NOTPARALELL */
142 Parallel, /* .PARALLEL */
146 Precious, /* .PRECIOUS */
147 ExShell, /* .SHELL */
148 Silent, /* .SILENT */
149 SingleShell, /* .SINGLESHELL */
150 Suffixes, /* .SUFFIXES */
152 Attribute /* Generic attribute */
155 static ParseSpecial specType;
159 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
160 * seen, then set to each successive source on the line.
162 static GNode *predecessor;
165 * The parseKeywords table is searched using binary search when deciding
166 * if a target or source is special. The 'spec' field is the ParseSpecial
167 * type of the keyword ("Not" if the keyword isn't special as a target) while
168 * the 'op' field is the operator to apply to the list of targets if the
169 * keyword is used as a source ("0" if the keyword isn't special as a source)
172 char *name; /* Name of keyword */
173 ParseSpecial spec; /* Type when used as a target */
174 int op; /* Operator when used as a source */
175 } parseKeywords[] = {
176 { ".BEGIN", Begin, 0 },
177 { ".DEFAULT", Default, 0 },
179 { ".EXEC", Attribute, OP_EXEC },
180 { ".IGNORE", Ignore, OP_IGNORE },
181 { ".INCLUDES", Includes, 0 },
182 { ".INTERRUPT", Interrupt, 0 },
183 { ".INVISIBLE", Attribute, OP_INVISIBLE },
184 { ".JOIN", Attribute, OP_JOIN },
185 { ".LIBS", Libs, 0 },
186 { ".MAIN", Main, 0 },
187 { ".MAKE", Attribute, OP_MAKE },
188 { ".MAKEFLAGS", MFlags, 0 },
189 { ".MFLAGS", MFlags, 0 },
190 { ".NOTMAIN", Attribute, OP_NOTMAIN },
191 { ".NOTPARALLEL", NotParallel, 0 },
192 { ".NO_PARALLEL", NotParallel, 0 },
193 { ".NULL", Null, 0 },
194 { ".OPTIONAL", Attribute, OP_OPTIONAL },
195 { ".ORDER", Order, 0 },
196 { ".PARALLEL", Parallel, 0 },
197 { ".PATH", ExPath, 0 },
198 { ".PHONY", Phony, OP_PHONY },
199 { ".POSIX", Posix, 0 },
200 { ".PRECIOUS", Precious, OP_PRECIOUS },
201 { ".RECURSIVE", Attribute, OP_MAKE },
202 { ".SHELL", ExShell, 0 },
203 { ".SILENT", Silent, OP_SILENT },
204 { ".SINGLESHELL", SingleShell, 0 },
205 { ".SUFFIXES", Suffixes, 0 },
206 { ".USE", Attribute, OP_USE },
207 { ".WAIT", Wait, 0 },
210 static int ParseFindKeyword(char *);
211 static int ParseLinkSrc(void *, void *);
212 static int ParseDoOp(void *, void *);
213 static int ParseAddDep(void *, void *);
214 static void ParseDoSrc(int, char *, Lst);
215 static int ParseFindMain(void *, void *);
216 static int ParseAddDir(void *, void *);
217 static int ParseClearPath(void *, void *);
218 static void ParseDoDependency(char *);
219 static int ParseAddCmd(void *, void *);
220 static int ParseReadc(void);
221 static void ParseUnreadc(int);
222 static void ParseHasCommands(void *);
223 static void ParseDoInclude(char *);
224 static void ParseDoError(char *);
225 static void ParseDoWarning(char *);
227 static void ParseTraditionalInclude(char *);
229 static int ParseEOF(int);
230 static char *ParseReadLine(void);
231 static char *ParseSkipLine(int, int);
232 static void ParseFinishLine(void);
235 *----------------------------------------------------------------------
236 * ParseFindKeyword --
237 * Look in the table of keywords for one matching the given string.
240 * The index of the keyword, or -1 if it isn't there.
244 *----------------------------------------------------------------------
247 ParseFindKeyword (char *str)
255 end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
258 cur = start + ((end - start) / 2);
259 diff = strcmp (str, parseKeywords[cur].name);
263 } else if (diff < 0) {
268 } while (start <= end);
274 * Error message abort function for parsing. Prints out the context
275 * of the error (line number and file) as well as the message with
276 * two optional arguments.
282 * "fatals" is incremented if the level is PARSE_FATAL.
286 Parse_Error(int type, const char *fmt, ...)
291 (void)fprintf(stderr, "\"%s\", line %d: ",
292 curFile.fname, curFile.lineno);
293 if (type == PARSE_WARNING)
294 (void)fprintf(stderr, "warning: ");
295 (void)vfprintf(stderr, fmt, ap);
297 (void)fprintf(stderr, "\n");
298 (void)fflush(stderr);
299 if (type == PARSE_FATAL)
304 *---------------------------------------------------------------------
306 * Link the parent node to its new child. Used in a Lst_ForEach by
307 * ParseDoDependency. If the specType isn't 'Not', the parent
308 * isn't linked as a parent of the child.
314 * New elements are added to the parents list of cgn and the
315 * children list of cgn. the unmade field of pgn is updated
316 * to reflect the additional child.
317 *---------------------------------------------------------------------
320 ParseLinkSrc (void *pgnp, void *cgnp)
322 GNode *pgn = (GNode *) pgnp;
323 GNode *cgn = (GNode *) cgnp;
324 if (Lst_Member (pgn->children, (void *)cgn) == NULL) {
325 (void)Lst_AtEnd (pgn->children, (void *)cgn);
326 if (specType == Not) {
327 (void)Lst_AtEnd (cgn->parents, (void *)pgn);
335 *---------------------------------------------------------------------
337 * Apply the parsed operator to the given target node. Used in a
338 * Lst_ForEach call by ParseDoDependency once all targets have
339 * been found and their operator parsed. If the previous and new
340 * operators are incompatible, a major error is taken.
346 * The type field of the node is altered to reflect any new bits in
348 *---------------------------------------------------------------------
351 ParseDoOp (void *gnp, void *opp)
353 GNode *gn = (GNode *) gnp;
354 int op = *(int *) opp;
356 * If the dependency mask of the operator and the node don't match and
357 * the node has actually had an operator applied to it before, and
358 * the operator actually has some dependency information in it, complain.
360 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
361 !OP_NOP(gn->type) && !OP_NOP(op))
363 Parse_Error (PARSE_FATAL, "Inconsistent operator for %s", gn->name);
367 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
369 * If the node was the object of a :: operator, we need to create a
370 * new instance of it for the children and commands on this dependency
371 * line. The new instance is placed on the 'cohorts' list of the
372 * initial one (note the initial one is not on its own cohorts list)
373 * and the new instance is linked to all parents of the initial
379 cohort = Targ_NewGN(gn->name);
381 * Duplicate links to parents so graph traversal is simple. Perhaps
382 * some type bits should be duplicated?
384 * Make the cohort invisible as well to avoid duplicating it into
385 * other variables. True, parents of this target won't tend to do
386 * anything with their local variables, but better safe than
389 Lst_ForEach(gn->parents, ParseLinkSrc, (void *)cohort);
390 cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
391 (void)Lst_AtEnd(gn->cohorts, (void *)cohort);
394 * Replace the node in the targets list with the new copy
396 ln = Lst_Member(targets, (void *)gn);
397 Lst_Replace(ln, (void *)cohort);
401 * We don't want to nuke any previous flags (whatever they were) so we
402 * just OR the new operator into the old
410 *---------------------------------------------------------------------
412 * Check if the pair of GNodes given needs to be synchronized.
413 * This has to be when two nodes are on different sides of a
417 * Returns 1 if the two targets need to be ordered, 0 otherwise.
418 * If it returns 1, the search can stop
421 * A dependency can be added between the two nodes.
423 *---------------------------------------------------------------------
426 ParseAddDep(void *pp, void *sp)
428 GNode *p = (GNode *) pp;
429 GNode *s = (GNode *) sp;
431 if (p->order < s->order) {
433 * XXX: This can cause loops, and loops can cause unmade targets,
434 * but checking is tedious, and the debugging output can show the
437 (void)Lst_AtEnd(p->successors, (void *)s);
438 (void)Lst_AtEnd(s->preds, (void *)p);
447 *---------------------------------------------------------------------
449 * Given the name of a source, figure out if it is an attribute
450 * and apply it to the targets if it is. Else decide if there is
451 * some attribute which should be applied *to* the source because
452 * of some special target and apply it if so. Otherwise, make the
453 * source be a child of the targets in the list 'targets'
459 * Operator bits may be added to the list of targets or to the source.
460 * The targets may have a new source added to their lists of children.
461 *---------------------------------------------------------------------
464 ParseDoSrc (int tOp, char *src, Lst allsrc)
468 if (*src == '.' && isupper ((unsigned char) src[1])) {
469 int keywd = ParseFindKeyword(src);
471 int op = parseKeywords[keywd].op;
473 Lst_ForEach (targets, ParseDoOp, (void *)&op);
476 if (parseKeywords[keywd].spec == Wait) {
486 * If we have noted the existence of a .MAIN, it means we need
487 * to add the sources of said target to the list of things
488 * to create. The string 'src' is likely to be free, so we
489 * must make a new copy of it. Note that this will only be
490 * invoked if the user didn't specify a target on the command
491 * line. This is to allow #ifmake's to succeed, or something...
493 (void) Lst_AtEnd (create, (void *)estrdup(src));
495 * Add the name to the .TARGETS variable as well, so the user cna
496 * employ that, if desired.
498 Var_Append(".TARGETS", src, VAR_GLOBAL);
503 * Create proper predecessor/successor links between the previous
504 * source and the current one.
506 gn = Targ_FindNode(src, TARG_CREATE);
507 if (predecessor != NULL) {
508 (void)Lst_AtEnd(predecessor->successors, (void *)gn);
509 (void)Lst_AtEnd(gn->preds, (void *)predecessor);
512 * The current source now becomes the predecessor for the next one.
519 * If the source is not an attribute, we need to find/create
520 * a node for it. After that we can apply any operator to it
521 * from a special target or link it to its parents, as
524 * In the case of a source that was the object of a :: operator,
525 * the attribute is applied to all of its instances (as kept in
526 * the 'cohorts' list of the node) or all the cohorts are linked
527 * to all the targets.
529 gn = Targ_FindNode (src, TARG_CREATE);
533 Lst_ForEach (targets, ParseLinkSrc, (void *)gn);
535 if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
539 for (ln=Lst_First(gn->cohorts); ln != NULL; ln = Lst_Succ(ln)){
540 cohort = (GNode *)Lst_Datum(ln);
544 Lst_ForEach(targets, ParseLinkSrc, (void *)cohort);
552 (void)Lst_AtEnd(allsrc, (void *)gn);
554 Lst_ForEach(allsrc, ParseAddDep, (void *)gn);
559 *-----------------------------------------------------------------------
561 * Find a real target in the list and set it to be the main one.
562 * Called by ParseDoDependency when a main target hasn't been found
566 * 0 if main not found yet, 1 if it is.
569 * mainNode is changed and Targ_SetMain is called.
571 *-----------------------------------------------------------------------
574 ParseFindMain(void *gnp, void *dummy __unused)
576 GNode *gn = (GNode *) gnp;
577 if ((gn->type & (OP_NOTMAIN|OP_USE|OP_EXEC|OP_TRANSFORM)) == 0) {
587 *-----------------------------------------------------------------------
589 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
597 *-----------------------------------------------------------------------
600 ParseAddDir(void *path, void *name)
602 Dir_AddDir((Lst) path, (char *) name);
607 *-----------------------------------------------------------------------
609 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
617 *-----------------------------------------------------------------------
620 ParseClearPath(void *path, void *dummy __unused)
622 Dir_ClearPath((Lst) path);
627 *---------------------------------------------------------------------
628 * ParseDoDependency --
629 * Parse the dependency line in line.
635 * The nodes of the sources are linked as children to the nodes of the
636 * targets. Some nodes may be created.
638 * We parse a dependency line by first extracting words from the line and
639 * finding nodes in the list of all targets with that name. This is done
640 * until a character is encountered which is an operator character. Currently
641 * these are only ! and :. At this point the operator is parsed and the
642 * pointer into the line advanced until the first source is encountered.
643 * The parsed operator is applied to each node in the 'targets' list,
644 * which is where the nodes found for the targets are kept, by means of
645 * the ParseDoOp function.
646 * The sources are read in much the same way as the targets were except
647 * that now they are expanded using the wildcarding scheme of the C-Shell
648 * and all instances of the resulting words in the list of all targets
649 * are found. Each of the resulting nodes is then linked to each of the
650 * targets as one of its children.
651 * Certain targets are handled specially. These are the ones detailed
652 * by the specType variable.
653 * The storing of transformation rules is also taken care of here.
654 * A target is recognized as a transformation rule by calling
655 * Suff_IsTransform. If it is a transformation rule, its node is gotten
656 * from the suffix module via Suff_AddTransform rather than the standard
657 * Targ_FindNode in the target module.
658 *---------------------------------------------------------------------
661 ParseDoDependency (char *line)
663 char *cp; /* our current position */
664 GNode *gn; /* a general purpose temporary node */
665 int op; /* the operator on the line */
666 char savec; /* a place to save a character */
667 Lst paths; /* List of search paths to alter when parsing
668 * a list of .PATH targets */
669 int tOp; /* operator from special target */
670 Lst sources; /* list of archive source names after
672 Lst curTargs; /* list of target names to be found and added
673 * to the targets list */
674 Lst curSrcs; /* list of sources in order */
682 curTargs = Lst_Init(FALSE);
683 curSrcs = Lst_Init(FALSE);
687 *cp && !isspace ((unsigned char) *cp) && *cp != '(';
692 * Must be a dynamic source (would have been expanded
693 * otherwise), so call the Var module to parse the puppy
694 * so we can safely advance beyond it...There should be
695 * no errors in this, as they would have been discovered
696 * in the initial Var_Subst and we wouldn't be here.
702 result=Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
708 } else if (*cp == '!' || *cp == ':') {
710 * We don't want to end a word on ':' or '!' if there is a
711 * better match later on in the string (greedy matching).
712 * This allows the user to have targets like:
715 * where "fie::fi:fo" and "foo::bar" are the targets. In
716 * real life this is used for perl5 library man pages where
717 * "::" separates an object from its class.
718 * Ie: "File::Spec::Unix". This behaviour is also consistent
719 * with other versions of make.
723 if (*cp == ':' && *p == ':')
726 /* Found the best match already. */
727 if (*p == '\0' || isspace(*p))
730 p += strcspn(p, "!:");
732 /* No better match later on... */
740 * Archives must be handled specially to make sure the OP_ARCHV
741 * flag is set in their 'type' field, for one thing, and because
742 * things like "archive(file1.o file2.o file3.o)" are permissible.
743 * Arch_ParseArchive will set 'line' to be the first non-blank
744 * after the archive-spec. It creates/finds nodes for the members
745 * and places them on the given list, returning SUCCESS if all
746 * went well and FAILURE if there was an error in the
747 * specification. On error, line should remain untouched.
749 if (Arch_ParseArchive (&line, targets, VAR_CMD) != SUCCESS) {
750 Parse_Error (PARSE_FATAL,
751 "Error in archive specification: \"%s\"", line);
761 * Ending a dependency line without an operator is a Bozo
762 * no-no. As a heuristic, this is also often triggered by
763 * undetected conflicts from cvs/rcs merges.
765 if ((strncmp(line, "<<<<<<", 6) == 0) ||
766 (strncmp(line, "======", 6) == 0) ||
767 (strncmp(line, ">>>>>>", 6) == 0))
768 Parse_Error (PARSE_FATAL,
769 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
771 Parse_Error (PARSE_FATAL, "Need an operator");
776 * Have a word in line. See if it's a special target and set
777 * specType to match it.
779 if (*line == '.' && isupper ((unsigned char) line[1])) {
781 * See if the target is a special target that must have it
782 * or its sources handled specially.
784 int keywd = ParseFindKeyword(line);
786 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
787 Parse_Error(PARSE_FATAL, "Mismatched special targets");
791 specType = parseKeywords[keywd].spec;
792 tOp = parseKeywords[keywd].op;
795 * Certain special targets have special semantics:
796 * .PATH Have to set the dirSearchPath
798 * .MAIN Its sources are only used if
799 * nothing has been specified to
801 * .DEFAULT Need to create a node to hang
802 * commands on, but we don't want
803 * it in the graph, nor do we want
804 * it to be the Main Target, so we
805 * create it, set OP_NOTMAIN and
806 * add it to the list, setting
807 * DEFAULT to the new node for
808 * later use. We claim the node is
809 * A transformation rule to make
810 * life easier later, when we'll
811 * use Make_HandleUse to actually
812 * apply the .DEFAULT commands.
813 * .PHONY The list of targets
816 * .INTERRUPT Are not to be considered the
818 * .NOTPARALLEL Make only one target at a time.
819 * .SINGLESHELL Create a shell for each command.
820 * .ORDER Must set initial predecessor to NULL
825 paths = Lst_Init(FALSE);
827 (void)Lst_AtEnd(paths, (void *)dirSearchPath);
830 if (!Lst_IsEmpty(create)) {
837 gn = Targ_FindNode(line, TARG_CREATE);
838 gn->type |= OP_NOTMAIN;
839 (void)Lst_AtEnd(targets, (void *)gn);
842 gn = Targ_NewGN(".DEFAULT");
843 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
844 (void)Lst_AtEnd(targets, (void *)gn);
861 } else if (strncmp (line, ".PATH", 5) == 0) {
863 * .PATH<suffix> has to be handled specially.
864 * Call on the suffix module to give us a path to
870 path = Suff_GetPath (&line[5]);
872 Parse_Error (PARSE_FATAL,
873 "Suffix '%s' not defined (yet)",
877 if (paths == (Lst)NULL) {
878 paths = Lst_Init(FALSE);
880 (void)Lst_AtEnd(paths, (void *)path);
886 * Have word in line. Get or create its node and stick it at
887 * the end of the targets list
889 if ((specType == Not) && (*line != '\0')) {
890 if (Dir_HasWildcards(line)) {
892 * Targets are to be sought only in the current directory,
893 * so create an empty path for the thing. Note we need to
894 * use Dir_Destroy in the destruction of the path as the
895 * Dir module could have added a directory to the path...
897 Lst emptyPath = Lst_Init(FALSE);
899 Dir_Expand(line, emptyPath, curTargs);
901 Lst_Destroy(emptyPath, Dir_Destroy);
904 * No wildcards, but we want to avoid code duplication,
905 * so create a list with the word on it.
907 (void)Lst_AtEnd(curTargs, (void *)line);
910 while(!Lst_IsEmpty(curTargs)) {
911 char *targName = (char *)Lst_DeQueue(curTargs);
913 if (!Suff_IsTransform (targName)) {
914 gn = Targ_FindNode (targName, TARG_CREATE);
916 gn = Suff_AddTransform (targName);
919 (void)Lst_AtEnd (targets, (void *)gn);
921 } else if (specType == ExPath && *line != '.' && *line != '\0') {
922 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
927 * If it is a special type and not .PATH, it's the only target we
928 * allow on this line...
930 if (specType != Not && specType != ExPath) {
931 Boolean warn = FALSE;
933 while ((*cp != '!') && (*cp != ':') && *cp) {
934 if (*cp != ' ' && *cp != '\t') {
940 Parse_Error(PARSE_WARNING, "Extra target ignored");
943 while (*cp && isspace ((unsigned char) *cp)) {
948 } while ((*line != '!') && (*line != ':') && *line);
951 * Don't need the list of target names anymore...
953 Lst_Destroy(curTargs, NOFREE);
955 if (!Lst_IsEmpty(targets)) {
958 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
965 * These four create nodes on which to hang commands, so
966 * targets shouldn't be empty...
970 * Nothing special here -- targets can be empty if it wants.
977 * Have now parsed all the target names. Must parse the operator next. The
978 * result is left in op .
982 } else if (*cp == ':') {
990 Parse_Error (PARSE_FATAL, "Missing dependency operator");
994 cp++; /* Advance beyond operator */
996 Lst_ForEach (targets, ParseDoOp, (void *)&op);
999 * Get to the first source
1001 while (*cp && isspace ((unsigned char) *cp)) {
1007 * Several special targets take different actions if present with no
1009 * a .SUFFIXES line with no sources clears out all old suffixes
1010 * a .PRECIOUS line makes all targets precious
1011 * a .IGNORE line ignores errors for all targets
1012 * a .SILENT line creates silence when making all targets
1013 * a .PATH removes all directories from the search path(s).
1018 Suff_ClearSuffixes ();
1024 ignoreErrors = TRUE;
1030 Lst_ForEach(paths, ParseClearPath, (void *)NULL);
1033 Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1038 } else if (specType == MFlags) {
1040 * Call on functions in main.c to deal with these arguments and
1041 * set the initial character to a null-character so the loop to
1042 * get sources won't get anything
1044 Main_ParseArgLine (line);
1046 } else if (specType == ExShell) {
1047 if (Job_ParseShell (line) != SUCCESS) {
1048 Parse_Error (PARSE_FATAL, "improper shell specification");
1052 } else if ((specType == NotParallel) || (specType == SingleShell)) {
1057 * NOW GO FOR THE SOURCES
1059 if ((specType == Suffixes) || (specType == ExPath) ||
1060 (specType == Includes) || (specType == Libs) ||
1065 * If the target was one that doesn't take files as its sources
1066 * but takes something like suffixes, we take each
1067 * space-separated word on the line as a something and deal
1068 * with it accordingly.
1070 * If the target was .SUFFIXES, we take each source as a
1071 * suffix and add it to the list of suffixes maintained by the
1074 * If the target was a .PATH, we add the source as a directory
1075 * to search on the search path.
1077 * If it was .INCLUDES, the source is taken to be the suffix of
1078 * files which will be #included and whose search path should
1079 * be present in the .INCLUDES variable.
1081 * If it was .LIBS, the source is taken to be the suffix of
1082 * files which are considered libraries and whose search path
1083 * should be present in the .LIBS variable.
1085 * If it was .NULL, the source is the suffix to use when a file
1086 * has no valid suffix.
1089 while (*cp && !isspace ((unsigned char) *cp)) {
1096 Suff_AddSuffix (line);
1099 Lst_ForEach(paths, ParseAddDir, (void *)line);
1102 Suff_AddInclude (line);
1108 Suff_SetNull (line);
1114 if (savech != '\0') {
1117 while (*cp && isspace ((unsigned char) *cp)) {
1123 Lst_Destroy(paths, NOFREE);
1128 * The targets take real sources, so we must beware of archive
1129 * specifications (i.e. things with left parentheses in them)
1130 * and handle them accordingly.
1132 while (*cp && !isspace ((unsigned char) *cp)) {
1133 if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) {
1135 * Only stop for a left parenthesis if it isn't at the
1136 * start of a word (that'll be for variable changes
1137 * later) and isn't preceded by a dollar sign (a dynamic
1149 sources = Lst_Init (FALSE);
1150 if (Arch_ParseArchive (&line, sources, VAR_CMD) != SUCCESS) {
1151 Parse_Error (PARSE_FATAL,
1152 "Error in source archive spec \"%s\"", line);
1156 while (!Lst_IsEmpty (sources)) {
1157 gnp = (GNode *) Lst_DeQueue (sources);
1158 ParseDoSrc (tOp, gnp->name, curSrcs);
1160 Lst_Destroy (sources, NOFREE);
1168 ParseDoSrc (tOp, line, curSrcs);
1170 while (*cp && isspace ((unsigned char) *cp)) {
1177 if (mainNode == NULL) {
1179 * If we have yet to decide on a main target to make, in the
1180 * absence of any user input, we want the first target on
1181 * the first dependency line that is actually a real target
1182 * (i.e. isn't a .USE or .EXEC rule) to be made.
1184 Lst_ForEach (targets, ParseFindMain, (void *)0);
1188 * Finally, destroy the list of sources
1190 Lst_Destroy(curSrcs, NOFREE);
1194 *---------------------------------------------------------------------
1196 * Return TRUE if the passed line is a variable assignment. A variable
1197 * assignment consists of a single word followed by optional whitespace
1198 * followed by either a += or an = operator.
1199 * This function is used both by the Parse_File function and main when
1200 * parsing the command-line arguments.
1203 * TRUE if it is. FALSE if it ain't
1207 *---------------------------------------------------------------------
1210 Parse_IsVar (char *line)
1212 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1213 Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1215 #define ISEQOPERATOR(c) \
1216 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1219 * Skip to variable name
1221 for (;(*line == ' ') || (*line == '\t'); line++)
1224 for (; *line != '=' || level != 0; line++)
1228 * end-of-line -- can't be a variable assignment.
1235 * there can be as much white space as desired so long as there is
1236 * only one word before the operator
1252 if (wasSpace && haveName) {
1253 if (ISEQOPERATOR(*line)) {
1255 * We must have a finished word
1261 * When an = operator [+?!:] is found, the next
1262 * character must be an = or it ain't a valid
1269 * This is a shell command
1271 if (strncmp(line, ":sh", 3) == 0)
1276 * This is the start of another word, so not assignment.
1291 *---------------------------------------------------------------------
1293 * Take the variable assignment in the passed line and do it in the
1296 * Note: There is a lexical ambiguity with assignment modifier characters
1297 * in variable names. This routine interprets the character before the =
1298 * as a modifier. Therefore, an assignment like
1300 * is interpreted as "C+ +=" instead of "C++ =".
1306 * the variable structure of the given variable name is altered in the
1308 *---------------------------------------------------------------------
1311 Parse_DoVar (char *line, GNode *ctxt)
1313 char *cp; /* pointer into line */
1315 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1316 } type; /* Type of assignment */
1317 char *opc; /* ptr to operator character to
1318 * null-terminate the variable name */
1320 * Avoid clobbered variable warnings by forcing the compiler
1321 * to ``unregister'' variables
1329 * Skip to variable name
1331 while ((*line == ' ') || (*line == '\t')) {
1336 * Skip to operator character, nulling out whitespace as we go
1338 for (cp = line + 1; *cp != '='; cp++) {
1339 if (isspace ((unsigned char) *cp)) {
1343 opc = cp-1; /* operator is the previous character */
1344 *cp++ = '\0'; /* nuke the = */
1347 * Check operator type
1357 * If the variable already has a value, we don't do anything.
1360 if (Var_Exists(line, ctxt)) {
1385 if (strncmp(opc, ":sh", 3) == 0) {
1395 while (isspace ((unsigned char) *cp)) {
1399 if (type == VAR_APPEND) {
1400 Var_Append (line, cp, ctxt);
1401 } else if (type == VAR_SUBST) {
1403 * Allow variables in the old value to be undefined, but leave their
1404 * invocation alone -- this is done by forcing oldVars to be false.
1405 * XXX: This can cause recursive variables, but that's not hard to do,
1406 * and this allows someone to do something like
1408 * CFLAGS = $(.INCLUDES)
1409 * CFLAGS := -I.. $(CFLAGS)
1411 * And not get an error.
1413 Boolean oldOldVars = oldVars;
1418 * make sure that we set the variable the first time to nothing
1419 * so that it gets substituted!
1421 if (!Var_Exists(line, ctxt))
1422 Var_Set(line, "", ctxt);
1424 cp = Var_Subst(NULL, cp, ctxt, FALSE);
1425 oldVars = oldOldVars;
1427 Var_Set(line, cp, ctxt);
1429 } else if (type == VAR_SHELL) {
1430 Boolean freeCmd = FALSE; /* TRUE if the command needs to be freed, i.e.
1431 * if any variable expansion was performed */
1434 if (strchr(cp, '$') != NULL) {
1436 * There's a dollar sign in the command, so perform variable
1437 * expansion on the whole thing. The resulting string will need
1438 * freeing when we're done, so set freeCmd to TRUE.
1440 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1444 res = Cmd_Exec(cp, &error);
1445 Var_Set(line, res, ctxt);
1449 Parse_Error(PARSE_WARNING, error, cp);
1455 * Normal assignment -- just do it.
1457 Var_Set(line, cp, ctxt);
1464 * Lst_ForEach function to add a command line to all targets
1470 * A new element is added to the commands list of the node.
1473 ParseAddCmd(void *gnp, void *cmd)
1475 GNode *gn = (GNode *) gnp;
1476 /* if target already supplied, ignore commands */
1477 if (!(gn->type & OP_HAS_COMMANDS))
1478 (void)Lst_AtEnd(gn->commands, cmd);
1480 Parse_Error(PARSE_WARNING,
1481 "duplicate script for target \"%s\" ignored",
1487 *-----------------------------------------------------------------------
1488 * ParseHasCommands --
1489 * Callback procedure for Parse_File when destroying the list of
1490 * targets on the last dependency line. Marks a target as already
1491 * having commands if it does, to keep from having shell commands
1492 * on multiple dependency lines.
1498 * OP_HAS_COMMANDS may be set for the target.
1500 *-----------------------------------------------------------------------
1503 ParseHasCommands(void *gnp)
1505 GNode *gn = (GNode *) gnp;
1506 if (!Lst_IsEmpty(gn->commands)) {
1507 gn->type |= OP_HAS_COMMANDS;
1512 *-----------------------------------------------------------------------
1513 * Parse_AddIncludeDir --
1514 * Add a directory to the path searched for included makefiles
1515 * bracketed by double-quotes. Used by functions in main.c
1521 * The directory is appended to the list.
1523 *-----------------------------------------------------------------------
1526 Parse_AddIncludeDir (char *dir)
1528 Dir_AddDir (parseIncPath, dir);
1531 /*---------------------------------------------------------------------
1533 * Handle error directive
1535 * The input is the line minus the ".error". We substitute variables,
1536 * print the message and exit(1) or just print a warning if the ".error"
1537 * directive is malformed.
1539 *---------------------------------------------------------------------
1542 ParseDoError(char *errmsg)
1544 if (!isspace((unsigned char) *errmsg)) {
1545 Parse_Error(PARSE_WARNING, "invalid syntax: .error%s", errmsg);
1549 while (isspace((unsigned char) *errmsg))
1552 errmsg = Var_Subst(NULL, errmsg, VAR_GLOBAL, FALSE);
1554 Parse_Error(PARSE_FATAL, "%s", errmsg);
1555 /* Terminate immediately. */
1559 /*---------------------------------------------------------------------
1561 * Handle warning directive
1563 * The input is the line minus the ".warning". We substitute variables
1564 * and print the message or just print a warning if the ".warning"
1565 * directive is malformed.
1567 *---------------------------------------------------------------------
1570 ParseDoWarning(char *warnmsg)
1572 if (!isspace((unsigned char) *warnmsg)) {
1573 Parse_Error(PARSE_WARNING, "invalid syntax: .warning%s",
1578 while (isspace((unsigned char) *warnmsg))
1581 warnmsg = Var_Subst(NULL, warnmsg, VAR_GLOBAL, FALSE);
1583 Parse_Error(PARSE_WARNING, "%s", warnmsg);
1587 *---------------------------------------------------------------------
1589 * Push to another file.
1591 * The input is the line minus the #include. A file spec is a string
1592 * enclosed in <> or "". The former is looked for only in sysIncPath.
1593 * The latter in . and the directories specified by -I command line
1600 * A structure is added to the includes Lst and readProc, curFile.lineno,
1601 * curFile.fname and curFile.F are altered for the new file
1602 *---------------------------------------------------------------------
1605 ParseDoInclude (char *file)
1607 char *fullname; /* full pathname of file */
1608 IFile *oldFile; /* state associated with current file */
1609 char endc; /* the character which ends the file spec */
1610 char *cp; /* current position in file spec */
1611 Boolean isSystem; /* TRUE if makefile is a system makefile */
1614 * Skip to delimiter character so we know where to look
1616 while ((*file == ' ') || (*file == '\t')) {
1620 if ((*file != '"') && (*file != '<')) {
1621 Parse_Error (PARSE_FATAL,
1622 ".include filename must be delimited by '\"' or '<'");
1627 * Set the search path on which to find the include file based on the
1628 * characters which bracket its name. Angle-brackets imply it's
1629 * a system Makefile while double-quotes imply it's a user makefile
1640 * Skip to matching delimiter
1642 for (cp = ++file; *cp && *cp != endc; cp++) {
1647 Parse_Error (PARSE_FATAL,
1648 "Unclosed %cinclude filename. '%c' expected",
1655 * Substitute for any variables in the file name before trying to
1658 file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1661 * Now we know the file's name and its search path, we attempt to
1662 * find the durn thing. A return of NULL indicates the file don't
1667 * Include files contained in double-quotes are first searched for
1668 * relative to the including file's location. We don't want to
1669 * cd there, of course, so we just tack on the old file's
1670 * leading path components and call Dir_FindFile to see if
1671 * we can locate the beast.
1673 char *prefEnd, *Fname;
1675 /* Make a temporary copy of this, to be safe. */
1676 Fname = estrdup(curFile.fname);
1678 prefEnd = strrchr (Fname, '/');
1679 if (prefEnd != (char *)NULL) {
1684 newName = estrdup(file);
1686 newName = str_concat (Fname, file, STR_ADDSLASH);
1687 fullname = Dir_FindFile (newName, parseIncPath);
1688 if (fullname == (char *)NULL) {
1689 fullname = Dir_FindFile(newName, dirSearchPath);
1694 fullname = (char *)NULL;
1698 fullname = (char *)NULL;
1701 if (fullname == (char *)NULL) {
1703 * System makefile or makefile wasn't found in same directory as
1704 * included makefile. Search for it first on the -I search path,
1705 * then on the .PATH search path, if not found in a -I directory.
1706 * XXX: Suffix specific?
1708 fullname = Dir_FindFile (file, parseIncPath);
1709 if (fullname == (char *)NULL) {
1710 fullname = Dir_FindFile(file, dirSearchPath);
1714 if (fullname == (char *)NULL) {
1716 * Still haven't found the makefile. Look for it on the system
1717 * path as a last resort.
1719 fullname = Dir_FindFile(file, sysIncPath);
1722 if (fullname == (char *) NULL) {
1724 Parse_Error (PARSE_FATAL, "Could not find %s", file);
1731 * Once we find the absolute path to the file, we get to save all the
1732 * state from the current file before we can start reading this
1733 * include file. The state is stored in an IFile structure which
1734 * is placed on a list with other IFile structures. The list makes
1735 * a very nice stack to track how we got here...
1737 oldFile = (IFile *) emalloc (sizeof (IFile));
1738 memcpy(oldFile, &curFile, sizeof (IFile));
1740 (void) Lst_AtFront (includes, (void *)oldFile);
1743 * Once the previous state has been saved, we can get down to reading
1744 * the new file. We set up the name of the file to be the absolute
1745 * name of the include file so error messages refer to the right
1746 * place. Naturally enough, we start reading at line number 0.
1748 curFile.fname = fullname;
1751 curFile.F = fopen (fullname, "r");
1753 if (curFile.F == (FILE * ) NULL) {
1754 Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1756 * Pop to previous file
1760 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
1767 *---------------------------------------------------------------------
1768 * Parse_FromString --
1769 * Start Parsing from the given string
1775 * A structure is added to the includes Lst and readProc, curFile.lineno,
1776 * curFile.fname and curFile.F are altered for the new file
1777 *---------------------------------------------------------------------
1780 Parse_FromString(char *str, int lineno)
1782 IFile *oldFile; /* state associated with this file */
1784 DEBUGF(FOR, ("%s\n---- at line %d\n", str, lineno));
1786 oldFile = (IFile *) emalloc (sizeof (IFile));
1787 memcpy(oldFile, &curFile, sizeof (IFile));
1789 (void) Lst_AtFront (includes, (void *)oldFile);
1792 curFile.p = (PTR *) emalloc (sizeof (PTR));
1793 curFile.p->str = curFile.p->ptr = str;
1794 curFile.lineno = lineno;
1795 curFile.fname = estrdup(curFile.fname);
1801 *---------------------------------------------------------------------
1802 * ParseTraditionalInclude --
1803 * Push to another file.
1805 * The input is the line minus the "include". The file name is
1806 * the string following the "include".
1812 * A structure is added to the includes Lst and readProc, curFile.lineno,
1813 * curFile.fname and curFile.F are altered for the new file
1814 *---------------------------------------------------------------------
1817 ParseTraditionalInclude (char *file)
1819 char *fullname; /* full pathname of file */
1820 IFile *oldFile; /* state associated with current file */
1821 char *cp; /* current position in file spec */
1824 * Skip over whitespace
1826 while ((*file == ' ') || (*file == '\t')) {
1830 if (*file == '\0') {
1831 Parse_Error (PARSE_FATAL,
1832 "Filename missing from \"include\"");
1837 * Skip to end of line or next whitespace
1839 for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1846 * Substitute for any variables in the file name before trying to
1849 file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1852 * Now we know the file's name, we attempt to find the durn thing.
1853 * Search for it first on the -I search path, then on the .PATH
1854 * search path, if not found in a -I directory.
1856 fullname = Dir_FindFile (file, parseIncPath);
1857 if (fullname == (char *)NULL) {
1858 fullname = Dir_FindFile(file, dirSearchPath);
1861 if (fullname == (char *)NULL) {
1863 * Still haven't found the makefile. Look for it on the system
1864 * path as a last resort.
1866 fullname = Dir_FindFile(file, sysIncPath);
1869 if (fullname == (char *) NULL) {
1870 Parse_Error (PARSE_FATAL, "Could not find %s", file);
1875 * Once we find the absolute path to the file, we get to save all the
1876 * state from the current file before we can start reading this
1877 * include file. The state is stored in an IFile structure which
1878 * is placed on a list with other IFile structures. The list makes
1879 * a very nice stack to track how we got here...
1881 oldFile = (IFile *) emalloc (sizeof (IFile));
1882 memcpy(oldFile, &curFile, sizeof (IFile));
1884 (void) Lst_AtFront (includes, (void *)oldFile);
1887 * Once the previous state has been saved, we can get down to reading
1888 * the new file. We set up the name of the file to be the absolute
1889 * name of the include file so error messages refer to the right
1890 * place. Naturally enough, we start reading at line number 0.
1892 curFile.fname = fullname;
1895 curFile.F = fopen (fullname, "r");
1897 if (curFile.F == (FILE * ) NULL) {
1898 Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1900 * Pop to previous file
1904 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
1910 *---------------------------------------------------------------------
1912 * Called when EOF is reached in the current file. If we were reading
1913 * an include file, the includes stack is popped and things set up
1914 * to go back to reading the previous file at the previous location.
1917 * CONTINUE if there's more to do. DONE if not.
1920 * The old curFile.F is closed. The includes list is shortened.
1921 * curFile.lineno, curFile.F, and curFile.fname are changed if
1922 * CONTINUE is returned.
1923 *---------------------------------------------------------------------
1926 ParseEOF (int opened)
1928 IFile *ifile; /* the state on the top of the includes stack */
1930 if (Lst_IsEmpty (includes)) {
1931 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
1935 ifile = (IFile *) Lst_DeQueue (includes);
1936 free (curFile.fname);
1937 if (opened && curFile.F) {
1938 (void) fclose (curFile.F);
1939 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
1942 free(curFile.p->str);
1945 memcpy(&curFile, ifile, sizeof (IFile));
1951 *---------------------------------------------------------------------
1953 * Read a character from the current file
1956 * The character that was read
1959 *---------------------------------------------------------------------
1965 return fgetc(curFile.F);
1967 if (curFile.p && *curFile.p->ptr)
1968 return *curFile.p->ptr++;
1974 *---------------------------------------------------------------------
1976 * Put back a character to the current file
1982 *---------------------------------------------------------------------
1988 ungetc(c, curFile.F);
1992 *--(curFile.p->ptr) = c;
1999 * Grab the next line unless it begins with a dot (`.') and we're told to
2000 * ignore such lines.
2003 ParseSkipLine(int skip, int keep_newline)
2006 int c, lastc, lineLength = 0;
2009 buf = Buf_Init(MAKE_BSIZE);
2012 Buf_Discard(buf, lineLength);
2015 while (((c = ParseReadc()) != '\n' || lastc == '\\')
2017 if (skip && c == '#' && lastc != '\\') {
2018 /* let a comment be terminated even by an escaped \n.
2019 * This is consistent to comment handling in ParseReadLine */
2020 while ((c = ParseReadc()) != '\n' && c != EOF)
2026 Buf_AddByte(buf, (Byte)c);
2028 Buf_ReplaceLastByte(buf, (Byte)' ');
2031 while ((c = ParseReadc()) == ' ' || c == '\t')
2038 Buf_AddByte(buf, (Byte)c);
2043 Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
2044 Buf_Destroy(buf, TRUE);
2045 return((char *)NULL);
2049 Buf_AddByte(buf, (Byte)'\0');
2050 line = (char *)Buf_GetAll(buf, &lineLength);
2051 } while (skip == 1 && line[0] != '.');
2053 Buf_Destroy(buf, FALSE);
2059 *---------------------------------------------------------------------
2061 * Read an entire line from the input file. Called only by Parse_File.
2062 * To facilitate escaped newlines and what have you, a character is
2063 * buffered in 'lastc', which is '\0' when no characters have been
2064 * read. When we break out of the loop, c holds the terminating
2065 * character and lastc holds a character that should be added to
2066 * the line (unless we don't read anything but a terminator).
2069 * A line w/o its newline
2072 * Only those associated with reading a character
2073 *---------------------------------------------------------------------
2076 ParseReadLine (void)
2078 Buffer buf; /* Buffer for current line */
2079 int c; /* the current character */
2080 int lastc; /* The most-recent character */
2081 Boolean semiNL; /* treat semi-colons as newlines */
2082 Boolean ignDepOp; /* TRUE if should ignore dependency operators
2083 * for the purposes of setting semiNL */
2084 Boolean ignComment; /* TRUE if should ignore comments (in a
2086 char *line; /* Result */
2087 char *ep; /* to strip trailing blanks */
2088 int lineLength; /* Length of result */
2089 int lineno; /* Saved line # */
2096 * Handle special-characters at the beginning of the line. Either a
2097 * leading tab (shell command) or pound-sign (possible conditional)
2098 * forces us to ignore comments and dependency operators and treat
2099 * semi-colons as semi-colons (by leaving semiNL FALSE). This also
2100 * discards completely blank lines.
2106 ignComment = ignDepOp = TRUE;
2108 } else if (c == '\n') {
2110 } else if (c == '#') {
2115 * Anything else breaks out without doing anything
2123 buf = Buf_Init(MAKE_BSIZE);
2125 while (((c = ParseReadc ()) != '\n' || (lastc == '\\')) &&
2132 * Escaped newline: read characters until a non-space or an
2133 * unescaped newline and replace them all by a single space.
2134 * This is done by storing the space over the backslash and
2135 * dropping through with the next nonspace. If it is a
2136 * semi-colon and semiNL is TRUE, it will be recognized as a
2137 * newline in the code below this...
2141 while ((c = ParseReadc ()) == ' ' || c == '\t') {
2144 if (c == EOF || c == '\n') {
2148 * Check for comments, semiNL's, etc. -- easier than
2149 * ParseUnreadc(c); continue;
2158 * Semi-colon: Need to see if it should be interpreted as a
2163 * To make sure the command that may be following this
2164 * semi-colon begins with a tab, we push one back into the
2165 * input stream. This will overwrite the semi-colon in the
2166 * buffer. If there is no command following, this does no
2167 * harm, since the newline remains in the buffer and the
2168 * whole line is ignored.
2177 * Haven't seen a dependency operator before this, so this
2178 * must be a variable assignment -- don't pay attention to
2179 * dependency operators after this.
2182 } else if (lastc == ':' || lastc == '!') {
2184 * Well, we've seen a dependency operator already, but it
2185 * was the previous character, so this is really just an
2186 * expanded variable assignment. Revert semi-colons to
2187 * being just semi-colons again and ignore any more
2188 * dependency operators.
2190 * XXX: Note that a line like "foo : a:=b" will blow up,
2191 * but who'd write a line like that anyway?
2193 ignDepOp = TRUE; semiNL = FALSE;
2198 if (lastc != '\\') {
2200 * If the character is a hash mark and it isn't escaped
2201 * (or we're being compatible), the thing is a comment.
2202 * Skip to the end of the line.
2206 } while ((c != '\n') && (c != EOF));
2210 * Don't add the backslash. Just let the # get copied
2220 if (!ignDepOp && (c == ':' || c == '!')) {
2222 * A semi-colon is recognized as a newline only on
2223 * dependency lines. Dependency lines are lines with a
2224 * colon or an exclamation point. Ergo...
2233 * Copy in the previous character and save this one in lastc.
2235 Buf_AddByte (buf, (Byte)lastc);
2242 if (lastc != '\0') {
2243 Buf_AddByte (buf, (Byte)lastc);
2245 Buf_AddByte (buf, (Byte)'\0');
2246 line = (char *)Buf_GetAll (buf, &lineLength);
2247 Buf_Destroy (buf, FALSE);
2250 * Strip trailing blanks and tabs from the line.
2251 * Do not strip a blank or tab that is preceded by
2257 while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
2258 if (ep > line + 1 && ep[-2] == '\\')
2264 if (line[0] == '.') {
2266 * The line might be a conditional. Ask the conditional module
2267 * about it and act accordingly
2269 switch (Cond_Eval (line)) {
2272 * Skip to next conditional that evaluates to COND_PARSE.
2276 line = ParseSkipLine(1, 0);
2277 } while (line && Cond_Eval(line) != COND_PARSE);
2283 line = ParseReadLine();
2286 if (For_Eval(line)) {
2289 lineno = curFile.lineno;
2292 * Skip after the matching end
2294 line = ParseSkipLine(0, 1);
2296 Parse_Error (PARSE_FATAL,
2297 "Unexpected end of file in for loop.\n");
2300 ok = For_Eval(line);
2306 line = ParseReadLine();
2317 * Hit end-of-file, so return a NULL line to indicate this.
2319 return((char *)NULL);
2324 *-----------------------------------------------------------------------
2325 * ParseFinishLine --
2326 * Handle the end of a dependency group.
2332 * inLine set FALSE. 'targets' list destroyed.
2334 *-----------------------------------------------------------------------
2337 ParseFinishLine(void)
2340 Lst_ForEach(targets, Suff_EndTransform, (void *)NULL);
2341 Lst_Destroy (targets, ParseHasCommands);
2348 stripvarname(char *cp)
2352 while (isspace((unsigned char)*cp))
2355 while (*cp2 && !isspace((unsigned char)*cp2))
2363 *---------------------------------------------------------------------
2365 * Parse a file into its component parts, incorporating it into the
2366 * current dependency graph. This is the main function and controls
2367 * almost every other function in this module
2373 * Loads. Nodes are added to the list of all targets, nodes and links
2374 * are added to the dependency graph. etc. etc. etc.
2375 *---------------------------------------------------------------------
2378 Parse_File(char *name, FILE *stream)
2380 char *cp, /* pointer into the line */
2381 *line; /* the line we're working on */
2384 curFile.fname = name;
2389 Var_Append(".MAKEFILE_LIST", name, VAR_GLOBAL);
2392 while ((line = ParseReadLine ()) != NULL) {
2395 * Lines that begin with the special character are either
2396 * include or undef directives.
2398 for (cp = line + 1; isspace ((unsigned char) *cp); cp++) {
2401 if (strncmp (cp, "include", 7) == 0) {
2402 ParseDoInclude (cp + 7);
2404 } else if (strncmp (cp, "error", 5) == 0) {
2405 ParseDoError(cp + 5);
2407 } else if (strncmp (cp, "warning", 7) == 0) {
2408 ParseDoWarning(cp + 7);
2410 } else if (strncmp(cp, "undef", 5) == 0) {
2411 cp = stripvarname(cp + 5);
2412 cp = Var_Subst(NULL, cp, VAR_CMD, FALSE);
2413 Var_Delete(cp, VAR_GLOBAL);
2415 } else if (strncmp(cp, "makeenv", 7) == 0) {
2416 cp = stripvarname(cp + 7);
2417 Var_SetEnv(cp, VAR_GLOBAL);
2422 /* If we're this far, the line must be a comment. */
2426 if (*line == '\t') {
2428 * If a line starts with a tab, it can only hope to be
2429 * a creation command.
2431 for (cp = line + 1; isspace ((unsigned char) *cp); cp++) {
2437 * So long as it's not a blank line and we're actually
2438 * in a dependency spec, add the command to the list of
2439 * commands of all targets in the dependency spec
2441 Lst_ForEach (targets, ParseAddCmd, cp);
2442 Lst_AtEnd(targCmds, (void *) line);
2445 Parse_Error (PARSE_FATAL,
2446 "Unassociated shell command \"%s\"",
2451 } else if (strncmp (line, "include", 7) == 0 &&
2452 isspace((unsigned char) line[7]) &&
2453 strchr(line, ':') == NULL) {
2455 * It's an S3/S5-style "include".
2457 ParseTraditionalInclude (line + 7);
2460 } else if (Parse_IsVar (line)) {
2462 Parse_DoVar (line, VAR_GLOBAL);
2465 * We now know it's a dependency line so it needs to have all
2466 * variables expanded before being parsed. Tell the variable
2467 * module to complain if some variable is undefined...
2468 * To make life easier on novices, if the line is indented we
2469 * first make sure the line has a dependency operator in it.
2470 * If it doesn't have an operator and we're in a dependency
2471 * line's script, we assume it's actually a shell command
2472 * and add it to the current list of targets.
2475 if (isspace((unsigned char) line[0])) {
2476 while ((*cp != '\0') && isspace((unsigned char) *cp)) {
2486 cp = Var_Subst (NULL, line, VAR_CMD, TRUE);
2491 * Need a non-circular list for the target nodes
2494 Lst_Destroy(targets, NOFREE);
2496 targets = Lst_Init (FALSE);
2499 ParseDoDependency (line);
2507 * Reached EOF, but it may be just EOF of an include file...
2509 } while (ParseEOF(1) == CONTINUE);
2512 * Make sure conditionals are clean
2517 errx(1, "fatal errors encountered -- cannot continue");
2521 *---------------------------------------------------------------------
2523 * initialize the parsing module
2529 * the parseIncPath list is initialized...
2530 *---------------------------------------------------------------------
2536 parseIncPath = Lst_Init (FALSE);
2537 sysIncPath = Lst_Init (FALSE);
2538 includes = Lst_Init (FALSE);
2539 targCmds = Lst_Init (FALSE);
2545 Lst_Destroy(targCmds, (void (*) (void *)) free);
2547 Lst_Destroy(targets, NOFREE);
2548 Lst_Destroy(sysIncPath, Dir_Destroy);
2549 Lst_Destroy(parseIncPath, Dir_Destroy);
2550 Lst_Destroy(includes, NOFREE); /* Should be empty now */
2555 *-----------------------------------------------------------------------
2557 * Return a Lst of the main target to create for main()'s sake. If
2558 * no such target exists, we Punt with an obnoxious error message.
2561 * A Lst of the single node to create.
2566 *-----------------------------------------------------------------------
2569 Parse_MainName(void)
2571 Lst listmain; /* result list */
2573 listmain = Lst_Init (FALSE);
2575 if (mainNode == NULL) {
2576 Punt ("no target to make.");
2578 } else if (mainNode->type & OP_DOUBLEDEP) {
2579 (void) Lst_AtEnd (listmain, (void *)mainNode);
2580 Lst_Concat(listmain, mainNode->cohorts, LST_CONCNEW);
2583 (void) Lst_AtEnd (listmain, (void *)mainNode);