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