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