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