xref: /freebsd/contrib/bmake/cond.c (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1 /*	$NetBSD: cond.c,v 1.267 2021/06/11 14:52:03 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_save_depth
85  *	Cond_restore_depth
86  *			Save and restore the nesting of the conditions, at
87  *			the start and end of including another makefile, to
88  *			ensure that in each makefile the conditional
89  *			directives are well-balanced.
90  */
91 
92 #include <errno.h>
93 
94 #include "make.h"
95 #include "dir.h"
96 
97 /*	"@(#)cond.c	8.2 (Berkeley) 1/2/94"	*/
98 MAKE_RCSID("$NetBSD: cond.c,v 1.267 2021/06/11 14:52:03 rillig Exp $");
99 
100 /*
101  * The parsing of conditional expressions is based on this grammar:
102  *	Or -> And '||' Or
103  *	Or -> And
104  *	And -> Term '&&' And
105  *	And -> Term
106  *	Term -> Function '(' Argument ')'
107  *	Term -> Leaf Operator Leaf
108  *	Term -> Leaf
109  *	Term -> '(' Or ')'
110  *	Term -> '!' Term
111  *	Leaf -> "string"
112  *	Leaf -> Number
113  *	Leaf -> VariableExpression
114  *	Leaf -> Symbol
115  *	Operator -> '==' | '!=' | '>' | '<' | '>=' | '<='
116  *
117  * 'Symbol' is an unquoted string literal to which the default function is
118  * applied.
119  *
120  * The tokens are scanned by CondToken, which returns:
121  *	TOK_AND		for '&&'
122  *	TOK_OR		for '||'
123  *	TOK_NOT		for '!'
124  *	TOK_LPAREN	for '('
125  *	TOK_RPAREN	for ')'
126  *
127  * Other terminal symbols are evaluated using either the default function or
128  * the function given in the terminal, they return either TOK_TRUE or
129  * TOK_FALSE.
130  */
131 typedef enum Token {
132 	TOK_FALSE, TOK_TRUE, TOK_AND, TOK_OR, TOK_NOT,
133 	TOK_LPAREN, TOK_RPAREN, TOK_EOF, TOK_NONE, TOK_ERROR
134 } Token;
135 
136 typedef enum CondResult {
137 	CR_FALSE, CR_TRUE, CR_ERROR
138 } CondResult;
139 
140 typedef enum ComparisonOp {
141 	LT, LE, GT, GE, EQ, NE
142 } ComparisonOp;
143 
144 typedef struct CondParser {
145 
146 	/*
147 	 * The plain '.if ${VAR}' evaluates to true if the value of the
148 	 * expression has length > 0.  The other '.if' variants delegate
149 	 * to evalBare instead.
150 	 */
151 	bool plain;
152 
153 	/* The function to apply on unquoted bare words. */
154 	bool (*evalBare)(size_t, const char *);
155 	bool negateEvalBare;
156 
157 	const char *p;		/* The remaining condition to parse */
158 	Token curr;		/* Single push-back token used in parsing */
159 
160 	/*
161 	 * Whether an error message has already been printed for this
162 	 * condition. The first available error message is usually the most
163 	 * specific one, therefore it makes sense to suppress the standard
164 	 * "Malformed conditional" message.
165 	 */
166 	bool printedError;
167 } CondParser;
168 
169 static CondResult CondParser_Or(CondParser *par, bool);
170 
171 static unsigned int cond_depth = 0;	/* current .if nesting level */
172 static unsigned int cond_min_depth = 0;	/* depth at makefile open */
173 
174 static const char *opname[] = { "<", "<=", ">", ">=", "==", "!=" };
175 
176 /*
177  * Indicate when we should be strict about lhs of comparisons.
178  * In strict mode, the lhs must be a variable expression or a string literal
179  * in quotes. In non-strict mode it may also be an unquoted string literal.
180  *
181  * True when CondEvalExpression is called from Cond_EvalLine (.if etc).
182  * False when CondEvalExpression is called from ApplyModifier_IfElse
183  * since lhs is already expanded, and at that point we cannot tell if
184  * it was a variable reference or not.
185  */
186 static bool lhsStrict;
187 
188 static bool
189 is_token(const char *str, const char *tok, size_t len)
190 {
191 	return strncmp(str, tok, len) == 0 && !ch_isalpha(str[len]);
192 }
193 
194 static Token
195 ToToken(bool cond)
196 {
197 	return cond ? TOK_TRUE : TOK_FALSE;
198 }
199 
200 /* Push back the most recent token read. We only need one level of this. */
201 static void
202 CondParser_PushBack(CondParser *par, Token t)
203 {
204 	assert(par->curr == TOK_NONE);
205 	assert(t != TOK_NONE);
206 
207 	par->curr = t;
208 }
209 
210 static void
211 CondParser_SkipWhitespace(CondParser *par)
212 {
213 	cpp_skip_whitespace(&par->p);
214 }
215 
216 /*
217  * Parse the argument of a built-in function.
218  *
219  * Arguments:
220  *	*pp initially points at the '(',
221  *	upon successful return it points right after the ')'.
222  *
223  *	*out_arg receives the argument as string.
224  *
225  *	func says whether the argument belongs to an actual function, or
226  *	whether the parsed argument is passed to the default function.
227  *
228  * Return the length of the argument, or 0 on error.
229  */
230 static size_t
231 ParseFuncArg(CondParser *par, const char **pp, bool doEval, const char *func,
232 	     char **out_arg)
233 {
234 	const char *p = *pp;
235 	Buffer argBuf;
236 	int paren_depth;
237 	size_t argLen;
238 
239 	if (func != NULL)
240 		p++;		/* Skip opening '(' - verified by caller */
241 
242 	if (*p == '\0') {
243 		*out_arg = NULL; /* Missing closing parenthesis: */
244 		return 0;	/* .if defined( */
245 	}
246 
247 	cpp_skip_hspace(&p);
248 
249 	Buf_InitSize(&argBuf, 16);
250 
251 	paren_depth = 0;
252 	for (;;) {
253 		char ch = *p;
254 		if (ch == '\0' || ch == ' ' || ch == '\t')
255 			break;
256 		if ((ch == '&' || ch == '|') && paren_depth == 0)
257 			break;
258 		if (*p == '$') {
259 			/*
260 			 * Parse the variable expression and install it as
261 			 * part of the argument if it's valid. We tell
262 			 * Var_Parse to complain on an undefined variable,
263 			 * (XXX: but Var_Parse ignores that request)
264 			 * so we don't need to do it. Nor do we return an
265 			 * error, though perhaps we should.
266 			 */
267 			VarEvalMode emode = doEval
268 			    ? VARE_UNDEFERR
269 			    : VARE_PARSE_ONLY;
270 			FStr nestedVal;
271 			(void)Var_Parse(&p, SCOPE_CMDLINE, emode, &nestedVal);
272 			/* TODO: handle errors */
273 			Buf_AddStr(&argBuf, nestedVal.str);
274 			FStr_Done(&nestedVal);
275 			continue;
276 		}
277 		if (ch == '(')
278 			paren_depth++;
279 		else if (ch == ')' && --paren_depth < 0)
280 			break;
281 		Buf_AddByte(&argBuf, *p);
282 		p++;
283 	}
284 
285 	argLen = argBuf.len;
286 	*out_arg = Buf_DoneData(&argBuf);
287 
288 	cpp_skip_hspace(&p);
289 
290 	if (func != NULL && *p++ != ')') {
291 		Parse_Error(PARSE_FATAL,
292 		    "Missing closing parenthesis for %s()", func);
293 		par->printedError = true;
294 		return 0;
295 	}
296 
297 	*pp = p;
298 	return argLen;
299 }
300 
301 /* Test whether the given variable is defined. */
302 /*ARGSUSED*/
303 static bool
304 FuncDefined(size_t argLen MAKE_ATTR_UNUSED, const char *arg)
305 {
306 	FStr value = Var_Value(SCOPE_CMDLINE, arg);
307 	bool result = value.str != NULL;
308 	FStr_Done(&value);
309 	return result;
310 }
311 
312 /* See if the given target is being made. */
313 /*ARGSUSED*/
314 static bool
315 FuncMake(size_t argLen MAKE_ATTR_UNUSED, const char *arg)
316 {
317 	StringListNode *ln;
318 
319 	for (ln = opts.create.first; ln != NULL; ln = ln->next)
320 		if (Str_Match(ln->datum, arg))
321 			return true;
322 	return false;
323 }
324 
325 /* See if the given file exists. */
326 /*ARGSUSED*/
327 static bool
328 FuncExists(size_t argLen MAKE_ATTR_UNUSED, const char *arg)
329 {
330 	bool result;
331 	char *path;
332 
333 	path = Dir_FindFile(arg, &dirSearchPath);
334 	DEBUG2(COND, "exists(%s) result is \"%s\"\n",
335 	       arg, path != NULL ? path : "");
336 	result = path != NULL;
337 	free(path);
338 	return result;
339 }
340 
341 /* See if the given node exists and is an actual target. */
342 /*ARGSUSED*/
343 static bool
344 FuncTarget(size_t argLen MAKE_ATTR_UNUSED, const char *arg)
345 {
346 	GNode *gn = Targ_FindNode(arg);
347 	return gn != NULL && GNode_IsTarget(gn);
348 }
349 
350 /*
351  * See if the given node exists and is an actual target with commands
352  * associated with it.
353  */
354 /*ARGSUSED*/
355 static bool
356 FuncCommands(size_t argLen MAKE_ATTR_UNUSED, const char *arg)
357 {
358 	GNode *gn = Targ_FindNode(arg);
359 	return gn != NULL && GNode_IsTarget(gn) && !Lst_IsEmpty(&gn->commands);
360 }
361 
362 /*
363  * Convert the given number into a double.
364  * We try a base 10 or 16 integer conversion first, if that fails
365  * then we try a floating point conversion instead.
366  *
367  * Results:
368  *	Returns true if the conversion succeeded.
369  *	Sets 'out_value' to the converted number.
370  */
371 static bool
372 TryParseNumber(const char *str, double *out_value)
373 {
374 	char *end;
375 	unsigned long ul_val;
376 	double dbl_val;
377 
378 	errno = 0;
379 	if (str[0] == '\0') {	/* XXX: why is an empty string a number? */
380 		*out_value = 0.0;
381 		return true;
382 	}
383 
384 	ul_val = strtoul(str, &end, str[1] == 'x' ? 16 : 10);
385 	if (*end == '\0' && errno != ERANGE) {
386 		*out_value = str[0] == '-' ? -(double)-ul_val : (double)ul_val;
387 		return true;
388 	}
389 
390 	if (*end != '\0' && *end != '.' && *end != 'e' && *end != 'E')
391 		return false;	/* skip the expensive strtod call */
392 	dbl_val = strtod(str, &end);
393 	if (*end != '\0')
394 		return false;
395 
396 	*out_value = dbl_val;
397 	return true;
398 }
399 
400 static bool
401 is_separator(char ch)
402 {
403 	return ch == '\0' || ch_isspace(ch) || ch == '!' || ch == '=' ||
404 	       ch == '>' || ch == '<' || ch == ')' /* but not '(' */;
405 }
406 
407 /*
408  * In a quoted or unquoted string literal or a number, parse a variable
409  * expression.
410  *
411  * Example: .if x${CENTER}y == "${PREFIX}${SUFFIX}" || 0x${HEX}
412  */
413 static bool
414 CondParser_StringExpr(CondParser *par, const char *start,
415 		      bool const doEval, bool const quoted,
416 		      Buffer *buf, FStr *const inout_str)
417 {
418 	VarEvalMode emode;
419 	const char *nested_p;
420 	bool atStart;
421 	VarParseResult parseResult;
422 
423 	/* if we are in quotes, an undefined variable is ok */
424 	emode = doEval && !quoted ? VARE_UNDEFERR
425 	    : doEval ? VARE_WANTRES
426 	    : VARE_PARSE_ONLY;
427 
428 	nested_p = par->p;
429 	atStart = nested_p == start;
430 	parseResult = Var_Parse(&nested_p, SCOPE_CMDLINE, emode, inout_str);
431 	/* TODO: handle errors */
432 	if (inout_str->str == var_Error) {
433 		if (parseResult == VPR_ERR) {
434 			/*
435 			 * FIXME: Even if an error occurs, there is no
436 			 *  guarantee that it is reported.
437 			 *
438 			 * See cond-token-plain.mk $$$$$$$$.
439 			 */
440 			par->printedError = true;
441 		}
442 		/*
443 		 * XXX: Can there be any situation in which a returned
444 		 * var_Error needs to be freed?
445 		 */
446 		FStr_Done(inout_str);
447 		/*
448 		 * Even if !doEval, we still report syntax errors, which is
449 		 * what getting var_Error back with !doEval means.
450 		 */
451 		*inout_str = FStr_InitRefer(NULL);
452 		return false;
453 	}
454 	par->p = nested_p;
455 
456 	/*
457 	 * If the '$' started the string literal (which means no quotes), and
458 	 * the variable expression is followed by a space, looks like a
459 	 * comparison operator or is the end of the expression, we are done.
460 	 */
461 	if (atStart && is_separator(par->p[0]))
462 		return false;
463 
464 	Buf_AddStr(buf, inout_str->str);
465 	FStr_Done(inout_str);
466 	*inout_str = FStr_InitRefer(NULL); /* not finished yet */
467 	return true;
468 }
469 
470 /*
471  * Parse a string from a variable expression or an optionally quoted
472  * string.  This is called for the left-hand and right-hand sides of
473  * comparisons.
474  *
475  * Results:
476  *	Returns the string, absent any quotes, or NULL on error.
477  *	Sets out_quoted if the leaf was a quoted string literal.
478  */
479 static void
480 CondParser_Leaf(CondParser *par, bool doEval, bool strictLHS,
481 		  FStr *out_str, bool *out_quoted)
482 {
483 	Buffer buf;
484 	FStr str;
485 	bool quoted;
486 	const char *start;
487 
488 	Buf_Init(&buf);
489 	str = FStr_InitRefer(NULL);
490 	*out_quoted = quoted = par->p[0] == '"';
491 	start = par->p;
492 	if (quoted)
493 		par->p++;
494 
495 	while (par->p[0] != '\0' && str.str == NULL) {
496 		switch (par->p[0]) {
497 		case '\\':
498 			par->p++;
499 			if (par->p[0] != '\0') {
500 				Buf_AddByte(&buf, par->p[0]);
501 				par->p++;
502 			}
503 			continue;
504 		case '"':
505 			par->p++;
506 			if (quoted)
507 				goto got_str;	/* skip the closing quote */
508 			Buf_AddByte(&buf, '"');
509 			continue;
510 		case ')':	/* see is_separator */
511 		case '!':
512 		case '=':
513 		case '>':
514 		case '<':
515 		case ' ':
516 		case '\t':
517 			if (!quoted)
518 				goto got_str;
519 			Buf_AddByte(&buf, par->p[0]);
520 			par->p++;
521 			continue;
522 		case '$':
523 			if (!CondParser_StringExpr(par,
524 			    start, doEval, quoted, &buf, &str))
525 				goto cleanup;
526 			continue;
527 		default:
528 			if (strictLHS && !quoted && *start != '$' &&
529 			    !ch_isdigit(*start)) {
530 				/*
531 				 * The left-hand side must be quoted,
532 				 * a variable reference or a number.
533 				 */
534 				str = FStr_InitRefer(NULL);
535 				goto cleanup;
536 			}
537 			Buf_AddByte(&buf, par->p[0]);
538 			par->p++;
539 			continue;
540 		}
541 	}
542 got_str:
543 	str = FStr_InitOwn(buf.data);
544 cleanup:
545 	Buf_DoneData(&buf);	/* XXX: memory leak on failure? */
546 	*out_str = str;
547 }
548 
549 static bool
550 EvalBare(const CondParser *par, const char *arg, size_t arglen)
551 {
552 	bool res = par->evalBare(arglen, arg);
553 	return par->negateEvalBare ? !res : res;
554 }
555 
556 /*
557  * Evaluate a "comparison without operator", such as in ".if ${VAR}" or
558  * ".if 0".
559  */
560 static bool
561 EvalNotEmpty(CondParser *par, const char *value, bool quoted)
562 {
563 	double num;
564 
565 	/* For .ifxxx "...", check for non-empty string. */
566 	if (quoted)
567 		return value[0] != '\0';
568 
569 	/* For .ifxxx <number>, compare against zero */
570 	if (TryParseNumber(value, &num))
571 		return num != 0.0;
572 
573 	/* For .if ${...}, check for non-empty string.  This is different from
574 	 * the evaluation function from that .if variant, which would test
575 	 * whether a variable of the given name were defined. */
576 	/* XXX: Whitespace should count as empty, just as in ParseEmptyArg. */
577 	if (par->plain)
578 		return value[0] != '\0';
579 
580 	return EvalBare(par, value, strlen(value));
581 }
582 
583 /* Evaluate a numerical comparison, such as in ".if ${VAR} >= 9". */
584 static bool
585 EvalCompareNum(double lhs, ComparisonOp op, double rhs)
586 {
587 	DEBUG3(COND, "lhs = %f, rhs = %f, op = %.2s\n", lhs, rhs, opname[op]);
588 
589 	switch (op) {
590 	case LT:
591 		return lhs < rhs;
592 	case LE:
593 		return lhs <= rhs;
594 	case GT:
595 		return lhs > rhs;
596 	case GE:
597 		return lhs >= rhs;
598 	case NE:
599 		return lhs != rhs;
600 	default:
601 		return lhs == rhs;
602 	}
603 }
604 
605 static Token
606 EvalCompareStr(CondParser *par, const char *lhs,
607 	       ComparisonOp op, const char *rhs)
608 {
609 	if (op != EQ && op != NE) {
610 		Parse_Error(PARSE_FATAL,
611 		    "String comparison operator must be either == or !=");
612 		par->printedError = true;
613 		return TOK_ERROR;
614 	}
615 
616 	DEBUG3(COND, "lhs = \"%s\", rhs = \"%s\", op = %.2s\n",
617 	    lhs, rhs, opname[op]);
618 	return ToToken((op == EQ) == (strcmp(lhs, rhs) == 0));
619 }
620 
621 /* Evaluate a comparison, such as "${VAR} == 12345". */
622 static Token
623 EvalCompare(CondParser *par, const char *lhs, bool lhsQuoted,
624 	    ComparisonOp op, const char *rhs, bool rhsQuoted)
625 {
626 	double left, right;
627 
628 	if (!rhsQuoted && !lhsQuoted)
629 		if (TryParseNumber(lhs, &left) && TryParseNumber(rhs, &right))
630 			return ToToken(EvalCompareNum(left, op, right));
631 
632 	return EvalCompareStr(par, lhs, op, rhs);
633 }
634 
635 static bool
636 CondParser_ComparisonOp(CondParser *par, ComparisonOp *out_op)
637 {
638 	const char *p = par->p;
639 
640 	if (p[0] == '<' && p[1] == '=') {
641 		*out_op = LE;
642 		goto length_2;
643 	} else if (p[0] == '<') {
644 		*out_op = LT;
645 		goto length_1;
646 	} else if (p[0] == '>' && p[1] == '=') {
647 		*out_op = GE;
648 		goto length_2;
649 	} else if (p[0] == '>') {
650 		*out_op = GT;
651 		goto length_1;
652 	} else if (p[0] == '=' && p[1] == '=') {
653 		*out_op = EQ;
654 		goto length_2;
655 	} else if (p[0] == '!' && p[1] == '=') {
656 		*out_op = NE;
657 		goto length_2;
658 	}
659 	return false;
660 
661 length_2:
662 	par->p = p + 2;
663 	return true;
664 length_1:
665 	par->p = p + 1;
666 	return true;
667 }
668 
669 /*
670  * Parse a comparison condition such as:
671  *
672  *	0
673  *	${VAR:Mpattern}
674  *	${VAR} == value
675  *	${VAR:U0} < 12345
676  */
677 static Token
678 CondParser_Comparison(CondParser *par, bool doEval)
679 {
680 	Token t = TOK_ERROR;
681 	FStr lhs, rhs;
682 	ComparisonOp op;
683 	bool lhsQuoted, rhsQuoted;
684 
685 	/*
686 	 * Parse the variable spec and skip over it, saving its
687 	 * value in lhs.
688 	 */
689 	CondParser_Leaf(par, doEval, lhsStrict, &lhs, &lhsQuoted);
690 	if (lhs.str == NULL)
691 		goto done_lhs;
692 
693 	CondParser_SkipWhitespace(par);
694 
695 	if (!CondParser_ComparisonOp(par, &op)) {
696 		/* Unknown operator, compare against an empty string or 0. */
697 		t = ToToken(doEval && EvalNotEmpty(par, lhs.str, lhsQuoted));
698 		goto done_lhs;
699 	}
700 
701 	CondParser_SkipWhitespace(par);
702 
703 	if (par->p[0] == '\0') {
704 		Parse_Error(PARSE_FATAL,
705 		    "Missing right-hand-side of operator '%s'", opname[op]);
706 		par->printedError = true;
707 		goto done_lhs;
708 	}
709 
710 	CondParser_Leaf(par, doEval, false, &rhs, &rhsQuoted);
711 	if (rhs.str == NULL)
712 		goto done_rhs;
713 
714 	if (!doEval) {
715 		t = TOK_FALSE;
716 		goto done_rhs;
717 	}
718 
719 	t = EvalCompare(par, lhs.str, lhsQuoted, op, rhs.str, rhsQuoted);
720 
721 done_rhs:
722 	FStr_Done(&rhs);
723 done_lhs:
724 	FStr_Done(&lhs);
725 	return t;
726 }
727 
728 /*
729  * The argument to empty() is a variable name, optionally followed by
730  * variable modifiers.
731  */
732 /*ARGSUSED*/
733 static size_t
734 ParseEmptyArg(CondParser *par MAKE_ATTR_UNUSED, const char **pp,
735 	      bool doEval, const char *func MAKE_ATTR_UNUSED,
736 	      char **out_arg)
737 {
738 	FStr val;
739 	size_t magic_res;
740 
741 	/* We do all the work here and return the result as the length */
742 	*out_arg = NULL;
743 
744 	(*pp)--;		/* Make (*pp)[1] point to the '('. */
745 	(void)Var_Parse(pp, SCOPE_CMDLINE,
746 	    doEval ? VARE_WANTRES : VARE_PARSE_ONLY, &val);
747 	/* TODO: handle errors */
748 	/* If successful, *pp points beyond the closing ')' now. */
749 
750 	if (val.str == var_Error) {
751 		FStr_Done(&val);
752 		return (size_t)-1;
753 	}
754 
755 	/*
756 	 * A variable is empty when it just contains spaces...
757 	 * 4/15/92, christos
758 	 */
759 	cpp_skip_whitespace(&val.str);
760 
761 	/*
762 	 * For consistency with the other functions we can't generate the
763 	 * true/false here.
764 	 */
765 	magic_res = val.str[0] != '\0' ? 2 : 1;
766 	FStr_Done(&val);
767 	return magic_res;
768 }
769 
770 /*ARGSUSED*/
771 static bool
772 FuncEmpty(size_t arglen, const char *arg MAKE_ATTR_UNUSED)
773 {
774 	/* Magic values ahead, see ParseEmptyArg. */
775 	return arglen == 1;
776 }
777 
778 /* Parse a function call expression, such as 'defined(${file})'. */
779 static bool
780 CondParser_FuncCall(CondParser *par, bool doEval, Token *out_token)
781 {
782 	static const struct fn_def {
783 		const char *fn_name;
784 		size_t fn_name_len;
785 		size_t (*fn_parse)(CondParser *, const char **, bool,
786 				   const char *, char **);
787 		bool (*fn_eval)(size_t, const char *);
788 	} fns[] = {
789 		{ "defined",  7, ParseFuncArg,  FuncDefined },
790 		{ "make",     4, ParseFuncArg,  FuncMake },
791 		{ "exists",   6, ParseFuncArg,  FuncExists },
792 		{ "empty",    5, ParseEmptyArg, FuncEmpty },
793 		{ "target",   6, ParseFuncArg,  FuncTarget },
794 		{ "commands", 8, ParseFuncArg,  FuncCommands }
795 	};
796 	const struct fn_def *fn;
797 	char *arg = NULL;
798 	size_t arglen;
799 	const char *cp = par->p;
800 	const struct fn_def *fns_end = fns + sizeof fns / sizeof fns[0];
801 
802 	for (fn = fns; fn != fns_end; fn++) {
803 		if (!is_token(cp, fn->fn_name, fn->fn_name_len))
804 			continue;
805 
806 		cp += fn->fn_name_len;
807 		cpp_skip_whitespace(&cp);
808 		if (*cp != '(')
809 			break;
810 
811 		arglen = fn->fn_parse(par, &cp, doEval, fn->fn_name, &arg);
812 		if (arglen == 0 || arglen == (size_t)-1) {
813 			par->p = cp;
814 			*out_token = arglen == 0 ? TOK_FALSE : TOK_ERROR;
815 			return true;
816 		}
817 
818 		/* Evaluate the argument using the required function. */
819 		*out_token = ToToken(!doEval || fn->fn_eval(arglen, arg));
820 		free(arg);
821 		par->p = cp;
822 		return true;
823 	}
824 
825 	return false;
826 }
827 
828 /*
829  * Parse a comparison such as '${VAR} == "value"', or a simple leaf without
830  * operator, which is a number, a variable expression or a string literal.
831  */
832 static Token
833 CondParser_ComparisonOrLeaf(CondParser *par, bool doEval)
834 {
835 	Token t;
836 	char *arg = NULL;
837 	size_t arglen;
838 	const char *cp;
839 	const char *cp1;
840 
841 	/* Push anything numeric through the compare expression */
842 	cp = par->p;
843 	if (ch_isdigit(cp[0]) || cp[0] == '-' || cp[0] == '+')
844 		return CondParser_Comparison(par, doEval);
845 
846 	/*
847 	 * Most likely we have a naked token to apply the default function to.
848 	 * However ".if a == b" gets here when the "a" is unquoted and doesn't
849 	 * start with a '$'. This surprises people.
850 	 * If what follows the function argument is a '=' or '!' then the
851 	 * syntax would be invalid if we did "defined(a)" - so instead treat
852 	 * as an expression.
853 	 */
854 	/*
855 	 * XXX: Is it possible to have a variable expression evaluated twice
856 	 *  at this point?
857 	 */
858 	arglen = ParseFuncArg(par, &cp, doEval, NULL, &arg);
859 	cp1 = cp;
860 	cpp_skip_whitespace(&cp1);
861 	if (*cp1 == '=' || *cp1 == '!' || *cp1 == '<' || *cp1 == '>')
862 		return CondParser_Comparison(par, doEval);
863 	par->p = cp;
864 
865 	/*
866 	 * Evaluate the argument using the default function.
867 	 * This path always treats .if as .ifdef. To get here, the character
868 	 * after .if must have been taken literally, so the argument cannot
869 	 * be empty - even if it contained a variable expansion.
870 	 */
871 	t = ToToken(!doEval || EvalBare(par, arg, arglen));
872 	free(arg);
873 	return t;
874 }
875 
876 /* Return the next token or comparison result from the parser. */
877 static Token
878 CondParser_Token(CondParser *par, bool doEval)
879 {
880 	Token t;
881 
882 	t = par->curr;
883 	if (t != TOK_NONE) {
884 		par->curr = TOK_NONE;
885 		return t;
886 	}
887 
888 	cpp_skip_hspace(&par->p);
889 
890 	switch (par->p[0]) {
891 
892 	case '(':
893 		par->p++;
894 		return TOK_LPAREN;
895 
896 	case ')':
897 		par->p++;
898 		return TOK_RPAREN;
899 
900 	case '|':
901 		par->p++;
902 		if (par->p[0] == '|')
903 			par->p++;
904 		else if (opts.strict) {
905 			Parse_Error(PARSE_FATAL, "Unknown operator '|'");
906 			par->printedError = true;
907 			return TOK_ERROR;
908 		}
909 		return TOK_OR;
910 
911 	case '&':
912 		par->p++;
913 		if (par->p[0] == '&')
914 			par->p++;
915 		else if (opts.strict) {
916 			Parse_Error(PARSE_FATAL, "Unknown operator '&'");
917 			par->printedError = true;
918 			return TOK_ERROR;
919 		}
920 		return TOK_AND;
921 
922 	case '!':
923 		par->p++;
924 		return TOK_NOT;
925 
926 	case '#':		/* XXX: see unit-tests/cond-token-plain.mk */
927 	case '\n':		/* XXX: why should this end the condition? */
928 		/* Probably obsolete now, from 1993-03-21. */
929 	case '\0':
930 		return TOK_EOF;
931 
932 	case '"':
933 	case '$':
934 		return CondParser_Comparison(par, doEval);
935 
936 	default:
937 		if (CondParser_FuncCall(par, doEval, &t))
938 			return t;
939 		return CondParser_ComparisonOrLeaf(par, doEval);
940 	}
941 }
942 
943 /*
944  * Term -> '(' Or ')'
945  * Term -> '!' Term
946  * Term -> Leaf Operator Leaf
947  * Term -> Leaf
948  */
949 static CondResult
950 CondParser_Term(CondParser *par, bool doEval)
951 {
952 	CondResult res;
953 	Token t;
954 
955 	t = CondParser_Token(par, doEval);
956 	if (t == TOK_TRUE)
957 		return CR_TRUE;
958 	if (t == TOK_FALSE)
959 		return CR_FALSE;
960 
961 	if (t == TOK_LPAREN) {
962 		res = CondParser_Or(par, doEval);
963 		if (res == CR_ERROR)
964 			return CR_ERROR;
965 		if (CondParser_Token(par, doEval) != TOK_RPAREN)
966 			return CR_ERROR;
967 		return res;
968 	}
969 
970 	if (t == TOK_NOT) {
971 		res = CondParser_Term(par, doEval);
972 		if (res == CR_TRUE)
973 			res = CR_FALSE;
974 		else if (res == CR_FALSE)
975 			res = CR_TRUE;
976 		return res;
977 	}
978 
979 	return CR_ERROR;
980 }
981 
982 /*
983  * And -> Term '&&' And
984  * And -> Term
985  */
986 static CondResult
987 CondParser_And(CondParser *par, bool doEval)
988 {
989 	CondResult res;
990 	Token op;
991 
992 	res = CondParser_Term(par, doEval);
993 	if (res == CR_ERROR)
994 		return CR_ERROR;
995 
996 	op = CondParser_Token(par, doEval);
997 	if (op == TOK_AND) {
998 		if (res == CR_TRUE)
999 			return CondParser_And(par, doEval);
1000 		if (CondParser_And(par, false) == CR_ERROR)
1001 			return CR_ERROR;
1002 		return res;
1003 	}
1004 
1005 	CondParser_PushBack(par, op);
1006 	return res;
1007 }
1008 
1009 /*
1010  * Or -> And '||' Or
1011  * Or -> And
1012  */
1013 static CondResult
1014 CondParser_Or(CondParser *par, bool doEval)
1015 {
1016 	CondResult res;
1017 	Token op;
1018 
1019 	res = CondParser_And(par, doEval);
1020 	if (res == CR_ERROR)
1021 		return CR_ERROR;
1022 
1023 	op = CondParser_Token(par, doEval);
1024 	if (op == TOK_OR) {
1025 		if (res == CR_FALSE)
1026 			return CondParser_Or(par, doEval);
1027 		if (CondParser_Or(par, false) == CR_ERROR)
1028 			return CR_ERROR;
1029 		return res;
1030 	}
1031 
1032 	CondParser_PushBack(par, op);
1033 	return res;
1034 }
1035 
1036 static CondEvalResult
1037 CondParser_Eval(CondParser *par, bool *out_value)
1038 {
1039 	CondResult res;
1040 
1041 	DEBUG1(COND, "CondParser_Eval: %s\n", par->p);
1042 
1043 	res = CondParser_Or(par, true);
1044 	if (res == CR_ERROR)
1045 		return COND_INVALID;
1046 
1047 	if (CondParser_Token(par, false) != TOK_EOF)
1048 		return COND_INVALID;
1049 
1050 	*out_value = res == CR_TRUE;
1051 	return COND_PARSE;
1052 }
1053 
1054 /*
1055  * Evaluate the condition, including any side effects from the variable
1056  * expressions in the condition. The condition consists of &&, ||, !,
1057  * function(arg), comparisons and parenthetical groupings thereof.
1058  *
1059  * Results:
1060  *	COND_PARSE	if the condition was valid grammatically
1061  *	COND_INVALID	if not a valid conditional.
1062  *
1063  *	(*value) is set to the boolean value of the condition
1064  */
1065 static CondEvalResult
1066 CondEvalExpression(const char *cond, bool *out_value, bool plain,
1067 		   bool (*evalBare)(size_t, const char *), bool negate,
1068 		   bool eprint, bool strictLHS)
1069 {
1070 	CondParser par;
1071 	CondEvalResult rval;
1072 
1073 	lhsStrict = strictLHS;
1074 
1075 	cpp_skip_hspace(&cond);
1076 
1077 	par.plain = plain;
1078 	par.evalBare = evalBare;
1079 	par.negateEvalBare = negate;
1080 	par.p = cond;
1081 	par.curr = TOK_NONE;
1082 	par.printedError = false;
1083 
1084 	rval = CondParser_Eval(&par, out_value);
1085 
1086 	if (rval == COND_INVALID && eprint && !par.printedError)
1087 		Parse_Error(PARSE_FATAL, "Malformed conditional (%s)", cond);
1088 
1089 	return rval;
1090 }
1091 
1092 /*
1093  * Evaluate a condition in a :? modifier, such as
1094  * ${"${VAR}" == value:?yes:no}.
1095  */
1096 CondEvalResult
1097 Cond_EvalCondition(const char *cond, bool *out_value)
1098 {
1099 	return CondEvalExpression(cond, out_value, true,
1100 	    FuncDefined, false, false, false);
1101 }
1102 
1103 static bool
1104 IsEndif(const char *p)
1105 {
1106 	return p[0] == 'e' && p[1] == 'n' && p[2] == 'd' &&
1107 	       p[3] == 'i' && p[4] == 'f' && !ch_isalpha(p[5]);
1108 }
1109 
1110 static bool
1111 DetermineKindOfConditional(const char **pp, bool *out_plain,
1112 			   bool (**out_evalBare)(size_t, const char *),
1113 			   bool *out_negate)
1114 {
1115 	const char *p = *pp;
1116 
1117 	p += 2;
1118 	*out_plain = false;
1119 	*out_evalBare = FuncDefined;
1120 	*out_negate = false;
1121 	if (*p == 'n') {
1122 		p++;
1123 		*out_negate = true;
1124 	}
1125 	if (is_token(p, "def", 3)) {		/* .ifdef and .ifndef */
1126 		p += 3;
1127 	} else if (is_token(p, "make", 4)) {	/* .ifmake and .ifnmake */
1128 		p += 4;
1129 		*out_evalBare = FuncMake;
1130 	} else if (is_token(p, "", 0) && !*out_negate) { /* plain .if */
1131 		*out_plain = true;
1132 	} else {
1133 		/*
1134 		 * TODO: Add error message about unknown directive,
1135 		 * since there is no other known directive that starts
1136 		 * with 'el' or 'if'.
1137 		 *
1138 		 * Example: .elifx 123
1139 		 */
1140 		return false;
1141 	}
1142 
1143 	*pp = p;
1144 	return true;
1145 }
1146 
1147 /*
1148  * Evaluate the conditional directive in the line, which is one of:
1149  *
1150  *	.if <cond>
1151  *	.ifmake <cond>
1152  *	.ifnmake <cond>
1153  *	.ifdef <cond>
1154  *	.ifndef <cond>
1155  *	.elif <cond>
1156  *	.elifmake <cond>
1157  *	.elifnmake <cond>
1158  *	.elifdef <cond>
1159  *	.elifndef <cond>
1160  *	.else
1161  *	.endif
1162  *
1163  * In these directives, <cond> consists of &&, ||, !, function(arg),
1164  * comparisons, expressions, bare words, numbers and strings, and
1165  * parenthetical groupings thereof.
1166  *
1167  * Results:
1168  *	COND_PARSE	to continue parsing the lines that follow the
1169  *			conditional (when <cond> evaluates to true)
1170  *	COND_SKIP	to skip the lines after the conditional
1171  *			(when <cond> evaluates to false, or when a previous
1172  *			branch has already been taken)
1173  *	COND_INVALID	if the conditional was not valid, either because of
1174  *			a syntax error or because some variable was undefined
1175  *			or because the condition could not be evaluated
1176  */
1177 CondEvalResult
1178 Cond_EvalLine(const char *line)
1179 {
1180 	typedef enum IfState {
1181 
1182 		/* None of the previous <cond> evaluated to true. */
1183 		IFS_INITIAL	= 0,
1184 
1185 		/* The previous <cond> evaluated to true.
1186 		 * The lines following this condition are interpreted. */
1187 		IFS_ACTIVE	= 1 << 0,
1188 
1189 		/* The previous directive was an '.else'. */
1190 		IFS_SEEN_ELSE	= 1 << 1,
1191 
1192 		/* One of the previous <cond> evaluated to true. */
1193 		IFS_WAS_ACTIVE	= 1 << 2
1194 
1195 	} IfState;
1196 
1197 	static enum IfState *cond_states = NULL;
1198 	static unsigned int cond_states_cap = 128;
1199 
1200 	bool plain;
1201 	bool (*evalBare)(size_t, const char *);
1202 	bool negate;
1203 	bool isElif;
1204 	bool value;
1205 	IfState state;
1206 	const char *p = line;
1207 
1208 	if (cond_states == NULL) {
1209 		cond_states = bmake_malloc(
1210 		    cond_states_cap * sizeof *cond_states);
1211 		cond_states[0] = IFS_ACTIVE;
1212 	}
1213 
1214 	p++;			/* skip the leading '.' */
1215 	cpp_skip_hspace(&p);
1216 
1217 	if (IsEndif(p)) {	/* It is an '.endif'. */
1218 		if (p[5] != '\0') {
1219 			Parse_Error(PARSE_FATAL,
1220 			    "The .endif directive does not take arguments.");
1221 		}
1222 
1223 		if (cond_depth == cond_min_depth) {
1224 			Parse_Error(PARSE_FATAL, "if-less endif");
1225 			return COND_PARSE;
1226 		}
1227 
1228 		/* Return state for previous conditional */
1229 		cond_depth--;
1230 		return cond_states[cond_depth] & IFS_ACTIVE
1231 		    ? COND_PARSE : COND_SKIP;
1232 	}
1233 
1234 	/* Parse the name of the directive, such as 'if', 'elif', 'endif'. */
1235 	if (p[0] == 'e') {
1236 		if (p[1] != 'l') {
1237 			/*
1238 			 * Unknown directive.  It might still be a
1239 			 * transformation rule like '.elisp.scm',
1240 			 * therefore no error message here.
1241 			 */
1242 			return COND_INVALID;
1243 		}
1244 
1245 		/* Quite likely this is 'else' or 'elif' */
1246 		p += 2;
1247 		if (is_token(p, "se", 2)) {	/* It is an 'else'. */
1248 
1249 			if (p[2] != '\0')
1250 				Parse_Error(PARSE_FATAL,
1251 					    "The .else directive "
1252 					    "does not take arguments.");
1253 
1254 			if (cond_depth == cond_min_depth) {
1255 				Parse_Error(PARSE_FATAL, "if-less else");
1256 				return COND_PARSE;
1257 			}
1258 
1259 			state = cond_states[cond_depth];
1260 			if (state == IFS_INITIAL) {
1261 				state = IFS_ACTIVE | IFS_SEEN_ELSE;
1262 			} else {
1263 				if (state & IFS_SEEN_ELSE)
1264 					Parse_Error(PARSE_WARNING,
1265 						    "extra else");
1266 				state = IFS_WAS_ACTIVE | IFS_SEEN_ELSE;
1267 			}
1268 			cond_states[cond_depth] = state;
1269 
1270 			return state & IFS_ACTIVE ? COND_PARSE : COND_SKIP;
1271 		}
1272 		/* Assume for now it is an elif */
1273 		isElif = true;
1274 	} else
1275 		isElif = false;
1276 
1277 	if (p[0] != 'i' || p[1] != 'f') {
1278 		/*
1279 		 * Unknown directive.  It might still be a transformation rule
1280 		 * like '.elisp.scm', therefore no error message here.
1281 		 */
1282 		return COND_INVALID;	/* Not an ifxxx or elifxxx line */
1283 	}
1284 
1285 	if (!DetermineKindOfConditional(&p, &plain, &evalBare, &negate))
1286 		return COND_INVALID;
1287 
1288 	if (isElif) {
1289 		if (cond_depth == cond_min_depth) {
1290 			Parse_Error(PARSE_FATAL, "if-less elif");
1291 			return COND_PARSE;
1292 		}
1293 		state = cond_states[cond_depth];
1294 		if (state & IFS_SEEN_ELSE) {
1295 			Parse_Error(PARSE_WARNING, "extra elif");
1296 			cond_states[cond_depth] =
1297 			    IFS_WAS_ACTIVE | IFS_SEEN_ELSE;
1298 			return COND_SKIP;
1299 		}
1300 		if (state != IFS_INITIAL) {
1301 			cond_states[cond_depth] = IFS_WAS_ACTIVE;
1302 			return COND_SKIP;
1303 		}
1304 	} else {
1305 		/* Normal .if */
1306 		if (cond_depth + 1 >= cond_states_cap) {
1307 			/*
1308 			 * This is rare, but not impossible.
1309 			 * In meta mode, dirdeps.mk (only runs at level 0)
1310 			 * can need more than the default.
1311 			 */
1312 			cond_states_cap += 32;
1313 			cond_states = bmake_realloc(cond_states,
1314 						    cond_states_cap *
1315 						    sizeof *cond_states);
1316 		}
1317 		state = cond_states[cond_depth];
1318 		cond_depth++;
1319 		if (!(state & IFS_ACTIVE)) {
1320 			/*
1321 			 * If we aren't parsing the data,
1322 			 * treat as always false.
1323 			 */
1324 			cond_states[cond_depth] = IFS_WAS_ACTIVE;
1325 			return COND_SKIP;
1326 		}
1327 	}
1328 
1329 	/* And evaluate the conditional expression */
1330 	if (CondEvalExpression(p, &value, plain, evalBare, negate,
1331 	    true, true) == COND_INVALID) {
1332 		/* Syntax error in conditional, error message already output. */
1333 		/* Skip everything to matching .endif */
1334 		/* XXX: An extra '.else' is not detected in this case. */
1335 		cond_states[cond_depth] = IFS_WAS_ACTIVE;
1336 		return COND_SKIP;
1337 	}
1338 
1339 	if (!value) {
1340 		cond_states[cond_depth] = IFS_INITIAL;
1341 		return COND_SKIP;
1342 	}
1343 	cond_states[cond_depth] = IFS_ACTIVE;
1344 	return COND_PARSE;
1345 }
1346 
1347 void
1348 Cond_restore_depth(unsigned int saved_depth)
1349 {
1350 	unsigned int open_conds = cond_depth - cond_min_depth;
1351 
1352 	if (open_conds != 0 || saved_depth > cond_depth) {
1353 		Parse_Error(PARSE_FATAL, "%u open conditional%s",
1354 			    open_conds, open_conds == 1 ? "" : "s");
1355 		cond_depth = cond_min_depth;
1356 	}
1357 
1358 	cond_min_depth = saved_depth;
1359 }
1360 
1361 unsigned int
1362 Cond_save_depth(void)
1363 {
1364 	unsigned int depth = cond_min_depth;
1365 
1366 	cond_min_depth = cond_depth;
1367 	return depth;
1368 }
1369