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