xref: /freebsd/contrib/bmake/cond.c (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1 /*	$NetBSD: cond.c,v 1.359 2023/12/29 12:59:43 rillig Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5  * 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) 1988, 1989 by Adam de Boor
37  * Copyright (c) 1989 by Berkeley Softworks
38  * All rights reserved.
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Adam de Boor.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  */
71 
72 /*
73  * Handling of conditionals in a makefile.
74  *
75  * Interface:
76  *	Cond_EvalLine   Evaluate the conditional directive, such as
77  *			'.if <cond>', '.elifnmake <cond>', '.else', '.endif'.
78  *
79  *	Cond_EvalCondition
80  *			Evaluate the conditional, which is either the argument
81  *			of one of the .if directives or the condition in a
82  *			':?then:else' variable modifier.
83  *
84  *	Cond_EndFile	At the end of reading a makefile, ensure that the
85  *			conditional directives are well-balanced.
86  */
87 
88 #include <errno.h>
89 
90 #include "make.h"
91 #include "dir.h"
92 
93 /*	"@(#)cond.c	8.2 (Berkeley) 1/2/94"	*/
94 MAKE_RCSID("$NetBSD: cond.c,v 1.359 2023/12/29 12:59:43 rillig Exp $");
95 
96 /*
97  * Conditional expressions conform to this grammar:
98  *	Or -> And ('||' And)*
99  *	And -> Term ('&&' Term)*
100  *	Term -> Function '(' Argument ')'
101  *	Term -> Leaf Operator Leaf
102  *	Term -> Leaf
103  *	Term -> '(' Or ')'
104  *	Term -> '!' Term
105  *	Leaf -> "string"
106  *	Leaf -> Number
107  *	Leaf -> VariableExpression
108  *	Leaf -> BareWord
109  *	Operator -> '==' | '!=' | '>' | '<' | '>=' | '<='
110  *
111  * BareWord is an unquoted string literal, its evaluation depends on the kind
112  * of '.if' directive.
113  *
114  * The tokens are scanned by CondParser_Token, which returns:
115  *	TOK_AND		for '&&'
116  *	TOK_OR		for '||'
117  *	TOK_NOT		for '!'
118  *	TOK_LPAREN	for '('
119  *	TOK_RPAREN	for ')'
120  *
121  * Other terminal symbols are evaluated using either the default function or
122  * the function given in the terminal, they return either TOK_TRUE, TOK_FALSE
123  * or TOK_ERROR.
124  */
125 typedef enum Token {
126 	TOK_FALSE, TOK_TRUE, TOK_AND, TOK_OR, TOK_NOT,
127 	TOK_LPAREN, TOK_RPAREN, TOK_EOF, TOK_NONE, TOK_ERROR
128 } Token;
129 
130 typedef enum ComparisonOp {
131 	LT, LE, GT, GE, EQ, NE
132 } ComparisonOp;
133 
134 typedef struct CondParser {
135 
136 	/*
137 	 * The plain '.if ${VAR}' evaluates to true if the value of the
138 	 * expression has length > 0 and is not numerically zero.  The other
139 	 * '.if' variants delegate to evalBare instead, for example '.ifdef
140 	 * ${VAR}' is equivalent to '.if defined(${VAR})', checking whether
141 	 * the variable named by the expression '${VAR}' is defined.
142 	 */
143 	bool plain;
144 
145 	/* The function to apply on unquoted bare words. */
146 	bool (*evalBare)(const char *);
147 	bool negateEvalBare;
148 
149 	/*
150 	 * Whether the left-hand side of a comparison may be an unquoted
151 	 * string.  This is allowed for expressions of the form
152 	 * ${condition:?:}, see ApplyModifier_IfElse.  Such a condition is
153 	 * expanded before it is evaluated, due to ease of implementation.
154 	 * This means that at the point where the condition is evaluated,
155 	 * make cannot know anymore whether the left-hand side had originally
156 	 * been an expression or a plain word.
157 	 *
158 	 * In conditional directives like '.if', the left-hand side must
159 	 * either be an expression, a quoted string or a number.
160 	 */
161 	bool leftUnquotedOK;
162 
163 	const char *p;		/* The remaining condition to parse */
164 	Token curr;		/* Single push-back token used in parsing */
165 
166 	/*
167 	 * Whether an error message has already been printed for this
168 	 * condition.
169 	 */
170 	bool printedError;
171 } CondParser;
172 
173 static CondResult CondParser_Or(CondParser *, bool);
174 
175 unsigned int cond_depth = 0;	/* current .if nesting level */
176 
177 /* Names for ComparisonOp. */
178 static const char opname[][3] = { "<", "<=", ">", ">=", "==", "!=" };
179 
180 MAKE_INLINE bool
181 skip_string(const char **pp, const char *str)
182 {
183 	size_t len = strlen(str);
184 	bool ok = strncmp(*pp, str, len) == 0;
185 	if (ok)
186 		*pp += len;
187 	return ok;
188 }
189 
190 static Token
191 ToToken(bool cond)
192 {
193 	return cond ? TOK_TRUE : TOK_FALSE;
194 }
195 
196 static void
197 CondParser_SkipWhitespace(CondParser *par)
198 {
199 	cpp_skip_whitespace(&par->p);
200 }
201 
202 /*
203  * Parse a single word, taking into account balanced parentheses as well as
204  * embedded expressions.  Used for the argument of a built-in function as
205  * well as for bare words, which are then passed to the default function.
206  */
207 static char *
208 ParseWord(const char **pp, bool doEval)
209 {
210 	const char *p = *pp;
211 	Buffer word;
212 	int depth;
213 
214 	Buf_Init(&word);
215 
216 	depth = 0;
217 	for (;;) {
218 		char ch = *p;
219 		if (ch == '\0' || ch == ' ' || ch == '\t')
220 			break;
221 		if ((ch == '&' || ch == '|') && depth == 0)
222 			break;
223 		if (ch == '$') {
224 			VarEvalMode emode = doEval
225 			    ? VARE_UNDEFERR
226 			    : VARE_PARSE_ONLY;
227 			/*
228 			 * TODO: make Var_Parse complain about undefined
229 			 * variables.
230 			 */
231 			FStr nestedVal = Var_Parse(&p, SCOPE_CMDLINE, emode);
232 			/* TODO: handle errors */
233 			Buf_AddStr(&word, nestedVal.str);
234 			FStr_Done(&nestedVal);
235 			continue;
236 		}
237 		if (ch == '(')
238 			depth++;
239 		else if (ch == ')' && --depth < 0)
240 			break;
241 		Buf_AddByte(&word, ch);
242 		p++;
243 	}
244 
245 	cpp_skip_hspace(&p);
246 	*pp = p;
247 
248 	return Buf_DoneData(&word);
249 }
250 
251 /* Parse the function argument, including the surrounding parentheses. */
252 static char *
253 ParseFuncArg(CondParser *par, const char **pp, bool doEval, const char *func)
254 {
255 	const char *p = *pp;
256 	char *res;
257 
258 	p++;			/* skip the '(' */
259 	cpp_skip_hspace(&p);
260 	res = ParseWord(&p, doEval);
261 	cpp_skip_hspace(&p);
262 
263 	if (*p++ != ')') {
264 		int len = 0;
265 		while (ch_isalpha(func[len]))
266 			len++;
267 
268 		Parse_Error(PARSE_FATAL,
269 		    "Missing closing parenthesis for %.*s()", len, func);
270 		par->printedError = true;
271 		free(res);
272 		return NULL;
273 	}
274 
275 	*pp = p;
276 	return res;
277 }
278 
279 /* See if the given variable is defined. */
280 static bool
281 FuncDefined(const char *var)
282 {
283 	return Var_Exists(SCOPE_CMDLINE, var);
284 }
285 
286 /* See if a target matching targetPattern is requested to be made. */
287 static bool
288 FuncMake(const char *targetPattern)
289 {
290 	StringListNode *ln;
291 	bool warned = false;
292 
293 	for (ln = opts.create.first; ln != NULL; ln = ln->next) {
294 		StrMatchResult res = Str_Match(ln->datum, targetPattern);
295 		if (res.error != NULL && !warned) {
296 			warned = true;
297 			Parse_Error(PARSE_WARNING,
298 			    "%s in pattern argument '%s' to function 'make'",
299 			    res.error, targetPattern);
300 		}
301 		if (res.matched)
302 			return true;
303 	}
304 	return false;
305 }
306 
307 /* See if the given file exists. */
308 static bool
309 FuncExists(const char *file)
310 {
311 	bool result;
312 	char *path;
313 
314 	path = Dir_FindFile(file, &dirSearchPath);
315 	DEBUG2(COND, "exists(%s) result is \"%s\"\n",
316 	    file, path != NULL ? path : "");
317 	result = path != NULL;
318 	free(path);
319 	return result;
320 }
321 
322 /* See if the given node exists and is an actual target. */
323 static bool
324 FuncTarget(const char *node)
325 {
326 	GNode *gn = Targ_FindNode(node);
327 	return gn != NULL && GNode_IsTarget(gn);
328 }
329 
330 /*
331  * See if the given node exists and is an actual target with commands
332  * associated with it.
333  */
334 static bool
335 FuncCommands(const char *node)
336 {
337 	GNode *gn = Targ_FindNode(node);
338 	return gn != NULL && GNode_IsTarget(gn) &&
339 	       !Lst_IsEmpty(&gn->commands);
340 }
341 
342 /*
343  * Convert the string to a floating point number.  Accepted formats are
344  * base-10 integer, base-16 integer and finite floating point numbers.
345  */
346 static bool
347 TryParseNumber(const char *str, double *out_value)
348 {
349 	char *end;
350 	unsigned long ul_val;
351 	double dbl_val;
352 
353 	if (str[0] == '\0') {	/* XXX: why is an empty string a number? */
354 		*out_value = 0.0;
355 		return true;
356 	}
357 
358 	errno = 0;
359 	ul_val = strtoul(str, &end, str[1] == 'x' ? 16 : 10);
360 	if (*end == '\0' && errno != ERANGE) {
361 		*out_value = str[0] == '-' ? -(double)-ul_val : (double)ul_val;
362 		return true;
363 	}
364 
365 	if (*end != '\0' && *end != '.' && *end != 'e' && *end != 'E')
366 		return false;	/* skip the expensive strtod call */
367 	dbl_val = strtod(str, &end);
368 	if (*end != '\0')
369 		return false;
370 
371 	*out_value = dbl_val;
372 	return true;
373 }
374 
375 static bool
376 is_separator(char ch)
377 {
378 	return ch == '\0' || ch_isspace(ch) || ch == '!' || ch == '=' ||
379 	       ch == '>' || ch == '<' || ch == ')' /* but not '(' */;
380 }
381 
382 /*
383  * In a quoted or unquoted string literal or a number, parse an
384  * expression and add its value to the buffer.
385  *
386  * Return whether to continue parsing the leaf.
387  *
388  * Example: .if x${CENTER}y == "${PREFIX}${SUFFIX}" || 0x${HEX}
389  */
390 static bool
391 CondParser_StringExpr(CondParser *par, const char *start,
392 		      bool doEval, bool quoted,
393 		      Buffer *buf, FStr *inout_str)
394 {
395 	VarEvalMode emode;
396 	const char *p;
397 	bool atStart;
398 
399 	emode = doEval && quoted ? VARE_WANTRES
400 	    : doEval ? VARE_UNDEFERR
401 	    : VARE_PARSE_ONLY;
402 
403 	p = par->p;
404 	atStart = p == start;
405 	*inout_str = Var_Parse(&p, SCOPE_CMDLINE, emode);
406 	/* TODO: handle errors */
407 	if (inout_str->str == var_Error) {
408 		FStr_Done(inout_str);
409 		*inout_str = FStr_InitRefer(NULL);
410 		return false;
411 	}
412 	par->p = p;
413 
414 	/*
415 	 * If the '$' started the string literal (which means no quotes), and
416 	 * the expression is followed by a space, a comparison operator or
417 	 * the end of the expression, we are done.
418 	 */
419 	if (atStart && is_separator(par->p[0]))
420 		return false;
421 
422 	Buf_AddStr(buf, inout_str->str);
423 	FStr_Done(inout_str);
424 	*inout_str = FStr_InitRefer(NULL);	/* not finished yet */
425 	return true;
426 }
427 
428 /*
429  * Parse a string from an expression or an optionally quoted string,
430  * on the left-hand and right-hand sides of comparisons.
431  *
432  * Results:
433  *	Returns the string without any enclosing quotes, or NULL on error.
434  *	Sets out_quoted if the leaf was a quoted string literal.
435  */
436 static void
437 CondParser_Leaf(CondParser *par, bool doEval, bool unquotedOK,
438 		  FStr *out_str, bool *out_quoted)
439 {
440 	Buffer buf;
441 	FStr str;
442 	bool quoted;
443 	const char *start;
444 
445 	Buf_Init(&buf);
446 	str = FStr_InitRefer(NULL);
447 	*out_quoted = quoted = par->p[0] == '"';
448 	start = par->p;
449 	if (quoted)
450 		par->p++;
451 
452 	while (par->p[0] != '\0' && str.str == NULL) {
453 		switch (par->p[0]) {
454 		case '\\':
455 			par->p++;
456 			if (par->p[0] != '\0') {
457 				Buf_AddByte(&buf, par->p[0]);
458 				par->p++;
459 			}
460 			continue;
461 		case '"':
462 			par->p++;
463 			if (quoted)
464 				goto return_buf;	/* skip the closing quote */
465 			Buf_AddByte(&buf, '"');
466 			continue;
467 		case ')':	/* see is_separator */
468 		case '!':
469 		case '=':
470 		case '>':
471 		case '<':
472 		case ' ':
473 		case '\t':
474 			if (!quoted)
475 				goto return_buf;
476 			Buf_AddByte(&buf, par->p[0]);
477 			par->p++;
478 			continue;
479 		case '$':
480 			if (!CondParser_StringExpr(par,
481 			    start, doEval, quoted, &buf, &str))
482 				goto return_str;
483 			continue;
484 		default:
485 			if (!unquotedOK && !quoted && *start != '$' &&
486 			    !ch_isdigit(*start)) {
487 				str = FStr_InitRefer(NULL);
488 				goto return_str;
489 			}
490 			Buf_AddByte(&buf, par->p[0]);
491 			par->p++;
492 			continue;
493 		}
494 	}
495 return_buf:
496 	str = FStr_InitOwn(buf.data);
497 	buf.data = NULL;
498 return_str:
499 	Buf_Done(&buf);
500 	*out_str = str;
501 }
502 
503 /*
504  * Evaluate a "comparison without operator", such as in ".if ${VAR}" or
505  * ".if 0".
506  */
507 static bool
508 EvalTruthy(CondParser *par, const char *value, bool quoted)
509 {
510 	double num;
511 
512 	/* For .ifxxx "...", check for non-empty string. */
513 	if (quoted)
514 		return value[0] != '\0';
515 
516 	/* For .ifxxx <number>, compare against zero */
517 	if (TryParseNumber(value, &num))
518 		return num != 0.0;
519 
520 	/*
521 	 * For .if ${...}, check for non-empty string.  This is different
522 	 * from the evaluation function from that .if variant, which would
523 	 * test whether a variable of the given name were defined.
524 	 */
525 	/*
526 	 * XXX: Whitespace should count as empty, just as in
527 	 * CondParser_FuncCallEmpty.
528 	 */
529 	if (par->plain)
530 		return value[0] != '\0';
531 
532 	return par->evalBare(value) != par->negateEvalBare;
533 }
534 
535 /* Evaluate a numerical comparison, such as in ".if ${VAR} >= 9". */
536 static bool
537 EvalCompareNum(double lhs, ComparisonOp op, double rhs)
538 {
539 	DEBUG3(COND, "Comparing %f %s %f\n", lhs, opname[op], rhs);
540 
541 	switch (op) {
542 	case LT:
543 		return lhs < rhs;
544 	case LE:
545 		return lhs <= rhs;
546 	case GT:
547 		return lhs > rhs;
548 	case GE:
549 		return lhs >= rhs;
550 	case EQ:
551 		return lhs == rhs;
552 	default:
553 		return lhs != rhs;
554 	}
555 }
556 
557 static Token
558 EvalCompareStr(CondParser *par, const char *lhs,
559 	       ComparisonOp op, const char *rhs)
560 {
561 	if (op != EQ && op != NE) {
562 		Parse_Error(PARSE_FATAL,
563 		    "Comparison with '%s' requires both operands "
564 		    "'%s' and '%s' to be numeric",
565 		    opname[op], lhs, rhs);
566 		par->printedError = true;
567 		return TOK_ERROR;
568 	}
569 
570 	DEBUG3(COND, "Comparing \"%s\" %s \"%s\"\n", lhs, opname[op], rhs);
571 	return ToToken((op == EQ) == (strcmp(lhs, rhs) == 0));
572 }
573 
574 /* Evaluate a comparison, such as "${VAR} == 12345". */
575 static Token
576 EvalCompare(CondParser *par, const char *lhs, bool lhsQuoted,
577 	    ComparisonOp op, const char *rhs, bool rhsQuoted)
578 {
579 	double left, right;
580 
581 	if (!rhsQuoted && !lhsQuoted)
582 		if (TryParseNumber(lhs, &left) && TryParseNumber(rhs, &right))
583 			return ToToken(EvalCompareNum(left, op, right));
584 
585 	return EvalCompareStr(par, lhs, op, rhs);
586 }
587 
588 static bool
589 CondParser_ComparisonOp(CondParser *par, ComparisonOp *out_op)
590 {
591 	const char *p = par->p;
592 
593 	if (p[0] == '<' && p[1] == '=')
594 		return par->p += 2, *out_op = LE, true;
595 	if (p[0] == '<')
596 		return par->p += 1, *out_op = LT, true;
597 	if (p[0] == '>' && p[1] == '=')
598 		return par->p += 2, *out_op = GE, true;
599 	if (p[0] == '>')
600 		return par->p += 1, *out_op = GT, true;
601 	if (p[0] == '=' && p[1] == '=')
602 		return par->p += 2, *out_op = EQ, true;
603 	if (p[0] == '!' && p[1] == '=')
604 		return par->p += 2, *out_op = NE, true;
605 	return false;
606 }
607 
608 /*
609  * Parse a comparison condition such as:
610  *
611  *	0
612  *	${VAR:Mpattern}
613  *	${VAR} == value
614  *	${VAR:U0} < 12345
615  */
616 static Token
617 CondParser_Comparison(CondParser *par, bool doEval)
618 {
619 	Token t = TOK_ERROR;
620 	FStr lhs, rhs;
621 	ComparisonOp op;
622 	bool lhsQuoted, rhsQuoted;
623 
624 	CondParser_Leaf(par, doEval, par->leftUnquotedOK, &lhs, &lhsQuoted);
625 	if (lhs.str == NULL)
626 		goto done_lhs;
627 
628 	CondParser_SkipWhitespace(par);
629 
630 	if (!CondParser_ComparisonOp(par, &op)) {
631 		t = ToToken(doEval && EvalTruthy(par, lhs.str, lhsQuoted));
632 		goto done_lhs;
633 	}
634 
635 	CondParser_SkipWhitespace(par);
636 
637 	if (par->p[0] == '\0') {
638 		Parse_Error(PARSE_FATAL,
639 		    "Missing right-hand side of operator '%s'", opname[op]);
640 		par->printedError = true;
641 		goto done_lhs;
642 	}
643 
644 	CondParser_Leaf(par, doEval, true, &rhs, &rhsQuoted);
645 	t = rhs.str == NULL ? TOK_ERROR
646 	    : !doEval ? TOK_FALSE
647 	    : EvalCompare(par, lhs.str, lhsQuoted, op, rhs.str, rhsQuoted);
648 	FStr_Done(&rhs);
649 
650 done_lhs:
651 	FStr_Done(&lhs);
652 	return t;
653 }
654 
655 /*
656  * The argument to empty() is a variable name, optionally followed by
657  * variable modifiers.
658  */
659 static bool
660 CondParser_FuncCallEmpty(CondParser *par, bool doEval, Token *out_token)
661 {
662 	const char *p = par->p;
663 	Token tok;
664 	FStr val;
665 
666 	if (!skip_string(&p, "empty"))
667 		return false;
668 
669 	cpp_skip_whitespace(&p);
670 	if (*p != '(')
671 		return false;
672 
673 	p--;			/* Make p[1] point to the '('. */
674 	val = Var_Parse(&p, SCOPE_CMDLINE,
675 	    doEval ? VARE_WANTRES : VARE_PARSE_ONLY);
676 	/* TODO: handle errors */
677 
678 	if (val.str == var_Error)
679 		tok = TOK_ERROR;
680 	else {
681 		cpp_skip_whitespace(&val.str);
682 		tok = ToToken(doEval && val.str[0] == '\0');
683 	}
684 
685 	FStr_Done(&val);
686 	*out_token = tok;
687 	par->p = p;
688 	return true;
689 }
690 
691 /* Parse a function call expression, such as 'exists(${file})'. */
692 static bool
693 CondParser_FuncCall(CondParser *par, bool doEval, Token *out_token)
694 {
695 	char *arg;
696 	const char *p = par->p;
697 	bool (*fn)(const char *);
698 	const char *fn_name = p;
699 
700 	if (skip_string(&p, "defined"))
701 		fn = FuncDefined;
702 	else if (skip_string(&p, "make"))
703 		fn = FuncMake;
704 	else if (skip_string(&p, "exists"))
705 		fn = FuncExists;
706 	else if (skip_string(&p, "target"))
707 		fn = FuncTarget;
708 	else if (skip_string(&p, "commands"))
709 		fn = FuncCommands;
710 	else
711 		return false;
712 
713 	cpp_skip_whitespace(&p);
714 	if (*p != '(')
715 		return false;
716 
717 	arg = ParseFuncArg(par, &p, doEval, fn_name);
718 	*out_token = ToToken(doEval &&
719 	    arg != NULL && arg[0] != '\0' && fn(arg));
720 	free(arg);
721 
722 	par->p = p;
723 	return true;
724 }
725 
726 /*
727  * Parse a comparison that neither starts with '"' nor '$', such as the
728  * unusual 'bare == right' or '3 == ${VAR}', or a simple leaf without
729  * operator, which is a number, an expression or a string literal.
730  *
731  * TODO: Can this be merged into CondParser_Comparison?
732  */
733 static Token
734 CondParser_ComparisonOrLeaf(CondParser *par, bool doEval)
735 {
736 	Token t;
737 	char *arg;
738 	const char *p;
739 
740 	p = par->p;
741 	if (ch_isdigit(p[0]) || p[0] == '-' || p[0] == '+')
742 		return CondParser_Comparison(par, doEval);
743 
744 	/*
745 	 * Most likely we have a bare word to apply the default function to.
746 	 * However, ".if a == b" gets here when the "a" is unquoted and
747 	 * doesn't start with a '$'. This surprises people.
748 	 * If what follows the function argument is a '=' or '!' then the
749 	 * syntax would be invalid if we did "defined(a)" - so instead treat
750 	 * as an expression.
751 	 */
752 	/*
753 	 * XXX: In edge cases, an expression may be evaluated twice,
754 	 *  see cond-token-plain.mk, keyword 'twice'.
755 	 */
756 	arg = ParseWord(&p, doEval);
757 	assert(arg[0] != '\0');
758 
759 	if (*p == '=' || *p == '!' || *p == '<' || *p == '>')
760 		return CondParser_Comparison(par, doEval);
761 	par->p = p;
762 
763 	/*
764 	 * Evaluate the argument using the default function.
765 	 * This path always treats .if as .ifdef. To get here, the character
766 	 * after .if must have been taken literally, so the argument cannot
767 	 * be empty - even if it contained an expression.
768 	 */
769 	t = ToToken(doEval && par->evalBare(arg) != par->negateEvalBare);
770 	free(arg);
771 	return t;
772 }
773 
774 /* Return the next token or comparison result from the parser. */
775 static Token
776 CondParser_Token(CondParser *par, bool doEval)
777 {
778 	Token t;
779 
780 	t = par->curr;
781 	if (t != TOK_NONE) {
782 		par->curr = TOK_NONE;
783 		return t;
784 	}
785 
786 	cpp_skip_hspace(&par->p);
787 
788 	switch (par->p[0]) {
789 
790 	case '(':
791 		par->p++;
792 		return TOK_LPAREN;
793 
794 	case ')':
795 		par->p++;
796 		return TOK_RPAREN;
797 
798 	case '|':
799 		par->p++;
800 		if (par->p[0] == '|')
801 			par->p++;
802 		else if (opts.strict) {
803 			Parse_Error(PARSE_FATAL, "Unknown operator '|'");
804 			par->printedError = true;
805 			return TOK_ERROR;
806 		}
807 		return TOK_OR;
808 
809 	case '&':
810 		par->p++;
811 		if (par->p[0] == '&')
812 			par->p++;
813 		else if (opts.strict) {
814 			Parse_Error(PARSE_FATAL, "Unknown operator '&'");
815 			par->printedError = true;
816 			return TOK_ERROR;
817 		}
818 		return TOK_AND;
819 
820 	case '!':
821 		par->p++;
822 		return TOK_NOT;
823 
824 	case '#':		/* XXX: see unit-tests/cond-token-plain.mk */
825 	case '\n':		/* XXX: why should this end the condition? */
826 		/* Probably obsolete now, from 1993-03-21. */
827 	case '\0':
828 		return TOK_EOF;
829 
830 	case '"':
831 	case '$':
832 		return CondParser_Comparison(par, doEval);
833 
834 	default:
835 		if (CondParser_FuncCallEmpty(par, doEval, &t))
836 			return t;
837 		if (CondParser_FuncCall(par, doEval, &t))
838 			return t;
839 		return CondParser_ComparisonOrLeaf(par, doEval);
840 	}
841 }
842 
843 /* Skip the next token if it equals t. */
844 static bool
845 CondParser_Skip(CondParser *par, Token t)
846 {
847 	Token actual;
848 
849 	actual = CondParser_Token(par, false);
850 	if (actual == t)
851 		return true;
852 
853 	assert(par->curr == TOK_NONE);
854 	assert(actual != TOK_NONE);
855 	par->curr = actual;
856 	return false;
857 }
858 
859 /*
860  * Term -> '(' Or ')'
861  * Term -> '!' Term
862  * Term -> Leaf Operator Leaf
863  * Term -> Leaf
864  */
865 static CondResult
866 CondParser_Term(CondParser *par, bool doEval)
867 {
868 	CondResult res;
869 	Token t;
870 
871 	t = CondParser_Token(par, doEval);
872 	if (t == TOK_TRUE)
873 		return CR_TRUE;
874 	if (t == TOK_FALSE)
875 		return CR_FALSE;
876 
877 	if (t == TOK_LPAREN) {
878 		res = CondParser_Or(par, doEval);
879 		if (res == CR_ERROR)
880 			return CR_ERROR;
881 		if (CondParser_Token(par, doEval) != TOK_RPAREN)
882 			return CR_ERROR;
883 		return res;
884 	}
885 
886 	if (t == TOK_NOT) {
887 		res = CondParser_Term(par, doEval);
888 		if (res == CR_TRUE)
889 			res = CR_FALSE;
890 		else if (res == CR_FALSE)
891 			res = CR_TRUE;
892 		return res;
893 	}
894 
895 	return CR_ERROR;
896 }
897 
898 /*
899  * And -> Term ('&&' Term)*
900  */
901 static CondResult
902 CondParser_And(CondParser *par, bool doEval)
903 {
904 	CondResult res, rhs;
905 
906 	res = CR_TRUE;
907 	do {
908 		if ((rhs = CondParser_Term(par, doEval)) == CR_ERROR)
909 			return CR_ERROR;
910 		if (rhs == CR_FALSE) {
911 			res = CR_FALSE;
912 			doEval = false;
913 		}
914 	} while (CondParser_Skip(par, TOK_AND));
915 
916 	return res;
917 }
918 
919 /*
920  * Or -> And ('||' And)*
921  */
922 static CondResult
923 CondParser_Or(CondParser *par, bool doEval)
924 {
925 	CondResult res, rhs;
926 
927 	res = CR_FALSE;
928 	do {
929 		if ((rhs = CondParser_And(par, doEval)) == CR_ERROR)
930 			return CR_ERROR;
931 		if (rhs == CR_TRUE) {
932 			res = CR_TRUE;
933 			doEval = false;
934 		}
935 	} while (CondParser_Skip(par, TOK_OR));
936 
937 	return res;
938 }
939 
940 static CondResult
941 CondParser_Eval(CondParser *par)
942 {
943 	CondResult res;
944 
945 	DEBUG1(COND, "CondParser_Eval: %s\n", par->p);
946 
947 	res = CondParser_Or(par, true);
948 	if (res != CR_ERROR && CondParser_Token(par, false) != TOK_EOF)
949 		return CR_ERROR;
950 
951 	return res;
952 }
953 
954 /*
955  * Evaluate the condition, including any side effects from the
956  * expressions in the condition. The condition consists of &&, ||, !,
957  * function(arg), comparisons and parenthetical groupings thereof.
958  */
959 static CondResult
960 CondEvalExpression(const char *cond, bool plain,
961 		   bool (*evalBare)(const char *), bool negate,
962 		   bool eprint, bool leftUnquotedOK)
963 {
964 	CondParser par;
965 	CondResult rval;
966 
967 	cpp_skip_hspace(&cond);
968 
969 	par.plain = plain;
970 	par.evalBare = evalBare;
971 	par.negateEvalBare = negate;
972 	par.leftUnquotedOK = leftUnquotedOK;
973 	par.p = cond;
974 	par.curr = TOK_NONE;
975 	par.printedError = false;
976 
977 	rval = CondParser_Eval(&par);
978 
979 	if (rval == CR_ERROR && eprint && !par.printedError)
980 		Parse_Error(PARSE_FATAL, "Malformed conditional (%s)", cond);
981 
982 	return rval;
983 }
984 
985 /*
986  * Evaluate a condition in a :? modifier, such as
987  * ${"${VAR}" == value:?yes:no}.
988  */
989 CondResult
990 Cond_EvalCondition(const char *cond)
991 {
992 	return CondEvalExpression(cond, true,
993 	    FuncDefined, false, false, true);
994 }
995 
996 static bool
997 IsEndif(const char *p)
998 {
999 	return p[0] == 'e' && p[1] == 'n' && p[2] == 'd' &&
1000 	       p[3] == 'i' && p[4] == 'f' && !ch_isalpha(p[5]);
1001 }
1002 
1003 static bool
1004 DetermineKindOfConditional(const char **pp, bool *out_plain,
1005 			   bool (**out_evalBare)(const char *),
1006 			   bool *out_negate)
1007 {
1008 	const char *p = *pp + 2;
1009 
1010 	*out_plain = false;
1011 	*out_evalBare = FuncDefined;
1012 	*out_negate = skip_string(&p, "n");
1013 
1014 	if (skip_string(&p, "def")) {		/* .ifdef and .ifndef */
1015 	} else if (skip_string(&p, "make"))	/* .ifmake and .ifnmake */
1016 		*out_evalBare = FuncMake;
1017 	else if (!*out_negate)			/* plain .if */
1018 		*out_plain = true;
1019 	else
1020 		goto unknown_directive;
1021 	if (ch_isalpha(*p))
1022 		goto unknown_directive;
1023 
1024 	*pp = p;
1025 	return true;
1026 
1027 unknown_directive:
1028 	return false;
1029 }
1030 
1031 /*
1032  * Evaluate the conditional directive in the line, which is one of:
1033  *
1034  *	.if <cond>
1035  *	.ifmake <cond>
1036  *	.ifnmake <cond>
1037  *	.ifdef <cond>
1038  *	.ifndef <cond>
1039  *	.elif <cond>
1040  *	.elifmake <cond>
1041  *	.elifnmake <cond>
1042  *	.elifdef <cond>
1043  *	.elifndef <cond>
1044  *	.else
1045  *	.endif
1046  *
1047  * In these directives, <cond> consists of &&, ||, !, function(arg),
1048  * comparisons, expressions, bare words, numbers and strings, and
1049  * parenthetical groupings thereof.
1050  *
1051  * Results:
1052  *	CR_TRUE		to continue parsing the lines that follow the
1053  *			conditional (when <cond> evaluates to true)
1054  *	CR_FALSE	to skip the lines after the conditional
1055  *			(when <cond> evaluates to false, or when a previous
1056  *			branch was already taken)
1057  *	CR_ERROR	if the conditional was not valid, either because of
1058  *			a syntax error or because some variable was undefined
1059  *			or because the condition could not be evaluated
1060  */
1061 CondResult
1062 Cond_EvalLine(const char *line)
1063 {
1064 	typedef enum IfState {
1065 
1066 		/* None of the previous <cond> evaluated to true. */
1067 		IFS_INITIAL	= 0,
1068 
1069 		/*
1070 		 * The previous <cond> evaluated to true. The lines following
1071 		 * this condition are interpreted.
1072 		 */
1073 		IFS_ACTIVE	= 1 << 0,
1074 
1075 		/* The previous directive was an '.else'. */
1076 		IFS_SEEN_ELSE	= 1 << 1,
1077 
1078 		/* One of the previous <cond> evaluated to true. */
1079 		IFS_WAS_ACTIVE	= 1 << 2
1080 
1081 	} IfState;
1082 
1083 	static enum IfState *cond_states = NULL;
1084 	static unsigned int cond_states_cap = 128;
1085 
1086 	bool plain;
1087 	bool (*evalBare)(const char *);
1088 	bool negate;
1089 	bool isElif;
1090 	CondResult res;
1091 	IfState state;
1092 	const char *p = line;
1093 
1094 	if (cond_states == NULL) {
1095 		cond_states = bmake_malloc(
1096 		    cond_states_cap * sizeof *cond_states);
1097 		cond_states[0] = IFS_ACTIVE;
1098 	}
1099 
1100 	p++;			/* skip the leading '.' */
1101 	cpp_skip_hspace(&p);
1102 
1103 	if (IsEndif(p)) {
1104 		if (p[5] != '\0') {
1105 			Parse_Error(PARSE_FATAL,
1106 			    "The .endif directive does not take arguments");
1107 		}
1108 
1109 		if (cond_depth == CurFile_CondMinDepth()) {
1110 			Parse_Error(PARSE_FATAL, "if-less endif");
1111 			return CR_TRUE;
1112 		}
1113 
1114 		/* Return state for previous conditional */
1115 		cond_depth--;
1116 		Parse_GuardEndif();
1117 		return cond_states[cond_depth] & IFS_ACTIVE
1118 		    ? CR_TRUE : CR_FALSE;
1119 	}
1120 
1121 	/* Parse the name of the directive, such as 'if', 'elif', 'endif'. */
1122 	if (p[0] == 'e') {
1123 		if (p[1] != 'l')
1124 			return CR_ERROR;
1125 
1126 		/* Quite likely this is 'else' or 'elif' */
1127 		p += 2;
1128 		if (strncmp(p, "se", 2) == 0 && !ch_isalpha(p[2])) {
1129 			if (p[2] != '\0')
1130 				Parse_Error(PARSE_FATAL,
1131 				    "The .else directive "
1132 				    "does not take arguments");
1133 
1134 			if (cond_depth == CurFile_CondMinDepth()) {
1135 				Parse_Error(PARSE_FATAL, "if-less else");
1136 				return CR_TRUE;
1137 			}
1138 			Parse_GuardElse();
1139 
1140 			state = cond_states[cond_depth];
1141 			if (state == IFS_INITIAL) {
1142 				state = IFS_ACTIVE | IFS_SEEN_ELSE;
1143 			} else {
1144 				if (state & IFS_SEEN_ELSE)
1145 					Parse_Error(PARSE_WARNING,
1146 					    "extra else");
1147 				state = IFS_WAS_ACTIVE | IFS_SEEN_ELSE;
1148 			}
1149 			cond_states[cond_depth] = state;
1150 
1151 			return state & IFS_ACTIVE ? CR_TRUE : CR_FALSE;
1152 		}
1153 		/* Assume for now it is an elif */
1154 		isElif = true;
1155 	} else
1156 		isElif = false;
1157 
1158 	if (p[0] != 'i' || p[1] != 'f')
1159 		return CR_ERROR;
1160 
1161 	if (!DetermineKindOfConditional(&p, &plain, &evalBare, &negate))
1162 		return CR_ERROR;
1163 
1164 	if (isElif) {
1165 		if (cond_depth == CurFile_CondMinDepth()) {
1166 			Parse_Error(PARSE_FATAL, "if-less elif");
1167 			return CR_TRUE;
1168 		}
1169 		Parse_GuardElse();
1170 		state = cond_states[cond_depth];
1171 		if (state & IFS_SEEN_ELSE) {
1172 			Parse_Error(PARSE_WARNING, "extra elif");
1173 			cond_states[cond_depth] =
1174 			    IFS_WAS_ACTIVE | IFS_SEEN_ELSE;
1175 			return CR_FALSE;
1176 		}
1177 		if (state != IFS_INITIAL) {
1178 			cond_states[cond_depth] = IFS_WAS_ACTIVE;
1179 			return CR_FALSE;
1180 		}
1181 	} else {
1182 		/* Normal .if */
1183 		if (cond_depth + 1 >= cond_states_cap) {
1184 			/*
1185 			 * This is rare, but not impossible.
1186 			 * In meta mode, dirdeps.mk (only runs at level 0)
1187 			 * can need more than the default.
1188 			 */
1189 			cond_states_cap += 32;
1190 			cond_states = bmake_realloc(cond_states,
1191 			    cond_states_cap * sizeof *cond_states);
1192 		}
1193 		state = cond_states[cond_depth];
1194 		cond_depth++;
1195 		if (!(state & IFS_ACTIVE)) {
1196 			cond_states[cond_depth] = IFS_WAS_ACTIVE;
1197 			return CR_FALSE;
1198 		}
1199 	}
1200 
1201 	res = CondEvalExpression(p, plain, evalBare, negate, true, false);
1202 	if (res == CR_ERROR) {
1203 		/* Syntax error, error message already output. */
1204 		/* Skip everything to the matching '.endif'. */
1205 		/* An extra '.else' is not detected in this case. */
1206 		cond_states[cond_depth] = IFS_WAS_ACTIVE;
1207 		return CR_FALSE;
1208 	}
1209 
1210 	cond_states[cond_depth] = res == CR_TRUE ? IFS_ACTIVE : IFS_INITIAL;
1211 	return res;
1212 }
1213 
1214 static bool
1215 ParseVarnameGuard(const char **pp, const char **varname)
1216 {
1217 	const char *p = *pp;
1218 
1219 	if (ch_isalpha(*p) || *p == '_') {
1220 		while (ch_isalnum(*p) || *p == '_')
1221 			p++;
1222 		*varname = *pp;
1223 		*pp = p;
1224 		return true;
1225 	}
1226 	return false;
1227 }
1228 
1229 /* Extracts the multiple-inclusion guard from a conditional, if any. */
1230 Guard *
1231 Cond_ExtractGuard(const char *line)
1232 {
1233 	const char *p, *varname;
1234 	Substring dir;
1235 	Guard *guard;
1236 
1237 	p = line + 1;		/* skip the '.' */
1238 	cpp_skip_hspace(&p);
1239 
1240 	dir.start = p;
1241 	while (ch_isalpha(*p))
1242 		p++;
1243 	dir.end = p;
1244 	cpp_skip_hspace(&p);
1245 
1246 	if (Substring_Equals(dir, "if")) {
1247 		if (skip_string(&p, "!defined(")) {
1248 			if (ParseVarnameGuard(&p, &varname)
1249 			    && strcmp(p, ")") == 0)
1250 				goto found_variable;
1251 		} else if (skip_string(&p, "!target(")) {
1252 			const char *arg_p = p;
1253 			free(ParseWord(&p, false));
1254 			if (strcmp(p, ")") == 0) {
1255 				guard = bmake_malloc(sizeof(*guard));
1256 				guard->kind = GK_TARGET;
1257 				guard->name = ParseWord(&arg_p, true);
1258 				return guard;
1259 			}
1260 		}
1261 	} else if (Substring_Equals(dir, "ifndef")) {
1262 		if (ParseVarnameGuard(&p, &varname) && *p == '\0')
1263 			goto found_variable;
1264 	}
1265 	return NULL;
1266 
1267 found_variable:
1268 	guard = bmake_malloc(sizeof(*guard));
1269 	guard->kind = GK_VARIABLE;
1270 	guard->name = bmake_strsedup(varname, p);
1271 	return guard;
1272 }
1273 
1274 void
1275 Cond_EndFile(void)
1276 {
1277 	unsigned int open_conds = cond_depth - CurFile_CondMinDepth();
1278 
1279 	if (open_conds != 0) {
1280 		Parse_Error(PARSE_FATAL, "%u open conditional%s",
1281 		    open_conds, open_conds == 1 ? "" : "s");
1282 		cond_depth = CurFile_CondMinDepth();
1283 	}
1284 }
1285