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