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