1 /* $NetBSD: var.c,v 1.184 2013/09/04 15:38:26 sjg Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1989 by Berkeley Softworks 37 * All rights reserved. 38 * 39 * This code is derived from software contributed to Berkeley by 40 * Adam de Boor. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by the University of 53 * California, Berkeley and its contributors. 54 * 4. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 */ 70 71 #ifndef MAKE_NATIVE 72 static char rcsid[] = "$NetBSD: var.c,v 1.184 2013/09/04 15:38:26 sjg Exp $"; 73 #else 74 #include <sys/cdefs.h> 75 #ifndef lint 76 #if 0 77 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; 78 #else 79 __RCSID("$NetBSD: var.c,v 1.184 2013/09/04 15:38:26 sjg Exp $"); 80 #endif 81 #endif /* not lint */ 82 #endif 83 84 /*- 85 * var.c -- 86 * Variable-handling functions 87 * 88 * Interface: 89 * Var_Set Set the value of a variable in the given 90 * context. The variable is created if it doesn't 91 * yet exist. The value and variable name need not 92 * be preserved. 93 * 94 * Var_Append Append more characters to an existing variable 95 * in the given context. The variable needn't 96 * exist already -- it will be created if it doesn't. 97 * A space is placed between the old value and the 98 * new one. 99 * 100 * Var_Exists See if a variable exists. 101 * 102 * Var_Value Return the value of a variable in a context or 103 * NULL if the variable is undefined. 104 * 105 * Var_Subst Substitute named variable, or all variables if 106 * NULL in a string using 107 * the given context as the top-most one. If the 108 * third argument is non-zero, Parse_Error is 109 * called if any variables are undefined. 110 * 111 * Var_Parse Parse a variable expansion from a string and 112 * return the result and the number of characters 113 * consumed. 114 * 115 * Var_Delete Delete a variable in a context. 116 * 117 * Var_Init Initialize this module. 118 * 119 * Debugging: 120 * Var_Dump Print out all variables defined in the given 121 * context. 122 * 123 * XXX: There's a lot of duplication in these functions. 124 */ 125 126 #include <sys/stat.h> 127 #ifndef NO_REGEX 128 #include <sys/types.h> 129 #include <regex.h> 130 #endif 131 #include <ctype.h> 132 #include <inttypes.h> 133 #include <stdlib.h> 134 #include <limits.h> 135 #include <time.h> 136 137 #include "make.h" 138 #include "buf.h" 139 #include "dir.h" 140 #include "job.h" 141 142 extern int makelevel; 143 /* 144 * XXX transition hack for FreeBSD ports. 145 * bsd.port.mk can set .MAKE.FreeBSD_UL=yes 146 * to cause us to treat :[LU] as aliases for :t[lu] 147 * To be reverted when ports converts to :t[lu] (when 8.3 is EOL) 148 */ 149 #define MAKE_FREEBSD_UL ".MAKE.FreeBSD_UL" 150 #ifdef MAKE_FREEBSD_UL 151 static int FreeBSD_UL = FALSE; 152 #endif 153 154 /* 155 * This lets us tell if we have replaced the original environ 156 * (which we cannot free). 157 */ 158 char **savedEnv = NULL; 159 160 /* 161 * This is a harmless return value for Var_Parse that can be used by Var_Subst 162 * to determine if there was an error in parsing -- easier than returning 163 * a flag, as things outside this module don't give a hoot. 164 */ 165 char var_Error[] = ""; 166 167 /* 168 * Similar to var_Error, but returned when the 'errnum' flag for Var_Parse is 169 * set false. Why not just use a constant? Well, gcc likes to condense 170 * identical string instances... 171 */ 172 static char varNoError[] = ""; 173 174 /* 175 * Internally, variables are contained in four different contexts. 176 * 1) the environment. They may not be changed. If an environment 177 * variable is appended-to, the result is placed in the global 178 * context. 179 * 2) the global context. Variables set in the Makefile are located in 180 * the global context. It is the penultimate context searched when 181 * substituting. 182 * 3) the command-line context. All variables set on the command line 183 * are placed in this context. They are UNALTERABLE once placed here. 184 * 4) the local context. Each target has associated with it a context 185 * list. On this list are located the structures describing such 186 * local variables as $(@) and $(*) 187 * The four contexts are searched in the reverse order from which they are 188 * listed. 189 */ 190 GNode *VAR_INTERNAL; /* variables from make itself */ 191 GNode *VAR_GLOBAL; /* variables from the makefile */ 192 GNode *VAR_CMD; /* variables defined on the command-line */ 193 194 #define FIND_CMD 0x1 /* look in VAR_CMD when searching */ 195 #define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */ 196 #define FIND_ENV 0x4 /* look in the environment also */ 197 198 typedef struct Var { 199 char *name; /* the variable's name */ 200 Buffer val; /* its value */ 201 int flags; /* miscellaneous status flags */ 202 #define VAR_IN_USE 1 /* Variable's value currently being used. 203 * Used to avoid recursion */ 204 #define VAR_FROM_ENV 2 /* Variable comes from the environment */ 205 #define VAR_JUNK 4 /* Variable is a junk variable that 206 * should be destroyed when done with 207 * it. Used by Var_Parse for undefined, 208 * modified variables */ 209 #define VAR_KEEP 8 /* Variable is VAR_JUNK, but we found 210 * a use for it in some modifier and 211 * the value is therefore valid */ 212 #define VAR_EXPORTED 16 /* Variable is exported */ 213 #define VAR_REEXPORT 32 /* Indicate if var needs re-export. 214 * This would be true if it contains $'s 215 */ 216 #define VAR_FROM_CMD 64 /* Variable came from command line */ 217 } Var; 218 219 /* 220 * Exporting vars is expensive so skip it if we can 221 */ 222 #define VAR_EXPORTED_NONE 0 223 #define VAR_EXPORTED_YES 1 224 #define VAR_EXPORTED_ALL 2 225 static int var_exportedVars = VAR_EXPORTED_NONE; 226 /* 227 * We pass this to Var_Export when doing the initial export 228 * or after updating an exported var. 229 */ 230 #define VAR_EXPORT_PARENT 1 231 232 /* Var*Pattern flags */ 233 #define VAR_SUB_GLOBAL 0x01 /* Apply substitution globally */ 234 #define VAR_SUB_ONE 0x02 /* Apply substitution to one word */ 235 #define VAR_SUB_MATCHED 0x04 /* There was a match */ 236 #define VAR_MATCH_START 0x08 /* Match at start of word */ 237 #define VAR_MATCH_END 0x10 /* Match at end of word */ 238 #define VAR_NOSUBST 0x20 /* don't expand vars in VarGetPattern */ 239 240 /* Var_Set flags */ 241 #define VAR_NO_EXPORT 0x01 /* do not export */ 242 243 typedef struct { 244 /* 245 * The following fields are set by Var_Parse() when it 246 * encounters modifiers that need to keep state for use by 247 * subsequent modifiers within the same variable expansion. 248 */ 249 Byte varSpace; /* Word separator in expansions */ 250 Boolean oneBigWord; /* TRUE if we will treat the variable as a 251 * single big word, even if it contains 252 * embedded spaces (as opposed to the 253 * usual behaviour of treating it as 254 * several space-separated words). */ 255 } Var_Parse_State; 256 257 /* struct passed as 'void *' to VarSubstitute() for ":S/lhs/rhs/", 258 * to VarSYSVMatch() for ":lhs=rhs". */ 259 typedef struct { 260 const char *lhs; /* String to match */ 261 int leftLen; /* Length of string */ 262 const char *rhs; /* Replacement string (w/ &'s removed) */ 263 int rightLen; /* Length of replacement */ 264 int flags; 265 } VarPattern; 266 267 /* struct passed as 'void *' to VarLoopExpand() for ":@tvar@str@" */ 268 typedef struct { 269 GNode *ctxt; /* variable context */ 270 char *tvar; /* name of temp var */ 271 int tvarLen; 272 char *str; /* string to expand */ 273 int strLen; 274 int errnum; /* errnum for not defined */ 275 } VarLoop_t; 276 277 #ifndef NO_REGEX 278 /* struct passed as 'void *' to VarRESubstitute() for ":C///" */ 279 typedef struct { 280 regex_t re; 281 int nsub; 282 regmatch_t *matches; 283 char *replace; 284 int flags; 285 } VarREPattern; 286 #endif 287 288 /* struct passed to VarSelectWords() for ":[start..end]" */ 289 typedef struct { 290 int start; /* first word to select */ 291 int end; /* last word to select */ 292 } VarSelectWords_t; 293 294 static Var *VarFind(const char *, GNode *, int); 295 static void VarAdd(const char *, const char *, GNode *); 296 static Boolean VarHead(GNode *, Var_Parse_State *, 297 char *, Boolean, Buffer *, void *); 298 static Boolean VarTail(GNode *, Var_Parse_State *, 299 char *, Boolean, Buffer *, void *); 300 static Boolean VarSuffix(GNode *, Var_Parse_State *, 301 char *, Boolean, Buffer *, void *); 302 static Boolean VarRoot(GNode *, Var_Parse_State *, 303 char *, Boolean, Buffer *, void *); 304 static Boolean VarMatch(GNode *, Var_Parse_State *, 305 char *, Boolean, Buffer *, void *); 306 #ifdef SYSVVARSUB 307 static Boolean VarSYSVMatch(GNode *, Var_Parse_State *, 308 char *, Boolean, Buffer *, void *); 309 #endif 310 static Boolean VarNoMatch(GNode *, Var_Parse_State *, 311 char *, Boolean, Buffer *, void *); 312 #ifndef NO_REGEX 313 static void VarREError(int, regex_t *, const char *); 314 static Boolean VarRESubstitute(GNode *, Var_Parse_State *, 315 char *, Boolean, Buffer *, void *); 316 #endif 317 static Boolean VarSubstitute(GNode *, Var_Parse_State *, 318 char *, Boolean, Buffer *, void *); 319 static Boolean VarLoopExpand(GNode *, Var_Parse_State *, 320 char *, Boolean, Buffer *, void *); 321 static char *VarGetPattern(GNode *, Var_Parse_State *, 322 int, const char **, int, int *, int *, 323 VarPattern *); 324 static char *VarQuote(char *); 325 static char *VarHash(char *); 326 static char *VarModify(GNode *, Var_Parse_State *, 327 const char *, 328 Boolean (*)(GNode *, Var_Parse_State *, char *, Boolean, Buffer *, void *), 329 void *); 330 static char *VarOrder(const char *, const char); 331 static char *VarUniq(const char *); 332 static int VarWordCompare(const void *, const void *); 333 static void VarPrintVar(void *); 334 335 #define BROPEN '{' 336 #define BRCLOSE '}' 337 #define PROPEN '(' 338 #define PRCLOSE ')' 339 340 /*- 341 *----------------------------------------------------------------------- 342 * VarFind -- 343 * Find the given variable in the given context and any other contexts 344 * indicated. 345 * 346 * Input: 347 * name name to find 348 * ctxt context in which to find it 349 * flags FIND_GLOBAL set means to look in the 350 * VAR_GLOBAL context as well. FIND_CMD set means 351 * to look in the VAR_CMD context also. FIND_ENV 352 * set means to look in the environment 353 * 354 * Results: 355 * A pointer to the structure describing the desired variable or 356 * NULL if the variable does not exist. 357 * 358 * Side Effects: 359 * None 360 *----------------------------------------------------------------------- 361 */ 362 static Var * 363 VarFind(const char *name, GNode *ctxt, int flags) 364 { 365 Hash_Entry *var; 366 Var *v; 367 368 /* 369 * If the variable name begins with a '.', it could very well be one of 370 * the local ones. We check the name against all the local variables 371 * and substitute the short version in for 'name' if it matches one of 372 * them. 373 */ 374 if (*name == '.' && isupper((unsigned char) name[1])) 375 switch (name[1]) { 376 case 'A': 377 if (!strcmp(name, ".ALLSRC")) 378 name = ALLSRC; 379 if (!strcmp(name, ".ARCHIVE")) 380 name = ARCHIVE; 381 break; 382 case 'I': 383 if (!strcmp(name, ".IMPSRC")) 384 name = IMPSRC; 385 break; 386 case 'M': 387 if (!strcmp(name, ".MEMBER")) 388 name = MEMBER; 389 break; 390 case 'O': 391 if (!strcmp(name, ".OODATE")) 392 name = OODATE; 393 break; 394 case 'P': 395 if (!strcmp(name, ".PREFIX")) 396 name = PREFIX; 397 break; 398 case 'T': 399 if (!strcmp(name, ".TARGET")) 400 name = TARGET; 401 break; 402 } 403 #ifdef notyet 404 /* for compatibility with gmake */ 405 if (name[0] == '^' && name[1] == '\0') 406 name = ALLSRC; 407 #endif 408 409 /* 410 * First look for the variable in the given context. If it's not there, 411 * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order, 412 * depending on the FIND_* flags in 'flags' 413 */ 414 var = Hash_FindEntry(&ctxt->context, name); 415 416 if ((var == NULL) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) { 417 var = Hash_FindEntry(&VAR_CMD->context, name); 418 } 419 if (!checkEnvFirst && (var == NULL) && (flags & FIND_GLOBAL) && 420 (ctxt != VAR_GLOBAL)) 421 { 422 var = Hash_FindEntry(&VAR_GLOBAL->context, name); 423 if ((var == NULL) && (ctxt != VAR_INTERNAL)) { 424 /* VAR_INTERNAL is subordinate to VAR_GLOBAL */ 425 var = Hash_FindEntry(&VAR_INTERNAL->context, name); 426 } 427 } 428 if ((var == NULL) && (flags & FIND_ENV)) { 429 char *env; 430 431 if ((env = getenv(name)) != NULL) { 432 int len; 433 434 v = bmake_malloc(sizeof(Var)); 435 v->name = bmake_strdup(name); 436 437 len = strlen(env); 438 439 Buf_Init(&v->val, len + 1); 440 Buf_AddBytes(&v->val, len, env); 441 442 v->flags = VAR_FROM_ENV; 443 return (v); 444 } else if (checkEnvFirst && (flags & FIND_GLOBAL) && 445 (ctxt != VAR_GLOBAL)) 446 { 447 var = Hash_FindEntry(&VAR_GLOBAL->context, name); 448 if ((var == NULL) && (ctxt != VAR_INTERNAL)) { 449 var = Hash_FindEntry(&VAR_INTERNAL->context, name); 450 } 451 if (var == NULL) { 452 return NULL; 453 } else { 454 return ((Var *)Hash_GetValue(var)); 455 } 456 } else { 457 return NULL; 458 } 459 } else if (var == NULL) { 460 return NULL; 461 } else { 462 return ((Var *)Hash_GetValue(var)); 463 } 464 } 465 466 /*- 467 *----------------------------------------------------------------------- 468 * VarFreeEnv -- 469 * If the variable is an environment variable, free it 470 * 471 * Input: 472 * v the variable 473 * destroy true if the value buffer should be destroyed. 474 * 475 * Results: 476 * 1 if it is an environment variable 0 ow. 477 * 478 * Side Effects: 479 * The variable is free'ed if it is an environent variable. 480 *----------------------------------------------------------------------- 481 */ 482 static Boolean 483 VarFreeEnv(Var *v, Boolean destroy) 484 { 485 if ((v->flags & VAR_FROM_ENV) == 0) 486 return FALSE; 487 free(v->name); 488 Buf_Destroy(&v->val, destroy); 489 free(v); 490 return TRUE; 491 } 492 493 /*- 494 *----------------------------------------------------------------------- 495 * VarAdd -- 496 * Add a new variable of name name and value val to the given context 497 * 498 * Input: 499 * name name of variable to add 500 * val value to set it to 501 * ctxt context in which to set it 502 * 503 * Results: 504 * None 505 * 506 * Side Effects: 507 * The new variable is placed at the front of the given context 508 * The name and val arguments are duplicated so they may 509 * safely be freed. 510 *----------------------------------------------------------------------- 511 */ 512 static void 513 VarAdd(const char *name, const char *val, GNode *ctxt) 514 { 515 Var *v; 516 int len; 517 Hash_Entry *h; 518 519 v = bmake_malloc(sizeof(Var)); 520 521 len = val ? strlen(val) : 0; 522 Buf_Init(&v->val, len+1); 523 Buf_AddBytes(&v->val, len, val); 524 525 v->flags = 0; 526 527 h = Hash_CreateEntry(&ctxt->context, name, NULL); 528 Hash_SetValue(h, v); 529 v->name = h->name; 530 if (DEBUG(VAR)) { 531 fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val); 532 } 533 } 534 535 /*- 536 *----------------------------------------------------------------------- 537 * Var_Delete -- 538 * Remove a variable from a context. 539 * 540 * Results: 541 * None. 542 * 543 * Side Effects: 544 * The Var structure is removed and freed. 545 * 546 *----------------------------------------------------------------------- 547 */ 548 void 549 Var_Delete(const char *name, GNode *ctxt) 550 { 551 Hash_Entry *ln; 552 char *cp; 553 554 if (strchr(name, '$')) { 555 cp = Var_Subst(NULL, name, VAR_GLOBAL, 0); 556 } else { 557 cp = (char *)name; 558 } 559 ln = Hash_FindEntry(&ctxt->context, cp); 560 if (DEBUG(VAR)) { 561 fprintf(debug_file, "%s:delete %s%s\n", 562 ctxt->name, cp, ln ? "" : " (not found)"); 563 } 564 if (cp != name) { 565 free(cp); 566 } 567 if (ln != NULL) { 568 Var *v; 569 570 v = (Var *)Hash_GetValue(ln); 571 if ((v->flags & VAR_EXPORTED)) { 572 unsetenv(v->name); 573 } 574 if (strcmp(MAKE_EXPORTED, v->name) == 0) { 575 var_exportedVars = VAR_EXPORTED_NONE; 576 } 577 if (v->name != ln->name) 578 free(v->name); 579 Hash_DeleteEntry(&ctxt->context, ln); 580 Buf_Destroy(&v->val, TRUE); 581 free(v); 582 } 583 } 584 585 586 /* 587 * Export a var. 588 * We ignore make internal variables (those which start with '.') 589 * Also we jump through some hoops to avoid calling setenv 590 * more than necessary since it can leak. 591 * We only manipulate flags of vars if 'parent' is set. 592 */ 593 static int 594 Var_Export1(const char *name, int parent) 595 { 596 char tmp[BUFSIZ]; 597 Var *v; 598 char *val = NULL; 599 int n; 600 601 if (*name == '.') 602 return 0; /* skip internals */ 603 if (!name[1]) { 604 /* 605 * A single char. 606 * If it is one of the vars that should only appear in 607 * local context, skip it, else we can get Var_Subst 608 * into a loop. 609 */ 610 switch (name[0]) { 611 case '@': 612 case '%': 613 case '*': 614 case '!': 615 return 0; 616 } 617 } 618 v = VarFind(name, VAR_GLOBAL, 0); 619 if (v == NULL) { 620 return 0; 621 } 622 if (!parent && 623 (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) { 624 return 0; /* nothing to do */ 625 } 626 val = Buf_GetAll(&v->val, NULL); 627 if (strchr(val, '$')) { 628 if (parent) { 629 /* 630 * Flag this as something we need to re-export. 631 * No point actually exporting it now though, 632 * the child can do it at the last minute. 633 */ 634 v->flags |= (VAR_EXPORTED|VAR_REEXPORT); 635 return 1; 636 } 637 if (v->flags & VAR_IN_USE) { 638 /* 639 * We recursed while exporting in a child. 640 * This isn't going to end well, just skip it. 641 */ 642 return 0; 643 } 644 n = snprintf(tmp, sizeof(tmp), "${%s}", name); 645 if (n < (int)sizeof(tmp)) { 646 val = Var_Subst(NULL, tmp, VAR_GLOBAL, 0); 647 setenv(name, val, 1); 648 free(val); 649 } 650 } else { 651 if (parent) { 652 v->flags &= ~VAR_REEXPORT; /* once will do */ 653 } 654 if (parent || !(v->flags & VAR_EXPORTED)) { 655 setenv(name, val, 1); 656 } 657 } 658 /* 659 * This is so Var_Set knows to call Var_Export again... 660 */ 661 if (parent) { 662 v->flags |= VAR_EXPORTED; 663 } 664 return 1; 665 } 666 667 /* 668 * This gets called from our children. 669 */ 670 void 671 Var_ExportVars(void) 672 { 673 char tmp[BUFSIZ]; 674 Hash_Entry *var; 675 Hash_Search state; 676 Var *v; 677 char *val; 678 int n; 679 680 /* 681 * Several make's support this sort of mechanism for tracking 682 * recursion - but each uses a different name. 683 * We allow the makefiles to update MAKELEVEL and ensure 684 * children see a correctly incremented value. 685 */ 686 snprintf(tmp, sizeof(tmp), "%d", makelevel + 1); 687 setenv(MAKE_LEVEL_ENV, tmp, 1); 688 689 if (VAR_EXPORTED_NONE == var_exportedVars) 690 return; 691 692 if (VAR_EXPORTED_ALL == var_exportedVars) { 693 /* 694 * Ouch! This is crazy... 695 */ 696 for (var = Hash_EnumFirst(&VAR_GLOBAL->context, &state); 697 var != NULL; 698 var = Hash_EnumNext(&state)) { 699 v = (Var *)Hash_GetValue(var); 700 Var_Export1(v->name, 0); 701 } 702 return; 703 } 704 /* 705 * We have a number of exported vars, 706 */ 707 n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}"); 708 if (n < (int)sizeof(tmp)) { 709 char **av; 710 char *as; 711 int ac; 712 int i; 713 714 val = Var_Subst(NULL, tmp, VAR_GLOBAL, 0); 715 av = brk_string(val, &ac, FALSE, &as); 716 for (i = 0; i < ac; i++) { 717 Var_Export1(av[i], 0); 718 } 719 free(val); 720 free(as); 721 free(av); 722 } 723 } 724 725 /* 726 * This is called when .export is seen or 727 * .MAKE.EXPORTED is modified. 728 * It is also called when any exported var is modified. 729 */ 730 void 731 Var_Export(char *str, int isExport) 732 { 733 char *name; 734 char *val; 735 char **av; 736 char *as; 737 int track; 738 int ac; 739 int i; 740 741 if (isExport && (!str || !str[0])) { 742 var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */ 743 return; 744 } 745 746 if (strncmp(str, "-env", 4) == 0) { 747 track = 0; 748 str += 4; 749 } else { 750 track = VAR_EXPORT_PARENT; 751 } 752 val = Var_Subst(NULL, str, VAR_GLOBAL, 0); 753 av = brk_string(val, &ac, FALSE, &as); 754 for (i = 0; i < ac; i++) { 755 name = av[i]; 756 if (!name[1]) { 757 /* 758 * A single char. 759 * If it is one of the vars that should only appear in 760 * local context, skip it, else we can get Var_Subst 761 * into a loop. 762 */ 763 switch (name[0]) { 764 case '@': 765 case '%': 766 case '*': 767 case '!': 768 continue; 769 } 770 } 771 if (Var_Export1(name, track)) { 772 if (VAR_EXPORTED_ALL != var_exportedVars) 773 var_exportedVars = VAR_EXPORTED_YES; 774 if (isExport && track) { 775 Var_Append(MAKE_EXPORTED, name, VAR_GLOBAL); 776 } 777 } 778 } 779 free(val); 780 free(as); 781 free(av); 782 } 783 784 785 /* 786 * This is called when .unexport[-env] is seen. 787 */ 788 extern char **environ; 789 790 void 791 Var_UnExport(char *str) 792 { 793 char tmp[BUFSIZ]; 794 char *vlist; 795 char *cp; 796 Boolean unexport_env; 797 int n; 798 799 if (!str || !str[0]) { 800 return; /* assert? */ 801 } 802 803 vlist = NULL; 804 805 str += 8; 806 unexport_env = (strncmp(str, "-env", 4) == 0); 807 if (unexport_env) { 808 char **newenv; 809 810 cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */ 811 if (environ == savedEnv) { 812 /* we have been here before! */ 813 newenv = bmake_realloc(environ, 2 * sizeof(char *)); 814 } else { 815 if (savedEnv) { 816 free(savedEnv); 817 savedEnv = NULL; 818 } 819 newenv = bmake_malloc(2 * sizeof(char *)); 820 } 821 if (!newenv) 822 return; 823 /* Note: we cannot safely free() the original environ. */ 824 environ = savedEnv = newenv; 825 newenv[0] = NULL; 826 newenv[1] = NULL; 827 setenv(MAKE_LEVEL_ENV, cp, 1); 828 } else { 829 for (; *str != '\n' && isspace((unsigned char) *str); str++) 830 continue; 831 if (str[0] && str[0] != '\n') { 832 vlist = str; 833 } 834 } 835 836 if (!vlist) { 837 /* Using .MAKE.EXPORTED */ 838 n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}"); 839 if (n < (int)sizeof(tmp)) { 840 vlist = Var_Subst(NULL, tmp, VAR_GLOBAL, 0); 841 } 842 } 843 if (vlist) { 844 Var *v; 845 char **av; 846 char *as; 847 int ac; 848 int i; 849 850 av = brk_string(vlist, &ac, FALSE, &as); 851 for (i = 0; i < ac; i++) { 852 v = VarFind(av[i], VAR_GLOBAL, 0); 853 if (!v) 854 continue; 855 if (!unexport_env && 856 (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) { 857 unsetenv(v->name); 858 } 859 v->flags &= ~(VAR_EXPORTED|VAR_REEXPORT); 860 /* 861 * If we are unexporting a list, 862 * remove each one from .MAKE.EXPORTED. 863 * If we are removing them all, 864 * just delete .MAKE.EXPORTED below. 865 */ 866 if (vlist == str) { 867 n = snprintf(tmp, sizeof(tmp), 868 "${" MAKE_EXPORTED ":N%s}", v->name); 869 if (n < (int)sizeof(tmp)) { 870 cp = Var_Subst(NULL, tmp, VAR_GLOBAL, 0); 871 Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL, 0); 872 free(cp); 873 } 874 } 875 } 876 free(as); 877 free(av); 878 if (vlist != str) { 879 Var_Delete(MAKE_EXPORTED, VAR_GLOBAL); 880 free(vlist); 881 } 882 } 883 } 884 885 /*- 886 *----------------------------------------------------------------------- 887 * Var_Set -- 888 * Set the variable name to the value val in the given context. 889 * 890 * Input: 891 * name name of variable to set 892 * val value to give to the variable 893 * ctxt context in which to set it 894 * 895 * Results: 896 * None. 897 * 898 * Side Effects: 899 * If the variable doesn't yet exist, a new record is created for it. 900 * Else the old value is freed and the new one stuck in its place 901 * 902 * Notes: 903 * The variable is searched for only in its context before being 904 * created in that context. I.e. if the context is VAR_GLOBAL, 905 * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only 906 * VAR_CMD->context is searched. This is done to avoid the literally 907 * thousands of unnecessary strcmp's that used to be done to 908 * set, say, $(@) or $(<). 909 * If the context is VAR_GLOBAL though, we check if the variable 910 * was set in VAR_CMD from the command line and skip it if so. 911 *----------------------------------------------------------------------- 912 */ 913 void 914 Var_Set(const char *name, const char *val, GNode *ctxt, int flags) 915 { 916 Var *v; 917 char *expanded_name = NULL; 918 919 /* 920 * We only look for a variable in the given context since anything set 921 * here will override anything in a lower context, so there's not much 922 * point in searching them all just to save a bit of memory... 923 */ 924 if (strchr(name, '$') != NULL) { 925 expanded_name = Var_Subst(NULL, name, ctxt, 0); 926 if (expanded_name[0] == 0) { 927 if (DEBUG(VAR)) { 928 fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) " 929 "name expands to empty string - ignored\n", 930 name, val); 931 } 932 free(expanded_name); 933 return; 934 } 935 name = expanded_name; 936 } 937 if (ctxt == VAR_GLOBAL) { 938 v = VarFind(name, VAR_CMD, 0); 939 if (v != NULL) { 940 if ((v->flags & VAR_FROM_CMD)) { 941 if (DEBUG(VAR)) { 942 fprintf(debug_file, "%s:%s = %s ignored!\n", ctxt->name, name, val); 943 } 944 goto out; 945 } 946 VarFreeEnv(v, TRUE); 947 } 948 } 949 v = VarFind(name, ctxt, 0); 950 if (v == NULL) { 951 if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) { 952 /* 953 * This var would normally prevent the same name being added 954 * to VAR_GLOBAL, so delete it from there if needed. 955 * Otherwise -V name may show the wrong value. 956 */ 957 Var_Delete(name, VAR_GLOBAL); 958 } 959 VarAdd(name, val, ctxt); 960 } else { 961 Buf_Empty(&v->val); 962 Buf_AddBytes(&v->val, strlen(val), val); 963 964 if (DEBUG(VAR)) { 965 fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val); 966 } 967 if ((v->flags & VAR_EXPORTED)) { 968 Var_Export1(name, VAR_EXPORT_PARENT); 969 } 970 } 971 /* 972 * Any variables given on the command line are automatically exported 973 * to the environment (as per POSIX standard) 974 */ 975 if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) { 976 if (v == NULL) { 977 /* we just added it */ 978 v = VarFind(name, ctxt, 0); 979 } 980 if (v != NULL) 981 v->flags |= VAR_FROM_CMD; 982 /* 983 * If requested, don't export these in the environment 984 * individually. We still put them in MAKEOVERRIDES so 985 * that the command-line settings continue to override 986 * Makefile settings. 987 */ 988 if (varNoExportEnv != TRUE) 989 setenv(name, val, 1); 990 991 Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL); 992 } 993 994 #ifdef MAKE_FREEBSD_UL 995 if (strcmp(MAKE_FREEBSD_UL, name) == 0) { 996 FreeBSD_UL = getBoolean(MAKE_FREEBSD_UL, FALSE); 997 } 998 #endif 999 1000 1001 out: 1002 if (expanded_name != NULL) 1003 free(expanded_name); 1004 if (v != NULL) 1005 VarFreeEnv(v, TRUE); 1006 } 1007 1008 /*- 1009 *----------------------------------------------------------------------- 1010 * Var_Append -- 1011 * The variable of the given name has the given value appended to it in 1012 * the given context. 1013 * 1014 * Input: 1015 * name name of variable to modify 1016 * val String to append to it 1017 * ctxt Context in which this should occur 1018 * 1019 * Results: 1020 * None 1021 * 1022 * Side Effects: 1023 * If the variable doesn't exist, it is created. Else the strings 1024 * are concatenated (with a space in between). 1025 * 1026 * Notes: 1027 * Only if the variable is being sought in the global context is the 1028 * environment searched. 1029 * XXX: Knows its calling circumstances in that if called with ctxt 1030 * an actual target, it will only search that context since only 1031 * a local variable could be being appended to. This is actually 1032 * a big win and must be tolerated. 1033 *----------------------------------------------------------------------- 1034 */ 1035 void 1036 Var_Append(const char *name, const char *val, GNode *ctxt) 1037 { 1038 Var *v; 1039 Hash_Entry *h; 1040 char *expanded_name = NULL; 1041 1042 if (strchr(name, '$') != NULL) { 1043 expanded_name = Var_Subst(NULL, name, ctxt, 0); 1044 if (expanded_name[0] == 0) { 1045 if (DEBUG(VAR)) { 1046 fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) " 1047 "name expands to empty string - ignored\n", 1048 name, val); 1049 } 1050 free(expanded_name); 1051 return; 1052 } 1053 name = expanded_name; 1054 } 1055 1056 v = VarFind(name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0); 1057 1058 if (v == NULL) { 1059 VarAdd(name, val, ctxt); 1060 } else { 1061 Buf_AddByte(&v->val, ' '); 1062 Buf_AddBytes(&v->val, strlen(val), val); 1063 1064 if (DEBUG(VAR)) { 1065 fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, 1066 Buf_GetAll(&v->val, NULL)); 1067 } 1068 1069 if (v->flags & VAR_FROM_ENV) { 1070 /* 1071 * If the original variable came from the environment, we 1072 * have to install it in the global context (we could place 1073 * it in the environment, but then we should provide a way to 1074 * export other variables...) 1075 */ 1076 v->flags &= ~VAR_FROM_ENV; 1077 h = Hash_CreateEntry(&ctxt->context, name, NULL); 1078 Hash_SetValue(h, v); 1079 } 1080 } 1081 if (expanded_name != NULL) 1082 free(expanded_name); 1083 } 1084 1085 /*- 1086 *----------------------------------------------------------------------- 1087 * Var_Exists -- 1088 * See if the given variable exists. 1089 * 1090 * Input: 1091 * name Variable to find 1092 * ctxt Context in which to start search 1093 * 1094 * Results: 1095 * TRUE if it does, FALSE if it doesn't 1096 * 1097 * Side Effects: 1098 * None. 1099 * 1100 *----------------------------------------------------------------------- 1101 */ 1102 Boolean 1103 Var_Exists(const char *name, GNode *ctxt) 1104 { 1105 Var *v; 1106 char *cp; 1107 1108 if ((cp = strchr(name, '$')) != NULL) { 1109 cp = Var_Subst(NULL, name, ctxt, FALSE); 1110 } 1111 v = VarFind(cp ? cp : name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV); 1112 if (cp != NULL) { 1113 free(cp); 1114 } 1115 if (v == NULL) { 1116 return(FALSE); 1117 } else { 1118 (void)VarFreeEnv(v, TRUE); 1119 } 1120 return(TRUE); 1121 } 1122 1123 /*- 1124 *----------------------------------------------------------------------- 1125 * Var_Value -- 1126 * Return the value of the named variable in the given context 1127 * 1128 * Input: 1129 * name name to find 1130 * ctxt context in which to search for it 1131 * 1132 * Results: 1133 * The value if the variable exists, NULL if it doesn't 1134 * 1135 * Side Effects: 1136 * None 1137 *----------------------------------------------------------------------- 1138 */ 1139 char * 1140 Var_Value(const char *name, GNode *ctxt, char **frp) 1141 { 1142 Var *v; 1143 1144 v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); 1145 *frp = NULL; 1146 if (v != NULL) { 1147 char *p = (Buf_GetAll(&v->val, NULL)); 1148 if (VarFreeEnv(v, FALSE)) 1149 *frp = p; 1150 return p; 1151 } else { 1152 return NULL; 1153 } 1154 } 1155 1156 /*- 1157 *----------------------------------------------------------------------- 1158 * VarHead -- 1159 * Remove the tail of the given word and place the result in the given 1160 * buffer. 1161 * 1162 * Input: 1163 * word Word to trim 1164 * addSpace True if need to add a space to the buffer 1165 * before sticking in the head 1166 * buf Buffer in which to store it 1167 * 1168 * Results: 1169 * TRUE if characters were added to the buffer (a space needs to be 1170 * added to the buffer before the next word). 1171 * 1172 * Side Effects: 1173 * The trimmed word is added to the buffer. 1174 * 1175 *----------------------------------------------------------------------- 1176 */ 1177 static Boolean 1178 VarHead(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1179 char *word, Boolean addSpace, Buffer *buf, 1180 void *dummy) 1181 { 1182 char *slash; 1183 1184 slash = strrchr(word, '/'); 1185 if (slash != NULL) { 1186 if (addSpace && vpstate->varSpace) { 1187 Buf_AddByte(buf, vpstate->varSpace); 1188 } 1189 *slash = '\0'; 1190 Buf_AddBytes(buf, strlen(word), word); 1191 *slash = '/'; 1192 return (TRUE); 1193 } else { 1194 /* 1195 * If no directory part, give . (q.v. the POSIX standard) 1196 */ 1197 if (addSpace && vpstate->varSpace) 1198 Buf_AddByte(buf, vpstate->varSpace); 1199 Buf_AddByte(buf, '.'); 1200 } 1201 return(dummy ? TRUE : TRUE); 1202 } 1203 1204 /*- 1205 *----------------------------------------------------------------------- 1206 * VarTail -- 1207 * Remove the head of the given word and place the result in the given 1208 * buffer. 1209 * 1210 * Input: 1211 * word Word to trim 1212 * addSpace True if need to add a space to the buffer 1213 * before adding the tail 1214 * buf Buffer in which to store it 1215 * 1216 * Results: 1217 * TRUE if characters were added to the buffer (a space needs to be 1218 * added to the buffer before the next word). 1219 * 1220 * Side Effects: 1221 * The trimmed word is added to the buffer. 1222 * 1223 *----------------------------------------------------------------------- 1224 */ 1225 static Boolean 1226 VarTail(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1227 char *word, Boolean addSpace, Buffer *buf, 1228 void *dummy) 1229 { 1230 char *slash; 1231 1232 if (addSpace && vpstate->varSpace) { 1233 Buf_AddByte(buf, vpstate->varSpace); 1234 } 1235 1236 slash = strrchr(word, '/'); 1237 if (slash != NULL) { 1238 *slash++ = '\0'; 1239 Buf_AddBytes(buf, strlen(slash), slash); 1240 slash[-1] = '/'; 1241 } else { 1242 Buf_AddBytes(buf, strlen(word), word); 1243 } 1244 return (dummy ? TRUE : TRUE); 1245 } 1246 1247 /*- 1248 *----------------------------------------------------------------------- 1249 * VarSuffix -- 1250 * Place the suffix of the given word in the given buffer. 1251 * 1252 * Input: 1253 * word Word to trim 1254 * addSpace TRUE if need to add a space before placing the 1255 * suffix in the buffer 1256 * buf Buffer in which to store it 1257 * 1258 * Results: 1259 * TRUE if characters were added to the buffer (a space needs to be 1260 * added to the buffer before the next word). 1261 * 1262 * Side Effects: 1263 * The suffix from the word is placed in the buffer. 1264 * 1265 *----------------------------------------------------------------------- 1266 */ 1267 static Boolean 1268 VarSuffix(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1269 char *word, Boolean addSpace, Buffer *buf, 1270 void *dummy) 1271 { 1272 char *dot; 1273 1274 dot = strrchr(word, '.'); 1275 if (dot != NULL) { 1276 if (addSpace && vpstate->varSpace) { 1277 Buf_AddByte(buf, vpstate->varSpace); 1278 } 1279 *dot++ = '\0'; 1280 Buf_AddBytes(buf, strlen(dot), dot); 1281 dot[-1] = '.'; 1282 addSpace = TRUE; 1283 } 1284 return (dummy ? addSpace : addSpace); 1285 } 1286 1287 /*- 1288 *----------------------------------------------------------------------- 1289 * VarRoot -- 1290 * Remove the suffix of the given word and place the result in the 1291 * buffer. 1292 * 1293 * Input: 1294 * word Word to trim 1295 * addSpace TRUE if need to add a space to the buffer 1296 * before placing the root in it 1297 * buf Buffer in which to store it 1298 * 1299 * Results: 1300 * TRUE if characters were added to the buffer (a space needs to be 1301 * added to the buffer before the next word). 1302 * 1303 * Side Effects: 1304 * The trimmed word is added to the buffer. 1305 * 1306 *----------------------------------------------------------------------- 1307 */ 1308 static Boolean 1309 VarRoot(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1310 char *word, Boolean addSpace, Buffer *buf, 1311 void *dummy) 1312 { 1313 char *dot; 1314 1315 if (addSpace && vpstate->varSpace) { 1316 Buf_AddByte(buf, vpstate->varSpace); 1317 } 1318 1319 dot = strrchr(word, '.'); 1320 if (dot != NULL) { 1321 *dot = '\0'; 1322 Buf_AddBytes(buf, strlen(word), word); 1323 *dot = '.'; 1324 } else { 1325 Buf_AddBytes(buf, strlen(word), word); 1326 } 1327 return (dummy ? TRUE : TRUE); 1328 } 1329 1330 /*- 1331 *----------------------------------------------------------------------- 1332 * VarMatch -- 1333 * Place the word in the buffer if it matches the given pattern. 1334 * Callback function for VarModify to implement the :M modifier. 1335 * 1336 * Input: 1337 * word Word to examine 1338 * addSpace TRUE if need to add a space to the buffer 1339 * before adding the word, if it matches 1340 * buf Buffer in which to store it 1341 * pattern Pattern the word must match 1342 * 1343 * Results: 1344 * TRUE if a space should be placed in the buffer before the next 1345 * word. 1346 * 1347 * Side Effects: 1348 * The word may be copied to the buffer. 1349 * 1350 *----------------------------------------------------------------------- 1351 */ 1352 static Boolean 1353 VarMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1354 char *word, Boolean addSpace, Buffer *buf, 1355 void *pattern) 1356 { 1357 if (DEBUG(VAR)) 1358 fprintf(debug_file, "VarMatch [%s] [%s]\n", word, (char *)pattern); 1359 if (Str_Match(word, (char *)pattern)) { 1360 if (addSpace && vpstate->varSpace) { 1361 Buf_AddByte(buf, vpstate->varSpace); 1362 } 1363 addSpace = TRUE; 1364 Buf_AddBytes(buf, strlen(word), word); 1365 } 1366 return(addSpace); 1367 } 1368 1369 #ifdef SYSVVARSUB 1370 /*- 1371 *----------------------------------------------------------------------- 1372 * VarSYSVMatch -- 1373 * Place the word in the buffer if it matches the given pattern. 1374 * Callback function for VarModify to implement the System V % 1375 * modifiers. 1376 * 1377 * Input: 1378 * word Word to examine 1379 * addSpace TRUE if need to add a space to the buffer 1380 * before adding the word, if it matches 1381 * buf Buffer in which to store it 1382 * patp Pattern the word must match 1383 * 1384 * Results: 1385 * TRUE if a space should be placed in the buffer before the next 1386 * word. 1387 * 1388 * Side Effects: 1389 * The word may be copied to the buffer. 1390 * 1391 *----------------------------------------------------------------------- 1392 */ 1393 static Boolean 1394 VarSYSVMatch(GNode *ctx, Var_Parse_State *vpstate, 1395 char *word, Boolean addSpace, Buffer *buf, 1396 void *patp) 1397 { 1398 int len; 1399 char *ptr; 1400 VarPattern *pat = (VarPattern *)patp; 1401 char *varexp; 1402 1403 if (addSpace && vpstate->varSpace) 1404 Buf_AddByte(buf, vpstate->varSpace); 1405 1406 addSpace = TRUE; 1407 1408 if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL) { 1409 varexp = Var_Subst(NULL, pat->rhs, ctx, 0); 1410 Str_SYSVSubst(buf, varexp, ptr, len); 1411 free(varexp); 1412 } else { 1413 Buf_AddBytes(buf, strlen(word), word); 1414 } 1415 1416 return(addSpace); 1417 } 1418 #endif 1419 1420 1421 /*- 1422 *----------------------------------------------------------------------- 1423 * VarNoMatch -- 1424 * Place the word in the buffer if it doesn't match the given pattern. 1425 * Callback function for VarModify to implement the :N modifier. 1426 * 1427 * Input: 1428 * word Word to examine 1429 * addSpace TRUE if need to add a space to the buffer 1430 * before adding the word, if it matches 1431 * buf Buffer in which to store it 1432 * pattern Pattern the word must match 1433 * 1434 * Results: 1435 * TRUE if a space should be placed in the buffer before the next 1436 * word. 1437 * 1438 * Side Effects: 1439 * The word may be copied to the buffer. 1440 * 1441 *----------------------------------------------------------------------- 1442 */ 1443 static Boolean 1444 VarNoMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1445 char *word, Boolean addSpace, Buffer *buf, 1446 void *pattern) 1447 { 1448 if (!Str_Match(word, (char *)pattern)) { 1449 if (addSpace && vpstate->varSpace) { 1450 Buf_AddByte(buf, vpstate->varSpace); 1451 } 1452 addSpace = TRUE; 1453 Buf_AddBytes(buf, strlen(word), word); 1454 } 1455 return(addSpace); 1456 } 1457 1458 1459 /*- 1460 *----------------------------------------------------------------------- 1461 * VarSubstitute -- 1462 * Perform a string-substitution on the given word, placing the 1463 * result in the passed buffer. 1464 * 1465 * Input: 1466 * word Word to modify 1467 * addSpace True if space should be added before 1468 * other characters 1469 * buf Buffer for result 1470 * patternp Pattern for substitution 1471 * 1472 * Results: 1473 * TRUE if a space is needed before more characters are added. 1474 * 1475 * Side Effects: 1476 * None. 1477 * 1478 *----------------------------------------------------------------------- 1479 */ 1480 static Boolean 1481 VarSubstitute(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1482 char *word, Boolean addSpace, Buffer *buf, 1483 void *patternp) 1484 { 1485 int wordLen; /* Length of word */ 1486 char *cp; /* General pointer */ 1487 VarPattern *pattern = (VarPattern *)patternp; 1488 1489 wordLen = strlen(word); 1490 if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) != 1491 (VAR_SUB_ONE|VAR_SUB_MATCHED)) { 1492 /* 1493 * Still substituting -- break it down into simple anchored cases 1494 * and if none of them fits, perform the general substitution case. 1495 */ 1496 if ((pattern->flags & VAR_MATCH_START) && 1497 (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) { 1498 /* 1499 * Anchored at start and beginning of word matches pattern 1500 */ 1501 if ((pattern->flags & VAR_MATCH_END) && 1502 (wordLen == pattern->leftLen)) { 1503 /* 1504 * Also anchored at end and matches to the end (word 1505 * is same length as pattern) add space and rhs only 1506 * if rhs is non-null. 1507 */ 1508 if (pattern->rightLen != 0) { 1509 if (addSpace && vpstate->varSpace) { 1510 Buf_AddByte(buf, vpstate->varSpace); 1511 } 1512 addSpace = TRUE; 1513 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs); 1514 } 1515 pattern->flags |= VAR_SUB_MATCHED; 1516 } else if (pattern->flags & VAR_MATCH_END) { 1517 /* 1518 * Doesn't match to end -- copy word wholesale 1519 */ 1520 goto nosub; 1521 } else { 1522 /* 1523 * Matches at start but need to copy in trailing characters 1524 */ 1525 if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){ 1526 if (addSpace && vpstate->varSpace) { 1527 Buf_AddByte(buf, vpstate->varSpace); 1528 } 1529 addSpace = TRUE; 1530 } 1531 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs); 1532 Buf_AddBytes(buf, wordLen - pattern->leftLen, 1533 (word + pattern->leftLen)); 1534 pattern->flags |= VAR_SUB_MATCHED; 1535 } 1536 } else if (pattern->flags & VAR_MATCH_START) { 1537 /* 1538 * Had to match at start of word and didn't -- copy whole word. 1539 */ 1540 goto nosub; 1541 } else if (pattern->flags & VAR_MATCH_END) { 1542 /* 1543 * Anchored at end, Find only place match could occur (leftLen 1544 * characters from the end of the word) and see if it does. Note 1545 * that because the $ will be left at the end of the lhs, we have 1546 * to use strncmp. 1547 */ 1548 cp = word + (wordLen - pattern->leftLen); 1549 if ((cp >= word) && 1550 (strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) { 1551 /* 1552 * Match found. If we will place characters in the buffer, 1553 * add a space before hand as indicated by addSpace, then 1554 * stuff in the initial, unmatched part of the word followed 1555 * by the right-hand-side. 1556 */ 1557 if (((cp - word) + pattern->rightLen) != 0) { 1558 if (addSpace && vpstate->varSpace) { 1559 Buf_AddByte(buf, vpstate->varSpace); 1560 } 1561 addSpace = TRUE; 1562 } 1563 Buf_AddBytes(buf, cp - word, word); 1564 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs); 1565 pattern->flags |= VAR_SUB_MATCHED; 1566 } else { 1567 /* 1568 * Had to match at end and didn't. Copy entire word. 1569 */ 1570 goto nosub; 1571 } 1572 } else { 1573 /* 1574 * Pattern is unanchored: search for the pattern in the word using 1575 * String_FindSubstring, copying unmatched portions and the 1576 * right-hand-side for each match found, handling non-global 1577 * substitutions correctly, etc. When the loop is done, any 1578 * remaining part of the word (word and wordLen are adjusted 1579 * accordingly through the loop) is copied straight into the 1580 * buffer. 1581 * addSpace is set FALSE as soon as a space is added to the 1582 * buffer. 1583 */ 1584 Boolean done; 1585 int origSize; 1586 1587 done = FALSE; 1588 origSize = Buf_Size(buf); 1589 while (!done) { 1590 cp = Str_FindSubstring(word, pattern->lhs); 1591 if (cp != NULL) { 1592 if (addSpace && (((cp - word) + pattern->rightLen) != 0)){ 1593 Buf_AddByte(buf, vpstate->varSpace); 1594 addSpace = FALSE; 1595 } 1596 Buf_AddBytes(buf, cp-word, word); 1597 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs); 1598 wordLen -= (cp - word) + pattern->leftLen; 1599 word = cp + pattern->leftLen; 1600 if (wordLen == 0) { 1601 done = TRUE; 1602 } 1603 if ((pattern->flags & VAR_SUB_GLOBAL) == 0) { 1604 done = TRUE; 1605 } 1606 pattern->flags |= VAR_SUB_MATCHED; 1607 } else { 1608 done = TRUE; 1609 } 1610 } 1611 if (wordLen != 0) { 1612 if (addSpace && vpstate->varSpace) { 1613 Buf_AddByte(buf, vpstate->varSpace); 1614 } 1615 Buf_AddBytes(buf, wordLen, word); 1616 } 1617 /* 1618 * If added characters to the buffer, need to add a space 1619 * before we add any more. If we didn't add any, just return 1620 * the previous value of addSpace. 1621 */ 1622 return ((Buf_Size(buf) != origSize) || addSpace); 1623 } 1624 return (addSpace); 1625 } 1626 nosub: 1627 if (addSpace && vpstate->varSpace) { 1628 Buf_AddByte(buf, vpstate->varSpace); 1629 } 1630 Buf_AddBytes(buf, wordLen, word); 1631 return(TRUE); 1632 } 1633 1634 #ifndef NO_REGEX 1635 /*- 1636 *----------------------------------------------------------------------- 1637 * VarREError -- 1638 * Print the error caused by a regcomp or regexec call. 1639 * 1640 * Results: 1641 * None. 1642 * 1643 * Side Effects: 1644 * An error gets printed. 1645 * 1646 *----------------------------------------------------------------------- 1647 */ 1648 static void 1649 VarREError(int errnum, regex_t *pat, const char *str) 1650 { 1651 char *errbuf; 1652 int errlen; 1653 1654 errlen = regerror(errnum, pat, 0, 0); 1655 errbuf = bmake_malloc(errlen); 1656 regerror(errnum, pat, errbuf, errlen); 1657 Error("%s: %s", str, errbuf); 1658 free(errbuf); 1659 } 1660 1661 1662 /*- 1663 *----------------------------------------------------------------------- 1664 * VarRESubstitute -- 1665 * Perform a regex substitution on the given word, placing the 1666 * result in the passed buffer. 1667 * 1668 * Results: 1669 * TRUE if a space is needed before more characters are added. 1670 * 1671 * Side Effects: 1672 * None. 1673 * 1674 *----------------------------------------------------------------------- 1675 */ 1676 static Boolean 1677 VarRESubstitute(GNode *ctx MAKE_ATTR_UNUSED, 1678 Var_Parse_State *vpstate MAKE_ATTR_UNUSED, 1679 char *word, Boolean addSpace, Buffer *buf, 1680 void *patternp) 1681 { 1682 VarREPattern *pat; 1683 int xrv; 1684 char *wp; 1685 char *rp; 1686 int added; 1687 int flags = 0; 1688 1689 #define MAYBE_ADD_SPACE() \ 1690 if (addSpace && !added) \ 1691 Buf_AddByte(buf, ' '); \ 1692 added = 1 1693 1694 added = 0; 1695 wp = word; 1696 pat = patternp; 1697 1698 if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) == 1699 (VAR_SUB_ONE|VAR_SUB_MATCHED)) 1700 xrv = REG_NOMATCH; 1701 else { 1702 tryagain: 1703 xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, flags); 1704 } 1705 1706 switch (xrv) { 1707 case 0: 1708 pat->flags |= VAR_SUB_MATCHED; 1709 if (pat->matches[0].rm_so > 0) { 1710 MAYBE_ADD_SPACE(); 1711 Buf_AddBytes(buf, pat->matches[0].rm_so, wp); 1712 } 1713 1714 for (rp = pat->replace; *rp; rp++) { 1715 if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) { 1716 MAYBE_ADD_SPACE(); 1717 Buf_AddByte(buf,rp[1]); 1718 rp++; 1719 } 1720 else if ((*rp == '&') || 1721 ((*rp == '\\') && isdigit((unsigned char)rp[1]))) { 1722 int n; 1723 const char *subbuf; 1724 int sublen; 1725 char errstr[3]; 1726 1727 if (*rp == '&') { 1728 n = 0; 1729 errstr[0] = '&'; 1730 errstr[1] = '\0'; 1731 } else { 1732 n = rp[1] - '0'; 1733 errstr[0] = '\\'; 1734 errstr[1] = rp[1]; 1735 errstr[2] = '\0'; 1736 rp++; 1737 } 1738 1739 if (n > pat->nsub) { 1740 Error("No subexpression %s", &errstr[0]); 1741 subbuf = ""; 1742 sublen = 0; 1743 } else if ((pat->matches[n].rm_so == -1) && 1744 (pat->matches[n].rm_eo == -1)) { 1745 Error("No match for subexpression %s", &errstr[0]); 1746 subbuf = ""; 1747 sublen = 0; 1748 } else { 1749 subbuf = wp + pat->matches[n].rm_so; 1750 sublen = pat->matches[n].rm_eo - pat->matches[n].rm_so; 1751 } 1752 1753 if (sublen > 0) { 1754 MAYBE_ADD_SPACE(); 1755 Buf_AddBytes(buf, sublen, subbuf); 1756 } 1757 } else { 1758 MAYBE_ADD_SPACE(); 1759 Buf_AddByte(buf, *rp); 1760 } 1761 } 1762 wp += pat->matches[0].rm_eo; 1763 if (pat->flags & VAR_SUB_GLOBAL) { 1764 flags |= REG_NOTBOL; 1765 if (pat->matches[0].rm_so == 0 && pat->matches[0].rm_eo == 0) { 1766 MAYBE_ADD_SPACE(); 1767 Buf_AddByte(buf, *wp); 1768 wp++; 1769 1770 } 1771 if (*wp) 1772 goto tryagain; 1773 } 1774 if (*wp) { 1775 MAYBE_ADD_SPACE(); 1776 Buf_AddBytes(buf, strlen(wp), wp); 1777 } 1778 break; 1779 default: 1780 VarREError(xrv, &pat->re, "Unexpected regex error"); 1781 /* fall through */ 1782 case REG_NOMATCH: 1783 if (*wp) { 1784 MAYBE_ADD_SPACE(); 1785 Buf_AddBytes(buf,strlen(wp),wp); 1786 } 1787 break; 1788 } 1789 return(addSpace||added); 1790 } 1791 #endif 1792 1793 1794 1795 /*- 1796 *----------------------------------------------------------------------- 1797 * VarLoopExpand -- 1798 * Implements the :@<temp>@<string>@ modifier of ODE make. 1799 * We set the temp variable named in pattern.lhs to word and expand 1800 * pattern.rhs storing the result in the passed buffer. 1801 * 1802 * Input: 1803 * word Word to modify 1804 * addSpace True if space should be added before 1805 * other characters 1806 * buf Buffer for result 1807 * pattern Datafor substitution 1808 * 1809 * Results: 1810 * TRUE if a space is needed before more characters are added. 1811 * 1812 * Side Effects: 1813 * None. 1814 * 1815 *----------------------------------------------------------------------- 1816 */ 1817 static Boolean 1818 VarLoopExpand(GNode *ctx MAKE_ATTR_UNUSED, 1819 Var_Parse_State *vpstate MAKE_ATTR_UNUSED, 1820 char *word, Boolean addSpace, Buffer *buf, 1821 void *loopp) 1822 { 1823 VarLoop_t *loop = (VarLoop_t *)loopp; 1824 char *s; 1825 int slen; 1826 1827 if (word && *word) { 1828 Var_Set(loop->tvar, word, loop->ctxt, VAR_NO_EXPORT); 1829 s = Var_Subst(NULL, loop->str, loop->ctxt, loop->errnum); 1830 if (s != NULL && *s != '\0') { 1831 if (addSpace && *s != '\n') 1832 Buf_AddByte(buf, ' '); 1833 Buf_AddBytes(buf, (slen = strlen(s)), s); 1834 addSpace = (slen > 0 && s[slen - 1] != '\n'); 1835 free(s); 1836 } 1837 } 1838 return addSpace; 1839 } 1840 1841 1842 /*- 1843 *----------------------------------------------------------------------- 1844 * VarSelectWords -- 1845 * Implements the :[start..end] modifier. 1846 * This is a special case of VarModify since we want to be able 1847 * to scan the list backwards if start > end. 1848 * 1849 * Input: 1850 * str String whose words should be trimmed 1851 * seldata words to select 1852 * 1853 * Results: 1854 * A string of all the words selected. 1855 * 1856 * Side Effects: 1857 * None. 1858 * 1859 *----------------------------------------------------------------------- 1860 */ 1861 static char * 1862 VarSelectWords(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1863 const char *str, VarSelectWords_t *seldata) 1864 { 1865 Buffer buf; /* Buffer for the new string */ 1866 Boolean addSpace; /* TRUE if need to add a space to the 1867 * buffer before adding the trimmed 1868 * word */ 1869 char **av; /* word list */ 1870 char *as; /* word list memory */ 1871 int ac, i; 1872 int start, end, step; 1873 1874 Buf_Init(&buf, 0); 1875 addSpace = FALSE; 1876 1877 if (vpstate->oneBigWord) { 1878 /* fake what brk_string() would do if there were only one word */ 1879 ac = 1; 1880 av = bmake_malloc((ac + 1) * sizeof(char *)); 1881 as = bmake_strdup(str); 1882 av[0] = as; 1883 av[1] = NULL; 1884 } else { 1885 av = brk_string(str, &ac, FALSE, &as); 1886 } 1887 1888 /* 1889 * Now sanitize seldata. 1890 * If seldata->start or seldata->end are negative, convert them to 1891 * the positive equivalents (-1 gets converted to argc, -2 gets 1892 * converted to (argc-1), etc.). 1893 */ 1894 if (seldata->start < 0) 1895 seldata->start = ac + seldata->start + 1; 1896 if (seldata->end < 0) 1897 seldata->end = ac + seldata->end + 1; 1898 1899 /* 1900 * We avoid scanning more of the list than we need to. 1901 */ 1902 if (seldata->start > seldata->end) { 1903 start = MIN(ac, seldata->start) - 1; 1904 end = MAX(0, seldata->end - 1); 1905 step = -1; 1906 } else { 1907 start = MAX(0, seldata->start - 1); 1908 end = MIN(ac, seldata->end); 1909 step = 1; 1910 } 1911 1912 for (i = start; 1913 (step < 0 && i >= end) || (step > 0 && i < end); 1914 i += step) { 1915 if (av[i] && *av[i]) { 1916 if (addSpace && vpstate->varSpace) { 1917 Buf_AddByte(&buf, vpstate->varSpace); 1918 } 1919 Buf_AddBytes(&buf, strlen(av[i]), av[i]); 1920 addSpace = TRUE; 1921 } 1922 } 1923 1924 free(as); 1925 free(av); 1926 1927 return Buf_Destroy(&buf, FALSE); 1928 } 1929 1930 1931 /*- 1932 * VarRealpath -- 1933 * Replace each word with the result of realpath() 1934 * if successful. 1935 */ 1936 static Boolean 1937 VarRealpath(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1938 char *word, Boolean addSpace, Buffer *buf, 1939 void *patternp MAKE_ATTR_UNUSED) 1940 { 1941 struct stat st; 1942 char rbuf[MAXPATHLEN]; 1943 char *rp; 1944 1945 if (addSpace && vpstate->varSpace) { 1946 Buf_AddByte(buf, vpstate->varSpace); 1947 } 1948 addSpace = TRUE; 1949 rp = realpath(word, rbuf); 1950 if (rp && *rp == '/' && stat(rp, &st) == 0) 1951 word = rp; 1952 1953 Buf_AddBytes(buf, strlen(word), word); 1954 return(addSpace); 1955 } 1956 1957 /*- 1958 *----------------------------------------------------------------------- 1959 * VarModify -- 1960 * Modify each of the words of the passed string using the given 1961 * function. Used to implement all modifiers. 1962 * 1963 * Input: 1964 * str String whose words should be trimmed 1965 * modProc Function to use to modify them 1966 * datum Datum to pass it 1967 * 1968 * Results: 1969 * A string of all the words modified appropriately. 1970 * 1971 * Side Effects: 1972 * None. 1973 * 1974 *----------------------------------------------------------------------- 1975 */ 1976 static char * 1977 VarModify(GNode *ctx, Var_Parse_State *vpstate, 1978 const char *str, 1979 Boolean (*modProc)(GNode *, Var_Parse_State *, char *, 1980 Boolean, Buffer *, void *), 1981 void *datum) 1982 { 1983 Buffer buf; /* Buffer for the new string */ 1984 Boolean addSpace; /* TRUE if need to add a space to the 1985 * buffer before adding the trimmed 1986 * word */ 1987 char **av; /* word list */ 1988 char *as; /* word list memory */ 1989 int ac, i; 1990 1991 Buf_Init(&buf, 0); 1992 addSpace = FALSE; 1993 1994 if (vpstate->oneBigWord) { 1995 /* fake what brk_string() would do if there were only one word */ 1996 ac = 1; 1997 av = bmake_malloc((ac + 1) * sizeof(char *)); 1998 as = bmake_strdup(str); 1999 av[0] = as; 2000 av[1] = NULL; 2001 } else { 2002 av = brk_string(str, &ac, FALSE, &as); 2003 } 2004 2005 for (i = 0; i < ac; i++) { 2006 addSpace = (*modProc)(ctx, vpstate, av[i], addSpace, &buf, datum); 2007 } 2008 2009 free(as); 2010 free(av); 2011 2012 return Buf_Destroy(&buf, FALSE); 2013 } 2014 2015 2016 static int 2017 VarWordCompare(const void *a, const void *b) 2018 { 2019 int r = strcmp(*(const char * const *)a, *(const char * const *)b); 2020 return r; 2021 } 2022 2023 /*- 2024 *----------------------------------------------------------------------- 2025 * VarOrder -- 2026 * Order the words in the string. 2027 * 2028 * Input: 2029 * str String whose words should be sorted. 2030 * otype How to order: s - sort, x - random. 2031 * 2032 * Results: 2033 * A string containing the words ordered. 2034 * 2035 * Side Effects: 2036 * None. 2037 * 2038 *----------------------------------------------------------------------- 2039 */ 2040 static char * 2041 VarOrder(const char *str, const char otype) 2042 { 2043 Buffer buf; /* Buffer for the new string */ 2044 char **av; /* word list [first word does not count] */ 2045 char *as; /* word list memory */ 2046 int ac, i; 2047 2048 Buf_Init(&buf, 0); 2049 2050 av = brk_string(str, &ac, FALSE, &as); 2051 2052 if (ac > 0) 2053 switch (otype) { 2054 case 's': /* sort alphabetically */ 2055 qsort(av, ac, sizeof(char *), VarWordCompare); 2056 break; 2057 case 'x': /* randomize */ 2058 { 2059 int rndidx; 2060 char *t; 2061 2062 /* 2063 * We will use [ac..2] range for mod factors. This will produce 2064 * random numbers in [(ac-1)..0] interval, and minimal 2065 * reasonable value for mod factor is 2 (the mod 1 will produce 2066 * 0 with probability 1). 2067 */ 2068 for (i = ac-1; i > 0; i--) { 2069 rndidx = random() % (i + 1); 2070 if (i != rndidx) { 2071 t = av[i]; 2072 av[i] = av[rndidx]; 2073 av[rndidx] = t; 2074 } 2075 } 2076 } 2077 } /* end of switch */ 2078 2079 for (i = 0; i < ac; i++) { 2080 Buf_AddBytes(&buf, strlen(av[i]), av[i]); 2081 if (i != ac - 1) 2082 Buf_AddByte(&buf, ' '); 2083 } 2084 2085 free(as); 2086 free(av); 2087 2088 return Buf_Destroy(&buf, FALSE); 2089 } 2090 2091 2092 /*- 2093 *----------------------------------------------------------------------- 2094 * VarUniq -- 2095 * Remove adjacent duplicate words. 2096 * 2097 * Input: 2098 * str String whose words should be sorted 2099 * 2100 * Results: 2101 * A string containing the resulting words. 2102 * 2103 * Side Effects: 2104 * None. 2105 * 2106 *----------------------------------------------------------------------- 2107 */ 2108 static char * 2109 VarUniq(const char *str) 2110 { 2111 Buffer buf; /* Buffer for new string */ 2112 char **av; /* List of words to affect */ 2113 char *as; /* Word list memory */ 2114 int ac, i, j; 2115 2116 Buf_Init(&buf, 0); 2117 av = brk_string(str, &ac, FALSE, &as); 2118 2119 if (ac > 1) { 2120 for (j = 0, i = 1; i < ac; i++) 2121 if (strcmp(av[i], av[j]) != 0 && (++j != i)) 2122 av[j] = av[i]; 2123 ac = j + 1; 2124 } 2125 2126 for (i = 0; i < ac; i++) { 2127 Buf_AddBytes(&buf, strlen(av[i]), av[i]); 2128 if (i != ac - 1) 2129 Buf_AddByte(&buf, ' '); 2130 } 2131 2132 free(as); 2133 free(av); 2134 2135 return Buf_Destroy(&buf, FALSE); 2136 } 2137 2138 2139 /*- 2140 *----------------------------------------------------------------------- 2141 * VarGetPattern -- 2142 * Pass through the tstr looking for 1) escaped delimiters, 2143 * '$'s and backslashes (place the escaped character in 2144 * uninterpreted) and 2) unescaped $'s that aren't before 2145 * the delimiter (expand the variable substitution unless flags 2146 * has VAR_NOSUBST set). 2147 * Return the expanded string or NULL if the delimiter was missing 2148 * If pattern is specified, handle escaped ampersands, and replace 2149 * unescaped ampersands with the lhs of the pattern. 2150 * 2151 * Results: 2152 * A string of all the words modified appropriately. 2153 * If length is specified, return the string length of the buffer 2154 * If flags is specified and the last character of the pattern is a 2155 * $ set the VAR_MATCH_END bit of flags. 2156 * 2157 * Side Effects: 2158 * None. 2159 *----------------------------------------------------------------------- 2160 */ 2161 static char * 2162 VarGetPattern(GNode *ctxt, Var_Parse_State *vpstate MAKE_ATTR_UNUSED, 2163 int errnum, const char **tstr, int delim, int *flags, 2164 int *length, VarPattern *pattern) 2165 { 2166 const char *cp; 2167 char *rstr; 2168 Buffer buf; 2169 int junk; 2170 2171 Buf_Init(&buf, 0); 2172 if (length == NULL) 2173 length = &junk; 2174 2175 #define IS_A_MATCH(cp, delim) \ 2176 ((cp[0] == '\\') && ((cp[1] == delim) || \ 2177 (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&')))) 2178 2179 /* 2180 * Skim through until the matching delimiter is found; 2181 * pick up variable substitutions on the way. Also allow 2182 * backslashes to quote the delimiter, $, and \, but don't 2183 * touch other backslashes. 2184 */ 2185 for (cp = *tstr; *cp && (*cp != delim); cp++) { 2186 if (IS_A_MATCH(cp, delim)) { 2187 Buf_AddByte(&buf, cp[1]); 2188 cp++; 2189 } else if (*cp == '$') { 2190 if (cp[1] == delim) { 2191 if (flags == NULL) 2192 Buf_AddByte(&buf, *cp); 2193 else 2194 /* 2195 * Unescaped $ at end of pattern => anchor 2196 * pattern at end. 2197 */ 2198 *flags |= VAR_MATCH_END; 2199 } else { 2200 if (flags == NULL || (*flags & VAR_NOSUBST) == 0) { 2201 char *cp2; 2202 int len; 2203 void *freeIt; 2204 2205 /* 2206 * If unescaped dollar sign not before the 2207 * delimiter, assume it's a variable 2208 * substitution and recurse. 2209 */ 2210 cp2 = Var_Parse(cp, ctxt, errnum, &len, &freeIt); 2211 Buf_AddBytes(&buf, strlen(cp2), cp2); 2212 if (freeIt) 2213 free(freeIt); 2214 cp += len - 1; 2215 } else { 2216 const char *cp2 = &cp[1]; 2217 2218 if (*cp2 == PROPEN || *cp2 == BROPEN) { 2219 /* 2220 * Find the end of this variable reference 2221 * and suck it in without further ado. 2222 * It will be interperated later. 2223 */ 2224 int have = *cp2; 2225 int want = (*cp2 == PROPEN) ? PRCLOSE : BRCLOSE; 2226 int depth = 1; 2227 2228 for (++cp2; *cp2 != '\0' && depth > 0; ++cp2) { 2229 if (cp2[-1] != '\\') { 2230 if (*cp2 == have) 2231 ++depth; 2232 if (*cp2 == want) 2233 --depth; 2234 } 2235 } 2236 Buf_AddBytes(&buf, cp2 - cp, cp); 2237 cp = --cp2; 2238 } else 2239 Buf_AddByte(&buf, *cp); 2240 } 2241 } 2242 } 2243 else if (pattern && *cp == '&') 2244 Buf_AddBytes(&buf, pattern->leftLen, pattern->lhs); 2245 else 2246 Buf_AddByte(&buf, *cp); 2247 } 2248 2249 if (*cp != delim) { 2250 *tstr = cp; 2251 *length = 0; 2252 return NULL; 2253 } 2254 2255 *tstr = ++cp; 2256 *length = Buf_Size(&buf); 2257 rstr = Buf_Destroy(&buf, FALSE); 2258 if (DEBUG(VAR)) 2259 fprintf(debug_file, "Modifier pattern: \"%s\"\n", rstr); 2260 return rstr; 2261 } 2262 2263 /*- 2264 *----------------------------------------------------------------------- 2265 * VarQuote -- 2266 * Quote shell meta-characters in the string 2267 * 2268 * Results: 2269 * The quoted string 2270 * 2271 * Side Effects: 2272 * None. 2273 * 2274 *----------------------------------------------------------------------- 2275 */ 2276 static char * 2277 VarQuote(char *str) 2278 { 2279 2280 Buffer buf; 2281 /* This should cover most shells :-( */ 2282 static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~"; 2283 const char *newline; 2284 size_t len, nlen; 2285 2286 if ((newline = Shell_GetNewline()) == NULL) 2287 newline = "\\\n"; 2288 nlen = strlen(newline); 2289 2290 Buf_Init(&buf, 0); 2291 while (*str != '\0') { 2292 if ((len = strcspn(str, meta)) != 0) { 2293 Buf_AddBytes(&buf, len, str); 2294 str += len; 2295 } else if (*str == '\n') { 2296 Buf_AddBytes(&buf, nlen, newline); 2297 ++str; 2298 } else { 2299 Buf_AddByte(&buf, '\\'); 2300 Buf_AddByte(&buf, *str); 2301 ++str; 2302 } 2303 } 2304 str = Buf_Destroy(&buf, FALSE); 2305 if (DEBUG(VAR)) 2306 fprintf(debug_file, "QuoteMeta: [%s]\n", str); 2307 return str; 2308 } 2309 2310 /*- 2311 *----------------------------------------------------------------------- 2312 * VarHash -- 2313 * Hash the string using the MurmurHash3 algorithm. 2314 * Output is computed using 32bit Little Endian arithmetic. 2315 * 2316 * Input: 2317 * str String to modify 2318 * 2319 * Results: 2320 * Hash value of str, encoded as 8 hex digits. 2321 * 2322 * Side Effects: 2323 * None. 2324 * 2325 *----------------------------------------------------------------------- 2326 */ 2327 static char * 2328 VarHash(char *str) 2329 { 2330 static const char hexdigits[16] = "0123456789abcdef"; 2331 Buffer buf; 2332 size_t len, len2; 2333 unsigned char *ustr = (unsigned char *)str; 2334 uint32_t h, k, c1, c2; 2335 2336 h = 0x971e137bU; 2337 c1 = 0x95543787U; 2338 c2 = 0x2ad7eb25U; 2339 len2 = strlen(str); 2340 2341 for (len = len2; len; ) { 2342 k = 0; 2343 switch (len) { 2344 default: 2345 k = (ustr[3] << 24) | (ustr[2] << 16) | (ustr[1] << 8) | ustr[0]; 2346 len -= 4; 2347 ustr += 4; 2348 break; 2349 case 3: 2350 k |= (ustr[2] << 16); 2351 case 2: 2352 k |= (ustr[1] << 8); 2353 case 1: 2354 k |= ustr[0]; 2355 len = 0; 2356 } 2357 c1 = c1 * 5 + 0x7b7d159cU; 2358 c2 = c2 * 5 + 0x6bce6396U; 2359 k *= c1; 2360 k = (k << 11) ^ (k >> 21); 2361 k *= c2; 2362 h = (h << 13) ^ (h >> 19); 2363 h = h * 5 + 0x52dce729U; 2364 h ^= k; 2365 } 2366 h ^= len2; 2367 h *= 0x85ebca6b; 2368 h ^= h >> 13; 2369 h *= 0xc2b2ae35; 2370 h ^= h >> 16; 2371 2372 Buf_Init(&buf, 0); 2373 for (len = 0; len < 8; ++len) { 2374 Buf_AddByte(&buf, hexdigits[h & 15]); 2375 h >>= 4; 2376 } 2377 2378 return Buf_Destroy(&buf, FALSE); 2379 } 2380 2381 static char * 2382 VarStrftime(const char *fmt, int zulu) 2383 { 2384 char buf[BUFSIZ]; 2385 time_t utc; 2386 2387 time(&utc); 2388 if (!*fmt) 2389 fmt = "%c"; 2390 strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&utc) : localtime(&utc)); 2391 2392 buf[sizeof(buf) - 1] = '\0'; 2393 return bmake_strdup(buf); 2394 } 2395 2396 /* 2397 * Now we need to apply any modifiers the user wants applied. 2398 * These are: 2399 * :M<pattern> words which match the given <pattern>. 2400 * <pattern> is of the standard file 2401 * wildcarding form. 2402 * :N<pattern> words which do not match the given <pattern>. 2403 * :S<d><pat1><d><pat2><d>[1gW] 2404 * Substitute <pat2> for <pat1> in the value 2405 * :C<d><pat1><d><pat2><d>[1gW] 2406 * Substitute <pat2> for regex <pat1> in the value 2407 * :H Substitute the head of each word 2408 * :T Substitute the tail of each word 2409 * :E Substitute the extension (minus '.') of 2410 * each word 2411 * :R Substitute the root of each word 2412 * (pathname minus the suffix). 2413 * :O ("Order") Alphabeticaly sort words in variable. 2414 * :Ox ("intermiX") Randomize words in variable. 2415 * :u ("uniq") Remove adjacent duplicate words. 2416 * :tu Converts the variable contents to uppercase. 2417 * :tl Converts the variable contents to lowercase. 2418 * :ts[c] Sets varSpace - the char used to 2419 * separate words to 'c'. If 'c' is 2420 * omitted then no separation is used. 2421 * :tW Treat the variable contents as a single 2422 * word, even if it contains spaces. 2423 * (Mnemonic: one big 'W'ord.) 2424 * :tw Treat the variable contents as multiple 2425 * space-separated words. 2426 * (Mnemonic: many small 'w'ords.) 2427 * :[index] Select a single word from the value. 2428 * :[start..end] Select multiple words from the value. 2429 * :[*] or :[0] Select the entire value, as a single 2430 * word. Equivalent to :tW. 2431 * :[@] Select the entire value, as multiple 2432 * words. Undoes the effect of :[*]. 2433 * Equivalent to :tw. 2434 * :[#] Returns the number of words in the value. 2435 * 2436 * :?<true-value>:<false-value> 2437 * If the variable evaluates to true, return 2438 * true value, else return the second value. 2439 * :lhs=rhs Like :S, but the rhs goes to the end of 2440 * the invocation. 2441 * :sh Treat the current value as a command 2442 * to be run, new value is its output. 2443 * The following added so we can handle ODE makefiles. 2444 * :@<tmpvar>@<newval>@ 2445 * Assign a temporary local variable <tmpvar> 2446 * to the current value of each word in turn 2447 * and replace each word with the result of 2448 * evaluating <newval> 2449 * :D<newval> Use <newval> as value if variable defined 2450 * :U<newval> Use <newval> as value if variable undefined 2451 * :L Use the name of the variable as the value. 2452 * :P Use the path of the node that has the same 2453 * name as the variable as the value. This 2454 * basically includes an implied :L so that 2455 * the common method of refering to the path 2456 * of your dependent 'x' in a rule is to use 2457 * the form '${x:P}'. 2458 * :!<cmd>! Run cmd much the same as :sh run's the 2459 * current value of the variable. 2460 * The ::= modifiers, actually assign a value to the variable. 2461 * Their main purpose is in supporting modifiers of .for loop 2462 * iterators and other obscure uses. They always expand to 2463 * nothing. In a target rule that would otherwise expand to an 2464 * empty line they can be preceded with @: to keep make happy. 2465 * Eg. 2466 * 2467 * foo: .USE 2468 * .for i in ${.TARGET} ${.TARGET:R}.gz 2469 * @: ${t::=$i} 2470 * @echo blah ${t:T} 2471 * .endfor 2472 * 2473 * ::=<str> Assigns <str> as the new value of variable. 2474 * ::?=<str> Assigns <str> as value of variable if 2475 * it was not already set. 2476 * ::+=<str> Appends <str> to variable. 2477 * ::!=<cmd> Assigns output of <cmd> as the new value of 2478 * variable. 2479 */ 2480 2481 /* we now have some modifiers with long names */ 2482 #define STRMOD_MATCH(s, want, n) \ 2483 (strncmp(s, want, n) == 0 && (s[n] == endc || s[n] == ':')) 2484 2485 static char * 2486 ApplyModifiers(char *nstr, const char *tstr, 2487 int startc, int endc, 2488 Var *v, GNode *ctxt, Boolean errnum, 2489 int *lengthPtr, void **freePtr) 2490 { 2491 const char *start; 2492 const char *cp; /* Secondary pointer into str (place marker 2493 * for tstr) */ 2494 char *newStr; /* New value to return */ 2495 char termc; /* Character which terminated scan */ 2496 int cnt; /* Used to count brace pairs when variable in 2497 * in parens or braces */ 2498 char delim; 2499 int modifier; /* that we are processing */ 2500 Var_Parse_State parsestate; /* Flags passed to helper functions */ 2501 2502 delim = '\0'; 2503 parsestate.oneBigWord = FALSE; 2504 parsestate.varSpace = ' '; /* word separator */ 2505 2506 start = cp = tstr; 2507 2508 while (*tstr && *tstr != endc) { 2509 2510 if (*tstr == '$') { 2511 /* 2512 * We may have some complex modifiers in a variable. 2513 */ 2514 void *freeIt; 2515 char *rval; 2516 int rlen; 2517 int c; 2518 2519 rval = Var_Parse(tstr, ctxt, errnum, &rlen, &freeIt); 2520 2521 /* 2522 * If we have not parsed up to endc or ':', 2523 * we are not interested. 2524 */ 2525 if (rval != NULL && *rval && 2526 (c = tstr[rlen]) != '\0' && 2527 c != ':' && 2528 c != endc) { 2529 if (freeIt) 2530 free(freeIt); 2531 goto apply_mods; 2532 } 2533 2534 if (DEBUG(VAR)) { 2535 fprintf(debug_file, "Got '%s' from '%.*s'%.*s\n", 2536 rval, rlen, tstr, rlen, tstr + rlen); 2537 } 2538 2539 tstr += rlen; 2540 2541 if (rval != NULL && *rval) { 2542 int used; 2543 2544 nstr = ApplyModifiers(nstr, rval, 2545 0, 0, 2546 v, ctxt, errnum, &used, freePtr); 2547 if (nstr == var_Error 2548 || (nstr == varNoError && errnum == 0) 2549 || strlen(rval) != (size_t) used) { 2550 if (freeIt) 2551 free(freeIt); 2552 goto out; /* error already reported */ 2553 } 2554 } 2555 if (freeIt) 2556 free(freeIt); 2557 if (*tstr == ':') 2558 tstr++; 2559 else if (!*tstr && endc) { 2560 Error("Unclosed variable specification after complex modifier (expecting '%c') for %s", endc, v->name); 2561 goto out; 2562 } 2563 continue; 2564 } 2565 apply_mods: 2566 if (DEBUG(VAR)) { 2567 fprintf(debug_file, "Applying[%s] :%c to \"%s\"\n", v->name, 2568 *tstr, nstr); 2569 } 2570 newStr = var_Error; 2571 switch ((modifier = *tstr)) { 2572 case ':': 2573 { 2574 if (tstr[1] == '=' || 2575 (tstr[2] == '=' && 2576 (tstr[1] == '!' || tstr[1] == '+' || tstr[1] == '?'))) { 2577 /* 2578 * "::=", "::!=", "::+=", or "::?=" 2579 */ 2580 GNode *v_ctxt; /* context where v belongs */ 2581 const char *emsg; 2582 char *sv_name; 2583 VarPattern pattern; 2584 int how; 2585 2586 if (v->name[0] == 0) 2587 goto bad_modifier; 2588 2589 v_ctxt = ctxt; 2590 sv_name = NULL; 2591 ++tstr; 2592 if (v->flags & VAR_JUNK) { 2593 /* 2594 * We need to bmake_strdup() it incase 2595 * VarGetPattern() recurses. 2596 */ 2597 sv_name = v->name; 2598 v->name = bmake_strdup(v->name); 2599 } else if (ctxt != VAR_GLOBAL) { 2600 Var *gv = VarFind(v->name, ctxt, 0); 2601 if (gv == NULL) 2602 v_ctxt = VAR_GLOBAL; 2603 else 2604 VarFreeEnv(gv, TRUE); 2605 } 2606 2607 switch ((how = *tstr)) { 2608 case '+': 2609 case '?': 2610 case '!': 2611 cp = &tstr[2]; 2612 break; 2613 default: 2614 cp = ++tstr; 2615 break; 2616 } 2617 delim = startc == PROPEN ? PRCLOSE : BRCLOSE; 2618 pattern.flags = 0; 2619 2620 pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum, 2621 &cp, delim, NULL, 2622 &pattern.rightLen, 2623 NULL); 2624 if (v->flags & VAR_JUNK) { 2625 /* restore original name */ 2626 free(v->name); 2627 v->name = sv_name; 2628 } 2629 if (pattern.rhs == NULL) 2630 goto cleanup; 2631 2632 termc = *--cp; 2633 delim = '\0'; 2634 2635 switch (how) { 2636 case '+': 2637 Var_Append(v->name, pattern.rhs, v_ctxt); 2638 break; 2639 case '!': 2640 newStr = Cmd_Exec(pattern.rhs, &emsg); 2641 if (emsg) 2642 Error(emsg, nstr); 2643 else 2644 Var_Set(v->name, newStr, v_ctxt, 0); 2645 if (newStr) 2646 free(newStr); 2647 break; 2648 case '?': 2649 if ((v->flags & VAR_JUNK) == 0) 2650 break; 2651 /* FALLTHROUGH */ 2652 default: 2653 Var_Set(v->name, pattern.rhs, v_ctxt, 0); 2654 break; 2655 } 2656 free(UNCONST(pattern.rhs)); 2657 newStr = var_Error; 2658 break; 2659 } 2660 goto default_case; /* "::<unrecognised>" */ 2661 } 2662 case '@': 2663 { 2664 VarLoop_t loop; 2665 int flags = VAR_NOSUBST; 2666 2667 cp = ++tstr; 2668 delim = '@'; 2669 if ((loop.tvar = VarGetPattern(ctxt, &parsestate, errnum, 2670 &cp, delim, 2671 &flags, &loop.tvarLen, 2672 NULL)) == NULL) 2673 goto cleanup; 2674 2675 if ((loop.str = VarGetPattern(ctxt, &parsestate, errnum, 2676 &cp, delim, 2677 &flags, &loop.strLen, 2678 NULL)) == NULL) 2679 goto cleanup; 2680 2681 termc = *cp; 2682 delim = '\0'; 2683 2684 loop.errnum = errnum; 2685 loop.ctxt = ctxt; 2686 newStr = VarModify(ctxt, &parsestate, nstr, VarLoopExpand, 2687 &loop); 2688 free(loop.tvar); 2689 free(loop.str); 2690 break; 2691 } 2692 case 'U': 2693 #ifdef MAKE_FREEBSD_UL 2694 if (FreeBSD_UL) { 2695 int nc = tstr[1]; 2696 2697 /* we have to be careful, since :U is used internally */ 2698 if (nc == ':' || nc == endc) { 2699 char *dp = bmake_strdup(nstr); 2700 for (newStr = dp; *dp; dp++) 2701 *dp = toupper((unsigned char)*dp); 2702 cp = tstr + 1; 2703 termc = *cp; 2704 break; /* yes inside the conditional */ 2705 } 2706 /* FALLTHROUGH */ 2707 } 2708 #endif 2709 case 'D': 2710 { 2711 Buffer buf; /* Buffer for patterns */ 2712 int wantit; /* want data in buffer */ 2713 2714 /* 2715 * Pass through tstr looking for 1) escaped delimiters, 2716 * '$'s and backslashes (place the escaped character in 2717 * uninterpreted) and 2) unescaped $'s that aren't before 2718 * the delimiter (expand the variable substitution). 2719 * The result is left in the Buffer buf. 2720 */ 2721 Buf_Init(&buf, 0); 2722 for (cp = tstr + 1; 2723 *cp != endc && *cp != ':' && *cp != '\0'; 2724 cp++) { 2725 if ((*cp == '\\') && 2726 ((cp[1] == ':') || 2727 (cp[1] == '$') || 2728 (cp[1] == endc) || 2729 (cp[1] == '\\'))) 2730 { 2731 Buf_AddByte(&buf, cp[1]); 2732 cp++; 2733 } else if (*cp == '$') { 2734 /* 2735 * If unescaped dollar sign, assume it's a 2736 * variable substitution and recurse. 2737 */ 2738 char *cp2; 2739 int len; 2740 void *freeIt; 2741 2742 cp2 = Var_Parse(cp, ctxt, errnum, &len, &freeIt); 2743 Buf_AddBytes(&buf, strlen(cp2), cp2); 2744 if (freeIt) 2745 free(freeIt); 2746 cp += len - 1; 2747 } else { 2748 Buf_AddByte(&buf, *cp); 2749 } 2750 } 2751 2752 termc = *cp; 2753 2754 if (*tstr == 'U') 2755 wantit = ((v->flags & VAR_JUNK) != 0); 2756 else 2757 wantit = ((v->flags & VAR_JUNK) == 0); 2758 if ((v->flags & VAR_JUNK) != 0) 2759 v->flags |= VAR_KEEP; 2760 if (wantit) { 2761 newStr = Buf_Destroy(&buf, FALSE); 2762 } else { 2763 newStr = nstr; 2764 Buf_Destroy(&buf, TRUE); 2765 } 2766 break; 2767 } 2768 case 'L': 2769 #ifdef MAKE_FREEBSD_UL 2770 if (FreeBSD_UL) { 2771 char *dp = bmake_strdup(nstr); 2772 for (newStr = dp; *dp; dp++) 2773 *dp = tolower((unsigned char)*dp); 2774 cp = tstr + 1; 2775 termc = *cp; 2776 break; 2777 } 2778 /* FALLTHROUGH */ 2779 #endif 2780 { 2781 if ((v->flags & VAR_JUNK) != 0) 2782 v->flags |= VAR_KEEP; 2783 newStr = bmake_strdup(v->name); 2784 cp = ++tstr; 2785 termc = *tstr; 2786 break; 2787 } 2788 case 'P': 2789 { 2790 GNode *gn; 2791 2792 if ((v->flags & VAR_JUNK) != 0) 2793 v->flags |= VAR_KEEP; 2794 gn = Targ_FindNode(v->name, TARG_NOCREATE); 2795 if (gn == NULL || gn->type & OP_NOPATH) { 2796 newStr = NULL; 2797 } else if (gn->path) { 2798 newStr = bmake_strdup(gn->path); 2799 } else { 2800 newStr = Dir_FindFile(v->name, Suff_FindPath(gn)); 2801 } 2802 if (!newStr) { 2803 newStr = bmake_strdup(v->name); 2804 } 2805 cp = ++tstr; 2806 termc = *tstr; 2807 break; 2808 } 2809 case '!': 2810 { 2811 const char *emsg; 2812 VarPattern pattern; 2813 pattern.flags = 0; 2814 2815 delim = '!'; 2816 2817 cp = ++tstr; 2818 if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum, 2819 &cp, delim, 2820 NULL, &pattern.rightLen, 2821 NULL)) == NULL) 2822 goto cleanup; 2823 newStr = Cmd_Exec(pattern.rhs, &emsg); 2824 free(UNCONST(pattern.rhs)); 2825 if (emsg) 2826 Error(emsg, nstr); 2827 termc = *cp; 2828 delim = '\0'; 2829 if (v->flags & VAR_JUNK) { 2830 v->flags |= VAR_KEEP; 2831 } 2832 break; 2833 } 2834 case '[': 2835 { 2836 /* 2837 * Look for the closing ']', recursively 2838 * expanding any embedded variables. 2839 * 2840 * estr is a pointer to the expanded result, 2841 * which we must free(). 2842 */ 2843 char *estr; 2844 2845 cp = tstr+1; /* point to char after '[' */ 2846 delim = ']'; /* look for closing ']' */ 2847 estr = VarGetPattern(ctxt, &parsestate, 2848 errnum, &cp, delim, 2849 NULL, NULL, NULL); 2850 if (estr == NULL) 2851 goto cleanup; /* report missing ']' */ 2852 /* now cp points just after the closing ']' */ 2853 delim = '\0'; 2854 if (cp[0] != ':' && cp[0] != endc) { 2855 /* Found junk after ']' */ 2856 free(estr); 2857 goto bad_modifier; 2858 } 2859 if (estr[0] == '\0') { 2860 /* Found empty square brackets in ":[]". */ 2861 free(estr); 2862 goto bad_modifier; 2863 } else if (estr[0] == '#' && estr[1] == '\0') { 2864 /* Found ":[#]" */ 2865 2866 /* 2867 * We will need enough space for the decimal 2868 * representation of an int. We calculate the 2869 * space needed for the octal representation, 2870 * and add enough slop to cope with a '-' sign 2871 * (which should never be needed) and a '\0' 2872 * string terminator. 2873 */ 2874 int newStrSize = 2875 (sizeof(int) * CHAR_BIT + 2) / 3 + 2; 2876 2877 newStr = bmake_malloc(newStrSize); 2878 if (parsestate.oneBigWord) { 2879 strncpy(newStr, "1", newStrSize); 2880 } else { 2881 /* XXX: brk_string() is a rather expensive 2882 * way of counting words. */ 2883 char **av; 2884 char *as; 2885 int ac; 2886 2887 av = brk_string(nstr, &ac, FALSE, &as); 2888 snprintf(newStr, newStrSize, "%d", ac); 2889 free(as); 2890 free(av); 2891 } 2892 termc = *cp; 2893 free(estr); 2894 break; 2895 } else if (estr[0] == '*' && estr[1] == '\0') { 2896 /* Found ":[*]" */ 2897 parsestate.oneBigWord = TRUE; 2898 newStr = nstr; 2899 termc = *cp; 2900 free(estr); 2901 break; 2902 } else if (estr[0] == '@' && estr[1] == '\0') { 2903 /* Found ":[@]" */ 2904 parsestate.oneBigWord = FALSE; 2905 newStr = nstr; 2906 termc = *cp; 2907 free(estr); 2908 break; 2909 } else { 2910 /* 2911 * We expect estr to contain a single 2912 * integer for :[N], or two integers 2913 * separated by ".." for :[start..end]. 2914 */ 2915 char *ep; 2916 2917 VarSelectWords_t seldata = { 0, 0 }; 2918 2919 seldata.start = strtol(estr, &ep, 0); 2920 if (ep == estr) { 2921 /* Found junk instead of a number */ 2922 free(estr); 2923 goto bad_modifier; 2924 } else if (ep[0] == '\0') { 2925 /* Found only one integer in :[N] */ 2926 seldata.end = seldata.start; 2927 } else if (ep[0] == '.' && ep[1] == '.' && 2928 ep[2] != '\0') { 2929 /* Expecting another integer after ".." */ 2930 ep += 2; 2931 seldata.end = strtol(ep, &ep, 0); 2932 if (ep[0] != '\0') { 2933 /* Found junk after ".." */ 2934 free(estr); 2935 goto bad_modifier; 2936 } 2937 } else { 2938 /* Found junk instead of ".." */ 2939 free(estr); 2940 goto bad_modifier; 2941 } 2942 /* 2943 * Now seldata is properly filled in, 2944 * but we still have to check for 0 as 2945 * a special case. 2946 */ 2947 if (seldata.start == 0 && seldata.end == 0) { 2948 /* ":[0]" or perhaps ":[0..0]" */ 2949 parsestate.oneBigWord = TRUE; 2950 newStr = nstr; 2951 termc = *cp; 2952 free(estr); 2953 break; 2954 } else if (seldata.start == 0 || 2955 seldata.end == 0) { 2956 /* ":[0..N]" or ":[N..0]" */ 2957 free(estr); 2958 goto bad_modifier; 2959 } 2960 /* 2961 * Normal case: select the words 2962 * described by seldata. 2963 */ 2964 newStr = VarSelectWords(ctxt, &parsestate, 2965 nstr, &seldata); 2966 2967 termc = *cp; 2968 free(estr); 2969 break; 2970 } 2971 2972 } 2973 case 'g': 2974 cp = tstr + 1; /* make sure it is set */ 2975 if (STRMOD_MATCH(tstr, "gmtime", 6)) { 2976 newStr = VarStrftime(nstr, 1); 2977 cp = tstr + 6; 2978 termc = *cp; 2979 } else { 2980 goto default_case; 2981 } 2982 break; 2983 case 'h': 2984 cp = tstr + 1; /* make sure it is set */ 2985 if (STRMOD_MATCH(tstr, "hash", 4)) { 2986 newStr = VarHash(nstr); 2987 cp = tstr + 4; 2988 termc = *cp; 2989 } else { 2990 goto default_case; 2991 } 2992 break; 2993 case 'l': 2994 cp = tstr + 1; /* make sure it is set */ 2995 if (STRMOD_MATCH(tstr, "localtime", 9)) { 2996 newStr = VarStrftime(nstr, 0); 2997 cp = tstr + 9; 2998 termc = *cp; 2999 } else { 3000 goto default_case; 3001 } 3002 break; 3003 case 't': 3004 { 3005 cp = tstr + 1; /* make sure it is set */ 3006 if (tstr[1] != endc && tstr[1] != ':') { 3007 if (tstr[1] == 's') { 3008 /* 3009 * Use the char (if any) at tstr[2] 3010 * as the word separator. 3011 */ 3012 VarPattern pattern; 3013 3014 if (tstr[2] != endc && 3015 (tstr[3] == endc || tstr[3] == ':')) { 3016 /* ":ts<unrecognised><endc>" or 3017 * ":ts<unrecognised>:" */ 3018 parsestate.varSpace = tstr[2]; 3019 cp = tstr + 3; 3020 } else if (tstr[2] == endc || tstr[2] == ':') { 3021 /* ":ts<endc>" or ":ts:" */ 3022 parsestate.varSpace = 0; /* no separator */ 3023 cp = tstr + 2; 3024 } else if (tstr[2] == '\\') { 3025 switch (tstr[3]) { 3026 case 'n': 3027 parsestate.varSpace = '\n'; 3028 cp = tstr + 4; 3029 break; 3030 case 't': 3031 parsestate.varSpace = '\t'; 3032 cp = tstr + 4; 3033 break; 3034 default: 3035 if (isdigit((unsigned char)tstr[3])) { 3036 char *ep; 3037 3038 parsestate.varSpace = 3039 strtoul(&tstr[3], &ep, 0); 3040 if (*ep != ':' && *ep != endc) 3041 goto bad_modifier; 3042 cp = ep; 3043 } else { 3044 /* 3045 * ":ts<backslash><unrecognised>". 3046 */ 3047 goto bad_modifier; 3048 } 3049 break; 3050 } 3051 } else { 3052 /* 3053 * Found ":ts<unrecognised><unrecognised>". 3054 */ 3055 goto bad_modifier; 3056 } 3057 3058 termc = *cp; 3059 3060 /* 3061 * We cannot be certain that VarModify 3062 * will be used - even if there is a 3063 * subsequent modifier, so do a no-op 3064 * VarSubstitute now to for str to be 3065 * re-expanded without the spaces. 3066 */ 3067 pattern.flags = VAR_SUB_ONE; 3068 pattern.lhs = pattern.rhs = "\032"; 3069 pattern.leftLen = pattern.rightLen = 1; 3070 3071 newStr = VarModify(ctxt, &parsestate, nstr, 3072 VarSubstitute, 3073 &pattern); 3074 } else if (tstr[2] == endc || tstr[2] == ':') { 3075 /* 3076 * Check for two-character options: 3077 * ":tu", ":tl" 3078 */ 3079 if (tstr[1] == 'A') { /* absolute path */ 3080 newStr = VarModify(ctxt, &parsestate, nstr, 3081 VarRealpath, NULL); 3082 cp = tstr + 2; 3083 termc = *cp; 3084 } else if (tstr[1] == 'u') { 3085 char *dp = bmake_strdup(nstr); 3086 for (newStr = dp; *dp; dp++) 3087 *dp = toupper((unsigned char)*dp); 3088 cp = tstr + 2; 3089 termc = *cp; 3090 } else if (tstr[1] == 'l') { 3091 char *dp = bmake_strdup(nstr); 3092 for (newStr = dp; *dp; dp++) 3093 *dp = tolower((unsigned char)*dp); 3094 cp = tstr + 2; 3095 termc = *cp; 3096 } else if (tstr[1] == 'W' || tstr[1] == 'w') { 3097 parsestate.oneBigWord = (tstr[1] == 'W'); 3098 newStr = nstr; 3099 cp = tstr + 2; 3100 termc = *cp; 3101 } else { 3102 /* Found ":t<unrecognised>:" or 3103 * ":t<unrecognised><endc>". */ 3104 goto bad_modifier; 3105 } 3106 } else { 3107 /* 3108 * Found ":t<unrecognised><unrecognised>". 3109 */ 3110 goto bad_modifier; 3111 } 3112 } else { 3113 /* 3114 * Found ":t<endc>" or ":t:". 3115 */ 3116 goto bad_modifier; 3117 } 3118 break; 3119 } 3120 case 'N': 3121 case 'M': 3122 { 3123 char *pattern; 3124 const char *endpat; /* points just after end of pattern */ 3125 char *cp2; 3126 Boolean copy; /* pattern should be, or has been, copied */ 3127 Boolean needSubst; 3128 int nest; 3129 3130 copy = FALSE; 3131 needSubst = FALSE; 3132 nest = 1; 3133 /* 3134 * In the loop below, ignore ':' unless we are at 3135 * (or back to) the original brace level. 3136 * XXX This will likely not work right if $() and ${} 3137 * are intermixed. 3138 */ 3139 for (cp = tstr + 1; 3140 *cp != '\0' && !(*cp == ':' && nest == 1); 3141 cp++) 3142 { 3143 if (*cp == '\\' && 3144 (cp[1] == ':' || 3145 cp[1] == endc || cp[1] == startc)) { 3146 if (!needSubst) { 3147 copy = TRUE; 3148 } 3149 cp++; 3150 continue; 3151 } 3152 if (*cp == '$') { 3153 needSubst = TRUE; 3154 } 3155 if (*cp == '(' || *cp == '{') 3156 ++nest; 3157 if (*cp == ')' || *cp == '}') { 3158 --nest; 3159 if (nest == 0) 3160 break; 3161 } 3162 } 3163 termc = *cp; 3164 endpat = cp; 3165 if (copy) { 3166 /* 3167 * Need to compress the \:'s out of the pattern, so 3168 * allocate enough room to hold the uncompressed 3169 * pattern (note that cp started at tstr+1, so 3170 * cp - tstr takes the null byte into account) and 3171 * compress the pattern into the space. 3172 */ 3173 pattern = bmake_malloc(cp - tstr); 3174 for (cp2 = pattern, cp = tstr + 1; 3175 cp < endpat; 3176 cp++, cp2++) 3177 { 3178 if ((*cp == '\\') && (cp+1 < endpat) && 3179 (cp[1] == ':' || cp[1] == endc)) { 3180 cp++; 3181 } 3182 *cp2 = *cp; 3183 } 3184 *cp2 = '\0'; 3185 endpat = cp2; 3186 } else { 3187 /* 3188 * Either Var_Subst or VarModify will need a 3189 * nul-terminated string soon, so construct one now. 3190 */ 3191 pattern = bmake_strndup(tstr+1, endpat - (tstr + 1)); 3192 } 3193 if (needSubst) { 3194 /* 3195 * pattern contains embedded '$', so use Var_Subst to 3196 * expand it. 3197 */ 3198 cp2 = pattern; 3199 pattern = Var_Subst(NULL, cp2, ctxt, errnum); 3200 free(cp2); 3201 } 3202 if (DEBUG(VAR)) 3203 fprintf(debug_file, "Pattern[%s] for [%s] is [%s]\n", 3204 v->name, nstr, pattern); 3205 if (*tstr == 'M') { 3206 newStr = VarModify(ctxt, &parsestate, nstr, VarMatch, 3207 pattern); 3208 } else { 3209 newStr = VarModify(ctxt, &parsestate, nstr, VarNoMatch, 3210 pattern); 3211 } 3212 free(pattern); 3213 break; 3214 } 3215 case 'S': 3216 { 3217 VarPattern pattern; 3218 Var_Parse_State tmpparsestate; 3219 3220 pattern.flags = 0; 3221 tmpparsestate = parsestate; 3222 delim = tstr[1]; 3223 tstr += 2; 3224 3225 /* 3226 * If pattern begins with '^', it is anchored to the 3227 * start of the word -- skip over it and flag pattern. 3228 */ 3229 if (*tstr == '^') { 3230 pattern.flags |= VAR_MATCH_START; 3231 tstr += 1; 3232 } 3233 3234 cp = tstr; 3235 if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, errnum, 3236 &cp, delim, 3237 &pattern.flags, 3238 &pattern.leftLen, 3239 NULL)) == NULL) 3240 goto cleanup; 3241 3242 if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum, 3243 &cp, delim, NULL, 3244 &pattern.rightLen, 3245 &pattern)) == NULL) 3246 goto cleanup; 3247 3248 /* 3249 * Check for global substitution. If 'g' after the final 3250 * delimiter, substitution is global and is marked that 3251 * way. 3252 */ 3253 for (;; cp++) { 3254 switch (*cp) { 3255 case 'g': 3256 pattern.flags |= VAR_SUB_GLOBAL; 3257 continue; 3258 case '1': 3259 pattern.flags |= VAR_SUB_ONE; 3260 continue; 3261 case 'W': 3262 tmpparsestate.oneBigWord = TRUE; 3263 continue; 3264 } 3265 break; 3266 } 3267 3268 termc = *cp; 3269 newStr = VarModify(ctxt, &tmpparsestate, nstr, 3270 VarSubstitute, 3271 &pattern); 3272 3273 /* 3274 * Free the two strings. 3275 */ 3276 free(UNCONST(pattern.lhs)); 3277 free(UNCONST(pattern.rhs)); 3278 delim = '\0'; 3279 break; 3280 } 3281 case '?': 3282 { 3283 VarPattern pattern; 3284 Boolean value; 3285 3286 /* find ':', and then substitute accordingly */ 3287 3288 pattern.flags = 0; 3289 3290 cp = ++tstr; 3291 delim = ':'; 3292 if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, errnum, 3293 &cp, delim, NULL, 3294 &pattern.leftLen, 3295 NULL)) == NULL) 3296 goto cleanup; 3297 3298 /* BROPEN or PROPEN */ 3299 delim = endc; 3300 if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum, 3301 &cp, delim, NULL, 3302 &pattern.rightLen, 3303 NULL)) == NULL) 3304 goto cleanup; 3305 3306 termc = *--cp; 3307 delim = '\0'; 3308 if (Cond_EvalExpression(NULL, v->name, &value, 0) 3309 == COND_INVALID) { 3310 Error("Bad conditional expression `%s' in %s?%s:%s", 3311 v->name, v->name, pattern.lhs, pattern.rhs); 3312 goto cleanup; 3313 } 3314 3315 if (value) { 3316 newStr = UNCONST(pattern.lhs); 3317 free(UNCONST(pattern.rhs)); 3318 } else { 3319 newStr = UNCONST(pattern.rhs); 3320 free(UNCONST(pattern.lhs)); 3321 } 3322 if (v->flags & VAR_JUNK) { 3323 v->flags |= VAR_KEEP; 3324 } 3325 break; 3326 } 3327 #ifndef NO_REGEX 3328 case 'C': 3329 { 3330 VarREPattern pattern; 3331 char *re; 3332 int error; 3333 Var_Parse_State tmpparsestate; 3334 3335 pattern.flags = 0; 3336 tmpparsestate = parsestate; 3337 delim = tstr[1]; 3338 tstr += 2; 3339 3340 cp = tstr; 3341 3342 if ((re = VarGetPattern(ctxt, &parsestate, errnum, &cp, delim, 3343 NULL, NULL, NULL)) == NULL) 3344 goto cleanup; 3345 3346 if ((pattern.replace = VarGetPattern(ctxt, &parsestate, 3347 errnum, &cp, delim, NULL, 3348 NULL, NULL)) == NULL){ 3349 free(re); 3350 goto cleanup; 3351 } 3352 3353 for (;; cp++) { 3354 switch (*cp) { 3355 case 'g': 3356 pattern.flags |= VAR_SUB_GLOBAL; 3357 continue; 3358 case '1': 3359 pattern.flags |= VAR_SUB_ONE; 3360 continue; 3361 case 'W': 3362 tmpparsestate.oneBigWord = TRUE; 3363 continue; 3364 } 3365 break; 3366 } 3367 3368 termc = *cp; 3369 3370 error = regcomp(&pattern.re, re, REG_EXTENDED); 3371 free(re); 3372 if (error) { 3373 *lengthPtr = cp - start + 1; 3374 VarREError(error, &pattern.re, "RE substitution error"); 3375 free(pattern.replace); 3376 goto cleanup; 3377 } 3378 3379 pattern.nsub = pattern.re.re_nsub + 1; 3380 if (pattern.nsub < 1) 3381 pattern.nsub = 1; 3382 if (pattern.nsub > 10) 3383 pattern.nsub = 10; 3384 pattern.matches = bmake_malloc(pattern.nsub * 3385 sizeof(regmatch_t)); 3386 newStr = VarModify(ctxt, &tmpparsestate, nstr, 3387 VarRESubstitute, 3388 &pattern); 3389 regfree(&pattern.re); 3390 free(pattern.replace); 3391 free(pattern.matches); 3392 delim = '\0'; 3393 break; 3394 } 3395 #endif 3396 case 'Q': 3397 if (tstr[1] == endc || tstr[1] == ':') { 3398 newStr = VarQuote(nstr); 3399 cp = tstr + 1; 3400 termc = *cp; 3401 break; 3402 } 3403 goto default_case; 3404 case 'T': 3405 if (tstr[1] == endc || tstr[1] == ':') { 3406 newStr = VarModify(ctxt, &parsestate, nstr, VarTail, 3407 NULL); 3408 cp = tstr + 1; 3409 termc = *cp; 3410 break; 3411 } 3412 goto default_case; 3413 case 'H': 3414 if (tstr[1] == endc || tstr[1] == ':') { 3415 newStr = VarModify(ctxt, &parsestate, nstr, VarHead, 3416 NULL); 3417 cp = tstr + 1; 3418 termc = *cp; 3419 break; 3420 } 3421 goto default_case; 3422 case 'E': 3423 if (tstr[1] == endc || tstr[1] == ':') { 3424 newStr = VarModify(ctxt, &parsestate, nstr, VarSuffix, 3425 NULL); 3426 cp = tstr + 1; 3427 termc = *cp; 3428 break; 3429 } 3430 goto default_case; 3431 case 'R': 3432 if (tstr[1] == endc || tstr[1] == ':') { 3433 newStr = VarModify(ctxt, &parsestate, nstr, VarRoot, 3434 NULL); 3435 cp = tstr + 1; 3436 termc = *cp; 3437 break; 3438 } 3439 goto default_case; 3440 case 'O': 3441 { 3442 char otype; 3443 3444 cp = tstr + 1; /* skip to the rest in any case */ 3445 if (tstr[1] == endc || tstr[1] == ':') { 3446 otype = 's'; 3447 termc = *cp; 3448 } else if ( (tstr[1] == 'x') && 3449 (tstr[2] == endc || tstr[2] == ':') ) { 3450 otype = tstr[1]; 3451 cp = tstr + 2; 3452 termc = *cp; 3453 } else { 3454 goto bad_modifier; 3455 } 3456 newStr = VarOrder(nstr, otype); 3457 break; 3458 } 3459 case 'u': 3460 if (tstr[1] == endc || tstr[1] == ':') { 3461 newStr = VarUniq(nstr); 3462 cp = tstr + 1; 3463 termc = *cp; 3464 break; 3465 } 3466 goto default_case; 3467 #ifdef SUNSHCMD 3468 case 's': 3469 if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) { 3470 const char *emsg; 3471 newStr = Cmd_Exec(nstr, &emsg); 3472 if (emsg) 3473 Error(emsg, nstr); 3474 cp = tstr + 2; 3475 termc = *cp; 3476 break; 3477 } 3478 goto default_case; 3479 #endif 3480 default: 3481 default_case: 3482 { 3483 #ifdef SYSVVARSUB 3484 /* 3485 * This can either be a bogus modifier or a System-V 3486 * substitution command. 3487 */ 3488 VarPattern pattern; 3489 Boolean eqFound; 3490 3491 pattern.flags = 0; 3492 eqFound = FALSE; 3493 /* 3494 * First we make a pass through the string trying 3495 * to verify it is a SYSV-make-style translation: 3496 * it must be: <string1>=<string2>) 3497 */ 3498 cp = tstr; 3499 cnt = 1; 3500 while (*cp != '\0' && cnt) { 3501 if (*cp == '=') { 3502 eqFound = TRUE; 3503 /* continue looking for endc */ 3504 } 3505 else if (*cp == endc) 3506 cnt--; 3507 else if (*cp == startc) 3508 cnt++; 3509 if (cnt) 3510 cp++; 3511 } 3512 if (*cp == endc && eqFound) { 3513 3514 /* 3515 * Now we break this sucker into the lhs and 3516 * rhs. We must null terminate them of course. 3517 */ 3518 delim='='; 3519 cp = tstr; 3520 if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, 3521 errnum, &cp, delim, &pattern.flags, 3522 &pattern.leftLen, NULL)) == NULL) 3523 goto cleanup; 3524 delim = endc; 3525 if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, 3526 errnum, &cp, delim, NULL, &pattern.rightLen, 3527 &pattern)) == NULL) 3528 goto cleanup; 3529 3530 /* 3531 * SYSV modifications happen through the whole 3532 * string. Note the pattern is anchored at the end. 3533 */ 3534 termc = *--cp; 3535 delim = '\0'; 3536 if (pattern.leftLen == 0 && *nstr == '\0') { 3537 newStr = nstr; /* special case */ 3538 } else { 3539 newStr = VarModify(ctxt, &parsestate, nstr, 3540 VarSYSVMatch, 3541 &pattern); 3542 } 3543 free(UNCONST(pattern.lhs)); 3544 free(UNCONST(pattern.rhs)); 3545 } else 3546 #endif 3547 { 3548 Error("Unknown modifier '%c'", *tstr); 3549 for (cp = tstr+1; 3550 *cp != ':' && *cp != endc && *cp != '\0'; 3551 cp++) 3552 continue; 3553 termc = *cp; 3554 newStr = var_Error; 3555 } 3556 } 3557 } 3558 if (DEBUG(VAR)) { 3559 fprintf(debug_file, "Result[%s] of :%c is \"%s\"\n", 3560 v->name, modifier, newStr); 3561 } 3562 3563 if (newStr != nstr) { 3564 if (*freePtr) { 3565 free(nstr); 3566 *freePtr = NULL; 3567 } 3568 nstr = newStr; 3569 if (nstr != var_Error && nstr != varNoError) { 3570 *freePtr = nstr; 3571 } 3572 } 3573 if (termc == '\0' && endc != '\0') { 3574 Error("Unclosed variable specification (expecting '%c') for \"%s\" (value \"%s\") modifier %c", endc, v->name, nstr, modifier); 3575 } else if (termc == ':') { 3576 cp++; 3577 } 3578 tstr = cp; 3579 } 3580 out: 3581 *lengthPtr = tstr - start; 3582 return (nstr); 3583 3584 bad_modifier: 3585 /* "{(" */ 3586 Error("Bad modifier `:%.*s' for %s", (int)strcspn(tstr, ":)}"), tstr, 3587 v->name); 3588 3589 cleanup: 3590 *lengthPtr = cp - start; 3591 if (delim != '\0') 3592 Error("Unclosed substitution for %s (%c missing)", 3593 v->name, delim); 3594 if (*freePtr) { 3595 free(*freePtr); 3596 *freePtr = NULL; 3597 } 3598 return (var_Error); 3599 } 3600 3601 /*- 3602 *----------------------------------------------------------------------- 3603 * Var_Parse -- 3604 * Given the start of a variable invocation, extract the variable 3605 * name and find its value, then modify it according to the 3606 * specification. 3607 * 3608 * Input: 3609 * str The string to parse 3610 * ctxt The context for the variable 3611 * errnum TRUE if undefined variables are an error 3612 * lengthPtr OUT: The length of the specification 3613 * freePtr OUT: Non-NULL if caller should free *freePtr 3614 * 3615 * Results: 3616 * The (possibly-modified) value of the variable or var_Error if the 3617 * specification is invalid. The length of the specification is 3618 * placed in *lengthPtr (for invalid specifications, this is just 3619 * 2...?). 3620 * If *freePtr is non-NULL then it's a pointer that the caller 3621 * should pass to free() to free memory used by the result. 3622 * 3623 * Side Effects: 3624 * None. 3625 * 3626 *----------------------------------------------------------------------- 3627 */ 3628 /* coverity[+alloc : arg-*4] */ 3629 char * 3630 Var_Parse(const char *str, GNode *ctxt, Boolean errnum, int *lengthPtr, 3631 void **freePtr) 3632 { 3633 const char *tstr; /* Pointer into str */ 3634 Var *v; /* Variable in invocation */ 3635 Boolean haveModifier;/* TRUE if have modifiers for the variable */ 3636 char endc; /* Ending character when variable in parens 3637 * or braces */ 3638 char startc; /* Starting character when variable in parens 3639 * or braces */ 3640 int vlen; /* Length of variable name */ 3641 const char *start; /* Points to original start of str */ 3642 char *nstr; /* New string, used during expansion */ 3643 Boolean dynamic; /* TRUE if the variable is local and we're 3644 * expanding it in a non-local context. This 3645 * is done to support dynamic sources. The 3646 * result is just the invocation, unaltered */ 3647 Var_Parse_State parsestate; /* Flags passed to helper functions */ 3648 char name[2]; 3649 3650 *freePtr = NULL; 3651 dynamic = FALSE; 3652 start = str; 3653 parsestate.oneBigWord = FALSE; 3654 parsestate.varSpace = ' '; /* word separator */ 3655 3656 startc = str[1]; 3657 if (startc != PROPEN && startc != BROPEN) { 3658 /* 3659 * If it's not bounded by braces of some sort, life is much simpler. 3660 * We just need to check for the first character and return the 3661 * value if it exists. 3662 */ 3663 3664 /* Error out some really stupid names */ 3665 if (startc == '\0' || strchr(")}:$", startc)) { 3666 *lengthPtr = 1; 3667 return var_Error; 3668 } 3669 name[0] = startc; 3670 name[1] = '\0'; 3671 3672 v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); 3673 if (v == NULL) { 3674 *lengthPtr = 2; 3675 3676 if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) { 3677 /* 3678 * If substituting a local variable in a non-local context, 3679 * assume it's for dynamic source stuff. We have to handle 3680 * this specially and return the longhand for the variable 3681 * with the dollar sign escaped so it makes it back to the 3682 * caller. Only four of the local variables are treated 3683 * specially as they are the only four that will be set 3684 * when dynamic sources are expanded. 3685 */ 3686 switch (str[1]) { 3687 case '@': 3688 return UNCONST("$(.TARGET)"); 3689 case '%': 3690 return UNCONST("$(.ARCHIVE)"); 3691 case '*': 3692 return UNCONST("$(.PREFIX)"); 3693 case '!': 3694 return UNCONST("$(.MEMBER)"); 3695 } 3696 } 3697 /* 3698 * Error 3699 */ 3700 return (errnum ? var_Error : varNoError); 3701 } else { 3702 haveModifier = FALSE; 3703 tstr = &str[1]; 3704 endc = str[1]; 3705 } 3706 } else { 3707 Buffer buf; /* Holds the variable name */ 3708 3709 endc = startc == PROPEN ? PRCLOSE : BRCLOSE; 3710 Buf_Init(&buf, 0); 3711 3712 /* 3713 * Skip to the end character or a colon, whichever comes first. 3714 */ 3715 for (tstr = str + 2; 3716 *tstr != '\0' && *tstr != endc && *tstr != ':'; 3717 tstr++) 3718 { 3719 /* 3720 * A variable inside a variable, expand 3721 */ 3722 if (*tstr == '$') { 3723 int rlen; 3724 void *freeIt; 3725 char *rval = Var_Parse(tstr, ctxt, errnum, &rlen, &freeIt); 3726 if (rval != NULL) { 3727 Buf_AddBytes(&buf, strlen(rval), rval); 3728 } 3729 if (freeIt) 3730 free(freeIt); 3731 tstr += rlen - 1; 3732 } 3733 else 3734 Buf_AddByte(&buf, *tstr); 3735 } 3736 if (*tstr == ':') { 3737 haveModifier = TRUE; 3738 } else if (*tstr != '\0') { 3739 haveModifier = FALSE; 3740 } else { 3741 /* 3742 * If we never did find the end character, return NULL 3743 * right now, setting the length to be the distance to 3744 * the end of the string, since that's what make does. 3745 */ 3746 *lengthPtr = tstr - str; 3747 Buf_Destroy(&buf, TRUE); 3748 return (var_Error); 3749 } 3750 str = Buf_GetAll(&buf, &vlen); 3751 3752 /* 3753 * At this point, str points into newly allocated memory from 3754 * buf, containing only the name of the variable. 3755 * 3756 * start and tstr point into the const string that was pointed 3757 * to by the original value of the str parameter. start points 3758 * to the '$' at the beginning of the string, while tstr points 3759 * to the char just after the end of the variable name -- this 3760 * will be '\0', ':', PRCLOSE, or BRCLOSE. 3761 */ 3762 3763 v = VarFind(str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); 3764 /* 3765 * Check also for bogus D and F forms of local variables since we're 3766 * in a local context and the name is the right length. 3767 */ 3768 if ((v == NULL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) && 3769 (vlen == 2) && (str[1] == 'F' || str[1] == 'D') && 3770 strchr("@%*!<>", str[0]) != NULL) { 3771 /* 3772 * Well, it's local -- go look for it. 3773 */ 3774 name[0] = *str; 3775 name[1] = '\0'; 3776 v = VarFind(name, ctxt, 0); 3777 3778 if (v != NULL) { 3779 /* 3780 * No need for nested expansion or anything, as we're 3781 * the only one who sets these things and we sure don't 3782 * but nested invocations in them... 3783 */ 3784 nstr = Buf_GetAll(&v->val, NULL); 3785 3786 if (str[1] == 'D') { 3787 nstr = VarModify(ctxt, &parsestate, nstr, VarHead, 3788 NULL); 3789 } else { 3790 nstr = VarModify(ctxt, &parsestate, nstr, VarTail, 3791 NULL); 3792 } 3793 /* 3794 * Resulting string is dynamically allocated, so 3795 * tell caller to free it. 3796 */ 3797 *freePtr = nstr; 3798 *lengthPtr = tstr-start+1; 3799 Buf_Destroy(&buf, TRUE); 3800 VarFreeEnv(v, TRUE); 3801 return nstr; 3802 } 3803 } 3804 3805 if (v == NULL) { 3806 if (((vlen == 1) || 3807 (((vlen == 2) && (str[1] == 'F' || str[1] == 'D')))) && 3808 ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL))) 3809 { 3810 /* 3811 * If substituting a local variable in a non-local context, 3812 * assume it's for dynamic source stuff. We have to handle 3813 * this specially and return the longhand for the variable 3814 * with the dollar sign escaped so it makes it back to the 3815 * caller. Only four of the local variables are treated 3816 * specially as they are the only four that will be set 3817 * when dynamic sources are expanded. 3818 */ 3819 switch (*str) { 3820 case '@': 3821 case '%': 3822 case '*': 3823 case '!': 3824 dynamic = TRUE; 3825 break; 3826 } 3827 } else if ((vlen > 2) && (*str == '.') && 3828 isupper((unsigned char) str[1]) && 3829 ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL))) 3830 { 3831 int len; 3832 3833 len = vlen - 1; 3834 if ((strncmp(str, ".TARGET", len) == 0) || 3835 (strncmp(str, ".ARCHIVE", len) == 0) || 3836 (strncmp(str, ".PREFIX", len) == 0) || 3837 (strncmp(str, ".MEMBER", len) == 0)) 3838 { 3839 dynamic = TRUE; 3840 } 3841 } 3842 3843 if (!haveModifier) { 3844 /* 3845 * No modifiers -- have specification length so we can return 3846 * now. 3847 */ 3848 *lengthPtr = tstr - start + 1; 3849 if (dynamic) { 3850 char *pstr = bmake_strndup(start, *lengthPtr); 3851 *freePtr = pstr; 3852 Buf_Destroy(&buf, TRUE); 3853 return(pstr); 3854 } else { 3855 Buf_Destroy(&buf, TRUE); 3856 return (errnum ? var_Error : varNoError); 3857 } 3858 } else { 3859 /* 3860 * Still need to get to the end of the variable specification, 3861 * so kludge up a Var structure for the modifications 3862 */ 3863 v = bmake_malloc(sizeof(Var)); 3864 v->name = UNCONST(str); 3865 Buf_Init(&v->val, 1); 3866 v->flags = VAR_JUNK; 3867 Buf_Destroy(&buf, FALSE); 3868 } 3869 } else 3870 Buf_Destroy(&buf, TRUE); 3871 } 3872 3873 if (v->flags & VAR_IN_USE) { 3874 Fatal("Variable %s is recursive.", v->name); 3875 /*NOTREACHED*/ 3876 } else { 3877 v->flags |= VAR_IN_USE; 3878 } 3879 /* 3880 * Before doing any modification, we have to make sure the value 3881 * has been fully expanded. If it looks like recursion might be 3882 * necessary (there's a dollar sign somewhere in the variable's value) 3883 * we just call Var_Subst to do any other substitutions that are 3884 * necessary. Note that the value returned by Var_Subst will have 3885 * been dynamically-allocated, so it will need freeing when we 3886 * return. 3887 */ 3888 nstr = Buf_GetAll(&v->val, NULL); 3889 if (strchr(nstr, '$') != NULL) { 3890 nstr = Var_Subst(NULL, nstr, ctxt, errnum); 3891 *freePtr = nstr; 3892 } 3893 3894 v->flags &= ~VAR_IN_USE; 3895 3896 if ((nstr != NULL) && haveModifier) { 3897 int used; 3898 /* 3899 * Skip initial colon. 3900 */ 3901 tstr++; 3902 3903 nstr = ApplyModifiers(nstr, tstr, startc, endc, 3904 v, ctxt, errnum, &used, freePtr); 3905 tstr += used; 3906 } 3907 if (*tstr) { 3908 *lengthPtr = tstr - start + 1; 3909 } else { 3910 *lengthPtr = tstr - start; 3911 } 3912 3913 if (v->flags & VAR_FROM_ENV) { 3914 Boolean destroy = FALSE; 3915 3916 if (nstr != Buf_GetAll(&v->val, NULL)) { 3917 destroy = TRUE; 3918 } else { 3919 /* 3920 * Returning the value unmodified, so tell the caller to free 3921 * the thing. 3922 */ 3923 *freePtr = nstr; 3924 } 3925 VarFreeEnv(v, destroy); 3926 } else if (v->flags & VAR_JUNK) { 3927 /* 3928 * Perform any free'ing needed and set *freePtr to NULL so the caller 3929 * doesn't try to free a static pointer. 3930 * If VAR_KEEP is also set then we want to keep str as is. 3931 */ 3932 if (!(v->flags & VAR_KEEP)) { 3933 if (*freePtr) { 3934 free(nstr); 3935 *freePtr = NULL; 3936 } 3937 if (dynamic) { 3938 nstr = bmake_strndup(start, *lengthPtr); 3939 *freePtr = nstr; 3940 } else { 3941 nstr = errnum ? var_Error : varNoError; 3942 } 3943 } 3944 if (nstr != Buf_GetAll(&v->val, NULL)) 3945 Buf_Destroy(&v->val, TRUE); 3946 free(v->name); 3947 free(v); 3948 } 3949 return (nstr); 3950 } 3951 3952 /*- 3953 *----------------------------------------------------------------------- 3954 * Var_Subst -- 3955 * Substitute for all variables in the given string in the given context 3956 * If undefErr is TRUE, Parse_Error will be called when an undefined 3957 * variable is encountered. 3958 * 3959 * Input: 3960 * var Named variable || NULL for all 3961 * str the string which to substitute 3962 * ctxt the context wherein to find variables 3963 * undefErr TRUE if undefineds are an error 3964 * 3965 * Results: 3966 * The resulting string. 3967 * 3968 * Side Effects: 3969 * None. The old string must be freed by the caller 3970 *----------------------------------------------------------------------- 3971 */ 3972 char * 3973 Var_Subst(const char *var, const char *str, GNode *ctxt, Boolean undefErr) 3974 { 3975 Buffer buf; /* Buffer for forming things */ 3976 char *val; /* Value to substitute for a variable */ 3977 int length; /* Length of the variable invocation */ 3978 Boolean trailingBslash; /* variable ends in \ */ 3979 void *freeIt = NULL; /* Set if it should be freed */ 3980 static Boolean errorReported; /* Set true if an error has already 3981 * been reported to prevent a plethora 3982 * of messages when recursing */ 3983 3984 Buf_Init(&buf, 0); 3985 errorReported = FALSE; 3986 trailingBslash = FALSE; 3987 3988 while (*str) { 3989 if (*str == '\n' && trailingBslash) 3990 Buf_AddByte(&buf, ' '); 3991 if (var == NULL && (*str == '$') && (str[1] == '$')) { 3992 /* 3993 * A dollar sign may be escaped either with another dollar sign. 3994 * In such a case, we skip over the escape character and store the 3995 * dollar sign into the buffer directly. 3996 */ 3997 str++; 3998 Buf_AddByte(&buf, *str); 3999 str++; 4000 } else if (*str != '$') { 4001 /* 4002 * Skip as many characters as possible -- either to the end of 4003 * the string or to the next dollar sign (variable invocation). 4004 */ 4005 const char *cp; 4006 4007 for (cp = str++; *str != '$' && *str != '\0'; str++) 4008 continue; 4009 Buf_AddBytes(&buf, str - cp, cp); 4010 } else { 4011 if (var != NULL) { 4012 int expand; 4013 for (;;) { 4014 if (str[1] == '\0') { 4015 /* A trailing $ is kind of a special case */ 4016 Buf_AddByte(&buf, str[0]); 4017 str++; 4018 expand = FALSE; 4019 } else if (str[1] != PROPEN && str[1] != BROPEN) { 4020 if (str[1] != *var || strlen(var) > 1) { 4021 Buf_AddBytes(&buf, 2, str); 4022 str += 2; 4023 expand = FALSE; 4024 } 4025 else 4026 expand = TRUE; 4027 break; 4028 } 4029 else { 4030 const char *p; 4031 4032 /* 4033 * Scan up to the end of the variable name. 4034 */ 4035 for (p = &str[2]; *p && 4036 *p != ':' && *p != PRCLOSE && *p != BRCLOSE; p++) 4037 if (*p == '$') 4038 break; 4039 /* 4040 * A variable inside the variable. We cannot expand 4041 * the external variable yet, so we try again with 4042 * the nested one 4043 */ 4044 if (*p == '$') { 4045 Buf_AddBytes(&buf, p - str, str); 4046 str = p; 4047 continue; 4048 } 4049 4050 if (strncmp(var, str + 2, p - str - 2) != 0 || 4051 var[p - str - 2] != '\0') { 4052 /* 4053 * Not the variable we want to expand, scan 4054 * until the next variable 4055 */ 4056 for (;*p != '$' && *p != '\0'; p++) 4057 continue; 4058 Buf_AddBytes(&buf, p - str, str); 4059 str = p; 4060 expand = FALSE; 4061 } 4062 else 4063 expand = TRUE; 4064 break; 4065 } 4066 } 4067 if (!expand) 4068 continue; 4069 } 4070 4071 val = Var_Parse(str, ctxt, undefErr, &length, &freeIt); 4072 4073 /* 4074 * When we come down here, val should either point to the 4075 * value of this variable, suitably modified, or be NULL. 4076 * Length should be the total length of the potential 4077 * variable invocation (from $ to end character...) 4078 */ 4079 if (val == var_Error || val == varNoError) { 4080 /* 4081 * If performing old-time variable substitution, skip over 4082 * the variable and continue with the substitution. Otherwise, 4083 * store the dollar sign and advance str so we continue with 4084 * the string... 4085 */ 4086 if (oldVars) { 4087 str += length; 4088 } else if (undefErr) { 4089 /* 4090 * If variable is undefined, complain and skip the 4091 * variable. The complaint will stop us from doing anything 4092 * when the file is parsed. 4093 */ 4094 if (!errorReported) { 4095 Parse_Error(PARSE_FATAL, 4096 "Undefined variable \"%.*s\"",length,str); 4097 } 4098 str += length; 4099 errorReported = TRUE; 4100 } else { 4101 Buf_AddByte(&buf, *str); 4102 str += 1; 4103 } 4104 } else { 4105 /* 4106 * We've now got a variable structure to store in. But first, 4107 * advance the string pointer. 4108 */ 4109 str += length; 4110 4111 /* 4112 * Copy all the characters from the variable value straight 4113 * into the new string. 4114 */ 4115 length = strlen(val); 4116 Buf_AddBytes(&buf, length, val); 4117 trailingBslash = length > 0 && val[length - 1] == '\\'; 4118 } 4119 if (freeIt) { 4120 free(freeIt); 4121 freeIt = NULL; 4122 } 4123 } 4124 } 4125 4126 return Buf_DestroyCompact(&buf); 4127 } 4128 4129 /*- 4130 *----------------------------------------------------------------------- 4131 * Var_GetTail -- 4132 * Return the tail from each of a list of words. Used to set the 4133 * System V local variables. 4134 * 4135 * Input: 4136 * file Filename to modify 4137 * 4138 * Results: 4139 * The resulting string. 4140 * 4141 * Side Effects: 4142 * None. 4143 * 4144 *----------------------------------------------------------------------- 4145 */ 4146 #if 0 4147 char * 4148 Var_GetTail(char *file) 4149 { 4150 return(VarModify(file, VarTail, NULL)); 4151 } 4152 4153 /*- 4154 *----------------------------------------------------------------------- 4155 * Var_GetHead -- 4156 * Find the leading components of a (list of) filename(s). 4157 * XXX: VarHead does not replace foo by ., as (sun) System V make 4158 * does. 4159 * 4160 * Input: 4161 * file Filename to manipulate 4162 * 4163 * Results: 4164 * The leading components. 4165 * 4166 * Side Effects: 4167 * None. 4168 * 4169 *----------------------------------------------------------------------- 4170 */ 4171 char * 4172 Var_GetHead(char *file) 4173 { 4174 return(VarModify(file, VarHead, NULL)); 4175 } 4176 #endif 4177 4178 /*- 4179 *----------------------------------------------------------------------- 4180 * Var_Init -- 4181 * Initialize the module 4182 * 4183 * Results: 4184 * None 4185 * 4186 * Side Effects: 4187 * The VAR_CMD and VAR_GLOBAL contexts are created 4188 *----------------------------------------------------------------------- 4189 */ 4190 void 4191 Var_Init(void) 4192 { 4193 VAR_INTERNAL = Targ_NewGN("Internal"); 4194 VAR_GLOBAL = Targ_NewGN("Global"); 4195 VAR_CMD = Targ_NewGN("Command"); 4196 4197 } 4198 4199 4200 void 4201 Var_End(void) 4202 { 4203 } 4204 4205 4206 /****************** PRINT DEBUGGING INFO *****************/ 4207 static void 4208 VarPrintVar(void *vp) 4209 { 4210 Var *v = (Var *)vp; 4211 fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL)); 4212 } 4213 4214 /*- 4215 *----------------------------------------------------------------------- 4216 * Var_Dump -- 4217 * print all variables in a context 4218 *----------------------------------------------------------------------- 4219 */ 4220 void 4221 Var_Dump(GNode *ctxt) 4222 { 4223 Hash_Search search; 4224 Hash_Entry *h; 4225 4226 for (h = Hash_EnumFirst(&ctxt->context, &search); 4227 h != NULL; 4228 h = Hash_EnumNext(&search)) { 4229 VarPrintVar(Hash_GetValue(h)); 4230 } 4231 } 4232