1 /* $OpenBSD: var.c,v 1.87 2010/07/19 19:46:44 espie Exp $ */ 2 /* $NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1999,2000,2007 Marc Espie. 6 * 7 * Extensive code modifications for the OpenBSD project. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD 22 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 /* 31 * Copyright (c) 1988, 1989, 1990, 1993 32 * The Regents of the University of California. All rights reserved. 33 * Copyright (c) 1989 by Berkeley Softworks 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to Berkeley by 37 * Adam de Boor. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. Neither the name of the University nor the names of its contributors 48 * may be used to endorse or promote products derived from this software 49 * without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 */ 63 64 #include <assert.h> 65 #include <stddef.h> 66 #include <stdio.h> 67 #include <stdint.h> 68 #include <stdlib.h> 69 #include <string.h> 70 71 #include "config.h" 72 #include "defines.h" 73 #include "buf.h" 74 #include "stats.h" 75 #include "ohash.h" 76 #include "pathnames.h" 77 #include "varmodifiers.h" 78 #include "var.h" 79 #include "varname.h" 80 #include "error.h" 81 #include "str.h" 82 #include "var_int.h" 83 #include "memory.h" 84 #include "symtable.h" 85 #include "gnode.h" 86 87 /* 88 * This is a harmless return value for Var_Parse that can be used by Var_Subst 89 * to determine if there was an error in parsing -- easier than returning 90 * a flag, as things outside this module don't give a hoot. 91 */ 92 char var_Error[] = ""; 93 94 /* 95 * Similar to var_Error, but returned when the 'err' flag for Var_Parse is 96 * set false. Why not just use a constant? Well, gcc likes to condense 97 * identical string instances... 98 */ 99 static char varNoError[] = ""; 100 bool errorIsOkay; 101 static bool checkEnvFirst; /* true if environment should be searched for 102 * variables before the global context */ 103 104 void 105 Var_setCheckEnvFirst(bool yes) 106 { 107 checkEnvFirst = yes; 108 } 109 110 /* 111 * The rules for variable look-up are complicated. 112 * 113 * - Dynamic variables like $@ and $* are special. They always pertain to 114 * a given variable. In this implementation of make, it is an error to 115 * try to affect them manually. They are stored in a local symtable directly 116 * inside the gnode. 117 * 118 * Global variables can be obtained: 119 * - from the command line 120 * - from the environment 121 * - from the Makefile proper. 122 * All of these are stored in a hash global_variables. 123 * 124 * Variables set on the command line override Makefile contents, are 125 * passed to submakes (see Var_AddCmdLine), and are also exported to the 126 * environment. 127 * 128 * Without -e (!checkEnvFirst), make will see variables set in the 129 * Makefile, and default to the environment otherwise. 130 * 131 * With -e (checkEnvFirst), make will see the environment first, and that 132 * will override anything that's set in the Makefile (but not set on 133 * the command line). 134 * 135 * The SHELL variable is very special: it is never obtained from the 136 * environment, and never passed to the environment. 137 */ 138 139 /* definitions pertaining to dynamic variables */ 140 141 /* full names of dynamic variables */ 142 static char *varnames[] = { 143 TARGET, 144 PREFIX, 145 ARCHIVE, 146 MEMBER, 147 OODATE, 148 ALLSRC, 149 IMPSRC, 150 FTARGET, 151 DTARGET, 152 FPREFIX, 153 DPREFIX, 154 FARCHIVE, 155 DARCHIVE, 156 FMEMBER, 157 DMEMBER 158 }; 159 160 /* hashed names of dynamic variables */ 161 #include "varhashconsts.h" 162 163 /* extended indices for System V stuff */ 164 #define FTARGET_INDEX 7 165 #define DTARGET_INDEX 8 166 #define FPREFIX_INDEX 9 167 #define DPREFIX_INDEX 10 168 #define FARCHIVE_INDEX 11 169 #define DARCHIVE_INDEX 12 170 #define FMEMBER_INDEX 13 171 #define DMEMBER_INDEX 14 172 173 #define GLOBAL_INDEX -1 174 175 #define EXTENDED2SIMPLE(i) (((i)-LOCAL_SIZE)/2) 176 #define IS_EXTENDED_F(i) ((i)%2 == 1) 177 178 179 static struct ohash global_variables; 180 181 182 typedef struct Var_ { 183 BUFFER val; /* the variable value */ 184 unsigned int flags; /* miscellaneous status flags */ 185 #define VAR_IN_USE 1 /* Variable's value currently being used. */ 186 /* (Used to avoid recursion) */ 187 #define VAR_DUMMY 2 /* Variable is currently just a name */ 188 /* In particular: BUFFER is invalid */ 189 #define VAR_FROM_CMD 4 /* Special source: command line */ 190 #define VAR_FROM_ENV 8 /* Special source: environment */ 191 #define VAR_SEEN_ENV 16 /* No need to go look up environment again */ 192 #define VAR_SHELL 32 /* Magic behavior */ 193 194 #define POISONS (POISON_NORMAL | POISON_EMPTY | POISON_NOT_DEFINED) 195 /* Defined in var.h */ 196 char name[1]; /* the variable's name */ 197 } Var; 198 199 200 static struct ohash_info var_info = { 201 offsetof(Var, name), 202 NULL, 203 hash_alloc, hash_free, element_alloc 204 }; 205 206 static int classify_var(const char *, const char **, uint32_t *); 207 static Var *find_global_var(const char *, const char *, uint32_t); 208 static Var *find_global_var_without_env(const char *, const char *, uint32_t); 209 static void fill_from_env(Var *); 210 static Var *create_var(const char *, const char *); 211 static void var_set_initial_value(Var *, const char *); 212 static void var_set_value(Var *, const char *); 213 #define var_get_value(v) Buf_Retrieve(&((v)->val)) 214 static void var_append_value(Var *, const char *); 215 static void poison_check(Var *); 216 static void var_set_append(const char *, const char *, const char *, int, bool); 217 static void set_magic_shell_variable(void); 218 219 static void delete_var(Var *); 220 static void print_var(Var *); 221 222 223 static const char *find_rparen(const char *); 224 static const char *find_ket(const char *); 225 typedef const char * (*find_t)(const char *); 226 static find_t find_pos(int); 227 static void push_used(Var *); 228 static void pop_used(Var *); 229 static char *get_expanded_value(const char *, const char *, int, uint32_t, 230 SymTable *, bool, bool *); 231 static bool parse_base_variable_name(const char **, struct Name *, SymTable *); 232 233 234 235 /* Variable lookup function: return idx for dynamic variable, or 236 * GLOBAL_INDEX if name is not dynamic. Set up *pk for further use. 237 */ 238 static int 239 classify_var(const char *name, const char **enamePtr, uint32_t *pk) 240 { 241 size_t len; 242 243 *pk = ohash_interval(name, enamePtr); 244 len = *enamePtr - name; 245 /* substitute short version for long local name */ 246 switch (*pk % MAGICSLOTS1) { /* MAGICSLOTS should be the */ 247 case K_LONGALLSRC % MAGICSLOTS1:/* smallest constant yielding */ 248 /* distinct case values */ 249 if (*pk == K_LONGALLSRC && len == strlen(LONGALLSRC) && 250 strncmp(name, LONGALLSRC, len) == 0) 251 return ALLSRC_INDEX; 252 break; 253 case K_LONGARCHIVE % MAGICSLOTS1: 254 if (*pk == K_LONGARCHIVE && len == strlen(LONGARCHIVE) && 255 strncmp(name, LONGARCHIVE, len) == 0) 256 return ARCHIVE_INDEX; 257 break; 258 case K_LONGIMPSRC % MAGICSLOTS1: 259 if (*pk == K_LONGIMPSRC && len == strlen(LONGIMPSRC) && 260 strncmp(name, LONGIMPSRC, len) == 0) 261 return IMPSRC_INDEX; 262 break; 263 case K_LONGMEMBER % MAGICSLOTS1: 264 if (*pk == K_LONGMEMBER && len == strlen(LONGMEMBER) && 265 strncmp(name, LONGMEMBER, len) == 0) 266 return MEMBER_INDEX; 267 break; 268 case K_LONGOODATE % MAGICSLOTS1: 269 if (*pk == K_LONGOODATE && len == strlen(LONGOODATE) && 270 strncmp(name, LONGOODATE, len) == 0) 271 return OODATE_INDEX; 272 break; 273 case K_LONGPREFIX % MAGICSLOTS1: 274 if (*pk == K_LONGPREFIX && len == strlen(LONGPREFIX) && 275 strncmp(name, LONGPREFIX, len) == 0) 276 return PREFIX_INDEX; 277 break; 278 case K_LONGTARGET % MAGICSLOTS1: 279 if (*pk == K_LONGTARGET && len == strlen(LONGTARGET) && 280 strncmp(name, LONGTARGET, len) == 0) 281 return TARGET_INDEX; 282 break; 283 case K_TARGET % MAGICSLOTS1: 284 if (name[0] == TARGET[0] && len == 1) 285 return TARGET_INDEX; 286 break; 287 case K_OODATE % MAGICSLOTS1: 288 if (name[0] == OODATE[0] && len == 1) 289 return OODATE_INDEX; 290 break; 291 case K_ALLSRC % MAGICSLOTS1: 292 if (name[0] == ALLSRC[0] && len == 1) 293 return ALLSRC_INDEX; 294 break; 295 case K_IMPSRC % MAGICSLOTS1: 296 if (name[0] == IMPSRC[0] && len == 1) 297 return IMPSRC_INDEX; 298 break; 299 case K_PREFIX % MAGICSLOTS1: 300 if (name[0] == PREFIX[0] && len == 1) 301 return PREFIX_INDEX; 302 break; 303 case K_ARCHIVE % MAGICSLOTS1: 304 if (name[0] == ARCHIVE[0] && len == 1) 305 return ARCHIVE_INDEX; 306 break; 307 case K_MEMBER % MAGICSLOTS1: 308 if (name[0] == MEMBER[0] && len == 1) 309 return MEMBER_INDEX; 310 break; 311 case K_FTARGET % MAGICSLOTS1: 312 if (name[0] == FTARGET[0] && name[1] == FTARGET[1] && len == 2) 313 return FTARGET_INDEX; 314 break; 315 case K_DTARGET % MAGICSLOTS1: 316 if (name[0] == DTARGET[0] && name[1] == DTARGET[1] && len == 2) 317 return DTARGET_INDEX; 318 break; 319 case K_FPREFIX % MAGICSLOTS1: 320 if (name[0] == FPREFIX[0] && name[1] == FPREFIX[1] && len == 2) 321 return FPREFIX_INDEX; 322 break; 323 case K_DPREFIX % MAGICSLOTS1: 324 if (name[0] == DPREFIX[0] && name[1] == DPREFIX[1] && len == 2) 325 return DPREFIX_INDEX; 326 break; 327 case K_FARCHIVE % MAGICSLOTS1: 328 if (name[0] == FARCHIVE[0] && name[1] == FARCHIVE[1] && 329 len == 2) 330 return FARCHIVE_INDEX; 331 break; 332 case K_DARCHIVE % MAGICSLOTS1: 333 if (name[0] == DARCHIVE[0] && name[1] == DARCHIVE[1] && 334 len == 2) 335 return DARCHIVE_INDEX; 336 break; 337 case K_FMEMBER % MAGICSLOTS1: 338 if (name[0] == FMEMBER[0] && name[1] == FMEMBER[1] && len == 2) 339 return FMEMBER_INDEX; 340 break; 341 case K_DMEMBER % MAGICSLOTS1: 342 if (name[0] == DMEMBER[0] && name[1] == DMEMBER[1] && len == 2) 343 return DMEMBER_INDEX; 344 break; 345 default: 346 break; 347 } 348 return GLOBAL_INDEX; 349 } 350 351 352 /*** 353 *** Internal handling of variables. 354 ***/ 355 356 357 /* Create a new variable, does not initialize anything except the name. 358 * in particular, buffer is invalid, and flag value is invalid. Accordingly, 359 * must either: 360 * - set flags to VAR_DUMMY 361 * - set flags to !VAR_DUMMY, and initialize buffer, for instance with 362 * var_set_initial_value(). 363 */ 364 static Var * 365 create_var(const char *name, const char *ename) 366 { 367 return ohash_create_entry(&var_info, name, &ename); 368 } 369 370 /* Initial version of var_set_value(), to be called after create_var(). 371 */ 372 static void 373 var_set_initial_value(Var *v, const char *val) 374 { 375 size_t len; 376 377 len = strlen(val); 378 Buf_Init(&(v->val), len+1); 379 Buf_AddChars(&(v->val), len, val); 380 } 381 382 /* Normal version of var_set_value(), to be called after variable is fully 383 * initialized. 384 */ 385 static void 386 var_set_value(Var *v, const char *val) 387 { 388 if ((v->flags & VAR_DUMMY) == 0) { 389 Buf_Reset(&(v->val)); 390 Buf_AddString(&(v->val), val); 391 } else { 392 var_set_initial_value(v, val); 393 v->flags &= ~VAR_DUMMY; 394 } 395 } 396 397 /* Add to a variable, insert a separating space if the variable was already 398 * defined. 399 */ 400 static void 401 var_append_value(Var *v, const char *val) 402 { 403 if ((v->flags & VAR_DUMMY) == 0) { 404 Buf_AddSpace(&(v->val)); 405 Buf_AddString(&(v->val), val); 406 } else { 407 var_set_initial_value(v, val); 408 v->flags &= ~VAR_DUMMY; 409 } 410 } 411 412 413 /* Delete a variable and all the space associated with it. 414 */ 415 static void 416 delete_var(Var *v) 417 { 418 if ((v->flags & VAR_DUMMY) == 0) 419 Buf_Destroy(&(v->val)); 420 free(v); 421 } 422 423 424 425 426 /*** 427 *** Dynamic variable handling. 428 ***/ 429 430 431 432 /* create empty symtable. 433 * XXX: to save space, dynamic variables may be NULL pointers. 434 */ 435 void 436 SymTable_Init(SymTable *ctxt) 437 { 438 static SymTable sym_template; 439 memcpy(ctxt, &sym_template, sizeof(*ctxt)); 440 } 441 442 /* free symtable. 443 */ 444 #ifdef CLEANUP 445 void 446 SymTable_Destroy(SymTable *ctxt) 447 { 448 int i; 449 450 for (i = 0; i < LOCAL_SIZE; i++) 451 if (ctxt->locals[i] != NULL) 452 delete_var(ctxt->locals[i]); 453 } 454 #endif 455 456 /*** 457 *** Global variable handling. 458 ***/ 459 460 /* Create a new global var if necessary, and set it up correctly. 461 * Do not take environment into account. 462 */ 463 static Var * 464 find_global_var_without_env(const char *name, const char *ename, uint32_t k) 465 { 466 unsigned int slot; 467 Var *v; 468 469 slot = ohash_lookup_interval(&global_variables, name, ename, k); 470 v = ohash_find(&global_variables, slot); 471 if (v == NULL) { 472 v = create_var(name, ename); 473 v->flags = VAR_DUMMY; 474 ohash_insert(&global_variables, slot, v); 475 } 476 return v; 477 } 478 479 /* Helper for find_global_var(): grab environment value if needed. 480 */ 481 static void 482 fill_from_env(Var *v) 483 { 484 char *env; 485 486 env = getenv(v->name); 487 if (env == NULL) 488 v->flags |= VAR_SEEN_ENV; 489 else { 490 var_set_value(v, env); 491 v->flags |= VAR_FROM_ENV | VAR_SEEN_ENV; 492 } 493 494 #ifdef STATS_VAR_LOOKUP 495 STAT_VAR_FROM_ENV++; 496 #endif 497 } 498 499 /* Find global var, and obtain its value from the environment if needed. 500 */ 501 static Var * 502 find_global_var(const char *name, const char *ename, uint32_t k) 503 { 504 Var *v; 505 506 v = find_global_var_without_env(name, ename, k); 507 508 if ((v->flags & VAR_SEEN_ENV) == 0) 509 if ((checkEnvFirst && (v->flags & VAR_FROM_CMD) == 0) || 510 (v->flags & VAR_DUMMY) != 0) 511 fill_from_env(v); 512 513 return v; 514 } 515 516 /* mark variable as poisoned, in a given setup. 517 */ 518 void 519 Var_MarkPoisoned(const char *name, const char *ename, unsigned int type) 520 { 521 Var *v; 522 uint32_t k; 523 int idx; 524 idx = classify_var(name, &ename, &k); 525 526 if (idx != GLOBAL_INDEX) { 527 Parse_Error(PARSE_FATAL, 528 "Trying to poison dynamic variable $%s", 529 varnames[idx]); 530 return; 531 } 532 533 v = find_global_var(name, ename, k); 534 v->flags |= type; 535 /* POISON_NORMAL is not lazy: if the variable already exists in 536 * the Makefile, then it's a mistake. 537 */ 538 if (v->flags & POISON_NORMAL) { 539 if (v->flags & VAR_DUMMY) 540 return; 541 if (v->flags & VAR_FROM_ENV) 542 return; 543 Parse_Error(PARSE_FATAL, 544 "Poisoned variable %s is already set\n", v->name); 545 } 546 } 547 548 /* Check if there's any reason not to use the variable in this context. 549 */ 550 static void 551 poison_check(Var *v) 552 { 553 if (v->flags & POISON_NORMAL) { 554 Parse_Error(PARSE_FATAL, 555 "Poisoned variable %s has been referenced\n", v->name); 556 return; 557 } 558 if (v->flags & VAR_DUMMY) { 559 Parse_Error(PARSE_FATAL, 560 "Poisoned variable %s is not defined\n", v->name); 561 return; 562 } 563 if (v->flags & POISON_EMPTY) 564 if (strcmp(var_get_value(v), "") == 0) 565 Parse_Error(PARSE_FATAL, 566 "Poisoned variable %s is empty\n", v->name); 567 } 568 569 /* Delete global variable. 570 */ 571 void 572 Var_Deletei(const char *name, const char *ename) 573 { 574 Var *v; 575 uint32_t k; 576 unsigned int slot; 577 int idx; 578 579 idx = classify_var(name, &ename, &k); 580 if (idx != GLOBAL_INDEX) { 581 Parse_Error(PARSE_FATAL, 582 "Trying to delete dynamic variable $%s", varnames[idx]); 583 return; 584 } 585 slot = ohash_lookup_interval(&global_variables, name, ename, k); 586 v = ohash_find(&global_variables, slot); 587 588 if (v == NULL) 589 return; 590 591 if (checkEnvFirst && (v->flags & VAR_FROM_ENV)) 592 return; 593 594 if (v->flags & VAR_FROM_CMD) 595 return; 596 597 ohash_remove(&global_variables, slot); 598 delete_var(v); 599 } 600 601 /* Set or add a global variable, in VAR_CMD or VAR_GLOBAL context. 602 */ 603 static void 604 var_set_append(const char *name, const char *ename, const char *val, int ctxt, 605 bool append) 606 { 607 Var *v; 608 uint32_t k; 609 int idx; 610 611 idx = classify_var(name, &ename, &k); 612 if (idx != GLOBAL_INDEX) { 613 Parse_Error(PARSE_FATAL, "Trying to %s dynamic variable $%s", 614 append ? "append to" : "set", varnames[idx]); 615 return; 616 } 617 618 v = find_global_var(name, ename, k); 619 if (v->flags & POISON_NORMAL) 620 Parse_Error(PARSE_FATAL, "Trying to %s poisoned variable %s\n", 621 append ? "append to" : "set", v->name); 622 /* so can we write to it ? */ 623 if (ctxt == VAR_CMD) { /* always for command line */ 624 (append ? var_append_value : var_set_value)(v, val); 625 v->flags |= VAR_FROM_CMD; 626 if ((v->flags & VAR_SHELL) == 0) { 627 /* Any variables given on the command line are 628 * automatically exported to the environment, 629 * except for SHELL (as per POSIX standard). 630 */ 631 esetenv(v->name, val); 632 } 633 if (DEBUG(VAR)) 634 printf("command:%s = %s\n", v->name, var_get_value(v)); 635 } else if ((v->flags & VAR_FROM_CMD) == 0 && 636 (!checkEnvFirst || (v->flags & VAR_FROM_ENV) == 0)) { 637 (append ? var_append_value : var_set_value)(v, val); 638 if (DEBUG(VAR)) 639 printf("global:%s = %s\n", v->name, var_get_value(v)); 640 } else if (DEBUG(VAR)) 641 printf("overridden:%s = %s\n", v->name, var_get_value(v)); 642 } 643 644 void 645 Var_Seti_with_ctxt(const char *name, const char *ename, const char *val, 646 int ctxt) 647 { 648 var_set_append(name, ename, val, ctxt, false); 649 } 650 651 void 652 Var_Appendi_with_ctxt(const char *name, const char *ename, const char *val, 653 int ctxt) 654 { 655 var_set_append(name, ename, val, ctxt, true); 656 } 657 658 /* XXX different semantics for Var_Valuei() and Var_Definedi(): 659 * references to poisoned value variables will error out in Var_Valuei(), 660 * but not in Var_Definedi(), so the following construct works: 661 * .poison BINDIR 662 * BINDIR ?= /usr/bin 663 */ 664 char * 665 Var_Valuei(const char *name, const char *ename) 666 { 667 Var *v; 668 uint32_t k; 669 int idx; 670 671 idx = classify_var(name, &ename, &k); 672 if (idx != GLOBAL_INDEX) { 673 Parse_Error(PARSE_FATAL, 674 "Trying to get value of dynamic variable $%s", 675 varnames[idx]); 676 return NULL; 677 } 678 v = find_global_var(name, ename, k); 679 if (v->flags & POISONS) 680 poison_check(v); 681 if ((v->flags & VAR_DUMMY) == 0) 682 return var_get_value(v); 683 else 684 return NULL; 685 } 686 687 bool 688 Var_Definedi(const char *name, const char *ename) 689 { 690 Var *v; 691 uint32_t k; 692 int idx; 693 694 idx = classify_var(name, &ename, &k); 695 /* We don't bother writing an error message for dynamic variables, 696 * these will be caught when getting set later, usually. 697 */ 698 if (idx == GLOBAL_INDEX) { 699 v = find_global_var(name, ename, k); 700 if (v->flags & POISON_NORMAL) 701 poison_check(v); 702 if ((v->flags & VAR_DUMMY) == 0) 703 return true; 704 } 705 return false; 706 } 707 708 709 /*** 710 *** Substitution functions, handling both global and dynamic variables. 711 ***/ 712 713 714 /* All the scanning functions needed to account for all the forms of 715 * variable names that exist: 716 * $A, ${AB}, $(ABC), ${A:mod}, $(A:mod) 717 */ 718 719 static const char * 720 find_rparen(const char *p) 721 { 722 while (*p != '$' && *p != '\0' && *p != ')' && *p != ':') 723 p++; 724 return p; 725 } 726 727 static const char * 728 find_ket(const char *p) 729 { 730 while (*p != '$' && *p != '\0' && *p != '}' && *p != ':') 731 p++; 732 return p; 733 } 734 735 /* Figure out what kind of name we're looking for from a start character. 736 */ 737 static find_t 738 find_pos(int c) 739 { 740 switch(c) { 741 case '(': 742 return find_rparen; 743 case '{': 744 return find_ket; 745 default: 746 Parse_Error(PARSE_FATAL, 747 "Wrong character in variable spec %c (can't happen)"); 748 return find_rparen; 749 } 750 } 751 752 static bool 753 parse_base_variable_name(const char **pstr, struct Name *name, SymTable *ctxt) 754 { 755 const char *str = *pstr; 756 const char *tstr; 757 bool has_modifier = false; 758 759 switch(str[1]) { 760 case '(': 761 case '{': 762 /* Find eventual modifiers in the variable */ 763 tstr = VarName_Get(str+2, name, ctxt, false, find_pos(str[1])); 764 if (*tstr == ':') 765 has_modifier = true; 766 else if (*tstr != '\0') { 767 tstr++; 768 } 769 break; 770 default: 771 name->s = str+1; 772 name->e = str+2; 773 name->tofree = false; 774 tstr = str + 2; 775 break; 776 } 777 *pstr = tstr; 778 return has_modifier; 779 } 780 781 bool 782 Var_ParseSkip(const char **pstr, SymTable *ctxt) 783 { 784 const char *str = *pstr; 785 struct Name name; 786 bool result; 787 bool has_modifier; 788 const char *tstr = str; 789 790 has_modifier = parse_base_variable_name(&tstr, &name, ctxt); 791 VarName_Free(&name); 792 result = true; 793 if (has_modifier) 794 if (VarModifiers_Apply(NULL, NULL, ctxt, true, NULL, &tstr, 795 str[1]) == var_Error) 796 result = false; 797 *pstr = tstr; 798 return result; 799 } 800 801 /* As of now, Var_ParseBuffer is just a wrapper around Var_Parse. For 802 * speed, it may be better to revisit the implementation to do things 803 * directly. */ 804 bool 805 Var_ParseBuffer(Buffer buf, const char *str, SymTable *ctxt, bool err, 806 size_t *lengthPtr) 807 { 808 char *result; 809 bool freeIt; 810 811 result = Var_Parse(str, ctxt, err, lengthPtr, &freeIt); 812 if (result == var_Error) 813 return false; 814 815 Buf_AddString(buf, result); 816 if (freeIt) 817 free(result); 818 return true; 819 } 820 821 /* Helper function for Var_Parse: still recursive, but we tag what variables 822 * we expand for better error messages. 823 */ 824 #define MAX_DEPTH 350 825 static Var *call_trace[MAX_DEPTH]; 826 static int current_depth = 0; 827 828 static void 829 push_used(Var *v) 830 { 831 if (v->flags & VAR_IN_USE) { 832 int i; 833 fprintf(stderr, "Problem with variable expansion chain: "); 834 for (i = 0; 835 i < (current_depth > MAX_DEPTH ? MAX_DEPTH : current_depth); 836 i++) 837 fprintf(stderr, "%s -> ", call_trace[i]->name); 838 fprintf(stderr, "%s\n", v->name); 839 Fatal("\tVariable %s is recursive.", v->name); 840 /*NOTREACHED*/ 841 } 842 843 v->flags |= VAR_IN_USE; 844 if (current_depth < MAX_DEPTH) 845 call_trace[current_depth] = v; 846 current_depth++; 847 } 848 849 static void 850 pop_used(Var *v) 851 { 852 v->flags &= ~VAR_IN_USE; 853 current_depth--; 854 } 855 856 static char * 857 get_expanded_value(const char *name, const char *ename, int idx, uint32_t k, 858 SymTable *ctxt, bool err, bool *freePtr) 859 { 860 char *val; 861 862 /* Before doing any modification, we have to make sure the 863 * value has been fully expanded. If it looks like recursion 864 * might be necessary (there's a dollar sign somewhere in 865 * the variable's value) we just call Var_Subst to do any 866 * other substitutions that are necessary. Note that the 867 * value returned by Var_Subst will have been dynamically 868 * allocated, so it will need freeing when we return. 869 */ 870 if (idx == GLOBAL_INDEX) { 871 Var *v = find_global_var(name, ename, k); 872 873 if (v == NULL) 874 return NULL; 875 876 if ((v->flags & POISONS) != 0) 877 poison_check(v); 878 if ((v->flags & VAR_DUMMY) != 0) 879 return NULL; 880 881 val = var_get_value(v); 882 if (strchr(val, '$') != NULL) { 883 push_used(v); 884 val = Var_Subst(val, ctxt, err); 885 pop_used(v); 886 *freePtr = true; 887 } 888 } else { 889 if (ctxt != NULL) { 890 if (idx < LOCAL_SIZE) 891 val = ctxt->locals[idx]; 892 else 893 val = ctxt->locals[EXTENDED2SIMPLE(idx)]; 894 } else 895 val = NULL; 896 if (val == NULL) 897 return NULL; 898 899 if (idx >= LOCAL_SIZE) { 900 if (IS_EXTENDED_F(idx)) 901 val = Var_GetTail(val); 902 else 903 val = Var_GetHead(val); 904 *freePtr = true; 905 } 906 } 907 return val; 908 } 909 910 char * 911 Var_Parse(const char *str, /* The string to parse */ 912 SymTable *ctxt, /* The context for the variable */ 913 bool err, /* true if undefined variables are an error */ 914 size_t *lengthPtr, /* OUT: The length of the specification */ 915 bool *freePtr) /* OUT: true if caller should free result */ 916 { 917 const char *tstr; 918 struct Name name; 919 char *val; 920 uint32_t k; 921 int idx; 922 bool has_modifier; 923 924 *freePtr = false; 925 926 tstr = str; 927 928 has_modifier = parse_base_variable_name(&tstr, &name, ctxt); 929 930 idx = classify_var(name.s, &name.e, &k); 931 val = get_expanded_value(name.s, name.e, idx, k, ctxt, err, freePtr); 932 if (has_modifier) { 933 val = VarModifiers_Apply(val, &name, ctxt, err, freePtr, 934 &tstr, str[1]); 935 } 936 if (val == NULL) { 937 val = err ? var_Error : varNoError; 938 /* Dynamic source */ 939 if (idx != GLOBAL_INDEX) { 940 /* can't be expanded for now: copy the spec instead. */ 941 if (ctxt == NULL) { 942 *freePtr = true; 943 val = Str_dupi(str, tstr); 944 } else { 945 /* somehow, this should have been expanded already. */ 946 GNode *n; 947 948 /* XXX */ 949 n = (GNode *)(((char *)ctxt) - 950 offsetof(GNode, context)); 951 if (idx >= LOCAL_SIZE) 952 idx = EXTENDED2SIMPLE(idx); 953 switch(idx) { 954 case IMPSRC_INDEX: 955 Fatal( 956 "Using $< in a non-suffix rule context is a GNUmake idiom (line %lu of %s)", 957 n->lineno, n->fname); 958 break; 959 default: 960 Error( 961 "Using undefined dynamic variable $%s (line %lu of %s)", 962 varnames[idx], n->lineno, n->fname); 963 break; 964 } 965 } 966 } 967 } 968 VarName_Free(&name); 969 *lengthPtr = tstr - str; 970 return val; 971 } 972 973 974 char * 975 Var_Subst(const char *str, /* the string in which to substitute */ 976 SymTable *ctxt, /* the context wherein to find variables */ 977 bool undefErr) /* true if undefineds are an error */ 978 { 979 BUFFER buf; /* Buffer for forming things */ 980 static bool errorReported; 981 982 Buf_Init(&buf, MAKE_BSIZE); 983 errorReported = false; 984 985 for (;;) { 986 char *val; /* Value to substitute for a variable */ 987 size_t length; /* Length of the variable invocation */ 988 bool doFree; /* Set true if val should be freed */ 989 const char *cp; 990 991 /* copy uninteresting stuff */ 992 for (cp = str; *str != '\0' && *str != '$'; str++) 993 ; 994 Buf_Addi(&buf, cp, str); 995 if (*str == '\0') 996 break; 997 if (str[1] == '$') { 998 /* A $ may be escaped with another $. */ 999 Buf_AddChar(&buf, '$'); 1000 str += 2; 1001 continue; 1002 } 1003 val = Var_Parse(str, ctxt, undefErr, &length, &doFree); 1004 /* When we come down here, val should either point to the 1005 * value of this variable, suitably modified, or be NULL. 1006 * Length should be the total length of the potential 1007 * variable invocation (from $ to end character...) */ 1008 if (val == var_Error || val == varNoError) { 1009 /* If errors are not an issue, skip over the variable 1010 * and continue with the substitution. Otherwise, store 1011 * the dollar sign and advance str so we continue with 1012 * the string... */ 1013 if (errorIsOkay) 1014 str += length; 1015 else if (undefErr) { 1016 /* If variable is undefined, complain and 1017 * skip the variable name. The complaint 1018 * will stop us from doing anything when 1019 * the file is parsed. */ 1020 if (!errorReported) 1021 Parse_Error(PARSE_FATAL, 1022 "Undefined variable \"%.*s\"", 1023 length, str); 1024 str += length; 1025 errorReported = true; 1026 } else { 1027 Buf_AddChar(&buf, *str); 1028 str++; 1029 } 1030 } else { 1031 /* We've now got a variable structure to store in. 1032 * But first, advance the string pointer. */ 1033 str += length; 1034 1035 /* Copy all the characters from the variable value 1036 * straight into the new string. */ 1037 Buf_AddString(&buf, val); 1038 if (doFree) 1039 free(val); 1040 } 1041 } 1042 return Buf_Retrieve(&buf); 1043 } 1044 1045 static BUFFER subst_buffer; 1046 1047 /* we would like to subst on intervals, but it's complicated, so we cheat 1048 * by storing the interval in a static buffer. 1049 */ 1050 char * 1051 Var_Substi(const char *str, const char *estr, SymTable *ctxt, bool undefErr) 1052 { 1053 /* delimited string: no need to copy */ 1054 if (estr == NULL || *estr == '\0') 1055 return Var_Subst(str, ctxt, undefErr); 1056 1057 Buf_Reset(&subst_buffer); 1058 Buf_Addi(&subst_buffer, str, estr); 1059 return Var_Subst(Buf_Retrieve(&subst_buffer), ctxt, undefErr); 1060 } 1061 1062 /*** 1063 *** Supplementary support for .for loops. 1064 ***/ 1065 1066 1067 1068 struct LoopVar 1069 { 1070 Var old; /* keep old variable value (before the loop) */ 1071 Var *me; /* the variable we're dealing with */ 1072 }; 1073 1074 1075 struct LoopVar * 1076 Var_NewLoopVar(const char *name, const char *ename) 1077 { 1078 struct LoopVar *l; 1079 uint32_t k; 1080 1081 l = emalloc(sizeof(struct LoopVar)); 1082 1083 /* we obtain a new variable quickly, make a snapshot of its old 1084 * value, and make sure the environment cannot touch us. 1085 */ 1086 /* XXX: should we avoid dynamic variables ? */ 1087 k = ohash_interval(name, &ename); 1088 1089 l->me = find_global_var_without_env(name, ename, k); 1090 l->old = *(l->me); 1091 l->me->flags = VAR_SEEN_ENV | VAR_DUMMY; 1092 return l; 1093 } 1094 1095 char * 1096 Var_LoopVarName(struct LoopVar *v) 1097 { 1098 return v->me->name; 1099 } 1100 1101 void 1102 Var_DeleteLoopVar(struct LoopVar *l) 1103 { 1104 if ((l->me->flags & VAR_DUMMY) == 0) 1105 Buf_Destroy(&(l->me->val)); 1106 *(l->me) = l->old; 1107 free(l); 1108 } 1109 1110 void 1111 Var_SubstVar(Buffer buf, /* To store result */ 1112 const char *str, /* The string in which to substitute */ 1113 struct LoopVar *l, /* Handle */ 1114 const char *val) /* Its value */ 1115 { 1116 const char *var = l->me->name; 1117 1118 var_set_value(l->me, val); 1119 1120 for (;;) { 1121 const char *start; 1122 /* Copy uninteresting stuff */ 1123 for (start = str; *str != '\0' && *str != '$'; str++) 1124 ; 1125 Buf_Addi(buf, start, str); 1126 1127 start = str; 1128 if (*str++ == '\0') 1129 break; 1130 str++; 1131 /* and escaped dollars */ 1132 if (start[1] == '$') { 1133 Buf_Addi(buf, start, start+2); 1134 continue; 1135 } 1136 /* Simple variable, if it's not us, copy. */ 1137 if (start[1] != '(' && start[1] != '{') { 1138 if (start[1] != *var || var[1] != '\0') { 1139 Buf_AddChars(buf, 2, start); 1140 continue; 1141 } 1142 } else { 1143 const char *p; 1144 char paren = start[1]; 1145 1146 1147 /* Find the end of the variable specification. */ 1148 p = find_pos(paren)(str); 1149 /* A variable inside the variable. We don't know how to 1150 * expand the external variable at this point, so we 1151 * try again with the nested variable. */ 1152 if (*p == '$') { 1153 Buf_Addi(buf, start, p); 1154 str = p; 1155 continue; 1156 } 1157 1158 if (strncmp(var, str, p - str) != 0 || 1159 var[p - str] != '\0') { 1160 /* Not the variable we want to expand. */ 1161 Buf_Addi(buf, start, p); 1162 str = p; 1163 continue; 1164 } 1165 if (*p == ':') { 1166 bool doFree; /* should val be freed ? */ 1167 char *newval; 1168 struct Name name; 1169 1170 doFree = false; 1171 name.s = var; 1172 name.e = var + (p-str); 1173 1174 /* val won't be freed since !doFree, but 1175 * VarModifiers_Apply doesn't know that, 1176 * hence the cast. */ 1177 newval = VarModifiers_Apply((char *)val, 1178 &name, NULL, false, &doFree, &p, paren); 1179 Buf_AddString(buf, newval); 1180 if (doFree) 1181 free(newval); 1182 str = p; 1183 continue; 1184 } else 1185 str = p+1; 1186 } 1187 Buf_AddString(buf, val); 1188 } 1189 } 1190 1191 /*** 1192 *** Odds and ends 1193 ***/ 1194 1195 static void 1196 set_magic_shell_variable() 1197 { 1198 const char *name = "SHELL"; 1199 const char *ename = NULL; 1200 uint32_t k; 1201 Var *v; 1202 1203 k = ohash_interval(name, &ename); 1204 v = find_global_var_without_env(name, ename, k); 1205 var_set_value(v, _PATH_BSHELL); 1206 /* XXX the environment shall never affect it */ 1207 v->flags = VAR_SHELL | VAR_SEEN_ENV; 1208 } 1209 1210 /* 1211 * Var_Init 1212 * Initialize the module 1213 */ 1214 void 1215 Var_Init(void) 1216 { 1217 ohash_init(&global_variables, 10, &var_info); 1218 set_magic_shell_variable(); 1219 1220 1221 errorIsOkay = true; 1222 Var_setCheckEnvFirst(false); 1223 1224 VarModifiers_Init(); 1225 Buf_Init(&subst_buffer, MAKE_BSIZE); 1226 } 1227 1228 1229 #ifdef CLEANUP 1230 void 1231 Var_End(void) 1232 { 1233 Var *v; 1234 unsigned int i; 1235 1236 for (v = ohash_first(&global_variables, &i); v != NULL; 1237 v = ohash_next(&global_variables, &i)) 1238 delete_var(v); 1239 } 1240 #endif 1241 1242 static const char *interpret(int); 1243 1244 static const char * 1245 interpret(int f) 1246 { 1247 if (f & VAR_DUMMY) 1248 return "(D)"; 1249 return ""; 1250 } 1251 1252 1253 static void 1254 print_var(Var *v) 1255 { 1256 printf("%-16s%s = %s\n", v->name, interpret(v->flags), 1257 (v->flags & VAR_DUMMY) == 0 ? var_get_value(v) : "(none)"); 1258 } 1259 1260 void 1261 Var_Dump(void) 1262 { 1263 Var *v; 1264 unsigned int i; 1265 1266 printf("#*** Global Variables:\n"); 1267 1268 for (v = ohash_first(&global_variables, &i); v != NULL; 1269 v = ohash_next(&global_variables, &i)) 1270 print_var(v); 1271 } 1272 1273 static const char *quotable = " \t\n\\'\""; 1274 1275 /* POSIX says that variable assignments passed on the command line should be 1276 * propagated to sub makes through MAKEFLAGS. 1277 */ 1278 void 1279 Var_AddCmdline(const char *name) 1280 { 1281 Var *v; 1282 unsigned int i; 1283 BUFFER buf; 1284 char *s; 1285 1286 Buf_Init(&buf, MAKE_BSIZE); 1287 1288 for (v = ohash_first(&global_variables, &i); v != NULL; 1289 v = ohash_next(&global_variables, &i)) { 1290 /* This is not as expensive as it looks: this function is 1291 * called before parsing Makefiles, so there are just a 1292 * few non cmdling variables in there. 1293 */ 1294 if (!(v->flags & VAR_FROM_CMD)) { 1295 continue; 1296 } 1297 /* We assume variable names don't need quoting */ 1298 Buf_AddString(&buf, v->name); 1299 Buf_AddChar(&buf, '='); 1300 for (s = var_get_value(v); *s != '\0'; s++) { 1301 if (strchr(quotable, *s)) 1302 Buf_AddChar(&buf, '\\'); 1303 Buf_AddChar(&buf, *s); 1304 } 1305 Buf_AddSpace(&buf); 1306 } 1307 Var_Append(name, Buf_Retrieve(&buf)); 1308 Buf_Destroy(&buf); 1309 } 1310