Debian Codegraph @

texk/kpathsea/pathsearch.c

raw
1 /* pathsearch.c: look up a filename in a path.
2
3 Copyright 1993, 1994, 1995, 1997, 2007, 2009-2012, 2018,
4 2020, 2025 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/c-pathch.h>
22 #include <kpathsea/c-fopen.h>
23 #include <kpathsea/absolute.h>
24 #include <kpathsea/expand.h>
25 #include <kpathsea/db.h>
26 #include <kpathsea/pathsearch.h>
27 #include <kpathsea/readable.h>
28 #include <kpathsea/str-list.h>
29 #include <kpathsea/str-llist.h>
30 #include <kpathsea/variable.h>
31 #include <kpathsea/xopendir.h>
32
33 #include <time.h> /* for `time' */
34
35 #ifdef __DJGPP__
36 #include <sys/stat.h> /* for stat bits */
37 #endif
38
39 #ifdef WIN32
40 #undef fputs
41 #undef puts
42 #define fputs win32_fputs
43 #define puts win32_puts
44 #endif
45
46 /* The very first search is for texmf.cnf, called when someone tries to
47 initialize the TFM path or whatever. init_path calls kpse_cnf_get
48 which calls kpse_all_path_search to find all the texmf.cnf's. We
49 need to do various special things in this case, since we obviously
50 don't yet have the configuration files when we're searching for the
51 configuration files. Therefore we have a followup_search member in
52 kpathsea_instance to distinguish the first search from all others. */
53
54
55
56 #ifdef KPSE_DEBUG
57 /* Print on FH elements of L surrounded by brackets, separated by spaces. */
58
59 static void
60 print_space_list (FILE *fh, string *l)
61 {
62 fputs ("[", fh);
63 while (l && *l) {
64 fputs (*l, fh);
65 l++;
66 if (*l)
67 fputs (" ", fh);
68 }
69 fputs ("]", fh);
70 }
71 #endif /* KPSE_DEBUG */
72
73 /* This function is called after every search (except the first, since
74 we definitely want to allow enabling the logging in texmf.cnf) to
75 record the filename(s) found in $TEXMFLOG. */
76
77 static void
78 log_search (kpathsea kpse, str_list_type filenames)
79 {
80 if (kpse->log_opened == false) {
81 /* Get name from either envvar or config file. Thus, the first time
82 is called, for the first search, we will be opening texmf.cnf
83 and ls-R just to resolve the variable. */
84 string log_name = kpathsea_var_value (kpse, "TEXMFLOG");
85 kpse->log_opened = true;
86 if (log_name) {
87 kpse->log_file = fopen (log_name, FOPEN_A_MODE);
88 if (!kpse->log_file)
89 perror (log_name);
90 free (log_name);
91 }
92 }
93
94 if (
95 #ifdef KPSE_DEBUG
96 KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH) ||
97 #endif /* KPSE_DEBUG */
98 kpse->log_file) {
99 unsigned e;
100
101 /* FILENAMES should never be null, but safety doesn't hurt. */
102 for (e = 0; e < STR_LIST_LENGTH (filenames) && STR_LIST_ELT (filenames, e);
103 e++) {
104 string filename = STR_LIST_ELT (filenames, e);
105
106 /* Only record absolute filenames, for privacy. */
107 if (kpse->log_file && kpathsea_absolute_p (kpse, filename, false)) {
108 fprintf (kpse->log_file, "%lu %s\n", (long unsigned) time (NULL),
109 filename);
110 }
111
112 #ifdef KPSE_DEBUG
113 /* And show them online, if debugging. We've already started the
114 debugging line in `search' and
115 `kpathsea_path_search_list_generic', where this is called, so
116 just print the filename here, don't use DEBUGF. */
117 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) {
118 putc (' ', stderr);
119 fputs (filename, stderr);
120 }
121 #endif /* KPSE_DEBUG */
122 }
123 }
124 }
125
126 /* Either casefold_readable_file or kpathsea_readable_file. We need the
127 type even if we don't have casefolding enabled at compile-time. */
128 typedef string (*readable_file_fn_type) (kpathsea, string);
129
130 #ifdef MONOCASE_FILENAMES
131 /* Don't do any of this fallback casefolding stuff on Windows. */
132 #undef KPSE_CASEFOLDING_SEARCH
133 #else
134 #define KPSE_CASEFOLDING_SEARCH 1
135 #endif
136
137 #ifdef KPSE_CASEFOLDING_SEARCH
138
139 /* Same as kpathsea_readable_file (readable.c), but check
140 case-insensitively on the last file name component. We return the
141 first match (in new memory), or NULL. In practice this is always
142 called after checking the filename as-is, so there's no use in
143 continuing to look for an exact match. The KPSE arg is only passed
144 to kpathsea_readable_file if we have a candidate match.
145
146 Clearly we could do some caching here, but let's see if it's actually
147 necessary, given all the other levels of caching (disk, memory, cpu)
148 that are going on these days. */
149
150 static string
151 casefold_readable_file (kpathsea kpse, string name)
152 {
153 string ret = NULL;
154 const_string this_base_name = xbasename (name);
155 string this_dir_name = xdirname (name);
156 DIR *thisdir = opendir (this_dir_name);
157
158 #ifdef KPSE_DEBUG
159 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) {
160 DEBUGF2 (" casefold_readable_file(%s) in %s => ",
161 this_base_name, this_dir_name);
162 }
163 #endif
164
165 /* We can be called with a name like `subdir/somefile', where subdir/
166 does not exist. So the opendir might fail, and that's ok. */
167 if (thisdir) {
168 struct dirent *e;
169
170 while ((e = readdir (thisdir)) != NULL) {
171 /* The standard strcasecmp seems like enough for the comparison? */
172 if (strcasecmp (e->d_name, this_base_name) == 0) {
173 ret = concat3 (this_dir_name, DIR_SEP_STRING, e->d_name);
174 if (kpathsea_readable_file (kpse, ret)) {
175 break; /* success */
176 } else {
177 /* This only happens when the name matches, but the potential file is
178 not actually readable, e.g., a broken symlink. It seems
179 sufficiently unusual to be worth logging. */
180 #ifdef KPSE_DEBUG
181 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) {
182 fprintf (stderr, "{casefolded candidate %s not readable, continuing}",ret);
183 }
184 #endif
185 free (ret); /* not readable, keep looking */
186 ret = NULL;
187 }
188 }
189 } /* end of readdir loop */
190 xclosedir (thisdir);
191 }
192 free (this_dir_name);
193
194 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) {
195 fputs (ret ? ret : "(nil)", stderr);
196 fputc ('\n', stderr);
197 }
198 return ret;
199 }
200 #endif /* KPSE_CASEFOLDING_SEARCH */
201
202 /* Return a str_list of matches in DIRS of NAME.
203
204 Concatenate each element in DIRS with NAME, assuming each ends with /.
205 If SEARCH_ALL is false, return a list containing just the first
206 readable (according to the READABLE_FILE_P function) regular file.
207 Else continue to search and return them all. In any case, if none,
208 return a list containing just NULL.
209
210 DIRS is modified only in that the directory where a search matches is
211 floated toward the top of the list.
212
213 We keep a single buffer for the potential filenames and reallocate
214 only when necessary. I'm not sure it's noticeably faster, but it
215 does seem cleaner. (We do waste a bit of space in the return
216 value, since we don't shrink it to the final size returned.) */
217
218 #define INIT_ALLOC 75 /* Doesn't much matter what this number is. */
219
220 static str_list_type
221 dir_list_search (kpathsea kpse, str_llist_type *dirs, const_string name,
222 boolean search_all, readable_file_fn_type readable_file_p)
223 {
224 str_llist_elt_type *elt;
225 str_llist_elt_type *next_elt;
226 str_list_type ret;
227 unsigned name_len = strlen (name);
228 unsigned allocated = INIT_ALLOC;
229 string potential = (string) xmalloc (allocated);
230
231 #ifdef KPSE_DEBUG
232 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) {
233 const_string casefold =
234 #ifdef KPSE_CASEFOLDING_SEARCH
235 (readable_file_p == casefold_readable_file) ? "yes" : "no";
236 #else
237 "No";
238 #endif
239 DEBUGF3 (" dir_list_search(file=%s, find_all=%d, casefold=%s)\n",
240 name, search_all, casefold);
241 }
242 #endif
243
244 ret = str_list_init ();
245
246 for (elt = *dirs; elt; elt = next_elt) {
247 string readable_name;
248 const_string dir = STR_LLIST (*elt);
249 unsigned dir_len = strlen (dir);
250
251 next_elt = STR_LLIST_NEXT (*elt); /* in case elt floats */
252
253 while (dir_len + name_len + 1 > allocated) {
254 allocated += allocated;
255 XRETALLOC (potential, allocated, char);
256 }
257 strcpy (potential, dir);
258 strcat (potential, name);
259
260 readable_name = readable_file_p (kpse, potential);
261 if (readable_name) {
262 str_list_add (&ret, readable_name);
263
264 /* Move this element towards the top of the list. */
265 str_llist_float (dirs, elt);
266
267 /* If caller only wanted one file returned, no need to
268 terminate the list with NULL; the caller knows to only look
269 at the first element. */
270 if (!search_all)
271 return ret;
272
273 /* Start new filename. */
274 allocated = INIT_ALLOC;
275 potential = (string) xmalloc (allocated);
276 }
277 }
278
279 /* If we get here, either we didn't find any files, or we were finding
280 all the files. But we're done with the last filename, anyway. */
281 free (potential);
282
283 return ret;
284 }
285
286 /* This is analogous to dir_list_search above, except we search for
287 multiple NAMES instead of one (unfortunately the code is duplicated).
288 Absolute or explicitly relative items in NAMES are ignored; the
289 caller (kpathsea_path_search_list_generic) deals with those
290 separately. NAMES[i] is not modified. */
291
292
293 static str_list_type
294 dir_list_search_list (kpathsea kpse, str_llist_type *dirs, string* names,
295 boolean search_all, readable_file_fn_type readable_file_p)
296 {
297 str_llist_elt_type *elt;
298 str_llist_elt_type *next_elt;
299 str_list_type ret;
300 unsigned allocated = INIT_ALLOC;
301 string potential = XTALLOC (allocated, char);
302
303 #ifdef KPSE_DEBUG
304 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) {
305 const_string casefold =
306 #ifdef KPSE_CASEFOLDING_SEARCH
307 (readable_file_p == casefold_readable_file) ? "yes" : "no";
308 #else
309 "No";
310 #endif
311 DEBUGF (" dir_list_search_list(files=");
312 print_space_list (stderr, names);
313 fprintf (stderr, ", find_all=%d, casefold=%s)\n", search_all, casefold);
314 }
315 #endif
316
317 ret = str_list_init ();
318
319 for (elt = *dirs; elt; elt = next_elt) {
320 int i;
321 string readable_name;
322 const_string dir = STR_LLIST (*elt);
323 unsigned dir_len = strlen (dir);
324
325 next_elt = STR_LLIST_NEXT (*elt); /* in case elt floats */
326
327 for (i = 0; names[i]; i++) {
328 const_string name = names[i];
329 unsigned name_len;
330
331 /* Don't bother with absolute & explicit relative. */
332 if (kpathsea_absolute_p (kpse, name, true))
333 continue;
334
335 name_len = strlen (name);
336
337 while (dir_len + name_len + 1 > allocated) {
338 allocated += allocated;
339 XRETALLOC (potential, allocated, char);
340 }
341
342 strcpy (potential, dir);
343 strcat (potential + dir_len, name);
344 readable_name = readable_file_p (kpse, potential);
345 if (readable_name) {
346 str_list_add (&ret, readable_name);
347
348 /* Move this element towards the top of the list. */
349 str_llist_float (dirs, elt);
350
351 /* If caller only wanted one file returned, no need to
352 terminate the list with NULL; the caller knows to only look
353 at the first element. */
354 if (!search_all)
355 return ret;
356
357 /* Start new filename. */
358 allocated = INIT_ALLOC;
359 potential = XTALLOC (allocated, char);
360 }
361 }
362 }
363
364 /* If we get here, either we didn't find any files, or we were finding
365 all the files. But we're done with the last filename, anyway. */
366 free (potential);
367
368 return ret;
369 }
370
371 /* This is called when NAME is absolute or explicitly relative; if it's
372 readable, return a one-element str_list containing it (in new
373 memory); otherwise, return an empty list. (We return a list so as to
374 have the same return value as the path_search function.) We also
375 check case-insensitively if enabled and needed. */
376
377 static str_list_type
378 absolute_search (kpathsea kpse, string name)
379 {
380 str_list_type ret_list;
381 string found;
382
383 /* Some old compilers can't initialize structs. */
384 ret_list = str_list_init ();
385
386 /* Do the first check. */
387 found = kpathsea_readable_file (kpse, name);
388 #ifdef KPSE_DEBUG
389 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) {
390 DEBUGF2 (" absolute_search(%s) => %s\n", name, found ? found : "(nil)");
391 }
392 #endif
393 if (found) {
394 found = xstrdup (found); /* Return new memory. */
395 }
396
397 #ifdef KPSE_CASEFOLDING_SEARCH
398 /* Do the casefolding search only if both needed and enabled. */
399 if (!found) {
400 if (kpse_cnf_p (kpathsea_var_value (kpse, "texmf_casefold_search"))) {
401 found = casefold_readable_file (kpse, name);
402 #ifdef KPSE_DEBUG
403 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) {
404 DEBUGF2 (" casefold search(%s) => %s\n",name,found ? found : "(nil)");
405 }
406 #endif /* KPSE_DEBUG */
407 }
408 }
409 #endif /* KPSE_CASEFOLDING_SEARCH */
410
411 if (found) {
412 /* If we didn't find anything, we'll return an empty list. */
413 str_list_add (&ret_list, found);
414 }
415
416 return ret_list;
417 }
418
419 /* This is the hard case -- look for NAME in PATH. If ALL is false,
420 return the first file found. Otherwise, search all elements of PATH.
421 We also check case-insensitively if needed and requested. */
422
423 static str_list_type
424 path_search (kpathsea kpse, const_string path, string name,
425 boolean must_exist, boolean all)
426 {
427 string elt;
428 str_list_type ret_list;
429 boolean done = false;
430 ret_list = str_list_init (); /* some compilers lack struct initialization */
431
432 #ifdef KPSE_DEBUG
433 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) {
434 DEBUGF4 (" path_search(file=%s, must_exist=%d, find_all=%d, path=%s)\n",
435 name, all, must_exist, path);
436 }
437 #endif
438
439 for (elt = kpathsea_path_element (kpse, path); !done && elt;
440 elt = kpathsea_path_element (kpse, NULL)) {
441 str_list_type *found;
442 boolean allow_disk_search = true;
443
444 if (*elt == '!' && *(elt + 1) == '!') {
445 /* Those magic leading chars in a path element means don't search the
446 disk for this elt. And move past the magic to get to the name. */
447 allow_disk_search = false;
448 elt += 2;
449 }
450
451 /* See elt-dirs.c for side effects of this function. */
452 kpathsea_normalize_path (kpse, elt);
453
454 /* Try ls-R, unless we're searching for texmf.cnf. Our caller
455 (search), also tests followup_search, and does the resetting. */
456 found = kpse->followup_search ? kpathsea_db_search (kpse, name, elt, all)
457 : NULL;
458
459 /* Search the filesystem if (1) the path spec allows it, and either
460 (2a) we are searching for texmf.cnf; or
461 (2b) no db exists; or
462 (2c) no db's are relevant to this elt; or
463 (3) MUST_EXIST && NAME was not in the db.
464 In (2*), `found' will be NULL.
465 In (3), `found' will be an empty list. */
466 if (allow_disk_search
467 && (!found || (must_exist && !STR_LIST (*found)))) {
468 /* Determine the directories in which to search: */
469 str_llist_type *dirs = kpathsea_element_dirs (kpse, elt);
470 if (dirs && *dirs) {
471 if (!found) {
472 found = XTALLOC1 (str_list_type);
473 }
474 /* Search in the directories: */
475 *found = dir_list_search (kpse, dirs, name, all,
476 kpathsea_readable_file);
477 #ifdef KPSE_CASEFOLDING_SEARCH
478 if (!STR_LIST (*found)
479 && kpse_cnf_p (kpathsea_var_value (kpse,
480 "texmf_casefold_search"))) {
481 /* Nothing found; search again, case-insensitively: */
482 *found = dir_list_search (kpse, dirs, name, all,
483 casefold_readable_file);
484 }
485 #endif /* KPSE_CASEFOLDING_SEARCH */
486 }
487 }
488
489 /* Did we find anything? */
490 if (found && STR_LIST (*found)) {
491 if (all) {
492 str_list_concat (&ret_list, *found);
493 } else {
494 str_list_add (&ret_list, STR_LIST_FIRST_ELT (*found));
495 done = true;
496 }
497 }
498
499 /* Free the list space, if any (but not the elements). */
500 if (found) {
501 str_list_free (found);
502 free (found);
503 }
504 }
505
506 return ret_list;
507 }
508
509 /* Search PATH for ORIGINAL_NAME. If ALL is false, or ORIGINAL_NAME is
510 absolute_p, check ORIGINAL_NAME itself. Otherwise, look at each
511 element of PATH for the first readable ORIGINAL_NAME.
512
513 Always return a list; if no files are found, the list will
514 contain just NULL. If ALL is true, the list will be
515 terminated with NULL.
516
517 This function is a special case of kpathsea_path_search_list_generic
518 below (which takes a list of names, instead of a single name, to
519 search for), and so should be rewritten to call it. But this
520 function came first, and life is short, so the code duplication
521 is here. Sorry. Please fix. */
522
523 static string *
524 search (kpathsea kpse, const_string path, const_string original_name,
525 boolean must_exist, boolean all)
526 {
527 str_list_type ret_list;
528 string name;
529 boolean absolute_p;
530 #ifdef __DJGPP__
531 /* We will use `stat' heavily, so let's request for
532 the fastest possible version of `stat', by telling
533 it what members of struct stat do we really need.
534
535 We need to set this on each call because this is a
536 library function; the caller might need other options
537 from `stat'. Thus save the flags and restore them
538 before exit.
539
540 This call tells `stat' that we do NOT need to recognize
541 executable files (neither by an extension nor by a magic
542 signature); that we do NOT need time stamp of root directories;
543 and that we do NOT need the write access bit in st_mode.
544
545 Note that `kpse_set_program_name' needs the EXEC bits,
546 but it was already called by the time we get here. */
547 unsigned short save_djgpp_flags = _djstat_flags;
548
549 _djstat_flags = _STAT_EXEC_MAGIC | _STAT_EXEC_EXT
550 | _STAT_ROOT_TIME | _STAT_WRITEBIT;
551 #endif
552
553 /* Make a leading ~ count as an absolute filename, and expand $FOO's. */
554 name = kpathsea_expand (kpse, original_name);
555
556 #ifdef KPSE_DEBUG
557 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH))
558 DEBUGF4 ("start search(xname=%s, must_exist=%d, find_all=%d, path=%s).\n",
559 name, must_exist, all, path);
560 #endif /* KPSE_DEBUG */
561
562 /* If the first name is absolute or explicitly relative, no need to
563 consider PATH at all. */
564 absolute_p = kpathsea_absolute_p (kpse, name, true);
565
566 /* Find the file(s). */
567 ret_list = absolute_p ? absolute_search (kpse, name)
568 : path_search (kpse, path, name, must_exist, all);
569
570 /* Append NULL terminator if we didn't find anything at all, or we're
571 supposed to find ALL and the list doesn't end in NULL now. */
572 if (STR_LIST_EMPTY (ret_list)
573 || (all && STR_LIST_LAST_ELT (ret_list) != NULL))
574 str_list_add (&ret_list, NULL);
575
576 /* The very first search is for texmf.cnf. We can't log that, since
577 we want to allow setting TEXMFLOG in texmf.cnf. */
578 if (kpse->followup_search == false) {
579 kpse->followup_search = true;
580 } else {
581 /* Record the filenames we found, if desired. And wrap them in a
582 debugging line if we're doing that. */
583 #ifdef KPSE_DEBUG
584 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH))
585 DEBUGF1 ("returning from search(%s) =>", original_name);
586 #endif /* KPSE_DEBUG */
587 log_search (kpse, ret_list);
588 #ifdef KPSE_DEBUG
589 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH))
590 putc ('\n', stderr);
591 #endif /* KPSE_DEBUG */
592 }
593
594 #ifdef __DJGPP__
595 /* Undo any side effects. */
596 _djstat_flags = save_djgpp_flags;
597 #endif
598
599 /* Free the expanded name we were passed. It can't be in the return
600 list, since the path directories got unconditionally prepended. */
601 free (name);
602
603 return STR_LIST (ret_list);
604 }
605
606 /* Search PATH for null-terminated array of NAMES. Always return a list;
607 if no files are found, the list will contain just NULL. If ALL is
608 true, the list will be terminated with NULL (but no NULL terminator
609 if ALL is false). This is a generalization of the `search' fn above. */
610
611 string *
612 kpathsea_path_search_list_generic (kpathsea kpse,
613 const_string path, string* names,
614 boolean must_exist, boolean all)
615 {
616 str_list_type ret_list;
617 string* namep;
618 string elt;
619 boolean done = false;
620 boolean all_absolute = true;
621 #ifdef __DJGPP__
622 /* See DJGPP comments above. */
623 unsigned short save_djgpp_flags = _djstat_flags;
624
625 _djstat_flags = _STAT_EXEC_MAGIC | _STAT_EXEC_EXT
626 | _STAT_ROOT_TIME | _STAT_WRITEBIT;
627 #endif
628
629 ret_list = str_list_init ();
630
631 #ifdef KPSE_DEBUG
632 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) {
633 DEBUGF ("start generic search(files=");
634 print_space_list (stderr, names);
635 fprintf (stderr, ", must_exist=%d, find_all=%d, path=%s)\n",
636 must_exist, all, path);
637 }
638 #endif /* KPSE_DEBUG */
639
640 /* kpathsea_find_file_generic in tex-file.c does the variable and
641 tilde expansion, so don't redo that here. Maybe we should have
642 done it differently, but we certainly don't want to create an
643 incompatibility now. */
644
645 /* First catch any absolute or explicit relative names. */
646 for (namep = names; *namep; namep++) {
647 if (kpathsea_absolute_p (kpse, *namep, true)) {
648 str_list_type abs_ret_list = absolute_search (kpse, *namep);
649 /* That search can only return a zero- or one-element list, so: */
650 if (!STR_LIST_EMPTY (abs_ret_list)) {
651 str_list_add (&ret_list, STR_LIST_FIRST_ELT (abs_ret_list));
652 if (!all) { /* if they only wanted one, we're done */
653 goto out;
654 }
655 }
656 } else {
657 all_absolute = false;
658 }
659 }
660 /* Shortcut: if we were only given absolute/explicit relative names,
661 we can skip the rest. Typically, if one name is absolute, they
662 all are, because our caller derived them from each other. */
663 if (all_absolute) {
664 #ifdef KPSE_DEBUG
665 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) {
666 unsigned i;
667 DEBUGF (" generic search: all absolute, candidates are:");
668 /* List might not be NULL-terminated, so can't use print_space_list. */
669 for (i = 0; i < STR_LIST_LENGTH (ret_list); i++) {
670 fprintf (stderr, " %s", STR_LIST_ELT (ret_list, i));
671 }
672 fputs (".\n", stderr);
673 }
674 #endif
675 goto out;
676 }
677
678 /* Look at each path element in turn. This is essentially the same
679 code as `path_search' above, unfortunately. */
680 for (elt = kpathsea_path_element (kpse, path); !done && elt;
681 elt = kpathsea_path_element (kpse, NULL)) {
682 str_list_type *found;
683 boolean allow_disk_search = true;
684
685 if (elt[0] == '!' && elt[1] == '!') {
686 /* !! magic string -> disallow disk searches. */
687 allow_disk_search = false;
688 elt += 2;
689 }
690
691 /* See elt-dirs.c for side effects of this function. */
692 kpathsea_normalize_path (kpse, elt);
693
694 /* Try ls-R, unless we're searching for texmf.cnf. */
695 found = kpse->followup_search
696 ? kpathsea_db_search_list (kpse, names, elt, all) : NULL;
697
698 /* Search the filesystem in the same cases as `path_search' above. */
699 if (allow_disk_search
700 && (!found || (must_exist && !STR_LIST (*found)))) {
701 str_llist_type *dirs = kpathsea_element_dirs (kpse, elt);
702 if (dirs && *dirs) {
703 if (!found) {
704 found = XTALLOC1 (str_list_type);
705 }
706 /* Search in the directories: */
707 *found = dir_list_search_list (kpse, dirs, names, all,
708 kpathsea_readable_file);
709 #ifdef KPSE_CASEFOLDING_SEARCH
710 if (!STR_LIST (*found) && kpse_cnf_p (kpathsea_var_value (kpse,
711 "texmf_casefold_search"))) {
712 /* Still nothing; search again, case-insensitively: */
713 *found = dir_list_search_list (kpse, dirs, names, all,
714 casefold_readable_file);
715 }
716 #endif
717 }
718 }
719
720 /* Did we find anything? */
721 if (found && STR_LIST (*found)) {
722 if (all) {
723 str_list_concat (&ret_list, *found);
724 } else {
725 str_list_add (&ret_list, STR_LIST_FIRST_ELT (*found));
726 done = true;
727 }
728 }
729 }
730
731 out:
732 /* Uniqify, since our paths can often end up finding the same file
733 more than once. */
734 str_list_uniqify (&ret_list);
735
736 /* Add NULL element to terminate return list if empty or multiple. */
737 if (STR_LIST_EMPTY (ret_list)
738 || (all && STR_LIST_LAST_ELT (ret_list) != NULL))
739 str_list_add (&ret_list, NULL);
740
741 if (kpse->followup_search == false) {
742 kpse->followup_search = true;
743 } else {
744 /* Record the filenames we found, if desired. And wrap them in a
745 debugging line if we're doing that. */
746 #ifdef KPSE_DEBUG
747 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH)) {
748 DEBUGF ("returning from generic search(");
749 print_space_list (stderr, names);
750 fputs (") =>", stderr);
751 }
752 #endif /* KPSE_DEBUG */
753 log_search (kpse, ret_list);
754 #ifdef KPSE_DEBUG
755 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH))
756 putc ('\n', stderr);
757 #endif /* KPSE_DEBUG */
758 }
759
760 #ifdef __DJGPP__
761 /* Undo any side effects. */
762 _djstat_flags = save_djgpp_flags;
763 #endif
764
765 return STR_LIST (ret_list);
766 }
767
768 /* Search PATH for the first NAME according to MUST_EXIST. */
769
770 string
771 kpathsea_path_search (kpathsea kpse, const_string path, const_string name,
772 boolean must_exist)
773 {
774 string *ret_list = search (kpse, path, name, must_exist, false);
775 string ret = *ret_list;
776 free (ret_list);
777 return ret;
778 }
779
780 /* Search PATH for all files named NAME. Might have been better not
781 to assert `must_exist' here, but it's too late to change. */
782
783 string *
784 kpathsea_all_path_search (kpathsea kpse, const_string path, const_string name)
785 {
786 string *ret = search (kpse, path, name, true, true);
787 return ret;
788 }
789
790 #if defined (KPSE_COMPAT_API)
791 string
792 kpse_path_search (const_string path, const_string name, boolean must_exist)
793 {
794 return kpathsea_path_search (kpse_def, path, name, must_exist);
795 }
796
797 string *
798 kpse_all_path_search (const_string path, const_string name)
799 {
800 return kpathsea_all_path_search (kpse_def, path, name);
801 }
802 #endif /* KPSE_COMPAT_API */
803
804
805 #ifdef TEST
806
807 /* Each element of L on its own line, prefixed by a tab. */
808
809 static void
810 print_tab_list (string *l)
811 {
812 while (l && *l) {
813 if (*l)
814 putchar ('\t');
815 printf ("%s\n", *l);
816 l++;
817 }
818 }
819
820 static void
821 test_path_search (const_string path, const_string file)
822 {
823 string answer;
824 string *answer_list;
825
826 printf ("\nSearch %s for %s:\t", path, file);
827 answer = kpse_path_search (path, file, 0);
828 puts (answer ? answer : "(nil)");
829
830 printf ("Search %s for all %s:\t", path, file);
831 answer_list = kpse_all_path_search (path, file);
832 putchar ('\n');
833 print_tab_list (answer_list);
834 }
835
836 static void
837 test_path_search_list_generic (void)
838 {
839 const_string path = "/u/karl/.fonts";
840 /* absolute: should return just console/both, because no case
841 sensitive match */
842 // string names[] = { "/u/karl/.fonts/lucidaConsoleDK.otf",
843 // "/u/karl/.fonts/lucidaGrandeMonoDK.otf", NULL };
844
845 /* dirs: should return just grande, because case sensitive wins */
846 // string names[] = { "lucidaConsoleDK.otf", "LucidaGrandeMonoDK.otf", NULL };
847
848 /* dirs: should return just console/both, because no case sensitive match */
849 string names[] = { "lucidaConsoleDK.otf", "lucidaGrandeMonoDK.otf", NULL };
850
851 boolean all;
852 string *answer_list;
853
854 printf ("\nGeneric search %s for ", path);
855 print_space_list (stdout, names);
856 puts (":\t");
857
858 all = false; answer_list
859 = kpathsea_path_search_list_generic (kpse_def, path, names, false, all);
860 puts (answer_list && *answer_list ? *answer_list : "(nil)");
861
862 printf ("\nGeneric search %s for all ", path);
863 print_space_list (stdout, names);
864 puts (":");
865
866 all = true; answer_list
867 = kpathsea_path_search_list_generic (kpse_def, path, names, false, all);
868 print_tab_list (answer_list);
869 }
870
871 #define TEXFONTS "/usr/local/texlive-rel/texmf-dist/fonts"
872
873 int
874 main (int argc, char **argv)
875 {
876 xputenv ("KPATHSEA_DEBUG", "-1"); /* must be before setting progname */
877 kpse_set_program_name (argv[0], NULL);
878
879 xputenv ("MALLOC_CHECK_", "3");
880 xputenv ("MALLOC_PERTURB_", "75");
881
882 xputenv ("texmf_casefold_search", "1");
883 test_path_search (".:/k", "readme");
884 exit (0);
885 test_path_search_list_generic ();
886 exit (0);
887
888 xputenv ("TEXMFCNF", "/nc");
889 /* casefolding with absolute search: */
890 test_path_search ("/k", "/u/karl/.fonts/lucidaConsoleDK.otf");
891 /* casefolding with directory search: */
892 test_path_search ("/u/karl/.fonts", "Lucidaconsoledk.otf");
893 /* exit (0); */
894 xputenv ("texmf_casefold_search", "0");
895 /* should fail since no casefolding: */
896 test_path_search ("/u/karl/.fonts", "lucidaconsoledk.otf");
897
898 /* All lists end with NULL. */
899 test_path_search (".", "nonexistent");
900 test_path_search (".", "/nonexistent");
901 test_path_search ("/k" ENV_SEP_STRING ".", "README");
902 test_path_search ("/k" ENV_SEP_STRING ".", "/etc/fstab");
903 test_path_search ("." ENV_SEP_STRING TEXFONTS "//", "cmr10.tfm");
904 test_path_search ("." ENV_SEP_STRING TEXFONTS "//", "logo10.tfm");
905 test_path_search (TEXFONTS "//times" ENV_SEP_STRING "."
906 ENV_SEP_STRING ENV_SEP_STRING, "ptmr.vf");
907 test_path_search (TEXFONTS ENV_SEP_STRING
908 "/u/karl/.fonts", "LucidaConsoleDK.otf");
909
910 test_path_search ("~karl", ".profile"); // nil, no ~ expansion on path
911 test_path_search ("/k", "~karl/.profile");
912
913 xputenv ("NONEXIST", "nonexistent");
914 test_path_search (".", "$NONEXIST");
915 xputenv ("KPATHSEA", "kpathsea");
916 test_path_search ("/k" ENV_SEP_STRING "./doc", "$KPATHSEA.texi");
917 test_path_search ("/k" ENV_SEP_STRING "./doc", "${KPATHSEA}.texi");
918 test_path_search ("$KPATHSEA" ENV_SEP_STRING ".", "README");
919 test_path_search ("." ENV_SEP_STRING "$KPATHSEA", "README");
920
921 return 0;
922 }
923
924 #endif /* TEST */
925
926
927 /* ${wc} is the corresponding build directory.
928 -DMAKE_KPSE_DLL for inlined str_list_init etc.
929 Memory checking: -fsanitize=address or -lefence.
930 Local variables:
931 standalone-compile-command: "make --no-print-dir -C ${wc} && gcc -g -I. -I.. -I${wc}/.. -DMAKE_KPSE_DLL -DTEST pathsearch.c ${wc}/.libs/libkpathsea.a && ./a.out"
932 End:
933 */