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