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