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 |
4389
|
1153 assert (! name.empty ()); |
4391
|
1154 |
4378
|
1155 /* If no leading tilde, do nothing. */ |
4391
|
1156 if (name[0] != '~') |
|
1157 { |
|
1158 expansion = name; |
|
1159 |
|
1160 /* If a bare tilde, return the home directory or `.'. (Very |
|
1161 unlikely that the directory name will do anyone any good, but |
|
1162 ... */ |
|
1163 } |
|
1164 else if (name.length () == 1) |
|
1165 { |
|
1166 expansion = octave_env::getenv ("HOME"); |
|
1167 |
|
1168 if (expansion.empty ()) |
|
1169 expansion = "."; |
|
1170 |
|
1171 /* If `~/', remove any trailing / or replace leading // in $HOME. |
|
1172 Should really check for doubled intermediate slashes, too. */ |
4378
|
1173 } |
4391
|
1174 else if (IS_DIR_SEP (name[1])) |
|
1175 { |
|
1176 unsigned c = 1; |
|
1177 std::string home = octave_env::getenv ("HOME"); |
|
1178 |
|
1179 if (home.empty ()) |
|
1180 home = "."; |
|
1181 |
|
1182 size_t home_len = home.length (); |
|
1183 |
|
1184 /* handle leading // */ |
|
1185 if (home_len > 1 && IS_DIR_SEP (home[0]) && IS_DIR_SEP (home[1])) |
|
1186 home = home.substr (1); |
|
1187 |
|
1188 /* omit / after ~ */ |
|
1189 if (IS_DIR_SEP (home[home_len - 1])) |
|
1190 c++; |
|
1191 |
|
1192 expansion = home + name.substr (c); |
|
1193 |
|
1194 /* If `~user' or `~user/', look up user in the passwd database (but |
|
1195 OS/2 doesn't have this concept. */ |
4378
|
1196 } |
4391
|
1197 else |
4378
|
1198 #ifdef HAVE_PWD_H |
|
1199 { |
|
1200 unsigned c = 2; |
4391
|
1201 |
|
1202 /* find user name */ |
|
1203 while (name.length () > c && ! IS_DIR_SEP (name[c])) |
4378
|
1204 c++; |
4391
|
1205 |
|
1206 std::string user = name.substr (1, c-1); |
|
1207 |
4378
|
1208 /* We only need the cast here for (deficient) systems |
|
1209 which do not declare `getpwnam' in <pwd.h>. */ |
4391
|
1210 octave_passwd p = octave_passwd::getpwnam (user); |
4378
|
1211 |
|
1212 /* If no such user, just use `.'. */ |
4391
|
1213 std::string home = p ? p.dir () : std::string ("."); |
|
1214 |
|
1215 if (home.empty ()) |
|
1216 home = "."; |
|
1217 |
|
1218 /* handle leading // */ |
|
1219 if (home.length () > 1 && IS_DIR_SEP (home[0]) && IS_DIR_SEP (home[1])) |
|
1220 home = home.substr (1); |
|
1221 |
|
1222 /* If HOME ends in /, omit the / after ~user. */ |
|
1223 if (name.length () > c && IS_DIR_SEP (home[home.length () - 1])) |
|
1224 c++; |
|
1225 |
|
1226 expansion = name.length () > c ? home : home + name.substr (c); |
4378
|
1227 } |
|
1228 #else /* not HAVE_PWD_H */ |
4391
|
1229 expansion = name; |
4378
|
1230 #endif /* not HAVE_PWD_H */ |
|
1231 |
4389
|
1232 return expansion; |
4378
|
1233 } |
|
1234 |
|
1235 /* Do variable expansion first so ~${USER} works. (Besides, it's what the |
|
1236 shells do.) */ |
|
1237 |
4399
|
1238 /* Call kpse_var_expand and kpse_tilde_expand (in that order). Result |
|
1239 is always in fresh memory, even if no expansions were done. */ |
|
1240 |
|
1241 static std::string |
4389
|
1242 kpse_expand (const std::string& s) |
4378
|
1243 { |
4389
|
1244 std::string var_expansion = kpse_var_expand (s); |
|
1245 return kpse_tilde_expand (var_expansion); |
4378
|
1246 } |
|
1247 |
|
1248 /* Forward declarations of functions from the original expand.c */ |
4397
|
1249 static string_vector brace_expand (const std::string&); |
4378
|
1250 |
|
1251 /* If $KPSE_DOT is defined in the environment, prepend it to any relative |
|
1252 path components. */ |
|
1253 |
4389
|
1254 static std::string |
|
1255 kpse_expand_kpse_dot (const std::string& path) |
4378
|
1256 { |
4389
|
1257 std::string ret; |
4391
|
1258 std::string kpse_dot = octave_env::getenv ("KPSE_DOT"); |
|
1259 |
|
1260 if (kpse_dot.empty ()) |
4378
|
1261 return path; |
|
1262 |
4394
|
1263 for (kpse_path_iterator pi (path); pi != NPOS; pi++) |
4391
|
1264 { |
4394
|
1265 std::string elt = *pi; |
|
1266 |
4391
|
1267 /* We assume that the !! magic is only used on absolute components. |
|
1268 Single "." get special treatment, as does "./" or its equivalent. */ |
|
1269 |
4394
|
1270 size_t elt_len = elt.length (); |
|
1271 |
|
1272 if (kpse_absolute_p (elt, false) |
|
1273 || (elt_len > 1 && elt[0] == '!' && elt[1] == '!')) |
|
1274 ret += elt + ENV_SEP_STRING; |
|
1275 else if (elt_len == 1 && elt[0] == '.') |
|
1276 ret += kpse_dot + ENV_SEP_STRING; |
|
1277 else if (elt_len > 1 && elt[0] == '.' && IS_DIR_SEP (elt[1])) |
|
1278 ret += kpse_dot + elt.substr (1) + ENV_SEP_STRING; |
4391
|
1279 else |
4394
|
1280 ret += kpse_dot + DIR_SEP_STRING + elt + ENV_SEP_STRING; |
4378
|
1281 } |
|
1282 |
4389
|
1283 int len = ret.length (); |
|
1284 if (len > 0) |
4395
|
1285 ret.resize (len-1); |
4389
|
1286 |
4378
|
1287 return ret; |
|
1288 } |
|
1289 |
|
1290 /* Do brace expansion on ELT; then do variable and ~ expansion on each |
|
1291 element of the result; then do brace expansion again, in case a |
|
1292 variable definition contained braces (e.g., $TEXMF). Return a |
|
1293 string comprising all of the results separated by ENV_SEP_STRING. */ |
|
1294 |
4389
|
1295 static std::string |
4394
|
1296 kpse_brace_expand_element (const std::string& elt) |
4378
|
1297 { |
4389
|
1298 std::string ret; |
4378
|
1299 |
4397
|
1300 string_vector expansions = brace_expand (elt); |
|
1301 |
|
1302 for (int i = 0; i < expansions.length (); i++) |
4391
|
1303 { |
|
1304 /* Do $ and ~ expansion on each element. */ |
|
1305 std::string x = kpse_expand (expansions[i]); |
|
1306 |
|
1307 if (x != expansions[i]) |
|
1308 { |
|
1309 /* If we did any expansions, do brace expansion again. Since |
|
1310 recursive variable definitions are not allowed, this recursion |
|
1311 must terminate. (In practice, it's unlikely there will ever be |
|
1312 more than one level of recursion.) */ |
4394
|
1313 x = kpse_brace_expand_element (x); |
4391
|
1314 } |
|
1315 |
|
1316 ret += x + ENV_SEP_STRING; |
4378
|
1317 } |
|
1318 |
4389
|
1319 ret.resize (ret.length () - 1); |
4391
|
1320 |
4378
|
1321 return ret; |
|
1322 } |
|
1323 |
4399
|
1324 /* Do brace expansion and call `kpse_expand' on each element of the |
|
1325 result; return the final expansion (always in fresh memory, even if |
|
1326 no expansions were done). We don't call `kpse_expand_default' |
|
1327 because there is a whole sequence of defaults to run through; see |
|
1328 `kpse_init_format'. */ |
|
1329 |
|
1330 static std::string |
4397
|
1331 kpse_brace_expand (const std::string& path) |
4378
|
1332 { |
|
1333 /* Must do variable expansion first because if we have |
|
1334 foo = .:~ |
|
1335 TEXINPUTS = $foo |
|
1336 we want to end up with TEXINPUTS = .:/home/karl. |
|
1337 Since kpse_path_element is not reentrant, we must get all |
|
1338 the path elements before we start the loop. */ |
4389
|
1339 std::string tmp = kpse_var_expand (path); |
4394
|
1340 |
4389
|
1341 std::string ret; |
4378
|
1342 |
4394
|
1343 for (kpse_path_iterator pi (tmp); pi != NPOS; pi++) |
4391
|
1344 { |
4394
|
1345 std::string elt = *pi; |
|
1346 |
4391
|
1347 /* Do brace expansion first, so tilde expansion happens in {~ka,~kb}. */ |
|
1348 std::string expansion = kpse_brace_expand_element (elt); |
|
1349 ret += expansion + ENV_SEP_STRING; |
|
1350 } |
4378
|
1351 |
4394
|
1352 size_t len = ret.length (); |
4389
|
1353 if (len > 0) |
4395
|
1354 ret.resize (len-1); |
4389
|
1355 |
|
1356 return kpse_expand_kpse_dot (ret); |
4378
|
1357 } |
4392
|
1358 |
4378
|
1359 /* Expand all special constructs in a path, and include only the actually |
|
1360 existing directories in the result. */ |
4399
|
1361 |
|
1362 /* Do brace expansion and call `kpse_expand' on each argument of the |
|
1363 result, then expand any `//' constructs. The final expansion (always |
|
1364 in fresh memory) is a path of all the existing directories that match |
|
1365 the pattern. */ |
|
1366 |
|
1367 static std::string |
4397
|
1368 kpse_path_expand (const std::string& path) |
4378
|
1369 { |
4392
|
1370 std::string ret; |
4378
|
1371 unsigned len; |
|
1372 |
|
1373 len = 0; |
4391
|
1374 |
4378
|
1375 /* Expand variables and braces first. */ |
4389
|
1376 std::string tmp = kpse_brace_expand (path); |
4392
|
1377 |
4378
|
1378 /* Now expand each of the path elements, printing the results */ |
4394
|
1379 for (kpse_path_iterator pi (tmp); pi != NPOS; pi++) |
4391
|
1380 { |
4394
|
1381 std::string elt = *pi; |
|
1382 |
4391
|
1383 str_llist_type *dirs; |
|
1384 |
|
1385 /* Skip and ignore magic leading chars. */ |
4394
|
1386 if (elt.length () > 1 && elt[0] == '!' && elt[1] == '!') |
|
1387 elt = elt.substr (2); |
4391
|
1388 |
|
1389 /* Do not touch the device if present */ |
|
1390 if (NAME_BEGINS_WITH_DEVICE (elt)) |
|
1391 { |
4394
|
1392 while (elt.length () > 3 |
|
1393 && IS_DIR_SEP (elt[2]) && IS_DIR_SEP (elt[3])) |
4391
|
1394 { |
4394
|
1395 elt[2] = elt[1]; |
|
1396 elt[1] = elt[0]; |
|
1397 elt = elt.substr (1); |
4391
|
1398 } |
|
1399 } |
|
1400 else |
|
1401 { |
|
1402 /* We never want to search the whole disk. */ |
4394
|
1403 while (elt.length () > 1 |
|
1404 && IS_DIR_SEP (elt[0]) && IS_DIR_SEP (elt[1])) |
|
1405 elt = elt.substr (1); |
4391
|
1406 } |
4378
|
1407 |
4394
|
1408 /* Search the disk for all dirs in the component specified. |
|
1409 Be faster to check the database, but this is more reliable. */ |
|
1410 dirs = kpse_element_dirs (elt); |
|
1411 |
|
1412 if (dirs && *dirs) |
|
1413 { |
|
1414 str_llist_elt_type *dir; |
|
1415 |
|
1416 for (dir = *dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
|
1417 { |
|
1418 const std::string thedir = STR_LLIST (*dir); |
|
1419 unsigned dirlen = thedir.length (); |
|
1420 |
4395
|
1421 ret += thedir; |
|
1422 len += dirlen; |
|
1423 |
4394
|
1424 /* Retain trailing slash if that's the root directory. */ |
|
1425 if (dirlen == 1 |
|
1426 || (dirlen == 3 && NAME_BEGINS_WITH_DEVICE (thedir) |
|
1427 && IS_DIR_SEP (thedir[2]))) |
|
1428 { |
4395
|
1429 ret += ENV_SEP_STRING; |
|
1430 len++; |
4394
|
1431 } |
4395
|
1432 |
|
1433 ret[len-1] = ENV_SEP; |
4394
|
1434 } |
|
1435 } |
4378
|
1436 } |
4391
|
1437 |
4395
|
1438 if (len > 0) |
|
1439 ret.resize (len-1); |
4391
|
1440 |
4378
|
1441 return ret; |
|
1442 } |
4392
|
1443 |
4378
|
1444 /* braces.c -- code for doing word expansion in curly braces. Taken from |
|
1445 bash 1.14.5. [Ans subsequently modified for kpatshea.] |
|
1446 |
|
1447 Copyright (C) 1987,1991 Free Software Foundation, Inc. |
|
1448 |
|
1449 This program is free software; you can redistribute it and/or modify it |
|
1450 under the terms of the GNU General Public License as published by |
|
1451 the Free Software Foundation; either version 1, or (at your option) |
|
1452 any later version. |
|
1453 |
|
1454 This program is distributed in the hope that it will be useful, but |
|
1455 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
1456 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
1457 General Public License for more details. |
|
1458 |
|
1459 You should have received a copy of the GNU General Public License |
|
1460 along with this program; see the file COPYING. If not, write to the |
|
1461 Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
|
1462 MA 02111-1307, USA. */ |
|
1463 |
4391
|
1464 #define brace_whitespace(c) (! (c) || (c) == ' ' || (c) == '\t' || (c) == '\n') |
4378
|
1465 |
|
1466 /* Basic idea: |
|
1467 |
|
1468 Segregate the text into 3 sections: preamble (stuff before an open brace), |
|
1469 postamble (stuff after the matching close brace) and amble (stuff after |
|
1470 preamble, and before postamble). Expand amble, and then tack on the |
|
1471 expansions to preamble. Expand postamble, and tack on the expansions to |
4391
|
1472 the result so far. */ |
4378
|
1473 |
4397
|
1474 /* Return a new array of strings which is the result of appending each |
|
1475 string in ARR2 to each string in ARR1. The resultant array is |
|
1476 len (arr1) * len (arr2) long. For convenience, ARR1 (and its contents) |
|
1477 are free ()'ed. ARR1 can be NULL, in that case, a new version of ARR2 |
|
1478 is returned. */ |
|
1479 |
|
1480 static string_vector |
|
1481 array_concat (const string_vector& arr1, const string_vector& arr2) |
4378
|
1482 { |
4397
|
1483 string_vector result; |
|
1484 |
|
1485 if (arr1.empty ()) |
|
1486 result = arr2; |
|
1487 else if (arr2.empty ()) |
|
1488 result = arr1; |
|
1489 else |
|
1490 { |
|
1491 int len1 = arr1.length (); |
|
1492 int len2 = arr2.length (); |
|
1493 |
|
1494 result = string_vector (len1 * len2); |
|
1495 |
|
1496 int k = 0; |
|
1497 for (int i = 0; i < len2; i++) |
|
1498 for (int j = 0; j < len1; j++) |
|
1499 result[k++] = arr1[j] + arr2[i]; |
|
1500 } |
|
1501 |
|
1502 return result; |
4378
|
1503 } |
|
1504 |
4397
|
1505 static int brace_gobbler (const std::string&, int&, int); |
|
1506 static string_vector expand_amble (const std::string&); |
4378
|
1507 |
|
1508 /* Return an array of strings; the brace expansion of TEXT. */ |
4397
|
1509 static string_vector |
|
1510 brace_expand (const std::string& text) |
4378
|
1511 { |
|
1512 /* Find the text of the preamble. */ |
4397
|
1513 int i = 0; |
|
1514 int c = brace_gobbler (text, i, '{'); |
|
1515 |
|
1516 std::string preamble = text.substr (0, i); |
|
1517 |
|
1518 string_vector result = string_vector (preamble); |
|
1519 |
|
1520 if (c == '{') |
4378
|
1521 { |
4397
|
1522 /* Find the amble. This is the stuff inside this set of braces. */ |
|
1523 int start = ++i; |
|
1524 c = brace_gobbler (text, i, '}'); |
|
1525 |
|
1526 /* What if there isn't a matching close brace? */ |
|
1527 if (! c) |
|
1528 { |
|
1529 (*current_liboctave_warning_handler) |
|
1530 ("%s: Unmatched {", text.c_str ()); |
|
1531 |
|
1532 result = string_vector (text); |
|
1533 } |
|
1534 else |
|
1535 { |
|
1536 std::string amble = text.substr (start, i-start); |
|
1537 result = array_concat (result, expand_amble (amble)); |
|
1538 |
|
1539 std::string postamble = text.substr (i+1); |
|
1540 result = array_concat (result, brace_expand (postamble)); |
|
1541 } |
4378
|
1542 } |
|
1543 |
4397
|
1544 return result; |
4378
|
1545 } |
|
1546 |
4397
|
1547 /* The character which is used to separate arguments. */ |
|
1548 static int brace_arg_separator = ','; |
|
1549 |
4378
|
1550 /* Expand the text found inside of braces. We simply try to split the |
|
1551 text at BRACE_ARG_SEPARATORs into separate strings. We then brace |
|
1552 expand each slot which needs it, until there are no more slots which |
|
1553 need it. */ |
4397
|
1554 static string_vector |
|
1555 expand_amble (const std::string& text) |
4378
|
1556 { |
4397
|
1557 string_vector result; |
|
1558 |
|
1559 size_t text_len = text.length (); |
|
1560 size_t start; |
|
1561 int i, c; |
|
1562 |
|
1563 for (start = 0, i = 0, c = 1; c && start < text_len; start = ++i) |
4378
|
1564 { |
4397
|
1565 int i0 = i; |
|
1566 int c0 = brace_gobbler (text, i0, brace_arg_separator); |
|
1567 int i1 = i; |
|
1568 int c1 = brace_gobbler (text, i1, ENV_SEP); |
4378
|
1569 c = c0 | c1; |
|
1570 i = (i0 < i1 ? i0 : i1); |
|
1571 |
4397
|
1572 std::string tem = text.substr (start, i-start); |
|
1573 |
|
1574 string_vector partial = brace_expand (tem); |
|
1575 |
|
1576 if (result.empty ()) |
4378
|
1577 result = partial; |
|
1578 else |
4397
|
1579 result.append (partial); |
4378
|
1580 } |
4397
|
1581 |
|
1582 return result; |
4378
|
1583 } |
|
1584 |
|
1585 /* Start at INDEX, and skip characters in TEXT. Set INDEX to the |
|
1586 index of the character matching SATISFY. This understands about |
|
1587 quoting. Return the character that caused us to stop searching; |
|
1588 this is either the same as SATISFY, or 0. */ |
|
1589 static int |
4397
|
1590 brace_gobbler (const std::string& text, int& indx, int satisfy) |
4378
|
1591 { |
4397
|
1592 int c = 0, level = 0, quoted = 0, pass_next = 0; |
|
1593 |
|
1594 size_t text_len = text.length (); |
|
1595 |
|
1596 size_t i = indx; |
|
1597 |
|
1598 for (; i < text_len; i++) |
4378
|
1599 { |
4397
|
1600 c = text[i]; |
|
1601 |
4378
|
1602 if (pass_next) |
|
1603 { |
|
1604 pass_next = 0; |
|
1605 continue; |
|
1606 } |
|
1607 |
|
1608 /* A backslash escapes the next character. This allows backslash to |
|
1609 escape the quote character in a double-quoted string. */ |
|
1610 if (c == '\\' && (quoted == 0 || quoted == '"' || quoted == '`')) |
|
1611 { |
|
1612 pass_next = 1; |
|
1613 continue; |
|
1614 } |
|
1615 |
|
1616 if (quoted) |
|
1617 { |
|
1618 if (c == quoted) |
|
1619 quoted = 0; |
|
1620 continue; |
|
1621 } |
|
1622 |
|
1623 if (c == '"' || c == '\'' || c == '`') |
|
1624 { |
|
1625 quoted = c; |
|
1626 continue; |
|
1627 } |
4391
|
1628 |
4378
|
1629 if (c == satisfy && !level && !quoted) |
|
1630 { |
|
1631 /* We ignore an open brace surrounded by whitespace, and also |
|
1632 an open brace followed immediately by a close brace, that |
|
1633 was preceded with whitespace. */ |
|
1634 if (c == '{' && |
4397
|
1635 ((i == 0 || brace_whitespace (text[i-1])) && |
|
1636 (i+1 < text_len && |
|
1637 (brace_whitespace (text[i+1]) || text[i+1] == '}')))) |
4378
|
1638 continue; |
|
1639 /* If this is being compiled as part of bash, ignore the `{' |
|
1640 in a `${}' construct */ |
4397
|
1641 if ((c != '{') || i == 0 || (text[i-1] != '$')) |
4378
|
1642 break; |
|
1643 } |
|
1644 |
|
1645 if (c == '{') |
|
1646 level++; |
|
1647 else if (c == '}' && level) |
|
1648 level--; |
|
1649 } |
|
1650 |
4397
|
1651 indx = i; |
|
1652 return c; |
4378
|
1653 } |
|
1654 |
4399
|
1655 /* An external database to avoid filesystem lookups. */ |
4378
|
1656 |
|
1657 #ifndef DEFAULT_TEXMFDBS |
|
1658 #define DEFAULT_TEXMFDBS "/usr/local/share/texmf:/var/tmp/texfonts" |
|
1659 #endif |
|
1660 |
|
1661 /* For each file format, we record the following information. The main |
|
1662 thing that is not part of this structure is the environment variable |
|
1663 lists. They are used directly in tex-file.c. We could incorporate |
|
1664 them here, but it would complicate the code a bit. We could also do |
|
1665 it via variable expansion, but not now, maybe not ever: |
|
1666 ${PKFONTS-${TEXFONTS-/usr/local/lib/texmf/fonts//}}. */ |
|
1667 |
4399
|
1668 struct kpse_format_info_type |
4378
|
1669 { |
4390
|
1670 std::string type; /* Human-readable description. */ |
|
1671 std::string path; /* The search path to use. */ |
|
1672 std::string raw_path; /* Pre-$~ (but post-default) expansion. */ |
|
1673 std::string path_source; /* Where the path started from. */ |
|
1674 std::string override_path; /* From client environment variable. */ |
|
1675 std::string client_path; /* E.g., from dvips's config.ps. */ |
|
1676 std::string cnf_path; /* From texmf.cnf. */ |
|
1677 std::string default_path; /* If all else fails. */ |
4394
|
1678 string_vector suffix; /* For kpse_find_file to check for/append. */ |
4399
|
1679 }; |
4378
|
1680 |
|
1681 /* The sole variable of that type, indexed by `kpse_file_format_type'. |
|
1682 Initialized by calls to `kpse_find_file' for `kpse_init_format'. */ |
|
1683 static kpse_format_info_type kpse_format_info; |
|
1684 |
|
1685 #define DB_ENVS "TEXMFDBS" |
4392
|
1686 |
4378
|
1687 /* And EXPAND_DEFAULT calls kpse_expand_default on try_path and the |
|
1688 present info->path. */ |
4396
|
1689 #define EXPAND_DEFAULT(try_path, source_string) \ |
|
1690 do \ |
|
1691 { \ |
|
1692 if (! try_path.empty ()) \ |
|
1693 { \ |
|
1694 info.raw_path = try_path; \ |
|
1695 info.path = kpse_expand_default (try_path, info.path); \ |
|
1696 info.path_source = source_string; \ |
|
1697 } \ |
|
1698 } \ |
|
1699 while (0) |
4378
|
1700 |
|
1701 static hash_table_type db; /* The hash table for all the ls-R's. */ |
4928
|
1702 |
4378
|
1703 /* SMALL: The old size of the hash table was 7603, with the assumption |
|
1704 that a minimal ls-R bas about 3500 entries. But a typical ls-R will |
|
1705 be more like double that size. */ |
|
1706 #ifndef DB_HASH_SIZE |
|
1707 #define DB_HASH_SIZE 15991 |
|
1708 #endif |
|
1709 #ifndef DB_NAME |
|
1710 #define DB_NAME "ls-R" |
|
1711 #endif |
|
1712 |
|
1713 static hash_table_type alias_db; |
|
1714 #ifndef ALIAS_NAME |
|
1715 #define ALIAS_NAME "aliases" |
|
1716 #endif |
|
1717 #ifndef ALIAS_HASH_SIZE |
|
1718 #define ALIAS_HASH_SIZE 1009 |
|
1719 #endif |
|
1720 |
4390
|
1721 static string_vector db_dir_list; |
4392
|
1722 |
4378
|
1723 /* Return true if FILENAME could be in PATH_ELT, i.e., if the directory |
|
1724 part of FILENAME matches PATH_ELT. Have to consider // wildcards, but |
|
1725 $ and ~ expansion have already been done. */ |
4391
|
1726 |
4378
|
1727 static bool |
4390
|
1728 match (const std::string& filename_arg, const std::string& path_elt_arg) |
4378
|
1729 { |
4390
|
1730 const char *filename = filename_arg.c_str (); |
|
1731 const char *path_elt = path_elt_arg.c_str (); |
|
1732 |
4378
|
1733 const char *original_filename = filename; |
|
1734 bool matched = false; |
4391
|
1735 |
|
1736 for (; *filename && *path_elt; filename++, path_elt++) |
|
1737 { |
4394
|
1738 if (*filename == *path_elt) /* normal character match */ |
4391
|
1739 ; |
|
1740 |
|
1741 else if (IS_DIR_SEP (*path_elt) /* at // */ |
|
1742 && original_filename < filename && IS_DIR_SEP (path_elt[-1])) |
|
1743 { |
|
1744 while (IS_DIR_SEP (*path_elt)) |
|
1745 path_elt++; /* get past second and any subsequent /'s */ |
|
1746 |
|
1747 if (*path_elt == 0) |
|
1748 { |
|
1749 /* Trailing //, matches anything. We could make this |
|
1750 part of the other case, but it seems pointless to do |
|
1751 the extra work. */ |
|
1752 matched = true; |
|
1753 break; |
|
1754 } |
|
1755 else |
|
1756 { |
|
1757 /* Intermediate //, have to match rest of PATH_ELT. */ |
|
1758 for (; !matched && *filename; filename++) |
|
1759 { |
|
1760 /* Try matching at each possible character. */ |
4394
|
1761 if (IS_DIR_SEP (filename[-1]) && *filename == *path_elt) |
4391
|
1762 matched = match (filename, path_elt); |
|
1763 } |
|
1764 |
|
1765 /* Prevent filename++ when *filename='\0'. */ |
|
1766 break; |
|
1767 } |
|
1768 } |
|
1769 else |
|
1770 /* normal character nonmatch, quit */ |
|
1771 break; |
4378
|
1772 } |
|
1773 |
|
1774 /* If we've reached the end of PATH_ELT, check that we're at the last |
|
1775 component of FILENAME, we've matched. */ |
4391
|
1776 if (! matched && *path_elt == 0) |
|
1777 { |
|
1778 /* Probably PATH_ELT ended with `vf' or some such, and FILENAME |
|
1779 ends with `vf/ptmr.vf'. In that case, we'll be at a |
|
1780 directory separator. On the other hand, if PATH_ELT ended |
|
1781 with a / (as in `vf/'), FILENAME being the same `vf/ptmr.vf', |
|
1782 we'll be at the `p'. Upshot: if we're at a dir separator in |
|
1783 FILENAME, skip it. But if not, that's ok, as long as there |
|
1784 are no more dir separators. */ |
|
1785 |
|
1786 if (IS_DIR_SEP (*filename)) |
|
1787 filename++; |
|
1788 |
|
1789 while (*filename && !IS_DIR_SEP (*filename)) |
|
1790 filename++; |
|
1791 |
|
1792 matched = *filename == 0; |
|
1793 } |
|
1794 |
4378
|
1795 return matched; |
|
1796 } |
|
1797 |
|
1798 /* If DB_DIR is a prefix of PATH_ELT, return true; otherwise false. |
|
1799 That is, the question is whether to try the db for a file looked up |
|
1800 in PATH_ELT. If PATH_ELT == ".", for example, the answer is no. If |
|
1801 PATH_ELT == "/usr/local/lib/texmf/fonts//tfm", the answer is yes. |
4391
|
1802 |
4378
|
1803 In practice, ls-R is only needed for lengthy subdirectory |
|
1804 comparisons, but there's no gain to checking PATH_ELT to see if it is |
|
1805 a subdir match, since the only way to do that is to do a string |
|
1806 search in it, which is all we do anyway. */ |
4391
|
1807 |
4378
|
1808 static bool |
4390
|
1809 elt_in_db (const std::string& db_dir, const std::string& path_elt) |
4378
|
1810 { |
|
1811 bool found = false; |
|
1812 |
4390
|
1813 size_t db_dir_len = db_dir.length (); |
|
1814 size_t path_elt_len = path_elt.length (); |
|
1815 |
|
1816 size_t i = 0; |
|
1817 |
4394
|
1818 while (! found && db_dir[i] == path_elt[i]) |
4391
|
1819 { |
|
1820 i++; |
|
1821 /* If we've matched the entire db directory, it's good. */ |
|
1822 if (i == db_dir_len) |
|
1823 found = true; |
|
1824 |
4378
|
1825 /* If we've reached the end of PATH_ELT, but not the end of the db |
|
1826 directory, it's no good. */ |
4391
|
1827 else if (i == path_elt_len) |
|
1828 break; |
|
1829 } |
4378
|
1830 |
|
1831 return found; |
|
1832 } |
4392
|
1833 |
4378
|
1834 /* Avoid doing anything if this PATH_ELT is irrelevant to the databases. */ |
|
1835 |
4399
|
1836 /* Return list of matches for NAME in the ls-R file matching PATH_ELT. If |
|
1837 ALL is set, return (null-terminated list) of all matches, else just |
|
1838 the first. If no matches, return a pointer to an empty list. If no |
|
1839 databases can be read, or PATH_ELT is not in any of the databases, |
|
1840 return NULL. */ |
|
1841 |
|
1842 static string_vector |
4390
|
1843 kpse_db_search (const std::string& name_arg, |
4394
|
1844 const std::string& orig_path_elt, bool all) |
4378
|
1845 { |
|
1846 bool done; |
4390
|
1847 string_vector ret; |
|
1848 string_vector aliases; |
4378
|
1849 bool relevant = false; |
4390
|
1850 |
4394
|
1851 std::string name = name_arg; |
4391
|
1852 |
4378
|
1853 /* If we failed to build the database (or if this is the recursive |
|
1854 call to build the db path), quit. */ |
4398
|
1855 if (! db.buckets) |
4390
|
1856 return ret; |
4391
|
1857 |
4378
|
1858 /* When tex-glyph.c calls us looking for, e.g., dpi600/cmr10.pk, we |
|
1859 won't find it unless we change NAME to just `cmr10.pk' and append |
|
1860 `/dpi600' to PATH_ELT. We are justified in using a literal `/' |
|
1861 here, since that's what tex-glyph.c unconditionally uses in |
|
1862 DPI_BITMAP_SPEC. But don't do anything if the / begins NAME; that |
|
1863 should never happen. */ |
4394
|
1864 std::string path_elt; |
|
1865 size_t last_slash = name.rfind ('/'); |
|
1866 if (last_slash != NPOS && last_slash != 0) |
4390
|
1867 { |
4394
|
1868 std::string dir_part = name.substr (0, last_slash); |
|
1869 name = name.substr (last_slash + 1); |
4390
|
1870 } |
|
1871 else |
4394
|
1872 path_elt = orig_path_elt; |
4378
|
1873 |
|
1874 /* Don't bother doing any lookups if this `path_elt' isn't covered by |
|
1875 any of database directories. We do this not so much because the |
|
1876 extra couple of hash lookups matter -- they don't -- but rather |
|
1877 because we want to return NULL in this case, so path_search can |
|
1878 know to do a disk search. */ |
4394
|
1879 for (int e = 0; ! relevant && e < db_dir_list.length (); e++) |
4390
|
1880 relevant = elt_in_db (db_dir_list[e], path_elt); |
|
1881 |
4391
|
1882 if (! relevant) |
4390
|
1883 return ret; |
4378
|
1884 |
|
1885 /* If we have aliases for this name, use them. */ |
|
1886 if (alias_db.buckets) |
|
1887 aliases = hash_lookup (alias_db, name); |
|
1888 |
4390
|
1889 /* Push aliases up by one and insert the original name at the front. */ |
|
1890 int len = aliases.length (); |
|
1891 aliases.resize (len+1); |
|
1892 for (int i = len; i > 0; i--) |
|
1893 aliases[i] = aliases[i - 1]; |
|
1894 aliases[0] = name; |
4378
|
1895 |
|
1896 done = false; |
4390
|
1897 len = aliases.length (); |
|
1898 for (int i = 0; i < len && !done; i++) |
|
1899 { |
|
1900 std::string atry = aliases[i]; |
|
1901 |
|
1902 /* We have an ls-R db. Look up `atry'. */ |
|
1903 string_vector db_dirs = hash_lookup (db, atry); |
|
1904 |
|
1905 /* For each filename found, see if it matches the path element. For |
|
1906 example, if we have .../cx/cmr10.300pk and .../ricoh/cmr10.300pk, |
|
1907 and the path looks like .../cx, we don't want the ricoh file. */ |
|
1908 |
|
1909 int db_dirs_len = db_dirs.length (); |
|
1910 for (int j = 0; j < db_dirs_len && !done; j++) |
|
1911 { |
|
1912 std::string db_file = db_dirs[j] + atry; |
|
1913 bool matched = match (db_file, path_elt); |
4378
|
1914 |
|
1915 #ifdef KPSE_DEBUG |
4390
|
1916 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
4394
|
1917 DEBUGF3 ("db:match (%s,%s) = %d\n", db_file.c_str (), path_elt.c_str (), matched); |
4378
|
1918 #endif |
|
1919 |
4390
|
1920 /* We got a hit in the database. Now see if the file actually |
|
1921 exists, possibly under an alias. */ |
|
1922 if (matched) |
|
1923 { |
|
1924 std::string found; |
4393
|
1925 std::string tmp = kpse_readable_file (db_file); |
|
1926 if (! tmp.empty ()) |
4390
|
1927 found = db_file; |
|
1928 else |
|
1929 { |
|
1930 /* The hit in the DB doesn't exist in disk. Now try |
|
1931 all its aliases. For example, suppose we have a |
|
1932 hierarchy on CD, thus `mf.bas', but ls-R contains |
|
1933 `mf.base'. Find it anyway. Could probably work |
|
1934 around this with aliases, but this is pretty easy |
|
1935 and shouldn't hurt. The upshot is that if one of |
|
1936 the aliases actually exists, we use that. */ |
|
1937 |
|
1938 int aliases_len = aliases.length (); |
|
1939 |
|
1940 for (int k = 1; k < aliases_len && found.empty (); k++) |
|
1941 { |
4587
|
1942 std::string aatry = db_dirs[j] + aliases[k]; |
|
1943 tmp = kpse_readable_file (aatry); |
4393
|
1944 if (! tmp.empty ()) |
4587
|
1945 found = aatry; |
4390
|
1946 } |
|
1947 } |
4391
|
1948 |
4390
|
1949 /* If we have a real file, add it to the list, maybe done. */ |
|
1950 if (! found.empty ()) |
|
1951 { |
4392
|
1952 ret.append (found); |
|
1953 |
4390
|
1954 if (! (all || found.empty ())) |
|
1955 done = true; |
|
1956 } |
|
1957 } |
|
1958 } |
4378
|
1959 } |
4391
|
1960 |
4378
|
1961 return ret; |
|
1962 } |
|
1963 |
4399
|
1964 /* Expand extra colons. */ |
4378
|
1965 |
|
1966 /* Check for leading colon first, then trailing, then doubled, since |
|
1967 that is fastest. Usually it will be leading or trailing. */ |
|
1968 |
4399
|
1969 /* Replace a leading or trailing or doubled : in PATH with DFLT. If |
|
1970 no extra colons, return PATH. Only one extra colon is replaced. |
|
1971 DFLT may not be NULL. */ |
|
1972 |
|
1973 static std::string |
4394
|
1974 kpse_expand_default (const std::string& path, const std::string& fallback) |
4378
|
1975 { |
4394
|
1976 std::string expansion; |
|
1977 |
|
1978 size_t path_len = path.length (); |
|
1979 |
|
1980 if (path_len == 0) |
|
1981 expansion = fallback; |
4378
|
1982 |
|
1983 /* Solitary or leading :? */ |
4394
|
1984 else if (IS_ENV_SEP (path[0])) |
4378
|
1985 { |
4394
|
1986 expansion = path_len == 1 ? fallback : fallback + path; |
4378
|
1987 } |
|
1988 |
|
1989 /* Sorry about the assignment in the middle of the expression, but |
|
1990 conventions were made to be flouted and all that. I don't see the |
|
1991 point of calling strlen twice or complicating the logic just to |
|
1992 avoid the assignment (especially now that I've pointed it out at |
|
1993 such great length). */ |
4394
|
1994 else if (IS_ENV_SEP (path[path_len-1])) |
|
1995 expansion = path + fallback; |
4378
|
1996 |
|
1997 /* OK, not leading or trailing. Check for doubled. */ |
|
1998 else |
|
1999 { |
|
2000 /* What we'll return if we find none. */ |
4394
|
2001 expansion = path; |
|
2002 |
|
2003 for (size_t i = 0; i < path_len; i++) |
4378
|
2004 { |
4394
|
2005 if (i + 1 < path_len |
|
2006 && IS_ENV_SEP (path[i]) && IS_ENV_SEP (path[i+1])) |
|
2007 { |
|
2008 /* We have a doubled colon. */ |
4391
|
2009 |
4378
|
2010 /* Copy stuff up to and including the first colon. */ |
|
2011 /* Copy in FALLBACK, and then the rest of PATH. */ |
4394
|
2012 expansion = path.substr (0, i+1) + fallback + path.substr (i+1); |
4378
|
2013 |
|
2014 break; |
|
2015 } |
|
2016 } |
|
2017 } |
4391
|
2018 |
4378
|
2019 return expansion; |
|
2020 } |
|
2021 |
4399
|
2022 /* Translate a path element to its corresponding director{y,ies}. */ |
4378
|
2023 |
|
2024 /* To avoid giving prototypes for all the routines and then their real |
|
2025 definitions, we give all the subroutines first. The entry point is |
|
2026 the last routine in the file. */ |
4392
|
2027 |
4378
|
2028 /* Make a copy of DIR (unless it's null) and save it in L. Ensure that |
|
2029 DIR ends with a DIR_SEP for the benefit of later searches. */ |
|
2030 |
|
2031 static void |
4390
|
2032 dir_list_add (str_llist_type *l, const std::string& dir) |
4378
|
2033 { |
4390
|
2034 char last_char = dir[dir.length () - 1]; |
4407
|
2035 |
4390
|
2036 std::string saved_dir = dir; |
4407
|
2037 |
|
2038 if (! (IS_DIR_SEP (last_char) || IS_DEVICE_SEP (last_char))) |
4390
|
2039 saved_dir += DIR_SEP_STRING; |
4391
|
2040 |
4378
|
2041 str_llist_add (l, saved_dir); |
|
2042 } |
|
2043 |
4390
|
2044 /* Return true if FN is a directory or a symlink to a directory, |
|
2045 false if not. */ |
|
2046 |
|
2047 static bool |
|
2048 dir_p (const std::string& fn) |
|
2049 { |
|
2050 #ifdef WIN32 |
4391
|
2051 unsigned int fa = GetFileAttributes (fn.c_str ()); |
4390
|
2052 return (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY)); |
|
2053 #else |
|
2054 struct stat stats; |
|
2055 return stat (fn.c_str (), &stats) == 0 && S_ISDIR (stats.st_mode); |
|
2056 #endif |
|
2057 } |
4391
|
2058 |
4378
|
2059 /* If DIR is a directory, add it to the list L. */ |
|
2060 |
|
2061 static void |
4390
|
2062 checked_dir_list_add (str_llist_type *l, const std::string& dir) |
4378
|
2063 { |
|
2064 if (dir_p (dir)) |
|
2065 dir_list_add (l, dir); |
|
2066 } |
4392
|
2067 |
4378
|
2068 /* The cache. Typically, several paths have the same element; for |
|
2069 example, /usr/local/lib/texmf/fonts//. We don't want to compute the |
|
2070 expansion of such a thing more than once. Even though we also cache |
|
2071 the dir_links call, that's not enough -- without this path element |
|
2072 caching as well, the execution time doubles. */ |
|
2073 |
4398
|
2074 struct cache_entry |
4378
|
2075 { |
4398
|
2076 std::string key; |
4378
|
2077 str_llist_type *value; |
4398
|
2078 }; |
|
2079 |
|
2080 static cache_entry *the_cache = 0; |
4378
|
2081 static unsigned cache_length = 0; |
|
2082 |
|
2083 /* Associate KEY with VALUE. We implement the cache as a simple linear |
|
2084 list, since it's unlikely to ever be more than a dozen or so elements |
|
2085 long. We don't bother to check here if PATH has already been saved; |
|
2086 we always add it to our list. We copy KEY but not VALUE; not sure |
|
2087 that's right, but it seems to be all that's needed. */ |
|
2088 |
|
2089 static void |
4398
|
2090 cache (const std::string key, str_llist_type *value) |
4378
|
2091 { |
4398
|
2092 cache_entry *new_cache = new cache_entry [cache_length+1]; |
|
2093 |
4399
|
2094 for (unsigned i = 0; i < cache_length; i++) |
4398
|
2095 { |
|
2096 new_cache[i].key = the_cache[i].key; |
|
2097 new_cache[i].value = the_cache[i].value; |
|
2098 } |
|
2099 |
|
2100 delete [] the_cache; |
|
2101 |
|
2102 the_cache = new_cache; |
|
2103 |
|
2104 the_cache[cache_length].key = key; |
|
2105 the_cache[cache_length].value = value; |
|
2106 |
4378
|
2107 cache_length++; |
|
2108 } |
|
2109 |
|
2110 /* To retrieve, just check the list in order. */ |
|
2111 |
|
2112 static str_llist_type * |
4398
|
2113 cached (const std::string& key) |
4378
|
2114 { |
|
2115 unsigned p; |
4391
|
2116 |
4378
|
2117 for (p = 0; p < cache_length; p++) |
|
2118 { |
4398
|
2119 if (key == the_cache[p].key) |
4378
|
2120 return the_cache[p].value; |
|
2121 } |
4391
|
2122 |
4398
|
2123 return 0; |
4378
|
2124 } |
4392
|
2125 |
4378
|
2126 /* Handle the magic path constructs. */ |
|
2127 |
|
2128 /* Declare recursively called routine. */ |
4398
|
2129 static void expand_elt (str_llist_type *, const std::string&, unsigned); |
4378
|
2130 |
|
2131 /* POST is a pointer into the original element (which may no longer be |
|
2132 ELT) to just after the doubled DIR_SEP, perhaps to the null. Append |
|
2133 subdirectories of ELT (up to ELT_LENGTH, which must be a /) to |
|
2134 STR_LIST_PTR. */ |
|
2135 |
|
2136 #ifdef WIN32 |
4398
|
2137 |
4378
|
2138 /* Shared across recursive calls, it acts like a stack. */ |
4398
|
2139 static std::string dirname; |
|
2140 |
|
2141 #else /* WIN32 */ |
|
2142 |
|
2143 /* Return -1 if FN isn't a directory, else its number of links. |
|
2144 Duplicate the call to stat; no need to incur overhead of a function |
|
2145 call for that little bit of cleanliness. */ |
|
2146 |
|
2147 static int |
|
2148 dir_links (const std::string& fn) |
|
2149 { |
|
2150 std::map<std::string, long> link_table; |
|
2151 |
|
2152 long ret; |
|
2153 |
|
2154 if (link_table.find (fn) != link_table.end ()) |
|
2155 ret = link_table[fn]; |
|
2156 else |
|
2157 { |
|
2158 struct stat stats; |
|
2159 |
|
2160 ret = stat (fn.c_str (), &stats) == 0 && S_ISDIR (stats.st_mode) |
|
2161 ? stats.st_nlink : (unsigned) -1; |
|
2162 |
|
2163 link_table[fn] = ret; |
|
2164 |
|
2165 #ifdef KPSE_DEBUG |
|
2166 if (KPSE_DEBUG_P (KPSE_DEBUG_STAT)) |
|
2167 DEBUGF2 ("dir_links (%s) => %ld\n", fn.c_str (), ret); |
4378
|
2168 #endif |
4398
|
2169 } |
|
2170 |
|
2171 return ret; |
|
2172 } |
|
2173 |
|
2174 #endif /* WIN32 */ |
4378
|
2175 |
|
2176 static void |
4398
|
2177 do_subdir (str_llist_type *str_list_ptr, const std::string& elt, |
|
2178 unsigned elt_length, const std::string& post) |
4378
|
2179 { |
|
2180 #ifdef WIN32 |
|
2181 WIN32_FIND_DATA find_file_data; |
|
2182 HANDLE hnd; |
|
2183 int proceed; |
|
2184 #else |
|
2185 DIR *dir; |
|
2186 struct dirent *e; |
|
2187 #endif /* not WIN32 */ |
4389
|
2188 |
4398
|
2189 std::string name = elt.substr (0, elt_length); |
4389
|
2190 |
4378
|
2191 assert (IS_DIR_SEP (elt[elt_length - 1]) |
|
2192 || IS_DEVICE_SEP (elt[elt_length - 1])); |
4391
|
2193 |
4378
|
2194 #if defined (WIN32) |
4398
|
2195 |
|
2196 dirname = name + "/*.*"; /* "*.*" or "*" -- seems equivalent. */ |
|
2197 |
|
2198 hnd = FindFirstFile (dirname.c_str (), &find_file_data); |
4378
|
2199 |
4389
|
2200 if (hnd == INVALID_HANDLE_VALUE) |
4378
|
2201 return; |
|
2202 |
|
2203 /* Include top level before subdirectories, if nothing to match. */ |
4398
|
2204 if (post.empty ()) |
4390
|
2205 dir_list_add (str_list_ptr, name); |
4398
|
2206 else |
|
2207 { |
|
2208 /* If we do have something to match, see if it exists. For |
|
2209 example, POST might be `pk/ljfour', and they might have a |
|
2210 directory `$TEXMF/fonts/pk/ljfour' that we should find. */ |
|
2211 name += post; |
|
2212 expand_elt (str_list_ptr, name, elt_length); |
|
2213 name.resize (elt_length); |
|
2214 } |
|
2215 |
4378
|
2216 proceed = 1; |
4398
|
2217 |
4391
|
2218 while (proceed) |
|
2219 { |
|
2220 if (find_file_data.cFileName[0] != '.') |
|
2221 { |
|
2222 /* Construct the potential subdirectory name. */ |
|
2223 name += find_file_data.cFileName; |
4398
|
2224 |
4391
|
2225 if (find_file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
|
2226 { |
|
2227 /* It's a directory, so append the separator. */ |
|
2228 name += DIR_SEP_STRING; |
|
2229 unsigned potential_len = name.length (); |
4398
|
2230 |
|
2231 do_subdir (str_list_ptr, name, potential_len, post); |
4391
|
2232 } |
|
2233 name.resize (elt_length); |
|
2234 } |
4398
|
2235 |
4391
|
2236 proceed = FindNextFile (hnd, &find_file_data); |
4378
|
2237 } |
4398
|
2238 |
4391
|
2239 FindClose (hnd); |
4378
|
2240 |
|
2241 #else /* not WIN32 */ |
|
2242 |
|
2243 /* If we can't open it, quit. */ |
4389
|
2244 dir = opendir (name.c_str ()); |
4398
|
2245 |
|
2246 if (! dir) |
4389
|
2247 return; |
4391
|
2248 |
4378
|
2249 /* Include top level before subdirectories, if nothing to match. */ |
4398
|
2250 if (post.empty ()) |
4390
|
2251 dir_list_add (str_list_ptr, name); |
4378
|
2252 else |
4398
|
2253 { |
|
2254 /* If we do have something to match, see if it exists. For |
4378
|
2255 example, POST might be `pk/ljfour', and they might have a |
|
2256 directory `$TEXMF/fonts/pk/ljfour' that we should find. */ |
4389
|
2257 name += post; |
4398
|
2258 expand_elt (str_list_ptr, name, elt_length); |
4389
|
2259 name.resize (elt_length); |
4378
|
2260 } |
|
2261 |
4398
|
2262 while ((e = readdir (dir))) |
|
2263 { |
|
2264 /* If it begins with a `.', never mind. (This allows ``hidden'' |
4378
|
2265 directories that the algorithm won't find.) */ |
4398
|
2266 |
4378
|
2267 if (e->d_name[0] != '.') |
|
2268 { |
|
2269 int links; |
4391
|
2270 |
4378
|
2271 /* Construct the potential subdirectory name. */ |
4389
|
2272 name += e->d_name; |
4391
|
2273 |
4378
|
2274 /* If we can't stat it, or if it isn't a directory, continue. */ |
4398
|
2275 links = dir_links (name); |
4378
|
2276 |
|
2277 if (links >= 0) |
4391
|
2278 { |
4378
|
2279 /* It's a directory, so append the separator. */ |
4389
|
2280 name += DIR_SEP_STRING; |
|
2281 unsigned potential_len = name.length (); |
4391
|
2282 |
4378
|
2283 /* Should we recurse? To see if the subdirectory is a |
|
2284 leaf, check if it has two links (one for . and one for |
|
2285 ..). This means that symbolic links to directories do |
|
2286 not affect the leaf-ness. This is arguably wrong, but |
|
2287 the only alternative I know of is to stat every entry |
|
2288 in the directory, and that is unacceptably slow. |
4391
|
2289 |
4378
|
2290 The #ifdef here makes all this configurable at |
|
2291 compile-time, so that if we're using VMS directories or |
|
2292 some such, we can still find subdirectories, even if it |
|
2293 is much slower. */ |
|
2294 #ifdef ST_NLINK_TRICK |
|
2295 if (links > 2) |
|
2296 #endif /* not ST_NLINK_TRICK */ |
|
2297 /* All criteria are met; find subdirectories. */ |
4398
|
2298 do_subdir (str_list_ptr, name, potential_len, post); |
4378
|
2299 #ifdef ST_NLINK_TRICK |
4398
|
2300 else if (post.empty ()) |
4378
|
2301 /* Nothing to match, no recursive subdirectories to |
|
2302 look for: we're done with this branch. Add it. */ |
4390
|
2303 dir_list_add (str_list_ptr, name); |
4378
|
2304 #endif |
|
2305 } |
|
2306 |
|
2307 /* Remove the directory entry we just checked from `name'. */ |
4389
|
2308 name.resize (elt_length); |
4378
|
2309 } |
|
2310 } |
4391
|
2311 |
4378
|
2312 xclosedir (dir); |
|
2313 #endif /* not WIN32 */ |
|
2314 } |
|
2315 |
|
2316 /* Assume ELT is non-empty and non-NULL. Return list of corresponding |
|
2317 directories (with no terminating NULL entry) in STR_LIST_PTR. Start |
|
2318 looking for magic constructs at START. */ |
|
2319 |
|
2320 static void |
4398
|
2321 expand_elt (str_llist_type *str_list_ptr, const std::string& elt, |
|
2322 unsigned start) |
4378
|
2323 { |
4398
|
2324 size_t elt_len = elt.length (); |
|
2325 |
|
2326 size_t dir = start; |
|
2327 |
|
2328 |
|
2329 while (dir < elt_len) |
4378
|
2330 { |
4398
|
2331 if (IS_DIR_SEP (elt[dir])) |
4378
|
2332 { |
|
2333 /* If two or more consecutive /'s, find subdirectories. */ |
4398
|
2334 if (++dir < elt_len && IS_DIR_SEP (elt[dir])) |
4378
|
2335 { |
4398
|
2336 size_t i = dir; |
|
2337 while (i < elt_len && IS_DIR_SEP (elt[i])) |
|
2338 i++; |
|
2339 |
|
2340 std::string post = elt.substr (i); |
|
2341 |
|
2342 do_subdir (str_list_ptr, elt, dir, post); |
|
2343 |
4378
|
2344 return; |
|
2345 } |
|
2346 |
|
2347 /* No special stuff at this slash. Keep going. */ |
|
2348 } |
4398
|
2349 else |
|
2350 dir++; |
4378
|
2351 } |
4391
|
2352 |
4378
|
2353 /* When we reach the end of ELT, it will be a normal filename. */ |
|
2354 checked_dir_list_add (str_list_ptr, elt); |
|
2355 } |
4392
|
2356 |
4378
|
2357 /* Here is the entry point. Returns directory list for ELT. */ |
|
2358 |
4399
|
2359 /* Given a path element ELT, return a pointer to a NULL-terminated list |
|
2360 of the corresponding (existing) directory or directories, with |
|
2361 trailing slashes, or NULL. If ELT is the empty string, check the |
|
2362 current working directory. |
|
2363 |
|
2364 It's up to the caller to expand ELT. This is because this routine is |
|
2365 most likely only useful to be called from `kpse_path_search', which |
|
2366 has already assumed expansion has been done. */ |
|
2367 |
|
2368 static str_llist_type * |
4398
|
2369 kpse_element_dirs (const std::string& elt) |
4378
|
2370 { |
|
2371 str_llist_type *ret; |
|
2372 |
|
2373 /* If given nothing, return nothing. */ |
4398
|
2374 if (elt.empty ()) |
|
2375 return 0; |
4378
|
2376 |
|
2377 /* If we've already cached the answer for ELT, return it. */ |
|
2378 ret = cached (elt); |
|
2379 if (ret) |
|
2380 return ret; |
|
2381 |
|
2382 /* We're going to have a real directory list to return. */ |
4390
|
2383 ret = new str_llist_type; |
4398
|
2384 *ret = 0; |
4378
|
2385 |
|
2386 /* We handle the hard case in a subroutine. */ |
|
2387 expand_elt (ret, elt, 0); |
|
2388 |
|
2389 /* Remember the directory list we just found, in case future calls are |
|
2390 made with the same ELT. */ |
|
2391 cache (elt, ret); |
|
2392 |
|
2393 #ifdef KPSE_DEBUG |
|
2394 if (KPSE_DEBUG_P (KPSE_DEBUG_EXPAND)) |
|
2395 { |
4398
|
2396 DEBUGF1 ("path element %s =>", elt.c_str ()); |
4378
|
2397 if (ret) |
|
2398 { |
|
2399 str_llist_elt_type *e; |
|
2400 for (e = *ret; e; e = STR_LLIST_NEXT (*e)) |
4390
|
2401 fprintf (stderr, " %s", (STR_LLIST (*e)).c_str ()); |
4378
|
2402 } |
|
2403 putc ('\n', stderr); |
|
2404 fflush (stderr); |
|
2405 } |
|
2406 #endif /* KPSE_DEBUG */ |
|
2407 |
|
2408 return ret; |
|
2409 } |
|
2410 |
4386
|
2411 #ifndef WIN32 |
4385
|
2412 void |
|
2413 xclosedir (DIR *d) |
|
2414 { |
|
2415 #ifdef CLOSEDIR_VOID |
|
2416 closedir (d); |
|
2417 #else |
|
2418 int ret = closedir (d); |
4391
|
2419 |
4385
|
2420 if (ret != 0) |
|
2421 FATAL ("closedir failed"); |
|
2422 #endif |
|
2423 } |
4386
|
2424 #endif |
4385
|
2425 |
4399
|
2426 /* Help the user discover what's going on. */ |
4385
|
2427 |
|
2428 #ifdef KPSE_DEBUG |
|
2429 |
|
2430 /* If the real definitions of fopen or fclose are macros, we lose -- the |
|
2431 #undef won't restore them. */ |
|
2432 |
4399
|
2433 static FILE * |
4385
|
2434 fopen (const char *filename, const char *mode) |
|
2435 { |
|
2436 #undef fopen |
|
2437 FILE *ret = fopen (filename, mode); |
|
2438 |
|
2439 if (KPSE_DEBUG_P (KPSE_DEBUG_FOPEN)) |
4391
|
2440 DEBUGF3 ("fopen (%s, %s) => 0x%lx\n", filename, mode, (unsigned long) ret); |
4385
|
2441 |
|
2442 return ret; |
|
2443 } |
|
2444 |
|
2445 #endif |
|
2446 |
4399
|
2447 /* Implementation of a linked list of strings. */ |
4385
|
2448 |
|
2449 /* Add the new string STR to the end of the list L. */ |
|
2450 |
|
2451 void |
4390
|
2452 str_llist_add (str_llist_type *l, const std::string& str) |
4385
|
2453 { |
|
2454 str_llist_elt_type *e; |
4390
|
2455 str_llist_elt_type *new_elt = new str_llist_elt_type; |
4391
|
2456 |
4385
|
2457 /* The new element will be at the end of the list. */ |
|
2458 STR_LLIST (*new_elt) = str; |
|
2459 STR_LLIST_MOVED (*new_elt) = 0; |
4398
|
2460 STR_LLIST_NEXT (*new_elt) = 0; |
4391
|
2461 |
4385
|
2462 /* Find the current end of the list. */ |
|
2463 for (e = *l; e && STR_LLIST_NEXT (*e); e = STR_LLIST_NEXT (*e)) |
|
2464 ; |
4391
|
2465 |
|
2466 if (! e) |
4385
|
2467 *l = new_elt; |
|
2468 else |
|
2469 STR_LLIST_NEXT (*e) = new_elt; |
|
2470 } |
4392
|
2471 |
4385
|
2472 /* Move an element towards the top. The idea is that when a file is |
|
2473 found in a given directory, later files will likely be in that same |
|
2474 directory, and looking for the file in all the directories in between |
|
2475 is thus a waste. */ |
|
2476 |
|
2477 void |
|
2478 str_llist_float (str_llist_type *l, str_llist_elt_type *mover) |
|
2479 { |
|
2480 str_llist_elt_type *last_moved, *unmoved; |
4391
|
2481 |
4385
|
2482 /* If we've already moved this element, never mind. */ |
|
2483 if (STR_LLIST_MOVED (*mover)) |
|
2484 return; |
4391
|
2485 |
4385
|
2486 /* Find the first unmoved element (to insert before). We're |
|
2487 guaranteed this will terminate, since MOVER itself is currently |
|
2488 unmoved, and it must be in L (by hypothesis). */ |
4398
|
2489 for (last_moved = 0, unmoved = *l; STR_LLIST_MOVED (*unmoved); |
4385
|
2490 last_moved = unmoved, unmoved = STR_LLIST_NEXT (*unmoved)) |
|
2491 ; |
|
2492 |
|
2493 /* If we are the first unmoved element, nothing to relink. */ |
|
2494 if (unmoved != mover) |
|
2495 { /* Remember `mover's current successor, so we can relink `mover's |
|
2496 predecessor to it. */ |
|
2497 str_llist_elt_type *before_mover; |
|
2498 str_llist_elt_type *after_mover = STR_LLIST_NEXT (*mover); |
4391
|
2499 |
4385
|
2500 /* Find `mover's predecessor. */ |
|
2501 for (before_mover = unmoved; STR_LLIST_NEXT (*before_mover) != mover; |
|
2502 before_mover = STR_LLIST_NEXT (*before_mover)) |
|
2503 ; |
4391
|
2504 |
4385
|
2505 /* `before_mover' now links to `after_mover'. */ |
|
2506 STR_LLIST_NEXT (*before_mover) = after_mover; |
|
2507 |
|
2508 /* Insert `mover' before `unmoved' and after `last_moved' (or at |
|
2509 the head of the list). */ |
|
2510 STR_LLIST_NEXT (*mover) = unmoved; |
4391
|
2511 if (! last_moved) |
4385
|
2512 *l = mover; |
|
2513 else |
|
2514 STR_LLIST_NEXT (*last_moved) = mover; |
|
2515 } |
|
2516 |
|
2517 /* We've moved it. */ |
|
2518 STR_LLIST_MOVED (*mover) = 1; |
|
2519 } |
|
2520 |
4399
|
2521 /* Variable expansion. */ |
4385
|
2522 |
|
2523 /* We have to keep track of variables being expanded, otherwise |
|
2524 constructs like TEXINPUTS = $TEXINPUTS result in an infinite loop. |
|
2525 (Or indirectly recursive variables, etc.) Our simple solution is to |
|
2526 add to a list each time an expansion is started, and check the list |
|
2527 before expanding. */ |
|
2528 |
4391
|
2529 static std::map <std::string, bool> expansions; |
4385
|
2530 |
|
2531 static void |
4391
|
2532 expanding (const std::string& var, bool xp) |
4385
|
2533 { |
4391
|
2534 expansions[var] = xp; |
4385
|
2535 } |
|
2536 |
|
2537 /* Return whether VAR is currently being expanding. */ |
|
2538 |
4391
|
2539 static bool |
|
2540 expanding_p (const std::string& var) |
4385
|
2541 { |
4391
|
2542 return (expansions.find (var) != expansions.end ()) |
|
2543 ? expansions[var] : false; |
4385
|
2544 } |
4392
|
2545 |
4385
|
2546 /* Append the result of value of `var' to EXPANSION, where `var' begins |
|
2547 at START and ends at END. If `var' is not set, do not complain. |
|
2548 This is a subroutine for the more complicated expansion function. */ |
|
2549 |
|
2550 static void |
4391
|
2551 expand (std::string &expansion, const std::string& var) |
4385
|
2552 { |
4391
|
2553 if (expanding_p (var)) |
|
2554 { |
4396
|
2555 (*current_liboctave_warning_handler) |
|
2556 ("kpathsea: variable `%s' references itself (eventually)", |
|
2557 var.c_str ()); |
4385
|
2558 } |
4391
|
2559 else |
|
2560 { |
|
2561 /* Check for an environment variable. */ |
|
2562 std::string value = octave_env::getenv (var); |
|
2563 |
|
2564 if (! value.empty ()) |
|
2565 { |
|
2566 expanding (var, true); |
|
2567 std::string tmp = kpse_var_expand (value); |
|
2568 expanding (var, false); |
|
2569 expansion += tmp; |
|
2570 } |
|
2571 } |
4385
|
2572 } |
4392
|
2573 |
4385
|
2574 /* Can't think of when it would be useful to change these (and the |
|
2575 diagnostic messages assume them), but ... */ |
|
2576 #ifndef IS_VAR_START /* starts all variable references */ |
|
2577 #define IS_VAR_START(c) ((c) == '$') |
|
2578 #endif |
|
2579 #ifndef IS_VAR_CHAR /* variable name constituent */ |
|
2580 #define IS_VAR_CHAR(c) (isalnum (c) || (c) == '_') |
|
2581 #endif |
|
2582 #ifndef IS_VAR_BEGIN_DELIMITER /* start delimited variable name (after $) */ |
|
2583 #define IS_VAR_BEGIN_DELIMITER(c) ((c) == '{') |
|
2584 #endif |
|
2585 #ifndef IS_VAR_END_DELIMITER |
|
2586 #define IS_VAR_END_DELIMITER(c) ((c) == '}') |
|
2587 #endif |
|
2588 |
|
2589 /* Maybe we should support some or all of the various shell ${...} |
|
2590 constructs, especially ${var-value}. */ |
|
2591 |
4389
|
2592 std::string |
4391
|
2593 kpse_var_expand (const std::string& src) |
4385
|
2594 { |
4389
|
2595 std::string expansion; |
4391
|
2596 |
|
2597 size_t src_len = src.length (); |
|
2598 |
4385
|
2599 /* Copy everything but variable constructs. */ |
4391
|
2600 for (size_t i = 0; i < src_len; i++) |
|
2601 { |
|
2602 if (IS_VAR_START (src[i])) |
|
2603 { |
|
2604 i++; |
|
2605 |
|
2606 /* Three cases: `$VAR', `${VAR}', `$<anything-else>'. */ |
|
2607 if (IS_VAR_CHAR (src[i])) |
|
2608 { |
|
2609 /* $V: collect name constituents, then expand. */ |
|
2610 size_t var_end = i; |
|
2611 |
|
2612 do |
|
2613 { |
|
2614 var_end++; |
|
2615 } |
|
2616 while (IS_VAR_CHAR (src[var_end])); |
|
2617 |
|
2618 var_end--; /* had to go one past */ |
|
2619 expand (expansion, src.substr (i, var_end - i + 1)); |
|
2620 i = var_end; |
|
2621 |
|
2622 } |
|
2623 else if (IS_VAR_BEGIN_DELIMITER (src[i])) |
|
2624 { |
|
2625 /* ${: scan ahead for matching delimiter, then expand. */ |
|
2626 size_t var_end = ++i; |
|
2627 |
|
2628 while (var_end < src_len && !IS_VAR_END_DELIMITER (src[var_end])) |
|
2629 var_end++; |
|
2630 |
|
2631 if (var_end == src_len) |
|
2632 { |
4396
|
2633 (*current_liboctave_warning_handler) |
|
2634 ("%s: No matching } for ${", src.c_str ()); |
4391
|
2635 i = var_end - 1; /* will incr to eos at top of loop */ |
|
2636 } |
|
2637 else |
|
2638 { |
|
2639 expand (expansion, src.substr (i, var_end - i)); |
|
2640 i = var_end; /* will incr past } at top of loop*/ |
|
2641 } |
|
2642 } |
|
2643 else |
|
2644 { |
|
2645 /* $<something-else>: error. */ |
4396
|
2646 (*current_liboctave_warning_handler) |
|
2647 ("%s: Unrecognized variable construct `$%c'", |
|
2648 src.c_str (), src[i]); |
|
2649 |
4391
|
2650 /* Just ignore those chars and keep going. */ |
|
2651 } |
|
2652 } |
|
2653 else |
|
2654 expansion += src[i]; |
|
2655 } |
4389
|
2656 |
|
2657 return expansion; |
4385
|
2658 } |
4399
|
2659 |
|
2660 /* |
|
2661 ;;; Local Variables: *** |
|
2662 ;;; mode: C++ *** |
|
2663 ;;; End: *** |
|
2664 */ |