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