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 */ |
4397
|
1219 static string_vector brace_expand (const std::string&); |
4378
|
1220 |
|
1221 /* If $KPSE_DOT is defined in the environment, prepend it to any relative |
|
1222 path components. */ |
|
1223 |
4389
|
1224 static std::string |
|
1225 kpse_expand_kpse_dot (const std::string& path) |
4378
|
1226 { |
4389
|
1227 std::string ret; |
4391
|
1228 std::string kpse_dot = octave_env::getenv ("KPSE_DOT"); |
|
1229 |
|
1230 if (kpse_dot.empty ()) |
4378
|
1231 return path; |
|
1232 |
4394
|
1233 for (kpse_path_iterator pi (path); pi != NPOS; pi++) |
4391
|
1234 { |
4394
|
1235 std::string elt = *pi; |
|
1236 |
4391
|
1237 /* We assume that the !! magic is only used on absolute components. |
|
1238 Single "." get special treatment, as does "./" or its equivalent. */ |
|
1239 |
4394
|
1240 size_t elt_len = elt.length (); |
|
1241 |
|
1242 if (kpse_absolute_p (elt, false) |
|
1243 || (elt_len > 1 && elt[0] == '!' && elt[1] == '!')) |
|
1244 ret += elt + ENV_SEP_STRING; |
|
1245 else if (elt_len == 1 && elt[0] == '.') |
|
1246 ret += kpse_dot + ENV_SEP_STRING; |
|
1247 else if (elt_len > 1 && elt[0] == '.' && IS_DIR_SEP (elt[1])) |
|
1248 ret += kpse_dot + elt.substr (1) + ENV_SEP_STRING; |
4391
|
1249 else |
4394
|
1250 ret += kpse_dot + DIR_SEP_STRING + elt + ENV_SEP_STRING; |
4378
|
1251 } |
|
1252 |
4389
|
1253 int len = ret.length (); |
|
1254 if (len > 0) |
4395
|
1255 ret.resize (len-1); |
4389
|
1256 |
4378
|
1257 return ret; |
|
1258 } |
|
1259 |
|
1260 /* Do brace expansion on ELT; then do variable and ~ expansion on each |
|
1261 element of the result; then do brace expansion again, in case a |
|
1262 variable definition contained braces (e.g., $TEXMF). Return a |
|
1263 string comprising all of the results separated by ENV_SEP_STRING. */ |
|
1264 |
4389
|
1265 static std::string |
4394
|
1266 kpse_brace_expand_element (const std::string& elt) |
4378
|
1267 { |
4389
|
1268 std::string ret; |
4378
|
1269 |
4397
|
1270 string_vector expansions = brace_expand (elt); |
|
1271 |
|
1272 for (int i = 0; i < expansions.length (); i++) |
4391
|
1273 { |
|
1274 /* Do $ and ~ expansion on each element. */ |
|
1275 std::string x = kpse_expand (expansions[i]); |
|
1276 |
|
1277 if (x != expansions[i]) |
|
1278 { |
|
1279 /* If we did any expansions, do brace expansion again. Since |
|
1280 recursive variable definitions are not allowed, this recursion |
|
1281 must terminate. (In practice, it's unlikely there will ever be |
|
1282 more than one level of recursion.) */ |
4394
|
1283 x = kpse_brace_expand_element (x); |
4391
|
1284 } |
|
1285 |
|
1286 ret += x + ENV_SEP_STRING; |
4378
|
1287 } |
|
1288 |
4389
|
1289 ret.resize (ret.length () - 1); |
4391
|
1290 |
4378
|
1291 return ret; |
|
1292 } |
|
1293 |
|
1294 /* Be careful to not waste all the memory we allocate for each element. */ |
|
1295 |
4389
|
1296 std::string |
4397
|
1297 kpse_brace_expand (const std::string& path) |
4378
|
1298 { |
|
1299 /* Must do variable expansion first because if we have |
|
1300 foo = .:~ |
|
1301 TEXINPUTS = $foo |
|
1302 we want to end up with TEXINPUTS = .:/home/karl. |
|
1303 Since kpse_path_element is not reentrant, we must get all |
|
1304 the path elements before we start the loop. */ |
4389
|
1305 std::string tmp = kpse_var_expand (path); |
4394
|
1306 |
4389
|
1307 std::string ret; |
4378
|
1308 |
4394
|
1309 for (kpse_path_iterator pi (tmp); pi != NPOS; pi++) |
4391
|
1310 { |
4394
|
1311 std::string elt = *pi; |
|
1312 |
4391
|
1313 /* Do brace expansion first, so tilde expansion happens in {~ka,~kb}. */ |
|
1314 std::string expansion = kpse_brace_expand_element (elt); |
|
1315 ret += expansion + ENV_SEP_STRING; |
|
1316 } |
4378
|
1317 |
4394
|
1318 size_t len = ret.length (); |
4389
|
1319 if (len > 0) |
4395
|
1320 ret.resize (len-1); |
4389
|
1321 |
|
1322 return kpse_expand_kpse_dot (ret); |
4378
|
1323 } |
4392
|
1324 |
4378
|
1325 /* Expand all special constructs in a path, and include only the actually |
|
1326 existing directories in the result. */ |
4392
|
1327 std::string |
4397
|
1328 kpse_path_expand (const std::string& path) |
4378
|
1329 { |
4392
|
1330 std::string ret; |
4378
|
1331 unsigned len; |
|
1332 |
|
1333 len = 0; |
4391
|
1334 |
4378
|
1335 /* Expand variables and braces first. */ |
4389
|
1336 std::string tmp = kpse_brace_expand (path); |
4392
|
1337 |
4378
|
1338 /* Now expand each of the path elements, printing the results */ |
4394
|
1339 for (kpse_path_iterator pi (tmp); pi != NPOS; pi++) |
4391
|
1340 { |
4394
|
1341 std::string elt = *pi; |
|
1342 |
4391
|
1343 str_llist_type *dirs; |
|
1344 |
|
1345 /* Skip and ignore magic leading chars. */ |
4394
|
1346 if (elt.length () > 1 && elt[0] == '!' && elt[1] == '!') |
|
1347 elt = elt.substr (2); |
4391
|
1348 |
|
1349 /* Do not touch the device if present */ |
|
1350 if (NAME_BEGINS_WITH_DEVICE (elt)) |
|
1351 { |
4394
|
1352 while (elt.length () > 3 |
|
1353 && IS_DIR_SEP (elt[2]) && IS_DIR_SEP (elt[3])) |
4391
|
1354 { |
4394
|
1355 elt[2] = elt[1]; |
|
1356 elt[1] = elt[0]; |
|
1357 elt = elt.substr (1); |
4391
|
1358 } |
|
1359 } |
|
1360 else |
|
1361 { |
|
1362 /* We never want to search the whole disk. */ |
4394
|
1363 while (elt.length () > 1 |
|
1364 && IS_DIR_SEP (elt[0]) && IS_DIR_SEP (elt[1])) |
|
1365 elt = elt.substr (1); |
4391
|
1366 } |
4378
|
1367 |
4394
|
1368 /* Search the disk for all dirs in the component specified. |
|
1369 Be faster to check the database, but this is more reliable. */ |
|
1370 dirs = kpse_element_dirs (elt); |
|
1371 |
|
1372 if (dirs && *dirs) |
|
1373 { |
|
1374 str_llist_elt_type *dir; |
|
1375 |
|
1376 for (dir = *dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
|
1377 { |
|
1378 const std::string thedir = STR_LLIST (*dir); |
|
1379 unsigned dirlen = thedir.length (); |
|
1380 |
4395
|
1381 ret += thedir; |
|
1382 len += dirlen; |
|
1383 |
4394
|
1384 /* Retain trailing slash if that's the root directory. */ |
|
1385 if (dirlen == 1 |
|
1386 || (dirlen == 3 && NAME_BEGINS_WITH_DEVICE (thedir) |
|
1387 && IS_DIR_SEP (thedir[2]))) |
|
1388 { |
4395
|
1389 ret += ENV_SEP_STRING; |
|
1390 len++; |
4394
|
1391 } |
4395
|
1392 |
|
1393 ret[len-1] = ENV_SEP; |
4394
|
1394 } |
|
1395 } |
4378
|
1396 } |
4391
|
1397 |
4395
|
1398 if (len > 0) |
|
1399 ret.resize (len-1); |
4391
|
1400 |
4378
|
1401 return ret; |
|
1402 } |
4392
|
1403 |
4378
|
1404 /* braces.c -- code for doing word expansion in curly braces. Taken from |
|
1405 bash 1.14.5. [Ans subsequently modified for kpatshea.] |
|
1406 |
|
1407 Copyright (C) 1987,1991 Free Software Foundation, Inc. |
|
1408 |
|
1409 This program is free software; you can redistribute it and/or modify it |
|
1410 under the terms of the GNU General Public License as published by |
|
1411 the Free Software Foundation; either version 1, or (at your option) |
|
1412 any later version. |
|
1413 |
|
1414 This program is distributed in the hope that it will be useful, but |
|
1415 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
1416 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
1417 General Public License for more details. |
|
1418 |
|
1419 You should have received a copy of the GNU General Public License |
|
1420 along with this program; see the file COPYING. If not, write to the |
|
1421 Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
|
1422 MA 02111-1307, USA. */ |
|
1423 |
4391
|
1424 #define brace_whitespace(c) (! (c) || (c) == ' ' || (c) == '\t' || (c) == '\n') |
4378
|
1425 |
|
1426 /* Basic idea: |
|
1427 |
|
1428 Segregate the text into 3 sections: preamble (stuff before an open brace), |
|
1429 postamble (stuff after the matching close brace) and amble (stuff after |
|
1430 preamble, and before postamble). Expand amble, and then tack on the |
|
1431 expansions to preamble. Expand postamble, and tack on the expansions to |
4391
|
1432 the result so far. */ |
4378
|
1433 |
4397
|
1434 /* Return a new array of strings which is the result of appending each |
|
1435 string in ARR2 to each string in ARR1. The resultant array is |
|
1436 len (arr1) * len (arr2) long. For convenience, ARR1 (and its contents) |
|
1437 are free ()'ed. ARR1 can be NULL, in that case, a new version of ARR2 |
|
1438 is returned. */ |
|
1439 |
|
1440 static string_vector |
|
1441 array_concat (const string_vector& arr1, const string_vector& arr2) |
4378
|
1442 { |
4397
|
1443 string_vector result; |
|
1444 |
|
1445 if (arr1.empty ()) |
|
1446 result = arr2; |
|
1447 else if (arr2.empty ()) |
|
1448 result = arr1; |
|
1449 else |
|
1450 { |
|
1451 int len1 = arr1.length (); |
|
1452 int len2 = arr2.length (); |
|
1453 |
|
1454 result = string_vector (len1 * len2); |
|
1455 |
|
1456 int k = 0; |
|
1457 for (int i = 0; i < len2; i++) |
|
1458 for (int j = 0; j < len1; j++) |
|
1459 result[k++] = arr1[j] + arr2[i]; |
|
1460 } |
|
1461 |
|
1462 return result; |
4378
|
1463 } |
|
1464 |
4397
|
1465 static int brace_gobbler (const std::string&, int&, int); |
|
1466 static string_vector expand_amble (const std::string&); |
4378
|
1467 |
|
1468 /* Return an array of strings; the brace expansion of TEXT. */ |
4397
|
1469 static string_vector |
|
1470 brace_expand (const std::string& text) |
4378
|
1471 { |
|
1472 /* Find the text of the preamble. */ |
4397
|
1473 int i = 0; |
|
1474 int c = brace_gobbler (text, i, '{'); |
|
1475 |
|
1476 std::string preamble = text.substr (0, i); |
|
1477 |
|
1478 string_vector result = string_vector (preamble); |
|
1479 |
|
1480 if (c == '{') |
4378
|
1481 { |
4397
|
1482 /* Find the amble. This is the stuff inside this set of braces. */ |
|
1483 int start = ++i; |
|
1484 c = brace_gobbler (text, i, '}'); |
|
1485 |
|
1486 /* What if there isn't a matching close brace? */ |
|
1487 if (! c) |
|
1488 { |
|
1489 (*current_liboctave_warning_handler) |
|
1490 ("%s: Unmatched {", text.c_str ()); |
|
1491 |
|
1492 result = string_vector (text); |
|
1493 } |
|
1494 else |
|
1495 { |
|
1496 std::string amble = text.substr (start, i-start); |
|
1497 result = array_concat (result, expand_amble (amble)); |
|
1498 |
|
1499 std::string postamble = text.substr (i+1); |
|
1500 result = array_concat (result, brace_expand (postamble)); |
|
1501 } |
4378
|
1502 } |
|
1503 |
4397
|
1504 return result; |
4378
|
1505 } |
|
1506 |
4397
|
1507 /* The character which is used to separate arguments. */ |
|
1508 static int brace_arg_separator = ','; |
|
1509 |
4378
|
1510 /* Expand the text found inside of braces. We simply try to split the |
|
1511 text at BRACE_ARG_SEPARATORs into separate strings. We then brace |
|
1512 expand each slot which needs it, until there are no more slots which |
|
1513 need it. */ |
4397
|
1514 static string_vector |
|
1515 expand_amble (const std::string& text) |
4378
|
1516 { |
4397
|
1517 string_vector result; |
|
1518 |
|
1519 size_t text_len = text.length (); |
|
1520 size_t start; |
|
1521 int i, c; |
|
1522 |
|
1523 for (start = 0, i = 0, c = 1; c && start < text_len; start = ++i) |
4378
|
1524 { |
4397
|
1525 int i0 = i; |
|
1526 int c0 = brace_gobbler (text, i0, brace_arg_separator); |
|
1527 int i1 = i; |
|
1528 int c1 = brace_gobbler (text, i1, ENV_SEP); |
4378
|
1529 c = c0 | c1; |
|
1530 i = (i0 < i1 ? i0 : i1); |
|
1531 |
4397
|
1532 std::string tem = text.substr (start, i-start); |
|
1533 |
|
1534 string_vector partial = brace_expand (tem); |
|
1535 |
|
1536 if (result.empty ()) |
4378
|
1537 result = partial; |
|
1538 else |
4397
|
1539 result.append (partial); |
4378
|
1540 } |
4397
|
1541 |
|
1542 return result; |
4378
|
1543 } |
|
1544 |
|
1545 /* Start at INDEX, and skip characters in TEXT. Set INDEX to the |
|
1546 index of the character matching SATISFY. This understands about |
|
1547 quoting. Return the character that caused us to stop searching; |
|
1548 this is either the same as SATISFY, or 0. */ |
|
1549 static int |
4397
|
1550 brace_gobbler (const std::string& text, int& indx, int satisfy) |
4378
|
1551 { |
4397
|
1552 int c = 0, level = 0, quoted = 0, pass_next = 0; |
|
1553 |
|
1554 size_t text_len = text.length (); |
|
1555 |
|
1556 size_t i = indx; |
|
1557 |
|
1558 for (; i < text_len; i++) |
4378
|
1559 { |
4397
|
1560 c = text[i]; |
|
1561 |
4378
|
1562 if (pass_next) |
|
1563 { |
|
1564 pass_next = 0; |
|
1565 continue; |
|
1566 } |
|
1567 |
|
1568 /* A backslash escapes the next character. This allows backslash to |
|
1569 escape the quote character in a double-quoted string. */ |
|
1570 if (c == '\\' && (quoted == 0 || quoted == '"' || quoted == '`')) |
|
1571 { |
|
1572 pass_next = 1; |
|
1573 continue; |
|
1574 } |
|
1575 |
|
1576 if (quoted) |
|
1577 { |
|
1578 if (c == quoted) |
|
1579 quoted = 0; |
|
1580 continue; |
|
1581 } |
|
1582 |
|
1583 if (c == '"' || c == '\'' || c == '`') |
|
1584 { |
|
1585 quoted = c; |
|
1586 continue; |
|
1587 } |
4391
|
1588 |
4378
|
1589 if (c == satisfy && !level && !quoted) |
|
1590 { |
|
1591 /* We ignore an open brace surrounded by whitespace, and also |
|
1592 an open brace followed immediately by a close brace, that |
|
1593 was preceded with whitespace. */ |
|
1594 if (c == '{' && |
4397
|
1595 ((i == 0 || brace_whitespace (text[i-1])) && |
|
1596 (i+1 < text_len && |
|
1597 (brace_whitespace (text[i+1]) || text[i+1] == '}')))) |
4378
|
1598 continue; |
|
1599 /* If this is being compiled as part of bash, ignore the `{' |
|
1600 in a `${}' construct */ |
4397
|
1601 if ((c != '{') || i == 0 || (text[i-1] != '$')) |
4378
|
1602 break; |
|
1603 } |
|
1604 |
|
1605 if (c == '{') |
|
1606 level++; |
|
1607 else if (c == '}' && level) |
|
1608 level--; |
|
1609 } |
|
1610 |
4397
|
1611 indx = i; |
|
1612 return c; |
4378
|
1613 } |
|
1614 |
|
1615 /* db.c: an external database to avoid filesystem lookups. */ |
|
1616 |
|
1617 #ifndef DEFAULT_TEXMFDBS |
|
1618 #define DEFAULT_TEXMFDBS "/usr/local/share/texmf:/var/tmp/texfonts" |
|
1619 #endif |
|
1620 |
|
1621 /* For each file format, we record the following information. The main |
|
1622 thing that is not part of this structure is the environment variable |
|
1623 lists. They are used directly in tex-file.c. We could incorporate |
|
1624 them here, but it would complicate the code a bit. We could also do |
|
1625 it via variable expansion, but not now, maybe not ever: |
|
1626 ${PKFONTS-${TEXFONTS-/usr/local/lib/texmf/fonts//}}. */ |
|
1627 |
|
1628 typedef struct |
|
1629 { |
4390
|
1630 std::string type; /* Human-readable description. */ |
|
1631 std::string path; /* The search path to use. */ |
|
1632 std::string raw_path; /* Pre-$~ (but post-default) expansion. */ |
|
1633 std::string path_source; /* Where the path started from. */ |
|
1634 std::string override_path; /* From client environment variable. */ |
|
1635 std::string client_path; /* E.g., from dvips's config.ps. */ |
|
1636 std::string cnf_path; /* From texmf.cnf. */ |
|
1637 std::string default_path; /* If all else fails. */ |
4394
|
1638 string_vector suffix; /* For kpse_find_file to check for/append. */ |
4378
|
1639 } kpse_format_info_type; |
|
1640 |
|
1641 /* The sole variable of that type, indexed by `kpse_file_format_type'. |
|
1642 Initialized by calls to `kpse_find_file' for `kpse_init_format'. */ |
|
1643 static kpse_format_info_type kpse_format_info; |
|
1644 |
|
1645 #define DB_ENVS "TEXMFDBS" |
4392
|
1646 |
4378
|
1647 /* And EXPAND_DEFAULT calls kpse_expand_default on try_path and the |
|
1648 present info->path. */ |
4396
|
1649 #define EXPAND_DEFAULT(try_path, source_string) \ |
|
1650 do \ |
|
1651 { \ |
|
1652 if (! try_path.empty ()) \ |
|
1653 { \ |
|
1654 info.raw_path = try_path; \ |
|
1655 info.path = kpse_expand_default (try_path, info.path); \ |
|
1656 info.path_source = source_string; \ |
|
1657 } \ |
|
1658 } \ |
|
1659 while (0) |
4378
|
1660 |
|
1661 /* Find the final search path to use for the format entry INFO, given |
|
1662 the compile-time default (DEFAULT_PATH), and the environment |
|
1663 variables to check (the remaining arguments, terminated with NULL). |
|
1664 We set the `path' and `path_source' members of INFO. The |
|
1665 `client_path' member must already be set upon entry. */ |
|
1666 |
|
1667 static void |
4396
|
1668 init_path (kpse_format_info_type& info, const char *default_path, ...) |
4378
|
1669 { |
|
1670 va_list ap; |
|
1671 |
|
1672 va_start (ap, default_path); |
|
1673 |
4396
|
1674 info.default_path = default_path; |
4378
|
1675 |
|
1676 /* First envvar that's set to a nonempty value will exit the loop. If |
|
1677 none are set, we want the first cnf entry that matches. Find the |
|
1678 cnf entries simultaneously, to avoid having to go through envvar |
|
1679 list twice -- because of the PVAR?C macro, that would mean having |
|
1680 to create a str_list and then use it twice. Yuck. */ |
4391
|
1681 |
4396
|
1682 char *env_name; |
|
1683 |
|
1684 std::string var; |
|
1685 |
4391
|
1686 while ((env_name = va_arg (ap, char *)) != NULL) |
|
1687 { |
|
1688 /* Since sh doesn't like envvar names with `.', check PATH_prog |
|
1689 rather than PATH.prog. */ |
|
1690 |
4396
|
1691 if (var.empty ()) |
4391
|
1692 { |
|
1693 /* Try simply PATH. */ |
|
1694 std::string env_value = octave_env::getenv (env_name); |
|
1695 |
|
1696 if (! env_value.empty ()) |
|
1697 var = env_name; |
|
1698 } |
|
1699 |
4396
|
1700 if (! var.empty () && ! info.cnf_path.empty ()) |
4391
|
1701 break; |
4378
|
1702 } |
4391
|
1703 |
4378
|
1704 va_end (ap); |
4391
|
1705 |
4378
|
1706 /* Expand any extra :'s. For each level, we replace an extra : with |
|
1707 the path at the next lower level. For example, an extra : in a |
|
1708 user-set envvar should be replaced with the path from the cnf file. |
|
1709 things are complicated because none of the levels above the very |
|
1710 bottom are guaranteed to exist. */ |
|
1711 |
|
1712 /* Assume we can reliably start with the compile-time default. */ |
4396
|
1713 info.path = info.raw_path = info.default_path; |
|
1714 info.path_source = "compile-time paths.h"; |
|
1715 |
|
1716 EXPAND_DEFAULT (info.cnf_path, "texmf.cnf"); |
|
1717 EXPAND_DEFAULT (info.client_path, "program config file"); |
|
1718 |
|
1719 if (! var.empty ()) |
4390
|
1720 { |
4391
|
1721 std::string val = octave_env::getenv (var); |
4396
|
1722 EXPAND_DEFAULT (val, var + " environment variable"); |
4390
|
1723 } |
4391
|
1724 |
4396
|
1725 EXPAND_DEFAULT (info.override_path, "application override variable"); |
|
1726 std::string tmp = kpse_brace_expand (info.path); |
|
1727 info.path = tmp; |
4378
|
1728 } |
|
1729 |
4393
|
1730 static std::string |
|
1731 remove_dbonly (const std::string& path) |
4378
|
1732 { |
4393
|
1733 std::string ret = path; |
|
1734 size_t path_len = path.length (); |
|
1735 |
|
1736 size_t i = 0, j = 0; |
|
1737 |
|
1738 bool new_elt = true; |
|
1739 |
|
1740 while (i < path_len) |
4391
|
1741 { |
4393
|
1742 if (new_elt && i + 1 < path_len && path[i] == '!' && path[i+1] == '!') |
|
1743 i += 2; |
4391
|
1744 else |
|
1745 { |
4393
|
1746 new_elt = (path[i] == ENV_SEP); |
|
1747 ret[j++] = path[i++]; |
4391
|
1748 } |
4378
|
1749 } |
4391
|
1750 |
4393
|
1751 ret.resize (j); |
4391
|
1752 |
|
1753 return ret; |
4378
|
1754 } |
|
1755 |
|
1756 /* Initialize everything for FORMAT. */ |
|
1757 |
4390
|
1758 static std::string |
4378
|
1759 kpse_init_format (void) |
|
1760 { |
|
1761 /* If we get called twice, don't redo all the work. */ |
4390
|
1762 if (! kpse_format_info.path.empty ()) |
4378
|
1763 return kpse_format_info.path; |
4391
|
1764 |
4378
|
1765 kpse_format_info.type = "ls-R"; |
4396
|
1766 init_path (kpse_format_info, DEFAULT_TEXMFDBS, DB_ENVS, NULL); |
4394
|
1767 kpse_format_info.suffix.append (std::string ("ls-R")); |
4393
|
1768 kpse_format_info.path = remove_dbonly (kpse_format_info.path); |
4378
|
1769 |
|
1770 #ifdef KPSE_DEBUG |
4393
|
1771 #define MAYBE(member) \ |
|
1772 (kpse_format_info.member.empty () \ |
|
1773 ? "(none)" : kpse_format_info.member.c_str ()) |
4378
|
1774 |
|
1775 /* Describe the monster we've created. */ |
|
1776 if (KPSE_DEBUG_P (KPSE_DEBUG_PATHS)) |
|
1777 { |
|
1778 DEBUGF2 ("Search path for %s files (from %s)\n", |
4390
|
1779 kpse_format_info.type.c_str (), |
|
1780 kpse_format_info.path_source.c_str ()); |
4391
|
1781 |
4390
|
1782 DEBUGF1 (" = %s\n", kpse_format_info.path.c_str ()); |
4391
|
1783 |
|
1784 DEBUGF1 (" before expansion = %s\n", |
|
1785 kpse_format_info.raw_path.c_str ()); |
|
1786 |
4378
|
1787 DEBUGF1 (" application override path = %s\n", MAYBE (override_path)); |
4391
|
1788 |
4378
|
1789 DEBUGF1 (" application config file path = %s\n", MAYBE (client_path)); |
4391
|
1790 |
4378
|
1791 DEBUGF1 (" texmf.cnf path = %s\n", MAYBE (cnf_path)); |
4391
|
1792 |
4378
|
1793 DEBUGF1 (" compile-time path = %s\n", MAYBE (default_path)); |
4391
|
1794 |
4378
|
1795 DEBUGF (" default suffixes ="); |
4391
|
1796 |
4394
|
1797 if (! kpse_format_info.suffix.empty ()) |
4391
|
1798 { |
4394
|
1799 string_vector tmp = kpse_format_info.suffix; |
|
1800 int len = tmp.length (); |
|
1801 for (int i = 0; i < len; i++) |
4391
|
1802 { |
4394
|
1803 fprintf (stderr, " %s", tmp[i].c_str ()); |
4391
|
1804 } |
|
1805 putc ('\n', stderr); |
|
1806 } |
|
1807 else |
|
1808 { |
|
1809 fputs (" (none)\n", stderr); |
|
1810 } |
4378
|
1811 } |
|
1812 #endif /* KPSE_DEBUG */ |
|
1813 |
|
1814 return kpse_format_info.path; |
|
1815 } |
4392
|
1816 |
4378
|
1817 static hash_table_type db; /* The hash table for all the ls-R's. */ |
|
1818 /* SMALL: The old size of the hash table was 7603, with the assumption |
|
1819 that a minimal ls-R bas about 3500 entries. But a typical ls-R will |
|
1820 be more like double that size. */ |
|
1821 #ifndef DB_HASH_SIZE |
|
1822 #define DB_HASH_SIZE 15991 |
|
1823 #endif |
|
1824 #ifndef DB_NAME |
|
1825 #define DB_NAME "ls-R" |
|
1826 #endif |
|
1827 |
|
1828 static hash_table_type alias_db; |
|
1829 #ifndef ALIAS_NAME |
|
1830 #define ALIAS_NAME "aliases" |
|
1831 #endif |
|
1832 #ifndef ALIAS_HASH_SIZE |
|
1833 #define ALIAS_HASH_SIZE 1009 |
|
1834 #endif |
|
1835 |
4390
|
1836 static string_vector db_dir_list; |
4392
|
1837 |
4378
|
1838 /* If DIRNAME contains any element beginning with a `.' (that is more |
|
1839 than just `./'), return true. This is to allow ``hidden'' |
|
1840 directories -- ones that don't get searched. */ |
|
1841 |
|
1842 static bool |
4393
|
1843 ignore_dir_p (const std::string& dirname_arg) |
4378
|
1844 { |
4393
|
1845 const char *dirname = dirname_arg.c_str (); |
|
1846 |
4378
|
1847 const char *dot_pos = dirname; |
4391
|
1848 |
|
1849 while ((dot_pos = strchr (dot_pos + 1, '.'))) |
|
1850 { |
|
1851 /* If / before and no / after, skip it. */ |
|
1852 if (IS_DIR_SEP (dot_pos[-1]) && dot_pos[1] && !IS_DIR_SEP (dot_pos[1])) |
|
1853 return true; |
|
1854 } |
|
1855 |
4378
|
1856 return false; |
|
1857 } |
4392
|
1858 |
4393
|
1859 static bool |
|
1860 read_line (FILE *f, std::string& line) |
4378
|
1861 { |
4393
|
1862 bool read_something = false; |
|
1863 |
4378
|
1864 int c; |
4393
|
1865 |
|
1866 OSSTREAM buf; |
|
1867 |
|
1868 while ((c = getc (f)) != EOF) |
4378
|
1869 { |
4393
|
1870 read_something = true; |
|
1871 |
|
1872 if (c == '\n' || c == '\r') |
|
1873 break; |
|
1874 |
|
1875 buf << static_cast<char> (c); |
4378
|
1876 } |
4391
|
1877 |
4378
|
1878 /* If we read anything, return it. This can't represent a last |
|
1879 ``line'' which doesn't end in a newline, but so what. */ |
4393
|
1880 if (read_something) |
4378
|
1881 { |
|
1882 /* Absorb LF of a CRLF pair. */ |
4391
|
1883 if (c == '\r') |
|
1884 { |
4378
|
1885 c = getc (f); |
|
1886 if (c != '\n') |
4391
|
1887 ungetc (c, f); |
|
1888 } |
4393
|
1889 |
|
1890 buf << OSSTREAM_ENDS; |
|
1891 line = OSSTREAM_STR (buf); |
|
1892 OSSTREAM_FREEZE (buf); |
4378
|
1893 } |
4393
|
1894 |
|
1895 return read_something; |
4378
|
1896 } |
|
1897 |
|
1898 /* If no DB_FILENAME, return false (maybe they aren't using this feature). |
|
1899 Otherwise, add entries from DB_FILENAME to TABLE, and return true. */ |
|
1900 |
|
1901 static bool |
4390
|
1902 db_build (hash_table_type *table, const std::string& db_filename) |
4378
|
1903 { |
4393
|
1904 std::string line; |
|
1905 |
4378
|
1906 unsigned dir_count = 0, file_count = 0, ignore_dir_count = 0; |
4393
|
1907 |
4390
|
1908 unsigned len = db_filename.length () - sizeof (DB_NAME) + 1; /* Keep the /. */ |
4393
|
1909 std::string top_dir = db_filename.substr (0, len); |
|
1910 |
|
1911 std::string cur_dir; |
|
1912 |
|
1913 FILE *db_file = xfopen (db_filename, "r"); |
4391
|
1914 |
|
1915 if (db_file) |
|
1916 { |
4393
|
1917 while (read_line (db_file, line)) |
4391
|
1918 { |
4393
|
1919 len = line.length (); |
4391
|
1920 |
|
1921 /* A line like `/foo:' = new dir foo. Allow both absolute (/...) |
|
1922 and explicitly relative (./...) names here. It's a kludge to |
|
1923 pass in the directory name with the trailing : still attached, |
|
1924 but it doesn't actually hurt. */ |
|
1925 if (len > 0 && line[len - 1] == ':' && kpse_absolute_p (line, true)) |
|
1926 { |
|
1927 /* New directory line. */ |
|
1928 if (! ignore_dir_p (line)) |
|
1929 { |
|
1930 /* If they gave a relative name, prepend full |
|
1931 directory name now. */ |
|
1932 line[len - 1] = DIR_SEP; |
|
1933 |
|
1934 /* Skip over leading `./', it confuses `match' and |
|
1935 is just a waste of space, anyway. This will lose |
|
1936 on `../', but `match' won't work there, either, |
|
1937 so it doesn't matter. */ |
|
1938 |
4393
|
1939 cur_dir = line[0] == '.' ? top_dir + line.substr (2) : line; |
4391
|
1940 |
|
1941 dir_count++; |
|
1942 } |
|
1943 else |
|
1944 { |
4393
|
1945 cur_dir = std::string (); |
4391
|
1946 ignore_dir_count++; |
|
1947 } |
|
1948 |
|
1949 /* Ignore blank, `.' and `..' lines. */ |
|
1950 |
|
1951 } |
4393
|
1952 else if (len > 0 && ! cur_dir.empty () /* a file line? */ |
|
1953 && ! (line[0] == '.' |
|
1954 && (len == 1 || (len == 2 && line[1] == '.')))) |
4391
|
1955 { |
|
1956 /* Make a new hash table entry with a key of `line' and |
|
1957 a data of `cur_dir'. An already-existing identical |
|
1958 key is ok, since a file named `foo' can be in more |
|
1959 than one directory. Share `cur_dir' among all its |
|
1960 files (and hence never free it). */ |
4393
|
1961 hash_insert (table, line, cur_dir); |
4391
|
1962 file_count++; |
|
1963 } |
|
1964 } |
|
1965 |
4393
|
1966 xfclose (db_file, db_filename); |
4391
|
1967 |
|
1968 if (file_count == 0) |
|
1969 { |
4396
|
1970 (*current_liboctave_warning_handler) |
|
1971 ("kpathsea: No usable entries in %s", db_filename.c_str ()); |
|
1972 |
|
1973 (*current_liboctave_warning_handler) |
|
1974 ("kpathsea: See the manual for how to generate ls-R"); |
|
1975 |
4391
|
1976 db_file = NULL; |
|
1977 } |
|
1978 else |
4394
|
1979 db_dir_list.append (top_dir); |
4378
|
1980 |
|
1981 #ifdef KPSE_DEBUG |
4391
|
1982 if (KPSE_DEBUG_P (KPSE_DEBUG_HASH)) |
|
1983 { |
|
1984 /* Don't make this a debugging bit, since the output is so |
|
1985 voluminous, and being able to specify -1 is too useful. |
|
1986 Instead, let people who want it run the program under |
|
1987 a debugger and change the variable that way. */ |
|
1988 bool hash_summary_only = true; |
|
1989 |
|
1990 DEBUGF4 ("%s: %u entries in %d directories (%d hidden).\n", |
|
1991 db_filename.c_str (), file_count, dir_count, |
|
1992 ignore_dir_count); |
|
1993 |
|
1994 DEBUGF ("ls-R hash table:"); |
|
1995 hash_print (*table, hash_summary_only); |
|
1996 fflush (stderr); |
|
1997 } |
|
1998 #endif /* KPSE_DEBUG */ |
4378
|
1999 } |
|
2000 |
|
2001 return db_file != NULL; |
|
2002 } |
|
2003 |
|
2004 /* Insert FNAME into the hash table. This is for files that get built |
|
2005 during a run. We wouldn't want to reread all of ls-R, even if it got |
|
2006 rebuilt. */ |
|
2007 |
|
2008 void |
4394
|
2009 kpse_db_insert (const std::string& passed_fname) |
4378
|
2010 { |
|
2011 /* We might not have found ls-R, or even had occasion to look for it |
|
2012 yet, so do nothing if we have no hash table. */ |
4391
|
2013 if (db.buckets) |
|
2014 { |
|
2015 const char *dir_part; |
4394
|
2016 char *fname = xstrdup (passed_fname.c_str ()); |
4391
|
2017 char *baseptr = xbasename (fname); |
|
2018 const char *file_part = xstrdup (baseptr); |
|
2019 |
|
2020 *baseptr = '\0'; /* Chop off the filename. */ |
|
2021 dir_part = fname; /* That leaves the dir, with the trailing /. */ |
|
2022 |
|
2023 hash_insert (&db, file_part, dir_part); |
|
2024 } |
4378
|
2025 } |
4392
|
2026 |
4378
|
2027 /* Return true if FILENAME could be in PATH_ELT, i.e., if the directory |
|
2028 part of FILENAME matches PATH_ELT. Have to consider // wildcards, but |
|
2029 $ and ~ expansion have already been done. */ |
4391
|
2030 |
4378
|
2031 static bool |
4390
|
2032 match (const std::string& filename_arg, const std::string& path_elt_arg) |
4378
|
2033 { |
4390
|
2034 const char *filename = filename_arg.c_str (); |
|
2035 const char *path_elt = path_elt_arg.c_str (); |
|
2036 |
4378
|
2037 const char *original_filename = filename; |
|
2038 bool matched = false; |
4391
|
2039 |
|
2040 for (; *filename && *path_elt; filename++, path_elt++) |
|
2041 { |
4394
|
2042 if (*filename == *path_elt) /* normal character match */ |
4391
|
2043 ; |
|
2044 |
|
2045 else if (IS_DIR_SEP (*path_elt) /* at // */ |
|
2046 && original_filename < filename && IS_DIR_SEP (path_elt[-1])) |
|
2047 { |
|
2048 while (IS_DIR_SEP (*path_elt)) |
|
2049 path_elt++; /* get past second and any subsequent /'s */ |
|
2050 |
|
2051 if (*path_elt == 0) |
|
2052 { |
|
2053 /* Trailing //, matches anything. We could make this |
|
2054 part of the other case, but it seems pointless to do |
|
2055 the extra work. */ |
|
2056 matched = true; |
|
2057 break; |
|
2058 } |
|
2059 else |
|
2060 { |
|
2061 /* Intermediate //, have to match rest of PATH_ELT. */ |
|
2062 for (; !matched && *filename; filename++) |
|
2063 { |
|
2064 /* Try matching at each possible character. */ |
4394
|
2065 if (IS_DIR_SEP (filename[-1]) && *filename == *path_elt) |
4391
|
2066 matched = match (filename, path_elt); |
|
2067 } |
|
2068 |
|
2069 /* Prevent filename++ when *filename='\0'. */ |
|
2070 break; |
|
2071 } |
|
2072 } |
|
2073 else |
|
2074 /* normal character nonmatch, quit */ |
|
2075 break; |
4378
|
2076 } |
|
2077 |
|
2078 /* If we've reached the end of PATH_ELT, check that we're at the last |
|
2079 component of FILENAME, we've matched. */ |
4391
|
2080 if (! matched && *path_elt == 0) |
|
2081 { |
|
2082 /* Probably PATH_ELT ended with `vf' or some such, and FILENAME |
|
2083 ends with `vf/ptmr.vf'. In that case, we'll be at a |
|
2084 directory separator. On the other hand, if PATH_ELT ended |
|
2085 with a / (as in `vf/'), FILENAME being the same `vf/ptmr.vf', |
|
2086 we'll be at the `p'. Upshot: if we're at a dir separator in |
|
2087 FILENAME, skip it. But if not, that's ok, as long as there |
|
2088 are no more dir separators. */ |
|
2089 |
|
2090 if (IS_DIR_SEP (*filename)) |
|
2091 filename++; |
|
2092 |
|
2093 while (*filename && !IS_DIR_SEP (*filename)) |
|
2094 filename++; |
|
2095 |
|
2096 matched = *filename == 0; |
|
2097 } |
|
2098 |
4378
|
2099 return matched; |
|
2100 } |
|
2101 |
|
2102 /* If DB_DIR is a prefix of PATH_ELT, return true; otherwise false. |
|
2103 That is, the question is whether to try the db for a file looked up |
|
2104 in PATH_ELT. If PATH_ELT == ".", for example, the answer is no. If |
|
2105 PATH_ELT == "/usr/local/lib/texmf/fonts//tfm", the answer is yes. |
4391
|
2106 |
4378
|
2107 In practice, ls-R is only needed for lengthy subdirectory |
|
2108 comparisons, but there's no gain to checking PATH_ELT to see if it is |
|
2109 a subdir match, since the only way to do that is to do a string |
|
2110 search in it, which is all we do anyway. */ |
4391
|
2111 |
4378
|
2112 static bool |
4390
|
2113 elt_in_db (const std::string& db_dir, const std::string& path_elt) |
4378
|
2114 { |
|
2115 bool found = false; |
|
2116 |
4390
|
2117 size_t db_dir_len = db_dir.length (); |
|
2118 size_t path_elt_len = path_elt.length (); |
|
2119 |
|
2120 size_t i = 0; |
|
2121 |
4394
|
2122 while (! found && db_dir[i] == path_elt[i]) |
4391
|
2123 { |
|
2124 i++; |
|
2125 /* If we've matched the entire db directory, it's good. */ |
|
2126 if (i == db_dir_len) |
|
2127 found = true; |
|
2128 |
4378
|
2129 /* If we've reached the end of PATH_ELT, but not the end of the db |
|
2130 directory, it's no good. */ |
4391
|
2131 else if (i == path_elt_len) |
|
2132 break; |
|
2133 } |
4378
|
2134 |
|
2135 return found; |
|
2136 } |
4392
|
2137 |
4378
|
2138 /* If ALIAS_FILENAME exists, read it into TABLE. */ |
|
2139 |
|
2140 static bool |
4390
|
2141 alias_build (hash_table_type *table, const std::string& alias_filename) |
4378
|
2142 { |
|
2143 unsigned count = 0; |
4393
|
2144 |
|
2145 FILE *alias_file = xfopen (alias_filename, "r"); |
4378
|
2146 |
4391
|
2147 if (alias_file) |
|
2148 { |
4393
|
2149 std::string line; |
|
2150 |
|
2151 while (read_line (alias_file, line)) |
4391
|
2152 { |
4393
|
2153 size_t len = line.length (); |
|
2154 |
4391
|
2155 /* comments or empty */ |
4393
|
2156 if (len == 0 || line[0] == '%' || line[0] == '#') |
4391
|
2157 /* do nothing */ ; |
|
2158 else |
|
2159 { |
4393
|
2160 size_t i = 0; |
|
2161 |
|
2162 while (i < len && isspace (line[i])) |
|
2163 i++; |
|
2164 |
|
2165 size_t real_beg = i; |
|
2166 |
|
2167 while (i < len && ! isspace (line[i])) |
|
2168 i++; |
|
2169 |
|
2170 size_t real_len = i - real_beg; |
|
2171 |
|
2172 while (i < len && isspace (line[i])) |
|
2173 i++; |
|
2174 |
|
2175 size_t alias_beg = i; |
|
2176 |
|
2177 while (i < len && ! isspace (line[i])) |
|
2178 i++; |
|
2179 |
|
2180 size_t alias_len = i - alias_beg; |
4391
|
2181 |
|
2182 /* Is the check for errors strong enough? Should we |
|
2183 warn the user for potential errors? */ |
4393
|
2184 if (real_len > 0 && alias_len > 0) |
4391
|
2185 { |
4393
|
2186 hash_insert (table, line.substr (alias_beg, alias_len), |
|
2187 line.substr (real_beg, real_len)); |
4391
|
2188 count++; |
|
2189 } |
|
2190 } |
|
2191 } |
4378
|
2192 |
|
2193 #ifdef KPSE_DEBUG |
4391
|
2194 if (KPSE_DEBUG_P (KPSE_DEBUG_HASH)) |
|
2195 { |
|
2196 /* As with ls-R above ... */ |
|
2197 bool hash_summary_only = true; |
|
2198 DEBUGF2 ("%s: %u aliases.\n", alias_filename.c_str (), count); |
|
2199 DEBUGF ("alias hash table:"); |
|
2200 hash_print (*table, hash_summary_only); |
|
2201 fflush (stderr); |
|
2202 } |
|
2203 #endif /* KPSE_DEBUG */ |
|
2204 |
4393
|
2205 xfclose (alias_file, alias_filename); |
4378
|
2206 } |
|
2207 |
|
2208 return alias_file != NULL; |
|
2209 } |
4392
|
2210 |
4378
|
2211 /* Initialize the path for ls-R files, and read them all into the hash |
|
2212 table `db'. If no usable ls-R's are found, set db.buckets to NULL. */ |
|
2213 |
|
2214 void |
|
2215 kpse_init_db (void) |
|
2216 { |
|
2217 bool ok = false; |
4390
|
2218 const std::string db_path = kpse_init_format (); |
|
2219 string_vector db_files = kpse_all_path_search (db_path.c_str (), DB_NAME); |
4378
|
2220 |
|
2221 /* Must do this after the path searching (which ends up calling |
|
2222 kpse_db_search recursively), so db.buckets stays NULL. */ |
|
2223 db = hash_create (DB_HASH_SIZE); |
|
2224 |
4390
|
2225 int len = db_files.length (); |
|
2226 for (int i = 0; i < len; i++) |
|
2227 { |
|
2228 if (! db_files[i].empty ()) |
|
2229 { |
|
2230 if (db_build (&db, db_files[i])) |
|
2231 ok = true; |
|
2232 } |
|
2233 } |
4391
|
2234 |
|
2235 if (! ok) |
|
2236 { |
|
2237 /* If db can't be built, leave `size' nonzero (so we don't |
|
2238 rebuild it), but clear `buckets' (so we don't look in it). */ |
|
2239 free (db.buckets); |
|
2240 db.buckets = NULL; |
|
2241 } |
4378
|
2242 |
|
2243 /* Add the content of any alias databases. There may exist more than |
|
2244 one alias file along DB_NAME files. This duplicates the above code |
|
2245 -- should be a function. */ |
|
2246 ok = false; |
4390
|
2247 db_files = kpse_all_path_search (db_path.c_str (), ALIAS_NAME); |
4378
|
2248 |
|
2249 alias_db = hash_create (ALIAS_HASH_SIZE); |
|
2250 |
4390
|
2251 len = db_files.length (); |
|
2252 for (int i = 0; i < len; i++) |
|
2253 { |
|
2254 if (! db_files[i].empty ()) |
|
2255 { |
|
2256 if (alias_build (&alias_db, db_files[i])) |
|
2257 ok = true; |
|
2258 } |
|
2259 } |
4378
|
2260 |
4391
|
2261 if (! ok) |
|
2262 { |
|
2263 free (alias_db.buckets); |
|
2264 alias_db.buckets = NULL; |
|
2265 } |
4378
|
2266 } |
4392
|
2267 |
4378
|
2268 /* Avoid doing anything if this PATH_ELT is irrelevant to the databases. */ |
|
2269 |
4390
|
2270 string_vector |
|
2271 kpse_db_search (const std::string& name_arg, |
4394
|
2272 const std::string& orig_path_elt, bool all) |
4378
|
2273 { |
|
2274 bool done; |
4390
|
2275 string_vector ret; |
|
2276 string_vector aliases; |
4378
|
2277 bool relevant = false; |
4390
|
2278 |
4394
|
2279 std::string name = name_arg; |
4391
|
2280 |
4378
|
2281 /* If we failed to build the database (or if this is the recursive |
|
2282 call to build the db path), quit. */ |
|
2283 if (db.buckets == NULL) |
4390
|
2284 return ret; |
4391
|
2285 |
4378
|
2286 /* When tex-glyph.c calls us looking for, e.g., dpi600/cmr10.pk, we |
|
2287 won't find it unless we change NAME to just `cmr10.pk' and append |
|
2288 `/dpi600' to PATH_ELT. We are justified in using a literal `/' |
|
2289 here, since that's what tex-glyph.c unconditionally uses in |
|
2290 DPI_BITMAP_SPEC. But don't do anything if the / begins NAME; that |
|
2291 should never happen. */ |
4394
|
2292 std::string path_elt; |
|
2293 size_t last_slash = name.rfind ('/'); |
|
2294 if (last_slash != NPOS && last_slash != 0) |
4390
|
2295 { |
4394
|
2296 std::string dir_part = name.substr (0, last_slash); |
|
2297 name = name.substr (last_slash + 1); |
4390
|
2298 } |
|
2299 else |
4394
|
2300 path_elt = orig_path_elt; |
4378
|
2301 |
|
2302 /* Don't bother doing any lookups if this `path_elt' isn't covered by |
|
2303 any of database directories. We do this not so much because the |
|
2304 extra couple of hash lookups matter -- they don't -- but rather |
|
2305 because we want to return NULL in this case, so path_search can |
|
2306 know to do a disk search. */ |
4394
|
2307 for (int e = 0; ! relevant && e < db_dir_list.length (); e++) |
4390
|
2308 relevant = elt_in_db (db_dir_list[e], path_elt); |
|
2309 |
4391
|
2310 if (! relevant) |
4390
|
2311 return ret; |
4378
|
2312 |
|
2313 /* If we have aliases for this name, use them. */ |
|
2314 if (alias_db.buckets) |
|
2315 aliases = hash_lookup (alias_db, name); |
|
2316 |
4390
|
2317 /* Push aliases up by one and insert the original name at the front. */ |
|
2318 int len = aliases.length (); |
|
2319 aliases.resize (len+1); |
|
2320 for (int i = len; i > 0; i--) |
|
2321 aliases[i] = aliases[i - 1]; |
|
2322 aliases[0] = name; |
4378
|
2323 |
|
2324 done = false; |
4390
|
2325 len = aliases.length (); |
|
2326 for (int i = 0; i < len && !done; i++) |
|
2327 { |
|
2328 std::string atry = aliases[i]; |
|
2329 |
|
2330 /* We have an ls-R db. Look up `atry'. */ |
|
2331 string_vector db_dirs = hash_lookup (db, atry); |
|
2332 |
|
2333 /* For each filename found, see if it matches the path element. For |
|
2334 example, if we have .../cx/cmr10.300pk and .../ricoh/cmr10.300pk, |
|
2335 and the path looks like .../cx, we don't want the ricoh file. */ |
|
2336 |
|
2337 int db_dirs_len = db_dirs.length (); |
|
2338 for (int j = 0; j < db_dirs_len && !done; j++) |
|
2339 { |
|
2340 std::string db_file = db_dirs[j] + atry; |
|
2341 bool matched = match (db_file, path_elt); |
4378
|
2342 |
|
2343 #ifdef KPSE_DEBUG |
4390
|
2344 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
4394
|
2345 DEBUGF3 ("db:match (%s,%s) = %d\n", db_file.c_str (), path_elt.c_str (), matched); |
4378
|
2346 #endif |
|
2347 |
4390
|
2348 /* We got a hit in the database. Now see if the file actually |
|
2349 exists, possibly under an alias. */ |
|
2350 if (matched) |
|
2351 { |
|
2352 std::string found; |
4393
|
2353 std::string tmp = kpse_readable_file (db_file); |
|
2354 if (! tmp.empty ()) |
4390
|
2355 found = db_file; |
|
2356 else |
|
2357 { |
|
2358 /* The hit in the DB doesn't exist in disk. Now try |
|
2359 all its aliases. For example, suppose we have a |
|
2360 hierarchy on CD, thus `mf.bas', but ls-R contains |
|
2361 `mf.base'. Find it anyway. Could probably work |
|
2362 around this with aliases, but this is pretty easy |
|
2363 and shouldn't hurt. The upshot is that if one of |
|
2364 the aliases actually exists, we use that. */ |
|
2365 |
|
2366 int aliases_len = aliases.length (); |
|
2367 |
|
2368 for (int k = 1; k < aliases_len && found.empty (); k++) |
|
2369 { |
|
2370 std::string atry = db_dirs[j] + aliases[k]; |
4393
|
2371 std::string tmp = kpse_readable_file (atry); |
|
2372 if (! tmp.empty ()) |
4390
|
2373 found = atry; |
|
2374 } |
|
2375 } |
4391
|
2376 |
4390
|
2377 /* If we have a real file, add it to the list, maybe done. */ |
|
2378 if (! found.empty ()) |
|
2379 { |
4392
|
2380 ret.append (found); |
|
2381 |
4390
|
2382 if (! (all || found.empty ())) |
|
2383 done = true; |
|
2384 } |
|
2385 } |
|
2386 } |
4378
|
2387 } |
4391
|
2388 |
4378
|
2389 return ret; |
|
2390 } |
|
2391 |
|
2392 /* kdefault.c: Expand extra colons. */ |
|
2393 |
|
2394 /* Check for leading colon first, then trailing, then doubled, since |
|
2395 that is fastest. Usually it will be leading or trailing. */ |
|
2396 |
4394
|
2397 std::string |
|
2398 kpse_expand_default (const std::string& path, const std::string& fallback) |
4378
|
2399 { |
4394
|
2400 std::string expansion; |
|
2401 |
|
2402 size_t path_len = path.length (); |
|
2403 |
|
2404 if (path_len == 0) |
|
2405 expansion = fallback; |
4378
|
2406 |
|
2407 /* Solitary or leading :? */ |
4394
|
2408 else if (IS_ENV_SEP (path[0])) |
4378
|
2409 { |
4394
|
2410 expansion = path_len == 1 ? fallback : fallback + path; |
4378
|
2411 } |
|
2412 |
|
2413 /* Sorry about the assignment in the middle of the expression, but |
|
2414 conventions were made to be flouted and all that. I don't see the |
|
2415 point of calling strlen twice or complicating the logic just to |
|
2416 avoid the assignment (especially now that I've pointed it out at |
|
2417 such great length). */ |
4394
|
2418 else if (IS_ENV_SEP (path[path_len-1])) |
|
2419 expansion = path + fallback; |
4378
|
2420 |
|
2421 /* OK, not leading or trailing. Check for doubled. */ |
|
2422 else |
|
2423 { |
|
2424 /* What we'll return if we find none. */ |
4394
|
2425 expansion = path; |
|
2426 |
|
2427 for (size_t i = 0; i < path_len; i++) |
4378
|
2428 { |
4394
|
2429 if (i + 1 < path_len |
|
2430 && IS_ENV_SEP (path[i]) && IS_ENV_SEP (path[i+1])) |
|
2431 { |
|
2432 /* We have a doubled colon. */ |
4391
|
2433 |
4378
|
2434 /* Copy stuff up to and including the first colon. */ |
|
2435 /* Copy in FALLBACK, and then the rest of PATH. */ |
4394
|
2436 expansion = path.substr (0, i+1) + fallback + path.substr (i+1); |
4378
|
2437 |
|
2438 break; |
|
2439 } |
|
2440 } |
|
2441 } |
4391
|
2442 |
4378
|
2443 return expansion; |
|
2444 } |
|
2445 |
|
2446 /* elt-dirs.c: Translate a path element to its corresponding |
|
2447 director{y,ies}. */ |
|
2448 |
|
2449 /* To avoid giving prototypes for all the routines and then their real |
|
2450 definitions, we give all the subroutines first. The entry point is |
|
2451 the last routine in the file. */ |
4392
|
2452 |
4378
|
2453 /* Make a copy of DIR (unless it's null) and save it in L. Ensure that |
|
2454 DIR ends with a DIR_SEP for the benefit of later searches. */ |
|
2455 |
|
2456 static void |
4390
|
2457 dir_list_add (str_llist_type *l, const std::string& dir) |
4378
|
2458 { |
4390
|
2459 char last_char = dir[dir.length () - 1]; |
|
2460 std::string saved_dir = dir; |
|
2461 if (IS_DIR_SEP (last_char) || IS_DEVICE_SEP (last_char)) |
|
2462 saved_dir += DIR_SEP_STRING; |
4391
|
2463 |
4378
|
2464 str_llist_add (l, saved_dir); |
|
2465 } |
|
2466 |
4390
|
2467 /* Return true if FN is a directory or a symlink to a directory, |
|
2468 false if not. */ |
|
2469 |
|
2470 static bool |
|
2471 dir_p (const std::string& fn) |
|
2472 { |
|
2473 #ifdef WIN32 |
4391
|
2474 unsigned int fa = GetFileAttributes (fn.c_str ()); |
4390
|
2475 return (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY)); |
|
2476 #else |
|
2477 struct stat stats; |
|
2478 return stat (fn.c_str (), &stats) == 0 && S_ISDIR (stats.st_mode); |
|
2479 #endif |
|
2480 } |
4391
|
2481 |
4378
|
2482 /* If DIR is a directory, add it to the list L. */ |
|
2483 |
|
2484 static void |
4390
|
2485 checked_dir_list_add (str_llist_type *l, const std::string& dir) |
4378
|
2486 { |
|
2487 if (dir_p (dir)) |
|
2488 dir_list_add (l, dir); |
|
2489 } |
4392
|
2490 |
4378
|
2491 /* The cache. Typically, several paths have the same element; for |
|
2492 example, /usr/local/lib/texmf/fonts//. We don't want to compute the |
|
2493 expansion of such a thing more than once. Even though we also cache |
|
2494 the dir_links call, that's not enough -- without this path element |
|
2495 caching as well, the execution time doubles. */ |
|
2496 |
|
2497 typedef struct |
|
2498 { |
|
2499 const char *key; |
|
2500 str_llist_type *value; |
|
2501 } cache_entry; |
|
2502 |
|
2503 static cache_entry *the_cache = NULL; |
|
2504 static unsigned cache_length = 0; |
|
2505 |
|
2506 /* Associate KEY with VALUE. We implement the cache as a simple linear |
|
2507 list, since it's unlikely to ever be more than a dozen or so elements |
|
2508 long. We don't bother to check here if PATH has already been saved; |
|
2509 we always add it to our list. We copy KEY but not VALUE; not sure |
|
2510 that's right, but it seems to be all that's needed. */ |
|
2511 |
|
2512 static void |
|
2513 cache (const char *key, str_llist_type *value) |
|
2514 { |
|
2515 cache_length++; |
4396
|
2516 |
|
2517 the_cache = (cache_entry *) xrealloc (the_cache, |
|
2518 cache_length * sizeof (cache_entry)); |
|
2519 |
4378
|
2520 the_cache[cache_length - 1].key = xstrdup (key); |
|
2521 the_cache[cache_length - 1].value = value; |
|
2522 } |
|
2523 |
|
2524 /* To retrieve, just check the list in order. */ |
|
2525 |
|
2526 static str_llist_type * |
|
2527 cached (const char *key) |
|
2528 { |
|
2529 unsigned p; |
4391
|
2530 |
4378
|
2531 for (p = 0; p < cache_length; p++) |
|
2532 { |
4394
|
2533 if (! strcmp (the_cache[p].key, key)) |
4378
|
2534 return the_cache[p].value; |
|
2535 } |
4391
|
2536 |
4378
|
2537 return NULL; |
|
2538 } |
4392
|
2539 |
4378
|
2540 /* Handle the magic path constructs. */ |
|
2541 |
|
2542 /* Declare recursively called routine. */ |
|
2543 static void expand_elt (str_llist_type *, const char *, unsigned); |
|
2544 |
|
2545 /* POST is a pointer into the original element (which may no longer be |
|
2546 ELT) to just after the doubled DIR_SEP, perhaps to the null. Append |
|
2547 subdirectories of ELT (up to ELT_LENGTH, which must be a /) to |
|
2548 STR_LIST_PTR. */ |
|
2549 |
|
2550 #ifdef WIN32 |
|
2551 /* Shared across recursive calls, it acts like a stack. */ |
|
2552 static char dirname[MAX_PATH]; |
|
2553 #endif |
|
2554 |
|
2555 static void |
|
2556 do_subdir (str_llist_type *str_list_ptr, const char *elt, |
|
2557 unsigned elt_length, const char *post) |
|
2558 { |
|
2559 #ifdef WIN32 |
|
2560 WIN32_FIND_DATA find_file_data; |
|
2561 HANDLE hnd; |
|
2562 int proceed; |
|
2563 #else |
|
2564 DIR *dir; |
|
2565 struct dirent *e; |
|
2566 #endif /* not WIN32 */ |
4389
|
2567 |
|
2568 std::string name (elt, elt_length); |
|
2569 |
4378
|
2570 assert (IS_DIR_SEP (elt[elt_length - 1]) |
|
2571 || IS_DEVICE_SEP (elt[elt_length - 1])); |
4391
|
2572 |
4378
|
2573 #if defined (WIN32) |
4391
|
2574 strcpy (dirname, name.c_str ()); |
|
2575 strcat (dirname, "/*.*"); /* "*.*" or "*" -- seems equivalent. */ |
|
2576 hnd = FindFirstFile (dirname, &find_file_data); |
4378
|
2577 |
4389
|
2578 if (hnd == INVALID_HANDLE_VALUE) |
4378
|
2579 return; |
|
2580 |
|
2581 /* Include top level before subdirectories, if nothing to match. */ |
|
2582 if (*post == 0) |
4390
|
2583 dir_list_add (str_list_ptr, name); |
4378
|
2584 else { |
|
2585 /* If we do have something to match, see if it exists. For |
|
2586 example, POST might be `pk/ljfour', and they might have a |
|
2587 directory `$TEXMF/fonts/pk/ljfour' that we should find. */ |
4389
|
2588 name += post; |
|
2589 expand_elt (str_list_ptr, name.c_str (), elt_length); |
|
2590 name.resize (elt_length); |
4378
|
2591 } |
|
2592 proceed = 1; |
4391
|
2593 while (proceed) |
|
2594 { |
|
2595 if (find_file_data.cFileName[0] != '.') |
|
2596 { |
|
2597 /* Construct the potential subdirectory name. */ |
|
2598 name += find_file_data.cFileName; |
|
2599 if (find_file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
|
2600 { |
|
2601 /* It's a directory, so append the separator. */ |
|
2602 name += DIR_SEP_STRING; |
|
2603 unsigned potential_len = name.length (); |
|
2604 do_subdir (str_list_ptr, name.c_str (), |
|
2605 potential_len, post); |
|
2606 } |
|
2607 name.resize (elt_length); |
|
2608 } |
|
2609 proceed = FindNextFile (hnd, &find_file_data); |
4378
|
2610 } |
4391
|
2611 FindClose (hnd); |
4378
|
2612 |
|
2613 #else /* not WIN32 */ |
|
2614 |
|
2615 /* If we can't open it, quit. */ |
4389
|
2616 dir = opendir (name.c_str ()); |
4378
|
2617 if (dir == NULL) |
4389
|
2618 return; |
4391
|
2619 |
4378
|
2620 /* Include top level before subdirectories, if nothing to match. */ |
|
2621 if (*post == 0) |
4390
|
2622 dir_list_add (str_list_ptr, name); |
4378
|
2623 else |
|
2624 { /* If we do have something to match, see if it exists. For |
|
2625 example, POST might be `pk/ljfour', and they might have a |
|
2626 directory `$TEXMF/fonts/pk/ljfour' that we should find. */ |
4389
|
2627 name += post; |
|
2628 expand_elt (str_list_ptr, name.c_str (), elt_length); |
|
2629 name.resize (elt_length); |
4378
|
2630 } |
|
2631 |
|
2632 while ((e = readdir (dir)) != NULL) |
|
2633 { /* If it begins with a `.', never mind. (This allows ``hidden'' |
|
2634 directories that the algorithm won't find.) */ |
|
2635 if (e->d_name[0] != '.') |
|
2636 { |
|
2637 int links; |
4391
|
2638 |
4378
|
2639 /* Construct the potential subdirectory name. */ |
4389
|
2640 name += e->d_name; |
4391
|
2641 |
4378
|
2642 /* If we can't stat it, or if it isn't a directory, continue. */ |
4389
|
2643 links = dir_links (name.c_str ()); |
4378
|
2644 |
|
2645 if (links >= 0) |
4391
|
2646 { |
4378
|
2647 /* It's a directory, so append the separator. */ |
4389
|
2648 name += DIR_SEP_STRING; |
|
2649 unsigned potential_len = name.length (); |
4391
|
2650 |
4378
|
2651 /* Should we recurse? To see if the subdirectory is a |
|
2652 leaf, check if it has two links (one for . and one for |
|
2653 ..). This means that symbolic links to directories do |
|
2654 not affect the leaf-ness. This is arguably wrong, but |
|
2655 the only alternative I know of is to stat every entry |
|
2656 in the directory, and that is unacceptably slow. |
4391
|
2657 |
4378
|
2658 The #ifdef here makes all this configurable at |
|
2659 compile-time, so that if we're using VMS directories or |
|
2660 some such, we can still find subdirectories, even if it |
|
2661 is much slower. */ |
|
2662 #ifdef ST_NLINK_TRICK |
|
2663 if (links > 2) |
|
2664 #endif /* not ST_NLINK_TRICK */ |
|
2665 /* All criteria are met; find subdirectories. */ |
4389
|
2666 do_subdir (str_list_ptr, name.c_str (), |
4378
|
2667 potential_len, post); |
|
2668 #ifdef ST_NLINK_TRICK |
|
2669 else if (*post == 0) |
|
2670 /* Nothing to match, no recursive subdirectories to |
|
2671 look for: we're done with this branch. Add it. */ |
4390
|
2672 dir_list_add (str_list_ptr, name); |
4378
|
2673 #endif |
|
2674 } |
|
2675 |
|
2676 /* Remove the directory entry we just checked from `name'. */ |
4389
|
2677 name.resize (elt_length); |
4378
|
2678 } |
|
2679 } |
4391
|
2680 |
4378
|
2681 xclosedir (dir); |
|
2682 #endif /* not WIN32 */ |
|
2683 } |
|
2684 |
|
2685 /* Assume ELT is non-empty and non-NULL. Return list of corresponding |
|
2686 directories (with no terminating NULL entry) in STR_LIST_PTR. Start |
|
2687 looking for magic constructs at START. */ |
|
2688 |
|
2689 static void |
|
2690 expand_elt (str_llist_type *str_list_ptr, const char *elt, unsigned start) |
|
2691 { |
|
2692 const char *dir = elt + start; |
|
2693 const char *post; |
4391
|
2694 |
4378
|
2695 while (*dir != 0) |
|
2696 { |
|
2697 if (IS_DIR_SEP (*dir)) |
|
2698 { |
|
2699 /* If two or more consecutive /'s, find subdirectories. */ |
|
2700 if (IS_DIR_SEP (dir[1])) |
|
2701 { |
|
2702 for (post = dir + 1; IS_DIR_SEP (*post); post++) ; |
|
2703 do_subdir (str_list_ptr, elt, dir - elt + 1, post); |
|
2704 return; |
|
2705 } |
|
2706 |
|
2707 /* No special stuff at this slash. Keep going. */ |
|
2708 } |
4391
|
2709 |
4378
|
2710 dir++; |
|
2711 } |
4391
|
2712 |
4378
|
2713 /* When we reach the end of ELT, it will be a normal filename. */ |
|
2714 checked_dir_list_add (str_list_ptr, elt); |
|
2715 } |
4392
|
2716 |
4378
|
2717 /* Here is the entry point. Returns directory list for ELT. */ |
|
2718 |
|
2719 str_llist_type * |
4394
|
2720 kpse_element_dirs (const std::string& elt_arg) |
4378
|
2721 { |
4394
|
2722 const char *elt = elt_arg.c_str (); |
|
2723 |
4378
|
2724 str_llist_type *ret; |
|
2725 |
|
2726 /* If given nothing, return nothing. */ |
4391
|
2727 if (! elt || !*elt) |
4378
|
2728 return NULL; |
|
2729 |
|
2730 /* If we've already cached the answer for ELT, return it. */ |
|
2731 ret = cached (elt); |
|
2732 if (ret) |
|
2733 return ret; |
|
2734 |
|
2735 /* We're going to have a real directory list to return. */ |
4390
|
2736 ret = new str_llist_type; |
4378
|
2737 *ret = NULL; |
|
2738 |
|
2739 /* We handle the hard case in a subroutine. */ |
|
2740 expand_elt (ret, elt, 0); |
|
2741 |
|
2742 /* Remember the directory list we just found, in case future calls are |
|
2743 made with the same ELT. */ |
|
2744 cache (elt, ret); |
|
2745 |
|
2746 #ifdef KPSE_DEBUG |
|
2747 if (KPSE_DEBUG_P (KPSE_DEBUG_EXPAND)) |
|
2748 { |
|
2749 DEBUGF1 ("path element %s =>", elt); |
|
2750 if (ret) |
|
2751 { |
|
2752 str_llist_elt_type *e; |
|
2753 for (e = *ret; e; e = STR_LLIST_NEXT (*e)) |
4390
|
2754 fprintf (stderr, " %s", (STR_LLIST (*e)).c_str ()); |
4378
|
2755 } |
|
2756 putc ('\n', stderr); |
|
2757 fflush (stderr); |
|
2758 } |
|
2759 #endif /* KPSE_DEBUG */ |
|
2760 |
|
2761 return ret; |
|
2762 } |
|
2763 |
4386
|
2764 #ifndef WIN32 |
4385
|
2765 void |
|
2766 xclosedir (DIR *d) |
|
2767 { |
|
2768 #ifdef CLOSEDIR_VOID |
|
2769 closedir (d); |
|
2770 #else |
|
2771 int ret = closedir (d); |
4391
|
2772 |
4385
|
2773 if (ret != 0) |
|
2774 FATAL ("closedir failed"); |
|
2775 #endif |
|
2776 } |
4386
|
2777 #endif |
4385
|
2778 |
|
2779 /* dir.c: directory operations. */ |
|
2780 |
|
2781 #ifndef WIN32 |
|
2782 |
|
2783 /* Return -1 if FN isn't a directory, else its number of links. |
|
2784 Duplicate the call to stat; no need to incur overhead of a function |
|
2785 call for that little bit of cleanliness. */ |
|
2786 |
|
2787 int |
|
2788 dir_links (const char *fn) |
|
2789 { |
4390
|
2790 std::map<std::string, long> link_table; |
|
2791 |
4385
|
2792 long ret; |
4391
|
2793 |
4390
|
2794 if (link_table.find (fn) != link_table.end ()) |
|
2795 ret = link_table[fn]; |
4385
|
2796 else |
|
2797 { |
|
2798 struct stat stats; |
4390
|
2799 |
4385
|
2800 ret = stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode) |
|
2801 ? stats.st_nlink : (unsigned) -1; |
|
2802 |
4390
|
2803 link_table[fn] = ret; |
4391
|
2804 |
4385
|
2805 #ifdef KPSE_DEBUG |
|
2806 if (KPSE_DEBUG_P (KPSE_DEBUG_STAT)) |
4391
|
2807 DEBUGF2 ("dir_links (%s) => %ld\n", fn, ret); |
4385
|
2808 #endif |
|
2809 } |
|
2810 |
|
2811 return ret; |
|
2812 } |
|
2813 |
|
2814 #endif /* !WIN32 */ |
|
2815 |
|
2816 /* debug.c: Help the user discover what's going on. */ |
|
2817 |
|
2818 #ifdef KPSE_DEBUG |
|
2819 |
|
2820 unsigned int kpathsea_debug = 0; |
|
2821 |
|
2822 /* If the real definitions of fopen or fclose are macros, we lose -- the |
|
2823 #undef won't restore them. */ |
|
2824 |
|
2825 FILE * |
|
2826 fopen (const char *filename, const char *mode) |
|
2827 { |
|
2828 #undef fopen |
|
2829 FILE *ret = fopen (filename, mode); |
|
2830 |
|
2831 if (KPSE_DEBUG_P (KPSE_DEBUG_FOPEN)) |
4391
|
2832 DEBUGF3 ("fopen (%s, %s) => 0x%lx\n", filename, mode, (unsigned long) ret); |
4385
|
2833 |
|
2834 return ret; |
|
2835 } |
|
2836 |
|
2837 int |
|
2838 fclose (FILE *f) |
|
2839 { |
|
2840 #undef fclose |
|
2841 int ret = fclose (f); |
4391
|
2842 |
4385
|
2843 if (KPSE_DEBUG_P (KPSE_DEBUG_FOPEN)) |
4391
|
2844 DEBUGF2 ("fclose (0x%lx) => %d\n", (unsigned long) f, ret); |
4385
|
2845 |
|
2846 return ret; |
|
2847 } |
|
2848 |
|
2849 #endif |
|
2850 |
|
2851 /* str-llist.c: Implementation of a linked list of strings. */ |
|
2852 |
|
2853 /* Add the new string STR to the end of the list L. */ |
|
2854 |
|
2855 void |
4390
|
2856 str_llist_add (str_llist_type *l, const std::string& str) |
4385
|
2857 { |
|
2858 str_llist_elt_type *e; |
4390
|
2859 str_llist_elt_type *new_elt = new str_llist_elt_type; |
4391
|
2860 |
4385
|
2861 /* The new element will be at the end of the list. */ |
|
2862 STR_LLIST (*new_elt) = str; |
|
2863 STR_LLIST_MOVED (*new_elt) = 0; |
|
2864 STR_LLIST_NEXT (*new_elt) = NULL; |
4391
|
2865 |
4385
|
2866 /* Find the current end of the list. */ |
|
2867 for (e = *l; e && STR_LLIST_NEXT (*e); e = STR_LLIST_NEXT (*e)) |
|
2868 ; |
4391
|
2869 |
|
2870 if (! e) |
4385
|
2871 *l = new_elt; |
|
2872 else |
|
2873 STR_LLIST_NEXT (*e) = new_elt; |
|
2874 } |
4392
|
2875 |
4385
|
2876 /* Move an element towards the top. The idea is that when a file is |
|
2877 found in a given directory, later files will likely be in that same |
|
2878 directory, and looking for the file in all the directories in between |
|
2879 is thus a waste. */ |
|
2880 |
|
2881 void |
|
2882 str_llist_float (str_llist_type *l, str_llist_elt_type *mover) |
|
2883 { |
|
2884 str_llist_elt_type *last_moved, *unmoved; |
4391
|
2885 |
4385
|
2886 /* If we've already moved this element, never mind. */ |
|
2887 if (STR_LLIST_MOVED (*mover)) |
|
2888 return; |
4391
|
2889 |
4385
|
2890 /* Find the first unmoved element (to insert before). We're |
|
2891 guaranteed this will terminate, since MOVER itself is currently |
|
2892 unmoved, and it must be in L (by hypothesis). */ |
|
2893 for (last_moved = NULL, unmoved = *l; STR_LLIST_MOVED (*unmoved); |
|
2894 last_moved = unmoved, unmoved = STR_LLIST_NEXT (*unmoved)) |
|
2895 ; |
|
2896 |
|
2897 /* If we are the first unmoved element, nothing to relink. */ |
|
2898 if (unmoved != mover) |
|
2899 { /* Remember `mover's current successor, so we can relink `mover's |
|
2900 predecessor to it. */ |
|
2901 str_llist_elt_type *before_mover; |
|
2902 str_llist_elt_type *after_mover = STR_LLIST_NEXT (*mover); |
4391
|
2903 |
4385
|
2904 /* Find `mover's predecessor. */ |
|
2905 for (before_mover = unmoved; STR_LLIST_NEXT (*before_mover) != mover; |
|
2906 before_mover = STR_LLIST_NEXT (*before_mover)) |
|
2907 ; |
4391
|
2908 |
4385
|
2909 /* `before_mover' now links to `after_mover'. */ |
|
2910 STR_LLIST_NEXT (*before_mover) = after_mover; |
|
2911 |
|
2912 /* Insert `mover' before `unmoved' and after `last_moved' (or at |
|
2913 the head of the list). */ |
|
2914 STR_LLIST_NEXT (*mover) = unmoved; |
4391
|
2915 if (! last_moved) |
4385
|
2916 *l = mover; |
|
2917 else |
|
2918 STR_LLIST_NEXT (*last_moved) = mover; |
|
2919 } |
|
2920 |
|
2921 /* We've moved it. */ |
|
2922 STR_LLIST_MOVED (*mover) = 1; |
|
2923 } |
|
2924 |
|
2925 /* variable.c: variable expansion. */ |
|
2926 |
|
2927 /* We have to keep track of variables being expanded, otherwise |
|
2928 constructs like TEXINPUTS = $TEXINPUTS result in an infinite loop. |
|
2929 (Or indirectly recursive variables, etc.) Our simple solution is to |
|
2930 add to a list each time an expansion is started, and check the list |
|
2931 before expanding. */ |
|
2932 |
4391
|
2933 static std::map <std::string, bool> expansions; |
4385
|
2934 |
|
2935 static void |
4391
|
2936 expanding (const std::string& var, bool xp) |
4385
|
2937 { |
4391
|
2938 expansions[var] = xp; |
4385
|
2939 } |
|
2940 |
|
2941 /* Return whether VAR is currently being expanding. */ |
|
2942 |
4391
|
2943 static bool |
|
2944 expanding_p (const std::string& var) |
4385
|
2945 { |
4391
|
2946 return (expansions.find (var) != expansions.end ()) |
|
2947 ? expansions[var] : false; |
4385
|
2948 } |
4392
|
2949 |
4385
|
2950 /* Append the result of value of `var' to EXPANSION, where `var' begins |
|
2951 at START and ends at END. If `var' is not set, do not complain. |
|
2952 This is a subroutine for the more complicated expansion function. */ |
|
2953 |
|
2954 static void |
4391
|
2955 expand (std::string &expansion, const std::string& var) |
4385
|
2956 { |
4391
|
2957 if (expanding_p (var)) |
|
2958 { |
4396
|
2959 (*current_liboctave_warning_handler) |
|
2960 ("kpathsea: variable `%s' references itself (eventually)", |
|
2961 var.c_str ()); |
4385
|
2962 } |
4391
|
2963 else |
|
2964 { |
|
2965 /* Check for an environment variable. */ |
|
2966 std::string value = octave_env::getenv (var); |
|
2967 |
|
2968 if (! value.empty ()) |
|
2969 { |
|
2970 expanding (var, true); |
|
2971 std::string tmp = kpse_var_expand (value); |
|
2972 expanding (var, false); |
|
2973 expansion += tmp; |
|
2974 } |
|
2975 } |
4385
|
2976 } |
4392
|
2977 |
4385
|
2978 /* Can't think of when it would be useful to change these (and the |
|
2979 diagnostic messages assume them), but ... */ |
|
2980 #ifndef IS_VAR_START /* starts all variable references */ |
|
2981 #define IS_VAR_START(c) ((c) == '$') |
|
2982 #endif |
|
2983 #ifndef IS_VAR_CHAR /* variable name constituent */ |
|
2984 #define IS_VAR_CHAR(c) (isalnum (c) || (c) == '_') |
|
2985 #endif |
|
2986 #ifndef IS_VAR_BEGIN_DELIMITER /* start delimited variable name (after $) */ |
|
2987 #define IS_VAR_BEGIN_DELIMITER(c) ((c) == '{') |
|
2988 #endif |
|
2989 #ifndef IS_VAR_END_DELIMITER |
|
2990 #define IS_VAR_END_DELIMITER(c) ((c) == '}') |
|
2991 #endif |
|
2992 |
|
2993 /* Maybe we should support some or all of the various shell ${...} |
|
2994 constructs, especially ${var-value}. */ |
|
2995 |
4389
|
2996 std::string |
4391
|
2997 kpse_var_expand (const std::string& src) |
4385
|
2998 { |
4389
|
2999 std::string expansion; |
4391
|
3000 |
|
3001 size_t src_len = src.length (); |
|
3002 |
4385
|
3003 /* Copy everything but variable constructs. */ |
4391
|
3004 for (size_t i = 0; i < src_len; i++) |
|
3005 { |
|
3006 if (IS_VAR_START (src[i])) |
|
3007 { |
|
3008 i++; |
|
3009 |
|
3010 /* Three cases: `$VAR', `${VAR}', `$<anything-else>'. */ |
|
3011 if (IS_VAR_CHAR (src[i])) |
|
3012 { |
|
3013 /* $V: collect name constituents, then expand. */ |
|
3014 size_t var_end = i; |
|
3015 |
|
3016 do |
|
3017 { |
|
3018 var_end++; |
|
3019 } |
|
3020 while (IS_VAR_CHAR (src[var_end])); |
|
3021 |
|
3022 var_end--; /* had to go one past */ |
|
3023 expand (expansion, src.substr (i, var_end - i + 1)); |
|
3024 i = var_end; |
|
3025 |
|
3026 } |
|
3027 else if (IS_VAR_BEGIN_DELIMITER (src[i])) |
|
3028 { |
|
3029 /* ${: scan ahead for matching delimiter, then expand. */ |
|
3030 size_t var_end = ++i; |
|
3031 |
|
3032 while (var_end < src_len && !IS_VAR_END_DELIMITER (src[var_end])) |
|
3033 var_end++; |
|
3034 |
|
3035 if (var_end == src_len) |
|
3036 { |
4396
|
3037 (*current_liboctave_warning_handler) |
|
3038 ("%s: No matching } for ${", src.c_str ()); |
4391
|
3039 i = var_end - 1; /* will incr to eos at top of loop */ |
|
3040 } |
|
3041 else |
|
3042 { |
|
3043 expand (expansion, src.substr (i, var_end - i)); |
|
3044 i = var_end; /* will incr past } at top of loop*/ |
|
3045 } |
|
3046 } |
|
3047 else |
|
3048 { |
|
3049 /* $<something-else>: error. */ |
4396
|
3050 (*current_liboctave_warning_handler) |
|
3051 ("%s: Unrecognized variable construct `$%c'", |
|
3052 src.c_str (), src[i]); |
|
3053 |
4391
|
3054 /* Just ignore those chars and keep going. */ |
|
3055 } |
|
3056 } |
|
3057 else |
|
3058 expansion += src[i]; |
|
3059 } |
4389
|
3060 |
|
3061 return expansion; |
4385
|
3062 } |