Home | History | Annotate | Line # | Download | only in make
cond.c revision 1.119
      1 /*	$NetBSD: cond.c,v 1.119 2020/09/10 23:27:27 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1988, 1989 by Adam de Boor
     37  * Copyright (c) 1989 by Berkeley Softworks
     38  * All rights reserved.
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Adam de Boor.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  */
     71 
     72 #ifndef MAKE_NATIVE
     73 static char rcsid[] = "$NetBSD: cond.c,v 1.119 2020/09/10 23:27:27 rillig Exp $";
     74 #else
     75 #include <sys/cdefs.h>
     76 #ifndef lint
     77 #if 0
     78 static char sccsid[] = "@(#)cond.c	8.2 (Berkeley) 1/2/94";
     79 #else
     80 __RCSID("$NetBSD: cond.c,v 1.119 2020/09/10 23:27:27 rillig Exp $");
     81 #endif
     82 #endif /* not lint */
     83 #endif
     84 
     85 /* Handling of conditionals in a makefile.
     86  *
     87  * Interface:
     88  *	Cond_Eval 	Evaluate the conditional in the passed line.
     89  *
     90  *	Cond_EvalExpression
     91  *			Evaluate the conditional in the passed line, which
     92  *			is either the argument of one of the .if directives
     93  *			or the condition in a :?true:false variable modifier.
     94  *
     95  *	Cond_save_depth
     96  *	Cond_restore_depth
     97  *			Save and restore the nesting of the conditions, at
     98  *			the start and end of including another makefile, to
     99  *			ensure that in each makefile the conditional
    100  *			directives are well-balanced.
    101  */
    102 
    103 #include <errno.h>
    104 
    105 #include "make.h"
    106 #include "dir.h"
    107 
    108 /*
    109  * The parsing of conditional expressions is based on this grammar:
    110  *	E -> F || E
    111  *	E -> F
    112  *	F -> T && F
    113  *	F -> T
    114  *	T -> defined(variable)
    115  *	T -> make(target)
    116  *	T -> exists(file)
    117  *	T -> empty(varspec)
    118  *	T -> target(name)
    119  *	T -> commands(name)
    120  *	T -> symbol
    121  *	T -> $(varspec) op value
    122  *	T -> $(varspec) == "string"
    123  *	T -> $(varspec) != "string"
    124  *	T -> "string"
    125  *	T -> ( E )
    126  *	T -> ! T
    127  *	op -> == | != | > | < | >= | <=
    128  *
    129  * 'symbol' is some other symbol to which the default function is applied.
    130  *
    131  * Tokens are scanned from the lexer. The scanner (CondToken)
    132  * will return TOK_AND for '&' and '&&', TOK_OR for '|' and '||',
    133  * TOK_NOT for '!', TOK_LPAREN for '(', TOK_RPAREN for ')' and will evaluate
    134  * the other terminal symbols, using either the default function or the
    135  * function given in the terminal, and return the result as either TOK_TRUE
    136  * or TOK_FALSE.
    137  *
    138  * TOK_FALSE is 0 and TOK_TRUE 1 so we can directly assign C comparisons.
    139  *
    140  * All Non-Terminal functions (CondE, CondF and CondT) return TOK_ERROR on
    141  * error.
    142  */
    143 typedef enum {
    144     TOK_FALSE = 0, TOK_TRUE = 1, TOK_AND, TOK_OR, TOK_NOT,
    145     TOK_LPAREN, TOK_RPAREN, TOK_EOF, TOK_NONE, TOK_ERROR
    146 } Token;
    147 
    148 typedef struct {
    149     const struct If *if_info;	/* Info for current statement */
    150     const char *condExpr;	/* The expression to parse */
    151     Token curr;			/* Single push-back token used in parsing */
    152 } CondLexer;
    153 
    154 static Token CondE(CondLexer *lex, Boolean);
    155 static CondEvalResult do_Cond_EvalExpression(CondLexer *lex, Boolean *);
    156 
    157 static unsigned int cond_depth = 0;	/* current .if nesting level */
    158 static unsigned int cond_min_depth = 0;	/* depth at makefile open */
    159 
    160 /*
    161  * Indicate when we should be strict about lhs of comparisons.
    162  * In strict mode, the lhs must be a variable expression or a string literal
    163  * in quotes. In non-strict mode it may also be an unquoted string literal.
    164  *
    165  * TRUE when Cond_EvalExpression is called from Cond_Eval (.if etc)
    166  * FALSE when Cond_EvalExpression is called from var.c:ApplyModifiers
    167  * since lhs is already expanded and we cannot tell if
    168  * it was a variable reference or not.
    169  */
    170 static Boolean lhsStrict;
    171 
    172 static int
    173 istoken(const char *str, const char *tok, size_t len)
    174 {
    175     return strncmp(str, tok, len) == 0 && !isalpha((unsigned char)str[len]);
    176 }
    177 
    178 /* Push back the most recent token read. We only need one level of this. */
    179 static void
    180 CondLexer_PushBack(CondLexer *lex, Token t)
    181 {
    182     assert(lex->curr == TOK_NONE);
    183     assert(t != TOK_NONE);
    184 
    185     lex->curr = t;
    186 }
    187 
    188 static void
    189 CondLexer_SkipWhitespace(CondLexer *lex)
    190 {
    191     while (isspace((unsigned char)lex->condExpr[0]))
    192 	lex->condExpr++;
    193 }
    194 
    195 /* Parse the argument of a built-in function.
    196  *
    197  * Arguments:
    198  *	*linePtr initially points to the '(', upon successful return points
    199  *	beyond the ')'.
    200  *
    201  *	*out_arg receives the argument as string.
    202  *
    203  *	func says whether the argument belongs to an actual function, or
    204  *	whether the parsed argument is passed to the default function.
    205  *
    206  * Return the length of the argument. */
    207 static int
    208 ParseFuncArg(Boolean doEval, const char **linePtr, char **out_arg,
    209 	     const char *func) {
    210     const char *cp;
    211     Buffer buf;
    212     int paren_depth;
    213     char ch;
    214     size_t argLen;
    215 
    216     cp = *linePtr;
    217     if (func != NULL)
    218 	/* Skip opening '(' - verified by caller */
    219 	cp++;
    220 
    221     if (*cp == '\0') {
    222 	/*
    223 	 * No arguments whatsoever. Because 'make' and 'defined' aren't really
    224 	 * "reserved words", we don't print a message. I think this is better
    225 	 * than hitting the user with a warning message every time s/he uses
    226 	 * the word 'make' or 'defined' at the beginning of a symbol...
    227 	 */
    228 	*out_arg = NULL;
    229 	return 0;
    230     }
    231 
    232     while (*cp == ' ' || *cp == '\t') {
    233 	cp++;
    234     }
    235 
    236     /*
    237      * Create a buffer for the argument and start it out at 16 characters
    238      * long. Why 16? Why not?
    239      */
    240     Buf_Init(&buf, 16);
    241 
    242     paren_depth = 0;
    243     for (;;) {
    244 	ch = *cp;
    245 	if (ch == 0 || ch == ' ' || ch == '\t')
    246 	    break;
    247 	if ((ch == '&' || ch == '|') && paren_depth == 0)
    248 	    break;
    249 	if (*cp == '$') {
    250 	    /*
    251 	     * Parse the variable spec and install it as part of the argument
    252 	     * if it's valid. We tell Var_Parse to complain on an undefined
    253 	     * variable, so we don't need to do it. Nor do we return an error,
    254 	     * though perhaps we should...
    255 	     */
    256 	    void *freeIt;
    257 	    VarEvalFlags eflags = VARE_UNDEFERR | (doEval ? VARE_WANTRES : 0);
    258 	    const char *cp2 = Var_ParsePP(&cp, VAR_CMD, eflags, &freeIt);
    259 	    Buf_AddStr(&buf, cp2);
    260 	    free(freeIt);
    261 	    continue;
    262 	}
    263 	if (ch == '(')
    264 	    paren_depth++;
    265 	else if (ch == ')' && --paren_depth < 0)
    266 	    break;
    267 	Buf_AddByte(&buf, *cp);
    268 	cp++;
    269     }
    270 
    271     *out_arg = Buf_GetAll(&buf, &argLen);
    272     Buf_Destroy(&buf, FALSE);
    273 
    274     while (*cp == ' ' || *cp == '\t') {
    275 	cp++;
    276     }
    277 
    278     if (func != NULL && *cp++ != ')') {
    279 	Parse_Error(PARSE_WARNING, "Missing closing parenthesis for %s()",
    280 		    func);
    281 	return 0;
    282     }
    283 
    284     *linePtr = cp;
    285     return argLen;
    286 }
    287 
    288 /* Test whether the given variable is defined. */
    289 static Boolean
    290 CondDoDefined(int argLen MAKE_ATTR_UNUSED, const char *arg)
    291 {
    292     char *freeIt;
    293     Boolean result = Var_Value(arg, VAR_CMD, &freeIt) != NULL;
    294     bmake_free(freeIt);
    295     return result;
    296 }
    297 
    298 /* Wrapper around Str_Match, to be used by Lst_Find. */
    299 static Boolean
    300 CondFindStrMatch(const void *string, const void *pattern)
    301 {
    302     return Str_Match(string, pattern);
    303 }
    304 
    305 /* See if the given target is being made. */
    306 static Boolean
    307 CondDoMake(int argLen MAKE_ATTR_UNUSED, const char *arg)
    308 {
    309     return Lst_Find(create, CondFindStrMatch, arg) != NULL;
    310 }
    311 
    312 /* See if the given file exists. */
    313 static Boolean
    314 CondDoExists(int argLen MAKE_ATTR_UNUSED, const char *arg)
    315 {
    316     Boolean result;
    317     char *path;
    318 
    319     path = Dir_FindFile(arg, dirSearchPath);
    320     if (DEBUG(COND)) {
    321 	fprintf(debug_file, "exists(%s) result is \"%s\"\n",
    322 		arg, path ? path : "");
    323     }
    324     if (path != NULL) {
    325 	result = TRUE;
    326 	free(path);
    327     } else {
    328 	result = FALSE;
    329     }
    330     return result;
    331 }
    332 
    333 /* See if the given node exists and is an actual target. */
    334 static Boolean
    335 CondDoTarget(int argLen MAKE_ATTR_UNUSED, const char *arg)
    336 {
    337     GNode *gn;
    338 
    339     gn = Targ_FindNode(arg, TARG_NOCREATE);
    340     return gn != NULL && !OP_NOP(gn->type);
    341 }
    342 
    343 /* See if the given node exists and is an actual target with commands
    344  * associated with it. */
    345 static Boolean
    346 CondDoCommands(int argLen MAKE_ATTR_UNUSED, const char *arg)
    347 {
    348     GNode *gn;
    349 
    350     gn = Targ_FindNode(arg, TARG_NOCREATE);
    351     return gn != NULL && !OP_NOP(gn->type) && !Lst_IsEmpty(gn->commands);
    352 }
    353 
    354 /*-
    355  * Convert the given number into a double.
    356  * We try a base 10 or 16 integer conversion first, if that fails
    357  * then we try a floating point conversion instead.
    358  *
    359  * Results:
    360  *	Sets 'value' to double value of string.
    361  *	Returns TRUE if the conversion succeeded.
    362  */
    363 static Boolean
    364 CondCvtArg(const char *str, double *value)
    365 {
    366     char *eptr, ech;
    367     unsigned long l_val;
    368     double d_val;
    369 
    370     errno = 0;
    371     if (!*str) {
    372 	*value = (double)0;
    373 	return TRUE;
    374     }
    375     l_val = strtoul(str, &eptr, str[1] == 'x' ? 16 : 10);
    376     ech = *eptr;
    377     if (ech == 0 && errno != ERANGE) {
    378 	d_val = str[0] == '-' ? -(double)-l_val : (double)l_val;
    379     } else {
    380 	if (ech != 0 && ech != '.' && ech != 'e' && ech != 'E')
    381 	    return FALSE;
    382 	d_val = strtod(str, &eptr);
    383 	if (*eptr)
    384 	    return FALSE;
    385     }
    386 
    387     *value = d_val;
    388     return TRUE;
    389 }
    390 
    391 /*-
    392  * Parse a string from a variable reference or an optionally quoted
    393  * string.  This is called for the lhs and rhs of string compares.
    394  *
    395  * Results:
    396  *	Returns the string, absent any quotes, or NULL on error.
    397  *	Sets quoted if the string was quoted.
    398  *	Sets freeIt if needed.
    399  */
    400 /* coverity:[+alloc : arg-*3] */
    401 static const char *
    402 CondGetString(CondLexer *lex, Boolean doEval, Boolean *quoted, void **freeIt,
    403 	      Boolean strictLHS)
    404 {
    405     Buffer buf;
    406     const char *str;
    407     int len;
    408     Boolean qt;
    409     const char *start;
    410     VarEvalFlags eflags;
    411 
    412     Buf_Init(&buf, 0);
    413     str = NULL;
    414     *freeIt = NULL;
    415     *quoted = qt = *lex->condExpr == '"' ? 1 : 0;
    416     if (qt)
    417 	lex->condExpr++;
    418     for (start = lex->condExpr; *lex->condExpr && str == NULL;) {
    419 	switch (*lex->condExpr) {
    420 	case '\\':
    421 	    if (lex->condExpr[1] != '\0') {
    422 		lex->condExpr++;
    423 		Buf_AddByte(&buf, *lex->condExpr);
    424 	    }
    425 	    lex->condExpr++;
    426 	    continue;
    427 	case '"':
    428 	    if (qt) {
    429 		lex->condExpr++;	/* we don't want the quotes */
    430 		goto got_str;
    431 	    } else
    432 		Buf_AddByte(&buf, *lex->condExpr); /* likely? */
    433 	    lex->condExpr++;
    434 	    continue;
    435 	case ')':
    436 	case '!':
    437 	case '=':
    438 	case '>':
    439 	case '<':
    440 	case ' ':
    441 	case '\t':
    442 	    if (!qt)
    443 		goto got_str;
    444 	    else
    445 		Buf_AddByte(&buf, *lex->condExpr);
    446 	    lex->condExpr++;
    447 	    continue;
    448 	case '$':
    449 	    /* if we are in quotes, then an undefined variable is ok */
    450 	    eflags = ((!qt && doEval) ? VARE_UNDEFERR : 0) |
    451 		     (doEval ? VARE_WANTRES : 0);
    452 	    str = Var_Parse(lex->condExpr, VAR_CMD, eflags, &len, freeIt);
    453 	    if (str == var_Error) {
    454 		if (*freeIt) {
    455 		    free(*freeIt);
    456 		    *freeIt = NULL;
    457 		}
    458 		/*
    459 		 * Even if !doEval, we still report syntax errors, which
    460 		 * is what getting var_Error back with !doEval means.
    461 		 */
    462 		str = NULL;
    463 		goto cleanup;
    464 	    }
    465 	    lex->condExpr += len;
    466 	    /*
    467 	     * If the '$' was first char (no quotes), and we are
    468 	     * followed by space, the operator or end of expression,
    469 	     * we are done.
    470 	     */
    471 	    if ((lex->condExpr == start + len) &&
    472 		(*lex->condExpr == '\0' ||
    473 		 isspace((unsigned char)*lex->condExpr) ||
    474 		 strchr("!=><)", *lex->condExpr))) {
    475 		goto cleanup;
    476 	    }
    477 
    478 	    Buf_AddStr(&buf, str);
    479 	    if (*freeIt) {
    480 		free(*freeIt);
    481 		*freeIt = NULL;
    482 	    }
    483 	    str = NULL;		/* not finished yet */
    484 	    continue;
    485 	default:
    486 	    if (strictLHS && !qt && *start != '$' &&
    487 		!isdigit((unsigned char)*start)) {
    488 		/* lhs must be quoted, a variable reference or number */
    489 		if (*freeIt) {
    490 		    free(*freeIt);
    491 		    *freeIt = NULL;
    492 		}
    493 		str = NULL;
    494 		goto cleanup;
    495 	    }
    496 	    Buf_AddByte(&buf, *lex->condExpr);
    497 	    lex->condExpr++;
    498 	    continue;
    499 	}
    500     }
    501 got_str:
    502     *freeIt = Buf_GetAll(&buf, NULL);
    503     str = *freeIt;
    504 cleanup:
    505     Buf_Destroy(&buf, FALSE);
    506     return str;
    507 }
    508 
    509 /* The different forms of .if directives. */
    510 static const struct If {
    511     const char *form;		/* Form of if */
    512     size_t formlen;		/* Length of form */
    513     Boolean doNot;		/* TRUE if default function should be negated */
    514     Boolean (*defProc)(int, const char *); /* Default function to apply */
    515 } ifs[] = {
    516     { "def",   3, FALSE, CondDoDefined },
    517     { "ndef",  4, TRUE,  CondDoDefined },
    518     { "make",  4, FALSE, CondDoMake },
    519     { "nmake", 5, TRUE,  CondDoMake },
    520     { "",      0, FALSE, CondDoDefined },
    521     { NULL,    0, FALSE, NULL }
    522 };
    523 
    524 static Token
    525 compare_expression(CondLexer *lex, Boolean doEval)
    526 {
    527     Token t;
    528     const char *lhs;
    529     const char *rhs;
    530     const char *op;
    531     void *lhsFree;
    532     void *rhsFree;
    533     Boolean lhsQuoted;
    534     Boolean rhsQuoted;
    535     double left, right;
    536 
    537     t = TOK_ERROR;
    538     rhs = NULL;
    539     lhsFree = rhsFree = NULL;
    540     lhsQuoted = rhsQuoted = FALSE;
    541 
    542     /*
    543      * Parse the variable spec and skip over it, saving its
    544      * value in lhs.
    545      */
    546     lhs = CondGetString(lex, doEval, &lhsQuoted, &lhsFree, lhsStrict);
    547     if (!lhs)
    548 	goto done;
    549 
    550     CondLexer_SkipWhitespace(lex);
    551 
    552     /*
    553      * Make sure the operator is a valid one. If it isn't a
    554      * known relational operator, pretend we got a
    555      * != 0 comparison.
    556      */
    557     op = lex->condExpr;
    558     switch (*lex->condExpr) {
    559     case '!':
    560     case '=':
    561     case '<':
    562     case '>':
    563 	if (lex->condExpr[1] == '=') {
    564 	    lex->condExpr += 2;
    565 	} else {
    566 	    lex->condExpr += 1;
    567 	}
    568 	break;
    569     default:
    570 	if (!doEval) {
    571 	    t = TOK_FALSE;
    572 	    goto done;
    573 	}
    574 	/* For .ifxxx "..." check for non-empty string. */
    575 	if (lhsQuoted) {
    576 	    t = lhs[0] != 0;
    577 	    goto done;
    578 	}
    579 	/* For .ifxxx <number> compare against zero */
    580 	if (CondCvtArg(lhs, &left)) {
    581 	    t = left != 0.0;
    582 	    goto done;
    583 	}
    584 	/* For .if ${...} check for non-empty string (defProc is ifdef). */
    585 	if (lex->if_info->form[0] == 0) {
    586 	    t = lhs[0] != 0;
    587 	    goto done;
    588 	}
    589 	/* Otherwise action default test ... */
    590 	t = lex->if_info->defProc(strlen(lhs), lhs) != lex->if_info->doNot;
    591 	goto done;
    592     }
    593 
    594     CondLexer_SkipWhitespace(lex);
    595 
    596     if (*lex->condExpr == '\0') {
    597 	Parse_Error(PARSE_WARNING,
    598 		    "Missing right-hand-side of operator");
    599 	goto done;
    600     }
    601 
    602     rhs = CondGetString(lex, doEval, &rhsQuoted, &rhsFree, FALSE);
    603     if (!rhs)
    604 	goto done;
    605 
    606     if (!doEval) {
    607 	t = TOK_FALSE;
    608 	goto done;
    609     }
    610 
    611     if (rhsQuoted || lhsQuoted) {
    612     do_string_compare:
    613 	if (((*op != '!') && (*op != '=')) || (op[1] != '=')) {
    614 	    Parse_Error(PARSE_WARNING,
    615 			"String comparison operator should be either == or !=");
    616 	    goto done;
    617 	}
    618 
    619 	if (DEBUG(COND)) {
    620 	    fprintf(debug_file, "lhs = \"%s\", rhs = \"%s\", op = %.2s\n",
    621 		    lhs, rhs, op);
    622 	}
    623 	/*
    624 	 * Null-terminate rhs and perform the comparison.
    625 	 * t is set to the result.
    626 	 */
    627 	if (*op == '=') {
    628 	    t = strcmp(lhs, rhs) == 0;
    629 	} else {
    630 	    t = strcmp(lhs, rhs) != 0;
    631 	}
    632     } else {
    633 	/*
    634 	 * rhs is either a float or an integer. Convert both the
    635 	 * lhs and the rhs to a double and compare the two.
    636 	 */
    637 
    638 	if (!CondCvtArg(lhs, &left) || !CondCvtArg(rhs, &right))
    639 	    goto do_string_compare;
    640 
    641 	if (DEBUG(COND)) {
    642 	    fprintf(debug_file, "left = %f, right = %f, op = %.2s\n", left,
    643 		    right, op);
    644 	}
    645 	switch (op[0]) {
    646 	case '!':
    647 	    if (op[1] != '=') {
    648 		Parse_Error(PARSE_WARNING,
    649 			    "Unknown operator");
    650 		goto done;
    651 	    }
    652 	    t = (left != right);
    653 	    break;
    654 	case '=':
    655 	    if (op[1] != '=') {
    656 		Parse_Error(PARSE_WARNING,
    657 			    "Unknown operator");
    658 		goto done;
    659 	    }
    660 	    t = (left == right);
    661 	    break;
    662 	case '<':
    663 	    if (op[1] == '=') {
    664 		t = (left <= right);
    665 	    } else {
    666 		t = (left < right);
    667 	    }
    668 	    break;
    669 	case '>':
    670 	    if (op[1] == '=') {
    671 		t = (left >= right);
    672 	    } else {
    673 		t = (left > right);
    674 	    }
    675 	    break;
    676 	}
    677     }
    678 
    679 done:
    680     free(lhsFree);
    681     free(rhsFree);
    682     return t;
    683 }
    684 
    685 static int
    686 ParseEmptyArg(Boolean doEval, const char **linePtr, char **argPtr,
    687 	      const char *func MAKE_ATTR_UNUSED)
    688 {
    689     void *val_freeIt;
    690     const char *val;
    691     int magic_res;
    692 
    693     /* We do all the work here and return the result as the length */
    694     *argPtr = NULL;
    695 
    696     (*linePtr)--;		/* Make (*linePtr)[1] point to the '('. */
    697     val = Var_ParsePP(linePtr, VAR_CMD, doEval ? VARE_WANTRES : 0, &val_freeIt);
    698     /* If successful, *linePtr points beyond the closing ')' now. */
    699 
    700     if (val == var_Error) {
    701 	free(val_freeIt);
    702 	return -1;
    703     }
    704 
    705     /* A variable is empty when it just contains spaces... 4/15/92, christos */
    706     while (isspace((unsigned char)val[0]))
    707 	val++;
    708 
    709     /*
    710      * For consistency with the other functions we can't generate the
    711      * true/false here.
    712      */
    713     magic_res = *val != '\0' ? 2 : 1;
    714     free(val_freeIt);
    715     return magic_res;
    716 }
    717 
    718 static Boolean
    719 CondDoEmpty(int arglen, const char *arg MAKE_ATTR_UNUSED)
    720 {
    721     /* Magic values ahead, see ParseEmptyArg. */
    722     return arglen == 1;
    723 }
    724 
    725 static Token
    726 compare_function(CondLexer *lex, Boolean doEval)
    727 {
    728     static const struct fn_def {
    729 	const char *fn_name;
    730 	size_t fn_name_len;
    731 	int (*fn_getarg)(Boolean, const char **, char **, const char *);
    732 	Boolean (*fn_proc)(int, const char *);
    733     } fn_defs[] = {
    734 	{ "defined",  7, ParseFuncArg,  CondDoDefined },
    735 	{ "make",     4, ParseFuncArg,  CondDoMake },
    736 	{ "exists",   6, ParseFuncArg,  CondDoExists },
    737 	{ "empty",    5, ParseEmptyArg, CondDoEmpty },
    738 	{ "target",   6, ParseFuncArg,  CondDoTarget },
    739 	{ "commands", 8, ParseFuncArg,  CondDoCommands },
    740 	{ NULL,       0, NULL, NULL },
    741     };
    742     const struct fn_def *fn_def;
    743     Token t;
    744     char *arg = NULL;
    745     int arglen;
    746     const char *cp = lex->condExpr;
    747     const char *cp1;
    748 
    749     for (fn_def = fn_defs; fn_def->fn_name != NULL; fn_def++) {
    750 	if (!istoken(cp, fn_def->fn_name, fn_def->fn_name_len))
    751 	    continue;
    752 	cp += fn_def->fn_name_len;
    753 	/* There can only be whitespace before the '(' */
    754 	while (isspace((unsigned char)*cp))
    755 	    cp++;
    756 	if (*cp != '(')
    757 	    break;
    758 
    759 	arglen = fn_def->fn_getarg(doEval, &cp, &arg, fn_def->fn_name);
    760 	if (arglen <= 0) {
    761 	    lex->condExpr = cp;
    762 	    return arglen < 0 ? TOK_ERROR : TOK_FALSE;
    763 	}
    764 	/* Evaluate the argument using the required function. */
    765 	t = !doEval || fn_def->fn_proc(arglen, arg);
    766 	free(arg);
    767 	lex->condExpr = cp;
    768 	return t;
    769     }
    770 
    771     /* Push anything numeric through the compare expression */
    772     cp = lex->condExpr;
    773     if (isdigit((unsigned char)cp[0]) || strchr("+-", cp[0]))
    774 	return compare_expression(lex, doEval);
    775 
    776     /*
    777      * Most likely we have a naked token to apply the default function to.
    778      * However ".if a == b" gets here when the "a" is unquoted and doesn't
    779      * start with a '$'. This surprises people.
    780      * If what follows the function argument is a '=' or '!' then the syntax
    781      * would be invalid if we did "defined(a)" - so instead treat as an
    782      * expression.
    783      */
    784     arglen = ParseFuncArg(doEval, &cp, &arg, NULL);
    785     for (cp1 = cp; isspace((unsigned char)*cp1); cp1++)
    786 	continue;
    787     if (*cp1 == '=' || *cp1 == '!')
    788 	return compare_expression(lex, doEval);
    789     lex->condExpr = cp;
    790 
    791     /*
    792      * Evaluate the argument using the default function.
    793      * This path always treats .if as .ifdef. To get here, the character
    794      * after .if must have been taken literally, so the argument cannot
    795      * be empty - even if it contained a variable expansion.
    796      */
    797     t = !doEval || lex->if_info->defProc(arglen, arg) != lex->if_info->doNot;
    798     free(arg);
    799     return t;
    800 }
    801 
    802 /* Return the next token or comparison result from the lexer. */
    803 static Token
    804 CondToken(CondLexer *lex, Boolean doEval)
    805 {
    806     Token t;
    807 
    808     t = lex->curr;
    809     if (t != TOK_NONE) {
    810 	lex->curr = TOK_NONE;
    811 	return t;
    812     }
    813 
    814     while (lex->condExpr[0] == ' ' || lex->condExpr[0] == '\t') {
    815 	lex->condExpr++;
    816     }
    817 
    818     switch (lex->condExpr[0]) {
    819 
    820     case '(':
    821 	lex->condExpr++;
    822 	return TOK_LPAREN;
    823 
    824     case ')':
    825 	lex->condExpr++;
    826 	return TOK_RPAREN;
    827 
    828     case '|':
    829 	lex->condExpr++;
    830 	if (lex->condExpr[0] == '|') {
    831 	    lex->condExpr++;
    832 	}
    833 	return TOK_OR;
    834 
    835     case '&':
    836 	lex->condExpr++;
    837 	if (lex->condExpr[0] == '&') {
    838 	    lex->condExpr++;
    839 	}
    840 	return TOK_AND;
    841 
    842     case '!':
    843 	lex->condExpr++;
    844 	return TOK_NOT;
    845 
    846     case '#':
    847     case '\n':
    848     case '\0':
    849 	return TOK_EOF;
    850 
    851     case '"':
    852     case '$':
    853 	return compare_expression(lex, doEval);
    854 
    855     default:
    856 	return compare_function(lex, doEval);
    857     }
    858 }
    859 
    860 /* Parse a single term in the expression. This consists of a terminal symbol
    861  * or TOK_NOT and a term (not including the binary operators):
    862  *
    863  *	T -> defined(variable) | make(target) | exists(file) | symbol
    864  *	T -> ! T | ( E )
    865  *
    866  * Results:
    867  *	TOK_TRUE, TOK_FALSE or TOK_ERROR.
    868  */
    869 static Token
    870 CondT(CondLexer *lex, Boolean doEval)
    871 {
    872     Token t;
    873 
    874     t = CondToken(lex, doEval);
    875 
    876     if (t == TOK_EOF) {
    877 	/*
    878 	 * If we reached the end of the expression, the expression
    879 	 * is malformed...
    880 	 */
    881 	t = TOK_ERROR;
    882     } else if (t == TOK_LPAREN) {
    883 	/*
    884 	 * T -> ( E )
    885 	 */
    886 	t = CondE(lex, doEval);
    887 	if (t != TOK_ERROR) {
    888 	    if (CondToken(lex, doEval) != TOK_RPAREN) {
    889 		t = TOK_ERROR;
    890 	    }
    891 	}
    892     } else if (t == TOK_NOT) {
    893 	t = CondT(lex, doEval);
    894 	if (t == TOK_TRUE) {
    895 	    t = TOK_FALSE;
    896 	} else if (t == TOK_FALSE) {
    897 	    t = TOK_TRUE;
    898 	}
    899     }
    900     return t;
    901 }
    902 
    903 /* Parse a conjunctive factor (nice name, wot?)
    904  *
    905  *	F -> T && F | T
    906  *
    907  * Results:
    908  *	TOK_TRUE, TOK_FALSE or TOK_ERROR
    909  */
    910 static Token
    911 CondF(CondLexer *lex, Boolean doEval)
    912 {
    913     Token l, o;
    914 
    915     l = CondT(lex, doEval);
    916     if (l != TOK_ERROR) {
    917 	o = CondToken(lex, doEval);
    918 
    919 	if (o == TOK_AND) {
    920 	    /*
    921 	     * F -> T && F
    922 	     *
    923 	     * If T is TOK_FALSE, the whole thing will be TOK_FALSE, but we
    924 	     * have to parse the r.h.s. anyway (to throw it away).
    925 	     * If T is TOK_TRUE, the result is the r.h.s., be it a TOK_ERROR
    926 	     * or not.
    927 	     */
    928 	    if (l == TOK_TRUE) {
    929 		l = CondF(lex, doEval);
    930 	    } else {
    931 		(void)CondF(lex, FALSE);
    932 	    }
    933 	} else {
    934 	    /*
    935 	     * F -> T
    936 	     */
    937 	    CondLexer_PushBack(lex, o);
    938 	}
    939     }
    940     return l;
    941 }
    942 
    943 /* Main expression production.
    944  *
    945  *	E -> F || E | F
    946  *
    947  * Results:
    948  *	TOK_TRUE, TOK_FALSE or TOK_ERROR.
    949  */
    950 static Token
    951 CondE(CondLexer *lex, Boolean doEval)
    952 {
    953     Token l, o;
    954 
    955     l = CondF(lex, doEval);
    956     if (l != TOK_ERROR) {
    957 	o = CondToken(lex, doEval);
    958 
    959 	if (o == TOK_OR) {
    960 	    /*
    961 	     * E -> F || E
    962 	     *
    963 	     * A similar thing occurs for ||, except that here we make sure
    964 	     * the l.h.s. is TOK_FALSE before we bother to evaluate the r.h.s.
    965 	     * Once again, if l is TOK_FALSE, the result is the r.h.s. and once
    966 	     * again if l is TOK_TRUE, we parse the r.h.s. to throw it away.
    967 	     */
    968 	    if (l == TOK_FALSE) {
    969 		l = CondE(lex, doEval);
    970 	    } else {
    971 		(void)CondE(lex, FALSE);
    972 	    }
    973 	} else {
    974 	    /*
    975 	     * E -> F
    976 	     */
    977 	    CondLexer_PushBack(lex, o);
    978 	}
    979     }
    980     return l;
    981 }
    982 
    983 static CondEvalResult
    984 do_Cond_EvalExpression(CondLexer *lex, Boolean *value)
    985 {
    986 
    987     switch (CondE(lex, TRUE)) {
    988     case TOK_TRUE:
    989 	if (CondToken(lex, TRUE) == TOK_EOF) {
    990 	    *value = TRUE;
    991 	    return COND_PARSE;
    992 	}
    993 	break;
    994     case TOK_FALSE:
    995 	if (CondToken(lex, TRUE) == TOK_EOF) {
    996 	    *value = FALSE;
    997 	    return COND_PARSE;
    998 	}
    999 	break;
   1000     default:
   1001     case TOK_ERROR:
   1002 	break;
   1003     }
   1004 
   1005     return COND_INVALID;
   1006 }
   1007 
   1008 /* Evaluate the condition in the passed line, including any side effects from
   1009  * the variable expressions in the condition. The condition consists of &&,
   1010  * ||, !, function(arg), comparisons and parenthetical groupings thereof.
   1011  *
   1012  * Results:
   1013  *	COND_PARSE	if the condition was valid grammatically
   1014  *	COND_INVALID  	if not a valid conditional.
   1015  *
   1016  *	(*value) is set to the boolean value of the condition
   1017  */
   1018 CondEvalResult
   1019 Cond_EvalExpression(const struct If *info, const char *line, Boolean *value,
   1020 		    int eprint, Boolean strictLHS)
   1021 {
   1022     static const struct If *dflt_info;
   1023     CondLexer lex;
   1024     int rval;
   1025 
   1026     lhsStrict = strictLHS;
   1027 
   1028     while (*line == ' ' || *line == '\t')
   1029 	line++;
   1030 
   1031     if (info == NULL && (info = dflt_info) == NULL) {
   1032 	/* Scan for the entry for .if - it can't be first */
   1033 	for (info = ifs;; info++)
   1034 	    if (info->form[0] == 0)
   1035 		break;
   1036 	dflt_info = info;
   1037     }
   1038     assert(info != NULL);
   1039 
   1040     lex.if_info = info;
   1041     lex.condExpr = line;
   1042     lex.curr = TOK_NONE;
   1043 
   1044     rval = do_Cond_EvalExpression(&lex, value);
   1045 
   1046     if (rval == COND_INVALID && eprint)
   1047 	Parse_Error(PARSE_FATAL, "Malformed conditional (%s)", line);
   1048 
   1049     return rval;
   1050 }
   1051 
   1052 
   1053 /* Evaluate the conditional in the passed line. The line looks like this:
   1054  *	.<cond-type> <expr>
   1055  * In this line, <cond-type> is any of if, ifmake, ifnmake, ifdef, ifndef,
   1056  * elif, elifmake, elifnmake, elifdef, elifndef.
   1057  * In this line, <expr> consists of &&, ||, !, function(arg), comparisons
   1058  * and parenthetical groupings thereof.
   1059  *
   1060  * Note that the states IF_ACTIVE and ELSE_ACTIVE are only different in order
   1061  * to detect spurious .else lines (as are SKIP_TO_ELSE and SKIP_TO_ENDIF),
   1062  * otherwise .else could be treated as '.elif 1'.
   1063  *
   1064  * Results:
   1065  *	COND_PARSE	to continue parsing the lines after the conditional
   1066  *			(when .if or .else returns TRUE)
   1067  *	COND_SKIP	to skip the lines after the conditional
   1068  *			(when .if or .elif returns FALSE, or when a previous
   1069  *			branch has already been taken)
   1070  *	COND_INVALID  	if the conditional was not valid, either because of
   1071  *			a syntax error or because some variable was undefined
   1072  *			or because the condition could not be evaluated
   1073  */
   1074 CondEvalResult
   1075 Cond_Eval(const char *line)
   1076 {
   1077     enum { MAXIF = 128 };	/* maximum depth of .if'ing */
   1078     enum { MAXIF_BUMP = 32 };	/* how much to grow by */
   1079     enum if_states {
   1080 	IF_ACTIVE,		/* .if or .elif part active */
   1081 	ELSE_ACTIVE,		/* .else part active */
   1082 	SEARCH_FOR_ELIF,	/* searching for .elif/else to execute */
   1083 	SKIP_TO_ELSE,		/* has been true, but not seen '.else' */
   1084 	SKIP_TO_ENDIF		/* nothing else to execute */
   1085     };
   1086     static enum if_states *cond_state = NULL;
   1087     static unsigned int max_if_depth = MAXIF;
   1088 
   1089     const struct If *ifp;
   1090     Boolean isElif;
   1091     Boolean value;
   1092     int level;			/* Level at which to report errors. */
   1093     enum if_states state;
   1094 
   1095     level = PARSE_FATAL;
   1096     if (!cond_state) {
   1097 	cond_state = bmake_malloc(max_if_depth * sizeof(*cond_state));
   1098 	cond_state[0] = IF_ACTIVE;
   1099     }
   1100     /* skip leading character (the '.') and any whitespace */
   1101     for (line++; *line == ' ' || *line == '\t'; line++)
   1102 	continue;
   1103 
   1104     /* Find what type of if we're dealing with.  */
   1105     if (line[0] == 'e') {
   1106 	if (line[1] != 'l') {
   1107 	    if (!istoken(line + 1, "ndif", 4))
   1108 		return COND_INVALID;
   1109 	    /* End of conditional section */
   1110 	    if (cond_depth == cond_min_depth) {
   1111 		Parse_Error(level, "if-less endif");
   1112 		return COND_PARSE;
   1113 	    }
   1114 	    /* Return state for previous conditional */
   1115 	    cond_depth--;
   1116 	    return cond_state[cond_depth] <= ELSE_ACTIVE
   1117 		   ? COND_PARSE : COND_SKIP;
   1118 	}
   1119 
   1120 	/* Quite likely this is 'else' or 'elif' */
   1121 	line += 2;
   1122 	if (istoken(line, "se", 2)) {
   1123 	    /* It is else... */
   1124 	    if (cond_depth == cond_min_depth) {
   1125 		Parse_Error(level, "if-less else");
   1126 		return COND_PARSE;
   1127 	    }
   1128 
   1129 	    state = cond_state[cond_depth];
   1130 	    switch (state) {
   1131 	    case SEARCH_FOR_ELIF:
   1132 		state = ELSE_ACTIVE;
   1133 		break;
   1134 	    case ELSE_ACTIVE:
   1135 	    case SKIP_TO_ENDIF:
   1136 		Parse_Error(PARSE_WARNING, "extra else");
   1137 		/* FALLTHROUGH */
   1138 	    default:
   1139 	    case IF_ACTIVE:
   1140 	    case SKIP_TO_ELSE:
   1141 		state = SKIP_TO_ENDIF;
   1142 		break;
   1143 	    }
   1144 	    cond_state[cond_depth] = state;
   1145 	    return state <= ELSE_ACTIVE ? COND_PARSE : COND_SKIP;
   1146 	}
   1147 	/* Assume for now it is an elif */
   1148 	isElif = TRUE;
   1149     } else
   1150 	isElif = FALSE;
   1151 
   1152     if (line[0] != 'i' || line[1] != 'f')
   1153 	/* Not an ifxxx or elifxxx line */
   1154 	return COND_INVALID;
   1155 
   1156     /*
   1157      * Figure out what sort of conditional it is -- what its default
   1158      * function is, etc. -- by looking in the table of valid "ifs"
   1159      */
   1160     line += 2;
   1161     for (ifp = ifs;; ifp++) {
   1162 	if (ifp->form == NULL)
   1163 	    return COND_INVALID;
   1164 	if (istoken(ifp->form, line, ifp->formlen)) {
   1165 	    line += ifp->formlen;
   1166 	    break;
   1167 	}
   1168     }
   1169 
   1170     /* Now we know what sort of 'if' it is... */
   1171 
   1172     if (isElif) {
   1173 	if (cond_depth == cond_min_depth) {
   1174 	    Parse_Error(level, "if-less elif");
   1175 	    return COND_PARSE;
   1176 	}
   1177 	state = cond_state[cond_depth];
   1178 	if (state == SKIP_TO_ENDIF || state == ELSE_ACTIVE) {
   1179 	    Parse_Error(PARSE_WARNING, "extra elif");
   1180 	    cond_state[cond_depth] = SKIP_TO_ENDIF;
   1181 	    return COND_SKIP;
   1182 	}
   1183 	if (state != SEARCH_FOR_ELIF) {
   1184 	    /* Either just finished the 'true' block, or already SKIP_TO_ELSE */
   1185 	    cond_state[cond_depth] = SKIP_TO_ELSE;
   1186 	    return COND_SKIP;
   1187 	}
   1188     } else {
   1189 	/* Normal .if */
   1190 	if (cond_depth + 1 >= max_if_depth) {
   1191 	    /*
   1192 	     * This is rare, but not impossible.
   1193 	     * In meta mode, dirdeps.mk (only runs at level 0)
   1194 	     * can need more than the default.
   1195 	     */
   1196 	    max_if_depth += MAXIF_BUMP;
   1197 	    cond_state = bmake_realloc(cond_state,
   1198 				       max_if_depth * sizeof(*cond_state));
   1199 	}
   1200 	state = cond_state[cond_depth];
   1201 	cond_depth++;
   1202 	if (state > ELSE_ACTIVE) {
   1203 	    /* If we aren't parsing the data, treat as always false */
   1204 	    cond_state[cond_depth] = SKIP_TO_ELSE;
   1205 	    return COND_SKIP;
   1206 	}
   1207     }
   1208 
   1209     /* And evaluate the conditional expression */
   1210     if (Cond_EvalExpression(ifp, line, &value, 1, TRUE) == COND_INVALID) {
   1211 	/* Syntax error in conditional, error message already output. */
   1212 	/* Skip everything to matching .endif */
   1213 	cond_state[cond_depth] = SKIP_TO_ELSE;
   1214 	return COND_SKIP;
   1215     }
   1216 
   1217     if (!value) {
   1218 	cond_state[cond_depth] = SEARCH_FOR_ELIF;
   1219 	return COND_SKIP;
   1220     }
   1221     cond_state[cond_depth] = IF_ACTIVE;
   1222     return COND_PARSE;
   1223 }
   1224 
   1225 void
   1226 Cond_restore_depth(unsigned int saved_depth)
   1227 {
   1228     int open_conds = cond_depth - cond_min_depth;
   1229 
   1230     if (open_conds != 0 || saved_depth > cond_depth) {
   1231 	Parse_Error(PARSE_FATAL, "%d open conditional%s", open_conds,
   1232 		    open_conds == 1 ? "" : "s");
   1233 	cond_depth = cond_min_depth;
   1234     }
   1235 
   1236     cond_min_depth = saved_depth;
   1237 }
   1238 
   1239 unsigned int
   1240 Cond_save_depth(void)
   1241 {
   1242     int depth = cond_min_depth;
   1243 
   1244     cond_min_depth = cond_depth;
   1245     return depth;
   1246 }
   1247