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