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