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