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