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