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.17 2004/11/30 17:58:41 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 */
112 static int fatals = 0;
114 static GNode *mainNode; /* The main target to create. This is the
115 * first target on the first dependency
116 * line in the first makefile */
118 * Definitions for handling #include specifications
120 typedef struct IFile {
121 char *fname; /* name of previous file */
122 int lineno; /* saved line number */
123 FILE * F; /* the open stream */
124 PTR * p; /* the char pointer */
127 static IFile curFile;
129 static Lst includes; /* stack of IFiles generated by
131 Lst parseIncPath; /* list of directories for "..." includes */
132 Lst sysIncPath; /* list of directories for <...> includes */
135 * specType contains the SPECial TYPE of the current target. It is
136 * Not if the target is unspecial. If it *is* special, however, the children
137 * are linked as children of the parent but not vice versa. This variable is
138 * set in ParseDoDependency
142 Default, /* .DEFAULT */
144 Ignore, /* .IGNORE */
145 Includes, /* .INCLUDES */
146 Interrupt, /* .INTERRUPT */
148 MFlags, /* .MFLAGS or .MAKEFLAGS */
149 Main, /* .MAIN and we don't have anything user-specified to
151 NoExport, /* .NOEXPORT */
152 Not, /* Not special */
153 NotParallel, /* .NOTPARALELL */
156 Parallel, /* .PARALLEL */
162 Precious, /* .PRECIOUS */
163 ExShell, /* .SHELL */
164 Silent, /* .SILENT */
165 SingleShell, /* .SINGLESHELL */
166 Suffixes, /* .SUFFIXES */
168 Attribute /* Generic attribute */
171 static ParseSpecial specType;
175 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
176 * seen, then set to each successive source on the line.
178 static GNode *predecessor;
181 * The parseKeywords table is searched using binary search when deciding
182 * if a target or source is special. The 'spec' field is the ParseSpecial
183 * type of the keyword ("Not" if the keyword isn't special as a target) while
184 * the 'op' field is the operator to apply to the list of targets if the
185 * keyword is used as a source ("0" if the keyword isn't special as a source)
188 char *name; /* Name of keyword */
189 ParseSpecial spec; /* Type when used as a target */
190 int op; /* Operator when used as a source */
191 } parseKeywords[] = {
192 { ".BEGIN", Begin, 0 },
193 { ".DEFAULT", Default, 0 },
195 { ".EXEC", Attribute, OP_EXEC },
196 { ".IGNORE", Ignore, OP_IGNORE },
197 { ".INCLUDES", Includes, 0 },
198 { ".INTERRUPT", Interrupt, 0 },
199 { ".INVISIBLE", Attribute, OP_INVISIBLE },
200 { ".JOIN", Attribute, OP_JOIN },
201 { ".LIBS", Libs, 0 },
202 { ".MAIN", Main, 0 },
203 { ".MAKE", Attribute, OP_MAKE },
204 { ".MAKEFLAGS", MFlags, 0 },
205 { ".MFLAGS", MFlags, 0 },
206 { ".NOTMAIN", Attribute, OP_NOTMAIN },
207 { ".NOTPARALLEL", NotParallel, 0 },
208 { ".NO_PARALLEL", NotParallel, 0 },
209 { ".NULL", Null, 0 },
210 { ".OPTIONAL", Attribute, OP_OPTIONAL },
211 { ".ORDER", Order, 0 },
212 { ".PARALLEL", Parallel, 0 },
213 { ".PATH", ExPath, 0 },
214 { ".PHONY", Phony, OP_PHONY },
216 { ".POSIX", Posix, 0 },
218 { ".PRECIOUS", Precious, OP_PRECIOUS },
219 { ".RECURSIVE", Attribute, OP_MAKE },
220 { ".SHELL", ExShell, 0 },
221 { ".SILENT", Silent, OP_SILENT },
222 { ".SINGLESHELL", SingleShell, 0 },
223 { ".SUFFIXES", Suffixes, 0 },
224 { ".USE", Attribute, OP_USE },
225 { ".WAIT", Wait, 0 },
228 static int ParseFindKeyword(char *);
229 static int ParseLinkSrc(void *, void *);
230 static int ParseDoOp(void *, void *);
231 static int ParseAddDep(void *, void *);
232 static void ParseDoSrc(int, char *, Lst);
233 static int ParseFindMain(void *, void *);
234 static int ParseAddDir(void *, void *);
235 static int ParseClearPath(void *, void *);
236 static void ParseDoDependency(char *);
237 static int ParseAddCmd(void *, void *);
238 static int ParseReadc(void);
239 static void ParseUnreadc(int);
240 static void ParseHasCommands(void *);
241 static void ParseDoInclude(char *);
242 static void ParseDoError(char *);
243 static void ParseDoWarning(char *);
245 static void ParseTraditionalInclude(char *);
247 static int ParseEOF(int);
248 static char *ParseReadLine(void);
249 static char *ParseSkipLine(int, int);
250 static void ParseFinishLine(void);
253 *----------------------------------------------------------------------
254 * ParseFindKeyword --
255 * Look in the table of keywords for one matching the given string.
258 * The index of the keyword, or -1 if it isn't there.
262 *----------------------------------------------------------------------
265 ParseFindKeyword (char *str)
273 end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
276 cur = start + ((end - start) / 2);
277 diff = strcmp (str, parseKeywords[cur].name);
281 } else if (diff < 0) {
286 } while (start <= end);
292 * Error message abort function for parsing. Prints out the context
293 * of the error (line number and file) as well as the message with
294 * two optional arguments.
300 * "fatals" is incremented if the level is PARSE_FATAL.
304 Parse_Error(int type, const char *fmt, ...)
309 (void)fprintf(stderr, "\"%s\", line %d: ",
310 curFile.fname, curFile.lineno);
311 if (type == PARSE_WARNING)
312 (void)fprintf(stderr, "warning: ");
313 (void)vfprintf(stderr, fmt, ap);
315 (void)fprintf(stderr, "\n");
316 (void)fflush(stderr);
317 if (type == PARSE_FATAL)
322 *---------------------------------------------------------------------
324 * Link the parent node to its new child. Used in a Lst_ForEach by
325 * ParseDoDependency. If the specType isn't 'Not', the parent
326 * isn't linked as a parent of the child.
332 * New elements are added to the parents list of cgn and the
333 * children list of cgn. the unmade field of pgn is updated
334 * to reflect the additional child.
335 *---------------------------------------------------------------------
338 ParseLinkSrc (void *pgnp, void *cgnp)
340 GNode *pgn = (GNode *) pgnp;
341 GNode *cgn = (GNode *) cgnp;
342 if (Lst_Member (pgn->children, (void *)cgn) == NULL) {
343 (void)Lst_AtEnd (pgn->children, (void *)cgn);
344 if (specType == Not) {
345 (void)Lst_AtEnd (cgn->parents, (void *)pgn);
353 *---------------------------------------------------------------------
355 * Apply the parsed operator to the given target node. Used in a
356 * Lst_ForEach call by ParseDoDependency once all targets have
357 * been found and their operator parsed. If the previous and new
358 * operators are incompatible, a major error is taken.
364 * The type field of the node is altered to reflect any new bits in
366 *---------------------------------------------------------------------
369 ParseDoOp (void *gnp, void *opp)
371 GNode *gn = (GNode *) gnp;
372 int op = *(int *) opp;
374 * If the dependency mask of the operator and the node don't match and
375 * the node has actually had an operator applied to it before, and
376 * the operator actually has some dependency information in it, complain.
378 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
379 !OP_NOP(gn->type) && !OP_NOP(op))
381 Parse_Error (PARSE_FATAL, "Inconsistent operator for %s", gn->name);
385 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
387 * If the node was the object of a :: operator, we need to create a
388 * new instance of it for the children and commands on this dependency
389 * line. The new instance is placed on the 'cohorts' list of the
390 * initial one (note the initial one is not on its own cohorts list)
391 * and the new instance is linked to all parents of the initial
397 cohort = Targ_NewGN(gn->name);
399 * Duplicate links to parents so graph traversal is simple. Perhaps
400 * some type bits should be duplicated?
402 * Make the cohort invisible as well to avoid duplicating it into
403 * other variables. True, parents of this target won't tend to do
404 * anything with their local variables, but better safe than
407 Lst_ForEach(gn->parents, ParseLinkSrc, (void *)cohort);
408 cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
409 (void)Lst_AtEnd(gn->cohorts, (void *)cohort);
412 * Replace the node in the targets list with the new copy
414 ln = Lst_Member(targets, (void *)gn);
415 Lst_Replace(ln, (void *)cohort);
419 * We don't want to nuke any previous flags (whatever they were) so we
420 * just OR the new operator into the old
428 *---------------------------------------------------------------------
430 * Check if the pair of GNodes given needs to be synchronized.
431 * This has to be when two nodes are on different sides of a
435 * Returns 1 if the two targets need to be ordered, 0 otherwise.
436 * If it returns 1, the search can stop
439 * A dependency can be added between the two nodes.
441 *---------------------------------------------------------------------
444 ParseAddDep(void *pp, void *sp)
446 GNode *p = (GNode *) pp;
447 GNode *s = (GNode *) sp;
449 if (p->order < s->order) {
451 * XXX: This can cause loops, and loops can cause unmade targets,
452 * but checking is tedious, and the debugging output can show the
455 (void)Lst_AtEnd(p->successors, (void *)s);
456 (void)Lst_AtEnd(s->preds, (void *)p);
465 *---------------------------------------------------------------------
467 * Given the name of a source, figure out if it is an attribute
468 * and apply it to the targets if it is. Else decide if there is
469 * some attribute which should be applied *to* the source because
470 * of some special target and apply it if so. Otherwise, make the
471 * source be a child of the targets in the list 'targets'
477 * Operator bits may be added to the list of targets or to the source.
478 * The targets may have a new source added to their lists of children.
479 *---------------------------------------------------------------------
482 ParseDoSrc (int tOp, char *src, Lst allsrc)
486 if (*src == '.' && isupper ((unsigned char) src[1])) {
487 int keywd = ParseFindKeyword(src);
489 int op = parseKeywords[keywd].op;
491 Lst_ForEach (targets, ParseDoOp, (void *)&op);
494 if (parseKeywords[keywd].spec == Wait) {
504 * If we have noted the existence of a .MAIN, it means we need
505 * to add the sources of said target to the list of things
506 * to create. The string 'src' is likely to be free, so we
507 * must make a new copy of it. Note that this will only be
508 * invoked if the user didn't specify a target on the command
509 * line. This is to allow #ifmake's to succeed, or something...
511 (void) Lst_AtEnd (create, (void *)estrdup(src));
513 * Add the name to the .TARGETS variable as well, so the user cna
514 * employ that, if desired.
516 Var_Append(".TARGETS", src, VAR_GLOBAL);
521 * Create proper predecessor/successor links between the previous
522 * source and the current one.
524 gn = Targ_FindNode(src, TARG_CREATE);
525 if (predecessor != NULL) {
526 (void)Lst_AtEnd(predecessor->successors, (void *)gn);
527 (void)Lst_AtEnd(gn->preds, (void *)predecessor);
530 * The current source now becomes the predecessor for the next one.
537 * If the source is not an attribute, we need to find/create
538 * a node for it. After that we can apply any operator to it
539 * from a special target or link it to its parents, as
542 * In the case of a source that was the object of a :: operator,
543 * the attribute is applied to all of its instances (as kept in
544 * the 'cohorts' list of the node) or all the cohorts are linked
545 * to all the targets.
547 gn = Targ_FindNode (src, TARG_CREATE);
551 Lst_ForEach (targets, ParseLinkSrc, (void *)gn);
553 if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
557 for (ln=Lst_First(gn->cohorts); ln != NULL; ln = Lst_Succ(ln)){
558 cohort = (GNode *)Lst_Datum(ln);
562 Lst_ForEach(targets, ParseLinkSrc, (void *)cohort);
570 (void)Lst_AtEnd(allsrc, (void *)gn);
572 Lst_ForEach(allsrc, ParseAddDep, (void *)gn);
577 *-----------------------------------------------------------------------
579 * Find a real target in the list and set it to be the main one.
580 * Called by ParseDoDependency when a main target hasn't been found
584 * 0 if main not found yet, 1 if it is.
587 * mainNode is changed and Targ_SetMain is called.
589 *-----------------------------------------------------------------------
592 ParseFindMain(void *gnp, void *dummy __unused)
594 GNode *gn = (GNode *) gnp;
595 if ((gn->type & (OP_NOTMAIN|OP_USE|OP_EXEC|OP_TRANSFORM)) == 0) {
605 *-----------------------------------------------------------------------
607 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
615 *-----------------------------------------------------------------------
618 ParseAddDir(void *path, void *name)
620 Dir_AddDir((Lst) path, (char *) name);
625 *-----------------------------------------------------------------------
627 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
635 *-----------------------------------------------------------------------
638 ParseClearPath(void *path, void *dummy __unused)
640 Dir_ClearPath((Lst) path);
645 *---------------------------------------------------------------------
646 * ParseDoDependency --
647 * Parse the dependency line in line.
653 * The nodes of the sources are linked as children to the nodes of the
654 * targets. Some nodes may be created.
656 * We parse a dependency line by first extracting words from the line and
657 * finding nodes in the list of all targets with that name. This is done
658 * until a character is encountered which is an operator character. Currently
659 * these are only ! and :. At this point the operator is parsed and the
660 * pointer into the line advanced until the first source is encountered.
661 * The parsed operator is applied to each node in the 'targets' list,
662 * which is where the nodes found for the targets are kept, by means of
663 * the ParseDoOp function.
664 * The sources are read in much the same way as the targets were except
665 * that now they are expanded using the wildcarding scheme of the C-Shell
666 * and all instances of the resulting words in the list of all targets
667 * are found. Each of the resulting nodes is then linked to each of the
668 * targets as one of its children.
669 * Certain targets are handled specially. These are the ones detailed
670 * by the specType variable.
671 * The storing of transformation rules is also taken care of here.
672 * A target is recognized as a transformation rule by calling
673 * Suff_IsTransform. If it is a transformation rule, its node is gotten
674 * from the suffix module via Suff_AddTransform rather than the standard
675 * Targ_FindNode in the target module.
676 *---------------------------------------------------------------------
679 ParseDoDependency (char *line)
681 char *cp; /* our current position */
682 GNode *gn; /* a general purpose temporary node */
683 int op; /* the operator on the line */
684 char savec; /* a place to save a character */
685 Lst paths; /* List of search paths to alter when parsing
686 * a list of .PATH targets */
687 int tOp; /* operator from special target */
688 Lst sources; /* list of archive source names after
690 Lst curTargs; /* list of target names to be found and added
691 * to the targets list */
692 Lst curSrcs; /* list of sources in order */
700 curTargs = Lst_Init(FALSE);
701 curSrcs = Lst_Init(FALSE);
705 *cp && !isspace ((unsigned char) *cp) && *cp != '(';
710 * Must be a dynamic source (would have been expanded
711 * otherwise), so call the Var module to parse the puppy
712 * so we can safely advance beyond it...There should be
713 * no errors in this, as they would have been discovered
714 * in the initial Var_Subst and we wouldn't be here.
720 result=Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
726 } else if (*cp == '!' || *cp == ':') {
728 * We don't want to end a word on ':' or '!' if there is a
729 * better match later on in the string (greedy matching).
730 * This allows the user to have targets like:
733 * where "fie::fi:fo" and "foo::bar" are the targets. In
734 * real life this is used for perl5 library man pages where
735 * "::" separates an object from its class.
736 * Ie: "File::Spec::Unix". This behaviour is also consistent
737 * with other versions of make.
741 if (*cp == ':' && *p == ':')
744 /* Found the best match already. */
745 if (*p == '\0' || isspace(*p))
748 p += strcspn(p, "!:");
750 /* No better match later on... */
758 * Archives must be handled specially to make sure the OP_ARCHV
759 * flag is set in their 'type' field, for one thing, and because
760 * things like "archive(file1.o file2.o file3.o)" are permissible.
761 * Arch_ParseArchive will set 'line' to be the first non-blank
762 * after the archive-spec. It creates/finds nodes for the members
763 * and places them on the given list, returning SUCCESS if all
764 * went well and FAILURE if there was an error in the
765 * specification. On error, line should remain untouched.
767 if (Arch_ParseArchive (&line, targets, VAR_CMD) != SUCCESS) {
768 Parse_Error (PARSE_FATAL,
769 "Error in archive specification: \"%s\"", line);
779 * Ending a dependency line without an operator is a Bozo
780 * no-no. As a heuristic, this is also often triggered by
781 * undetected conflicts from cvs/rcs merges.
783 if ((strncmp(line, "<<<<<<", 6) == 0) ||
784 (strncmp(line, "======", 6) == 0) ||
785 (strncmp(line, ">>>>>>", 6) == 0))
786 Parse_Error (PARSE_FATAL,
787 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
789 Parse_Error (PARSE_FATAL, "Need an operator");
794 * Have a word in line. See if it's a special target and set
795 * specType to match it.
797 if (*line == '.' && isupper ((unsigned char) line[1])) {
799 * See if the target is a special target that must have it
800 * or its sources handled specially.
802 int keywd = ParseFindKeyword(line);
804 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
805 Parse_Error(PARSE_FATAL, "Mismatched special targets");
809 specType = parseKeywords[keywd].spec;
810 tOp = parseKeywords[keywd].op;
813 * Certain special targets have special semantics:
814 * .PATH Have to set the dirSearchPath
816 * .MAIN Its sources are only used if
817 * nothing has been specified to
819 * .DEFAULT Need to create a node to hang
820 * commands on, but we don't want
821 * it in the graph, nor do we want
822 * it to be the Main Target, so we
823 * create it, set OP_NOTMAIN and
824 * add it to the list, setting
825 * DEFAULT to the new node for
826 * later use. We claim the node is
827 * A transformation rule to make
828 * life easier later, when we'll
829 * use Make_HandleUse to actually
830 * apply the .DEFAULT commands.
831 * .PHONY The list of targets
834 * .INTERRUPT Are not to be considered the
836 * .NOTPARALLEL Make only one target at a time.
837 * .SINGLESHELL Create a shell for each command.
838 * .ORDER Must set initial predecessor to NULL
843 paths = Lst_Init(FALSE);
845 (void)Lst_AtEnd(paths, (void *)dirSearchPath);
848 if (!Lst_IsEmpty(create)) {
855 gn = Targ_FindNode(line, TARG_CREATE);
856 gn->type |= OP_NOTMAIN;
857 (void)Lst_AtEnd(targets, (void *)gn);
860 gn = Targ_NewGN(".DEFAULT");
861 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
862 (void)Lst_AtEnd(targets, (void *)gn);
879 } else if (strncmp (line, ".PATH", 5) == 0) {
881 * .PATH<suffix> has to be handled specially.
882 * Call on the suffix module to give us a path to
888 path = Suff_GetPath (&line[5]);
890 Parse_Error (PARSE_FATAL,
891 "Suffix '%s' not defined (yet)",
895 if (paths == (Lst)NULL) {
896 paths = Lst_Init(FALSE);
898 (void)Lst_AtEnd(paths, (void *)path);
904 * Have word in line. Get or create its node and stick it at
905 * the end of the targets list
907 if ((specType == Not) && (*line != '\0')) {
908 if (Dir_HasWildcards(line)) {
910 * Targets are to be sought only in the current directory,
911 * so create an empty path for the thing. Note we need to
912 * use Dir_Destroy in the destruction of the path as the
913 * Dir module could have added a directory to the path...
915 Lst emptyPath = Lst_Init(FALSE);
917 Dir_Expand(line, emptyPath, curTargs);
919 Lst_Destroy(emptyPath, Dir_Destroy);
922 * No wildcards, but we want to avoid code duplication,
923 * so create a list with the word on it.
925 (void)Lst_AtEnd(curTargs, (void *)line);
928 while(!Lst_IsEmpty(curTargs)) {
929 char *targName = (char *)Lst_DeQueue(curTargs);
931 if (!Suff_IsTransform (targName)) {
932 gn = Targ_FindNode (targName, TARG_CREATE);
934 gn = Suff_AddTransform (targName);
937 (void)Lst_AtEnd (targets, (void *)gn);
939 } else if (specType == ExPath && *line != '.' && *line != '\0') {
940 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
945 * If it is a special type and not .PATH, it's the only target we
946 * allow on this line...
948 if (specType != Not && specType != ExPath) {
949 Boolean warn = FALSE;
951 while ((*cp != '!') && (*cp != ':') && *cp) {
952 if (*cp != ' ' && *cp != '\t') {
958 Parse_Error(PARSE_WARNING, "Extra target ignored");
961 while (*cp && isspace ((unsigned char) *cp)) {
966 } while ((*line != '!') && (*line != ':') && *line);
969 * Don't need the list of target names anymore...
971 Lst_Destroy(curTargs, NOFREE);
973 if (!Lst_IsEmpty(targets)) {
976 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
983 * These four create nodes on which to hang commands, so
984 * targets shouldn't be empty...
988 * Nothing special here -- targets can be empty if it wants.
995 * Have now parsed all the target names. Must parse the operator next. The
996 * result is left in op .
1000 } else if (*cp == ':') {
1008 Parse_Error (PARSE_FATAL, "Missing dependency operator");
1012 cp++; /* Advance beyond operator */
1014 Lst_ForEach (targets, ParseDoOp, (void *)&op);
1017 * Get to the first source
1019 while (*cp && isspace ((unsigned char) *cp)) {
1025 * Several special targets take different actions if present with no
1027 * a .SUFFIXES line with no sources clears out all old suffixes
1028 * a .PRECIOUS line makes all targets precious
1029 * a .IGNORE line ignores errors for all targets
1030 * a .SILENT line creates silence when making all targets
1031 * a .PATH removes all directories from the search path(s).
1036 Suff_ClearSuffixes ();
1042 ignoreErrors = TRUE;
1048 Lst_ForEach(paths, ParseClearPath, (void *)NULL);
1052 Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1058 } else if (specType == MFlags) {
1060 * Call on functions in main.c to deal with these arguments and
1061 * set the initial character to a null-character so the loop to
1062 * get sources won't get anything
1064 Main_ParseArgLine (line);
1066 } else if (specType == ExShell) {
1067 if (Job_ParseShell (line) != SUCCESS) {
1068 Parse_Error (PARSE_FATAL, "improper shell specification");
1072 } else if ((specType == NotParallel) || (specType == SingleShell)) {
1077 * NOW GO FOR THE SOURCES
1079 if ((specType == Suffixes) || (specType == ExPath) ||
1080 (specType == Includes) || (specType == Libs) ||
1085 * If the target was one that doesn't take files as its sources
1086 * but takes something like suffixes, we take each
1087 * space-separated word on the line as a something and deal
1088 * with it accordingly.
1090 * If the target was .SUFFIXES, we take each source as a
1091 * suffix and add it to the list of suffixes maintained by the
1094 * If the target was a .PATH, we add the source as a directory
1095 * to search on the search path.
1097 * If it was .INCLUDES, the source is taken to be the suffix of
1098 * files which will be #included and whose search path should
1099 * be present in the .INCLUDES variable.
1101 * If it was .LIBS, the source is taken to be the suffix of
1102 * files which are considered libraries and whose search path
1103 * should be present in the .LIBS variable.
1105 * If it was .NULL, the source is the suffix to use when a file
1106 * has no valid suffix.
1109 while (*cp && !isspace ((unsigned char) *cp)) {
1116 Suff_AddSuffix (line);
1119 Lst_ForEach(paths, ParseAddDir, (void *)line);
1122 Suff_AddInclude (line);
1128 Suff_SetNull (line);
1134 if (savech != '\0') {
1137 while (*cp && isspace ((unsigned char) *cp)) {
1143 Lst_Destroy(paths, NOFREE);
1148 * The targets take real sources, so we must beware of archive
1149 * specifications (i.e. things with left parentheses in them)
1150 * and handle them accordingly.
1152 while (*cp && !isspace ((unsigned char) *cp)) {
1153 if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) {
1155 * Only stop for a left parenthesis if it isn't at the
1156 * start of a word (that'll be for variable changes
1157 * later) and isn't preceded by a dollar sign (a dynamic
1169 sources = Lst_Init (FALSE);
1170 if (Arch_ParseArchive (&line, sources, VAR_CMD) != SUCCESS) {
1171 Parse_Error (PARSE_FATAL,
1172 "Error in source archive spec \"%s\"", line);
1176 while (!Lst_IsEmpty (sources)) {
1177 gnp = (GNode *) Lst_DeQueue (sources);
1178 ParseDoSrc (tOp, gnp->name, curSrcs);
1180 Lst_Destroy (sources, NOFREE);
1188 ParseDoSrc (tOp, line, curSrcs);
1190 while (*cp && isspace ((unsigned char) *cp)) {
1197 if (mainNode == NULL) {
1199 * If we have yet to decide on a main target to make, in the
1200 * absence of any user input, we want the first target on
1201 * the first dependency line that is actually a real target
1202 * (i.e. isn't a .USE or .EXEC rule) to be made.
1204 Lst_ForEach (targets, ParseFindMain, (void *)0);
1208 * Finally, destroy the list of sources
1210 Lst_Destroy(curSrcs, NOFREE);
1214 *---------------------------------------------------------------------
1216 * Return TRUE if the passed line is a variable assignment. A variable
1217 * assignment consists of a single word followed by optional whitespace
1218 * followed by either a += or an = operator.
1219 * This function is used both by the Parse_File function and main when
1220 * parsing the command-line arguments.
1223 * TRUE if it is. FALSE if it ain't
1227 *---------------------------------------------------------------------
1230 Parse_IsVar (char *line)
1232 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1233 Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1235 #define ISEQOPERATOR(c) \
1236 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1239 * Skip to variable name
1241 for (;(*line == ' ') || (*line == '\t'); line++)
1244 for (; *line != '=' || level != 0; line++)
1248 * end-of-line -- can't be a variable assignment.
1255 * there can be as much white space as desired so long as there is
1256 * only one word before the operator
1272 if (wasSpace && haveName) {
1273 if (ISEQOPERATOR(*line)) {
1275 * We must have a finished word
1281 * When an = operator [+?!:] is found, the next
1282 * character must be an = or it ain't a valid
1289 * This is a shell command
1291 if (strncmp(line, ":sh", 3) == 0)
1296 * This is the start of another word, so not assignment.
1311 *---------------------------------------------------------------------
1313 * Take the variable assignment in the passed line and do it in the
1316 * Note: There is a lexical ambiguity with assignment modifier characters
1317 * in variable names. This routine interprets the character before the =
1318 * as a modifier. Therefore, an assignment like
1320 * is interpreted as "C+ +=" instead of "C++ =".
1326 * the variable structure of the given variable name is altered in the
1328 *---------------------------------------------------------------------
1331 Parse_DoVar (char *line, GNode *ctxt)
1333 char *cp; /* pointer into line */
1335 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1336 } type; /* Type of assignment */
1337 char *opc; /* ptr to operator character to
1338 * null-terminate the variable name */
1340 * Avoid clobbered variable warnings by forcing the compiler
1341 * to ``unregister'' variables
1349 * Skip to variable name
1351 while ((*line == ' ') || (*line == '\t')) {
1356 * Skip to operator character, nulling out whitespace as we go
1358 for (cp = line + 1; *cp != '='; cp++) {
1359 if (isspace ((unsigned char) *cp)) {
1363 opc = cp-1; /* operator is the previous character */
1364 *cp++ = '\0'; /* nuke the = */
1367 * Check operator type
1377 * If the variable already has a value, we don't do anything.
1380 if (Var_Exists(line, ctxt)) {
1405 if (strncmp(opc, ":sh", 3) == 0) {
1415 while (isspace ((unsigned char) *cp)) {
1419 if (type == VAR_APPEND) {
1420 Var_Append (line, cp, ctxt);
1421 } else if (type == VAR_SUBST) {
1423 * Allow variables in the old value to be undefined, but leave their
1424 * invocation alone -- this is done by forcing oldVars to be false.
1425 * XXX: This can cause recursive variables, but that's not hard to do,
1426 * and this allows someone to do something like
1428 * CFLAGS = $(.INCLUDES)
1429 * CFLAGS := -I.. $(CFLAGS)
1431 * And not get an error.
1433 Boolean oldOldVars = oldVars;
1438 * make sure that we set the variable the first time to nothing
1439 * so that it gets substituted!
1441 if (!Var_Exists(line, ctxt))
1442 Var_Set(line, "", ctxt);
1444 cp = Var_Subst(NULL, cp, ctxt, FALSE);
1445 oldVars = oldOldVars;
1447 Var_Set(line, cp, ctxt);
1449 } else if (type == VAR_SHELL) {
1450 Boolean freeCmd = FALSE; /* TRUE if the command needs to be freed, i.e.
1451 * if any variable expansion was performed */
1454 if (strchr(cp, '$') != NULL) {
1456 * There's a dollar sign in the command, so perform variable
1457 * expansion on the whole thing. The resulting string will need
1458 * freeing when we're done, so set freeCmd to TRUE.
1460 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1464 res = Cmd_Exec(cp, &error);
1465 Var_Set(line, res, ctxt);
1469 Parse_Error(PARSE_WARNING, error, cp);
1475 * Normal assignment -- just do it.
1477 Var_Set(line, cp, ctxt);
1484 * Lst_ForEach function to add a command line to all targets
1490 * A new element is added to the commands list of the node.
1493 ParseAddCmd(void *gnp, void *cmd)
1495 GNode *gn = (GNode *) gnp;
1496 /* if target already supplied, ignore commands */
1497 if (!(gn->type & OP_HAS_COMMANDS))
1498 (void)Lst_AtEnd(gn->commands, cmd);
1500 Parse_Error(PARSE_WARNING,
1501 "duplicate script for target \"%s\" ignored",
1507 *-----------------------------------------------------------------------
1508 * ParseHasCommands --
1509 * Callback procedure for Parse_File when destroying the list of
1510 * targets on the last dependency line. Marks a target as already
1511 * having commands if it does, to keep from having shell commands
1512 * on multiple dependency lines.
1518 * OP_HAS_COMMANDS may be set for the target.
1520 *-----------------------------------------------------------------------
1523 ParseHasCommands(void *gnp)
1525 GNode *gn = (GNode *) gnp;
1526 if (!Lst_IsEmpty(gn->commands)) {
1527 gn->type |= OP_HAS_COMMANDS;
1532 *-----------------------------------------------------------------------
1533 * Parse_AddIncludeDir --
1534 * Add a directory to the path searched for included makefiles
1535 * bracketed by double-quotes. Used by functions in main.c
1541 * The directory is appended to the list.
1543 *-----------------------------------------------------------------------
1546 Parse_AddIncludeDir (char *dir)
1548 Dir_AddDir (parseIncPath, dir);
1551 /*---------------------------------------------------------------------
1553 * Handle error directive
1555 * The input is the line minus the ".error". We substitute variables,
1556 * print the message and exit(1) or just print a warning if the ".error"
1557 * directive is malformed.
1559 *---------------------------------------------------------------------
1562 ParseDoError(char *errmsg)
1564 if (!isspace((unsigned char) *errmsg)) {
1565 Parse_Error(PARSE_WARNING, "invalid syntax: .error%s", errmsg);
1569 while (isspace((unsigned char) *errmsg))
1572 errmsg = Var_Subst(NULL, errmsg, VAR_GLOBAL, FALSE);
1574 Parse_Error(PARSE_FATAL, "%s", errmsg);
1575 /* Terminate immediately. */
1579 /*---------------------------------------------------------------------
1581 * Handle warning directive
1583 * The input is the line minus the ".warning". We substitute variables
1584 * and print the message or just print a warning if the ".warning"
1585 * directive is malformed.
1587 *---------------------------------------------------------------------
1590 ParseDoWarning(char *warnmsg)
1592 if (!isspace((unsigned char) *warnmsg)) {
1593 Parse_Error(PARSE_WARNING, "invalid syntax: .warning%s",
1598 while (isspace((unsigned char) *warnmsg))
1601 warnmsg = Var_Subst(NULL, warnmsg, VAR_GLOBAL, FALSE);
1603 Parse_Error(PARSE_WARNING, "%s", warnmsg);
1607 *---------------------------------------------------------------------
1609 * Push to another file.
1611 * The input is the line minus the #include. A file spec is a string
1612 * enclosed in <> or "". The former is looked for only in sysIncPath.
1613 * The latter in . and the directories specified by -I command line
1620 * A structure is added to the includes Lst and readProc, curFile.lineno,
1621 * curFile.fname and curFile.F are altered for the new file
1622 *---------------------------------------------------------------------
1625 ParseDoInclude (char *file)
1627 char *fullname; /* full pathname of file */
1628 IFile *oldFile; /* state associated with current file */
1629 char endc; /* the character which ends the file spec */
1630 char *cp; /* current position in file spec */
1631 Boolean isSystem; /* TRUE if makefile is a system makefile */
1634 * Skip to delimiter character so we know where to look
1636 while ((*file == ' ') || (*file == '\t')) {
1640 if ((*file != '"') && (*file != '<')) {
1641 Parse_Error (PARSE_FATAL,
1642 ".include filename must be delimited by '\"' or '<'");
1647 * Set the search path on which to find the include file based on the
1648 * characters which bracket its name. Angle-brackets imply it's
1649 * a system Makefile while double-quotes imply it's a user makefile
1660 * Skip to matching delimiter
1662 for (cp = ++file; *cp && *cp != endc; cp++) {
1667 Parse_Error (PARSE_FATAL,
1668 "Unclosed %cinclude filename. '%c' expected",
1675 * Substitute for any variables in the file name before trying to
1678 file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1681 * Now we know the file's name and its search path, we attempt to
1682 * find the durn thing. A return of NULL indicates the file don't
1687 * Include files contained in double-quotes are first searched for
1688 * relative to the including file's location. We don't want to
1689 * cd there, of course, so we just tack on the old file's
1690 * leading path components and call Dir_FindFile to see if
1691 * we can locate the beast.
1693 char *prefEnd, *Fname;
1695 /* Make a temporary copy of this, to be safe. */
1696 Fname = estrdup(curFile.fname);
1698 prefEnd = strrchr (Fname, '/');
1699 if (prefEnd != (char *)NULL) {
1704 newName = estrdup(file);
1706 newName = str_concat (Fname, file, STR_ADDSLASH);
1707 fullname = Dir_FindFile (newName, parseIncPath);
1708 if (fullname == (char *)NULL) {
1709 fullname = Dir_FindFile(newName, dirSearchPath);
1714 fullname = (char *)NULL;
1718 fullname = (char *)NULL;
1721 if (fullname == (char *)NULL) {
1723 * System makefile or makefile wasn't found in same directory as
1724 * included makefile. Search for it first on the -I search path,
1725 * then on the .PATH search path, if not found in a -I directory.
1726 * XXX: Suffix specific?
1728 fullname = Dir_FindFile (file, parseIncPath);
1729 if (fullname == (char *)NULL) {
1730 fullname = Dir_FindFile(file, dirSearchPath);
1734 if (fullname == (char *)NULL) {
1736 * Still haven't found the makefile. Look for it on the system
1737 * path as a last resort.
1739 fullname = Dir_FindFile(file, sysIncPath);
1742 if (fullname == (char *) NULL) {
1744 Parse_Error (PARSE_FATAL, "Could not find %s", file);
1751 * Once we find the absolute path to the file, we get to save all the
1752 * state from the current file before we can start reading this
1753 * include file. The state is stored in an IFile structure which
1754 * is placed on a list with other IFile structures. The list makes
1755 * a very nice stack to track how we got here...
1757 oldFile = (IFile *) emalloc (sizeof (IFile));
1758 memcpy(oldFile, &curFile, sizeof (IFile));
1760 (void) Lst_AtFront (includes, (void *)oldFile);
1763 * Once the previous state has been saved, we can get down to reading
1764 * the new file. We set up the name of the file to be the absolute
1765 * name of the include file so error messages refer to the right
1766 * place. Naturally enough, we start reading at line number 0.
1768 curFile.fname = fullname;
1771 curFile.F = fopen (fullname, "r");
1773 if (curFile.F == (FILE * ) NULL) {
1774 Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1776 * Pop to previous file
1780 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
1787 *---------------------------------------------------------------------
1788 * Parse_FromString --
1789 * Start Parsing from the given string
1795 * A structure is added to the includes Lst and readProc, curFile.lineno,
1796 * curFile.fname and curFile.F are altered for the new file
1797 *---------------------------------------------------------------------
1800 Parse_FromString(char *str, int lineno)
1802 IFile *oldFile; /* state associated with this file */
1804 DEBUGF(FOR, ("%s\n---- at line %d\n", str, lineno));
1806 oldFile = (IFile *) emalloc (sizeof (IFile));
1807 memcpy(oldFile, &curFile, sizeof (IFile));
1809 (void) Lst_AtFront (includes, (void *)oldFile);
1812 curFile.p = (PTR *) emalloc (sizeof (PTR));
1813 curFile.p->str = curFile.p->ptr = str;
1814 curFile.lineno = lineno;
1815 curFile.fname = estrdup(curFile.fname);
1821 *---------------------------------------------------------------------
1822 * ParseTraditionalInclude --
1823 * Push to another file.
1825 * The input is the line minus the "include". The file name is
1826 * the string following the "include".
1832 * A structure is added to the includes Lst and readProc, curFile.lineno,
1833 * curFile.fname and curFile.F are altered for the new file
1834 *---------------------------------------------------------------------
1837 ParseTraditionalInclude (char *file)
1839 char *fullname; /* full pathname of file */
1840 IFile *oldFile; /* state associated with current file */
1841 char *cp; /* current position in file spec */
1844 * Skip over whitespace
1846 while ((*file == ' ') || (*file == '\t')) {
1850 if (*file == '\0') {
1851 Parse_Error (PARSE_FATAL,
1852 "Filename missing from \"include\"");
1857 * Skip to end of line or next whitespace
1859 for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1866 * Substitute for any variables in the file name before trying to
1869 file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1872 * Now we know the file's name, we attempt to find the durn thing.
1873 * Search for it first on the -I search path, then on the .PATH
1874 * search path, if not found in a -I directory.
1876 fullname = Dir_FindFile (file, parseIncPath);
1877 if (fullname == (char *)NULL) {
1878 fullname = Dir_FindFile(file, dirSearchPath);
1881 if (fullname == (char *)NULL) {
1883 * Still haven't found the makefile. Look for it on the system
1884 * path as a last resort.
1886 fullname = Dir_FindFile(file, sysIncPath);
1889 if (fullname == (char *) NULL) {
1890 Parse_Error (PARSE_FATAL, "Could not find %s", file);
1895 * Once we find the absolute path to the file, we get to save all the
1896 * state from the current file before we can start reading this
1897 * include file. The state is stored in an IFile structure which
1898 * is placed on a list with other IFile structures. The list makes
1899 * a very nice stack to track how we got here...
1901 oldFile = (IFile *) emalloc (sizeof (IFile));
1902 memcpy(oldFile, &curFile, sizeof (IFile));
1904 (void) Lst_AtFront (includes, (void *)oldFile);
1907 * Once the previous state has been saved, we can get down to reading
1908 * the new file. We set up the name of the file to be the absolute
1909 * name of the include file so error messages refer to the right
1910 * place. Naturally enough, we start reading at line number 0.
1912 curFile.fname = fullname;
1915 curFile.F = fopen (fullname, "r");
1917 if (curFile.F == (FILE * ) NULL) {
1918 Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1920 * Pop to previous file
1924 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
1930 *---------------------------------------------------------------------
1932 * Called when EOF is reached in the current file. If we were reading
1933 * an include file, the includes stack is popped and things set up
1934 * to go back to reading the previous file at the previous location.
1937 * CONTINUE if there's more to do. DONE if not.
1940 * The old curFile.F is closed. The includes list is shortened.
1941 * curFile.lineno, curFile.F, and curFile.fname are changed if
1942 * CONTINUE is returned.
1943 *---------------------------------------------------------------------
1946 ParseEOF (int opened)
1948 IFile *ifile; /* the state on the top of the includes stack */
1950 if (Lst_IsEmpty (includes)) {
1951 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
1955 ifile = (IFile *) Lst_DeQueue (includes);
1956 free (curFile.fname);
1957 if (opened && curFile.F) {
1958 (void) fclose (curFile.F);
1959 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
1962 free(curFile.p->str);
1965 memcpy(&curFile, ifile, sizeof (IFile));
1971 *---------------------------------------------------------------------
1973 * Read a character from the current file
1976 * The character that was read
1979 *---------------------------------------------------------------------
1985 return fgetc(curFile.F);
1987 if (curFile.p && *curFile.p->ptr)
1988 return *curFile.p->ptr++;
1994 *---------------------------------------------------------------------
1996 * Put back a character to the current file
2002 *---------------------------------------------------------------------
2008 ungetc(c, curFile.F);
2012 *--(curFile.p->ptr) = c;
2019 * Grab the next line unless it begins with a dot (`.') and we're told to
2020 * ignore such lines.
2023 ParseSkipLine(int skip, int keep_newline)
2026 int c, lastc, lineLength = 0;
2029 buf = Buf_Init(MAKE_BSIZE);
2032 Buf_Discard(buf, lineLength);
2035 while (((c = ParseReadc()) != '\n' || lastc == '\\')
2039 Buf_AddByte(buf, (Byte)c);
2041 Buf_ReplaceLastByte(buf, (Byte)' ');
2044 while ((c = ParseReadc()) == ' ' || c == '\t')
2051 Buf_AddByte(buf, (Byte)c);
2056 Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
2057 Buf_Destroy(buf, TRUE);
2058 return((char *)NULL);
2062 Buf_AddByte(buf, (Byte)'\0');
2063 line = (char *)Buf_GetAll(buf, &lineLength);
2064 } while (skip == 1 && line[0] != '.');
2066 Buf_Destroy(buf, FALSE);
2072 *---------------------------------------------------------------------
2074 * Read an entire line from the input file. Called only by Parse_File.
2075 * To facilitate escaped newlines and what have you, a character is
2076 * buffered in 'lastc', which is '\0' when no characters have been
2077 * read. When we break out of the loop, c holds the terminating
2078 * character and lastc holds a character that should be added to
2079 * the line (unless we don't read anything but a terminator).
2082 * A line w/o its newline
2085 * Only those associated with reading a character
2086 *---------------------------------------------------------------------
2089 ParseReadLine (void)
2091 Buffer buf; /* Buffer for current line */
2092 int c; /* the current character */
2093 int lastc; /* The most-recent character */
2094 Boolean semiNL; /* treat semi-colons as newlines */
2095 Boolean ignDepOp; /* TRUE if should ignore dependency operators
2096 * for the purposes of setting semiNL */
2097 Boolean ignComment; /* TRUE if should ignore comments (in a
2099 char *line; /* Result */
2100 char *ep; /* to strip trailing blanks */
2101 int lineLength; /* Length of result */
2102 int lineno; /* Saved line # */
2109 * Handle special-characters at the beginning of the line. Either a
2110 * leading tab (shell command) or pound-sign (possible conditional)
2111 * forces us to ignore comments and dependency operators and treat
2112 * semi-colons as semi-colons (by leaving semiNL FALSE). This also
2113 * discards completely blank lines.
2119 ignComment = ignDepOp = TRUE;
2121 } else if (c == '\n') {
2123 } else if (c == '#') {
2128 * Anything else breaks out without doing anything
2136 buf = Buf_Init(MAKE_BSIZE);
2138 while (((c = ParseReadc ()) != '\n' || (lastc == '\\')) &&
2145 * Escaped newline: read characters until a non-space or an
2146 * unescaped newline and replace them all by a single space.
2147 * This is done by storing the space over the backslash and
2148 * dropping through with the next nonspace. If it is a
2149 * semi-colon and semiNL is TRUE, it will be recognized as a
2150 * newline in the code below this...
2154 while ((c = ParseReadc ()) == ' ' || c == '\t') {
2157 if (c == EOF || c == '\n') {
2161 * Check for comments, semiNL's, etc. -- easier than
2162 * ParseUnreadc(c); continue;
2171 * Semi-colon: Need to see if it should be interpreted as a
2176 * To make sure the command that may be following this
2177 * semi-colon begins with a tab, we push one back into the
2178 * input stream. This will overwrite the semi-colon in the
2179 * buffer. If there is no command following, this does no
2180 * harm, since the newline remains in the buffer and the
2181 * whole line is ignored.
2190 * Haven't seen a dependency operator before this, so this
2191 * must be a variable assignment -- don't pay attention to
2192 * dependency operators after this.
2195 } else if (lastc == ':' || lastc == '!') {
2197 * Well, we've seen a dependency operator already, but it
2198 * was the previous character, so this is really just an
2199 * expanded variable assignment. Revert semi-colons to
2200 * being just semi-colons again and ignore any more
2201 * dependency operators.
2203 * XXX: Note that a line like "foo : a:=b" will blow up,
2204 * but who'd write a line like that anyway?
2206 ignDepOp = TRUE; semiNL = FALSE;
2211 if (lastc != '\\') {
2213 * If the character is a hash mark and it isn't escaped
2214 * (or we're being compatible), the thing is a comment.
2215 * Skip to the end of the line.
2219 } while ((c != '\n') && (c != EOF));
2223 * Don't add the backslash. Just let the # get copied
2233 if (!ignDepOp && (c == ':' || c == '!')) {
2235 * A semi-colon is recognized as a newline only on
2236 * dependency lines. Dependency lines are lines with a
2237 * colon or an exclamation point. Ergo...
2246 * Copy in the previous character and save this one in lastc.
2248 Buf_AddByte (buf, (Byte)lastc);
2255 if (lastc != '\0') {
2256 Buf_AddByte (buf, (Byte)lastc);
2258 Buf_AddByte (buf, (Byte)'\0');
2259 line = (char *)Buf_GetAll (buf, &lineLength);
2260 Buf_Destroy (buf, FALSE);
2263 * Strip trailing blanks and tabs from the line.
2264 * Do not strip a blank or tab that is preceded by
2270 while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
2271 if (ep > line + 1 && ep[-2] == '\\')
2277 if (line[0] == '.') {
2279 * The line might be a conditional. Ask the conditional module
2280 * about it and act accordingly
2282 switch (Cond_Eval (line)) {
2285 * Skip to next conditional that evaluates to COND_PARSE.
2289 line = ParseSkipLine(1, 0);
2290 } while (line && Cond_Eval(line) != COND_PARSE);
2296 line = ParseReadLine();
2299 if (For_Eval(line)) {
2302 lineno = curFile.lineno;
2305 * Skip after the matching end
2307 line = ParseSkipLine(0, 1);
2309 Parse_Error (PARSE_FATAL,
2310 "Unexpected end of file in for loop.\n");
2313 ok = For_Eval(line);
2319 line = ParseReadLine();
2330 * Hit end-of-file, so return a NULL line to indicate this.
2332 return((char *)NULL);
2337 *-----------------------------------------------------------------------
2338 * ParseFinishLine --
2339 * Handle the end of a dependency group.
2345 * inLine set FALSE. 'targets' list destroyed.
2347 *-----------------------------------------------------------------------
2350 ParseFinishLine(void)
2353 Lst_ForEach(targets, Suff_EndTransform, (void *)NULL);
2354 Lst_Destroy (targets, ParseHasCommands);
2361 stripvarname(char *cp)
2365 while (isspace((unsigned char)*cp))
2368 while (*cp2 && !isspace((unsigned char)*cp2))
2376 *---------------------------------------------------------------------
2378 * Parse a file into its component parts, incorporating it into the
2379 * current dependency graph. This is the main function and controls
2380 * almost every other function in this module
2386 * Loads. Nodes are added to the list of all targets, nodes and links
2387 * are added to the dependency graph. etc. etc. etc.
2388 *---------------------------------------------------------------------
2391 Parse_File(char *name, FILE *stream)
2393 char *cp, /* pointer into the line */
2394 *line; /* the line we're working on */
2397 curFile.fname = name;
2402 Var_Append(".MAKEFILE_LIST", name, VAR_GLOBAL);
2405 while ((line = ParseReadLine ()) != NULL) {
2408 * Lines that begin with the special character are either
2409 * include or undef directives.
2411 for (cp = line + 1; isspace ((unsigned char) *cp); cp++) {
2414 if (strncmp (cp, "include", 7) == 0) {
2415 ParseDoInclude (cp + 7);
2417 } else if (strncmp (cp, "error", 5) == 0) {
2418 ParseDoError(cp + 5);
2420 } else if (strncmp (cp, "warning", 7) == 0) {
2421 ParseDoWarning(cp + 7);
2423 } else if (strncmp(cp, "undef", 5) == 0) {
2424 cp = stripvarname(cp + 5);
2425 cp = Var_Subst(NULL, cp, VAR_CMD, FALSE);
2426 Var_Delete(cp, VAR_GLOBAL);
2428 } else if (strncmp(cp, "makeenv", 7) == 0) {
2429 cp = stripvarname(cp + 7);
2430 Var_SetEnv(cp, VAR_GLOBAL);
2435 /* If we're this far, the line must be a comment. */
2439 if (*line == '\t') {
2441 * If a line starts with a tab, it can only hope to be
2442 * a creation command.
2447 for (cp = line + 1; isspace ((unsigned char) *cp); cp++) {
2453 * So long as it's not a blank line and we're actually
2454 * in a dependency spec, add the command to the list of
2455 * commands of all targets in the dependency spec
2457 Lst_ForEach (targets, ParseAddCmd, cp);
2458 Lst_AtEnd(targCmds, (void *) line);
2461 Parse_Error (PARSE_FATAL,
2462 "Unassociated shell command \"%s\"",
2467 } else if (strncmp (line, "include", 7) == 0 &&
2468 isspace((unsigned char) line[7]) &&
2469 strchr(line, ':') == NULL) {
2471 * It's an S3/S5-style "include".
2473 ParseTraditionalInclude (line + 7);
2476 } else if (Parse_IsVar (line)) {
2478 Parse_DoVar (line, VAR_GLOBAL);
2481 * We now know it's a dependency line so it needs to have all
2482 * variables expanded before being parsed. Tell the variable
2483 * module to complain if some variable is undefined...
2484 * To make life easier on novices, if the line is indented we
2485 * first make sure the line has a dependency operator in it.
2486 * If it doesn't have an operator and we're in a dependency
2487 * line's script, we assume it's actually a shell command
2488 * and add it to the current list of targets.
2491 Boolean nonSpace = FALSE;
2495 if (isspace((unsigned char) line[0])) {
2496 while ((*cp != '\0') && isspace((unsigned char) *cp)) {
2503 while ((*cp != ':') && (*cp != '!') && (*cp != '\0')) {
2513 Parse_Error (PARSE_WARNING,
2514 "Shell command needs a leading tab");
2516 } else if (nonSpace) {
2517 Parse_Error (PARSE_FATAL, "Missing operator");
2523 cp = Var_Subst (NULL, line, VAR_CMD, TRUE);
2528 * Need a non-circular list for the target nodes
2531 Lst_Destroy(targets, NOFREE);
2533 targets = Lst_Init (FALSE);
2536 ParseDoDependency (line);
2547 * Reached EOF, but it may be just EOF of an include file...
2549 } while (ParseEOF(1) == CONTINUE);
2552 * Make sure conditionals are clean
2557 errx(1, "fatal errors encountered -- cannot continue");
2561 *---------------------------------------------------------------------
2563 * initialize the parsing module
2569 * the parseIncPath list is initialized...
2570 *---------------------------------------------------------------------
2576 parseIncPath = Lst_Init (FALSE);
2577 sysIncPath = Lst_Init (FALSE);
2578 includes = Lst_Init (FALSE);
2579 targCmds = Lst_Init (FALSE);
2585 Lst_Destroy(targCmds, (void (*) (void *)) free);
2587 Lst_Destroy(targets, NOFREE);
2588 Lst_Destroy(sysIncPath, Dir_Destroy);
2589 Lst_Destroy(parseIncPath, Dir_Destroy);
2590 Lst_Destroy(includes, NOFREE); /* Should be empty now */
2595 *-----------------------------------------------------------------------
2597 * Return a Lst of the main target to create for main()'s sake. If
2598 * no such target exists, we Punt with an obnoxious error message.
2601 * A Lst of the single node to create.
2606 *-----------------------------------------------------------------------
2609 Parse_MainName(void)
2611 Lst listmain; /* result list */
2613 listmain = Lst_Init (FALSE);
2615 if (mainNode == NULL) {
2616 Punt ("no target to make.");
2618 } else if (mainNode->type & OP_DOUBLEDEP) {
2619 (void) Lst_AtEnd (listmain, (void *)mainNode);
2620 Lst_Concat(listmain, mainNode->cohorts, LST_CONCNEW);
2623 (void) Lst_AtEnd (listmain, (void *)mainNode);