| 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 "job.h" |
| 23 |
#include "commands.h" |
| 24 |
#include "variable.h" |
| 25 |
#include "rule.h" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
const gmk_floc **expanding_var = &reading_file; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#define VARIABLE_BUFFER_ZONE 5 |
| 44 |
|
| 45 |
static size_t variable_buffer_length; |
| 46 |
char *variable_buffer; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
char * |
| 56 |
variable_buffer_output (char *ptr, const char *string, size_t length) |
| 57 |
{ |
| 58 |
size_t newlen = length + (ptr - variable_buffer); |
| 59 |
|
| 60 |
if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length) |
| 61 |
{ |
| 62 |
size_t offset = ptr - variable_buffer; |
| 63 |
variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length |
| 64 |
? newlen + 100 |
| 65 |
: 2 * variable_buffer_length); |
| 66 |
variable_buffer = xrealloc (variable_buffer, variable_buffer_length); |
| 67 |
ptr = variable_buffer + offset; |
| 68 |
} |
| 69 |
|
| 70 |
memcpy (ptr, string, length); |
| 71 |
return ptr + length; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static char * |
| 77 |
initialize_variable_output (void) |
| 78 |
{ |
| 79 |
|
| 80 |
|
| 81 |
if (variable_buffer == 0) |
| 82 |
{ |
| 83 |
variable_buffer_length = 200; |
| 84 |
variable_buffer = xmalloc (variable_buffer_length); |
| 85 |
variable_buffer[0] = '\0'; |
| 86 |
} |
| 87 |
|
| 88 |
return variable_buffer; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
static char *allocated_variable_append (const struct variable *v); |
| 94 |
|
| 95 |
char * |
| 96 |
recursively_expand_for_file (struct variable *v, struct file *file) |
| 97 |
{ |
| 98 |
char *value; |
| 99 |
const gmk_floc *this_var; |
| 100 |
const gmk_floc **saved_varp; |
| 101 |
struct variable_set_list *save = 0; |
| 102 |
int set_reading = 0; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
saved_varp = expanding_var; |
| 107 |
if (v->fileinfo.filenm) |
| 108 |
{ |
| 109 |
this_var = &v->fileinfo; |
| 110 |
expanding_var = &this_var; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
if (!reading_file) |
| 115 |
{ |
| 116 |
set_reading = 1; |
| 117 |
reading_file = &v->fileinfo; |
| 118 |
} |
| 119 |
|
| 120 |
if (v->expanding) |
| 121 |
{ |
| 122 |
if (!v->exp_count) |
| 123 |
|
| 124 |
OS (fatal, *expanding_var, |
| 125 |
_("Recursive variable '%s' references itself (eventually)"), |
| 126 |
v->name); |
| 127 |
--v->exp_count; |
| 128 |
} |
| 129 |
|
| 130 |
if (file) |
| 131 |
{ |
| 132 |
save = current_variable_set_list; |
| 133 |
current_variable_set_list = file->variables; |
| 134 |
} |
| 135 |
|
| 136 |
v->expanding = 1; |
| 137 |
if (v->append) |
| 138 |
value = allocated_variable_append (v); |
| 139 |
else |
| 140 |
value = allocated_variable_expand (v->value); |
| 141 |
v->expanding = 0; |
| 142 |
|
| 143 |
if (set_reading) |
| 144 |
reading_file = 0; |
| 145 |
|
| 146 |
if (file) |
| 147 |
current_variable_set_list = save; |
| 148 |
|
| 149 |
expanding_var = saved_varp; |
| 150 |
|
| 151 |
return value; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
#ifdef __GNUC__ |
| 157 |
__inline |
| 158 |
#endif |
| 159 |
static char * |
| 160 |
reference_variable (char *o, const char *name, size_t length) |
| 161 |
{ |
| 162 |
struct variable *v; |
| 163 |
char *value; |
| 164 |
|
| 165 |
v = lookup_variable (name, length); |
| 166 |
|
| 167 |
if (v == 0) |
| 168 |
warn_undefined (name, length); |
| 169 |
|
| 170 |
|
| 171 |
if (v == 0 || (*v->value == '\0' && !v->append)) |
| 172 |
return o; |
| 173 |
|
| 174 |
value = (v->recursive ? recursively_expand (v) : v->value); |
| 175 |
|
| 176 |
o = variable_buffer_output (o, value, strlen (value)); |
| 177 |
|
| 178 |
if (v->recursive) |
| 179 |
free (value); |
| 180 |
|
| 181 |
return o; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
char * |
| 194 |
variable_expand_string (char *line, const char *string, size_t length) |
| 195 |
{ |
| 196 |
struct variable *v; |
| 197 |
const char *p, *p1; |
| 198 |
char *save; |
| 199 |
char *o; |
| 200 |
size_t line_offset; |
| 201 |
|
| 202 |
if (!line) |
| 203 |
line = initialize_variable_output (); |
| 204 |
o = line; |
| 205 |
line_offset = line - variable_buffer; |
| 206 |
|
| 207 |
if (length == 0) |
| 208 |
{ |
| 209 |
variable_buffer_output (o, "", 1); |
| 210 |
return (variable_buffer); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
save = length == SIZE_MAX ? xstrdup (string) : xstrndup (string, length); |
| 217 |
p = save; |
| 218 |
|
| 219 |
while (1) |
| 220 |
{ |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
p1 = strchr (p, '$'); |
| 226 |
|
| 227 |
o = variable_buffer_output (o, p, p1 != 0 ? (size_t) (p1 - p) : strlen (p) + 1); |
| 228 |
|
| 229 |
if (p1 == 0) |
| 230 |
break; |
| 231 |
p = p1 + 1; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
switch (*p) |
| 236 |
{ |
| 237 |
case '$': |
| 238 |
case '\0': |
| 239 |
|
| 240 |
|
| 241 |
o = variable_buffer_output (o, p1, 1); |
| 242 |
break; |
| 243 |
|
| 244 |
case '(': |
| 245 |
case '{': |
| 246 |
|
| 247 |
{ |
| 248 |
char openparen = *p; |
| 249 |
char closeparen = (openparen == '(') ? ')' : '}'; |
| 250 |
const char *begp; |
| 251 |
const char *beg = p + 1; |
| 252 |
char *op; |
| 253 |
char *abeg = NULL; |
| 254 |
const char *end, *colon; |
| 255 |
|
| 256 |
op = o; |
| 257 |
begp = p; |
| 258 |
if (handle_function (&op, &begp)) |
| 259 |
{ |
| 260 |
o = op; |
| 261 |
p = begp; |
| 262 |
break; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
end = strchr (beg, closeparen); |
| 269 |
if (end == 0) |
| 270 |
|
| 271 |
O (fatal, *expanding_var, _("unterminated variable reference")); |
| 272 |
p1 = lindex (beg, end, '$'); |
| 273 |
if (p1 != 0) |
| 274 |
{ |
| 275 |
|
| 276 |
|
| 277 |
int count = 0; |
| 278 |
for (p = beg; *p != '\0'; ++p) |
| 279 |
{ |
| 280 |
if (*p == openparen) |
| 281 |
++count; |
| 282 |
else if (*p == closeparen && --count < 0) |
| 283 |
break; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
if (count < 0) |
| 289 |
{ |
| 290 |
abeg = expand_argument (beg, p); |
| 291 |
beg = abeg; |
| 292 |
end = strchr (beg, '\0'); |
| 293 |
} |
| 294 |
} |
| 295 |
else |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
p = end; |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
colon = lindex (beg, end, ':'); |
| 306 |
if (colon) |
| 307 |
{ |
| 308 |
|
| 309 |
const char *subst_beg = colon + 1; |
| 310 |
const char *subst_end = lindex (subst_beg, end, '='); |
| 311 |
if (subst_end == 0) |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
colon = 0; |
| 316 |
else |
| 317 |
{ |
| 318 |
const char *replace_beg = subst_end + 1; |
| 319 |
const char *replace_end = end; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
v = lookup_variable (beg, colon - beg); |
| 324 |
if (v == 0) |
| 325 |
warn_undefined (beg, colon - beg); |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
if (v != 0 && *v->value != '\0') |
| 330 |
{ |
| 331 |
char *pattern, *replace, *ppercent, *rpercent; |
| 332 |
char *value = (v->recursive |
| 333 |
? recursively_expand (v) |
| 334 |
: v->value); |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
pattern = alloca (subst_end - subst_beg + 2); |
| 340 |
*(pattern++) = '%'; |
| 341 |
memcpy (pattern, subst_beg, subst_end - subst_beg); |
| 342 |
pattern[subst_end - subst_beg] = '\0'; |
| 343 |
|
| 344 |
replace = alloca (replace_end - replace_beg + 2); |
| 345 |
*(replace++) = '%'; |
| 346 |
memcpy (replace, replace_beg, |
| 347 |
replace_end - replace_beg); |
| 348 |
replace[replace_end - replace_beg] = '\0'; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
ppercent = find_percent (pattern); |
| 353 |
if (ppercent) |
| 354 |
{ |
| 355 |
++ppercent; |
| 356 |
rpercent = find_percent (replace); |
| 357 |
if (rpercent) |
| 358 |
++rpercent; |
| 359 |
} |
| 360 |
else |
| 361 |
{ |
| 362 |
ppercent = pattern; |
| 363 |
rpercent = replace; |
| 364 |
--pattern; |
| 365 |
--replace; |
| 366 |
} |
| 367 |
|
| 368 |
o = patsubst_expand_pat (o, value, pattern, replace, |
| 369 |
ppercent, rpercent); |
| 370 |
|
| 371 |
if (v->recursive) |
| 372 |
free (value); |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
if (colon == 0) |
| 378 |
|
| 379 |
|
| 380 |
o = reference_variable (o, beg, end - beg); |
| 381 |
|
| 382 |
free (abeg); |
| 383 |
} |
| 384 |
break; |
| 385 |
|
| 386 |
default: |
| 387 |
if (ISSPACE (p[-1])) |
| 388 |
break; |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
o = reference_variable (o, p, 1); |
| 393 |
|
| 394 |
break; |
| 395 |
} |
| 396 |
|
| 397 |
if (*p == '\0') |
| 398 |
break; |
| 399 |
|
| 400 |
++p; |
| 401 |
} |
| 402 |
|
| 403 |
free (save); |
| 404 |
|
| 405 |
variable_buffer_output (o, "", 1); |
| 406 |
return (variable_buffer + line_offset); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
char * |
| 415 |
variable_expand (const char *line) |
| 416 |
{ |
| 417 |
return variable_expand_string (NULL, line, SIZE_MAX); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
char * |
| 427 |
expand_argument (const char *str, const char *end) |
| 428 |
{ |
| 429 |
char *tmp, *alloc = NULL; |
| 430 |
char *r; |
| 431 |
|
| 432 |
if (str == end) |
| 433 |
return xstrdup (""); |
| 434 |
|
| 435 |
if (!end || *end == '\0') |
| 436 |
return allocated_variable_expand (str); |
| 437 |
|
| 438 |
if (end - str + 1 > 1000) |
| 439 |
tmp = alloc = xmalloc (end - str + 1); |
| 440 |
else |
| 441 |
tmp = alloca (end - str + 1); |
| 442 |
|
| 443 |
memcpy (tmp, str, end - str); |
| 444 |
tmp[end - str] = '\0'; |
| 445 |
|
| 446 |
r = allocated_variable_expand (tmp); |
| 447 |
|
| 448 |
free (alloc); |
| 449 |
|
| 450 |
return r; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
char * |
| 457 |
variable_expand_for_file (const char *line, struct file *file) |
| 458 |
{ |
| 459 |
char *result; |
| 460 |
struct variable_set_list *savev; |
| 461 |
const gmk_floc *savef; |
| 462 |
|
| 463 |
if (file == 0) |
| 464 |
return variable_expand (line); |
| 465 |
|
| 466 |
savev = current_variable_set_list; |
| 467 |
current_variable_set_list = file->variables; |
| 468 |
|
| 469 |
savef = reading_file; |
| 470 |
if (file->cmds && file->cmds->fileinfo.filenm) |
| 471 |
reading_file = &file->cmds->fileinfo; |
| 472 |
else |
| 473 |
reading_file = 0; |
| 474 |
|
| 475 |
result = variable_expand (line); |
| 476 |
|
| 477 |
current_variable_set_list = savev; |
| 478 |
reading_file = savef; |
| 479 |
|
| 480 |
return result; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
char * |
| 485 |
variable_expand_set (char *psz_line, variable_set_list_t *p_file_vars) |
| 486 |
{ |
| 487 |
char *psz_result; |
| 488 |
variable_set_list_t *p_vars_save; |
| 489 |
|
| 490 |
p_vars_save = current_variable_set_list; |
| 491 |
if (p_file_vars) |
| 492 |
current_variable_set_list = p_file_vars; |
| 493 |
psz_result = variable_expand (psz_line); |
| 494 |
current_variable_set_list = p_vars_save; |
| 495 |
|
| 496 |
return psz_result; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
static char * |
| 504 |
variable_append (const char *name, size_t length, |
| 505 |
const struct variable_set_list *set, int local) |
| 506 |
{ |
| 507 |
const struct variable *v; |
| 508 |
char *buf = 0; |
| 509 |
int nextlocal; |
| 510 |
|
| 511 |
|
| 512 |
if (!set) |
| 513 |
return initialize_variable_output (); |
| 514 |
|
| 515 |
|
| 516 |
nextlocal = local && set->next_is_parent == 0; |
| 517 |
|
| 518 |
|
| 519 |
v = lookup_variable_in_set (name, length, set->set); |
| 520 |
|
| 521 |
|
| 522 |
if (!v || (!local && v->private_var)) |
| 523 |
return variable_append (name, length, set->next, nextlocal); |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
if (v->append) |
| 528 |
buf = variable_append (name, length, set->next, nextlocal); |
| 529 |
else |
| 530 |
buf = initialize_variable_output (); |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
if (buf > variable_buffer) |
| 535 |
buf = variable_buffer_output (buf, " ", 1); |
| 536 |
|
| 537 |
|
| 538 |
if (! v->recursive) |
| 539 |
return variable_buffer_output (buf, v->value, strlen (v->value)); |
| 540 |
|
| 541 |
buf = variable_expand_string (buf, v->value, strlen (v->value)); |
| 542 |
return (buf + strlen (buf)); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
static char * |
| 547 |
allocated_variable_append (const struct variable *v) |
| 548 |
{ |
| 549 |
char *val; |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
char *obuf = variable_buffer; |
| 554 |
size_t olen = variable_buffer_length; |
| 555 |
|
| 556 |
variable_buffer = 0; |
| 557 |
|
| 558 |
val = variable_append (v->name, strlen (v->name), |
| 559 |
current_variable_set_list, 1); |
| 560 |
variable_buffer_output (val, "", 1); |
| 561 |
val = variable_buffer; |
| 562 |
|
| 563 |
variable_buffer = obuf; |
| 564 |
variable_buffer_length = olen; |
| 565 |
|
| 566 |
return val; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
char * |
| 573 |
allocated_variable_expand_for_file (const char *line, struct file *file) |
| 574 |
{ |
| 575 |
char *value; |
| 576 |
|
| 577 |
char *obuf = variable_buffer; |
| 578 |
size_t olen = variable_buffer_length; |
| 579 |
|
| 580 |
variable_buffer = 0; |
| 581 |
|
| 582 |
value = variable_expand_for_file (line, file); |
| 583 |
|
| 584 |
variable_buffer = obuf; |
| 585 |
variable_buffer_length = olen; |
| 586 |
|
| 587 |
return value; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
void |
| 594 |
install_variable_buffer (char **bufp, size_t *lenp) |
| 595 |
{ |
| 596 |
*bufp = variable_buffer; |
| 597 |
*lenp = variable_buffer_length; |
| 598 |
|
| 599 |
variable_buffer = 0; |
| 600 |
initialize_variable_output (); |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
void |
| 607 |
restore_variable_buffer (char *buf, size_t len) |
| 608 |
{ |
| 609 |
free (variable_buffer); |
| 610 |
|
| 611 |
variable_buffer = buf; |
| 612 |
variable_buffer_length = len; |
| 613 |
} |