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