4378
|
1 /* pathsearch.c: look up a filename in a path. |
|
2 |
4385
|
3 Copyright (C) 1993, 94, 95, 96, 97, 98 Karl Berry. |
4378
|
4 Copyright (C) 1993, 94, 95, 96, 97 Karl Berry & O. Weber. |
4385
|
5 Copyright (C) 1992, 93, 94, 95, 96 Free Software Foundation, Inc. |
4378
|
6 |
|
7 This library is free software; you can redistribute it and/or |
|
8 modify it under the terms of the GNU Library General Public |
|
9 License as published by the Free Software Foundation; either |
|
10 version 2 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 Library General Public License for more details. |
|
16 |
|
17 You should have received a copy of the GNU Library General Public |
|
18 License along with this library; if not, write to the Free Software |
|
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
|
20 |
|
21 #if defined (HAVE_CONFIG_H) |
|
22 #include <config.h> |
|
23 #endif |
|
24 |
|
25 #include "kpse-config.h" |
4379
|
26 #include "kpse-xfns.h" |
4378
|
27 #include "kpse.h" |
|
28 |
4385
|
29 /* c-std.h: the first header files. */ |
|
30 |
|
31 /* Header files that essentially all of our sources need, and |
|
32 that all implementations have. We include these first, to help with |
|
33 NULL being defined multiple times. */ |
|
34 #include <cstdio> |
|
35 #include <cstdarg> |
|
36 #include <cstdlib> |
|
37 #include <cstring> |
|
38 #include <climits> |
|
39 #include <cerrno> |
|
40 #include <cassert> |
|
41 |
|
42 #ifdef HAVE_UNISTD_H |
|
43 #ifdef HAVE_SYS_TYPES_H |
|
44 #include <sys/types.h> |
|
45 #endif |
|
46 #include <unistd.h> |
|
47 #endif |
|
48 |
|
49 #ifdef WIN32 |
|
50 #include <malloc.h> |
|
51 #endif /* not WIN32 */ |
|
52 |
|
53 #include "sysdir.h" |
|
54 #include "statdefs.h" |
|
55 |
|
56 /* define NAME_MAX, the maximum length of a single |
|
57 component in a filename. No such limit may exist, or may vary |
|
58 depending on the filesystem. */ |
|
59 |
|
60 /* Most likely the system will truncate filenames if it is not POSIX, |
|
61 and so we can use the BSD value here. */ |
|
62 #ifndef _POSIX_NAME_MAX |
|
63 #define _POSIX_NAME_MAX 255 |
|
64 #endif |
|
65 |
|
66 #ifndef NAME_MAX |
|
67 #define NAME_MAX _POSIX_NAME_MAX |
|
68 #endif |
|
69 |
|
70 /* c-ctype.h: ASCII-safe versions of the <ctype.h> macros. */ |
|
71 |
|
72 #include <cctype> |
|
73 |
|
74 /* What separates elements in environment variable path lists? */ |
|
75 #ifndef ENV_SEP |
|
76 #ifdef DOSISH |
|
77 #define ENV_SEP ';' |
|
78 #define ENV_SEP_STRING ";" |
|
79 #else |
|
80 #define ENV_SEP ':' |
|
81 #define ENV_SEP_STRING ":" |
|
82 #endif /* not DOS */ |
|
83 #endif /* not ENV_SEP */ |
|
84 |
|
85 #ifndef IS_ENV_SEP |
|
86 #define IS_ENV_SEP(ch) ((ch) == ENV_SEP) |
|
87 #endif |
|
88 |
|
89 /* c-pathmx.h: define PATH_MAX, the maximum length of a filename. |
|
90 Since no such limit may exist, it's preferable to dynamically grow |
|
91 filenames as needed. */ |
|
92 |
|
93 /* Cheat and define this as a manifest constant no matter what, instead |
|
94 of using pathconf. I forget why we want to do this. */ |
|
95 |
|
96 #ifndef _POSIX_PATH_MAX |
|
97 #define _POSIX_PATH_MAX 255 |
|
98 #endif |
|
99 |
|
100 #ifndef PATH_MAX |
|
101 #ifdef MAXPATHLEN |
|
102 #define PATH_MAX MAXPATHLEN |
|
103 #else |
|
104 #define PATH_MAX _POSIX_PATH_MAX |
|
105 #endif |
|
106 #endif /* not PATH_MAX */ |
|
107 |
|
108 /* debug.h: Runtime tracing. */ |
|
109 |
|
110 /* If NO_DEBUG is defined (not recommended), skip all this. */ |
|
111 #ifndef NO_DEBUG |
|
112 |
|
113 /* OK, we'll have tracing support. */ |
|
114 #define KPSE_DEBUG |
|
115 |
|
116 /* Set a bit. */ |
|
117 #define KPSE_DEBUG_SET(bit) kpathsea_debug |= 1 << (bit) |
|
118 |
|
119 /* Test if a bit is on. */ |
|
120 #define KPSE_DEBUG_P(bit) (kpathsea_debug & (1 << (bit))) |
|
121 |
|
122 #define KPSE_DEBUG_STAT 0 /* stat calls */ |
|
123 #define KPSE_DEBUG_HASH 1 /* hash lookups */ |
|
124 #define KPSE_DEBUG_FOPEN 2 /* fopen/fclose calls */ |
|
125 #define KPSE_DEBUG_PATHS 3 /* search path initializations */ |
|
126 #define KPSE_DEBUG_EXPAND 4 /* path element expansion */ |
|
127 #define KPSE_DEBUG_SEARCH 5 /* searches */ |
|
128 #define KPSE_DEBUG_VARS 6 /* variable values */ |
|
129 #define KPSE_LAST_DEBUG KPSE_DEBUG_VARS |
|
130 |
|
131 /* A printf for the debugging. */ |
|
132 #define DEBUGF_START() do { fputs ("kdebug:", stderr) |
|
133 #define DEBUGF_END() fflush (stderr); } while (0) |
|
134 |
|
135 #define DEBUGF(str) \ |
|
136 DEBUGF_START (); fputs (str, stderr); DEBUGF_END () |
|
137 #define DEBUGF1(str, e1) \ |
|
138 DEBUGF_START (); fprintf (stderr, str, e1); DEBUGF_END () |
|
139 #define DEBUGF2(str, e1, e2) \ |
|
140 DEBUGF_START (); fprintf (stderr, str, e1, e2); DEBUGF_END () |
|
141 #define DEBUGF3(str, e1, e2, e3) \ |
|
142 DEBUGF_START (); fprintf (stderr, str, e1, e2, e3); DEBUGF_END () |
|
143 #define DEBUGF4(str, e1, e2, e3, e4) \ |
|
144 DEBUGF_START (); fprintf (stderr, str, e1, e2, e3, e4); DEBUGF_END () |
|
145 |
|
146 #undef fopen |
|
147 #define fopen kpse_fopen_trace |
|
148 extern FILE *fopen (const char *filename, const char *mode); |
|
149 #undef fclose |
|
150 #define fclose kpse_fclose_trace |
|
151 extern int fclose (FILE *); |
|
152 |
|
153 #endif /* not NO_DEBUG */ |
|
154 |
|
155 #if defined (WIN32) && !defined (__MINGW32__) |
|
156 |
|
157 /* System description file for Windows NT. */ |
|
158 |
|
159 /* |
|
160 * Define symbols to identify the version of Unix this is. |
|
161 * Define all the symbols that apply correctly. |
|
162 */ |
|
163 |
|
164 #ifndef DOSISH |
|
165 #define DOSISH |
|
166 #endif |
|
167 |
|
168 #ifndef MAXPATHLEN |
|
169 #define MAXPATHLEN _MAX_PATH |
4378
|
170 #endif |
|
171 |
4385
|
172 #define HAVE_DUP2 1 |
|
173 #define HAVE_RENAME 1 |
|
174 #define HAVE_RMDIR 1 |
|
175 #define HAVE_MKDIR 1 |
|
176 #define HAVE_GETHOSTNAME 1 |
|
177 #define HAVE_RANDOM 1 |
|
178 #define USE_UTIME 1 |
|
179 #define HAVE_MOUSE 1 |
|
180 #define HAVE_TZNAME 1 |
|
181 |
|
182 /* These have to be defined because our compilers treat __STDC__ as being |
|
183 defined (most of them anyway). */ |
|
184 |
|
185 #define access _access |
|
186 #define stat _stat |
|
187 #define strcasecmp _stricmp |
|
188 #define strdup _strdup |
|
189 #define strncasecmp _strnicmp |
|
190 |
|
191 #define S_IFMT _S_IFMT |
|
192 #define S_IFDIR _S_IFDIR |
|
193 |
|
194 /* Define this so that winsock.h definitions don't get included when |
|
195 windows.h is... For this to have proper effect, config.h must |
|
196 always be included before windows.h. */ |
|
197 #define _WINSOCKAPI_ 1 |
|
198 |
|
199 #include <windows.h> |
|
200 |
|
201 /* Defines size_t and alloca (). */ |
|
202 #include <malloc.h> |
|
203 |
|
204 /* For proper declaration of environ. */ |
|
205 #include <io.h> |
|
206 #include <fcntl.h> |
|
207 #include <process.h> |
|
208 |
|
209 /* ============================================================ */ |
|
210 |
|
211 #endif /* WIN32 */ |
|
212 |
|
213 /* hash.h: declarations for a hash table. */ |
|
214 |
|
215 /* A single (key,value) pair. */ |
|
216 typedef struct hash_element_struct |
|
217 { |
|
218 const char *key; |
|
219 const char *value; |
|
220 struct hash_element_struct *next; |
|
221 } hash_element_type; |
|
222 |
|
223 /* The usual arrangement of buckets initialized to null. */ |
|
224 typedef struct |
|
225 { |
|
226 hash_element_type **buckets; |
|
227 unsigned size; |
|
228 } hash_table_type; |
|
229 |
|
230 static hash_table_type hash_create (unsigned size); |
|
231 |
|
232 |
|
233 #ifdef KPSE_DEBUG |
|
234 /* How to print the hash results when debugging. */ |
|
235 extern int kpse_debug_hash_lookup_int; |
|
236 #endif |
|
237 |
|
238 /* fn.h: arbitrarily long filenames (or just strings). */ |
|
239 |
|
240 /* Arbitrarily long filenames; it's inconvenient to use obstacks here, |
|
241 because we want to maintain a null terminator. Also used for |
|
242 dynamically growing strings even when the null byte isn't necessary, |
|
243 e.g., in `variable.c', since I don't want to pass obstacks around |
|
244 everywhere, and one can't free parts of an obstack arbitrarily. */ |
|
245 |
|
246 typedef struct |
|
247 { |
|
248 char *str; |
|
249 unsigned allocated; |
|
250 unsigned length; /* includes the terminating null byte, if any */ |
|
251 } fn_type; |
|
252 |
|
253 #define FN_STRING(fn) ((fn).str) |
|
254 #define FN_ALLOCATED(fn) ((fn).allocated) |
|
255 #define FN_LENGTH(fn) ((fn).length) |
|
256 |
|
257 /* lib.h: other stuff. */ |
|
258 |
|
259 /* Define common sorts of messages. */ |
|
260 |
|
261 /* This should be called only after a system call fails. Don't exit |
|
262 with status `errno', because that might be 256, which would mean |
|
263 success (exit statuses are truncated to eight bits). */ |
|
264 #define FATAL_PERROR(str) do { \ |
|
265 fputs ("pathsearch: ", stderr); \ |
|
266 perror (str); exit (EXIT_FAILURE); } while (0) |
|
267 |
|
268 #define START_FATAL() do { \ |
|
269 fputs ("pathsearch: fatal: ", stderr); |
|
270 #define END_FATAL() fputs (".\n", stderr); exit (1); } while (0) |
|
271 |
|
272 #define FATAL(str) \ |
|
273 START_FATAL (); fputs (str, stderr); END_FATAL () |
|
274 #define FATAL1(str, e1) \ |
|
275 START_FATAL (); fprintf (stderr, str, e1); END_FATAL () |
|
276 #define FATAL2(str, e1, e2) \ |
|
277 START_FATAL (); fprintf (stderr, str, e1, e2); END_FATAL () |
|
278 #define FATAL3(str, e1, e2, e3) \ |
|
279 START_FATAL (); fprintf (stderr, str, e1, e2, e3); END_FATAL () |
|
280 #define FATAL4(str, e1, e2, e3, e4) \ |
|
281 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4); END_FATAL () |
|
282 #define FATAL5(str, e1, e2, e3, e4, e5) \ |
|
283 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5); END_FATAL () |
|
284 #define FATAL6(str, e1, e2, e3, e4, e5, e6) \ |
|
285 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5, e6); END_FATAL () |
|
286 |
|
287 |
|
288 #define START_WARNING() do { fputs ("warning: ", stderr) |
|
289 #define END_WARNING() fputs (".\n", stderr); fflush (stderr); } while (0) |
|
290 |
|
291 #define WARNING(str) \ |
|
292 START_WARNING (); fputs (str, stderr); END_WARNING () |
|
293 #define WARNING1(str, e1) \ |
|
294 START_WARNING (); fprintf (stderr, str, e1); END_WARNING () |
|
295 #define WARNING2(str, e1, e2) \ |
|
296 START_WARNING (); fprintf (stderr, str, e1, e2); END_WARNING () |
|
297 #define WARNING3(str, e1, e2, e3) \ |
|
298 START_WARNING (); fprintf (stderr, str, e1, e2, e3); END_WARNING () |
|
299 #define WARNING4(str, e1, e2, e3, e4) \ |
|
300 START_WARNING (); fprintf (stderr, str, e1, e2, e3, e4); END_WARNING () |
|
301 |
|
302 |
|
303 /* I find this easier to read. */ |
|
304 #define STREQ(s1, s2) (strcmp (s1, s2) == 0) |
|
305 #define STRNEQ(s1, s2, n) (strncmp (s1, s2, n) == 0) |
|
306 |
|
307 /* Support for FAT/ISO-9660 filesystems. Theoretically this should be |
|
308 done at runtime, per filesystem, but that's painful to program. */ |
|
309 #ifdef MONOCASE_FILENAMES |
|
310 #define FILESTRCASEEQ(s1, s2) (strcasecmp (s1, s2) == 0) |
|
311 #define FILESTRNCASEEQ(s1, s2, l) (strncasecmp (s1, s2, l) == 0) |
|
312 #define FILECHARCASEEQ(c1, c2) (toupper (c1) == toupper (c2)) |
|
313 #else |
|
314 #define FILESTRCASEEQ STREQ |
|
315 #define FILESTRNCASEEQ STRNEQ |
|
316 #define FILECHARCASEEQ(c1, c2) ((c1) == (c2)) |
|
317 #endif |
|
318 |
|
319 /* This is the maximum number of numerals that result when a 64-bit |
|
320 integer is converted to a string, plus one for a trailing null byte, |
|
321 plus one for a sign. */ |
|
322 #define MAX_INT_LENGTH 21 |
|
323 |
|
324 /* If the environment variable TEST is set, return it; otherwise, |
|
325 DEFAULT. This is useful for paths that use more than one envvar. */ |
|
326 #define ENVVAR(test, default) (getenv (test) ? (test) : (default)) |
|
327 |
|
328 |
|
329 /* (Re)Allocate N items of type T using xmalloc/xrealloc. */ |
|
330 #define XTALLOC(n, t) ((t *) xmalloc ((n) * sizeof (t))) |
|
331 #define XTALLOC1(t) XTALLOC (1, t) |
|
332 #define XRETALLOC(addr, n, t) ((addr) = (t *) xrealloc (addr, (n) * sizeof(t))) |
|
333 |
|
334 extern "C" char *xbasename (const char *name); |
|
335 |
|
336 static FILE *xfopen (const char *filename, const char *mode); |
|
337 |
|
338 static void xfclose (FILE *f, const char *filename); |
|
339 |
|
340 unsigned long xftell (FILE *f, char *filename); |
|
341 |
4386
|
342 #ifndef WIN32 |
4385
|
343 static void xclosedir (DIR *d); |
4386
|
344 #endif |
4385
|
345 |
|
346 static void *xmalloc (unsigned size); |
|
347 |
|
348 static void *xrealloc (void *old_ptr, unsigned size); |
|
349 |
|
350 static char *xstrdup (const char *s); |
|
351 |
|
352 extern char *xbasename (const char *name); |
|
353 |
|
354 static int dir_p (const char *fn); |
|
355 |
|
356 #ifndef WIN32 |
|
357 int dir_links (const char *fn); |
|
358 #endif |
|
359 |
|
360 static unsigned hash (hash_table_type table, const char *key); |
|
361 |
|
362 static void hash_insert (hash_table_type *table, const char *key, |
|
363 const char *value); |
|
364 |
|
365 static char **hash_lookup (hash_table_type table, const char *key); |
|
366 |
|
367 static void hash_print (hash_table_type table, int summary_only); |
|
368 |
|
369 static char *concat (const char *s1, const char *s2); |
|
370 |
|
371 static char *concat3 (const char *s1, const char *s2, const char *s3); |
|
372 |
|
373 static char *concatn (const char *str1, ...); |
|
374 |
|
375 static char *find_suffix (const char *name); |
|
376 |
|
377 static char *kpse_truncate_filename (const char *name); |
|
378 |
|
379 static char *kpse_readable_file (const char *name); |
|
380 |
|
381 static int kpse_absolute_p (const char *filename, int relative_ok); |
|
382 |
|
383 static str_list_type str_list_init (void); |
|
384 |
|
385 static void str_list_add (str_list_type *l, char *s); |
|
386 |
|
387 static void str_list_concat (str_list_type *target, str_list_type more); |
|
388 |
|
389 static void str_list_free (str_list_type *l); |
|
390 |
|
391 static void str_llist_add (str_llist_type *l, char *str); |
|
392 |
|
393 static void str_llist_float (str_llist_type *l, str_llist_elt_type *mover); |
|
394 |
|
395 static fn_type fn_init (void); |
|
396 |
|
397 static fn_type fn_copy0 (const char *s, unsigned len); |
|
398 |
|
399 static void fn_free (fn_type *f); |
|
400 |
|
401 static void grow (fn_type *f, unsigned len); |
|
402 |
|
403 static void fn_1grow (fn_type *f, char c); |
|
404 |
|
405 static void fn_grow (fn_type *f, void *source, unsigned len); |
|
406 |
|
407 static void fn_str_grow (fn_type *f, const char *s); |
|
408 |
|
409 static void fn_shrink_to (fn_type *f, unsigned loc); |
|
410 |
|
411 static char *kpse_var_value (const char *var); |
|
412 |
|
413 static void expanding (const char *var, int xp); |
|
414 |
|
415 static int expanding_p (const char *var); |
|
416 |
|
417 static void expand (fn_type *expansion, const char *start, const char *end); |
|
418 |
|
419 static char *kpse_var_expand (const char *src); |
|
420 |
|
421 #include <ctime> /* for `time' */ |
|
422 |
4378
|
423 /* The very first search is for texmf.cnf, called when someone tries to |
|
424 initialize the TFM path or whatever. init_path calls kpse_cnf_get |
|
425 which calls kpse_all_path_search to find all the texmf.cnf's. We |
|
426 need to do various special things in this case, since we obviously |
|
427 don't yet have the configuration files when we're searching for the |
|
428 configuration files. */ |
|
429 static bool first_search = true; |
|
430 |
|
431 |
|
432 |
|
433 /* This function is called after every search (except the first, since |
|
434 we definitely want to allow enabling the logging in texmf.cnf) to |
|
435 record the filename(s) found in $TEXMFLOG. */ |
|
436 |
|
437 static void |
|
438 log_search (str_list_type filenames) |
|
439 { |
|
440 static FILE *log_file = NULL; |
|
441 static bool first_time = true; /* Need to open the log file? */ |
|
442 |
|
443 if (first_time) { |
|
444 /* Get name from either envvar or config file. */ |
|
445 char *log_name = kpse_var_value ("TEXMFLOG"); |
|
446 first_time = false; |
|
447 if (log_name) { |
4385
|
448 log_file = xfopen (log_name, "a"); |
4378
|
449 if (!log_file) |
|
450 perror (log_name); |
|
451 free (log_name); |
|
452 } |
|
453 } |
|
454 |
|
455 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH) || log_file) { |
|
456 unsigned e; |
|
457 |
|
458 /* FILENAMES should never be null, but safety doesn't hurt. */ |
|
459 for (e = 0; e < STR_LIST_LENGTH (filenames) && STR_LIST_ELT (filenames, e); |
|
460 e++) { |
|
461 char *filename = STR_LIST_ELT (filenames, e); |
|
462 |
|
463 /* Only record absolute filenames, for privacy. */ |
|
464 if (log_file && kpse_absolute_p (filename, false)) |
|
465 fprintf (log_file, "%lu %s\n", (long unsigned) time (NULL), |
|
466 filename); |
|
467 |
|
468 /* And show them online, if debugging. We've already started |
|
469 the debugging line in `search', where this is called, so |
|
470 just print the filename here, don't use DEBUGF. */ |
|
471 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
472 fputs (filename, stderr); |
|
473 } |
|
474 } |
|
475 } |
|
476 |
|
477 /* Concatenate each element in DIRS with NAME (assume each ends with a |
|
478 /, to save time). If SEARCH_ALL is false, return the first readable |
|
479 regular file. Else continue to search for more. In any case, if |
|
480 none, return a list containing just NULL. |
|
481 |
|
482 We keep a single buffer for the potential filenames and reallocate |
|
483 only when necessary. I'm not sure it's noticeably faster, but it |
|
484 does seem cleaner. (We do waste a bit of space in the return |
|
485 value, though, since we don't shrink it to the final size returned.) */ |
|
486 |
|
487 #define INIT_ALLOC 75 /* Doesn't much matter what this number is. */ |
|
488 |
|
489 static str_list_type |
|
490 dir_list_search (str_llist_type *dirs, const char *name, bool search_all) |
|
491 { |
|
492 str_llist_elt_type *elt; |
|
493 str_list_type ret; |
|
494 unsigned name_len = strlen (name); |
|
495 unsigned allocated = INIT_ALLOC; |
|
496 char *potential = (char *) xmalloc (allocated); |
|
497 |
|
498 ret = str_list_init (); |
|
499 |
|
500 for (elt = *dirs; elt; elt = STR_LLIST_NEXT (*elt)) |
|
501 { |
|
502 const char *dir = STR_LLIST (*elt); |
|
503 unsigned dir_len = strlen (dir); |
|
504 |
|
505 while (dir_len + name_len + 1 > allocated) |
|
506 { |
|
507 allocated += allocated; |
|
508 XRETALLOC (potential, allocated, char); |
|
509 } |
|
510 |
|
511 strcpy (potential, dir); |
|
512 strcat (potential, name); |
|
513 |
|
514 if (kpse_readable_file (potential)) |
|
515 { |
|
516 str_list_add (&ret, potential); |
|
517 |
|
518 /* Move this element towards the top of the list. */ |
|
519 str_llist_float (dirs, elt); |
|
520 |
|
521 /* If caller only wanted one file returned, no need to |
|
522 terminate the list with NULL; the caller knows to only look |
|
523 at the first element. */ |
|
524 if (!search_all) |
|
525 return ret; |
|
526 |
|
527 /* Start new filename. */ |
|
528 allocated = INIT_ALLOC; |
|
529 potential = (char *) xmalloc (allocated); |
|
530 } |
|
531 } |
|
532 |
|
533 /* If we get here, either we didn't find any files, or we were finding |
|
534 all the files. But we're done with the last filename, anyway. */ |
|
535 free (potential); |
|
536 |
|
537 return ret; |
|
538 } |
|
539 |
|
540 /* This is called when NAME is absolute or explicitly relative; if it's |
|
541 readable, return (a list containing) it; otherwise, return NULL. */ |
|
542 |
|
543 static str_list_type |
|
544 absolute_search (char *name) |
|
545 { |
|
546 str_list_type ret_list; |
|
547 char *found = kpse_readable_file (name); |
|
548 |
|
549 /* Some old compilers can't initialize structs. */ |
|
550 ret_list = str_list_init (); |
|
551 |
|
552 /* If NAME wasn't found, free the expansion. */ |
|
553 if (name != found) |
|
554 free (name); |
|
555 |
|
556 /* Add `found' to the return list even if it's null; that tells |
|
557 the caller we didn't find anything. */ |
|
558 str_list_add (&ret_list, found); |
|
559 |
|
560 return ret_list; |
|
561 } |
|
562 |
|
563 /* This is the hard case -- look for NAME in PATH. If ALL is false, |
|
564 return the first file found. Otherwise, search all elements of PATH. */ |
|
565 |
|
566 static str_list_type |
|
567 path_search (const char *path, char *name, bool must_exist, bool all) |
|
568 { |
|
569 char *elt; |
|
570 str_list_type ret_list; |
|
571 bool done = false; |
|
572 ret_list = str_list_init (); /* some compilers lack struct initialization */ |
|
573 |
|
574 for (elt = kpse_path_element (path); !done && elt; |
|
575 elt = kpse_path_element (NULL)) { |
|
576 str_list_type *found; |
|
577 bool allow_disk_search = true; |
|
578 |
|
579 if (*elt == '!' && *(elt + 1) == '!') { |
|
580 /* Those magic leading chars in a path element means don't search the |
|
581 disk for this elt. And move past the magic to get to the name. */ |
|
582 allow_disk_search = false; |
|
583 elt += 2; |
|
584 } |
|
585 |
|
586 /* Do not touch the device if present */ |
|
587 if (NAME_BEGINS_WITH_DEVICE (elt)) { |
|
588 while (IS_DIR_SEP (*(elt + 2)) && IS_DIR_SEP (*(elt + 3))) { |
|
589 *(elt + 2) = *(elt + 1); |
|
590 *(elt + 1) = *elt; |
|
591 elt++; |
|
592 } |
|
593 } else { |
|
594 /* We never want to search the whole disk. */ |
|
595 while (IS_DIR_SEP (*elt) && IS_DIR_SEP (*(elt + 1))) |
|
596 elt++; |
|
597 } |
|
598 |
|
599 /* Try ls-R, unless we're searching for texmf.cnf. Our caller |
|
600 (search), also tests first_search, and does the resetting. */ |
|
601 found = first_search ? NULL : kpse_db_search (name, elt, all); |
|
602 |
|
603 /* Search the filesystem if (1) the path spec allows it, and either |
|
604 (2a) we are searching for texmf.cnf ; or |
|
605 (2b) no db exists; or |
|
606 (2c) no db's are relevant to this elt; or |
|
607 (3) MUST_EXIST && NAME was not in the db. |
|
608 In (2*), `found' will be NULL. |
|
609 In (3), `found' will be an empty list. */ |
|
610 if (allow_disk_search && (!found || (must_exist && !STR_LIST (*found)))) { |
|
611 str_llist_type *dirs = kpse_element_dirs (elt); |
|
612 if (dirs && *dirs) { |
|
613 if (!found) |
|
614 found = XTALLOC1 (str_list_type); |
|
615 *found = dir_list_search (dirs, name, all); |
|
616 } |
|
617 } |
|
618 |
|
619 /* Did we find anything anywhere? */ |
|
620 if (found && STR_LIST (*found)) |
|
621 if (all) |
|
622 str_list_concat (&ret_list, *found); |
|
623 else { |
|
624 str_list_add (&ret_list, STR_LIST_ELT (*found, 0)); |
|
625 done = true; |
|
626 } |
|
627 |
|
628 /* Free the list space, if any (but not the elements). */ |
|
629 if (found) { |
|
630 str_list_free (found); |
|
631 free (found); |
|
632 } |
|
633 } |
|
634 |
|
635 /* Free the expanded name we were passed. It can't be in the return |
|
636 list, since the path directories got unconditionally prepended. */ |
|
637 free (name); |
|
638 |
|
639 return ret_list; |
|
640 } |
|
641 |
|
642 /* Search PATH for ORIGINAL_NAME. If ALL is false, or ORIGINAL_NAME is |
|
643 absolute_p, check ORIGINAL_NAME itself. Otherwise, look at each |
|
644 element of PATH for the first readable ORIGINAL_NAME. |
|
645 |
|
646 Always return a list; if no files are found, the list will |
|
647 contain just NULL. If ALL is true, the list will be |
|
648 terminated with NULL. */ |
|
649 |
|
650 static char ** |
|
651 search (const char *path, const char *original_name, |
|
652 bool must_exist, bool all) |
|
653 { |
|
654 str_list_type ret_list; |
|
655 char *name; |
|
656 bool absolute_p; |
|
657 |
|
658 /* Make a leading ~ count as an absolute filename, and expand $FOO's. */ |
|
659 name = kpse_expand (original_name); |
|
660 |
|
661 /* If the first name is absolute or explicitly relative, no need to |
|
662 consider PATH at all. */ |
|
663 absolute_p = kpse_absolute_p (name, true); |
|
664 |
|
665 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
666 DEBUGF4 ("start search(file=%s, must_exist=%d, find_all=%d, path=%s).\n", |
|
667 name, must_exist, all, path); |
|
668 |
|
669 /* Find the file(s). */ |
|
670 ret_list = absolute_p ? absolute_search (name) |
|
671 : path_search (path, name, must_exist, all); |
|
672 |
|
673 /* Append NULL terminator if we didn't find anything at all, or we're |
|
674 supposed to find ALL and the list doesn't end in NULL now. */ |
|
675 if (STR_LIST_LENGTH (ret_list) == 0 |
|
676 || (all && STR_LIST_LAST_ELT (ret_list) != NULL)) |
|
677 str_list_add (&ret_list, NULL); |
|
678 |
|
679 /* The very first search is for texmf.cnf. We can't log that, since |
|
680 we want to allow setting TEXMFLOG in texmf.cnf. */ |
|
681 if (first_search) { |
|
682 first_search = false; |
|
683 } else { |
|
684 /* Record the filenames we found, if desired. And wrap them in a |
|
685 debugging line if we're doing that. */ |
|
686 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
687 DEBUGF1 ("search(%s) =>", original_name); |
|
688 log_search (ret_list); |
|
689 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
690 putc ('\n', stderr); |
|
691 } |
|
692 |
|
693 return STR_LIST (ret_list); |
|
694 } |
|
695 |
|
696 /* Search PATH for the first NAME. */ |
|
697 |
|
698 char * |
|
699 kpse_path_search (const char *path, const char *name, bool must_exist) |
|
700 { |
|
701 static char **ret_list = 0; |
|
702 |
|
703 if (ret_list) |
|
704 { |
|
705 free (ret_list); |
|
706 ret_list = 0; /* Don't let an interrupt in search() cause trouble */ |
|
707 } |
|
708 |
|
709 ret_list = search (path, name, must_exist, false); |
|
710 |
|
711 return *ret_list; /* Freeing this is caller's responsibility */ |
|
712 } |
|
713 |
|
714 |
|
715 /* Search all elements of PATH for files named NAME. Not sure if it's |
|
716 right to assert `must_exist' here, but it suffices now. */ |
|
717 |
|
718 char ** |
|
719 kpse_all_path_search (const char *path, const char *name) |
|
720 { |
|
721 char **ret = search (path, name, true, true); |
|
722 return ret; |
|
723 } |
|
724 |
|
725 /* This is the hard case -- look in each element of PATH for each |
|
726 element of NAMES. If ALL is false, return the first file found. |
|
727 Otherwise, search all elements of PATH. */ |
|
728 |
|
729 static str_list_type |
|
730 path_find_first_of (const char *path, const char **names, |
|
731 bool must_exist, bool all) |
|
732 { |
|
733 const char **p; |
|
734 char *elt; |
|
735 const char *name; |
|
736 str_list_type ret_list; |
|
737 bool done = false; |
|
738 ret_list = str_list_init (); /* some compilers lack struct initialization */ |
|
739 |
|
740 for (elt = kpse_path_element (path); !done && elt; |
|
741 elt = kpse_path_element (NULL)) |
|
742 { |
|
743 str_llist_type *dirs; |
|
744 str_llist_elt_type *dirs_elt; |
|
745 str_list_type *found; |
|
746 bool allow_disk_search = true; |
|
747 |
|
748 if (*elt == '!' && *(elt + 1) == '!') |
|
749 { |
|
750 /* Those magic leading chars in a path element means don't |
|
751 search the disk for this elt. And move past the magic to |
|
752 get to the name. */ |
|
753 |
|
754 allow_disk_search = false; |
|
755 elt += 2; |
|
756 } |
|
757 |
|
758 /* Do not touch the device if present */ |
|
759 |
|
760 if (NAME_BEGINS_WITH_DEVICE (elt)) |
|
761 { |
|
762 while (IS_DIR_SEP (*(elt + 2)) && IS_DIR_SEP (*(elt + 3))) |
|
763 { |
|
764 *(elt + 2) = *(elt + 1); |
|
765 *(elt + 1) = *elt; |
|
766 elt++; |
|
767 } |
|
768 } |
|
769 else |
|
770 { |
|
771 /* We never want to search the whole disk. */ |
|
772 while (IS_DIR_SEP (*elt) && IS_DIR_SEP (*(elt + 1))) |
|
773 elt++; |
|
774 } |
|
775 |
|
776 /* We have to search one directory at a time. */ |
|
777 dirs = kpse_element_dirs (elt); |
|
778 for (dirs_elt = *dirs; dirs_elt; dirs_elt = STR_LLIST_NEXT (*dirs_elt)) |
|
779 { |
|
780 char *dir = STR_LLIST (*dirs_elt); |
|
781 |
|
782 for (p = names; !done && *p; p++) |
|
783 { |
|
784 name = *p; |
|
785 |
|
786 /* Try ls-R, unless we're searching for texmf.cnf. Our caller |
|
787 (find_first_of), also tests first_search, and does the |
|
788 resetting. */ |
|
789 found = first_search ? NULL : kpse_db_search (name, dir, all); |
|
790 |
|
791 /* Search the filesystem if (1) the path spec allows it, |
|
792 and either |
|
793 |
|
794 (2a) we are searching for texmf.cnf ; or |
|
795 (2b) no db exists; or |
|
796 (2c) no db's are relevant to this elt; or |
|
797 (3) MUST_EXIST && NAME was not in the db. |
|
798 |
|
799 In (2*), `found' will be NULL. |
|
800 In (3), `found' will be an empty list. */ |
|
801 |
|
802 if (allow_disk_search |
|
803 && (!found || (must_exist && !STR_LIST (*found)))) |
|
804 { |
|
805 static str_llist_type *tmp = 0; |
|
806 |
|
807 if (! tmp) |
|
808 { |
|
809 tmp = XTALLOC1 (str_llist_type); |
|
810 *tmp = NULL; |
|
811 str_llist_add (tmp, ""); |
|
812 } |
|
813 |
|
814 STR_LLIST (*(*tmp)) = dir; |
|
815 |
|
816 if (!found) |
|
817 found = XTALLOC1 (str_list_type); |
|
818 |
|
819 *found = dir_list_search (tmp, name, all); |
|
820 } |
|
821 |
|
822 /* Did we find anything anywhere? */ |
|
823 if (found && STR_LIST (*found)) |
|
824 { |
|
825 if (all) |
|
826 str_list_concat (&ret_list, *found); |
|
827 else |
|
828 { |
|
829 str_list_add (&ret_list, STR_LIST_ELT (*found, 0)); |
|
830 done = true; |
|
831 } |
|
832 } |
|
833 |
|
834 /* Free the list space, if any (but not the elements). */ |
|
835 if (found) |
|
836 { |
|
837 str_list_free (found); |
|
838 free (found); |
|
839 } |
|
840 } |
|
841 } |
|
842 } |
|
843 |
|
844 return ret_list; |
|
845 } |
|
846 |
|
847 static char ** |
|
848 find_first_of (const char *path, const char **names, |
|
849 bool must_exist, bool all) |
|
850 { |
|
851 str_list_type ret_list; |
|
852 |
|
853 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
854 { |
|
855 const char **p; |
|
856 fputs ("start find_first_of((", stderr); |
|
857 for (p = names; *p; p++) |
|
858 { |
|
859 if (p == names) |
|
860 fputs (*p, stderr); |
|
861 else |
|
862 fprintf (stderr, ", %s", *p); |
|
863 } |
|
864 fprintf (stderr, "), path=%s, must_exist=%d).\n", path, must_exist); |
|
865 } |
|
866 |
|
867 /* Find the file. */ |
|
868 ret_list = path_find_first_of (path, names, must_exist, all); |
|
869 |
|
870 /* Append NULL terminator if we didn't find anything at all, or we're |
|
871 supposed to find ALL and the list doesn't end in NULL now. */ |
|
872 if (STR_LIST_LENGTH (ret_list) == 0 |
|
873 || (all && STR_LIST_LAST_ELT (ret_list) != NULL)) |
|
874 str_list_add (&ret_list, NULL); |
|
875 |
|
876 /* The very first search is for texmf.cnf. We can't log that, since |
|
877 we want to allow setting TEXMFLOG in texmf.cnf. */ |
|
878 if (first_search) { |
|
879 first_search = false; |
|
880 } else { |
|
881 /* Record the filenames we found, if desired. And wrap them in a |
|
882 debugging line if we're doing that. */ |
|
883 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
884 { |
|
885 const char **p; |
|
886 fputs ("find_first_of(", stderr); |
|
887 for (p = names; *p; p++) |
|
888 { |
|
889 if (p == names) |
|
890 fputs (*p, stderr); |
|
891 else |
|
892 fprintf (stderr, ", %s", *p); |
|
893 } |
|
894 fputs (") =>", stderr); |
|
895 } |
|
896 log_search (ret_list); |
|
897 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
898 putc ('\n', stderr); |
|
899 } |
|
900 |
|
901 return STR_LIST (ret_list); |
|
902 } |
|
903 |
|
904 /* Search each element of PATH for each element of NAMES. Return the |
|
905 first one found. */ |
|
906 |
|
907 char * |
|
908 kpse_path_find_first_of (const char *path, const char **names, |
|
909 bool must_exist) |
|
910 { |
|
911 static char **ret_list = 0; |
|
912 |
|
913 if (ret_list) |
|
914 { |
|
915 free (ret_list); |
|
916 ret_list = 0; /* Don't let an interrupt in search() cause trouble */ |
|
917 } |
|
918 |
|
919 ret_list = find_first_of (path, names, must_exist, false); |
|
920 |
|
921 return *ret_list; /* Freeing this is caller's responsibility */ |
|
922 } |
|
923 |
|
924 /* Search each element of PATH for each element of NAMES and return a |
|
925 list containing everything found, in the order found. */ |
|
926 |
|
927 char ** |
|
928 kpse_all_path_find_first_of (const char *path, const char **names) |
|
929 { |
|
930 char **ret = find_first_of (path, names, true, true); |
|
931 return ret; |
|
932 } |
|
933 |
|
934 /* expand.c: general expansion. Some of this file (the brace-expansion |
|
935 code from bash) is covered by the GPL; this is the only GPL-covered |
|
936 code in kpathsea. The part of the file that I wrote (the first |
|
937 couple of functions) is covered by the LGPL. */ |
|
938 |
|
939 #ifdef HAVE_PWD_H |
|
940 #include <pwd.h> |
|
941 #endif |
|
942 |
|
943 /* If NAME has a leading ~ or ~user, Unix-style, expand it to the user's |
|
944 home directory, and return a new malloced string. If no ~, or no |
|
945 <pwd.h>, just return NAME. */ |
|
946 |
|
947 static char * |
|
948 kpse_tilde_expand (const char *name) |
|
949 { |
|
950 const char *expansion; |
|
951 const char *home; |
|
952 |
|
953 assert (name); |
|
954 |
|
955 /* If no leading tilde, do nothing. */ |
|
956 if (*name != '~') { |
|
957 expansion = name; |
|
958 |
|
959 /* If a bare tilde, return the home directory or `.'. (Very unlikely |
|
960 that the directory name will do anyone any good, but ... */ |
|
961 } else if (name[1] == 0) { |
|
962 expansion = xstrdup (getenv ("HOME")); |
|
963 if (!expansion) { |
|
964 expansion = xstrdup ("."); |
|
965 } |
|
966 |
|
967 /* If `~/', remove any trailing / or replace leading // in $HOME. |
|
968 Should really check for doubled intermediate slashes, too. */ |
|
969 } else if (IS_DIR_SEP (name[1])) { |
|
970 unsigned c = 1; |
|
971 home = getenv ("HOME"); |
|
972 if (!home) { |
|
973 home = "."; |
|
974 } |
|
975 if (IS_DIR_SEP (*home) && IS_DIR_SEP (home[1])) { /* handle leading // */ |
|
976 home++; |
|
977 } |
|
978 if (IS_DIR_SEP (home[strlen (home) - 1])) { /* omit / after ~ */ |
|
979 c++; |
|
980 } |
|
981 expansion = concat (home, name + c); |
|
982 |
|
983 /* If `~user' or `~user/', look up user in the passwd database (but |
|
984 OS/2 doesn't have this concept. */ |
|
985 } else |
|
986 #ifdef HAVE_PWD_H |
|
987 { |
|
988 struct passwd *p; |
|
989 char *user; |
|
990 unsigned c = 2; |
|
991 while (!IS_DIR_SEP (name[c]) && name[c] != 0) /* find user name */ |
|
992 c++; |
|
993 |
|
994 user = (char *) xmalloc (c); |
|
995 strncpy (user, name + 1, c - 1); |
|
996 user[c - 1] = 0; |
|
997 |
|
998 /* We only need the cast here for (deficient) systems |
|
999 which do not declare `getpwnam' in <pwd.h>. */ |
|
1000 p = (struct passwd *) getpwnam (user); |
|
1001 free (user); |
|
1002 |
|
1003 /* If no such user, just use `.'. */ |
|
1004 home = p ? p->pw_dir : "."; |
|
1005 if (IS_DIR_SEP (*home) && IS_DIR_SEP (home[1])) { /* handle leading // */ |
|
1006 home++; |
|
1007 } |
|
1008 if (IS_DIR_SEP (home[strlen (home) - 1]) && name[c] != 0) |
|
1009 c++; /* If HOME ends in /, omit the / after ~user. */ |
|
1010 |
|
1011 expansion = name[c] == 0 ? xstrdup (home) : concat (home, name + c); |
|
1012 } |
|
1013 #else /* not HAVE_PWD_H */ |
|
1014 expansion = name; |
|
1015 #endif /* not HAVE_PWD_H */ |
|
1016 |
|
1017 /* We may return the same thing as the original, and then we might not |
|
1018 be returning a malloc-ed string. Callers beware. Sorry. */ |
|
1019 return (char *) expansion; |
|
1020 } |
|
1021 |
|
1022 /* Do variable expansion first so ~${USER} works. (Besides, it's what the |
|
1023 shells do.) */ |
|
1024 |
|
1025 char * |
|
1026 kpse_expand (const char *s) |
|
1027 { |
|
1028 char *var_expansion = kpse_var_expand (s); |
|
1029 char *tilde_expansion = kpse_tilde_expand (var_expansion); |
|
1030 |
|
1031 /* `kpse_var_expand' always gives us new memory; `kpse_tilde_expand' |
|
1032 doesn't, necessarily. So be careful that we don't free what we are |
|
1033 about to return. */ |
|
1034 if (tilde_expansion != var_expansion) |
|
1035 free (var_expansion); |
|
1036 |
|
1037 return tilde_expansion; |
|
1038 } |
|
1039 |
|
1040 |
|
1041 /* Forward declarations of functions from the original expand.c */ |
|
1042 static char **brace_expand (const char *); |
|
1043 static void free_array (char **); |
|
1044 |
|
1045 /* If $KPSE_DOT is defined in the environment, prepend it to any relative |
|
1046 path components. */ |
|
1047 |
|
1048 static char * |
|
1049 kpse_expand_kpse_dot (char *path) |
|
1050 { |
|
1051 char *ret, *elt; |
|
1052 char *kpse_dot = getenv("KPSE_DOT"); |
|
1053 #ifdef MSDOS |
|
1054 bool malloced_kpse_dot = false; |
|
1055 #endif |
|
1056 |
|
1057 if (kpse_dot == NULL) |
|
1058 return path; |
|
1059 ret = (char *) xmalloc(1); |
|
1060 *ret = 0; |
|
1061 |
|
1062 #ifdef MSDOS |
|
1063 /* Some setups of ported Bash force $KPSE_DOT to have the //d/foo/bar |
|
1064 form (when `pwd' is used), which is not understood by libc and the OS. |
|
1065 Convert them back to the usual d:/foo/bar form. */ |
|
1066 if (kpse_dot[0] == '/' && kpse_dot[1] == '/' |
|
1067 && kpse_dot[2] >= 'A' && kpse_dot[2] <= 'z' && kpse_dot[3] == '/') { |
|
1068 kpse_dot++; |
|
1069 kpse_dot = xstrdup (kpse_dot); |
|
1070 kpse_dot[0] = kpse_dot[1]; /* drive letter */ |
|
1071 kpse_dot[1] = ':'; |
|
1072 malloced_kpse_dot = true; |
|
1073 } |
|
1074 #endif |
|
1075 |
|
1076 for (elt = kpse_path_element (path); elt; elt = kpse_path_element (NULL)) { |
|
1077 char *save_ret = ret; |
|
1078 /* We assume that the !! magic is only used on absolute components. |
|
1079 Single "." get special treatment, as does "./" or its equivalent. */ |
|
1080 if (kpse_absolute_p (elt, false) || (elt[0] == '!' && elt[1] == '!')) { |
|
1081 ret = concat3(ret, elt, ENV_SEP_STRING); |
|
1082 } else if (elt[0] == '.' && elt[1] == 0) { |
|
1083 ret = concat3 (ret, kpse_dot, ENV_SEP_STRING); |
|
1084 } else if (elt[0] == '.' && IS_DIR_SEP(elt[1])) { |
|
1085 ret = concatn (ret, kpse_dot, elt + 1, ENV_SEP_STRING, NULL); |
|
1086 } else { |
|
1087 ret = concatn (ret, kpse_dot, DIR_SEP_STRING, elt, ENV_SEP_STRING, NULL); |
|
1088 } |
|
1089 free (save_ret); |
|
1090 } |
|
1091 |
|
1092 #ifdef MSDOS |
|
1093 if (malloced_kpse_dot) free (kpse_dot); |
|
1094 #endif |
|
1095 |
|
1096 ret[strlen (ret) - 1] = 0; |
|
1097 return ret; |
|
1098 } |
|
1099 |
|
1100 /* Do brace expansion on ELT; then do variable and ~ expansion on each |
|
1101 element of the result; then do brace expansion again, in case a |
|
1102 variable definition contained braces (e.g., $TEXMF). Return a |
|
1103 string comprising all of the results separated by ENV_SEP_STRING. */ |
|
1104 |
|
1105 static char * |
|
1106 kpse_brace_expand_element (const char *elt) |
|
1107 { |
|
1108 unsigned i; |
|
1109 char **expansions = brace_expand (elt); |
|
1110 char *ret = (char *) xmalloc (1); |
|
1111 *ret = 0; |
|
1112 |
|
1113 for (i = 0; expansions[i]; i++) { |
|
1114 /* Do $ and ~ expansion on each element. */ |
|
1115 char *x = kpse_expand (expansions[i]); |
|
1116 char *save_ret = ret; |
|
1117 if (!STREQ (x, expansions[i])) { |
|
1118 /* If we did any expansions, do brace expansion again. Since |
|
1119 recursive variable definitions are not allowed, this recursion |
|
1120 must terminate. (In practice, it's unlikely there will ever be |
|
1121 more than one level of recursion.) */ |
|
1122 char *save_x = x; |
|
1123 x = kpse_brace_expand_element (x); |
|
1124 free (save_x); |
|
1125 } |
|
1126 ret = concat3 (ret, x, ENV_SEP_STRING); |
|
1127 free (save_ret); |
|
1128 free (x); |
|
1129 } |
|
1130 |
|
1131 free_array (expansions); |
|
1132 ret[strlen (ret) - 1] = 0; /* waste the trailing null */ |
|
1133 return ret; |
|
1134 } |
|
1135 |
|
1136 /* Be careful to not waste all the memory we allocate for each element. */ |
|
1137 |
|
1138 char * |
|
1139 kpse_brace_expand (const char *path) |
|
1140 { |
|
1141 char *kpse_dot_expansion; |
|
1142 char *elt; |
|
1143 unsigned len; |
|
1144 /* Must do variable expansion first because if we have |
|
1145 foo = .:~ |
|
1146 TEXINPUTS = $foo |
|
1147 we want to end up with TEXINPUTS = .:/home/karl. |
|
1148 Since kpse_path_element is not reentrant, we must get all |
|
1149 the path elements before we start the loop. */ |
|
1150 char *xpath = kpse_var_expand (path); |
|
1151 char *ret = (char *) xmalloc (1); |
|
1152 *ret = 0; |
|
1153 |
|
1154 for (elt = kpse_path_element (xpath); elt; elt = kpse_path_element (NULL)) { |
|
1155 char *save_ret = ret; |
|
1156 /* Do brace expansion first, so tilde expansion happens in {~ka,~kb}. */ |
|
1157 char *expansion = kpse_brace_expand_element (elt); |
|
1158 ret = concat3 (ret, expansion, ENV_SEP_STRING); |
|
1159 free (expansion); |
|
1160 free (save_ret); |
|
1161 } |
|
1162 |
|
1163 /* Waste the last byte by overwriting the trailing env_sep with a null. */ |
|
1164 len = strlen (ret); |
|
1165 if (len != 0) |
|
1166 ret[len - 1] = 0; |
|
1167 free (xpath); |
|
1168 |
|
1169 kpse_dot_expansion = kpse_expand_kpse_dot (ret); |
|
1170 if (kpse_dot_expansion != ret) |
|
1171 free (ret); |
|
1172 |
|
1173 return kpse_dot_expansion; |
|
1174 } |
|
1175 |
|
1176 /* Expand all special constructs in a path, and include only the actually |
|
1177 existing directories in the result. */ |
|
1178 char * |
|
1179 kpse_path_expand (const char *path) |
|
1180 { |
|
1181 char *ret; |
|
1182 char *xpath; |
|
1183 char *elt; |
|
1184 unsigned len; |
|
1185 |
|
1186 /* Initialise ret to the empty string. */ |
|
1187 ret = (char *) xmalloc (1); |
|
1188 *ret = 0; |
|
1189 len = 0; |
|
1190 |
|
1191 /* Expand variables and braces first. */ |
|
1192 xpath = kpse_brace_expand (path); |
|
1193 |
|
1194 /* Now expand each of the path elements, printing the results */ |
|
1195 for (elt = kpse_path_element (xpath); elt; elt = kpse_path_element (NULL)) { |
|
1196 str_llist_type *dirs; |
|
1197 |
|
1198 /* Skip and ignore magic leading chars. */ |
|
1199 if (*elt == '!' && *(elt + 1) == '!') |
|
1200 elt += 2; |
|
1201 |
|
1202 /* Do not touch the device if present */ |
|
1203 if (NAME_BEGINS_WITH_DEVICE (elt)) { |
|
1204 while (IS_DIR_SEP (*(elt + 2)) && IS_DIR_SEP (*(elt + 3))) { |
|
1205 *(elt + 2) = *(elt + 1); |
|
1206 *(elt + 1) = *elt; |
|
1207 elt++; |
|
1208 } |
|
1209 } else { |
|
1210 /* We never want to search the whole disk. */ |
|
1211 while (IS_DIR_SEP (*elt) && IS_DIR_SEP (*(elt + 1))) |
|
1212 elt++; |
|
1213 } |
|
1214 |
|
1215 /* Search the disk for all dirs in the component specified. |
|
1216 Be faster to check the database, but this is more reliable. */ |
|
1217 dirs = kpse_element_dirs (elt); |
|
1218 if (dirs && *dirs) { |
|
1219 str_llist_elt_type *dir; |
|
1220 |
|
1221 for (dir = *dirs; dir; dir = STR_LLIST_NEXT (*dir)) { |
|
1222 char *thedir = STR_LLIST (*dir); |
|
1223 unsigned dirlen = strlen (thedir); |
|
1224 char *save_ret = ret; |
|
1225 /* Retain trailing slash if that's the root directory. */ |
|
1226 if (dirlen == 1 || (dirlen == 3 && NAME_BEGINS_WITH_DEVICE (thedir) |
|
1227 && IS_DIR_SEP (thedir[2]))) { |
|
1228 ret = concat3 (ret, thedir, ENV_SEP_STRING); |
|
1229 len += dirlen + 1; |
|
1230 ret[len - 1] = ENV_SEP; |
|
1231 } else { |
|
1232 ret = concat (ret, thedir); |
|
1233 len += dirlen; |
|
1234 ret [len - 1] = ENV_SEP; |
|
1235 } |
|
1236 free (save_ret); |
|
1237 } |
|
1238 } |
|
1239 } |
|
1240 /* Get rid of trailing ':', if any. */ |
|
1241 if (len != 0) |
|
1242 ret[len - 1] = 0; |
|
1243 return ret; |
|
1244 } |
|
1245 |
|
1246 /* braces.c -- code for doing word expansion in curly braces. Taken from |
|
1247 bash 1.14.5. [Ans subsequently modified for kpatshea.] |
|
1248 |
|
1249 Copyright (C) 1987,1991 Free Software Foundation, Inc. |
|
1250 |
|
1251 This program is free software; you can redistribute it and/or modify it |
|
1252 under the terms of the GNU General Public License as published by |
|
1253 the Free Software Foundation; either version 1, or (at your option) |
|
1254 any later version. |
|
1255 |
|
1256 This program is distributed in the hope that it will be useful, but |
|
1257 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
1258 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
1259 General Public License for more details. |
|
1260 |
|
1261 You should have received a copy of the GNU General Public License |
|
1262 along with this program; see the file COPYING. If not, write to the |
|
1263 Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
|
1264 MA 02111-1307, USA. */ |
|
1265 |
|
1266 |
|
1267 #define brace_whitespace(c) (!(c) || (c) == ' ' || (c) == '\t' || (c) == '\n') |
|
1268 #define savestring xstrdup |
|
1269 |
|
1270 /* Basic idea: |
|
1271 |
|
1272 Segregate the text into 3 sections: preamble (stuff before an open brace), |
|
1273 postamble (stuff after the matching close brace) and amble (stuff after |
|
1274 preamble, and before postamble). Expand amble, and then tack on the |
|
1275 expansions to preamble. Expand postamble, and tack on the expansions to |
|
1276 the result so far. |
|
1277 */ |
|
1278 |
|
1279 /* The character which is used to separate arguments. */ |
|
1280 static int brace_arg_separator = ','; |
|
1281 |
|
1282 static int brace_gobbler (const char *, int *, int); |
|
1283 static char **expand_amble (const char *), |
|
1284 **array_concat (char **, char **); |
|
1285 |
|
1286 /* Return the length of ARRAY, a NULL terminated array of char *. */ |
|
1287 static int |
|
1288 array_len (char **array) |
|
1289 { |
|
1290 register int i; |
|
1291 for (i = 0; array[i]; i++); |
|
1292 return (i); |
|
1293 } |
|
1294 |
|
1295 /* Free the contents of ARRAY, a NULL terminated array of char *. */ |
|
1296 static void |
|
1297 free_array (char **array) |
|
1298 { |
|
1299 register int i = 0; |
|
1300 |
|
1301 if (!array) return; |
|
1302 |
|
1303 while (array[i]) |
|
1304 free (array[i++]); |
|
1305 free (array); |
|
1306 } |
|
1307 |
|
1308 /* Allocate and return a new copy of ARRAY and its contents. */ |
|
1309 static char ** |
|
1310 copy_array (char **array) |
|
1311 { |
|
1312 register int i; |
|
1313 int len; |
|
1314 char **new_array; |
|
1315 |
|
1316 len = array_len (array); |
|
1317 |
|
1318 new_array = (char **)xmalloc ((len + 1) * sizeof (char *)); |
|
1319 for (i = 0; array[i]; i++) |
|
1320 new_array[i] = savestring (array[i]); |
|
1321 new_array[i] = (char *)NULL; |
|
1322 |
|
1323 return (new_array); |
|
1324 } |
|
1325 |
|
1326 |
|
1327 /* Return an array of strings; the brace expansion of TEXT. */ |
|
1328 static char ** |
|
1329 brace_expand (const char *text) |
|
1330 { |
|
1331 register int start; |
|
1332 char *preamble, *amble; |
|
1333 const char *postamble; |
|
1334 char **tack, **result; |
|
1335 int i, c; |
|
1336 |
|
1337 /* Find the text of the preamble. */ |
|
1338 i = 0; |
|
1339 c = brace_gobbler (text, &i, '{'); |
|
1340 |
|
1341 preamble = (char *) xmalloc (i + 1); |
|
1342 strncpy (preamble, text, i); |
|
1343 preamble[i] = 0; |
|
1344 |
|
1345 result = (char **) xmalloc (2 * sizeof (char *)); |
|
1346 result[0] = preamble; |
|
1347 result[1] = NULL; |
|
1348 |
|
1349 /* Special case. If we never found an exciting character, then |
|
1350 the preamble is all of the text, so just return that. */ |
|
1351 if (c != '{') |
|
1352 return (result); |
|
1353 |
|
1354 /* Find the amble. This is the stuff inside this set of braces. */ |
|
1355 start = ++i; |
|
1356 c = brace_gobbler (text, &i, '}'); |
|
1357 |
|
1358 /* What if there isn't a matching close brace? */ |
|
1359 if (!c) |
|
1360 { |
|
1361 WARNING1 ("%s: Unmatched {", text); |
|
1362 free (preamble); /* Same as result[0]; see initialization. */ |
|
1363 result[0] = savestring (text); |
|
1364 return (result); |
|
1365 } |
|
1366 |
|
1367 amble = (char *) xmalloc (1 + (i - start)); |
|
1368 strncpy (amble, &text[start], (i - start)); |
|
1369 amble[i - start] = 0; |
|
1370 |
|
1371 postamble = &text[i + 1]; |
|
1372 |
|
1373 tack = expand_amble (amble); |
|
1374 result = array_concat (result, tack); |
|
1375 free (amble); |
|
1376 free_array (tack); |
|
1377 |
|
1378 tack = brace_expand (postamble); |
|
1379 result = array_concat (result, tack); |
|
1380 free_array (tack); |
|
1381 |
|
1382 return (result); |
|
1383 } |
|
1384 |
|
1385 |
|
1386 /* Expand the text found inside of braces. We simply try to split the |
|
1387 text at BRACE_ARG_SEPARATORs into separate strings. We then brace |
|
1388 expand each slot which needs it, until there are no more slots which |
|
1389 need it. */ |
|
1390 static char ** |
|
1391 expand_amble (const char *text) |
|
1392 { |
|
1393 char **result, **partial; |
|
1394 char *tem; |
|
1395 int start, i, c; |
|
1396 |
|
1397 result = NULL; |
|
1398 |
|
1399 for (start = 0, i = 0, c = 1; c; start = ++i) |
|
1400 { |
|
1401 int c0, c1; |
|
1402 int i0, i1; |
|
1403 i0 = i; |
|
1404 c0 = brace_gobbler (text, &i0, brace_arg_separator); |
|
1405 i1 = i; |
|
1406 c1 = brace_gobbler (text, &i1, ENV_SEP); |
|
1407 c = c0 | c1; |
|
1408 i = (i0 < i1 ? i0 : i1); |
|
1409 |
|
1410 tem = (char *) xmalloc (1 + (i - start)); |
|
1411 strncpy (tem, &text[start], (i - start)); |
|
1412 tem[i- start] = 0; |
|
1413 |
|
1414 partial = brace_expand (tem); |
|
1415 |
|
1416 if (!result) |
|
1417 result = partial; |
|
1418 else |
|
1419 { |
|
1420 register int lr = array_len (result); |
|
1421 register int lp = array_len (partial); |
|
1422 register int j; |
|
1423 |
|
1424 result = (char **) xrealloc (result, (1 + lp + lr) * sizeof (char *)); |
|
1425 |
|
1426 for (j = 0; j < lp; j++) |
|
1427 result[lr + j] = partial[j]; |
|
1428 |
|
1429 result[lr + j] = NULL; |
|
1430 free (partial); |
|
1431 } |
|
1432 free (tem); |
|
1433 } |
|
1434 return (result); |
|
1435 } |
|
1436 |
|
1437 /* Return a new array of strings which is the result of appending each |
|
1438 string in ARR2 to each string in ARR1. The resultant array is |
|
1439 len (arr1) * len (arr2) long. For convenience, ARR1 (and its contents) |
|
1440 are free ()'ed. ARR1 can be NULL, in that case, a new version of ARR2 |
|
1441 is returned. */ |
|
1442 static char ** |
|
1443 array_concat (char **arr1, char **arr2) |
|
1444 { |
|
1445 register int i, j, len, len1, len2; |
|
1446 register char **result; |
|
1447 |
|
1448 if (!arr1) |
|
1449 return (copy_array (arr2)); |
|
1450 |
|
1451 if (!arr2) |
|
1452 return (copy_array (arr1)); |
|
1453 |
|
1454 len1 = array_len (arr1); |
|
1455 len2 = array_len (arr2); |
|
1456 |
|
1457 result = (char **) xmalloc ((1 + (len1 * len2)) * sizeof (char *)); |
|
1458 |
|
1459 len = 0; |
|
1460 for (i = 0; i < len2; i++) |
|
1461 { |
|
1462 int strlen_2 = strlen (arr2[i]); |
|
1463 |
|
1464 for (j = 0; j < len1; j++) |
|
1465 { |
|
1466 int strlen_1 = strlen (arr1[j]); |
|
1467 |
|
1468 result[len] = (char *) xmalloc (1 + strlen_1 + strlen_2); |
|
1469 strcpy (result[len], arr1[j]); |
|
1470 strcpy (result[len] + strlen_1, arr2[i]); |
|
1471 len++; |
|
1472 } |
|
1473 } |
|
1474 free_array (arr1); |
|
1475 |
|
1476 result[len] = NULL; |
|
1477 return (result); |
|
1478 } |
|
1479 |
|
1480 /* Start at INDEX, and skip characters in TEXT. Set INDEX to the |
|
1481 index of the character matching SATISFY. This understands about |
|
1482 quoting. Return the character that caused us to stop searching; |
|
1483 this is either the same as SATISFY, or 0. */ |
|
1484 static int |
|
1485 brace_gobbler (const char *text, int *indx, int satisfy) |
|
1486 { |
|
1487 register int i, c, quoted, level, pass_next; |
|
1488 |
|
1489 level = quoted = pass_next = 0; |
|
1490 |
|
1491 for (i = *indx; (c = text[i]); i++) |
|
1492 { |
|
1493 if (pass_next) |
|
1494 { |
|
1495 pass_next = 0; |
|
1496 continue; |
|
1497 } |
|
1498 |
|
1499 /* A backslash escapes the next character. This allows backslash to |
|
1500 escape the quote character in a double-quoted string. */ |
|
1501 if (c == '\\' && (quoted == 0 || quoted == '"' || quoted == '`')) |
|
1502 { |
|
1503 pass_next = 1; |
|
1504 continue; |
|
1505 } |
|
1506 |
|
1507 if (quoted) |
|
1508 { |
|
1509 if (c == quoted) |
|
1510 quoted = 0; |
|
1511 continue; |
|
1512 } |
|
1513 |
|
1514 if (c == '"' || c == '\'' || c == '`') |
|
1515 { |
|
1516 quoted = c; |
|
1517 continue; |
|
1518 } |
|
1519 |
|
1520 if (c == satisfy && !level && !quoted) |
|
1521 { |
|
1522 /* We ignore an open brace surrounded by whitespace, and also |
|
1523 an open brace followed immediately by a close brace, that |
|
1524 was preceded with whitespace. */ |
|
1525 if (c == '{' && |
|
1526 ((!i || brace_whitespace (text[i - 1])) && |
|
1527 (brace_whitespace (text[i + 1]) || text[i + 1] == '}'))) |
|
1528 continue; |
|
1529 /* If this is being compiled as part of bash, ignore the `{' |
|
1530 in a `${}' construct */ |
|
1531 if ((c != '{') || !i || (text[i - 1] != '$')) |
|
1532 break; |
|
1533 } |
|
1534 |
|
1535 if (c == '{') |
|
1536 level++; |
|
1537 else if (c == '}' && level) |
|
1538 level--; |
|
1539 } |
|
1540 |
|
1541 *indx = i; |
|
1542 return (c); |
|
1543 } |
|
1544 |
|
1545 /* db.c: an external database to avoid filesystem lookups. */ |
|
1546 |
|
1547 #ifndef DEFAULT_TEXMFDBS |
|
1548 #define DEFAULT_TEXMFDBS "/usr/local/share/texmf:/var/tmp/texfonts" |
|
1549 #endif |
|
1550 |
|
1551 /* Perhaps we could use this for path values themselves; for now, we use |
|
1552 it only for the program_enabled_p value. */ |
|
1553 typedef enum |
|
1554 { |
|
1555 kpse_src_implicit, /* C initialization to zero */ |
|
1556 kpse_src_compile, /* configure/compile-time default */ |
|
1557 kpse_src_texmf_cnf, /* texmf.cnf, the kpathsea config file */ |
|
1558 kpse_src_client_cnf, /* application config file, e.g., config.ps */ |
|
1559 kpse_src_env, /* environment variable */ |
|
1560 kpse_src_x, /* X Window System resource */ |
|
1561 kpse_src_cmdline /* command-line option */ |
|
1562 } kpse_src_type; |
|
1563 |
|
1564 |
|
1565 /* For each file format, we record the following information. The main |
|
1566 thing that is not part of this structure is the environment variable |
|
1567 lists. They are used directly in tex-file.c. We could incorporate |
|
1568 them here, but it would complicate the code a bit. We could also do |
|
1569 it via variable expansion, but not now, maybe not ever: |
|
1570 ${PKFONTS-${TEXFONTS-/usr/local/lib/texmf/fonts//}}. */ |
|
1571 |
|
1572 typedef struct |
|
1573 { |
|
1574 const char *type; /* Human-readable description. */ |
|
1575 const char *path; /* The search path to use. */ |
|
1576 const char *raw_path; /* Pre-$~ (but post-default) expansion. */ |
|
1577 const char *path_source; /* Where the path started from. */ |
|
1578 const char *override_path; /* From client environment variable. */ |
|
1579 const char *client_path; /* E.g., from dvips's config.ps. */ |
|
1580 const char *cnf_path; /* From texmf.cnf. */ |
|
1581 const char *default_path; /* If all else fails. */ |
|
1582 const char **suffix; /* For kpse_find_file to check for/append. */ |
|
1583 const char **alt_suffix; /* More suffixes to check for. */ |
|
1584 bool suffix_search_only; /* Only search with a suffix? */ |
|
1585 const char *program; /* ``mktexpk'', etc. */ |
|
1586 const char *program_args; /* Args to `program'. */ |
|
1587 bool program_enabled_p; /* Invoke `program'? */ |
|
1588 kpse_src_type program_enable_level; /* Who said to invoke `program'. */ |
|
1589 bool binmode; /* The files must be opened in binary mode. */ |
|
1590 } kpse_format_info_type; |
|
1591 |
|
1592 /* The sole variable of that type, indexed by `kpse_file_format_type'. |
|
1593 Initialized by calls to `kpse_find_file' for `kpse_init_format'. */ |
|
1594 static kpse_format_info_type kpse_format_info; |
|
1595 |
|
1596 #define DB_ENVS "TEXMFDBS" |
|
1597 |
|
1598 /* And EXPAND_DEFAULT calls kpse_expand_default on try_path and the |
|
1599 present info->path. */ |
|
1600 #define EXPAND_DEFAULT(try_path, source_string) \ |
|
1601 if (try_path) { \ |
|
1602 info->raw_path = try_path; \ |
|
1603 info->path = kpse_expand_default (try_path, info->path); \ |
|
1604 info->path_source = source_string; \ |
|
1605 } |
|
1606 |
|
1607 /* Find the final search path to use for the format entry INFO, given |
|
1608 the compile-time default (DEFAULT_PATH), and the environment |
|
1609 variables to check (the remaining arguments, terminated with NULL). |
|
1610 We set the `path' and `path_source' members of INFO. The |
|
1611 `client_path' member must already be set upon entry. */ |
|
1612 |
|
1613 static void |
|
1614 init_path (kpse_format_info_type *info, const char *default_path, ...) |
|
1615 { |
|
1616 char *env_name; |
|
1617 char *var = NULL; |
|
1618 va_list ap; |
|
1619 |
|
1620 va_start (ap, default_path); |
|
1621 |
|
1622 info->default_path = default_path; |
|
1623 |
|
1624 /* First envvar that's set to a nonempty value will exit the loop. If |
|
1625 none are set, we want the first cnf entry that matches. Find the |
|
1626 cnf entries simultaneously, to avoid having to go through envvar |
|
1627 list twice -- because of the PVAR?C macro, that would mean having |
|
1628 to create a str_list and then use it twice. Yuck. */ |
|
1629 while ((env_name = va_arg (ap, char *)) != NULL) { |
|
1630 /* Since sh doesn't like envvar names with `.', check PATH_prog |
|
1631 rather than PATH.prog. */ |
|
1632 if (!var) { |
|
1633 /* Try simply PATH. */ |
|
1634 char *env_value = getenv (env_name); |
|
1635 if (env_value && *env_value) { |
|
1636 var = env_name; |
|
1637 } |
|
1638 } |
|
1639 |
|
1640 if (var && info->cnf_path) |
|
1641 break; |
|
1642 } |
|
1643 va_end (ap); |
|
1644 |
|
1645 /* Expand any extra :'s. For each level, we replace an extra : with |
|
1646 the path at the next lower level. For example, an extra : in a |
|
1647 user-set envvar should be replaced with the path from the cnf file. |
|
1648 things are complicated because none of the levels above the very |
|
1649 bottom are guaranteed to exist. */ |
|
1650 |
|
1651 /* Assume we can reliably start with the compile-time default. */ |
|
1652 info->path = info->raw_path = info->default_path; |
|
1653 info->path_source = "compile-time paths.h"; |
|
1654 |
|
1655 EXPAND_DEFAULT (info->cnf_path, "texmf.cnf"); |
|
1656 EXPAND_DEFAULT (info->client_path, "program config file"); |
|
1657 if (var) |
|
1658 EXPAND_DEFAULT (getenv (var), concat (var, " environment variable")); |
|
1659 EXPAND_DEFAULT (info->override_path, "application override variable"); |
|
1660 info->path = kpse_brace_expand (info->path); |
|
1661 } |
|
1662 |
|
1663 |
|
1664 /* Some file types have more than one suffix. */ |
|
1665 |
|
1666 static void |
|
1667 add_suffixes (const char ***list, ...) |
|
1668 { |
|
1669 const char *s; |
|
1670 unsigned count = 0; |
|
1671 va_list ap; |
|
1672 |
|
1673 va_start (ap, list); |
|
1674 |
|
1675 while ((s = va_arg (ap, char *)) != NULL) { |
|
1676 count++; |
|
1677 XRETALLOC (*list, count + 1, const char *); |
|
1678 (*list)[count - 1] = s; |
|
1679 } |
|
1680 va_end (ap); |
|
1681 (*list)[count] = NULL; |
|
1682 } |
|
1683 |
|
1684 |
|
1685 static char * |
|
1686 remove_dbonly (const char *path) |
|
1687 { |
|
1688 char *ret = XTALLOC(strlen (path) + 1, char), *q=ret; |
|
1689 const char *p=path; |
|
1690 bool new_elt=true; |
|
1691 |
|
1692 while (*p) { |
|
1693 if (new_elt && *p && *p == '!' && *(p+1) == '!') |
|
1694 p += 2; |
|
1695 else { |
|
1696 new_elt = (*p == ENV_SEP); |
|
1697 *q++ = *p++; |
|
1698 } |
|
1699 } |
|
1700 *q = '\0'; |
|
1701 return(ret); |
|
1702 } |
|
1703 |
|
1704 /* Initialize everything for FORMAT. */ |
|
1705 |
|
1706 static const char * |
|
1707 kpse_init_format (void) |
|
1708 { |
|
1709 /* If we get called twice, don't redo all the work. */ |
|
1710 if (kpse_format_info.path) |
|
1711 return kpse_format_info.path; |
|
1712 |
|
1713 kpse_format_info.type = "ls-R"; |
|
1714 init_path (&kpse_format_info, DEFAULT_TEXMFDBS, DB_ENVS, NULL); |
|
1715 add_suffixes(&kpse_format_info.suffix, "ls-R", NULL); |
|
1716 kpse_format_info.path = remove_dbonly (kpse_format_info.path); |
|
1717 |
|
1718 #ifdef KPSE_DEBUG |
|
1719 #define MAYBE(member) (kpse_format_info.member ? kpse_format_info.member : "(none)") |
|
1720 |
|
1721 /* Describe the monster we've created. */ |
|
1722 if (KPSE_DEBUG_P (KPSE_DEBUG_PATHS)) |
|
1723 { |
|
1724 DEBUGF2 ("Search path for %s files (from %s)\n", |
|
1725 kpse_format_info.type, kpse_format_info.path_source); |
|
1726 DEBUGF1 (" = %s\n", kpse_format_info.path); |
|
1727 DEBUGF1 (" before expansion = %s\n", kpse_format_info.raw_path); |
|
1728 DEBUGF1 (" application override path = %s\n", MAYBE (override_path)); |
|
1729 DEBUGF1 (" application config file path = %s\n", MAYBE (client_path)); |
|
1730 DEBUGF1 (" texmf.cnf path = %s\n", MAYBE (cnf_path)); |
|
1731 DEBUGF1 (" compile-time path = %s\n", MAYBE (default_path)); |
|
1732 DEBUGF (" default suffixes ="); |
|
1733 if (kpse_format_info.suffix) { |
|
1734 const char **ext; |
|
1735 for (ext = kpse_format_info.suffix; ext && *ext; ext++) { |
|
1736 fprintf (stderr, " %s", *ext); |
|
1737 } |
|
1738 putc ('\n', stderr); |
|
1739 } else { |
|
1740 fputs (" (none)\n", stderr); |
|
1741 } |
|
1742 DEBUGF (" other suffixes ="); |
|
1743 if (kpse_format_info.alt_suffix) { |
|
1744 const char **alt; |
|
1745 for (alt = kpse_format_info.alt_suffix; alt && *alt; alt++) { |
|
1746 fprintf (stderr, " %s", *alt); |
|
1747 } |
|
1748 putc ('\n', stderr); |
|
1749 } else { |
|
1750 fputs (" (none)\n", stderr); |
|
1751 } |
|
1752 DEBUGF1 (" search only with suffix = %d\n",kpse_format_info.suffix_search_only); |
|
1753 DEBUGF1 (" runtime generation program = %s\n", MAYBE (program)); |
|
1754 DEBUGF1 (" extra program args = %s\n", MAYBE (program_args)); |
|
1755 DEBUGF1 (" program enabled = %d\n", kpse_format_info.program_enabled_p); |
|
1756 DEBUGF1 (" program enable level = %d\n", kpse_format_info.program_enable_level); |
|
1757 } |
|
1758 #endif /* KPSE_DEBUG */ |
|
1759 |
|
1760 return kpse_format_info.path; |
|
1761 } |
|
1762 |
|
1763 static hash_table_type db; /* The hash table for all the ls-R's. */ |
|
1764 /* SMALL: The old size of the hash table was 7603, with the assumption |
|
1765 that a minimal ls-R bas about 3500 entries. But a typical ls-R will |
|
1766 be more like double that size. */ |
|
1767 #ifndef DB_HASH_SIZE |
|
1768 #define DB_HASH_SIZE 15991 |
|
1769 #endif |
|
1770 #ifndef DB_NAME |
|
1771 #define DB_NAME "ls-R" |
|
1772 #endif |
|
1773 |
|
1774 static hash_table_type alias_db; |
|
1775 #ifndef ALIAS_NAME |
|
1776 #define ALIAS_NAME "aliases" |
|
1777 #endif |
|
1778 #ifndef ALIAS_HASH_SIZE |
|
1779 #define ALIAS_HASH_SIZE 1009 |
|
1780 #endif |
|
1781 |
|
1782 static str_list_type db_dir_list; |
|
1783 |
|
1784 /* If DIRNAME contains any element beginning with a `.' (that is more |
|
1785 than just `./'), return true. This is to allow ``hidden'' |
|
1786 directories -- ones that don't get searched. */ |
|
1787 |
|
1788 static bool |
|
1789 ignore_dir_p (const char *dirname) |
|
1790 { |
|
1791 const char *dot_pos = dirname; |
|
1792 |
|
1793 while ((dot_pos = strchr (dot_pos + 1, '.'))) { |
|
1794 /* If / before and no / after, skip it. */ |
|
1795 if (IS_DIR_SEP (dot_pos[-1]) && dot_pos[1] && !IS_DIR_SEP (dot_pos[1])) |
|
1796 return true; |
|
1797 } |
|
1798 |
|
1799 return false; |
|
1800 } |
|
1801 |
|
1802 /* Allocate in increments of this size. */ |
|
1803 #define BLOCK_SIZE 75 |
|
1804 |
|
1805 static char * |
|
1806 read_line (FILE *f) |
|
1807 { |
|
1808 int c; |
|
1809 unsigned limit = BLOCK_SIZE; |
|
1810 unsigned loc = 0; |
|
1811 char *line = (char *) xmalloc (limit); |
|
1812 |
|
1813 while ((c = getc (f)) != EOF && c != '\n' && c != '\r') |
|
1814 { |
|
1815 line[loc] = c; |
|
1816 loc++; |
|
1817 |
|
1818 /* By testing after the assignment, we guarantee that we'll always |
|
1819 have space for the null we append below. We know we always |
|
1820 have room for the first char, since we start with BLOCK_SIZE. */ |
|
1821 if (loc == limit) |
|
1822 { |
|
1823 limit += BLOCK_SIZE; |
|
1824 line = (char *) xrealloc (line, limit); |
|
1825 } |
|
1826 } |
|
1827 |
|
1828 /* If we read anything, return it. This can't represent a last |
|
1829 ``line'' which doesn't end in a newline, but so what. */ |
|
1830 if (c != EOF) |
|
1831 { |
|
1832 /* Terminate the string. We can't represent nulls in the file, |
|
1833 either. Again, it doesn't matter. */ |
|
1834 line[loc] = 0; |
|
1835 /* Absorb LF of a CRLF pair. */ |
|
1836 if (c == '\r') { |
|
1837 c = getc (f); |
|
1838 if (c != '\n') |
|
1839 ungetc (c, f); |
|
1840 } |
|
1841 } |
|
1842 else /* At end of file. */ |
|
1843 { |
|
1844 free (line); |
|
1845 line = NULL; |
|
1846 } |
|
1847 |
|
1848 return line; |
|
1849 } |
|
1850 |
|
1851 /* If no DB_FILENAME, return false (maybe they aren't using this feature). |
|
1852 Otherwise, add entries from DB_FILENAME to TABLE, and return true. */ |
|
1853 |
|
1854 static bool |
|
1855 db_build (hash_table_type *table, const char *db_filename) |
|
1856 { |
|
1857 char *line; |
|
1858 unsigned dir_count = 0, file_count = 0, ignore_dir_count = 0; |
|
1859 unsigned len = strlen (db_filename) - sizeof (DB_NAME) + 1; /* Keep the /. */ |
|
1860 char *top_dir = (char *) xmalloc (len + 1); |
|
1861 char *cur_dir = NULL; /* First thing in ls-R might be a filename. */ |
4385
|
1862 FILE *db_file = xfopen (db_filename, "r"); |
4378
|
1863 |
|
1864 strncpy (top_dir, db_filename, len); |
|
1865 top_dir[len] = 0; |
|
1866 |
|
1867 if (db_file) { |
|
1868 while ((line = read_line (db_file)) != NULL) { |
|
1869 len = strlen (line); |
|
1870 |
|
1871 /* A line like `/foo:' = new dir foo. Allow both absolute (/...) |
|
1872 and explicitly relative (./...) names here. It's a kludge to |
|
1873 pass in the directory name with the trailing : still attached, |
|
1874 but it doesn't actually hurt. */ |
|
1875 if (len > 0 && line[len - 1] == ':' && kpse_absolute_p (line, true)) { |
|
1876 /* New directory line. */ |
|
1877 if (!ignore_dir_p (line)) { |
|
1878 /* If they gave a relative name, prepend full directory name now. */ |
|
1879 line[len - 1] = DIR_SEP; |
|
1880 /* Skip over leading `./', it confuses `match' and is just a |
|
1881 waste of space, anyway. This will lose on `../', but `match' |
|
1882 won't work there, either, so it doesn't matter. */ |
|
1883 cur_dir = *line == '.' ? concat (top_dir, line + 2) : xstrdup (line); |
|
1884 dir_count++; |
|
1885 } else { |
|
1886 cur_dir = NULL; |
|
1887 ignore_dir_count++; |
|
1888 } |
|
1889 |
|
1890 /* Ignore blank, `.' and `..' lines. */ |
|
1891 } else if (*line != 0 && cur_dir /* a file line? */ |
|
1892 && !(*line == '.' |
|
1893 && (line[1] == '0' || (line[1] == '.' && line[2] == 0)))) |
|
1894 {/* Make a new hash table entry with a key of `line' and a data |
|
1895 of `cur_dir'. An already-existing identical key is ok, since |
|
1896 a file named `foo' can be in more than one directory. Share |
|
1897 `cur_dir' among all its files (and hence never free it). */ |
|
1898 hash_insert (table, xstrdup (line), cur_dir); |
|
1899 file_count++; |
|
1900 |
|
1901 } /* else ignore blank lines or top-level files |
|
1902 or files in ignored directories*/ |
|
1903 |
|
1904 free (line); |
|
1905 } |
|
1906 |
|
1907 xfclose (db_file, db_filename); |
|
1908 |
|
1909 if (file_count == 0) { |
|
1910 WARNING1 ("kpathsea: No usable entries in %s", db_filename); |
|
1911 WARNING ("kpathsea: See the manual for how to generate ls-R"); |
|
1912 db_file = NULL; |
|
1913 } else { |
|
1914 str_list_add (&db_dir_list, xstrdup (top_dir)); |
|
1915 } |
|
1916 |
|
1917 #ifdef KPSE_DEBUG |
|
1918 if (KPSE_DEBUG_P (KPSE_DEBUG_HASH)) { |
|
1919 /* Don't make this a debugging bit, since the output is so |
|
1920 voluminous, and being able to specify -1 is too useful. |
|
1921 Instead, let people who want it run the program under |
|
1922 a debugger and change the variable that way. */ |
|
1923 bool hash_summary_only = true; |
|
1924 |
|
1925 DEBUGF4 ("%s: %u entries in %d directories (%d hidden).\n", |
|
1926 db_filename, file_count, dir_count, ignore_dir_count); |
|
1927 DEBUGF ("ls-R hash table:"); |
|
1928 hash_print (*table, hash_summary_only); |
|
1929 fflush (stderr); |
|
1930 } |
|
1931 #endif /* KPSE_DEBUG */ |
|
1932 } |
|
1933 |
|
1934 free (top_dir); |
|
1935 |
|
1936 return db_file != NULL; |
|
1937 } |
|
1938 |
|
1939 |
|
1940 /* Insert FNAME into the hash table. This is for files that get built |
|
1941 during a run. We wouldn't want to reread all of ls-R, even if it got |
|
1942 rebuilt. */ |
|
1943 |
|
1944 void |
|
1945 kpse_db_insert (const char *passed_fname) |
|
1946 { |
|
1947 /* We might not have found ls-R, or even had occasion to look for it |
|
1948 yet, so do nothing if we have no hash table. */ |
|
1949 if (db.buckets) { |
|
1950 const char *dir_part; |
|
1951 char *fname = xstrdup (passed_fname); |
|
1952 char *baseptr = xbasename (fname); |
|
1953 const char *file_part = xstrdup (baseptr); |
|
1954 |
|
1955 *baseptr = '\0'; /* Chop off the filename. */ |
|
1956 dir_part = fname; /* That leaves the dir, with the trailing /. */ |
|
1957 |
|
1958 hash_insert (&db, file_part, dir_part); |
|
1959 } |
|
1960 } |
|
1961 |
|
1962 /* Return true if FILENAME could be in PATH_ELT, i.e., if the directory |
|
1963 part of FILENAME matches PATH_ELT. Have to consider // wildcards, but |
|
1964 $ and ~ expansion have already been done. */ |
|
1965 |
|
1966 static bool |
|
1967 match (const char *filename, const char *path_elt) |
|
1968 { |
|
1969 const char *original_filename = filename; |
|
1970 bool matched = false; |
|
1971 |
|
1972 for (; *filename && *path_elt; filename++, path_elt++) { |
|
1973 if (FILECHARCASEEQ (*filename, *path_elt)) /* normal character match */ |
|
1974 ; |
|
1975 |
|
1976 else if (IS_DIR_SEP (*path_elt) /* at // */ |
|
1977 && original_filename < filename && IS_DIR_SEP (path_elt[-1])) { |
|
1978 while (IS_DIR_SEP (*path_elt)) |
|
1979 path_elt++; /* get past second and any subsequent /'s */ |
|
1980 if (*path_elt == 0) { |
|
1981 /* Trailing //, matches anything. We could make this part of the |
|
1982 other case, but it seems pointless to do the extra work. */ |
|
1983 matched = true; |
|
1984 break; |
|
1985 } else { |
|
1986 /* Intermediate //, have to match rest of PATH_ELT. */ |
|
1987 for (; !matched && *filename; filename++) { |
|
1988 /* Try matching at each possible character. */ |
|
1989 if (IS_DIR_SEP (filename[-1]) |
|
1990 && FILECHARCASEEQ (*filename, *path_elt)) |
|
1991 matched = match (filename, path_elt); |
|
1992 } |
|
1993 /* Prevent filename++ when *filename='\0'. */ |
|
1994 break; |
|
1995 } |
|
1996 } |
|
1997 |
|
1998 else /* normal character nonmatch, quit */ |
|
1999 break; |
|
2000 } |
|
2001 |
|
2002 /* If we've reached the end of PATH_ELT, check that we're at the last |
|
2003 component of FILENAME, we've matched. */ |
|
2004 if (!matched && *path_elt == 0) { |
|
2005 /* Probably PATH_ELT ended with `vf' or some such, and FILENAME ends |
|
2006 with `vf/ptmr.vf'. In that case, we'll be at a directory |
|
2007 separator. On the other hand, if PATH_ELT ended with a / (as in |
|
2008 `vf/'), FILENAME being the same `vf/ptmr.vf', we'll be at the |
|
2009 `p'. Upshot: if we're at a dir separator in FILENAME, skip it. |
|
2010 But if not, that's ok, as long as there are no more dir separators. */ |
|
2011 if (IS_DIR_SEP (*filename)) |
|
2012 filename++; |
|
2013 |
|
2014 while (*filename && !IS_DIR_SEP (*filename)) |
|
2015 filename++; |
|
2016 matched = *filename == 0; |
|
2017 } |
|
2018 |
|
2019 return matched; |
|
2020 } |
|
2021 |
|
2022 |
|
2023 /* If DB_DIR is a prefix of PATH_ELT, return true; otherwise false. |
|
2024 That is, the question is whether to try the db for a file looked up |
|
2025 in PATH_ELT. If PATH_ELT == ".", for example, the answer is no. If |
|
2026 PATH_ELT == "/usr/local/lib/texmf/fonts//tfm", the answer is yes. |
|
2027 |
|
2028 In practice, ls-R is only needed for lengthy subdirectory |
|
2029 comparisons, but there's no gain to checking PATH_ELT to see if it is |
|
2030 a subdir match, since the only way to do that is to do a string |
|
2031 search in it, which is all we do anyway. */ |
|
2032 |
|
2033 static bool |
|
2034 elt_in_db (const char *db_dir, const char *path_elt) |
|
2035 { |
|
2036 bool found = false; |
|
2037 |
|
2038 while (!found && FILECHARCASEEQ (*db_dir++, *path_elt++)) { |
|
2039 /* If we've matched the entire db directory, it's good. */ |
|
2040 if (*db_dir == 0) |
|
2041 found = true; |
|
2042 |
|
2043 /* If we've reached the end of PATH_ELT, but not the end of the db |
|
2044 directory, it's no good. */ |
|
2045 else if (*path_elt == 0) |
|
2046 break; |
|
2047 } |
|
2048 |
|
2049 return found; |
|
2050 } |
|
2051 |
|
2052 /* If ALIAS_FILENAME exists, read it into TABLE. */ |
|
2053 |
|
2054 static bool |
|
2055 alias_build (hash_table_type *table, const char *alias_filename) |
|
2056 { |
|
2057 char *line, *real, *alias; |
|
2058 unsigned count = 0; |
4385
|
2059 FILE *alias_file = xfopen (alias_filename, "r"); |
4378
|
2060 |
|
2061 if (alias_file) { |
|
2062 while ((line = read_line (alias_file)) != NULL) { |
|
2063 /* comments or empty */ |
|
2064 if (*line == 0 || *line == '%' || *line == '#') { |
|
2065 ; |
|
2066 } else { |
|
2067 /* Each line should have two fields: realname aliasname. */ |
|
2068 real = line; |
4385
|
2069 while (*real && isspace (*real)) |
4378
|
2070 real++; |
|
2071 alias = real; |
4385
|
2072 while (*alias && !isspace (*alias)) |
4378
|
2073 alias++; |
|
2074 *alias++ = 0; |
4385
|
2075 while (*alias && isspace (*alias)) |
4378
|
2076 alias++; |
|
2077 /* Is the check for errors strong enough? Should we warn the user |
|
2078 for potential errors? */ |
|
2079 if (strlen (real) != 0 && strlen (alias) != 0) { |
|
2080 hash_insert (table, xstrdup (alias), xstrdup (real)); |
|
2081 count++; |
|
2082 } |
|
2083 } |
|
2084 free (line); |
|
2085 } |
|
2086 |
|
2087 #ifdef KPSE_DEBUG |
|
2088 if (KPSE_DEBUG_P (KPSE_DEBUG_HASH)) { |
|
2089 /* As with ls-R above ... */ |
|
2090 bool hash_summary_only = true; |
|
2091 DEBUGF2 ("%s: %u aliases.\n", alias_filename, count); |
|
2092 DEBUGF ("alias hash table:"); |
|
2093 hash_print (*table, hash_summary_only); |
|
2094 fflush (stderr); |
|
2095 } |
|
2096 #endif /* KPSE_DEBUG */ |
|
2097 |
|
2098 xfclose (alias_file, alias_filename); |
|
2099 } |
|
2100 |
|
2101 return alias_file != NULL; |
|
2102 } |
|
2103 |
|
2104 /* Initialize the path for ls-R files, and read them all into the hash |
|
2105 table `db'. If no usable ls-R's are found, set db.buckets to NULL. */ |
|
2106 |
|
2107 void |
|
2108 kpse_init_db (void) |
|
2109 { |
|
2110 bool ok = false; |
|
2111 const char *db_path = kpse_init_format (); |
|
2112 char **db_files = kpse_all_path_search (db_path, DB_NAME); |
|
2113 char **orig_db_files = db_files; |
|
2114 |
|
2115 /* Must do this after the path searching (which ends up calling |
|
2116 kpse_db_search recursively), so db.buckets stays NULL. */ |
|
2117 db = hash_create (DB_HASH_SIZE); |
|
2118 |
|
2119 while (db_files && *db_files) { |
|
2120 if (db_build (&db, *db_files)) |
|
2121 ok = true; |
|
2122 free (*db_files); |
|
2123 db_files++; |
|
2124 } |
|
2125 |
|
2126 if (!ok) { |
|
2127 /* If db can't be built, leave `size' nonzero (so we don't |
|
2128 rebuild it), but clear `buckets' (so we don't look in it). */ |
|
2129 free (db.buckets); |
|
2130 db.buckets = NULL; |
|
2131 } |
|
2132 |
|
2133 free (orig_db_files); |
|
2134 |
|
2135 /* Add the content of any alias databases. There may exist more than |
|
2136 one alias file along DB_NAME files. This duplicates the above code |
|
2137 -- should be a function. */ |
|
2138 ok = false; |
|
2139 db_files = kpse_all_path_search (db_path, ALIAS_NAME); |
|
2140 orig_db_files = db_files; |
|
2141 |
|
2142 alias_db = hash_create (ALIAS_HASH_SIZE); |
|
2143 |
|
2144 while (db_files && *db_files) { |
|
2145 if (alias_build (&alias_db, *db_files)) |
|
2146 ok = true; |
|
2147 free (*db_files); |
|
2148 db_files++; |
|
2149 } |
|
2150 |
|
2151 if (!ok) { |
|
2152 free (alias_db.buckets); |
|
2153 alias_db.buckets = NULL; |
|
2154 } |
|
2155 |
|
2156 free (orig_db_files); |
|
2157 } |
|
2158 |
|
2159 /* Avoid doing anything if this PATH_ELT is irrelevant to the databases. */ |
|
2160 |
|
2161 str_list_type * |
|
2162 kpse_db_search (const char *name, const char *orig_path_elt, bool all) |
|
2163 { |
|
2164 char **db_dirs, **orig_dirs, **r; |
|
2165 const char *last_slash; |
|
2166 char *path_elt; |
|
2167 bool done; |
|
2168 str_list_type *ret = 0; |
|
2169 unsigned e; |
|
2170 char **aliases = NULL; |
|
2171 bool relevant = false; |
|
2172 |
|
2173 /* If we failed to build the database (or if this is the recursive |
|
2174 call to build the db path), quit. */ |
|
2175 if (db.buckets == NULL) |
|
2176 return NULL; |
|
2177 |
|
2178 /* When tex-glyph.c calls us looking for, e.g., dpi600/cmr10.pk, we |
|
2179 won't find it unless we change NAME to just `cmr10.pk' and append |
|
2180 `/dpi600' to PATH_ELT. We are justified in using a literal `/' |
|
2181 here, since that's what tex-glyph.c unconditionally uses in |
|
2182 DPI_BITMAP_SPEC. But don't do anything if the / begins NAME; that |
|
2183 should never happen. */ |
|
2184 last_slash = strrchr (name, '/'); |
|
2185 if (last_slash && last_slash != name) { |
|
2186 unsigned len = last_slash - name + 1; |
|
2187 char *dir_part = (char *) xmalloc (len); |
|
2188 strncpy (dir_part, name, len - 1); |
|
2189 dir_part[len - 1] = 0; |
|
2190 path_elt = concat3 (orig_path_elt, "/", dir_part); |
|
2191 name = last_slash + 1; |
|
2192 } else |
|
2193 path_elt = (char *) orig_path_elt; |
|
2194 |
|
2195 /* Don't bother doing any lookups if this `path_elt' isn't covered by |
|
2196 any of database directories. We do this not so much because the |
|
2197 extra couple of hash lookups matter -- they don't -- but rather |
|
2198 because we want to return NULL in this case, so path_search can |
|
2199 know to do a disk search. */ |
|
2200 for (e = 0; !relevant && e < STR_LIST_LENGTH (db_dir_list); e++) { |
|
2201 relevant = elt_in_db (STR_LIST_ELT (db_dir_list, e), path_elt); |
|
2202 } |
|
2203 if (!relevant) |
|
2204 return NULL; |
|
2205 |
|
2206 /* If we have aliases for this name, use them. */ |
|
2207 if (alias_db.buckets) |
|
2208 aliases = hash_lookup (alias_db, name); |
|
2209 |
|
2210 if (!aliases) { |
|
2211 aliases = XTALLOC1 (char *); |
|
2212 aliases[0] = NULL; |
|
2213 } |
|
2214 { /* Push aliases up by one and insert the original name at the front. */ |
|
2215 unsigned i; |
|
2216 unsigned len = 1; /* Have NULL element already allocated. */ |
|
2217 for (r = aliases; *r; r++) |
|
2218 len++; |
|
2219 XRETALLOC (aliases, len + 1, char *); |
|
2220 for (i = len; i > 0; i--) { |
|
2221 aliases[i] = aliases[i - 1]; |
|
2222 } |
|
2223 aliases[0] = (char *) name; |
|
2224 } |
|
2225 |
|
2226 done = false; |
|
2227 for (r = aliases; !done && *r; r++) { |
|
2228 char *atry = *r; |
|
2229 |
|
2230 /* We have an ls-R db. Look up `atry'. */ |
|
2231 orig_dirs = db_dirs = hash_lookup (db, atry); |
|
2232 |
|
2233 ret = XTALLOC1 (str_list_type); |
|
2234 *ret = str_list_init (); |
|
2235 |
|
2236 /* For each filename found, see if it matches the path element. For |
|
2237 example, if we have .../cx/cmr10.300pk and .../ricoh/cmr10.300pk, |
|
2238 and the path looks like .../cx, we don't want the ricoh file. */ |
|
2239 while (!done && db_dirs && *db_dirs) { |
|
2240 char *db_file = concat (*db_dirs, atry); |
|
2241 bool matched = match (db_file, path_elt); |
|
2242 |
|
2243 #ifdef KPSE_DEBUG |
|
2244 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
2245 DEBUGF3 ("db:match(%s,%s) = %d\n", db_file, path_elt, matched); |
|
2246 #endif |
|
2247 |
|
2248 /* We got a hit in the database. Now see if the file actually |
|
2249 exists, possibly under an alias. */ |
|
2250 if (matched) { |
|
2251 char *found = NULL; |
|
2252 if (kpse_readable_file (db_file)) { |
|
2253 found = db_file; |
|
2254 |
|
2255 } else { |
|
2256 char **a; |
|
2257 |
|
2258 free (db_file); /* `db_file' wasn't on disk. */ |
|
2259 |
|
2260 /* The hit in the DB doesn't exist in disk. Now try all its |
|
2261 aliases. For example, suppose we have a hierarchy on CD, |
|
2262 thus `mf.bas', but ls-R contains `mf.base'. Find it anyway. |
|
2263 Could probably work around this with aliases, but |
|
2264 this is pretty easy and shouldn't hurt. The upshot is that |
|
2265 if one of the aliases actually exists, we use that. */ |
|
2266 for (a = aliases + 1; *a && !found; a++) { |
|
2267 char *atry = concat (*db_dirs, *a); |
|
2268 if (kpse_readable_file (atry)) |
|
2269 found = atry; |
|
2270 else |
|
2271 free (atry); |
|
2272 } |
|
2273 } |
|
2274 |
|
2275 /* If we have a real file, add it to the list, maybe done. */ |
|
2276 if (found) { |
|
2277 str_list_add (ret, found); |
|
2278 if (!all && found) |
|
2279 done = true; |
|
2280 } |
|
2281 } else { /* no match in the db */ |
|
2282 free (db_file); |
|
2283 } |
|
2284 |
|
2285 |
|
2286 /* On to the next directory, if any. */ |
|
2287 db_dirs++; |
|
2288 } |
|
2289 |
|
2290 /* This is just the space for the pointers, not the strings. */ |
|
2291 if (orig_dirs && *orig_dirs) |
|
2292 free (orig_dirs); |
|
2293 } |
|
2294 |
|
2295 free (aliases); |
|
2296 |
|
2297 /* If we had to break up NAME, free the temporary PATH_ELT. */ |
|
2298 if (path_elt != orig_path_elt) |
|
2299 free (path_elt); |
|
2300 |
|
2301 return ret; |
|
2302 } |
|
2303 |
|
2304 /* kdefault.c: Expand extra colons. */ |
|
2305 |
|
2306 /* Check for leading colon first, then trailing, then doubled, since |
|
2307 that is fastest. Usually it will be leading or trailing. */ |
|
2308 |
|
2309 char * |
|
2310 kpse_expand_default (const char *path, const char *fallback) |
|
2311 { |
|
2312 unsigned path_length; |
|
2313 char *expansion; |
|
2314 |
|
2315 /* The default path better not be null. */ |
|
2316 assert (fallback); |
|
2317 |
|
2318 if (path == NULL) |
|
2319 expansion = xstrdup (fallback); |
|
2320 |
|
2321 /* Solitary or leading :? */ |
|
2322 else if (IS_ENV_SEP (*path)) |
|
2323 { |
|
2324 expansion = path[1] == 0 ? xstrdup (fallback) : concat (fallback, path); |
|
2325 } |
|
2326 |
|
2327 /* Sorry about the assignment in the middle of the expression, but |
|
2328 conventions were made to be flouted and all that. I don't see the |
|
2329 point of calling strlen twice or complicating the logic just to |
|
2330 avoid the assignment (especially now that I've pointed it out at |
|
2331 such great length). */ |
|
2332 else if (path[(path_length = strlen (path)) - 1] == ENV_SEP) |
|
2333 expansion = concat (path, fallback); |
|
2334 |
|
2335 /* OK, not leading or trailing. Check for doubled. */ |
|
2336 else |
|
2337 { |
|
2338 const char *loc; |
|
2339 |
|
2340 /* What we'll return if we find none. */ |
|
2341 expansion = xstrdup (path); |
|
2342 |
|
2343 for (loc = path; *loc; loc++) |
|
2344 { |
|
2345 if (IS_ENV_SEP (loc[0]) && IS_ENV_SEP (loc[1])) |
|
2346 { /* We have a doubled colon. */ |
|
2347 expansion = (char *) xmalloc (path_length + strlen (fallback) + 1); |
|
2348 |
|
2349 /* Copy stuff up to and including the first colon. */ |
|
2350 strncpy (expansion, path, loc - path + 1); |
|
2351 expansion[loc - path + 1] = 0; |
|
2352 |
|
2353 /* Copy in FALLBACK, and then the rest of PATH. */ |
|
2354 strcat (expansion, fallback); |
|
2355 strcat (expansion, loc + 1); |
|
2356 |
|
2357 break; |
|
2358 } |
|
2359 } |
|
2360 } |
|
2361 |
|
2362 return expansion; |
|
2363 } |
|
2364 |
|
2365 /* elt-dirs.c: Translate a path element to its corresponding |
|
2366 director{y,ies}. */ |
|
2367 |
|
2368 /* To avoid giving prototypes for all the routines and then their real |
|
2369 definitions, we give all the subroutines first. The entry point is |
|
2370 the last routine in the file. */ |
|
2371 |
|
2372 /* Make a copy of DIR (unless it's null) and save it in L. Ensure that |
|
2373 DIR ends with a DIR_SEP for the benefit of later searches. */ |
|
2374 |
|
2375 static void |
|
2376 dir_list_add (str_llist_type *l, const char *dir) |
|
2377 { |
|
2378 char last_char = dir[strlen (dir) - 1]; |
|
2379 char *saved_dir |
|
2380 = IS_DIR_SEP (last_char) || IS_DEVICE_SEP (last_char) |
|
2381 ? xstrdup (dir) |
|
2382 : concat (dir, DIR_SEP_STRING); |
|
2383 |
|
2384 str_llist_add (l, saved_dir); |
|
2385 } |
|
2386 |
|
2387 |
|
2388 /* If DIR is a directory, add it to the list L. */ |
|
2389 |
|
2390 static void |
|
2391 checked_dir_list_add (str_llist_type *l, const char *dir) |
|
2392 { |
|
2393 if (dir_p (dir)) |
|
2394 dir_list_add (l, dir); |
|
2395 } |
|
2396 |
|
2397 /* The cache. Typically, several paths have the same element; for |
|
2398 example, /usr/local/lib/texmf/fonts//. We don't want to compute the |
|
2399 expansion of such a thing more than once. Even though we also cache |
|
2400 the dir_links call, that's not enough -- without this path element |
|
2401 caching as well, the execution time doubles. */ |
|
2402 |
|
2403 typedef struct |
|
2404 { |
|
2405 const char *key; |
|
2406 str_llist_type *value; |
|
2407 } cache_entry; |
|
2408 |
|
2409 static cache_entry *the_cache = NULL; |
|
2410 static unsigned cache_length = 0; |
|
2411 |
|
2412 /* Associate KEY with VALUE. We implement the cache as a simple linear |
|
2413 list, since it's unlikely to ever be more than a dozen or so elements |
|
2414 long. We don't bother to check here if PATH has already been saved; |
|
2415 we always add it to our list. We copy KEY but not VALUE; not sure |
|
2416 that's right, but it seems to be all that's needed. */ |
|
2417 |
|
2418 static void |
|
2419 cache (const char *key, str_llist_type *value) |
|
2420 { |
|
2421 cache_length++; |
|
2422 XRETALLOC (the_cache, cache_length, cache_entry); |
|
2423 the_cache[cache_length - 1].key = xstrdup (key); |
|
2424 the_cache[cache_length - 1].value = value; |
|
2425 } |
|
2426 |
|
2427 |
|
2428 /* To retrieve, just check the list in order. */ |
|
2429 |
|
2430 static str_llist_type * |
|
2431 cached (const char *key) |
|
2432 { |
|
2433 unsigned p; |
|
2434 |
|
2435 for (p = 0; p < cache_length; p++) |
|
2436 { |
|
2437 if (FILESTRCASEEQ (the_cache[p].key, key)) |
|
2438 return the_cache[p].value; |
|
2439 } |
|
2440 |
|
2441 return NULL; |
|
2442 } |
|
2443 |
|
2444 /* Handle the magic path constructs. */ |
|
2445 |
|
2446 /* Declare recursively called routine. */ |
|
2447 static void expand_elt (str_llist_type *, const char *, unsigned); |
|
2448 |
|
2449 |
|
2450 /* POST is a pointer into the original element (which may no longer be |
|
2451 ELT) to just after the doubled DIR_SEP, perhaps to the null. Append |
|
2452 subdirectories of ELT (up to ELT_LENGTH, which must be a /) to |
|
2453 STR_LIST_PTR. */ |
|
2454 |
|
2455 #ifdef WIN32 |
|
2456 /* Shared across recursive calls, it acts like a stack. */ |
|
2457 static char dirname[MAX_PATH]; |
|
2458 #endif |
|
2459 |
|
2460 static void |
|
2461 do_subdir (str_llist_type *str_list_ptr, const char *elt, |
|
2462 unsigned elt_length, const char *post) |
|
2463 { |
|
2464 #ifdef WIN32 |
|
2465 WIN32_FIND_DATA find_file_data; |
|
2466 HANDLE hnd; |
|
2467 int proceed; |
|
2468 #else |
|
2469 DIR *dir; |
|
2470 struct dirent *e; |
|
2471 #endif /* not WIN32 */ |
|
2472 fn_type name; |
|
2473 |
|
2474 /* Some old compilers don't allow aggregate initialization. */ |
|
2475 name = fn_copy0 (elt, elt_length); |
|
2476 |
|
2477 assert (IS_DIR_SEP (elt[elt_length - 1]) |
|
2478 || IS_DEVICE_SEP (elt[elt_length - 1])); |
|
2479 |
|
2480 #if defined (WIN32) |
|
2481 strcpy(dirname, FN_STRING(name)); |
|
2482 strcat(dirname, "/*.*"); /* "*.*" or "*" -- seems equivalent. */ |
|
2483 hnd = FindFirstFile(dirname, &find_file_data); |
|
2484 |
|
2485 if (hnd == INVALID_HANDLE_VALUE) { |
|
2486 fn_free(&name); |
|
2487 return; |
|
2488 } |
|
2489 |
|
2490 /* Include top level before subdirectories, if nothing to match. */ |
|
2491 if (*post == 0) |
|
2492 dir_list_add (str_list_ptr, FN_STRING (name)); |
|
2493 else { |
|
2494 /* If we do have something to match, see if it exists. For |
|
2495 example, POST might be `pk/ljfour', and they might have a |
|
2496 directory `$TEXMF/fonts/pk/ljfour' that we should find. */ |
|
2497 fn_str_grow (&name, post); |
|
2498 expand_elt (str_list_ptr, FN_STRING (name), elt_length); |
|
2499 fn_shrink_to (&name, elt_length); |
|
2500 } |
|
2501 proceed = 1; |
|
2502 while (proceed) { |
|
2503 if (find_file_data.cFileName[0] != '.') { |
|
2504 /* Construct the potential subdirectory name. */ |
|
2505 fn_str_grow (&name, find_file_data.cFileName); |
|
2506 if (find_file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
|
2507 unsigned potential_len = FN_LENGTH (name); |
|
2508 |
|
2509 /* It's a directory, so append the separator. */ |
|
2510 fn_str_grow (&name, DIR_SEP_STRING); |
|
2511 |
|
2512 do_subdir (str_list_ptr, FN_STRING (name), |
|
2513 potential_len, post); |
|
2514 } |
|
2515 fn_shrink_to (&name, elt_length); |
|
2516 } |
|
2517 proceed = FindNextFile (hnd, &find_file_data); |
|
2518 } |
|
2519 fn_free (&name); |
|
2520 FindClose(hnd); |
|
2521 |
|
2522 #else /* not WIN32 */ |
|
2523 |
|
2524 /* If we can't open it, quit. */ |
|
2525 dir = opendir (FN_STRING (name)); |
|
2526 if (dir == NULL) |
|
2527 { |
|
2528 fn_free (&name); |
|
2529 return; |
|
2530 } |
|
2531 |
|
2532 /* Include top level before subdirectories, if nothing to match. */ |
|
2533 if (*post == 0) |
|
2534 dir_list_add (str_list_ptr, FN_STRING (name)); |
|
2535 else |
|
2536 { /* If we do have something to match, see if it exists. For |
|
2537 example, POST might be `pk/ljfour', and they might have a |
|
2538 directory `$TEXMF/fonts/pk/ljfour' that we should find. */ |
|
2539 fn_str_grow (&name, post); |
|
2540 expand_elt (str_list_ptr, FN_STRING (name), elt_length); |
|
2541 fn_shrink_to (&name, elt_length); |
|
2542 } |
|
2543 |
|
2544 while ((e = readdir (dir)) != NULL) |
|
2545 { /* If it begins with a `.', never mind. (This allows ``hidden'' |
|
2546 directories that the algorithm won't find.) */ |
|
2547 if (e->d_name[0] != '.') |
|
2548 { |
|
2549 int links; |
|
2550 |
|
2551 /* Construct the potential subdirectory name. */ |
|
2552 fn_str_grow (&name, e->d_name); |
|
2553 |
|
2554 /* If we can't stat it, or if it isn't a directory, continue. */ |
|
2555 links = dir_links (FN_STRING (name)); |
|
2556 |
|
2557 if (links >= 0) |
|
2558 { |
|
2559 unsigned potential_len = FN_LENGTH (name); |
|
2560 |
|
2561 /* It's a directory, so append the separator. */ |
|
2562 fn_str_grow (&name, DIR_SEP_STRING); |
|
2563 |
|
2564 /* Should we recurse? To see if the subdirectory is a |
|
2565 leaf, check if it has two links (one for . and one for |
|
2566 ..). This means that symbolic links to directories do |
|
2567 not affect the leaf-ness. This is arguably wrong, but |
|
2568 the only alternative I know of is to stat every entry |
|
2569 in the directory, and that is unacceptably slow. |
|
2570 |
|
2571 The #ifdef here makes all this configurable at |
|
2572 compile-time, so that if we're using VMS directories or |
|
2573 some such, we can still find subdirectories, even if it |
|
2574 is much slower. */ |
|
2575 #ifdef ST_NLINK_TRICK |
|
2576 #ifdef AMIGA |
|
2577 /* With SAS/C++ 6.55 on the Amiga, `stat' sets the `st_nlink' |
|
2578 field to -1 for a file, or to 1 for a directory. */ |
|
2579 if (links == 1) |
|
2580 #else |
|
2581 if (links > 2) |
|
2582 #endif /* not AMIGA */ |
|
2583 #endif /* not ST_NLINK_TRICK */ |
|
2584 /* All criteria are met; find subdirectories. */ |
|
2585 do_subdir (str_list_ptr, FN_STRING (name), |
|
2586 potential_len, post); |
|
2587 #ifdef ST_NLINK_TRICK |
|
2588 else if (*post == 0) |
|
2589 /* Nothing to match, no recursive subdirectories to |
|
2590 look for: we're done with this branch. Add it. */ |
|
2591 dir_list_add (str_list_ptr, FN_STRING (name)); |
|
2592 #endif |
|
2593 } |
|
2594 |
|
2595 /* Remove the directory entry we just checked from `name'. */ |
|
2596 fn_shrink_to (&name, elt_length); |
|
2597 } |
|
2598 } |
|
2599 |
|
2600 fn_free (&name); |
|
2601 xclosedir (dir); |
|
2602 #endif /* not WIN32 */ |
|
2603 } |
|
2604 |
|
2605 |
|
2606 /* Assume ELT is non-empty and non-NULL. Return list of corresponding |
|
2607 directories (with no terminating NULL entry) in STR_LIST_PTR. Start |
|
2608 looking for magic constructs at START. */ |
|
2609 |
|
2610 static void |
|
2611 expand_elt (str_llist_type *str_list_ptr, const char *elt, unsigned start) |
|
2612 { |
|
2613 const char *dir = elt + start; |
|
2614 const char *post; |
|
2615 |
|
2616 while (*dir != 0) |
|
2617 { |
|
2618 if (IS_DIR_SEP (*dir)) |
|
2619 { |
|
2620 /* If two or more consecutive /'s, find subdirectories. */ |
|
2621 if (IS_DIR_SEP (dir[1])) |
|
2622 { |
|
2623 for (post = dir + 1; IS_DIR_SEP (*post); post++) ; |
|
2624 do_subdir (str_list_ptr, elt, dir - elt + 1, post); |
|
2625 return; |
|
2626 } |
|
2627 |
|
2628 /* No special stuff at this slash. Keep going. */ |
|
2629 } |
|
2630 |
|
2631 dir++; |
|
2632 } |
|
2633 |
|
2634 /* When we reach the end of ELT, it will be a normal filename. */ |
|
2635 checked_dir_list_add (str_list_ptr, elt); |
|
2636 } |
|
2637 |
|
2638 /* Here is the entry point. Returns directory list for ELT. */ |
|
2639 |
|
2640 str_llist_type * |
|
2641 kpse_element_dirs (const char *elt) |
|
2642 { |
|
2643 str_llist_type *ret; |
|
2644 |
|
2645 /* If given nothing, return nothing. */ |
|
2646 if (!elt || !*elt) |
|
2647 return NULL; |
|
2648 |
|
2649 /* If we've already cached the answer for ELT, return it. */ |
|
2650 ret = cached (elt); |
|
2651 if (ret) |
|
2652 return ret; |
|
2653 |
|
2654 /* We're going to have a real directory list to return. */ |
|
2655 ret = XTALLOC1 (str_llist_type); |
|
2656 *ret = NULL; |
|
2657 |
|
2658 /* We handle the hard case in a subroutine. */ |
|
2659 expand_elt (ret, elt, 0); |
|
2660 |
|
2661 /* Remember the directory list we just found, in case future calls are |
|
2662 made with the same ELT. */ |
|
2663 cache (elt, ret); |
|
2664 |
|
2665 #ifdef KPSE_DEBUG |
|
2666 if (KPSE_DEBUG_P (KPSE_DEBUG_EXPAND)) |
|
2667 { |
|
2668 DEBUGF1 ("path element %s =>", elt); |
|
2669 if (ret) |
|
2670 { |
|
2671 str_llist_elt_type *e; |
|
2672 for (e = *ret; e; e = STR_LLIST_NEXT (*e)) |
|
2673 fprintf (stderr, " %s", STR_LLIST (*e)); |
|
2674 } |
|
2675 putc ('\n', stderr); |
|
2676 fflush (stderr); |
|
2677 } |
|
2678 #endif /* KPSE_DEBUG */ |
|
2679 |
|
2680 return ret; |
|
2681 } |
|
2682 |
|
2683 /* path-elt.c: Return the stuff between colons. */ |
|
2684 |
|
2685 /* The static (but dynamically allocated) area we return the answer in, |
|
2686 and how much we've currently allocated for it. */ |
|
2687 static char *elt = NULL; |
|
2688 static unsigned elt_alloc = 0; |
|
2689 |
|
2690 /* The path we're currently working on. */ |
|
2691 static const char *path = NULL; |
|
2692 |
|
2693 /* Upon entry, the static `path' is at the first (and perhaps last) |
|
2694 character of the return value, or else NULL if we're at the end (or |
|
2695 haven't been called). I make no provision for caching the results; |
|
2696 thus, we parse the same path over and over, on every lookup. If that |
|
2697 turns out to be a significant lose, it can be fixed, but I'm guessing |
|
2698 disk accesses overwhelm everything else. If ENV_P is true, use |
|
2699 IS_ENV_SEP; else use IS_DIR_SEP. */ |
|
2700 |
|
2701 static char * |
|
2702 element (const char *passed_path, bool env_p) |
|
2703 { |
|
2704 const char *p; |
|
2705 char *ret; |
|
2706 int brace_level; |
|
2707 unsigned len; |
|
2708 |
|
2709 if (passed_path) |
|
2710 path = passed_path; |
|
2711 /* Check if called with NULL, and no previous path (perhaps we reached |
|
2712 the end). */ |
|
2713 else if (!path) |
|
2714 return NULL; |
|
2715 |
|
2716 /* OK, we have a non-null `path' if we get here. */ |
|
2717 assert (path); |
|
2718 p = path; |
|
2719 |
|
2720 /* Find the next colon not enclosed by braces (or the end of the path). */ |
|
2721 brace_level = 0; |
|
2722 while (*p != 0 && !(brace_level == 0 |
|
2723 && (env_p ? IS_ENV_SEP (*p) : IS_DIR_SEP (*p)))) { |
|
2724 if (*p == '{') ++brace_level; |
|
2725 else if (*p == '}') --brace_level; |
|
2726 ++p; |
|
2727 } |
|
2728 |
|
2729 /* Return the substring starting at `path'. */ |
|
2730 len = p - path; |
|
2731 |
|
2732 /* Make sure we have enough space (including the null byte). */ |
|
2733 if (len + 1 > elt_alloc) |
|
2734 { |
|
2735 elt_alloc = len + 1; |
|
2736 elt = (char *) xrealloc (elt, elt_alloc); |
|
2737 } |
|
2738 |
|
2739 strncpy (elt, path, len); |
|
2740 elt[len] = 0; |
|
2741 ret = elt; |
|
2742 |
|
2743 /* If we are at the end, return NULL next time. */ |
|
2744 if (path[len] == 0) |
|
2745 path = NULL; |
|
2746 else |
|
2747 path += len + 1; |
|
2748 |
|
2749 return ret; |
|
2750 } |
|
2751 |
|
2752 char * |
|
2753 kpse_path_element (const char *p) |
|
2754 { |
|
2755 return element (p, true); |
|
2756 } |
|
2757 |
|
2758 char * |
|
2759 kpse_filename_component (const char *p) |
|
2760 { |
|
2761 return element (p, false); |
|
2762 } |
|
2763 |
4385
|
2764 /* xfopen.c: fopen and fclose with error checking. */ |
|
2765 |
|
2766 /* These routines just check the return status from standard library |
|
2767 routines and abort if an error happens. */ |
|
2768 |
|
2769 FILE * |
|
2770 xfopen (const char *filename, const char *mode) |
|
2771 { |
|
2772 FILE *f; |
|
2773 |
|
2774 assert (filename && mode); |
|
2775 |
|
2776 f = fopen (filename, mode); |
|
2777 if (f == NULL) |
|
2778 FATAL_PERROR (filename); |
|
2779 |
|
2780 return f; |
|
2781 } |
|
2782 |
|
2783 void |
|
2784 xfclose (FILE *f, const char *filename) |
|
2785 { |
|
2786 assert (f); |
|
2787 |
|
2788 if (fclose (f) == EOF) |
|
2789 FATAL_PERROR (filename); |
|
2790 } |
|
2791 |
|
2792 /* xftell.c: ftell with error checking. */ |
|
2793 |
|
2794 unsigned long |
|
2795 xftell (FILE *f, char *filename) |
|
2796 { |
|
2797 long where = ftell (f); |
|
2798 |
|
2799 if (where < 0) |
|
2800 FATAL_PERROR (filename); |
|
2801 |
|
2802 return where; |
|
2803 } |
|
2804 |
4386
|
2805 #ifndef WIN32 |
4385
|
2806 void |
|
2807 xclosedir (DIR *d) |
|
2808 { |
|
2809 #ifdef CLOSEDIR_VOID |
|
2810 closedir (d); |
|
2811 #else |
|
2812 int ret = closedir (d); |
|
2813 |
|
2814 if (ret != 0) |
|
2815 FATAL ("closedir failed"); |
|
2816 #endif |
|
2817 } |
4386
|
2818 #endif |
4385
|
2819 |
|
2820 /* xmalloc.c: malloc with error checking. */ |
|
2821 |
|
2822 void * |
|
2823 xmalloc (unsigned size) |
|
2824 { |
|
2825 void *new_mem = (void *) malloc (size); |
|
2826 |
|
2827 if (new_mem == NULL) |
|
2828 { |
|
2829 fprintf (stderr, "fatal: memory exhausted (xmalloc of %u bytes).\n", |
|
2830 size); |
|
2831 /* 1 means success on VMS, so pick a random number (ASCII `K'). */ |
|
2832 exit (75); |
|
2833 } |
|
2834 |
|
2835 return new_mem; |
|
2836 } |
|
2837 |
|
2838 /* xrealloc.c: realloc with error checking. */ |
|
2839 |
|
2840 extern void *xmalloc (unsigned); |
|
2841 |
|
2842 void * |
|
2843 xrealloc (void *old_ptr, unsigned size) |
|
2844 { |
|
2845 void *new_mem; |
|
2846 |
|
2847 if (old_ptr == NULL) |
|
2848 new_mem = xmalloc (size); |
|
2849 else |
|
2850 { |
|
2851 new_mem = (void *) realloc (old_ptr, size); |
|
2852 if (new_mem == NULL) |
|
2853 { |
|
2854 /* We used to print OLD_PTR here using %x, and casting its |
|
2855 value to unsigned, but that lost on the Alpha, where |
|
2856 pointers and unsigned had different sizes. Since the info |
|
2857 is of little or no value anyway, just don't print it. */ |
|
2858 fprintf (stderr, "fatal: memory exhausted (realloc of %u bytes).\n", |
|
2859 size); |
|
2860 /* 1 means success on VMS, so pick a random number (ASCII `B'). */ |
|
2861 exit (66); |
|
2862 } |
|
2863 } |
|
2864 |
|
2865 return new_mem; |
|
2866 } |
|
2867 |
|
2868 /* xstrdup.c: strdup with error checking. */ |
|
2869 |
|
2870 /* Return a copy of S in new storage. */ |
|
2871 |
|
2872 char * |
|
2873 xstrdup (const char *s) |
|
2874 { |
|
2875 char *new_string = (char *) xmalloc (strlen (s) + 1); |
|
2876 return strcpy (new_string, s); |
|
2877 } |
|
2878 |
|
2879 /* dir.c: directory operations. */ |
|
2880 |
|
2881 /* Return true if FN is a directory or a symlink to a directory, |
|
2882 false if not. */ |
|
2883 |
|
2884 int |
|
2885 dir_p (const char *fn) |
|
2886 { |
|
2887 #ifdef WIN32 |
4386
|
2888 unsigned int fa = GetFileAttributes(fn); |
4385
|
2889 return (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY)); |
|
2890 #else |
|
2891 struct stat stats; |
|
2892 return stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode); |
|
2893 #endif |
|
2894 } |
|
2895 |
|
2896 #ifndef WIN32 |
|
2897 |
|
2898 /* Return -1 if FN isn't a directory, else its number of links. |
|
2899 Duplicate the call to stat; no need to incur overhead of a function |
|
2900 call for that little bit of cleanliness. */ |
|
2901 |
|
2902 int |
|
2903 dir_links (const char *fn) |
|
2904 { |
|
2905 static hash_table_type link_table; |
|
2906 char **hash_ret; |
|
2907 long ret; |
|
2908 |
|
2909 if (link_table.size == 0) |
|
2910 link_table = hash_create (457); |
|
2911 |
|
2912 #ifdef KPSE_DEBUG |
|
2913 /* This is annoying, but since we're storing integers as pointers, we |
|
2914 can't print them as strings. */ |
|
2915 if (KPSE_DEBUG_P (KPSE_DEBUG_HASH)) |
|
2916 kpse_debug_hash_lookup_int = 1; |
|
2917 #endif |
|
2918 |
|
2919 hash_ret = hash_lookup (link_table, fn); |
|
2920 |
|
2921 #ifdef KPSE_DEBUG |
|
2922 if (KPSE_DEBUG_P (KPSE_DEBUG_HASH)) |
|
2923 kpse_debug_hash_lookup_int = 0; |
|
2924 #endif |
|
2925 |
|
2926 /* Have to cast the int we need to/from the const_string that the hash |
|
2927 table stores for values. Let's hope an int fits in a pointer. */ |
|
2928 if (hash_ret) |
|
2929 ret = (long) *hash_ret; |
|
2930 else |
|
2931 { |
|
2932 struct stat stats; |
|
2933 ret = stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode) |
|
2934 ? stats.st_nlink : (unsigned) -1; |
|
2935 |
|
2936 /* It's up to us to copy the value. */ |
|
2937 hash_insert (&link_table, xstrdup (fn), (const char *) ret); |
|
2938 |
|
2939 #ifdef KPSE_DEBUG |
|
2940 if (KPSE_DEBUG_P (KPSE_DEBUG_STAT)) |
|
2941 DEBUGF2 ("dir_links(%s) => %ld\n", fn, ret); |
|
2942 #endif |
|
2943 } |
|
2944 |
|
2945 return ret; |
|
2946 } |
|
2947 |
|
2948 #endif /* !WIN32 */ |
|
2949 |
|
2950 /* hash.c: hash table operations. */ |
|
2951 |
|
2952 /* The hash function. We go for simplicity here. */ |
|
2953 |
|
2954 /* All our hash tables are related to filenames. */ |
|
2955 #ifdef MONOCASE_FILENAMES |
|
2956 #define TRANSFORM(x) toupper (x) |
|
2957 #else |
|
2958 #define TRANSFORM(x) (x) |
|
2959 #endif |
|
2960 |
|
2961 static unsigned |
|
2962 hash (hash_table_type table, const char *key) |
|
2963 { |
|
2964 unsigned n = 0; |
|
2965 |
|
2966 /* Our keys aren't often anagrams of each other, so no point in |
|
2967 weighting the characters. */ |
|
2968 while (*key != 0) |
|
2969 n = (n + n + TRANSFORM (*key++)) % table.size; |
|
2970 |
|
2971 return n; |
|
2972 } |
|
2973 |
|
2974 hash_table_type |
|
2975 hash_create (unsigned size) |
|
2976 { |
|
2977 /* hash_table_type ret; changed into "static ..." to work around gcc |
|
2978 optimizer bug for Alpha. */ |
|
2979 static hash_table_type ret; |
|
2980 unsigned b; |
|
2981 ret.buckets = XTALLOC (size, hash_element_type *); |
|
2982 ret.size = size; |
|
2983 |
|
2984 /* calloc's zeroes aren't necessarily NULL, so be safe. */ |
|
2985 for (b = 0; b <ret.size; b++) |
|
2986 ret.buckets[b] = NULL; |
|
2987 |
|
2988 return ret; |
|
2989 } |
|
2990 |
|
2991 /* Whether or not KEY is already in MAP, insert it and VALUE. Do not |
|
2992 duplicate the strings, in case they're being purposefully shared. */ |
|
2993 |
|
2994 void |
|
2995 hash_insert (hash_table_type *table, const char *key, const char *value) |
|
2996 { |
|
2997 unsigned n = hash (*table, key); |
|
2998 hash_element_type *new_elt = XTALLOC1 (hash_element_type); |
|
2999 |
|
3000 new_elt->key = key; |
|
3001 new_elt->value = value; |
|
3002 new_elt->next = NULL; |
|
3003 |
|
3004 /* Insert the new element at the end of the list. */ |
|
3005 if (!table->buckets[n]) |
|
3006 /* first element in bucket is a special case. */ |
|
3007 table->buckets[n] = new_elt; |
|
3008 else |
|
3009 { |
|
3010 hash_element_type *loc = table->buckets[n]; |
|
3011 while (loc->next) /* Find the last element. */ |
|
3012 loc = loc->next; |
|
3013 loc->next = new_elt; /* Insert the new one after. */ |
|
3014 } |
|
3015 } |
|
3016 |
|
3017 /* Look up STR in MAP. Return a (dynamically-allocated) list of the |
|
3018 corresponding strings or NULL if no match. */ |
|
3019 |
|
3020 #ifdef KPSE_DEBUG |
|
3021 /* Print the hash values as integers if this is nonzero. */ |
|
3022 int kpse_debug_hash_lookup_int = 0; |
|
3023 #endif |
|
3024 |
|
3025 char ** |
|
3026 hash_lookup (hash_table_type table, const char *key) |
|
3027 { |
|
3028 hash_element_type *p; |
|
3029 str_list_type ret; |
|
3030 unsigned n = hash (table, key); |
|
3031 ret = str_list_init (); |
|
3032 |
|
3033 /* Look at everything in this bucket. */ |
|
3034 for (p = table.buckets[n]; p != NULL; p = p->next) |
|
3035 if (FILESTRCASEEQ (key, p->key)) |
|
3036 /* Cast because the general str_list_type shouldn't force const data. */ |
|
3037 str_list_add (&ret, (char *) p->value); |
|
3038 |
|
3039 /* If we found anything, mark end of list with null. */ |
|
3040 if (STR_LIST (ret)) |
|
3041 str_list_add (&ret, NULL); |
|
3042 |
|
3043 #ifdef KPSE_DEBUG |
|
3044 if (KPSE_DEBUG_P (KPSE_DEBUG_HASH)) |
|
3045 { |
|
3046 DEBUGF1 ("hash_lookup(%s) =>", key); |
|
3047 if (!STR_LIST (ret)) |
|
3048 fputs (" (nil)\n", stderr); |
|
3049 else |
|
3050 { |
|
3051 char **r; |
|
3052 for (r = STR_LIST (ret); *r; r++) |
|
3053 { |
|
3054 putc (' ', stderr); |
|
3055 if (kpse_debug_hash_lookup_int) |
|
3056 fprintf (stderr, "%ld", (long) *r); |
|
3057 else |
|
3058 fputs (*r, stderr); |
|
3059 } |
|
3060 putc ('\n', stderr); |
|
3061 } |
|
3062 fflush (stderr); |
|
3063 } |
|
3064 #endif |
|
3065 |
|
3066 return STR_LIST (ret); |
|
3067 } |
|
3068 |
|
3069 /* We only print nonempty buckets, to decrease output volume. */ |
|
3070 |
|
3071 void |
|
3072 hash_print (hash_table_type table, int summary_only) |
|
3073 { |
|
3074 unsigned b; |
|
3075 unsigned total_elements = 0, total_buckets = 0; |
|
3076 |
|
3077 for (b = 0; b < table.size; b++) { |
|
3078 hash_element_type *bucket = table.buckets[b]; |
|
3079 |
|
3080 if (bucket) { |
|
3081 unsigned len = 1; |
|
3082 hash_element_type *tb; |
|
3083 |
|
3084 total_buckets++; |
|
3085 if (!summary_only) fprintf (stderr, "%4d ", b); |
|
3086 |
|
3087 for (tb = bucket->next; tb != NULL; tb = tb->next) |
|
3088 len++; |
|
3089 if (!summary_only) fprintf (stderr, ":%-5d", len); |
|
3090 total_elements += len; |
|
3091 |
|
3092 if (!summary_only) { |
|
3093 for (tb = bucket; tb != NULL; tb = tb->next) |
|
3094 fprintf (stderr, " %s=>%s", tb->key, tb->value); |
|
3095 putc ('\n', stderr); |
|
3096 } |
|
3097 } |
|
3098 } |
|
3099 |
|
3100 fprintf (stderr, |
|
3101 "%u buckets, %u nonempty (%u%%); %u entries, average chain %.1f.\n", |
|
3102 table.size, |
|
3103 total_buckets, |
|
3104 100 * total_buckets / table.size, |
|
3105 total_elements, |
|
3106 total_buckets ? total_elements / (double) total_buckets : 0.0); |
|
3107 } |
|
3108 |
|
3109 /* concat.c: dynamic string concatenation. */ |
|
3110 |
|
3111 /* Return the concatenation of S1 and S2. See `concatn.c' for a |
|
3112 `concatn', which takes a variable number of arguments. */ |
|
3113 |
|
3114 char * |
|
3115 concat (const char *s1, const char *s2) |
|
3116 { |
|
3117 char *answer = (char *) xmalloc (strlen (s1) + strlen (s2) + 1); |
|
3118 strcpy (answer, s1); |
|
3119 strcat (answer, s2); |
|
3120 |
|
3121 return answer; |
|
3122 } |
|
3123 |
|
3124 /* concat3.c: concatenate three strings. */ |
|
3125 |
|
3126 char * |
|
3127 concat3 (const char *s1, const char *s2, const char *s3) |
|
3128 { |
|
3129 char *answer |
|
3130 = (char *) xmalloc (strlen (s1) + strlen (s2) + strlen (s3) + 1); |
|
3131 strcpy (answer, s1); |
|
3132 strcat (answer, s2); |
|
3133 strcat (answer, s3); |
|
3134 |
|
3135 return answer; |
|
3136 } |
|
3137 |
|
3138 /* concatn.c: Concatenate an arbitrary number of strings. */ |
|
3139 |
|
3140 /* OK, it would be epsilon more efficient to compute the total length |
|
3141 and then do the copying ourselves, but I doubt it matters in reality. */ |
|
3142 |
|
3143 char * |
|
3144 concatn (const char *str1, ...) |
|
3145 { |
|
3146 char *arg; |
|
3147 char *ret; |
|
3148 va_list ap; |
|
3149 |
|
3150 va_start (ap, str1); |
|
3151 |
|
3152 if (!str1) |
|
3153 return NULL; |
|
3154 |
|
3155 ret = xstrdup (str1); |
|
3156 |
|
3157 while ((arg = va_arg (ap, char *)) != NULL) |
|
3158 { |
|
3159 char *temp = concat (ret, arg); |
|
3160 free (ret); |
|
3161 ret = temp; |
|
3162 } |
|
3163 va_end (ap); |
|
3164 |
|
3165 return ret; |
|
3166 } |
|
3167 |
|
3168 /* debug.c: Help the user discover what's going on. */ |
|
3169 |
|
3170 #ifdef KPSE_DEBUG |
|
3171 |
|
3172 unsigned int kpathsea_debug = 0; |
|
3173 |
|
3174 /* If the real definitions of fopen or fclose are macros, we lose -- the |
|
3175 #undef won't restore them. */ |
|
3176 |
|
3177 FILE * |
|
3178 fopen (const char *filename, const char *mode) |
|
3179 { |
|
3180 #undef fopen |
|
3181 FILE *ret = fopen (filename, mode); |
|
3182 |
|
3183 if (KPSE_DEBUG_P (KPSE_DEBUG_FOPEN)) |
|
3184 DEBUGF3 ("fopen(%s, %s) => 0x%lx\n", filename, mode, (unsigned long) ret); |
|
3185 |
|
3186 return ret; |
|
3187 } |
|
3188 |
|
3189 int |
|
3190 fclose (FILE *f) |
|
3191 { |
|
3192 #undef fclose |
|
3193 int ret = fclose (f); |
|
3194 |
|
3195 if (KPSE_DEBUG_P (KPSE_DEBUG_FOPEN)) |
|
3196 DEBUGF2 ("fclose(0x%lx) => %d\n", (unsigned long) f, ret); |
|
3197 |
|
3198 return ret; |
|
3199 } |
|
3200 |
|
3201 #endif |
|
3202 |
|
3203 /* find-suffix.c: return the stuff after a dot. */ |
|
3204 |
|
3205 /* Return pointer to first character after `.' in last directory element |
|
3206 of NAME. If the name is `foo' or `/foo.bar/baz', we have no extension. */ |
|
3207 |
|
3208 char * |
|
3209 find_suffix (const char *name) |
|
3210 { |
|
3211 const char *slash_pos; |
|
3212 char *dot_pos = strrchr (name, '.'); |
|
3213 |
|
3214 if (dot_pos == NULL) |
|
3215 return NULL; |
|
3216 |
|
3217 for (slash_pos = name + strlen (name); |
|
3218 slash_pos > dot_pos && !IS_DIR_SEP (*slash_pos); |
|
3219 slash_pos--) |
|
3220 ; |
|
3221 |
|
3222 return slash_pos > dot_pos ? NULL : dot_pos + 1; |
|
3223 } |
|
3224 |
|
3225 /* rm-suffix.c: remove any suffix. */ |
|
3226 |
|
3227 /* Generic const warning -- see extend-fname.c. */ |
|
3228 |
|
3229 char * |
|
3230 remove_suffix (const char *s) |
|
3231 { |
|
3232 char *ret; |
|
3233 const char *suffix = find_suffix (s); |
|
3234 |
|
3235 if (suffix) |
|
3236 { |
|
3237 /* Back up to before the dot. */ |
|
3238 suffix--; |
|
3239 ret = (char *) xmalloc (suffix - s + 1); |
|
3240 strncpy (ret, s, suffix - s); |
|
3241 ret[suffix - s] = 0; |
|
3242 } |
|
3243 else |
|
3244 ret = (char *) s; |
|
3245 |
|
3246 return ret; |
|
3247 } |
|
3248 |
|
3249 /* readable.c: check if a filename is a readable non-directory file. */ |
|
3250 |
|
3251 /* Truncate any too-long components in NAME, returning the result. It's |
|
3252 too bad this is necessary. See comments in readable.c for why. */ |
|
3253 |
|
3254 static char * |
|
3255 kpse_truncate_filename (const char *name) |
|
3256 { |
|
3257 unsigned c_len = 0; /* Length of current component. */ |
|
3258 unsigned ret_len = 0; /* Length of constructed result. */ |
|
3259 |
|
3260 /* Allocate enough space. */ |
|
3261 char *ret = (char *) xmalloc (strlen (name) + 1); |
|
3262 |
|
3263 for (; *name; name++) |
|
3264 { |
|
3265 if (IS_DIR_SEP (*name) || IS_DEVICE_SEP (*name)) |
|
3266 { /* At a directory delimiter, reset component length. */ |
|
3267 c_len = 0; |
|
3268 } |
|
3269 else if (c_len > NAME_MAX) |
|
3270 { /* If past the max for a component, ignore this character. */ |
|
3271 continue; |
|
3272 } |
|
3273 |
|
3274 /* Copy this character. */ |
|
3275 ret[ret_len++] = *name; |
|
3276 c_len++; |
|
3277 } |
|
3278 ret[ret_len] = 0; |
|
3279 |
|
3280 return ret; |
|
3281 } |
|
3282 |
|
3283 /* If access can read FN, run stat (assigning to stat buffer ST) and |
|
3284 check that fn is not a directory. Don't check for just being a |
|
3285 regular file, as it is potentially useful to read fifo's or some |
|
3286 kinds of devices. */ |
|
3287 |
|
3288 #ifdef WIN32 |
4386
|
3289 static inline bool |
|
3290 READABLE (const char *fn, struct stat&) |
|
3291 { |
|
3292 return (GetFileAttributes(fn) != 0xFFFFFFFF |
|
3293 && !(GetFileAttributes(fn) & FILE_ATTRIBUTE_DIRECTORY)); |
|
3294 } |
4385
|
3295 #else |
4386
|
3296 static inline bool |
|
3297 READABLE (const char *fn, struct stat& st) |
|
3298 { |
|
3299 return (access (fn, R_OK) == 0 |
|
3300 && stat (fn, &(st)) == 0 |
|
3301 && !S_ISDIR (st.st_mode)); |
|
3302 } |
4385
|
3303 #endif |
|
3304 |
|
3305 /* POSIX invented the brain-damage of not necessarily truncating |
|
3306 filename components; the system's behavior is defined by the value of |
|
3307 the symbol _POSIX_NO_TRUNC, but you can't change it dynamically! |
|
3308 |
|
3309 Generic const return warning. See extend-fname.c. */ |
|
3310 |
|
3311 char * |
|
3312 kpse_readable_file (const char *name) |
|
3313 { |
|
3314 struct stat st; |
|
3315 char *ret; |
|
3316 |
|
3317 if (READABLE (name, st)) { |
|
3318 ret = (char *) name; |
|
3319 |
|
3320 #ifdef ENAMETOOLONG |
|
3321 } else if (errno == ENAMETOOLONG) { |
|
3322 ret = kpse_truncate_filename (name); |
|
3323 |
|
3324 /* Perhaps some other error will occur with the truncated name, so |
|
3325 let's call access again. */ |
|
3326 if (!READABLE (ret, st)) |
|
3327 { /* Failed. */ |
|
3328 if (ret != name) free (ret); |
|
3329 ret = NULL; |
|
3330 } |
|
3331 #endif /* ENAMETOOLONG */ |
|
3332 |
|
3333 } else { /* Some other error. */ |
|
3334 if (errno == EACCES) { /* Maybe warn them if permissions are bad. */ |
|
3335 perror (name); |
|
3336 } |
|
3337 ret = NULL; |
|
3338 } |
|
3339 |
|
3340 return ret; |
|
3341 } |
|
3342 |
|
3343 /* absolute.c: Test if a filename is absolute or explicitly relative. */ |
|
3344 |
|
3345 /* Sorry this is such a system-dependent mess, but I can't see any way |
|
3346 to usefully generalize. */ |
|
3347 |
|
3348 int |
|
3349 kpse_absolute_p (const char *filename, int relative_ok) |
|
3350 { |
|
3351 int absolute = IS_DIR_SEP (*filename) |
|
3352 #ifdef DOSISH |
|
3353 /* Novell allows non-alphanumeric drive letters. */ |
|
3354 || (*filename && IS_DEVICE_SEP (filename[1])) |
|
3355 #endif /* DOSISH */ |
|
3356 #ifdef WIN32 |
|
3357 /* UNC names */ |
|
3358 || (*filename == '\\' && filename[1] == '\\') |
|
3359 #endif |
|
3360 ; |
|
3361 int explicit_relative |
|
3362 = relative_ok |
|
3363 && (*filename == '.' && (IS_DIR_SEP (filename[1]) |
|
3364 || (filename[1] == '.' && IS_DIR_SEP (filename[2])))); |
|
3365 |
|
3366 return absolute || explicit_relative; |
|
3367 } |
|
3368 |
|
3369 /* str-list.c: define routines for string lists. */ |
|
3370 |
|
3371 /* See the lib.h file for comments. */ |
|
3372 |
|
3373 str_list_type |
|
3374 str_list_init (void) |
|
3375 { |
|
3376 str_list_type ret; |
|
3377 |
|
3378 STR_LIST_LENGTH (ret) = 0; |
|
3379 STR_LIST (ret) = NULL; |
|
3380 |
|
3381 return ret; |
|
3382 } |
|
3383 |
|
3384 void |
|
3385 str_list_add (str_list_type *l, char *s) |
|
3386 { |
|
3387 STR_LIST_LENGTH (*l)++; |
|
3388 XRETALLOC (STR_LIST (*l), STR_LIST_LENGTH (*l), char *); |
|
3389 STR_LIST_LAST_ELT (*l) = s; |
|
3390 } |
|
3391 |
|
3392 /* May as well save some reallocations and do everything in a chunk |
|
3393 instead of calling str_list_add on each element. */ |
|
3394 |
|
3395 void |
|
3396 str_list_concat (str_list_type *target, str_list_type more) |
|
3397 { |
|
3398 unsigned e; |
|
3399 unsigned prev_len = STR_LIST_LENGTH (*target); |
|
3400 |
|
3401 STR_LIST_LENGTH (*target) += STR_LIST_LENGTH (more); |
|
3402 XRETALLOC (STR_LIST (*target), STR_LIST_LENGTH (*target), char *); |
|
3403 |
|
3404 for (e = 0; e < STR_LIST_LENGTH (more); e++) |
|
3405 STR_LIST_ELT (*target, prev_len + e) = STR_LIST_ELT (more, e); |
|
3406 } |
|
3407 |
|
3408 /* Free the list (but not the elements within it). */ |
|
3409 |
|
3410 void |
|
3411 str_list_free (str_list_type *l) |
|
3412 { |
|
3413 if (STR_LIST (*l)) |
|
3414 { |
|
3415 free (STR_LIST (*l)); |
|
3416 STR_LIST (*l) = NULL; |
|
3417 } |
|
3418 } |
|
3419 |
|
3420 /* str-llist.c: Implementation of a linked list of strings. */ |
|
3421 |
|
3422 /* Add the new string STR to the end of the list L. */ |
|
3423 |
|
3424 void |
|
3425 str_llist_add (str_llist_type *l, char *str) |
|
3426 { |
|
3427 str_llist_elt_type *e; |
|
3428 str_llist_elt_type *new_elt = XTALLOC1 (str_llist_elt_type); |
|
3429 |
|
3430 /* The new element will be at the end of the list. */ |
|
3431 STR_LLIST (*new_elt) = str; |
|
3432 STR_LLIST_MOVED (*new_elt) = 0; |
|
3433 STR_LLIST_NEXT (*new_elt) = NULL; |
|
3434 |
|
3435 /* Find the current end of the list. */ |
|
3436 for (e = *l; e && STR_LLIST_NEXT (*e); e = STR_LLIST_NEXT (*e)) |
|
3437 ; |
|
3438 |
|
3439 if (!e) |
|
3440 *l = new_elt; |
|
3441 else |
|
3442 STR_LLIST_NEXT (*e) = new_elt; |
|
3443 } |
|
3444 |
|
3445 /* Move an element towards the top. The idea is that when a file is |
|
3446 found in a given directory, later files will likely be in that same |
|
3447 directory, and looking for the file in all the directories in between |
|
3448 is thus a waste. */ |
|
3449 |
|
3450 void |
|
3451 str_llist_float (str_llist_type *l, str_llist_elt_type *mover) |
|
3452 { |
|
3453 str_llist_elt_type *last_moved, *unmoved; |
|
3454 |
|
3455 /* If we've already moved this element, never mind. */ |
|
3456 if (STR_LLIST_MOVED (*mover)) |
|
3457 return; |
|
3458 |
|
3459 /* Find the first unmoved element (to insert before). We're |
|
3460 guaranteed this will terminate, since MOVER itself is currently |
|
3461 unmoved, and it must be in L (by hypothesis). */ |
|
3462 for (last_moved = NULL, unmoved = *l; STR_LLIST_MOVED (*unmoved); |
|
3463 last_moved = unmoved, unmoved = STR_LLIST_NEXT (*unmoved)) |
|
3464 ; |
|
3465 |
|
3466 /* If we are the first unmoved element, nothing to relink. */ |
|
3467 if (unmoved != mover) |
|
3468 { /* Remember `mover's current successor, so we can relink `mover's |
|
3469 predecessor to it. */ |
|
3470 str_llist_elt_type *before_mover; |
|
3471 str_llist_elt_type *after_mover = STR_LLIST_NEXT (*mover); |
|
3472 |
|
3473 /* Find `mover's predecessor. */ |
|
3474 for (before_mover = unmoved; STR_LLIST_NEXT (*before_mover) != mover; |
|
3475 before_mover = STR_LLIST_NEXT (*before_mover)) |
|
3476 ; |
|
3477 |
|
3478 /* `before_mover' now links to `after_mover'. */ |
|
3479 STR_LLIST_NEXT (*before_mover) = after_mover; |
|
3480 |
|
3481 /* Insert `mover' before `unmoved' and after `last_moved' (or at |
|
3482 the head of the list). */ |
|
3483 STR_LLIST_NEXT (*mover) = unmoved; |
|
3484 if (!last_moved) |
|
3485 *l = mover; |
|
3486 else |
|
3487 STR_LLIST_NEXT (*last_moved) = mover; |
|
3488 } |
|
3489 |
|
3490 /* We've moved it. */ |
|
3491 STR_LLIST_MOVED (*mover) = 1; |
|
3492 } |
|
3493 |
|
3494 /* fn.c: arbitrarily long filenames (or just strings). */ |
|
3495 |
|
3496 /* /usr/local/lib/texmf/fonts/public/cm/pk/ljfour/cmr10.300pk is 58 |
|
3497 chars, so ASCII `K' seems a good choice. */ |
|
3498 #define CHUNK_SIZE 75 |
|
3499 |
|
3500 fn_type |
|
3501 fn_init (void) |
|
3502 { |
|
3503 fn_type ret; |
|
3504 |
|
3505 FN_ALLOCATED (ret) = FN_LENGTH (ret) = 0; |
|
3506 FN_STRING (ret) = NULL; |
|
3507 |
|
3508 return ret; |
|
3509 } |
|
3510 |
|
3511 fn_type |
|
3512 fn_copy0 (const char *s, unsigned len) |
|
3513 { |
|
3514 fn_type ret; |
|
3515 |
|
3516 FN_ALLOCATED (ret) = CHUNK_SIZE > len ? CHUNK_SIZE : len + 1; |
|
3517 FN_STRING (ret) = (char *) xmalloc (FN_ALLOCATED (ret)); |
|
3518 |
|
3519 strncpy (FN_STRING (ret), s, len); |
|
3520 FN_STRING (ret)[len] = 0; |
|
3521 FN_LENGTH (ret) = len + 1; |
|
3522 |
|
3523 return ret; |
|
3524 } |
|
3525 |
|
3526 /* Don't think we ever try to free something that might usefully be |
|
3527 empty, so give fatal error if nothing allocated. */ |
|
3528 |
|
3529 void |
|
3530 fn_free (fn_type *f) |
|
3531 { |
|
3532 assert (FN_STRING (*f) != NULL); |
|
3533 free (FN_STRING (*f)); |
|
3534 FN_STRING (*f) = NULL; |
|
3535 FN_ALLOCATED (*f) = 0; |
|
3536 FN_LENGTH (*f) = 0; |
|
3537 } |
|
3538 |
|
3539 /* An arithmetic increase seems more reasonable than geometric. We |
|
3540 don't increase the length member since it may be more convenient for |
|
3541 the caller to add than subtract when appending the stuff that will |
|
3542 presumably follow. */ |
|
3543 |
|
3544 static void |
|
3545 grow (fn_type *f, unsigned len) |
|
3546 { |
|
3547 while (FN_LENGTH (*f) + len > FN_ALLOCATED (*f)) |
|
3548 { |
|
3549 FN_ALLOCATED (*f) += CHUNK_SIZE; |
|
3550 XRETALLOC (FN_STRING (*f), FN_ALLOCATED (*f), char); |
|
3551 } |
|
3552 } |
|
3553 |
|
3554 void |
|
3555 fn_1grow (fn_type *f, char c) |
|
3556 { |
|
3557 grow (f, 1); |
|
3558 FN_STRING (*f)[FN_LENGTH (*f)] = c; |
|
3559 FN_LENGTH (*f)++; |
|
3560 } |
|
3561 |
|
3562 void |
|
3563 fn_grow (fn_type *f, void *source, unsigned len) |
|
3564 { |
|
3565 grow (f, len); |
|
3566 strncpy (FN_STRING (*f) + FN_LENGTH (*f), (char *) source, len); |
|
3567 FN_LENGTH (*f) += len; |
|
3568 } |
|
3569 |
|
3570 void |
|
3571 fn_str_grow (fn_type *f, const char *s) |
|
3572 { |
|
3573 unsigned more_len = strlen (s); |
|
3574 grow (f, more_len); |
|
3575 strcat (FN_STRING (*f), s); |
|
3576 FN_LENGTH (*f) += more_len; |
|
3577 } |
|
3578 |
|
3579 void |
|
3580 fn_shrink_to (fn_type *f, unsigned loc) |
|
3581 { |
|
3582 assert (FN_LENGTH (*f) > loc); |
|
3583 FN_STRING (*f)[loc] = 0; |
|
3584 FN_LENGTH (*f) = loc + 1; |
|
3585 } |
|
3586 |
|
3587 /* variable.c: variable expansion. */ |
|
3588 |
|
3589 /* Here's the simple one, when a program just wants a value. */ |
|
3590 |
|
3591 char * |
|
3592 kpse_var_value (const char *var) |
|
3593 { |
|
3594 char *ret = getenv (var); |
|
3595 |
|
3596 if (ret) |
|
3597 ret = kpse_var_expand (ret); |
|
3598 |
|
3599 #ifdef KPSE_DEBUG |
|
3600 if (KPSE_DEBUG_P (KPSE_DEBUG_VARS)) |
|
3601 DEBUGF2("variable: %s = %s\n", var, ret ? ret : "(nil)"); |
|
3602 #endif |
|
3603 |
|
3604 return ret; |
|
3605 } |
|
3606 |
|
3607 /* We have to keep track of variables being expanded, otherwise |
|
3608 constructs like TEXINPUTS = $TEXINPUTS result in an infinite loop. |
|
3609 (Or indirectly recursive variables, etc.) Our simple solution is to |
|
3610 add to a list each time an expansion is started, and check the list |
|
3611 before expanding. */ |
|
3612 |
|
3613 typedef struct { |
|
3614 const char *var; |
|
3615 int expanding; |
|
3616 } expansion_type; |
|
3617 static expansion_type *expansions; /* The sole variable of this type. */ |
|
3618 static unsigned expansion_len = 0; |
|
3619 |
|
3620 static void |
|
3621 expanding (const char *var, int xp) |
|
3622 { |
|
3623 unsigned e; |
|
3624 for (e = 0; e < expansion_len; e++) { |
|
3625 if (STREQ (expansions[e].var, var)) { |
|
3626 expansions[e].expanding = xp; |
|
3627 return; |
|
3628 } |
|
3629 } |
|
3630 |
|
3631 /* New variable, add it to the list. */ |
|
3632 expansion_len++; |
|
3633 XRETALLOC (expansions, expansion_len, expansion_type); |
|
3634 expansions[expansion_len - 1].var = xstrdup (var); |
|
3635 expansions[expansion_len - 1].expanding = xp; |
|
3636 } |
|
3637 |
|
3638 |
|
3639 /* Return whether VAR is currently being expanding. */ |
|
3640 |
|
3641 static int |
|
3642 expanding_p (const char *var) |
|
3643 { |
|
3644 unsigned e; |
|
3645 for (e = 0; e < expansion_len; e++) { |
|
3646 if (STREQ (expansions[e].var, var)) |
|
3647 return expansions[e].expanding; |
|
3648 } |
|
3649 |
|
3650 return 0; |
|
3651 } |
|
3652 |
|
3653 /* Append the result of value of `var' to EXPANSION, where `var' begins |
|
3654 at START and ends at END. If `var' is not set, do not complain. |
|
3655 This is a subroutine for the more complicated expansion function. */ |
|
3656 |
|
3657 static void |
|
3658 expand (fn_type *expansion, const char *start, const char *end) |
|
3659 { |
|
3660 char *value; |
|
3661 unsigned len = end - start + 1; |
|
3662 char *var = (char *) xmalloc (len + 1); |
|
3663 strncpy (var, start, len); |
|
3664 var[len] = 0; |
|
3665 |
|
3666 if (expanding_p (var)) { |
|
3667 WARNING1 ("kpathsea: variable `%s' references itself (eventually)", var); |
|
3668 } else { |
|
3669 /* Check for an environment variable. */ |
|
3670 value = getenv (var); |
|
3671 |
|
3672 if (value) { |
|
3673 expanding (var, 1); |
|
3674 value = kpse_var_expand (value); |
|
3675 expanding (var, 0); |
|
3676 fn_grow (expansion, value, strlen (value)); |
|
3677 free (value); |
|
3678 } |
|
3679 |
|
3680 free (var); |
|
3681 } |
|
3682 } |
|
3683 |
|
3684 /* Can't think of when it would be useful to change these (and the |
|
3685 diagnostic messages assume them), but ... */ |
|
3686 #ifndef IS_VAR_START /* starts all variable references */ |
|
3687 #define IS_VAR_START(c) ((c) == '$') |
|
3688 #endif |
|
3689 #ifndef IS_VAR_CHAR /* variable name constituent */ |
|
3690 #define IS_VAR_CHAR(c) (isalnum (c) || (c) == '_') |
|
3691 #endif |
|
3692 #ifndef IS_VAR_BEGIN_DELIMITER /* start delimited variable name (after $) */ |
|
3693 #define IS_VAR_BEGIN_DELIMITER(c) ((c) == '{') |
|
3694 #endif |
|
3695 #ifndef IS_VAR_END_DELIMITER |
|
3696 #define IS_VAR_END_DELIMITER(c) ((c) == '}') |
|
3697 #endif |
|
3698 |
|
3699 |
|
3700 /* Maybe we should support some or all of the various shell ${...} |
|
3701 constructs, especially ${var-value}. */ |
|
3702 |
|
3703 char * |
|
3704 kpse_var_expand (const char *src) |
|
3705 { |
|
3706 const char *s; |
|
3707 char *ret; |
|
3708 fn_type expansion; |
|
3709 expansion = fn_init (); |
|
3710 |
|
3711 /* Copy everything but variable constructs. */ |
|
3712 for (s = src; *s; s++) { |
|
3713 if (IS_VAR_START (*s)) { |
|
3714 s++; |
|
3715 |
|
3716 /* Three cases: `$VAR', `${VAR}', `$<anything-else>'. */ |
|
3717 if (IS_VAR_CHAR (*s)) { |
|
3718 /* $V: collect name constituents, then expand. */ |
|
3719 const char *var_end = s; |
|
3720 |
|
3721 do { |
|
3722 var_end++; |
|
3723 } while (IS_VAR_CHAR (*var_end)); |
|
3724 |
|
3725 var_end--; /* had to go one past */ |
|
3726 expand (&expansion, s, var_end); |
|
3727 s = var_end; |
|
3728 |
|
3729 } else if (IS_VAR_BEGIN_DELIMITER (*s)) { |
|
3730 /* ${: scan ahead for matching delimiter, then expand. */ |
|
3731 const char *var_end = ++s; |
|
3732 |
|
3733 while (*var_end && !IS_VAR_END_DELIMITER (*var_end)) |
|
3734 var_end++; |
|
3735 |
|
3736 if (! *var_end) { |
|
3737 WARNING1 ("%s: No matching } for ${", src); |
|
3738 s = var_end - 1; /* will incr to null at top of loop */ |
|
3739 } else { |
|
3740 expand (&expansion, s, var_end - 1); |
|
3741 s = var_end; /* will incr past } at top of loop*/ |
|
3742 } |
|
3743 |
|
3744 } else { |
|
3745 /* $<something-else>: error. */ |
|
3746 WARNING2 ("%s: Unrecognized variable construct `$%c'", src, *s); |
|
3747 /* Just ignore those chars and keep going. */ |
|
3748 } |
|
3749 } else |
|
3750 fn_1grow (&expansion, *s); |
|
3751 } |
|
3752 fn_1grow (&expansion, 0); |
|
3753 |
|
3754 ret = FN_STRING (expansion); |
|
3755 return ret; |
|
3756 } |