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