ld.elf_so, grep, make: use mmap()
[minix3.git] / usr.bin / make / parse.c
blob3bc751e6d8aae5ac4c1ad758827ab5bfa9cf3c7e
1 /* $NetBSD: parse.c,v 1.185 2012/06/12 19:21:51 joerg Exp $ */
3 /*
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
8 * Adam de Boor.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
32 * SUCH DAMAGE.
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
40 * Adam de Boor.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
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
68 * SUCH DAMAGE.
72 #ifdef __minix
73 #define mmap minix_mmap
74 #define munmap minix_munmap
75 #ifndef MAP_COPY
76 #define MAP_COPY MAP_PRIVATE
77 #endif
78 #endif
80 #ifndef MAKE_NATIVE
81 static char rcsid[] = "$NetBSD: parse.c,v 1.185 2012/06/12 19:21:51 joerg Exp $";
82 #else
83 #include <sys/cdefs.h>
84 #ifndef lint
85 #if 0
86 static char sccsid[] = "@(#)parse.c 8.3 (Berkeley) 3/19/94";
87 #else
88 __RCSID("$NetBSD: parse.c,v 1.185 2012/06/12 19:21:51 joerg Exp $");
89 #endif
90 #endif /* not lint */
91 #endif
93 /*-
94 * parse.c --
95 * Functions to parse a makefile.
97 * One function, Parse_Init, must be called before any functions
98 * in this module are used. After that, the function Parse_File is the
99 * main entry point and controls most of the other functions in this
100 * module.
102 * Most important structures are kept in Lsts. Directories for
103 * the .include "..." function are kept in the 'parseIncPath' Lst, while
104 * those for the .include <...> are kept in the 'sysIncPath' Lst. The
105 * targets currently being defined are kept in the 'targets' Lst.
107 * The variables 'fname' and 'lineno' are used to track the name
108 * of the current file and the line number in that file so that error
109 * messages can be more meaningful.
111 * Interface:
112 * Parse_Init Initialization function which must be
113 * called before anything else in this module
114 * is used.
116 * Parse_End Cleanup the module
118 * Parse_File Function used to parse a makefile. It must
119 * be given the name of the file, which should
120 * already have been opened, and a function
121 * to call to read a character from the file.
123 * Parse_IsVar Returns TRUE if the given line is a
124 * variable assignment. Used by MainParseArgs
125 * to determine if an argument is a target
126 * or a variable assignment. Used internally
127 * for pretty much the same thing...
129 * Parse_Error Function called when an error occurs in
130 * parsing. Used by the variable and
131 * conditional modules.
132 * Parse_MainName Returns a Lst of the main target to create.
135 #include <sys/types.h>
136 #include <sys/mman.h>
137 #include <sys/stat.h>
138 #include <assert.h>
139 #include <ctype.h>
140 #include <errno.h>
141 #include <fcntl.h>
142 #include <stdarg.h>
143 #include <stdio.h>
145 #ifndef MAP_COPY
146 #define MAP_COPY MAP_PRIVATE
147 #endif
149 #include "make.h"
150 #include "hash.h"
151 #include "dir.h"
152 #include "job.h"
153 #include "buf.h"
154 #include "pathnames.h"
156 ////////////////////////////////////////////////////////////
157 // types and constants
160 * Structure for a file being read ("included file")
162 typedef struct IFile {
163 const 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 char *P_str; /* point to base of string buffer */
168 char *P_ptr; /* point to next char of string buffer */
169 char *P_end; /* point to the end of string buffer */
170 char *(*nextbuf)(void *, size_t *); /* Function to get more data */
171 void *nextbuf_arg; /* Opaque arg for nextbuf() */
172 struct loadedfile *lf; /* loadedfile object, if any */
173 } IFile;
177 * These values are returned by ParseEOF to tell Parse_File whether to
178 * CONTINUE parsing, i.e. it had only reached the end of an include file,
179 * or if it's DONE.
181 #define CONTINUE 1
182 #define DONE 0
185 * Tokens for target attributes
187 typedef enum {
188 Begin, /* .BEGIN */
189 Default, /* .DEFAULT */
190 End, /* .END */
191 dotError, /* .ERROR */
192 Ignore, /* .IGNORE */
193 Includes, /* .INCLUDES */
194 Interrupt, /* .INTERRUPT */
195 Libs, /* .LIBS */
196 Meta, /* .META */
197 MFlags, /* .MFLAGS or .MAKEFLAGS */
198 Main, /* .MAIN and we don't have anything user-specified to
199 * make */
200 NoExport, /* .NOEXPORT */
201 NoMeta, /* .NOMETA */
202 NoMetaCmp, /* .NOMETA_CMP */
203 NoPath, /* .NOPATH */
204 Not, /* Not special */
205 NotParallel, /* .NOTPARALLEL */
206 Null, /* .NULL */
207 ExObjdir, /* .OBJDIR */
208 Order, /* .ORDER */
209 Parallel, /* .PARALLEL */
210 ExPath, /* .PATH */
211 Phony, /* .PHONY */
212 #ifdef POSIX
213 Posix, /* .POSIX */
214 #endif
215 Precious, /* .PRECIOUS */
216 ExShell, /* .SHELL */
217 Silent, /* .SILENT */
218 SingleShell, /* .SINGLESHELL */
219 Suffixes, /* .SUFFIXES */
220 Wait, /* .WAIT */
221 Attribute /* Generic attribute */
222 } ParseSpecial;
225 * Other tokens
227 #define LPAREN '('
228 #define RPAREN ')'
231 ////////////////////////////////////////////////////////////
232 // result data
235 * The main target to create. This is the first target on the first
236 * dependency line in the first makefile.
238 static GNode *mainNode;
240 ////////////////////////////////////////////////////////////
241 // eval state
243 /* targets we're working on */
244 static Lst targets;
246 #ifdef CLEANUP
247 /* command lines for targets */
248 static Lst targCmds;
249 #endif
252 * specType contains the SPECial TYPE of the current target. It is
253 * Not if the target is unspecial. If it *is* special, however, the children
254 * are linked as children of the parent but not vice versa. This variable is
255 * set in ParseDoDependency
257 static ParseSpecial specType;
260 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
261 * seen, then set to each successive source on the line.
263 static GNode *predecessor;
265 ////////////////////////////////////////////////////////////
266 // parser state
268 /* true if currently in a dependency line or its commands */
269 static Boolean inLine;
271 /* number of fatal errors */
272 static int fatals = 0;
275 * Variables for doing includes
278 /* current file being read */
279 static IFile *curFile;
281 /* stack of IFiles generated by .includes */
282 static Lst includes;
284 /* include paths (lists of directories) */
285 Lst parseIncPath; /* dirs for "..." includes */
286 Lst sysIncPath; /* dirs for <...> includes */
287 Lst defIncPath; /* default for sysIncPath */
289 ////////////////////////////////////////////////////////////
290 // parser tables
293 * The parseKeywords table is searched using binary search when deciding
294 * if a target or source is special. The 'spec' field is the ParseSpecial
295 * type of the keyword ("Not" if the keyword isn't special as a target) while
296 * the 'op' field is the operator to apply to the list of targets if the
297 * keyword is used as a source ("0" if the keyword isn't special as a source)
299 static const struct {
300 const char *name; /* Name of keyword */
301 ParseSpecial spec; /* Type when used as a target */
302 int op; /* Operator when used as a source */
303 } parseKeywords[] = {
304 { ".BEGIN", Begin, 0 },
305 { ".DEFAULT", Default, 0 },
306 { ".END", End, 0 },
307 { ".ERROR", dotError, 0 },
308 { ".EXEC", Attribute, OP_EXEC },
309 { ".IGNORE", Ignore, OP_IGNORE },
310 { ".INCLUDES", Includes, 0 },
311 { ".INTERRUPT", Interrupt, 0 },
312 { ".INVISIBLE", Attribute, OP_INVISIBLE },
313 { ".JOIN", Attribute, OP_JOIN },
314 { ".LIBS", Libs, 0 },
315 { ".MADE", Attribute, OP_MADE },
316 { ".MAIN", Main, 0 },
317 { ".MAKE", Attribute, OP_MAKE },
318 { ".MAKEFLAGS", MFlags, 0 },
319 { ".META", Meta, OP_META },
320 { ".MFLAGS", MFlags, 0 },
321 { ".NOMETA", NoMeta, OP_NOMETA },
322 { ".NOMETA_CMP", NoMetaCmp, OP_NOMETA_CMP },
323 { ".NOPATH", NoPath, OP_NOPATH },
324 { ".NOTMAIN", Attribute, OP_NOTMAIN },
325 { ".NOTPARALLEL", NotParallel, 0 },
326 { ".NO_PARALLEL", NotParallel, 0 },
327 { ".NULL", Null, 0 },
328 { ".OBJDIR", ExObjdir, 0 },
329 { ".OPTIONAL", Attribute, OP_OPTIONAL },
330 { ".ORDER", Order, 0 },
331 { ".PARALLEL", Parallel, 0 },
332 { ".PATH", ExPath, 0 },
333 { ".PHONY", Phony, OP_PHONY },
334 #ifdef POSIX
335 { ".POSIX", Posix, 0 },
336 #endif
337 { ".PRECIOUS", Precious, OP_PRECIOUS },
338 { ".RECURSIVE", Attribute, OP_MAKE },
339 { ".SHELL", ExShell, 0 },
340 { ".SILENT", Silent, OP_SILENT },
341 { ".SINGLESHELL", SingleShell, 0 },
342 { ".SUFFIXES", Suffixes, 0 },
343 { ".USE", Attribute, OP_USE },
344 { ".USEBEFORE", Attribute, OP_USEBEFORE },
345 { ".WAIT", Wait, 0 },
348 ////////////////////////////////////////////////////////////
349 // local functions
351 static int ParseIsEscaped(const char *, const char *);
352 static void ParseErrorInternal(const char *, size_t, int, const char *, ...)
353 MAKE_ATTR_PRINTFLIKE(4,5);
354 static void ParseVErrorInternal(FILE *, const char *, size_t, int, const char *, va_list)
355 MAKE_ATTR_PRINTFLIKE(5, 0);
356 static int ParseFindKeyword(const char *);
357 static int ParseLinkSrc(void *, void *);
358 static int ParseDoOp(void *, void *);
359 static void ParseDoSrc(int, const char *);
360 static int ParseFindMain(void *, void *);
361 static int ParseAddDir(void *, void *);
362 static int ParseClearPath(void *, void *);
363 static void ParseDoDependency(char *);
364 static int ParseAddCmd(void *, void *);
365 static void ParseHasCommands(void *);
366 static void ParseDoInclude(char *);
367 static void ParseSetParseFile(const char *);
368 #ifdef SYSVINCLUDE
369 static void ParseTraditionalInclude(char *);
370 #endif
371 #ifdef GMAKEEXPORT
372 static void ParseGmakeExport(char *);
373 #endif
374 static int ParseEOF(void);
375 static char *ParseReadLine(void);
376 static void ParseFinishLine(void);
377 static void ParseMark(GNode *);
379 ////////////////////////////////////////////////////////////
380 // file loader
382 struct loadedfile {
383 const char *path; /* name, for error reports */
384 char *buf; /* contents buffer */
385 size_t len; /* length of contents */
386 size_t maplen; /* length of mmap area, or 0 */
387 Boolean used; /* XXX: have we used the data yet */
391 * Constructor/destructor for loadedfile
393 static struct loadedfile *
394 loadedfile_create(const char *path)
396 struct loadedfile *lf;
398 lf = bmake_malloc(sizeof(*lf));
399 lf->path = (path == NULL ? "(stdin)" : path);
400 lf->buf = NULL;
401 lf->len = 0;
402 lf->maplen = 0;
403 lf->used = FALSE;
404 return lf;
407 static void
408 loadedfile_destroy(struct loadedfile *lf)
410 if (lf->buf != NULL) {
411 if (lf->maplen > 0) {
412 munmap(lf->buf, lf->maplen);
413 } else {
414 free(lf->buf);
417 free(lf);
421 * nextbuf() operation for loadedfile, as needed by the weird and twisted
422 * logic below. Once that's cleaned up, we can get rid of lf->used...
424 static char *
425 loadedfile_nextbuf(void *x, size_t *len)
427 struct loadedfile *lf = x;
429 if (lf->used) {
430 return NULL;
432 lf->used = TRUE;
433 *len = lf->len;
434 return lf->buf;
438 * Try to get the size of a file.
440 static ReturnStatus
441 load_getsize(int fd, size_t *ret)
443 struct stat st;
445 if (fstat(fd, &st) < 0) {
446 return FAILURE;
449 if (!S_ISREG(st.st_mode)) {
450 return FAILURE;
454 * st_size is an off_t, which is 64 bits signed; *ret is
455 * size_t, which might be 32 bits unsigned or 64 bits
456 * unsigned. Rather than being elaborate, just punt on
457 * files that are more than 2^31 bytes. We should never
458 * see a makefile that size in practice...
460 * While we're at it reject negative sizes too, just in case.
462 if (st.st_size < 0 || st.st_size > 0x7fffffff) {
463 return FAILURE;
466 *ret = (size_t) st.st_size;
467 return SUCCESS;
471 * Read in a file.
473 * Until the path search logic can be moved under here instead of
474 * being in the caller in another source file, we need to have the fd
475 * passed in already open. Bleh.
477 * If the path is NULL use stdin and (to insure against fd leaks)
478 * assert that the caller passed in -1.
480 static struct loadedfile *
481 loadfile(const char *path, int fd)
483 struct loadedfile *lf;
484 long pagesize;
485 ssize_t result;
486 size_t bufpos;
488 lf = loadedfile_create(path);
490 if (path == NULL) {
491 assert(fd == -1);
492 fd = STDIN_FILENO;
493 } else {
494 #if 0 /* notyet */
495 fd = open(path, O_RDONLY);
496 if (fd < 0) {
498 Error("%s: %s", path, strerror(errno));
499 exit(1);
501 #endif
504 if (load_getsize(fd, &lf->len) == SUCCESS) {
505 /* found a size, try mmap */
506 pagesize = sysconf(_SC_PAGESIZE);
507 if (pagesize <= 0) {
508 pagesize = 0x1000;
510 /* round size up to a page */
511 lf->maplen = pagesize * ((lf->len + pagesize - 1)/pagesize);
514 * XXX hack for dealing with empty files; remove when
515 * we're no longer limited by interfacing to the old
516 * logic elsewhere in this file.
518 if (lf->maplen == 0) {
519 lf->maplen = pagesize;
523 * FUTURE: remove PROT_WRITE when the parser no longer
524 * needs to scribble on the input.
527 lf->buf = mmap(NULL, lf->maplen, PROT_READ|PROT_WRITE,
528 MAP_FILE|MAP_COPY, fd, 0);
529 if (lf->buf != MAP_FAILED) {
530 /* succeeded */
531 if (lf->len == lf->maplen && lf->buf[lf->len - 1] != '\n') {
532 char *b = malloc(lf->len + 1);
533 b[lf->len] = '\n';
534 memcpy(b, lf->buf, lf->len++);
535 munmap(lf->buf, lf->maplen);
536 lf->maplen = 0;
537 lf->buf = b;
539 goto done;
543 /* cannot mmap; load the traditional way */
545 lf->maplen = 0;
546 lf->len = 1024;
547 lf->buf = bmake_malloc(lf->len);
549 bufpos = 0;
550 while (1) {
551 assert(bufpos <= lf->len);
552 if (bufpos == lf->len) {
553 lf->len *= 2;
554 lf->buf = bmake_realloc(lf->buf, lf->len);
556 result = read(fd, lf->buf + bufpos, lf->len - bufpos);
557 if (result < 0) {
558 Error("%s: read error: %s", path, strerror(errno));
559 exit(1);
561 if (result == 0) {
562 break;
564 bufpos += result;
566 assert(bufpos <= lf->len);
567 lf->len = bufpos;
569 /* truncate malloc region to actual length (maybe not useful) */
570 if (lf->len > 0) {
571 lf->buf = bmake_realloc(lf->buf, lf->len);
574 done:
575 if (path != NULL) {
576 close(fd);
578 return lf;
581 ////////////////////////////////////////////////////////////
582 // old code
585 *----------------------------------------------------------------------
586 * ParseIsEscaped --
587 * Check if the current character is escaped on the current line
589 * Results:
590 * 0 if the character is not backslash escaped, 1 otherwise
592 * Side Effects:
593 * None
594 *----------------------------------------------------------------------
596 static int
597 ParseIsEscaped(const char *line, const char *c)
599 int active = 0;
600 for (;;) {
601 if (line == c)
602 return active;
603 if (*--c != '\\')
604 return active;
605 active = !active;
610 *----------------------------------------------------------------------
611 * ParseFindKeyword --
612 * Look in the table of keywords for one matching the given string.
614 * Input:
615 * str String to find
617 * Results:
618 * The index of the keyword, or -1 if it isn't there.
620 * Side Effects:
621 * None
622 *----------------------------------------------------------------------
624 static int
625 ParseFindKeyword(const char *str)
627 int start, end, cur;
628 int diff;
630 start = 0;
631 end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
633 do {
634 cur = start + ((end - start) / 2);
635 diff = strcmp(str, parseKeywords[cur].name);
637 if (diff == 0) {
638 return (cur);
639 } else if (diff < 0) {
640 end = cur - 1;
641 } else {
642 start = cur + 1;
644 } while (start <= end);
645 return (-1);
649 * ParseVErrorInternal --
650 * Error message abort function for parsing. Prints out the context
651 * of the error (line number and file) as well as the message with
652 * two optional arguments.
654 * Results:
655 * None
657 * Side Effects:
658 * "fatals" is incremented if the level is PARSE_FATAL.
660 /* VARARGS */
661 static void
662 ParseVErrorInternal(FILE *f, const char *cfname, size_t clineno, int type,
663 const char *fmt, va_list ap)
665 static Boolean fatal_warning_error_printed = FALSE;
667 (void)fprintf(f, "%s: ", progname);
669 if (cfname != NULL) {
670 (void)fprintf(f, "\"");
671 if (*cfname != '/' && strcmp(cfname, "(stdin)") != 0) {
672 char *cp;
673 const char *dir;
676 * Nothing is more annoying than not knowing
677 * which Makefile is the culprit.
679 dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &cp);
680 if (dir == NULL || *dir == '\0' ||
681 (*dir == '.' && dir[1] == '\0'))
682 dir = Var_Value(".CURDIR", VAR_GLOBAL, &cp);
683 if (dir == NULL)
684 dir = ".";
686 (void)fprintf(f, "%s/%s", dir, cfname);
687 } else
688 (void)fprintf(f, "%s", cfname);
690 (void)fprintf(f, "\" line %d: ", (int)clineno);
692 if (type == PARSE_WARNING)
693 (void)fprintf(f, "warning: ");
694 (void)vfprintf(f, fmt, ap);
695 (void)fprintf(f, "\n");
696 (void)fflush(f);
697 if (type == PARSE_FATAL || parseWarnFatal)
698 fatals += 1;
699 if (parseWarnFatal && !fatal_warning_error_printed) {
700 Error("parsing warnings being treated as errors");
701 fatal_warning_error_printed = TRUE;
706 * ParseErrorInternal --
707 * Error function
709 * Results:
710 * None
712 * Side Effects:
713 * None
715 /* VARARGS */
716 static void
717 ParseErrorInternal(const char *cfname, size_t clineno, int type,
718 const char *fmt, ...)
720 va_list ap;
722 va_start(ap, fmt);
723 (void)fflush(stdout);
724 ParseVErrorInternal(stderr, cfname, clineno, type, fmt, ap);
725 va_end(ap);
727 if (debug_file != stderr && debug_file != stdout) {
728 va_start(ap, fmt);
729 ParseVErrorInternal(debug_file, cfname, clineno, type, fmt, ap);
730 va_end(ap);
735 * Parse_Error --
736 * External interface to ParseErrorInternal; uses the default filename
737 * Line number.
739 * Results:
740 * None
742 * Side Effects:
743 * None
745 /* VARARGS */
746 void
747 Parse_Error(int type, const char *fmt, ...)
749 va_list ap;
750 const char *fname;
751 size_t lineno;
753 if (curFile == NULL) {
754 fname = NULL;
755 lineno = 0;
756 } else {
757 fname = curFile->fname;
758 lineno = curFile->lineno;
761 va_start(ap, fmt);
762 (void)fflush(stdout);
763 ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
764 va_end(ap);
766 if (debug_file != stderr && debug_file != stdout) {
767 va_start(ap, fmt);
768 ParseVErrorInternal(debug_file, fname, lineno, type, fmt, ap);
769 va_end(ap);
775 * ParseMessage
776 * Parse a .info .warning or .error directive
778 * The input is the line minus the ".". We substitute
779 * variables, print the message and exit(1) (for .error) or just print
780 * a warning if the directive is malformed.
782 static Boolean
783 ParseMessage(char *line)
785 int mtype;
787 switch(*line) {
788 case 'i':
789 mtype = 0;
790 break;
791 case 'w':
792 mtype = PARSE_WARNING;
793 break;
794 case 'e':
795 mtype = PARSE_FATAL;
796 break;
797 default:
798 Parse_Error(PARSE_WARNING, "invalid syntax: \".%s\"", line);
799 return FALSE;
802 while (isalpha((u_char)*line))
803 line++;
804 if (!isspace((u_char)*line))
805 return FALSE; /* not for us */
806 while (isspace((u_char)*line))
807 line++;
809 line = Var_Subst(NULL, line, VAR_CMD, 0);
810 Parse_Error(mtype, "%s", line);
811 free(line);
813 if (mtype == PARSE_FATAL) {
814 /* Terminate immediately. */
815 exit(1);
817 return TRUE;
821 *---------------------------------------------------------------------
822 * ParseLinkSrc --
823 * Link the parent node to its new child. Used in a Lst_ForEach by
824 * ParseDoDependency. If the specType isn't 'Not', the parent
825 * isn't linked as a parent of the child.
827 * Input:
828 * pgnp The parent node
829 * cgpn The child node
831 * Results:
832 * Always = 0
834 * Side Effects:
835 * New elements are added to the parents list of cgn and the
836 * children list of cgn. the unmade field of pgn is updated
837 * to reflect the additional child.
838 *---------------------------------------------------------------------
840 static int
841 ParseLinkSrc(void *pgnp, void *cgnp)
843 GNode *pgn = (GNode *)pgnp;
844 GNode *cgn = (GNode *)cgnp;
846 if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (pgn->cohorts))
847 pgn = (GNode *)Lst_Datum(Lst_Last(pgn->cohorts));
848 (void)Lst_AtEnd(pgn->children, cgn);
849 if (specType == Not)
850 (void)Lst_AtEnd(cgn->parents, pgn);
851 pgn->unmade += 1;
852 if (DEBUG(PARSE)) {
853 fprintf(debug_file, "# ParseLinkSrc: added child %s - %s\n", pgn->name, cgn->name);
854 Targ_PrintNode(pgn, 0);
855 Targ_PrintNode(cgn, 0);
857 return (0);
861 *---------------------------------------------------------------------
862 * ParseDoOp --
863 * Apply the parsed operator to the given target node. Used in a
864 * Lst_ForEach call by ParseDoDependency once all targets have
865 * been found and their operator parsed. If the previous and new
866 * operators are incompatible, a major error is taken.
868 * Input:
869 * gnp The node to which the operator is to be applied
870 * opp The operator to apply
872 * Results:
873 * Always 0
875 * Side Effects:
876 * The type field of the node is altered to reflect any new bits in
877 * the op.
878 *---------------------------------------------------------------------
880 static int
881 ParseDoOp(void *gnp, void *opp)
883 GNode *gn = (GNode *)gnp;
884 int op = *(int *)opp;
886 * If the dependency mask of the operator and the node don't match and
887 * the node has actually had an operator applied to it before, and
888 * the operator actually has some dependency information in it, complain.
890 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
891 !OP_NOP(gn->type) && !OP_NOP(op))
893 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
894 return (1);
897 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
899 * If the node was the object of a :: operator, we need to create a
900 * new instance of it for the children and commands on this dependency
901 * line. The new instance is placed on the 'cohorts' list of the
902 * initial one (note the initial one is not on its own cohorts list)
903 * and the new instance is linked to all parents of the initial
904 * instance.
906 GNode *cohort;
909 * Propagate copied bits to the initial node. They'll be propagated
910 * back to the rest of the cohorts later.
912 gn->type |= op & ~OP_OPMASK;
914 cohort = Targ_FindNode(gn->name, TARG_NOHASH);
916 * Make the cohort invisible as well to avoid duplicating it into
917 * other variables. True, parents of this target won't tend to do
918 * anything with their local variables, but better safe than
919 * sorry. (I think this is pointless now, since the relevant list
920 * traversals will no longer see this node anyway. -mycroft)
922 cohort->type = op | OP_INVISIBLE;
923 (void)Lst_AtEnd(gn->cohorts, cohort);
924 cohort->centurion = gn;
925 gn->unmade_cohorts += 1;
926 snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
927 gn->unmade_cohorts);
928 } else {
930 * We don't want to nuke any previous flags (whatever they were) so we
931 * just OR the new operator into the old
933 gn->type |= op;
936 return (0);
940 *---------------------------------------------------------------------
941 * ParseDoSrc --
942 * Given the name of a source, figure out if it is an attribute
943 * and apply it to the targets if it is. Else decide if there is
944 * some attribute which should be applied *to* the source because
945 * of some special target and apply it if so. Otherwise, make the
946 * source be a child of the targets in the list 'targets'
948 * Input:
949 * tOp operator (if any) from special targets
950 * src name of the source to handle
952 * Results:
953 * None
955 * Side Effects:
956 * Operator bits may be added to the list of targets or to the source.
957 * The targets may have a new source added to their lists of children.
958 *---------------------------------------------------------------------
960 static void
961 ParseDoSrc(int tOp, const char *src)
963 GNode *gn = NULL;
964 static int wait_number = 0;
965 char wait_src[16];
967 if (*src == '.' && isupper ((unsigned char)src[1])) {
968 int keywd = ParseFindKeyword(src);
969 if (keywd != -1) {
970 int op = parseKeywords[keywd].op;
971 if (op != 0) {
972 Lst_ForEach(targets, ParseDoOp, &op);
973 return;
975 if (parseKeywords[keywd].spec == Wait) {
977 * We add a .WAIT node in the dependency list.
978 * After any dynamic dependencies (and filename globbing)
979 * have happened, it is given a dependency on the each
980 * previous child back to and previous .WAIT node.
981 * The next child won't be scheduled until the .WAIT node
982 * is built.
983 * We give each .WAIT node a unique name (mainly for diag).
985 snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
986 gn = Targ_FindNode(wait_src, TARG_NOHASH);
987 gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
988 Lst_ForEach(targets, ParseLinkSrc, gn);
989 return;
994 switch (specType) {
995 case Main:
997 * If we have noted the existence of a .MAIN, it means we need
998 * to add the sources of said target to the list of things
999 * to create. The string 'src' is likely to be free, so we
1000 * must make a new copy of it. Note that this will only be
1001 * invoked if the user didn't specify a target on the command
1002 * line. This is to allow #ifmake's to succeed, or something...
1004 (void)Lst_AtEnd(create, bmake_strdup(src));
1006 * Add the name to the .TARGETS variable as well, so the user can
1007 * employ that, if desired.
1009 Var_Append(".TARGETS", src, VAR_GLOBAL);
1010 return;
1012 case Order:
1014 * Create proper predecessor/successor links between the previous
1015 * source and the current one.
1017 gn = Targ_FindNode(src, TARG_CREATE);
1018 if (predecessor != NULL) {
1019 (void)Lst_AtEnd(predecessor->order_succ, gn);
1020 (void)Lst_AtEnd(gn->order_pred, predecessor);
1021 if (DEBUG(PARSE)) {
1022 fprintf(debug_file, "# ParseDoSrc: added Order dependency %s - %s\n",
1023 predecessor->name, gn->name);
1024 Targ_PrintNode(predecessor, 0);
1025 Targ_PrintNode(gn, 0);
1029 * The current source now becomes the predecessor for the next one.
1031 predecessor = gn;
1032 break;
1034 default:
1036 * If the source is not an attribute, we need to find/create
1037 * a node for it. After that we can apply any operator to it
1038 * from a special target or link it to its parents, as
1039 * appropriate.
1041 * In the case of a source that was the object of a :: operator,
1042 * the attribute is applied to all of its instances (as kept in
1043 * the 'cohorts' list of the node) or all the cohorts are linked
1044 * to all the targets.
1047 /* Find/create the 'src' node and attach to all targets */
1048 gn = Targ_FindNode(src, TARG_CREATE);
1049 if (tOp) {
1050 gn->type |= tOp;
1051 } else {
1052 Lst_ForEach(targets, ParseLinkSrc, gn);
1054 break;
1059 *-----------------------------------------------------------------------
1060 * ParseFindMain --
1061 * Find a real target in the list and set it to be the main one.
1062 * Called by ParseDoDependency when a main target hasn't been found
1063 * yet.
1065 * Input:
1066 * gnp Node to examine
1068 * Results:
1069 * 0 if main not found yet, 1 if it is.
1071 * Side Effects:
1072 * mainNode is changed and Targ_SetMain is called.
1074 *-----------------------------------------------------------------------
1076 static int
1077 ParseFindMain(void *gnp, void *dummy)
1079 GNode *gn = (GNode *)gnp;
1080 if ((gn->type & OP_NOTARGET) == 0) {
1081 mainNode = gn;
1082 Targ_SetMain(gn);
1083 return (dummy ? 1 : 1);
1084 } else {
1085 return (dummy ? 0 : 0);
1090 *-----------------------------------------------------------------------
1091 * ParseAddDir --
1092 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
1094 * Results:
1095 * === 0
1097 * Side Effects:
1098 * See Dir_AddDir.
1100 *-----------------------------------------------------------------------
1102 static int
1103 ParseAddDir(void *path, void *name)
1105 (void)Dir_AddDir((Lst) path, (char *)name);
1106 return(0);
1110 *-----------------------------------------------------------------------
1111 * ParseClearPath --
1112 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
1114 * Results:
1115 * === 0
1117 * Side Effects:
1118 * See Dir_ClearPath
1120 *-----------------------------------------------------------------------
1122 static int
1123 ParseClearPath(void *path, void *dummy)
1125 Dir_ClearPath((Lst) path);
1126 return(dummy ? 0 : 0);
1130 *---------------------------------------------------------------------
1131 * ParseDoDependency --
1132 * Parse the dependency line in line.
1134 * Input:
1135 * line the line to parse
1137 * Results:
1138 * None
1140 * Side Effects:
1141 * The nodes of the sources are linked as children to the nodes of the
1142 * targets. Some nodes may be created.
1144 * We parse a dependency line by first extracting words from the line and
1145 * finding nodes in the list of all targets with that name. This is done
1146 * until a character is encountered which is an operator character. Currently
1147 * these are only ! and :. At this point the operator is parsed and the
1148 * pointer into the line advanced until the first source is encountered.
1149 * The parsed operator is applied to each node in the 'targets' list,
1150 * which is where the nodes found for the targets are kept, by means of
1151 * the ParseDoOp function.
1152 * The sources are read in much the same way as the targets were except
1153 * that now they are expanded using the wildcarding scheme of the C-Shell
1154 * and all instances of the resulting words in the list of all targets
1155 * are found. Each of the resulting nodes is then linked to each of the
1156 * targets as one of its children.
1157 * Certain targets are handled specially. These are the ones detailed
1158 * by the specType variable.
1159 * The storing of transformation rules is also taken care of here.
1160 * A target is recognized as a transformation rule by calling
1161 * Suff_IsTransform. If it is a transformation rule, its node is gotten
1162 * from the suffix module via Suff_AddTransform rather than the standard
1163 * Targ_FindNode in the target module.
1164 *---------------------------------------------------------------------
1166 static void
1167 ParseDoDependency(char *line)
1169 char *cp; /* our current position */
1170 GNode *gn = NULL; /* a general purpose temporary node */
1171 int op; /* the operator on the line */
1172 char savec; /* a place to save a character */
1173 Lst paths; /* List of search paths to alter when parsing
1174 * a list of .PATH targets */
1175 int tOp; /* operator from special target */
1176 Lst sources; /* list of archive source names after
1177 * expansion */
1178 Lst curTargs; /* list of target names to be found and added
1179 * to the targets list */
1180 char *lstart = line;
1182 if (DEBUG(PARSE))
1183 fprintf(debug_file, "ParseDoDependency(%s)\n", line);
1184 tOp = 0;
1186 specType = Not;
1187 paths = NULL;
1189 curTargs = Lst_Init(FALSE);
1191 do {
1192 for (cp = line; *cp && (ParseIsEscaped(lstart, cp) ||
1193 !(isspace((unsigned char)*cp) ||
1194 *cp == '!' || *cp == ':' || *cp == LPAREN));
1195 cp++) {
1196 if (*cp == '$') {
1198 * Must be a dynamic source (would have been expanded
1199 * otherwise), so call the Var module to parse the puppy
1200 * so we can safely advance beyond it...There should be
1201 * no errors in this, as they would have been discovered
1202 * in the initial Var_Subst and we wouldn't be here.
1204 int length;
1205 void *freeIt;
1206 char *result;
1208 result = Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
1209 if (freeIt)
1210 free(freeIt);
1211 cp += length-1;
1215 if (!ParseIsEscaped(lstart, cp) && *cp == LPAREN) {
1217 * Archives must be handled specially to make sure the OP_ARCHV
1218 * flag is set in their 'type' field, for one thing, and because
1219 * things like "archive(file1.o file2.o file3.o)" are permissible.
1220 * Arch_ParseArchive will set 'line' to be the first non-blank
1221 * after the archive-spec. It creates/finds nodes for the members
1222 * and places them on the given list, returning SUCCESS if all
1223 * went well and FAILURE if there was an error in the
1224 * specification. On error, line should remain untouched.
1226 if (Arch_ParseArchive(&line, targets, VAR_CMD) != SUCCESS) {
1227 Parse_Error(PARSE_FATAL,
1228 "Error in archive specification: \"%s\"", line);
1229 goto out;
1230 } else {
1231 continue;
1234 savec = *cp;
1236 if (!*cp) {
1238 * Ending a dependency line without an operator is a Bozo
1239 * no-no. As a heuristic, this is also often triggered by
1240 * undetected conflicts from cvs/rcs merges.
1242 if ((strncmp(line, "<<<<<<", 6) == 0) ||
1243 (strncmp(line, "======", 6) == 0) ||
1244 (strncmp(line, ">>>>>>", 6) == 0))
1245 Parse_Error(PARSE_FATAL,
1246 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
1247 else
1248 Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive"
1249 : "Need an operator");
1250 goto out;
1252 *cp = '\0';
1255 * Have a word in line. See if it's a special target and set
1256 * specType to match it.
1258 if (*line == '.' && isupper ((unsigned char)line[1])) {
1260 * See if the target is a special target that must have it
1261 * or its sources handled specially.
1263 int keywd = ParseFindKeyword(line);
1264 if (keywd != -1) {
1265 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
1266 Parse_Error(PARSE_FATAL, "Mismatched special targets");
1267 goto out;
1270 specType = parseKeywords[keywd].spec;
1271 tOp = parseKeywords[keywd].op;
1274 * Certain special targets have special semantics:
1275 * .PATH Have to set the dirSearchPath
1276 * variable too
1277 * .MAIN Its sources are only used if
1278 * nothing has been specified to
1279 * create.
1280 * .DEFAULT Need to create a node to hang
1281 * commands on, but we don't want
1282 * it in the graph, nor do we want
1283 * it to be the Main Target, so we
1284 * create it, set OP_NOTMAIN and
1285 * add it to the list, setting
1286 * DEFAULT to the new node for
1287 * later use. We claim the node is
1288 * A transformation rule to make
1289 * life easier later, when we'll
1290 * use Make_HandleUse to actually
1291 * apply the .DEFAULT commands.
1292 * .PHONY The list of targets
1293 * .NOPATH Don't search for file in the path
1294 * .BEGIN
1295 * .END
1296 * .ERROR
1297 * .INTERRUPT Are not to be considered the
1298 * main target.
1299 * .NOTPARALLEL Make only one target at a time.
1300 * .SINGLESHELL Create a shell for each command.
1301 * .ORDER Must set initial predecessor to NULL
1303 switch (specType) {
1304 case ExPath:
1305 if (paths == NULL) {
1306 paths = Lst_Init(FALSE);
1308 (void)Lst_AtEnd(paths, dirSearchPath);
1309 break;
1310 case Main:
1311 if (!Lst_IsEmpty(create)) {
1312 specType = Not;
1314 break;
1315 case Begin:
1316 case End:
1317 case dotError:
1318 case Interrupt:
1319 gn = Targ_FindNode(line, TARG_CREATE);
1320 gn->type |= OP_NOTMAIN|OP_SPECIAL;
1321 (void)Lst_AtEnd(targets, gn);
1322 break;
1323 case Default:
1324 gn = Targ_NewGN(".DEFAULT");
1325 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
1326 (void)Lst_AtEnd(targets, gn);
1327 DEFAULT = gn;
1328 break;
1329 case NotParallel:
1330 maxJobs = 1;
1331 break;
1332 case SingleShell:
1333 compatMake = TRUE;
1334 break;
1335 case Order:
1336 predecessor = NULL;
1337 break;
1338 default:
1339 break;
1341 } else if (strncmp(line, ".PATH", 5) == 0) {
1343 * .PATH<suffix> has to be handled specially.
1344 * Call on the suffix module to give us a path to
1345 * modify.
1347 Lst path;
1349 specType = ExPath;
1350 path = Suff_GetPath(&line[5]);
1351 if (path == NULL) {
1352 Parse_Error(PARSE_FATAL,
1353 "Suffix '%s' not defined (yet)",
1354 &line[5]);
1355 goto out;
1356 } else {
1357 if (paths == NULL) {
1358 paths = Lst_Init(FALSE);
1360 (void)Lst_AtEnd(paths, path);
1366 * Have word in line. Get or create its node and stick it at
1367 * the end of the targets list
1369 if ((specType == Not) && (*line != '\0')) {
1370 if (Dir_HasWildcards(line)) {
1372 * Targets are to be sought only in the current directory,
1373 * so create an empty path for the thing. Note we need to
1374 * use Dir_Destroy in the destruction of the path as the
1375 * Dir module could have added a directory to the path...
1377 Lst emptyPath = Lst_Init(FALSE);
1379 Dir_Expand(line, emptyPath, curTargs);
1381 Lst_Destroy(emptyPath, Dir_Destroy);
1382 } else {
1384 * No wildcards, but we want to avoid code duplication,
1385 * so create a list with the word on it.
1387 (void)Lst_AtEnd(curTargs, line);
1390 while(!Lst_IsEmpty(curTargs)) {
1391 char *targName = (char *)Lst_DeQueue(curTargs);
1393 if (!Suff_IsTransform (targName)) {
1394 gn = Targ_FindNode(targName, TARG_CREATE);
1395 } else {
1396 gn = Suff_AddTransform(targName);
1399 (void)Lst_AtEnd(targets, gn);
1401 } else if (specType == ExPath && *line != '.' && *line != '\0') {
1402 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
1405 *cp = savec;
1407 * If it is a special type and not .PATH, it's the only target we
1408 * allow on this line...
1410 if (specType != Not && specType != ExPath) {
1411 Boolean warning = FALSE;
1413 while (*cp && (ParseIsEscaped(lstart, cp) ||
1414 ((*cp != '!') && (*cp != ':')))) {
1415 if (ParseIsEscaped(lstart, cp) ||
1416 (*cp != ' ' && *cp != '\t')) {
1417 warning = TRUE;
1419 cp++;
1421 if (warning) {
1422 Parse_Error(PARSE_WARNING, "Extra target ignored");
1424 } else {
1425 while (*cp && isspace ((unsigned char)*cp)) {
1426 cp++;
1429 line = cp;
1430 } while (*line && (ParseIsEscaped(lstart, line) ||
1431 ((*line != '!') && (*line != ':'))));
1434 * Don't need the list of target names anymore...
1436 Lst_Destroy(curTargs, NULL);
1437 curTargs = NULL;
1439 if (!Lst_IsEmpty(targets)) {
1440 switch(specType) {
1441 default:
1442 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
1443 break;
1444 case Default:
1445 case Begin:
1446 case End:
1447 case dotError:
1448 case Interrupt:
1450 * These four create nodes on which to hang commands, so
1451 * targets shouldn't be empty...
1453 case Not:
1455 * Nothing special here -- targets can be empty if it wants.
1457 break;
1462 * Have now parsed all the target names. Must parse the operator next. The
1463 * result is left in op .
1465 if (*cp == '!') {
1466 op = OP_FORCE;
1467 } else if (*cp == ':') {
1468 if (cp[1] == ':') {
1469 op = OP_DOUBLEDEP;
1470 cp++;
1471 } else {
1472 op = OP_DEPENDS;
1474 } else {
1475 Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive"
1476 : "Missing dependency operator");
1477 goto out;
1480 cp++; /* Advance beyond operator */
1482 Lst_ForEach(targets, ParseDoOp, &op);
1485 * Get to the first source
1487 while (*cp && isspace ((unsigned char)*cp)) {
1488 cp++;
1490 line = cp;
1493 * Several special targets take different actions if present with no
1494 * sources:
1495 * a .SUFFIXES line with no sources clears out all old suffixes
1496 * a .PRECIOUS line makes all targets precious
1497 * a .IGNORE line ignores errors for all targets
1498 * a .SILENT line creates silence when making all targets
1499 * a .PATH removes all directories from the search path(s).
1501 if (!*line) {
1502 switch (specType) {
1503 case Suffixes:
1504 Suff_ClearSuffixes();
1505 break;
1506 case Precious:
1507 allPrecious = TRUE;
1508 break;
1509 case Ignore:
1510 ignoreErrors = TRUE;
1511 break;
1512 case Silent:
1513 beSilent = TRUE;
1514 break;
1515 case ExPath:
1516 Lst_ForEach(paths, ParseClearPath, NULL);
1517 Dir_SetPATH();
1518 break;
1519 #ifdef POSIX
1520 case Posix:
1521 Var_Set("%POSIX", "1003.2", VAR_GLOBAL, 0);
1522 break;
1523 #endif
1524 default:
1525 break;
1527 } else if (specType == MFlags) {
1529 * Call on functions in main.c to deal with these arguments and
1530 * set the initial character to a null-character so the loop to
1531 * get sources won't get anything
1533 Main_ParseArgLine(line);
1534 *line = '\0';
1535 } else if (specType == ExShell) {
1536 if (Job_ParseShell(line) != SUCCESS) {
1537 Parse_Error(PARSE_FATAL, "improper shell specification");
1538 goto out;
1540 *line = '\0';
1541 } else if ((specType == NotParallel) || (specType == SingleShell)) {
1542 *line = '\0';
1546 * NOW GO FOR THE SOURCES
1548 if ((specType == Suffixes) || (specType == ExPath) ||
1549 (specType == Includes) || (specType == Libs) ||
1550 (specType == Null) || (specType == ExObjdir))
1552 while (*line) {
1554 * If the target was one that doesn't take files as its sources
1555 * but takes something like suffixes, we take each
1556 * space-separated word on the line as a something and deal
1557 * with it accordingly.
1559 * If the target was .SUFFIXES, we take each source as a
1560 * suffix and add it to the list of suffixes maintained by the
1561 * Suff module.
1563 * If the target was a .PATH, we add the source as a directory
1564 * to search on the search path.
1566 * If it was .INCLUDES, the source is taken to be the suffix of
1567 * files which will be #included and whose search path should
1568 * be present in the .INCLUDES variable.
1570 * If it was .LIBS, the source is taken to be the suffix of
1571 * files which are considered libraries and whose search path
1572 * should be present in the .LIBS variable.
1574 * If it was .NULL, the source is the suffix to use when a file
1575 * has no valid suffix.
1577 * If it was .OBJDIR, the source is a new definition for .OBJDIR,
1578 * and will cause make to do a new chdir to that path.
1580 while (*cp && !isspace ((unsigned char)*cp)) {
1581 cp++;
1583 savec = *cp;
1584 *cp = '\0';
1585 switch (specType) {
1586 case Suffixes:
1587 Suff_AddSuffix(line, &mainNode);
1588 break;
1589 case ExPath:
1590 Lst_ForEach(paths, ParseAddDir, line);
1591 break;
1592 case Includes:
1593 Suff_AddInclude(line);
1594 break;
1595 case Libs:
1596 Suff_AddLib(line);
1597 break;
1598 case Null:
1599 Suff_SetNull(line);
1600 break;
1601 case ExObjdir:
1602 Main_SetObjdir(line);
1603 break;
1604 default:
1605 break;
1607 *cp = savec;
1608 if (savec != '\0') {
1609 cp++;
1611 while (*cp && isspace ((unsigned char)*cp)) {
1612 cp++;
1614 line = cp;
1616 if (paths) {
1617 Lst_Destroy(paths, NULL);
1619 if (specType == ExPath)
1620 Dir_SetPATH();
1621 } else {
1622 while (*line) {
1624 * The targets take real sources, so we must beware of archive
1625 * specifications (i.e. things with left parentheses in them)
1626 * and handle them accordingly.
1628 for (; *cp && !isspace ((unsigned char)*cp); cp++) {
1629 if ((*cp == LPAREN) && (cp > line) && (cp[-1] != '$')) {
1631 * Only stop for a left parenthesis if it isn't at the
1632 * start of a word (that'll be for variable changes
1633 * later) and isn't preceded by a dollar sign (a dynamic
1634 * source).
1636 break;
1640 if (*cp == LPAREN) {
1641 sources = Lst_Init(FALSE);
1642 if (Arch_ParseArchive(&line, sources, VAR_CMD) != SUCCESS) {
1643 Parse_Error(PARSE_FATAL,
1644 "Error in source archive spec \"%s\"", line);
1645 goto out;
1648 while (!Lst_IsEmpty (sources)) {
1649 gn = (GNode *)Lst_DeQueue(sources);
1650 ParseDoSrc(tOp, gn->name);
1652 Lst_Destroy(sources, NULL);
1653 cp = line;
1654 } else {
1655 if (*cp) {
1656 *cp = '\0';
1657 cp += 1;
1660 ParseDoSrc(tOp, line);
1662 while (*cp && isspace ((unsigned char)*cp)) {
1663 cp++;
1665 line = cp;
1669 if (mainNode == NULL) {
1671 * If we have yet to decide on a main target to make, in the
1672 * absence of any user input, we want the first target on
1673 * the first dependency line that is actually a real target
1674 * (i.e. isn't a .USE or .EXEC rule) to be made.
1676 Lst_ForEach(targets, ParseFindMain, NULL);
1679 out:
1680 if (curTargs)
1681 Lst_Destroy(curTargs, NULL);
1685 *---------------------------------------------------------------------
1686 * Parse_IsVar --
1687 * Return TRUE if the passed line is a variable assignment. A variable
1688 * assignment consists of a single word followed by optional whitespace
1689 * followed by either a += or an = operator.
1690 * This function is used both by the Parse_File function and main when
1691 * parsing the command-line arguments.
1693 * Input:
1694 * line the line to check
1696 * Results:
1697 * TRUE if it is. FALSE if it ain't
1699 * Side Effects:
1700 * none
1701 *---------------------------------------------------------------------
1703 Boolean
1704 Parse_IsVar(char *line)
1706 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1707 char ch;
1708 int level = 0;
1709 #define ISEQOPERATOR(c) \
1710 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1713 * Skip to variable name
1715 for (;(*line == ' ') || (*line == '\t'); line++)
1716 continue;
1718 /* Scan for one of the assignment operators outside a variable expansion */
1719 while ((ch = *line++) != 0) {
1720 if (ch == '(' || ch == '{') {
1721 level++;
1722 continue;
1724 if (ch == ')' || ch == '}') {
1725 level--;
1726 continue;
1728 if (level != 0)
1729 continue;
1730 while (ch == ' ' || ch == '\t') {
1731 ch = *line++;
1732 wasSpace = TRUE;
1734 if (ch == '=')
1735 return TRUE;
1736 if (*line == '=' && ISEQOPERATOR(ch))
1737 return TRUE;
1738 if (wasSpace)
1739 return FALSE;
1742 return FALSE;
1746 *---------------------------------------------------------------------
1747 * Parse_DoVar --
1748 * Take the variable assignment in the passed line and do it in the
1749 * global context.
1751 * Note: There is a lexical ambiguity with assignment modifier characters
1752 * in variable names. This routine interprets the character before the =
1753 * as a modifier. Therefore, an assignment like
1754 * C++=/usr/bin/CC
1755 * is interpreted as "C+ +=" instead of "C++ =".
1757 * Input:
1758 * line a line guaranteed to be a variable assignment.
1759 * This reduces error checks
1760 * ctxt Context in which to do the assignment
1762 * Results:
1763 * none
1765 * Side Effects:
1766 * the variable structure of the given variable name is altered in the
1767 * global context.
1768 *---------------------------------------------------------------------
1770 void
1771 Parse_DoVar(char *line, GNode *ctxt)
1773 char *cp; /* pointer into line */
1774 enum {
1775 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1776 } type; /* Type of assignment */
1777 char *opc; /* ptr to operator character to
1778 * null-terminate the variable name */
1779 Boolean freeCp = FALSE; /* TRUE if cp needs to be freed,
1780 * i.e. if any variable expansion was
1781 * performed */
1782 int depth;
1785 * Skip to variable name
1787 while ((*line == ' ') || (*line == '\t')) {
1788 line++;
1792 * Skip to operator character, nulling out whitespace as we go
1793 * XXX Rather than counting () and {} we should look for $ and
1794 * then expand the variable.
1796 for (depth = 0, cp = line + 1; depth != 0 || *cp != '='; cp++) {
1797 if (*cp == '(' || *cp == '{') {
1798 depth++;
1799 continue;
1801 if (*cp == ')' || *cp == '}') {
1802 depth--;
1803 continue;
1805 if (depth == 0 && isspace ((unsigned char)*cp)) {
1806 *cp = '\0';
1809 opc = cp-1; /* operator is the previous character */
1810 *cp++ = '\0'; /* nuke the = */
1813 * Check operator type
1815 switch (*opc) {
1816 case '+':
1817 type = VAR_APPEND;
1818 *opc = '\0';
1819 break;
1821 case '?':
1823 * If the variable already has a value, we don't do anything.
1825 *opc = '\0';
1826 if (Var_Exists(line, ctxt)) {
1827 return;
1828 } else {
1829 type = VAR_NORMAL;
1831 break;
1833 case ':':
1834 type = VAR_SUBST;
1835 *opc = '\0';
1836 break;
1838 case '!':
1839 type = VAR_SHELL;
1840 *opc = '\0';
1841 break;
1843 default:
1844 #ifdef SUNSHCMD
1845 while (opc > line && *opc != ':')
1846 opc--;
1848 if (strncmp(opc, ":sh", 3) == 0) {
1849 type = VAR_SHELL;
1850 *opc = '\0';
1851 break;
1853 #endif
1854 type = VAR_NORMAL;
1855 break;
1858 while (isspace ((unsigned char)*cp)) {
1859 cp++;
1862 if (type == VAR_APPEND) {
1863 Var_Append(line, cp, ctxt);
1864 } else if (type == VAR_SUBST) {
1866 * Allow variables in the old value to be undefined, but leave their
1867 * invocation alone -- this is done by forcing oldVars to be false.
1868 * XXX: This can cause recursive variables, but that's not hard to do,
1869 * and this allows someone to do something like
1871 * CFLAGS = $(.INCLUDES)
1872 * CFLAGS := -I.. $(CFLAGS)
1874 * And not get an error.
1876 Boolean oldOldVars = oldVars;
1878 oldVars = FALSE;
1881 * make sure that we set the variable the first time to nothing
1882 * so that it gets substituted!
1884 if (!Var_Exists(line, ctxt))
1885 Var_Set(line, "", ctxt, 0);
1887 cp = Var_Subst(NULL, cp, ctxt, FALSE);
1888 oldVars = oldOldVars;
1889 freeCp = TRUE;
1891 Var_Set(line, cp, ctxt, 0);
1892 } else if (type == VAR_SHELL) {
1893 char *res;
1894 const char *error;
1896 if (strchr(cp, '$') != NULL) {
1898 * There's a dollar sign in the command, so perform variable
1899 * expansion on the whole thing. The resulting string will need
1900 * freeing when we're done, so set freeCmd to TRUE.
1902 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1903 freeCp = TRUE;
1906 res = Cmd_Exec(cp, &error);
1907 Var_Set(line, res, ctxt, 0);
1908 free(res);
1910 if (error)
1911 Parse_Error(PARSE_WARNING, error, cp);
1912 } else {
1914 * Normal assignment -- just do it.
1916 Var_Set(line, cp, ctxt, 0);
1918 if (strcmp(line, MAKEOVERRIDES) == 0)
1919 Main_ExportMAKEFLAGS(FALSE); /* re-export MAKEFLAGS */
1920 else if (strcmp(line, ".CURDIR") == 0) {
1922 * Somone is being (too?) clever...
1923 * Let's pretend they know what they are doing and
1924 * re-initialize the 'cur' Path.
1926 Dir_InitCur(cp);
1927 Dir_SetPATH();
1928 } else if (strcmp(line, MAKE_JOB_PREFIX) == 0) {
1929 Job_SetPrefix();
1930 } else if (strcmp(line, MAKE_EXPORTED) == 0) {
1931 Var_Export(cp, 0);
1933 if (freeCp)
1934 free(cp);
1939 * ParseAddCmd --
1940 * Lst_ForEach function to add a command line to all targets
1942 * Input:
1943 * gnp the node to which the command is to be added
1944 * cmd the command to add
1946 * Results:
1947 * Always 0
1949 * Side Effects:
1950 * A new element is added to the commands list of the node.
1952 static int
1953 ParseAddCmd(void *gnp, void *cmd)
1955 GNode *gn = (GNode *)gnp;
1957 /* Add to last (ie current) cohort for :: targets */
1958 if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts))
1959 gn = (GNode *)Lst_Datum(Lst_Last(gn->cohorts));
1961 /* if target already supplied, ignore commands */
1962 if (!(gn->type & OP_HAS_COMMANDS)) {
1963 (void)Lst_AtEnd(gn->commands, cmd);
1964 ParseMark(gn);
1965 } else {
1966 #ifdef notyet
1967 /* XXX: We cannot do this until we fix the tree */
1968 (void)Lst_AtEnd(gn->commands, cmd);
1969 Parse_Error(PARSE_WARNING,
1970 "overriding commands for target \"%s\"; "
1971 "previous commands defined at %s: %d ignored",
1972 gn->name, gn->fname, gn->lineno);
1973 #else
1974 Parse_Error(PARSE_WARNING,
1975 "duplicate script for target \"%s\" ignored",
1976 gn->name);
1977 ParseErrorInternal(gn->fname, gn->lineno, PARSE_WARNING,
1978 "using previous script for \"%s\" defined here",
1979 gn->name);
1980 #endif
1982 return(0);
1986 *-----------------------------------------------------------------------
1987 * ParseHasCommands --
1988 * Callback procedure for Parse_File when destroying the list of
1989 * targets on the last dependency line. Marks a target as already
1990 * having commands if it does, to keep from having shell commands
1991 * on multiple dependency lines.
1993 * Input:
1994 * gnp Node to examine
1996 * Results:
1997 * None
1999 * Side Effects:
2000 * OP_HAS_COMMANDS may be set for the target.
2002 *-----------------------------------------------------------------------
2004 static void
2005 ParseHasCommands(void *gnp)
2007 GNode *gn = (GNode *)gnp;
2008 if (!Lst_IsEmpty(gn->commands)) {
2009 gn->type |= OP_HAS_COMMANDS;
2014 *-----------------------------------------------------------------------
2015 * Parse_AddIncludeDir --
2016 * Add a directory to the path searched for included makefiles
2017 * bracketed by double-quotes. Used by functions in main.c
2019 * Input:
2020 * dir The name of the directory to add
2022 * Results:
2023 * None.
2025 * Side Effects:
2026 * The directory is appended to the list.
2028 *-----------------------------------------------------------------------
2030 void
2031 Parse_AddIncludeDir(char *dir)
2033 (void)Dir_AddDir(parseIncPath, dir);
2037 *---------------------------------------------------------------------
2038 * ParseDoInclude --
2039 * Push to another file.
2041 * The input is the line minus the `.'. A file spec is a string
2042 * enclosed in <> or "". The former is looked for only in sysIncPath.
2043 * The latter in . and the directories specified by -I command line
2044 * options
2046 * Results:
2047 * None
2049 * Side Effects:
2050 * A structure is added to the includes Lst and readProc, lineno,
2051 * fname and curFILE are altered for the new file
2052 *---------------------------------------------------------------------
2055 static void
2056 Parse_include_file(char *file, Boolean isSystem, int silent)
2058 struct loadedfile *lf;
2059 char *fullname; /* full pathname of file */
2060 char *newName;
2061 char *prefEnd, *incdir;
2062 int fd;
2063 int i;
2066 * Now we know the file's name and its search path, we attempt to
2067 * find the durn thing. A return of NULL indicates the file don't
2068 * exist.
2070 fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
2072 if (fullname == NULL && !isSystem) {
2074 * Include files contained in double-quotes are first searched for
2075 * relative to the including file's location. We don't want to
2076 * cd there, of course, so we just tack on the old file's
2077 * leading path components and call Dir_FindFile to see if
2078 * we can locate the beast.
2081 incdir = bmake_strdup(curFile->fname);
2082 prefEnd = strrchr(incdir, '/');
2083 if (prefEnd != NULL) {
2084 *prefEnd = '\0';
2085 /* Now do lexical processing of leading "../" on the filename */
2086 for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
2087 prefEnd = strrchr(incdir + 1, '/');
2088 if (prefEnd == NULL || strcmp(prefEnd, "/..") == 0)
2089 break;
2090 *prefEnd = '\0';
2092 newName = str_concat(incdir, file + i, STR_ADDSLASH);
2093 fullname = Dir_FindFile(newName, parseIncPath);
2094 if (fullname == NULL)
2095 fullname = Dir_FindFile(newName, dirSearchPath);
2096 free(newName);
2098 free(incdir);
2100 if (fullname == NULL) {
2102 * Makefile wasn't found in same directory as included makefile.
2103 * Search for it first on the -I search path,
2104 * then on the .PATH search path, if not found in a -I directory.
2105 * If we have a suffix specific path we should use that.
2107 char *suff;
2108 Lst suffPath = NULL;
2110 if ((suff = strrchr(file, '.'))) {
2111 suffPath = Suff_GetPath(suff);
2112 if (suffPath != NULL) {
2113 fullname = Dir_FindFile(file, suffPath);
2116 if (fullname == NULL) {
2117 fullname = Dir_FindFile(file, parseIncPath);
2118 if (fullname == NULL) {
2119 fullname = Dir_FindFile(file, dirSearchPath);
2125 /* Looking for a system file or file still not found */
2126 if (fullname == NULL) {
2128 * Look for it on the system path
2130 fullname = Dir_FindFile(file,
2131 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
2134 if (fullname == NULL) {
2135 if (!silent)
2136 Parse_Error(PARSE_FATAL, "Could not find %s", file);
2137 return;
2140 /* Actually open the file... */
2141 fd = open(fullname, O_RDONLY);
2142 if (fd == -1) {
2143 if (!silent)
2144 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
2145 free(fullname);
2146 return;
2149 /* load it */
2150 lf = loadfile(fullname, fd);
2152 /* Start reading from this file next */
2153 Parse_SetInput(fullname, 0, -1, loadedfile_nextbuf, lf);
2154 curFile->lf = lf;
2157 static void
2158 ParseDoInclude(char *line)
2160 char endc; /* the character which ends the file spec */
2161 char *cp; /* current position in file spec */
2162 int silent = (*line != 'i') ? 1 : 0;
2163 char *file = &line[7 + silent];
2165 /* Skip to delimiter character so we know where to look */
2166 while (*file == ' ' || *file == '\t')
2167 file++;
2169 if (*file != '"' && *file != '<') {
2170 Parse_Error(PARSE_FATAL,
2171 ".include filename must be delimited by '\"' or '<'");
2172 return;
2176 * Set the search path on which to find the include file based on the
2177 * characters which bracket its name. Angle-brackets imply it's
2178 * a system Makefile while double-quotes imply it's a user makefile
2180 if (*file == '<') {
2181 endc = '>';
2182 } else {
2183 endc = '"';
2186 /* Skip to matching delimiter */
2187 for (cp = ++file; *cp && *cp != endc; cp++)
2188 continue;
2190 if (*cp != endc) {
2191 Parse_Error(PARSE_FATAL,
2192 "Unclosed %cinclude filename. '%c' expected",
2193 '.', endc);
2194 return;
2196 *cp = '\0';
2199 * Substitute for any variables in the file name before trying to
2200 * find the thing.
2202 file = Var_Subst(NULL, file, VAR_CMD, FALSE);
2204 Parse_include_file(file, endc == '>', silent);
2205 free(file);
2210 *---------------------------------------------------------------------
2211 * ParseSetParseFile --
2212 * Set the .PARSEDIR and .PARSEFILE variables to the dirname and
2213 * basename of the given filename
2215 * Results:
2216 * None
2218 * Side Effects:
2219 * The .PARSEDIR and .PARSEFILE variables are overwritten by the
2220 * dirname and basename of the given filename.
2221 *---------------------------------------------------------------------
2223 static void
2224 ParseSetParseFile(const char *filename)
2226 char *slash, *dirname;
2227 const char *pd, *pf;
2228 int len;
2230 slash = strrchr(filename, '/');
2231 if (slash == NULL) {
2232 Var_Set(".PARSEDIR", pd = curdir, VAR_GLOBAL, 0);
2233 Var_Set(".PARSEFILE", pf = filename, VAR_GLOBAL, 0);
2234 dirname= NULL;
2235 } else {
2236 len = slash - filename;
2237 dirname = bmake_malloc(len + 1);
2238 memcpy(dirname, filename, len);
2239 dirname[len] = '\0';
2240 Var_Set(".PARSEDIR", pd = dirname, VAR_GLOBAL, 0);
2241 Var_Set(".PARSEFILE", pf = slash + 1, VAR_GLOBAL, 0);
2243 if (DEBUG(PARSE))
2244 fprintf(debug_file, "ParseSetParseFile: ${.PARSEDIR} = `%s' "
2245 "${.PARSEFILE} = `%s'\n", pd, pf);
2246 free(dirname);
2250 * Track the makefiles we read - so makefiles can
2251 * set dependencies on them.
2252 * Avoid adding anything more than once.
2255 static void
2256 ParseTrackInput(const char *name)
2258 char *old;
2259 char *fp = NULL;
2260 size_t name_len = strlen(name);
2262 old = Var_Value(MAKE_MAKEFILES, VAR_GLOBAL, &fp);
2263 if (old) {
2264 /* does it contain name? */
2265 for (; old != NULL; old = strchr(old, ' ')) {
2266 if (*old == ' ')
2267 old++;
2268 if (memcmp(old, name, name_len) == 0
2269 && (old[name_len] == 0 || old[name_len] == ' '))
2270 goto cleanup;
2273 Var_Append (MAKE_MAKEFILES, name, VAR_GLOBAL);
2274 cleanup:
2275 if (fp) {
2276 free(fp);
2282 *---------------------------------------------------------------------
2283 * Parse_setInput --
2284 * Start Parsing from the given source
2286 * Results:
2287 * None
2289 * Side Effects:
2290 * A structure is added to the includes Lst and readProc, lineno,
2291 * fname and curFile are altered for the new file
2292 *---------------------------------------------------------------------
2294 void
2295 Parse_SetInput(const char *name, int line, int fd,
2296 char *(*nextbuf)(void *, size_t *), void *arg)
2298 char *buf;
2299 size_t len;
2301 if (name == NULL)
2302 name = curFile->fname;
2303 else
2304 ParseTrackInput(name);
2306 if (DEBUG(PARSE))
2307 fprintf(debug_file, "Parse_SetInput: file %s, line %d, fd %d, nextbuf %p, arg %p\n",
2308 name, line, fd, nextbuf, arg);
2310 if (fd == -1 && nextbuf == NULL)
2311 /* sanity */
2312 return;
2314 if (curFile != NULL)
2315 /* Save exiting file info */
2316 Lst_AtFront(includes, curFile);
2318 /* Allocate and fill in new structure */
2319 curFile = bmake_malloc(sizeof *curFile);
2322 * Once the previous state has been saved, we can get down to reading
2323 * the new file. We set up the name of the file to be the absolute
2324 * name of the include file so error messages refer to the right
2325 * place.
2327 curFile->fname = name;
2328 curFile->lineno = line;
2329 curFile->first_lineno = line;
2330 curFile->nextbuf = nextbuf;
2331 curFile->nextbuf_arg = arg;
2332 curFile->lf = NULL;
2334 assert(nextbuf != NULL);
2336 /* Get first block of input data */
2337 buf = curFile->nextbuf(curFile->nextbuf_arg, &len);
2338 if (buf == NULL) {
2339 /* Was all a waste of time ... */
2340 free(curFile);
2341 return;
2343 curFile->P_str = buf;
2344 curFile->P_ptr = buf;
2345 curFile->P_end = buf+len;
2347 curFile->cond_depth = Cond_save_depth();
2348 ParseSetParseFile(name);
2351 #ifdef SYSVINCLUDE
2353 *---------------------------------------------------------------------
2354 * ParseTraditionalInclude --
2355 * Push to another file.
2357 * The input is the current line. The file name(s) are
2358 * following the "include".
2360 * Results:
2361 * None
2363 * Side Effects:
2364 * A structure is added to the includes Lst and readProc, lineno,
2365 * fname and curFILE are altered for the new file
2366 *---------------------------------------------------------------------
2368 static void
2369 ParseTraditionalInclude(char *line)
2371 char *cp; /* current position in file spec */
2372 int done = 0;
2373 int silent = (line[0] != 'i') ? 1 : 0;
2374 char *file = &line[silent + 7];
2375 char *all_files;
2377 if (DEBUG(PARSE)) {
2378 fprintf(debug_file, "ParseTraditionalInclude: %s\n", file);
2382 * Skip over whitespace
2384 while (isspace((unsigned char)*file))
2385 file++;
2388 * Substitute for any variables in the file name before trying to
2389 * find the thing.
2391 all_files = Var_Subst(NULL, file, VAR_CMD, FALSE);
2393 if (*file == '\0') {
2394 Parse_Error(PARSE_FATAL,
2395 "Filename missing from \"include\"");
2396 return;
2399 for (file = all_files; !done; file = cp + 1) {
2400 /* Skip to end of line or next whitespace */
2401 for (cp = file; *cp && !isspace((unsigned char) *cp); cp++)
2402 continue;
2404 if (*cp)
2405 *cp = '\0';
2406 else
2407 done = 1;
2409 Parse_include_file(file, FALSE, silent);
2411 free(all_files);
2413 #endif
2415 #ifdef GMAKEEXPORT
2417 *---------------------------------------------------------------------
2418 * ParseGmakeExport --
2419 * Parse export <variable>=<value>
2421 * And set the environment with it.
2423 * Results:
2424 * None
2426 * Side Effects:
2427 * None
2428 *---------------------------------------------------------------------
2430 static void
2431 ParseGmakeExport(char *line)
2433 char *variable = &line[6];
2434 char *value;
2436 if (DEBUG(PARSE)) {
2437 fprintf(debug_file, "ParseGmakeExport: %s\n", variable);
2441 * Skip over whitespace
2443 while (isspace((unsigned char)*variable))
2444 variable++;
2446 for (value = variable; *value && *value != '='; value++)
2447 continue;
2449 if (*value != '=') {
2450 Parse_Error(PARSE_FATAL,
2451 "Variable/Value missing from \"export\"");
2452 return;
2456 * Expand the value before putting it in the environment.
2458 value = Var_Subst(NULL, value, VAR_CMD, FALSE);
2459 setenv(variable, value, 1);
2461 #endif
2464 *---------------------------------------------------------------------
2465 * ParseEOF --
2466 * Called when EOF is reached in the current file. If we were reading
2467 * an include file, the includes stack is popped and things set up
2468 * to go back to reading the previous file at the previous location.
2470 * Results:
2471 * CONTINUE if there's more to do. DONE if not.
2473 * Side Effects:
2474 * The old curFILE, is closed. The includes list is shortened.
2475 * lineno, curFILE, and fname are changed if CONTINUE is returned.
2476 *---------------------------------------------------------------------
2478 static int
2479 ParseEOF(void)
2481 char *ptr;
2482 size_t len;
2484 assert(curFile->nextbuf != NULL);
2486 /* get next input buffer, if any */
2487 ptr = curFile->nextbuf(curFile->nextbuf_arg, &len);
2488 curFile->P_ptr = ptr;
2489 curFile->P_str = ptr;
2490 curFile->P_end = ptr + len;
2491 curFile->lineno = curFile->first_lineno;
2492 if (ptr != NULL) {
2493 /* Iterate again */
2494 return CONTINUE;
2497 /* Ensure the makefile (or loop) didn't have mismatched conditionals */
2498 Cond_restore_depth(curFile->cond_depth);
2500 if (curFile->lf != NULL) {
2501 loadedfile_destroy(curFile->lf);
2502 curFile->lf = NULL;
2505 /* Dispose of curFile info */
2506 /* Leak curFile->fname because all the gnodes have pointers to it */
2507 free(curFile->P_str);
2508 free(curFile);
2510 curFile = Lst_DeQueue(includes);
2512 if (curFile == NULL) {
2513 /* We've run out of input */
2514 Var_Delete(".PARSEDIR", VAR_GLOBAL);
2515 Var_Delete(".PARSEFILE", VAR_GLOBAL);
2516 return DONE;
2519 if (DEBUG(PARSE))
2520 fprintf(debug_file, "ParseEOF: returning to file %s, line %d\n",
2521 curFile->fname, curFile->lineno);
2523 /* Restore the PARSEDIR/PARSEFILE variables */
2524 ParseSetParseFile(curFile->fname);
2525 return (CONTINUE);
2528 #define PARSE_RAW 1
2529 #define PARSE_SKIP 2
2531 static char *
2532 ParseGetLine(int flags, int *length)
2534 IFile *cf = curFile;
2535 char *ptr;
2536 char ch;
2537 char *line;
2538 char *line_end;
2539 char *escaped;
2540 char *comment;
2541 char *tp;
2543 /* Loop through blank lines and comment lines */
2544 for (;;) {
2545 cf->lineno++;
2546 line = cf->P_ptr;
2547 ptr = line;
2548 line_end = line;
2549 escaped = NULL;
2550 comment = NULL;
2551 for (;;) {
2552 if (cf->P_end != NULL && ptr == cf->P_end) {
2553 /* end of buffer */
2554 ch = 0;
2555 break;
2557 ch = *ptr;
2558 if (ch == 0 || (ch == '\\' && ptr[1] == 0)) {
2559 if (cf->P_end == NULL)
2560 /* End of string (aka for loop) data */
2561 break;
2562 if (cf->nextbuf != NULL) {
2564 * End of this buffer; return EOF and outer logic
2565 * will get the next one. (eww)
2567 break;
2569 Parse_Error(PARSE_FATAL, "Zero byte read from file");
2570 return NULL;
2573 if (ch == '\\') {
2574 /* Don't treat next character as special, remember first one */
2575 if (escaped == NULL)
2576 escaped = ptr;
2577 if (ptr[1] == '\n')
2578 cf->lineno++;
2579 ptr += 2;
2580 line_end = ptr;
2581 continue;
2583 if (ch == '#' && comment == NULL) {
2584 /* Remember first '#' for comment stripping */
2585 /* Unless previous char was '[', as in modifier :[#] */
2586 if (!(ptr > line && ptr[-1] == '['))
2587 comment = line_end;
2589 ptr++;
2590 if (ch == '\n')
2591 break;
2592 if (!isspace((unsigned char)ch))
2593 /* We are not interested in trailing whitespace */
2594 line_end = ptr;
2597 /* Save next 'to be processed' location */
2598 cf->P_ptr = ptr;
2600 /* Check we have a non-comment, non-blank line */
2601 if (line_end == line || comment == line) {
2602 if (ch == 0)
2603 /* At end of file */
2604 return NULL;
2605 /* Parse another line */
2606 continue;
2609 /* We now have a line of data */
2610 *line_end = 0;
2612 if (flags & PARSE_RAW) {
2613 /* Leave '\' (etc) in line buffer (eg 'for' lines) */
2614 *length = line_end - line;
2615 return line;
2618 if (flags & PARSE_SKIP) {
2619 /* Completely ignore non-directives */
2620 if (line[0] != '.')
2621 continue;
2622 /* We could do more of the .else/.elif/.endif checks here */
2624 break;
2627 /* Brutally ignore anything after a non-escaped '#' in non-commands */
2628 if (comment != NULL && line[0] != '\t') {
2629 line_end = comment;
2630 *line_end = 0;
2633 /* If we didn't see a '\\' then the in-situ data is fine */
2634 if (escaped == NULL) {
2635 *length = line_end - line;
2636 return line;
2639 /* Remove escapes from '\n' and '#' */
2640 tp = ptr = escaped;
2641 escaped = line;
2642 for (; ; *tp++ = ch) {
2643 ch = *ptr++;
2644 if (ch != '\\') {
2645 if (ch == 0)
2646 break;
2647 continue;
2650 ch = *ptr++;
2651 if (ch == 0) {
2652 /* Delete '\\' at end of buffer */
2653 tp--;
2654 break;
2657 if (ch == '#' && line[0] != '\t')
2658 /* Delete '\\' from before '#' on non-command lines */
2659 continue;
2661 if (ch != '\n') {
2662 /* Leave '\\' in buffer for later */
2663 *tp++ = '\\';
2664 /* Make sure we don't delete an escaped ' ' from the line end */
2665 escaped = tp + 1;
2666 continue;
2669 /* Escaped '\n' replace following whitespace with a single ' ' */
2670 while (ptr[0] == ' ' || ptr[0] == '\t')
2671 ptr++;
2672 ch = ' ';
2675 /* Delete any trailing spaces - eg from empty continuations */
2676 while (tp > escaped && isspace((unsigned char)tp[-1]))
2677 tp--;
2679 *tp = 0;
2680 *length = tp - line;
2681 return line;
2685 *---------------------------------------------------------------------
2686 * ParseReadLine --
2687 * Read an entire line from the input file. Called only by Parse_File.
2689 * Results:
2690 * A line w/o its newline
2692 * Side Effects:
2693 * Only those associated with reading a character
2694 *---------------------------------------------------------------------
2696 static char *
2697 ParseReadLine(void)
2699 char *line; /* Result */
2700 int lineLength; /* Length of result */
2701 int lineno; /* Saved line # */
2702 int rval;
2704 for (;;) {
2705 line = ParseGetLine(0, &lineLength);
2706 if (line == NULL)
2707 return NULL;
2709 if (line[0] != '.')
2710 return line;
2713 * The line might be a conditional. Ask the conditional module
2714 * about it and act accordingly
2716 switch (Cond_Eval(line)) {
2717 case COND_SKIP:
2718 /* Skip to next conditional that evaluates to COND_PARSE. */
2719 do {
2720 line = ParseGetLine(PARSE_SKIP, &lineLength);
2721 } while (line && Cond_Eval(line) != COND_PARSE);
2722 if (line == NULL)
2723 break;
2724 continue;
2725 case COND_PARSE:
2726 continue;
2727 case COND_INVALID: /* Not a conditional line */
2728 /* Check for .for loops */
2729 rval = For_Eval(line);
2730 if (rval == 0)
2731 /* Not a .for line */
2732 break;
2733 if (rval < 0)
2734 /* Syntax error - error printed, ignore line */
2735 continue;
2736 /* Start of a .for loop */
2737 lineno = curFile->lineno;
2738 /* Accumulate loop lines until matching .endfor */
2739 do {
2740 line = ParseGetLine(PARSE_RAW, &lineLength);
2741 if (line == NULL) {
2742 Parse_Error(PARSE_FATAL,
2743 "Unexpected end of file in for loop.");
2744 break;
2746 } while (For_Accum(line));
2747 /* Stash each iteration as a new 'input file' */
2748 For_Run(lineno);
2749 /* Read next line from for-loop buffer */
2750 continue;
2752 return (line);
2757 *-----------------------------------------------------------------------
2758 * ParseFinishLine --
2759 * Handle the end of a dependency group.
2761 * Results:
2762 * Nothing.
2764 * Side Effects:
2765 * inLine set FALSE. 'targets' list destroyed.
2767 *-----------------------------------------------------------------------
2769 static void
2770 ParseFinishLine(void)
2772 if (inLine) {
2773 Lst_ForEach(targets, Suff_EndTransform, NULL);
2774 Lst_Destroy(targets, ParseHasCommands);
2775 targets = NULL;
2776 inLine = FALSE;
2782 *---------------------------------------------------------------------
2783 * Parse_File --
2784 * Parse a file into its component parts, incorporating it into the
2785 * current dependency graph. This is the main function and controls
2786 * almost every other function in this module
2788 * Input:
2789 * name the name of the file being read
2790 * fd Open file to makefile to parse
2792 * Results:
2793 * None
2795 * Side Effects:
2796 * closes fd.
2797 * Loads. Nodes are added to the list of all targets, nodes and links
2798 * are added to the dependency graph. etc. etc. etc.
2799 *---------------------------------------------------------------------
2801 void
2802 Parse_File(const char *name, int fd)
2804 char *cp; /* pointer into the line */
2805 char *line; /* the line we're working on */
2806 struct loadedfile *lf;
2808 lf = loadfile(name, fd);
2810 inLine = FALSE;
2811 fatals = 0;
2813 if (name == NULL) {
2814 name = "(stdin)";
2817 Parse_SetInput(name, 0, -1, loadedfile_nextbuf, lf);
2818 curFile->lf = lf;
2820 do {
2821 for (; (line = ParseReadLine()) != NULL; ) {
2822 if (DEBUG(PARSE))
2823 fprintf(debug_file, "ParseReadLine (%d): '%s'\n",
2824 curFile->lineno, line);
2825 if (*line == '.') {
2827 * Lines that begin with the special character may be
2828 * include or undef directives.
2829 * On the other hand they can be suffix rules (.c.o: ...)
2830 * or just dependencies for filenames that start '.'.
2832 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2833 continue;
2835 if (strncmp(cp, "include", 7) == 0 ||
2836 ((cp[0] == 's' || cp[0] == '-') &&
2837 strncmp(&cp[1], "include", 7) == 0)) {
2838 ParseDoInclude(cp);
2839 continue;
2841 if (strncmp(cp, "undef", 5) == 0) {
2842 char *cp2;
2843 for (cp += 5; isspace((unsigned char) *cp); cp++)
2844 continue;
2845 for (cp2 = cp; !isspace((unsigned char) *cp2) &&
2846 (*cp2 != '\0'); cp2++)
2847 continue;
2848 *cp2 = '\0';
2849 Var_Delete(cp, VAR_GLOBAL);
2850 continue;
2851 } else if (strncmp(cp, "export", 6) == 0) {
2852 for (cp += 6; isspace((unsigned char) *cp); cp++)
2853 continue;
2854 Var_Export(cp, 1);
2855 continue;
2856 } else if (strncmp(cp, "unexport", 8) == 0) {
2857 Var_UnExport(cp);
2858 continue;
2859 } else if (strncmp(cp, "info", 4) == 0 ||
2860 strncmp(cp, "error", 5) == 0 ||
2861 strncmp(cp, "warning", 7) == 0) {
2862 if (ParseMessage(cp))
2863 continue;
2867 if (*line == '\t') {
2869 * If a line starts with a tab, it can only hope to be
2870 * a creation command.
2872 cp = line + 1;
2873 shellCommand:
2874 for (; isspace ((unsigned char)*cp); cp++) {
2875 continue;
2877 if (*cp) {
2878 if (!inLine)
2879 Parse_Error(PARSE_FATAL,
2880 "Unassociated shell command \"%s\"",
2881 cp);
2883 * So long as it's not a blank line and we're actually
2884 * in a dependency spec, add the command to the list of
2885 * commands of all targets in the dependency spec
2887 if (targets) {
2888 cp = bmake_strdup(cp);
2889 Lst_ForEach(targets, ParseAddCmd, cp);
2890 #ifdef CLEANUP
2891 Lst_AtEnd(targCmds, cp);
2892 #endif
2895 continue;
2898 #ifdef SYSVINCLUDE
2899 if (((strncmp(line, "include", 7) == 0 &&
2900 isspace((unsigned char) line[7])) ||
2901 ((line[0] == 's' || line[0] == '-') &&
2902 strncmp(&line[1], "include", 7) == 0 &&
2903 isspace((unsigned char) line[8]))) &&
2904 strchr(line, ':') == NULL) {
2906 * It's an S3/S5-style "include".
2908 ParseTraditionalInclude(line);
2909 continue;
2911 #endif
2912 #ifdef GMAKEEXPORT
2913 if (strncmp(line, "export", 6) == 0 &&
2914 isspace((unsigned char) line[6]) &&
2915 strchr(line, ':') == NULL) {
2917 * It's a Gmake "export".
2919 ParseGmakeExport(line);
2920 continue;
2922 #endif
2923 if (Parse_IsVar(line)) {
2924 ParseFinishLine();
2925 Parse_DoVar(line, VAR_GLOBAL);
2926 continue;
2929 #ifndef POSIX
2931 * To make life easier on novices, if the line is indented we
2932 * first make sure the line has a dependency operator in it.
2933 * If it doesn't have an operator and we're in a dependency
2934 * line's script, we assume it's actually a shell command
2935 * and add it to the current list of targets.
2937 cp = line;
2938 if (isspace((unsigned char) line[0])) {
2939 while ((*cp != '\0') && isspace((unsigned char) *cp))
2940 cp++;
2941 while (*cp && (ParseIsEscaped(line, cp) ||
2942 (*cp != ':') && (*cp != '!'))) {
2943 cp++;
2945 if (*cp == '\0') {
2946 if (inLine) {
2947 Parse_Error(PARSE_WARNING,
2948 "Shell command needs a leading tab");
2949 goto shellCommand;
2953 #endif
2954 ParseFinishLine();
2957 * For some reason - probably to make the parser impossible -
2958 * a ';' can be used to separate commands from dependencies.
2959 * Attempt to avoid ';' inside substitution patterns.
2962 int level = 0;
2964 for (cp = line; *cp != 0; cp++) {
2965 if (*cp == '\\' && cp[1] != 0) {
2966 cp++;
2967 continue;
2969 if (*cp == '$' &&
2970 (cp[1] == '(' || cp[1] == '{')) {
2971 level++;
2972 continue;
2974 if (level > 0) {
2975 if (*cp == ')' || *cp == '}') {
2976 level--;
2977 continue;
2979 } else if (*cp == ';') {
2980 break;
2984 if (*cp != 0)
2985 /* Terminate the dependency list at the ';' */
2986 *cp++ = 0;
2987 else
2988 cp = NULL;
2991 * We now know it's a dependency line so it needs to have all
2992 * variables expanded before being parsed. Tell the variable
2993 * module to complain if some variable is undefined...
2995 line = Var_Subst(NULL, line, VAR_CMD, TRUE);
2998 * Need a non-circular list for the target nodes
3000 if (targets)
3001 Lst_Destroy(targets, NULL);
3003 targets = Lst_Init(FALSE);
3004 inLine = TRUE;
3006 ParseDoDependency(line);
3007 free(line);
3009 /* If there were commands after a ';', add them now */
3010 if (cp != NULL) {
3011 goto shellCommand;
3015 * Reached EOF, but it may be just EOF of an include file...
3017 } while (ParseEOF() == CONTINUE);
3019 if (fatals) {
3020 (void)fflush(stdout);
3021 (void)fprintf(stderr,
3022 "%s: Fatal errors encountered -- cannot continue",
3023 progname);
3024 PrintOnError(NULL, NULL);
3025 exit(1);
3030 *---------------------------------------------------------------------
3031 * Parse_Init --
3032 * initialize the parsing module
3034 * Results:
3035 * none
3037 * Side Effects:
3038 * the parseIncPath list is initialized...
3039 *---------------------------------------------------------------------
3041 void
3042 Parse_Init(void)
3044 mainNode = NULL;
3045 parseIncPath = Lst_Init(FALSE);
3046 sysIncPath = Lst_Init(FALSE);
3047 defIncPath = Lst_Init(FALSE);
3048 includes = Lst_Init(FALSE);
3049 #ifdef CLEANUP
3050 targCmds = Lst_Init(FALSE);
3051 #endif
3054 void
3055 Parse_End(void)
3057 #ifdef CLEANUP
3058 Lst_Destroy(targCmds, (FreeProc *)free);
3059 if (targets)
3060 Lst_Destroy(targets, NULL);
3061 Lst_Destroy(defIncPath, Dir_Destroy);
3062 Lst_Destroy(sysIncPath, Dir_Destroy);
3063 Lst_Destroy(parseIncPath, Dir_Destroy);
3064 Lst_Destroy(includes, NULL); /* Should be empty now */
3065 #endif
3070 *-----------------------------------------------------------------------
3071 * Parse_MainName --
3072 * Return a Lst of the main target to create for main()'s sake. If
3073 * no such target exists, we Punt with an obnoxious error message.
3075 * Results:
3076 * A Lst of the single node to create.
3078 * Side Effects:
3079 * None.
3081 *-----------------------------------------------------------------------
3084 Parse_MainName(void)
3086 Lst mainList; /* result list */
3088 mainList = Lst_Init(FALSE);
3090 if (mainNode == NULL) {
3091 Punt("no target to make.");
3092 /*NOTREACHED*/
3093 } else if (mainNode->type & OP_DOUBLEDEP) {
3094 (void)Lst_AtEnd(mainList, mainNode);
3095 Lst_Concat(mainList, mainNode->cohorts, LST_CONCNEW);
3097 else
3098 (void)Lst_AtEnd(mainList, mainNode);
3099 Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL);
3100 return (mainList);
3104 *-----------------------------------------------------------------------
3105 * ParseMark --
3106 * Add the filename and lineno to the GNode so that we remember
3107 * where it was first defined.
3109 * Side Effects:
3110 * None.
3112 *-----------------------------------------------------------------------
3114 static void
3115 ParseMark(GNode *gn)
3117 gn->fname = curFile->fname;
3118 gn->lineno = curFile->lineno;