Home | History | Annotate | Line # | Download | only in make
cond.c revision 1.85
      1 /*	$NetBSD: cond.c,v 1.85 2020/08/01 14:47:49 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 #ifndef MAKE_NATIVE
     73 static char rcsid[] = "$NetBSD: cond.c,v 1.85 2020/08/01 14:47:49 rillig Exp $";
     74 #else
     75 #include <sys/cdefs.h>
     76 #ifndef lint
     77 #if 0
     78 static char sccsid[] = "@(#)cond.c	8.2 (Berkeley) 1/2/94";
     79 #else
     80 __RCSID("$NetBSD: cond.c,v 1.85 2020/08/01 14:47:49 rillig Exp $");
     81 #endif
     82 #endif /* not lint */
     83 #endif
     84 
     85 /*-
     86  * cond.c --
     87  *	Functions to handle conditionals in a makefile.
     88  *
     89  * Interface:
     90  *	Cond_Eval 	Evaluate the conditional in the passed line.
     91  *
     92  */
     93 
     94 #include    <assert.h>
     95 #include    <ctype.h>
     96 #include    <errno.h>    /* For strtoul() error checking */
     97 
     98 #include    "make.h"
     99 #include    "hash.h"
    100 #include    "dir.h"
    101 #include    "buf.h"
    102 
    103 /*
    104  * The parsing of conditional expressions is based on this grammar:
    105  *	E -> F || E
    106  *	E -> F
    107  *	F -> T && F
    108  *	F -> T
    109  *	T -> defined(variable)
    110  *	T -> make(target)
    111  *	T -> exists(file)
    112  *	T -> empty(varspec)
    113  *	T -> target(name)
    114  *	T -> commands(name)
    115  *	T -> symbol
    116  *	T -> $(varspec) op value
    117  *	T -> $(varspec) == "string"
    118  *	T -> $(varspec) != "string"
    119  *	T -> "string"
    120  *	T -> ( E )
    121  *	T -> ! T
    122  *	op -> == | != | > | < | >= | <=
    123  *
    124  * 'symbol' is some other symbol to which the default function (condDefProc)
    125  * is applied.
    126  *
    127  * Tokens are scanned from the 'condExpr' string. The scanner (CondToken)
    128  * will return TOK_AND for '&' and '&&', TOK_OR for '|' and '||',
    129  * TOK_NOT for '!', TOK_LPAREN for '(', TOK_RPAREN for ')' and will evaluate
    130  * the other terminal symbols, using either the default function or the
    131  * function given in the terminal, and return the result as either TOK_TRUE
    132  * or TOK_FALSE.
    133  *
    134  * TOK_FALSE is 0 and TOK_TRUE 1 so we can directly assign C comparisons.
    135  *
    136  * All Non-Terminal functions (CondE, CondF and CondT) return TOK_ERROR on
    137  * error.
    138  */
    139 typedef enum {
    140     TOK_FALSE = 0, TOK_TRUE = 1, TOK_AND, TOK_OR, TOK_NOT,
    141     TOK_LPAREN, TOK_RPAREN, TOK_EOF, TOK_NONE, TOK_ERROR
    142 } Token;
    143 
    144 /*-
    145  * Structures to handle elegantly the different forms of #if's. The
    146  * last two fields are stored in condInvert and condDefProc, respectively.
    147  */
    148 static void CondPushBack(Token);
    149 static int CondGetArg(Boolean, char **, char **, const char *);
    150 static Boolean CondDoDefined(int, const char *);
    151 static int CondStrMatch(const void *, const void *);
    152 static Boolean CondDoMake(int, const char *);
    153 static Boolean CondDoExists(int, const char *);
    154 static Boolean CondDoTarget(int, const char *);
    155 static Boolean CondDoCommands(int, const char *);
    156 static Boolean CondCvtArg(const char *, double *);
    157 static Token CondToken(Boolean);
    158 static Token CondT(Boolean);
    159 static Token CondF(Boolean);
    160 static Token CondE(Boolean);
    161 static int do_Cond_EvalExpression(Boolean *);
    162 
    163 static const struct If {
    164     const char	*form;	      /* Form of if */
    165     int		formlen;      /* Length of form */
    166     Boolean	doNot;	      /* TRUE if default function should be negated */
    167     Boolean	(*defProc)(int, const char *); /* Default function to apply */
    168 } ifs[] = {
    169     { "def",	  3,	  FALSE,  CondDoDefined },
    170     { "ndef",	  4,	  TRUE,	  CondDoDefined },
    171     { "make",	  4,	  FALSE,  CondDoMake },
    172     { "nmake",	  5,	  TRUE,	  CondDoMake },
    173     { "",	  0,	  FALSE,  CondDoDefined },
    174     { NULL,	  0,	  FALSE,  NULL }
    175 };
    176 
    177 static const struct If *if_info;        /* Info for current statement */
    178 static char 	  *condExpr;	    	/* The expression to parse */
    179 static Token	  condPushBack=TOK_NONE;	/* Single push-back token used in
    180 					 * parsing */
    181 
    182 static unsigned int	cond_depth = 0;  	/* current .if nesting level */
    183 static unsigned int	cond_min_depth = 0;  	/* depth at makefile open */
    184 
    185 /*
    186  * Indicate when we should be strict about lhs of comparisons.
    187  * TRUE when Cond_EvalExpression is called from Cond_Eval (.if etc)
    188  * FALSE when Cond_EvalExpression is called from var.c:ApplyModifiers
    189  * since lhs is already expanded and we cannot tell if
    190  * it was a variable reference or not.
    191  */
    192 static Boolean lhsStrict;
    193 
    194 static int
    195 istoken(const char *str, const char *tok, size_t len)
    196 {
    197 	return strncmp(str, tok, len) == 0 && !isalpha((unsigned char)str[len]);
    198 }
    199 
    200 /*-
    201  *-----------------------------------------------------------------------
    202  * CondPushBack --
    203  *	Push back the most recent token read. We only need one level of
    204  *	this, so the thing is just stored in 'condPushback'.
    205  *
    206  * Input:
    207  *	t		Token to push back into the "stream"
    208  *
    209  * Results:
    210  *	None.
    211  *
    212  * Side Effects:
    213  *	condPushback is overwritten.
    214  *
    215  *-----------------------------------------------------------------------
    216  */
    217 static void
    218 CondPushBack(Token t)
    219 {
    220     condPushBack = t;
    221 }
    222 
    223 /*-
    225  *-----------------------------------------------------------------------
    226  * CondGetArg --
    227  *	Find the argument of a built-in function.
    228  *
    229  * Results:
    230  *	The length of the argument and the address of the argument.
    231  *
    232  * Side Effects:
    233  *	The pointer is set to point to the closing parenthesis of the
    234  *	function call.
    235  *
    236  *-----------------------------------------------------------------------
    237  */
    238 static int
    239 CondGetArg(Boolean doEval, char **linePtr, char **argPtr, const char *func)
    240 {
    241     char	  *cp;
    242     int	    	  argLen;
    243     Buffer	  buf;
    244     int           paren_depth;
    245     char          ch;
    246 
    247     cp = *linePtr;
    248     if (func != NULL)
    249 	/* Skip opening '(' - verfied by caller */
    250 	cp++;
    251 
    252     if (*cp == '\0') {
    253 	/*
    254 	 * No arguments whatsoever. Because 'make' and 'defined' aren't really
    255 	 * "reserved words", we don't print a message. I think this is better
    256 	 * than hitting the user with a warning message every time s/he uses
    257 	 * the word 'make' or 'defined' at the beginning of a symbol...
    258 	 */
    259 	*argPtr = NULL;
    260 	return 0;
    261     }
    262 
    263     while (*cp == ' ' || *cp == '\t') {
    264 	cp++;
    265     }
    266 
    267     /*
    268      * Create a buffer for the argument and start it out at 16 characters
    269      * long. Why 16? Why not?
    270      */
    271     Buf_Init(&buf, 16);
    272 
    273     paren_depth = 0;
    274     for (;;) {
    275 	ch = *cp;
    276 	if (ch == 0 || ch == ' ' || ch == '\t')
    277 	    break;
    278 	if ((ch == '&' || ch == '|') && paren_depth == 0)
    279 	    break;
    280 	if (*cp == '$') {
    281 	    /*
    282 	     * Parse the variable spec and install it as part of the argument
    283 	     * if it's valid. We tell Var_Parse to complain on an undefined
    284 	     * variable, so we don't do it too. Nor do we return an error,
    285 	     * though perhaps we should...
    286 	     */
    287 	    int		len;
    288 	    void	*freeIt;
    289 	    VarEvalFlags eflags = VARE_UNDEFERR | (doEval ? VARE_WANTRES : 0);
    290 	    const char *cp2 = Var_Parse(cp, VAR_CMD, eflags, &len, &freeIt);
    291 	    Buf_AddBytes(&buf, strlen(cp2), cp2);
    292 	    free(freeIt);
    293 	    cp += len;
    294 	    continue;
    295 	}
    296 	if (ch == '(')
    297 	    paren_depth++;
    298 	else
    299 	    if (ch == ')' && --paren_depth < 0)
    300 		break;
    301 	Buf_AddByte(&buf, *cp);
    302 	cp++;
    303     }
    304 
    305     *argPtr = Buf_GetAll(&buf, &argLen);
    306     Buf_Destroy(&buf, FALSE);
    307 
    308     while (*cp == ' ' || *cp == '\t') {
    309 	cp++;
    310     }
    311 
    312     if (func != NULL && *cp++ != ')') {
    313 	Parse_Error(PARSE_WARNING, "Missing closing parenthesis for %s()",
    314 		     func);
    315 	return 0;
    316     }
    317 
    318     *linePtr = cp;
    319     return argLen;
    320 }
    321 
    322 /*-
    324  *-----------------------------------------------------------------------
    325  * CondDoDefined --
    326  *	Handle the 'defined' function for conditionals.
    327  *
    328  * Results:
    329  *	TRUE if the given variable is defined.
    330  *
    331  * Side Effects:
    332  *	None.
    333  *
    334  *-----------------------------------------------------------------------
    335  */
    336 static Boolean
    337 CondDoDefined(int argLen MAKE_ATTR_UNUSED, const char *arg)
    338 {
    339     char *freeIt;
    340     Boolean result = Var_Value(arg, VAR_CMD, &freeIt) != NULL;
    341     bmake_free(freeIt);
    342     return result;
    343 }
    344 
    345 /*-
    347  *-----------------------------------------------------------------------
    348  * CondStrMatch --
    349  *	Front-end for Str_Match so it returns 0 on match and non-zero
    350  *	on mismatch. Callback function for CondDoMake via Lst_Find
    351  *
    352  * Results:
    353  *	0 if string matches pattern
    354  *
    355  * Side Effects:
    356  *	None
    357  *
    358  *-----------------------------------------------------------------------
    359  */
    360 static int
    361 CondStrMatch(const void *string, const void *pattern)
    362 {
    363     return !Str_Match(string, pattern);
    364 }
    365 
    366 /*-
    368  *-----------------------------------------------------------------------
    369  * CondDoMake --
    370  *	Handle the 'make' function for conditionals.
    371  *
    372  * Results:
    373  *	TRUE if the given target is being made.
    374  *
    375  * Side Effects:
    376  *	None.
    377  *
    378  *-----------------------------------------------------------------------
    379  */
    380 static Boolean
    381 CondDoMake(int argLen MAKE_ATTR_UNUSED, const char *arg)
    382 {
    383     return Lst_Find(create, arg, CondStrMatch) != NULL;
    384 }
    385 
    386 /*-
    388  *-----------------------------------------------------------------------
    389  * CondDoExists --
    390  *	See if the given file exists.
    391  *
    392  * Results:
    393  *	TRUE if the file exists and FALSE if it does not.
    394  *
    395  * Side Effects:
    396  *	None.
    397  *
    398  *-----------------------------------------------------------------------
    399  */
    400 static Boolean
    401 CondDoExists(int argLen MAKE_ATTR_UNUSED, const char *arg)
    402 {
    403     Boolean result;
    404     char    *path;
    405 
    406     path = Dir_FindFile(arg, dirSearchPath);
    407     if (DEBUG(COND)) {
    408 	fprintf(debug_file, "exists(%s) result is \"%s\"\n",
    409 	       arg, path ? path : "");
    410     }
    411     if (path != NULL) {
    412 	result = TRUE;
    413 	free(path);
    414     } else {
    415 	result = FALSE;
    416     }
    417     return result;
    418 }
    419 
    420 /*-
    422  *-----------------------------------------------------------------------
    423  * CondDoTarget --
    424  *	See if the given node exists and is an actual target.
    425  *
    426  * Results:
    427  *	TRUE if the node exists as a target and FALSE if it does not.
    428  *
    429  * Side Effects:
    430  *	None.
    431  *
    432  *-----------------------------------------------------------------------
    433  */
    434 static Boolean
    435 CondDoTarget(int argLen MAKE_ATTR_UNUSED, const char *arg)
    436 {
    437     GNode   *gn;
    438 
    439     gn = Targ_FindNode(arg, TARG_NOCREATE);
    440     return gn != NULL && !OP_NOP(gn->type);
    441 }
    442 
    443 /*-
    444  *-----------------------------------------------------------------------
    445  * CondDoCommands --
    446  *	See if the given node exists and is an actual target with commands
    447  *	associated with it.
    448  *
    449  * Results:
    450  *	TRUE if the node exists as a target and has commands associated with
    451  *	it and FALSE if it does not.
    452  *
    453  * Side Effects:
    454  *	None.
    455  *
    456  *-----------------------------------------------------------------------
    457  */
    458 static Boolean
    459 CondDoCommands(int argLen MAKE_ATTR_UNUSED, const char *arg)
    460 {
    461     GNode   *gn;
    462 
    463     gn = Targ_FindNode(arg, TARG_NOCREATE);
    464     return gn != NULL && !OP_NOP(gn->type) && !Lst_IsEmpty(gn->commands);
    465 }
    466 
    467 /*-
    469  *-----------------------------------------------------------------------
    470  * CondCvtArg --
    471  *	Convert the given number into a double.
    472  *	We try a base 10 or 16 integer conversion first, if that fails
    473  *	then we try a floating point conversion instead.
    474  *
    475  * Results:
    476  *	Sets 'value' to double value of string.
    477  *	Returns 'true' if the convertion suceeded
    478  *
    479  *-----------------------------------------------------------------------
    480  */
    481 static Boolean
    482 CondCvtArg(const char *str, double *value)
    483 {
    484     char *eptr, ech;
    485     unsigned long l_val;
    486     double d_val;
    487 
    488     errno = 0;
    489     if (!*str) {
    490 	*value = (double)0;
    491 	return TRUE;
    492     }
    493     l_val = strtoul(str, &eptr, str[1] == 'x' ? 16 : 10);
    494     ech = *eptr;
    495     if (ech == 0 && errno != ERANGE) {
    496 	d_val = str[0] == '-' ? -(double)-l_val : (double)l_val;
    497     } else {
    498 	if (ech != 0 && ech != '.' && ech != 'e' && ech != 'E')
    499 	    return FALSE;
    500 	d_val = strtod(str, &eptr);
    501 	if (*eptr)
    502 	    return FALSE;
    503     }
    504 
    505     *value = d_val;
    506     return TRUE;
    507 }
    508 
    509 /*-
    510  *-----------------------------------------------------------------------
    511  * CondGetString --
    512  *	Get a string from a variable reference or an optionally quoted
    513  *	string.  This is called for the lhs and rhs of string compares.
    514  *
    515  * Results:
    516  *	Sets freeIt if needed,
    517  *	Sets quoted if string was quoted,
    518  *	Returns NULL on error,
    519  *	else returns string - absent any quotes.
    520  *
    521  * Side Effects:
    522  *	Moves condExpr to end of this token.
    523  *
    524  *
    525  *-----------------------------------------------------------------------
    526  */
    527 /* coverity:[+alloc : arg-*2] */
    528 static const char *
    529 CondGetString(Boolean doEval, Boolean *quoted, void **freeIt, Boolean strictLHS)
    530 {
    531     Buffer buf;
    532     const char *cp;
    533     const char *str;
    534     int	len;
    535     int qt;
    536     char *start;
    537 
    538     Buf_Init(&buf, 0);
    539     str = NULL;
    540     *freeIt = NULL;
    541     *quoted = qt = *condExpr == '"' ? 1 : 0;
    542     if (qt)
    543 	condExpr++;
    544     for (start = condExpr; *condExpr && str == NULL; condExpr++) {
    545 	switch (*condExpr) {
    546 	case '\\':
    547 	    if (condExpr[1] != '\0') {
    548 		condExpr++;
    549 		Buf_AddByte(&buf, *condExpr);
    550 	    }
    551 	    break;
    552 	case '"':
    553 	    if (qt) {
    554 		condExpr++;		/* we don't want the quotes */
    555 		goto got_str;
    556 	    } else
    557 		Buf_AddByte(&buf, *condExpr); /* likely? */
    558 	    break;
    559 	case ')':
    560 	case '!':
    561 	case '=':
    562 	case '>':
    563 	case '<':
    564 	case ' ':
    565 	case '\t':
    566 	    if (!qt)
    567 		goto got_str;
    568 	    else
    569 		Buf_AddByte(&buf, *condExpr);
    570 	    break;
    571 	case '$':
    572 	    /* if we are in quotes, then an undefined variable is ok */
    573 	    str = Var_Parse(condExpr, VAR_CMD,
    574 			    ((!qt && doEval) ? VARE_UNDEFERR : 0) |
    575 			    (doEval ? VARE_WANTRES : 0), &len, freeIt);
    576 	    if (str == var_Error) {
    577 		if (*freeIt) {
    578 		    free(*freeIt);
    579 		    *freeIt = NULL;
    580 		}
    581 		/*
    582 		 * Even if !doEval, we still report syntax errors, which
    583 		 * is what getting var_Error back with !doEval means.
    584 		 */
    585 		str = NULL;
    586 		goto cleanup;
    587 	    }
    588 	    condExpr += len;
    589 	    /*
    590 	     * If the '$' was first char (no quotes), and we are
    591 	     * followed by space, the operator or end of expression,
    592 	     * we are done.
    593 	     */
    594 	    if ((condExpr == start + len) &&
    595 		(*condExpr == '\0' ||
    596 		 isspace((unsigned char) *condExpr) ||
    597 		 strchr("!=><)", *condExpr))) {
    598 		goto cleanup;
    599 	    }
    600 	    /*
    601 	     * Nope, we better copy str to buf
    602 	     */
    603 	    for (cp = str; *cp; cp++) {
    604 		Buf_AddByte(&buf, *cp);
    605 	    }
    606 	    if (*freeIt) {
    607 		free(*freeIt);
    608 		*freeIt = NULL;
    609 	    }
    610 	    str = NULL;			/* not finished yet */
    611 	    condExpr--;			/* don't skip over next char */
    612 	    break;
    613 	default:
    614 	    if (strictLHS && !qt && *start != '$' &&
    615 		!isdigit((unsigned char) *start)) {
    616 		/* lhs must be quoted, a variable reference or number */
    617 		if (*freeIt) {
    618 		    free(*freeIt);
    619 		    *freeIt = NULL;
    620 		}
    621 		str = NULL;
    622 		goto cleanup;
    623 	    }
    624 	    Buf_AddByte(&buf, *condExpr);
    625 	    break;
    626 	}
    627     }
    628  got_str:
    629     *freeIt = Buf_GetAll(&buf, NULL);
    630     str = *freeIt;
    631  cleanup:
    632     Buf_Destroy(&buf, FALSE);
    633     return str;
    634 }
    635 
    636 /*-
    638  *-----------------------------------------------------------------------
    639  * CondToken --
    640  *	Return the next token from the input.
    641  *
    642  * Results:
    643  *	A Token for the next lexical token in the stream.
    644  *
    645  * Side Effects:
    646  *	condPushback will be set back to TOK_NONE if it is used.
    647  *
    648  *-----------------------------------------------------------------------
    649  */
    650 static Token
    651 compare_expression(Boolean doEval)
    652 {
    653     Token	t;
    654     const char	*lhs;
    655     const char	*rhs;
    656     char	*op;
    657     void	*lhsFree;
    658     void	*rhsFree;
    659     Boolean lhsQuoted;
    660     Boolean rhsQuoted;
    661     double  	left, right;
    662 
    663     t = TOK_ERROR;
    664     rhs = NULL;
    665     lhsFree = rhsFree = FALSE;
    666     lhsQuoted = rhsQuoted = FALSE;
    667 
    668     /*
    669      * Parse the variable spec and skip over it, saving its
    670      * value in lhs.
    671      */
    672     lhs = CondGetString(doEval, &lhsQuoted, &lhsFree, lhsStrict);
    673     if (!lhs)
    674 	goto done;
    675 
    676     /*
    677      * Skip whitespace to get to the operator
    678      */
    679     while (isspace((unsigned char) *condExpr))
    680 	condExpr++;
    681 
    682     /*
    683      * Make sure the operator is a valid one. If it isn't a
    684      * known relational operator, pretend we got a
    685      * != 0 comparison.
    686      */
    687     op = condExpr;
    688     switch (*condExpr) {
    689 	case '!':
    690 	case '=':
    691 	case '<':
    692 	case '>':
    693 	    if (condExpr[1] == '=') {
    694 		condExpr += 2;
    695 	    } else {
    696 		condExpr += 1;
    697 	    }
    698 	    break;
    699 	default:
    700 	    if (!doEval) {
    701 		t = TOK_FALSE;
    702 		goto done;
    703 	    }
    704 	    /* For .ifxxx "..." check for non-empty string. */
    705 	    if (lhsQuoted) {
    706 		t = lhs[0] != 0;
    707 		goto done;
    708 	    }
    709 	    /* For .ifxxx <number> compare against zero */
    710 	    if (CondCvtArg(lhs, &left)) {
    711 		t = left != 0.0;
    712 		goto done;
    713 	    }
    714 	    /* For .if ${...} check for non-empty string (defProc is ifdef). */
    715 	    if (if_info->form[0] == 0) {
    716 		t = lhs[0] != 0;
    717 		goto done;
    718 	    }
    719 	    /* Otherwise action default test ... */
    720 	    t = if_info->defProc(strlen(lhs), lhs) != if_info->doNot;
    721 	    goto done;
    722     }
    723 
    724     while (isspace((unsigned char)*condExpr))
    725 	condExpr++;
    726 
    727     if (*condExpr == '\0') {
    728 	Parse_Error(PARSE_WARNING,
    729 		    "Missing right-hand-side of operator");
    730 	goto done;
    731     }
    732 
    733     rhs = CondGetString(doEval, &rhsQuoted, &rhsFree, FALSE);
    734     if (!rhs)
    735 	goto done;
    736 
    737     if (!doEval) {
    738 	t = TOK_FALSE;
    739 	goto done;
    740     }
    741 
    742     if (rhsQuoted || lhsQuoted) {
    743 do_string_compare:
    744 	if (((*op != '!') && (*op != '=')) || (op[1] != '=')) {
    745 	    Parse_Error(PARSE_WARNING,
    746     "String comparison operator should be either == or !=");
    747 	    goto done;
    748 	}
    749 
    750 	if (DEBUG(COND)) {
    751 	    fprintf(debug_file, "lhs = \"%s\", rhs = \"%s\", op = %.2s\n",
    752 		   lhs, rhs, op);
    753 	}
    754 	/*
    755 	 * Null-terminate rhs and perform the comparison.
    756 	 * t is set to the result.
    757 	 */
    758 	if (*op == '=') {
    759 	    t = strcmp(lhs, rhs) == 0;
    760 	} else {
    761 	    t = strcmp(lhs, rhs) != 0;
    762 	}
    763     } else {
    764 	/*
    765 	 * rhs is either a float or an integer. Convert both the
    766 	 * lhs and the rhs to a double and compare the two.
    767 	 */
    768 
    769 	if (!CondCvtArg(lhs, &left) || !CondCvtArg(rhs, &right))
    770 	    goto do_string_compare;
    771 
    772 	if (DEBUG(COND)) {
    773 	    fprintf(debug_file, "left = %f, right = %f, op = %.2s\n", left,
    774 		   right, op);
    775 	}
    776 	switch(op[0]) {
    777 	case '!':
    778 	    if (op[1] != '=') {
    779 		Parse_Error(PARSE_WARNING,
    780 			    "Unknown operator");
    781 		goto done;
    782 	    }
    783 	    t = (left != right);
    784 	    break;
    785 	case '=':
    786 	    if (op[1] != '=') {
    787 		Parse_Error(PARSE_WARNING,
    788 			    "Unknown operator");
    789 		goto done;
    790 	    }
    791 	    t = (left == right);
    792 	    break;
    793 	case '<':
    794 	    if (op[1] == '=') {
    795 		t = (left <= right);
    796 	    } else {
    797 		t = (left < right);
    798 	    }
    799 	    break;
    800 	case '>':
    801 	    if (op[1] == '=') {
    802 		t = (left >= right);
    803 	    } else {
    804 		t = (left > right);
    805 	    }
    806 	    break;
    807 	}
    808     }
    809 
    810 done:
    811     free(lhsFree);
    812     free(rhsFree);
    813     return t;
    814 }
    815 
    816 static int
    817 get_mpt_arg(Boolean doEval, char **linePtr, char **argPtr, const char *func MAKE_ATTR_UNUSED)
    818 {
    819     /*
    820      * Use Var_Parse to parse the spec in parens and return
    821      * TOK_TRUE if the resulting string is empty.
    822      */
    823     int	    length;
    824     void    *freeIt;
    825     const char *val;
    826     char    *cp = *linePtr;
    827 
    828     /* We do all the work here and return the result as the length */
    829     *argPtr = NULL;
    830 
    831     val = Var_Parse(cp - 1, VAR_CMD, doEval ? VARE_WANTRES : 0, &length, &freeIt);
    832     /*
    833      * Advance *linePtr to beyond the closing ). Note that
    834      * we subtract one because 'length' is calculated from 'cp - 1'.
    835      */
    836     *linePtr = cp - 1 + length;
    837 
    838     if (val == var_Error) {
    839 	free(freeIt);
    840 	return -1;
    841     }
    842 
    843     /* A variable is empty when it just contains spaces... 4/15/92, christos */
    844     while (isspace((unsigned char)val[0]))
    845 	val++;
    846 
    847     /*
    848      * For consistency with the other functions we can't generate the
    849      * true/false here.
    850      */
    851     length = *val ? 2 : 1;
    852     free(freeIt);
    853     return length;
    854 }
    855 
    856 static Boolean
    857 CondDoEmpty(int arglen, const char *arg MAKE_ATTR_UNUSED)
    858 {
    859     return arglen == 1;
    860 }
    861 
    862 static Token
    863 compare_function(Boolean doEval)
    864 {
    865     static const struct fn_def {
    866 	const char  *fn_name;
    867 	int         fn_name_len;
    868 	int         (*fn_getarg)(Boolean, char **, char **, const char *);
    869 	Boolean     (*fn_proc)(int, const char *);
    870     } fn_defs[] = {
    871 	{ "defined",   7, CondGetArg, CondDoDefined },
    872 	{ "make",      4, CondGetArg, CondDoMake },
    873 	{ "exists",    6, CondGetArg, CondDoExists },
    874 	{ "empty",     5, get_mpt_arg, CondDoEmpty },
    875 	{ "target",    6, CondGetArg, CondDoTarget },
    876 	{ "commands",  8, CondGetArg, CondDoCommands },
    877 	{ NULL,        0, NULL, NULL },
    878     };
    879     const struct fn_def *fn_def;
    880     Token	t;
    881     char	*arg = NULL;
    882     int	arglen;
    883     char *cp = condExpr;
    884     char *cp1;
    885 
    886     for (fn_def = fn_defs; fn_def->fn_name != NULL; fn_def++) {
    887 	if (!istoken(cp, fn_def->fn_name, fn_def->fn_name_len))
    888 	    continue;
    889 	cp += fn_def->fn_name_len;
    890 	/* There can only be whitespace before the '(' */
    891 	while (isspace(*(unsigned char *)cp))
    892 	    cp++;
    893 	if (*cp != '(')
    894 	    break;
    895 
    896 	arglen = fn_def->fn_getarg(doEval, &cp, &arg, fn_def->fn_name);
    897 	if (arglen <= 0) {
    898 	    condExpr = cp;
    899 	    return arglen < 0 ? TOK_ERROR : TOK_FALSE;
    900 	}
    901 	/* Evaluate the argument using the required function. */
    902 	t = !doEval || fn_def->fn_proc(arglen, arg);
    903 	free(arg);
    904 	condExpr = cp;
    905 	return t;
    906     }
    907 
    908     /* Push anything numeric through the compare expression */
    909     cp = condExpr;
    910     if (isdigit((unsigned char)cp[0]) || strchr("+-", cp[0]))
    911 	return compare_expression(doEval);
    912 
    913     /*
    914      * Most likely we have a naked token to apply the default function to.
    915      * However ".if a == b" gets here when the "a" is unquoted and doesn't
    916      * start with a '$'. This surprises people.
    917      * If what follows the function argument is a '=' or '!' then the syntax
    918      * would be invalid if we did "defined(a)" - so instead treat as an
    919      * expression.
    920      */
    921     arglen = CondGetArg(doEval, &cp, &arg, NULL);
    922     for (cp1 = cp; isspace(*(unsigned char *)cp1); cp1++)
    923 	continue;
    924     if (*cp1 == '=' || *cp1 == '!')
    925 	return compare_expression(doEval);
    926     condExpr = cp;
    927 
    928     /*
    929      * Evaluate the argument using the default function.
    930      * This path always treats .if as .ifdef. To get here the character
    931      * after .if must have been taken literally, so the argument cannot
    932      * be empty - even if it contained a variable expansion.
    933      */
    934     t = !doEval || if_info->defProc(arglen, arg) != if_info->doNot;
    935     free(arg);
    936     return t;
    937 }
    938 
    939 static Token
    940 CondToken(Boolean doEval)
    941 {
    942     Token t;
    943 
    944     t = condPushBack;
    945     if (t != TOK_NONE) {
    946 	condPushBack = TOK_NONE;
    947 	return t;
    948     }
    949 
    950     while (*condExpr == ' ' || *condExpr == '\t') {
    951 	condExpr++;
    952     }
    953 
    954     switch (*condExpr) {
    955 
    956     case '(':
    957 	condExpr++;
    958 	return TOK_LPAREN;
    959 
    960     case ')':
    961 	condExpr++;
    962 	return TOK_RPAREN;
    963 
    964     case '|':
    965 	if (condExpr[1] == '|') {
    966 	    condExpr++;
    967 	}
    968 	condExpr++;
    969 	return TOK_OR;
    970 
    971     case '&':
    972 	if (condExpr[1] == '&') {
    973 	    condExpr++;
    974 	}
    975 	condExpr++;
    976 	return TOK_AND;
    977 
    978     case '!':
    979 	condExpr++;
    980 	return TOK_NOT;
    981 
    982     case '#':
    983     case '\n':
    984     case '\0':
    985 	return TOK_EOF;
    986 
    987     case '"':
    988     case '$':
    989 	return compare_expression(doEval);
    990 
    991     default:
    992 	return compare_function(doEval);
    993     }
    994 }
    995 
    996 /*-
    997  *-----------------------------------------------------------------------
    998  * CondT --
    999  *	Parse a single term in the expression. This consists of a terminal
   1000  *	symbol or TOK_NOT and a terminal symbol (not including the binary
   1001  *	operators):
   1002  *	    T -> defined(variable) | make(target) | exists(file) | symbol
   1003  *	    T -> ! T | ( E )
   1004  *
   1005  * Results:
   1006  *	TOK_TRUE, TOK_FALSE or TOK_ERROR.
   1007  *
   1008  * Side Effects:
   1009  *	Tokens are consumed.
   1010  *
   1011  *-----------------------------------------------------------------------
   1012  */
   1013 static Token
   1014 CondT(Boolean doEval)
   1015 {
   1016     Token   t;
   1017 
   1018     t = CondToken(doEval);
   1019 
   1020     if (t == TOK_EOF) {
   1021 	/*
   1022 	 * If we reached the end of the expression, the expression
   1023 	 * is malformed...
   1024 	 */
   1025 	t = TOK_ERROR;
   1026     } else if (t == TOK_LPAREN) {
   1027 	/*
   1028 	 * T -> ( E )
   1029 	 */
   1030 	t = CondE(doEval);
   1031 	if (t != TOK_ERROR) {
   1032 	    if (CondToken(doEval) != TOK_RPAREN) {
   1033 		t = TOK_ERROR;
   1034 	    }
   1035 	}
   1036     } else if (t == TOK_NOT) {
   1037 	t = CondT(doEval);
   1038 	if (t == TOK_TRUE) {
   1039 	    t = TOK_FALSE;
   1040 	} else if (t == TOK_FALSE) {
   1041 	    t = TOK_TRUE;
   1042 	}
   1043     }
   1044     return t;
   1045 }
   1046 
   1047 /*-
   1049  *-----------------------------------------------------------------------
   1050  * CondF --
   1051  *	Parse a conjunctive factor (nice name, wot?)
   1052  *	    F -> T && F | T
   1053  *
   1054  * Results:
   1055  *	TOK_TRUE, TOK_FALSE or TOK_ERROR
   1056  *
   1057  * Side Effects:
   1058  *	Tokens are consumed.
   1059  *
   1060  *-----------------------------------------------------------------------
   1061  */
   1062 static Token
   1063 CondF(Boolean doEval)
   1064 {
   1065     Token   l, o;
   1066 
   1067     l = CondT(doEval);
   1068     if (l != TOK_ERROR) {
   1069 	o = CondToken(doEval);
   1070 
   1071 	if (o == TOK_AND) {
   1072 	    /*
   1073 	     * F -> T && F
   1074 	     *
   1075 	     * If T is TOK_FALSE, the whole thing will be TOK_FALSE, but we have to
   1076 	     * parse the r.h.s. anyway (to throw it away).
   1077 	     * If T is TOK_TRUE, the result is the r.h.s., be it an TOK_ERROR or no.
   1078 	     */
   1079 	    if (l == TOK_TRUE) {
   1080 		l = CondF(doEval);
   1081 	    } else {
   1082 		(void)CondF(FALSE);
   1083 	    }
   1084 	} else {
   1085 	    /*
   1086 	     * F -> T
   1087 	     */
   1088 	    CondPushBack(o);
   1089 	}
   1090     }
   1091     return l;
   1092 }
   1093 
   1094 /*-
   1096  *-----------------------------------------------------------------------
   1097  * CondE --
   1098  *	Main expression production.
   1099  *	    E -> F || E | F
   1100  *
   1101  * Results:
   1102  *	TOK_TRUE, TOK_FALSE or TOK_ERROR.
   1103  *
   1104  * Side Effects:
   1105  *	Tokens are, of course, consumed.
   1106  *
   1107  *-----------------------------------------------------------------------
   1108  */
   1109 static Token
   1110 CondE(Boolean doEval)
   1111 {
   1112     Token   l, o;
   1113 
   1114     l = CondF(doEval);
   1115     if (l != TOK_ERROR) {
   1116 	o = CondToken(doEval);
   1117 
   1118 	if (o == TOK_OR) {
   1119 	    /*
   1120 	     * E -> F || E
   1121 	     *
   1122 	     * A similar thing occurs for ||, except that here we make sure
   1123 	     * the l.h.s. is TOK_FALSE before we bother to evaluate the r.h.s.
   1124 	     * Once again, if l is TOK_FALSE, the result is the r.h.s. and once
   1125 	     * again if l is TOK_TRUE, we parse the r.h.s. to throw it away.
   1126 	     */
   1127 	    if (l == TOK_FALSE) {
   1128 		l = CondE(doEval);
   1129 	    } else {
   1130 		(void)CondE(FALSE);
   1131 	    }
   1132 	} else {
   1133 	    /*
   1134 	     * E -> F
   1135 	     */
   1136 	    CondPushBack(o);
   1137 	}
   1138     }
   1139     return l;
   1140 }
   1141 
   1142 /*-
   1143  *-----------------------------------------------------------------------
   1144  * Cond_EvalExpression --
   1145  *	Evaluate an expression in the passed line. The expression
   1146  *	consists of &&, ||, !, make(target), defined(variable)
   1147  *	and parenthetical groupings thereof.
   1148  *
   1149  * Results:
   1150  *	COND_PARSE	if the condition was valid grammatically
   1151  *	COND_INVALID  	if not a valid conditional.
   1152  *
   1153  *	(*value) is set to the boolean value of the condition
   1154  *
   1155  * Side Effects:
   1156  *	None.
   1157  *
   1158  *-----------------------------------------------------------------------
   1159  */
   1160 int
   1161 Cond_EvalExpression(const struct If *info, char *line, Boolean *value, int eprint, Boolean strictLHS)
   1162 {
   1163     static const struct If *dflt_info;
   1164     const struct If *sv_if_info = if_info;
   1165     char *sv_condExpr = condExpr;
   1166     Token sv_condPushBack = condPushBack;
   1167     int rval;
   1168 
   1169     lhsStrict = strictLHS;
   1170 
   1171     while (*line == ' ' || *line == '\t')
   1172 	line++;
   1173 
   1174     if (info == NULL && (info = dflt_info) == NULL) {
   1175 	/* Scan for the entry for .if - it can't be first */
   1176 	for (info = ifs; ; info++)
   1177 	    if (info->form[0] == 0)
   1178 		break;
   1179 	dflt_info = info;
   1180     }
   1181     assert(info != NULL);
   1182 
   1183     if_info = info;
   1184     condExpr = line;
   1185     condPushBack = TOK_NONE;
   1186 
   1187     rval = do_Cond_EvalExpression(value);
   1188 
   1189     if (rval == COND_INVALID && eprint)
   1190 	Parse_Error(PARSE_FATAL, "Malformed conditional (%s)", line);
   1191 
   1192     if_info = sv_if_info;
   1193     condExpr = sv_condExpr;
   1194     condPushBack = sv_condPushBack;
   1195 
   1196     return rval;
   1197 }
   1198 
   1199 static int
   1200 do_Cond_EvalExpression(Boolean *value)
   1201 {
   1202 
   1203     switch (CondE(TRUE)) {
   1204     case TOK_TRUE:
   1205 	if (CondToken(TRUE) == TOK_EOF) {
   1206 	    *value = TRUE;
   1207 	    return COND_PARSE;
   1208 	}
   1209 	break;
   1210     case TOK_FALSE:
   1211 	if (CondToken(TRUE) == TOK_EOF) {
   1212 	    *value = FALSE;
   1213 	    return COND_PARSE;
   1214 	}
   1215 	break;
   1216     default:
   1217     case TOK_ERROR:
   1218 	break;
   1219     }
   1220 
   1221     return COND_INVALID;
   1222 }
   1223 
   1224 
   1225 /*-
   1227  *-----------------------------------------------------------------------
   1228  * Cond_Eval --
   1229  *	Evaluate the conditional in the passed line. The line
   1230  *	looks like this:
   1231  *	    .<cond-type> <expr>
   1232  *	where <cond-type> is any of if, ifmake, ifnmake, ifdef,
   1233  *	ifndef, elif, elifmake, elifnmake, elifdef, elifndef
   1234  *	and <expr> consists of &&, ||, !, make(target), defined(variable)
   1235  *	and parenthetical groupings thereof.
   1236  *
   1237  * Input:
   1238  *	line		Line to parse
   1239  *
   1240  * Results:
   1241  *	COND_PARSE	if should parse lines after the conditional
   1242  *	COND_SKIP	if should skip lines after the conditional
   1243  *	COND_INVALID  	if not a valid conditional.
   1244  *
   1245  * Side Effects:
   1246  *	None.
   1247  *
   1248  * Note that the states IF_ACTIVE and ELSE_ACTIVE are only different in order
   1249  * to detect splurious .else lines (as are SKIP_TO_ELSE and SKIP_TO_ENDIF)
   1250  * otherwise .else could be treated as '.elif 1'.
   1251  *
   1252  *-----------------------------------------------------------------------
   1253  */
   1254 int
   1255 Cond_Eval(char *line)
   1256 {
   1257 #define	    MAXIF      128	/* maximum depth of .if'ing */
   1258 #define	    MAXIF_BUMP  32	/* how much to grow by */
   1259     enum if_states {
   1260 	IF_ACTIVE,		/* .if or .elif part active */
   1261 	ELSE_ACTIVE,		/* .else part active */
   1262 	SEARCH_FOR_ELIF,	/* searching for .elif/else to execute */
   1263 	SKIP_TO_ELSE,           /* has been true, but not seen '.else' */
   1264 	SKIP_TO_ENDIF		/* nothing else to execute */
   1265     };
   1266     static enum if_states *cond_state = NULL;
   1267     static unsigned int max_if_depth = MAXIF;
   1268 
   1269     const struct If *ifp;
   1270     Boolean 	    isElif;
   1271     Boolean 	    value;
   1272     int	    	    level;  	/* Level at which to report errors. */
   1273     enum if_states  state;
   1274 
   1275     level = PARSE_FATAL;
   1276     if (!cond_state) {
   1277 	cond_state = bmake_malloc(max_if_depth * sizeof(*cond_state));
   1278 	cond_state[0] = IF_ACTIVE;
   1279     }
   1280     /* skip leading character (the '.') and any whitespace */
   1281     for (line++; *line == ' ' || *line == '\t'; line++)
   1282 	continue;
   1283 
   1284     /* Find what type of if we're dealing with.  */
   1285     if (line[0] == 'e') {
   1286 	if (line[1] != 'l') {
   1287 	    if (!istoken(line + 1, "ndif", 4))
   1288 		return COND_INVALID;
   1289 	    /* End of conditional section */
   1290 	    if (cond_depth == cond_min_depth) {
   1291 		Parse_Error(level, "if-less endif");
   1292 		return COND_PARSE;
   1293 	    }
   1294 	    /* Return state for previous conditional */
   1295 	    cond_depth--;
   1296 	    return cond_state[cond_depth] <= ELSE_ACTIVE ? COND_PARSE : COND_SKIP;
   1297 	}
   1298 
   1299 	/* Quite likely this is 'else' or 'elif' */
   1300 	line += 2;
   1301 	if (istoken(line, "se", 2)) {
   1302 	    /* It is else... */
   1303 	    if (cond_depth == cond_min_depth) {
   1304 		Parse_Error(level, "if-less else");
   1305 		return COND_PARSE;
   1306 	    }
   1307 
   1308 	    state = cond_state[cond_depth];
   1309 	    switch (state) {
   1310 	    case SEARCH_FOR_ELIF:
   1311 		state = ELSE_ACTIVE;
   1312 		break;
   1313 	    case ELSE_ACTIVE:
   1314 	    case SKIP_TO_ENDIF:
   1315 		Parse_Error(PARSE_WARNING, "extra else");
   1316 		/* FALLTHROUGH */
   1317 	    default:
   1318 	    case IF_ACTIVE:
   1319 	    case SKIP_TO_ELSE:
   1320 		state = SKIP_TO_ENDIF;
   1321 		break;
   1322 	    }
   1323 	    cond_state[cond_depth] = state;
   1324 	    return state <= ELSE_ACTIVE ? COND_PARSE : COND_SKIP;
   1325 	}
   1326 	/* Assume for now it is an elif */
   1327 	isElif = TRUE;
   1328     } else
   1329 	isElif = FALSE;
   1330 
   1331     if (line[0] != 'i' || line[1] != 'f')
   1332 	/* Not an ifxxx or elifxxx line */
   1333 	return COND_INVALID;
   1334 
   1335     /*
   1336      * Figure out what sort of conditional it is -- what its default
   1337      * function is, etc. -- by looking in the table of valid "ifs"
   1338      */
   1339     line += 2;
   1340     for (ifp = ifs; ; ifp++) {
   1341 	if (ifp->form == NULL)
   1342 	    return COND_INVALID;
   1343 	if (istoken(ifp->form, line, ifp->formlen)) {
   1344 	    line += ifp->formlen;
   1345 	    break;
   1346 	}
   1347     }
   1348 
   1349     /* Now we know what sort of 'if' it is... */
   1350 
   1351     if (isElif) {
   1352 	if (cond_depth == cond_min_depth) {
   1353 	    Parse_Error(level, "if-less elif");
   1354 	    return COND_PARSE;
   1355 	}
   1356 	state = cond_state[cond_depth];
   1357 	if (state == SKIP_TO_ENDIF || state == ELSE_ACTIVE) {
   1358 	    Parse_Error(PARSE_WARNING, "extra elif");
   1359 	    cond_state[cond_depth] = SKIP_TO_ENDIF;
   1360 	    return COND_SKIP;
   1361 	}
   1362 	if (state != SEARCH_FOR_ELIF) {
   1363 	    /* Either just finished the 'true' block, or already SKIP_TO_ELSE */
   1364 	    cond_state[cond_depth] = SKIP_TO_ELSE;
   1365 	    return COND_SKIP;
   1366 	}
   1367     } else {
   1368 	/* Normal .if */
   1369 	if (cond_depth + 1 >= max_if_depth) {
   1370 	    /*
   1371 	     * This is rare, but not impossible.
   1372 	     * In meta mode, dirdeps.mk (only runs at level 0)
   1373 	     * can need more than the default.
   1374 	     */
   1375 	    max_if_depth += MAXIF_BUMP;
   1376 	    cond_state = bmake_realloc(cond_state, max_if_depth *
   1377 		sizeof(*cond_state));
   1378 	}
   1379 	state = cond_state[cond_depth];
   1380 	cond_depth++;
   1381 	if (state > ELSE_ACTIVE) {
   1382 	    /* If we aren't parsing the data, treat as always false */
   1383 	    cond_state[cond_depth] = SKIP_TO_ELSE;
   1384 	    return COND_SKIP;
   1385 	}
   1386     }
   1387 
   1388     /* And evaluate the conditional expresssion */
   1389     if (Cond_EvalExpression(ifp, line, &value, 1, TRUE) == COND_INVALID) {
   1390 	/* Syntax error in conditional, error message already output. */
   1391 	/* Skip everything to matching .endif */
   1392 	cond_state[cond_depth] = SKIP_TO_ELSE;
   1393 	return COND_SKIP;
   1394     }
   1395 
   1396     if (!value) {
   1397 	cond_state[cond_depth] = SEARCH_FOR_ELIF;
   1398 	return COND_SKIP;
   1399     }
   1400     cond_state[cond_depth] = IF_ACTIVE;
   1401     return COND_PARSE;
   1402 }
   1403 
   1404 
   1405 
   1406 /*-
   1408  *-----------------------------------------------------------------------
   1409  * Cond_End --
   1410  *	Make sure everything's clean at the end of a makefile.
   1411  *
   1412  * Results:
   1413  *	None.
   1414  *
   1415  * Side Effects:
   1416  *	Parse_Error will be called if open conditionals are around.
   1417  *
   1418  *-----------------------------------------------------------------------
   1419  */
   1420 void
   1421 Cond_restore_depth(unsigned int saved_depth)
   1422 {
   1423     int open_conds = cond_depth - cond_min_depth;
   1424 
   1425     if (open_conds != 0 || saved_depth > cond_depth) {
   1426 	Parse_Error(PARSE_FATAL, "%d open conditional%s", open_conds,
   1427 		    open_conds == 1 ? "" : "s");
   1428 	cond_depth = cond_min_depth;
   1429     }
   1430 
   1431     cond_min_depth = saved_depth;
   1432 }
   1433 
   1434 unsigned int
   1435 Cond_save_depth(void)
   1436 {
   1437     int depth = cond_min_depth;
   1438 
   1439     cond_min_depth = cond_depth;
   1440     return depth;
   1441 }
   1442