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