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.11 2004/11/12 22:57:04 dillon 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 'fname' and 'lineno' are used to track the name
58 * of the current file and the line number in that file so that error
59 * 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 char *fname; /* name of current file (for errors) */
113 static int lineno; /* line number in current file */
114 static FILE *curFILE = NULL; /* current makefile */
116 static PTR *curPTR = NULL; /* current makefile */
118 static int fatals = 0;
120 static GNode *mainNode; /* The main target to create. This is the
121 * first target on the first dependency
122 * line in the first makefile */
124 * Definitions for handling #include specifications
126 typedef struct IFile {
127 char *fname; /* name of previous file */
128 int lineno; /* saved line number */
129 FILE * F; /* the open stream */
130 PTR * p; /* the char pointer */
133 static Lst includes; /* stack of IFiles generated by
135 Lst parseIncPath; /* list of directories for "..." includes */
136 Lst sysIncPath; /* list of directories for <...> includes */
139 * specType contains the SPECial TYPE of the current target. It is
140 * Not if the target is unspecial. If it *is* special, however, the children
141 * are linked as children of the parent but not vice versa. This variable is
142 * set in ParseDoDependency
146 Default, /* .DEFAULT */
148 Ignore, /* .IGNORE */
149 Includes, /* .INCLUDES */
150 Interrupt, /* .INTERRUPT */
152 MFlags, /* .MFLAGS or .MAKEFLAGS */
153 Main, /* .MAIN and we don't have anything user-specified to
155 NoExport, /* .NOEXPORT */
156 Not, /* Not special */
157 NotParallel, /* .NOTPARALELL */
160 Parallel, /* .PARALLEL */
166 Precious, /* .PRECIOUS */
167 ExShell, /* .SHELL */
168 Silent, /* .SILENT */
169 SingleShell, /* .SINGLESHELL */
170 Suffixes, /* .SUFFIXES */
172 Attribute /* Generic attribute */
175 static ParseSpecial specType;
179 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
180 * seen, then set to each successive source on the line.
182 static GNode *predecessor;
185 * The parseKeywords table is searched using binary search when deciding
186 * if a target or source is special. The 'spec' field is the ParseSpecial
187 * type of the keyword ("Not" if the keyword isn't special as a target) while
188 * the 'op' field is the operator to apply to the list of targets if the
189 * keyword is used as a source ("0" if the keyword isn't special as a source)
192 char *name; /* Name of keyword */
193 ParseSpecial spec; /* Type when used as a target */
194 int op; /* Operator when used as a source */
195 } parseKeywords[] = {
196 { ".BEGIN", Begin, 0 },
197 { ".DEFAULT", Default, 0 },
199 { ".EXEC", Attribute, OP_EXEC },
200 { ".IGNORE", Ignore, OP_IGNORE },
201 { ".INCLUDES", Includes, 0 },
202 { ".INTERRUPT", Interrupt, 0 },
203 { ".INVISIBLE", Attribute, OP_INVISIBLE },
204 { ".JOIN", Attribute, OP_JOIN },
205 { ".LIBS", Libs, 0 },
206 { ".MAIN", Main, 0 },
207 { ".MAKE", Attribute, OP_MAKE },
208 { ".MAKEFLAGS", MFlags, 0 },
209 { ".MFLAGS", MFlags, 0 },
210 { ".NOTMAIN", Attribute, OP_NOTMAIN },
211 { ".NOTPARALLEL", NotParallel, 0 },
212 { ".NO_PARALLEL", NotParallel, 0 },
213 { ".NULL", Null, 0 },
214 { ".OPTIONAL", Attribute, OP_OPTIONAL },
215 { ".ORDER", Order, 0 },
216 { ".PARALLEL", Parallel, 0 },
217 { ".PATH", ExPath, 0 },
218 { ".PHONY", Phony, OP_PHONY },
220 { ".POSIX", Posix, 0 },
222 { ".PRECIOUS", Precious, OP_PRECIOUS },
223 { ".RECURSIVE", Attribute, OP_MAKE },
224 { ".SHELL", ExShell, 0 },
225 { ".SILENT", Silent, OP_SILENT },
226 { ".SINGLESHELL", SingleShell, 0 },
227 { ".SUFFIXES", Suffixes, 0 },
228 { ".USE", Attribute, OP_USE },
229 { ".WAIT", Wait, 0 },
232 static int ParseFindKeyword(char *);
233 static int ParseLinkSrc(void *, void *);
234 static int ParseDoOp(void *, void *);
235 static int ParseAddDep(void *, void *);
236 static void ParseDoSrc(int, char *, Lst);
237 static int ParseFindMain(void *, void *);
238 static int ParseAddDir(void *, void *);
239 static int ParseClearPath(void *, void *);
240 static void ParseDoDependency(char *);
241 static int ParseAddCmd(void *, void *);
242 static int ParseReadc(void);
243 static void ParseUnreadc(int);
244 static void ParseHasCommands(void *);
245 static void ParseDoInclude(char *);
246 static void ParseDoError(char *);
248 static void ParseTraditionalInclude(char *);
250 static int ParseEOF(int);
251 static char *ParseReadLine(void);
252 static char *ParseSkipLine(int);
253 static void ParseFinishLine(void);
256 *----------------------------------------------------------------------
257 * ParseFindKeyword --
258 * Look in the table of keywords for one matching the given string.
261 * The index of the keyword, or -1 if it isn't there.
265 *----------------------------------------------------------------------
268 ParseFindKeyword (char *str)
276 end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
279 cur = start + ((end - start) / 2);
280 diff = strcmp (str, parseKeywords[cur].name);
284 } else if (diff < 0) {
289 } while (start <= end);
295 * Error message abort function for parsing. Prints out the context
296 * of the error (line number and file) as well as the message with
297 * two optional arguments.
303 * "fatals" is incremented if the level is PARSE_FATAL.
307 Parse_Error(int type, const char *fmt, ...)
312 (void)fprintf(stderr, "\"%s\", line %d: ", fname, lineno);
313 if (type == PARSE_WARNING)
314 (void)fprintf(stderr, "warning: ");
315 (void)vfprintf(stderr, fmt, ap);
317 (void)fprintf(stderr, "\n");
318 (void)fflush(stderr);
319 if (type == PARSE_FATAL)
324 *---------------------------------------------------------------------
326 * Link the parent node to its new child. Used in a Lst_ForEach by
327 * ParseDoDependency. If the specType isn't 'Not', the parent
328 * isn't linked as a parent of the child.
334 * New elements are added to the parents list of cgn and the
335 * children list of cgn. the unmade field of pgn is updated
336 * to reflect the additional child.
337 *---------------------------------------------------------------------
340 ParseLinkSrc (void *pgnp, void *cgnp)
342 GNode *pgn = (GNode *) pgnp;
343 GNode *cgn = (GNode *) cgnp;
344 if (Lst_Member (pgn->children, (void *)cgn) == NULL) {
345 (void)Lst_AtEnd (pgn->children, (void *)cgn);
346 if (specType == Not) {
347 (void)Lst_AtEnd (cgn->parents, (void *)pgn);
355 *---------------------------------------------------------------------
357 * Apply the parsed operator to the given target node. Used in a
358 * Lst_ForEach call by ParseDoDependency once all targets have
359 * been found and their operator parsed. If the previous and new
360 * operators are incompatible, a major error is taken.
366 * The type field of the node is altered to reflect any new bits in
368 *---------------------------------------------------------------------
371 ParseDoOp (void *gnp, void *opp)
373 GNode *gn = (GNode *) gnp;
374 int op = *(int *) opp;
376 * If the dependency mask of the operator and the node don't match and
377 * the node has actually had an operator applied to it before, and
378 * the operator actually has some dependency information in it, complain.
380 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
381 !OP_NOP(gn->type) && !OP_NOP(op))
383 Parse_Error (PARSE_FATAL, "Inconsistent operator for %s", gn->name);
387 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
389 * If the node was the object of a :: operator, we need to create a
390 * new instance of it for the children and commands on this dependency
391 * line. The new instance is placed on the 'cohorts' list of the
392 * initial one (note the initial one is not on its own cohorts list)
393 * and the new instance is linked to all parents of the initial
399 cohort = Targ_NewGN(gn->name);
401 * Duplicate links to parents so graph traversal is simple. Perhaps
402 * some type bits should be duplicated?
404 * Make the cohort invisible as well to avoid duplicating it into
405 * other variables. True, parents of this target won't tend to do
406 * anything with their local variables, but better safe than
409 Lst_ForEach(gn->parents, ParseLinkSrc, (void *)cohort);
410 cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
411 (void)Lst_AtEnd(gn->cohorts, (void *)cohort);
414 * Replace the node in the targets list with the new copy
416 ln = Lst_Member(targets, (void *)gn);
417 Lst_Replace(ln, (void *)cohort);
421 * We don't want to nuke any previous flags (whatever they were) so we
422 * just OR the new operator into the old
430 *---------------------------------------------------------------------
432 * Check if the pair of GNodes given needs to be synchronized.
433 * This has to be when two nodes are on different sides of a
437 * Returns 1 if the two targets need to be ordered, 0 otherwise.
438 * If it returns 1, the search can stop
441 * A dependency can be added between the two nodes.
443 *---------------------------------------------------------------------
446 ParseAddDep(void *pp, void *sp)
448 GNode *p = (GNode *) pp;
449 GNode *s = (GNode *) sp;
451 if (p->order < s->order) {
453 * XXX: This can cause loops, and loops can cause unmade targets,
454 * but checking is tedious, and the debugging output can show the
457 (void)Lst_AtEnd(p->successors, (void *)s);
458 (void)Lst_AtEnd(s->preds, (void *)p);
467 *---------------------------------------------------------------------
469 * Given the name of a source, figure out if it is an attribute
470 * and apply it to the targets if it is. Else decide if there is
471 * some attribute which should be applied *to* the source because
472 * of some special target and apply it if so. Otherwise, make the
473 * source be a child of the targets in the list 'targets'
479 * Operator bits may be added to the list of targets or to the source.
480 * The targets may have a new source added to their lists of children.
481 *---------------------------------------------------------------------
484 ParseDoSrc (int tOp, char *src, Lst allsrc)
488 if (*src == '.' && isupper ((unsigned char) src[1])) {
489 int keywd = ParseFindKeyword(src);
491 int op = parseKeywords[keywd].op;
493 Lst_ForEach (targets, ParseDoOp, (void *)&op);
496 if (parseKeywords[keywd].spec == Wait) {
506 * If we have noted the existence of a .MAIN, it means we need
507 * to add the sources of said target to the list of things
508 * to create. The string 'src' is likely to be free, so we
509 * must make a new copy of it. Note that this will only be
510 * invoked if the user didn't specify a target on the command
511 * line. This is to allow #ifmake's to succeed, or something...
513 (void) Lst_AtEnd (create, (void *)estrdup(src));
515 * Add the name to the .TARGETS variable as well, so the user cna
516 * employ that, if desired.
518 Var_Append(".TARGETS", src, VAR_GLOBAL);
523 * Create proper predecessor/successor links between the previous
524 * source and the current one.
526 gn = Targ_FindNode(src, TARG_CREATE);
527 if (predecessor != NULL) {
528 (void)Lst_AtEnd(predecessor->successors, (void *)gn);
529 (void)Lst_AtEnd(gn->preds, (void *)predecessor);
532 * The current source now becomes the predecessor for the next one.
539 * If the source is not an attribute, we need to find/create
540 * a node for it. After that we can apply any operator to it
541 * from a special target or link it to its parents, as
544 * In the case of a source that was the object of a :: operator,
545 * the attribute is applied to all of its instances (as kept in
546 * the 'cohorts' list of the node) or all the cohorts are linked
547 * to all the targets.
549 gn = Targ_FindNode (src, TARG_CREATE);
553 Lst_ForEach (targets, ParseLinkSrc, (void *)gn);
555 if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
559 for (ln=Lst_First(gn->cohorts); ln != NULL; ln = Lst_Succ(ln)){
560 cohort = (GNode *)Lst_Datum(ln);
564 Lst_ForEach(targets, ParseLinkSrc, (void *)cohort);
572 (void)Lst_AtEnd(allsrc, (void *)gn);
574 Lst_ForEach(allsrc, ParseAddDep, (void *)gn);
579 *-----------------------------------------------------------------------
581 * Find a real target in the list and set it to be the main one.
582 * Called by ParseDoDependency when a main target hasn't been found
586 * 0 if main not found yet, 1 if it is.
589 * mainNode is changed and Targ_SetMain is called.
591 *-----------------------------------------------------------------------
594 ParseFindMain(void *gnp, void *dummy __unused)
596 GNode *gn = (GNode *) gnp;
597 if ((gn->type & (OP_NOTMAIN|OP_USE|OP_EXEC|OP_TRANSFORM)) == 0) {
607 *-----------------------------------------------------------------------
609 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
617 *-----------------------------------------------------------------------
620 ParseAddDir(void *path, void *name)
622 Dir_AddDir((Lst) path, (char *) name);
627 *-----------------------------------------------------------------------
629 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
637 *-----------------------------------------------------------------------
640 ParseClearPath(void *path, void *dummy __unused)
642 Dir_ClearPath((Lst) path);
647 *---------------------------------------------------------------------
648 * ParseDoDependency --
649 * Parse the dependency line in line.
655 * The nodes of the sources are linked as children to the nodes of the
656 * targets. Some nodes may be created.
658 * We parse a dependency line by first extracting words from the line and
659 * finding nodes in the list of all targets with that name. This is done
660 * until a character is encountered which is an operator character. Currently
661 * these are only ! and :. At this point the operator is parsed and the
662 * pointer into the line advanced until the first source is encountered.
663 * The parsed operator is applied to each node in the 'targets' list,
664 * which is where the nodes found for the targets are kept, by means of
665 * the ParseDoOp function.
666 * The sources are read in much the same way as the targets were except
667 * that now they are expanded using the wildcarding scheme of the C-Shell
668 * and all instances of the resulting words in the list of all targets
669 * are found. Each of the resulting nodes is then linked to each of the
670 * targets as one of its children.
671 * Certain targets are handled specially. These are the ones detailed
672 * by the specType variable.
673 * The storing of transformation rules is also taken care of here.
674 * A target is recognized as a transformation rule by calling
675 * Suff_IsTransform. If it is a transformation rule, its node is gotten
676 * from the suffix module via Suff_AddTransform rather than the standard
677 * Targ_FindNode in the target module.
678 *---------------------------------------------------------------------
681 ParseDoDependency (char *line)
683 char *cp; /* our current position */
684 GNode *gn; /* a general purpose temporary node */
685 int op; /* the operator on the line */
686 char savec; /* a place to save a character */
687 Lst paths; /* List of search paths to alter when parsing
688 * a list of .PATH targets */
689 int tOp; /* operator from special target */
690 Lst sources; /* list of archive source names after
692 Lst curTargs; /* list of target names to be found and added
693 * to the targets list */
694 Lst curSrcs; /* list of sources in order */
702 curTargs = Lst_Init(FALSE);
703 curSrcs = Lst_Init(FALSE);
707 *cp && !isspace ((unsigned char) *cp) && *cp != '(';
712 * Must be a dynamic source (would have been expanded
713 * otherwise), so call the Var module to parse the puppy
714 * so we can safely advance beyond it...There should be
715 * no errors in this, as they would have been discovered
716 * in the initial Var_Subst and we wouldn't be here.
722 result=Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
728 } else if (*cp == '!' || *cp == ':') {
730 * We don't want to end a word on ':' or '!' if there is a
731 * better match later on in the string (greedy matching).
732 * This allows the user to have targets like:
735 * where "fie::fi:fo" and "foo::bar" are the targets. In
736 * real life this is used for perl5 library man pages where
737 * "::" separates an object from its class.
738 * Ie: "File::Spec::Unix". This behaviour is also consistent
739 * with other versions of make.
743 if (*cp == ':' && *p == ':')
746 /* Found the best match already. */
747 if (*p == '\0' || isspace(*p))
750 p += strcspn(p, "!:");
752 /* No better match later on... */
760 * Archives must be handled specially to make sure the OP_ARCHV
761 * flag is set in their 'type' field, for one thing, and because
762 * things like "archive(file1.o file2.o file3.o)" are permissible.
763 * Arch_ParseArchive will set 'line' to be the first non-blank
764 * after the archive-spec. It creates/finds nodes for the members
765 * and places them on the given list, returning SUCCESS if all
766 * went well and FAILURE if there was an error in the
767 * specification. On error, line should remain untouched.
769 if (Arch_ParseArchive (&line, targets, VAR_CMD) != SUCCESS) {
770 Parse_Error (PARSE_FATAL,
771 "Error in archive specification: \"%s\"", line);
781 * Ending a dependency line without an operator is a Bozo
782 * no-no. As a heuristic, this is also often triggered by
783 * undetected conflicts from cvs/rcs merges.
785 if ((strncmp(line, "<<<<<<", 6) == 0) ||
786 (strncmp(line, "======", 6) == 0) ||
787 (strncmp(line, ">>>>>>", 6) == 0))
788 Parse_Error (PARSE_FATAL,
789 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
791 Parse_Error (PARSE_FATAL, "Need an operator");
796 * Have a word in line. See if it's a special target and set
797 * specType to match it.
799 if (*line == '.' && isupper ((unsigned char) line[1])) {
801 * See if the target is a special target that must have it
802 * or its sources handled specially.
804 int keywd = ParseFindKeyword(line);
806 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
807 Parse_Error(PARSE_FATAL, "Mismatched special targets");
811 specType = parseKeywords[keywd].spec;
812 tOp = parseKeywords[keywd].op;
815 * Certain special targets have special semantics:
816 * .PATH Have to set the dirSearchPath
818 * .MAIN Its sources are only used if
819 * nothing has been specified to
821 * .DEFAULT Need to create a node to hang
822 * commands on, but we don't want
823 * it in the graph, nor do we want
824 * it to be the Main Target, so we
825 * create it, set OP_NOTMAIN and
826 * add it to the list, setting
827 * DEFAULT to the new node for
828 * later use. We claim the node is
829 * A transformation rule to make
830 * life easier later, when we'll
831 * use Make_HandleUse to actually
832 * apply the .DEFAULT commands.
833 * .PHONY The list of targets
836 * .INTERRUPT Are not to be considered the
838 * .NOTPARALLEL Make only one target at a time.
839 * .SINGLESHELL Create a shell for each command.
840 * .ORDER Must set initial predecessor to NULL
845 paths = Lst_Init(FALSE);
847 (void)Lst_AtEnd(paths, (void *)dirSearchPath);
850 if (!Lst_IsEmpty(create)) {
857 gn = Targ_FindNode(line, TARG_CREATE);
858 gn->type |= OP_NOTMAIN;
859 (void)Lst_AtEnd(targets, (void *)gn);
862 gn = Targ_NewGN(".DEFAULT");
863 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
864 (void)Lst_AtEnd(targets, (void *)gn);
881 } else if (strncmp (line, ".PATH", 5) == 0) {
883 * .PATH<suffix> has to be handled specially.
884 * Call on the suffix module to give us a path to
890 path = Suff_GetPath (&line[5]);
892 Parse_Error (PARSE_FATAL,
893 "Suffix '%s' not defined (yet)",
897 if (paths == (Lst)NULL) {
898 paths = Lst_Init(FALSE);
900 (void)Lst_AtEnd(paths, (void *)path);
906 * Have word in line. Get or create its node and stick it at
907 * the end of the targets list
909 if ((specType == Not) && (*line != '\0')) {
910 if (Dir_HasWildcards(line)) {
912 * Targets are to be sought only in the current directory,
913 * so create an empty path for the thing. Note we need to
914 * use Dir_Destroy in the destruction of the path as the
915 * Dir module could have added a directory to the path...
917 Lst emptyPath = Lst_Init(FALSE);
919 Dir_Expand(line, emptyPath, curTargs);
921 Lst_Destroy(emptyPath, Dir_Destroy);
924 * No wildcards, but we want to avoid code duplication,
925 * so create a list with the word on it.
927 (void)Lst_AtEnd(curTargs, (void *)line);
930 while(!Lst_IsEmpty(curTargs)) {
931 char *targName = (char *)Lst_DeQueue(curTargs);
933 if (!Suff_IsTransform (targName)) {
934 gn = Targ_FindNode (targName, TARG_CREATE);
936 gn = Suff_AddTransform (targName);
939 (void)Lst_AtEnd (targets, (void *)gn);
941 } else if (specType == ExPath && *line != '.' && *line != '\0') {
942 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
947 * If it is a special type and not .PATH, it's the only target we
948 * allow on this line...
950 if (specType != Not && specType != ExPath) {
951 Boolean warn = FALSE;
953 while ((*cp != '!') && (*cp != ':') && *cp) {
954 if (*cp != ' ' && *cp != '\t') {
960 Parse_Error(PARSE_WARNING, "Extra target ignored");
963 while (*cp && isspace ((unsigned char) *cp)) {
968 } while ((*line != '!') && (*line != ':') && *line);
971 * Don't need the list of target names anymore...
973 Lst_Destroy(curTargs, NOFREE);
975 if (!Lst_IsEmpty(targets)) {
978 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
985 * These four create nodes on which to hang commands, so
986 * targets shouldn't be empty...
990 * Nothing special here -- targets can be empty if it wants.
997 * Have now parsed all the target names. Must parse the operator next. The
998 * result is left in op .
1002 } else if (*cp == ':') {
1010 Parse_Error (PARSE_FATAL, "Missing dependency operator");
1014 cp++; /* Advance beyond operator */
1016 Lst_ForEach (targets, ParseDoOp, (void *)&op);
1019 * Get to the first source
1021 while (*cp && isspace ((unsigned char) *cp)) {
1027 * Several special targets take different actions if present with no
1029 * a .SUFFIXES line with no sources clears out all old suffixes
1030 * a .PRECIOUS line makes all targets precious
1031 * a .IGNORE line ignores errors for all targets
1032 * a .SILENT line creates silence when making all targets
1033 * a .PATH removes all directories from the search path(s).
1038 Suff_ClearSuffixes ();
1044 ignoreErrors = TRUE;
1050 Lst_ForEach(paths, ParseClearPath, (void *)NULL);
1054 Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1060 } else if (specType == MFlags) {
1062 * Call on functions in main.c to deal with these arguments and
1063 * set the initial character to a null-character so the loop to
1064 * get sources won't get anything
1066 Main_ParseArgLine (line);
1068 } else if (specType == ExShell) {
1069 if (Job_ParseShell (line) != SUCCESS) {
1070 Parse_Error (PARSE_FATAL, "improper shell specification");
1074 } else if ((specType == NotParallel) || (specType == SingleShell)) {
1079 * NOW GO FOR THE SOURCES
1081 if ((specType == Suffixes) || (specType == ExPath) ||
1082 (specType == Includes) || (specType == Libs) ||
1087 * If the target was one that doesn't take files as its sources
1088 * but takes something like suffixes, we take each
1089 * space-separated word on the line as a something and deal
1090 * with it accordingly.
1092 * If the target was .SUFFIXES, we take each source as a
1093 * suffix and add it to the list of suffixes maintained by the
1096 * If the target was a .PATH, we add the source as a directory
1097 * to search on the search path.
1099 * If it was .INCLUDES, the source is taken to be the suffix of
1100 * files which will be #included and whose search path should
1101 * be present in the .INCLUDES variable.
1103 * If it was .LIBS, the source is taken to be the suffix of
1104 * files which are considered libraries and whose search path
1105 * should be present in the .LIBS variable.
1107 * If it was .NULL, the source is the suffix to use when a file
1108 * has no valid suffix.
1111 while (*cp && !isspace ((unsigned char) *cp)) {
1118 Suff_AddSuffix (line);
1121 Lst_ForEach(paths, ParseAddDir, (void *)line);
1124 Suff_AddInclude (line);
1130 Suff_SetNull (line);
1136 if (savec != '\0') {
1139 while (*cp && isspace ((unsigned char) *cp)) {
1145 Lst_Destroy(paths, NOFREE);
1150 * The targets take real sources, so we must beware of archive
1151 * specifications (i.e. things with left parentheses in them)
1152 * and handle them accordingly.
1154 while (*cp && !isspace ((unsigned char) *cp)) {
1155 if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) {
1157 * Only stop for a left parenthesis if it isn't at the
1158 * start of a word (that'll be for variable changes
1159 * later) and isn't preceded by a dollar sign (a dynamic
1171 sources = Lst_Init (FALSE);
1172 if (Arch_ParseArchive (&line, sources, VAR_CMD) != SUCCESS) {
1173 Parse_Error (PARSE_FATAL,
1174 "Error in source archive spec \"%s\"", line);
1178 while (!Lst_IsEmpty (sources)) {
1179 gn = (GNode *) Lst_DeQueue (sources);
1180 ParseDoSrc (tOp, gn->name, curSrcs);
1182 Lst_Destroy (sources, NOFREE);
1190 ParseDoSrc (tOp, line, curSrcs);
1192 while (*cp && isspace ((unsigned char) *cp)) {
1199 if (mainNode == NULL) {
1201 * If we have yet to decide on a main target to make, in the
1202 * absence of any user input, we want the first target on
1203 * the first dependency line that is actually a real target
1204 * (i.e. isn't a .USE or .EXEC rule) to be made.
1206 Lst_ForEach (targets, ParseFindMain, (void *)0);
1210 * Finally, destroy the list of sources
1212 Lst_Destroy(curSrcs, NOFREE);
1216 *---------------------------------------------------------------------
1218 * Return TRUE if the passed line is a variable assignment. A variable
1219 * assignment consists of a single word followed by optional whitespace
1220 * followed by either a += or an = operator.
1221 * This function is used both by the Parse_File function and main when
1222 * parsing the command-line arguments.
1225 * TRUE if it is. FALSE if it ain't
1229 *---------------------------------------------------------------------
1232 Parse_IsVar (char *line)
1234 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1235 Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1237 #define ISEQOPERATOR(c) \
1238 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1241 * Skip to variable name
1243 for (;(*line == ' ') || (*line == '\t'); line++)
1246 for (; *line != '=' || level != 0; line++)
1250 * end-of-line -- can't be a variable assignment.
1257 * there can be as much white space as desired so long as there is
1258 * only one word before the operator
1274 if (wasSpace && haveName) {
1275 if (ISEQOPERATOR(*line)) {
1277 * We must have a finished word
1283 * When an = operator [+?!:] is found, the next
1284 * character must be an = or it ain't a valid
1291 * This is a shell command
1293 if (strncmp(line, ":sh", 3) == 0)
1298 * This is the start of another word, so not assignment.
1313 *---------------------------------------------------------------------
1315 * Take the variable assignment in the passed line and do it in the
1318 * Note: There is a lexical ambiguity with assignment modifier characters
1319 * in variable names. This routine interprets the character before the =
1320 * as a modifier. Therefore, an assignment like
1322 * is interpreted as "C+ +=" instead of "C++ =".
1328 * the variable structure of the given variable name is altered in the
1330 *---------------------------------------------------------------------
1333 Parse_DoVar (char *line, GNode *ctxt)
1335 char *cp; /* pointer into line */
1337 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1338 } type; /* Type of assignment */
1339 char *opc; /* ptr to operator character to
1340 * null-terminate the variable name */
1342 * Avoid clobbered variable warnings by forcing the compiler
1343 * to ``unregister'' variables
1351 * Skip to variable name
1353 while ((*line == ' ') || (*line == '\t')) {
1358 * Skip to operator character, nulling out whitespace as we go
1360 for (cp = line + 1; *cp != '='; cp++) {
1361 if (isspace ((unsigned char) *cp)) {
1365 opc = cp-1; /* operator is the previous character */
1366 *cp++ = '\0'; /* nuke the = */
1369 * Check operator type
1379 * If the variable already has a value, we don't do anything.
1382 if (Var_Exists(line, ctxt)) {
1407 if (strncmp(opc, ":sh", 3) == 0) {
1417 while (isspace ((unsigned char) *cp)) {
1421 if (type == VAR_APPEND) {
1422 Var_Append (line, cp, ctxt);
1423 } else if (type == VAR_SUBST) {
1425 * Allow variables in the old value to be undefined, but leave their
1426 * invocation alone -- this is done by forcing oldVars to be false.
1427 * XXX: This can cause recursive variables, but that's not hard to do,
1428 * and this allows someone to do something like
1430 * CFLAGS = $(.INCLUDES)
1431 * CFLAGS := -I.. $(CFLAGS)
1433 * And not get an error.
1435 Boolean oldOldVars = oldVars;
1440 * make sure that we set the variable the first time to nothing
1441 * so that it gets substituted!
1443 if (!Var_Exists(line, ctxt))
1444 Var_Set(line, "", ctxt);
1446 cp = Var_Subst(NULL, cp, ctxt, FALSE);
1447 oldVars = oldOldVars;
1449 Var_Set(line, cp, ctxt);
1451 } else if (type == VAR_SHELL) {
1452 Boolean freeCmd = FALSE; /* TRUE if the command needs to be freed, i.e.
1453 * if any variable expansion was performed */
1456 if (strchr(cp, '$') != NULL) {
1458 * There's a dollar sign in the command, so perform variable
1459 * expansion on the whole thing. The resulting string will need
1460 * freeing when we're done, so set freeCmd to TRUE.
1462 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1466 res = Cmd_Exec(cp, &err);
1467 Var_Set(line, res, ctxt);
1471 Parse_Error(PARSE_WARNING, err, cp);
1477 * Normal assignment -- just do it.
1479 Var_Set(line, cp, ctxt);
1486 * Lst_ForEach function to add a command line to all targets
1492 * A new element is added to the commands list of the node.
1495 ParseAddCmd(void *gnp, void *cmd)
1497 GNode *gn = (GNode *) gnp;
1498 /* if target already supplied, ignore commands */
1499 if (!(gn->type & OP_HAS_COMMANDS))
1500 (void)Lst_AtEnd(gn->commands, cmd);
1505 *-----------------------------------------------------------------------
1506 * ParseHasCommands --
1507 * Callback procedure for Parse_File when destroying the list of
1508 * targets on the last dependency line. Marks a target as already
1509 * having commands if it does, to keep from having shell commands
1510 * on multiple dependency lines.
1516 * OP_HAS_COMMANDS may be set for the target.
1518 *-----------------------------------------------------------------------
1521 ParseHasCommands(void *gnp)
1523 GNode *gn = (GNode *) gnp;
1524 if (!Lst_IsEmpty(gn->commands)) {
1525 gn->type |= OP_HAS_COMMANDS;
1530 *-----------------------------------------------------------------------
1531 * Parse_AddIncludeDir --
1532 * Add a directory to the path searched for included makefiles
1533 * bracketed by double-quotes. Used by functions in main.c
1539 * The directory is appended to the list.
1541 *-----------------------------------------------------------------------
1544 Parse_AddIncludeDir (char *dir)
1546 Dir_AddDir (parseIncPath, dir);
1549 /*---------------------------------------------------------------------
1551 * Handle error directive
1553 * The input is the line minus the ".error". We substitute variables,
1554 * print the message and exit(1) or just print a warning if the ".error"
1555 * directive is malformed.
1557 *---------------------------------------------------------------------
1560 ParseDoError(char *errmsg)
1562 if (!isspace((unsigned char) *errmsg)) {
1563 Parse_Error(PARSE_WARNING, "invalid syntax: .error%s", errmsg);
1567 while (isspace((unsigned char) *errmsg))
1570 errmsg = Var_Subst(NULL, errmsg, VAR_GLOBAL, FALSE);
1572 /* use fprintf/exit instead of Parse_Error to terminate immediately */
1573 fprintf(stderr, "\"%s\", line %d: %s\n", fname, lineno, errmsg);
1578 *---------------------------------------------------------------------
1580 * Push to another file.
1582 * The input is the line minus the #include. A file spec is a string
1583 * enclosed in <> or "". The former is looked for only in sysIncPath.
1584 * The latter in . and the directories specified by -I command line
1591 * A structure is added to the includes Lst and readProc, lineno,
1592 * fname and curFILE are altered for the new file
1593 *---------------------------------------------------------------------
1596 ParseDoInclude (char *file)
1598 char *fullname; /* full pathname of file */
1599 IFile *oldFile; /* state associated with current file */
1600 char endc; /* the character which ends the file spec */
1601 char *cp; /* current position in file spec */
1602 Boolean isSystem; /* TRUE if makefile is a system makefile */
1605 * Skip to delimiter character so we know where to look
1607 while ((*file == ' ') || (*file == '\t')) {
1611 if ((*file != '"') && (*file != '<')) {
1612 Parse_Error (PARSE_FATAL,
1613 ".include filename must be delimited by '\"' or '<'");
1618 * Set the search path on which to find the include file based on the
1619 * characters which bracket its name. Angle-brackets imply it's
1620 * a system Makefile while double-quotes imply it's a user makefile
1631 * Skip to matching delimiter
1633 for (cp = ++file; *cp && *cp != endc; cp++) {
1638 Parse_Error (PARSE_FATAL,
1639 "Unclosed %cinclude filename. '%c' expected",
1646 * Substitute for any variables in the file name before trying to
1649 file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1652 * Now we know the file's name and its search path, we attempt to
1653 * find the durn thing. A return of NULL indicates the file don't
1658 * Include files contained in double-quotes are first searched for
1659 * relative to the including file's location. We don't want to
1660 * cd there, of course, so we just tack on the old file's
1661 * leading path components and call Dir_FindFile to see if
1662 * we can locate the beast.
1664 char *prefEnd, *Fname;
1666 /* Make a temporary copy of this, to be safe. */
1667 Fname = estrdup(fname);
1669 prefEnd = strrchr (Fname, '/');
1670 if (prefEnd != (char *)NULL) {
1675 newName = estrdup(file);
1677 newName = str_concat (Fname, file, STR_ADDSLASH);
1678 fullname = Dir_FindFile (newName, parseIncPath);
1679 if (fullname == (char *)NULL) {
1680 fullname = Dir_FindFile(newName, dirSearchPath);
1685 fullname = (char *)NULL;
1689 fullname = (char *)NULL;
1692 if (fullname == (char *)NULL) {
1694 * System makefile or makefile wasn't found in same directory as
1695 * included makefile. Search for it first on the -I search path,
1696 * then on the .PATH search path, if not found in a -I directory.
1697 * XXX: Suffix specific?
1699 fullname = Dir_FindFile (file, parseIncPath);
1700 if (fullname == (char *)NULL) {
1701 fullname = Dir_FindFile(file, dirSearchPath);
1705 if (fullname == (char *)NULL) {
1707 * Still haven't found the makefile. Look for it on the system
1708 * path as a last resort.
1710 fullname = Dir_FindFile(file, sysIncPath);
1713 if (fullname == (char *) NULL) {
1715 Parse_Error (PARSE_FATAL, "Could not find %s", file);
1722 * Once we find the absolute path to the file, we get to save all the
1723 * state from the current file before we can start reading this
1724 * include file. The state is stored in an IFile structure which
1725 * is placed on a list with other IFile structures. The list makes
1726 * a very nice stack to track how we got here...
1728 oldFile = (IFile *) emalloc (sizeof (IFile));
1729 oldFile->fname = fname;
1731 oldFile->F = curFILE;
1732 oldFile->p = curPTR;
1733 oldFile->lineno = lineno;
1735 (void) Lst_AtFront (includes, (void *)oldFile);
1738 * Once the previous state has been saved, we can get down to reading
1739 * the new file. We set up the name of the file to be the absolute
1740 * name of the include file so error messages refer to the right
1741 * place. Naturally enough, we start reading at line number 0.
1746 curFILE = fopen (fullname, "r");
1748 if (curFILE == (FILE * ) NULL) {
1749 Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1751 * Pop to previous file
1755 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
1762 *---------------------------------------------------------------------
1763 * Parse_FromString --
1764 * Start Parsing from the given string
1770 * A structure is added to the includes Lst and readProc, lineno,
1771 * fname and curFILE are altered for the new file
1772 *---------------------------------------------------------------------
1775 Parse_FromString(char *str)
1777 IFile *oldFile; /* state associated with this file */
1779 DEBUGF(FOR, ("%s\n----\n", str));
1781 oldFile = (IFile *) emalloc (sizeof (IFile));
1782 oldFile->lineno = lineno;
1783 oldFile->fname = fname;
1784 oldFile->F = curFILE;
1785 oldFile->p = curPTR;
1787 (void) Lst_AtFront (includes, (void *)oldFile);
1790 curPTR = (PTR *) emalloc (sizeof (PTR));
1791 curPTR->str = curPTR->ptr = str;
1793 fname = estrdup(fname);
1799 *---------------------------------------------------------------------
1800 * ParseTraditionalInclude --
1801 * Push to another file.
1803 * The input is the line minus the "include". The file name is
1804 * the string following the "include".
1810 * A structure is added to the includes Lst and readProc, lineno,
1811 * fname and curFILE are altered for the new file
1812 *---------------------------------------------------------------------
1815 ParseTraditionalInclude (char *file)
1817 char *fullname; /* full pathname of file */
1818 IFile *oldFile; /* state associated with current file */
1819 char *cp; /* current position in file spec */
1823 * Skip over whitespace
1825 while ((*file == ' ') || (*file == '\t')) {
1829 if (*file == '\0') {
1830 Parse_Error (PARSE_FATAL,
1831 "Filename missing from \"include\"");
1836 * Skip to end of line or next whitespace
1838 for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1845 * Substitute for any variables in the file name before trying to
1848 file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1851 * Now we know the file's name, we attempt to find the durn thing.
1852 * A return of NULL indicates the file don't exist.
1854 * Include files are first searched for relative to the including
1855 * file's location. We don't want to cd there, of course, so we
1856 * just tack on the old file's leading path components and call
1857 * Dir_FindFile to see if we can locate the beast.
1858 * XXX - this *does* search in the current directory, right?
1861 prefEnd = strrchr (fname, '/');
1862 if (prefEnd != (char *)NULL) {
1866 newName = str_concat (fname, file, STR_ADDSLASH);
1867 fullname = Dir_FindFile (newName, parseIncPath);
1868 if (fullname == (char *)NULL) {
1869 fullname = Dir_FindFile(newName, dirSearchPath);
1874 fullname = (char *)NULL;
1877 if (fullname == (char *)NULL) {
1879 * System makefile or makefile wasn't found in same directory as
1880 * included makefile. Search for it first on the -I search path,
1881 * then on the .PATH search path, if not found in a -I directory.
1882 * XXX: Suffix specific?
1884 fullname = Dir_FindFile (file, parseIncPath);
1885 if (fullname == (char *)NULL) {
1886 fullname = Dir_FindFile(file, dirSearchPath);
1890 if (fullname == (char *)NULL) {
1892 * Still haven't found the makefile. Look for it on the system
1893 * path as a last resort.
1895 fullname = Dir_FindFile(file, sysIncPath);
1898 if (fullname == (char *) NULL) {
1899 Parse_Error (PARSE_FATAL, "Could not find %s", file);
1904 * Once we find the absolute path to the file, we get to save all the
1905 * state from the current file before we can start reading this
1906 * include file. The state is stored in an IFile structure which
1907 * is placed on a list with other IFile structures. The list makes
1908 * a very nice stack to track how we got here...
1910 oldFile = (IFile *) emalloc (sizeof (IFile));
1911 oldFile->fname = fname;
1913 oldFile->F = curFILE;
1914 oldFile->p = curPTR;
1915 oldFile->lineno = lineno;
1917 (void) Lst_AtFront (includes, (void *)oldFile);
1920 * Once the previous state has been saved, we can get down to reading
1921 * the new file. We set up the name of the file to be the absolute
1922 * name of the include file so error messages refer to the right
1923 * place. Naturally enough, we start reading at line number 0.
1928 curFILE = fopen (fullname, "r");
1930 if (curFILE == (FILE * ) NULL) {
1931 Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1933 * Pop to previous file
1937 Var_Append(".MAKEFILE_LIST", fullname, VAR_GLOBAL);
1943 *---------------------------------------------------------------------
1945 * Called when EOF is reached in the current file. If we were reading
1946 * an include file, the includes stack is popped and things set up
1947 * to go back to reading the previous file at the previous location.
1950 * CONTINUE if there's more to do. DONE if not.
1953 * The old curFILE, is closed. The includes list is shortened.
1954 * lineno, curFILE, and fname are changed if CONTINUE is returned.
1955 *---------------------------------------------------------------------
1958 ParseEOF (int opened)
1960 IFile *ifile; /* the state on the top of the includes stack */
1962 if (Lst_IsEmpty (includes)) {
1963 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
1967 ifile = (IFile *) Lst_DeQueue (includes);
1969 fname = ifile->fname;
1970 lineno = ifile->lineno;
1971 if (opened && curFILE) {
1972 (void) fclose (curFILE);
1973 Var_Append(".MAKEFILE_LIST", "..", VAR_GLOBAL);
1986 *---------------------------------------------------------------------
1988 * Read a character from the current file
1991 * The character that was read
1994 *---------------------------------------------------------------------
2000 return fgetc(curFILE);
2002 if (curPTR && *curPTR->ptr)
2003 return *curPTR->ptr++;
2009 *---------------------------------------------------------------------
2011 * Put back a character to the current file
2017 *---------------------------------------------------------------------
2027 *--(curPTR->ptr) = c;
2034 * Grab the next line unless it begins with a dot (`.') and we're told to
2035 * ignore such lines.
2038 ParseSkipLine(int skip)
2041 int c, lastc, lineLength = 0;
2044 buf = Buf_Init(MAKE_BSIZE);
2047 Buf_Discard(buf, lineLength);
2050 while (((c = ParseReadc()) != '\n' || lastc == '\\')
2053 Buf_ReplaceLastByte(buf, (Byte)' ');
2056 while ((c = ParseReadc()) == ' ' || c == '\t');
2062 Buf_AddByte(buf, (Byte)c);
2067 Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
2068 Buf_Destroy(buf, TRUE);
2069 return((char *)NULL);
2073 Buf_AddByte(buf, (Byte)'\0');
2074 line = (char *)Buf_GetAll(buf, &lineLength);
2075 } while (skip == 1 && line[0] != '.');
2077 Buf_Destroy(buf, FALSE);
2083 *---------------------------------------------------------------------
2085 * Read an entire line from the input file. Called only by Parse_File.
2086 * To facilitate escaped newlines and what have you, a character is
2087 * buffered in 'lastc', which is '\0' when no characters have been
2088 * read. When we break out of the loop, c holds the terminating
2089 * character and lastc holds a character that should be added to
2090 * the line (unless we don't read anything but a terminator).
2093 * A line w/o its newline
2096 * Only those associated with reading a character
2097 *---------------------------------------------------------------------
2100 ParseReadLine (void)
2102 Buffer buf; /* Buffer for current line */
2103 int c; /* the current character */
2104 int lastc; /* The most-recent character */
2105 Boolean semiNL; /* treat semi-colons as newlines */
2106 Boolean ignDepOp; /* TRUE if should ignore dependency operators
2107 * for the purposes of setting semiNL */
2108 Boolean ignComment; /* TRUE if should ignore comments (in a
2110 char *line; /* Result */
2111 char *ep; /* to strip trailing blanks */
2112 int lineLength; /* Length of result */
2119 * Handle special-characters at the beginning of the line. Either a
2120 * leading tab (shell command) or pound-sign (possible conditional)
2121 * forces us to ignore comments and dependency operators and treat
2122 * semi-colons as semi-colons (by leaving semiNL FALSE). This also
2123 * discards completely blank lines.
2129 ignComment = ignDepOp = TRUE;
2131 } else if (c == '\n') {
2133 } else if (c == '#') {
2138 * Anything else breaks out without doing anything
2146 buf = Buf_Init(MAKE_BSIZE);
2148 while (((c = ParseReadc ()) != '\n' || (lastc == '\\')) &&
2155 * Escaped newline: read characters until a non-space or an
2156 * unescaped newline and replace them all by a single space.
2157 * This is done by storing the space over the backslash and
2158 * dropping through with the next nonspace. If it is a
2159 * semi-colon and semiNL is TRUE, it will be recognized as a
2160 * newline in the code below this...
2164 while ((c = ParseReadc ()) == ' ' || c == '\t') {
2167 if (c == EOF || c == '\n') {
2171 * Check for comments, semiNL's, etc. -- easier than
2172 * ParseUnreadc(c); continue;
2181 * Semi-colon: Need to see if it should be interpreted as a
2186 * To make sure the command that may be following this
2187 * semi-colon begins with a tab, we push one back into the
2188 * input stream. This will overwrite the semi-colon in the
2189 * buffer. If there is no command following, this does no
2190 * harm, since the newline remains in the buffer and the
2191 * whole line is ignored.
2200 * Haven't seen a dependency operator before this, so this
2201 * must be a variable assignment -- don't pay attention to
2202 * dependency operators after this.
2205 } else if (lastc == ':' || lastc == '!') {
2207 * Well, we've seen a dependency operator already, but it
2208 * was the previous character, so this is really just an
2209 * expanded variable assignment. Revert semi-colons to
2210 * being just semi-colons again and ignore any more
2211 * dependency operators.
2213 * XXX: Note that a line like "foo : a:=b" will blow up,
2214 * but who'd write a line like that anyway?
2216 ignDepOp = TRUE; semiNL = FALSE;
2227 * If the character is a hash mark and it isn't escaped
2228 * (or we're being compatible), the thing is a comment.
2229 * Skip to the end of the line.
2233 } while ((c != '\n') && (c != EOF));
2237 * Don't add the backslash. Just let the # get copied
2247 if (!ignDepOp && (c == ':' || c == '!')) {
2249 * A semi-colon is recognized as a newline only on
2250 * dependency lines. Dependency lines are lines with a
2251 * colon or an exclamation point. Ergo...
2258 * Copy in the previous character and save this one in lastc.
2260 Buf_AddByte (buf, (Byte)lastc);
2267 if (lastc != '\0') {
2268 Buf_AddByte (buf, (Byte)lastc);
2270 Buf_AddByte (buf, (Byte)'\0');
2271 line = (char *)Buf_GetAll (buf, &lineLength);
2272 Buf_Destroy (buf, FALSE);
2275 * Strip trailing blanks and tabs from the line.
2276 * Do not strip a blank or tab that is preceeded by
2282 while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
2283 if (ep > line + 1 && ep[-2] == '\\')
2289 if (line[0] == '.') {
2291 * The line might be a conditional. Ask the conditional module
2292 * about it and act accordingly
2294 switch (Cond_Eval (line)) {
2297 * Skip to next conditional that evaluates to COND_PARSE.
2301 line = ParseSkipLine(1);
2302 } while (line && Cond_Eval(line) != COND_PARSE);
2308 line = ParseReadLine();
2311 if (For_Eval(line)) {
2316 * Skip after the matching end
2318 line = ParseSkipLine(0);
2320 Parse_Error (PARSE_FATAL,
2321 "Unexpected end of file in for loop.\n");
2324 ok = For_Eval(line);
2330 line = ParseReadLine();
2339 * Hit end-of-file, so return a NULL line to indicate this.
2341 return((char *)NULL);
2346 *-----------------------------------------------------------------------
2347 * ParseFinishLine --
2348 * Handle the end of a dependency group.
2354 * inLine set FALSE. 'targets' list destroyed.
2356 *-----------------------------------------------------------------------
2359 ParseFinishLine(void)
2362 Lst_ForEach(targets, Suff_EndTransform, (void *)NULL);
2363 Lst_Destroy (targets, ParseHasCommands);
2370 stripvarname(char *cp)
2374 while (isspace((unsigned char)*cp))
2377 while (*cp2 && !isspace((unsigned char)*cp2))
2385 *---------------------------------------------------------------------
2387 * Parse a file into its component parts, incorporating it into the
2388 * current dependency graph. This is the main function and controls
2389 * almost every other function in this module
2395 * Loads. Nodes are added to the list of all targets, nodes and links
2396 * are added to the dependency graph. etc. etc. etc.
2397 *---------------------------------------------------------------------
2400 Parse_File(char *name, FILE *stream)
2402 char *cp, /* pointer into the line */
2403 *line; /* the line we're working on */
2411 Var_Append(".MAKEFILE_LIST", name, VAR_GLOBAL);
2414 while ((line = ParseReadLine ()) != NULL) {
2417 * Lines that begin with the special character are either
2418 * include or undef directives.
2420 for (cp = line + 1; isspace ((unsigned char) *cp); cp++) {
2423 if (strncmp (cp, "include", 7) == 0) {
2424 ParseDoInclude (cp + 7);
2426 } else if (strncmp (cp, "error", 5) == 0) {
2427 ParseDoError(cp + 5);
2429 } else if (strncmp(cp, "undef", 5) == 0) {
2430 cp = stripvarname(cp + 5);
2431 Var_Delete(cp, VAR_GLOBAL);
2433 } else if (strncmp(cp, "makeenv", 7) == 0) {
2434 cp = stripvarname(cp + 7);
2435 Var_SetEnv(cp, VAR_GLOBAL);
2440 /* If we're this far, the line must be a comment. */
2444 if (*line == '\t') {
2446 * If a line starts with a tab, it can only hope to be
2447 * a creation command.
2452 for (cp = line + 1; isspace ((unsigned char) *cp); cp++) {
2458 * So long as it's not a blank line and we're actually
2459 * in a dependency spec, add the command to the list of
2460 * commands of all targets in the dependency spec
2462 Lst_ForEach (targets, ParseAddCmd, cp);
2463 Lst_AtEnd(targCmds, (void *) line);
2466 Parse_Error (PARSE_FATAL,
2467 "Unassociated shell command \"%s\"",
2472 } else if (strncmp (line, "include", 7) == 0 &&
2473 isspace((unsigned char) line[7]) &&
2474 strchr(line, ':') == NULL) {
2476 * It's an S3/S5-style "include".
2478 ParseTraditionalInclude (line + 7);
2481 } else if (Parse_IsVar (line)) {
2483 Parse_DoVar (line, VAR_GLOBAL);
2486 * We now know it's a dependency line so it needs to have all
2487 * variables expanded before being parsed. Tell the variable
2488 * module to complain if some variable is undefined...
2489 * To make life easier on novices, if the line is indented we
2490 * first make sure the line has a dependency operator in it.
2491 * If it doesn't have an operator and we're in a dependency
2492 * line's script, we assume it's actually a shell command
2493 * and add it to the current list of targets.
2496 Boolean nonSpace = FALSE;
2500 if (isspace((unsigned char) line[0])) {
2501 while ((*cp != '\0') && isspace((unsigned char) *cp)) {
2508 while ((*cp != ':') && (*cp != '!') && (*cp != '\0')) {
2518 Parse_Error (PARSE_WARNING,
2519 "Shell command needs a leading tab");
2521 } else if (nonSpace) {
2522 Parse_Error (PARSE_FATAL, "Missing operator");
2528 cp = Var_Subst (NULL, line, VAR_CMD, TRUE);
2533 * Need a non-circular list for the target nodes
2536 Lst_Destroy(targets, NOFREE);
2538 targets = Lst_Init (FALSE);
2541 ParseDoDependency (line);
2552 * Reached EOF, but it may be just EOF of an include file...
2554 } while (ParseEOF(1) == CONTINUE);
2557 * Make sure conditionals are clean
2562 errx(1, "fatal errors encountered -- cannot continue");
2566 *---------------------------------------------------------------------
2568 * initialize the parsing module
2574 * the parseIncPath list is initialized...
2575 *---------------------------------------------------------------------
2581 parseIncPath = Lst_Init (FALSE);
2582 sysIncPath = Lst_Init (FALSE);
2583 includes = Lst_Init (FALSE);
2584 targCmds = Lst_Init (FALSE);
2590 Lst_Destroy(targCmds, (void (*) (void *)) free);
2592 Lst_Destroy(targets, NOFREE);
2593 Lst_Destroy(sysIncPath, Dir_Destroy);
2594 Lst_Destroy(parseIncPath, Dir_Destroy);
2595 Lst_Destroy(includes, NOFREE); /* Should be empty now */
2600 *-----------------------------------------------------------------------
2602 * Return a Lst of the main target to create for main()'s sake. If
2603 * no such target exists, we Punt with an obnoxious error message.
2606 * A Lst of the single node to create.
2611 *-----------------------------------------------------------------------
2614 Parse_MainName(void)
2616 Lst listmain; /* result list */
2618 listmain = Lst_Init (FALSE);
2620 if (mainNode == NULL) {
2621 Punt ("no target to make.");
2623 } else if (mainNode->type & OP_DOUBLEDEP) {
2624 (void) Lst_AtEnd (listmain, (void *)mainNode);
2625 Lst_Concat(listmain, mainNode->cohorts, LST_CONCNEW);
2628 (void) Lst_AtEnd (listmain, (void *)mainNode);