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