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