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