1 /* $NetBSD: parse.c,v 1.227 2018/02/22 01:59:28 sjg Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 static char rcsid[] = "$NetBSD: parse.c,v 1.227 2018/02/22 01:59:28 sjg Exp $";
74 #include <sys/cdefs.h>
77 static char sccsid[] = "@(#)parse.c 8.3 (Berkeley) 3/19/94";
79 __RCSID("$NetBSD: parse.c,v 1.227 2018/02/22 01:59:28 sjg Exp $");
86 * Functions to parse a makefile.
88 * One function, Parse_Init, must be called before any functions
89 * in this module are used. After that, the function Parse_File is the
90 * main entry point and controls most of the other functions in this
93 * Most important structures are kept in Lsts. Directories for
94 * the .include "..." function are kept in the 'parseIncPath' Lst, while
95 * those for the .include <...> are kept in the 'sysIncPath' Lst. The
96 * targets currently being defined are kept in the 'targets' Lst.
98 * The variables 'fname' and 'lineno' are used to track the name
99 * of the current file and the line number in that file so that error
100 * messages can be more meaningful.
103 * Parse_Init Initialization function which must be
104 * called before anything else in this module
107 * Parse_End Cleanup the module
109 * Parse_File Function used to parse a makefile. It must
110 * be given the name of the file, which should
111 * already have been opened, and a function
112 * to call to read a character from the file.
114 * Parse_IsVar Returns TRUE if the given line is a
115 * variable assignment. Used by MainParseArgs
116 * to determine if an argument is a target
117 * or a variable assignment. Used internally
118 * for pretty much the same thing...
120 * Parse_Error Function called when an error occurs in
121 * parsing. Used by the variable and
122 * conditional modules.
123 * Parse_MainName Returns a Lst of the main target to create.
126 #include <sys/types.h>
127 #include <sys/stat.h>
139 #include "pathnames.h"
146 #include <sys/mman.h>
149 #define MAP_COPY MAP_PRIVATE
156 ////////////////////////////////////////////////////////////
157 // types and constants
160 * Structure for a file being read ("included file")
162 typedef struct IFile {
163 char *fname; /* name of file */
164 int lineno; /* current line number in file */
165 int first_lineno; /* line number of start of text */
166 int cond_depth; /* 'if' nesting when file opened */
167 Boolean depending; /* state of doing_depend on EOF */
168 char *P_str; /* point to base of string buffer */
169 char *P_ptr; /* point to next char of string buffer */
170 char *P_end; /* point to the end of string buffer */
171 char *(*nextbuf)(void *, size_t *); /* Function to get more data */
172 void *nextbuf_arg; /* Opaque arg for nextbuf() */
173 struct loadedfile *lf; /* loadedfile object, if any */
178 * These values are returned by ParseEOF to tell Parse_File whether to
179 * CONTINUE parsing, i.e. it had only reached the end of an include file,
186 * Tokens for target attributes
190 Default, /* .DEFAULT */
191 DeleteOnError, /* .DELETE_ON_ERROR */
193 dotError, /* .ERROR */
194 Ignore, /* .IGNORE */
195 Includes, /* .INCLUDES */
196 Interrupt, /* .INTERRUPT */
199 MFlags, /* .MFLAGS or .MAKEFLAGS */
200 Main, /* .MAIN and we don't have anything user-specified to
202 NoExport, /* .NOEXPORT */
203 NoMeta, /* .NOMETA */
204 NoMetaCmp, /* .NOMETA_CMP */
205 NoPath, /* .NOPATH */
206 Not, /* Not special */
207 NotParallel, /* .NOTPARALLEL */
209 ExObjdir, /* .OBJDIR */
211 Parallel, /* .PARALLEL */
217 Precious, /* .PRECIOUS */
218 ExShell, /* .SHELL */
219 Silent, /* .SILENT */
220 SingleShell, /* .SINGLESHELL */
222 Suffixes, /* .SUFFIXES */
224 Attribute /* Generic attribute */
234 ////////////////////////////////////////////////////////////
238 * The main target to create. This is the first target on the first
239 * dependency line in the first makefile.
241 static GNode *mainNode;
243 ////////////////////////////////////////////////////////////
246 /* targets we're working on */
250 /* command lines for targets */
255 * specType contains the SPECial TYPE of the current target. It is
256 * Not if the target is unspecial. If it *is* special, however, the children
257 * are linked as children of the parent but not vice versa. This variable is
258 * set in ParseDoDependency
260 static ParseSpecial specType;
263 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
264 * seen, then set to each successive source on the line.
266 static GNode *predecessor;
268 ////////////////////////////////////////////////////////////
271 /* true if currently in a dependency line or its commands */
272 static Boolean inLine;
274 /* number of fatal errors */
275 static int fatals = 0;
278 * Variables for doing includes
281 /* current file being read */
282 static IFile *curFile;
284 /* stack of IFiles generated by .includes */
287 /* include paths (lists of directories) */
288 Lst parseIncPath; /* dirs for "..." includes */
289 Lst sysIncPath; /* dirs for <...> includes */
290 Lst defIncPath; /* default for sysIncPath */
292 ////////////////////////////////////////////////////////////
296 * The parseKeywords table is searched using binary search when deciding
297 * if a target or source is special. The 'spec' field is the ParseSpecial
298 * type of the keyword ("Not" if the keyword isn't special as a target) while
299 * the 'op' field is the operator to apply to the list of targets if the
300 * keyword is used as a source ("0" if the keyword isn't special as a source)
302 static const struct {
303 const char *name; /* Name of keyword */
304 ParseSpecial spec; /* Type when used as a target */
305 int op; /* Operator when used as a source */
306 } parseKeywords[] = {
307 { ".BEGIN", Begin, 0 },
308 { ".DEFAULT", Default, 0 },
309 { ".DELETE_ON_ERROR", DeleteOnError, 0 },
311 { ".ERROR", dotError, 0 },
312 { ".EXEC", Attribute, OP_EXEC },
313 { ".IGNORE", Ignore, OP_IGNORE },
314 { ".INCLUDES", Includes, 0 },
315 { ".INTERRUPT", Interrupt, 0 },
316 { ".INVISIBLE", Attribute, OP_INVISIBLE },
317 { ".JOIN", Attribute, OP_JOIN },
318 { ".LIBS", Libs, 0 },
319 { ".MADE", Attribute, OP_MADE },
320 { ".MAIN", Main, 0 },
321 { ".MAKE", Attribute, OP_MAKE },
322 { ".MAKEFLAGS", MFlags, 0 },
323 { ".META", Meta, OP_META },
324 { ".MFLAGS", MFlags, 0 },
325 { ".NOMETA", NoMeta, OP_NOMETA },
326 { ".NOMETA_CMP", NoMetaCmp, OP_NOMETA_CMP },
327 { ".NOPATH", NoPath, OP_NOPATH },
328 { ".NOTMAIN", Attribute, OP_NOTMAIN },
329 { ".NOTPARALLEL", NotParallel, 0 },
330 { ".NO_PARALLEL", NotParallel, 0 },
331 { ".NULL", Null, 0 },
332 { ".OBJDIR", ExObjdir, 0 },
333 { ".OPTIONAL", Attribute, OP_OPTIONAL },
334 { ".ORDER", Order, 0 },
335 { ".PARALLEL", Parallel, 0 },
336 { ".PATH", ExPath, 0 },
337 { ".PHONY", Phony, OP_PHONY },
339 { ".POSIX", Posix, 0 },
341 { ".PRECIOUS", Precious, OP_PRECIOUS },
342 { ".RECURSIVE", Attribute, OP_MAKE },
343 { ".SHELL", ExShell, 0 },
344 { ".SILENT", Silent, OP_SILENT },
345 { ".SINGLESHELL", SingleShell, 0 },
346 { ".STALE", Stale, 0 },
347 { ".SUFFIXES", Suffixes, 0 },
348 { ".USE", Attribute, OP_USE },
349 { ".USEBEFORE", Attribute, OP_USEBEFORE },
350 { ".WAIT", Wait, 0 },
353 ////////////////////////////////////////////////////////////
356 static int ParseIsEscaped(const char *, const char *);
357 static void ParseErrorInternal(const char *, size_t, int, const char *, ...)
358 MAKE_ATTR_PRINTFLIKE(4,5);
359 static void ParseVErrorInternal(FILE *, const char *, size_t, int, const char *, va_list)
360 MAKE_ATTR_PRINTFLIKE(5, 0);
361 static int ParseFindKeyword(const char *);
362 static int ParseLinkSrc(void *, void *);
363 static int ParseDoOp(void *, void *);
364 static void ParseDoSrc(int, const char *);
365 static int ParseFindMain(void *, void *);
366 static int ParseAddDir(void *, void *);
367 static int ParseClearPath(void *, void *);
368 static void ParseDoDependency(char *);
369 static int ParseAddCmd(void *, void *);
370 static void ParseHasCommands(void *);
371 static void ParseDoInclude(char *);
372 static void ParseSetParseFile(const char *);
373 static void ParseSetIncludedFile(void);
375 static void ParseTraditionalInclude(char *);
378 static void ParseGmakeExport(char *);
380 static int ParseEOF(void);
381 static char *ParseReadLine(void);
382 static void ParseFinishLine(void);
383 static void ParseMark(GNode *);
385 ////////////////////////////////////////////////////////////
389 const char *path; /* name, for error reports */
390 char *buf; /* contents buffer */
391 size_t len; /* length of contents */
392 size_t maplen; /* length of mmap area, or 0 */
393 Boolean used; /* XXX: have we used the data yet */
397 * Constructor/destructor for loadedfile
399 static struct loadedfile *
400 loadedfile_create(const char *path)
402 struct loadedfile *lf;
404 lf = bmake_malloc(sizeof(*lf));
405 lf->path = (path == NULL ? "(stdin)" : path);
414 loadedfile_destroy(struct loadedfile *lf)
416 if (lf->buf != NULL) {
417 if (lf->maplen > 0) {
419 munmap(lf->buf, lf->maplen);
429 * nextbuf() operation for loadedfile, as needed by the weird and twisted
430 * logic below. Once that's cleaned up, we can get rid of lf->used...
433 loadedfile_nextbuf(void *x, size_t *len)
435 struct loadedfile *lf = x;
446 * Try to get the size of a file.
449 load_getsize(int fd, size_t *ret)
453 if (fstat(fd, &st) < 0) {
457 if (!S_ISREG(st.st_mode)) {
462 * st_size is an off_t, which is 64 bits signed; *ret is
463 * size_t, which might be 32 bits unsigned or 64 bits
464 * unsigned. Rather than being elaborate, just punt on
465 * files that are more than 2^31 bytes. We should never
466 * see a makefile that size in practice...
468 * While we're at it reject negative sizes too, just in case.
470 if (st.st_size < 0 || st.st_size > 0x7fffffff) {
474 *ret = (size_t) st.st_size;
481 * Until the path search logic can be moved under here instead of
482 * being in the caller in another source file, we need to have the fd
483 * passed in already open. Bleh.
485 * If the path is NULL use stdin and (to insure against fd leaks)
486 * assert that the caller passed in -1.
488 static struct loadedfile *
489 loadfile(const char *path, int fd)
491 struct loadedfile *lf;
493 static long pagesize = 0;
498 lf = loadedfile_create(path);
505 fd = open(path, O_RDONLY);
508 Error("%s: %s", path, strerror(errno));
515 if (load_getsize(fd, &lf->len) == SUCCESS) {
516 /* found a size, try mmap */
519 pagesize = sysconf(_SC_PAGESIZE);
524 /* round size up to a page */
525 lf->maplen = pagesize * ((lf->len + pagesize - 1)/pagesize);
528 * XXX hack for dealing with empty files; remove when
529 * we're no longer limited by interfacing to the old
530 * logic elsewhere in this file.
532 if (lf->maplen == 0) {
533 lf->maplen = pagesize;
537 * FUTURE: remove PROT_WRITE when the parser no longer
538 * needs to scribble on the input.
540 lf->buf = mmap(NULL, lf->maplen, PROT_READ|PROT_WRITE,
541 MAP_FILE|MAP_COPY, fd, 0);
542 if (lf->buf != MAP_FAILED) {
544 if (lf->len == lf->maplen && lf->buf[lf->len - 1] != '\n') {
545 char *b = bmake_malloc(lf->len + 1);
547 memcpy(b, lf->buf, lf->len++);
548 munmap(lf->buf, lf->maplen);
556 /* cannot mmap; load the traditional way */
560 lf->buf = bmake_malloc(lf->len);
564 assert(bufpos <= lf->len);
565 if (bufpos == lf->len) {
566 if (lf->len > SIZE_MAX/2) {
568 Error("%s: file too large", path);
572 lf->buf = bmake_realloc(lf->buf, lf->len);
574 assert(bufpos < lf->len);
575 result = read(fd, lf->buf + bufpos, lf->len - bufpos);
577 Error("%s: read error: %s", path, strerror(errno));
585 assert(bufpos <= lf->len);
588 /* truncate malloc region to actual length (maybe not useful) */
590 /* as for mmap case, ensure trailing \n */
591 if (lf->buf[lf->len - 1] != '\n')
593 lf->buf = bmake_realloc(lf->buf, lf->len);
594 lf->buf[lf->len - 1] = '\n';
606 ////////////////////////////////////////////////////////////
610 *----------------------------------------------------------------------
612 * Check if the current character is escaped on the current line
615 * 0 if the character is not backslash escaped, 1 otherwise
619 *----------------------------------------------------------------------
622 ParseIsEscaped(const char *line, const char *c)
635 *----------------------------------------------------------------------
636 * ParseFindKeyword --
637 * Look in the table of keywords for one matching the given string.
643 * The index of the keyword, or -1 if it isn't there.
647 *----------------------------------------------------------------------
650 ParseFindKeyword(const char *str)
656 end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
659 cur = start + ((end - start) / 2);
660 diff = strcmp(str, parseKeywords[cur].name);
664 } else if (diff < 0) {
669 } while (start <= end);
674 * ParseVErrorInternal --
675 * Error message abort function for parsing. Prints out the context
676 * of the error (line number and file) as well as the message with
677 * two optional arguments.
683 * "fatals" is incremented if the level is PARSE_FATAL.
687 ParseVErrorInternal(FILE *f, const char *cfname, size_t clineno, int type,
688 const char *fmt, va_list ap)
690 static Boolean fatal_warning_error_printed = FALSE;
692 (void)fprintf(f, "%s: ", progname);
694 if (cfname != NULL) {
695 (void)fprintf(f, "\"");
696 if (*cfname != '/' && strcmp(cfname, "(stdin)") != 0) {
701 * Nothing is more annoying than not knowing
702 * which Makefile is the culprit.
704 dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &cp);
705 if (dir == NULL || *dir == '\0' ||
706 (*dir == '.' && dir[1] == '\0'))
707 dir = Var_Value(".CURDIR", VAR_GLOBAL, &cp);
711 (void)fprintf(f, "%s/%s", dir, cfname);
713 (void)fprintf(f, "%s", cfname);
715 (void)fprintf(f, "\" line %d: ", (int)clineno);
717 if (type == PARSE_WARNING)
718 (void)fprintf(f, "warning: ");
719 (void)vfprintf(f, fmt, ap);
720 (void)fprintf(f, "\n");
722 if (type == PARSE_INFO)
724 if (type == PARSE_FATAL || parseWarnFatal)
726 if (parseWarnFatal && !fatal_warning_error_printed) {
727 Error("parsing warnings being treated as errors");
728 fatal_warning_error_printed = TRUE;
733 * ParseErrorInternal --
744 ParseErrorInternal(const char *cfname, size_t clineno, int type,
745 const char *fmt, ...)
750 (void)fflush(stdout);
751 ParseVErrorInternal(stderr, cfname, clineno, type, fmt, ap);
754 if (debug_file != stderr && debug_file != stdout) {
756 ParseVErrorInternal(debug_file, cfname, clineno, type, fmt, ap);
763 * External interface to ParseErrorInternal; uses the default filename
774 Parse_Error(int type, const char *fmt, ...)
780 if (curFile == NULL) {
784 fname = curFile->fname;
785 lineno = curFile->lineno;
789 (void)fflush(stdout);
790 ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
793 if (debug_file != stderr && debug_file != stdout) {
795 ParseVErrorInternal(debug_file, fname, lineno, type, fmt, ap);
803 * Parse a .info .warning or .error directive
805 * The input is the line minus the ".". We substitute
806 * variables, print the message and exit(1) (for .error) or just print
807 * a warning if the directive is malformed.
810 ParseMessage(char *line)
819 mtype = PARSE_WARNING;
825 Parse_Error(PARSE_WARNING, "invalid syntax: \".%s\"", line);
829 while (isalpha((unsigned char)*line))
831 if (!isspace((unsigned char)*line))
832 return FALSE; /* not for us */
833 while (isspace((unsigned char)*line))
836 line = Var_Subst(NULL, line, VAR_CMD, VARF_WANTRES);
837 Parse_Error(mtype, "%s", line);
840 if (mtype == PARSE_FATAL) {
841 /* Terminate immediately. */
848 *---------------------------------------------------------------------
850 * Link the parent node to its new child. Used in a Lst_ForEach by
851 * ParseDoDependency. If the specType isn't 'Not', the parent
852 * isn't linked as a parent of the child.
855 * pgnp The parent node
856 * cgpn The child node
862 * New elements are added to the parents list of cgn and the
863 * children list of cgn. the unmade field of pgn is updated
864 * to reflect the additional child.
865 *---------------------------------------------------------------------
868 ParseLinkSrc(void *pgnp, void *cgnp)
870 GNode *pgn = (GNode *)pgnp;
871 GNode *cgn = (GNode *)cgnp;
873 if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (pgn->cohorts))
874 pgn = (GNode *)Lst_Datum(Lst_Last(pgn->cohorts));
875 (void)Lst_AtEnd(pgn->children, cgn);
877 (void)Lst_AtEnd(cgn->parents, pgn);
880 fprintf(debug_file, "# %s: added child %s - %s\n", __func__,
881 pgn->name, cgn->name);
882 Targ_PrintNode(pgn, 0);
883 Targ_PrintNode(cgn, 0);
889 *---------------------------------------------------------------------
891 * Apply the parsed operator to the given target node. Used in a
892 * Lst_ForEach call by ParseDoDependency once all targets have
893 * been found and their operator parsed. If the previous and new
894 * operators are incompatible, a major error is taken.
897 * gnp The node to which the operator is to be applied
898 * opp The operator to apply
904 * The type field of the node is altered to reflect any new bits in
906 *---------------------------------------------------------------------
909 ParseDoOp(void *gnp, void *opp)
911 GNode *gn = (GNode *)gnp;
912 int op = *(int *)opp;
914 * If the dependency mask of the operator and the node don't match and
915 * the node has actually had an operator applied to it before, and
916 * the operator actually has some dependency information in it, complain.
918 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
919 !OP_NOP(gn->type) && !OP_NOP(op))
921 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
925 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
927 * If the node was the object of a :: operator, we need to create a
928 * new instance of it for the children and commands on this dependency
929 * line. The new instance is placed on the 'cohorts' list of the
930 * initial one (note the initial one is not on its own cohorts list)
931 * and the new instance is linked to all parents of the initial
937 * Propagate copied bits to the initial node. They'll be propagated
938 * back to the rest of the cohorts later.
940 gn->type |= op & ~OP_OPMASK;
942 cohort = Targ_FindNode(gn->name, TARG_NOHASH);
946 * Make the cohort invisible as well to avoid duplicating it into
947 * other variables. True, parents of this target won't tend to do
948 * anything with their local variables, but better safe than
949 * sorry. (I think this is pointless now, since the relevant list
950 * traversals will no longer see this node anyway. -mycroft)
952 cohort->type = op | OP_INVISIBLE;
953 (void)Lst_AtEnd(gn->cohorts, cohort);
954 cohort->centurion = gn;
955 gn->unmade_cohorts += 1;
956 snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
960 * We don't want to nuke any previous flags (whatever they were) so we
961 * just OR the new operator into the old
970 *---------------------------------------------------------------------
972 * Given the name of a source, figure out if it is an attribute
973 * and apply it to the targets if it is. Else decide if there is
974 * some attribute which should be applied *to* the source because
975 * of some special target and apply it if so. Otherwise, make the
976 * source be a child of the targets in the list 'targets'
979 * tOp operator (if any) from special targets
980 * src name of the source to handle
986 * Operator bits may be added to the list of targets or to the source.
987 * The targets may have a new source added to their lists of children.
988 *---------------------------------------------------------------------
991 ParseDoSrc(int tOp, const char *src)
994 static int wait_number = 0;
997 if (*src == '.' && isupper ((unsigned char)src[1])) {
998 int keywd = ParseFindKeyword(src);
1000 int op = parseKeywords[keywd].op;
1002 Lst_ForEach(targets, ParseDoOp, &op);
1005 if (parseKeywords[keywd].spec == Wait) {
1007 * We add a .WAIT node in the dependency list.
1008 * After any dynamic dependencies (and filename globbing)
1009 * have happened, it is given a dependency on the each
1010 * previous child back to and previous .WAIT node.
1011 * The next child won't be scheduled until the .WAIT node
1013 * We give each .WAIT node a unique name (mainly for diag).
1015 snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
1016 gn = Targ_FindNode(wait_src, TARG_NOHASH);
1019 gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
1020 Lst_ForEach(targets, ParseLinkSrc, gn);
1029 * If we have noted the existence of a .MAIN, it means we need
1030 * to add the sources of said target to the list of things
1031 * to create. The string 'src' is likely to be free, so we
1032 * must make a new copy of it. Note that this will only be
1033 * invoked if the user didn't specify a target on the command
1034 * line. This is to allow #ifmake's to succeed, or something...
1036 (void)Lst_AtEnd(create, bmake_strdup(src));
1038 * Add the name to the .TARGETS variable as well, so the user can
1039 * employ that, if desired.
1041 Var_Append(".TARGETS", src, VAR_GLOBAL);
1046 * Create proper predecessor/successor links between the previous
1047 * source and the current one.
1049 gn = Targ_FindNode(src, TARG_CREATE);
1052 if (predecessor != NULL) {
1053 (void)Lst_AtEnd(predecessor->order_succ, gn);
1054 (void)Lst_AtEnd(gn->order_pred, predecessor);
1056 fprintf(debug_file, "# %s: added Order dependency %s - %s\n",
1057 __func__, predecessor->name, gn->name);
1058 Targ_PrintNode(predecessor, 0);
1059 Targ_PrintNode(gn, 0);
1063 * The current source now becomes the predecessor for the next one.
1070 * If the source is not an attribute, we need to find/create
1071 * a node for it. After that we can apply any operator to it
1072 * from a special target or link it to its parents, as
1075 * In the case of a source that was the object of a :: operator,
1076 * the attribute is applied to all of its instances (as kept in
1077 * the 'cohorts' list of the node) or all the cohorts are linked
1078 * to all the targets.
1081 /* Find/create the 'src' node and attach to all targets */
1082 gn = Targ_FindNode(src, TARG_CREATE);
1088 Lst_ForEach(targets, ParseLinkSrc, gn);
1095 *-----------------------------------------------------------------------
1097 * Find a real target in the list and set it to be the main one.
1098 * Called by ParseDoDependency when a main target hasn't been found
1102 * gnp Node to examine
1105 * 0 if main not found yet, 1 if it is.
1108 * mainNode is changed and Targ_SetMain is called.
1110 *-----------------------------------------------------------------------
1113 ParseFindMain(void *gnp, void *dummy MAKE_ATTR_UNUSED)
1115 GNode *gn = (GNode *)gnp;
1116 if ((gn->type & OP_NOTARGET) == 0) {
1126 *-----------------------------------------------------------------------
1128 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
1136 *-----------------------------------------------------------------------
1139 ParseAddDir(void *path, void *name)
1141 (void)Dir_AddDir((Lst) path, (char *)name);
1146 *-----------------------------------------------------------------------
1148 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
1156 *-----------------------------------------------------------------------
1159 ParseClearPath(void *path, void *dummy MAKE_ATTR_UNUSED)
1161 Dir_ClearPath((Lst) path);
1166 *---------------------------------------------------------------------
1167 * ParseDoDependency --
1168 * Parse the dependency line in line.
1171 * line the line to parse
1177 * The nodes of the sources are linked as children to the nodes of the
1178 * targets. Some nodes may be created.
1180 * We parse a dependency line by first extracting words from the line and
1181 * finding nodes in the list of all targets with that name. This is done
1182 * until a character is encountered which is an operator character. Currently
1183 * these are only ! and :. At this point the operator is parsed and the
1184 * pointer into the line advanced until the first source is encountered.
1185 * The parsed operator is applied to each node in the 'targets' list,
1186 * which is where the nodes found for the targets are kept, by means of
1187 * the ParseDoOp function.
1188 * The sources are read in much the same way as the targets were except
1189 * that now they are expanded using the wildcarding scheme of the C-Shell
1190 * and all instances of the resulting words in the list of all targets
1191 * are found. Each of the resulting nodes is then linked to each of the
1192 * targets as one of its children.
1193 * Certain targets are handled specially. These are the ones detailed
1194 * by the specType variable.
1195 * The storing of transformation rules is also taken care of here.
1196 * A target is recognized as a transformation rule by calling
1197 * Suff_IsTransform. If it is a transformation rule, its node is gotten
1198 * from the suffix module via Suff_AddTransform rather than the standard
1199 * Targ_FindNode in the target module.
1200 *---------------------------------------------------------------------
1203 ParseDoDependency(char *line)
1205 char *cp; /* our current position */
1206 GNode *gn = NULL; /* a general purpose temporary node */
1207 int op; /* the operator on the line */
1208 char savec; /* a place to save a character */
1209 Lst paths; /* List of search paths to alter when parsing
1210 * a list of .PATH targets */
1211 int tOp; /* operator from special target */
1212 Lst sources; /* list of archive source names after
1214 Lst curTargs; /* list of target names to be found and added
1215 * to the targets list */
1216 char *lstart = line;
1219 fprintf(debug_file, "ParseDoDependency(%s)\n", line);
1225 curTargs = Lst_Init(FALSE);
1228 * First, grind through the targets.
1233 * Here LINE points to the beginning of the next word, and
1234 * LSTART points to the actual beginning of the line.
1237 /* Find the end of the next word. */
1238 for (cp = line; *cp && (ParseIsEscaped(lstart, cp) ||
1239 !(isspace((unsigned char)*cp) ||
1240 *cp == '!' || *cp == ':' || *cp == LPAREN));
1244 * Must be a dynamic source (would have been expanded
1245 * otherwise), so call the Var module to parse the puppy
1246 * so we can safely advance beyond it...There should be
1247 * no errors in this, as they would have been discovered
1248 * in the initial Var_Subst and we wouldn't be here.
1253 (void)Var_Parse(cp, VAR_CMD, VARF_UNDEFERR|VARF_WANTRES,
1261 * If the word is followed by a left parenthesis, it's the
1262 * name of an object file inside an archive (ar file).
1264 if (!ParseIsEscaped(lstart, cp) && *cp == LPAREN) {
1266 * Archives must be handled specially to make sure the OP_ARCHV
1267 * flag is set in their 'type' field, for one thing, and because
1268 * things like "archive(file1.o file2.o file3.o)" are permissible.
1269 * Arch_ParseArchive will set 'line' to be the first non-blank
1270 * after the archive-spec. It creates/finds nodes for the members
1271 * and places them on the given list, returning SUCCESS if all
1272 * went well and FAILURE if there was an error in the
1273 * specification. On error, line should remain untouched.
1275 if (Arch_ParseArchive(&line, targets, VAR_CMD) != SUCCESS) {
1276 Parse_Error(PARSE_FATAL,
1277 "Error in archive specification: \"%s\"", line);
1280 /* Done with this word; on to the next. */
1288 * We got to the end of the line while we were still
1289 * looking at targets.
1291 * Ending a dependency line without an operator is a Bozo
1292 * no-no. As a heuristic, this is also often triggered by
1293 * undetected conflicts from cvs/rcs merges.
1295 if ((strncmp(line, "<<<<<<", 6) == 0) ||
1296 (strncmp(line, "======", 6) == 0) ||
1297 (strncmp(line, ">>>>>>", 6) == 0))
1298 Parse_Error(PARSE_FATAL,
1299 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
1301 Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive"
1302 : "Need an operator");
1306 /* Insert a null terminator. */
1311 * Got the word. See if it's a special target and if so set
1312 * specType to match it.
1314 if (*line == '.' && isupper ((unsigned char)line[1])) {
1316 * See if the target is a special target that must have it
1317 * or its sources handled specially.
1319 int keywd = ParseFindKeyword(line);
1321 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
1322 Parse_Error(PARSE_FATAL, "Mismatched special targets");
1326 specType = parseKeywords[keywd].spec;
1327 tOp = parseKeywords[keywd].op;
1330 * Certain special targets have special semantics:
1331 * .PATH Have to set the dirSearchPath
1333 * .MAIN Its sources are only used if
1334 * nothing has been specified to
1336 * .DEFAULT Need to create a node to hang
1337 * commands on, but we don't want
1338 * it in the graph, nor do we want
1339 * it to be the Main Target, so we
1340 * create it, set OP_NOTMAIN and
1341 * add it to the list, setting
1342 * DEFAULT to the new node for
1343 * later use. We claim the node is
1344 * A transformation rule to make
1345 * life easier later, when we'll
1346 * use Make_HandleUse to actually
1347 * apply the .DEFAULT commands.
1348 * .PHONY The list of targets
1349 * .NOPATH Don't search for file in the path
1355 * .INTERRUPT Are not to be considered the
1357 * .NOTPARALLEL Make only one target at a time.
1358 * .SINGLESHELL Create a shell for each command.
1359 * .ORDER Must set initial predecessor to NULL
1363 if (paths == NULL) {
1364 paths = Lst_Init(FALSE);
1366 (void)Lst_AtEnd(paths, dirSearchPath);
1369 if (!Lst_IsEmpty(create)) {
1378 gn = Targ_FindNode(line, TARG_CREATE);
1381 gn->type |= OP_NOTMAIN|OP_SPECIAL;
1382 (void)Lst_AtEnd(targets, gn);
1385 gn = Targ_NewGN(".DEFAULT");
1386 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
1387 (void)Lst_AtEnd(targets, gn);
1391 deleteOnError = TRUE;
1405 } else if (strncmp(line, ".PATH", 5) == 0) {
1407 * .PATH<suffix> has to be handled specially.
1408 * Call on the suffix module to give us a path to
1414 path = Suff_GetPath(&line[5]);
1416 Parse_Error(PARSE_FATAL,
1417 "Suffix '%s' not defined (yet)",
1421 if (paths == NULL) {
1422 paths = Lst_Init(FALSE);
1424 (void)Lst_AtEnd(paths, path);
1430 * Have word in line. Get or create its node and stick it at
1431 * the end of the targets list
1433 if ((specType == Not) && (*line != '\0')) {
1434 if (Dir_HasWildcards(line)) {
1436 * Targets are to be sought only in the current directory,
1437 * so create an empty path for the thing. Note we need to
1438 * use Dir_Destroy in the destruction of the path as the
1439 * Dir module could have added a directory to the path...
1441 Lst emptyPath = Lst_Init(FALSE);
1443 Dir_Expand(line, emptyPath, curTargs);
1445 Lst_Destroy(emptyPath, Dir_Destroy);
1448 * No wildcards, but we want to avoid code duplication,
1449 * so create a list with the word on it.
1451 (void)Lst_AtEnd(curTargs, line);
1454 /* Apply the targets. */
1456 while(!Lst_IsEmpty(curTargs)) {
1457 char *targName = (char *)Lst_DeQueue(curTargs);
1459 if (!Suff_IsTransform (targName)) {
1460 gn = Targ_FindNode(targName, TARG_CREATE);
1462 gn = Suff_AddTransform(targName);
1467 (void)Lst_AtEnd(targets, gn);
1469 } else if (specType == ExPath && *line != '.' && *line != '\0') {
1470 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
1473 /* Don't need the inserted null terminator any more. */
1477 * If it is a special type and not .PATH, it's the only target we
1478 * allow on this line...
1480 if (specType != Not && specType != ExPath) {
1481 Boolean warning = FALSE;
1483 while (*cp && (ParseIsEscaped(lstart, cp) ||
1484 ((*cp != '!') && (*cp != ':')))) {
1485 if (ParseIsEscaped(lstart, cp) ||
1486 (*cp != ' ' && *cp != '\t')) {
1492 Parse_Error(PARSE_WARNING, "Extra target ignored");
1495 while (*cp && isspace ((unsigned char)*cp)) {
1500 } while (*line && (ParseIsEscaped(lstart, line) ||
1501 ((*line != '!') && (*line != ':'))));
1504 * Don't need the list of target names anymore...
1506 Lst_Destroy(curTargs, NULL);
1509 if (!Lst_IsEmpty(targets)) {
1512 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
1521 * These four create nodes on which to hang commands, so
1522 * targets shouldn't be empty...
1526 * Nothing special here -- targets can be empty if it wants.
1533 * Have now parsed all the target names. Must parse the operator next. The
1534 * result is left in op .
1538 } else if (*cp == ':') {
1546 Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive"
1547 : "Missing dependency operator");
1551 /* Advance beyond the operator */
1555 * Apply the operator to the target. This is how we remember which
1556 * operator a target was defined with. It fails if the operator
1557 * used isn't consistent across all references.
1559 Lst_ForEach(targets, ParseDoOp, &op);
1562 * Onward to the sources.
1564 * LINE will now point to the first source word, if any, or the
1565 * end of the string if not.
1567 while (*cp && isspace ((unsigned char)*cp)) {
1573 * Several special targets take different actions if present with no
1575 * a .SUFFIXES line with no sources clears out all old suffixes
1576 * a .PRECIOUS line makes all targets precious
1577 * a .IGNORE line ignores errors for all targets
1578 * a .SILENT line creates silence when making all targets
1579 * a .PATH removes all directories from the search path(s).
1584 Suff_ClearSuffixes();
1590 ignoreErrors = TRUE;
1596 Lst_ForEach(paths, ParseClearPath, NULL);
1601 Var_Set("%POSIX", "1003.2", VAR_GLOBAL, 0);
1607 } else if (specType == MFlags) {
1609 * Call on functions in main.c to deal with these arguments and
1610 * set the initial character to a null-character so the loop to
1611 * get sources won't get anything
1613 Main_ParseArgLine(line);
1615 } else if (specType == ExShell) {
1616 if (Job_ParseShell(line) != SUCCESS) {
1617 Parse_Error(PARSE_FATAL, "improper shell specification");
1621 } else if ((specType == NotParallel) || (specType == SingleShell) ||
1622 (specType == DeleteOnError)) {
1627 * NOW GO FOR THE SOURCES
1629 if ((specType == Suffixes) || (specType == ExPath) ||
1630 (specType == Includes) || (specType == Libs) ||
1631 (specType == Null) || (specType == ExObjdir))
1635 * If the target was one that doesn't take files as its sources
1636 * but takes something like suffixes, we take each
1637 * space-separated word on the line as a something and deal
1638 * with it accordingly.
1640 * If the target was .SUFFIXES, we take each source as a
1641 * suffix and add it to the list of suffixes maintained by the
1644 * If the target was a .PATH, we add the source as a directory
1645 * to search on the search path.
1647 * If it was .INCLUDES, the source is taken to be the suffix of
1648 * files which will be #included and whose search path should
1649 * be present in the .INCLUDES variable.
1651 * If it was .LIBS, the source is taken to be the suffix of
1652 * files which are considered libraries and whose search path
1653 * should be present in the .LIBS variable.
1655 * If it was .NULL, the source is the suffix to use when a file
1656 * has no valid suffix.
1658 * If it was .OBJDIR, the source is a new definition for .OBJDIR,
1659 * and will cause make to do a new chdir to that path.
1661 while (*cp && !isspace ((unsigned char)*cp)) {
1668 Suff_AddSuffix(line, &mainNode);
1671 Lst_ForEach(paths, ParseAddDir, line);
1674 Suff_AddInclude(line);
1683 Main_SetObjdir("%s", line);
1689 if (savec != '\0') {
1692 while (*cp && isspace ((unsigned char)*cp)) {
1698 Lst_Destroy(paths, NULL);
1701 if (specType == ExPath)
1704 assert(paths == NULL);
1707 * The targets take real sources, so we must beware of archive
1708 * specifications (i.e. things with left parentheses in them)
1709 * and handle them accordingly.
1711 for (; *cp && !isspace ((unsigned char)*cp); cp++) {
1712 if ((*cp == LPAREN) && (cp > line) && (cp[-1] != '$')) {
1714 * Only stop for a left parenthesis if it isn't at the
1715 * start of a word (that'll be for variable changes
1716 * later) and isn't preceded by a dollar sign (a dynamic
1723 if (*cp == LPAREN) {
1724 sources = Lst_Init(FALSE);
1725 if (Arch_ParseArchive(&line, sources, VAR_CMD) != SUCCESS) {
1726 Parse_Error(PARSE_FATAL,
1727 "Error in source archive spec \"%s\"", line);
1731 while (!Lst_IsEmpty (sources)) {
1732 gn = (GNode *)Lst_DeQueue(sources);
1733 ParseDoSrc(tOp, gn->name);
1735 Lst_Destroy(sources, NULL);
1743 ParseDoSrc(tOp, line);
1745 while (*cp && isspace ((unsigned char)*cp)) {
1752 if (mainNode == NULL) {
1754 * If we have yet to decide on a main target to make, in the
1755 * absence of any user input, we want the first target on
1756 * the first dependency line that is actually a real target
1757 * (i.e. isn't a .USE or .EXEC rule) to be made.
1759 Lst_ForEach(targets, ParseFindMain, NULL);
1763 assert(paths == NULL);
1765 Lst_Destroy(curTargs, NULL);
1769 *---------------------------------------------------------------------
1771 * Return TRUE if the passed line is a variable assignment. A variable
1772 * assignment consists of a single word followed by optional whitespace
1773 * followed by either a += or an = operator.
1774 * This function is used both by the Parse_File function and main when
1775 * parsing the command-line arguments.
1778 * line the line to check
1781 * TRUE if it is. FALSE if it ain't
1785 *---------------------------------------------------------------------
1788 Parse_IsVar(char *line)
1790 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1793 #define ISEQOPERATOR(c) \
1794 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1797 * Skip to variable name
1799 for (;(*line == ' ') || (*line == '\t'); line++)
1802 /* Scan for one of the assignment operators outside a variable expansion */
1803 while ((ch = *line++) != 0) {
1804 if (ch == '(' || ch == '{') {
1808 if (ch == ')' || ch == '}') {
1814 while (ch == ' ' || ch == '\t') {
1819 if (ch == ':' && strncmp(line, "sh", 2) == 0) {
1826 if (*line == '=' && ISEQOPERATOR(ch))
1836 *---------------------------------------------------------------------
1838 * Take the variable assignment in the passed line and do it in the
1841 * Note: There is a lexical ambiguity with assignment modifier characters
1842 * in variable names. This routine interprets the character before the =
1843 * as a modifier. Therefore, an assignment like
1845 * is interpreted as "C+ +=" instead of "C++ =".
1848 * line a line guaranteed to be a variable assignment.
1849 * This reduces error checks
1850 * ctxt Context in which to do the assignment
1856 * the variable structure of the given variable name is altered in the
1858 *---------------------------------------------------------------------
1861 Parse_DoVar(char *line, GNode *ctxt)
1863 char *cp; /* pointer into line */
1865 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1866 } type; /* Type of assignment */
1867 char *opc; /* ptr to operator character to
1868 * null-terminate the variable name */
1869 Boolean freeCp = FALSE; /* TRUE if cp needs to be freed,
1870 * i.e. if any variable expansion was
1875 * Skip to variable name
1877 while ((*line == ' ') || (*line == '\t')) {
1882 * Skip to operator character, nulling out whitespace as we go
1883 * XXX Rather than counting () and {} we should look for $ and
1884 * then expand the variable.
1886 for (depth = 0, cp = line + 1; depth > 0 || *cp != '='; cp++) {
1887 if (*cp == '(' || *cp == '{') {
1891 if (*cp == ')' || *cp == '}') {
1895 if (depth == 0 && isspace ((unsigned char)*cp)) {
1899 opc = cp-1; /* operator is the previous character */
1900 *cp++ = '\0'; /* nuke the = */
1903 * Check operator type
1913 * If the variable already has a value, we don't do anything.
1916 if (Var_Exists(line, ctxt)) {
1935 while (opc > line && *opc != ':')
1938 if (strncmp(opc, ":sh", 3) == 0) {
1948 while (isspace ((unsigned char)*cp)) {
1952 if (type == VAR_APPEND) {
1953 Var_Append(line, cp, ctxt);
1954 } else if (type == VAR_SUBST) {
1956 * Allow variables in the old value to be undefined, but leave their
1957 * invocation alone -- this is done by forcing oldVars to be false.
1958 * XXX: This can cause recursive variables, but that's not hard to do,
1959 * and this allows someone to do something like
1961 * CFLAGS = $(.INCLUDES)
1962 * CFLAGS := -I.. $(CFLAGS)
1964 * And not get an error.
1966 Boolean oldOldVars = oldVars;
1971 * make sure that we set the variable the first time to nothing
1972 * so that it gets substituted!
1974 if (!Var_Exists(line, ctxt))
1975 Var_Set(line, "", ctxt, 0);
1977 cp = Var_Subst(NULL, cp, ctxt, VARF_WANTRES|VARF_ASSIGN);
1978 oldVars = oldOldVars;
1981 Var_Set(line, cp, ctxt, 0);
1982 } else if (type == VAR_SHELL) {
1986 if (strchr(cp, '$') != NULL) {
1988 * There's a dollar sign in the command, so perform variable
1989 * expansion on the whole thing. The resulting string will need
1990 * freeing when we're done, so set freeCmd to TRUE.
1992 cp = Var_Subst(NULL, cp, VAR_CMD, VARF_UNDEFERR|VARF_WANTRES);
1996 res = Cmd_Exec(cp, &error);
1997 Var_Set(line, res, ctxt, 0);
2001 Parse_Error(PARSE_WARNING, error, cp);
2004 * Normal assignment -- just do it.
2006 Var_Set(line, cp, ctxt, 0);
2008 if (strcmp(line, MAKEOVERRIDES) == 0)
2009 Main_ExportMAKEFLAGS(FALSE); /* re-export MAKEFLAGS */
2010 else if (strcmp(line, ".CURDIR") == 0) {
2012 * Somone is being (too?) clever...
2013 * Let's pretend they know what they are doing and
2014 * re-initialize the 'cur' Path.
2018 } else if (strcmp(line, MAKE_JOB_PREFIX) == 0) {
2020 } else if (strcmp(line, MAKE_EXPORTED) == 0) {
2029 * ParseMaybeSubMake --
2030 * Scan the command string to see if it a possible submake node
2032 * cmd the command to scan
2034 * TRUE if the command is possibly a submake, FALSE if not.
2037 ParseMaybeSubMake(const char *cmd)
2044 #define MKV(A) { A, sizeof(A) - 1 }
2051 for (i = 0; i < sizeof(vals)/sizeof(vals[0]); i++) {
2053 if ((ptr = strstr(cmd, vals[i].name)) == NULL)
2055 if ((ptr == cmd || !isalnum((unsigned char)ptr[-1]))
2056 && !isalnum((unsigned char)ptr[vals[i].len]))
2064 * Lst_ForEach function to add a command line to all targets
2067 * gnp the node to which the command is to be added
2068 * cmd the command to add
2074 * A new element is added to the commands list of the node,
2075 * and the node can be marked as a submake node if the command is
2076 * determined to be that.
2079 ParseAddCmd(void *gnp, void *cmd)
2081 GNode *gn = (GNode *)gnp;
2083 /* Add to last (ie current) cohort for :: targets */
2084 if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts))
2085 gn = (GNode *)Lst_Datum(Lst_Last(gn->cohorts));
2087 /* if target already supplied, ignore commands */
2088 if (!(gn->type & OP_HAS_COMMANDS)) {
2089 (void)Lst_AtEnd(gn->commands, cmd);
2090 if (ParseMaybeSubMake(cmd))
2091 gn->type |= OP_SUBMAKE;
2095 /* XXX: We cannot do this until we fix the tree */
2096 (void)Lst_AtEnd(gn->commands, cmd);
2097 Parse_Error(PARSE_WARNING,
2098 "overriding commands for target \"%s\"; "
2099 "previous commands defined at %s: %d ignored",
2100 gn->name, gn->fname, gn->lineno);
2102 Parse_Error(PARSE_WARNING,
2103 "duplicate script for target \"%s\" ignored",
2105 ParseErrorInternal(gn->fname, gn->lineno, PARSE_WARNING,
2106 "using previous script for \"%s\" defined here",
2114 *-----------------------------------------------------------------------
2115 * ParseHasCommands --
2116 * Callback procedure for Parse_File when destroying the list of
2117 * targets on the last dependency line. Marks a target as already
2118 * having commands if it does, to keep from having shell commands
2119 * on multiple dependency lines.
2122 * gnp Node to examine
2128 * OP_HAS_COMMANDS may be set for the target.
2130 *-----------------------------------------------------------------------
2133 ParseHasCommands(void *gnp)
2135 GNode *gn = (GNode *)gnp;
2136 if (!Lst_IsEmpty(gn->commands)) {
2137 gn->type |= OP_HAS_COMMANDS;
2142 *-----------------------------------------------------------------------
2143 * Parse_AddIncludeDir --
2144 * Add a directory to the path searched for included makefiles
2145 * bracketed by double-quotes. Used by functions in main.c
2148 * dir The name of the directory to add
2154 * The directory is appended to the list.
2156 *-----------------------------------------------------------------------
2159 Parse_AddIncludeDir(char *dir)
2161 (void)Dir_AddDir(parseIncPath, dir);
2165 *---------------------------------------------------------------------
2167 * Push to another file.
2169 * The input is the line minus the `.'. A file spec is a string
2170 * enclosed in <> or "". The former is looked for only in sysIncPath.
2171 * The latter in . and the directories specified by -I command line
2178 * A structure is added to the includes Lst and readProc, lineno,
2179 * fname and curFILE are altered for the new file
2180 *---------------------------------------------------------------------
2184 Parse_include_file(char *file, Boolean isSystem, Boolean depinc, int silent)
2186 struct loadedfile *lf;
2187 char *fullname; /* full pathname of file */
2189 char *prefEnd, *incdir;
2194 * Now we know the file's name and its search path, we attempt to
2195 * find the durn thing. A return of NULL indicates the file don't
2198 fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
2200 if (fullname == NULL && !isSystem) {
2202 * Include files contained in double-quotes are first searched for
2203 * relative to the including file's location. We don't want to
2204 * cd there, of course, so we just tack on the old file's
2205 * leading path components and call Dir_FindFile to see if
2206 * we can locate the beast.
2209 incdir = bmake_strdup(curFile->fname);
2210 prefEnd = strrchr(incdir, '/');
2211 if (prefEnd != NULL) {
2213 /* Now do lexical processing of leading "../" on the filename */
2214 for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
2215 prefEnd = strrchr(incdir + 1, '/');
2216 if (prefEnd == NULL || strcmp(prefEnd, "/..") == 0)
2220 newName = str_concat(incdir, file + i, STR_ADDSLASH);
2221 fullname = Dir_FindFile(newName, parseIncPath);
2222 if (fullname == NULL)
2223 fullname = Dir_FindFile(newName, dirSearchPath);
2228 if (fullname == NULL) {
2230 * Makefile wasn't found in same directory as included makefile.
2231 * Search for it first on the -I search path,
2232 * then on the .PATH search path, if not found in a -I directory.
2233 * If we have a suffix specific path we should use that.
2236 Lst suffPath = NULL;
2238 if ((suff = strrchr(file, '.'))) {
2239 suffPath = Suff_GetPath(suff);
2240 if (suffPath != NULL) {
2241 fullname = Dir_FindFile(file, suffPath);
2244 if (fullname == NULL) {
2245 fullname = Dir_FindFile(file, parseIncPath);
2246 if (fullname == NULL) {
2247 fullname = Dir_FindFile(file, dirSearchPath);
2253 /* Looking for a system file or file still not found */
2254 if (fullname == NULL) {
2256 * Look for it on the system path
2258 fullname = Dir_FindFile(file,
2259 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
2262 if (fullname == NULL) {
2264 Parse_Error(PARSE_FATAL, "Could not find %s", file);
2268 /* Actually open the file... */
2269 fd = open(fullname, O_RDONLY);
2272 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
2278 lf = loadfile(fullname, fd);
2280 ParseSetIncludedFile();
2281 /* Start reading from this file next */
2282 Parse_SetInput(fullname, 0, -1, loadedfile_nextbuf, lf);
2285 doing_depend = depinc; /* only turn it on */
2289 ParseDoInclude(char *line)
2291 char endc; /* the character which ends the file spec */
2292 char *cp; /* current position in file spec */
2293 int silent = (*line != 'i') ? 1 : 0;
2294 char *file = &line[7 + silent];
2296 /* Skip to delimiter character so we know where to look */
2297 while (*file == ' ' || *file == '\t')
2300 if (*file != '"' && *file != '<') {
2301 Parse_Error(PARSE_FATAL,
2302 ".include filename must be delimited by '\"' or '<'");
2307 * Set the search path on which to find the include file based on the
2308 * characters which bracket its name. Angle-brackets imply it's
2309 * a system Makefile while double-quotes imply it's a user makefile
2317 /* Skip to matching delimiter */
2318 for (cp = ++file; *cp && *cp != endc; cp++)
2322 Parse_Error(PARSE_FATAL,
2323 "Unclosed %cinclude filename. '%c' expected",
2330 * Substitute for any variables in the file name before trying to
2333 file = Var_Subst(NULL, file, VAR_CMD, VARF_WANTRES);
2335 Parse_include_file(file, endc == '>', (*line == 'd'), silent);
2341 *---------------------------------------------------------------------
2342 * ParseSetIncludedFile --
2343 * Set the .INCLUDEDFROMFILE variable to the contents of .PARSEFILE
2344 * and the .INCLUDEDFROMDIR variable to the contents of .PARSEDIR
2350 * The .INCLUDEDFROMFILE variable is overwritten by the contents
2351 * of .PARSEFILE and the .INCLUDEDFROMDIR variable is overwriten
2352 * by the contents of .PARSEDIR
2353 *---------------------------------------------------------------------
2356 ParseSetIncludedFile(void)
2358 char *pf, *fp = NULL;
2359 char *pd, *dp = NULL;
2361 pf = Var_Value(".PARSEFILE", VAR_GLOBAL, &fp);
2362 Var_Set(".INCLUDEDFROMFILE", pf, VAR_GLOBAL, 0);
2363 pd = Var_Value(".PARSEDIR", VAR_GLOBAL, &dp);
2364 Var_Set(".INCLUDEDFROMDIR", pd, VAR_GLOBAL, 0);
2367 fprintf(debug_file, "%s: ${.INCLUDEDFROMDIR} = `%s' "
2368 "${.INCLUDEDFROMFILE} = `%s'\n", __func__, pd, pf);
2374 *---------------------------------------------------------------------
2375 * ParseSetParseFile --
2376 * Set the .PARSEDIR and .PARSEFILE variables to the dirname and
2377 * basename of the given filename
2383 * The .PARSEDIR and .PARSEFILE variables are overwritten by the
2384 * dirname and basename of the given filename.
2385 *---------------------------------------------------------------------
2388 ParseSetParseFile(const char *filename)
2390 char *slash, *dirname;
2391 const char *pd, *pf;
2394 slash = strrchr(filename, '/');
2395 if (slash == NULL) {
2396 Var_Set(".PARSEDIR", pd = curdir, VAR_GLOBAL, 0);
2397 Var_Set(".PARSEFILE", pf = filename, VAR_GLOBAL, 0);
2400 len = slash - filename;
2401 dirname = bmake_malloc(len + 1);
2402 memcpy(dirname, filename, len);
2403 dirname[len] = '\0';
2404 Var_Set(".PARSEDIR", pd = dirname, VAR_GLOBAL, 0);
2405 Var_Set(".PARSEFILE", pf = slash + 1, VAR_GLOBAL, 0);
2408 fprintf(debug_file, "%s: ${.PARSEDIR} = `%s' ${.PARSEFILE} = `%s'\n",
2414 * Track the makefiles we read - so makefiles can
2415 * set dependencies on them.
2416 * Avoid adding anything more than once.
2420 ParseTrackInput(const char *name)
2425 size_t name_len = strlen(name);
2427 old = Var_Value(MAKE_MAKEFILES, VAR_GLOBAL, &fp);
2429 ep = old + strlen(old) - name_len;
2430 /* does it contain name? */
2431 for (; old != NULL; old = strchr(old, ' ')) {
2435 break; /* cannot contain name */
2436 if (memcmp(old, name, name_len) == 0
2437 && (old[name_len] == 0 || old[name_len] == ' '))
2441 Var_Append (MAKE_MAKEFILES, name, VAR_GLOBAL);
2450 *---------------------------------------------------------------------
2452 * Start Parsing from the given source
2458 * A structure is added to the includes Lst and readProc, lineno,
2459 * fname and curFile are altered for the new file
2460 *---------------------------------------------------------------------
2463 Parse_SetInput(const char *name, int line, int fd,
2464 char *(*nextbuf)(void *, size_t *), void *arg)
2470 name = curFile->fname;
2472 ParseTrackInput(name);
2475 fprintf(debug_file, "%s: file %s, line %d, fd %d, nextbuf %p, arg %p\n",
2476 __func__, name, line, fd, nextbuf, arg);
2478 if (fd == -1 && nextbuf == NULL)
2482 if (curFile != NULL)
2483 /* Save exiting file info */
2484 Lst_AtFront(includes, curFile);
2486 /* Allocate and fill in new structure */
2487 curFile = bmake_malloc(sizeof *curFile);
2490 * Once the previous state has been saved, we can get down to reading
2491 * the new file. We set up the name of the file to be the absolute
2492 * name of the include file so error messages refer to the right
2495 curFile->fname = bmake_strdup(name);
2496 curFile->lineno = line;
2497 curFile->first_lineno = line;
2498 curFile->nextbuf = nextbuf;
2499 curFile->nextbuf_arg = arg;
2501 curFile->depending = doing_depend; /* restore this on EOF */
2503 assert(nextbuf != NULL);
2505 /* Get first block of input data */
2506 buf = curFile->nextbuf(curFile->nextbuf_arg, &len);
2508 /* Was all a waste of time ... */
2510 free(curFile->fname);
2514 curFile->P_str = buf;
2515 curFile->P_ptr = buf;
2516 curFile->P_end = buf+len;
2518 curFile->cond_depth = Cond_save_depth();
2519 ParseSetParseFile(name);
2524 *---------------------------------------------------------------------
2525 * ParseTraditionalInclude --
2526 * Push to another file.
2528 * The input is the current line. The file name(s) are
2529 * following the "include".
2535 * A structure is added to the includes Lst and readProc, lineno,
2536 * fname and curFILE are altered for the new file
2537 *---------------------------------------------------------------------
2540 ParseTraditionalInclude(char *line)
2542 char *cp; /* current position in file spec */
2544 int silent = (line[0] != 'i') ? 1 : 0;
2545 char *file = &line[silent + 7];
2549 fprintf(debug_file, "%s: %s\n", __func__, file);
2553 * Skip over whitespace
2555 while (isspace((unsigned char)*file))
2559 * Substitute for any variables in the file name before trying to
2562 all_files = Var_Subst(NULL, file, VAR_CMD, VARF_WANTRES);
2564 if (*file == '\0') {
2565 Parse_Error(PARSE_FATAL,
2566 "Filename missing from \"include\"");
2570 for (file = all_files; !done; file = cp + 1) {
2571 /* Skip to end of line or next whitespace */
2572 for (cp = file; *cp && !isspace((unsigned char) *cp); cp++)
2580 Parse_include_file(file, FALSE, FALSE, silent);
2589 *---------------------------------------------------------------------
2590 * ParseGmakeExport --
2591 * Parse export <variable>=<value>
2593 * And set the environment with it.
2600 *---------------------------------------------------------------------
2603 ParseGmakeExport(char *line)
2605 char *variable = &line[6];
2609 fprintf(debug_file, "%s: %s\n", __func__, variable);
2613 * Skip over whitespace
2615 while (isspace((unsigned char)*variable))
2618 for (value = variable; *value && *value != '='; value++)
2621 if (*value != '=') {
2622 Parse_Error(PARSE_FATAL,
2623 "Variable/Value missing from \"export\"");
2626 *value++ = '\0'; /* terminate variable */
2629 * Expand the value before putting it in the environment.
2631 value = Var_Subst(NULL, value, VAR_CMD, VARF_WANTRES);
2632 setenv(variable, value, 1);
2638 *---------------------------------------------------------------------
2640 * Called when EOF is reached in the current file. If we were reading
2641 * an include file, the includes stack is popped and things set up
2642 * to go back to reading the previous file at the previous location.
2645 * CONTINUE if there's more to do. DONE if not.
2648 * The old curFILE, is closed. The includes list is shortened.
2649 * lineno, curFILE, and fname are changed if CONTINUE is returned.
2650 *---------------------------------------------------------------------
2658 assert(curFile->nextbuf != NULL);
2660 doing_depend = curFile->depending; /* restore this */
2661 /* get next input buffer, if any */
2662 ptr = curFile->nextbuf(curFile->nextbuf_arg, &len);
2663 curFile->P_ptr = ptr;
2664 curFile->P_str = ptr;
2665 curFile->P_end = ptr + len;
2666 curFile->lineno = curFile->first_lineno;
2672 /* Ensure the makefile (or loop) didn't have mismatched conditionals */
2673 Cond_restore_depth(curFile->cond_depth);
2675 if (curFile->lf != NULL) {
2676 loadedfile_destroy(curFile->lf);
2680 /* Dispose of curFile info */
2681 /* Leak curFile->fname because all the gnodes have pointers to it */
2682 free(curFile->P_str);
2685 curFile = Lst_DeQueue(includes);
2687 if (curFile == NULL) {
2688 /* We've run out of input */
2689 Var_Delete(".PARSEDIR", VAR_GLOBAL);
2690 Var_Delete(".PARSEFILE", VAR_GLOBAL);
2691 Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL);
2692 Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL);
2697 fprintf(debug_file, "ParseEOF: returning to file %s, line %d\n",
2698 curFile->fname, curFile->lineno);
2700 /* Restore the PARSEDIR/PARSEFILE variables */
2701 ParseSetParseFile(curFile->fname);
2706 #define PARSE_SKIP 2
2709 ParseGetLine(int flags, int *length)
2711 IFile *cf = curFile;
2720 /* Loop through blank lines and comment lines */
2729 if (cf->P_end != NULL && ptr == cf->P_end) {
2735 if (ch == 0 || (ch == '\\' && ptr[1] == 0)) {
2736 if (cf->P_end == NULL)
2737 /* End of string (aka for loop) data */
2739 /* see if there is more we can parse */
2740 while (ptr++ < cf->P_end) {
2741 if ((ch = *ptr) == '\n') {
2742 if (ptr > line && ptr[-1] == '\\')
2744 Parse_Error(PARSE_WARNING,
2745 "Zero byte read from file, skipping rest of line.");
2749 if (cf->nextbuf != NULL) {
2751 * End of this buffer; return EOF and outer logic
2752 * will get the next one. (eww)
2756 Parse_Error(PARSE_FATAL, "Zero byte read from file");
2761 /* Don't treat next character as special, remember first one */
2762 if (escaped == NULL)
2770 if (ch == '#' && comment == NULL) {
2771 /* Remember first '#' for comment stripping */
2772 /* Unless previous char was '[', as in modifier :[#] */
2773 if (!(ptr > line && ptr[-1] == '['))
2779 if (!isspace((unsigned char)ch))
2780 /* We are not interested in trailing whitespace */
2784 /* Save next 'to be processed' location */
2787 /* Check we have a non-comment, non-blank line */
2788 if (line_end == line || comment == line) {
2790 /* At end of file */
2792 /* Parse another line */
2796 /* We now have a line of data */
2799 if (flags & PARSE_RAW) {
2800 /* Leave '\' (etc) in line buffer (eg 'for' lines) */
2801 *length = line_end - line;
2805 if (flags & PARSE_SKIP) {
2806 /* Completely ignore non-directives */
2809 /* We could do more of the .else/.elif/.endif checks here */
2814 /* Brutally ignore anything after a non-escaped '#' in non-commands */
2815 if (comment != NULL && line[0] != '\t') {
2820 /* If we didn't see a '\\' then the in-situ data is fine */
2821 if (escaped == NULL) {
2822 *length = line_end - line;
2826 /* Remove escapes from '\n' and '#' */
2829 for (; ; *tp++ = ch) {
2839 /* Delete '\\' at end of buffer */
2844 if (ch == '#' && line[0] != '\t')
2845 /* Delete '\\' from before '#' on non-command lines */
2849 /* Leave '\\' in buffer for later */
2851 /* Make sure we don't delete an escaped ' ' from the line end */
2856 /* Escaped '\n' replace following whitespace with a single ' ' */
2857 while (ptr[0] == ' ' || ptr[0] == '\t')
2862 /* Delete any trailing spaces - eg from empty continuations */
2863 while (tp > escaped && isspace((unsigned char)tp[-1]))
2867 *length = tp - line;
2872 *---------------------------------------------------------------------
2874 * Read an entire line from the input file. Called only by Parse_File.
2877 * A line w/o its newline
2880 * Only those associated with reading a character
2881 *---------------------------------------------------------------------
2886 char *line; /* Result */
2887 int lineLength; /* Length of result */
2888 int lineno; /* Saved line # */
2892 line = ParseGetLine(0, &lineLength);
2900 * The line might be a conditional. Ask the conditional module
2901 * about it and act accordingly
2903 switch (Cond_Eval(line)) {
2905 /* Skip to next conditional that evaluates to COND_PARSE. */
2907 line = ParseGetLine(PARSE_SKIP, &lineLength);
2908 } while (line && Cond_Eval(line) != COND_PARSE);
2914 case COND_INVALID: /* Not a conditional line */
2915 /* Check for .for loops */
2916 rval = For_Eval(line);
2918 /* Not a .for line */
2921 /* Syntax error - error printed, ignore line */
2923 /* Start of a .for loop */
2924 lineno = curFile->lineno;
2925 /* Accumulate loop lines until matching .endfor */
2927 line = ParseGetLine(PARSE_RAW, &lineLength);
2929 Parse_Error(PARSE_FATAL,
2930 "Unexpected end of file in for loop.");
2933 } while (For_Accum(line));
2934 /* Stash each iteration as a new 'input file' */
2936 /* Read next line from for-loop buffer */
2944 *-----------------------------------------------------------------------
2945 * ParseFinishLine --
2946 * Handle the end of a dependency group.
2952 * inLine set FALSE. 'targets' list destroyed.
2954 *-----------------------------------------------------------------------
2957 ParseFinishLine(void)
2960 Lst_ForEach(targets, Suff_EndTransform, NULL);
2961 Lst_Destroy(targets, ParseHasCommands);
2969 *---------------------------------------------------------------------
2971 * Parse a file into its component parts, incorporating it into the
2972 * current dependency graph. This is the main function and controls
2973 * almost every other function in this module
2976 * name the name of the file being read
2977 * fd Open file to makefile to parse
2984 * Loads. Nodes are added to the list of all targets, nodes and links
2985 * are added to the dependency graph. etc. etc. etc.
2986 *---------------------------------------------------------------------
2989 Parse_File(const char *name, int fd)
2991 char *cp; /* pointer into the line */
2992 char *line; /* the line we're working on */
2993 struct loadedfile *lf;
2995 lf = loadfile(name, fd);
3004 Parse_SetInput(name, 0, -1, loadedfile_nextbuf, lf);
3008 for (; (line = ParseReadLine()) != NULL; ) {
3010 fprintf(debug_file, "ParseReadLine (%d): '%s'\n",
3011 curFile->lineno, line);
3014 * Lines that begin with the special character may be
3015 * include or undef directives.
3016 * On the other hand they can be suffix rules (.c.o: ...)
3017 * or just dependencies for filenames that start '.'.
3019 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
3022 if (strncmp(cp, "include", 7) == 0 ||
3023 ((cp[0] == 'd' || cp[0] == 's' || cp[0] == '-') &&
3024 strncmp(&cp[1], "include", 7) == 0)) {
3028 if (strncmp(cp, "undef", 5) == 0) {
3030 for (cp += 5; isspace((unsigned char) *cp); cp++)
3032 for (cp2 = cp; !isspace((unsigned char) *cp2) &&
3033 (*cp2 != '\0'); cp2++)
3036 Var_Delete(cp, VAR_GLOBAL);
3038 } else if (strncmp(cp, "export", 6) == 0) {
3039 for (cp += 6; isspace((unsigned char) *cp); cp++)
3043 } else if (strncmp(cp, "unexport", 8) == 0) {
3046 } else if (strncmp(cp, "info", 4) == 0 ||
3047 strncmp(cp, "error", 5) == 0 ||
3048 strncmp(cp, "warning", 7) == 0) {
3049 if (ParseMessage(cp))
3054 if (*line == '\t') {
3056 * If a line starts with a tab, it can only hope to be
3057 * a creation command.
3061 for (; isspace ((unsigned char)*cp); cp++) {
3066 Parse_Error(PARSE_FATAL,
3067 "Unassociated shell command \"%s\"",
3070 * So long as it's not a blank line and we're actually
3071 * in a dependency spec, add the command to the list of
3072 * commands of all targets in the dependency spec
3075 cp = bmake_strdup(cp);
3076 Lst_ForEach(targets, ParseAddCmd, cp);
3078 Lst_AtEnd(targCmds, cp);
3086 if (((strncmp(line, "include", 7) == 0 &&
3087 isspace((unsigned char) line[7])) ||
3088 ((line[0] == 's' || line[0] == '-') &&
3089 strncmp(&line[1], "include", 7) == 0 &&
3090 isspace((unsigned char) line[8]))) &&
3091 strchr(line, ':') == NULL) {
3093 * It's an S3/S5-style "include".
3095 ParseTraditionalInclude(line);
3100 if (strncmp(line, "export", 6) == 0 &&
3101 isspace((unsigned char) line[6]) &&
3102 strchr(line, ':') == NULL) {
3104 * It's a Gmake "export".
3106 ParseGmakeExport(line);
3110 if (Parse_IsVar(line)) {
3112 Parse_DoVar(line, VAR_GLOBAL);
3118 * To make life easier on novices, if the line is indented we
3119 * first make sure the line has a dependency operator in it.
3120 * If it doesn't have an operator and we're in a dependency
3121 * line's script, we assume it's actually a shell command
3122 * and add it to the current list of targets.
3125 if (isspace((unsigned char) line[0])) {
3126 while ((*cp != '\0') && isspace((unsigned char) *cp))
3128 while (*cp && (ParseIsEscaped(line, cp) ||
3129 (*cp != ':') && (*cp != '!'))) {
3134 Parse_Error(PARSE_WARNING,
3135 "Shell command needs a leading tab");
3144 * For some reason - probably to make the parser impossible -
3145 * a ';' can be used to separate commands from dependencies.
3146 * Attempt to avoid ';' inside substitution patterns.
3151 for (cp = line; *cp != 0; cp++) {
3152 if (*cp == '\\' && cp[1] != 0) {
3157 (cp[1] == '(' || cp[1] == '{')) {
3162 if (*cp == ')' || *cp == '}') {
3166 } else if (*cp == ';') {
3172 /* Terminate the dependency list at the ';' */
3178 * We now know it's a dependency line so it needs to have all
3179 * variables expanded before being parsed. Tell the variable
3180 * module to complain if some variable is undefined...
3182 line = Var_Subst(NULL, line, VAR_CMD, VARF_UNDEFERR|VARF_WANTRES);
3185 * Need a non-circular list for the target nodes
3188 Lst_Destroy(targets, NULL);
3190 targets = Lst_Init(FALSE);
3193 ParseDoDependency(line);
3196 /* If there were commands after a ';', add them now */
3202 * Reached EOF, but it may be just EOF of an include file...
3204 } while (ParseEOF() == CONTINUE);
3207 (void)fflush(stdout);
3208 (void)fprintf(stderr,
3209 "%s: Fatal errors encountered -- cannot continue",
3211 PrintOnError(NULL, NULL);
3217 *---------------------------------------------------------------------
3219 * initialize the parsing module
3225 * the parseIncPath list is initialized...
3226 *---------------------------------------------------------------------
3232 parseIncPath = Lst_Init(FALSE);
3233 sysIncPath = Lst_Init(FALSE);
3234 defIncPath = Lst_Init(FALSE);
3235 includes = Lst_Init(FALSE);
3237 targCmds = Lst_Init(FALSE);
3245 Lst_Destroy(targCmds, (FreeProc *)free);
3247 Lst_Destroy(targets, NULL);
3248 Lst_Destroy(defIncPath, Dir_Destroy);
3249 Lst_Destroy(sysIncPath, Dir_Destroy);
3250 Lst_Destroy(parseIncPath, Dir_Destroy);
3251 Lst_Destroy(includes, NULL); /* Should be empty now */
3257 *-----------------------------------------------------------------------
3259 * Return a Lst of the main target to create for main()'s sake. If
3260 * no such target exists, we Punt with an obnoxious error message.
3263 * A Lst of the single node to create.
3268 *-----------------------------------------------------------------------
3271 Parse_MainName(void)
3273 Lst mainList; /* result list */
3275 mainList = Lst_Init(FALSE);
3277 if (mainNode == NULL) {
3278 Punt("no target to make.");
3280 } else if (mainNode->type & OP_DOUBLEDEP) {
3281 (void)Lst_AtEnd(mainList, mainNode);
3282 Lst_Concat(mainList, mainNode->cohorts, LST_CONCNEW);
3285 (void)Lst_AtEnd(mainList, mainNode);
3286 Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL);
3291 *-----------------------------------------------------------------------
3293 * Add the filename and lineno to the GNode so that we remember
3294 * where it was first defined.
3299 *-----------------------------------------------------------------------
3302 ParseMark(GNode *gn)
3304 gn->fname = curFile->fname;
3305 gn->lineno = curFile->lineno;