1 /* $OpenBSD: var.c,v 1.88 2011/06/20 19:05:33 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 bool freePtr = false; 795 char *s = VarModifiers_Apply(NULL, NULL, ctxt, true, &freePtr, 796 &tstr, str[1]); 797 if (s == var_Error) 798 result = false; 799 if (freePtr) 800 free(s); 801 } 802 *pstr = tstr; 803 return result; 804 } 805 806 /* As of now, Var_ParseBuffer is just a wrapper around Var_Parse. For 807 * speed, it may be better to revisit the implementation to do things 808 * directly. */ 809 bool 810 Var_ParseBuffer(Buffer buf, const char *str, SymTable *ctxt, bool err, 811 size_t *lengthPtr) 812 { 813 char *result; 814 bool freeIt; 815 816 result = Var_Parse(str, ctxt, err, lengthPtr, &freeIt); 817 if (result == var_Error) 818 return false; 819 820 Buf_AddString(buf, result); 821 if (freeIt) 822 free(result); 823 return true; 824 } 825 826 /* Helper function for Var_Parse: still recursive, but we tag what variables 827 * we expand for better error messages. 828 */ 829 #define MAX_DEPTH 350 830 static Var *call_trace[MAX_DEPTH]; 831 static int current_depth = 0; 832 833 static void 834 push_used(Var *v) 835 { 836 if (v->flags & VAR_IN_USE) { 837 int i; 838 fprintf(stderr, "Problem with variable expansion chain: "); 839 for (i = 0; 840 i < (current_depth > MAX_DEPTH ? MAX_DEPTH : current_depth); 841 i++) 842 fprintf(stderr, "%s -> ", call_trace[i]->name); 843 fprintf(stderr, "%s\n", v->name); 844 Fatal("\tVariable %s is recursive.", v->name); 845 /*NOTREACHED*/ 846 } 847 848 v->flags |= VAR_IN_USE; 849 if (current_depth < MAX_DEPTH) 850 call_trace[current_depth] = v; 851 current_depth++; 852 } 853 854 static void 855 pop_used(Var *v) 856 { 857 v->flags &= ~VAR_IN_USE; 858 current_depth--; 859 } 860 861 static char * 862 get_expanded_value(const char *name, const char *ename, int idx, uint32_t k, 863 SymTable *ctxt, bool err, bool *freePtr) 864 { 865 char *val; 866 867 /* Before doing any modification, we have to make sure the 868 * value has been fully expanded. If it looks like recursion 869 * might be necessary (there's a dollar sign somewhere in 870 * the variable's value) we just call Var_Subst to do any 871 * other substitutions that are necessary. Note that the 872 * value returned by Var_Subst will have been dynamically 873 * allocated, so it will need freeing when we return. 874 */ 875 if (idx == GLOBAL_INDEX) { 876 Var *v = find_global_var(name, ename, k); 877 878 if (v == NULL) 879 return NULL; 880 881 if ((v->flags & POISONS) != 0) 882 poison_check(v); 883 if ((v->flags & VAR_DUMMY) != 0) 884 return NULL; 885 886 val = var_get_value(v); 887 if (strchr(val, '$') != NULL) { 888 push_used(v); 889 val = Var_Subst(val, ctxt, err); 890 pop_used(v); 891 *freePtr = true; 892 } 893 } else { 894 if (ctxt != NULL) { 895 if (idx < LOCAL_SIZE) 896 val = ctxt->locals[idx]; 897 else 898 val = ctxt->locals[EXTENDED2SIMPLE(idx)]; 899 } else 900 val = NULL; 901 if (val == NULL) 902 return NULL; 903 904 if (idx >= LOCAL_SIZE) { 905 if (IS_EXTENDED_F(idx)) 906 val = Var_GetTail(val); 907 else 908 val = Var_GetHead(val); 909 *freePtr = true; 910 } 911 } 912 return val; 913 } 914 915 char * 916 Var_Parse(const char *str, /* The string to parse */ 917 SymTable *ctxt, /* The context for the variable */ 918 bool err, /* true if undefined variables are an error */ 919 size_t *lengthPtr, /* OUT: The length of the specification */ 920 bool *freePtr) /* OUT: true if caller should free result */ 921 { 922 const char *tstr; 923 struct Name name; 924 char *val; 925 uint32_t k; 926 int idx; 927 bool has_modifier; 928 929 *freePtr = false; 930 931 tstr = str; 932 933 has_modifier = parse_base_variable_name(&tstr, &name, ctxt); 934 935 idx = classify_var(name.s, &name.e, &k); 936 val = get_expanded_value(name.s, name.e, idx, k, ctxt, err, freePtr); 937 if (has_modifier) { 938 val = VarModifiers_Apply(val, &name, ctxt, err, freePtr, 939 &tstr, str[1]); 940 } 941 if (val == NULL) { 942 val = err ? var_Error : varNoError; 943 /* Dynamic source */ 944 if (idx != GLOBAL_INDEX) { 945 /* can't be expanded for now: copy the spec instead. */ 946 if (ctxt == NULL) { 947 *freePtr = true; 948 val = Str_dupi(str, tstr); 949 } else { 950 /* somehow, this should have been expanded already. */ 951 GNode *n; 952 953 /* XXX */ 954 n = (GNode *)(((char *)ctxt) - 955 offsetof(GNode, context)); 956 if (idx >= LOCAL_SIZE) 957 idx = EXTENDED2SIMPLE(idx); 958 switch(idx) { 959 case IMPSRC_INDEX: 960 Fatal( 961 "Using $< in a non-suffix rule context is a GNUmake idiom (line %lu of %s)", 962 n->lineno, n->fname); 963 break; 964 default: 965 Error( 966 "Using undefined dynamic variable $%s (line %lu of %s)", 967 varnames[idx], n->lineno, n->fname); 968 break; 969 } 970 } 971 } 972 } 973 VarName_Free(&name); 974 *lengthPtr = tstr - str; 975 return val; 976 } 977 978 979 char * 980 Var_Subst(const char *str, /* the string in which to substitute */ 981 SymTable *ctxt, /* the context wherein to find variables */ 982 bool undefErr) /* true if undefineds are an error */ 983 { 984 BUFFER buf; /* Buffer for forming things */ 985 static bool errorReported; 986 987 Buf_Init(&buf, MAKE_BSIZE); 988 errorReported = false; 989 990 for (;;) { 991 char *val; /* Value to substitute for a variable */ 992 size_t length; /* Length of the variable invocation */ 993 bool doFree; /* Set true if val should be freed */ 994 const char *cp; 995 996 /* copy uninteresting stuff */ 997 for (cp = str; *str != '\0' && *str != '$'; str++) 998 ; 999 Buf_Addi(&buf, cp, str); 1000 if (*str == '\0') 1001 break; 1002 if (str[1] == '$') { 1003 /* A $ may be escaped with another $. */ 1004 Buf_AddChar(&buf, '$'); 1005 str += 2; 1006 continue; 1007 } 1008 val = Var_Parse(str, ctxt, undefErr, &length, &doFree); 1009 /* When we come down here, val should either point to the 1010 * value of this variable, suitably modified, or be NULL. 1011 * Length should be the total length of the potential 1012 * variable invocation (from $ to end character...) */ 1013 if (val == var_Error || val == varNoError) { 1014 /* If errors are not an issue, skip over the variable 1015 * and continue with the substitution. Otherwise, store 1016 * the dollar sign and advance str so we continue with 1017 * the string... */ 1018 if (errorIsOkay) 1019 str += length; 1020 else if (undefErr) { 1021 /* If variable is undefined, complain and 1022 * skip the variable name. The complaint 1023 * will stop us from doing anything when 1024 * the file is parsed. */ 1025 if (!errorReported) 1026 Parse_Error(PARSE_FATAL, 1027 "Undefined variable \"%.*s\"", 1028 length, str); 1029 str += length; 1030 errorReported = true; 1031 } else { 1032 Buf_AddChar(&buf, *str); 1033 str++; 1034 } 1035 } else { 1036 /* We've now got a variable structure to store in. 1037 * But first, advance the string pointer. */ 1038 str += length; 1039 1040 /* Copy all the characters from the variable value 1041 * straight into the new string. */ 1042 Buf_AddString(&buf, val); 1043 if (doFree) 1044 free(val); 1045 } 1046 } 1047 return Buf_Retrieve(&buf); 1048 } 1049 1050 static BUFFER subst_buffer; 1051 1052 /* we would like to subst on intervals, but it's complicated, so we cheat 1053 * by storing the interval in a static buffer. 1054 */ 1055 char * 1056 Var_Substi(const char *str, const char *estr, SymTable *ctxt, bool undefErr) 1057 { 1058 /* delimited string: no need to copy */ 1059 if (estr == NULL || *estr == '\0') 1060 return Var_Subst(str, ctxt, undefErr); 1061 1062 Buf_Reset(&subst_buffer); 1063 Buf_Addi(&subst_buffer, str, estr); 1064 return Var_Subst(Buf_Retrieve(&subst_buffer), ctxt, undefErr); 1065 } 1066 1067 /*** 1068 *** Supplementary support for .for loops. 1069 ***/ 1070 1071 1072 1073 struct LoopVar 1074 { 1075 Var old; /* keep old variable value (before the loop) */ 1076 Var *me; /* the variable we're dealing with */ 1077 }; 1078 1079 1080 struct LoopVar * 1081 Var_NewLoopVar(const char *name, const char *ename) 1082 { 1083 struct LoopVar *l; 1084 uint32_t k; 1085 1086 l = emalloc(sizeof(struct LoopVar)); 1087 1088 /* we obtain a new variable quickly, make a snapshot of its old 1089 * value, and make sure the environment cannot touch us. 1090 */ 1091 /* XXX: should we avoid dynamic variables ? */ 1092 k = ohash_interval(name, &ename); 1093 1094 l->me = find_global_var_without_env(name, ename, k); 1095 l->old = *(l->me); 1096 l->me->flags = VAR_SEEN_ENV | VAR_DUMMY; 1097 return l; 1098 } 1099 1100 char * 1101 Var_LoopVarName(struct LoopVar *v) 1102 { 1103 return v->me->name; 1104 } 1105 1106 void 1107 Var_DeleteLoopVar(struct LoopVar *l) 1108 { 1109 if ((l->me->flags & VAR_DUMMY) == 0) 1110 Buf_Destroy(&(l->me->val)); 1111 *(l->me) = l->old; 1112 free(l); 1113 } 1114 1115 void 1116 Var_SubstVar(Buffer buf, /* To store result */ 1117 const char *str, /* The string in which to substitute */ 1118 struct LoopVar *l, /* Handle */ 1119 const char *val) /* Its value */ 1120 { 1121 const char *var = l->me->name; 1122 1123 var_set_value(l->me, val); 1124 1125 for (;;) { 1126 const char *start; 1127 /* Copy uninteresting stuff */ 1128 for (start = str; *str != '\0' && *str != '$'; str++) 1129 ; 1130 Buf_Addi(buf, start, str); 1131 1132 start = str; 1133 if (*str++ == '\0') 1134 break; 1135 str++; 1136 /* and escaped dollars */ 1137 if (start[1] == '$') { 1138 Buf_Addi(buf, start, start+2); 1139 continue; 1140 } 1141 /* Simple variable, if it's not us, copy. */ 1142 if (start[1] != '(' && start[1] != '{') { 1143 if (start[1] != *var || var[1] != '\0') { 1144 Buf_AddChars(buf, 2, start); 1145 continue; 1146 } 1147 } else { 1148 const char *p; 1149 char paren = start[1]; 1150 1151 1152 /* Find the end of the variable specification. */ 1153 p = find_pos(paren)(str); 1154 /* A variable inside the variable. We don't know how to 1155 * expand the external variable at this point, so we 1156 * try again with the nested variable. */ 1157 if (*p == '$') { 1158 Buf_Addi(buf, start, p); 1159 str = p; 1160 continue; 1161 } 1162 1163 if (strncmp(var, str, p - str) != 0 || 1164 var[p - str] != '\0') { 1165 /* Not the variable we want to expand. */ 1166 Buf_Addi(buf, start, p); 1167 str = p; 1168 continue; 1169 } 1170 if (*p == ':') { 1171 bool doFree; /* should val be freed ? */ 1172 char *newval; 1173 struct Name name; 1174 1175 doFree = false; 1176 name.s = var; 1177 name.e = var + (p-str); 1178 1179 /* val won't be freed since !doFree, but 1180 * VarModifiers_Apply doesn't know that, 1181 * hence the cast. */ 1182 newval = VarModifiers_Apply((char *)val, 1183 &name, NULL, false, &doFree, &p, paren); 1184 Buf_AddString(buf, newval); 1185 if (doFree) 1186 free(newval); 1187 str = p; 1188 continue; 1189 } else 1190 str = p+1; 1191 } 1192 Buf_AddString(buf, val); 1193 } 1194 } 1195 1196 /*** 1197 *** Odds and ends 1198 ***/ 1199 1200 static void 1201 set_magic_shell_variable() 1202 { 1203 const char *name = "SHELL"; 1204 const char *ename = NULL; 1205 uint32_t k; 1206 Var *v; 1207 1208 k = ohash_interval(name, &ename); 1209 v = find_global_var_without_env(name, ename, k); 1210 var_set_value(v, _PATH_BSHELL); 1211 /* XXX the environment shall never affect it */ 1212 v->flags = VAR_SHELL | VAR_SEEN_ENV; 1213 } 1214 1215 /* 1216 * Var_Init 1217 * Initialize the module 1218 */ 1219 void 1220 Var_Init(void) 1221 { 1222 ohash_init(&global_variables, 10, &var_info); 1223 set_magic_shell_variable(); 1224 1225 1226 errorIsOkay = true; 1227 Var_setCheckEnvFirst(false); 1228 1229 VarModifiers_Init(); 1230 Buf_Init(&subst_buffer, MAKE_BSIZE); 1231 } 1232 1233 1234 #ifdef CLEANUP 1235 void 1236 Var_End(void) 1237 { 1238 Var *v; 1239 unsigned int i; 1240 1241 for (v = ohash_first(&global_variables, &i); v != NULL; 1242 v = ohash_next(&global_variables, &i)) 1243 delete_var(v); 1244 } 1245 #endif 1246 1247 static const char *interpret(int); 1248 1249 static const char * 1250 interpret(int f) 1251 { 1252 if (f & VAR_DUMMY) 1253 return "(D)"; 1254 return ""; 1255 } 1256 1257 1258 static void 1259 print_var(Var *v) 1260 { 1261 printf("%-16s%s = %s\n", v->name, interpret(v->flags), 1262 (v->flags & VAR_DUMMY) == 0 ? var_get_value(v) : "(none)"); 1263 } 1264 1265 void 1266 Var_Dump(void) 1267 { 1268 Var *v; 1269 unsigned int i; 1270 1271 printf("#*** Global Variables:\n"); 1272 1273 for (v = ohash_first(&global_variables, &i); v != NULL; 1274 v = ohash_next(&global_variables, &i)) 1275 print_var(v); 1276 } 1277 1278 static const char *quotable = " \t\n\\'\""; 1279 1280 /* POSIX says that variable assignments passed on the command line should be 1281 * propagated to sub makes through MAKEFLAGS. 1282 */ 1283 void 1284 Var_AddCmdline(const char *name) 1285 { 1286 Var *v; 1287 unsigned int i; 1288 BUFFER buf; 1289 char *s; 1290 1291 Buf_Init(&buf, MAKE_BSIZE); 1292 1293 for (v = ohash_first(&global_variables, &i); v != NULL; 1294 v = ohash_next(&global_variables, &i)) { 1295 /* This is not as expensive as it looks: this function is 1296 * called before parsing Makefiles, so there are just a 1297 * few non cmdling variables in there. 1298 */ 1299 if (!(v->flags & VAR_FROM_CMD)) { 1300 continue; 1301 } 1302 /* We assume variable names don't need quoting */ 1303 Buf_AddString(&buf, v->name); 1304 Buf_AddChar(&buf, '='); 1305 for (s = var_get_value(v); *s != '\0'; s++) { 1306 if (strchr(quotable, *s)) 1307 Buf_AddChar(&buf, '\\'); 1308 Buf_AddChar(&buf, *s); 1309 } 1310 Buf_AddSpace(&buf); 1311 } 1312 Var_Append(name, Buf_Retrieve(&buf)); 1313 Buf_Destroy(&buf); 1314 } 1315