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