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