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
41 static char sccsid[] = "@(#)parse.c 8.3 (Berkeley) 3/19/94";
43 static const char rcsid[] =
44 "$FreeBSD: src/usr.bin/make/parse.c,v 1.22.2.1 2002/12/26 14:36:38 ru Exp $";
50 * Functions to parse a makefile.
52 * One function, Parse_Init, must be called before any functions
53 * in this module are used. After that, the function Parse_File is the
54 * main entry point and controls most of the other functions in this
57 * Most important structures are kept in Lsts. Directories for
58 * the #include "..." function are kept in the 'parseIncPath' Lst, while
59 * those for the #include <...> are kept in the 'sysIncPath' Lst. The
60 * targets currently being defined are kept in the 'targets' Lst.
62 * The variables 'fname' and 'lineno' are used to track the name
63 * of the current file and the line number in that file so that error
64 * messages can be more meaningful.
67 * Parse_Init Initialization function which must be
68 * called before anything else in this module
71 * Parse_End Cleanup the module
73 * Parse_File Function used to parse a makefile. It must
74 * be given the name of the file, which should
75 * already have been opened, and a function
76 * to call to read a character from the file.
78 * Parse_IsVar Returns TRUE if the given line is a
79 * variable assignment. Used by MainParseArgs
80 * to determine if an argument is a target
81 * or a variable assignment. Used internally
82 * for pretty much the same thing...
84 * Parse_Error Function called when an error occurs in
85 * parsing. Used by the variable and
86 * conditional modules.
87 * Parse_MainName Returns a Lst of the main target to create.
103 #include "pathnames.h"
106 * These values are returned by ParseEOF to tell Parse_File whether to
107 * CONTINUE parsing, i.e. it had only reached the end of an include file,
112 static Lst targets; /* targets we're working on */
113 static Lst targCmds; /* command lines for targets */
114 static Boolean inLine; /* true if currently in a dependency
115 * line or its commands */
121 static char *fname; /* name of current file (for errors) */
122 static int lineno; /* line number in current file */
123 static FILE *curFILE = NULL; /* current makefile */
125 static PTR *curPTR = NULL; /* current makefile */
127 static int fatals = 0;
129 static GNode *mainNode; /* The main target to create. This is the
130 * first target on the first dependency
131 * line in the first makefile */
133 * Definitions for handling #include specifications
135 typedef struct IFile {
136 char *fname; /* name of previous file */
137 int lineno; /* saved line number */
138 FILE * F; /* the open stream */
139 PTR * p; /* the char pointer */
142 static Lst includes; /* stack of IFiles generated by
144 Lst parseIncPath; /* list of directories for "..." includes */
145 Lst sysIncPath; /* list of directories for <...> includes */
148 * specType contains the SPECial TYPE of the current target. It is
149 * Not if the target is unspecial. If it *is* special, however, the children
150 * are linked as children of the parent but not vice versa. This variable is
151 * set in ParseDoDependency
155 Default, /* .DEFAULT */
157 Ignore, /* .IGNORE */
158 Includes, /* .INCLUDES */
159 Interrupt, /* .INTERRUPT */
161 MFlags, /* .MFLAGS or .MAKEFLAGS */
162 Main, /* .MAIN and we don't have anything user-specified to
164 NoExport, /* .NOEXPORT */
165 Not, /* Not special */
166 NotParallel, /* .NOTPARALELL */
169 Parallel, /* .PARALLEL */
175 Precious, /* .PRECIOUS */
176 ExShell, /* .SHELL */
177 Silent, /* .SILENT */
178 SingleShell, /* .SINGLESHELL */
179 Suffixes, /* .SUFFIXES */
181 Attribute /* Generic attribute */
184 static ParseSpecial specType;
188 * Predecessor node for handling .ORDER. Initialized to NILGNODE when .ORDER
189 * seen, then set to each successive source on the line.
191 static GNode *predecessor;
194 * The parseKeywords table is searched using binary search when deciding
195 * if a target or source is special. The 'spec' field is the ParseSpecial
196 * type of the keyword ("Not" if the keyword isn't special as a target) while
197 * the 'op' field is the operator to apply to the list of targets if the
198 * keyword is used as a source ("0" if the keyword isn't special as a source)
201 char *name; /* Name of keyword */
202 ParseSpecial spec; /* Type when used as a target */
203 int op; /* Operator when used as a source */
204 } parseKeywords[] = {
205 { ".BEGIN", Begin, 0 },
206 { ".DEFAULT", Default, 0 },
208 { ".EXEC", Attribute, OP_EXEC },
209 { ".IGNORE", Ignore, OP_IGNORE },
210 { ".INCLUDES", Includes, 0 },
211 { ".INTERRUPT", Interrupt, 0 },
212 { ".INVISIBLE", Attribute, OP_INVISIBLE },
213 { ".JOIN", Attribute, OP_JOIN },
214 { ".LIBS", Libs, 0 },
215 { ".MAIN", Main, 0 },
216 { ".MAKE", Attribute, OP_MAKE },
217 { ".MAKEFLAGS", MFlags, 0 },
218 { ".MFLAGS", MFlags, 0 },
219 { ".NOTMAIN", Attribute, OP_NOTMAIN },
220 { ".NOTPARALLEL", NotParallel, 0 },
221 { ".NO_PARALLEL", NotParallel, 0 },
222 { ".NULL", Null, 0 },
223 { ".OPTIONAL", Attribute, OP_OPTIONAL },
224 { ".ORDER", Order, 0 },
225 { ".PARALLEL", Parallel, 0 },
226 { ".PATH", ExPath, 0 },
227 { ".PHONY", Phony, OP_PHONY },
229 { ".POSIX", Posix, 0 },
231 { ".PRECIOUS", Precious, OP_PRECIOUS },
232 { ".RECURSIVE", Attribute, OP_MAKE },
233 { ".SHELL", ExShell, 0 },
234 { ".SILENT", Silent, OP_SILENT },
235 { ".SINGLESHELL", SingleShell, 0 },
236 { ".SUFFIXES", Suffixes, 0 },
237 { ".USE", Attribute, OP_USE },
238 { ".WAIT", Wait, 0 },
241 static int ParseFindKeyword __P((char *));
242 static int ParseLinkSrc __P((ClientData, ClientData));
243 static int ParseDoOp __P((ClientData, ClientData));
244 static int ParseAddDep __P((ClientData, ClientData));
245 static void ParseDoSrc __P((int, char *, Lst));
246 static int ParseFindMain __P((ClientData, ClientData));
247 static int ParseAddDir __P((ClientData, ClientData));
248 static int ParseClearPath __P((ClientData, ClientData));
249 static void ParseDoDependency __P((char *));
250 static int ParseAddCmd __P((ClientData, ClientData));
251 static int ParseReadc __P((void));
252 static void ParseUnreadc __P((int));
253 static void ParseHasCommands __P((ClientData));
254 static void ParseDoInclude __P((char *));
255 static void ParseDoError __P((char *));
257 static void ParseTraditionalInclude __P((char *));
259 static int ParseEOF __P((int));
260 static char *ParseReadLine __P((void));
261 static char *ParseSkipLine __P((int));
262 static void ParseFinishLine __P((void));
265 *----------------------------------------------------------------------
266 * ParseFindKeyword --
267 * Look in the table of keywords for one matching the given string.
270 * The index of the keyword, or -1 if it isn't there.
274 *----------------------------------------------------------------------
277 ParseFindKeyword (str)
278 char *str; /* String to find */
286 end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
289 cur = start + ((end - start) / 2);
290 diff = strcmp (str, parseKeywords[cur].name);
294 } else if (diff < 0) {
299 } while (start <= end);
305 * Error message abort function for parsing. Prints out the context
306 * of the error (line number and file) as well as the message with
307 * two optional arguments.
313 * "fatals" is incremented if the level is PARSE_FATAL.
318 Parse_Error(int type, char *fmt, ...)
320 Parse_Error(va_alist)
328 int type; /* Error type (PARSE_WARNING, PARSE_FATAL) */
332 type = va_arg(ap, int);
333 fmt = va_arg(ap, char *);
336 (void)fprintf(stderr, "\"%s\", line %d: ", fname, lineno);
337 if (type == PARSE_WARNING)
338 (void)fprintf(stderr, "warning: ");
339 (void)vfprintf(stderr, fmt, ap);
341 (void)fprintf(stderr, "\n");
342 (void)fflush(stderr);
343 if (type == PARSE_FATAL)
348 *---------------------------------------------------------------------
350 * Link the parent node to its new child. Used in a Lst_ForEach by
351 * ParseDoDependency. If the specType isn't 'Not', the parent
352 * isn't linked as a parent of the child.
358 * New elements are added to the parents list of cgn and the
359 * children list of cgn. the unmade field of pgn is updated
360 * to reflect the additional child.
361 *---------------------------------------------------------------------
364 ParseLinkSrc (pgnp, cgnp)
365 ClientData pgnp; /* The parent node */
366 ClientData cgnp; /* The child node */
368 GNode *pgn = (GNode *) pgnp;
369 GNode *cgn = (GNode *) cgnp;
370 if (Lst_Member (pgn->children, (ClientData)cgn) == NILLNODE) {
371 (void)Lst_AtEnd (pgn->children, (ClientData)cgn);
372 if (specType == Not) {
373 (void)Lst_AtEnd (cgn->parents, (ClientData)pgn);
381 *---------------------------------------------------------------------
383 * Apply the parsed operator to the given target node. Used in a
384 * Lst_ForEach call by ParseDoDependency once all targets have
385 * been found and their operator parsed. If the previous and new
386 * operators are incompatible, a major error is taken.
392 * The type field of the node is altered to reflect any new bits in
394 *---------------------------------------------------------------------
398 ClientData gnp; /* The node to which the operator is to be
400 ClientData opp; /* The operator to apply */
402 GNode *gn = (GNode *) gnp;
403 int op = *(int *) opp;
405 * If the dependency mask of the operator and the node don't match and
406 * the node has actually had an operator applied to it before, and
407 * the operator actually has some dependency information in it, complain.
409 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
410 !OP_NOP(gn->type) && !OP_NOP(op))
412 Parse_Error (PARSE_FATAL, "Inconsistent operator for %s", gn->name);
416 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
418 * If the node was the object of a :: operator, we need to create a
419 * new instance of it for the children and commands on this dependency
420 * line. The new instance is placed on the 'cohorts' list of the
421 * initial one (note the initial one is not on its own cohorts list)
422 * and the new instance is linked to all parents of the initial
425 register GNode *cohort;
428 cohort = Targ_NewGN(gn->name);
430 * Duplicate links to parents so graph traversal is simple. Perhaps
431 * some type bits should be duplicated?
433 * Make the cohort invisible as well to avoid duplicating it into
434 * other variables. True, parents of this target won't tend to do
435 * anything with their local variables, but better safe than
438 Lst_ForEach(gn->parents, ParseLinkSrc, (ClientData)cohort);
439 cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
440 (void)Lst_AtEnd(gn->cohorts, (ClientData)cohort);
443 * Replace the node in the targets list with the new copy
445 ln = Lst_Member(targets, (ClientData)gn);
446 Lst_Replace(ln, (ClientData)cohort);
450 * We don't want to nuke any previous flags (whatever they were) so we
451 * just OR the new operator into the old
459 *---------------------------------------------------------------------
461 * Check if the pair of GNodes given needs to be synchronized.
462 * This has to be when two nodes are on different sides of a
466 * Returns 1 if the two targets need to be ordered, 0 otherwise.
467 * If it returns 1, the search can stop
470 * A dependency can be added between the two nodes.
472 *---------------------------------------------------------------------
479 GNode *p = (GNode *) pp;
480 GNode *s = (GNode *) sp;
482 if (p->order < s->order) {
484 * XXX: This can cause loops, and loops can cause unmade targets,
485 * but checking is tedious, and the debugging output can show the
488 (void)Lst_AtEnd(p->successors, (ClientData)s);
489 (void)Lst_AtEnd(s->preds, (ClientData)p);
498 *---------------------------------------------------------------------
500 * Given the name of a source, figure out if it is an attribute
501 * and apply it to the targets if it is. Else decide if there is
502 * some attribute which should be applied *to* the source because
503 * of some special target and apply it if so. Otherwise, make the
504 * source be a child of the targets in the list 'targets'
510 * Operator bits may be added to the list of targets or to the source.
511 * The targets may have a new source added to their lists of children.
512 *---------------------------------------------------------------------
515 ParseDoSrc (tOp, src, allsrc)
516 int tOp; /* operator (if any) from special targets */
517 char *src; /* name of the source to handle */
518 Lst allsrc; /* List of all sources to wait for */
522 if (*src == '.' && isupper (src[1])) {
523 int keywd = ParseFindKeyword(src);
525 int op = parseKeywords[keywd].op;
527 Lst_ForEach (targets, ParseDoOp, (ClientData)&op);
530 if (parseKeywords[keywd].spec == Wait) {
540 * If we have noted the existence of a .MAIN, it means we need
541 * to add the sources of said target to the list of things
542 * to create. The string 'src' is likely to be free, so we
543 * must make a new copy of it. Note that this will only be
544 * invoked if the user didn't specify a target on the command
545 * line. This is to allow #ifmake's to succeed, or something...
547 (void) Lst_AtEnd (create, (ClientData)estrdup(src));
549 * Add the name to the .TARGETS variable as well, so the user cna
550 * employ that, if desired.
552 Var_Append(".TARGETS", src, VAR_GLOBAL);
557 * Create proper predecessor/successor links between the previous
558 * source and the current one.
560 gn = Targ_FindNode(src, TARG_CREATE);
561 if (predecessor != NILGNODE) {
562 (void)Lst_AtEnd(predecessor->successors, (ClientData)gn);
563 (void)Lst_AtEnd(gn->preds, (ClientData)predecessor);
566 * The current source now becomes the predecessor for the next one.
573 * If the source is not an attribute, we need to find/create
574 * a node for it. After that we can apply any operator to it
575 * from a special target or link it to its parents, as
578 * In the case of a source that was the object of a :: operator,
579 * the attribute is applied to all of its instances (as kept in
580 * the 'cohorts' list of the node) or all the cohorts are linked
581 * to all the targets.
583 gn = Targ_FindNode (src, TARG_CREATE);
587 Lst_ForEach (targets, ParseLinkSrc, (ClientData)gn);
589 if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
590 register GNode *cohort;
593 for (ln=Lst_First(gn->cohorts); ln != NILLNODE; ln = Lst_Succ(ln)){
594 cohort = (GNode *)Lst_Datum(ln);
598 Lst_ForEach(targets, ParseLinkSrc, (ClientData)cohort);
606 (void)Lst_AtEnd(allsrc, (ClientData)gn);
608 Lst_ForEach(allsrc, ParseAddDep, (ClientData)gn);
613 *-----------------------------------------------------------------------
615 * Find a real target in the list and set it to be the main one.
616 * Called by ParseDoDependency when a main target hasn't been found
620 * 0 if main not found yet, 1 if it is.
623 * mainNode is changed and Targ_SetMain is called.
625 *-----------------------------------------------------------------------
628 ParseFindMain(gnp, dummy)
629 ClientData gnp; /* Node to examine */
632 GNode *gn = (GNode *) gnp;
633 if ((gn->type & (OP_NOTMAIN|OP_USE|OP_EXEC|OP_TRANSFORM)) == 0) {
636 return (dummy ? 1 : 1);
638 return (dummy ? 0 : 0);
643 *-----------------------------------------------------------------------
645 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
653 *-----------------------------------------------------------------------
656 ParseAddDir(path, name)
660 Dir_AddDir((Lst) path, (char *) name);
665 *-----------------------------------------------------------------------
667 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
675 *-----------------------------------------------------------------------
678 ParseClearPath(path, dummy)
682 Dir_ClearPath((Lst) path);
683 return(dummy ? 0 : 0);
687 *---------------------------------------------------------------------
688 * ParseDoDependency --
689 * Parse the dependency line in line.
695 * The nodes of the sources are linked as children to the nodes of the
696 * targets. Some nodes may be created.
698 * We parse a dependency line by first extracting words from the line and
699 * finding nodes in the list of all targets with that name. This is done
700 * until a character is encountered which is an operator character. Currently
701 * these are only ! and :. At this point the operator is parsed and the
702 * pointer into the line advanced until the first source is encountered.
703 * The parsed operator is applied to each node in the 'targets' list,
704 * which is where the nodes found for the targets are kept, by means of
705 * the ParseDoOp function.
706 * The sources are read in much the same way as the targets were except
707 * that now they are expanded using the wildcarding scheme of the C-Shell
708 * and all instances of the resulting words in the list of all targets
709 * are found. Each of the resulting nodes is then linked to each of the
710 * targets as one of its children.
711 * Certain targets are handled specially. These are the ones detailed
712 * by the specType variable.
713 * The storing of transformation rules is also taken care of here.
714 * A target is recognized as a transformation rule by calling
715 * Suff_IsTransform. If it is a transformation rule, its node is gotten
716 * from the suffix module via Suff_AddTransform rather than the standard
717 * Targ_FindNode in the target module.
718 *---------------------------------------------------------------------
721 ParseDoDependency (line)
722 char *line; /* the line to parse */
724 char *cp; /* our current position */
725 GNode *gn; /* a general purpose temporary node */
726 int op; /* the operator on the line */
727 char savec; /* a place to save a character */
728 Lst paths; /* List of search paths to alter when parsing
729 * a list of .PATH targets */
730 int tOp; /* operator from special target */
731 Lst sources; /* list of archive source names after
733 Lst curTargs; /* list of target names to be found and added
734 * to the targets list */
735 Lst curSrcs; /* list of sources in order */
743 curTargs = Lst_Init(FALSE);
744 curSrcs = Lst_Init(FALSE);
748 *cp && !isspace ((unsigned char) *cp) && *cp != '(';
753 * Must be a dynamic source (would have been expanded
754 * otherwise), so call the Var module to parse the puppy
755 * so we can safely advance beyond it...There should be
756 * no errors in this, as they would have been discovered
757 * in the initial Var_Subst and we wouldn't be here.
763 result=Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
769 } else if (*cp == '!' || *cp == ':') {
771 * We don't want to end a word on ':' or '!' if there is a
772 * better match later on in the string (greedy matching).
773 * This allows the user to have targets like:
776 * where "fie::fi:fo" and "foo::bar" are the targets. In
777 * real life this is used for perl5 library man pages where
778 * "::" separates an object from its class.
779 * Ie: "File::Spec::Unix". This behaviour is also consistent
780 * with other versions of make.
784 if (*cp == ':' && *p == ':')
787 /* Found the best match already. */
788 if (*p == '\0' || isspace(*p))
791 p += strcspn(p, "!:");
793 /* No better match later on... */
801 * Archives must be handled specially to make sure the OP_ARCHV
802 * flag is set in their 'type' field, for one thing, and because
803 * things like "archive(file1.o file2.o file3.o)" are permissible.
804 * Arch_ParseArchive will set 'line' to be the first non-blank
805 * after the archive-spec. It creates/finds nodes for the members
806 * and places them on the given list, returning SUCCESS if all
807 * went well and FAILURE if there was an error in the
808 * specification. On error, line should remain untouched.
810 if (Arch_ParseArchive (&line, targets, VAR_CMD) != SUCCESS) {
811 Parse_Error (PARSE_FATAL,
812 "Error in archive specification: \"%s\"", line);
822 * Ending a dependency line without an operator is a Bozo
825 Parse_Error (PARSE_FATAL, "Need an operator");
830 * Have a word in line. See if it's a special target and set
831 * specType to match it.
833 if (*line == '.' && isupper (line[1])) {
835 * See if the target is a special target that must have it
836 * or its sources handled specially.
838 int keywd = ParseFindKeyword(line);
840 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
841 Parse_Error(PARSE_FATAL, "Mismatched special targets");
845 specType = parseKeywords[keywd].spec;
846 tOp = parseKeywords[keywd].op;
849 * Certain special targets have special semantics:
850 * .PATH Have to set the dirSearchPath
852 * .MAIN Its sources are only used if
853 * nothing has been specified to
855 * .DEFAULT Need to create a node to hang
856 * commands on, but we don't want
857 * it in the graph, nor do we want
858 * it to be the Main Target, so we
859 * create it, set OP_NOTMAIN and
860 * add it to the list, setting
861 * DEFAULT to the new node for
862 * later use. We claim the node is
863 * A transformation rule to make
864 * life easier later, when we'll
865 * use Make_HandleUse to actually
866 * apply the .DEFAULT commands.
867 * .PHONY The list of targets
870 * .INTERRUPT Are not to be considered the
872 * .NOTPARALLEL Make only one target at a time.
873 * .SINGLESHELL Create a shell for each command.
874 * .ORDER Must set initial predecessor to NIL
879 paths = Lst_Init(FALSE);
881 (void)Lst_AtEnd(paths, (ClientData)dirSearchPath);
884 if (!Lst_IsEmpty(create)) {
891 gn = Targ_FindNode(line, TARG_CREATE);
892 gn->type |= OP_NOTMAIN;
893 (void)Lst_AtEnd(targets, (ClientData)gn);
896 gn = Targ_NewGN(".DEFAULT");
897 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
898 (void)Lst_AtEnd(targets, (ClientData)gn);
912 predecessor = NILGNODE;
917 } else if (strncmp (line, ".PATH", 5) == 0) {
919 * .PATH<suffix> has to be handled specially.
920 * Call on the suffix module to give us a path to
926 path = Suff_GetPath (&line[5]);
927 if (path == NILLST) {
928 Parse_Error (PARSE_FATAL,
929 "Suffix '%s' not defined (yet)",
933 if (paths == (Lst)NULL) {
934 paths = Lst_Init(FALSE);
936 (void)Lst_AtEnd(paths, (ClientData)path);
942 * Have word in line. Get or create its node and stick it at
943 * the end of the targets list
945 if ((specType == Not) && (*line != '\0')) {
946 if (Dir_HasWildcards(line)) {
948 * Targets are to be sought only in the current directory,
949 * so create an empty path for the thing. Note we need to
950 * use Dir_Destroy in the destruction of the path as the
951 * Dir module could have added a directory to the path...
953 Lst emptyPath = Lst_Init(FALSE);
955 Dir_Expand(line, emptyPath, curTargs);
957 Lst_Destroy(emptyPath, Dir_Destroy);
960 * No wildcards, but we want to avoid code duplication,
961 * so create a list with the word on it.
963 (void)Lst_AtEnd(curTargs, (ClientData)line);
966 while(!Lst_IsEmpty(curTargs)) {
967 char *targName = (char *)Lst_DeQueue(curTargs);
969 if (!Suff_IsTransform (targName)) {
970 gn = Targ_FindNode (targName, TARG_CREATE);
972 gn = Suff_AddTransform (targName);
975 (void)Lst_AtEnd (targets, (ClientData)gn);
977 } else if (specType == ExPath && *line != '.' && *line != '\0') {
978 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
983 * If it is a special type and not .PATH, it's the only target we
984 * allow on this line...
986 if (specType != Not && specType != ExPath) {
987 Boolean warn = FALSE;
989 while ((*cp != '!') && (*cp != ':') && *cp) {
990 if (*cp != ' ' && *cp != '\t') {
996 Parse_Error(PARSE_WARNING, "Extra target ignored");
999 while (*cp && isspace (*cp)) {
1004 } while ((*line != '!') && (*line != ':') && *line);
1007 * Don't need the list of target names anymore...
1009 Lst_Destroy(curTargs, NOFREE);
1011 if (!Lst_IsEmpty(targets)) {
1014 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
1021 * These four create nodes on which to hang commands, so
1022 * targets shouldn't be empty...
1026 * Nothing special here -- targets can be empty if it wants.
1033 * Have now parsed all the target names. Must parse the operator next. The
1034 * result is left in op .
1038 } else if (*cp == ':') {
1046 Parse_Error (PARSE_FATAL, "Missing dependency operator");
1050 cp++; /* Advance beyond operator */
1052 Lst_ForEach (targets, ParseDoOp, (ClientData)&op);
1055 * Get to the first source
1057 while (*cp && isspace (*cp)) {
1063 * Several special targets take different actions if present with no
1065 * a .SUFFIXES line with no sources clears out all old suffixes
1066 * a .PRECIOUS line makes all targets precious
1067 * a .IGNORE line ignores errors for all targets
1068 * a .SILENT line creates silence when making all targets
1069 * a .PATH removes all directories from the search path(s).
1074 Suff_ClearSuffixes ();
1080 ignoreErrors = TRUE;
1086 Lst_ForEach(paths, ParseClearPath, (ClientData)NULL);
1090 Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1096 } else if (specType == MFlags) {
1098 * Call on functions in main.c to deal with these arguments and
1099 * set the initial character to a null-character so the loop to
1100 * get sources won't get anything
1102 Main_ParseArgLine (line);
1104 } else if (specType == ExShell) {
1105 if (Job_ParseShell (line) != SUCCESS) {
1106 Parse_Error (PARSE_FATAL, "improper shell specification");
1110 } else if ((specType == NotParallel) || (specType == SingleShell)) {
1115 * NOW GO FOR THE SOURCES
1117 if ((specType == Suffixes) || (specType == ExPath) ||
1118 (specType == Includes) || (specType == Libs) ||
1123 * If the target was one that doesn't take files as its sources
1124 * but takes something like suffixes, we take each
1125 * space-separated word on the line as a something and deal
1126 * with it accordingly.
1128 * If the target was .SUFFIXES, we take each source as a
1129 * suffix and add it to the list of suffixes maintained by the
1132 * If the target was a .PATH, we add the source as a directory
1133 * to search on the search path.
1135 * If it was .INCLUDES, the source is taken to be the suffix of
1136 * files which will be #included and whose search path should
1137 * be present in the .INCLUDES variable.
1139 * If it was .LIBS, the source is taken to be the suffix of
1140 * files which are considered libraries and whose search path
1141 * should be present in the .LIBS variable.
1143 * If it was .NULL, the source is the suffix to use when a file
1144 * has no valid suffix.
1147 while (*cp && !isspace (*cp)) {
1154 Suff_AddSuffix (line);
1157 Lst_ForEach(paths, ParseAddDir, (ClientData)line);
1160 Suff_AddInclude (line);
1166 Suff_SetNull (line);
1172 if (savec != '\0') {
1175 while (*cp && isspace (*cp)) {
1181 Lst_Destroy(paths, NOFREE);
1186 * The targets take real sources, so we must beware of archive
1187 * specifications (i.e. things with left parentheses in them)
1188 * and handle them accordingly.
1190 while (*cp && !isspace (*cp)) {
1191 if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) {
1193 * Only stop for a left parenthesis if it isn't at the
1194 * start of a word (that'll be for variable changes
1195 * later) and isn't preceded by a dollar sign (a dynamic
1207 sources = Lst_Init (FALSE);
1208 if (Arch_ParseArchive (&line, sources, VAR_CMD) != SUCCESS) {
1209 Parse_Error (PARSE_FATAL,
1210 "Error in source archive spec \"%s\"", line);
1214 while (!Lst_IsEmpty (sources)) {
1215 gn = (GNode *) Lst_DeQueue (sources);
1216 ParseDoSrc (tOp, gn->name, curSrcs);
1218 Lst_Destroy (sources, NOFREE);
1226 ParseDoSrc (tOp, line, curSrcs);
1228 while (*cp && isspace (*cp)) {
1235 if (mainNode == NILGNODE) {
1237 * If we have yet to decide on a main target to make, in the
1238 * absence of any user input, we want the first target on
1239 * the first dependency line that is actually a real target
1240 * (i.e. isn't a .USE or .EXEC rule) to be made.
1242 Lst_ForEach (targets, ParseFindMain, (ClientData)0);
1246 * Finally, destroy the list of sources
1248 Lst_Destroy(curSrcs, NOFREE);
1252 *---------------------------------------------------------------------
1254 * Return TRUE if the passed line is a variable assignment. A variable
1255 * assignment consists of a single word followed by optional whitespace
1256 * followed by either a += or an = operator.
1257 * This function is used both by the Parse_File function and main when
1258 * parsing the command-line arguments.
1261 * TRUE if it is. FALSE if it ain't
1265 *---------------------------------------------------------------------
1269 register char *line; /* the line to check */
1271 register Boolean wasSpace = FALSE; /* set TRUE if found a space */
1272 register Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1274 #define ISEQOPERATOR(c) \
1275 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1278 * Skip to variable name
1280 for (;(*line == ' ') || (*line == '\t'); line++)
1283 for (; *line != '=' || level != 0; line++)
1287 * end-of-line -- can't be a variable assignment.
1294 * there can be as much white space as desired so long as there is
1295 * only one word before the operator
1311 if (wasSpace && haveName) {
1312 if (ISEQOPERATOR(*line)) {
1314 * We must have a finished word
1320 * When an = operator [+?!:] is found, the next
1321 * character must be an = or it ain't a valid
1328 * This is a shell command
1330 if (strncmp(line, ":sh", 3) == 0)
1335 * This is the start of another word, so not assignment.
1350 *---------------------------------------------------------------------
1352 * Take the variable assignment in the passed line and do it in the
1355 * Note: There is a lexical ambiguity with assignment modifier characters
1356 * in variable names. This routine interprets the character before the =
1357 * as a modifier. Therefore, an assignment like
1359 * is interpreted as "C+ +=" instead of "C++ =".
1365 * the variable structure of the given variable name is altered in the
1367 *---------------------------------------------------------------------
1370 Parse_DoVar (line, ctxt)
1371 char *line; /* a line guaranteed to be a variable
1372 * assignment. This reduces error checks */
1373 GNode *ctxt; /* Context in which to do the assignment */
1375 char *cp; /* pointer into line */
1377 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1378 } type; /* Type of assignment */
1379 char *opc; /* ptr to operator character to
1380 * null-terminate the variable name */
1382 * Avoid clobbered variable warnings by forcing the compiler
1383 * to ``unregister'' variables
1391 * Skip to variable name
1393 while ((*line == ' ') || (*line == '\t')) {
1398 * Skip to operator character, nulling out whitespace as we go
1400 for (cp = line + 1; *cp != '='; cp++) {
1401 if (isspace (*cp)) {
1405 opc = cp-1; /* operator is the previous character */
1406 *cp++ = '\0'; /* nuke the = */
1409 * Check operator type
1419 * If the variable already has a value, we don't do anything.
1422 if (Var_Exists(line, ctxt)) {
1447 if (strncmp(opc, ":sh", 3) == 0) {
1457 while (isspace (*cp)) {
1461 if (type == VAR_APPEND) {
1462 Var_Append (line, cp, ctxt);
1463 } else if (type == VAR_SUBST) {
1465 * Allow variables in the old value to be undefined, but leave their
1466 * invocation alone -- this is done by forcing oldVars to be false.
1467 * XXX: This can cause recursive variables, but that's not hard to do,
1468 * and this allows someone to do something like
1470 * CFLAGS = $(.INCLUDES)
1471 * CFLAGS := -I.. $(CFLAGS)
1473 * And not get an error.
1475 Boolean oldOldVars = oldVars;
1478 cp = Var_Subst(NULL, cp, ctxt, FALSE);
1479 oldVars = oldOldVars;
1481 Var_Set(line, cp, ctxt);
1483 } else if (type == VAR_SHELL) {
1484 Boolean freeCmd = FALSE; /* TRUE if the command needs to be freed, i.e.
1485 * if any variable expansion was performed */
1488 if (strchr(cp, '$') != NULL) {
1490 * There's a dollar sign in the command, so perform variable
1491 * expansion on the whole thing. The resulting string will need
1492 * freeing when we're done, so set freeCmd to TRUE.
1494 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1498 res = Cmd_Exec(cp, &err);
1499 Var_Set(line, res, ctxt);
1503 Parse_Error(PARSE_WARNING, err, cp);
1509 * Normal assignment -- just do it.
1511 Var_Set(line, cp, ctxt);
1518 * Lst_ForEach function to add a command line to all targets
1524 * A new element is added to the commands list of the node.
1527 ParseAddCmd(gnp, cmd)
1528 ClientData gnp; /* the node to which the command is to be added */
1529 ClientData cmd; /* the command to add */
1531 GNode *gn = (GNode *) gnp;
1532 /* if target already supplied, ignore commands */
1533 if (!(gn->type & OP_HAS_COMMANDS))
1534 (void)Lst_AtEnd(gn->commands, cmd);
1539 *-----------------------------------------------------------------------
1540 * ParseHasCommands --
1541 * Callback procedure for Parse_File when destroying the list of
1542 * targets on the last dependency line. Marks a target as already
1543 * having commands if it does, to keep from having shell commands
1544 * on multiple dependency lines.
1550 * OP_HAS_COMMANDS may be set for the target.
1552 *-----------------------------------------------------------------------
1555 ParseHasCommands(gnp)
1556 ClientData gnp; /* Node to examine */
1558 GNode *gn = (GNode *) gnp;
1559 if (!Lst_IsEmpty(gn->commands)) {
1560 gn->type |= OP_HAS_COMMANDS;
1565 *-----------------------------------------------------------------------
1566 * Parse_AddIncludeDir --
1567 * Add a directory to the path searched for included makefiles
1568 * bracketed by double-quotes. Used by functions in main.c
1574 * The directory is appended to the list.
1576 *-----------------------------------------------------------------------
1579 Parse_AddIncludeDir (dir)
1580 char *dir; /* The name of the directory to add */
1582 Dir_AddDir (parseIncPath, dir);
1585 /*---------------------------------------------------------------------
1587 * Handle error directive
1589 * The input is the line minus the ".error". We substitute variables,
1590 * print the message and exit(1) or just print a warning if the ".error"
1591 * directive is malformed.
1593 *---------------------------------------------------------------------
1596 ParseDoError(errmsg)
1597 char *errmsg; /* error message */
1599 if (!isspace(*errmsg)) {
1600 Parse_Error(PARSE_WARNING, "invalid syntax: .error%s", errmsg);
1604 while (isspace(*errmsg))
1607 errmsg = Var_Subst(NULL, errmsg, VAR_GLOBAL, FALSE);
1609 /* use fprintf/exit instead of Parse_Error to terminate immediately */
1610 fprintf(stderr, "\"%s\", line %d: %s\n", fname, lineno, errmsg);
1615 *---------------------------------------------------------------------
1617 * Push to another file.
1619 * The input is the line minus the #include. A file spec is a string
1620 * enclosed in <> or "". The former is looked for only in sysIncPath.
1621 * The latter in . and the directories specified by -I command line
1628 * A structure is added to the includes Lst and readProc, lineno,
1629 * fname and curFILE are altered for the new file
1630 *---------------------------------------------------------------------
1633 ParseDoInclude (file)
1634 char *file; /* file specification */
1636 char *fullname; /* full pathname of file */
1637 IFile *oldFile; /* state associated with current file */
1638 char endc; /* the character which ends the file spec */
1639 char *cp; /* current position in file spec */
1640 Boolean isSystem; /* TRUE if makefile is a system makefile */
1643 * Skip to delimiter character so we know where to look
1645 while ((*file == ' ') || (*file == '\t')) {
1649 if ((*file != '"') && (*file != '<')) {
1650 Parse_Error (PARSE_FATAL,
1651 ".include filename must be delimited by '\"' or '<'");
1656 * Set the search path on which to find the include file based on the
1657 * characters which bracket its name. Angle-brackets imply it's
1658 * a system Makefile while double-quotes imply it's a user makefile
1669 * Skip to matching delimiter
1671 for (cp = ++file; *cp && *cp != endc; cp++) {
1676 Parse_Error (PARSE_FATAL,
1677 "Unclosed %cinclude filename. '%c' expected",
1684 * Substitute for any variables in the file name before trying to
1687 file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1690 * Now we know the file's name and its search path, we attempt to
1691 * find the durn thing. A return of NULL indicates the file don't
1696 * Include files contained in double-quotes are first searched for
1697 * relative to the including file's location. We don't want to
1698 * cd there, of course, so we just tack on the old file's
1699 * leading path components and call Dir_FindFile to see if
1700 * we can locate the beast.
1702 char *prefEnd, *Fname;
1704 /* Make a temporary copy of this, to be safe. */
1705 Fname = estrdup(fname);
1707 prefEnd = strrchr (Fname, '/');
1708 if (prefEnd != (char *)NULL) {
1713 newName = estrdup(file);
1715 newName = str_concat (Fname, file, STR_ADDSLASH);
1716 fullname = Dir_FindFile (newName, parseIncPath);
1717 if (fullname == (char *)NULL) {
1718 fullname = Dir_FindFile(newName, dirSearchPath);
1723 fullname = (char *)NULL;
1727 fullname = (char *)NULL;
1730 if (fullname == (char *)NULL) {
1732 * System makefile or makefile wasn't found in same directory as
1733 * included makefile. Search for it first on the -I search path,
1734 * then on the .PATH search path, if not found in a -I directory.
1735 * XXX: Suffix specific?
1737 fullname = Dir_FindFile (file, parseIncPath);
1738 if (fullname == (char *)NULL) {
1739 fullname = Dir_FindFile(file, dirSearchPath);
1743 if (fullname == (char *)NULL) {
1745 * Still haven't found the makefile. Look for it on the system
1746 * path as a last resort.
1748 fullname = Dir_FindFile(file, sysIncPath);
1751 if (fullname == (char *) NULL) {
1753 Parse_Error (PARSE_FATAL, "Could not find %s", file);
1760 * Once we find the absolute path to the file, we get to save all the
1761 * state from the current file before we can start reading this
1762 * include file. The state is stored in an IFile structure which
1763 * is placed on a list with other IFile structures. The list makes
1764 * a very nice stack to track how we got here...
1766 oldFile = (IFile *) emalloc (sizeof (IFile));
1767 oldFile->fname = fname;
1769 oldFile->F = curFILE;
1770 oldFile->p = curPTR;
1771 oldFile->lineno = lineno;
1773 (void) Lst_AtFront (includes, (ClientData)oldFile);
1776 * Once the previous state has been saved, we can get down to reading
1777 * the new file. We set up the name of the file to be the absolute
1778 * name of the include file so error messages refer to the right
1779 * place. Naturally enough, we start reading at line number 0.
1784 curFILE = fopen (fullname, "r");
1786 if (curFILE == (FILE * ) NULL) {
1787 Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1789 * Pop to previous file
1798 *---------------------------------------------------------------------
1799 * Parse_FromString --
1800 * Start Parsing from the given string
1806 * A structure is added to the includes Lst and readProc, lineno,
1807 * fname and curFILE are altered for the new file
1808 *---------------------------------------------------------------------
1811 Parse_FromString(str)
1814 IFile *oldFile; /* state associated with this file */
1817 (void) fprintf(stderr, "%s\n----\n", str);
1819 oldFile = (IFile *) emalloc (sizeof (IFile));
1820 oldFile->lineno = lineno;
1821 oldFile->fname = fname;
1822 oldFile->F = curFILE;
1823 oldFile->p = curPTR;
1825 (void) Lst_AtFront (includes, (ClientData)oldFile);
1828 curPTR = (PTR *) emalloc (sizeof (PTR));
1829 curPTR->str = curPTR->ptr = str;
1831 fname = estrdup(fname);
1837 *---------------------------------------------------------------------
1838 * ParseTraditionalInclude --
1839 * Push to another file.
1841 * The input is the line minus the "include". The file name is
1842 * the string following the "include".
1848 * A structure is added to the includes Lst and readProc, lineno,
1849 * fname and curFILE are altered for the new file
1850 *---------------------------------------------------------------------
1853 ParseTraditionalInclude (file)
1854 char *file; /* file specification */
1856 char *fullname; /* full pathname of file */
1857 IFile *oldFile; /* state associated with current file */
1858 char *cp; /* current position in file spec */
1862 * Skip over whitespace
1864 while ((*file == ' ') || (*file == '\t')) {
1868 if (*file == '\0') {
1869 Parse_Error (PARSE_FATAL,
1870 "Filename missing from \"include\"");
1875 * Skip to end of line or next whitespace
1877 for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1884 * Substitute for any variables in the file name before trying to
1887 file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1890 * Now we know the file's name, we attempt to find the durn thing.
1891 * A return of NULL indicates the file don't exist.
1893 * Include files are first searched for relative to the including
1894 * file's location. We don't want to cd there, of course, so we
1895 * just tack on the old file's leading path components and call
1896 * Dir_FindFile to see if we can locate the beast.
1897 * XXX - this *does* search in the current directory, right?
1900 prefEnd = strrchr (fname, '/');
1901 if (prefEnd != (char *)NULL) {
1905 newName = str_concat (fname, file, STR_ADDSLASH);
1906 fullname = Dir_FindFile (newName, parseIncPath);
1907 if (fullname == (char *)NULL) {
1908 fullname = Dir_FindFile(newName, dirSearchPath);
1913 fullname = (char *)NULL;
1916 if (fullname == (char *)NULL) {
1918 * System makefile or makefile wasn't found in same directory as
1919 * included makefile. Search for it first on the -I search path,
1920 * then on the .PATH search path, if not found in a -I directory.
1921 * XXX: Suffix specific?
1923 fullname = Dir_FindFile (file, parseIncPath);
1924 if (fullname == (char *)NULL) {
1925 fullname = Dir_FindFile(file, dirSearchPath);
1929 if (fullname == (char *)NULL) {
1931 * Still haven't found the makefile. Look for it on the system
1932 * path as a last resort.
1934 fullname = Dir_FindFile(file, sysIncPath);
1937 if (fullname == (char *) NULL) {
1938 Parse_Error (PARSE_FATAL, "Could not find %s", file);
1943 * Once we find the absolute path to the file, we get to save all the
1944 * state from the current file before we can start reading this
1945 * include file. The state is stored in an IFile structure which
1946 * is placed on a list with other IFile structures. The list makes
1947 * a very nice stack to track how we got here...
1949 oldFile = (IFile *) emalloc (sizeof (IFile));
1950 oldFile->fname = fname;
1952 oldFile->F = curFILE;
1953 oldFile->p = curPTR;
1954 oldFile->lineno = lineno;
1956 (void) Lst_AtFront (includes, (ClientData)oldFile);
1959 * Once the previous state has been saved, we can get down to reading
1960 * the new file. We set up the name of the file to be the absolute
1961 * name of the include file so error messages refer to the right
1962 * place. Naturally enough, we start reading at line number 0.
1967 curFILE = fopen (fullname, "r");
1969 if (curFILE == (FILE * ) NULL) {
1970 Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1972 * Pop to previous file
1980 *---------------------------------------------------------------------
1982 * Called when EOF is reached in the current file. If we were reading
1983 * an include file, the includes stack is popped and things set up
1984 * to go back to reading the previous file at the previous location.
1987 * CONTINUE if there's more to do. DONE if not.
1990 * The old curFILE, is closed. The includes list is shortened.
1991 * lineno, curFILE, and fname are changed if CONTINUE is returned.
1992 *---------------------------------------------------------------------
1998 IFile *ifile; /* the state on the top of the includes stack */
2000 if (Lst_IsEmpty (includes)) {
2004 ifile = (IFile *) Lst_DeQueue (includes);
2005 free ((Address) fname);
2006 fname = ifile->fname;
2007 lineno = ifile->lineno;
2008 if (opened && curFILE)
2009 (void) fclose (curFILE);
2011 free((Address) curPTR->str);
2012 free((Address) curPTR);
2016 free ((Address)ifile);
2021 *---------------------------------------------------------------------
2023 * Read a character from the current file
2026 * The character that was read
2029 *---------------------------------------------------------------------
2035 return fgetc(curFILE);
2037 if (curPTR && *curPTR->ptr)
2038 return *curPTR->ptr++;
2044 *---------------------------------------------------------------------
2046 * Put back a character to the current file
2052 *---------------------------------------------------------------------
2063 *--(curPTR->ptr) = c;
2070 * Grab the next line
2074 int skip; /* Skip lines that don't start with . */
2077 int c, lastc, lineLength = 0;
2080 buf = Buf_Init(MAKE_BSIZE);
2083 Buf_Discard(buf, lineLength);
2086 while (((c = ParseReadc()) != '\n' || lastc == '\\')
2089 Buf_ReplaceLastByte(buf, (Byte)' ');
2092 while ((c = ParseReadc()) == ' ' || c == '\t');
2098 Buf_AddByte(buf, (Byte)c);
2103 Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
2104 Buf_Destroy(buf, TRUE);
2105 return((char *)NULL);
2109 Buf_AddByte(buf, (Byte)'\0');
2110 line = (char *)Buf_GetAll(buf, &lineLength);
2111 } while (skip == 1 && line[0] != '.');
2113 Buf_Destroy(buf, FALSE);
2119 *---------------------------------------------------------------------
2121 * Read an entire line from the input file. Called only by Parse_File.
2122 * To facilitate escaped newlines and what have you, a character is
2123 * buffered in 'lastc', which is '\0' when no characters have been
2124 * read. When we break out of the loop, c holds the terminating
2125 * character and lastc holds a character that should be added to
2126 * the line (unless we don't read anything but a terminator).
2129 * A line w/o its newline
2132 * Only those associated with reading a character
2133 *---------------------------------------------------------------------
2138 Buffer buf; /* Buffer for current line */
2139 register int c; /* the current character */
2140 register int lastc; /* The most-recent character */
2141 Boolean semiNL; /* treat semi-colons as newlines */
2142 Boolean ignDepOp; /* TRUE if should ignore dependency operators
2143 * for the purposes of setting semiNL */
2144 Boolean ignComment; /* TRUE if should ignore comments (in a
2146 char *line; /* Result */
2147 char *ep; /* to strip trailing blanks */
2148 int lineLength; /* Length of result */
2155 * Handle special-characters at the beginning of the line. Either a
2156 * leading tab (shell command) or pound-sign (possible conditional)
2157 * forces us to ignore comments and dependency operators and treat
2158 * semi-colons as semi-colons (by leaving semiNL FALSE). This also
2159 * discards completely blank lines.
2165 ignComment = ignDepOp = TRUE;
2167 } else if (c == '\n') {
2169 } else if (c == '#') {
2174 * Anything else breaks out without doing anything
2182 buf = Buf_Init(MAKE_BSIZE);
2184 while (((c = ParseReadc ()) != '\n' || (lastc == '\\')) &&
2191 * Escaped newline: read characters until a non-space or an
2192 * unescaped newline and replace them all by a single space.
2193 * This is done by storing the space over the backslash and
2194 * dropping through with the next nonspace. If it is a
2195 * semi-colon and semiNL is TRUE, it will be recognized as a
2196 * newline in the code below this...
2200 while ((c = ParseReadc ()) == ' ' || c == '\t') {
2203 if (c == EOF || c == '\n') {
2207 * Check for comments, semiNL's, etc. -- easier than
2208 * ParseUnreadc(c); continue;
2217 * Semi-colon: Need to see if it should be interpreted as a
2222 * To make sure the command that may be following this
2223 * semi-colon begins with a tab, we push one back into the
2224 * input stream. This will overwrite the semi-colon in the
2225 * buffer. If there is no command following, this does no
2226 * harm, since the newline remains in the buffer and the
2227 * whole line is ignored.
2236 * Haven't seen a dependency operator before this, so this
2237 * must be a variable assignment -- don't pay attention to
2238 * dependency operators after this.
2241 } else if (lastc == ':' || lastc == '!') {
2243 * Well, we've seen a dependency operator already, but it
2244 * was the previous character, so this is really just an
2245 * expanded variable assignment. Revert semi-colons to
2246 * being just semi-colons again and ignore any more
2247 * dependency operators.
2249 * XXX: Note that a line like "foo : a:=b" will blow up,
2250 * but who'd write a line like that anyway?
2252 ignDepOp = TRUE; semiNL = FALSE;
2263 * If the character is a hash mark and it isn't escaped
2264 * (or we're being compatible), the thing is a comment.
2265 * Skip to the end of the line.
2269 } while ((c != '\n') && (c != EOF));
2273 * Don't add the backslash. Just let the # get copied
2283 if (!ignDepOp && (c == ':' || c == '!')) {
2285 * A semi-colon is recognized as a newline only on
2286 * dependency lines. Dependency lines are lines with a
2287 * colon or an exclamation point. Ergo...
2294 * Copy in the previous character and save this one in lastc.
2296 Buf_AddByte (buf, (Byte)lastc);
2303 if (lastc != '\0') {
2304 Buf_AddByte (buf, (Byte)lastc);
2306 Buf_AddByte (buf, (Byte)'\0');
2307 line = (char *)Buf_GetAll (buf, &lineLength);
2308 Buf_Destroy (buf, FALSE);
2311 * Strip trailing blanks and tabs from the line.
2312 * Do not strip a blank or tab that is preceeded by
2318 while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
2319 if (ep > line + 1 && ep[-2] == '\\')
2325 if (line[0] == '.') {
2327 * The line might be a conditional. Ask the conditional module
2328 * about it and act accordingly
2330 switch (Cond_Eval (line)) {
2333 * Skip to next conditional that evaluates to COND_PARSE.
2337 line = ParseSkipLine(1);
2338 } while (line && Cond_Eval(line) != COND_PARSE);
2343 free ((Address) line);
2344 line = ParseReadLine();
2347 if (For_Eval(line)) {
2352 * Skip after the matching end
2354 line = ParseSkipLine(0);
2356 Parse_Error (PARSE_FATAL,
2357 "Unexpected end of file in for loop.\n");
2360 ok = For_Eval(line);
2366 line = ParseReadLine();
2375 * Hit end-of-file, so return a NULL line to indicate this.
2377 return((char *)NULL);
2382 *-----------------------------------------------------------------------
2383 * ParseFinishLine --
2384 * Handle the end of a dependency group.
2390 * inLine set FALSE. 'targets' list destroyed.
2392 *-----------------------------------------------------------------------
2398 Lst_ForEach(targets, Suff_EndTransform, (ClientData)NULL);
2399 Lst_Destroy (targets, ParseHasCommands);
2407 *---------------------------------------------------------------------
2409 * Parse a file into its component parts, incorporating it into the
2410 * current dependency graph. This is the main function and controls
2411 * almost every other function in this module
2417 * Loads. Nodes are added to the list of all targets, nodes and links
2418 * are added to the dependency graph. etc. etc. etc.
2419 *---------------------------------------------------------------------
2422 Parse_File(name, stream)
2423 char *name; /* the name of the file being read */
2424 FILE * stream; /* Stream open to makefile to parse */
2426 register char *cp, /* pointer into the line */
2427 *line; /* the line we're working on */
2436 while ((line = ParseReadLine ()) != NULL) {
2439 * Lines that begin with the special character are either
2440 * include or undef directives.
2442 for (cp = line + 1; isspace (*cp); cp++) {
2445 if (strncmp (cp, "include", 7) == 0) {
2446 ParseDoInclude (cp + 7);
2448 } else if (strncmp (cp, "error", 5) == 0) {
2449 ParseDoError(cp + 5);
2451 } else if (strncmp(cp, "undef", 5) == 0) {
2453 for (cp += 5; isspace((unsigned char) *cp); cp++) {
2457 for (cp2 = cp; !isspace((unsigned char) *cp2) &&
2458 (*cp2 != '\0'); cp2++) {
2464 Var_Delete(cp, VAR_GLOBAL);
2469 /* If we're this far, the line must be a comment. */
2473 if (*line == '\t') {
2475 * If a line starts with a tab, it can only hope to be
2476 * a creation command.
2481 for (cp = line + 1; isspace (*cp); cp++) {
2487 * So long as it's not a blank line and we're actually
2488 * in a dependency spec, add the command to the list of
2489 * commands of all targets in the dependency spec
2491 Lst_ForEach (targets, ParseAddCmd, cp);
2492 Lst_AtEnd(targCmds, (ClientData) line);
2495 Parse_Error (PARSE_FATAL,
2496 "Unassociated shell command \"%s\"",
2501 } else if (strncmp (line, "include", 7) == 0 &&
2502 isspace((unsigned char) line[7]) &&
2503 strchr(line, ':') == NULL) {
2505 * It's an S3/S5-style "include".
2507 ParseTraditionalInclude (line + 7);
2510 } else if (Parse_IsVar (line)) {
2512 Parse_DoVar (line, VAR_GLOBAL);
2515 * We now know it's a dependency line so it needs to have all
2516 * variables expanded before being parsed. Tell the variable
2517 * module to complain if some variable is undefined...
2518 * To make life easier on novices, if the line is indented we
2519 * first make sure the line has a dependency operator in it.
2520 * If it doesn't have an operator and we're in a dependency
2521 * line's script, we assume it's actually a shell command
2522 * and add it to the current list of targets.
2525 Boolean nonSpace = FALSE;
2529 if (isspace((unsigned char) line[0])) {
2530 while ((*cp != '\0') && isspace((unsigned char) *cp)) {
2537 while ((*cp != ':') && (*cp != '!') && (*cp != '\0')) {
2547 Parse_Error (PARSE_WARNING,
2548 "Shell command needs a leading tab");
2550 } else if (nonSpace) {
2551 Parse_Error (PARSE_FATAL, "Missing operator");
2557 cp = Var_Subst (NULL, line, VAR_CMD, TRUE);
2562 * Need a non-circular list for the target nodes
2565 Lst_Destroy(targets, NOFREE);
2567 targets = Lst_Init (FALSE);
2570 ParseDoDependency (line);
2581 * Reached EOF, but it may be just EOF of an include file...
2583 } while (ParseEOF(1) == CONTINUE);
2586 * Make sure conditionals are clean
2591 errx(1, "fatal errors encountered -- cannot continue");
2595 *---------------------------------------------------------------------
2597 * initialize the parsing module
2603 * the parseIncPath list is initialized...
2604 *---------------------------------------------------------------------
2609 mainNode = NILGNODE;
2610 parseIncPath = Lst_Init (FALSE);
2611 sysIncPath = Lst_Init (FALSE);
2612 includes = Lst_Init (FALSE);
2613 targCmds = Lst_Init (FALSE);
2619 Lst_Destroy(targCmds, (void (*) __P((ClientData))) free);
2621 Lst_Destroy(targets, NOFREE);
2622 Lst_Destroy(sysIncPath, Dir_Destroy);
2623 Lst_Destroy(parseIncPath, Dir_Destroy);
2624 Lst_Destroy(includes, NOFREE); /* Should be empty now */
2629 *-----------------------------------------------------------------------
2631 * Return a Lst of the main target to create for main()'s sake. If
2632 * no such target exists, we Punt with an obnoxious error message.
2635 * A Lst of the single node to create.
2640 *-----------------------------------------------------------------------
2645 Lst listmain; /* result list */
2647 listmain = Lst_Init (FALSE);
2649 if (mainNode == NILGNODE) {
2650 Punt ("no target to make.");
2652 } else if (mainNode->type & OP_DOUBLEDEP) {
2653 (void) Lst_AtEnd (listmain, (ClientData)mainNode);
2654 Lst_Concat(listmain, mainNode->cohorts, LST_CONCNEW);
2657 (void) Lst_AtEnd (listmain, (ClientData)mainNode);