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