parse.c revision 1.187 1 /* $NetBSD: parse.c,v 1.187 2013/03/05 22:01:44 christos Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
9 *
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.
21 *
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.
33 */
34
35 /*
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
38 *
39 * This code is derived from software contributed to Berkeley by
40 * Adam de Boor.
41 *
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.
57 *
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.
69 */
70
71 #ifndef MAKE_NATIVE
72 static char rcsid[] = "$NetBSD: parse.c,v 1.187 2013/03/05 22:01:44 christos Exp $";
73 #else
74 #include <sys/cdefs.h>
75 #ifndef lint
76 #if 0
77 static char sccsid[] = "@(#)parse.c 8.3 (Berkeley) 3/19/94";
78 #else
79 __RCSID("$NetBSD: parse.c,v 1.187 2013/03/05 22:01:44 christos Exp $");
80 #endif
81 #endif /* not lint */
82 #endif
83
84 /*-
85 * parse.c --
86 * Functions to parse a makefile.
87 *
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
91 * module.
92 *
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.
97 *
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.
101 *
102 * Interface:
103 * Parse_Init Initialization function which must be
104 * called before anything else in this module
105 * is used.
106 *
107 * Parse_End Cleanup the module
108 *
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.
113 *
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...
119 *
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.
124 */
125
126 #include <sys/types.h>
127 #include <sys/mman.h>
128 #include <sys/stat.h>
129 #include <assert.h>
130 #include <ctype.h>
131 #include <errno.h>
132 #include <fcntl.h>
133 #include <stdarg.h>
134 #include <stdio.h>
135
136 #ifndef MAP_FILE
137 #define MAP_FILE 0
138 #endif
139 #ifndef MAP_COPY
140 #define MAP_COPY MAP_PRIVATE
141 #endif
142
143 #include "make.h"
144 #include "hash.h"
145 #include "dir.h"
146 #include "job.h"
147 #include "buf.h"
148 #include "pathnames.h"
149
150 ////////////////////////////////////////////////////////////
151 // types and constants
152
153 /*
154 * Structure for a file being read ("included file")
155 */
156 typedef struct IFile {
157 const char *fname; /* name of file */
158 int lineno; /* current line number in file */
159 int first_lineno; /* line number of start of text */
160 int cond_depth; /* 'if' nesting when file opened */
161 char *P_str; /* point to base of string buffer */
162 char *P_ptr; /* point to next char of string buffer */
163 char *P_end; /* point to the end of string buffer */
164 char *(*nextbuf)(void *, size_t *); /* Function to get more data */
165 void *nextbuf_arg; /* Opaque arg for nextbuf() */
166 struct loadedfile *lf; /* loadedfile object, if any */
167 } IFile;
168
169
170 /*
171 * These values are returned by ParseEOF to tell Parse_File whether to
172 * CONTINUE parsing, i.e. it had only reached the end of an include file,
173 * or if it's DONE.
174 */
175 #define CONTINUE 1
176 #define DONE 0
177
178 /*
179 * Tokens for target attributes
180 */
181 typedef enum {
182 Begin, /* .BEGIN */
183 Default, /* .DEFAULT */
184 End, /* .END */
185 dotError, /* .ERROR */
186 Ignore, /* .IGNORE */
187 Includes, /* .INCLUDES */
188 Interrupt, /* .INTERRUPT */
189 Libs, /* .LIBS */
190 Meta, /* .META */
191 MFlags, /* .MFLAGS or .MAKEFLAGS */
192 Main, /* .MAIN and we don't have anything user-specified to
193 * make */
194 NoExport, /* .NOEXPORT */
195 NoMeta, /* .NOMETA */
196 NoMetaCmp, /* .NOMETA_CMP */
197 NoPath, /* .NOPATH */
198 Not, /* Not special */
199 NotParallel, /* .NOTPARALLEL */
200 Null, /* .NULL */
201 ExObjdir, /* .OBJDIR */
202 Order, /* .ORDER */
203 Parallel, /* .PARALLEL */
204 ExPath, /* .PATH */
205 Phony, /* .PHONY */
206 #ifdef POSIX
207 Posix, /* .POSIX */
208 #endif
209 Precious, /* .PRECIOUS */
210 ExShell, /* .SHELL */
211 Silent, /* .SILENT */
212 SingleShell, /* .SINGLESHELL */
213 Stale, /* .STALE */
214 Suffixes, /* .SUFFIXES */
215 Wait, /* .WAIT */
216 Attribute /* Generic attribute */
217 } ParseSpecial;
218
219 /*
220 * Other tokens
221 */
222 #define LPAREN '('
223 #define RPAREN ')'
224
225
226 ////////////////////////////////////////////////////////////
227 // result data
228
229 /*
230 * The main target to create. This is the first target on the first
231 * dependency line in the first makefile.
232 */
233 static GNode *mainNode;
234
235 ////////////////////////////////////////////////////////////
236 // eval state
237
238 /* targets we're working on */
239 static Lst targets;
240
241 #ifdef CLEANUP
242 /* command lines for targets */
243 static Lst targCmds;
244 #endif
245
246 /*
247 * specType contains the SPECial TYPE of the current target. It is
248 * Not if the target is unspecial. If it *is* special, however, the children
249 * are linked as children of the parent but not vice versa. This variable is
250 * set in ParseDoDependency
251 */
252 static ParseSpecial specType;
253
254 /*
255 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
256 * seen, then set to each successive source on the line.
257 */
258 static GNode *predecessor;
259
260 ////////////////////////////////////////////////////////////
261 // parser state
262
263 /* true if currently in a dependency line or its commands */
264 static Boolean inLine;
265
266 /* number of fatal errors */
267 static int fatals = 0;
268
269 /*
270 * Variables for doing includes
271 */
272
273 /* current file being read */
274 static IFile *curFile;
275
276 /* stack of IFiles generated by .includes */
277 static Lst includes;
278
279 /* include paths (lists of directories) */
280 Lst parseIncPath; /* dirs for "..." includes */
281 Lst sysIncPath; /* dirs for <...> includes */
282 Lst defIncPath; /* default for sysIncPath */
283
284 ////////////////////////////////////////////////////////////
285 // parser tables
286
287 /*
288 * The parseKeywords table is searched using binary search when deciding
289 * if a target or source is special. The 'spec' field is the ParseSpecial
290 * type of the keyword ("Not" if the keyword isn't special as a target) while
291 * the 'op' field is the operator to apply to the list of targets if the
292 * keyword is used as a source ("0" if the keyword isn't special as a source)
293 */
294 static const struct {
295 const char *name; /* Name of keyword */
296 ParseSpecial spec; /* Type when used as a target */
297 int op; /* Operator when used as a source */
298 } parseKeywords[] = {
299 { ".BEGIN", Begin, 0 },
300 { ".DEFAULT", Default, 0 },
301 { ".END", End, 0 },
302 { ".ERROR", dotError, 0 },
303 { ".EXEC", Attribute, OP_EXEC },
304 { ".IGNORE", Ignore, OP_IGNORE },
305 { ".INCLUDES", Includes, 0 },
306 { ".INTERRUPT", Interrupt, 0 },
307 { ".INVISIBLE", Attribute, OP_INVISIBLE },
308 { ".JOIN", Attribute, OP_JOIN },
309 { ".LIBS", Libs, 0 },
310 { ".MADE", Attribute, OP_MADE },
311 { ".MAIN", Main, 0 },
312 { ".MAKE", Attribute, OP_MAKE },
313 { ".MAKEFLAGS", MFlags, 0 },
314 { ".META", Meta, OP_META },
315 { ".MFLAGS", MFlags, 0 },
316 { ".NOMETA", NoMeta, OP_NOMETA },
317 { ".NOMETA_CMP", NoMetaCmp, OP_NOMETA_CMP },
318 { ".NOPATH", NoPath, OP_NOPATH },
319 { ".NOTMAIN", Attribute, OP_NOTMAIN },
320 { ".NOTPARALLEL", NotParallel, 0 },
321 { ".NO_PARALLEL", NotParallel, 0 },
322 { ".NULL", Null, 0 },
323 { ".OBJDIR", ExObjdir, 0 },
324 { ".OPTIONAL", Attribute, OP_OPTIONAL },
325 { ".ORDER", Order, 0 },
326 { ".PARALLEL", Parallel, 0 },
327 { ".PATH", ExPath, 0 },
328 { ".PHONY", Phony, OP_PHONY },
329 #ifdef POSIX
330 { ".POSIX", Posix, 0 },
331 #endif
332 { ".PRECIOUS", Precious, OP_PRECIOUS },
333 { ".RECURSIVE", Attribute, OP_MAKE },
334 { ".SHELL", ExShell, 0 },
335 { ".SILENT", Silent, OP_SILENT },
336 { ".SINGLESHELL", SingleShell, 0 },
337 { ".STALE", Stale, 0 },
338 { ".SUFFIXES", Suffixes, 0 },
339 { ".USE", Attribute, OP_USE },
340 { ".USEBEFORE", Attribute, OP_USEBEFORE },
341 { ".WAIT", Wait, 0 },
342 };
343
344 ////////////////////////////////////////////////////////////
345 // local functions
346
347 static int ParseIsEscaped(const char *, const char *);
348 static void ParseErrorInternal(const char *, size_t, int, const char *, ...)
349 MAKE_ATTR_PRINTFLIKE(4,5);
350 static void ParseVErrorInternal(FILE *, const char *, size_t, int, const char *, va_list)
351 MAKE_ATTR_PRINTFLIKE(5, 0);
352 static int ParseFindKeyword(const char *);
353 static int ParseLinkSrc(void *, void *);
354 static int ParseDoOp(void *, void *);
355 static void ParseDoSrc(int, const char *);
356 static int ParseFindMain(void *, void *);
357 static int ParseAddDir(void *, void *);
358 static int ParseClearPath(void *, void *);
359 static void ParseDoDependency(char *);
360 static int ParseAddCmd(void *, void *);
361 static void ParseHasCommands(void *);
362 static void ParseDoInclude(char *);
363 static void ParseSetParseFile(const char *);
364 #ifdef SYSVINCLUDE
365 static void ParseTraditionalInclude(char *);
366 #endif
367 #ifdef GMAKEEXPORT
368 static void ParseGmakeExport(char *);
369 #endif
370 static int ParseEOF(void);
371 static char *ParseReadLine(void);
372 static void ParseFinishLine(void);
373 static void ParseMark(GNode *);
374
375 ////////////////////////////////////////////////////////////
376 // file loader
377
378 struct loadedfile {
379 const char *path; /* name, for error reports */
380 char *buf; /* contents buffer */
381 size_t len; /* length of contents */
382 size_t maplen; /* length of mmap area, or 0 */
383 Boolean used; /* XXX: have we used the data yet */
384 };
385
386 /*
387 * Constructor/destructor for loadedfile
388 */
389 static struct loadedfile *
390 loadedfile_create(const char *path)
391 {
392 struct loadedfile *lf;
393
394 lf = bmake_malloc(sizeof(*lf));
395 lf->path = (path == NULL ? "(stdin)" : path);
396 lf->buf = NULL;
397 lf->len = 0;
398 lf->maplen = 0;
399 lf->used = FALSE;
400 return lf;
401 }
402
403 static void
404 loadedfile_destroy(struct loadedfile *lf)
405 {
406 if (lf->buf != NULL) {
407 if (lf->maplen > 0) {
408 munmap(lf->buf, lf->maplen);
409 } else {
410 free(lf->buf);
411 }
412 }
413 free(lf);
414 }
415
416 /*
417 * nextbuf() operation for loadedfile, as needed by the weird and twisted
418 * logic below. Once that's cleaned up, we can get rid of lf->used...
419 */
420 static char *
421 loadedfile_nextbuf(void *x, size_t *len)
422 {
423 struct loadedfile *lf = x;
424
425 if (lf->used) {
426 return NULL;
427 }
428 lf->used = TRUE;
429 *len = lf->len;
430 return lf->buf;
431 }
432
433 /*
434 * Try to get the size of a file.
435 */
436 static ReturnStatus
437 load_getsize(int fd, size_t *ret)
438 {
439 struct stat st;
440
441 if (fstat(fd, &st) < 0) {
442 return FAILURE;
443 }
444
445 if (!S_ISREG(st.st_mode)) {
446 return FAILURE;
447 }
448
449 /*
450 * st_size is an off_t, which is 64 bits signed; *ret is
451 * size_t, which might be 32 bits unsigned or 64 bits
452 * unsigned. Rather than being elaborate, just punt on
453 * files that are more than 2^31 bytes. We should never
454 * see a makefile that size in practice...
455 *
456 * While we're at it reject negative sizes too, just in case.
457 */
458 if (st.st_size < 0 || st.st_size > 0x7fffffff) {
459 return FAILURE;
460 }
461
462 *ret = (size_t) st.st_size;
463 return SUCCESS;
464 }
465
466 /*
467 * Read in a file.
468 *
469 * Until the path search logic can be moved under here instead of
470 * being in the caller in another source file, we need to have the fd
471 * passed in already open. Bleh.
472 *
473 * If the path is NULL use stdin and (to insure against fd leaks)
474 * assert that the caller passed in -1.
475 */
476 static struct loadedfile *
477 loadfile(const char *path, int fd)
478 {
479 struct loadedfile *lf;
480 long pagesize;
481 ssize_t result;
482 size_t bufpos;
483
484 lf = loadedfile_create(path);
485
486 if (path == NULL) {
487 assert(fd == -1);
488 fd = STDIN_FILENO;
489 } else {
490 #if 0 /* notyet */
491 fd = open(path, O_RDONLY);
492 if (fd < 0) {
493 ...
494 Error("%s: %s", path, strerror(errno));
495 exit(1);
496 }
497 #endif
498 }
499
500 if (load_getsize(fd, &lf->len) == SUCCESS) {
501 /* found a size, try mmap */
502 pagesize = sysconf(_SC_PAGESIZE);
503 if (pagesize <= 0) {
504 pagesize = 0x1000;
505 }
506 /* round size up to a page */
507 lf->maplen = pagesize * ((lf->len + pagesize - 1)/pagesize);
508
509 /*
510 * XXX hack for dealing with empty files; remove when
511 * we're no longer limited by interfacing to the old
512 * logic elsewhere in this file.
513 */
514 if (lf->maplen == 0) {
515 lf->maplen = pagesize;
516 }
517
518 /*
519 * FUTURE: remove PROT_WRITE when the parser no longer
520 * needs to scribble on the input.
521 */
522 lf->buf = mmap(NULL, lf->maplen, PROT_READ|PROT_WRITE,
523 MAP_FILE|MAP_COPY, fd, 0);
524 if (lf->buf != MAP_FAILED) {
525 /* succeeded */
526 if (lf->len == lf->maplen && lf->buf[lf->len - 1] != '\n') {
527 char *b = malloc(lf->len + 1);
528 b[lf->len] = '\n';
529 memcpy(b, lf->buf, lf->len++);
530 munmap(lf->buf, lf->maplen);
531 lf->maplen = 0;
532 lf->buf = b;
533 }
534 goto done;
535 }
536 }
537
538 /* cannot mmap; load the traditional way */
539
540 lf->maplen = 0;
541 lf->len = 1024;
542 lf->buf = bmake_malloc(lf->len);
543
544 bufpos = 0;
545 while (1) {
546 assert(bufpos <= lf->len);
547 if (bufpos == lf->len) {
548 lf->len *= 2;
549 lf->buf = bmake_realloc(lf->buf, lf->len);
550 }
551 result = read(fd, lf->buf + bufpos, lf->len - bufpos);
552 if (result < 0) {
553 Error("%s: read error: %s", path, strerror(errno));
554 exit(1);
555 }
556 if (result == 0) {
557 break;
558 }
559 bufpos += result;
560 }
561 assert(bufpos <= lf->len);
562 lf->len = bufpos;
563
564 /* truncate malloc region to actual length (maybe not useful) */
565 if (lf->len > 0) {
566 lf->buf = bmake_realloc(lf->buf, lf->len);
567 }
568
569 done:
570 if (path != NULL) {
571 close(fd);
572 }
573 return lf;
574 }
575
576 ////////////////////////////////////////////////////////////
577 // old code
578
579 /*-
580 *----------------------------------------------------------------------
581 * ParseIsEscaped --
582 * Check if the current character is escaped on the current line
583 *
584 * Results:
585 * 0 if the character is not backslash escaped, 1 otherwise
586 *
587 * Side Effects:
588 * None
589 *----------------------------------------------------------------------
590 */
591 static int
592 ParseIsEscaped(const char *line, const char *c)
593 {
594 int active = 0;
595 for (;;) {
596 if (line == c)
597 return active;
598 if (*--c != '\\')
599 return active;
600 active = !active;
601 }
602 }
603
604 /*-
605 *----------------------------------------------------------------------
606 * ParseFindKeyword --
607 * Look in the table of keywords for one matching the given string.
608 *
609 * Input:
610 * str String to find
611 *
612 * Results:
613 * The index of the keyword, or -1 if it isn't there.
614 *
615 * Side Effects:
616 * None
617 *----------------------------------------------------------------------
618 */
619 static int
620 ParseFindKeyword(const char *str)
621 {
622 int start, end, cur;
623 int diff;
624
625 start = 0;
626 end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
627
628 do {
629 cur = start + ((end - start) / 2);
630 diff = strcmp(str, parseKeywords[cur].name);
631
632 if (diff == 0) {
633 return (cur);
634 } else if (diff < 0) {
635 end = cur - 1;
636 } else {
637 start = cur + 1;
638 }
639 } while (start <= end);
640 return (-1);
641 }
642
643 /*-
644 * ParseVErrorInternal --
645 * Error message abort function for parsing. Prints out the context
646 * of the error (line number and file) as well as the message with
647 * two optional arguments.
648 *
649 * Results:
650 * None
651 *
652 * Side Effects:
653 * "fatals" is incremented if the level is PARSE_FATAL.
654 */
655 /* VARARGS */
656 static void
657 ParseVErrorInternal(FILE *f, const char *cfname, size_t clineno, int type,
658 const char *fmt, va_list ap)
659 {
660 static Boolean fatal_warning_error_printed = FALSE;
661
662 (void)fprintf(f, "%s: ", progname);
663
664 if (cfname != NULL) {
665 (void)fprintf(f, "\"");
666 if (*cfname != '/' && strcmp(cfname, "(stdin)") != 0) {
667 char *cp;
668 const char *dir;
669
670 /*
671 * Nothing is more annoying than not knowing
672 * which Makefile is the culprit.
673 */
674 dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &cp);
675 if (dir == NULL || *dir == '\0' ||
676 (*dir == '.' && dir[1] == '\0'))
677 dir = Var_Value(".CURDIR", VAR_GLOBAL, &cp);
678 if (dir == NULL)
679 dir = ".";
680
681 (void)fprintf(f, "%s/%s", dir, cfname);
682 } else
683 (void)fprintf(f, "%s", cfname);
684
685 (void)fprintf(f, "\" line %d: ", (int)clineno);
686 }
687 if (type == PARSE_WARNING)
688 (void)fprintf(f, "warning: ");
689 (void)vfprintf(f, fmt, ap);
690 (void)fprintf(f, "\n");
691 (void)fflush(f);
692 if (type == PARSE_FATAL || parseWarnFatal)
693 fatals += 1;
694 if (parseWarnFatal && !fatal_warning_error_printed) {
695 Error("parsing warnings being treated as errors");
696 fatal_warning_error_printed = TRUE;
697 }
698 }
699
700 /*-
701 * ParseErrorInternal --
702 * Error function
703 *
704 * Results:
705 * None
706 *
707 * Side Effects:
708 * None
709 */
710 /* VARARGS */
711 static void
712 ParseErrorInternal(const char *cfname, size_t clineno, int type,
713 const char *fmt, ...)
714 {
715 va_list ap;
716
717 va_start(ap, fmt);
718 (void)fflush(stdout);
719 ParseVErrorInternal(stderr, cfname, clineno, type, fmt, ap);
720 va_end(ap);
721
722 if (debug_file != stderr && debug_file != stdout) {
723 va_start(ap, fmt);
724 ParseVErrorInternal(debug_file, cfname, clineno, type, fmt, ap);
725 va_end(ap);
726 }
727 }
728
729 /*-
730 * Parse_Error --
731 * External interface to ParseErrorInternal; uses the default filename
732 * Line number.
733 *
734 * Results:
735 * None
736 *
737 * Side Effects:
738 * None
739 */
740 /* VARARGS */
741 void
742 Parse_Error(int type, const char *fmt, ...)
743 {
744 va_list ap;
745 const char *fname;
746 size_t lineno;
747
748 if (curFile == NULL) {
749 fname = NULL;
750 lineno = 0;
751 } else {
752 fname = curFile->fname;
753 lineno = curFile->lineno;
754 }
755
756 va_start(ap, fmt);
757 (void)fflush(stdout);
758 ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
759 va_end(ap);
760
761 if (debug_file != stderr && debug_file != stdout) {
762 va_start(ap, fmt);
763 ParseVErrorInternal(debug_file, fname, lineno, type, fmt, ap);
764 va_end(ap);
765 }
766 }
767
768
769 /*
770 * ParseMessage
771 * Parse a .info .warning or .error directive
772 *
773 * The input is the line minus the ".". We substitute
774 * variables, print the message and exit(1) (for .error) or just print
775 * a warning if the directive is malformed.
776 */
777 static Boolean
778 ParseMessage(char *line)
779 {
780 int mtype;
781
782 switch(*line) {
783 case 'i':
784 mtype = 0;
785 break;
786 case 'w':
787 mtype = PARSE_WARNING;
788 break;
789 case 'e':
790 mtype = PARSE_FATAL;
791 break;
792 default:
793 Parse_Error(PARSE_WARNING, "invalid syntax: \".%s\"", line);
794 return FALSE;
795 }
796
797 while (isalpha((u_char)*line))
798 line++;
799 if (!isspace((u_char)*line))
800 return FALSE; /* not for us */
801 while (isspace((u_char)*line))
802 line++;
803
804 line = Var_Subst(NULL, line, VAR_CMD, 0);
805 Parse_Error(mtype, "%s", line);
806 free(line);
807
808 if (mtype == PARSE_FATAL) {
809 /* Terminate immediately. */
810 exit(1);
811 }
812 return TRUE;
813 }
814
815 /*-
816 *---------------------------------------------------------------------
817 * ParseLinkSrc --
818 * Link the parent node to its new child. Used in a Lst_ForEach by
819 * ParseDoDependency. If the specType isn't 'Not', the parent
820 * isn't linked as a parent of the child.
821 *
822 * Input:
823 * pgnp The parent node
824 * cgpn The child node
825 *
826 * Results:
827 * Always = 0
828 *
829 * Side Effects:
830 * New elements are added to the parents list of cgn and the
831 * children list of cgn. the unmade field of pgn is updated
832 * to reflect the additional child.
833 *---------------------------------------------------------------------
834 */
835 static int
836 ParseLinkSrc(void *pgnp, void *cgnp)
837 {
838 GNode *pgn = (GNode *)pgnp;
839 GNode *cgn = (GNode *)cgnp;
840
841 if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (pgn->cohorts))
842 pgn = (GNode *)Lst_Datum(Lst_Last(pgn->cohorts));
843 (void)Lst_AtEnd(pgn->children, cgn);
844 if (specType == Not)
845 (void)Lst_AtEnd(cgn->parents, pgn);
846 pgn->unmade += 1;
847 if (DEBUG(PARSE)) {
848 fprintf(debug_file, "# ParseLinkSrc: added child %s - %s\n", pgn->name, cgn->name);
849 Targ_PrintNode(pgn, 0);
850 Targ_PrintNode(cgn, 0);
851 }
852 return (0);
853 }
854
855 /*-
856 *---------------------------------------------------------------------
857 * ParseDoOp --
858 * Apply the parsed operator to the given target node. Used in a
859 * Lst_ForEach call by ParseDoDependency once all targets have
860 * been found and their operator parsed. If the previous and new
861 * operators are incompatible, a major error is taken.
862 *
863 * Input:
864 * gnp The node to which the operator is to be applied
865 * opp The operator to apply
866 *
867 * Results:
868 * Always 0
869 *
870 * Side Effects:
871 * The type field of the node is altered to reflect any new bits in
872 * the op.
873 *---------------------------------------------------------------------
874 */
875 static int
876 ParseDoOp(void *gnp, void *opp)
877 {
878 GNode *gn = (GNode *)gnp;
879 int op = *(int *)opp;
880 /*
881 * If the dependency mask of the operator and the node don't match and
882 * the node has actually had an operator applied to it before, and
883 * the operator actually has some dependency information in it, complain.
884 */
885 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
886 !OP_NOP(gn->type) && !OP_NOP(op))
887 {
888 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
889 return (1);
890 }
891
892 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
893 /*
894 * If the node was the object of a :: operator, we need to create a
895 * new instance of it for the children and commands on this dependency
896 * line. The new instance is placed on the 'cohorts' list of the
897 * initial one (note the initial one is not on its own cohorts list)
898 * and the new instance is linked to all parents of the initial
899 * instance.
900 */
901 GNode *cohort;
902
903 /*
904 * Propagate copied bits to the initial node. They'll be propagated
905 * back to the rest of the cohorts later.
906 */
907 gn->type |= op & ~OP_OPMASK;
908
909 cohort = Targ_FindNode(gn->name, TARG_NOHASH);
910 if (doing_depend)
911 ParseMark(cohort);
912 /*
913 * Make the cohort invisible as well to avoid duplicating it into
914 * other variables. True, parents of this target won't tend to do
915 * anything with their local variables, but better safe than
916 * sorry. (I think this is pointless now, since the relevant list
917 * traversals will no longer see this node anyway. -mycroft)
918 */
919 cohort->type = op | OP_INVISIBLE;
920 (void)Lst_AtEnd(gn->cohorts, cohort);
921 cohort->centurion = gn;
922 gn->unmade_cohorts += 1;
923 snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
924 gn->unmade_cohorts);
925 } else {
926 /*
927 * We don't want to nuke any previous flags (whatever they were) so we
928 * just OR the new operator into the old
929 */
930 gn->type |= op;
931 }
932
933 return (0);
934 }
935
936 /*-
937 *---------------------------------------------------------------------
938 * ParseDoSrc --
939 * Given the name of a source, figure out if it is an attribute
940 * and apply it to the targets if it is. Else decide if there is
941 * some attribute which should be applied *to* the source because
942 * of some special target and apply it if so. Otherwise, make the
943 * source be a child of the targets in the list 'targets'
944 *
945 * Input:
946 * tOp operator (if any) from special targets
947 * src name of the source to handle
948 *
949 * Results:
950 * None
951 *
952 * Side Effects:
953 * Operator bits may be added to the list of targets or to the source.
954 * The targets may have a new source added to their lists of children.
955 *---------------------------------------------------------------------
956 */
957 static void
958 ParseDoSrc(int tOp, const char *src)
959 {
960 GNode *gn = NULL;
961 static int wait_number = 0;
962 char wait_src[16];
963
964 if (*src == '.' && isupper ((unsigned char)src[1])) {
965 int keywd = ParseFindKeyword(src);
966 if (keywd != -1) {
967 int op = parseKeywords[keywd].op;
968 if (op != 0) {
969 Lst_ForEach(targets, ParseDoOp, &op);
970 return;
971 }
972 if (parseKeywords[keywd].spec == Wait) {
973 /*
974 * We add a .WAIT node in the dependency list.
975 * After any dynamic dependencies (and filename globbing)
976 * have happened, it is given a dependency on the each
977 * previous child back to and previous .WAIT node.
978 * The next child won't be scheduled until the .WAIT node
979 * is built.
980 * We give each .WAIT node a unique name (mainly for diag).
981 */
982 snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
983 gn = Targ_FindNode(wait_src, TARG_NOHASH);
984 if (doing_depend)
985 ParseMark(gn);
986 gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
987 Lst_ForEach(targets, ParseLinkSrc, gn);
988 return;
989 }
990 }
991 }
992
993 switch (specType) {
994 case Main:
995 /*
996 * If we have noted the existence of a .MAIN, it means we need
997 * to add the sources of said target to the list of things
998 * to create. The string 'src' is likely to be free, so we
999 * must make a new copy of it. Note that this will only be
1000 * invoked if the user didn't specify a target on the command
1001 * line. This is to allow #ifmake's to succeed, or something...
1002 */
1003 (void)Lst_AtEnd(create, bmake_strdup(src));
1004 /*
1005 * Add the name to the .TARGETS variable as well, so the user can
1006 * employ that, if desired.
1007 */
1008 Var_Append(".TARGETS", src, VAR_GLOBAL);
1009 return;
1010
1011 case Order:
1012 /*
1013 * Create proper predecessor/successor links between the previous
1014 * source and the current one.
1015 */
1016 gn = Targ_FindNode(src, TARG_CREATE);
1017 if (doing_depend)
1018 ParseMark(gn);
1019 if (predecessor != NULL) {
1020 (void)Lst_AtEnd(predecessor->order_succ, gn);
1021 (void)Lst_AtEnd(gn->order_pred, predecessor);
1022 if (DEBUG(PARSE)) {
1023 fprintf(debug_file, "# ParseDoSrc: added Order dependency %s - %s\n",
1024 predecessor->name, gn->name);
1025 Targ_PrintNode(predecessor, 0);
1026 Targ_PrintNode(gn, 0);
1027 }
1028 }
1029 /*
1030 * The current source now becomes the predecessor for the next one.
1031 */
1032 predecessor = gn;
1033 break;
1034
1035 default:
1036 /*
1037 * If the source is not an attribute, we need to find/create
1038 * a node for it. After that we can apply any operator to it
1039 * from a special target or link it to its parents, as
1040 * appropriate.
1041 *
1042 * In the case of a source that was the object of a :: operator,
1043 * the attribute is applied to all of its instances (as kept in
1044 * the 'cohorts' list of the node) or all the cohorts are linked
1045 * to all the targets.
1046 */
1047
1048 /* Find/create the 'src' node and attach to all targets */
1049 gn = Targ_FindNode(src, TARG_CREATE);
1050 if (doing_depend)
1051 ParseMark(gn);
1052 if (tOp) {
1053 gn->type |= tOp;
1054 } else {
1055 Lst_ForEach(targets, ParseLinkSrc, gn);
1056 }
1057 break;
1058 }
1059 }
1060
1061 /*-
1062 *-----------------------------------------------------------------------
1063 * ParseFindMain --
1064 * Find a real target in the list and set it to be the main one.
1065 * Called by ParseDoDependency when a main target hasn't been found
1066 * yet.
1067 *
1068 * Input:
1069 * gnp Node to examine
1070 *
1071 * Results:
1072 * 0 if main not found yet, 1 if it is.
1073 *
1074 * Side Effects:
1075 * mainNode is changed and Targ_SetMain is called.
1076 *
1077 *-----------------------------------------------------------------------
1078 */
1079 static int
1080 ParseFindMain(void *gnp, void *dummy)
1081 {
1082 GNode *gn = (GNode *)gnp;
1083 if ((gn->type & OP_NOTARGET) == 0) {
1084 mainNode = gn;
1085 Targ_SetMain(gn);
1086 return (dummy ? 1 : 1);
1087 } else {
1088 return (dummy ? 0 : 0);
1089 }
1090 }
1091
1092 /*-
1093 *-----------------------------------------------------------------------
1094 * ParseAddDir --
1095 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
1096 *
1097 * Results:
1098 * === 0
1099 *
1100 * Side Effects:
1101 * See Dir_AddDir.
1102 *
1103 *-----------------------------------------------------------------------
1104 */
1105 static int
1106 ParseAddDir(void *path, void *name)
1107 {
1108 (void)Dir_AddDir((Lst) path, (char *)name);
1109 return(0);
1110 }
1111
1112 /*-
1113 *-----------------------------------------------------------------------
1114 * ParseClearPath --
1115 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
1116 *
1117 * Results:
1118 * === 0
1119 *
1120 * Side Effects:
1121 * See Dir_ClearPath
1122 *
1123 *-----------------------------------------------------------------------
1124 */
1125 static int
1126 ParseClearPath(void *path, void *dummy)
1127 {
1128 Dir_ClearPath((Lst) path);
1129 return(dummy ? 0 : 0);
1130 }
1131
1132 /*-
1133 *---------------------------------------------------------------------
1134 * ParseDoDependency --
1135 * Parse the dependency line in line.
1136 *
1137 * Input:
1138 * line the line to parse
1139 *
1140 * Results:
1141 * None
1142 *
1143 * Side Effects:
1144 * The nodes of the sources are linked as children to the nodes of the
1145 * targets. Some nodes may be created.
1146 *
1147 * We parse a dependency line by first extracting words from the line and
1148 * finding nodes in the list of all targets with that name. This is done
1149 * until a character is encountered which is an operator character. Currently
1150 * these are only ! and :. At this point the operator is parsed and the
1151 * pointer into the line advanced until the first source is encountered.
1152 * The parsed operator is applied to each node in the 'targets' list,
1153 * which is where the nodes found for the targets are kept, by means of
1154 * the ParseDoOp function.
1155 * The sources are read in much the same way as the targets were except
1156 * that now they are expanded using the wildcarding scheme of the C-Shell
1157 * and all instances of the resulting words in the list of all targets
1158 * are found. Each of the resulting nodes is then linked to each of the
1159 * targets as one of its children.
1160 * Certain targets are handled specially. These are the ones detailed
1161 * by the specType variable.
1162 * The storing of transformation rules is also taken care of here.
1163 * A target is recognized as a transformation rule by calling
1164 * Suff_IsTransform. If it is a transformation rule, its node is gotten
1165 * from the suffix module via Suff_AddTransform rather than the standard
1166 * Targ_FindNode in the target module.
1167 *---------------------------------------------------------------------
1168 */
1169 static void
1170 ParseDoDependency(char *line)
1171 {
1172 char *cp; /* our current position */
1173 GNode *gn = NULL; /* a general purpose temporary node */
1174 int op; /* the operator on the line */
1175 char savec; /* a place to save a character */
1176 Lst paths; /* List of search paths to alter when parsing
1177 * a list of .PATH targets */
1178 int tOp; /* operator from special target */
1179 Lst sources; /* list of archive source names after
1180 * expansion */
1181 Lst curTargs; /* list of target names to be found and added
1182 * to the targets list */
1183 char *lstart = line;
1184
1185 if (DEBUG(PARSE))
1186 fprintf(debug_file, "ParseDoDependency(%s)\n", line);
1187 tOp = 0;
1188
1189 specType = Not;
1190 paths = NULL;
1191
1192 curTargs = Lst_Init(FALSE);
1193
1194 do {
1195 for (cp = line; *cp && (ParseIsEscaped(lstart, cp) ||
1196 !(isspace((unsigned char)*cp) ||
1197 *cp == '!' || *cp == ':' || *cp == LPAREN));
1198 cp++) {
1199 if (*cp == '$') {
1200 /*
1201 * Must be a dynamic source (would have been expanded
1202 * otherwise), so call the Var module to parse the puppy
1203 * so we can safely advance beyond it...There should be
1204 * no errors in this, as they would have been discovered
1205 * in the initial Var_Subst and we wouldn't be here.
1206 */
1207 int length;
1208 void *freeIt;
1209 char *result;
1210
1211 result = Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
1212 if (freeIt)
1213 free(freeIt);
1214 cp += length-1;
1215 }
1216 }
1217
1218 if (!ParseIsEscaped(lstart, cp) && *cp == LPAREN) {
1219 /*
1220 * Archives must be handled specially to make sure the OP_ARCHV
1221 * flag is set in their 'type' field, for one thing, and because
1222 * things like "archive(file1.o file2.o file3.o)" are permissible.
1223 * Arch_ParseArchive will set 'line' to be the first non-blank
1224 * after the archive-spec. It creates/finds nodes for the members
1225 * and places them on the given list, returning SUCCESS if all
1226 * went well and FAILURE if there was an error in the
1227 * specification. On error, line should remain untouched.
1228 */
1229 if (Arch_ParseArchive(&line, targets, VAR_CMD) != SUCCESS) {
1230 Parse_Error(PARSE_FATAL,
1231 "Error in archive specification: \"%s\"", line);
1232 goto out;
1233 } else {
1234 continue;
1235 }
1236 }
1237 savec = *cp;
1238
1239 if (!*cp) {
1240 /*
1241 * Ending a dependency line without an operator is a Bozo
1242 * no-no. As a heuristic, this is also often triggered by
1243 * undetected conflicts from cvs/rcs merges.
1244 */
1245 if ((strncmp(line, "<<<<<<", 6) == 0) ||
1246 (strncmp(line, "======", 6) == 0) ||
1247 (strncmp(line, ">>>>>>", 6) == 0))
1248 Parse_Error(PARSE_FATAL,
1249 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
1250 else
1251 Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive"
1252 : "Need an operator");
1253 goto out;
1254 }
1255 *cp = '\0';
1256
1257 /*
1258 * Have a word in line. See if it's a special target and set
1259 * specType to match it.
1260 */
1261 if (*line == '.' && isupper ((unsigned char)line[1])) {
1262 /*
1263 * See if the target is a special target that must have it
1264 * or its sources handled specially.
1265 */
1266 int keywd = ParseFindKeyword(line);
1267 if (keywd != -1) {
1268 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
1269 Parse_Error(PARSE_FATAL, "Mismatched special targets");
1270 goto out;
1271 }
1272
1273 specType = parseKeywords[keywd].spec;
1274 tOp = parseKeywords[keywd].op;
1275
1276 /*
1277 * Certain special targets have special semantics:
1278 * .PATH Have to set the dirSearchPath
1279 * variable too
1280 * .MAIN Its sources are only used if
1281 * nothing has been specified to
1282 * create.
1283 * .DEFAULT Need to create a node to hang
1284 * commands on, but we don't want
1285 * it in the graph, nor do we want
1286 * it to be the Main Target, so we
1287 * create it, set OP_NOTMAIN and
1288 * add it to the list, setting
1289 * DEFAULT to the new node for
1290 * later use. We claim the node is
1291 * A transformation rule to make
1292 * life easier later, when we'll
1293 * use Make_HandleUse to actually
1294 * apply the .DEFAULT commands.
1295 * .PHONY The list of targets
1296 * .NOPATH Don't search for file in the path
1297 * .STALE
1298 * .BEGIN
1299 * .END
1300 * .ERROR
1301 * .INTERRUPT Are not to be considered the
1302 * main target.
1303 * .NOTPARALLEL Make only one target at a time.
1304 * .SINGLESHELL Create a shell for each command.
1305 * .ORDER Must set initial predecessor to NULL
1306 */
1307 switch (specType) {
1308 case ExPath:
1309 if (paths == NULL) {
1310 paths = Lst_Init(FALSE);
1311 }
1312 (void)Lst_AtEnd(paths, dirSearchPath);
1313 break;
1314 case Main:
1315 if (!Lst_IsEmpty(create)) {
1316 specType = Not;
1317 }
1318 break;
1319 case Begin:
1320 case End:
1321 case Stale:
1322 case dotError:
1323 case Interrupt:
1324 gn = Targ_FindNode(line, TARG_CREATE);
1325 if (doing_depend)
1326 ParseMark(gn);
1327 gn->type |= OP_NOTMAIN|OP_SPECIAL;
1328 (void)Lst_AtEnd(targets, gn);
1329 break;
1330 case Default:
1331 gn = Targ_NewGN(".DEFAULT");
1332 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
1333 (void)Lst_AtEnd(targets, gn);
1334 DEFAULT = gn;
1335 break;
1336 case NotParallel:
1337 maxJobs = 1;
1338 break;
1339 case SingleShell:
1340 compatMake = TRUE;
1341 break;
1342 case Order:
1343 predecessor = NULL;
1344 break;
1345 default:
1346 break;
1347 }
1348 } else if (strncmp(line, ".PATH", 5) == 0) {
1349 /*
1350 * .PATH<suffix> has to be handled specially.
1351 * Call on the suffix module to give us a path to
1352 * modify.
1353 */
1354 Lst path;
1355
1356 specType = ExPath;
1357 path = Suff_GetPath(&line[5]);
1358 if (path == NULL) {
1359 Parse_Error(PARSE_FATAL,
1360 "Suffix '%s' not defined (yet)",
1361 &line[5]);
1362 goto out;
1363 } else {
1364 if (paths == NULL) {
1365 paths = Lst_Init(FALSE);
1366 }
1367 (void)Lst_AtEnd(paths, path);
1368 }
1369 }
1370 }
1371
1372 /*
1373 * Have word in line. Get or create its node and stick it at
1374 * the end of the targets list
1375 */
1376 if ((specType == Not) && (*line != '\0')) {
1377 if (Dir_HasWildcards(line)) {
1378 /*
1379 * Targets are to be sought only in the current directory,
1380 * so create an empty path for the thing. Note we need to
1381 * use Dir_Destroy in the destruction of the path as the
1382 * Dir module could have added a directory to the path...
1383 */
1384 Lst emptyPath = Lst_Init(FALSE);
1385
1386 Dir_Expand(line, emptyPath, curTargs);
1387
1388 Lst_Destroy(emptyPath, Dir_Destroy);
1389 } else {
1390 /*
1391 * No wildcards, but we want to avoid code duplication,
1392 * so create a list with the word on it.
1393 */
1394 (void)Lst_AtEnd(curTargs, line);
1395 }
1396
1397 while(!Lst_IsEmpty(curTargs)) {
1398 char *targName = (char *)Lst_DeQueue(curTargs);
1399
1400 if (!Suff_IsTransform (targName)) {
1401 gn = Targ_FindNode(targName, TARG_CREATE);
1402 } else {
1403 gn = Suff_AddTransform(targName);
1404 }
1405 if (doing_depend)
1406 ParseMark(gn);
1407
1408 (void)Lst_AtEnd(targets, gn);
1409 }
1410 } else if (specType == ExPath && *line != '.' && *line != '\0') {
1411 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
1412 }
1413
1414 *cp = savec;
1415 /*
1416 * If it is a special type and not .PATH, it's the only target we
1417 * allow on this line...
1418 */
1419 if (specType != Not && specType != ExPath) {
1420 Boolean warning = FALSE;
1421
1422 while (*cp && (ParseIsEscaped(lstart, cp) ||
1423 ((*cp != '!') && (*cp != ':')))) {
1424 if (ParseIsEscaped(lstart, cp) ||
1425 (*cp != ' ' && *cp != '\t')) {
1426 warning = TRUE;
1427 }
1428 cp++;
1429 }
1430 if (warning) {
1431 Parse_Error(PARSE_WARNING, "Extra target ignored");
1432 }
1433 } else {
1434 while (*cp && isspace ((unsigned char)*cp)) {
1435 cp++;
1436 }
1437 }
1438 line = cp;
1439 } while (*line && (ParseIsEscaped(lstart, line) ||
1440 ((*line != '!') && (*line != ':'))));
1441
1442 /*
1443 * Don't need the list of target names anymore...
1444 */
1445 Lst_Destroy(curTargs, NULL);
1446 curTargs = NULL;
1447
1448 if (!Lst_IsEmpty(targets)) {
1449 switch(specType) {
1450 default:
1451 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
1452 break;
1453 case Default:
1454 case Stale:
1455 case Begin:
1456 case End:
1457 case dotError:
1458 case Interrupt:
1459 /*
1460 * These four create nodes on which to hang commands, so
1461 * targets shouldn't be empty...
1462 */
1463 case Not:
1464 /*
1465 * Nothing special here -- targets can be empty if it wants.
1466 */
1467 break;
1468 }
1469 }
1470
1471 /*
1472 * Have now parsed all the target names. Must parse the operator next. The
1473 * result is left in op .
1474 */
1475 if (*cp == '!') {
1476 op = OP_FORCE;
1477 } else if (*cp == ':') {
1478 if (cp[1] == ':') {
1479 op = OP_DOUBLEDEP;
1480 cp++;
1481 } else {
1482 op = OP_DEPENDS;
1483 }
1484 } else {
1485 Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive"
1486 : "Missing dependency operator");
1487 goto out;
1488 }
1489
1490 cp++; /* Advance beyond operator */
1491
1492 Lst_ForEach(targets, ParseDoOp, &op);
1493
1494 /*
1495 * Get to the first source
1496 */
1497 while (*cp && isspace ((unsigned char)*cp)) {
1498 cp++;
1499 }
1500 line = cp;
1501
1502 /*
1503 * Several special targets take different actions if present with no
1504 * sources:
1505 * a .SUFFIXES line with no sources clears out all old suffixes
1506 * a .PRECIOUS line makes all targets precious
1507 * a .IGNORE line ignores errors for all targets
1508 * a .SILENT line creates silence when making all targets
1509 * a .PATH removes all directories from the search path(s).
1510 */
1511 if (!*line) {
1512 switch (specType) {
1513 case Suffixes:
1514 Suff_ClearSuffixes();
1515 break;
1516 case Precious:
1517 allPrecious = TRUE;
1518 break;
1519 case Ignore:
1520 ignoreErrors = TRUE;
1521 break;
1522 case Silent:
1523 beSilent = TRUE;
1524 break;
1525 case ExPath:
1526 Lst_ForEach(paths, ParseClearPath, NULL);
1527 Dir_SetPATH();
1528 break;
1529 #ifdef POSIX
1530 case Posix:
1531 Var_Set("%POSIX", "1003.2", VAR_GLOBAL, 0);
1532 break;
1533 #endif
1534 default:
1535 break;
1536 }
1537 } else if (specType == MFlags) {
1538 /*
1539 * Call on functions in main.c to deal with these arguments and
1540 * set the initial character to a null-character so the loop to
1541 * get sources won't get anything
1542 */
1543 Main_ParseArgLine(line);
1544 *line = '\0';
1545 } else if (specType == ExShell) {
1546 if (Job_ParseShell(line) != SUCCESS) {
1547 Parse_Error(PARSE_FATAL, "improper shell specification");
1548 goto out;
1549 }
1550 *line = '\0';
1551 } else if ((specType == NotParallel) || (specType == SingleShell)) {
1552 *line = '\0';
1553 }
1554
1555 /*
1556 * NOW GO FOR THE SOURCES
1557 */
1558 if ((specType == Suffixes) || (specType == ExPath) ||
1559 (specType == Includes) || (specType == Libs) ||
1560 (specType == Null) || (specType == ExObjdir))
1561 {
1562 while (*line) {
1563 /*
1564 * If the target was one that doesn't take files as its sources
1565 * but takes something like suffixes, we take each
1566 * space-separated word on the line as a something and deal
1567 * with it accordingly.
1568 *
1569 * If the target was .SUFFIXES, we take each source as a
1570 * suffix and add it to the list of suffixes maintained by the
1571 * Suff module.
1572 *
1573 * If the target was a .PATH, we add the source as a directory
1574 * to search on the search path.
1575 *
1576 * If it was .INCLUDES, the source is taken to be the suffix of
1577 * files which will be #included and whose search path should
1578 * be present in the .INCLUDES variable.
1579 *
1580 * If it was .LIBS, the source is taken to be the suffix of
1581 * files which are considered libraries and whose search path
1582 * should be present in the .LIBS variable.
1583 *
1584 * If it was .NULL, the source is the suffix to use when a file
1585 * has no valid suffix.
1586 *
1587 * If it was .OBJDIR, the source is a new definition for .OBJDIR,
1588 * and will cause make to do a new chdir to that path.
1589 */
1590 while (*cp && !isspace ((unsigned char)*cp)) {
1591 cp++;
1592 }
1593 savec = *cp;
1594 *cp = '\0';
1595 switch (specType) {
1596 case Suffixes:
1597 Suff_AddSuffix(line, &mainNode);
1598 break;
1599 case ExPath:
1600 Lst_ForEach(paths, ParseAddDir, line);
1601 break;
1602 case Includes:
1603 Suff_AddInclude(line);
1604 break;
1605 case Libs:
1606 Suff_AddLib(line);
1607 break;
1608 case Null:
1609 Suff_SetNull(line);
1610 break;
1611 case ExObjdir:
1612 Main_SetObjdir(line);
1613 break;
1614 default:
1615 break;
1616 }
1617 *cp = savec;
1618 if (savec != '\0') {
1619 cp++;
1620 }
1621 while (*cp && isspace ((unsigned char)*cp)) {
1622 cp++;
1623 }
1624 line = cp;
1625 }
1626 if (paths) {
1627 Lst_Destroy(paths, NULL);
1628 }
1629 if (specType == ExPath)
1630 Dir_SetPATH();
1631 } else {
1632 while (*line) {
1633 /*
1634 * The targets take real sources, so we must beware of archive
1635 * specifications (i.e. things with left parentheses in them)
1636 * and handle them accordingly.
1637 */
1638 for (; *cp && !isspace ((unsigned char)*cp); cp++) {
1639 if ((*cp == LPAREN) && (cp > line) && (cp[-1] != '$')) {
1640 /*
1641 * Only stop for a left parenthesis if it isn't at the
1642 * start of a word (that'll be for variable changes
1643 * later) and isn't preceded by a dollar sign (a dynamic
1644 * source).
1645 */
1646 break;
1647 }
1648 }
1649
1650 if (*cp == LPAREN) {
1651 sources = Lst_Init(FALSE);
1652 if (Arch_ParseArchive(&line, sources, VAR_CMD) != SUCCESS) {
1653 Parse_Error(PARSE_FATAL,
1654 "Error in source archive spec \"%s\"", line);
1655 goto out;
1656 }
1657
1658 while (!Lst_IsEmpty (sources)) {
1659 gn = (GNode *)Lst_DeQueue(sources);
1660 ParseDoSrc(tOp, gn->name);
1661 }
1662 Lst_Destroy(sources, NULL);
1663 cp = line;
1664 } else {
1665 if (*cp) {
1666 *cp = '\0';
1667 cp += 1;
1668 }
1669
1670 ParseDoSrc(tOp, line);
1671 }
1672 while (*cp && isspace ((unsigned char)*cp)) {
1673 cp++;
1674 }
1675 line = cp;
1676 }
1677 }
1678
1679 if (mainNode == NULL) {
1680 /*
1681 * If we have yet to decide on a main target to make, in the
1682 * absence of any user input, we want the first target on
1683 * the first dependency line that is actually a real target
1684 * (i.e. isn't a .USE or .EXEC rule) to be made.
1685 */
1686 Lst_ForEach(targets, ParseFindMain, NULL);
1687 }
1688
1689 out:
1690 if (curTargs)
1691 Lst_Destroy(curTargs, NULL);
1692 }
1693
1694 /*-
1695 *---------------------------------------------------------------------
1696 * Parse_IsVar --
1697 * Return TRUE if the passed line is a variable assignment. A variable
1698 * assignment consists of a single word followed by optional whitespace
1699 * followed by either a += or an = operator.
1700 * This function is used both by the Parse_File function and main when
1701 * parsing the command-line arguments.
1702 *
1703 * Input:
1704 * line the line to check
1705 *
1706 * Results:
1707 * TRUE if it is. FALSE if it ain't
1708 *
1709 * Side Effects:
1710 * none
1711 *---------------------------------------------------------------------
1712 */
1713 Boolean
1714 Parse_IsVar(char *line)
1715 {
1716 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1717 char ch;
1718 int level = 0;
1719 #define ISEQOPERATOR(c) \
1720 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1721
1722 /*
1723 * Skip to variable name
1724 */
1725 for (;(*line == ' ') || (*line == '\t'); line++)
1726 continue;
1727
1728 /* Scan for one of the assignment operators outside a variable expansion */
1729 while ((ch = *line++) != 0) {
1730 if (ch == '(' || ch == '{') {
1731 level++;
1732 continue;
1733 }
1734 if (ch == ')' || ch == '}') {
1735 level--;
1736 continue;
1737 }
1738 if (level != 0)
1739 continue;
1740 while (ch == ' ' || ch == '\t') {
1741 ch = *line++;
1742 wasSpace = TRUE;
1743 }
1744 if (ch == '=')
1745 return TRUE;
1746 if (*line == '=' && ISEQOPERATOR(ch))
1747 return TRUE;
1748 if (wasSpace)
1749 return FALSE;
1750 }
1751
1752 return FALSE;
1753 }
1754
1755 /*-
1756 *---------------------------------------------------------------------
1757 * Parse_DoVar --
1758 * Take the variable assignment in the passed line and do it in the
1759 * global context.
1760 *
1761 * Note: There is a lexical ambiguity with assignment modifier characters
1762 * in variable names. This routine interprets the character before the =
1763 * as a modifier. Therefore, an assignment like
1764 * C++=/usr/bin/CC
1765 * is interpreted as "C+ +=" instead of "C++ =".
1766 *
1767 * Input:
1768 * line a line guaranteed to be a variable assignment.
1769 * This reduces error checks
1770 * ctxt Context in which to do the assignment
1771 *
1772 * Results:
1773 * none
1774 *
1775 * Side Effects:
1776 * the variable structure of the given variable name is altered in the
1777 * global context.
1778 *---------------------------------------------------------------------
1779 */
1780 void
1781 Parse_DoVar(char *line, GNode *ctxt)
1782 {
1783 char *cp; /* pointer into line */
1784 enum {
1785 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1786 } type; /* Type of assignment */
1787 char *opc; /* ptr to operator character to
1788 * null-terminate the variable name */
1789 Boolean freeCp = FALSE; /* TRUE if cp needs to be freed,
1790 * i.e. if any variable expansion was
1791 * performed */
1792 int depth;
1793
1794 /*
1795 * Skip to variable name
1796 */
1797 while ((*line == ' ') || (*line == '\t')) {
1798 line++;
1799 }
1800
1801 /*
1802 * Skip to operator character, nulling out whitespace as we go
1803 * XXX Rather than counting () and {} we should look for $ and
1804 * then expand the variable.
1805 */
1806 for (depth = 0, cp = line + 1; depth != 0 || *cp != '='; cp++) {
1807 if (*cp == '(' || *cp == '{') {
1808 depth++;
1809 continue;
1810 }
1811 if (*cp == ')' || *cp == '}') {
1812 depth--;
1813 continue;
1814 }
1815 if (depth == 0 && isspace ((unsigned char)*cp)) {
1816 *cp = '\0';
1817 }
1818 }
1819 opc = cp-1; /* operator is the previous character */
1820 *cp++ = '\0'; /* nuke the = */
1821
1822 /*
1823 * Check operator type
1824 */
1825 switch (*opc) {
1826 case '+':
1827 type = VAR_APPEND;
1828 *opc = '\0';
1829 break;
1830
1831 case '?':
1832 /*
1833 * If the variable already has a value, we don't do anything.
1834 */
1835 *opc = '\0';
1836 if (Var_Exists(line, ctxt)) {
1837 return;
1838 } else {
1839 type = VAR_NORMAL;
1840 }
1841 break;
1842
1843 case ':':
1844 type = VAR_SUBST;
1845 *opc = '\0';
1846 break;
1847
1848 case '!':
1849 type = VAR_SHELL;
1850 *opc = '\0';
1851 break;
1852
1853 default:
1854 #ifdef SUNSHCMD
1855 while (opc > line && *opc != ':')
1856 opc--;
1857
1858 if (strncmp(opc, ":sh", 3) == 0) {
1859 type = VAR_SHELL;
1860 *opc = '\0';
1861 break;
1862 }
1863 #endif
1864 type = VAR_NORMAL;
1865 break;
1866 }
1867
1868 while (isspace ((unsigned char)*cp)) {
1869 cp++;
1870 }
1871
1872 if (type == VAR_APPEND) {
1873 Var_Append(line, cp, ctxt);
1874 } else if (type == VAR_SUBST) {
1875 /*
1876 * Allow variables in the old value to be undefined, but leave their
1877 * invocation alone -- this is done by forcing oldVars to be false.
1878 * XXX: This can cause recursive variables, but that's not hard to do,
1879 * and this allows someone to do something like
1880 *
1881 * CFLAGS = $(.INCLUDES)
1882 * CFLAGS := -I.. $(CFLAGS)
1883 *
1884 * And not get an error.
1885 */
1886 Boolean oldOldVars = oldVars;
1887
1888 oldVars = FALSE;
1889
1890 /*
1891 * make sure that we set the variable the first time to nothing
1892 * so that it gets substituted!
1893 */
1894 if (!Var_Exists(line, ctxt))
1895 Var_Set(line, "", ctxt, 0);
1896
1897 cp = Var_Subst(NULL, cp, ctxt, FALSE);
1898 oldVars = oldOldVars;
1899 freeCp = TRUE;
1900
1901 Var_Set(line, cp, ctxt, 0);
1902 } else if (type == VAR_SHELL) {
1903 char *res;
1904 const char *error;
1905
1906 if (strchr(cp, '$') != NULL) {
1907 /*
1908 * There's a dollar sign in the command, so perform variable
1909 * expansion on the whole thing. The resulting string will need
1910 * freeing when we're done, so set freeCmd to TRUE.
1911 */
1912 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1913 freeCp = TRUE;
1914 }
1915
1916 res = Cmd_Exec(cp, &error);
1917 Var_Set(line, res, ctxt, 0);
1918 free(res);
1919
1920 if (error)
1921 Parse_Error(PARSE_WARNING, error, cp);
1922 } else {
1923 /*
1924 * Normal assignment -- just do it.
1925 */
1926 Var_Set(line, cp, ctxt, 0);
1927 }
1928 if (strcmp(line, MAKEOVERRIDES) == 0)
1929 Main_ExportMAKEFLAGS(FALSE); /* re-export MAKEFLAGS */
1930 else if (strcmp(line, ".CURDIR") == 0) {
1931 /*
1932 * Somone is being (too?) clever...
1933 * Let's pretend they know what they are doing and
1934 * re-initialize the 'cur' Path.
1935 */
1936 Dir_InitCur(cp);
1937 Dir_SetPATH();
1938 } else if (strcmp(line, MAKE_JOB_PREFIX) == 0) {
1939 Job_SetPrefix();
1940 } else if (strcmp(line, MAKE_EXPORTED) == 0) {
1941 Var_Export(cp, 0);
1942 }
1943 if (freeCp)
1944 free(cp);
1945 }
1946
1947
1948 /*-
1949 * ParseAddCmd --
1950 * Lst_ForEach function to add a command line to all targets
1951 *
1952 * Input:
1953 * gnp the node to which the command is to be added
1954 * cmd the command to add
1955 *
1956 * Results:
1957 * Always 0
1958 *
1959 * Side Effects:
1960 * A new element is added to the commands list of the node.
1961 */
1962 static int
1963 ParseAddCmd(void *gnp, void *cmd)
1964 {
1965 GNode *gn = (GNode *)gnp;
1966
1967 /* Add to last (ie current) cohort for :: targets */
1968 if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts))
1969 gn = (GNode *)Lst_Datum(Lst_Last(gn->cohorts));
1970
1971 /* if target already supplied, ignore commands */
1972 if (!(gn->type & OP_HAS_COMMANDS)) {
1973 (void)Lst_AtEnd(gn->commands, cmd);
1974 ParseMark(gn);
1975 } else {
1976 #ifdef notyet
1977 /* XXX: We cannot do this until we fix the tree */
1978 (void)Lst_AtEnd(gn->commands, cmd);
1979 Parse_Error(PARSE_WARNING,
1980 "overriding commands for target \"%s\"; "
1981 "previous commands defined at %s: %d ignored",
1982 gn->name, gn->fname, gn->lineno);
1983 #else
1984 Parse_Error(PARSE_WARNING,
1985 "duplicate script for target \"%s\" ignored",
1986 gn->name);
1987 ParseErrorInternal(gn->fname, gn->lineno, PARSE_WARNING,
1988 "using previous script for \"%s\" defined here",
1989 gn->name);
1990 #endif
1991 }
1992 return(0);
1993 }
1994
1995 /*-
1996 *-----------------------------------------------------------------------
1997 * ParseHasCommands --
1998 * Callback procedure for Parse_File when destroying the list of
1999 * targets on the last dependency line. Marks a target as already
2000 * having commands if it does, to keep from having shell commands
2001 * on multiple dependency lines.
2002 *
2003 * Input:
2004 * gnp Node to examine
2005 *
2006 * Results:
2007 * None
2008 *
2009 * Side Effects:
2010 * OP_HAS_COMMANDS may be set for the target.
2011 *
2012 *-----------------------------------------------------------------------
2013 */
2014 static void
2015 ParseHasCommands(void *gnp)
2016 {
2017 GNode *gn = (GNode *)gnp;
2018 if (!Lst_IsEmpty(gn->commands)) {
2019 gn->type |= OP_HAS_COMMANDS;
2020 }
2021 }
2022
2023 /*-
2024 *-----------------------------------------------------------------------
2025 * Parse_AddIncludeDir --
2026 * Add a directory to the path searched for included makefiles
2027 * bracketed by double-quotes. Used by functions in main.c
2028 *
2029 * Input:
2030 * dir The name of the directory to add
2031 *
2032 * Results:
2033 * None.
2034 *
2035 * Side Effects:
2036 * The directory is appended to the list.
2037 *
2038 *-----------------------------------------------------------------------
2039 */
2040 void
2041 Parse_AddIncludeDir(char *dir)
2042 {
2043 (void)Dir_AddDir(parseIncPath, dir);
2044 }
2045
2046 /*-
2047 *---------------------------------------------------------------------
2048 * ParseDoInclude --
2049 * Push to another file.
2050 *
2051 * The input is the line minus the `.'. A file spec is a string
2052 * enclosed in <> or "". The former is looked for only in sysIncPath.
2053 * The latter in . and the directories specified by -I command line
2054 * options
2055 *
2056 * Results:
2057 * None
2058 *
2059 * Side Effects:
2060 * A structure is added to the includes Lst and readProc, lineno,
2061 * fname and curFILE are altered for the new file
2062 *---------------------------------------------------------------------
2063 */
2064
2065 static void
2066 Parse_include_file(char *file, Boolean isSystem, int silent)
2067 {
2068 struct loadedfile *lf;
2069 char *fullname; /* full pathname of file */
2070 char *newName;
2071 char *prefEnd, *incdir;
2072 int fd;
2073 int i;
2074
2075 /*
2076 * Now we know the file's name and its search path, we attempt to
2077 * find the durn thing. A return of NULL indicates the file don't
2078 * exist.
2079 */
2080 fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
2081
2082 if (fullname == NULL && !isSystem) {
2083 /*
2084 * Include files contained in double-quotes are first searched for
2085 * relative to the including file's location. We don't want to
2086 * cd there, of course, so we just tack on the old file's
2087 * leading path components and call Dir_FindFile to see if
2088 * we can locate the beast.
2089 */
2090
2091 incdir = bmake_strdup(curFile->fname);
2092 prefEnd = strrchr(incdir, '/');
2093 if (prefEnd != NULL) {
2094 *prefEnd = '\0';
2095 /* Now do lexical processing of leading "../" on the filename */
2096 for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
2097 prefEnd = strrchr(incdir + 1, '/');
2098 if (prefEnd == NULL || strcmp(prefEnd, "/..") == 0)
2099 break;
2100 *prefEnd = '\0';
2101 }
2102 newName = str_concat(incdir, file + i, STR_ADDSLASH);
2103 fullname = Dir_FindFile(newName, parseIncPath);
2104 if (fullname == NULL)
2105 fullname = Dir_FindFile(newName, dirSearchPath);
2106 free(newName);
2107 }
2108 free(incdir);
2109
2110 if (fullname == NULL) {
2111 /*
2112 * Makefile wasn't found in same directory as included makefile.
2113 * Search for it first on the -I search path,
2114 * then on the .PATH search path, if not found in a -I directory.
2115 * If we have a suffix specific path we should use that.
2116 */
2117 char *suff;
2118 Lst suffPath = NULL;
2119
2120 if ((suff = strrchr(file, '.'))) {
2121 suffPath = Suff_GetPath(suff);
2122 if (suffPath != NULL) {
2123 fullname = Dir_FindFile(file, suffPath);
2124 }
2125 }
2126 if (fullname == NULL) {
2127 fullname = Dir_FindFile(file, parseIncPath);
2128 if (fullname == NULL) {
2129 fullname = Dir_FindFile(file, dirSearchPath);
2130 }
2131 }
2132 }
2133 }
2134
2135 /* Looking for a system file or file still not found */
2136 if (fullname == NULL) {
2137 /*
2138 * Look for it on the system path
2139 */
2140 fullname = Dir_FindFile(file,
2141 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
2142 }
2143
2144 if (fullname == NULL) {
2145 if (!silent)
2146 Parse_Error(PARSE_FATAL, "Could not find %s", file);
2147 return;
2148 }
2149
2150 /* Actually open the file... */
2151 fd = open(fullname, O_RDONLY);
2152 if (fd == -1) {
2153 if (!silent)
2154 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
2155 free(fullname);
2156 return;
2157 }
2158
2159 /* load it */
2160 lf = loadfile(fullname, fd);
2161
2162 /* Start reading from this file next */
2163 Parse_SetInput(fullname, 0, -1, loadedfile_nextbuf, lf);
2164 curFile->lf = lf;
2165 }
2166
2167 static void
2168 ParseDoInclude(char *line)
2169 {
2170 char endc; /* the character which ends the file spec */
2171 char *cp; /* current position in file spec */
2172 int silent = (*line != 'i') ? 1 : 0;
2173 char *file = &line[7 + silent];
2174
2175 /* Skip to delimiter character so we know where to look */
2176 while (*file == ' ' || *file == '\t')
2177 file++;
2178
2179 if (*file != '"' && *file != '<') {
2180 Parse_Error(PARSE_FATAL,
2181 ".include filename must be delimited by '\"' or '<'");
2182 return;
2183 }
2184
2185 /*
2186 * Set the search path on which to find the include file based on the
2187 * characters which bracket its name. Angle-brackets imply it's
2188 * a system Makefile while double-quotes imply it's a user makefile
2189 */
2190 if (*file == '<') {
2191 endc = '>';
2192 } else {
2193 endc = '"';
2194 }
2195
2196 /* Skip to matching delimiter */
2197 for (cp = ++file; *cp && *cp != endc; cp++)
2198 continue;
2199
2200 if (*cp != endc) {
2201 Parse_Error(PARSE_FATAL,
2202 "Unclosed %cinclude filename. '%c' expected",
2203 '.', endc);
2204 return;
2205 }
2206 *cp = '\0';
2207
2208 /*
2209 * Substitute for any variables in the file name before trying to
2210 * find the thing.
2211 */
2212 file = Var_Subst(NULL, file, VAR_CMD, FALSE);
2213
2214 Parse_include_file(file, endc == '>', silent);
2215 free(file);
2216 }
2217
2218
2219 /*-
2220 *---------------------------------------------------------------------
2221 * ParseSetParseFile --
2222 * Set the .PARSEDIR and .PARSEFILE variables to the dirname and
2223 * basename of the given filename
2224 *
2225 * Results:
2226 * None
2227 *
2228 * Side Effects:
2229 * The .PARSEDIR and .PARSEFILE variables are overwritten by the
2230 * dirname and basename of the given filename.
2231 *---------------------------------------------------------------------
2232 */
2233 static void
2234 ParseSetParseFile(const char *filename)
2235 {
2236 char *slash, *dirname;
2237 const char *pd, *pf;
2238 int len;
2239
2240 slash = strrchr(filename, '/');
2241 if (slash == NULL) {
2242 Var_Set(".PARSEDIR", pd = curdir, VAR_GLOBAL, 0);
2243 Var_Set(".PARSEFILE", pf = filename, VAR_GLOBAL, 0);
2244 dirname= NULL;
2245 } else {
2246 len = slash - filename;
2247 dirname = bmake_malloc(len + 1);
2248 memcpy(dirname, filename, len);
2249 dirname[len] = '\0';
2250 Var_Set(".PARSEDIR", pd = dirname, VAR_GLOBAL, 0);
2251 Var_Set(".PARSEFILE", pf = slash + 1, VAR_GLOBAL, 0);
2252 }
2253 if (DEBUG(PARSE))
2254 fprintf(debug_file, "ParseSetParseFile: ${.PARSEDIR} = `%s' "
2255 "${.PARSEFILE} = `%s'\n", pd, pf);
2256 free(dirname);
2257 }
2258
2259 /*
2260 * Track the makefiles we read - so makefiles can
2261 * set dependencies on them.
2262 * Avoid adding anything more than once.
2263 */
2264
2265 static void
2266 ParseTrackInput(const char *name)
2267 {
2268 char *old;
2269 char *fp = NULL;
2270 size_t name_len = strlen(name);
2271
2272 old = Var_Value(MAKE_MAKEFILES, VAR_GLOBAL, &fp);
2273 if (old) {
2274 /* does it contain name? */
2275 for (; old != NULL; old = strchr(old, ' ')) {
2276 if (*old == ' ')
2277 old++;
2278 if (memcmp(old, name, name_len) == 0
2279 && (old[name_len] == 0 || old[name_len] == ' '))
2280 goto cleanup;
2281 }
2282 }
2283 Var_Append (MAKE_MAKEFILES, name, VAR_GLOBAL);
2284 cleanup:
2285 if (fp) {
2286 free(fp);
2287 }
2288 }
2289
2290
2291 /*-
2292 *---------------------------------------------------------------------
2293 * Parse_setInput --
2294 * Start Parsing from the given source
2295 *
2296 * Results:
2297 * None
2298 *
2299 * Side Effects:
2300 * A structure is added to the includes Lst and readProc, lineno,
2301 * fname and curFile are altered for the new file
2302 *---------------------------------------------------------------------
2303 */
2304 void
2305 Parse_SetInput(const char *name, int line, int fd,
2306 char *(*nextbuf)(void *, size_t *), void *arg)
2307 {
2308 char *buf;
2309 size_t len;
2310
2311 if (name == NULL)
2312 name = curFile->fname;
2313 else
2314 ParseTrackInput(name);
2315
2316 if (DEBUG(PARSE))
2317 fprintf(debug_file, "Parse_SetInput: file %s, line %d, fd %d, nextbuf %p, arg %p\n",
2318 name, line, fd, nextbuf, arg);
2319
2320 if (fd == -1 && nextbuf == NULL)
2321 /* sanity */
2322 return;
2323
2324 if (curFile != NULL)
2325 /* Save exiting file info */
2326 Lst_AtFront(includes, curFile);
2327
2328 /* Allocate and fill in new structure */
2329 curFile = bmake_malloc(sizeof *curFile);
2330
2331 /*
2332 * Once the previous state has been saved, we can get down to reading
2333 * the new file. We set up the name of the file to be the absolute
2334 * name of the include file so error messages refer to the right
2335 * place.
2336 */
2337 curFile->fname = name;
2338 curFile->lineno = line;
2339 curFile->first_lineno = line;
2340 curFile->nextbuf = nextbuf;
2341 curFile->nextbuf_arg = arg;
2342 curFile->lf = NULL;
2343
2344 assert(nextbuf != NULL);
2345
2346 /* Get first block of input data */
2347 buf = curFile->nextbuf(curFile->nextbuf_arg, &len);
2348 if (buf == NULL) {
2349 /* Was all a waste of time ... */
2350 free(curFile);
2351 return;
2352 }
2353 curFile->P_str = buf;
2354 curFile->P_ptr = buf;
2355 curFile->P_end = buf+len;
2356
2357 curFile->cond_depth = Cond_save_depth();
2358 ParseSetParseFile(name);
2359 }
2360
2361 #ifdef SYSVINCLUDE
2362 /*-
2363 *---------------------------------------------------------------------
2364 * ParseTraditionalInclude --
2365 * Push to another file.
2366 *
2367 * The input is the current line. The file name(s) are
2368 * following the "include".
2369 *
2370 * Results:
2371 * None
2372 *
2373 * Side Effects:
2374 * A structure is added to the includes Lst and readProc, lineno,
2375 * fname and curFILE are altered for the new file
2376 *---------------------------------------------------------------------
2377 */
2378 static void
2379 ParseTraditionalInclude(char *line)
2380 {
2381 char *cp; /* current position in file spec */
2382 int done = 0;
2383 int silent = (line[0] != 'i') ? 1 : 0;
2384 char *file = &line[silent + 7];
2385 char *all_files;
2386
2387 if (DEBUG(PARSE)) {
2388 fprintf(debug_file, "ParseTraditionalInclude: %s\n", file);
2389 }
2390
2391 /*
2392 * Skip over whitespace
2393 */
2394 while (isspace((unsigned char)*file))
2395 file++;
2396
2397 /*
2398 * Substitute for any variables in the file name before trying to
2399 * find the thing.
2400 */
2401 all_files = Var_Subst(NULL, file, VAR_CMD, FALSE);
2402
2403 if (*file == '\0') {
2404 Parse_Error(PARSE_FATAL,
2405 "Filename missing from \"include\"");
2406 return;
2407 }
2408
2409 for (file = all_files; !done; file = cp + 1) {
2410 /* Skip to end of line or next whitespace */
2411 for (cp = file; *cp && !isspace((unsigned char) *cp); cp++)
2412 continue;
2413
2414 if (*cp)
2415 *cp = '\0';
2416 else
2417 done = 1;
2418
2419 Parse_include_file(file, FALSE, silent);
2420 }
2421 free(all_files);
2422 }
2423 #endif
2424
2425 #ifdef GMAKEEXPORT
2426 /*-
2427 *---------------------------------------------------------------------
2428 * ParseGmakeExport --
2429 * Parse export <variable>=<value>
2430 *
2431 * And set the environment with it.
2432 *
2433 * Results:
2434 * None
2435 *
2436 * Side Effects:
2437 * None
2438 *---------------------------------------------------------------------
2439 */
2440 static void
2441 ParseGmakeExport(char *line)
2442 {
2443 char *variable = &line[6];
2444 char *value;
2445
2446 if (DEBUG(PARSE)) {
2447 fprintf(debug_file, "ParseGmakeExport: %s\n", variable);
2448 }
2449
2450 /*
2451 * Skip over whitespace
2452 */
2453 while (isspace((unsigned char)*variable))
2454 variable++;
2455
2456 for (value = variable; *value && *value != '='; value++)
2457 continue;
2458
2459 if (*value != '=') {
2460 Parse_Error(PARSE_FATAL,
2461 "Variable/Value missing from \"export\"");
2462 return;
2463 }
2464
2465 /*
2466 * Expand the value before putting it in the environment.
2467 */
2468 value = Var_Subst(NULL, value, VAR_CMD, FALSE);
2469 setenv(variable, value, 1);
2470 }
2471 #endif
2472
2473 /*-
2474 *---------------------------------------------------------------------
2475 * ParseEOF --
2476 * Called when EOF is reached in the current file. If we were reading
2477 * an include file, the includes stack is popped and things set up
2478 * to go back to reading the previous file at the previous location.
2479 *
2480 * Results:
2481 * CONTINUE if there's more to do. DONE if not.
2482 *
2483 * Side Effects:
2484 * The old curFILE, is closed. The includes list is shortened.
2485 * lineno, curFILE, and fname are changed if CONTINUE is returned.
2486 *---------------------------------------------------------------------
2487 */
2488 static int
2489 ParseEOF(void)
2490 {
2491 char *ptr;
2492 size_t len;
2493
2494 assert(curFile->nextbuf != NULL);
2495
2496 /* get next input buffer, if any */
2497 ptr = curFile->nextbuf(curFile->nextbuf_arg, &len);
2498 curFile->P_ptr = ptr;
2499 curFile->P_str = ptr;
2500 curFile->P_end = ptr + len;
2501 curFile->lineno = curFile->first_lineno;
2502 if (ptr != NULL) {
2503 /* Iterate again */
2504 return CONTINUE;
2505 }
2506
2507 /* Ensure the makefile (or loop) didn't have mismatched conditionals */
2508 Cond_restore_depth(curFile->cond_depth);
2509
2510 if (curFile->lf != NULL) {
2511 loadedfile_destroy(curFile->lf);
2512 curFile->lf = NULL;
2513 }
2514
2515 /* Dispose of curFile info */
2516 /* Leak curFile->fname because all the gnodes have pointers to it */
2517 free(curFile->P_str);
2518 free(curFile);
2519
2520 curFile = Lst_DeQueue(includes);
2521
2522 if (curFile == NULL) {
2523 /* We've run out of input */
2524 Var_Delete(".PARSEDIR", VAR_GLOBAL);
2525 Var_Delete(".PARSEFILE", VAR_GLOBAL);
2526 return DONE;
2527 }
2528
2529 if (DEBUG(PARSE))
2530 fprintf(debug_file, "ParseEOF: returning to file %s, line %d\n",
2531 curFile->fname, curFile->lineno);
2532
2533 /* Restore the PARSEDIR/PARSEFILE variables */
2534 ParseSetParseFile(curFile->fname);
2535 return (CONTINUE);
2536 }
2537
2538 #define PARSE_RAW 1
2539 #define PARSE_SKIP 2
2540
2541 static char *
2542 ParseGetLine(int flags, int *length)
2543 {
2544 IFile *cf = curFile;
2545 char *ptr;
2546 char ch;
2547 char *line;
2548 char *line_end;
2549 char *escaped;
2550 char *comment;
2551 char *tp;
2552
2553 /* Loop through blank lines and comment lines */
2554 for (;;) {
2555 cf->lineno++;
2556 line = cf->P_ptr;
2557 ptr = line;
2558 line_end = line;
2559 escaped = NULL;
2560 comment = NULL;
2561 for (;;) {
2562 if (cf->P_end != NULL && ptr == cf->P_end) {
2563 /* end of buffer */
2564 ch = 0;
2565 break;
2566 }
2567 ch = *ptr;
2568 if (ch == 0 || (ch == '\\' && ptr[1] == 0)) {
2569 if (cf->P_end == NULL)
2570 /* End of string (aka for loop) data */
2571 break;
2572 if (cf->nextbuf != NULL) {
2573 /*
2574 * End of this buffer; return EOF and outer logic
2575 * will get the next one. (eww)
2576 */
2577 break;
2578 }
2579 Parse_Error(PARSE_FATAL, "Zero byte read from file");
2580 return NULL;
2581 }
2582
2583 if (ch == '\\') {
2584 /* Don't treat next character as special, remember first one */
2585 if (escaped == NULL)
2586 escaped = ptr;
2587 if (ptr[1] == '\n')
2588 cf->lineno++;
2589 ptr += 2;
2590 line_end = ptr;
2591 continue;
2592 }
2593 if (ch == '#' && comment == NULL) {
2594 /* Remember first '#' for comment stripping */
2595 /* Unless previous char was '[', as in modifier :[#] */
2596 if (!(ptr > line && ptr[-1] == '['))
2597 comment = line_end;
2598 }
2599 ptr++;
2600 if (ch == '\n')
2601 break;
2602 if (!isspace((unsigned char)ch))
2603 /* We are not interested in trailing whitespace */
2604 line_end = ptr;
2605 }
2606
2607 /* Save next 'to be processed' location */
2608 cf->P_ptr = ptr;
2609
2610 /* Check we have a non-comment, non-blank line */
2611 if (line_end == line || comment == line) {
2612 if (ch == 0)
2613 /* At end of file */
2614 return NULL;
2615 /* Parse another line */
2616 continue;
2617 }
2618
2619 /* We now have a line of data */
2620 *line_end = 0;
2621
2622 if (flags & PARSE_RAW) {
2623 /* Leave '\' (etc) in line buffer (eg 'for' lines) */
2624 *length = line_end - line;
2625 return line;
2626 }
2627
2628 if (flags & PARSE_SKIP) {
2629 /* Completely ignore non-directives */
2630 if (line[0] != '.')
2631 continue;
2632 /* We could do more of the .else/.elif/.endif checks here */
2633 }
2634 break;
2635 }
2636
2637 /* Brutally ignore anything after a non-escaped '#' in non-commands */
2638 if (comment != NULL && line[0] != '\t') {
2639 line_end = comment;
2640 *line_end = 0;
2641 }
2642
2643 /* If we didn't see a '\\' then the in-situ data is fine */
2644 if (escaped == NULL) {
2645 *length = line_end - line;
2646 return line;
2647 }
2648
2649 /* Remove escapes from '\n' and '#' */
2650 tp = ptr = escaped;
2651 escaped = line;
2652 for (; ; *tp++ = ch) {
2653 ch = *ptr++;
2654 if (ch != '\\') {
2655 if (ch == 0)
2656 break;
2657 continue;
2658 }
2659
2660 ch = *ptr++;
2661 if (ch == 0) {
2662 /* Delete '\\' at end of buffer */
2663 tp--;
2664 break;
2665 }
2666
2667 if (ch == '#' && line[0] != '\t')
2668 /* Delete '\\' from before '#' on non-command lines */
2669 continue;
2670
2671 if (ch != '\n') {
2672 /* Leave '\\' in buffer for later */
2673 *tp++ = '\\';
2674 /* Make sure we don't delete an escaped ' ' from the line end */
2675 escaped = tp + 1;
2676 continue;
2677 }
2678
2679 /* Escaped '\n' replace following whitespace with a single ' ' */
2680 while (ptr[0] == ' ' || ptr[0] == '\t')
2681 ptr++;
2682 ch = ' ';
2683 }
2684
2685 /* Delete any trailing spaces - eg from empty continuations */
2686 while (tp > escaped && isspace((unsigned char)tp[-1]))
2687 tp--;
2688
2689 *tp = 0;
2690 *length = tp - line;
2691 return line;
2692 }
2693
2694 /*-
2695 *---------------------------------------------------------------------
2696 * ParseReadLine --
2697 * Read an entire line from the input file. Called only by Parse_File.
2698 *
2699 * Results:
2700 * A line w/o its newline
2701 *
2702 * Side Effects:
2703 * Only those associated with reading a character
2704 *---------------------------------------------------------------------
2705 */
2706 static char *
2707 ParseReadLine(void)
2708 {
2709 char *line; /* Result */
2710 int lineLength; /* Length of result */
2711 int lineno; /* Saved line # */
2712 int rval;
2713
2714 for (;;) {
2715 line = ParseGetLine(0, &lineLength);
2716 if (line == NULL)
2717 return NULL;
2718
2719 if (line[0] != '.')
2720 return line;
2721
2722 /*
2723 * The line might be a conditional. Ask the conditional module
2724 * about it and act accordingly
2725 */
2726 switch (Cond_Eval(line)) {
2727 case COND_SKIP:
2728 /* Skip to next conditional that evaluates to COND_PARSE. */
2729 do {
2730 line = ParseGetLine(PARSE_SKIP, &lineLength);
2731 } while (line && Cond_Eval(line) != COND_PARSE);
2732 if (line == NULL)
2733 break;
2734 continue;
2735 case COND_PARSE:
2736 continue;
2737 case COND_INVALID: /* Not a conditional line */
2738 /* Check for .for loops */
2739 rval = For_Eval(line);
2740 if (rval == 0)
2741 /* Not a .for line */
2742 break;
2743 if (rval < 0)
2744 /* Syntax error - error printed, ignore line */
2745 continue;
2746 /* Start of a .for loop */
2747 lineno = curFile->lineno;
2748 /* Accumulate loop lines until matching .endfor */
2749 do {
2750 line = ParseGetLine(PARSE_RAW, &lineLength);
2751 if (line == NULL) {
2752 Parse_Error(PARSE_FATAL,
2753 "Unexpected end of file in for loop.");
2754 break;
2755 }
2756 } while (For_Accum(line));
2757 /* Stash each iteration as a new 'input file' */
2758 For_Run(lineno);
2759 /* Read next line from for-loop buffer */
2760 continue;
2761 }
2762 return (line);
2763 }
2764 }
2765
2766 /*-
2767 *-----------------------------------------------------------------------
2768 * ParseFinishLine --
2769 * Handle the end of a dependency group.
2770 *
2771 * Results:
2772 * Nothing.
2773 *
2774 * Side Effects:
2775 * inLine set FALSE. 'targets' list destroyed.
2776 *
2777 *-----------------------------------------------------------------------
2778 */
2779 static void
2780 ParseFinishLine(void)
2781 {
2782 if (inLine) {
2783 Lst_ForEach(targets, Suff_EndTransform, NULL);
2784 Lst_Destroy(targets, ParseHasCommands);
2785 targets = NULL;
2786 inLine = FALSE;
2787 }
2788 }
2789
2790
2791 /*-
2792 *---------------------------------------------------------------------
2793 * Parse_File --
2794 * Parse a file into its component parts, incorporating it into the
2795 * current dependency graph. This is the main function and controls
2796 * almost every other function in this module
2797 *
2798 * Input:
2799 * name the name of the file being read
2800 * fd Open file to makefile to parse
2801 *
2802 * Results:
2803 * None
2804 *
2805 * Side Effects:
2806 * closes fd.
2807 * Loads. Nodes are added to the list of all targets, nodes and links
2808 * are added to the dependency graph. etc. etc. etc.
2809 *---------------------------------------------------------------------
2810 */
2811 void
2812 Parse_File(const char *name, int fd)
2813 {
2814 char *cp; /* pointer into the line */
2815 char *line; /* the line we're working on */
2816 struct loadedfile *lf;
2817
2818 lf = loadfile(name, fd);
2819
2820 inLine = FALSE;
2821 fatals = 0;
2822
2823 if (name == NULL) {
2824 name = "(stdin)";
2825 }
2826
2827 Parse_SetInput(name, 0, -1, loadedfile_nextbuf, lf);
2828 curFile->lf = lf;
2829
2830 do {
2831 for (; (line = ParseReadLine()) != NULL; ) {
2832 if (DEBUG(PARSE))
2833 fprintf(debug_file, "ParseReadLine (%d): '%s'\n",
2834 curFile->lineno, line);
2835 if (*line == '.') {
2836 /*
2837 * Lines that begin with the special character may be
2838 * include or undef directives.
2839 * On the other hand they can be suffix rules (.c.o: ...)
2840 * or just dependencies for filenames that start '.'.
2841 */
2842 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2843 continue;
2844 }
2845 if (strncmp(cp, "include", 7) == 0 ||
2846 ((cp[0] == 's' || cp[0] == '-') &&
2847 strncmp(&cp[1], "include", 7) == 0)) {
2848 ParseDoInclude(cp);
2849 continue;
2850 }
2851 if (strncmp(cp, "undef", 5) == 0) {
2852 char *cp2;
2853 for (cp += 5; isspace((unsigned char) *cp); cp++)
2854 continue;
2855 for (cp2 = cp; !isspace((unsigned char) *cp2) &&
2856 (*cp2 != '\0'); cp2++)
2857 continue;
2858 *cp2 = '\0';
2859 Var_Delete(cp, VAR_GLOBAL);
2860 continue;
2861 } else if (strncmp(cp, "export", 6) == 0) {
2862 for (cp += 6; isspace((unsigned char) *cp); cp++)
2863 continue;
2864 Var_Export(cp, 1);
2865 continue;
2866 } else if (strncmp(cp, "unexport", 8) == 0) {
2867 Var_UnExport(cp);
2868 continue;
2869 } else if (strncmp(cp, "info", 4) == 0 ||
2870 strncmp(cp, "error", 5) == 0 ||
2871 strncmp(cp, "warning", 7) == 0) {
2872 if (ParseMessage(cp))
2873 continue;
2874 }
2875 }
2876
2877 if (*line == '\t') {
2878 /*
2879 * If a line starts with a tab, it can only hope to be
2880 * a creation command.
2881 */
2882 cp = line + 1;
2883 shellCommand:
2884 for (; isspace ((unsigned char)*cp); cp++) {
2885 continue;
2886 }
2887 if (*cp) {
2888 if (!inLine)
2889 Parse_Error(PARSE_FATAL,
2890 "Unassociated shell command \"%s\"",
2891 cp);
2892 /*
2893 * So long as it's not a blank line and we're actually
2894 * in a dependency spec, add the command to the list of
2895 * commands of all targets in the dependency spec
2896 */
2897 if (targets) {
2898 cp = bmake_strdup(cp);
2899 Lst_ForEach(targets, ParseAddCmd, cp);
2900 #ifdef CLEANUP
2901 Lst_AtEnd(targCmds, cp);
2902 #endif
2903 }
2904 }
2905 continue;
2906 }
2907
2908 #ifdef SYSVINCLUDE
2909 if (((strncmp(line, "include", 7) == 0 &&
2910 isspace((unsigned char) line[7])) ||
2911 ((line[0] == 's' || line[0] == '-') &&
2912 strncmp(&line[1], "include", 7) == 0 &&
2913 isspace((unsigned char) line[8]))) &&
2914 strchr(line, ':') == NULL) {
2915 /*
2916 * It's an S3/S5-style "include".
2917 */
2918 ParseTraditionalInclude(line);
2919 continue;
2920 }
2921 #endif
2922 #ifdef GMAKEEXPORT
2923 if (strncmp(line, "export", 6) == 0 &&
2924 isspace((unsigned char) line[6]) &&
2925 strchr(line, ':') == NULL) {
2926 /*
2927 * It's a Gmake "export".
2928 */
2929 ParseGmakeExport(line);
2930 continue;
2931 }
2932 #endif
2933 if (Parse_IsVar(line)) {
2934 ParseFinishLine();
2935 Parse_DoVar(line, VAR_GLOBAL);
2936 continue;
2937 }
2938
2939 #ifndef POSIX
2940 /*
2941 * To make life easier on novices, if the line is indented we
2942 * first make sure the line has a dependency operator in it.
2943 * If it doesn't have an operator and we're in a dependency
2944 * line's script, we assume it's actually a shell command
2945 * and add it to the current list of targets.
2946 */
2947 cp = line;
2948 if (isspace((unsigned char) line[0])) {
2949 while ((*cp != '\0') && isspace((unsigned char) *cp))
2950 cp++;
2951 while (*cp && (ParseIsEscaped(line, cp) ||
2952 (*cp != ':') && (*cp != '!'))) {
2953 cp++;
2954 }
2955 if (*cp == '\0') {
2956 if (inLine) {
2957 Parse_Error(PARSE_WARNING,
2958 "Shell command needs a leading tab");
2959 goto shellCommand;
2960 }
2961 }
2962 }
2963 #endif
2964 ParseFinishLine();
2965
2966 /*
2967 * For some reason - probably to make the parser impossible -
2968 * a ';' can be used to separate commands from dependencies.
2969 * Attempt to avoid ';' inside substitution patterns.
2970 */
2971 {
2972 int level = 0;
2973
2974 for (cp = line; *cp != 0; cp++) {
2975 if (*cp == '\\' && cp[1] != 0) {
2976 cp++;
2977 continue;
2978 }
2979 if (*cp == '$' &&
2980 (cp[1] == '(' || cp[1] == '{')) {
2981 level++;
2982 continue;
2983 }
2984 if (level > 0) {
2985 if (*cp == ')' || *cp == '}') {
2986 level--;
2987 continue;
2988 }
2989 } else if (*cp == ';') {
2990 break;
2991 }
2992 }
2993 }
2994 if (*cp != 0)
2995 /* Terminate the dependency list at the ';' */
2996 *cp++ = 0;
2997 else
2998 cp = NULL;
2999
3000 /*
3001 * We now know it's a dependency line so it needs to have all
3002 * variables expanded before being parsed. Tell the variable
3003 * module to complain if some variable is undefined...
3004 */
3005 line = Var_Subst(NULL, line, VAR_CMD, TRUE);
3006
3007 /*
3008 * Need a non-circular list for the target nodes
3009 */
3010 if (targets)
3011 Lst_Destroy(targets, NULL);
3012
3013 targets = Lst_Init(FALSE);
3014 inLine = TRUE;
3015
3016 ParseDoDependency(line);
3017 free(line);
3018
3019 /* If there were commands after a ';', add them now */
3020 if (cp != NULL) {
3021 goto shellCommand;
3022 }
3023 }
3024 /*
3025 * Reached EOF, but it may be just EOF of an include file...
3026 */
3027 } while (ParseEOF() == CONTINUE);
3028
3029 if (fatals) {
3030 (void)fflush(stdout);
3031 (void)fprintf(stderr,
3032 "%s: Fatal errors encountered -- cannot continue",
3033 progname);
3034 PrintOnError(NULL, NULL);
3035 exit(1);
3036 }
3037 }
3038
3039 /*-
3040 *---------------------------------------------------------------------
3041 * Parse_Init --
3042 * initialize the parsing module
3043 *
3044 * Results:
3045 * none
3046 *
3047 * Side Effects:
3048 * the parseIncPath list is initialized...
3049 *---------------------------------------------------------------------
3050 */
3051 void
3052 Parse_Init(void)
3053 {
3054 mainNode = NULL;
3055 parseIncPath = Lst_Init(FALSE);
3056 sysIncPath = Lst_Init(FALSE);
3057 defIncPath = Lst_Init(FALSE);
3058 includes = Lst_Init(FALSE);
3059 #ifdef CLEANUP
3060 targCmds = Lst_Init(FALSE);
3061 #endif
3062 }
3063
3064 void
3065 Parse_End(void)
3066 {
3067 #ifdef CLEANUP
3068 Lst_Destroy(targCmds, (FreeProc *)free);
3069 if (targets)
3070 Lst_Destroy(targets, NULL);
3071 Lst_Destroy(defIncPath, Dir_Destroy);
3072 Lst_Destroy(sysIncPath, Dir_Destroy);
3073 Lst_Destroy(parseIncPath, Dir_Destroy);
3074 Lst_Destroy(includes, NULL); /* Should be empty now */
3075 #endif
3076 }
3077
3078
3079 /*-
3080 *-----------------------------------------------------------------------
3081 * Parse_MainName --
3082 * Return a Lst of the main target to create for main()'s sake. If
3083 * no such target exists, we Punt with an obnoxious error message.
3084 *
3085 * Results:
3086 * A Lst of the single node to create.
3087 *
3088 * Side Effects:
3089 * None.
3090 *
3091 *-----------------------------------------------------------------------
3092 */
3093 Lst
3094 Parse_MainName(void)
3095 {
3096 Lst mainList; /* result list */
3097
3098 mainList = Lst_Init(FALSE);
3099
3100 if (mainNode == NULL) {
3101 Punt("no target to make.");
3102 /*NOTREACHED*/
3103 } else if (mainNode->type & OP_DOUBLEDEP) {
3104 (void)Lst_AtEnd(mainList, mainNode);
3105 Lst_Concat(mainList, mainNode->cohorts, LST_CONCNEW);
3106 }
3107 else
3108 (void)Lst_AtEnd(mainList, mainNode);
3109 Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL);
3110 return (mainList);
3111 }
3112
3113 /*-
3114 *-----------------------------------------------------------------------
3115 * ParseMark --
3116 * Add the filename and lineno to the GNode so that we remember
3117 * where it was first defined.
3118 *
3119 * Side Effects:
3120 * None.
3121 *
3122 *-----------------------------------------------------------------------
3123 */
3124 static void
3125 ParseMark(GNode *gn)
3126 {
3127 gn->fname = curFile->fname;
3128 gn->lineno = curFile->lineno;
3129 }
3130