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