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