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