Changeset 903 in kBuild for trunk/src/gmakenew/vpath.c
- Timestamp:
- May 23, 2007 5:31:19 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/gmakenew/vpath.c
r527 r903 30 30 { 31 31 struct vpath *next; /* Pointer to next struct in the linked list. */ 32 c har *pattern;/* The pattern to match. */33 c har *percent;/* Pointer into `pattern' where the `%' is. */32 const char *pattern;/* The pattern to match. */ 33 const char *percent;/* Pointer into `pattern' where the `%' is. */ 34 34 unsigned int patlen;/* Length of the pattern. */ 35 c har **searchpath;/* Null-terminated list of directories. */35 const char **searchpath; /* Null-terminated list of directories. */ 36 36 unsigned int maxlen;/* Maximum length of any entry in the list. */ 37 37 }; … … 50 50 51 51 52 static int selective_vpath_search PARAMS ((struct vpath *path, char **file, FILE_TIMESTAMP *mtime_ptr)); 53 54 /* Reverse the chain of selective VPATH lists so they 55 will be searched in the order given in the makefiles 56 and construct the list from the VPATH variable. */ 52 53 /* Reverse the chain of selective VPATH lists so they will be searched in the 54 order given in the makefiles and construct the list from the VPATH 55 variable. */ 57 56 58 57 void … … 97 96 /* Save the list of vpaths. */ 98 97 struct vpath *save_vpaths = vpaths; 98 char gp[] = "%"; 99 99 100 100 /* Empty `vpaths' so the new one will have no next, and `vpaths' … … 103 103 104 104 /* Parse P. */ 105 construct_vpath_list ( "%", p);105 construct_vpath_list (gp, p); 106 106 107 107 /* Store the created path as the general path, … … 133 133 /* Save the list of vpaths. */ 134 134 struct vpath *save_vpaths = vpaths; 135 char gp[] = "%"; 135 136 136 137 /* Empty `vpaths' so the new one will have no next, and `vpaths' … … 139 140 140 141 /* Parse P. */ 141 construct_vpath_list ( "%", p);142 construct_vpath_list (gp, p); 142 143 143 144 /* Store the created path as the GPATH, … … 171 172 construct_vpath_list (char *pattern, char *dirpath) 172 173 { 173 registerunsigned int elem;174 registerchar *p;175 registerchar **vpath;176 registerunsigned int maxvpath;174 unsigned int elem; 175 char *p; 176 const char **vpath; 177 unsigned int maxvpath; 177 178 unsigned int maxelem; 178 c har *percent = NULL;179 const char *percent = NULL; 179 180 180 181 if (pattern != 0) 181 { 182 pattern = xstrdup (pattern); 183 percent = find_percent (pattern); 184 } 182 percent = find_percent (pattern); 185 183 186 184 if (dirpath == 0) 187 185 { 188 186 /* Remove matching listings. */ 189 registerstruct vpath *path, *lastpath;187 struct vpath *path, *lastpath; 190 188 191 189 lastpath = 0; … … 207 205 208 206 /* Free its unused storage. */ 209 free (path->pattern); 210 free ((char *) path->searchpath); 211 free ((char *) path); 207 free (path->searchpath); 208 free (path); 212 209 } 213 210 else … … 217 214 } 218 215 219 if (pattern != 0)220 free (pattern);221 216 return; 222 217 } … … 225 220 convert_vpath_to_windows32(dirpath, ';'); 226 221 #endif 222 223 /* Skip over any initial separators and blanks. */ 224 while (*dirpath == PATH_SEPARATOR_CHAR || isblank ((unsigned char)*dirpath)) 225 ++dirpath; 227 226 228 227 /* Figure out the maximum number of VPATH entries and put it in … … 236 235 ++maxelem; 237 236 238 vpath = (char **) xmalloc (maxelem * sizeof (char *));237 vpath = xmalloc (maxelem * sizeof (const char *)); 239 238 maxvpath = 0; 240 239 241 /* Skip over any initial separators and blanks. */240 elem = 0; 242 241 p = dirpath; 243 while (*p == PATH_SEPARATOR_CHAR || isblank ((unsigned char)*p))244 ++p;245 246 elem = 0;247 242 while (*p != '\0') 248 243 { … … 266 261 --len; 267 262 263 /* Put the directory on the vpath list. */ 268 264 if (len > 1 || *v != '.') 269 265 { 270 v = savestring (v, len); 271 272 /* Verify that the directory actually exists. */ 273 274 if (dir_file_exists_p (v, "")) 275 { 276 /* It does. Put it in the list. */ 277 vpath[elem++] = dir_name (v); 278 free (v); 279 if (len > maxvpath) 280 maxvpath = len; 281 } 282 else 283 /* The directory does not exist. Omit from the list. */ 284 free (v); 266 vpath[elem++] = dir_name (strcache_add_len (v, len)); 267 if (len > maxvpath) 268 maxvpath = len; 285 269 } 286 270 … … 297 281 Usually this is maxelem - 1. If not, shrink down. */ 298 282 if (elem < (maxelem - 1)) 299 vpath = (char **) xrealloc ((char *) vpath, 300 (elem + 1) * sizeof (char *)); 283 vpath = xrealloc (vpath, (elem+1) * sizeof (const char *)); 301 284 302 285 /* Put the nil-pointer terminator on the end of the VPATH list. */ 303 vpath[elem] = 0;286 vpath[elem] = NULL; 304 287 305 288 /* Construct the vpath structure and put it into the linked list. */ 306 path = (struct vpath *)xmalloc (sizeof (struct vpath));289 path = xmalloc (sizeof (struct vpath)); 307 290 path->searchpath = vpath; 308 291 path->maxlen = maxvpath; … … 311 294 312 295 /* Set up the members. */ 313 path->pattern = pattern; 314 path->percent = percent; 296 path->pattern = strcache_add (pattern); 315 297 path->patlen = strlen (pattern); 298 path->percent = percent ? path->pattern + (percent - pattern) : 0; 316 299 } 317 300 else 318 { 319 /* There were no entries, so free whatever space we allocated. */ 320 free ((char *) vpath); 321 if (pattern != 0) 322 free (pattern); 323 } 301 /* There were no entries, so free whatever space we allocated. */ 302 free (vpath); 324 303 } 325 304 … … 329 308 330 309 int 331 gpath_search (c har *file, unsigned int len)310 gpath_search (const char *file, unsigned int len) 332 311 { 333 c har **gp;312 const char **gp; 334 313 335 314 if (gpaths && (len <= gpaths->maxlen)) … … 342 321 343 322 344 /* Search the VPATH list whose pattern matches *FILE for a directory 345 where the name pointed to by FILE exists. If it is found, we set *FILE to 346 the newly malloc'd name of the existing file, *MTIME_PTR (if MTIME_PTR is 347 not NULL) to its modtime (or zero if no stat call was done), and return 1. 348 Otherwise we return 0. */ 349 350 int 351 vpath_search (char **file, FILE_TIMESTAMP *mtime_ptr) 352 { 353 register struct vpath *v; 354 355 /* If there are no VPATH entries or FILENAME starts at the root, 356 there is nothing we can do. */ 357 358 if (**file == '/' 359 #ifdef HAVE_DOS_PATHS 360 || **file == '\\' 361 || (*file)[1] == ':' 362 #endif 363 || (vpaths == 0 && general_vpath == 0)) 364 return 0; 365 366 for (v = vpaths; v != 0; v = v->next) 367 if (pattern_matches (v->pattern, v->percent, *file)) 368 if (selective_vpath_search (v, file, mtime_ptr)) 369 return 1; 370 371 if (general_vpath != 0 372 && selective_vpath_search (general_vpath, file, mtime_ptr)) 373 return 1; 374 375 return 0; 376 } 377 378 379 /* Search the given VPATH list for a directory where the name pointed 380 to by FILE exists. If it is found, we set *FILE to the newly malloc'd 381 name of the existing file, *MTIME_PTR (if MTIME_PTR is not NULL) to 382 its modtime (or zero if no stat call was done), and we return 1. 383 Otherwise we return 0. */ 384 385 static int 386 selective_vpath_search (struct vpath *path, char **file, 323 324 /* Search the given VPATH list for a directory where the name pointed to by 325 FILE exists. If it is found, we return a cached name of the existing file 326 and set *MTIME_PTR (if MTIME_PTR is not NULL) to its modtime (or zero if no 327 stat call was done). Otherwise we return NULL. */ 328 329 static const char * 330 selective_vpath_search (struct vpath *path, const char *file, 387 331 FILE_TIMESTAMP *mtime_ptr) 388 332 { 389 333 int not_target; 390 char *name, *n; 391 char *filename; 392 register char **vpath = path->searchpath; 334 char *name; 335 const char *n; 336 const char *filename; 337 const char **vpath = path->searchpath; 393 338 unsigned int maxvpath = path->maxlen; 394 registerunsigned int i;339 unsigned int i; 395 340 unsigned int flen, vlen, name_dplen; 396 341 int exists = 0; … … 400 345 files that don't exist but are mentioned in a makefile. */ 401 346 { 402 struct file *f = lookup_file ( *file);347 struct file *f = lookup_file (file); 403 348 not_target = f == 0 || !f->is_target; 404 349 } 405 350 406 flen = strlen ( *file);351 flen = strlen (file); 407 352 408 353 /* Split *FILE into a directory prefix and a name-within-directory. 409 NAME_DPLEN gets the length of the prefix; FILENAME gets the 410 pointer tothe name-within-directory and FLEN is its length. */411 412 n = strrchr ( *file, '/');354 NAME_DPLEN gets the length of the prefix; FILENAME gets the pointer to 355 the name-within-directory and FLEN is its length. */ 356 357 n = strrchr (file, '/'); 413 358 #ifdef HAVE_DOS_PATHS 414 359 /* We need the rightmost slash or backslash. */ 415 360 { 416 c har *bslash = strrchr(*file, '\\');361 const char *bslash = strrchr(file, '\\'); 417 362 if (!n || bslash > n) 418 363 n = bslash; 419 364 } 420 365 #endif 421 name_dplen = n != 0 ? n - *file : 0;422 filename = name_dplen > 0 ? n + 1 : *file;366 name_dplen = n != 0 ? n - file : 0; 367 filename = name_dplen > 0 ? n + 1 : file; 423 368 if (name_dplen > 0) 424 369 flen -= name_dplen + 1; 425 370 426 /* Allocate enough space for the biggest VPATH entry, 427 a slash, the directory prefix that came with *FILE, 428 another slash (although this one may not always be 429 necessary), the filename, and a null terminator. */ 430 name = (char *) xmalloc (maxvpath + 1 + name_dplen + 1 + flen + 1); 371 /* Get enough space for the biggest VPATH entry, a slash, the directory 372 prefix that came with FILE, another slash (although this one may not 373 always be necessary), the filename, and a null terminator. */ 374 name = alloca (maxvpath + 1 + name_dplen + 1 + flen + 1); 431 375 432 376 /* Try each VPATH entry. */ … … 434 378 { 435 379 int exists_in_cache = 0; 436 437 n = name; 438 439 /* Put the next VPATH entry into NAME at N and increment N past it. */ 380 char *p; 381 382 p = name; 383 384 /* Put the next VPATH entry into NAME at P and increment P past it. */ 440 385 vlen = strlen (vpath[i]); 441 bcopy (vpath[i], n, vlen);442 n+= vlen;386 memcpy (p, vpath[i], vlen); 387 p += vlen; 443 388 444 389 /* Add the directory prefix already in *FILE. */ … … 446 391 { 447 392 #ifndef VMS 448 * n++ = '/';449 #endif 450 bcopy (*file, n, name_dplen);451 n+= name_dplen;393 *p++ = '/'; 394 #endif 395 memcpy (p, file, name_dplen); 396 p += name_dplen; 452 397 } 453 398 454 399 #ifdef HAVE_DOS_PATHS 455 400 /* Cause the next if to treat backslash and slash alike. */ 456 if ( n != name && n[-1] == '\\' )457 n[-1] = '/';401 if (p != name && p[-1] == '\\' ) 402 p[-1] = '/'; 458 403 #endif 459 404 /* Now add the name-within-directory at the end of NAME. */ 460 405 #ifndef VMS 461 if ( n != name && n[-1] != '/')406 if (p != name && p[-1] != '/') 462 407 { 463 * n= '/';464 bcopy (filename, n + 1, flen + 1);408 *p = '/'; 409 memcpy (p + 1, filename, flen + 1); 465 410 } 466 411 else 467 412 #endif 468 bcopy (filename, n, flen + 1);413 memcpy (p, filename, flen + 1); 469 414 470 415 /* Check if the file is mentioned in a makefile. If *FILE is not … … 513 458 /* Clobber a null into the name at the last slash. 514 459 Now NAME is the name of the directory to look in. */ 515 * n= '\0';460 *p = '\0'; 516 461 517 462 /* We know the directory is in the hash table now because either … … 534 479 #ifndef VMS 535 480 /* Put the slash back in NAME. */ 536 * n= '/';481 *p = '/'; 537 482 #endif 538 483 … … 557 502 558 503 /* We have found a file. 559 Store the name we found into *FILE for the caller. */ 560 561 *file = savestring (name, (n + 1 - name) + flen); 562 563 /* If we get here and mtime_ptr hasn't been set, record 504 If we get here and mtime_ptr hasn't been set, record 564 505 UNKNOWN_MTIME to indicate this. */ 565 506 if (mtime_ptr != 0) 566 507 *mtime_ptr = UNKNOWN_MTIME; 567 508 568 free (name); 569 return 1; 509 /* Store the name we found and return it. */ 510 511 return strcache_add_len (name, (p + 1 - name) + flen); 570 512 } 571 513 } 572 514 573 free (name); 515 return 0; 516 } 517 518 519 /* Search the VPATH list whose pattern matches FILE for a directory where FILE 520 exists. If it is found, return the cached name of an existing file, and 521 set *MTIME_PTR (if MTIME_PTR is not NULL) to its modtime (or zero if no 522 stat call was done). Otherwise we return 0. */ 523 524 const char * 525 vpath_search (const char *file, FILE_TIMESTAMP *mtime_ptr) 526 { 527 struct vpath *v; 528 529 /* If there are no VPATH entries or FILENAME starts at the root, 530 there is nothing we can do. */ 531 532 if (file[0] == '/' 533 #ifdef HAVE_DOS_PATHS 534 || file[0] == '\\' || file[1] == ':' 535 #endif 536 || (vpaths == 0 && general_vpath == 0)) 537 return 0; 538 539 for (v = vpaths; v != 0; v = v->next) 540 if (pattern_matches (v->pattern, v->percent, file)) 541 { 542 const char *p = selective_vpath_search (v, file, mtime_ptr); 543 if (p) 544 return p; 545 } 546 547 if (general_vpath != 0) 548 { 549 const char *p = selective_vpath_search (general_vpath, file, mtime_ptr); 550 if (p) 551 return p; 552 } 553 574 554 return 0; 575 555 } … … 581 561 print_vpath_data_base (void) 582 562 { 583 registerunsigned int nvpaths;584 registerstruct vpath *v;563 unsigned int nvpaths; 564 struct vpath *v; 585 565 586 566 puts (_("\n# VPATH Search Paths\n")); … … 609 589 else 610 590 { 611 registerchar **path = general_vpath->searchpath;612 registerunsigned int i;591 const char **path = general_vpath->searchpath; 592 unsigned int i; 613 593 614 594 fputs (_("\n# General (`VPATH' variable) search path:\n# "), stdout);
Note:
See TracChangeset
for help on using the changeset viewer.