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