1 /* $NetBSD: cond.c,v 1.379 2026/04/06 17:13:54 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 /* 73 * Handling of conditionals in a makefile. 74 * 75 * Interface: 76 * Cond_EvalLine Evaluate the conditional directive, such as 77 * '.if <cond>', '.elifnmake <cond>', '.else', '.endif'. 78 * 79 * Cond_EvalCondition 80 * Evaluate a condition, either from one of the .if 81 * directives, or from a ':?then:else' modifier. 82 * 83 * Cond_EndFile At the end of reading a makefile, ensure that the 84 * conditional directives are well-balanced. 85 */ 86 87 #include <errno.h> 88 89 #include "make.h" 90 #include "dir.h" 91 92 /* "@(#)cond.c 8.2 (Berkeley) 1/2/94" */ 93 MAKE_RCSID("$NetBSD: cond.c,v 1.379 2026/04/06 17:13:54 rillig Exp $"); 94 95 /* 96 * Conditional expressions conform to this grammar: 97 * Or -> And ('||' And)* 98 * And -> Term ('&&' Term)* 99 * Term -> Function '(' Argument ')' 100 * Term -> Leaf ComparisonOp Leaf 101 * Term -> Leaf 102 * Term -> '(' Or ')' 103 * Term -> '!' Term 104 * Leaf -> "string" 105 * Leaf -> Number 106 * Leaf -> VariableExpression 107 * Leaf -> BareWord 108 * ComparisonOp -> '==' | '!=' | '>' | '<' | '>=' | '<=' 109 * 110 * BareWord is an unquoted string literal, its evaluation depends on the kind 111 * of '.if' directive. 112 * 113 * The tokens are scanned by CondParser_Token, which returns: 114 * TOK_AND for '&&' 115 * TOK_OR for '||' 116 * TOK_NOT for '!' 117 * TOK_LPAREN for '(' 118 * TOK_RPAREN for ')' 119 * 120 * Other terminal symbols are evaluated using either the default function or 121 * the function given in the terminal, they return either TOK_TRUE, TOK_FALSE 122 * or TOK_ERROR. 123 */ 124 typedef enum Token { 125 TOK_FALSE, TOK_TRUE, TOK_AND, TOK_OR, TOK_NOT, 126 TOK_LPAREN, TOK_RPAREN, TOK_EOF, TOK_NONE, TOK_ERROR 127 } Token; 128 129 typedef enum ComparisonOp { 130 LT, LE, GT, GE, EQ, NE 131 } ComparisonOp; 132 133 typedef struct CondParser { 134 135 /* 136 * The plain '.if ${VAR}' evaluates to true if the value of the 137 * expression has length > 0 and is not numerically zero. The other 138 * '.if' variants delegate to evalBare instead, for example '.ifdef 139 * ${VAR}' is equivalent to '.if defined(${VAR})', checking whether 140 * the variable named by the expression '${VAR}' is defined. 141 */ 142 bool plain; 143 144 /* The function to apply on unquoted bare words. */ 145 bool (*evalBare)(const char *); 146 bool negateEvalBare; 147 148 /* 149 * Whether the left-hand side of a comparison may be an unquoted 150 * string. This is allowed for expressions of the form 151 * ${condition:?:}, see ApplyModifier_IfElse. Such a condition is 152 * expanded before it is evaluated, due to ease of implementation. 153 * This means that at the point where the condition is evaluated, 154 * make cannot know anymore whether the left-hand side had originally 155 * been an expression or a plain word. 156 * 157 * In conditional directives like '.if', the left-hand side must 158 * either be a defined expression, a quoted string or a number. 159 */ 160 bool leftUnquotedOK; 161 162 const char *p; /* The remaining condition to parse */ 163 Token curr; /* The push-back token, or TOK_NONE */ 164 } CondParser; 165 166 static CondResult CondParser_Or(CondParser *, bool); 167 168 unsigned cond_depth = 0; /* current .if nesting level */ 169 170 /* Names for ComparisonOp. */ 171 static const char opname[][3] = { "<", "<=", ">", ">=", "==", "!=" }; 172 173 MAKE_INLINE bool 174 skip_string(const char **pp, const char *str) 175 { 176 size_t len = strlen(str); 177 bool ok = strncmp(*pp, str, len) == 0; 178 if (ok) 179 *pp += len; 180 return ok; 181 } 182 183 static Token 184 ToToken(bool cond) 185 { 186 return cond ? TOK_TRUE : TOK_FALSE; 187 } 188 189 static void 190 CondParser_SkipWhitespace(CondParser *par) 191 { 192 cpp_skip_whitespace(&par->p); 193 } 194 195 /* 196 * Parse a single word, taking into account balanced parentheses as well as 197 * embedded expressions. Used for the argument of a built-in function as 198 * well as for bare words, which are then passed to the default function. 199 */ 200 static char * 201 ParseWord(const char **pp, bool doEval) 202 { 203 const char *p = *pp; 204 Buffer word; 205 int depth; 206 207 Buf_Init(&word); 208 209 depth = 0; 210 for (;;) { 211 char ch = *p; 212 if (ch == '\0' || ch == ' ' || ch == '\t') 213 break; 214 if ((ch == '&' || ch == '|') && depth == 0) 215 break; 216 if (ch == '$') { 217 VarEvalMode emode = doEval ? VARE_EVAL : VARE_PARSE; 218 FStr nestedVal = Var_Parse(&p, SCOPE_CMDLINE, emode); 219 /* TODO: handle errors */ 220 Buf_AddStr(&word, nestedVal.str); 221 FStr_Done(&nestedVal); 222 continue; 223 } 224 if (ch == '(') 225 depth++; 226 else if (ch == ')' && --depth < 0) 227 break; 228 Buf_AddByte(&word, ch); 229 p++; 230 } 231 232 *pp = p; 233 234 return Buf_DoneData(&word); 235 } 236 237 /* Parse the function argument, including the surrounding parentheses. */ 238 static char * 239 ParseFuncArg(const char **pp, bool doEval, const char *func) 240 { 241 const char *p = *pp, *argStart, *argEnd; 242 char *res; 243 244 p++; /* skip the '(' */ 245 cpp_skip_hspace(&p); 246 argStart = p; 247 res = ParseWord(&p, doEval); 248 argEnd = p; 249 cpp_skip_hspace(&p); 250 251 if (*p++ != ')') { 252 int len = 0; 253 while (ch_isalpha(func[len])) 254 len++; 255 256 Parse_Error(PARSE_FATAL, 257 "Missing \")\" after argument \"%.*s\" for \"%.*s\"", 258 (int)(argEnd - argStart), argStart, len, func); 259 free(res); 260 return NULL; 261 } 262 263 *pp = p; 264 return res; 265 } 266 267 /* See if the given variable is defined. */ 268 static bool 269 FuncDefined(const char *var) 270 { 271 return Var_Exists(SCOPE_CMDLINE, var); 272 } 273 274 /* See if a target matching targetPattern is requested to be made. */ 275 static bool 276 FuncMake(const char *targetPattern) 277 { 278 StringListNode *ln; 279 bool warned = false; 280 281 for (ln = opts.create.first; ln != NULL; ln = ln->next) { 282 StrMatchResult res = Str_Match(ln->datum, targetPattern); 283 if (res.error != NULL && !warned) { 284 warned = true; 285 Parse_Error(PARSE_WARNING, 286 "%s in pattern argument \"%s\" " 287 "to function \"make\"", 288 res.error, targetPattern); 289 } 290 if (res.matched) 291 return true; 292 } 293 return false; 294 } 295 296 /* See if the given file exists. */ 297 static bool 298 FuncExists(const char *file) 299 { 300 bool result; 301 char *path; 302 303 path = Dir_FindFile(file, &dirSearchPath); 304 result = path != NULL; 305 if (result) 306 DEBUG2(COND, "\"%s\" exists in \"%s\"\n", file, path); 307 else 308 DEBUG1(COND, "\"%s\" does not exist\n", file); 309 free(path); 310 return result; 311 } 312 313 /* See if the given node is an actual target. */ 314 static bool 315 FuncTarget(const char *node) 316 { 317 GNode *gn = Targ_FindNode(node); 318 return gn != NULL && GNode_IsTarget(gn); 319 } 320 321 /* See if the given node is an actual target with commands. */ 322 static bool 323 FuncCommands(const char *node) 324 { 325 GNode *gn = Targ_FindNode(node); 326 return gn != NULL && GNode_IsTarget(gn) && 327 !Lst_IsEmpty(&gn->commands); 328 } 329 330 /* 331 * Convert the string to a floating point number. Accepted formats are 332 * base-10 integer, base-16 integer and finite floating point numbers. 333 */ 334 static bool 335 TryParseNumber(const char *str, double *out_value) 336 { 337 char *end; 338 unsigned long ul_val; 339 double dbl_val; 340 341 if (str[0] == '\0') { /* XXX: why is an empty string a number? */ 342 *out_value = 0.0; 343 return true; 344 } 345 346 errno = 0; 347 ul_val = strtoul(str, &end, str[1] == 'x' ? 16 : 10); 348 if (*end == '\0' && errno != ERANGE) { 349 *out_value = str[0] == '-' ? -(double)-ul_val : (double)ul_val; 350 return true; 351 } 352 353 if (*end != '\0' && *end != '.' && *end != 'e' && *end != 'E') 354 return false; /* skip the expensive strtod call */ 355 dbl_val = strtod(str, &end); 356 if (*end != '\0') 357 return false; 358 359 *out_value = dbl_val; 360 return true; 361 } 362 363 static bool 364 is_separator(char ch) 365 { 366 return ch == '\0' || ch_isspace(ch) || ch == '!' || ch == '=' || 367 ch == '>' || ch == '<' || ch == ')' /* but not '(' */; 368 } 369 370 /* 371 * In a quoted or unquoted string literal or a number, parse an 372 * expression and add its value to the buffer. 373 * 374 * Return whether to continue parsing the leaf. 375 * 376 * Examples: .if x${CENTER}y == "${PREFIX}${SUFFIX}" || 0x${HEX} 377 */ 378 static bool 379 CondParser_StringExpr(CondParser *par, const char *start, 380 bool doEval, bool quoted, 381 Buffer *buf, FStr *out_str) 382 { 383 VarEvalMode emode; 384 const char *p; 385 bool outsideQuotes; 386 387 emode = doEval && quoted ? VARE_EVAL 388 : doEval ? VARE_EVAL_DEFINED_LOUD 389 : VARE_PARSE; 390 391 p = par->p; 392 outsideQuotes = p == start; 393 *out_str = Var_Parse(&p, SCOPE_CMDLINE, emode); 394 if (out_str->str == var_Error) { 395 FStr_Done(out_str); 396 *out_str = FStr_InitRefer(NULL); 397 return false; 398 } 399 par->p = p; 400 401 if (outsideQuotes && is_separator(par->p[0])) 402 return false; 403 404 Buf_AddStr(buf, out_str->str); 405 FStr_Done(out_str); 406 *out_str = FStr_InitRefer(NULL); /* not finished yet */ 407 return true; 408 } 409 410 /* 411 * Parse a string from an expression or an optionally quoted string, 412 * on the left-hand and right-hand sides of comparisons. 413 * 414 * Return the string without any enclosing quotes, or NULL on error. 415 * Set out_quoted if the leaf was a quoted string literal. 416 */ 417 static FStr 418 CondParser_Leaf(CondParser *par, bool doEval, bool unquotedOK, 419 bool *out_quoted) 420 { 421 Buffer buf; 422 FStr str; 423 bool quoted; 424 const char *start; 425 426 Buf_Init(&buf); 427 str = FStr_InitRefer(NULL); 428 *out_quoted = quoted = par->p[0] == '"'; 429 start = par->p; 430 if (quoted) 431 par->p++; 432 433 while (par->p[0] != '\0' && str.str == NULL) { 434 switch (par->p[0]) { 435 case '\\': 436 par->p++; 437 if (par->p[0] != '\0') { 438 Buf_AddByte(&buf, par->p[0]); 439 par->p++; 440 } else 441 Parse_Error(PARSE_FATAL, 442 "Unfinished backslash escape sequence"); 443 continue; 444 case '"': 445 par->p++; 446 if (quoted) 447 goto return_buf; 448 Buf_AddByte(&buf, '"'); 449 continue; 450 case ')': /* see is_separator */ 451 case '!': 452 case '=': 453 case '>': 454 case '<': 455 case ' ': 456 case '\t': 457 if (!quoted) 458 goto return_buf; 459 Buf_AddByte(&buf, par->p[0]); 460 par->p++; 461 continue; 462 case '$': 463 if (!CondParser_StringExpr(par, 464 start, doEval, quoted, &buf, &str)) 465 goto return_str; 466 continue; 467 default: 468 if (!unquotedOK && !quoted && *start != '$' && 469 !ch_isdigit(*start)) { 470 str = FStr_InitRefer(NULL); 471 goto return_str; 472 } 473 Buf_AddByte(&buf, par->p[0]); 474 par->p++; 475 continue; 476 } 477 } 478 if (quoted) 479 Parse_Error(PARSE_FATAL, 480 "Unfinished string literal \"%s\"", start); 481 return_buf: 482 str = FStr_InitOwn(buf.data); 483 buf.data = NULL; 484 return_str: 485 Buf_Done(&buf); 486 return str; 487 } 488 489 /* 490 * Evaluate a "comparison without operator", such as in ".if ${VAR}" or 491 * ".if 0". 492 */ 493 static bool 494 EvalTruthy(CondParser *par, const char *value, bool quoted) 495 { 496 double num; 497 498 if (quoted) 499 return value[0] != '\0'; 500 if (TryParseNumber(value, &num)) 501 return num != 0.0; 502 if (par->plain) 503 return value[0] != '\0'; 504 return par->evalBare(value) != par->negateEvalBare; 505 } 506 507 /* Evaluate a numerical comparison, such as in ".if ${VAR} >= 9". */ 508 static bool 509 EvalCompareNum(double lhs, ComparisonOp op, double rhs) 510 { 511 DEBUG3(COND, "Comparing %f %s %f\n", lhs, opname[op], rhs); 512 513 switch (op) { 514 case LT: 515 return lhs < rhs; 516 case LE: 517 return lhs <= rhs; 518 case GT: 519 return lhs > rhs; 520 case GE: 521 return lhs >= rhs; 522 case EQ: 523 return lhs == rhs; 524 default: 525 return lhs != rhs; 526 } 527 } 528 529 static Token 530 EvalCompareStr(const char *lhs, ComparisonOp op, const char *rhs) 531 { 532 if (op != EQ && op != NE) { 533 Parse_Error(PARSE_FATAL, 534 "Comparison with \"%s\" requires both operands " 535 "\"%s\" and \"%s\" to be numeric", 536 opname[op], lhs, rhs); 537 return TOK_ERROR; 538 } 539 540 DEBUG3(COND, "Comparing \"%s\" %s \"%s\"\n", lhs, opname[op], rhs); 541 return ToToken((op == EQ) == (strcmp(lhs, rhs) == 0)); 542 } 543 544 /* Evaluate a comparison, such as "${VAR} == 12345". */ 545 static Token 546 EvalCompare(const char *lhs, bool lhsQuoted, 547 ComparisonOp op, const char *rhs, bool rhsQuoted) 548 { 549 double left, right; 550 551 if (!rhsQuoted && !lhsQuoted) 552 if (TryParseNumber(lhs, &left) && TryParseNumber(rhs, &right)) 553 return ToToken(EvalCompareNum(left, op, right)); 554 555 return EvalCompareStr(lhs, op, rhs); 556 } 557 558 static bool 559 CondParser_ComparisonOp(CondParser *par, ComparisonOp *out_op) 560 { 561 const char *p = par->p; 562 563 if (p[0] == '<' && p[1] == '=') 564 return par->p += 2, *out_op = LE, true; 565 if (p[0] == '<') 566 return par->p += 1, *out_op = LT, true; 567 if (p[0] == '>' && p[1] == '=') 568 return par->p += 2, *out_op = GE, true; 569 if (p[0] == '>') 570 return par->p += 1, *out_op = GT, true; 571 if (p[0] == '=' && p[1] == '=') 572 return par->p += 2, *out_op = EQ, true; 573 if (p[0] == '!' && p[1] == '=') 574 return par->p += 2, *out_op = NE, true; 575 return false; 576 } 577 578 /* 579 * Parse a comparison condition such as: 580 * 581 * 0 582 * ${VAR:Mpattern} 583 * ${VAR} == value 584 * ${VAR:U0} < 12345 585 */ 586 static Token 587 CondParser_Comparison(CondParser *par, bool doEval) 588 { 589 Token t = TOK_ERROR; 590 FStr lhs, rhs; 591 ComparisonOp op; 592 bool lhsQuoted, rhsQuoted; 593 594 lhs = CondParser_Leaf(par, doEval, par->leftUnquotedOK, &lhsQuoted); 595 if (lhs.str == NULL) 596 goto done_lhs; 597 598 CondParser_SkipWhitespace(par); 599 600 if (!CondParser_ComparisonOp(par, &op)) { 601 t = ToToken(doEval && EvalTruthy(par, lhs.str, lhsQuoted)); 602 goto done_lhs; 603 } 604 605 CondParser_SkipWhitespace(par); 606 607 if (par->p[0] == '\0') { 608 Parse_Error(PARSE_FATAL, 609 "Missing right-hand side of operator \"%s\"", opname[op]); 610 goto done_lhs; 611 } 612 613 rhs = CondParser_Leaf(par, doEval, true, &rhsQuoted); 614 t = rhs.str == NULL ? TOK_ERROR 615 : !doEval ? TOK_FALSE 616 : EvalCompare(lhs.str, lhsQuoted, op, rhs.str, rhsQuoted); 617 FStr_Done(&rhs); 618 619 done_lhs: 620 FStr_Done(&lhs); 621 return t; 622 } 623 624 /* 625 * The argument to empty() is a variable name, optionally followed by 626 * modifiers. 627 */ 628 static bool 629 CondParser_FuncCallEmpty(CondParser *par, bool doEval, Token *out_token) 630 { 631 const char *p = par->p; 632 Token tok; 633 FStr val; 634 635 if (!skip_string(&p, "empty")) 636 return false; 637 638 cpp_skip_whitespace(&p); 639 if (*p != '(') 640 return false; 641 642 p--; /* Make p[1] point to the '('. */ 643 val = Var_Parse(&p, SCOPE_CMDLINE, doEval ? VARE_EVAL : VARE_PARSE); 644 if (val.str == var_Error) 645 tok = TOK_ERROR; 646 else { 647 cpp_skip_whitespace(&val.str); 648 tok = ToToken(doEval && val.str[0] == '\0'); 649 } 650 651 FStr_Done(&val); 652 *out_token = tok; 653 par->p = p; 654 return true; 655 } 656 657 /* Parse a function call expression, such as 'exists(${file})'. */ 658 static bool 659 CondParser_FuncCall(CondParser *par, bool doEval, Token *out_token) 660 { 661 char *arg; 662 const char *p = par->p; 663 bool (*fn)(const char *); 664 const char *fn_name = p; 665 666 if (skip_string(&p, "defined")) 667 fn = FuncDefined; 668 else if (skip_string(&p, "make")) 669 fn = FuncMake; 670 else if (skip_string(&p, "exists")) 671 fn = FuncExists; 672 else if (skip_string(&p, "target")) 673 fn = FuncTarget; 674 else if (skip_string(&p, "commands")) 675 fn = FuncCommands; 676 else 677 return false; 678 679 cpp_skip_whitespace(&p); 680 if (*p != '(') 681 return false; 682 683 arg = ParseFuncArg(&p, doEval, fn_name); 684 *out_token = ToToken(doEval && 685 arg != NULL && arg[0] != '\0' && fn(arg)); 686 free(arg); 687 688 par->p = p; 689 return true; 690 } 691 692 /* 693 * Parse a comparison that neither starts with '"' nor '$', such as the 694 * unusual 'bare == right' or '3 == ${VAR}', or a simple leaf without 695 * operator, which is a number, an expression or a string literal. 696 * 697 * TODO: Can this be merged into CondParser_Comparison? 698 */ 699 static Token 700 CondParser_ComparisonOrLeaf(CondParser *par, bool doEval) 701 { 702 Token t; 703 char *arg; 704 const char *p; 705 706 p = par->p; 707 if (ch_isdigit(p[0]) || p[0] == '-' || p[0] == '+') 708 return CondParser_Comparison(par, doEval); 709 710 /* 711 * Most likely we have a bare word to apply the default function to. 712 * However, ".if a == b" gets here when the "a" is unquoted and 713 * doesn't start with a '$'. This surprises people. 714 * If what follows the function argument is a '=' or '!' then the 715 * syntax would be invalid if we did "defined(a)" - so instead treat 716 * as an expression. 717 */ 718 /* 719 * XXX: In edge cases, an expression may be evaluated twice, 720 * see cond-token-plain.mk, keyword 'twice'. 721 */ 722 arg = ParseWord(&p, doEval); 723 assert(arg[0] != '\0'); 724 cpp_skip_hspace(&p); 725 726 if (*p == '=' || *p == '!' || *p == '<' || *p == '>') { 727 free(arg); 728 return CondParser_Comparison(par, doEval); 729 } 730 par->p = p; 731 732 /* 733 * Evaluate the argument using the default function. 734 * This path always treats .if as .ifdef. To get here, the character 735 * after .if must have been taken literally, so the argument cannot 736 * be empty - even if it contained an expression. 737 */ 738 t = ToToken(doEval && par->evalBare(arg) != par->negateEvalBare); 739 free(arg); 740 return t; 741 } 742 743 /* Return the next token or comparison result from the parser. */ 744 static Token 745 CondParser_Token(CondParser *par, bool doEval) 746 { 747 Token t; 748 749 t = par->curr; 750 if (t != TOK_NONE) { 751 par->curr = TOK_NONE; 752 return t; 753 } 754 755 cpp_skip_hspace(&par->p); 756 757 switch (par->p[0]) { 758 759 case '(': 760 par->p++; 761 return TOK_LPAREN; 762 763 case ')': 764 par->p++; 765 return TOK_RPAREN; 766 767 case '|': 768 par->p++; 769 if (par->p[0] == '|') 770 par->p++; 771 else { 772 Parse_Error(PARSE_FATAL, "Unknown operator \"|\""); 773 return TOK_ERROR; 774 } 775 return TOK_OR; 776 777 case '&': 778 par->p++; 779 if (par->p[0] == '&') 780 par->p++; 781 else { 782 Parse_Error(PARSE_FATAL, "Unknown operator \"&\""); 783 return TOK_ERROR; 784 } 785 return TOK_AND; 786 787 case '!': 788 par->p++; 789 return TOK_NOT; 790 791 case '#': /* XXX: see unit-tests/cond-token-plain.mk */ 792 case '\n': /* XXX: why should this end the condition? */ 793 /* Probably obsolete now, from 1993-03-21. */ 794 case '\0': 795 return TOK_EOF; 796 797 case '"': 798 case '$': 799 return CondParser_Comparison(par, doEval); 800 801 default: 802 if (CondParser_FuncCallEmpty(par, doEval, &t)) 803 return t; 804 if (CondParser_FuncCall(par, doEval, &t)) 805 return t; 806 return CondParser_ComparisonOrLeaf(par, doEval); 807 } 808 } 809 810 /* Skip the next token if it equals t. */ 811 static bool 812 CondParser_Skip(CondParser *par, Token t) 813 { 814 Token actual; 815 816 actual = CondParser_Token(par, false); 817 if (actual == t) 818 return true; 819 820 assert(par->curr == TOK_NONE); 821 assert(actual != TOK_NONE); 822 par->curr = actual; 823 return false; 824 } 825 826 /* 827 * Term -> '(' Or ')' 828 * Term -> '!' Term 829 * Term -> Leaf ComparisonOp Leaf 830 * Term -> Leaf 831 */ 832 static CondResult 833 CondParser_Term(CondParser *par, bool doEval) 834 { 835 CondResult res; 836 Token t; 837 bool neg = false; 838 839 while ((t = CondParser_Token(par, doEval)) == TOK_NOT) 840 neg = !neg; 841 842 if (t == TOK_TRUE || t == TOK_FALSE) 843 return neg == (t == TOK_FALSE) ? CR_TRUE : CR_FALSE; 844 845 if (t == TOK_LPAREN) { 846 res = CondParser_Or(par, doEval); 847 if (res == CR_ERROR) 848 return CR_ERROR; 849 if (CondParser_Token(par, doEval) != TOK_RPAREN) 850 return CR_ERROR; 851 return neg == (res == CR_FALSE) ? CR_TRUE : CR_FALSE; 852 } 853 854 return CR_ERROR; 855 } 856 857 /* 858 * And -> Term ('&&' Term)* 859 */ 860 static CondResult 861 CondParser_And(CondParser *par, bool doEval) 862 { 863 CondResult res, rhs; 864 865 res = CR_TRUE; 866 do { 867 if ((rhs = CondParser_Term(par, doEval)) == CR_ERROR) 868 return CR_ERROR; 869 if (rhs == CR_FALSE) { 870 res = CR_FALSE; 871 doEval = false; 872 } 873 } while (CondParser_Skip(par, TOK_AND)); 874 875 return res; 876 } 877 878 /* 879 * Or -> And ('||' And)* 880 */ 881 static CondResult 882 CondParser_Or(CondParser *par, bool doEval) 883 { 884 CondResult res, rhs; 885 886 res = CR_FALSE; 887 do { 888 if ((rhs = CondParser_And(par, doEval)) == CR_ERROR) 889 return CR_ERROR; 890 if (rhs == CR_TRUE) { 891 res = CR_TRUE; 892 doEval = false; 893 } 894 } while (CondParser_Skip(par, TOK_OR)); 895 896 return res; 897 } 898 899 /* 900 * Evaluate the condition, including any side effects from the 901 * expressions in the condition. The condition consists of &&, ||, !, 902 * function(arg), comparisons and parenthetical groupings thereof. 903 */ 904 static CondResult 905 CondEvalExpression(const char *cond, bool plain, 906 bool (*evalBare)(const char *), bool negate, 907 bool eprint, bool leftUnquotedOK) 908 { 909 CondParser par; 910 CondResult rval; 911 int parseErrorsBefore = parseErrors; 912 913 cpp_skip_hspace(&cond); 914 915 par.plain = plain; 916 par.evalBare = evalBare; 917 par.negateEvalBare = negate; 918 par.leftUnquotedOK = leftUnquotedOK; 919 par.p = cond; 920 par.curr = TOK_NONE; 921 922 DEBUG1(COND, "CondParser_Eval: %s\n", par.p); 923 rval = CondParser_Or(&par, true); 924 if (par.curr != TOK_EOF) 925 rval = CR_ERROR; 926 927 if (parseErrors != parseErrorsBefore) 928 rval = CR_ERROR; 929 else if (rval == CR_ERROR && eprint) 930 Parse_Error(PARSE_FATAL, "Malformed conditional \"%s\"", cond); 931 932 return rval; 933 } 934 935 /* 936 * Evaluate a condition in a :? modifier, such as 937 * ${"${VAR}" == value:?yes:no}. 938 */ 939 CondResult 940 Cond_EvalCondition(const char *cond) 941 { 942 return CondEvalExpression(cond, true, 943 FuncDefined, false, false, true); 944 } 945 946 static bool 947 IsEndif(const char *p) 948 { 949 return p[0] == 'e' && p[1] == 'n' && p[2] == 'd' && 950 p[3] == 'i' && p[4] == 'f' && !ch_isalpha(p[5]); 951 } 952 953 static bool 954 DetermineKindOfConditional(const char **pp, bool *out_plain, 955 bool (**out_evalBare)(const char *), 956 bool *out_negate) 957 { 958 const char *p = *pp + 2; 959 960 *out_plain = false; 961 *out_evalBare = FuncDefined; 962 *out_negate = skip_string(&p, "n"); 963 964 if (skip_string(&p, "def")) { /* .ifdef and .ifndef */ 965 } else if (skip_string(&p, "make")) /* .ifmake and .ifnmake */ 966 *out_evalBare = FuncMake; 967 else if (!*out_negate) /* plain .if */ 968 *out_plain = true; 969 else 970 goto unknown_directive; 971 if (ch_isalpha(*p)) 972 goto unknown_directive; 973 974 *pp = p; 975 return true; 976 977 unknown_directive: 978 return false; 979 } 980 981 /* 982 * Evaluate the conditional directive in the line, which is one of: 983 * 984 * .if <cond> 985 * .ifmake <cond> 986 * .ifnmake <cond> 987 * .ifdef <cond> 988 * .ifndef <cond> 989 * .elif <cond> 990 * .elifmake <cond> 991 * .elifnmake <cond> 992 * .elifdef <cond> 993 * .elifndef <cond> 994 * .else 995 * .endif 996 * 997 * In these directives, <cond> consists of &&, ||, !, function(arg), 998 * comparisons, expressions, bare words, numbers and strings, and 999 * parenthetical groupings thereof. 1000 * 1001 * Results: 1002 * CR_TRUE to continue parsing the lines that follow the 1003 * conditional (when <cond> evaluates to true) 1004 * CR_FALSE to skip the lines after the conditional 1005 * (when <cond> evaluates to false, or when a previous 1006 * branch was already taken) 1007 * CR_ERROR if the conditional was not valid, either because of 1008 * a syntax error or because some variable was undefined 1009 * or because the condition could not be evaluated 1010 */ 1011 CondResult 1012 Cond_EvalLine(const char *line) 1013 { 1014 typedef enum { 1015 1016 /* None of the previous <cond> evaluated to true. */ 1017 IFS_INITIAL = 0, 1018 1019 /* 1020 * The previous <cond> evaluated to true. The lines following 1021 * this condition are interpreted. 1022 */ 1023 IFS_ACTIVE = 1 << 0, 1024 1025 /* The previous directive was an '.else'. */ 1026 IFS_SEEN_ELSE = 1 << 1, 1027 1028 /* One of the previous <cond> evaluated to true. */ 1029 IFS_WAS_ACTIVE = 1 << 2 1030 1031 } IfState; 1032 1033 static IfState *cond_states = NULL; 1034 static unsigned cond_states_cap = 128; 1035 1036 bool plain; 1037 bool (*evalBare)(const char *); 1038 bool negate; 1039 bool isElif; 1040 CondResult res; 1041 IfState state; 1042 const char *p = line; 1043 1044 if (cond_states == NULL) { 1045 cond_states = bmake_malloc( 1046 cond_states_cap * sizeof *cond_states); 1047 cond_states[0] = IFS_ACTIVE; 1048 } 1049 1050 p++; /* skip the leading '.' */ 1051 cpp_skip_hspace(&p); 1052 1053 if (IsEndif(p)) { 1054 if (p[5] != '\0') { 1055 Parse_Error(PARSE_FATAL, 1056 "The .endif directive does not take arguments"); 1057 } 1058 1059 if (cond_depth == CurFile_CondMinDepth()) { 1060 Parse_Error(PARSE_FATAL, "if-less endif"); 1061 return CR_TRUE; 1062 } 1063 1064 /* Return state for previous conditional */ 1065 cond_depth--; 1066 Parse_GuardEndif(); 1067 return cond_states[cond_depth] & IFS_ACTIVE 1068 ? CR_TRUE : CR_FALSE; 1069 } 1070 1071 /* Parse the name of the directive, such as 'if', 'elif', 'endif'. */ 1072 if (p[0] == 'e') { 1073 if (p[1] != 'l') 1074 return CR_ERROR; 1075 1076 /* Quite likely this is 'else' or 'elif' */ 1077 p += 2; 1078 if (strncmp(p, "se", 2) == 0 && !ch_isalpha(p[2])) { 1079 if (p[2] != '\0') 1080 Parse_Error(PARSE_FATAL, 1081 "The .else directive " 1082 "does not take arguments"); 1083 1084 if (cond_depth == CurFile_CondMinDepth()) { 1085 Parse_Error(PARSE_FATAL, "if-less else"); 1086 return CR_TRUE; 1087 } 1088 Parse_GuardElse(); 1089 1090 state = cond_states[cond_depth]; 1091 if (state == IFS_INITIAL) { 1092 state = IFS_ACTIVE | IFS_SEEN_ELSE; 1093 } else { 1094 if (state & IFS_SEEN_ELSE) 1095 Parse_Error(PARSE_WARNING, 1096 "extra else"); 1097 state = IFS_WAS_ACTIVE | IFS_SEEN_ELSE; 1098 } 1099 cond_states[cond_depth] = state; 1100 1101 return state & IFS_ACTIVE ? CR_TRUE : CR_FALSE; 1102 } 1103 /* Assume for now it is an elif */ 1104 isElif = true; 1105 } else 1106 isElif = false; 1107 1108 if (p[0] != 'i' || p[1] != 'f') 1109 return CR_ERROR; 1110 1111 if (!DetermineKindOfConditional(&p, &plain, &evalBare, &negate)) 1112 return CR_ERROR; 1113 1114 if (isElif) { 1115 if (cond_depth == CurFile_CondMinDepth()) { 1116 Parse_Error(PARSE_FATAL, "if-less elif"); 1117 return CR_TRUE; 1118 } 1119 Parse_GuardElse(); 1120 state = cond_states[cond_depth]; 1121 if (state & IFS_SEEN_ELSE) { 1122 Parse_Error(PARSE_WARNING, "extra elif"); 1123 cond_states[cond_depth] = 1124 IFS_WAS_ACTIVE | IFS_SEEN_ELSE; 1125 return CR_FALSE; 1126 } 1127 if (state != IFS_INITIAL) { 1128 cond_states[cond_depth] = IFS_WAS_ACTIVE; 1129 return CR_FALSE; 1130 } 1131 } else { 1132 /* Normal .if */ 1133 if (cond_depth + 1 >= cond_states_cap) { 1134 /* 1135 * This is rare, but not impossible. 1136 * In meta mode, dirdeps.mk (only runs at level 0) 1137 * can need more than the default. 1138 */ 1139 cond_states_cap += 32; 1140 cond_states = bmake_realloc(cond_states, 1141 cond_states_cap * sizeof *cond_states); 1142 } 1143 state = cond_states[cond_depth]; 1144 cond_depth++; 1145 if (!(state & IFS_ACTIVE)) { 1146 cond_states[cond_depth] = IFS_WAS_ACTIVE; 1147 return CR_FALSE; 1148 } 1149 } 1150 1151 res = CondEvalExpression(p, plain, evalBare, negate, true, false); 1152 if (res == CR_ERROR) { 1153 /* Syntax error, error message already output. */ 1154 /* Skip everything to the matching '.endif'. */ 1155 /* An extra '.else' is not detected in this case. */ 1156 cond_states[cond_depth] = IFS_WAS_ACTIVE; 1157 return CR_FALSE; 1158 } 1159 1160 cond_states[cond_depth] = res == CR_TRUE ? IFS_ACTIVE : IFS_INITIAL; 1161 return res; 1162 } 1163 1164 static bool 1165 ParseVarnameGuard(const char **pp, const char **varname) 1166 { 1167 const char *p = *pp; 1168 1169 if (ch_isalpha(*p) || *p == '_') { 1170 while (ch_isalnum(*p) || *p == '_') 1171 p++; 1172 *varname = *pp; 1173 *pp = p; 1174 return true; 1175 } 1176 return false; 1177 } 1178 1179 /* Extracts the multiple-inclusion guard from a conditional, if any. */ 1180 Guard * 1181 Cond_ExtractGuard(const char *line) 1182 { 1183 const char *p, *varname; 1184 Substring dir; 1185 Guard *guard; 1186 1187 p = line + 1; /* skip the '.' */ 1188 cpp_skip_hspace(&p); 1189 1190 dir.start = p; 1191 while (ch_isalpha(*p)) 1192 p++; 1193 dir.end = p; 1194 cpp_skip_hspace(&p); 1195 1196 if (Substring_Equals(dir, "if")) { 1197 if (skip_string(&p, "!defined(")) { 1198 if (ParseVarnameGuard(&p, &varname) 1199 && strcmp(p, ")") == 0) 1200 goto found_variable; 1201 } else if (skip_string(&p, "!target(")) { 1202 const char *arg_p = p; 1203 free(ParseWord(&p, false)); 1204 if (strcmp(p, ")") == 0) { 1205 guard = bmake_malloc(sizeof(*guard)); 1206 guard->kind = GK_TARGET; 1207 guard->name = ParseWord(&arg_p, true); 1208 return guard; 1209 } 1210 } 1211 } else if (Substring_Equals(dir, "ifndef")) { 1212 if (ParseVarnameGuard(&p, &varname) && *p == '\0') 1213 goto found_variable; 1214 } 1215 return NULL; 1216 1217 found_variable: 1218 guard = bmake_malloc(sizeof(*guard)); 1219 guard->kind = GK_VARIABLE; 1220 guard->name = bmake_strsedup(varname, p); 1221 return guard; 1222 } 1223 1224 void 1225 Cond_EndFile(void) 1226 { 1227 unsigned open_conds = cond_depth - CurFile_CondMinDepth(); 1228 1229 if (open_conds != 0) { 1230 Parse_Error(PARSE_FATAL, "%u open conditional%s", 1231 open_conds, open_conds == 1 ? "" : "s"); 1232 cond_depth = CurFile_CondMinDepth(); 1233 } 1234 } 1235