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