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