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