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.1 2002/12/26 14:36:38 ru Exp $
40 * $DragonFly: src/usr.bin/make/parse.c,v 1.3 2003/11/03 19:31:30 eirikn 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.
98 #include "pathnames.h"
101 * These values are returned by ParseEOF to tell Parse_File whether to
102 * CONTINUE parsing, i.e. it had only reached the end of an include file,
107 static Lst targets; /* targets we're working on */
108 static Lst targCmds; /* command lines for targets */
109 static Boolean inLine; /* true if currently in a dependency
110 * line or its commands */
116 static char *fname; /* name of current file (for errors) */
117 static int lineno; /* line number in current file */
118 static FILE *curFILE = NULL; /* current makefile */
120 static PTR *curPTR = NULL; /* current makefile */
122 static int fatals = 0;
124 static GNode *mainNode; /* The main target to create. This is the
125 * first target on the first dependency
126 * line in the first makefile */
128 * Definitions for handling #include specifications
130 typedef struct IFile {
131 char *fname; /* name of previous file */
132 int lineno; /* saved line number */
133 FILE * F; /* the open stream */
134 PTR * p; /* the char pointer */
137 static Lst includes; /* stack of IFiles generated by
139 Lst parseIncPath; /* list of directories for "..." includes */
140 Lst sysIncPath; /* list of directories for <...> includes */
143 * specType contains the SPECial TYPE of the current target. It is
144 * Not if the target is unspecial. If it *is* special, however, the children
145 * are linked as children of the parent but not vice versa. This variable is
146 * set in ParseDoDependency
150 Default, /* .DEFAULT */
152 Ignore, /* .IGNORE */
153 Includes, /* .INCLUDES */
154 Interrupt, /* .INTERRUPT */
156 MFlags, /* .MFLAGS or .MAKEFLAGS */
157 Main, /* .MAIN and we don't have anything user-specified to
159 NoExport, /* .NOEXPORT */
160 Not, /* Not special */
161 NotParallel, /* .NOTPARALELL */
164 Parallel, /* .PARALLEL */
170 Precious, /* .PRECIOUS */
171 ExShell, /* .SHELL */
172 Silent, /* .SILENT */
173 SingleShell, /* .SINGLESHELL */
174 Suffixes, /* .SUFFIXES */
176 Attribute /* Generic attribute */
179 static ParseSpecial specType;
183 * Predecessor node for handling .ORDER. Initialized to NILGNODE when .ORDER
184 * seen, then set to each successive source on the line.
186 static GNode *predecessor;
189 * The parseKeywords table is searched using binary search when deciding
190 * if a target or source is special. The 'spec' field is the ParseSpecial
191 * type of the keyword ("Not" if the keyword isn't special as a target) while
192 * the 'op' field is the operator to apply to the list of targets if the
193 * keyword is used as a source ("0" if the keyword isn't special as a source)
196 char *name; /* Name of keyword */
197 ParseSpecial spec; /* Type when used as a target */
198 int op; /* Operator when used as a source */
199 } parseKeywords[] = {
200 { ".BEGIN", Begin, 0 },
201 { ".DEFAULT", Default, 0 },
203 { ".EXEC", Attribute, OP_EXEC },
204 { ".IGNORE", Ignore, OP_IGNORE },
205 { ".INCLUDES", Includes, 0 },
206 { ".INTERRUPT", Interrupt, 0 },
207 { ".INVISIBLE", Attribute, OP_INVISIBLE },
208 { ".JOIN", Attribute, OP_JOIN },
209 { ".LIBS", Libs, 0 },
210 { ".MAIN", Main, 0 },
211 { ".MAKE", Attribute, OP_MAKE },
212 { ".MAKEFLAGS", MFlags, 0 },
213 { ".MFLAGS", MFlags, 0 },
214 { ".NOTMAIN", Attribute, OP_NOTMAIN },
215 { ".NOTPARALLEL", NotParallel, 0 },
216 { ".NO_PARALLEL", NotParallel, 0 },
217 { ".NULL", Null, 0 },
218 { ".OPTIONAL", Attribute, OP_OPTIONAL },
219 { ".ORDER", Order, 0 },
220 { ".PARALLEL", Parallel, 0 },
221 { ".PATH", ExPath, 0 },
222 { ".PHONY", Phony, OP_PHONY },
224 { ".POSIX", Posix, 0 },
226 { ".PRECIOUS", Precious, OP_PRECIOUS },
227 { ".RECURSIVE", Attribute, OP_MAKE },
228 { ".SHELL", ExShell, 0 },
229 { ".SILENT", Silent, OP_SILENT },
230 { ".SINGLESHELL", SingleShell, 0 },
231 { ".SUFFIXES", Suffixes, 0 },
232 { ".USE", Attribute, OP_USE },
233 { ".WAIT", Wait, 0 },
236 static int ParseFindKeyword(char *);
237 static int ParseLinkSrc(ClientData, ClientData);
238 static int ParseDoOp(ClientData, ClientData);
239 static int ParseAddDep(ClientData, ClientData);
240 static void ParseDoSrc(int, char *, Lst);
241 static int ParseFindMain(ClientData, ClientData);
242 static int ParseAddDir(ClientData, ClientData);
243 static int ParseClearPath(ClientData, ClientData);
244 static void ParseDoDependency(char *);
245 static int ParseAddCmd(ClientData, ClientData);
246 static int ParseReadc(void);
247 static void ParseUnreadc(int);
248 static void ParseHasCommands(ClientData);
249 static void ParseDoInclude(char *);
250 static void ParseDoError(char *);
252 static void ParseTraditionalInclude(char *);
254 static int ParseEOF(int);
255 static char *ParseReadLine(void);
256 static char *ParseSkipLine(int);
257 static void ParseFinishLine(void);
260 *----------------------------------------------------------------------
261 * ParseFindKeyword --
262 * Look in the table of keywords for one matching the given string.
265 * The index of the keyword, or -1 if it isn't there.
269 *----------------------------------------------------------------------
272 ParseFindKeyword (str)
273 char *str; /* String to find */
281 end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
284 cur = start + ((end - start) / 2);
285 diff = strcmp (str, parseKeywords[cur].name);
289 } else if (diff < 0) {
294 } while (start <= end);
300 * Error message abort function for parsing. Prints out the context
301 * of the error (line number and file) as well as the message with
302 * two optional arguments.
308 * "fatals" is incremented if the level is PARSE_FATAL.
313 Parse_Error(int type, char *fmt, ...)
315 Parse_Error(va_alist)
323 int type; /* Error type (PARSE_WARNING, PARSE_FATAL) */
327 type = va_arg(ap, int);
328 fmt = va_arg(ap, char *);
331 (void)fprintf(stderr, "\"%s\", line %d: ", fname, lineno);
332 if (type == PARSE_WARNING)
333 (void)fprintf(stderr, "warning: ");
334 (void)vfprintf(stderr, fmt, ap);
336 (void)fprintf(stderr, "\n");
337 (void)fflush(stderr);
338 if (type == PARSE_FATAL)
343 *---------------------------------------------------------------------
345 * Link the parent node to its new child. Used in a Lst_ForEach by
346 * ParseDoDependency. If the specType isn't 'Not', the parent
347 * isn't linked as a parent of the child.
353 * New elements are added to the parents list of cgn and the
354 * children list of cgn. the unmade field of pgn is updated
355 * to reflect the additional child.
356 *---------------------------------------------------------------------
359 ParseLinkSrc (pgnp, cgnp)
360 ClientData pgnp; /* The parent node */
361 ClientData cgnp; /* The child node */
363 GNode *pgn = (GNode *) pgnp;
364 GNode *cgn = (GNode *) cgnp;
365 if (Lst_Member (pgn->children, (ClientData)cgn) == NILLNODE) {
366 (void)Lst_AtEnd (pgn->children, (ClientData)cgn);
367 if (specType == Not) {
368 (void)Lst_AtEnd (cgn->parents, (ClientData)pgn);
376 *---------------------------------------------------------------------
378 * Apply the parsed operator to the given target node. Used in a
379 * Lst_ForEach call by ParseDoDependency once all targets have
380 * been found and their operator parsed. If the previous and new
381 * operators are incompatible, a major error is taken.
387 * The type field of the node is altered to reflect any new bits in
389 *---------------------------------------------------------------------
393 ClientData gnp; /* The node to which the operator is to be
395 ClientData opp; /* The operator to apply */
397 GNode *gn = (GNode *) gnp;
398 int op = *(int *) opp;
400 * If the dependency mask of the operator and the node don't match and
401 * the node has actually had an operator applied to it before, and
402 * the operator actually has some dependency information in it, complain.
404 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
405 !OP_NOP(gn->type) && !OP_NOP(op))
407 Parse_Error (PARSE_FATAL, "Inconsistent operator for %s", gn->name);
411 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
413 * If the node was the object of a :: operator, we need to create a
414 * new instance of it for the children and commands on this dependency
415 * line. The new instance is placed on the 'cohorts' list of the
416 * initial one (note the initial one is not on its own cohorts list)
417 * and the new instance is linked to all parents of the initial
420 register GNode *cohort;
423 cohort = Targ_NewGN(gn->name);
425 * Duplicate links to parents so graph traversal is simple. Perhaps
426 * some type bits should be duplicated?
428 * Make the cohort invisible as well to avoid duplicating it into
429 * other variables. True, parents of this target won't tend to do
430 * anything with their local variables, but better safe than
433 Lst_ForEach(gn->parents, ParseLinkSrc, (ClientData)cohort);
434 cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
435 (void)Lst_AtEnd(gn->cohorts, (ClientData)cohort);
438 * Replace the node in the targets list with the new copy
440 ln = Lst_Member(targets, (ClientData)gn);
441 Lst_Replace(ln, (ClientData)cohort);
445 * We don't want to nuke any previous flags (whatever they were) so we
446 * just OR the new operator into the old
454 *---------------------------------------------------------------------
456 * Check if the pair of GNodes given needs to be synchronized.
457 * This has to be when two nodes are on different sides of a
461 * Returns 1 if the two targets need to be ordered, 0 otherwise.
462 * If it returns 1, the search can stop
465 * A dependency can be added between the two nodes.
467 *---------------------------------------------------------------------
474 GNode *p = (GNode *) pp;
475 GNode *s = (GNode *) sp;
477 if (p->order < s->order) {
479 * XXX: This can cause loops, and loops can cause unmade targets,
480 * but checking is tedious, and the debugging output can show the
483 (void)Lst_AtEnd(p->successors, (ClientData)s);
484 (void)Lst_AtEnd(s->preds, (ClientData)p);
493 *---------------------------------------------------------------------
495 * Given the name of a source, figure out if it is an attribute
496 * and apply it to the targets if it is. Else decide if there is
497 * some attribute which should be applied *to* the source because
498 * of some special target and apply it if so. Otherwise, make the
499 * source be a child of the targets in the list 'targets'
505 * Operator bits may be added to the list of targets or to the source.
506 * The targets may have a new source added to their lists of children.
507 *---------------------------------------------------------------------
510 ParseDoSrc (tOp, src, allsrc)
511 int tOp; /* operator (if any) from special targets */
512 char *src; /* name of the source to handle */
513 Lst allsrc; /* List of all sources to wait for */
517 if (*src == '.' && isupper (src[1])) {
518 int keywd = ParseFindKeyword(src);
520 int op = parseKeywords[keywd].op;
522 Lst_ForEach (targets, ParseDoOp, (ClientData)&op);
525 if (parseKeywords[keywd].spec == Wait) {
535 * If we have noted the existence of a .MAIN, it means we need
536 * to add the sources of said target to the list of things
537 * to create. The string 'src' is likely to be free, so we
538 * must make a new copy of it. Note that this will only be
539 * invoked if the user didn't specify a target on the command
540 * line. This is to allow #ifmake's to succeed, or something...
542 (void) Lst_AtEnd (create, (ClientData)estrdup(src));
544 * Add the name to the .TARGETS variable as well, so the user cna
545 * employ that, if desired.
547 Var_Append(".TARGETS", src, VAR_GLOBAL);
552 * Create proper predecessor/successor links between the previous
553 * source and the current one.
555 gn = Targ_FindNode(src, TARG_CREATE);
556 if (predecessor != NILGNODE) {
557 (void)Lst_AtEnd(predecessor->successors, (ClientData)gn);
558 (void)Lst_AtEnd(gn->preds, (ClientData)predecessor);
561 * The current source now becomes the predecessor for the next one.
568 * If the source is not an attribute, we need to find/create
569 * a node for it. After that we can apply any operator to it
570 * from a special target or link it to its parents, as
573 * In the case of a source that was the object of a :: operator,
574 * the attribute is applied to all of its instances (as kept in
575 * the 'cohorts' list of the node) or all the cohorts are linked
576 * to all the targets.
578 gn = Targ_FindNode (src, TARG_CREATE);
582 Lst_ForEach (targets, ParseLinkSrc, (ClientData)gn);
584 if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
585 register GNode *cohort;
588 for (ln=Lst_First(gn->cohorts); ln != NILLNODE; ln = Lst_Succ(ln)){
589 cohort = (GNode *)Lst_Datum(ln);
593 Lst_ForEach(targets, ParseLinkSrc, (ClientData)cohort);
601 (void)Lst_AtEnd(allsrc, (ClientData)gn);
603 Lst_ForEach(allsrc, ParseAddDep, (ClientData)gn);
608 *-----------------------------------------------------------------------
610 * Find a real target in the list and set it to be the main one.
611 * Called by ParseDoDependency when a main target hasn't been found
615 * 0 if main not found yet, 1 if it is.
618 * mainNode is changed and Targ_SetMain is called.
620 *-----------------------------------------------------------------------
623 ParseFindMain(gnp, dummy)
624 ClientData gnp; /* Node to examine */
627 GNode *gn = (GNode *) gnp;
628 if ((gn->type & (OP_NOTMAIN|OP_USE|OP_EXEC|OP_TRANSFORM)) == 0) {
631 return (dummy ? 1 : 1);
633 return (dummy ? 0 : 0);
638 *-----------------------------------------------------------------------
640 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
648 *-----------------------------------------------------------------------
651 ParseAddDir(path, name)
655 Dir_AddDir((Lst) path, (char *) name);
660 *-----------------------------------------------------------------------
662 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
670 *-----------------------------------------------------------------------
673 ParseClearPath(path, dummy)
677 Dir_ClearPath((Lst) path);
678 return(dummy ? 0 : 0);
682 *---------------------------------------------------------------------
683 * ParseDoDependency --
684 * Parse the dependency line in line.
690 * The nodes of the sources are linked as children to the nodes of the
691 * targets. Some nodes may be created.
693 * We parse a dependency line by first extracting words from the line and
694 * finding nodes in the list of all targets with that name. This is done
695 * until a character is encountered which is an operator character. Currently
696 * these are only ! and :. At this point the operator is parsed and the
697 * pointer into the line advanced until the first source is encountered.
698 * The parsed operator is applied to each node in the 'targets' list,
699 * which is where the nodes found for the targets are kept, by means of
700 * the ParseDoOp function.
701 * The sources are read in much the same way as the targets were except
702 * that now they are expanded using the wildcarding scheme of the C-Shell
703 * and all instances of the resulting words in the list of all targets
704 * are found. Each of the resulting nodes is then linked to each of the
705 * targets as one of its children.
706 * Certain targets are handled specially. These are the ones detailed
707 * by the specType variable.
708 * The storing of transformation rules is also taken care of here.
709 * A target is recognized as a transformation rule by calling
710 * Suff_IsTransform. If it is a transformation rule, its node is gotten
711 * from the suffix module via Suff_AddTransform rather than the standard
712 * Targ_FindNode in the target module.
713 *---------------------------------------------------------------------
716 ParseDoDependency (line)
717 char *line; /* the line to parse */
719 char *cp; /* our current position */
720 GNode *gn; /* a general purpose temporary node */
721 int op; /* the operator on the line */
722 char savec; /* a place to save a character */
723 Lst paths; /* List of search paths to alter when parsing
724 * a list of .PATH targets */
725 int tOp; /* operator from special target */
726 Lst sources; /* list of archive source names after
728 Lst curTargs; /* list of target names to be found and added
729 * to the targets list */
730 Lst curSrcs; /* list of sources in order */
738 curTargs = Lst_Init(FALSE);
739 curSrcs = Lst_Init(FALSE);
743 *cp && !isspace ((unsigned char) *cp) && *cp != '(';
748 * Must be a dynamic source (would have been expanded
749 * otherwise), so call the Var module to parse the puppy
750 * so we can safely advance beyond it...There should be
751 * no errors in this, as they would have been discovered
752 * in the initial Var_Subst and we wouldn't be here.
758 result=Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
764 } else if (*cp == '!' || *cp == ':') {
766 * We don't want to end a word on ':' or '!' if there is a
767 * better match later on in the string (greedy matching).
768 * This allows the user to have targets like:
771 * where "fie::fi:fo" and "foo::bar" are the targets. In
772 * real life this is used for perl5 library man pages where
773 * "::" separates an object from its class.
774 * Ie: "File::Spec::Unix". This behaviour is also consistent
775 * with other versions of make.
779 if (*cp == ':' && *p == ':')
782 /* Found the best match already. */
783 if (*p == '\0' || isspace(*p))
786 p += strcspn(p, "!:");
788 /* No better match later on... */
796 * Archives must be handled specially to make sure the OP_ARCHV
797 * flag is set in their 'type' field, for one thing, and because
798 * things like "archive(file1.o file2.o file3.o)" are permissible.
799 * Arch_ParseArchive will set 'line' to be the first non-blank
800 * after the archive-spec. It creates/finds nodes for the members
801 * and places them on the given list, returning SUCCESS if all
802 * went well and FAILURE if there was an error in the
803 * specification. On error, line should remain untouched.
805 if (Arch_ParseArchive (&line, targets, VAR_CMD) != SUCCESS) {
806 Parse_Error (PARSE_FATAL,
807 "Error in archive specification: \"%s\"", line);
817 * Ending a dependency line without an operator is a Bozo
820 Parse_Error (PARSE_FATAL, "Need an operator");
825 * Have a word in line. See if it's a special target and set
826 * specType to match it.
828 if (*line == '.' && isupper (line[1])) {
830 * See if the target is a special target that must have it
831 * or its sources handled specially.
833 int keywd = ParseFindKeyword(line);
835 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
836 Parse_Error(PARSE_FATAL, "Mismatched special targets");
840 specType = parseKeywords[keywd].spec;
841 tOp = parseKeywords[keywd].op;
844 * Certain special targets have special semantics:
845 * .PATH Have to set the dirSearchPath
847 * .MAIN Its sources are only used if
848 * nothing has been specified to
850 * .DEFAULT Need to create a node to hang
851 * commands on, but we don't want
852 * it in the graph, nor do we want
853 * it to be the Main Target, so we
854 * create it, set OP_NOTMAIN and
855 * add it to the list, setting
856 * DEFAULT to the new node for
857 * later use. We claim the node is
858 * A transformation rule to make
859 * life easier later, when we'll
860 * use Make_HandleUse to actually
861 * apply the .DEFAULT commands.
862 * .PHONY The list of targets
865 * .INTERRUPT Are not to be considered the
867 * .NOTPARALLEL Make only one target at a time.
868 * .SINGLESHELL Create a shell for each command.
869 * .ORDER Must set initial predecessor to NIL
874 paths = Lst_Init(FALSE);
876 (void)Lst_AtEnd(paths, (ClientData)dirSearchPath);
879 if (!Lst_IsEmpty(create)) {
886 gn = Targ_FindNode(line, TARG_CREATE);
887 gn->type |= OP_NOTMAIN;
888 (void)Lst_AtEnd(targets, (ClientData)gn);
891 gn = Targ_NewGN(".DEFAULT");
892 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
893 (void)Lst_AtEnd(targets, (ClientData)gn);
907 predecessor = NILGNODE;
912 } else if (strncmp (line, ".PATH", 5) == 0) {
914 * .PATH<suffix> has to be handled specially.
915 * Call on the suffix module to give us a path to
921 path = Suff_GetPath (&line[5]);
922 if (path == NILLST) {
923 Parse_Error (PARSE_FATAL,
924 "Suffix '%s' not defined (yet)",
928 if (paths == (Lst)NULL) {
929 paths = Lst_Init(FALSE);
931 (void)Lst_AtEnd(paths, (ClientData)path);
937 * Have word in line. Get or create its node and stick it at
938 * the end of the targets list
940 if ((specType == Not) && (*line != '\0')) {
941 if (Dir_HasWildcards(line)) {
943 * Targets are to be sought only in the current directory,
944 * so create an empty path for the thing. Note we need to
945 * use Dir_Destroy in the destruction of the path as the
946 * Dir module could have added a directory to the path...
948 Lst emptyPath = Lst_Init(FALSE);
950 Dir_Expand(line, emptyPath, curTargs);
952 Lst_Destroy(emptyPath, Dir_Destroy);
955 * No wildcards, but we want to avoid code duplication,
956 * so create a list with the word on it.
958 (void)Lst_AtEnd(curTargs, (ClientData)line);
961 while(!Lst_IsEmpty(curTargs)) {
962 char *targName = (char *)Lst_DeQueue(curTargs);
964 if (!Suff_IsTransform (targName)) {
965 gn = Targ_FindNode (targName, TARG_CREATE);
967 gn = Suff_AddTransform (targName);
970 (void)Lst_AtEnd (targets, (ClientData)gn);
972 } else if (specType == ExPath && *line != '.' && *line != '\0') {
973 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
978 * If it is a special type and not .PATH, it's the only target we
979 * allow on this line...
981 if (specType != Not && specType != ExPath) {
982 Boolean warn = FALSE;
984 while ((*cp != '!') && (*cp != ':') && *cp) {
985 if (*cp != ' ' && *cp != '\t') {
991 Parse_Error(PARSE_WARNING, "Extra target ignored");
994 while (*cp && isspace (*cp)) {
999 } while ((*line != '!') && (*line != ':') && *line);
1002 * Don't need the list of target names anymore...
1004 Lst_Destroy(curTargs, NOFREE);
1006 if (!Lst_IsEmpty(targets)) {
1009 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
1016 * These four create nodes on which to hang commands, so
1017 * targets shouldn't be empty...
1021 * Nothing special here -- targets can be empty if it wants.
1028 * Have now parsed all the target names. Must parse the operator next. The
1029 * result is left in op .
1033 } else if (*cp == ':') {
1041 Parse_Error (PARSE_FATAL, "Missing dependency operator");
1045 cp++; /* Advance beyond operator */
1047 Lst_ForEach (targets, ParseDoOp, (ClientData)&op);
1050 * Get to the first source
1052 while (*cp && isspace (*cp)) {
1058 * Several special targets take different actions if present with no
1060 * a .SUFFIXES line with no sources clears out all old suffixes
1061 * a .PRECIOUS line makes all targets precious
1062 * a .IGNORE line ignores errors for all targets
1063 * a .SILENT line creates silence when making all targets
1064 * a .PATH removes all directories from the search path(s).
1069 Suff_ClearSuffixes ();
1075 ignoreErrors = TRUE;
1081 Lst_ForEach(paths, ParseClearPath, (ClientData)NULL);
1085 Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1091 } else if (specType == MFlags) {
1093 * Call on functions in main.c to deal with these arguments and
1094 * set the initial character to a null-character so the loop to
1095 * get sources won't get anything
1097 Main_ParseArgLine (line);
1099 } else if (specType == ExShell) {
1100 if (Job_ParseShell (line) != SUCCESS) {
1101 Parse_Error (PARSE_FATAL, "improper shell specification");
1105 } else if ((specType == NotParallel) || (specType == SingleShell)) {
1110 * NOW GO FOR THE SOURCES
1112 if ((specType == Suffixes) || (specType == ExPath) ||
1113 (specType == Includes) || (specType == Libs) ||
1118 * If the target was one that doesn't take files as its sources
1119 * but takes something like suffixes, we take each
1120 * space-separated word on the line as a something and deal
1121 * with it accordingly.
1123 * If the target was .SUFFIXES, we take each source as a
1124 * suffix and add it to the list of suffixes maintained by the
1127 * If the target was a .PATH, we add the source as a directory
1128 * to search on the search path.
1130 * If it was .INCLUDES, the source is taken to be the suffix of
1131 * files which will be #included and whose search path should
1132 * be present in the .INCLUDES variable.
1134 * If it was .LIBS, the source is taken to be the suffix of
1135 * files which are considered libraries and whose search path
1136 * should be present in the .LIBS variable.
1138 * If it was .NULL, the source is the suffix to use when a file
1139 * has no valid suffix.
1142 while (*cp && !isspace (*cp)) {
1149 Suff_AddSuffix (line);
1152 Lst_ForEach(paths, ParseAddDir, (ClientData)line);
1155 Suff_AddInclude (line);
1161 Suff_SetNull (line);
1167 if (savec != '\0') {
1170 while (*cp && isspace (*cp)) {
1176 Lst_Destroy(paths, NOFREE);
1181 * The targets take real sources, so we must beware of archive
1182 * specifications (i.e. things with left parentheses in them)
1183 * and handle them accordingly.
1185 while (*cp && !isspace (*cp)) {
1186 if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) {
1188 * Only stop for a left parenthesis if it isn't at the
1189 * start of a word (that'll be for variable changes
1190 * later) and isn't preceded by a dollar sign (a dynamic
1202 sources = Lst_Init (FALSE);
1203 if (Arch_ParseArchive (&line, sources, VAR_CMD) != SUCCESS) {
1204 Parse_Error (PARSE_FATAL,
1205 "Error in source archive spec \"%s\"", line);
1209 while (!Lst_IsEmpty (sources)) {
1210 gn = (GNode *) Lst_DeQueue (sources);
1211 ParseDoSrc (tOp, gn->name, curSrcs);
1213 Lst_Destroy (sources, NOFREE);
1221 ParseDoSrc (tOp, line, curSrcs);
1223 while (*cp && isspace (*cp)) {
1230 if (mainNode == NILGNODE) {
1232 * If we have yet to decide on a main target to make, in the
1233 * absence of any user input, we want the first target on
1234 * the first dependency line that is actually a real target
1235 * (i.e. isn't a .USE or .EXEC rule) to be made.
1237 Lst_ForEach (targets, ParseFindMain, (ClientData)0);
1241 * Finally, destroy the list of sources
1243 Lst_Destroy(curSrcs, NOFREE);
1247 *---------------------------------------------------------------------
1249 * Return TRUE if the passed line is a variable assignment. A variable
1250 * assignment consists of a single word followed by optional whitespace
1251 * followed by either a += or an = operator.
1252 * This function is used both by the Parse_File function and main when
1253 * parsing the command-line arguments.
1256 * TRUE if it is. FALSE if it ain't
1260 *---------------------------------------------------------------------
1264 register char *line; /* the line to check */
1266 register Boolean wasSpace = FALSE; /* set TRUE if found a space */
1267 register Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1269 #define ISEQOPERATOR(c) \
1270 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1273 * Skip to variable name
1275 for (;(*line == ' ') || (*line == '\t'); line++)
1278 for (; *line != '=' || level != 0; line++)
1282 * end-of-line -- can't be a variable assignment.
1289 * there can be as much white space as desired so long as there is
1290 * only one word before the operator
1306 if (wasSpace && haveName) {
1307 if (ISEQOPERATOR(*line)) {
1309 * We must have a finished word
1315 * When an = operator [+?!:] is found, the next
1316 * character must be an = or it ain't a valid
1323 * This is a shell command
1325 if (strncmp(line, ":sh", 3) == 0)
1330 * This is the start of another word, so not assignment.
1345 *---------------------------------------------------------------------
1347 * Take the variable assignment in the passed line and do it in the
1350 * Note: There is a lexical ambiguity with assignment modifier characters
1351 * in variable names. This routine interprets the character before the =
1352 * as a modifier. Therefore, an assignment like
1354 * is interpreted as "C+ +=" instead of "C++ =".
1360 * the variable structure of the given variable name is altered in the
1362 *---------------------------------------------------------------------
1365 Parse_DoVar (line, ctxt)
1366 char *line; /* a line guaranteed to be a variable
1367 * assignment. This reduces error checks */
1368 GNode *ctxt; /* Context in which to do the assignment */
1370 char *cp; /* pointer into line */
1372 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1373 } type; /* Type of assignment */
1374 char *opc; /* ptr to operator character to
1375 * null-terminate the variable name */
1377 * Avoid clobbered variable warnings by forcing the compiler
1378 * to ``unregister'' variables
1386 * Skip to variable name
1388 while ((*line == ' ') || (*line == '\t')) {
1393 * Skip to operator character, nulling out whitespace as we go
1395 for (cp = line + 1; *cp != '='; cp++) {
1396 if (isspace (*cp)) {
1400 opc = cp-1; /* operator is the previous character */
1401 *cp++ = '\0'; /* nuke the = */
1404 * Check operator type
1414 * If the variable already has a value, we don't do anything.
1417 if (Var_Exists(line, ctxt)) {
1442 if (strncmp(opc, ":sh", 3) == 0) {
1452 while (isspace (*cp)) {
1456 if (type == VAR_APPEND) {
1457 Var_Append (line, cp, ctxt);
1458 } else if (type == VAR_SUBST) {
1460 * Allow variables in the old value to be undefined, but leave their
1461 * invocation alone -- this is done by forcing oldVars to be false.
1462 * XXX: This can cause recursive variables, but that's not hard to do,
1463 * and this allows someone to do something like
1465 * CFLAGS = $(.INCLUDES)
1466 * CFLAGS := -I.. $(CFLAGS)
1468 * And not get an error.
1470 Boolean oldOldVars = oldVars;
1473 cp = Var_Subst(NULL, cp, ctxt, FALSE);
1474 oldVars = oldOldVars;
1476 Var_Set(line, cp, ctxt);
1478 } else if (type == VAR_SHELL) {
1479 Boolean freeCmd = FALSE; /* TRUE if the command needs to be freed, i.e.
1480 * if any variable expansion was performed */
1483 if (strchr(cp, '$') != NULL) {
1485 * There's a dollar sign in the command, so perform variable
1486 * expansion on the whole thing. The resulting string will need
1487 * freeing when we're done, so set freeCmd to TRUE.
1489 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1493 res = Cmd_Exec(cp, &err);
1494 Var_Set(line, res, ctxt);
1498 Parse_Error(PARSE_WARNING, err, cp);
1504 * Normal assignment -- just do it.
1506 Var_Set(line, cp, ctxt);
1513 * Lst_ForEach function to add a command line to all targets
1519 * A new element is added to the commands list of the node.
1522 ParseAddCmd(gnp, cmd)
1523 ClientData gnp; /* the node to which the command is to be added */
1524 ClientData cmd; /* the command to add */
1526 GNode *gn = (GNode *) gnp;
1527 /* if target already supplied, ignore commands */
1528 if (!(gn->type & OP_HAS_COMMANDS))
1529 (void)Lst_AtEnd(gn->commands, cmd);
1534 *-----------------------------------------------------------------------
1535 * ParseHasCommands --
1536 * Callback procedure for Parse_File when destroying the list of
1537 * targets on the last dependency line. Marks a target as already
1538 * having commands if it does, to keep from having shell commands
1539 * on multiple dependency lines.
1545 * OP_HAS_COMMANDS may be set for the target.
1547 *-----------------------------------------------------------------------
1550 ParseHasCommands(gnp)
1551 ClientData gnp; /* Node to examine */
1553 GNode *gn = (GNode *) gnp;
1554 if (!Lst_IsEmpty(gn->commands)) {
1555 gn->type |= OP_HAS_COMMANDS;
1560 *-----------------------------------------------------------------------
1561 * Parse_AddIncludeDir --
1562 * Add a directory to the path searched for included makefiles
1563 * bracketed by double-quotes. Used by functions in main.c
1569 * The directory is appended to the list.
1571 *-----------------------------------------------------------------------
1574 Parse_AddIncludeDir (dir)
1575 char *dir; /* The name of the directory to add */
1577 Dir_AddDir (parseIncPath, dir);
1580 /*---------------------------------------------------------------------
1582 * Handle error directive
1584 * The input is the line minus the ".error". We substitute variables,
1585 * print the message and exit(1) or just print a warning if the ".error"
1586 * directive is malformed.
1588 *---------------------------------------------------------------------
1591 ParseDoError(errmsg)
1592 char *errmsg; /* error message */
1594 if (!isspace(*errmsg)) {
1595 Parse_Error(PARSE_WARNING, "invalid syntax: .error%s", errmsg);
1599 while (isspace(*errmsg))
1602 errmsg = Var_Subst(NULL, errmsg, VAR_GLOBAL, FALSE);
1604 /* use fprintf/exit instead of Parse_Error to terminate immediately */
1605 fprintf(stderr, "\"%s\", line %d: %s\n", fname, lineno, errmsg);
1610 *---------------------------------------------------------------------
1612 * Push to another file.
1614 * The input is the line minus the #include. A file spec is a string
1615 * enclosed in <> or "". The former is looked for only in sysIncPath.
1616 * The latter in . and the directories specified by -I command line
1623 * A structure is added to the includes Lst and readProc, lineno,
1624 * fname and curFILE are altered for the new file
1625 *---------------------------------------------------------------------
1628 ParseDoInclude (file)
1629 char *file; /* file specification */
1631 char *fullname; /* full pathname of file */
1632 IFile *oldFile; /* state associated with current file */
1633 char endc; /* the character which ends the file spec */
1634 char *cp; /* current position in file spec */
1635 Boolean isSystem; /* TRUE if makefile is a system makefile */
1638 * Skip to delimiter character so we know where to look
1640 while ((*file == ' ') || (*file == '\t')) {
1644 if ((*file != '"') && (*file != '<')) {
1645 Parse_Error (PARSE_FATAL,
1646 ".include filename must be delimited by '\"' or '<'");
1651 * Set the search path on which to find the include file based on the
1652 * characters which bracket its name. Angle-brackets imply it's
1653 * a system Makefile while double-quotes imply it's a user makefile
1664 * Skip to matching delimiter
1666 for (cp = ++file; *cp && *cp != endc; cp++) {
1671 Parse_Error (PARSE_FATAL,
1672 "Unclosed %cinclude filename. '%c' expected",
1679 * Substitute for any variables in the file name before trying to
1682 file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1685 * Now we know the file's name and its search path, we attempt to
1686 * find the durn thing. A return of NULL indicates the file don't
1691 * Include files contained in double-quotes are first searched for
1692 * relative to the including file's location. We don't want to
1693 * cd there, of course, so we just tack on the old file's
1694 * leading path components and call Dir_FindFile to see if
1695 * we can locate the beast.
1697 char *prefEnd, *Fname;
1699 /* Make a temporary copy of this, to be safe. */
1700 Fname = estrdup(fname);
1702 prefEnd = strrchr (Fname, '/');
1703 if (prefEnd != (char *)NULL) {
1708 newName = estrdup(file);
1710 newName = str_concat (Fname, file, STR_ADDSLASH);
1711 fullname = Dir_FindFile (newName, parseIncPath);
1712 if (fullname == (char *)NULL) {
1713 fullname = Dir_FindFile(newName, dirSearchPath);
1718 fullname = (char *)NULL;
1722 fullname = (char *)NULL;
1725 if (fullname == (char *)NULL) {
1727 * System makefile or makefile wasn't found in same directory as
1728 * included makefile. Search for it first on the -I search path,
1729 * then on the .PATH search path, if not found in a -I directory.
1730 * XXX: Suffix specific?
1732 fullname = Dir_FindFile (file, parseIncPath);
1733 if (fullname == (char *)NULL) {
1734 fullname = Dir_FindFile(file, dirSearchPath);
1738 if (fullname == (char *)NULL) {
1740 * Still haven't found the makefile. Look for it on the system
1741 * path as a last resort.
1743 fullname = Dir_FindFile(file, sysIncPath);
1746 if (fullname == (char *) NULL) {
1748 Parse_Error (PARSE_FATAL, "Could not find %s", file);
1755 * Once we find the absolute path to the file, we get to save all the
1756 * state from the current file before we can start reading this
1757 * include file. The state is stored in an IFile structure which
1758 * is placed on a list with other IFile structures. The list makes
1759 * a very nice stack to track how we got here...
1761 oldFile = (IFile *) emalloc (sizeof (IFile));
1762 oldFile->fname = fname;
1764 oldFile->F = curFILE;
1765 oldFile->p = curPTR;
1766 oldFile->lineno = lineno;
1768 (void) Lst_AtFront (includes, (ClientData)oldFile);
1771 * Once the previous state has been saved, we can get down to reading
1772 * the new file. We set up the name of the file to be the absolute
1773 * name of the include file so error messages refer to the right
1774 * place. Naturally enough, we start reading at line number 0.
1779 curFILE = fopen (fullname, "r");
1781 if (curFILE == (FILE * ) NULL) {
1782 Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1784 * Pop to previous file
1793 *---------------------------------------------------------------------
1794 * Parse_FromString --
1795 * Start Parsing from the given string
1801 * A structure is added to the includes Lst and readProc, lineno,
1802 * fname and curFILE are altered for the new file
1803 *---------------------------------------------------------------------
1806 Parse_FromString(str)
1809 IFile *oldFile; /* state associated with this file */
1812 (void) fprintf(stderr, "%s\n----\n", str);
1814 oldFile = (IFile *) emalloc (sizeof (IFile));
1815 oldFile->lineno = lineno;
1816 oldFile->fname = fname;
1817 oldFile->F = curFILE;
1818 oldFile->p = curPTR;
1820 (void) Lst_AtFront (includes, (ClientData)oldFile);
1823 curPTR = (PTR *) emalloc (sizeof (PTR));
1824 curPTR->str = curPTR->ptr = str;
1826 fname = estrdup(fname);
1832 *---------------------------------------------------------------------
1833 * ParseTraditionalInclude --
1834 * Push to another file.
1836 * The input is the line minus the "include". The file name is
1837 * the string following the "include".
1843 * A structure is added to the includes Lst and readProc, lineno,
1844 * fname and curFILE are altered for the new file
1845 *---------------------------------------------------------------------
1848 ParseTraditionalInclude (file)
1849 char *file; /* file specification */
1851 char *fullname; /* full pathname of file */
1852 IFile *oldFile; /* state associated with current file */
1853 char *cp; /* current position in file spec */
1857 * Skip over whitespace
1859 while ((*file == ' ') || (*file == '\t')) {
1863 if (*file == '\0') {
1864 Parse_Error (PARSE_FATAL,
1865 "Filename missing from \"include\"");
1870 * Skip to end of line or next whitespace
1872 for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1879 * Substitute for any variables in the file name before trying to
1882 file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1885 * Now we know the file's name, we attempt to find the durn thing.
1886 * A return of NULL indicates the file don't exist.
1888 * Include files are first searched for relative to the including
1889 * file's location. We don't want to cd there, of course, so we
1890 * just tack on the old file's leading path components and call
1891 * Dir_FindFile to see if we can locate the beast.
1892 * XXX - this *does* search in the current directory, right?
1895 prefEnd = strrchr (fname, '/');
1896 if (prefEnd != (char *)NULL) {
1900 newName = str_concat (fname, file, STR_ADDSLASH);
1901 fullname = Dir_FindFile (newName, parseIncPath);
1902 if (fullname == (char *)NULL) {
1903 fullname = Dir_FindFile(newName, dirSearchPath);
1908 fullname = (char *)NULL;
1911 if (fullname == (char *)NULL) {
1913 * System makefile or makefile wasn't found in same directory as
1914 * included makefile. Search for it first on the -I search path,
1915 * then on the .PATH search path, if not found in a -I directory.
1916 * XXX: Suffix specific?
1918 fullname = Dir_FindFile (file, parseIncPath);
1919 if (fullname == (char *)NULL) {
1920 fullname = Dir_FindFile(file, dirSearchPath);
1924 if (fullname == (char *)NULL) {
1926 * Still haven't found the makefile. Look for it on the system
1927 * path as a last resort.
1929 fullname = Dir_FindFile(file, sysIncPath);
1932 if (fullname == (char *) NULL) {
1933 Parse_Error (PARSE_FATAL, "Could not find %s", file);
1938 * Once we find the absolute path to the file, we get to save all the
1939 * state from the current file before we can start reading this
1940 * include file. The state is stored in an IFile structure which
1941 * is placed on a list with other IFile structures. The list makes
1942 * a very nice stack to track how we got here...
1944 oldFile = (IFile *) emalloc (sizeof (IFile));
1945 oldFile->fname = fname;
1947 oldFile->F = curFILE;
1948 oldFile->p = curPTR;
1949 oldFile->lineno = lineno;
1951 (void) Lst_AtFront (includes, (ClientData)oldFile);
1954 * Once the previous state has been saved, we can get down to reading
1955 * the new file. We set up the name of the file to be the absolute
1956 * name of the include file so error messages refer to the right
1957 * place. Naturally enough, we start reading at line number 0.
1962 curFILE = fopen (fullname, "r");
1964 if (curFILE == (FILE * ) NULL) {
1965 Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1967 * Pop to previous file
1975 *---------------------------------------------------------------------
1977 * Called when EOF is reached in the current file. If we were reading
1978 * an include file, the includes stack is popped and things set up
1979 * to go back to reading the previous file at the previous location.
1982 * CONTINUE if there's more to do. DONE if not.
1985 * The old curFILE, is closed. The includes list is shortened.
1986 * lineno, curFILE, and fname are changed if CONTINUE is returned.
1987 *---------------------------------------------------------------------
1993 IFile *ifile; /* the state on the top of the includes stack */
1995 if (Lst_IsEmpty (includes)) {
1999 ifile = (IFile *) Lst_DeQueue (includes);
2000 free ((Address) fname);
2001 fname = ifile->fname;
2002 lineno = ifile->lineno;
2003 if (opened && curFILE)
2004 (void) fclose (curFILE);
2006 free((Address) curPTR->str);
2007 free((Address) curPTR);
2011 free ((Address)ifile);
2016 *---------------------------------------------------------------------
2018 * Read a character from the current file
2021 * The character that was read
2024 *---------------------------------------------------------------------
2030 return fgetc(curFILE);
2032 if (curPTR && *curPTR->ptr)
2033 return *curPTR->ptr++;
2039 *---------------------------------------------------------------------
2041 * Put back a character to the current file
2047 *---------------------------------------------------------------------
2058 *--(curPTR->ptr) = c;
2065 * Grab the next line
2069 int skip; /* Skip lines that don't start with . */
2072 int c, lastc, lineLength = 0;
2075 buf = Buf_Init(MAKE_BSIZE);
2078 Buf_Discard(buf, lineLength);
2081 while (((c = ParseReadc()) != '\n' || lastc == '\\')
2084 Buf_ReplaceLastByte(buf, (Byte)' ');
2087 while ((c = ParseReadc()) == ' ' || c == '\t');
2093 Buf_AddByte(buf, (Byte)c);
2098 Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
2099 Buf_Destroy(buf, TRUE);
2100 return((char *)NULL);
2104 Buf_AddByte(buf, (Byte)'\0');
2105 line = (char *)Buf_GetAll(buf, &lineLength);
2106 } while (skip == 1 && line[0] != '.');
2108 Buf_Destroy(buf, FALSE);
2114 *---------------------------------------------------------------------
2116 * Read an entire line from the input file. Called only by Parse_File.
2117 * To facilitate escaped newlines and what have you, a character is
2118 * buffered in 'lastc', which is '\0' when no characters have been
2119 * read. When we break out of the loop, c holds the terminating
2120 * character and lastc holds a character that should be added to
2121 * the line (unless we don't read anything but a terminator).
2124 * A line w/o its newline
2127 * Only those associated with reading a character
2128 *---------------------------------------------------------------------
2133 Buffer buf; /* Buffer for current line */
2134 register int c; /* the current character */
2135 register int lastc; /* The most-recent character */
2136 Boolean semiNL; /* treat semi-colons as newlines */
2137 Boolean ignDepOp; /* TRUE if should ignore dependency operators
2138 * for the purposes of setting semiNL */
2139 Boolean ignComment; /* TRUE if should ignore comments (in a
2141 char *line; /* Result */
2142 char *ep; /* to strip trailing blanks */
2143 int lineLength; /* Length of result */
2150 * Handle special-characters at the beginning of the line. Either a
2151 * leading tab (shell command) or pound-sign (possible conditional)
2152 * forces us to ignore comments and dependency operators and treat
2153 * semi-colons as semi-colons (by leaving semiNL FALSE). This also
2154 * discards completely blank lines.
2160 ignComment = ignDepOp = TRUE;
2162 } else if (c == '\n') {
2164 } else if (c == '#') {
2169 * Anything else breaks out without doing anything
2177 buf = Buf_Init(MAKE_BSIZE);
2179 while (((c = ParseReadc ()) != '\n' || (lastc == '\\')) &&
2186 * Escaped newline: read characters until a non-space or an
2187 * unescaped newline and replace them all by a single space.
2188 * This is done by storing the space over the backslash and
2189 * dropping through with the next nonspace. If it is a
2190 * semi-colon and semiNL is TRUE, it will be recognized as a
2191 * newline in the code below this...
2195 while ((c = ParseReadc ()) == ' ' || c == '\t') {
2198 if (c == EOF || c == '\n') {
2202 * Check for comments, semiNL's, etc. -- easier than
2203 * ParseUnreadc(c); continue;
2212 * Semi-colon: Need to see if it should be interpreted as a
2217 * To make sure the command that may be following this
2218 * semi-colon begins with a tab, we push one back into the
2219 * input stream. This will overwrite the semi-colon in the
2220 * buffer. If there is no command following, this does no
2221 * harm, since the newline remains in the buffer and the
2222 * whole line is ignored.
2231 * Haven't seen a dependency operator before this, so this
2232 * must be a variable assignment -- don't pay attention to
2233 * dependency operators after this.
2236 } else if (lastc == ':' || lastc == '!') {
2238 * Well, we've seen a dependency operator already, but it
2239 * was the previous character, so this is really just an
2240 * expanded variable assignment. Revert semi-colons to
2241 * being just semi-colons again and ignore any more
2242 * dependency operators.
2244 * XXX: Note that a line like "foo : a:=b" will blow up,
2245 * but who'd write a line like that anyway?
2247 ignDepOp = TRUE; semiNL = FALSE;
2258 * If the character is a hash mark and it isn't escaped
2259 * (or we're being compatible), the thing is a comment.
2260 * Skip to the end of the line.
2264 } while ((c != '\n') && (c != EOF));
2268 * Don't add the backslash. Just let the # get copied
2278 if (!ignDepOp && (c == ':' || c == '!')) {
2280 * A semi-colon is recognized as a newline only on
2281 * dependency lines. Dependency lines are lines with a
2282 * colon or an exclamation point. Ergo...
2289 * Copy in the previous character and save this one in lastc.
2291 Buf_AddByte (buf, (Byte)lastc);
2298 if (lastc != '\0') {
2299 Buf_AddByte (buf, (Byte)lastc);
2301 Buf_AddByte (buf, (Byte)'\0');
2302 line = (char *)Buf_GetAll (buf, &lineLength);
2303 Buf_Destroy (buf, FALSE);
2306 * Strip trailing blanks and tabs from the line.
2307 * Do not strip a blank or tab that is preceeded by
2313 while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
2314 if (ep > line + 1 && ep[-2] == '\\')
2320 if (line[0] == '.') {
2322 * The line might be a conditional. Ask the conditional module
2323 * about it and act accordingly
2325 switch (Cond_Eval (line)) {
2328 * Skip to next conditional that evaluates to COND_PARSE.
2332 line = ParseSkipLine(1);
2333 } while (line && Cond_Eval(line) != COND_PARSE);
2338 free ((Address) line);
2339 line = ParseReadLine();
2342 if (For_Eval(line)) {
2347 * Skip after the matching end
2349 line = ParseSkipLine(0);
2351 Parse_Error (PARSE_FATAL,
2352 "Unexpected end of file in for loop.\n");
2355 ok = For_Eval(line);
2361 line = ParseReadLine();
2370 * Hit end-of-file, so return a NULL line to indicate this.
2372 return((char *)NULL);
2377 *-----------------------------------------------------------------------
2378 * ParseFinishLine --
2379 * Handle the end of a dependency group.
2385 * inLine set FALSE. 'targets' list destroyed.
2387 *-----------------------------------------------------------------------
2393 Lst_ForEach(targets, Suff_EndTransform, (ClientData)NULL);
2394 Lst_Destroy (targets, ParseHasCommands);
2402 *---------------------------------------------------------------------
2404 * Parse a file into its component parts, incorporating it into the
2405 * current dependency graph. This is the main function and controls
2406 * almost every other function in this module
2412 * Loads. Nodes are added to the list of all targets, nodes and links
2413 * are added to the dependency graph. etc. etc. etc.
2414 *---------------------------------------------------------------------
2417 Parse_File(name, stream)
2418 char *name; /* the name of the file being read */
2419 FILE * stream; /* Stream open to makefile to parse */
2421 register char *cp, /* pointer into the line */
2422 *line; /* the line we're working on */
2431 while ((line = ParseReadLine ()) != NULL) {
2434 * Lines that begin with the special character are either
2435 * include or undef directives.
2437 for (cp = line + 1; isspace (*cp); cp++) {
2440 if (strncmp (cp, "include", 7) == 0) {
2441 ParseDoInclude (cp + 7);
2443 } else if (strncmp (cp, "error", 5) == 0) {
2444 ParseDoError(cp + 5);
2446 } else if (strncmp(cp, "undef", 5) == 0) {
2448 for (cp += 5; isspace((unsigned char) *cp); cp++) {
2452 for (cp2 = cp; !isspace((unsigned char) *cp2) &&
2453 (*cp2 != '\0'); cp2++) {
2459 Var_Delete(cp, VAR_GLOBAL);
2464 /* If we're this far, the line must be a comment. */
2468 if (*line == '\t') {
2470 * If a line starts with a tab, it can only hope to be
2471 * a creation command.
2476 for (cp = line + 1; isspace (*cp); cp++) {
2482 * So long as it's not a blank line and we're actually
2483 * in a dependency spec, add the command to the list of
2484 * commands of all targets in the dependency spec
2486 Lst_ForEach (targets, ParseAddCmd, cp);
2487 Lst_AtEnd(targCmds, (ClientData) line);
2490 Parse_Error (PARSE_FATAL,
2491 "Unassociated shell command \"%s\"",
2496 } else if (strncmp (line, "include", 7) == 0 &&
2497 isspace((unsigned char) line[7]) &&
2498 strchr(line, ':') == NULL) {
2500 * It's an S3/S5-style "include".
2502 ParseTraditionalInclude (line + 7);
2505 } else if (Parse_IsVar (line)) {
2507 Parse_DoVar (line, VAR_GLOBAL);
2510 * We now know it's a dependency line so it needs to have all
2511 * variables expanded before being parsed. Tell the variable
2512 * module to complain if some variable is undefined...
2513 * To make life easier on novices, if the line is indented we
2514 * first make sure the line has a dependency operator in it.
2515 * If it doesn't have an operator and we're in a dependency
2516 * line's script, we assume it's actually a shell command
2517 * and add it to the current list of targets.
2520 Boolean nonSpace = FALSE;
2524 if (isspace((unsigned char) line[0])) {
2525 while ((*cp != '\0') && isspace((unsigned char) *cp)) {
2532 while ((*cp != ':') && (*cp != '!') && (*cp != '\0')) {
2542 Parse_Error (PARSE_WARNING,
2543 "Shell command needs a leading tab");
2545 } else if (nonSpace) {
2546 Parse_Error (PARSE_FATAL, "Missing operator");
2552 cp = Var_Subst (NULL, line, VAR_CMD, TRUE);
2557 * Need a non-circular list for the target nodes
2560 Lst_Destroy(targets, NOFREE);
2562 targets = Lst_Init (FALSE);
2565 ParseDoDependency (line);
2576 * Reached EOF, but it may be just EOF of an include file...
2578 } while (ParseEOF(1) == CONTINUE);
2581 * Make sure conditionals are clean
2586 errx(1, "fatal errors encountered -- cannot continue");
2590 *---------------------------------------------------------------------
2592 * initialize the parsing module
2598 * the parseIncPath list is initialized...
2599 *---------------------------------------------------------------------
2604 mainNode = NILGNODE;
2605 parseIncPath = Lst_Init (FALSE);
2606 sysIncPath = Lst_Init (FALSE);
2607 includes = Lst_Init (FALSE);
2608 targCmds = Lst_Init (FALSE);
2614 Lst_Destroy(targCmds, (void (*)(ClientData)) free);
2616 Lst_Destroy(targets, NOFREE);
2617 Lst_Destroy(sysIncPath, Dir_Destroy);
2618 Lst_Destroy(parseIncPath, Dir_Destroy);
2619 Lst_Destroy(includes, NOFREE); /* Should be empty now */
2624 *-----------------------------------------------------------------------
2626 * Return a Lst of the main target to create for main()'s sake. If
2627 * no such target exists, we Punt with an obnoxious error message.
2630 * A Lst of the single node to create.
2635 *-----------------------------------------------------------------------
2640 Lst listmain; /* result list */
2642 listmain = Lst_Init (FALSE);
2644 if (mainNode == NILGNODE) {
2645 Punt ("no target to make.");
2647 } else if (mainNode->type & OP_DOUBLEDEP) {
2648 (void) Lst_AtEnd (listmain, (ClientData)mainNode);
2649 Lst_Concat(listmain, mainNode->cohorts, LST_CONCNEW);
2652 (void) Lst_AtEnd (listmain, (ClientData)mainNode);