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