| 1 |
/* db.c: an external database to avoid filesystem lookups. |
| 2 |
|
| 3 |
Copyright 1994, 1995, 1996, 1997, 2008, 2009, 2011, 2012, 2014, 2016, |
| 4 |
2017 Karl Berry. |
| 5 |
Copyright 1997-2005 Olaf Weber. |
| 6 |
|
| 7 |
This library is free software; you can redistribute it and/or |
| 8 |
modify it under the terms of the GNU Lesser General Public |
| 9 |
License as published by the Free Software Foundation; either |
| 10 |
version 2.1 of the License, or (at your option) any later version. |
| 11 |
|
| 12 |
This library is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
Lesser General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU Lesser General Public License |
| 18 |
along with this library; if not, see <http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include <kpathsea/config.h> |
| 21 |
#include <kpathsea/absolute.h> |
| 22 |
#include <kpathsea/c-stat.h> |
| 23 |
#include <kpathsea/c-fopen.h> |
| 24 |
#include <kpathsea/c-pathch.h> |
| 25 |
#include <kpathsea/db.h> |
| 26 |
#include <kpathsea/hash.h> |
| 27 |
#include <kpathsea/line.h> |
| 28 |
#include <kpathsea/pathsearch.h> |
| 29 |
#include <kpathsea/readable.h> |
| 30 |
#include <kpathsea/str-list.h> |
| 31 |
#include <kpathsea/tex-file.h> |
| 32 |
#include <kpathsea/variable.h> |
| 33 |
|
| 34 |
#ifndef DB_HASH_SIZE |
| 35 |
/* Based on the size of 2014 texmf-dist/ls-R, about 130,000 entries. */ |
| 36 |
#define DB_HASH_SIZE 64007 |
| 37 |
#endif |
| 38 |
#ifndef DB_NAME |
| 39 |
#define DB_NAME "ls-R" |
| 40 |
#endif |
| 41 |
#ifndef DB_NAME_LC |
| 42 |
#define DB_NAME_LC "ls-r" |
| 43 |
#endif |
| 44 |
|
| 45 |
/* In the loop in init() below where we check for DB_NAME_LC and DB_NAME |
| 46 |
being the same file, it's convenient to ignore the first of a pair. |
| 47 |
So put the canonical name second. Sure wish I hadn't used a capital |
| 48 |
letter in the name in the first place. */ |
| 49 |
/* We use non-const strings initialized with string constants in order |
| 50 |
to avoid some compiler warnings. */ |
| 51 |
static char db_name_lc[] = DB_NAME_LC; |
| 52 |
static char db_name[] = DB_NAME; |
| 53 |
static string db_names[] = { |
| 54 |
db_name_lc, |
| 55 |
db_name, |
| 56 |
NULL |
| 57 |
}; |
| 58 |
|
| 59 |
#ifndef ALIAS_NAME |
| 60 |
#define ALIAS_NAME "aliases" |
| 61 |
#endif |
| 62 |
#ifndef ALIAS_HASH_SIZE |
| 63 |
#define ALIAS_HASH_SIZE 1009 |
| 64 |
#endif |
| 65 |
|
| 66 |
|
| 67 |
/* If DIRNAME contains any element beginning with a `.' (that is more |
| 68 |
than just `./'), return true. This is to allow ``hidden'' |
| 69 |
directories -- ones that don't get searched. */ |
| 70 |
|
| 71 |
static boolean |
| 72 |
ignore_dir_p (const_string dirname) |
| 73 |
{ |
| 74 |
const_string dot_pos = dirname; |
| 75 |
|
| 76 |
while ((dot_pos = strchr (dot_pos + 1, '.'))) { |
| 77 |
/* If / before and no / after, skip it. */ |
| 78 |
if (IS_DIR_SEP_CH (dot_pos[-1]) && dot_pos[1] && !IS_DIR_SEP_CH (dot_pos[1])) |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
/* If no DB_FILENAME, return false (maybe they aren't using this feature). |
| 86 |
Otherwise, add entries from DB_FILENAME to TABLE, and return true. */ |
| 87 |
|
| 88 |
static boolean |
| 89 |
db_build (kpathsea kpse, hash_table_type *table, const_string db_filename) |
| 90 |
{ |
| 91 |
string line; |
| 92 |
unsigned dir_count = 0, file_count = 0, ignore_dir_count = 0; |
| 93 |
unsigned len = strlen (db_filename) - sizeof (DB_NAME) + 1; /* Keep the /. */ |
| 94 |
string top_dir = (string)xmalloc (len + 1); |
| 95 |
string cur_dir = NULL; /* First thing in ls-R might be a filename. */ |
| 96 |
FILE *db_file = fopen (db_filename, FOPEN_R_MODE); |
| 97 |
#if defined(MONOCASE_FILENAMES) |
| 98 |
string pp; |
| 99 |
#endif /* MONOCASE_FILENAMES */ |
| 100 |
|
| 101 |
strncpy (top_dir, db_filename, len); |
| 102 |
top_dir[len] = 0; |
| 103 |
|
| 104 |
if (db_file) { |
| 105 |
while ((line = read_line (db_file)) != NULL) { |
| 106 |
len = strlen (line); |
| 107 |
|
| 108 |
#if defined(MONOCASE_FILENAMES) |
| 109 |
for (pp = line; *pp; pp++) { |
| 110 |
#if defined(_WIN32) |
| 111 |
if (kpathsea_IS_KANJI(kpse, pp)) |
| 112 |
pp++; |
| 113 |
else |
| 114 |
#endif /* _WIN32 */ |
| 115 |
*pp = TRANSFORM(*pp); |
| 116 |
} |
| 117 |
#endif /* MONOCASE_FILENAMES */ |
| 118 |
|
| 119 |
/* A line like `/foo:' = new dir foo. Allow both absolute (/...) |
| 120 |
and explicitly relative (./...) names here. It's a kludge to |
| 121 |
pass in the directory name with the trailing : still attached, |
| 122 |
but it doesn't actually hurt. */ |
| 123 |
if (len > 0 && line[len - 1] == ':' |
| 124 |
&& kpathsea_absolute_p (kpse, line, true)) { |
| 125 |
/* New directory line. */ |
| 126 |
if (!ignore_dir_p (line)) { |
| 127 |
/* If they gave a relative name, prepend full directory name now. */ |
| 128 |
line[len - 1] = DIR_SEP; |
| 129 |
/* Skip over leading `./', it confuses `match' and is just a |
| 130 |
waste of space, anyway. This will lose on `../', but `match' |
| 131 |
won't work there, either, so it doesn't matter. */ |
| 132 |
cur_dir = *line == '.' ? concat (top_dir, line + 2) : xstrdup (line); |
| 133 |
dir_count++; |
| 134 |
} else { |
| 135 |
cur_dir = NULL; |
| 136 |
ignore_dir_count++; |
| 137 |
} |
| 138 |
|
| 139 |
/* Ignore blank, `.' and `..' lines. */ |
| 140 |
} else if (*line != 0 && cur_dir /* a file line? */ |
| 141 |
&& !(*line == '.' |
| 142 |
&& (line[1] == 0 || (line[1] == '.' && line[2] == 0)))) |
| 143 |
{ |
| 144 |
/* Make a new hash table entry with a key of `line' and a data |
| 145 |
of `cur_dir'. An already-existing identical key is ok, since |
| 146 |
a file named `foo' can be in more than one directory. Share |
| 147 |
`cur_dir' among all its files (and hence never free it). |
| 148 |
|
| 149 |
Note that we assume that all names in the ls-R file have already |
| 150 |
been case-smashed to lowercase where appropriate. |
| 151 |
*/ |
| 152 |
hash_insert_normalized (table, xstrdup (line), cur_dir); |
| 153 |
file_count++; |
| 154 |
|
| 155 |
} /* else ignore blank lines or top-level files |
| 156 |
or files in ignored directories*/ |
| 157 |
|
| 158 |
free (line); |
| 159 |
} |
| 160 |
|
| 161 |
xfclose (db_file, db_filename); |
| 162 |
|
| 163 |
if (file_count == 0) { |
| 164 |
WARNING1 ("kpathsea: %s: No usable entries in ls-R", db_filename); |
| 165 |
WARNING ("kpathsea: See the manual for how to generate ls-R"); |
| 166 |
db_file = NULL; |
| 167 |
} else { |
| 168 |
str_list_add (&(kpse->db_dir_list), xstrdup (top_dir)); |
| 169 |
} |
| 170 |
|
| 171 |
#ifdef KPSE_DEBUG |
| 172 |
if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH)) { |
| 173 |
/* Don't make this a debugging bit, since the output is so |
| 174 |
voluminous, and being able to specify -1 is too useful. |
| 175 |
Instead, let people who want it run the program under |
| 176 |
a debugger and change the variable that way. */ |
| 177 |
boolean hash_summary_only = true; |
| 178 |
|
| 179 |
DEBUGF4 ("%s: %u entries in %d directories (%d hidden).\n", |
| 180 |
db_filename, file_count, dir_count, ignore_dir_count); |
| 181 |
DEBUGF ("ls-R hash table:"); |
| 182 |
hash_print (*table, hash_summary_only); |
| 183 |
fflush (stderr); |
| 184 |
} |
| 185 |
#endif /* KPSE_DEBUG */ |
| 186 |
} |
| 187 |
|
| 188 |
free (top_dir); |
| 189 |
|
| 190 |
return db_file != NULL; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/* Insert FNAME into the hash table. This is for files that get built |
| 195 |
during a run. We wouldn't want to reread all of ls-R, even if it got |
| 196 |
rebuilt. */ |
| 197 |
|
| 198 |
void |
| 199 |
kpathsea_db_insert (kpathsea kpse, const_string passed_fname) |
| 200 |
{ |
| 201 |
/* We might not have found ls-R, or even had occasion to look for it |
| 202 |
yet, so do nothing if we have no hash table. */ |
| 203 |
if (kpse->db.buckets) { |
| 204 |
const_string dir_part; |
| 205 |
string fname = xstrdup (passed_fname); |
| 206 |
string baseptr = fname + (xbasename (fname) - fname); |
| 207 |
const_string file_part = xstrdup (baseptr); |
| 208 |
|
| 209 |
*baseptr = '\0'; /* Chop off the filename. */ |
| 210 |
dir_part = fname; /* That leaves the dir, with the trailing /. */ |
| 211 |
|
| 212 |
/* Note that we do not assuse that these names have been normalized. */ |
| 213 |
hash_insert (&(kpse->db), file_part, dir_part); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/* Return true if FILENAME could be in PATH_ELT, i.e., if the directory |
| 218 |
part of FILENAME matches PATH_ELT. Have to consider // wildcards, but |
| 219 |
$ and ~ expansion have already been done. */ |
| 220 |
|
| 221 |
static boolean |
| 222 |
match (const_string filename, const_string path_elt) |
| 223 |
{ |
| 224 |
const_string original_filename = filename; |
| 225 |
boolean matched = false; |
| 226 |
|
| 227 |
for (; *filename && *path_elt; filename++, path_elt++) { |
| 228 |
if (FILECHARCASEEQ (*filename, *path_elt)) /* normal character match */ |
| 229 |
; |
| 230 |
|
| 231 |
else if (IS_DIR_SEP_CH (*path_elt) /* at // */ |
| 232 |
&& original_filename < filename && IS_DIR_SEP_CH (path_elt[-1])) { |
| 233 |
while (IS_DIR_SEP_CH (*path_elt)) |
| 234 |
path_elt++; /* get past second and any subsequent /'s */ |
| 235 |
if (*path_elt == 0) { |
| 236 |
/* Trailing //, matches anything. We could make this part of the |
| 237 |
other case, but it seems pointless to do the extra work. */ |
| 238 |
matched = true; |
| 239 |
break; |
| 240 |
} else { |
| 241 |
/* Intermediate //, have to match rest of PATH_ELT. */ |
| 242 |
for (; !matched && *filename; filename++) { |
| 243 |
/* Try matching at each possible character. */ |
| 244 |
if (IS_DIR_SEP_CH (filename[-1]) |
| 245 |
&& FILECHARCASEEQ (*filename, *path_elt)) |
| 246 |
matched = match (filename, path_elt); |
| 247 |
} |
| 248 |
/* Prevent filename++ when *filename='\0'. */ |
| 249 |
break; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
else /* normal character nonmatch, quit */ |
| 254 |
break; |
| 255 |
} |
| 256 |
|
| 257 |
/* If we've reached the end of PATH_ELT, check that we're at the last |
| 258 |
component of FILENAME (that is, no directory separators remaining); |
| 259 |
only then have we matched. */ |
| 260 |
if (!matched && *path_elt == 0) { |
| 261 |
/* Typically PATH_ELT ends with, say, `vf', and FILENAME ends with |
| 262 |
`vf/ptmr.vf'. In that case, we'll be at the /. On the other |
| 263 |
hand, if PATH_ELT ended with a / (as in `vf/'), FILENAME being |
| 264 |
the same `vf/ptmr.vf', we'll be at the `p'. |
| 265 |
Upshot: if we're at a dir sep in FILENAME, skip it. */ |
| 266 |
if (IS_DIR_SEP_CH (*filename)) |
| 267 |
filename++; |
| 268 |
|
| 269 |
/* Here are the basic possibilities for the check on being at the |
| 270 |
last component: |
| 271 |
1) PATH_ELT is empty and FILENAME is `ptmr.vf' => match. |
| 272 |
(we now have original_filename == filename) |
| 273 |
2) PATH_ELT is empty and FILENAME is `foo/ptmr.vf' => no match. |
| 274 |
(we now have original_filename == filename) |
| 275 |
3) PATH_ELT is `vf/' and FILENAME is `vf/ptmr.vf' |
| 276 |
(we are now after the / in each) => match. |
| 277 |
4) PATH_ELT is `vf' and FILENAME is `vfoo.ext' |
| 278 |
(we are now after the f in each) => no match. |
| 279 |
|
| 280 |
When (the original) PATH_ELT was the empty string, we want to match |
| 281 |
a FILENAME without dir seps. (This could be argued, and may never |
| 282 |
happen in practice, but is the historical behavior.) */ |
| 283 |
/* if original_filename != filename then original_filename < filename */ |
| 284 |
if (original_filename == filename || IS_DIR_SEP_CH (filename[-1])) { |
| 285 |
while (*filename && !IS_DIR_SEP_CH (*filename)) |
| 286 |
filename++; |
| 287 |
matched = *filename == 0; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
return matched; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
/* If DB_DIR is a prefix of PATH_ELT, return true; otherwise false. |
| 296 |
That is, the question is whether to try the db for a file looked up |
| 297 |
in PATH_ELT. If PATH_ELT == ".", for example, the answer is no. If |
| 298 |
PATH_ELT == "/usr/local/lib/texmf/fonts//tfm", the answer is yes. |
| 299 |
If either string is NULL or empty, return false. |
| 300 |
|
| 301 |
In practice, ls-R is only needed for lengthy subdirectory |
| 302 |
comparisons, but there's no gain to checking PATH_ELT to see if it is |
| 303 |
a subdir match, since the only way to do that is to do a string |
| 304 |
search in it, which is all we do anyway. */ |
| 305 |
|
| 306 |
static boolean |
| 307 |
elt_in_db (const_string db_dir, const_string path_elt) |
| 308 |
{ |
| 309 |
boolean found = false; |
| 310 |
|
| 311 |
/* If both strings are empty or null return false on the grounds that |
| 312 |
it's useless to do anything further with such a strange case (which |
| 313 |
likely never happens). In theory one could argue that the empty |
| 314 |
string is a prefix of any other string, but let's just declare the |
| 315 |
result otherwise. */ |
| 316 |
if (db_dir == NULL || *db_dir == 0 |
| 317 |
|| path_elt == NULL || *path_elt == 0) |
| 318 |
return false; |
| 319 |
|
| 320 |
while (!found && FILECHARCASEEQ (*db_dir++, *path_elt++)) { |
| 321 |
/* If we've matched the entire db directory, it's good. */ |
| 322 |
if (*db_dir == 0) |
| 323 |
found = true; |
| 324 |
|
| 325 |
/* If we've reached the end of PATH_ELT, but not the end of the db |
| 326 |
directory, it's no good. */ |
| 327 |
else if (*path_elt == 0) |
| 328 |
break; |
| 329 |
} |
| 330 |
|
| 331 |
return found; |
| 332 |
} |
| 333 |
|
| 334 |
/* If ALIAS_FILENAME exists, read it into TABLE. */ |
| 335 |
|
| 336 |
static boolean |
| 337 |
alias_build (kpathsea kpse, hash_table_type *table, |
| 338 |
const_string alias_filename) |
| 339 |
{ |
| 340 |
string line, real, alias; |
| 341 |
unsigned count = 0; |
| 342 |
FILE *alias_file = fopen (alias_filename, FOPEN_R_MODE); |
| 343 |
|
| 344 |
if (alias_file) { |
| 345 |
while ((line = read_line (alias_file)) != NULL) { |
| 346 |
/* comments or empty */ |
| 347 |
if (*line == 0 || *line == '%' || *line == '#') { |
| 348 |
; |
| 349 |
} else { |
| 350 |
/* Each line should have two fields: realname aliasname. */ |
| 351 |
real = line; |
| 352 |
while (*real && ISSPACE (*real)) |
| 353 |
real++; |
| 354 |
alias = real; |
| 355 |
while (*alias && !ISSPACE (*alias)) |
| 356 |
alias++; |
| 357 |
*alias++ = 0; |
| 358 |
while (*alias && ISSPACE (*alias)) |
| 359 |
alias++; |
| 360 |
/* Is the check for errors strong enough? Should we warn the user |
| 361 |
for potential errors? */ |
| 362 |
if (strlen (real) != 0 && strlen (alias) != 0) { |
| 363 |
/* Stuff in the alias file should be normalized. */ |
| 364 |
hash_insert_normalized (table, xstrdup (alias), xstrdup (real)); |
| 365 |
count++; |
| 366 |
} |
| 367 |
} |
| 368 |
free (line); |
| 369 |
} |
| 370 |
|
| 371 |
#ifdef KPSE_DEBUG |
| 372 |
if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH)) { |
| 373 |
/* As with ls-R above ... */ |
| 374 |
boolean hash_summary_only = true; |
| 375 |
DEBUGF2 ("%s: %u aliases.\n", alias_filename, count); |
| 376 |
DEBUGF ("alias hash table:"); |
| 377 |
hash_print (*table, hash_summary_only); |
| 378 |
fflush (stderr); |
| 379 |
} |
| 380 |
#endif /* KPSE_DEBUG */ |
| 381 |
|
| 382 |
xfclose (alias_file, alias_filename); |
| 383 |
} |
| 384 |
|
| 385 |
return alias_file != NULL; |
| 386 |
} |
| 387 |
|
| 388 |
/* Initialize the path for ls-R files, and read them all into the hash |
| 389 |
table `db'. If no usable ls-R's found, set kpse->db.buckets to NULL. */ |
| 390 |
|
| 391 |
void |
| 392 |
kpathsea_init_db (kpathsea kpse) |
| 393 |
{ |
| 394 |
const_string db_path; |
| 395 |
string *db_files; |
| 396 |
string *orig_db_files; |
| 397 |
str_list_type unique_list; |
| 398 |
int dbi; |
| 399 |
boolean ok = false; |
| 400 |
|
| 401 |
assert (sizeof(DB_NAME) == sizeof(DB_NAME_LC)); |
| 402 |
|
| 403 |
db_path = kpathsea_init_format (kpse, kpse_db_format); |
| 404 |
db_files = kpathsea_path_search_list_generic (kpse, db_path, db_names, |
| 405 |
true, true); |
| 406 |
orig_db_files = db_files; |
| 407 |
|
| 408 |
/* Mac OS X and others can use a case-insensitive, case-preserving |
| 409 |
filesystem by default, in which case ls-R and ls-r point to the |
| 410 |
same file. Also, Windows is case-insensitive. In these cases, |
| 411 |
we want to avoid reading the same file multiple times. */ |
| 412 |
dbi = 0; |
| 413 |
unique_list = str_list_init (); |
| 414 |
|
| 415 |
while (db_files[dbi] != NULL) { |
| 416 |
string path1 = db_files[dbi]; |
| 417 |
string path2 = db_files[dbi + 1]; |
| 418 |
|
| 419 |
/* first-pass check in case path1/path2 aren't even |
| 420 |
potentially equal; mainly in case the order from |
| 421 |
kpathsea_path_search_list_generic changes. */ |
| 422 |
if (path2 |
| 423 |
&& strcasecmp (path1, path2) == 0 |
| 424 |
&& same_file_p (path1, path2)) { |
| 425 |
/* they are the same, skip over path1, we'll add path2 |
| 426 |
on the next iteration (when it's path1). */ |
| 427 |
#ifdef KPSE_DEBUG |
| 428 |
if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH)) { |
| 429 |
DEBUGF2 ("db:init(): skipping db same_file_p %s, will add %s.\n", |
| 430 |
path1, path2); |
| 431 |
} |
| 432 |
#endif |
| 433 |
free (path1); |
| 434 |
|
| 435 |
} else { |
| 436 |
/* they are not the same, add path1. */ |
| 437 |
#ifdef KPSE_DEBUG |
| 438 |
if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH)) { |
| 439 |
DEBUGF1 ("db:init(): using db file %s.\n", path1); |
| 440 |
} |
| 441 |
#endif |
| 442 |
str_list_add (&unique_list, path1); |
| 443 |
} |
| 444 |
|
| 445 |
/* could be more clever and increment by two, but then would |
| 446 |
have to avoid jumping off the end of db_files */ |
| 447 |
dbi++; |
| 448 |
} |
| 449 |
|
| 450 |
/* always add a NULL terminator. */ |
| 451 |
str_list_add (&unique_list, NULL); |
| 452 |
|
| 453 |
free (orig_db_files); |
| 454 |
db_files = STR_LIST (unique_list); |
| 455 |
orig_db_files = db_files; |
| 456 |
|
| 457 |
/* Must do this after the path searching (which ends up calling |
| 458 |
kpse_db_search recursively), so kpse->db.buckets stays NULL. */ |
| 459 |
kpse->db = hash_create (DB_HASH_SIZE); |
| 460 |
|
| 461 |
while (db_files && *db_files) { |
| 462 |
if (db_build (kpse, &(kpse->db), *db_files)) |
| 463 |
ok = true; |
| 464 |
free (*db_files); |
| 465 |
db_files++; |
| 466 |
} |
| 467 |
|
| 468 |
if (!ok) { |
| 469 |
/* If db can't be built, leave `size' nonzero (so we don't |
| 470 |
rebuild it), but clear `buckets' (so we don't look in it). */ |
| 471 |
free (kpse->db.buckets); |
| 472 |
kpse->db.buckets = NULL; |
| 473 |
} |
| 474 |
|
| 475 |
free (orig_db_files); |
| 476 |
|
| 477 |
/* Add the content of any alias databases. There may exist more than |
| 478 |
one alias file along DB_NAME files. This duplicates the above code |
| 479 |
-- should be a function. */ |
| 480 |
ok = false; |
| 481 |
db_files = kpathsea_all_path_search (kpse, db_path, ALIAS_NAME); |
| 482 |
orig_db_files = db_files; |
| 483 |
|
| 484 |
kpse->alias_db = hash_create (ALIAS_HASH_SIZE); |
| 485 |
|
| 486 |
while (db_files && *db_files) { |
| 487 |
if (alias_build (kpse, &(kpse->alias_db), *db_files)) |
| 488 |
ok = true; |
| 489 |
free (*db_files); |
| 490 |
db_files++; |
| 491 |
} |
| 492 |
|
| 493 |
if (!ok) { |
| 494 |
free (kpse->alias_db.buckets); |
| 495 |
kpse->alias_db.buckets = NULL; |
| 496 |
} |
| 497 |
|
| 498 |
free (orig_db_files); |
| 499 |
} |
| 500 |
|
| 501 |
/* Avoid doing anything if this PATH_ELT is irrelevant to the databases. */ |
| 502 |
str_list_type * |
| 503 |
kpathsea_db_search (kpathsea kpse, const_string name, |
| 504 |
const_string orig_path_elt, boolean all) |
| 505 |
{ |
| 506 |
const_string *db_dirs, *orig_dirs; |
| 507 |
const_string last_slash, path_elt; |
| 508 |
string temp_str = NULL; |
| 509 |
boolean done; |
| 510 |
unsigned e; |
| 511 |
str_list_type *ret = NULL; |
| 512 |
const_string *aliases, *r; |
| 513 |
boolean relevant = false; |
| 514 |
|
| 515 |
/* If we failed to build the database (or if this is the recursive |
| 516 |
call to build the db path), quit. */ |
| 517 |
if (kpse->db.buckets == NULL) |
| 518 |
return NULL; |
| 519 |
|
| 520 |
/* When tex-glyph.c calls us looking for, e.g., dpi600/cmr10.pk, we |
| 521 |
won't find it unless we change NAME to just `cmr10.pk' and append |
| 522 |
`/dpi600' to PATH_ELT. We are justified in using a literal `/' |
| 523 |
here, since that's what tex-glyph.c unconditionally uses in |
| 524 |
DPI_BITMAP_SPEC. But don't do anything if the / begins NAME; that |
| 525 |
should never happen. */ |
| 526 |
last_slash = strrchr (name, '/'); |
| 527 |
if (last_slash && last_slash != name) { |
| 528 |
unsigned len = last_slash - name + 1; |
| 529 |
string dir_part = (string)xmalloc (len); |
| 530 |
strncpy (dir_part, name, len - 1); |
| 531 |
dir_part[len - 1] = 0; |
| 532 |
path_elt = temp_str = concat3 (orig_path_elt, "/", dir_part); |
| 533 |
name = last_slash + 1; |
| 534 |
free (dir_part); |
| 535 |
} else |
| 536 |
path_elt = orig_path_elt; |
| 537 |
|
| 538 |
/* Don't bother doing any lookups if this `path_elt' isn't covered by |
| 539 |
any of database directories. We do this not so much because the |
| 540 |
extra couple of hash lookups matter -- they don't -- but rather |
| 541 |
because we want to return NULL in this case, so path_search can |
| 542 |
know to do a disk search. */ |
| 543 |
for (e = 0; !relevant && e < STR_LIST_LENGTH (kpse->db_dir_list); e++) { |
| 544 |
relevant = elt_in_db (STR_LIST_ELT (kpse->db_dir_list, e), path_elt); |
| 545 |
} |
| 546 |
if (!relevant) |
| 547 |
return NULL; |
| 548 |
|
| 549 |
/* If we have aliases for this name, use them. */ |
| 550 |
if (kpse->alias_db.buckets) |
| 551 |
aliases = hash_lookup (kpse->alias_db, name); |
| 552 |
else |
| 553 |
aliases = NULL; |
| 554 |
|
| 555 |
if (!aliases) { |
| 556 |
aliases = XTALLOC1 (const_string); |
| 557 |
aliases[0] = NULL; |
| 558 |
} |
| 559 |
{ /* Push aliases up by one and insert the original name at the front. */ |
| 560 |
unsigned i; |
| 561 |
unsigned len = 1; /* Have NULL element already allocated. */ |
| 562 |
for (r = aliases; *r; r++) |
| 563 |
len++; |
| 564 |
/* This is essentially |
| 565 |
XRETALLOC (aliases, len + 1, const_string); |
| 566 |
except that MSVC warns without the cast to `void *'. */ |
| 567 |
aliases = (const_string *) xrealloc ((void *) aliases, |
| 568 |
(len + 1) * sizeof(const_string)); |
| 569 |
for (i = len; i > 0; i--) { |
| 570 |
aliases[i] = aliases[i - 1]; |
| 571 |
} |
| 572 |
aliases[0] = name; |
| 573 |
} |
| 574 |
|
| 575 |
done = false; |
| 576 |
for (r = aliases; !done && *r; r++) { |
| 577 |
const_string ctry = *r; |
| 578 |
|
| 579 |
/* We have an ls-R db. Look up `try'. */ |
| 580 |
orig_dirs = db_dirs = hash_lookup (kpse->db, ctry); |
| 581 |
|
| 582 |
ret = XTALLOC1 (str_list_type); |
| 583 |
*ret = str_list_init (); |
| 584 |
|
| 585 |
/* For each filename found, see if it matches the path element. For |
| 586 |
example, if we have .../cx/cmr10.300pk and .../ricoh/cmr10.300pk, |
| 587 |
and the path looks like .../cx, we don't want the ricoh file. */ |
| 588 |
while (!done && db_dirs && *db_dirs) { |
| 589 |
string db_file = concat (*db_dirs, ctry); |
| 590 |
boolean matched = match (db_file, path_elt); |
| 591 |
|
| 592 |
#ifdef KPSE_DEBUG |
| 593 |
if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) |
| 594 |
DEBUGF3 ("db:match(%s,%s) = %d\n", db_file, path_elt, matched); |
| 595 |
#endif |
| 596 |
|
| 597 |
/* We got a hit in the database. Now see if the file actually |
| 598 |
exists, possibly under an alias. */ |
| 599 |
if (matched) { |
| 600 |
string found = NULL; |
| 601 |
if (kpathsea_readable_file (kpse, db_file)) { |
| 602 |
found = db_file; |
| 603 |
|
| 604 |
} else { |
| 605 |
const_string *a; |
| 606 |
|
| 607 |
free (db_file); /* `db_file' wasn't on disk. */ |
| 608 |
|
| 609 |
/* The hit in the DB doesn't exist in disk. Now try all its |
| 610 |
aliases. For example, suppose we have a hierarchy on CD, |
| 611 |
thus `mf.bas', but ls-R contains `mf.base'. Find it anyway. |
| 612 |
Could probably work around this with aliases, but |
| 613 |
this is pretty easy and shouldn't hurt. The upshot is that |
| 614 |
if one of the aliases actually exists, we use that. */ |
| 615 |
for (a = aliases + 1; *a && !found; a++) { |
| 616 |
string atry = concat (*db_dirs, *a); |
| 617 |
if (kpathsea_readable_file (kpse, atry)) |
| 618 |
found = atry; |
| 619 |
else |
| 620 |
free (atry); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
/* If we have a real file, add it to the list, maybe done. */ |
| 625 |
if (found) { |
| 626 |
str_list_add (ret, found); |
| 627 |
if (!all && found) |
| 628 |
done = true; |
| 629 |
} |
| 630 |
} else { /* no match in the db */ |
| 631 |
free (db_file); |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
/* On to the next directory, if any. */ |
| 636 |
db_dirs++; |
| 637 |
} |
| 638 |
|
| 639 |
/* This is just the space for the pointers, not the strings. */ |
| 640 |
if (orig_dirs && *orig_dirs) |
| 641 |
free (orig_dirs); |
| 642 |
} |
| 643 |
|
| 644 |
free ((void *) aliases); |
| 645 |
|
| 646 |
/* If we had to break up NAME, free the TEMP_STR. */ |
| 647 |
if (temp_str) |
| 648 |
free (temp_str); |
| 649 |
|
| 650 |
return ret; |
| 651 |
} |
| 652 |
|
| 653 |
str_list_type * |
| 654 |
kpathsea_db_search_list (kpathsea kpse, string* names, |
| 655 |
const_string path_elt, boolean all) |
| 656 |
{ |
| 657 |
const_string *db_dirs, *orig_dirs; |
| 658 |
const_string last_slash, name, path; |
| 659 |
string temp_str = NULL; |
| 660 |
boolean done; |
| 661 |
unsigned e; |
| 662 |
const_string *aliases, *r; |
| 663 |
int n; |
| 664 |
str_list_type *ret = NULL; |
| 665 |
boolean relevant = false; |
| 666 |
|
| 667 |
/* If we failed to build the database (or if this is the recursive |
| 668 |
call to build the db path), quit. */ |
| 669 |
if (kpse->db.buckets == NULL) |
| 670 |
return NULL; |
| 671 |
|
| 672 |
/* Don't bother doing any lookups if this `path_elt' isn't covered by |
| 673 |
any of database directories. We do this not so much because the |
| 674 |
extra couple of hash lookups matter -- they don't -- but rather |
| 675 |
because we want to return NULL in this case, so path_search can |
| 676 |
know to do a disk search. */ |
| 677 |
for (e = 0; !relevant && e < STR_LIST_LENGTH (kpse->db_dir_list); e++) { |
| 678 |
relevant = elt_in_db (STR_LIST_ELT (kpse->db_dir_list, e), path_elt); |
| 679 |
} |
| 680 |
if (!relevant) |
| 681 |
return NULL; |
| 682 |
|
| 683 |
done = false; |
| 684 |
ret = XTALLOC1 (str_list_type); |
| 685 |
*ret = str_list_init (); |
| 686 |
|
| 687 |
/* Handle each name. */ |
| 688 |
for (n = 0; !done && names[n]; n++) { |
| 689 |
name = names[n]; |
| 690 |
|
| 691 |
/* Absolute names should have been caught in our caller. */ |
| 692 |
if (kpathsea_absolute_p(kpse, name, true)) |
| 693 |
continue; |
| 694 |
|
| 695 |
/* When tex-glyph.c calls us looking for, e.g., dpi600/cmr10.pk, we |
| 696 |
won't find it unless we change NAME to just `cmr10.pk' and append |
| 697 |
`/dpi600' to PATH_ELT. We are justified in using a literal `/' |
| 698 |
here, since that's what tex-glyph.c unconditionally uses in |
| 699 |
DPI_BITMAP_SPEC. But don't do anything if the / begins NAME; that |
| 700 |
should never happen. */ |
| 701 |
last_slash = strrchr (name, '/'); |
| 702 |
if (last_slash && last_slash != name) { |
| 703 |
unsigned len = last_slash - name + 1; |
| 704 |
string dir_part = (string)xmalloc (len); |
| 705 |
strncpy (dir_part, name, len - 1); |
| 706 |
dir_part[len - 1] = 0; |
| 707 |
path = temp_str = concat3 (path_elt, "/", dir_part); |
| 708 |
name = last_slash + 1; |
| 709 |
free (dir_part); |
| 710 |
} else { |
| 711 |
path = path_elt; |
| 712 |
} |
| 713 |
|
| 714 |
/* If we have aliases for this name, use them. */ |
| 715 |
if (kpse->alias_db.buckets) |
| 716 |
aliases = hash_lookup (kpse->alias_db, name); |
| 717 |
else |
| 718 |
aliases = NULL; |
| 719 |
|
| 720 |
if (!aliases) { |
| 721 |
aliases = XTALLOC1 (const_string); |
| 722 |
aliases[0] = NULL; |
| 723 |
} |
| 724 |
{ /* Push aliases up by one and insert the original name at front. */ |
| 725 |
unsigned i; |
| 726 |
unsigned len = 1; /* Have NULL element already allocated. */ |
| 727 |
for (r = aliases; *r; r++) |
| 728 |
len++; |
| 729 |
aliases = (const_string *) xrealloc ((void *) aliases, |
| 730 |
(len + 1) * sizeof(const_string)); |
| 731 |
for (i = len; i > 0; i--) { |
| 732 |
aliases[i] = aliases[i - 1]; |
| 733 |
} |
| 734 |
aliases[0] = name; |
| 735 |
} |
| 736 |
|
| 737 |
for (r = aliases; !done && *r; r++) { |
| 738 |
const_string ctry = *r; |
| 739 |
|
| 740 |
/* We have an ls-R db. Look up `try'. */ |
| 741 |
orig_dirs = db_dirs = hash_lookup (kpse->db, ctry); |
| 742 |
|
| 743 |
/* For each filename found, see if it matches the path element. For |
| 744 |
example, if we have .../cx/cmr10.300pk and .../ricoh/cmr10.300pk, |
| 745 |
and the path looks like .../cx, we don't want the ricoh file. */ |
| 746 |
while (!done && db_dirs && *db_dirs) { |
| 747 |
string db_file = concat (*db_dirs, ctry); |
| 748 |
boolean matched = match (db_file, path); |
| 749 |
|
| 750 |
#ifdef KPSE_DEBUG |
| 751 |
if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) |
| 752 |
DEBUGF3 ("db:match(%s,%s) = %d\n", db_file, path, matched); |
| 753 |
#endif |
| 754 |
|
| 755 |
/* We got a hit in the database. Now see if the file actually |
| 756 |
exists, possibly under an alias. */ |
| 757 |
if (matched) { |
| 758 |
string found = NULL; |
| 759 |
if (kpathsea_readable_file (kpse, db_file)) { |
| 760 |
found = db_file; |
| 761 |
|
| 762 |
} else { |
| 763 |
const_string *a; |
| 764 |
|
| 765 |
free (db_file); /* `db_file' wasn't on disk. */ |
| 766 |
|
| 767 |
/* The hit in the DB doesn't exist in disk. Now try all its |
| 768 |
aliases. For example, suppose we have a hierarchy on CD, |
| 769 |
thus `mf.bas', but ls-R contains `mf.base'. Find it anyway. |
| 770 |
Could probably work around this with aliases, but |
| 771 |
this is pretty easy and shouldn't hurt. The upshot is that |
| 772 |
if one of the aliases actually exists, we use that. */ |
| 773 |
for (a = aliases + 1; *a && !found; a++) { |
| 774 |
string atry = concat (*db_dirs, *a); |
| 775 |
if (kpathsea_readable_file (kpse, atry)) |
| 776 |
found = atry; |
| 777 |
else |
| 778 |
free (atry); |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
/* If we have a real file, add it to the list, maybe done. */ |
| 783 |
if (found) { |
| 784 |
str_list_add (ret, found); |
| 785 |
if (!all) |
| 786 |
done = true; |
| 787 |
} |
| 788 |
} else { /* no match in the db */ |
| 789 |
free (db_file); |
| 790 |
} |
| 791 |
|
| 792 |
/* On to the next directory, if any. */ |
| 793 |
db_dirs++; |
| 794 |
} |
| 795 |
|
| 796 |
/* This is just the space for the pointers, not the strings. */ |
| 797 |
if (orig_dirs && *orig_dirs) |
| 798 |
free (orig_dirs); |
| 799 |
} |
| 800 |
|
| 801 |
free ((void *) aliases); |
| 802 |
if (temp_str) |
| 803 |
free (temp_str); |
| 804 |
} |
| 805 |
|
| 806 |
return ret; |
| 807 |
} |