Home | History | Annotate | Line # | Download | only in make
cond.c revision 1.72
      1 /*	$NetBSD: cond.c,v 1.72 2016/01/09 00:55:17 christos 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.72 2016/01/09 00:55:17 christos 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.72 2016/01/09 00:55:17 christos 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, TRUE, FALSE, &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     if (!*str) {
    501 	*value = (double)0;
    502 	return TRUE;
    503     }
    504     l_val = strtoul(str, &eptr, str[1] == 'x' ? 16 : 10);
    505     ech = *eptr;
    506     if (ech == 0 && errno != ERANGE) {
    507 	d_val = str[0] == '-' ? -(double)-l_val : (double)l_val;
    508     } else {
    509 	if (ech != 0 && ech != '.' && ech != 'e' && ech != 'E')
    510 	    return FALSE;
    511 	d_val = strtod(str, &eptr);
    512 	if (*eptr)
    513 	    return FALSE;
    514     }
    515 
    516     *value = d_val;
    517     return TRUE;
    518 }
    519 
    520 /*-
    521  *-----------------------------------------------------------------------
    522  * CondGetString --
    523  *	Get a string from a variable reference or an optionally quoted
    524  *	string.  This is called for the lhs and rhs of string compares.
    525  *
    526  * Results:
    527  *	Sets freeIt if needed,
    528  *	Sets quoted if string was quoted,
    529  *	Returns NULL on error,
    530  *	else returns string - absent any quotes.
    531  *
    532  * Side Effects:
    533  *	Moves condExpr to end of this token.
    534  *
    535  *
    536  *-----------------------------------------------------------------------
    537  */
    538 /* coverity:[+alloc : arg-*2] */
    539 static char *
    540 CondGetString(Boolean doEval, Boolean *quoted, void **freeIt, Boolean strictLHS)
    541 {
    542     Buffer buf;
    543     char *cp;
    544     char *str;
    545     int	len;
    546     int qt;
    547     char *start;
    548 
    549     Buf_Init(&buf, 0);
    550     str = NULL;
    551     *freeIt = NULL;
    552     *quoted = qt = *condExpr == '"' ? 1 : 0;
    553     if (qt)
    554 	condExpr++;
    555     for (start = condExpr; *condExpr && str == NULL; condExpr++) {
    556 	switch (*condExpr) {
    557 	case '\\':
    558 	    if (condExpr[1] != '\0') {
    559 		condExpr++;
    560 		Buf_AddByte(&buf, *condExpr);
    561 	    }
    562 	    break;
    563 	case '"':
    564 	    if (qt) {
    565 		condExpr++;		/* we don't want the quotes */
    566 		goto got_str;
    567 	    } else
    568 		Buf_AddByte(&buf, *condExpr); /* likely? */
    569 	    break;
    570 	case ')':
    571 	case '!':
    572 	case '=':
    573 	case '>':
    574 	case '<':
    575 	case ' ':
    576 	case '\t':
    577 	    if (!qt)
    578 		goto got_str;
    579 	    else
    580 		Buf_AddByte(&buf, *condExpr);
    581 	    break;
    582 	case '$':
    583 	    /* if we are in quotes, then an undefined variable is ok */
    584 	    str = Var_Parse(condExpr, VAR_CMD, (qt ? 0 : doEval),
    585 			    TRUE, FALSE, &len, freeIt);
    586 	    if (str == var_Error) {
    587 		if (*freeIt) {
    588 		    free(*freeIt);
    589 		    *freeIt = NULL;
    590 		}
    591 		/*
    592 		 * Even if !doEval, we still report syntax errors, which
    593 		 * is what getting var_Error back with !doEval means.
    594 		 */
    595 		str = NULL;
    596 		goto cleanup;
    597 	    }
    598 	    condExpr += len;
    599 	    /*
    600 	     * If the '$' was first char (no quotes), and we are
    601 	     * followed by space, the operator or end of expression,
    602 	     * we are done.
    603 	     */
    604 	    if ((condExpr == start + len) &&
    605 		(*condExpr == '\0' ||
    606 		 isspace((unsigned char) *condExpr) ||
    607 		 strchr("!=><)", *condExpr))) {
    608 		goto cleanup;
    609 	    }
    610 	    /*
    611 	     * Nope, we better copy str to buf
    612 	     */
    613 	    for (cp = str; *cp; cp++) {
    614 		Buf_AddByte(&buf, *cp);
    615 	    }
    616 	    if (*freeIt) {
    617 		free(*freeIt);
    618 		*freeIt = NULL;
    619 	    }
    620 	    str = NULL;			/* not finished yet */
    621 	    condExpr--;			/* don't skip over next char */
    622 	    break;
    623 	default:
    624 	    if (strictLHS && !qt && *start != '$' &&
    625 		!isdigit((unsigned char) *start)) {
    626 		/* lhs must be quoted, a variable reference or number */
    627 		if (*freeIt) {
    628 		    free(*freeIt);
    629 		    *freeIt = NULL;
    630 		}
    631 		str = NULL;
    632 		goto cleanup;
    633 	    }
    634 	    Buf_AddByte(&buf, *condExpr);
    635 	    break;
    636 	}
    637     }
    638  got_str:
    639     str = Buf_GetAll(&buf, NULL);
    640     *freeIt = str;
    641  cleanup:
    642     Buf_Destroy(&buf, FALSE);
    643     return str;
    644 }
    645 
    646 /*-
    648  *-----------------------------------------------------------------------
    649  * CondToken --
    650  *	Return the next token from the input.
    651  *
    652  * Results:
    653  *	A Token for the next lexical token in the stream.
    654  *
    655  * Side Effects:
    656  *	condPushback will be set back to TOK_NONE if it is used.
    657  *
    658  *-----------------------------------------------------------------------
    659  */
    660 static Token
    661 compare_expression(Boolean doEval)
    662 {
    663     Token	t;
    664     char	*lhs;
    665     char	*rhs;
    666     char	*op;
    667     void	*lhsFree;
    668     void	*rhsFree;
    669     Boolean lhsQuoted;
    670     Boolean rhsQuoted;
    671     double  	left, right;
    672 
    673     t = TOK_ERROR;
    674     rhs = NULL;
    675     lhsFree = rhsFree = FALSE;
    676     lhsQuoted = rhsQuoted = FALSE;
    677 
    678     /*
    679      * Parse the variable spec and skip over it, saving its
    680      * value in lhs.
    681      */
    682     lhs = CondGetString(doEval, &lhsQuoted, &lhsFree, lhsStrict);
    683     if (!lhs)
    684 	goto done;
    685 
    686     /*
    687      * Skip whitespace to get to the operator
    688      */
    689     while (isspace((unsigned char) *condExpr))
    690 	condExpr++;
    691 
    692     /*
    693      * Make sure the operator is a valid one. If it isn't a
    694      * known relational operator, pretend we got a
    695      * != 0 comparison.
    696      */
    697     op = condExpr;
    698     switch (*condExpr) {
    699 	case '!':
    700 	case '=':
    701 	case '<':
    702 	case '>':
    703 	    if (condExpr[1] == '=') {
    704 		condExpr += 2;
    705 	    } else {
    706 		condExpr += 1;
    707 	    }
    708 	    break;
    709 	default:
    710 	    if (!doEval) {
    711 		t = TOK_FALSE;
    712 		goto done;
    713 	    }
    714 	    /* For .ifxxx "..." check for non-empty string. */
    715 	    if (lhsQuoted) {
    716 		t = lhs[0] != 0;
    717 		goto done;
    718 	    }
    719 	    /* For .ifxxx <number> compare against zero */
    720 	    if (CondCvtArg(lhs, &left)) {
    721 		t = left != 0.0;
    722 		goto done;
    723 	    }
    724 	    /* For .if ${...} check for non-empty string (defProc is ifdef). */
    725 	    if (if_info->form[0] == 0) {
    726 		t = lhs[0] != 0;
    727 		goto done;
    728 	    }
    729 	    /* Otherwise action default test ... */
    730 	    t = if_info->defProc(strlen(lhs), lhs) != if_info->doNot;
    731 	    goto done;
    732     }
    733 
    734     while (isspace((unsigned char)*condExpr))
    735 	condExpr++;
    736 
    737     if (*condExpr == '\0') {
    738 	Parse_Error(PARSE_WARNING,
    739 		    "Missing right-hand-side of operator");
    740 	goto done;
    741     }
    742 
    743     rhs = CondGetString(doEval, &rhsQuoted, &rhsFree, FALSE);
    744     if (!rhs)
    745 	goto done;
    746 
    747     if (rhsQuoted || lhsQuoted) {
    748 do_string_compare:
    749 	if (((*op != '!') && (*op != '=')) || (op[1] != '=')) {
    750 	    Parse_Error(PARSE_WARNING,
    751     "String comparison operator should be either == or !=");
    752 	    goto done;
    753 	}
    754 
    755 	if (DEBUG(COND)) {
    756 	    fprintf(debug_file, "lhs = \"%s\", rhs = \"%s\", op = %.2s\n",
    757 		   lhs, rhs, op);
    758 	}
    759 	/*
    760 	 * Null-terminate rhs and perform the comparison.
    761 	 * t is set to the result.
    762 	 */
    763 	if (*op == '=') {
    764 	    t = strcmp(lhs, rhs) == 0;
    765 	} else {
    766 	    t = strcmp(lhs, rhs) != 0;
    767 	}
    768     } else {
    769 	/*
    770 	 * rhs is either a float or an integer. Convert both the
    771 	 * lhs and the rhs to a double and compare the two.
    772 	 */
    773 
    774 	if (!CondCvtArg(lhs, &left) || !CondCvtArg(rhs, &right))
    775 	    goto do_string_compare;
    776 
    777 	if (DEBUG(COND)) {
    778 	    fprintf(debug_file, "left = %f, right = %f, op = %.2s\n", left,
    779 		   right, op);
    780 	}
    781 	switch(op[0]) {
    782 	case '!':
    783 	    if (op[1] != '=') {
    784 		Parse_Error(PARSE_WARNING,
    785 			    "Unknown operator");
    786 		goto done;
    787 	    }
    788 	    t = (left != right);
    789 	    break;
    790 	case '=':
    791 	    if (op[1] != '=') {
    792 		Parse_Error(PARSE_WARNING,
    793 			    "Unknown operator");
    794 		goto done;
    795 	    }
    796 	    t = (left == right);
    797 	    break;
    798 	case '<':
    799 	    if (op[1] == '=') {
    800 		t = (left <= right);
    801 	    } else {
    802 		t = (left < right);
    803 	    }
    804 	    break;
    805 	case '>':
    806 	    if (op[1] == '=') {
    807 		t = (left >= right);
    808 	    } else {
    809 		t = (left > right);
    810 	    }
    811 	    break;
    812 	}
    813     }
    814 
    815 done:
    816     if (lhsFree)
    817 	free(lhsFree);
    818     if (rhsFree)
    819 	free(rhsFree);
    820     return t;
    821 }
    822 
    823 static int
    824 get_mpt_arg(char **linePtr, char **argPtr, const char *func MAKE_ATTR_UNUSED)
    825 {
    826     /*
    827      * Use Var_Parse to parse the spec in parens and return
    828      * TOK_TRUE if the resulting string is empty.
    829      */
    830     int	    length;
    831     void    *freeIt;
    832     char    *val;
    833     char    *cp = *linePtr;
    834 
    835     /* We do all the work here and return the result as the length */
    836     *argPtr = NULL;
    837 
    838     val = Var_Parse(cp - 1, VAR_CMD, FALSE, TRUE, FALSE, &length, &freeIt);
    839     /*
    840      * Advance *linePtr to beyond the closing ). Note that
    841      * we subtract one because 'length' is calculated from 'cp - 1'.
    842      */
    843     *linePtr = cp - 1 + length;
    844 
    845     if (val == var_Error) {
    846 	free(freeIt);
    847 	return -1;
    848     }
    849 
    850     /* A variable is empty when it just contains spaces... 4/15/92, christos */
    851     while (isspace(*(unsigned char *)val))
    852 	val++;
    853 
    854     /*
    855      * For consistency with the other functions we can't generate the
    856      * true/false here.
    857      */
    858     length = *val ? 2 : 1;
    859     if (freeIt)
    860 	free(freeIt);
    861     return length;
    862 }
    863 
    864 static Boolean
    865 CondDoEmpty(int arglen, const char *arg MAKE_ATTR_UNUSED)
    866 {
    867     return arglen == 1;
    868 }
    869 
    870 static Token
    871 compare_function(Boolean doEval)
    872 {
    873     static const struct fn_def {
    874 	const char  *fn_name;
    875 	int         fn_name_len;
    876         int         (*fn_getarg)(char **, char **, const char *);
    877 	Boolean     (*fn_proc)(int, const char *);
    878     } fn_defs[] = {
    879 	{ "defined",   7, CondGetArg, CondDoDefined },
    880 	{ "make",      4, CondGetArg, CondDoMake },
    881 	{ "exists",    6, CondGetArg, CondDoExists },
    882 	{ "empty",     5, get_mpt_arg, CondDoEmpty },
    883 	{ "target",    6, CondGetArg, CondDoTarget },
    884 	{ "commands",  8, CondGetArg, CondDoCommands },
    885 	{ NULL,        0, NULL, NULL },
    886     };
    887     const struct fn_def *fn_def;
    888     Token	t;
    889     char	*arg = NULL;
    890     int	arglen;
    891     char *cp = condExpr;
    892     char *cp1;
    893 
    894     for (fn_def = fn_defs; fn_def->fn_name != NULL; fn_def++) {
    895 	if (!istoken(cp, fn_def->fn_name, fn_def->fn_name_len))
    896 	    continue;
    897 	cp += fn_def->fn_name_len;
    898 	/* There can only be whitespace before the '(' */
    899 	while (isspace(*(unsigned char *)cp))
    900 	    cp++;
    901 	if (*cp != '(')
    902 	    break;
    903 
    904 	arglen = fn_def->fn_getarg(&cp, &arg, fn_def->fn_name);
    905 	if (arglen <= 0) {
    906 	    condExpr = cp;
    907 	    return arglen < 0 ? TOK_ERROR : TOK_FALSE;
    908 	}
    909 	/* Evaluate the argument using the required function. */
    910 	t = !doEval || fn_def->fn_proc(arglen, arg);
    911 	if (arg)
    912 	    free(arg);
    913 	condExpr = cp;
    914 	return t;
    915     }
    916 
    917     /* Push anything numeric through the compare expression */
    918     cp = condExpr;
    919     if (isdigit((unsigned char)cp[0]) || strchr("+-", cp[0]))
    920 	return compare_expression(doEval);
    921 
    922     /*
    923      * Most likely we have a naked token to apply the default function to.
    924      * However ".if a == b" gets here when the "a" is unquoted and doesn't
    925      * start with a '$'. This surprises people.
    926      * If what follows the function argument is a '=' or '!' then the syntax
    927      * would be invalid if we did "defined(a)" - so instead treat as an
    928      * expression.
    929      */
    930     arglen = CondGetArg(&cp, &arg, NULL);
    931     for (cp1 = cp; isspace(*(unsigned char *)cp1); cp1++)
    932 	continue;
    933     if (*cp1 == '=' || *cp1 == '!')
    934 	return compare_expression(doEval);
    935     condExpr = cp;
    936 
    937     /*
    938      * Evaluate the argument using the default function.
    939      * This path always treats .if as .ifdef. To get here the character
    940      * after .if must have been taken literally, so the argument cannot
    941      * be empty - even if it contained a variable expansion.
    942      */
    943     t = !doEval || if_info->defProc(arglen, arg) != if_info->doNot;
    944     if (arg)
    945 	free(arg);
    946     return t;
    947 }
    948 
    949 static Token
    950 CondToken(Boolean doEval)
    951 {
    952     Token t;
    953 
    954     t = condPushBack;
    955     if (t != TOK_NONE) {
    956 	condPushBack = TOK_NONE;
    957 	return t;
    958     }
    959 
    960     while (*condExpr == ' ' || *condExpr == '\t') {
    961 	condExpr++;
    962     }
    963 
    964     switch (*condExpr) {
    965 
    966     case '(':
    967 	condExpr++;
    968 	return TOK_LPAREN;
    969 
    970     case ')':
    971 	condExpr++;
    972 	return TOK_RPAREN;
    973 
    974     case '|':
    975 	if (condExpr[1] == '|') {
    976 	    condExpr++;
    977 	}
    978 	condExpr++;
    979 	return TOK_OR;
    980 
    981     case '&':
    982 	if (condExpr[1] == '&') {
    983 	    condExpr++;
    984 	}
    985 	condExpr++;
    986 	return TOK_AND;
    987 
    988     case '!':
    989 	condExpr++;
    990 	return TOK_NOT;
    991 
    992     case '#':
    993     case '\n':
    994     case '\0':
    995 	return TOK_EOF;
    996 
    997     case '"':
    998     case '$':
    999 	return compare_expression(doEval);
   1000 
   1001     default:
   1002 	return compare_function(doEval);
   1003     }
   1004 }
   1005 
   1006 /*-
   1007  *-----------------------------------------------------------------------
   1008  * CondT --
   1009  *	Parse a single term in the expression. This consists of a terminal
   1010  *	symbol or TOK_NOT and a terminal symbol (not including the binary
   1011  *	operators):
   1012  *	    T -> defined(variable) | make(target) | exists(file) | symbol
   1013  *	    T -> ! T | ( E )
   1014  *
   1015  * Results:
   1016  *	TOK_TRUE, TOK_FALSE or TOK_ERROR.
   1017  *
   1018  * Side Effects:
   1019  *	Tokens are consumed.
   1020  *
   1021  *-----------------------------------------------------------------------
   1022  */
   1023 static Token
   1024 CondT(Boolean doEval)
   1025 {
   1026     Token   t;
   1027 
   1028     t = CondToken(doEval);
   1029 
   1030     if (t == TOK_EOF) {
   1031 	/*
   1032 	 * If we reached the end of the expression, the expression
   1033 	 * is malformed...
   1034 	 */
   1035 	t = TOK_ERROR;
   1036     } else if (t == TOK_LPAREN) {
   1037 	/*
   1038 	 * T -> ( E )
   1039 	 */
   1040 	t = CondE(doEval);
   1041 	if (t != TOK_ERROR) {
   1042 	    if (CondToken(doEval) != TOK_RPAREN) {
   1043 		t = TOK_ERROR;
   1044 	    }
   1045 	}
   1046     } else if (t == TOK_NOT) {
   1047 	t = CondT(doEval);
   1048 	if (t == TOK_TRUE) {
   1049 	    t = TOK_FALSE;
   1050 	} else if (t == TOK_FALSE) {
   1051 	    t = TOK_TRUE;
   1052 	}
   1053     }
   1054     return (t);
   1055 }
   1056 
   1057 /*-
   1059  *-----------------------------------------------------------------------
   1060  * CondF --
   1061  *	Parse a conjunctive factor (nice name, wot?)
   1062  *	    F -> T && F | T
   1063  *
   1064  * Results:
   1065  *	TOK_TRUE, TOK_FALSE or TOK_ERROR
   1066  *
   1067  * Side Effects:
   1068  *	Tokens are consumed.
   1069  *
   1070  *-----------------------------------------------------------------------
   1071  */
   1072 static Token
   1073 CondF(Boolean doEval)
   1074 {
   1075     Token   l, o;
   1076 
   1077     l = CondT(doEval);
   1078     if (l != TOK_ERROR) {
   1079 	o = CondToken(doEval);
   1080 
   1081 	if (o == TOK_AND) {
   1082 	    /*
   1083 	     * F -> T && F
   1084 	     *
   1085 	     * If T is TOK_FALSE, the whole thing will be TOK_FALSE, but we have to
   1086 	     * parse the r.h.s. anyway (to throw it away).
   1087 	     * If T is TOK_TRUE, the result is the r.h.s., be it an TOK_ERROR or no.
   1088 	     */
   1089 	    if (l == TOK_TRUE) {
   1090 		l = CondF(doEval);
   1091 	    } else {
   1092 		(void)CondF(FALSE);
   1093 	    }
   1094 	} else {
   1095 	    /*
   1096 	     * F -> T
   1097 	     */
   1098 	    CondPushBack(o);
   1099 	}
   1100     }
   1101     return (l);
   1102 }
   1103 
   1104 /*-
   1106  *-----------------------------------------------------------------------
   1107  * CondE --
   1108  *	Main expression production.
   1109  *	    E -> F || E | F
   1110  *
   1111  * Results:
   1112  *	TOK_TRUE, TOK_FALSE or TOK_ERROR.
   1113  *
   1114  * Side Effects:
   1115  *	Tokens are, of course, consumed.
   1116  *
   1117  *-----------------------------------------------------------------------
   1118  */
   1119 static Token
   1120 CondE(Boolean doEval)
   1121 {
   1122     Token   l, o;
   1123 
   1124     l = CondF(doEval);
   1125     if (l != TOK_ERROR) {
   1126 	o = CondToken(doEval);
   1127 
   1128 	if (o == TOK_OR) {
   1129 	    /*
   1130 	     * E -> F || E
   1131 	     *
   1132 	     * A similar thing occurs for ||, except that here we make sure
   1133 	     * the l.h.s. is TOK_FALSE before we bother to evaluate the r.h.s.
   1134 	     * Once again, if l is TOK_FALSE, the result is the r.h.s. and once
   1135 	     * again if l is TOK_TRUE, we parse the r.h.s. to throw it away.
   1136 	     */
   1137 	    if (l == TOK_FALSE) {
   1138 		l = CondE(doEval);
   1139 	    } else {
   1140 		(void)CondE(FALSE);
   1141 	    }
   1142 	} else {
   1143 	    /*
   1144 	     * E -> F
   1145 	     */
   1146 	    CondPushBack(o);
   1147 	}
   1148     }
   1149     return (l);
   1150 }
   1151 
   1152 /*-
   1153  *-----------------------------------------------------------------------
   1154  * Cond_EvalExpression --
   1155  *	Evaluate an expression in the passed line. The expression
   1156  *	consists of &&, ||, !, make(target), defined(variable)
   1157  *	and parenthetical groupings thereof.
   1158  *
   1159  * Results:
   1160  *	COND_PARSE	if the condition was valid grammatically
   1161  *	COND_INVALID  	if not a valid conditional.
   1162  *
   1163  *	(*value) is set to the boolean value of the condition
   1164  *
   1165  * Side Effects:
   1166  *	None.
   1167  *
   1168  *-----------------------------------------------------------------------
   1169  */
   1170 int
   1171 Cond_EvalExpression(const struct If *info, char *line, Boolean *value, int eprint, Boolean strictLHS)
   1172 {
   1173     static const struct If *dflt_info;
   1174     const struct If *sv_if_info = if_info;
   1175     char *sv_condExpr = condExpr;
   1176     Token sv_condPushBack = condPushBack;
   1177     int rval;
   1178 
   1179     lhsStrict = strictLHS;
   1180 
   1181     while (*line == ' ' || *line == '\t')
   1182 	line++;
   1183 
   1184     if (info == NULL && (info = dflt_info) == NULL) {
   1185 	/* Scan for the entry for .if - it can't be first */
   1186 	for (info = ifs; ; info++)
   1187 	    if (info->form[0] == 0)
   1188 		break;
   1189 	dflt_info = info;
   1190     }
   1191 
   1192     if_info = info != NULL ? info : ifs + 4;
   1193     condExpr = line;
   1194     condPushBack = TOK_NONE;
   1195 
   1196     rval = do_Cond_EvalExpression(value);
   1197 
   1198     if (rval == COND_INVALID && eprint)
   1199 	Parse_Error(PARSE_FATAL, "Malformed conditional (%s)", line);
   1200 
   1201     if_info = sv_if_info;
   1202     condExpr = sv_condExpr;
   1203     condPushBack = sv_condPushBack;
   1204 
   1205     return rval;
   1206 }
   1207 
   1208 static int
   1209 do_Cond_EvalExpression(Boolean *value)
   1210 {
   1211 
   1212     switch (CondE(TRUE)) {
   1213     case TOK_TRUE:
   1214 	if (CondToken(TRUE) == TOK_EOF) {
   1215 	    *value = TRUE;
   1216 	    return COND_PARSE;
   1217 	}
   1218 	break;
   1219     case TOK_FALSE:
   1220 	if (CondToken(TRUE) == TOK_EOF) {
   1221 	    *value = FALSE;
   1222 	    return COND_PARSE;
   1223 	}
   1224 	break;
   1225     default:
   1226     case TOK_ERROR:
   1227 	break;
   1228     }
   1229 
   1230     return COND_INVALID;
   1231 }
   1232 
   1233 
   1234 /*-
   1236  *-----------------------------------------------------------------------
   1237  * Cond_Eval --
   1238  *	Evaluate the conditional in the passed line. The line
   1239  *	looks like this:
   1240  *	    .<cond-type> <expr>
   1241  *	where <cond-type> is any of if, ifmake, ifnmake, ifdef,
   1242  *	ifndef, elif, elifmake, elifnmake, elifdef, elifndef
   1243  *	and <expr> consists of &&, ||, !, make(target), defined(variable)
   1244  *	and parenthetical groupings thereof.
   1245  *
   1246  * Input:
   1247  *	line		Line to parse
   1248  *
   1249  * Results:
   1250  *	COND_PARSE	if should parse lines after the conditional
   1251  *	COND_SKIP	if should skip lines after the conditional
   1252  *	COND_INVALID  	if not a valid conditional.
   1253  *
   1254  * Side Effects:
   1255  *	None.
   1256  *
   1257  * Note that the states IF_ACTIVE and ELSE_ACTIVE are only different in order
   1258  * to detect splurious .else lines (as are SKIP_TO_ELSE and SKIP_TO_ENDIF)
   1259  * otherwise .else could be treated as '.elif 1'.
   1260  *
   1261  *-----------------------------------------------------------------------
   1262  */
   1263 int
   1264 Cond_Eval(char *line)
   1265 {
   1266 #define	    MAXIF      128	/* maximum depth of .if'ing */
   1267 #define	    MAXIF_BUMP  32	/* how much to grow by */
   1268     enum if_states {
   1269 	IF_ACTIVE,		/* .if or .elif part active */
   1270 	ELSE_ACTIVE,		/* .else part active */
   1271 	SEARCH_FOR_ELIF,	/* searching for .elif/else to execute */
   1272 	SKIP_TO_ELSE,           /* has been true, but not seen '.else' */
   1273 	SKIP_TO_ENDIF		/* nothing else to execute */
   1274     };
   1275     static enum if_states *cond_state = NULL;
   1276     static unsigned int max_if_depth = MAXIF;
   1277 
   1278     const struct If *ifp;
   1279     Boolean 	    isElif;
   1280     Boolean 	    value;
   1281     int	    	    level;  	/* Level at which to report errors. */
   1282     enum if_states  state;
   1283 
   1284     level = PARSE_FATAL;
   1285     if (!cond_state) {
   1286 	cond_state = bmake_malloc(max_if_depth * sizeof(*cond_state));
   1287 	cond_state[0] = IF_ACTIVE;
   1288     }
   1289     /* skip leading character (the '.') and any whitespace */
   1290     for (line++; *line == ' ' || *line == '\t'; line++)
   1291 	continue;
   1292 
   1293     /* Find what type of if we're dealing with.  */
   1294     if (line[0] == 'e') {
   1295 	if (line[1] != 'l') {
   1296 	    if (!istoken(line + 1, "ndif", 4))
   1297 		return COND_INVALID;
   1298 	    /* End of conditional section */
   1299 	    if (cond_depth == cond_min_depth) {
   1300 		Parse_Error(level, "if-less endif");
   1301 		return COND_PARSE;
   1302 	    }
   1303 	    /* Return state for previous conditional */
   1304 	    cond_depth--;
   1305 	    return cond_state[cond_depth] <= ELSE_ACTIVE ? COND_PARSE : COND_SKIP;
   1306 	}
   1307 
   1308 	/* Quite likely this is 'else' or 'elif' */
   1309 	line += 2;
   1310 	if (istoken(line, "se", 2)) {
   1311 	    /* It is else... */
   1312 	    if (cond_depth == cond_min_depth) {
   1313 		Parse_Error(level, "if-less else");
   1314 		return COND_PARSE;
   1315 	    }
   1316 
   1317 	    state = cond_state[cond_depth];
   1318 	    switch (state) {
   1319 	    case SEARCH_FOR_ELIF:
   1320 		state = ELSE_ACTIVE;
   1321 		break;
   1322 	    case ELSE_ACTIVE:
   1323 	    case SKIP_TO_ENDIF:
   1324 		Parse_Error(PARSE_WARNING, "extra else");
   1325 		/* FALLTHROUGH */
   1326 	    default:
   1327 	    case IF_ACTIVE:
   1328 	    case SKIP_TO_ELSE:
   1329 		state = SKIP_TO_ENDIF;
   1330 		break;
   1331 	    }
   1332 	    cond_state[cond_depth] = state;
   1333 	    return state <= ELSE_ACTIVE ? COND_PARSE : COND_SKIP;
   1334 	}
   1335 	/* Assume for now it is an elif */
   1336 	isElif = TRUE;
   1337     } else
   1338 	isElif = FALSE;
   1339 
   1340     if (line[0] != 'i' || line[1] != 'f')
   1341 	/* Not an ifxxx or elifxxx line */
   1342 	return COND_INVALID;
   1343 
   1344     /*
   1345      * Figure out what sort of conditional it is -- what its default
   1346      * function is, etc. -- by looking in the table of valid "ifs"
   1347      */
   1348     line += 2;
   1349     for (ifp = ifs; ; ifp++) {
   1350 	if (ifp->form == NULL)
   1351 	    return COND_INVALID;
   1352 	if (istoken(ifp->form, line, ifp->formlen)) {
   1353 	    line += ifp->formlen;
   1354 	    break;
   1355 	}
   1356     }
   1357 
   1358     /* Now we know what sort of 'if' it is... */
   1359 
   1360     if (isElif) {
   1361 	if (cond_depth == cond_min_depth) {
   1362 	    Parse_Error(level, "if-less elif");
   1363 	    return COND_PARSE;
   1364 	}
   1365 	state = cond_state[cond_depth];
   1366 	if (state == SKIP_TO_ENDIF || state == ELSE_ACTIVE) {
   1367 	    Parse_Error(PARSE_WARNING, "extra elif");
   1368 	    cond_state[cond_depth] = SKIP_TO_ENDIF;
   1369 	    return COND_SKIP;
   1370 	}
   1371 	if (state != SEARCH_FOR_ELIF) {
   1372 	    /* Either just finished the 'true' block, or already SKIP_TO_ELSE */
   1373 	    cond_state[cond_depth] = SKIP_TO_ELSE;
   1374 	    return COND_SKIP;
   1375 	}
   1376     } else {
   1377 	/* Normal .if */
   1378 	if (cond_depth + 1 >= max_if_depth) {
   1379 	    /*
   1380 	     * This is rare, but not impossible.
   1381 	     * In meta mode, dirdeps.mk (only runs at level 0)
   1382 	     * can need more than the default.
   1383 	     */
   1384 	    max_if_depth += MAXIF_BUMP;
   1385 	    cond_state = bmake_realloc(cond_state, max_if_depth *
   1386 		sizeof(*cond_state));
   1387 	}
   1388 	state = cond_state[cond_depth];
   1389 	cond_depth++;
   1390 	if (state > ELSE_ACTIVE) {
   1391 	    /* If we aren't parsing the data, treat as always false */
   1392 	    cond_state[cond_depth] = SKIP_TO_ELSE;
   1393 	    return COND_SKIP;
   1394 	}
   1395     }
   1396 
   1397     /* And evaluate the conditional expresssion */
   1398     if (Cond_EvalExpression(ifp, line, &value, 1, TRUE) == COND_INVALID) {
   1399 	/* Syntax error in conditional, error message already output. */
   1400 	/* Skip everything to matching .endif */
   1401 	cond_state[cond_depth] = SKIP_TO_ELSE;
   1402 	return COND_SKIP;
   1403     }
   1404 
   1405     if (!value) {
   1406 	cond_state[cond_depth] = SEARCH_FOR_ELIF;
   1407 	return COND_SKIP;
   1408     }
   1409     cond_state[cond_depth] = IF_ACTIVE;
   1410     return COND_PARSE;
   1411 }
   1412 
   1413 
   1414 
   1415 /*-
   1417  *-----------------------------------------------------------------------
   1418  * Cond_End --
   1419  *	Make sure everything's clean at the end of a makefile.
   1420  *
   1421  * Results:
   1422  *	None.
   1423  *
   1424  * Side Effects:
   1425  *	Parse_Error will be called if open conditionals are around.
   1426  *
   1427  *-----------------------------------------------------------------------
   1428  */
   1429 void
   1430 Cond_restore_depth(unsigned int saved_depth)
   1431 {
   1432     int open_conds = cond_depth - cond_min_depth;
   1433 
   1434     if (open_conds != 0 || saved_depth > cond_depth) {
   1435 	Parse_Error(PARSE_FATAL, "%d open conditional%s", open_conds,
   1436 		    open_conds == 1 ? "" : "s");
   1437 	cond_depth = cond_min_depth;
   1438     }
   1439 
   1440     cond_min_depth = saved_depth;
   1441 }
   1442 
   1443 unsigned int
   1444 Cond_save_depth(void)
   1445 {
   1446     int depth = cond_min_depth;
   1447 
   1448     cond_min_depth = cond_depth;
   1449     return depth;
   1450 }
   1451