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