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