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