| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "makeint.h" |
| 18 |
#include "filedef.h" |
| 19 |
#include "variable.h" |
| 20 |
#ifdef WINDOWS32 |
| 21 |
#include "pathstuff.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
struct vpath |
| 28 |
{ |
| 29 |
struct vpath *next; |
| 30 |
const char *pattern; |
| 31 |
const char *percent; |
| 32 |
size_t patlen; |
| 33 |
const char **searchpath; |
| 34 |
size_t maxlen; |
| 35 |
}; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
static struct vpath *vpaths; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static struct vpath *general_vpath; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
static struct vpath *gpaths; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
void |
| 55 |
build_vpath_lists (void) |
| 56 |
{ |
| 57 |
struct vpath *new = 0; |
| 58 |
struct vpath *old, *nexto; |
| 59 |
char *p; |
| 60 |
|
| 61 |
|
| 62 |
for (old = vpaths; old != 0; old = nexto) |
| 63 |
{ |
| 64 |
nexto = old->next; |
| 65 |
old->next = new; |
| 66 |
new = old; |
| 67 |
} |
| 68 |
|
| 69 |
vpaths = new; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
{ |
| 76 |
|
| 77 |
int save = warn_undefined_variables_flag; |
| 78 |
warn_undefined_variables_flag = 0; |
| 79 |
|
| 80 |
p = variable_expand ("$(strip $(VPATH))"); |
| 81 |
|
| 82 |
warn_undefined_variables_flag = save; |
| 83 |
} |
| 84 |
|
| 85 |
if (*p != '\0') |
| 86 |
{ |
| 87 |
|
| 88 |
struct vpath *save_vpaths = vpaths; |
| 89 |
char gp[] = "%"; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
vpaths = 0; |
| 94 |
|
| 95 |
|
| 96 |
construct_vpath_list (gp, p); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
general_vpath = vpaths; |
| 101 |
vpaths = save_vpaths; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
{ |
| 109 |
|
| 110 |
int save = warn_undefined_variables_flag; |
| 111 |
warn_undefined_variables_flag = 0; |
| 112 |
|
| 113 |
p = variable_expand ("$(strip $(GPATH))"); |
| 114 |
|
| 115 |
warn_undefined_variables_flag = save; |
| 116 |
} |
| 117 |
|
| 118 |
if (*p != '\0') |
| 119 |
{ |
| 120 |
|
| 121 |
struct vpath *save_vpaths = vpaths; |
| 122 |
char gp[] = "%"; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
vpaths = 0; |
| 127 |
|
| 128 |
|
| 129 |
construct_vpath_list (gp, p); |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
gpaths = vpaths; |
| 134 |
vpaths = save_vpaths; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
void |
| 158 |
construct_vpath_list (char *pattern, char *dirpath) |
| 159 |
{ |
| 160 |
unsigned int elem; |
| 161 |
char *p; |
| 162 |
const char **vpath; |
| 163 |
size_t maxvpath; |
| 164 |
unsigned int maxelem; |
| 165 |
const char *percent = NULL; |
| 166 |
|
| 167 |
if (pattern != 0) |
| 168 |
percent = find_percent (pattern); |
| 169 |
|
| 170 |
if (dirpath == 0) |
| 171 |
{ |
| 172 |
|
| 173 |
struct vpath *path, *lastpath; |
| 174 |
|
| 175 |
lastpath = 0; |
| 176 |
path = vpaths; |
| 177 |
while (path != 0) |
| 178 |
{ |
| 179 |
struct vpath *next = path->next; |
| 180 |
|
| 181 |
if (pattern == 0 |
| 182 |
|| (((percent == 0 && path->percent == 0) |
| 183 |
|| (percent - pattern == path->percent - path->pattern)) |
| 184 |
&& streq (pattern, path->pattern))) |
| 185 |
{ |
| 186 |
|
| 187 |
if (lastpath == 0) |
| 188 |
vpaths = path->next; |
| 189 |
else |
| 190 |
lastpath->next = next; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
free ((void *)path->searchpath); |
| 195 |
free (path); |
| 196 |
} |
| 197 |
else |
| 198 |
lastpath = path; |
| 199 |
|
| 200 |
path = next; |
| 201 |
} |
| 202 |
|
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
#ifdef WINDOWS32 |
| 207 |
convert_vpath_to_windows32 (dirpath, ';'); |
| 208 |
#endif |
| 209 |
|
| 210 |
|
| 211 |
while (STOP_SET (*dirpath, MAP_BLANK|MAP_PATHSEP)) |
| 212 |
++dirpath; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
maxelem = 2; |
| 219 |
p = dirpath; |
| 220 |
while (*p != '\0') |
| 221 |
if (STOP_SET (*p++, MAP_BLANK|MAP_PATHSEP)) |
| 222 |
++maxelem; |
| 223 |
|
| 224 |
vpath = xmalloc (maxelem * sizeof (const char *)); |
| 225 |
maxvpath = 0; |
| 226 |
|
| 227 |
elem = 0; |
| 228 |
p = dirpath; |
| 229 |
while (*p != '\0') |
| 230 |
{ |
| 231 |
char *v; |
| 232 |
size_t len; |
| 233 |
|
| 234 |
|
| 235 |
v = p; |
| 236 |
while (*p != '\0' |
| 237 |
#if defined(HAVE_DOS_PATHS) && (PATH_SEPARATOR_CHAR == ':') |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
&& (*p != PATH_SEPARATOR_CHAR |
| 243 |
|| (p == v + 1 && (p[1] == '/' || p[1] == '\\'))) |
| 244 |
#else |
| 245 |
&& *p != PATH_SEPARATOR_CHAR |
| 246 |
#endif |
| 247 |
&& !ISBLANK (*p)) |
| 248 |
++p; |
| 249 |
|
| 250 |
len = p - v; |
| 251 |
|
| 252 |
|
| 253 |
#if defined(__MSDOS__) || defined(__EMX__) || defined(HAVE_DOS_PATHS) |
| 254 |
|
| 255 |
if (len > 3 || (len > 1 && v[1] != ':')) |
| 256 |
#endif |
| 257 |
if (len > 1 && p[-1] == '/') |
| 258 |
--len; |
| 259 |
|
| 260 |
|
| 261 |
if (len > 1 || *v != '.') |
| 262 |
{ |
| 263 |
vpath[elem++] = dir_name (strcache_add_len (v, len)); |
| 264 |
if (len > maxvpath) |
| 265 |
maxvpath = len; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
while (STOP_SET (*p, MAP_BLANK|MAP_PATHSEP)) |
| 270 |
++p; |
| 271 |
} |
| 272 |
|
| 273 |
if (elem > 0) |
| 274 |
{ |
| 275 |
struct vpath *path; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
if (elem < (maxelem - 1)) |
| 280 |
vpath = xrealloc (vpath, (elem+1) * sizeof (const char *)); |
| 281 |
|
| 282 |
|
| 283 |
vpath[elem] = NULL; |
| 284 |
|
| 285 |
|
| 286 |
path = xmalloc (sizeof (struct vpath)); |
| 287 |
path->searchpath = vpath; |
| 288 |
path->maxlen = maxvpath; |
| 289 |
path->next = vpaths; |
| 290 |
vpaths = path; |
| 291 |
|
| 292 |
|
| 293 |
path->pattern = strcache_add (pattern); |
| 294 |
path->patlen = strlen (pattern); |
| 295 |
path->percent = percent ? path->pattern + (percent - pattern) : 0; |
| 296 |
} |
| 297 |
else |
| 298 |
|
| 299 |
|
| 300 |
free ((void *)vpath); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
int |
| 307 |
gpath_search (const char *file, size_t len) |
| 308 |
{ |
| 309 |
if (gpaths && (len <= gpaths->maxlen)) |
| 310 |
{ |
| 311 |
const char **gp; |
| 312 |
for (gp = gpaths->searchpath; *gp != NULL; ++gp) |
| 313 |
if (strneq (*gp, file, len) && (*gp)[len] == '\0') |
| 314 |
return 1; |
| 315 |
} |
| 316 |
|
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
static const char * |
| 328 |
selective_vpath_search (struct vpath *path, const char *file, |
| 329 |
FILE_TIMESTAMP *mtime_ptr, unsigned int* path_index) |
| 330 |
{ |
| 331 |
int not_target; |
| 332 |
char *name; |
| 333 |
const char *n; |
| 334 |
const char *filename; |
| 335 |
const char **vpath = path->searchpath; |
| 336 |
size_t maxvpath = path->maxlen; |
| 337 |
unsigned int i; |
| 338 |
size_t flen, name_dplen; |
| 339 |
int exists = 0; |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
{ |
| 345 |
struct file *f = lookup_file (file); |
| 346 |
not_target = f == 0 || !f->is_target; |
| 347 |
} |
| 348 |
|
| 349 |
flen = strlen (file); |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
n = strrchr (file, '/'); |
| 356 |
#ifdef HAVE_DOS_PATHS |
| 357 |
|
| 358 |
{ |
| 359 |
const char *bslash = strrchr (file, '\\'); |
| 360 |
if (!n || bslash > n) |
| 361 |
n = bslash; |
| 362 |
} |
| 363 |
#endif |
| 364 |
name_dplen = n != 0 ? n - file : 0; |
| 365 |
filename = name_dplen > 0 ? n + 1 : file; |
| 366 |
if (name_dplen > 0) |
| 367 |
flen -= name_dplen + 1; |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
name = alloca (maxvpath + 1 + name_dplen + 1 + flen + 1); |
| 373 |
|
| 374 |
|
| 375 |
for (i = 0; vpath[i] != 0; ++i) |
| 376 |
{ |
| 377 |
int exists_in_cache = 0; |
| 378 |
char *p = name; |
| 379 |
size_t vlen = strlen (vpath[i]); |
| 380 |
|
| 381 |
|
| 382 |
memcpy (p, vpath[i], vlen); |
| 383 |
p += vlen; |
| 384 |
|
| 385 |
|
| 386 |
if (name_dplen > 0) |
| 387 |
{ |
| 388 |
#ifndef VMS |
| 389 |
*p++ = '/'; |
| 390 |
#else |
| 391 |
|
| 392 |
if ((*p != ':') && (*p != ']') && (*p != '>')) |
| 393 |
*p++ = '/'; |
| 394 |
#endif |
| 395 |
memcpy (p, file, name_dplen); |
| 396 |
p += name_dplen; |
| 397 |
} |
| 398 |
|
| 399 |
#ifdef HAVE_DOS_PATHS |
| 400 |
|
| 401 |
if (p != name && p[-1] == '\\' ) |
| 402 |
p[-1] = '/'; |
| 403 |
#endif |
| 404 |
|
| 405 |
#ifndef VMS |
| 406 |
if (p != name && p[-1] != '/') |
| 407 |
{ |
| 408 |
*p = '/'; |
| 409 |
memcpy (p + 1, filename, flen + 1); |
| 410 |
} |
| 411 |
else |
| 412 |
#else |
| 413 |
|
| 414 |
if (p != name && p[-1] != '/' && p[-1] != ':' && |
| 415 |
p[-1] != '>' && p[-1] != ']') |
| 416 |
{ |
| 417 |
*p = '/'; |
| 418 |
memcpy (p + 1, filename, flen + 1); |
| 419 |
} |
| 420 |
else |
| 421 |
#endif |
| 422 |
memcpy (p, filename, flen + 1); |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
{ |
| 446 |
struct file *f = lookup_file (name); |
| 447 |
if (f != 0) |
| 448 |
{ |
| 449 |
exists = not_target || f->is_target; |
| 450 |
if (exists && mtime_ptr |
| 451 |
&& (f->last_mtime == OLD_MTIME || f->last_mtime == NEW_MTIME)) |
| 452 |
{ |
| 453 |
*mtime_ptr = f->last_mtime; |
| 454 |
mtime_ptr = 0; |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
if (!exists) |
| 460 |
{ |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
#ifdef VMS |
| 465 |
|
| 466 |
if (*p != '/') |
| 467 |
exists_in_cache = exists = dir_file_exists_p (vpath[i], filename); |
| 468 |
else |
| 469 |
#endif |
| 470 |
{ |
| 471 |
|
| 472 |
|
| 473 |
*p = '\0'; |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
exists_in_cache = exists = dir_file_exists_p (name, filename); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
if (exists) |
| 482 |
{ |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
struct stat st; |
| 490 |
|
| 491 |
#ifndef VMS |
| 492 |
|
| 493 |
*p = '/'; |
| 494 |
#else |
| 495 |
|
| 496 |
if (*p == 0) |
| 497 |
*p = '/'; |
| 498 |
#endif |
| 499 |
|
| 500 |
if (exists_in_cache) |
| 501 |
{ |
| 502 |
int e; |
| 503 |
|
| 504 |
EINTRLOOP (e, stat (name, &st)); |
| 505 |
if (e != 0) |
| 506 |
{ |
| 507 |
exists = 0; |
| 508 |
continue; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
if (mtime_ptr != 0) |
| 513 |
{ |
| 514 |
*mtime_ptr = FILE_TIMESTAMP_STAT_MODTIME (name, st); |
| 515 |
mtime_ptr = 0; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
if (mtime_ptr != 0) |
| 523 |
*mtime_ptr = UNKNOWN_MTIME; |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
if (path_index) |
| 528 |
*path_index = i; |
| 529 |
|
| 530 |
return strcache_add_len (name, (p + 1 - name) + flen); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
return 0; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
const char * |
| 545 |
vpath_search (const char *file, FILE_TIMESTAMP *mtime_ptr, |
| 546 |
unsigned int* vpath_index, unsigned int* path_index) |
| 547 |
{ |
| 548 |
struct vpath *v; |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
if (file[0] == '/' |
| 554 |
#ifdef HAVE_DOS_PATHS |
| 555 |
|| file[0] == '\\' || file[1] == ':' |
| 556 |
#endif |
| 557 |
|| (vpaths == 0 && general_vpath == 0)) |
| 558 |
return 0; |
| 559 |
|
| 560 |
if (vpath_index) |
| 561 |
{ |
| 562 |
*vpath_index = 0; |
| 563 |
*path_index = 0; |
| 564 |
} |
| 565 |
|
| 566 |
for (v = vpaths; v != 0; v = v->next) |
| 567 |
{ |
| 568 |
if (pattern_matches (v->pattern, v->percent, file)) |
| 569 |
{ |
| 570 |
const char *p = selective_vpath_search ( |
| 571 |
v, file, mtime_ptr, path_index); |
| 572 |
if (p) |
| 573 |
return p; |
| 574 |
} |
| 575 |
|
| 576 |
if (vpath_index) |
| 577 |
++*vpath_index; |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
if (general_vpath != 0) |
| 582 |
{ |
| 583 |
const char *p = selective_vpath_search ( |
| 584 |
general_vpath, file, mtime_ptr, path_index); |
| 585 |
if (p) |
| 586 |
return p; |
| 587 |
} |
| 588 |
|
| 589 |
return 0; |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
void |
| 598 |
print_vpath_data_base (void) |
| 599 |
{ |
| 600 |
unsigned int nvpaths; |
| 601 |
struct vpath *v; |
| 602 |
|
| 603 |
puts (_("\n# VPATH Search Paths\n")); |
| 604 |
|
| 605 |
nvpaths = 0; |
| 606 |
for (v = vpaths; v != 0; v = v->next) |
| 607 |
{ |
| 608 |
unsigned int i; |
| 609 |
|
| 610 |
++nvpaths; |
| 611 |
|
| 612 |
printf ("vpath %s ", v->pattern); |
| 613 |
|
| 614 |
for (i = 0; v->searchpath[i] != 0; ++i) |
| 615 |
printf ("%s%c", v->searchpath[i], |
| 616 |
v->searchpath[i + 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR); |
| 617 |
} |
| 618 |
|
| 619 |
if (vpaths == 0) |
| 620 |
puts (_("# No 'vpath' search paths.")); |
| 621 |
else |
| 622 |
printf (_("\n# %u 'vpath' search paths.\n"), nvpaths); |
| 623 |
|
| 624 |
if (general_vpath == 0) |
| 625 |
puts (_("\n# No general ('VPATH' variable) search path.")); |
| 626 |
else |
| 627 |
{ |
| 628 |
const char **path = general_vpath->searchpath; |
| 629 |
unsigned int i; |
| 630 |
|
| 631 |
fputs (_("\n# General ('VPATH' variable) search path:\n# "), stdout); |
| 632 |
|
| 633 |
for (i = 0; path[i] != 0; ++i) |
| 634 |
printf ("%s%c", path[i], |
| 635 |
path[i + 1] == 0 ? '\n' : PATH_SEPARATOR_CHAR); |
| 636 |
} |
| 637 |
} |