| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "makeint.h" |
| 18 |
|
| 19 |
#include <assert.h> |
| 20 |
|
| 21 |
#include "filedef.h" |
| 22 |
#include "dep.h" |
| 23 |
#include "job.h" |
| 24 |
#include "commands.h" |
| 25 |
#include "variable.h" |
| 26 |
#include "rule.h" |
| 27 |
#ifdef WINDOWS32 |
| 28 |
#include "pathstuff.h" |
| 29 |
#endif |
| 30 |
#include "hash.h" |
| 31 |
#include "main.h" |
| 32 |
|
| 33 |
|
| 34 |
static unsigned long variable_changenum; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
static struct pattern_var *pattern_vars; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
static struct pattern_var *last_pattern_vars[256]; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
const char * |
| 48 |
origin2str(variable_origin_t origin) |
| 49 |
{ |
| 50 |
switch (origin) |
| 51 |
{ |
| 52 |
case o_default: |
| 53 |
return _("default"); |
| 54 |
break; |
| 55 |
case o_env: |
| 56 |
return _("environment"); |
| 57 |
break; |
| 58 |
case o_file: |
| 59 |
return _("makefile"); |
| 60 |
break; |
| 61 |
case o_env_override: |
| 62 |
return _("environment under -e"); |
| 63 |
break; |
| 64 |
case o_command: |
| 65 |
return _("command line"); |
| 66 |
break; |
| 67 |
case o_override: |
| 68 |
return _("`override' directive"); |
| 69 |
break; |
| 70 |
case o_automatic: |
| 71 |
return _("automatic"); |
| 72 |
break; |
| 73 |
case o_debugger: |
| 74 |
return _("debugger"); |
| 75 |
break; |
| 76 |
case o_invalid: |
| 77 |
default: |
| 78 |
return _("invalid"); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
struct pattern_var * |
| 90 |
create_pattern_var (const char *target, const char *suffix) |
| 91 |
{ |
| 92 |
size_t len = strlen (target); |
| 93 |
struct pattern_var *p = xcalloc (sizeof (struct pattern_var)); |
| 94 |
|
| 95 |
if (pattern_vars != 0) |
| 96 |
{ |
| 97 |
if (len < 256 && last_pattern_vars[len] != 0) |
| 98 |
{ |
| 99 |
p->next = last_pattern_vars[len]->next; |
| 100 |
last_pattern_vars[len]->next = p; |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
|
| 105 |
struct pattern_var **v; |
| 106 |
|
| 107 |
for (v = &pattern_vars; ; v = &(*v)->next) |
| 108 |
{ |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
if (*v == 0 || (*v)->len > len) |
| 113 |
{ |
| 114 |
p->next = *v; |
| 115 |
*v = p; |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
else |
| 122 |
{ |
| 123 |
pattern_vars = p; |
| 124 |
p->next = 0; |
| 125 |
} |
| 126 |
|
| 127 |
p->target = target; |
| 128 |
p->len = len; |
| 129 |
p->suffix = suffix + 1; |
| 130 |
|
| 131 |
if (len < 256) |
| 132 |
last_pattern_vars[len] = p; |
| 133 |
|
| 134 |
return p; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
static struct pattern_var * |
| 140 |
lookup_pattern_var (struct pattern_var *start, const char *target) |
| 141 |
{ |
| 142 |
struct pattern_var *p; |
| 143 |
size_t targlen = strlen (target); |
| 144 |
|
| 145 |
for (p = start ? start->next : pattern_vars; p != 0; p = p->next) |
| 146 |
{ |
| 147 |
const char *stem; |
| 148 |
size_t stemlen; |
| 149 |
|
| 150 |
if (p->len > targlen) |
| 151 |
|
| 152 |
continue; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
stem = target + (p->suffix - p->target - 1); |
| 157 |
stemlen = targlen - p->len + 1; |
| 158 |
|
| 159 |
|
| 160 |
if (stem > target && !strneq (p->target, target, stem - target)) |
| 161 |
continue; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
if (*p->suffix == stem[stemlen] |
| 169 |
&& (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1]))) |
| 170 |
break; |
| 171 |
} |
| 172 |
|
| 173 |
return p; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
static unsigned long |
| 179 |
variable_hash_1 (const void *keyv) |
| 180 |
{ |
| 181 |
struct variable const *key = (struct variable const *) keyv; |
| 182 |
return_STRING_N_HASH_1 (key->name, key->length); |
| 183 |
} |
| 184 |
|
| 185 |
static unsigned long |
| 186 |
variable_hash_2 (const void *keyv) |
| 187 |
{ |
| 188 |
struct variable const *key = (struct variable const *) keyv; |
| 189 |
return_STRING_N_HASH_2 (key->name, key->length); |
| 190 |
} |
| 191 |
|
| 192 |
static int |
| 193 |
variable_hash_cmp (const void *xv, const void *yv) |
| 194 |
{ |
| 195 |
struct variable const *x = (struct variable const *) xv; |
| 196 |
struct variable const *y = (struct variable const *) yv; |
| 197 |
int result = x->length - y->length; |
| 198 |
if (result) |
| 199 |
return result; |
| 200 |
return_STRING_N_COMPARE (x->name, y->name, x->length); |
| 201 |
} |
| 202 |
|
| 203 |
#ifndef VARIABLE_BUCKETS |
| 204 |
#define VARIABLE_BUCKETS 523 |
| 205 |
#endif |
| 206 |
#ifndef PERFILE_VARIABLE_BUCKETS |
| 207 |
#define PERFILE_VARIABLE_BUCKETS 23 |
| 208 |
#endif |
| 209 |
#ifndef SMALL_SCOPE_VARIABLE_BUCKETS |
| 210 |
#define SMALL_SCOPE_VARIABLE_BUCKETS 13 |
| 211 |
#endif |
| 212 |
|
| 213 |
static struct variable_set global_variable_set; |
| 214 |
static struct variable_set_list global_setlist |
| 215 |
= { 0, &global_variable_set, 0 }; |
| 216 |
struct variable_set_list *current_variable_set_list = &global_setlist; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
void |
| 221 |
init_hash_global_variable_set (void) |
| 222 |
{ |
| 223 |
hash_init (&global_variable_set.table, VARIABLE_BUCKETS, |
| 224 |
variable_hash_1, variable_hash_2, variable_hash_cmp); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
struct variable * |
| 235 |
define_variable_in_set (const char *name, size_t length, |
| 236 |
const char *value, enum variable_origin origin, |
| 237 |
int recursive, struct variable_set *set, |
| 238 |
const gmk_floc *flocp) |
| 239 |
{ |
| 240 |
struct variable *v; |
| 241 |
struct variable **var_slot; |
| 242 |
struct variable var_key; |
| 243 |
|
| 244 |
if (set == NULL) |
| 245 |
set = &global_variable_set; |
| 246 |
|
| 247 |
var_key.name = (char *) name; |
| 248 |
var_key.length = (unsigned int) length; |
| 249 |
var_slot = (struct variable **) hash_find_slot (&set->table, &var_key); |
| 250 |
v = *var_slot; |
| 251 |
|
| 252 |
#ifdef VMS |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
if (HASH_VACANT (v) && (origin != o_env)) |
| 259 |
{ |
| 260 |
struct variable * vms_variable; |
| 261 |
char * vname = alloca (length + 1); |
| 262 |
char * vvalue; |
| 263 |
|
| 264 |
strncpy (vname, name, length); |
| 265 |
vvalue = getenv(vname); |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
if ((vvalue != NULL) && (vvalue[0] != '$')) |
| 270 |
{ |
| 271 |
vms_variable = lookup_variable(name, length); |
| 272 |
|
| 273 |
var_slot = (struct variable **) hash_find_slot (&set->table, |
| 274 |
&var_key); |
| 275 |
v = *var_slot; |
| 276 |
} |
| 277 |
} |
| 278 |
#endif |
| 279 |
|
| 280 |
if (env_overrides && origin == o_env) |
| 281 |
origin = o_env_override; |
| 282 |
|
| 283 |
if (! HASH_VACANT (v)) |
| 284 |
{ |
| 285 |
if (env_overrides && v->origin == o_env) |
| 286 |
|
| 287 |
|
| 288 |
v->origin = o_env_override; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
if ((int) origin >= (int) v->origin) |
| 294 |
{ |
| 295 |
free (v->value); |
| 296 |
v->value = xstrdup (value); |
| 297 |
if (flocp != 0) |
| 298 |
v->fileinfo = *flocp; |
| 299 |
else |
| 300 |
v->fileinfo.filenm = 0; |
| 301 |
v->origin = origin; |
| 302 |
v->recursive = recursive; |
| 303 |
} |
| 304 |
return v; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
v = xcalloc (sizeof (struct variable)); |
| 310 |
v->name = xstrndup (name, length); |
| 311 |
v->length = (unsigned int) length; |
| 312 |
hash_insert_at (&set->table, v, var_slot); |
| 313 |
if (set == &global_variable_set) |
| 314 |
++variable_changenum; |
| 315 |
|
| 316 |
v->value = xstrdup (value); |
| 317 |
if (flocp != 0) |
| 318 |
v->fileinfo = *flocp; |
| 319 |
v->origin = origin; |
| 320 |
v->recursive = recursive; |
| 321 |
|
| 322 |
v->export = v_default; |
| 323 |
v->exportable = 1; |
| 324 |
if (*name != '_' && (*name < 'A' || *name > 'Z') |
| 325 |
&& (*name < 'a' || *name > 'z')) |
| 326 |
v->exportable = 0; |
| 327 |
else |
| 328 |
{ |
| 329 |
for (++name; *name != '\0'; ++name) |
| 330 |
if (*name != '_' && (*name < 'a' || *name > 'z') |
| 331 |
&& (*name < 'A' || *name > 'Z') && !ISDIGIT(*name)) |
| 332 |
break; |
| 333 |
|
| 334 |
if (*name != '\0') |
| 335 |
v->exportable = 0; |
| 336 |
} |
| 337 |
|
| 338 |
return v; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
static void |
| 347 |
free_variable_name_and_value (const void *item) |
| 348 |
{ |
| 349 |
struct variable *v = (struct variable *) item; |
| 350 |
free (v->name); |
| 351 |
free (v->value); |
| 352 |
} |
| 353 |
|
| 354 |
void |
| 355 |
free_variable_set (struct variable_set_list *list) |
| 356 |
{ |
| 357 |
hash_map (&list->set->table, free_variable_name_and_value); |
| 358 |
hash_free (&list->set->table, 1); |
| 359 |
free (list->set); |
| 360 |
free (list); |
| 361 |
} |
| 362 |
|
| 363 |
void |
| 364 |
undefine_variable_in_set (const char *name, size_t length, |
| 365 |
enum variable_origin origin, |
| 366 |
struct variable_set *set) |
| 367 |
{ |
| 368 |
struct variable *v; |
| 369 |
struct variable **var_slot; |
| 370 |
struct variable var_key; |
| 371 |
|
| 372 |
if (set == NULL) |
| 373 |
set = &global_variable_set; |
| 374 |
|
| 375 |
var_key.name = (char *) name; |
| 376 |
var_key.length = (unsigned int) length; |
| 377 |
var_slot = (struct variable **) hash_find_slot (&set->table, &var_key); |
| 378 |
|
| 379 |
if (env_overrides && origin == o_env) |
| 380 |
origin = o_env_override; |
| 381 |
|
| 382 |
v = *var_slot; |
| 383 |
if (! HASH_VACANT (v)) |
| 384 |
{ |
| 385 |
if (env_overrides && v->origin == o_env) |
| 386 |
|
| 387 |
|
| 388 |
v->origin = o_env_override; |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
if ((int) origin >= (int) v->origin) |
| 393 |
{ |
| 394 |
hash_delete_at (&set->table, var_slot); |
| 395 |
free_variable_name_and_value (v); |
| 396 |
free (v); |
| 397 |
if (set == &global_variable_set) |
| 398 |
++variable_changenum; |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500) |
| 412 |
|
| 413 |
static struct variable * |
| 414 |
lookup_special_var (struct variable *var) |
| 415 |
{ |
| 416 |
static unsigned long last_changenum = 0; |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
if (variable_changenum != last_changenum && streq (var->name, ".VARIABLES")) |
| 445 |
{ |
| 446 |
size_t max = EXPANSION_INCREMENT (strlen (var->value)); |
| 447 |
size_t len; |
| 448 |
char *p; |
| 449 |
struct variable **vp = (struct variable **) global_variable_set.table.ht_vec; |
| 450 |
struct variable **end = &vp[global_variable_set.table.ht_size]; |
| 451 |
|
| 452 |
|
| 453 |
var->value = xrealloc (var->value, max); |
| 454 |
|
| 455 |
|
| 456 |
p = var->value; |
| 457 |
len = 0; |
| 458 |
for (; vp < end; ++vp) |
| 459 |
if (!HASH_VACANT (*vp)) |
| 460 |
{ |
| 461 |
struct variable *v = *vp; |
| 462 |
int l = v->length; |
| 463 |
|
| 464 |
len += l + 1; |
| 465 |
if (len > max) |
| 466 |
{ |
| 467 |
size_t off = p - var->value; |
| 468 |
|
| 469 |
max += EXPANSION_INCREMENT (l + 1); |
| 470 |
var->value = xrealloc (var->value, max); |
| 471 |
p = &var->value[off]; |
| 472 |
} |
| 473 |
|
| 474 |
memcpy (p, v->name, l); |
| 475 |
p += l; |
| 476 |
*(p++) = ' '; |
| 477 |
} |
| 478 |
*(p-1) = '\0'; |
| 479 |
|
| 480 |
|
| 481 |
last_changenum = variable_changenum; |
| 482 |
} |
| 483 |
|
| 484 |
return var; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
struct variable * |
| 494 |
lookup_variable (const char *name, size_t length) |
| 495 |
{ |
| 496 |
const struct variable_set_list *setlist; |
| 497 |
struct variable var_key; |
| 498 |
int is_parent = 0; |
| 499 |
|
| 500 |
var_key.name = (char *) name; |
| 501 |
var_key.length = (unsigned int) length; |
| 502 |
|
| 503 |
for (setlist = current_variable_set_list; |
| 504 |
setlist != 0; setlist = setlist->next) |
| 505 |
{ |
| 506 |
const struct variable_set *set = setlist->set; |
| 507 |
struct variable *v; |
| 508 |
|
| 509 |
v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key); |
| 510 |
if (v && (!is_parent || !v->private_var)) |
| 511 |
return v->special ? lookup_special_var (v) : v; |
| 512 |
|
| 513 |
is_parent |= setlist->next_is_parent; |
| 514 |
} |
| 515 |
|
| 516 |
#ifdef VMS |
| 517 |
|
| 518 |
|
| 519 |
{ |
| 520 |
char *vname = alloca (length + 1); |
| 521 |
char *value; |
| 522 |
strncpy (vname, name, length); |
| 523 |
vname[length] = 0; |
| 524 |
value = getenv (vname); |
| 525 |
if (value != 0) |
| 526 |
{ |
| 527 |
char *sptr; |
| 528 |
int scnt; |
| 529 |
|
| 530 |
sptr = value; |
| 531 |
scnt = 0; |
| 532 |
|
| 533 |
while ((sptr = strchr (sptr, '$'))) |
| 534 |
{ |
| 535 |
scnt++; |
| 536 |
sptr++; |
| 537 |
} |
| 538 |
|
| 539 |
if (scnt > 0) |
| 540 |
{ |
| 541 |
char *nvalue; |
| 542 |
char *nptr; |
| 543 |
|
| 544 |
nvalue = alloca (strlen (value) + scnt + 1); |
| 545 |
sptr = value; |
| 546 |
nptr = nvalue; |
| 547 |
|
| 548 |
while (*sptr) |
| 549 |
{ |
| 550 |
if (*sptr == '$') |
| 551 |
{ |
| 552 |
*nptr++ = '$'; |
| 553 |
*nptr++ = '$'; |
| 554 |
} |
| 555 |
else |
| 556 |
{ |
| 557 |
*nptr++ = *sptr; |
| 558 |
} |
| 559 |
sptr++; |
| 560 |
} |
| 561 |
|
| 562 |
*nptr = '\0'; |
| 563 |
return define_variable (vname, length, nvalue, o_env, 1); |
| 564 |
|
| 565 |
} |
| 566 |
|
| 567 |
return define_variable (vname, length, value, o_env, 1); |
| 568 |
} |
| 569 |
} |
| 570 |
#endif |
| 571 |
|
| 572 |
return 0; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
struct variable * |
| 581 |
lookup_variable_in_set (const char *name, size_t length, |
| 582 |
const struct variable_set *set) |
| 583 |
{ |
| 584 |
struct variable var_key; |
| 585 |
|
| 586 |
var_key.name = (char *) name; |
| 587 |
var_key.length = (unsigned int) length; |
| 588 |
|
| 589 |
return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key); |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
void |
| 602 |
initialize_file_variables (struct file *file, int reading) |
| 603 |
{ |
| 604 |
struct variable_set_list *l = file->variables; |
| 605 |
|
| 606 |
if (l == 0) |
| 607 |
{ |
| 608 |
l = (struct variable_set_list *) |
| 609 |
xmalloc (sizeof (struct variable_set_list)); |
| 610 |
l->set = xmalloc (sizeof (struct variable_set)); |
| 611 |
hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS, |
| 612 |
variable_hash_1, variable_hash_2, variable_hash_cmp); |
| 613 |
file->variables = l; |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
if (file->double_colon && file->double_colon != file) |
| 621 |
{ |
| 622 |
initialize_file_variables (file->double_colon, reading); |
| 623 |
l->next = file->double_colon->variables; |
| 624 |
l->next_is_parent = 0; |
| 625 |
return; |
| 626 |
} |
| 627 |
|
| 628 |
if (file->parent == 0) |
| 629 |
l->next = &global_setlist; |
| 630 |
else |
| 631 |
{ |
| 632 |
initialize_file_variables (file->parent, reading); |
| 633 |
l->next = file->parent->variables; |
| 634 |
} |
| 635 |
l->next_is_parent = 1; |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
if (!reading && !file->pat_searched) |
| 641 |
{ |
| 642 |
struct pattern_var *p; |
| 643 |
|
| 644 |
p = lookup_pattern_var (0, file->name); |
| 645 |
if (p != 0) |
| 646 |
{ |
| 647 |
struct variable_set_list *global = current_variable_set_list; |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
file->pat_variables = create_new_variable_set (); |
| 653 |
current_variable_set_list = file->pat_variables; |
| 654 |
|
| 655 |
do |
| 656 |
{ |
| 657 |
|
| 658 |
|
| 659 |
struct variable *v; |
| 660 |
|
| 661 |
if (p->variable.flavor == f_simple) |
| 662 |
{ |
| 663 |
v = define_variable_loc ( |
| 664 |
p->variable.name, strlen (p->variable.name), |
| 665 |
p->variable.value, p->variable.origin, |
| 666 |
0, &p->variable.fileinfo); |
| 667 |
|
| 668 |
v->flavor = f_simple; |
| 669 |
} |
| 670 |
else |
| 671 |
{ |
| 672 |
v = do_variable_definition ( |
| 673 |
&p->variable.fileinfo, p->variable.name, |
| 674 |
p->variable.value, p->variable.origin, |
| 675 |
p->variable.flavor, 1); |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
v->per_target = p->variable.per_target; |
| 680 |
v->export = p->variable.export; |
| 681 |
v->private_var = p->variable.private_var; |
| 682 |
} |
| 683 |
while ((p = lookup_pattern_var (p, file->name)) != 0); |
| 684 |
|
| 685 |
current_variable_set_list = global; |
| 686 |
} |
| 687 |
file->pat_searched = 1; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
if (file->pat_variables != 0) |
| 693 |
{ |
| 694 |
file->pat_variables->next = l->next; |
| 695 |
file->pat_variables->next_is_parent = l->next_is_parent; |
| 696 |
l->next = file->pat_variables; |
| 697 |
l->next_is_parent = 0; |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
struct variable_set_list * |
| 705 |
create_new_variable_set (void) |
| 706 |
{ |
| 707 |
struct variable_set_list *setlist; |
| 708 |
struct variable_set *set; |
| 709 |
|
| 710 |
set = xmalloc (sizeof (struct variable_set)); |
| 711 |
hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS, |
| 712 |
variable_hash_1, variable_hash_2, variable_hash_cmp); |
| 713 |
|
| 714 |
setlist = (struct variable_set_list *) |
| 715 |
xmalloc (sizeof (struct variable_set_list)); |
| 716 |
setlist->set = set; |
| 717 |
setlist->next = current_variable_set_list; |
| 718 |
setlist->next_is_parent = 0; |
| 719 |
|
| 720 |
return setlist; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
struct variable_set_list * |
| 730 |
push_new_variable_scope (void) |
| 731 |
{ |
| 732 |
current_variable_set_list = create_new_variable_set (); |
| 733 |
if (current_variable_set_list->next == &global_setlist) |
| 734 |
{ |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
struct variable_set *set = current_variable_set_list->set; |
| 739 |
current_variable_set_list->set = global_setlist.set; |
| 740 |
global_setlist.set = set; |
| 741 |
current_variable_set_list->next = global_setlist.next; |
| 742 |
global_setlist.next = current_variable_set_list; |
| 743 |
current_variable_set_list = &global_setlist; |
| 744 |
} |
| 745 |
return (current_variable_set_list); |
| 746 |
} |
| 747 |
|
| 748 |
void |
| 749 |
pop_variable_scope (void) |
| 750 |
{ |
| 751 |
struct variable_set_list *setlist; |
| 752 |
struct variable_set *set; |
| 753 |
|
| 754 |
|
| 755 |
assert (current_variable_set_list->next != NULL); |
| 756 |
|
| 757 |
if (current_variable_set_list != &global_setlist) |
| 758 |
{ |
| 759 |
|
| 760 |
setlist = current_variable_set_list; |
| 761 |
set = setlist->set; |
| 762 |
current_variable_set_list = setlist->next; |
| 763 |
} |
| 764 |
else |
| 765 |
{ |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
setlist = global_setlist.next; |
| 770 |
set = global_setlist.set; |
| 771 |
global_setlist.set = setlist->set; |
| 772 |
global_setlist.next = setlist->next; |
| 773 |
global_setlist.next_is_parent = setlist->next_is_parent; |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
free (setlist); |
| 778 |
hash_map (&set->table, free_variable_name_and_value); |
| 779 |
hash_free (&set->table, 1); |
| 780 |
free (set); |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
static void |
| 786 |
merge_variable_sets (struct variable_set *to_set, |
| 787 |
struct variable_set *from_set) |
| 788 |
{ |
| 789 |
struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec; |
| 790 |
struct variable **from_var_end = from_var_slot + from_set->table.ht_size; |
| 791 |
|
| 792 |
int inc = to_set == &global_variable_set ? 1 : 0; |
| 793 |
|
| 794 |
for ( ; from_var_slot < from_var_end; from_var_slot++) |
| 795 |
if (! HASH_VACANT (*from_var_slot)) |
| 796 |
{ |
| 797 |
struct variable *from_var = *from_var_slot; |
| 798 |
struct variable **to_var_slot |
| 799 |
= (struct variable **) hash_find_slot (&to_set->table, *from_var_slot); |
| 800 |
if (HASH_VACANT (*to_var_slot)) |
| 801 |
{ |
| 802 |
hash_insert_at (&to_set->table, from_var, to_var_slot); |
| 803 |
variable_changenum += inc; |
| 804 |
} |
| 805 |
else |
| 806 |
{ |
| 807 |
|
| 808 |
free (from_var->value); |
| 809 |
free (from_var); |
| 810 |
} |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
void |
| 817 |
merge_variable_set_lists (struct variable_set_list **setlist0, |
| 818 |
struct variable_set_list *setlist1) |
| 819 |
{ |
| 820 |
struct variable_set_list *to = *setlist0; |
| 821 |
struct variable_set_list *last0 = 0; |
| 822 |
|
| 823 |
|
| 824 |
if (!setlist1 || setlist1 == &global_setlist) |
| 825 |
return; |
| 826 |
|
| 827 |
if (to) |
| 828 |
{ |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
while (to != &global_setlist) |
| 834 |
{ |
| 835 |
if (to == setlist1) |
| 836 |
return; |
| 837 |
to = to->next; |
| 838 |
} |
| 839 |
|
| 840 |
to = *setlist0; |
| 841 |
while (setlist1 != &global_setlist && to != &global_setlist) |
| 842 |
{ |
| 843 |
struct variable_set_list *from = setlist1; |
| 844 |
setlist1 = setlist1->next; |
| 845 |
|
| 846 |
merge_variable_sets (to->set, from->set); |
| 847 |
|
| 848 |
last0 = to; |
| 849 |
to = to->next; |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
if (setlist1 != &global_setlist) |
| 854 |
{ |
| 855 |
if (last0 == 0) |
| 856 |
*setlist0 = setlist1; |
| 857 |
else |
| 858 |
last0->next = setlist1; |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
void |
| 866 |
define_automatic_variables (void) |
| 867 |
{ |
| 868 |
struct variable *v; |
| 869 |
char buf[200]; |
| 870 |
|
| 871 |
sprintf (buf, "%u", makelevel); |
| 872 |
define_variable_cname (MAKELEVEL_NAME, buf, o_env, 0); |
| 873 |
|
| 874 |
sprintf (buf, "%d", (int32_t)makeparent_pid); |
| 875 |
define_variable_cname (MAKEPARENT_PID_NAME, buf, o_env, 0); |
| 876 |
|
| 877 |
if (makeparent_target) { |
| 878 |
sprintf (buf, "%s", makeparent_target); |
| 879 |
define_variable_cname (MAKEPARENT_TARGET_NAME, buf, o_env, 0); |
| 880 |
} |
| 881 |
|
| 882 |
sprintf (buf, "%s%s%s", |
| 883 |
version_string, |
| 884 |
(remote_description == 0 || remote_description[0] == '\0') |
| 885 |
? "" : "-", |
| 886 |
(remote_description == 0 || remote_description[0] == '\0') |
| 887 |
? "" : remote_description); |
| 888 |
define_variable_cname ("MAKE_VERSION", buf, o_default, 0); |
| 889 |
define_variable_cname ("MAKE_HOST", make_host, o_default, 0); |
| 890 |
|
| 891 |
#ifdef __MSDOS__ |
| 892 |
|
| 893 |
|
| 894 |
{ |
| 895 |
static char shell_str[] = "SHELL"; |
| 896 |
const int shlen = sizeof (shell_str) - 1; |
| 897 |
struct variable *mshp = lookup_variable ("MAKESHELL", 9); |
| 898 |
struct variable *comp = lookup_variable ("COMSPEC", 7); |
| 899 |
|
| 900 |
|
| 901 |
if (mshp) |
| 902 |
(void) define_variable (shell_str, shlen, |
| 903 |
mshp->value, o_env_override, 0); |
| 904 |
else if (comp) |
| 905 |
{ |
| 906 |
|
| 907 |
struct variable *shp = lookup_variable (shell_str, shlen); |
| 908 |
|
| 909 |
if (!shp) |
| 910 |
(void) define_variable (shell_str, shlen, comp->value, o_env, 0); |
| 911 |
} |
| 912 |
} |
| 913 |
#elif defined(__EMX__) |
| 914 |
{ |
| 915 |
static char shell_str[] = "SHELL"; |
| 916 |
const int shlen = sizeof (shell_str) - 1; |
| 917 |
struct variable *shell = lookup_variable (shell_str, shlen); |
| 918 |
struct variable *replace = lookup_variable ("MAKESHELL", 9); |
| 919 |
|
| 920 |
|
| 921 |
if (replace && *replace->value && replace->origin == o_env) |
| 922 |
replace->origin = o_env_override; |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
if (!replace || !*replace->value) |
| 927 |
if (shell && *shell->value && (shell->origin == o_env |
| 928 |
|| shell->origin == o_env_override)) |
| 929 |
{ |
| 930 |
|
| 931 |
free (shell->value); |
| 932 |
shell->value = xstrdup (default_shell); |
| 933 |
shell->origin = o_default; |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
# ifndef NO_CMD_DEFAULT |
| 940 |
|
| 941 |
if (!replace || !*replace->value) |
| 942 |
replace = lookup_variable ("COMSPEC", 7); |
| 943 |
|
| 944 |
|
| 945 |
if (!replace || !*replace->value) |
| 946 |
replace = lookup_variable ("OS2_SHELL", 9); |
| 947 |
# else |
| 948 |
# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell |
| 949 |
# endif |
| 950 |
|
| 951 |
if (replace && *replace->value) |
| 952 |
|
| 953 |
(void) define_variable (shell_str, shlen, replace->value, |
| 954 |
replace->origin, 0); |
| 955 |
else |
| 956 |
|
| 957 |
(void) define_variable (shell_str, shlen, default_shell, |
| 958 |
o_default, 0); |
| 959 |
} |
| 960 |
|
| 961 |
#endif |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
v = define_variable_cname ("SHELL", default_shell, o_default, 0); |
| 966 |
#ifdef __MSDOS__ |
| 967 |
v->export = v_export; |
| 968 |
#endif |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
#if !defined(__MSDOS__) && !defined(__EMX__) |
| 975 |
|
| 976 |
if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override) |
| 977 |
{ |
| 978 |
free (v->value); |
| 979 |
v->origin = o_file; |
| 980 |
v->value = xstrdup (default_shell); |
| 981 |
} |
| 982 |
#endif |
| 983 |
|
| 984 |
|
| 985 |
v = define_variable_cname ("MAKEFILES", "", o_default, 0); |
| 986 |
v->export = v_ifset; |
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
#if defined(__MSDOS__) || defined(WINDOWS32) |
| 992 |
|
| 993 |
define_variable_cname ("@D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $@)))", |
| 994 |
o_automatic, 1); |
| 995 |
define_variable_cname ("%D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $%)))", |
| 996 |
o_automatic, 1); |
| 997 |
define_variable_cname ("*D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $*)))", |
| 998 |
o_automatic, 1); |
| 999 |
define_variable_cname ("<D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $<)))", |
| 1000 |
o_automatic, 1); |
| 1001 |
define_variable_cname ("?D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $?)))", |
| 1002 |
o_automatic, 1); |
| 1003 |
define_variable_cname ("^D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $^)))", |
| 1004 |
o_automatic, 1); |
| 1005 |
define_variable_cname ("+D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $+)))", |
| 1006 |
o_automatic, 1); |
| 1007 |
#else |
| 1008 |
define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic, 1); |
| 1009 |
define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic, 1); |
| 1010 |
define_variable_cname ("*D", "$(patsubst %/,%,$(dir $*))", o_automatic, 1); |
| 1011 |
define_variable_cname ("<D", "$(patsubst %/,%,$(dir $<))", o_automatic, 1); |
| 1012 |
define_variable_cname ("?D", "$(patsubst %/,%,$(dir $?))", o_automatic, 1); |
| 1013 |
define_variable_cname ("^D", "$(patsubst %/,%,$(dir $^))", o_automatic, 1); |
| 1014 |
define_variable_cname ("+D", "$(patsubst %/,%,$(dir $+))", o_automatic, 1); |
| 1015 |
#endif |
| 1016 |
define_variable_cname ("@F", "$(notdir $@)", o_automatic, 1); |
| 1017 |
define_variable_cname ("%F", "$(notdir $%)", o_automatic, 1); |
| 1018 |
define_variable_cname ("*F", "$(notdir $*)", o_automatic, 1); |
| 1019 |
define_variable_cname ("<F", "$(notdir $<)", o_automatic, 1); |
| 1020 |
define_variable_cname ("?F", "$(notdir $?)", o_automatic, 1); |
| 1021 |
define_variable_cname ("^F", "$(notdir $^)", o_automatic, 1); |
| 1022 |
define_variable_cname ("+F", "$(notdir $+)", o_automatic, 1); |
| 1023 |
} |
| 1024 |
|
| 1025 |
int export_all_variables; |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
char ** |
| 1032 |
target_environment (struct file *file) |
| 1033 |
{ |
| 1034 |
struct variable_set_list *set_list; |
| 1035 |
struct variable_set_list *s; |
| 1036 |
struct hash_table table; |
| 1037 |
struct variable **v_slot; |
| 1038 |
struct variable **v_end; |
| 1039 |
struct variable makelevel_key; |
| 1040 |
struct variable makeparent_key; |
| 1041 |
char **result_0; |
| 1042 |
char **result; |
| 1043 |
|
| 1044 |
if (file == 0) |
| 1045 |
set_list = current_variable_set_list; |
| 1046 |
else |
| 1047 |
set_list = file->variables; |
| 1048 |
|
| 1049 |
hash_init (&table, VARIABLE_BUCKETS, |
| 1050 |
variable_hash_1, variable_hash_2, variable_hash_cmp); |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
for (s = set_list; s != 0; s = s->next) |
| 1055 |
{ |
| 1056 |
struct variable_set *set = s->set; |
| 1057 |
v_slot = (struct variable **) set->table.ht_vec; |
| 1058 |
v_end = v_slot + set->table.ht_size; |
| 1059 |
for ( ; v_slot < v_end; v_slot++) |
| 1060 |
if (! HASH_VACANT (*v_slot)) |
| 1061 |
{ |
| 1062 |
struct variable **new_slot; |
| 1063 |
struct variable *v = *v_slot; |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
if (v->per_target && v->export == v_default) |
| 1069 |
{ |
| 1070 |
struct variable *gv; |
| 1071 |
|
| 1072 |
gv = lookup_variable_in_set (v->name, strlen (v->name), |
| 1073 |
&global_variable_set); |
| 1074 |
if (gv) |
| 1075 |
v->export = gv->export; |
| 1076 |
} |
| 1077 |
|
| 1078 |
switch (v->export) |
| 1079 |
{ |
| 1080 |
case v_default: |
| 1081 |
if (v->origin == o_default || v->origin == o_automatic) |
| 1082 |
|
| 1083 |
continue; |
| 1084 |
|
| 1085 |
|
| 1086 |
if (! v->exportable) |
| 1087 |
continue; |
| 1088 |
|
| 1089 |
if (! export_all_variables |
| 1090 |
&& v->origin != o_command |
| 1091 |
&& v->origin != o_env && v->origin != o_env_override) |
| 1092 |
continue; |
| 1093 |
break; |
| 1094 |
|
| 1095 |
case v_export: |
| 1096 |
break; |
| 1097 |
|
| 1098 |
case v_noexport: |
| 1099 |
{ |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
if (streq (v->name, "SHELL") && shell_var.value) |
| 1104 |
{ |
| 1105 |
v = &shell_var; |
| 1106 |
break; |
| 1107 |
} |
| 1108 |
continue; |
| 1109 |
} |
| 1110 |
|
| 1111 |
case v_ifset: |
| 1112 |
if (v->origin == o_default) |
| 1113 |
continue; |
| 1114 |
break; |
| 1115 |
} |
| 1116 |
|
| 1117 |
new_slot = (struct variable **) hash_find_slot (&table, v); |
| 1118 |
if (HASH_VACANT (*new_slot)) |
| 1119 |
hash_insert_at (&table, v, new_slot); |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
makelevel_key.name = (char *)MAKELEVEL_NAME; |
| 1124 |
makelevel_key.length = MAKELEVEL_LENGTH; |
| 1125 |
hash_delete (&table, &makelevel_key); |
| 1126 |
|
| 1127 |
makeparent_key.name = (char *)MAKEPARENT_PID_NAME; |
| 1128 |
makeparent_key.length = MAKEPARENT_PID_LENGTH; |
| 1129 |
hash_delete (&table, &makeparent_key); |
| 1130 |
|
| 1131 |
makeparent_key.name = (char *)MAKEPARENT_TARGET_NAME; |
| 1132 |
makeparent_key.length = MAKEPARENT_TARGET_LENGTH; |
| 1133 |
hash_delete (&table, &makeparent_key); |
| 1134 |
|
| 1135 |
|
| 1136 |
result = result_0 = xmalloc ((table.ht_fill + 4) * sizeof (char *)); |
| 1137 |
|
| 1138 |
v_slot = (struct variable **) table.ht_vec; |
| 1139 |
v_end = v_slot + table.ht_size; |
| 1140 |
for ( ; v_slot < v_end; v_slot++) |
| 1141 |
if (! HASH_VACANT (*v_slot)) |
| 1142 |
{ |
| 1143 |
struct variable *v = *v_slot; |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
if (v->recursive |
| 1149 |
&& v->origin != o_env && v->origin != o_env_override) |
| 1150 |
{ |
| 1151 |
char *value = recursively_expand_for_file (v, file); |
| 1152 |
#ifdef WINDOWS32 |
| 1153 |
if (strcmp (v->name, "Path") == 0 || |
| 1154 |
strcmp (v->name, "PATH") == 0) |
| 1155 |
convert_Path_to_windows32 (value, ';'); |
| 1156 |
#endif |
| 1157 |
*result++ = xstrdup (concat (3, v->name, "=", value)); |
| 1158 |
free (value); |
| 1159 |
} |
| 1160 |
else |
| 1161 |
{ |
| 1162 |
#ifdef WINDOWS32 |
| 1163 |
if (strcmp (v->name, "Path") == 0 || |
| 1164 |
strcmp (v->name, "PATH") == 0) |
| 1165 |
convert_Path_to_windows32 (v->value, ';'); |
| 1166 |
#endif |
| 1167 |
*result++ = xstrdup (concat (3, v->name, "=", v->value)); |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
*result = xmalloc (100); |
| 1172 |
sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1); |
| 1173 |
|
| 1174 |
*++result = xmalloc (100); |
| 1175 |
sprintf (*result, "%s=%d", MAKEPARENT_PID_NAME, getpid()); |
| 1176 |
|
| 1177 |
*++result = xmalloc (MAKEPARENT_TARGET_LENGTH + 2 + strlen(file->name)); |
| 1178 |
sprintf(*result, "%s=%s", MAKEPARENT_TARGET_NAME, file->name); |
| 1179 |
*++result = 0; |
| 1180 |
hash_free (&table, 0); |
| 1181 |
|
| 1182 |
return result_0; |
| 1183 |
} |
| 1184 |
|
| 1185 |
static struct variable * |
| 1186 |
set_special_var (struct variable *var) |
| 1187 |
{ |
| 1188 |
if (streq (var->name, RECIPEPREFIX_NAME)) |
| 1189 |
{ |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0]; |
| 1194 |
} |
| 1195 |
|
| 1196 |
return var; |
| 1197 |
} |
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
static char * |
| 1204 |
shell_result (const char *p) |
| 1205 |
{ |
| 1206 |
char *buf; |
| 1207 |
size_t len; |
| 1208 |
char *args[2]; |
| 1209 |
char *result; |
| 1210 |
|
| 1211 |
install_variable_buffer (&buf, &len); |
| 1212 |
|
| 1213 |
args[0] = (char *) p; |
| 1214 |
args[1] = NULL; |
| 1215 |
variable_buffer_output (func_shell_base (variable_buffer, args, 0), "\0", 1); |
| 1216 |
result = strdup (variable_buffer); |
| 1217 |
|
| 1218 |
restore_variable_buffer (buf, len); |
| 1219 |
return result; |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
struct variable * |
| 1226 |
do_variable_definition (const gmk_floc *flocp, const char *varname, |
| 1227 |
const char *value, enum variable_origin origin, |
| 1228 |
enum variable_flavor flavor, int target_var) |
| 1229 |
{ |
| 1230 |
const char *p; |
| 1231 |
char *alloc_value = NULL; |
| 1232 |
struct variable *v; |
| 1233 |
int append = 0; |
| 1234 |
int conditional = 0; |
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
switch (flavor) |
| 1239 |
{ |
| 1240 |
default: |
| 1241 |
case f_bogus: |
| 1242 |
|
| 1243 |
abort (); |
| 1244 |
case f_simple: |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
p = alloc_value = allocated_variable_expand (value); |
| 1250 |
break; |
| 1251 |
case f_shell: |
| 1252 |
{ |
| 1253 |
|
| 1254 |
|
| 1255 |
char *q = allocated_variable_expand (value); |
| 1256 |
p = alloc_value = shell_result (q); |
| 1257 |
free (q); |
| 1258 |
flavor = f_recursive; |
| 1259 |
break; |
| 1260 |
} |
| 1261 |
case f_conditional: |
| 1262 |
|
| 1263 |
|
| 1264 |
v = lookup_variable (varname, strlen (varname)); |
| 1265 |
if (v) |
| 1266 |
goto done; |
| 1267 |
|
| 1268 |
conditional = 1; |
| 1269 |
flavor = f_recursive; |
| 1270 |
|
| 1271 |
case f_recursive: |
| 1272 |
|
| 1273 |
|
| 1274 |
p = value; |
| 1275 |
break; |
| 1276 |
case f_append: |
| 1277 |
case f_append_value: |
| 1278 |
{ |
| 1279 |
|
| 1280 |
|
| 1281 |
if (target_var) |
| 1282 |
{ |
| 1283 |
append = 1; |
| 1284 |
v = lookup_variable_in_set (varname, strlen (varname), |
| 1285 |
current_variable_set_list->set); |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
if (v && !v->append) |
| 1290 |
append = 0; |
| 1291 |
} |
| 1292 |
else |
| 1293 |
v = lookup_variable (varname, strlen (varname)); |
| 1294 |
|
| 1295 |
if (v == 0) |
| 1296 |
{ |
| 1297 |
|
| 1298 |
|
| 1299 |
p = value; |
| 1300 |
flavor = f_recursive; |
| 1301 |
} |
| 1302 |
else |
| 1303 |
{ |
| 1304 |
|
| 1305 |
|
| 1306 |
size_t oldlen, vallen; |
| 1307 |
const char *val; |
| 1308 |
char *tp = NULL; |
| 1309 |
|
| 1310 |
val = value; |
| 1311 |
if (v->recursive) |
| 1312 |
|
| 1313 |
|
| 1314 |
flavor = f_recursive; |
| 1315 |
else if (flavor != f_append_value) |
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
val = tp = allocated_variable_expand (val); |
| 1322 |
|
| 1323 |
|
| 1324 |
vallen = strlen (val); |
| 1325 |
if (!vallen) |
| 1326 |
{ |
| 1327 |
alloc_value = tp; |
| 1328 |
goto done; |
| 1329 |
} |
| 1330 |
|
| 1331 |
oldlen = strlen (v->value); |
| 1332 |
p = alloc_value = xmalloc (oldlen + 1 + vallen + 1); |
| 1333 |
|
| 1334 |
if (oldlen) |
| 1335 |
{ |
| 1336 |
memcpy (alloc_value, v->value, oldlen); |
| 1337 |
alloc_value[oldlen] = ' '; |
| 1338 |
++oldlen; |
| 1339 |
} |
| 1340 |
|
| 1341 |
memcpy (&alloc_value[oldlen], val, vallen + 1); |
| 1342 |
|
| 1343 |
free (tp); |
| 1344 |
} |
| 1345 |
break; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
#ifdef __MSDOS__ |
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
if ((origin == o_file || origin == o_override) |
| 1364 |
&& strcmp (varname, "SHELL") == 0) |
| 1365 |
{ |
| 1366 |
PATH_VAR (shellpath); |
| 1367 |
extern char * __dosexec_find_on_path (const char *, char *[], char *); |
| 1368 |
|
| 1369 |
|
| 1370 |
if (__dosexec_find_on_path (p, NULL, shellpath)) |
| 1371 |
{ |
| 1372 |
char *tp; |
| 1373 |
|
| 1374 |
for (tp = shellpath; *tp; tp++) |
| 1375 |
if (*tp == '\\') |
| 1376 |
*tp = '/'; |
| 1377 |
|
| 1378 |
v = define_variable_loc (varname, strlen (varname), |
| 1379 |
shellpath, origin, flavor == f_recursive, |
| 1380 |
flocp); |
| 1381 |
} |
| 1382 |
else |
| 1383 |
{ |
| 1384 |
const char *shellbase, *bslash; |
| 1385 |
struct variable *pathv = lookup_variable ("PATH", 4); |
| 1386 |
char *path_string; |
| 1387 |
char *fake_env[2]; |
| 1388 |
size_t pathlen = 0; |
| 1389 |
|
| 1390 |
shellbase = strrchr (p, '/'); |
| 1391 |
bslash = strrchr (p, '\\'); |
| 1392 |
if (!shellbase || bslash > shellbase) |
| 1393 |
shellbase = bslash; |
| 1394 |
if (!shellbase && p[1] == ':') |
| 1395 |
shellbase = p + 1; |
| 1396 |
if (shellbase) |
| 1397 |
shellbase++; |
| 1398 |
else |
| 1399 |
shellbase = p; |
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
if (pathv) |
| 1404 |
pathlen = strlen (pathv->value); |
| 1405 |
path_string = xmalloc (5 + pathlen + 2 + 1); |
| 1406 |
|
| 1407 |
sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : ""); |
| 1408 |
fake_env[0] = path_string; |
| 1409 |
fake_env[1] = 0; |
| 1410 |
if (__dosexec_find_on_path (shellbase, fake_env, shellpath)) |
| 1411 |
{ |
| 1412 |
char *tp; |
| 1413 |
|
| 1414 |
for (tp = shellpath; *tp; tp++) |
| 1415 |
if (*tp == '\\') |
| 1416 |
*tp = '/'; |
| 1417 |
|
| 1418 |
v = define_variable_loc (varname, strlen (varname), |
| 1419 |
shellpath, origin, |
| 1420 |
flavor == f_recursive, flocp); |
| 1421 |
} |
| 1422 |
else |
| 1423 |
v = lookup_variable (varname, strlen (varname)); |
| 1424 |
|
| 1425 |
free (path_string); |
| 1426 |
} |
| 1427 |
} |
| 1428 |
else |
| 1429 |
#endif |
| 1430 |
#ifdef WINDOWS32 |
| 1431 |
if ((origin == o_file || origin == o_override || origin == o_command) |
| 1432 |
&& streq (varname, "SHELL")) |
| 1433 |
{ |
| 1434 |
extern const char *default_shell; |
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
if (find_and_set_default_shell (p)) |
| 1441 |
{ |
| 1442 |
v = define_variable_in_set (varname, strlen (varname), default_shell, |
| 1443 |
origin, flavor == f_recursive, |
| 1444 |
(target_var |
| 1445 |
? current_variable_set_list->set |
| 1446 |
: NULL), |
| 1447 |
flocp); |
| 1448 |
no_default_sh_exe = 0; |
| 1449 |
} |
| 1450 |
else |
| 1451 |
{ |
| 1452 |
char *tp = alloc_value; |
| 1453 |
|
| 1454 |
alloc_value = allocated_variable_expand (p); |
| 1455 |
|
| 1456 |
if (find_and_set_default_shell (alloc_value)) |
| 1457 |
{ |
| 1458 |
v = define_variable_in_set (varname, strlen (varname), p, |
| 1459 |
origin, flavor == f_recursive, |
| 1460 |
(target_var |
| 1461 |
? current_variable_set_list->set |
| 1462 |
: NULL), |
| 1463 |
flocp); |
| 1464 |
no_default_sh_exe = 0; |
| 1465 |
} |
| 1466 |
else |
| 1467 |
v = lookup_variable (varname, strlen (varname)); |
| 1468 |
|
| 1469 |
free (tp); |
| 1470 |
} |
| 1471 |
} |
| 1472 |
else |
| 1473 |
v = NULL; |
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
if (!v) |
| 1478 |
#endif |
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
v = define_variable_in_set (varname, strlen (varname), p, |
| 1487 |
origin, flavor == f_recursive, |
| 1488 |
(target_var |
| 1489 |
? current_variable_set_list->set : NULL), |
| 1490 |
flocp); |
| 1491 |
v->append = append; |
| 1492 |
v->conditional = conditional; |
| 1493 |
|
| 1494 |
done: |
| 1495 |
free (alloc_value); |
| 1496 |
return v->special ? set_special_var (v) : v; |
| 1497 |
} |
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
char * |
| 1514 |
parse_variable_definition (const char *p, struct variable *var) |
| 1515 |
{ |
| 1516 |
int wspace = 0; |
| 1517 |
const char *e = NULL; |
| 1518 |
|
| 1519 |
NEXT_TOKEN (p); |
| 1520 |
var->name = (char *)p; |
| 1521 |
var->length = 0; |
| 1522 |
|
| 1523 |
while (1) |
| 1524 |
{ |
| 1525 |
int c = *p++; |
| 1526 |
|
| 1527 |
|
| 1528 |
if (STOP_SET (c, MAP_COMMENT|MAP_NUL)) |
| 1529 |
return NULL; |
| 1530 |
|
| 1531 |
if (c == '$') |
| 1532 |
{ |
| 1533 |
|
| 1534 |
|
| 1535 |
char closeparen; |
| 1536 |
unsigned int count; |
| 1537 |
|
| 1538 |
c = *p++; |
| 1539 |
if (c == '(') |
| 1540 |
closeparen = ')'; |
| 1541 |
else if (c == '{') |
| 1542 |
closeparen = '}'; |
| 1543 |
else if (c == '\0') |
| 1544 |
return NULL; |
| 1545 |
else |
| 1546 |
|
| 1547 |
continue; |
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
for (count = 1; *p != '\0'; ++p) |
| 1552 |
{ |
| 1553 |
if (*p == closeparen && --count == 0) |
| 1554 |
{ |
| 1555 |
++p; |
| 1556 |
break; |
| 1557 |
} |
| 1558 |
if (*p == c) |
| 1559 |
++count; |
| 1560 |
} |
| 1561 |
continue; |
| 1562 |
} |
| 1563 |
|
| 1564 |
|
| 1565 |
if (ISBLANK (c)) |
| 1566 |
{ |
| 1567 |
wspace = 1; |
| 1568 |
e = p - 1; |
| 1569 |
NEXT_TOKEN (p); |
| 1570 |
c = *p; |
| 1571 |
if (c == '\0') |
| 1572 |
return NULL; |
| 1573 |
++p; |
| 1574 |
} |
| 1575 |
|
| 1576 |
|
| 1577 |
if (c == '=') |
| 1578 |
{ |
| 1579 |
var->flavor = f_recursive; |
| 1580 |
if (! e) |
| 1581 |
e = p - 1; |
| 1582 |
break; |
| 1583 |
} |
| 1584 |
|
| 1585 |
|
| 1586 |
if (*p == '=') |
| 1587 |
{ |
| 1588 |
switch (c) |
| 1589 |
{ |
| 1590 |
case ':': |
| 1591 |
var->flavor = f_simple; |
| 1592 |
break; |
| 1593 |
case '+': |
| 1594 |
var->flavor = f_append; |
| 1595 |
break; |
| 1596 |
case '?': |
| 1597 |
var->flavor = f_conditional; |
| 1598 |
break; |
| 1599 |
case '!': |
| 1600 |
var->flavor = f_shell; |
| 1601 |
break; |
| 1602 |
default: |
| 1603 |
|
| 1604 |
if (wspace) |
| 1605 |
return NULL; |
| 1606 |
|
| 1607 |
|
| 1608 |
continue; |
| 1609 |
} |
| 1610 |
if (! e) |
| 1611 |
e = p - 1; |
| 1612 |
++p; |
| 1613 |
break; |
| 1614 |
} |
| 1615 |
|
| 1616 |
|
| 1617 |
if (c == ':') |
| 1618 |
{ |
| 1619 |
|
| 1620 |
if (*p != ':' || p[1] != '=') |
| 1621 |
return NULL; |
| 1622 |
|
| 1623 |
|
| 1624 |
var->flavor = f_simple; |
| 1625 |
if (! e) |
| 1626 |
e = p - 1; |
| 1627 |
p += 2; |
| 1628 |
break; |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
if (wspace) |
| 1633 |
return NULL; |
| 1634 |
} |
| 1635 |
|
| 1636 |
var->length = (unsigned int) (e - var->name); |
| 1637 |
var->value = next_token (p); |
| 1638 |
return (char *)p; |
| 1639 |
} |
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
struct variable * |
| 1648 |
assign_variable_definition (struct variable *v, const char *line) |
| 1649 |
{ |
| 1650 |
char *name; |
| 1651 |
|
| 1652 |
if (!parse_variable_definition (line, v)) |
| 1653 |
return NULL; |
| 1654 |
|
| 1655 |
|
| 1656 |
name = alloca (v->length + 1); |
| 1657 |
memcpy (name, v->name, v->length); |
| 1658 |
name[v->length] = '\0'; |
| 1659 |
v->name = allocated_variable_expand (name); |
| 1660 |
|
| 1661 |
if (v->name[0] == '\0') |
| 1662 |
O (fatal, &v->fileinfo, _("empty variable name")); |
| 1663 |
|
| 1664 |
return v; |
| 1665 |
} |
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
struct variable * |
| 1681 |
try_variable_definition (const gmk_floc *flocp, const char *line, |
| 1682 |
enum variable_origin origin, int target_var) |
| 1683 |
{ |
| 1684 |
struct variable v; |
| 1685 |
struct variable *vp; |
| 1686 |
|
| 1687 |
if (flocp != 0) |
| 1688 |
v.fileinfo = *flocp; |
| 1689 |
else |
| 1690 |
v.fileinfo.filenm = 0; |
| 1691 |
|
| 1692 |
if (!assign_variable_definition (&v, line)) |
| 1693 |
return 0; |
| 1694 |
|
| 1695 |
vp = do_variable_definition (flocp, v.name, v.value, |
| 1696 |
origin, v.flavor, target_var); |
| 1697 |
|
| 1698 |
free (v.name); |
| 1699 |
|
| 1700 |
return vp; |
| 1701 |
} |
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
static void |
| 1706 |
print_variable (const void *item, void *arg) |
| 1707 |
{ |
| 1708 |
const struct variable *v = item; |
| 1709 |
const char *prefix = arg; |
| 1710 |
const char *origin; |
| 1711 |
|
| 1712 |
switch (v->origin) |
| 1713 |
{ |
| 1714 |
case o_automatic: |
| 1715 |
origin = _("automatic"); |
| 1716 |
break; |
| 1717 |
case o_default: |
| 1718 |
origin = _("default"); |
| 1719 |
break; |
| 1720 |
case o_env: |
| 1721 |
origin = _("environment"); |
| 1722 |
break; |
| 1723 |
case o_file: |
| 1724 |
origin = _("makefile"); |
| 1725 |
break; |
| 1726 |
case o_env_override: |
| 1727 |
origin = _("environment under -e"); |
| 1728 |
break; |
| 1729 |
case o_command: |
| 1730 |
origin = _("command line"); |
| 1731 |
break; |
| 1732 |
case o_override: |
| 1733 |
origin = _("'override' directive"); |
| 1734 |
break; |
| 1735 |
case o_debugger: |
| 1736 |
origin = _("debugger"); |
| 1737 |
break; |
| 1738 |
case o_invalid: |
| 1739 |
default: |
| 1740 |
abort (); |
| 1741 |
} |
| 1742 |
fputs ("# ", stdout); |
| 1743 |
fputs (origin, stdout); |
| 1744 |
if (v->private_var) |
| 1745 |
fputs (" private", stdout); |
| 1746 |
if (v->fileinfo.filenm) |
| 1747 |
printf (_(" (from '%s', line %lu)"), |
| 1748 |
v->fileinfo.filenm, v->fileinfo.lineno + v->fileinfo.offset); |
| 1749 |
putchar ('\n'); |
| 1750 |
fputs (prefix, stdout); |
| 1751 |
|
| 1752 |
|
| 1753 |
if (v->recursive && strchr (v->value, '\n') != 0) |
| 1754 |
printf ("define %s\n%s\nendef\n", v->name, v->value); |
| 1755 |
else |
| 1756 |
{ |
| 1757 |
char *p; |
| 1758 |
|
| 1759 |
printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":"); |
| 1760 |
|
| 1761 |
|
| 1762 |
p = next_token (v->value); |
| 1763 |
if (p != v->value && *p == '\0') |
| 1764 |
|
| 1765 |
printf ("$(subst ,,%s)", v->value); |
| 1766 |
else if (v->recursive) |
| 1767 |
fputs (v->value, stdout); |
| 1768 |
else |
| 1769 |
|
| 1770 |
for (p = v->value; *p != '\0'; ++p) |
| 1771 |
{ |
| 1772 |
if (*p == '$') |
| 1773 |
putchar ('$'); |
| 1774 |
putchar (*p); |
| 1775 |
} |
| 1776 |
putchar ('\n'); |
| 1777 |
} |
| 1778 |
} |
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
void |
| 1783 |
print_variable_info (const void *item, void *arg) |
| 1784 |
{ |
| 1785 |
const variable_t *v = (variable_t *) item; |
| 1786 |
const char *prefix = (char *) arg; |
| 1787 |
const char *origin; |
| 1788 |
|
| 1789 |
if (o_invalid == v->origin) abort (); |
| 1790 |
origin = origin2str(v->origin); |
| 1791 |
|
| 1792 |
if (prefix) { |
| 1793 |
fputs ("# ", stdout); |
| 1794 |
fputs (origin, stdout); |
| 1795 |
if (v->fileinfo.filenm) |
| 1796 |
printf (_(" (from `%s', line %lu)"), |
| 1797 |
v->fileinfo.filenm, v->fileinfo.lineno); |
| 1798 |
putchar ('\n'); |
| 1799 |
fputs (prefix, stdout); |
| 1800 |
} |
| 1801 |
|
| 1802 |
|
| 1803 |
if (v->recursive && strchr (v->value, '\n') != 0) |
| 1804 |
printf ("define %s\n%s\nendef\n", v->name, v->value); |
| 1805 |
else |
| 1806 |
{ |
| 1807 |
char *p; |
| 1808 |
|
| 1809 |
printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":"); |
| 1810 |
|
| 1811 |
|
| 1812 |
p = next_token (v->value); |
| 1813 |
if (p != v->value && *p == '\0') |
| 1814 |
|
| 1815 |
printf ("$(subst ,,%s)", v->value); |
| 1816 |
else if (v->recursive) |
| 1817 |
fputs (v->value, stdout); |
| 1818 |
else |
| 1819 |
|
| 1820 |
for (p = v->value; *p != '\0'; ++p) |
| 1821 |
{ |
| 1822 |
if (*p == '$') |
| 1823 |
putchar ('$'); |
| 1824 |
putchar (*p); |
| 1825 |
} |
| 1826 |
putchar ('\n'); |
| 1827 |
} |
| 1828 |
} |
| 1829 |
|
| 1830 |
static void |
| 1831 |
print_auto_variable (const void *item, void *arg) |
| 1832 |
{ |
| 1833 |
const struct variable *v = item; |
| 1834 |
|
| 1835 |
if (v->origin == o_automatic) |
| 1836 |
print_variable (item, arg); |
| 1837 |
} |
| 1838 |
|
| 1839 |
|
| 1840 |
static void |
| 1841 |
print_noauto_variable (const void *item, void *arg) |
| 1842 |
{ |
| 1843 |
const struct variable *v = item; |
| 1844 |
|
| 1845 |
if (v->origin != o_automatic) |
| 1846 |
print_variable (item, arg); |
| 1847 |
} |
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
void |
| 1854 |
print_variable_set (struct variable_set *set, const char *prefix, int pauto) |
| 1855 |
{ |
| 1856 |
hash_map_arg (&set->table, (pauto ? print_auto_variable : print_variable), |
| 1857 |
(void *)prefix); |
| 1858 |
|
| 1859 |
fputs (_("# variable set hash-table stats:\n"), stdout); |
| 1860 |
fputs ("# ", stdout); |
| 1861 |
hash_print_stats (&set->table, stdout); |
| 1862 |
putc ('\n', stdout); |
| 1863 |
} |
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
void |
| 1868 |
print_variable_data_base (void) |
| 1869 |
{ |
| 1870 |
puts (_("\n# Variables\n")); |
| 1871 |
|
| 1872 |
print_variable_set (&global_variable_set, "", 0); |
| 1873 |
|
| 1874 |
puts (_("\n# Pattern-specific Variable Values")); |
| 1875 |
|
| 1876 |
{ |
| 1877 |
struct pattern_var *p; |
| 1878 |
unsigned int rules = 0; |
| 1879 |
|
| 1880 |
for (p = pattern_vars; p != 0; p = p->next) |
| 1881 |
{ |
| 1882 |
++rules; |
| 1883 |
printf ("\n%s :\n", p->target); |
| 1884 |
print_variable (&p->variable, (void *)"# "); |
| 1885 |
} |
| 1886 |
|
| 1887 |
if (rules == 0) |
| 1888 |
puts (_("\n# No pattern-specific variable values.")); |
| 1889 |
else |
| 1890 |
printf (_("\n# %u pattern-specific variable values"), rules); |
| 1891 |
} |
| 1892 |
} |
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
void |
| 1898 |
print_file_variables (const file_t *p_target, bool b_hash_stats) |
| 1899 |
{ |
| 1900 |
if (p_target->variables != 0) |
| 1901 |
print_variable_set (p_target->variables->set, "# ", b_hash_stats); |
| 1902 |
} |
| 1903 |
|
| 1904 |
void |
| 1905 |
print_target_variables (const struct file *file) |
| 1906 |
{ |
| 1907 |
if (file->variables != 0) |
| 1908 |
{ |
| 1909 |
size_t l = strlen (file->name); |
| 1910 |
char *t = alloca (l + 3); |
| 1911 |
|
| 1912 |
strcpy (t, file->name); |
| 1913 |
t[l] = ':'; |
| 1914 |
t[l+1] = ' '; |
| 1915 |
t[l+2] = '\0'; |
| 1916 |
|
| 1917 |
hash_map_arg (&file->variables->set->table, print_noauto_variable, t); |
| 1918 |
} |
| 1919 |
} |
| 1920 |
|
| 1921 |
#ifdef WINDOWS32 |
| 1922 |
void |
| 1923 |
sync_Path_environment (void) |
| 1924 |
{ |
| 1925 |
char *path = allocated_variable_expand ("$(PATH)"); |
| 1926 |
static char *environ_path = NULL; |
| 1927 |
|
| 1928 |
if (!path) |
| 1929 |
return; |
| 1930 |
|
| 1931 |
|
| 1932 |
free (environ_path); |
| 1933 |
|
| 1934 |
|
| 1935 |
convert_Path_to_windows32 (path, ';'); |
| 1936 |
environ_path = xstrdup (concat (3, "PATH", "=", path)); |
| 1937 |
putenv (environ_path); |
| 1938 |
free (path); |
| 1939 |
} |
| 1940 |
#endif |