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