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