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