4378
|
1 /* pathsearch.c: look up a filename in a path. |
|
2 |
4385
|
3 Copyright (C) 1993, 94, 95, 96, 97, 98 Karl Berry. |
4378
|
4 Copyright (C) 1993, 94, 95, 96, 97 Karl Berry & O. Weber. |
4385
|
5 Copyright (C) 1992, 93, 94, 95, 96 Free Software Foundation, Inc. |
4378
|
6 |
|
7 This library is free software; you can redistribute it and/or |
|
8 modify it under the terms of the GNU Library General Public |
|
9 License as published by the Free Software Foundation; either |
|
10 version 2 of the License, or (at your option) any later version. |
|
11 |
|
12 This library is distributed in the hope that it will be useful, |
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 Library General Public License for more details. |
|
16 |
|
17 You should have received a copy of the GNU Library General Public |
|
18 License along with this library; if not, write to the Free Software |
|
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
|
20 |
|
21 #if defined (HAVE_CONFIG_H) |
|
22 #include <config.h> |
|
23 #endif |
|
24 |
4390
|
25 #include <map> |
4389
|
26 #include <string> |
|
27 |
4378
|
28 #include "kpse-config.h" |
4379
|
29 #include "kpse-xfns.h" |
4378
|
30 #include "kpse.h" |
|
31 |
4391
|
32 #include "oct-env.h" |
|
33 #include "oct-passwd.h" |
|
34 |
4385
|
35 /* c-std.h: the first header files. */ |
|
36 |
|
37 /* Header files that essentially all of our sources need, and |
|
38 that all implementations have. We include these first, to help with |
|
39 NULL being defined multiple times. */ |
|
40 #include <cstdio> |
|
41 #include <cstdarg> |
|
42 #include <cstdlib> |
|
43 #include <cstring> |
|
44 #include <climits> |
|
45 #include <cerrno> |
|
46 #include <cassert> |
|
47 |
|
48 #ifdef HAVE_UNISTD_H |
|
49 #ifdef HAVE_SYS_TYPES_H |
|
50 #include <sys/types.h> |
|
51 #endif |
|
52 #include <unistd.h> |
|
53 #endif |
|
54 |
|
55 #ifdef WIN32 |
|
56 #include <malloc.h> |
|
57 #endif /* not WIN32 */ |
|
58 |
|
59 #include "sysdir.h" |
|
60 #include "statdefs.h" |
|
61 |
|
62 /* define NAME_MAX, the maximum length of a single |
|
63 component in a filename. No such limit may exist, or may vary |
|
64 depending on the filesystem. */ |
|
65 |
|
66 /* Most likely the system will truncate filenames if it is not POSIX, |
|
67 and so we can use the BSD value here. */ |
|
68 #ifndef _POSIX_NAME_MAX |
|
69 #define _POSIX_NAME_MAX 255 |
|
70 #endif |
|
71 |
|
72 #ifndef NAME_MAX |
|
73 #define NAME_MAX _POSIX_NAME_MAX |
|
74 #endif |
|
75 |
|
76 /* c-ctype.h: ASCII-safe versions of the <ctype.h> macros. */ |
|
77 |
|
78 #include <cctype> |
|
79 |
|
80 /* What separates elements in environment variable path lists? */ |
|
81 #ifndef ENV_SEP |
|
82 #ifdef DOSISH |
|
83 #define ENV_SEP ';' |
|
84 #define ENV_SEP_STRING ";" |
|
85 #else |
|
86 #define ENV_SEP ':' |
|
87 #define ENV_SEP_STRING ":" |
|
88 #endif /* not DOS */ |
|
89 #endif /* not ENV_SEP */ |
|
90 |
|
91 #ifndef IS_ENV_SEP |
|
92 #define IS_ENV_SEP(ch) ((ch) == ENV_SEP) |
|
93 #endif |
|
94 |
|
95 /* c-pathmx.h: define PATH_MAX, the maximum length of a filename. |
|
96 Since no such limit may exist, it's preferable to dynamically grow |
|
97 filenames as needed. */ |
|
98 |
|
99 /* Cheat and define this as a manifest constant no matter what, instead |
|
100 of using pathconf. I forget why we want to do this. */ |
|
101 |
|
102 #ifndef _POSIX_PATH_MAX |
|
103 #define _POSIX_PATH_MAX 255 |
|
104 #endif |
|
105 |
|
106 #ifndef PATH_MAX |
|
107 #ifdef MAXPATHLEN |
|
108 #define PATH_MAX MAXPATHLEN |
|
109 #else |
|
110 #define PATH_MAX _POSIX_PATH_MAX |
|
111 #endif |
|
112 #endif /* not PATH_MAX */ |
|
113 |
|
114 /* debug.h: Runtime tracing. */ |
|
115 |
|
116 /* If NO_DEBUG is defined (not recommended), skip all this. */ |
|
117 #ifndef NO_DEBUG |
|
118 |
|
119 /* OK, we'll have tracing support. */ |
|
120 #define KPSE_DEBUG |
|
121 |
|
122 /* Set a bit. */ |
|
123 #define KPSE_DEBUG_SET(bit) kpathsea_debug |= 1 << (bit) |
|
124 |
|
125 /* Test if a bit is on. */ |
|
126 #define KPSE_DEBUG_P(bit) (kpathsea_debug & (1 << (bit))) |
|
127 |
|
128 #define KPSE_DEBUG_STAT 0 /* stat calls */ |
|
129 #define KPSE_DEBUG_HASH 1 /* hash lookups */ |
|
130 #define KPSE_DEBUG_FOPEN 2 /* fopen/fclose calls */ |
|
131 #define KPSE_DEBUG_PATHS 3 /* search path initializations */ |
|
132 #define KPSE_DEBUG_EXPAND 4 /* path element expansion */ |
|
133 #define KPSE_DEBUG_SEARCH 5 /* searches */ |
|
134 #define KPSE_DEBUG_VARS 6 /* variable values */ |
|
135 #define KPSE_LAST_DEBUG KPSE_DEBUG_VARS |
|
136 |
|
137 /* A printf for the debugging. */ |
|
138 #define DEBUGF_START() do { fputs ("kdebug:", stderr) |
|
139 #define DEBUGF_END() fflush (stderr); } while (0) |
|
140 |
|
141 #define DEBUGF(str) \ |
|
142 DEBUGF_START (); fputs (str, stderr); DEBUGF_END () |
|
143 #define DEBUGF1(str, e1) \ |
|
144 DEBUGF_START (); fprintf (stderr, str, e1); DEBUGF_END () |
|
145 #define DEBUGF2(str, e1, e2) \ |
|
146 DEBUGF_START (); fprintf (stderr, str, e1, e2); DEBUGF_END () |
|
147 #define DEBUGF3(str, e1, e2, e3) \ |
|
148 DEBUGF_START (); fprintf (stderr, str, e1, e2, e3); DEBUGF_END () |
|
149 #define DEBUGF4(str, e1, e2, e3, e4) \ |
|
150 DEBUGF_START (); fprintf (stderr, str, e1, e2, e3, e4); DEBUGF_END () |
|
151 |
|
152 #undef fopen |
|
153 #define fopen kpse_fopen_trace |
|
154 extern FILE *fopen (const char *filename, const char *mode); |
|
155 #undef fclose |
|
156 #define fclose kpse_fclose_trace |
|
157 extern int fclose (FILE *); |
|
158 |
|
159 #endif /* not NO_DEBUG */ |
|
160 |
|
161 #if defined (WIN32) && !defined (__MINGW32__) |
|
162 |
|
163 /* System description file for Windows NT. */ |
|
164 |
|
165 /* |
|
166 * Define symbols to identify the version of Unix this is. |
|
167 * Define all the symbols that apply correctly. |
|
168 */ |
|
169 |
|
170 #ifndef DOSISH |
|
171 #define DOSISH |
|
172 #endif |
|
173 |
|
174 #ifndef MAXPATHLEN |
|
175 #define MAXPATHLEN _MAX_PATH |
4378
|
176 #endif |
|
177 |
4385
|
178 #define HAVE_DUP2 1 |
|
179 #define HAVE_RENAME 1 |
|
180 #define HAVE_RMDIR 1 |
|
181 #define HAVE_MKDIR 1 |
|
182 #define HAVE_GETHOSTNAME 1 |
|
183 #define HAVE_RANDOM 1 |
|
184 #define USE_UTIME 1 |
|
185 #define HAVE_MOUSE 1 |
|
186 #define HAVE_TZNAME 1 |
|
187 |
|
188 /* These have to be defined because our compilers treat __STDC__ as being |
|
189 defined (most of them anyway). */ |
|
190 |
|
191 #define access _access |
|
192 #define stat _stat |
|
193 #define strcasecmp _stricmp |
|
194 #define strdup _strdup |
|
195 #define strncasecmp _strnicmp |
|
196 |
|
197 #define S_IFMT _S_IFMT |
|
198 #define S_IFDIR _S_IFDIR |
|
199 |
|
200 /* Define this so that winsock.h definitions don't get included when |
|
201 windows.h is... For this to have proper effect, config.h must |
4391
|
202 always be included before windows.h. */ |
4385
|
203 #define _WINSOCKAPI_ 1 |
|
204 |
|
205 #include <windows.h> |
|
206 |
|
207 /* Defines size_t and alloca (). */ |
|
208 #include <malloc.h> |
|
209 |
|
210 /* For proper declaration of environ. */ |
|
211 #include <io.h> |
|
212 #include <fcntl.h> |
|
213 #include <process.h> |
|
214 |
|
215 /* ============================================================ */ |
|
216 |
|
217 #endif /* WIN32 */ |
|
218 |
|
219 /* hash.h: declarations for a hash table. */ |
|
220 |
|
221 /* A single (key,value) pair. */ |
4390
|
222 struct hash_element_type |
4385
|
223 { |
4390
|
224 std::string key; |
|
225 std::string value; |
|
226 struct hash_element_type *next; |
|
227 }; |
4385
|
228 |
|
229 /* The usual arrangement of buckets initialized to null. */ |
4390
|
230 struct hash_table_type |
4385
|
231 { |
|
232 hash_element_type **buckets; |
|
233 unsigned size; |
4390
|
234 }; |
4385
|
235 |
|
236 static hash_table_type hash_create (unsigned size); |
|
237 |
|
238 |
|
239 /* lib.h: other stuff. */ |
|
240 |
|
241 /* Define common sorts of messages. */ |
|
242 |
|
243 /* This should be called only after a system call fails. Don't exit |
|
244 with status `errno', because that might be 256, which would mean |
|
245 success (exit statuses are truncated to eight bits). */ |
|
246 #define FATAL_PERROR(str) do { \ |
|
247 fputs ("pathsearch: ", stderr); \ |
|
248 perror (str); exit (EXIT_FAILURE); } while (0) |
|
249 |
|
250 #define START_FATAL() do { \ |
|
251 fputs ("pathsearch: fatal: ", stderr); |
|
252 #define END_FATAL() fputs (".\n", stderr); exit (1); } while (0) |
|
253 |
|
254 #define FATAL(str) \ |
|
255 START_FATAL (); fputs (str, stderr); END_FATAL () |
|
256 #define FATAL1(str, e1) \ |
|
257 START_FATAL (); fprintf (stderr, str, e1); END_FATAL () |
|
258 #define FATAL2(str, e1, e2) \ |
|
259 START_FATAL (); fprintf (stderr, str, e1, e2); END_FATAL () |
|
260 #define FATAL3(str, e1, e2, e3) \ |
|
261 START_FATAL (); fprintf (stderr, str, e1, e2, e3); END_FATAL () |
|
262 #define FATAL4(str, e1, e2, e3, e4) \ |
|
263 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4); END_FATAL () |
|
264 #define FATAL5(str, e1, e2, e3, e4, e5) \ |
|
265 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5); END_FATAL () |
|
266 #define FATAL6(str, e1, e2, e3, e4, e5, e6) \ |
|
267 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5, e6); END_FATAL () |
|
268 |
|
269 |
|
270 #define START_WARNING() do { fputs ("warning: ", stderr) |
|
271 #define END_WARNING() fputs (".\n", stderr); fflush (stderr); } while (0) |
|
272 |
|
273 #define WARNING(str) \ |
|
274 START_WARNING (); fputs (str, stderr); END_WARNING () |
|
275 #define WARNING1(str, e1) \ |
|
276 START_WARNING (); fprintf (stderr, str, e1); END_WARNING () |
|
277 #define WARNING2(str, e1, e2) \ |
|
278 START_WARNING (); fprintf (stderr, str, e1, e2); END_WARNING () |
|
279 #define WARNING3(str, e1, e2, e3) \ |
|
280 START_WARNING (); fprintf (stderr, str, e1, e2, e3); END_WARNING () |
|
281 #define WARNING4(str, e1, e2, e3, e4) \ |
|
282 START_WARNING (); fprintf (stderr, str, e1, e2, e3, e4); END_WARNING () |
|
283 |
|
284 |
|
285 /* I find this easier to read. */ |
|
286 #define STREQ(s1, s2) (strcmp (s1, s2) == 0) |
|
287 #define STRNEQ(s1, s2, n) (strncmp (s1, s2, n) == 0) |
4391
|
288 |
4385
|
289 /* Support for FAT/ISO-9660 filesystems. Theoretically this should be |
|
290 done at runtime, per filesystem, but that's painful to program. */ |
|
291 #ifdef MONOCASE_FILENAMES |
|
292 #define FILESTRCASEEQ(s1, s2) (strcasecmp (s1, s2) == 0) |
|
293 #define FILESTRNCASEEQ(s1, s2, l) (strncasecmp (s1, s2, l) == 0) |
|
294 #define FILECHARCASEEQ(c1, c2) (toupper (c1) == toupper (c2)) |
|
295 #else |
|
296 #define FILESTRCASEEQ STREQ |
|
297 #define FILESTRNCASEEQ STRNEQ |
|
298 #define FILECHARCASEEQ(c1, c2) ((c1) == (c2)) |
|
299 #endif |
|
300 |
|
301 |
|
302 |
|
303 /* (Re)Allocate N items of type T using xmalloc/xrealloc. */ |
|
304 #define XTALLOC(n, t) ((t *) xmalloc ((n) * sizeof (t))) |
|
305 #define XTALLOC1(t) XTALLOC (1, t) |
|
306 #define XRETALLOC(addr, n, t) ((addr) = (t *) xrealloc (addr, (n) * sizeof(t))) |
|
307 |
|
308 extern "C" char *xbasename (const char *name); |
|
309 |
|
310 static FILE *xfopen (const char *filename, const char *mode); |
|
311 |
|
312 static void xfclose (FILE *f, const char *filename); |
|
313 |
4386
|
314 #ifndef WIN32 |
4385
|
315 static void xclosedir (DIR *d); |
4386
|
316 #endif |
4385
|
317 |
|
318 static void *xmalloc (unsigned size); |
|
319 |
|
320 static void *xrealloc (void *old_ptr, unsigned size); |
|
321 |
|
322 #ifndef WIN32 |
|
323 int dir_links (const char *fn); |
|
324 #endif |
|
325 |
4390
|
326 static unsigned hash (hash_table_type table, const std::string& key); |
|
327 |
|
328 static void hash_insert (hash_table_type *table, const std::string& key, |
|
329 const std::string& value); |
|
330 |
|
331 static string_vector hash_lookup (hash_table_type table, |
|
332 const std::string& key); |
4385
|
333 |
|
334 static void hash_print (hash_table_type table, int summary_only); |
|
335 |
|
336 static char *concat (const char *s1, const char *s2); |
|
337 |
|
338 static char *concat3 (const char *s1, const char *s2, const char *s3); |
|
339 |
4390
|
340 static void str_list_add (string_vector& l, const std::string& s); |
|
341 |
|
342 static void str_list_concat (string_vector& target, const string_vector& more); |
|
343 |
|
344 static void str_llist_add (str_llist_type *l, const std::string& str); |
4385
|
345 |
|
346 static void str_llist_float (str_llist_type *l, str_llist_elt_type *mover); |
|
347 |
4389
|
348 static std::string kpse_var_expand (const std::string& src); |
4385
|
349 |
|
350 #include <ctime> /* for `time' */ |
|
351 |
4391
|
352 /* Return a copy of S in new storage. */ |
|
353 |
|
354 static char * |
|
355 xstrdup (const char *s) |
|
356 { |
|
357 char *new_string = (char *) xmalloc (strlen (s) + 1); |
|
358 return strcpy (new_string, s); |
|
359 } |
|
360 |
|
361 /* Here's the simple one, when a program just wants a value. */ |
|
362 |
|
363 static std::string |
|
364 kpse_var_value (const std::string& var) |
|
365 { |
|
366 std::string ret; |
|
367 |
|
368 std::string tmp = octave_env::getenv (var); |
|
369 |
|
370 if (! tmp.empty ()) |
|
371 ret = kpse_var_expand (tmp); |
|
372 |
|
373 #ifdef KPSE_DEBUG |
|
374 if (KPSE_DEBUG_P (KPSE_DEBUG_VARS)) |
|
375 DEBUGF2 ("variable: %s = %s\n", var.c_str (), |
|
376 tmp.empty () ? "(nil)" : tmp.c_str ()); |
|
377 #endif |
|
378 |
|
379 return ret; |
|
380 } |
|
381 |
|
382 /* Truncate any too-long components in NAME, returning the result. It's |
|
383 too bad this is necessary. See comments in readable.c for why. */ |
|
384 |
|
385 static char * |
|
386 kpse_truncate_filename (const char *name) |
|
387 { |
|
388 unsigned c_len = 0; /* Length of current component. */ |
|
389 unsigned ret_len = 0; /* Length of constructed result. */ |
|
390 |
|
391 /* Allocate enough space. */ |
|
392 char *ret = (char *) xmalloc (strlen (name) + 1); |
|
393 |
|
394 for (; *name; name++) |
|
395 { |
|
396 if (IS_DIR_SEP (*name) || IS_DEVICE_SEP (*name)) |
|
397 { |
|
398 /* At a directory delimiter, reset component length. */ |
|
399 c_len = 0; |
|
400 } |
|
401 else if (c_len > NAME_MAX) |
|
402 { |
|
403 /* If past the max for a component, ignore this character. */ |
|
404 continue; |
|
405 } |
|
406 |
|
407 /* Copy this character. */ |
|
408 ret[ret_len++] = *name; |
|
409 c_len++; |
|
410 } |
|
411 |
|
412 ret[ret_len] = 0; |
|
413 |
|
414 return ret; |
|
415 } |
|
416 |
|
417 /* If access can read FN, run stat (assigning to stat buffer ST) and |
|
418 check that fn is not a directory. Don't check for just being a |
|
419 regular file, as it is potentially useful to read fifo's or some |
|
420 kinds of devices. */ |
|
421 |
|
422 #ifdef WIN32 |
|
423 static inline bool |
|
424 READABLE (const char *fn, struct stat&) |
|
425 { |
|
426 return (GetFileAttributes (fn) != 0xFFFFFFFF |
|
427 && ! (GetFileAttributes (fn) & FILE_ATTRIBUTE_DIRECTORY)); |
|
428 } |
|
429 #else |
|
430 static inline bool |
|
431 READABLE (const char *fn, struct stat& st) |
|
432 { |
|
433 return (access (fn, R_OK) == 0 |
|
434 && stat (fn, &(st)) == 0 |
|
435 && !S_ISDIR (st.st_mode)); |
|
436 } |
|
437 #endif |
|
438 |
|
439 /* POSIX invented the brain-damage of not necessarily truncating |
|
440 filename components; the system's behavior is defined by the value of |
|
441 the symbol _POSIX_NO_TRUNC, but you can't change it dynamically! |
|
442 |
|
443 Generic const return warning. See extend-fname.c. */ |
|
444 |
|
445 static char * |
|
446 kpse_readable_file (const char *name) |
|
447 { |
|
448 struct stat st; |
|
449 char *ret; |
|
450 |
|
451 if (READABLE (name, st)) |
|
452 { |
|
453 ret = (char *) name; |
|
454 |
|
455 #ifdef ENAMETOOLONG |
|
456 } |
|
457 else if (errno == ENAMETOOLONG) |
|
458 { |
|
459 ret = kpse_truncate_filename (name); |
|
460 |
|
461 /* Perhaps some other error will occur with the truncated name, |
|
462 so let's call access again. */ |
|
463 |
|
464 if (! READABLE (ret, st)) |
|
465 { |
|
466 /* Failed. */ |
|
467 if (ret != name) |
|
468 free (ret); |
|
469 |
|
470 ret = NULL; |
|
471 } |
|
472 #endif /* ENAMETOOLONG */ |
|
473 |
|
474 } |
|
475 else |
|
476 { |
|
477 /* Some other error. */ |
|
478 if (errno == EACCES) |
|
479 { |
|
480 /* Maybe warn them if permissions are bad. */ |
|
481 perror (name); |
|
482 } |
|
483 ret = NULL; |
|
484 } |
|
485 |
|
486 return ret; |
|
487 } |
|
488 |
|
489 /* Sorry this is such a system-dependent mess, but I can't see any way |
|
490 to usefully generalize. */ |
|
491 |
|
492 static bool |
|
493 kpse_absolute_p (const std::string& filename, int relative_ok) |
|
494 { |
|
495 size_t len = filename.length (); |
|
496 |
|
497 int absolute = IS_DIR_SEP (len > 0 && filename[0]) |
|
498 #ifdef DOSISH |
|
499 /* Novell allows non-alphanumeric drive letters. */ |
|
500 || (len > 0 && IS_DEVICE_SEP (filename[1])) |
|
501 #endif /* DOSISH */ |
|
502 #ifdef WIN32 |
|
503 /* UNC names */ |
|
504 || (len > 1 && filename[0] == '\\' && filename[1] == '\\') |
|
505 #endif |
|
506 ; |
|
507 int explicit_relative |
|
508 = relative_ok |
|
509 && (len > 1 |
|
510 && filename[0] == '.' |
|
511 && (IS_DIR_SEP (filename[1]) |
|
512 || (len > 2 && filename[1] == '.' && IS_DIR_SEP (filename[2])))); |
|
513 |
|
514 return absolute || explicit_relative; |
|
515 } |
|
516 |
4378
|
517 /* The very first search is for texmf.cnf, called when someone tries to |
|
518 initialize the TFM path or whatever. init_path calls kpse_cnf_get |
|
519 which calls kpse_all_path_search to find all the texmf.cnf's. We |
|
520 need to do various special things in this case, since we obviously |
|
521 don't yet have the configuration files when we're searching for the |
|
522 configuration files. */ |
|
523 static bool first_search = true; |
|
524 |
|
525 /* This function is called after every search (except the first, since |
|
526 we definitely want to allow enabling the logging in texmf.cnf) to |
|
527 record the filename(s) found in $TEXMFLOG. */ |
|
528 |
|
529 static void |
4391
|
530 log_search (const string_vector& filenames) |
4378
|
531 { |
|
532 static FILE *log_file = NULL; |
|
533 static bool first_time = true; /* Need to open the log file? */ |
4391
|
534 |
|
535 if (first_time) |
|
536 { |
|
537 first_time = false; |
|
538 |
|
539 /* Get name from either envvar or config file. */ |
|
540 std::string log_name = kpse_var_value ("TEXMFLOG"); |
|
541 |
|
542 if (! log_name.empty ()) |
|
543 { |
|
544 log_file = xfopen (log_name.c_str (), "a"); |
|
545 |
|
546 if (! log_file) |
|
547 perror (log_name.c_str ()); |
|
548 } |
4378
|
549 } |
4391
|
550 |
|
551 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH) || log_file) |
|
552 { |
|
553 /* FILENAMES should never be null, but safety doesn't hurt. */ |
|
554 for (int e = 0; e < filenames.length () && ! filenames[e].empty (); e++) |
|
555 { |
|
556 std::string filename = filenames[e]; |
|
557 |
|
558 /* Only record absolute filenames, for privacy. */ |
|
559 if (log_file && kpse_absolute_p (filename.c_str (), false)) |
|
560 fprintf (log_file, "%lu %s\n", (long unsigned) time (NULL), |
|
561 filename.c_str ()); |
|
562 |
|
563 /* And show them online, if debugging. We've already started |
|
564 the debugging line in `search', where this is called, so |
|
565 just print the filename here, don't use DEBUGF. */ |
|
566 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
567 fputs (filename.c_str (), stderr); |
|
568 } |
4378
|
569 } |
|
570 } |
|
571 |
|
572 /* Concatenate each element in DIRS with NAME (assume each ends with a |
|
573 /, to save time). If SEARCH_ALL is false, return the first readable |
|
574 regular file. Else continue to search for more. In any case, if |
|
575 none, return a list containing just NULL. |
|
576 |
|
577 We keep a single buffer for the potential filenames and reallocate |
|
578 only when necessary. I'm not sure it's noticeably faster, but it |
|
579 does seem cleaner. (We do waste a bit of space in the return |
|
580 value, though, since we don't shrink it to the final size returned.) */ |
|
581 |
|
582 #define INIT_ALLOC 75 /* Doesn't much matter what this number is. */ |
|
583 |
4390
|
584 static string_vector |
|
585 dir_list_search (str_llist_type *dirs, const std::string& name, |
|
586 bool search_all) |
4378
|
587 { |
|
588 str_llist_elt_type *elt; |
4390
|
589 string_vector ret; |
|
590 unsigned name_len = name.length (); |
4378
|
591 unsigned allocated = INIT_ALLOC; |
|
592 char *potential = (char *) xmalloc (allocated); |
|
593 |
|
594 for (elt = *dirs; elt; elt = STR_LLIST_NEXT (*elt)) |
|
595 { |
4390
|
596 const std::string dir = STR_LLIST (*elt); |
|
597 unsigned dir_len = dir.length (); |
4391
|
598 |
4378
|
599 while (dir_len + name_len + 1 > allocated) |
|
600 { |
|
601 allocated += allocated; |
|
602 XRETALLOC (potential, allocated, char); |
|
603 } |
4391
|
604 |
4390
|
605 strcpy (potential, dir.c_str ()); |
|
606 strcat (potential, name.c_str ()); |
4391
|
607 |
4378
|
608 if (kpse_readable_file (potential)) |
4391
|
609 { |
4390
|
610 str_list_add (ret, potential); |
4391
|
611 |
4378
|
612 /* Move this element towards the top of the list. */ |
|
613 str_llist_float (dirs, elt); |
4391
|
614 |
4378
|
615 /* If caller only wanted one file returned, no need to |
|
616 terminate the list with NULL; the caller knows to only look |
|
617 at the first element. */ |
4391
|
618 if (! search_all) |
4378
|
619 return ret; |
|
620 |
|
621 /* Start new filename. */ |
|
622 allocated = INIT_ALLOC; |
|
623 potential = (char *) xmalloc (allocated); |
|
624 } |
|
625 } |
4391
|
626 |
4378
|
627 /* If we get here, either we didn't find any files, or we were finding |
|
628 all the files. But we're done with the last filename, anyway. */ |
|
629 free (potential); |
4391
|
630 |
4378
|
631 return ret; |
|
632 } |
|
633 |
|
634 /* This is called when NAME is absolute or explicitly relative; if it's |
|
635 readable, return (a list containing) it; otherwise, return NULL. */ |
|
636 |
4390
|
637 static string_vector |
|
638 absolute_search (const std::string& name_arg) |
4378
|
639 { |
4390
|
640 string_vector ret_list; |
|
641 const char *name = name_arg.c_str (); |
4378
|
642 char *found = kpse_readable_file (name); |
4391
|
643 |
4378
|
644 /* Add `found' to the return list even if it's null; that tells |
|
645 the caller we didn't find anything. */ |
4390
|
646 str_list_add (ret_list, found); |
4391
|
647 |
4378
|
648 return ret_list; |
|
649 } |
|
650 |
|
651 /* This is the hard case -- look for NAME in PATH. If ALL is false, |
|
652 return the first file found. Otherwise, search all elements of PATH. */ |
|
653 |
4390
|
654 static string_vector |
|
655 path_search (const std::string& path_arg, const std::string& name, |
|
656 bool must_exist, bool all) |
4378
|
657 { |
|
658 char *elt; |
4390
|
659 string_vector ret_list; |
4378
|
660 bool done = false; |
4390
|
661 |
|
662 const char *path = path_arg.c_str (); |
4378
|
663 |
|
664 for (elt = kpse_path_element (path); !done && elt; |
4390
|
665 elt = kpse_path_element (NULL)) |
|
666 { |
|
667 string_vector found; |
|
668 bool allow_disk_search = true; |
|
669 |
|
670 if (*elt == '!' && *(elt + 1) == '!') |
|
671 { |
|
672 /* Those magic leading chars in a path element means don't |
|
673 search the disk for this elt. And move past the magic to |
|
674 get to the name. */ |
|
675 allow_disk_search = false; |
|
676 elt += 2; |
|
677 } |
|
678 |
|
679 /* Do not touch the device if present */ |
|
680 if (NAME_BEGINS_WITH_DEVICE (elt)) |
|
681 { |
|
682 while (IS_DIR_SEP (*(elt + 2)) && IS_DIR_SEP (*(elt + 3))) |
|
683 { |
|
684 *(elt + 2) = *(elt + 1); |
|
685 *(elt + 1) = *elt; |
|
686 elt++; |
|
687 } |
|
688 } |
|
689 else |
|
690 { |
|
691 /* We never want to search the whole disk. */ |
|
692 while (IS_DIR_SEP (*elt) && IS_DIR_SEP (*(elt + 1))) |
|
693 elt++; |
|
694 } |
4391
|
695 |
4390
|
696 /* Try ls-R, unless we're searching for texmf.cnf. Our caller |
|
697 (search), also tests first_search, and does the resetting. */ |
|
698 found = first_search |
|
699 ? string_vector () : kpse_db_search (name, elt, all); |
|
700 |
|
701 /* Search the filesystem if (1) the path spec allows it, and either |
4378
|
702 (2a) we are searching for texmf.cnf ; or |
4391
|
703 (2b) no db exists; or |
4378
|
704 (2c) no db's are relevant to this elt; or |
|
705 (3) MUST_EXIST && NAME was not in the db. |
4390
|
706 In (2*), `found' will be NULL. |
|
707 In (3), `found' will be an empty list. */ |
|
708 |
|
709 if (allow_disk_search && found.empty ()) |
|
710 { |
|
711 str_llist_type *dirs = kpse_element_dirs (elt); |
4391
|
712 |
|
713 if (dirs && *dirs) |
4390
|
714 found = dir_list_search (dirs, name, all); |
|
715 } |
|
716 |
|
717 /* Did we find anything anywhere? */ |
|
718 if (! found.empty ()) |
|
719 { |
|
720 if (all) |
|
721 str_list_concat (ret_list, found); |
|
722 else |
|
723 { |
|
724 str_list_add (ret_list, found[0]); |
|
725 done = true; |
|
726 } |
|
727 } |
4378
|
728 } |
|
729 |
|
730 return ret_list; |
4390
|
731 } |
4378
|
732 |
|
733 /* Search PATH for ORIGINAL_NAME. If ALL is false, or ORIGINAL_NAME is |
|
734 absolute_p, check ORIGINAL_NAME itself. Otherwise, look at each |
|
735 element of PATH for the first readable ORIGINAL_NAME. |
4391
|
736 |
4378
|
737 Always return a list; if no files are found, the list will |
|
738 contain just NULL. If ALL is true, the list will be |
|
739 terminated with NULL. */ |
|
740 |
4390
|
741 static string_vector |
|
742 search (const std::string& path, const std::string& original_name, |
4378
|
743 bool must_exist, bool all) |
|
744 { |
4390
|
745 string_vector ret_list; |
4378
|
746 bool absolute_p; |
|
747 |
|
748 /* Make a leading ~ count as an absolute filename, and expand $FOO's. */ |
4390
|
749 std::string name = kpse_expand (original_name); |
4391
|
750 |
4378
|
751 /* If the first name is absolute or explicitly relative, no need to |
|
752 consider PATH at all. */ |
|
753 absolute_p = kpse_absolute_p (name, true); |
4391
|
754 |
4378
|
755 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
4391
|
756 DEBUGF4 ("start search (file=%s, must_exist=%d, find_all=%d, path=%s).\n", |
4390
|
757 name.c_str (), must_exist, all, path.c_str ()); |
4378
|
758 |
|
759 /* Find the file(s). */ |
|
760 ret_list = absolute_p ? absolute_search (name) |
|
761 : path_search (path, name, must_exist, all); |
4391
|
762 |
4378
|
763 /* The very first search is for texmf.cnf. We can't log that, since |
|
764 we want to allow setting TEXMFLOG in texmf.cnf. */ |
4391
|
765 if (first_search) |
|
766 { |
|
767 first_search = false; |
|
768 } |
|
769 else |
|
770 { |
|
771 /* Record the filenames we found, if desired. And wrap them in a |
|
772 debugging line if we're doing that. */ |
|
773 |
|
774 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
775 DEBUGF1 ("search (%s) =>", original_name.c_str ()); |
|
776 |
|
777 log_search (ret_list); |
|
778 |
|
779 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
780 putc ('\n', stderr); |
|
781 } |
4378
|
782 |
4390
|
783 return ret_list; |
4378
|
784 } |
|
785 |
|
786 /* Search PATH for the first NAME. */ |
|
787 |
4390
|
788 std::string |
|
789 kpse_path_search (const std::string& path, const std::string& name, |
|
790 bool must_exist) |
4378
|
791 { |
4390
|
792 string_vector ret_list = search (path, name, must_exist, false); |
|
793 |
|
794 return ret_list.empty () ? std::string () : ret_list[0]; |
4378
|
795 } |
|
796 |
|
797 |
|
798 /* Search all elements of PATH for files named NAME. Not sure if it's |
|
799 right to assert `must_exist' here, but it suffices now. */ |
|
800 |
4390
|
801 string_vector |
|
802 kpse_all_path_search (const std::string& path, const std::string& name) |
4378
|
803 { |
4390
|
804 return search (path, name, true, true); |
4378
|
805 } |
|
806 |
|
807 /* This is the hard case -- look in each element of PATH for each |
|
808 element of NAMES. If ALL is false, return the first file found. |
|
809 Otherwise, search all elements of PATH. */ |
|
810 |
4390
|
811 static string_vector |
|
812 path_find_first_of (const std::string& path_arg, const string_vector& names, |
4378
|
813 bool must_exist, bool all) |
|
814 { |
|
815 char *elt; |
4390
|
816 string_vector ret_list; |
4378
|
817 bool done = false; |
4390
|
818 |
|
819 const char *path = path_arg.c_str (); |
4378
|
820 |
|
821 for (elt = kpse_path_element (path); !done && elt; |
|
822 elt = kpse_path_element (NULL)) |
|
823 { |
|
824 str_llist_type *dirs; |
|
825 str_llist_elt_type *dirs_elt; |
4390
|
826 string_vector found; |
4378
|
827 bool allow_disk_search = true; |
|
828 |
|
829 if (*elt == '!' && *(elt + 1) == '!') |
|
830 { |
|
831 /* Those magic leading chars in a path element means don't |
|
832 search the disk for this elt. And move past the magic to |
|
833 get to the name. */ |
|
834 |
|
835 allow_disk_search = false; |
|
836 elt += 2; |
|
837 } |
|
838 |
|
839 /* Do not touch the device if present */ |
|
840 |
|
841 if (NAME_BEGINS_WITH_DEVICE (elt)) |
|
842 { |
|
843 while (IS_DIR_SEP (*(elt + 2)) && IS_DIR_SEP (*(elt + 3))) |
|
844 { |
|
845 *(elt + 2) = *(elt + 1); |
|
846 *(elt + 1) = *elt; |
|
847 elt++; |
|
848 } |
|
849 } |
|
850 else |
|
851 { |
|
852 /* We never want to search the whole disk. */ |
|
853 while (IS_DIR_SEP (*elt) && IS_DIR_SEP (*(elt + 1))) |
|
854 elt++; |
|
855 } |
|
856 |
|
857 /* We have to search one directory at a time. */ |
|
858 dirs = kpse_element_dirs (elt); |
|
859 for (dirs_elt = *dirs; dirs_elt; dirs_elt = STR_LLIST_NEXT (*dirs_elt)) |
|
860 { |
4390
|
861 const std::string dir = STR_LLIST (*dirs_elt); |
|
862 |
|
863 int len = names.length (); |
|
864 for (int i = 0; i < len && !done; i++) |
4378
|
865 { |
4390
|
866 std::string name = names[i]; |
4378
|
867 |
|
868 /* Try ls-R, unless we're searching for texmf.cnf. Our caller |
|
869 (find_first_of), also tests first_search, and does the |
|
870 resetting. */ |
4390
|
871 found = first_search ? string_vector () : kpse_db_search (name, dir.c_str (), all); |
4378
|
872 |
|
873 /* Search the filesystem if (1) the path spec allows it, |
|
874 and either |
|
875 |
|
876 (2a) we are searching for texmf.cnf ; or |
4391
|
877 (2b) no db exists; or |
4378
|
878 (2c) no db's are relevant to this elt; or |
|
879 (3) MUST_EXIST && NAME was not in the db. |
|
880 |
|
881 In (2*), `found' will be NULL. |
|
882 In (3), `found' will be an empty list. */ |
|
883 |
4390
|
884 if (allow_disk_search && found.empty ()) |
4378
|
885 { |
|
886 static str_llist_type *tmp = 0; |
|
887 |
|
888 if (! tmp) |
|
889 { |
4390
|
890 tmp = new str_llist_type; |
4378
|
891 *tmp = NULL; |
|
892 str_llist_add (tmp, ""); |
|
893 } |
|
894 |
|
895 STR_LLIST (*(*tmp)) = dir; |
|
896 |
4390
|
897 found = dir_list_search (tmp, name, all); |
4378
|
898 } |
|
899 |
|
900 /* Did we find anything anywhere? */ |
4390
|
901 if (! found.empty ()) |
4378
|
902 { |
|
903 if (all) |
4390
|
904 str_list_concat (ret_list, found); |
4378
|
905 else |
|
906 { |
4390
|
907 str_list_add (ret_list, found[0]); |
4378
|
908 done = true; |
|
909 } |
|
910 } |
|
911 } |
|
912 } |
|
913 } |
|
914 |
|
915 return ret_list; |
4391
|
916 } |
4378
|
917 |
4390
|
918 static string_vector |
|
919 find_first_of (const std::string& path, const string_vector& names, |
4378
|
920 bool must_exist, bool all) |
|
921 { |
4390
|
922 string_vector ret_list; |
4378
|
923 |
|
924 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
925 { |
4391
|
926 fputs ("start find_first_of ((", stderr); |
|
927 |
4390
|
928 int len = names.length (); |
4391
|
929 |
4390
|
930 for (int i = 0; i < len; i++) |
4378
|
931 { |
4390
|
932 if (i == 0) |
|
933 fputs (names[i].c_str (), stderr); |
4378
|
934 else |
4390
|
935 fprintf (stderr, ", %s", names[i].c_str ()); |
4378
|
936 } |
4391
|
937 |
|
938 fprintf (stderr, "), path=%s, must_exist=%d).\n", |
|
939 path.c_str (), must_exist); |
4378
|
940 } |
|
941 |
|
942 /* Find the file. */ |
|
943 ret_list = path_find_first_of (path, names, must_exist, all); |
|
944 |
|
945 /* The very first search is for texmf.cnf. We can't log that, since |
|
946 we want to allow setting TEXMFLOG in texmf.cnf. */ |
4391
|
947 if (first_search) |
|
948 { |
|
949 first_search = false; |
|
950 } |
|
951 else |
|
952 { |
|
953 /* Record the filenames we found, if desired. And wrap them in a |
|
954 debugging line if we're doing that. */ |
|
955 |
|
956 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
957 { |
|
958 fputs ("find_first_of (", stderr); |
|
959 |
|
960 int len = names.length (); |
|
961 |
|
962 for (int i = 0; i < len; i++) |
|
963 { |
|
964 if (i == 0) |
|
965 fputs (names[i].c_str (), stderr); |
|
966 else |
|
967 fprintf (stderr, ", %s", names[i].c_str ()); |
|
968 } |
|
969 fputs (") =>", stderr); |
|
970 } |
|
971 |
|
972 log_search (ret_list); |
|
973 |
|
974 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
|
975 putc ('\n', stderr); |
|
976 } |
4378
|
977 |
4390
|
978 return ret_list; |
4378
|
979 } |
|
980 |
|
981 /* Search each element of PATH for each element of NAMES. Return the |
|
982 first one found. */ |
|
983 |
4390
|
984 std::string |
|
985 kpse_path_find_first_of (const std::string& path, const string_vector& names, |
4378
|
986 bool must_exist) |
|
987 { |
4390
|
988 string_vector ret_list = find_first_of (path, names, must_exist, false); |
|
989 |
|
990 return ret_list.empty () ? std::string () : ret_list[0]; |
4378
|
991 } |
|
992 |
|
993 /* Search each element of PATH for each element of NAMES and return a |
|
994 list containing everything found, in the order found. */ |
|
995 |
4390
|
996 string_vector |
|
997 kpse_all_path_find_first_of (const std::string& path, |
|
998 const string_vector& names) |
4378
|
999 { |
4390
|
1000 return find_first_of (path, names, true, true); |
4378
|
1001 } |
|
1002 |
|
1003 /* expand.c: general expansion. Some of this file (the brace-expansion |
|
1004 code from bash) is covered by the GPL; this is the only GPL-covered |
|
1005 code in kpathsea. The part of the file that I wrote (the first |
|
1006 couple of functions) is covered by the LGPL. */ |
|
1007 |
|
1008 /* If NAME has a leading ~ or ~user, Unix-style, expand it to the user's |
|
1009 home directory, and return a new malloced string. If no ~, or no |
|
1010 <pwd.h>, just return NAME. */ |
|
1011 |
4389
|
1012 static std::string |
|
1013 kpse_tilde_expand (const std::string& name) |
4378
|
1014 { |
4389
|
1015 std::string expansion; |
4391
|
1016 |
4389
|
1017 assert (! name.empty ()); |
4391
|
1018 |
4378
|
1019 /* If no leading tilde, do nothing. */ |
4391
|
1020 if (name[0] != '~') |
|
1021 { |
|
1022 expansion = name; |
|
1023 |
|
1024 /* If a bare tilde, return the home directory or `.'. (Very |
|
1025 unlikely that the directory name will do anyone any good, but |
|
1026 ... */ |
|
1027 } |
|
1028 else if (name.length () == 1) |
|
1029 { |
|
1030 expansion = octave_env::getenv ("HOME"); |
|
1031 |
|
1032 if (expansion.empty ()) |
|
1033 expansion = "."; |
|
1034 |
|
1035 /* If `~/', remove any trailing / or replace leading // in $HOME. |
|
1036 Should really check for doubled intermediate slashes, too. */ |
4378
|
1037 } |
4391
|
1038 else if (IS_DIR_SEP (name[1])) |
|
1039 { |
|
1040 unsigned c = 1; |
|
1041 std::string home = octave_env::getenv ("HOME"); |
|
1042 |
|
1043 if (home.empty ()) |
|
1044 home = "."; |
|
1045 |
|
1046 size_t home_len = home.length (); |
|
1047 |
|
1048 /* handle leading // */ |
|
1049 if (home_len > 1 && IS_DIR_SEP (home[0]) && IS_DIR_SEP (home[1])) |
|
1050 home = home.substr (1); |
|
1051 |
|
1052 /* omit / after ~ */ |
|
1053 if (IS_DIR_SEP (home[home_len - 1])) |
|
1054 c++; |
|
1055 |
|
1056 expansion = home + name.substr (c); |
|
1057 |
|
1058 /* If `~user' or `~user/', look up user in the passwd database (but |
|
1059 OS/2 doesn't have this concept. */ |
4378
|
1060 } |
4391
|
1061 else |
4378
|
1062 #ifdef HAVE_PWD_H |
|
1063 { |
|
1064 unsigned c = 2; |
4391
|
1065 |
|
1066 /* find user name */ |
|
1067 while (name.length () > c && ! IS_DIR_SEP (name[c])) |
4378
|
1068 c++; |
4391
|
1069 |
|
1070 std::string user = name.substr (1, c-1); |
|
1071 |
4378
|
1072 /* We only need the cast here for (deficient) systems |
|
1073 which do not declare `getpwnam' in <pwd.h>. */ |
4391
|
1074 octave_passwd p = octave_passwd::getpwnam (user); |
4378
|
1075 |
|
1076 /* If no such user, just use `.'. */ |
4391
|
1077 std::string home = p ? p.dir () : std::string ("."); |
|
1078 |
|
1079 if (home.empty ()) |
|
1080 home = "."; |
|
1081 |
|
1082 /* handle leading // */ |
|
1083 if (home.length () > 1 && IS_DIR_SEP (home[0]) && IS_DIR_SEP (home[1])) |
|
1084 home = home.substr (1); |
|
1085 |
|
1086 /* If HOME ends in /, omit the / after ~user. */ |
|
1087 if (name.length () > c && IS_DIR_SEP (home[home.length () - 1])) |
|
1088 c++; |
|
1089 |
|
1090 expansion = name.length () > c ? home : home + name.substr (c); |
4378
|
1091 } |
|
1092 #else /* not HAVE_PWD_H */ |
4391
|
1093 expansion = name; |
4378
|
1094 #endif /* not HAVE_PWD_H */ |
|
1095 |
4389
|
1096 return expansion; |
4378
|
1097 } |
|
1098 |
|
1099 /* Do variable expansion first so ~${USER} works. (Besides, it's what the |
|
1100 shells do.) */ |
|
1101 |
4389
|
1102 std::string |
|
1103 kpse_expand (const std::string& s) |
4378
|
1104 { |
4389
|
1105 std::string var_expansion = kpse_var_expand (s); |
|
1106 return kpse_tilde_expand (var_expansion); |
4378
|
1107 } |
|
1108 |
|
1109 |
|
1110 /* Forward declarations of functions from the original expand.c */ |
|
1111 static char **brace_expand (const char *); |
|
1112 static void free_array (char **); |
|
1113 |
|
1114 /* If $KPSE_DOT is defined in the environment, prepend it to any relative |
|
1115 path components. */ |
|
1116 |
4389
|
1117 static std::string |
|
1118 kpse_expand_kpse_dot (const std::string& path) |
4378
|
1119 { |
4389
|
1120 std::string ret; |
|
1121 char *elt; |
4391
|
1122 std::string kpse_dot = octave_env::getenv ("KPSE_DOT"); |
|
1123 |
|
1124 if (kpse_dot.empty ()) |
4378
|
1125 return path; |
|
1126 |
|
1127 #ifdef MSDOS |
|
1128 /* Some setups of ported Bash force $KPSE_DOT to have the //d/foo/bar |
|
1129 form (when `pwd' is used), which is not understood by libc and the OS. |
|
1130 Convert them back to the usual d:/foo/bar form. */ |
4391
|
1131 if (kpse_dot.len > 3 && kpse_dot[0] == '/' && kpse_dot[1] == '/' |
|
1132 && kpse_dot[2] >= 'A' && kpse_dot[2] <= 'z' && kpse_dot[3] == '/') |
|
1133 { |
|
1134 kpse_dot = kpse_dot.substr (1); |
|
1135 kpse_dot = xstrdup (kpse_dot); |
|
1136 kpse_dot[0] = kpse_dot[1]; /* drive letter */ |
|
1137 kpse_dot[1] = ':'; |
|
1138 } |
4378
|
1139 #endif |
|
1140 |
4389
|
1141 char *tmp = xstrdup (path.c_str ()); |
|
1142 |
4391
|
1143 for (elt = kpse_path_element (tmp); elt; elt = kpse_path_element (NULL)) |
|
1144 { |
|
1145 /* We assume that the !! magic is only used on absolute components. |
|
1146 Single "." get special treatment, as does "./" or its equivalent. */ |
|
1147 |
|
1148 if (kpse_absolute_p (elt, false) || (elt[0] == '!' && elt[1] == '!')) |
|
1149 ret += std::string (elt) + ENV_SEP_STRING; |
|
1150 else if (elt[0] == '.' && elt[1] == 0) |
|
1151 ret += std::string (kpse_dot) + ENV_SEP_STRING; |
|
1152 else if (elt[0] == '.' && IS_DIR_SEP (elt[1])) |
|
1153 ret += std::string (kpse_dot) + (elt + 1) + ENV_SEP_STRING; |
|
1154 else |
|
1155 ret += std::string (kpse_dot) + DIR_SEP_STRING + elt + ENV_SEP_STRING; |
4378
|
1156 } |
|
1157 |
4389
|
1158 int len = ret.length (); |
4391
|
1159 |
4389
|
1160 if (len > 0) |
|
1161 ret.resize (len - 1); |
|
1162 |
4378
|
1163 return ret; |
|
1164 } |
|
1165 |
|
1166 /* Do brace expansion on ELT; then do variable and ~ expansion on each |
|
1167 element of the result; then do brace expansion again, in case a |
|
1168 variable definition contained braces (e.g., $TEXMF). Return a |
|
1169 string comprising all of the results separated by ENV_SEP_STRING. */ |
|
1170 |
4389
|
1171 static std::string |
4378
|
1172 kpse_brace_expand_element (const char *elt) |
|
1173 { |
|
1174 unsigned i; |
|
1175 char **expansions = brace_expand (elt); |
4389
|
1176 std::string ret; |
4378
|
1177 |
4391
|
1178 for (i = 0; expansions[i]; i++) |
|
1179 { |
|
1180 /* Do $ and ~ expansion on each element. */ |
|
1181 std::string x = kpse_expand (expansions[i]); |
|
1182 |
|
1183 if (x != expansions[i]) |
|
1184 { |
|
1185 /* If we did any expansions, do brace expansion again. Since |
|
1186 recursive variable definitions are not allowed, this recursion |
|
1187 must terminate. (In practice, it's unlikely there will ever be |
|
1188 more than one level of recursion.) */ |
|
1189 x = kpse_brace_expand_element (x.c_str ()); |
|
1190 } |
|
1191 |
|
1192 ret += x + ENV_SEP_STRING; |
4378
|
1193 } |
|
1194 |
|
1195 free_array (expansions); |
4391
|
1196 |
4389
|
1197 ret.resize (ret.length () - 1); |
4391
|
1198 |
4378
|
1199 return ret; |
|
1200 } |
|
1201 |
|
1202 /* Be careful to not waste all the memory we allocate for each element. */ |
|
1203 |
4389
|
1204 std::string |
4378
|
1205 kpse_brace_expand (const char *path) |
|
1206 { |
|
1207 char *elt; |
|
1208 unsigned len; |
|
1209 /* Must do variable expansion first because if we have |
|
1210 foo = .:~ |
|
1211 TEXINPUTS = $foo |
|
1212 we want to end up with TEXINPUTS = .:/home/karl. |
|
1213 Since kpse_path_element is not reentrant, we must get all |
|
1214 the path elements before we start the loop. */ |
4389
|
1215 std::string tmp = kpse_var_expand (path); |
|
1216 const char *xpath = tmp.c_str (); |
|
1217 std::string ret; |
4378
|
1218 |
4391
|
1219 for (elt = kpse_path_element (xpath); elt; elt = kpse_path_element (NULL)) |
|
1220 { |
|
1221 /* Do brace expansion first, so tilde expansion happens in {~ka,~kb}. */ |
|
1222 std::string expansion = kpse_brace_expand_element (elt); |
|
1223 ret += expansion + ENV_SEP_STRING; |
|
1224 } |
4378
|
1225 |
|
1226 /* Waste the last byte by overwriting the trailing env_sep with a null. */ |
4389
|
1227 len = ret.length (); |
4391
|
1228 |
4389
|
1229 if (len > 0) |
|
1230 ret.resize (len - 1); |
|
1231 |
|
1232 return kpse_expand_kpse_dot (ret); |
4378
|
1233 } |
|
1234 |
|
1235 /* Expand all special constructs in a path, and include only the actually |
|
1236 existing directories in the result. */ |
|
1237 char * |
|
1238 kpse_path_expand (const char *path) |
|
1239 { |
|
1240 char *ret; |
|
1241 char *elt; |
|
1242 unsigned len; |
|
1243 |
|
1244 /* Initialise ret to the empty string. */ |
|
1245 ret = (char *) xmalloc (1); |
|
1246 *ret = 0; |
|
1247 len = 0; |
4391
|
1248 |
4378
|
1249 /* Expand variables and braces first. */ |
4389
|
1250 std::string tmp = kpse_brace_expand (path); |
|
1251 const char *xpath = tmp.c_str (); |
4378
|
1252 |
|
1253 /* Now expand each of the path elements, printing the results */ |
4391
|
1254 for (elt = kpse_path_element (xpath); elt; elt = kpse_path_element (NULL)) |
|
1255 { |
|
1256 str_llist_type *dirs; |
|
1257 |
|
1258 /* Skip and ignore magic leading chars. */ |
|
1259 if (*elt == '!' && *(elt + 1) == '!') |
|
1260 elt += 2; |
|
1261 |
|
1262 /* Do not touch the device if present */ |
|
1263 if (NAME_BEGINS_WITH_DEVICE (elt)) |
|
1264 { |
|
1265 while (IS_DIR_SEP (*(elt + 2)) && IS_DIR_SEP (*(elt + 3))) |
|
1266 { |
|
1267 *(elt + 2) = *(elt + 1); |
|
1268 *(elt + 1) = *elt; |
|
1269 elt++; |
|
1270 } |
|
1271 } |
|
1272 else |
|
1273 { |
|
1274 /* We never want to search the whole disk. */ |
|
1275 while (IS_DIR_SEP (*elt) && IS_DIR_SEP (*(elt + 1))) |
|
1276 elt++; |
|
1277 } |
4378
|
1278 |
|
1279 /* Search the disk for all dirs in the component specified. |
|
1280 Be faster to check the database, but this is more reliable. */ |
4391
|
1281 dirs = kpse_element_dirs (elt); |
|
1282 |
|
1283 if (dirs && *dirs) |
|
1284 { |
|
1285 str_llist_elt_type *dir; |
|
1286 |
|
1287 for (dir = *dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
|
1288 { |
|
1289 const std::string thedir = STR_LLIST (*dir); |
|
1290 unsigned dirlen = thedir.length (); |
|
1291 char *save_ret = ret; |
|
1292 /* Retain trailing slash if that's the root directory. */ |
|
1293 if (dirlen == 1 || (dirlen == 3 && NAME_BEGINS_WITH_DEVICE (thedir) |
|
1294 && IS_DIR_SEP (thedir[2]))) |
|
1295 { |
|
1296 ret = concat3 (ret, thedir.c_str (), ENV_SEP_STRING); |
|
1297 len += dirlen + 1; |
|
1298 ret[len - 1] = ENV_SEP; |
|
1299 } |
|
1300 else |
|
1301 { |
|
1302 ret = concat (ret, thedir.c_str ()); |
|
1303 len += dirlen; |
|
1304 ret [len - 1] = ENV_SEP; |
|
1305 } |
|
1306 free (save_ret); |
|
1307 } |
4378
|
1308 } |
|
1309 } |
4391
|
1310 |
4378
|
1311 /* Get rid of trailing ':', if any. */ |
|
1312 if (len != 0) |
|
1313 ret[len - 1] = 0; |
4391
|
1314 |
4378
|
1315 return ret; |
|
1316 } |
|
1317 |
|
1318 /* braces.c -- code for doing word expansion in curly braces. Taken from |
|
1319 bash 1.14.5. [Ans subsequently modified for kpatshea.] |
|
1320 |
|
1321 Copyright (C) 1987,1991 Free Software Foundation, Inc. |
|
1322 |
|
1323 This program is free software; you can redistribute it and/or modify it |
|
1324 under the terms of the GNU General Public License as published by |
|
1325 the Free Software Foundation; either version 1, or (at your option) |
|
1326 any later version. |
|
1327 |
|
1328 This program is distributed in the hope that it will be useful, but |
|
1329 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
1330 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
1331 General Public License for more details. |
|
1332 |
|
1333 You should have received a copy of the GNU General Public License |
|
1334 along with this program; see the file COPYING. If not, write to the |
|
1335 Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
|
1336 MA 02111-1307, USA. */ |
|
1337 |
4391
|
1338 #define brace_whitespace(c) (! (c) || (c) == ' ' || (c) == '\t' || (c) == '\n') |
4378
|
1339 #define savestring xstrdup |
|
1340 |
|
1341 /* Basic idea: |
|
1342 |
|
1343 Segregate the text into 3 sections: preamble (stuff before an open brace), |
|
1344 postamble (stuff after the matching close brace) and amble (stuff after |
|
1345 preamble, and before postamble). Expand amble, and then tack on the |
|
1346 expansions to preamble. Expand postamble, and tack on the expansions to |
4391
|
1347 the result so far. */ |
4378
|
1348 |
|
1349 /* The character which is used to separate arguments. */ |
|
1350 static int brace_arg_separator = ','; |
|
1351 |
|
1352 static int brace_gobbler (const char *, int *, int); |
|
1353 static char **expand_amble (const char *), |
|
1354 **array_concat (char **, char **); |
|
1355 |
|
1356 /* Return the length of ARRAY, a NULL terminated array of char *. */ |
|
1357 static int |
|
1358 array_len (char **array) |
|
1359 { |
|
1360 register int i; |
|
1361 for (i = 0; array[i]; i++); |
|
1362 return (i); |
|
1363 } |
|
1364 |
|
1365 /* Free the contents of ARRAY, a NULL terminated array of char *. */ |
|
1366 static void |
|
1367 free_array (char **array) |
|
1368 { |
|
1369 register int i = 0; |
|
1370 |
4391
|
1371 if (! array) return; |
4378
|
1372 |
|
1373 while (array[i]) |
|
1374 free (array[i++]); |
4391
|
1375 |
4378
|
1376 free (array); |
|
1377 } |
|
1378 |
|
1379 /* Allocate and return a new copy of ARRAY and its contents. */ |
|
1380 static char ** |
|
1381 copy_array (char **array) |
|
1382 { |
|
1383 register int i; |
|
1384 int len; |
|
1385 char **new_array; |
|
1386 |
|
1387 len = array_len (array); |
|
1388 |
|
1389 new_array = (char **)xmalloc ((len + 1) * sizeof (char *)); |
|
1390 for (i = 0; array[i]; i++) |
|
1391 new_array[i] = savestring (array[i]); |
|
1392 new_array[i] = (char *)NULL; |
|
1393 |
|
1394 return (new_array); |
|
1395 } |
|
1396 |
|
1397 |
|
1398 /* Return an array of strings; the brace expansion of TEXT. */ |
|
1399 static char ** |
|
1400 brace_expand (const char *text) |
|
1401 { |
|
1402 register int start; |
|
1403 char *preamble, *amble; |
|
1404 const char *postamble; |
|
1405 char **tack, **result; |
|
1406 int i, c; |
|
1407 |
|
1408 /* Find the text of the preamble. */ |
|
1409 i = 0; |
|
1410 c = brace_gobbler (text, &i, '{'); |
|
1411 |
|
1412 preamble = (char *) xmalloc (i + 1); |
|
1413 strncpy (preamble, text, i); |
|
1414 preamble[i] = 0; |
|
1415 |
|
1416 result = (char **) xmalloc (2 * sizeof (char *)); |
|
1417 result[0] = preamble; |
|
1418 result[1] = NULL; |
4391
|
1419 |
4378
|
1420 /* Special case. If we never found an exciting character, then |
|
1421 the preamble is all of the text, so just return that. */ |
|
1422 if (c != '{') |
|
1423 return (result); |
|
1424 |
|
1425 /* Find the amble. This is the stuff inside this set of braces. */ |
|
1426 start = ++i; |
|
1427 c = brace_gobbler (text, &i, '}'); |
|
1428 |
|
1429 /* What if there isn't a matching close brace? */ |
4391
|
1430 if (! c) |
4378
|
1431 { |
|
1432 WARNING1 ("%s: Unmatched {", text); |
|
1433 free (preamble); /* Same as result[0]; see initialization. */ |
|
1434 result[0] = savestring (text); |
|
1435 return (result); |
|
1436 } |
|
1437 |
|
1438 amble = (char *) xmalloc (1 + (i - start)); |
|
1439 strncpy (amble, &text[start], (i - start)); |
|
1440 amble[i - start] = 0; |
|
1441 |
|
1442 postamble = &text[i + 1]; |
|
1443 |
|
1444 tack = expand_amble (amble); |
|
1445 result = array_concat (result, tack); |
|
1446 free (amble); |
|
1447 free_array (tack); |
|
1448 |
|
1449 tack = brace_expand (postamble); |
|
1450 result = array_concat (result, tack); |
|
1451 free_array (tack); |
|
1452 |
|
1453 return (result); |
|
1454 } |
|
1455 |
|
1456 |
|
1457 /* Expand the text found inside of braces. We simply try to split the |
|
1458 text at BRACE_ARG_SEPARATORs into separate strings. We then brace |
|
1459 expand each slot which needs it, until there are no more slots which |
|
1460 need it. */ |
|
1461 static char ** |
|
1462 expand_amble (const char *text) |
|
1463 { |
|
1464 char **result, **partial; |
|
1465 char *tem; |
|
1466 int start, i, c; |
|
1467 |
|
1468 result = NULL; |
|
1469 |
|
1470 for (start = 0, i = 0, c = 1; c; start = ++i) |
|
1471 { |
|
1472 int c0, c1; |
|
1473 int i0, i1; |
|
1474 i0 = i; |
|
1475 c0 = brace_gobbler (text, &i0, brace_arg_separator); |
|
1476 i1 = i; |
|
1477 c1 = brace_gobbler (text, &i1, ENV_SEP); |
|
1478 c = c0 | c1; |
|
1479 i = (i0 < i1 ? i0 : i1); |
|
1480 |
|
1481 tem = (char *) xmalloc (1 + (i - start)); |
|
1482 strncpy (tem, &text[start], (i - start)); |
|
1483 tem[i- start] = 0; |
|
1484 |
|
1485 partial = brace_expand (tem); |
|
1486 |
4391
|
1487 if (! result) |
4378
|
1488 result = partial; |
|
1489 else |
|
1490 { |
|
1491 register int lr = array_len (result); |
|
1492 register int lp = array_len (partial); |
|
1493 register int j; |
|
1494 |
|
1495 result = (char **) xrealloc (result, (1 + lp + lr) * sizeof (char *)); |
|
1496 |
|
1497 for (j = 0; j < lp; j++) |
|
1498 result[lr + j] = partial[j]; |
|
1499 |
|
1500 result[lr + j] = NULL; |
|
1501 free (partial); |
|
1502 } |
|
1503 free (tem); |
|
1504 } |
|
1505 return (result); |
|
1506 } |
|
1507 |
|
1508 /* Return a new array of strings which is the result of appending each |
|
1509 string in ARR2 to each string in ARR1. The resultant array is |
|
1510 len (arr1) * len (arr2) long. For convenience, ARR1 (and its contents) |
|
1511 are free ()'ed. ARR1 can be NULL, in that case, a new version of ARR2 |
|
1512 is returned. */ |
|
1513 static char ** |
|
1514 array_concat (char **arr1, char **arr2) |
|
1515 { |
|
1516 register int i, j, len, len1, len2; |
|
1517 register char **result; |
|
1518 |
4391
|
1519 if (! arr1) |
4378
|
1520 return (copy_array (arr2)); |
|
1521 |
4391
|
1522 if (! arr2) |
4378
|
1523 return (copy_array (arr1)); |
|
1524 |
|
1525 len1 = array_len (arr1); |
|
1526 len2 = array_len (arr2); |
|
1527 |
|
1528 result = (char **) xmalloc ((1 + (len1 * len2)) * sizeof (char *)); |
|
1529 |
|
1530 len = 0; |
|
1531 for (i = 0; i < len2; i++) |
|
1532 { |
|
1533 int strlen_2 = strlen (arr2[i]); |
|
1534 |
|
1535 for (j = 0; j < len1; j++) |
|
1536 { |
|
1537 int strlen_1 = strlen (arr1[j]); |
|
1538 |
|
1539 result[len] = (char *) xmalloc (1 + strlen_1 + strlen_2); |
|
1540 strcpy (result[len], arr1[j]); |
|
1541 strcpy (result[len] + strlen_1, arr2[i]); |
|
1542 len++; |
|
1543 } |
|
1544 } |
|
1545 free_array (arr1); |
|
1546 |
|
1547 result[len] = NULL; |
|
1548 return (result); |
|
1549 } |
|
1550 |
|
1551 /* Start at INDEX, and skip characters in TEXT. Set INDEX to the |
|
1552 index of the character matching SATISFY. This understands about |
|
1553 quoting. Return the character that caused us to stop searching; |
|
1554 this is either the same as SATISFY, or 0. */ |
|
1555 static int |
|
1556 brace_gobbler (const char *text, int *indx, int satisfy) |
|
1557 { |
|
1558 register int i, c, quoted, level, pass_next; |
|
1559 |
|
1560 level = quoted = pass_next = 0; |
|
1561 |
|
1562 for (i = *indx; (c = text[i]); i++) |
|
1563 { |
|
1564 if (pass_next) |
|
1565 { |
|
1566 pass_next = 0; |
|
1567 continue; |
|
1568 } |
|
1569 |
|
1570 /* A backslash escapes the next character. This allows backslash to |
|
1571 escape the quote character in a double-quoted string. */ |
|
1572 if (c == '\\' && (quoted == 0 || quoted == '"' || quoted == '`')) |
|
1573 { |
|
1574 pass_next = 1; |
|
1575 continue; |
|
1576 } |
|
1577 |
|
1578 if (quoted) |
|
1579 { |
|
1580 if (c == quoted) |
|
1581 quoted = 0; |
|
1582 continue; |
|
1583 } |
|
1584 |
|
1585 if (c == '"' || c == '\'' || c == '`') |
|
1586 { |
|
1587 quoted = c; |
|
1588 continue; |
|
1589 } |
4391
|
1590 |
4378
|
1591 if (c == satisfy && !level && !quoted) |
|
1592 { |
|
1593 /* We ignore an open brace surrounded by whitespace, and also |
|
1594 an open brace followed immediately by a close brace, that |
|
1595 was preceded with whitespace. */ |
|
1596 if (c == '{' && |
4391
|
1597 ((! i || brace_whitespace (text[i - 1])) && |
4378
|
1598 (brace_whitespace (text[i + 1]) || text[i + 1] == '}'))) |
|
1599 continue; |
|
1600 /* If this is being compiled as part of bash, ignore the `{' |
|
1601 in a `${}' construct */ |
|
1602 if ((c != '{') || !i || (text[i - 1] != '$')) |
|
1603 break; |
|
1604 } |
|
1605 |
|
1606 if (c == '{') |
|
1607 level++; |
|
1608 else if (c == '}' && level) |
|
1609 level--; |
|
1610 } |
|
1611 |
|
1612 *indx = i; |
|
1613 return (c); |
|
1614 } |
|
1615 |
|
1616 /* db.c: an external database to avoid filesystem lookups. */ |
|
1617 |
|
1618 #ifndef DEFAULT_TEXMFDBS |
|
1619 #define DEFAULT_TEXMFDBS "/usr/local/share/texmf:/var/tmp/texfonts" |
|
1620 #endif |
|
1621 |
|
1622 /* Perhaps we could use this for path values themselves; for now, we use |
|
1623 it only for the program_enabled_p value. */ |
|
1624 typedef enum |
|
1625 { |
|
1626 kpse_src_implicit, /* C initialization to zero */ |
|
1627 kpse_src_compile, /* configure/compile-time default */ |
|
1628 kpse_src_texmf_cnf, /* texmf.cnf, the kpathsea config file */ |
|
1629 kpse_src_client_cnf, /* application config file, e.g., config.ps */ |
|
1630 kpse_src_env, /* environment variable */ |
|
1631 kpse_src_x, /* X Window System resource */ |
|
1632 kpse_src_cmdline /* command-line option */ |
|
1633 } kpse_src_type; |
|
1634 |
|
1635 |
|
1636 /* For each file format, we record the following information. The main |
|
1637 thing that is not part of this structure is the environment variable |
|
1638 lists. They are used directly in tex-file.c. We could incorporate |
|
1639 them here, but it would complicate the code a bit. We could also do |
|
1640 it via variable expansion, but not now, maybe not ever: |
|
1641 ${PKFONTS-${TEXFONTS-/usr/local/lib/texmf/fonts//}}. */ |
|
1642 |
|
1643 typedef struct |
|
1644 { |
4390
|
1645 std::string type; /* Human-readable description. */ |
|
1646 std::string path; /* The search path to use. */ |
|
1647 std::string raw_path; /* Pre-$~ (but post-default) expansion. */ |
|
1648 std::string path_source; /* Where the path started from. */ |
|
1649 std::string override_path; /* From client environment variable. */ |
|
1650 std::string client_path; /* E.g., from dvips's config.ps. */ |
|
1651 std::string cnf_path; /* From texmf.cnf. */ |
|
1652 std::string default_path; /* If all else fails. */ |
|
1653 const char **suffix; /* For kpse_find_file to check for/append. */ |
|
1654 const char **alt_suffix; /* More suffixes to check for. */ |
|
1655 bool suffix_search_only; /* Only search with a suffix? */ |
|
1656 std::string program; /* ``mktexpk'', etc. */ |
|
1657 std::string program_args; /* Args to `program'. */ |
|
1658 bool program_enabled_p; /* Invoke `program'? */ |
4378
|
1659 kpse_src_type program_enable_level; /* Who said to invoke `program'. */ |
4390
|
1660 bool binmode; /* The files must be opened in binary mode. */ |
4378
|
1661 } kpse_format_info_type; |
|
1662 |
|
1663 /* The sole variable of that type, indexed by `kpse_file_format_type'. |
|
1664 Initialized by calls to `kpse_find_file' for `kpse_init_format'. */ |
|
1665 static kpse_format_info_type kpse_format_info; |
|
1666 |
|
1667 #define DB_ENVS "TEXMFDBS" |
|
1668 |
|
1669 /* And EXPAND_DEFAULT calls kpse_expand_default on try_path and the |
|
1670 present info->path. */ |
|
1671 #define EXPAND_DEFAULT(try_path, source_string) \ |
4391
|
1672 if (! try_path.empty ()) \ |
|
1673 { \ |
4378
|
1674 info->raw_path = try_path; \ |
4390
|
1675 info->path = kpse_expand_default (try_path.c_str (), (info->path).c_str ()); \ |
4378
|
1676 info->path_source = source_string; \ |
4391
|
1677 } |
4378
|
1678 |
|
1679 /* Find the final search path to use for the format entry INFO, given |
|
1680 the compile-time default (DEFAULT_PATH), and the environment |
|
1681 variables to check (the remaining arguments, terminated with NULL). |
|
1682 We set the `path' and `path_source' members of INFO. The |
|
1683 `client_path' member must already be set upon entry. */ |
|
1684 |
|
1685 static void |
|
1686 init_path (kpse_format_info_type *info, const char *default_path, ...) |
|
1687 { |
|
1688 char *env_name; |
|
1689 char *var = NULL; |
|
1690 va_list ap; |
|
1691 |
|
1692 va_start (ap, default_path); |
|
1693 |
|
1694 info->default_path = default_path; |
|
1695 |
|
1696 /* First envvar that's set to a nonempty value will exit the loop. If |
|
1697 none are set, we want the first cnf entry that matches. Find the |
|
1698 cnf entries simultaneously, to avoid having to go through envvar |
|
1699 list twice -- because of the PVAR?C macro, that would mean having |
|
1700 to create a str_list and then use it twice. Yuck. */ |
4391
|
1701 |
|
1702 while ((env_name = va_arg (ap, char *)) != NULL) |
|
1703 { |
|
1704 /* Since sh doesn't like envvar names with `.', check PATH_prog |
|
1705 rather than PATH.prog. */ |
|
1706 |
|
1707 if (! var) |
|
1708 { |
|
1709 /* Try simply PATH. */ |
|
1710 std::string env_value = octave_env::getenv (env_name); |
|
1711 |
|
1712 if (! env_value.empty ()) |
|
1713 var = env_name; |
|
1714 } |
|
1715 |
|
1716 if (var && ! info->cnf_path.empty ()) |
|
1717 break; |
4378
|
1718 } |
4391
|
1719 |
4378
|
1720 va_end (ap); |
4391
|
1721 |
4378
|
1722 /* Expand any extra :'s. For each level, we replace an extra : with |
|
1723 the path at the next lower level. For example, an extra : in a |
|
1724 user-set envvar should be replaced with the path from the cnf file. |
|
1725 things are complicated because none of the levels above the very |
|
1726 bottom are guaranteed to exist. */ |
|
1727 |
|
1728 /* Assume we can reliably start with the compile-time default. */ |
|
1729 info->path = info->raw_path = info->default_path; |
|
1730 info->path_source = "compile-time paths.h"; |
|
1731 |
|
1732 EXPAND_DEFAULT (info->cnf_path, "texmf.cnf"); |
|
1733 EXPAND_DEFAULT (info->client_path, "program config file"); |
4391
|
1734 |
4378
|
1735 if (var) |
4390
|
1736 { |
4391
|
1737 std::string val = octave_env::getenv (var); |
|
1738 EXPAND_DEFAULT (val, concat (var, " environment variable")); |
4390
|
1739 } |
4391
|
1740 |
4378
|
1741 EXPAND_DEFAULT (info->override_path, "application override variable"); |
4390
|
1742 std::string tmp = kpse_brace_expand ((info->path).c_str ()); |
|
1743 info->path = tmp; |
4378
|
1744 } |
|
1745 |
|
1746 |
|
1747 /* Some file types have more than one suffix. */ |
|
1748 |
|
1749 static void |
|
1750 add_suffixes (const char ***list, ...) |
|
1751 { |
|
1752 const char *s; |
|
1753 unsigned count = 0; |
|
1754 va_list ap; |
|
1755 |
|
1756 va_start (ap, list); |
4391
|
1757 |
|
1758 while ((s = va_arg (ap, char *)) != NULL) |
|
1759 { |
|
1760 count++; |
|
1761 XRETALLOC (*list, count + 1, const char *); |
|
1762 (*list)[count - 1] = s; |
|
1763 } |
|
1764 |
4378
|
1765 va_end (ap); |
4391
|
1766 |
4378
|
1767 (*list)[count] = NULL; |
|
1768 } |
|
1769 |
|
1770 |
|
1771 static char * |
|
1772 remove_dbonly (const char *path) |
|
1773 { |
4391
|
1774 char *ret = XTALLOC (strlen (path) + 1, char), *q=ret; |
4378
|
1775 const char *p=path; |
|
1776 bool new_elt=true; |
|
1777 |
4391
|
1778 while (*p) |
|
1779 { |
|
1780 if (new_elt && *p && *p == '!' && *(p+1) == '!') |
|
1781 p += 2; |
|
1782 else |
|
1783 { |
|
1784 new_elt = (*p == ENV_SEP); |
|
1785 *q++ = *p++; |
|
1786 } |
4378
|
1787 } |
4391
|
1788 |
4378
|
1789 *q = '\0'; |
4391
|
1790 |
|
1791 return ret; |
4378
|
1792 } |
|
1793 |
|
1794 /* Initialize everything for FORMAT. */ |
|
1795 |
4390
|
1796 static std::string |
4378
|
1797 kpse_init_format (void) |
|
1798 { |
|
1799 /* If we get called twice, don't redo all the work. */ |
4390
|
1800 if (! kpse_format_info.path.empty ()) |
4378
|
1801 return kpse_format_info.path; |
4391
|
1802 |
4378
|
1803 kpse_format_info.type = "ls-R"; |
|
1804 init_path (&kpse_format_info, DEFAULT_TEXMFDBS, DB_ENVS, NULL); |
4391
|
1805 add_suffixes (&kpse_format_info.suffix, "ls-R", NULL); |
4390
|
1806 kpse_format_info.path = remove_dbonly (kpse_format_info.path.c_str ()); |
4378
|
1807 |
|
1808 #ifdef KPSE_DEBUG |
4390
|
1809 #define MAYBE(member) (kpse_format_info.member.empty () ? "(none)" : kpse_format_info.member.c_str ()) |
4378
|
1810 |
|
1811 /* Describe the monster we've created. */ |
|
1812 if (KPSE_DEBUG_P (KPSE_DEBUG_PATHS)) |
|
1813 { |
|
1814 DEBUGF2 ("Search path for %s files (from %s)\n", |
4390
|
1815 kpse_format_info.type.c_str (), |
|
1816 kpse_format_info.path_source.c_str ()); |
4391
|
1817 |
4390
|
1818 DEBUGF1 (" = %s\n", kpse_format_info.path.c_str ()); |
4391
|
1819 |
|
1820 DEBUGF1 (" before expansion = %s\n", |
|
1821 kpse_format_info.raw_path.c_str ()); |
|
1822 |
4378
|
1823 DEBUGF1 (" application override path = %s\n", MAYBE (override_path)); |
4391
|
1824 |
4378
|
1825 DEBUGF1 (" application config file path = %s\n", MAYBE (client_path)); |
4391
|
1826 |
4378
|
1827 DEBUGF1 (" texmf.cnf path = %s\n", MAYBE (cnf_path)); |
4391
|
1828 |
4378
|
1829 DEBUGF1 (" compile-time path = %s\n", MAYBE (default_path)); |
4391
|
1830 |
4378
|
1831 DEBUGF (" default suffixes ="); |
4391
|
1832 |
|
1833 if (kpse_format_info.suffix) |
|
1834 { |
|
1835 const char **ext; |
|
1836 for (ext = kpse_format_info.suffix; ext && *ext; ext++) |
|
1837 { |
|
1838 fprintf (stderr, " %s", *ext); |
|
1839 } |
|
1840 putc ('\n', stderr); |
|
1841 } |
|
1842 else |
|
1843 { |
|
1844 fputs (" (none)\n", stderr); |
|
1845 } |
|
1846 |
4378
|
1847 DEBUGF (" other suffixes ="); |
4391
|
1848 |
|
1849 if (kpse_format_info.alt_suffix) |
|
1850 { |
|
1851 const char **alt; |
|
1852 for (alt = kpse_format_info.alt_suffix; alt && *alt; alt++) |
|
1853 { |
|
1854 fprintf (stderr, " %s", *alt); |
|
1855 } |
|
1856 putc ('\n', stderr); |
|
1857 } |
|
1858 else |
|
1859 { |
|
1860 fputs (" (none)\n", stderr); |
|
1861 } |
|
1862 |
|
1863 DEBUGF1 (" search only with suffix = %d\n", |
|
1864 kpse_format_info.suffix_search_only); |
|
1865 |
4378
|
1866 DEBUGF1 (" runtime generation program = %s\n", MAYBE (program)); |
4391
|
1867 |
4378
|
1868 DEBUGF1 (" extra program args = %s\n", MAYBE (program_args)); |
4391
|
1869 |
|
1870 DEBUGF1 (" program enabled = %d\n", |
|
1871 kpse_format_info.program_enabled_p); |
|
1872 |
|
1873 DEBUGF1 (" program enable level = %d\n", |
|
1874 kpse_format_info.program_enable_level); |
4378
|
1875 } |
|
1876 #endif /* KPSE_DEBUG */ |
|
1877 |
|
1878 return kpse_format_info.path; |
|
1879 } |
|
1880 |
|
1881 static hash_table_type db; /* The hash table for all the ls-R's. */ |
|
1882 /* SMALL: The old size of the hash table was 7603, with the assumption |
|
1883 that a minimal ls-R bas about 3500 entries. But a typical ls-R will |
|
1884 be more like double that size. */ |
|
1885 #ifndef DB_HASH_SIZE |
|
1886 #define DB_HASH_SIZE 15991 |
|
1887 #endif |
|
1888 #ifndef DB_NAME |
|
1889 #define DB_NAME "ls-R" |
|
1890 #endif |
|
1891 |
|
1892 static hash_table_type alias_db; |
|
1893 #ifndef ALIAS_NAME |
|
1894 #define ALIAS_NAME "aliases" |
|
1895 #endif |
|
1896 #ifndef ALIAS_HASH_SIZE |
|
1897 #define ALIAS_HASH_SIZE 1009 |
|
1898 #endif |
|
1899 |
4390
|
1900 static string_vector db_dir_list; |
4378
|
1901 |
|
1902 /* If DIRNAME contains any element beginning with a `.' (that is more |
|
1903 than just `./'), return true. This is to allow ``hidden'' |
|
1904 directories -- ones that don't get searched. */ |
|
1905 |
|
1906 static bool |
|
1907 ignore_dir_p (const char *dirname) |
|
1908 { |
|
1909 const char *dot_pos = dirname; |
4391
|
1910 |
|
1911 while ((dot_pos = strchr (dot_pos + 1, '.'))) |
|
1912 { |
|
1913 /* If / before and no / after, skip it. */ |
|
1914 if (IS_DIR_SEP (dot_pos[-1]) && dot_pos[1] && !IS_DIR_SEP (dot_pos[1])) |
|
1915 return true; |
|
1916 } |
|
1917 |
4378
|
1918 return false; |
|
1919 } |
|
1920 |
|
1921 /* Allocate in increments of this size. */ |
|
1922 #define BLOCK_SIZE 75 |
|
1923 |
|
1924 static char * |
|
1925 read_line (FILE *f) |
|
1926 { |
|
1927 int c; |
|
1928 unsigned limit = BLOCK_SIZE; |
|
1929 unsigned loc = 0; |
|
1930 char *line = (char *) xmalloc (limit); |
4391
|
1931 |
4378
|
1932 while ((c = getc (f)) != EOF && c != '\n' && c != '\r') |
|
1933 { |
|
1934 line[loc] = c; |
|
1935 loc++; |
4391
|
1936 |
4378
|
1937 /* By testing after the assignment, we guarantee that we'll always |
|
1938 have space for the null we append below. We know we always |
|
1939 have room for the first char, since we start with BLOCK_SIZE. */ |
|
1940 if (loc == limit) |
|
1941 { |
|
1942 limit += BLOCK_SIZE; |
|
1943 line = (char *) xrealloc (line, limit); |
|
1944 } |
|
1945 } |
4391
|
1946 |
4378
|
1947 /* If we read anything, return it. This can't represent a last |
|
1948 ``line'' which doesn't end in a newline, but so what. */ |
|
1949 if (c != EOF) |
|
1950 { |
|
1951 /* Terminate the string. We can't represent nulls in the file, |
|
1952 either. Again, it doesn't matter. */ |
|
1953 line[loc] = 0; |
4391
|
1954 |
4378
|
1955 /* Absorb LF of a CRLF pair. */ |
4391
|
1956 if (c == '\r') |
|
1957 { |
4378
|
1958 c = getc (f); |
|
1959 if (c != '\n') |
4391
|
1960 ungetc (c, f); |
|
1961 } |
4378
|
1962 } |
|
1963 else /* At end of file. */ |
|
1964 { |
|
1965 free (line); |
|
1966 line = NULL; |
|
1967 } |
|
1968 |
|
1969 return line; |
|
1970 } |
|
1971 |
|
1972 /* If no DB_FILENAME, return false (maybe they aren't using this feature). |
|
1973 Otherwise, add entries from DB_FILENAME to TABLE, and return true. */ |
|
1974 |
|
1975 static bool |
4390
|
1976 db_build (hash_table_type *table, const std::string& db_filename) |
4378
|
1977 { |
|
1978 char *line; |
|
1979 unsigned dir_count = 0, file_count = 0, ignore_dir_count = 0; |
4390
|
1980 unsigned len = db_filename.length () - sizeof (DB_NAME) + 1; /* Keep the /. */ |
4378
|
1981 char *top_dir = (char *) xmalloc (len + 1); |
|
1982 char *cur_dir = NULL; /* First thing in ls-R might be a filename. */ |
4390
|
1983 FILE *db_file = xfopen (db_filename.c_str (), "r"); |
4391
|
1984 |
4390
|
1985 strncpy (top_dir, db_filename.c_str (), len); |
4378
|
1986 top_dir[len] = 0; |
4391
|
1987 |
|
1988 if (db_file) |
|
1989 { |
|
1990 while ((line = read_line (db_file)) != NULL) |
|
1991 { |
|
1992 len = strlen (line); |
|
1993 |
|
1994 /* A line like `/foo:' = new dir foo. Allow both absolute (/...) |
|
1995 and explicitly relative (./...) names here. It's a kludge to |
|
1996 pass in the directory name with the trailing : still attached, |
|
1997 but it doesn't actually hurt. */ |
|
1998 if (len > 0 && line[len - 1] == ':' && kpse_absolute_p (line, true)) |
|
1999 { |
|
2000 /* New directory line. */ |
|
2001 if (! ignore_dir_p (line)) |
|
2002 { |
|
2003 /* If they gave a relative name, prepend full |
|
2004 directory name now. */ |
|
2005 line[len - 1] = DIR_SEP; |
|
2006 |
|
2007 /* Skip over leading `./', it confuses `match' and |
|
2008 is just a waste of space, anyway. This will lose |
|
2009 on `../', but `match' won't work there, either, |
|
2010 so it doesn't matter. */ |
|
2011 |
|
2012 cur_dir = *line == '.' |
|
2013 ? concat (top_dir, line + 2) : xstrdup (line); |
|
2014 |
|
2015 dir_count++; |
|
2016 } |
|
2017 else |
|
2018 { |
|
2019 cur_dir = NULL; |
|
2020 ignore_dir_count++; |
|
2021 } |
|
2022 |
|
2023 /* Ignore blank, `.' and `..' lines. */ |
|
2024 |
|
2025 } |
|
2026 else if (*line != 0 && cur_dir /* a file line? */ |
|
2027 && !(*line == '.' |
|
2028 && (line[1] == '0' |
|
2029 || (line[1] == '.' && line[2] == 0)))) |
|
2030 |
|
2031 { |
|
2032 /* Make a new hash table entry with a key of `line' and |
|
2033 a data of `cur_dir'. An already-existing identical |
|
2034 key is ok, since a file named `foo' can be in more |
|
2035 than one directory. Share `cur_dir' among all its |
|
2036 files (and hence never free it). */ |
|
2037 hash_insert (table, xstrdup (line), cur_dir); |
|
2038 file_count++; |
|
2039 } |
|
2040 /* else ignore blank lines or top-level files |
|
2041 or files in ignored directories */ |
|
2042 |
|
2043 free (line); |
|
2044 } |
|
2045 |
|
2046 xfclose (db_file, db_filename.c_str ()); |
|
2047 |
|
2048 if (file_count == 0) |
|
2049 { |
|
2050 WARNING1 ("kpathsea: No usable entries in %s", db_filename.c_str ()); |
|
2051 WARNING ("kpathsea: See the manual for how to generate ls-R"); |
|
2052 db_file = NULL; |
|
2053 } |
|
2054 else |
|
2055 { |
|
2056 str_list_add (db_dir_list, top_dir); |
|
2057 } |
4378
|
2058 |
|
2059 #ifdef KPSE_DEBUG |
4391
|
2060 if (KPSE_DEBUG_P (KPSE_DEBUG_HASH)) |
|
2061 { |
|
2062 /* Don't make this a debugging bit, since the output is so |
|
2063 voluminous, and being able to specify -1 is too useful. |
|
2064 Instead, let people who want it run the program under |
|
2065 a debugger and change the variable that way. */ |
|
2066 bool hash_summary_only = true; |
|
2067 |
|
2068 DEBUGF4 ("%s: %u entries in %d directories (%d hidden).\n", |
|
2069 db_filename.c_str (), file_count, dir_count, |
|
2070 ignore_dir_count); |
|
2071 |
|
2072 DEBUGF ("ls-R hash table:"); |
|
2073 hash_print (*table, hash_summary_only); |
|
2074 fflush (stderr); |
|
2075 } |
|
2076 #endif /* KPSE_DEBUG */ |
4378
|
2077 } |
|
2078 |
|
2079 free (top_dir); |
|
2080 |
|
2081 return db_file != NULL; |
|
2082 } |
|
2083 |
|
2084 |
|
2085 /* Insert FNAME into the hash table. This is for files that get built |
|
2086 during a run. We wouldn't want to reread all of ls-R, even if it got |
|
2087 rebuilt. */ |
|
2088 |
|
2089 void |
|
2090 kpse_db_insert (const char *passed_fname) |
|
2091 { |
|
2092 /* We might not have found ls-R, or even had occasion to look for it |
|
2093 yet, so do nothing if we have no hash table. */ |
4391
|
2094 if (db.buckets) |
|
2095 { |
|
2096 const char *dir_part; |
|
2097 char *fname = xstrdup (passed_fname); |
|
2098 char *baseptr = xbasename (fname); |
|
2099 const char *file_part = xstrdup (baseptr); |
|
2100 |
|
2101 *baseptr = '\0'; /* Chop off the filename. */ |
|
2102 dir_part = fname; /* That leaves the dir, with the trailing /. */ |
|
2103 |
|
2104 hash_insert (&db, file_part, dir_part); |
|
2105 } |
4378
|
2106 } |
|
2107 |
|
2108 /* Return true if FILENAME could be in PATH_ELT, i.e., if the directory |
|
2109 part of FILENAME matches PATH_ELT. Have to consider // wildcards, but |
|
2110 $ and ~ expansion have already been done. */ |
4391
|
2111 |
4378
|
2112 static bool |
4390
|
2113 match (const std::string& filename_arg, const std::string& path_elt_arg) |
4378
|
2114 { |
4390
|
2115 const char *filename = filename_arg.c_str (); |
|
2116 const char *path_elt = path_elt_arg.c_str (); |
|
2117 |
4378
|
2118 const char *original_filename = filename; |
|
2119 bool matched = false; |
4391
|
2120 |
|
2121 for (; *filename && *path_elt; filename++, path_elt++) |
|
2122 { |
|
2123 if (FILECHARCASEEQ (*filename, *path_elt)) /* normal character match */ |
|
2124 ; |
|
2125 |
|
2126 else if (IS_DIR_SEP (*path_elt) /* at // */ |
|
2127 && original_filename < filename && IS_DIR_SEP (path_elt[-1])) |
|
2128 { |
|
2129 while (IS_DIR_SEP (*path_elt)) |
|
2130 path_elt++; /* get past second and any subsequent /'s */ |
|
2131 |
|
2132 if (*path_elt == 0) |
|
2133 { |
|
2134 /* Trailing //, matches anything. We could make this |
|
2135 part of the other case, but it seems pointless to do |
|
2136 the extra work. */ |
|
2137 matched = true; |
|
2138 break; |
|
2139 } |
|
2140 else |
|
2141 { |
|
2142 /* Intermediate //, have to match rest of PATH_ELT. */ |
|
2143 for (; !matched && *filename; filename++) |
|
2144 { |
|
2145 /* Try matching at each possible character. */ |
|
2146 if (IS_DIR_SEP (filename[-1]) |
|
2147 && FILECHARCASEEQ (*filename, *path_elt)) |
|
2148 matched = match (filename, path_elt); |
|
2149 } |
|
2150 |
|
2151 /* Prevent filename++ when *filename='\0'. */ |
|
2152 break; |
|
2153 } |
|
2154 } |
|
2155 else |
|
2156 /* normal character nonmatch, quit */ |
|
2157 break; |
4378
|
2158 } |
|
2159 |
|
2160 /* If we've reached the end of PATH_ELT, check that we're at the last |
|
2161 component of FILENAME, we've matched. */ |
4391
|
2162 if (! matched && *path_elt == 0) |
|
2163 { |
|
2164 /* Probably PATH_ELT ended with `vf' or some such, and FILENAME |
|
2165 ends with `vf/ptmr.vf'. In that case, we'll be at a |
|
2166 directory separator. On the other hand, if PATH_ELT ended |
|
2167 with a / (as in `vf/'), FILENAME being the same `vf/ptmr.vf', |
|
2168 we'll be at the `p'. Upshot: if we're at a dir separator in |
|
2169 FILENAME, skip it. But if not, that's ok, as long as there |
|
2170 are no more dir separators. */ |
|
2171 |
|
2172 if (IS_DIR_SEP (*filename)) |
|
2173 filename++; |
|
2174 |
|
2175 while (*filename && !IS_DIR_SEP (*filename)) |
|
2176 filename++; |
|
2177 |
|
2178 matched = *filename == 0; |
|
2179 } |
|
2180 |
4378
|
2181 return matched; |
|
2182 } |
|
2183 |
|
2184 /* If DB_DIR is a prefix of PATH_ELT, return true; otherwise false. |
|
2185 That is, the question is whether to try the db for a file looked up |
|
2186 in PATH_ELT. If PATH_ELT == ".", for example, the answer is no. If |
|
2187 PATH_ELT == "/usr/local/lib/texmf/fonts//tfm", the answer is yes. |
4391
|
2188 |
4378
|
2189 In practice, ls-R is only needed for lengthy subdirectory |
|
2190 comparisons, but there's no gain to checking PATH_ELT to see if it is |
|
2191 a subdir match, since the only way to do that is to do a string |
|
2192 search in it, which is all we do anyway. */ |
4391
|
2193 |
4378
|
2194 static bool |
4390
|
2195 elt_in_db (const std::string& db_dir, const std::string& path_elt) |
4378
|
2196 { |
|
2197 bool found = false; |
|
2198 |
4390
|
2199 size_t db_dir_len = db_dir.length (); |
|
2200 size_t path_elt_len = path_elt.length (); |
|
2201 |
|
2202 size_t i = 0; |
|
2203 |
4391
|
2204 while (! found && FILECHARCASEEQ (db_dir[i], path_elt[i])) |
|
2205 { |
|
2206 i++; |
|
2207 /* If we've matched the entire db directory, it's good. */ |
|
2208 if (i == db_dir_len) |
|
2209 found = true; |
|
2210 |
4378
|
2211 /* If we've reached the end of PATH_ELT, but not the end of the db |
|
2212 directory, it's no good. */ |
4391
|
2213 else if (i == path_elt_len) |
|
2214 break; |
|
2215 } |
4378
|
2216 |
|
2217 return found; |
|
2218 } |
|
2219 |
|
2220 /* If ALIAS_FILENAME exists, read it into TABLE. */ |
|
2221 |
|
2222 static bool |
4390
|
2223 alias_build (hash_table_type *table, const std::string& alias_filename) |
4378
|
2224 { |
|
2225 char *line, *real, *alias; |
|
2226 unsigned count = 0; |
4390
|
2227 FILE *alias_file = xfopen (alias_filename.c_str (), "r"); |
4378
|
2228 |
4391
|
2229 if (alias_file) |
|
2230 { |
|
2231 while ((line = read_line (alias_file)) != NULL) |
|
2232 { |
|
2233 /* comments or empty */ |
|
2234 if (*line == 0 || *line == '%' || *line == '#') |
|
2235 /* do nothing */ ; |
|
2236 else |
|
2237 { |
|
2238 /* Each line should have two fields: realname aliasname. */ |
|
2239 real = line; |
|
2240 |
|
2241 while (*real && isspace (*real)) |
|
2242 real++; |
|
2243 |
|
2244 alias = real; |
|
2245 |
|
2246 while (*alias && !isspace (*alias)) |
|
2247 alias++; |
|
2248 |
|
2249 *alias++ = 0; |
|
2250 |
|
2251 while (*alias && isspace (*alias)) |
|
2252 alias++; |
|
2253 |
|
2254 /* Is the check for errors strong enough? Should we |
|
2255 warn the user for potential errors? */ |
|
2256 if (strlen (real) != 0 && strlen (alias) != 0) |
|
2257 { |
|
2258 hash_insert (table, xstrdup (alias), xstrdup (real)); |
|
2259 count++; |
|
2260 } |
|
2261 } |
|
2262 free (line); |
|
2263 } |
4378
|
2264 |
|
2265 #ifdef KPSE_DEBUG |
4391
|
2266 if (KPSE_DEBUG_P (KPSE_DEBUG_HASH)) |
|
2267 { |
|
2268 /* As with ls-R above ... */ |
|
2269 bool hash_summary_only = true; |
|
2270 DEBUGF2 ("%s: %u aliases.\n", alias_filename.c_str (), count); |
|
2271 DEBUGF ("alias hash table:"); |
|
2272 hash_print (*table, hash_summary_only); |
|
2273 fflush (stderr); |
|
2274 } |
|
2275 #endif /* KPSE_DEBUG */ |
|
2276 |
|
2277 xfclose (alias_file, alias_filename.c_str ()); |
4378
|
2278 } |
|
2279 |
|
2280 return alias_file != NULL; |
|
2281 } |
|
2282 |
|
2283 /* Initialize the path for ls-R files, and read them all into the hash |
|
2284 table `db'. If no usable ls-R's are found, set db.buckets to NULL. */ |
|
2285 |
|
2286 void |
|
2287 kpse_init_db (void) |
|
2288 { |
|
2289 bool ok = false; |
4390
|
2290 const std::string db_path = kpse_init_format (); |
|
2291 string_vector db_files = kpse_all_path_search (db_path.c_str (), DB_NAME); |
4378
|
2292 |
|
2293 /* Must do this after the path searching (which ends up calling |
|
2294 kpse_db_search recursively), so db.buckets stays NULL. */ |
|
2295 db = hash_create (DB_HASH_SIZE); |
|
2296 |
4390
|
2297 int len = db_files.length (); |
|
2298 for (int i = 0; i < len; i++) |
|
2299 { |
|
2300 if (! db_files[i].empty ()) |
|
2301 { |
|
2302 if (db_build (&db, db_files[i])) |
|
2303 ok = true; |
|
2304 } |
|
2305 } |
4391
|
2306 |
|
2307 if (! ok) |
|
2308 { |
|
2309 /* If db can't be built, leave `size' nonzero (so we don't |
|
2310 rebuild it), but clear `buckets' (so we don't look in it). */ |
|
2311 free (db.buckets); |
|
2312 db.buckets = NULL; |
|
2313 } |
4378
|
2314 |
|
2315 /* Add the content of any alias databases. There may exist more than |
|
2316 one alias file along DB_NAME files. This duplicates the above code |
|
2317 -- should be a function. */ |
|
2318 ok = false; |
4390
|
2319 db_files = kpse_all_path_search (db_path.c_str (), ALIAS_NAME); |
4378
|
2320 |
|
2321 alias_db = hash_create (ALIAS_HASH_SIZE); |
|
2322 |
4390
|
2323 len = db_files.length (); |
|
2324 for (int i = 0; i < len; i++) |
|
2325 { |
|
2326 if (! db_files[i].empty ()) |
|
2327 { |
|
2328 if (alias_build (&alias_db, db_files[i])) |
|
2329 ok = true; |
|
2330 } |
|
2331 } |
4378
|
2332 |
4391
|
2333 if (! ok) |
|
2334 { |
|
2335 free (alias_db.buckets); |
|
2336 alias_db.buckets = NULL; |
|
2337 } |
4378
|
2338 } |
|
2339 |
|
2340 /* Avoid doing anything if this PATH_ELT is irrelevant to the databases. */ |
|
2341 |
4390
|
2342 string_vector |
|
2343 kpse_db_search (const std::string& name_arg, |
|
2344 const std::string& orig_path_elt_arg, bool all) |
4378
|
2345 { |
|
2346 const char *last_slash; |
|
2347 char *path_elt; |
|
2348 bool done; |
4390
|
2349 string_vector ret; |
|
2350 string_vector aliases; |
4378
|
2351 bool relevant = false; |
4390
|
2352 |
|
2353 const char *name = name_arg.c_str (); |
|
2354 const char *orig_path_elt = orig_path_elt_arg.c_str (); |
4391
|
2355 |
4378
|
2356 /* If we failed to build the database (or if this is the recursive |
|
2357 call to build the db path), quit. */ |
|
2358 if (db.buckets == NULL) |
4390
|
2359 return ret; |
4391
|
2360 |
4378
|
2361 /* When tex-glyph.c calls us looking for, e.g., dpi600/cmr10.pk, we |
|
2362 won't find it unless we change NAME to just `cmr10.pk' and append |
|
2363 `/dpi600' to PATH_ELT. We are justified in using a literal `/' |
|
2364 here, since that's what tex-glyph.c unconditionally uses in |
|
2365 DPI_BITMAP_SPEC. But don't do anything if the / begins NAME; that |
|
2366 should never happen. */ |
|
2367 last_slash = strrchr (name, '/'); |
4390
|
2368 if (last_slash && last_slash != name) |
|
2369 { |
|
2370 unsigned len = last_slash - name + 1; |
|
2371 char *dir_part = (char *) xmalloc (len); |
|
2372 strncpy (dir_part, name, len - 1); |
|
2373 dir_part[len - 1] = 0; |
|
2374 path_elt = concat3 (orig_path_elt, "/", dir_part); |
|
2375 name = last_slash + 1; |
|
2376 } |
|
2377 else |
4378
|
2378 path_elt = (char *) orig_path_elt; |
|
2379 |
|
2380 /* Don't bother doing any lookups if this `path_elt' isn't covered by |
|
2381 any of database directories. We do this not so much because the |
|
2382 extra couple of hash lookups matter -- they don't -- but rather |
|
2383 because we want to return NULL in this case, so path_search can |
|
2384 know to do a disk search. */ |
4390
|
2385 for (int e = 0; !relevant && e < db_dir_list.length (); e++) |
|
2386 relevant = elt_in_db (db_dir_list[e], path_elt); |
|
2387 |
4391
|
2388 if (! relevant) |
4390
|
2389 return ret; |
4378
|
2390 |
|
2391 /* If we have aliases for this name, use them. */ |
|
2392 if (alias_db.buckets) |
|
2393 aliases = hash_lookup (alias_db, name); |
|
2394 |
4390
|
2395 /* Push aliases up by one and insert the original name at the front. */ |
|
2396 int len = aliases.length (); |
|
2397 aliases.resize (len+1); |
|
2398 for (int i = len; i > 0; i--) |
|
2399 aliases[i] = aliases[i - 1]; |
|
2400 aliases[0] = name; |
4378
|
2401 |
|
2402 done = false; |
4390
|
2403 len = aliases.length (); |
|
2404 for (int i = 0; i < len && !done; i++) |
|
2405 { |
|
2406 std::string atry = aliases[i]; |
|
2407 |
|
2408 /* We have an ls-R db. Look up `atry'. */ |
|
2409 string_vector db_dirs = hash_lookup (db, atry); |
|
2410 |
|
2411 /* For each filename found, see if it matches the path element. For |
|
2412 example, if we have .../cx/cmr10.300pk and .../ricoh/cmr10.300pk, |
|
2413 and the path looks like .../cx, we don't want the ricoh file. */ |
|
2414 |
|
2415 int db_dirs_len = db_dirs.length (); |
|
2416 for (int j = 0; j < db_dirs_len && !done; j++) |
|
2417 { |
|
2418 std::string db_file = db_dirs[j] + atry; |
|
2419 bool matched = match (db_file, path_elt); |
4378
|
2420 |
|
2421 #ifdef KPSE_DEBUG |
4390
|
2422 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
4391
|
2423 DEBUGF3 ("db:match (%s,%s) = %d\n", db_file.c_str (), path_elt, matched); |
4378
|
2424 #endif |
|
2425 |
4390
|
2426 /* We got a hit in the database. Now see if the file actually |
|
2427 exists, possibly under an alias. */ |
|
2428 if (matched) |
|
2429 { |
|
2430 std::string found; |
|
2431 if (kpse_readable_file (db_file.c_str ())) |
|
2432 found = db_file; |
|
2433 else |
|
2434 { |
|
2435 /* The hit in the DB doesn't exist in disk. Now try |
|
2436 all its aliases. For example, suppose we have a |
|
2437 hierarchy on CD, thus `mf.bas', but ls-R contains |
|
2438 `mf.base'. Find it anyway. Could probably work |
|
2439 around this with aliases, but this is pretty easy |
|
2440 and shouldn't hurt. The upshot is that if one of |
|
2441 the aliases actually exists, we use that. */ |
|
2442 |
|
2443 int aliases_len = aliases.length (); |
|
2444 |
|
2445 for (int k = 1; k < aliases_len && found.empty (); k++) |
|
2446 { |
|
2447 std::string atry = db_dirs[j] + aliases[k]; |
|
2448 if (kpse_readable_file (atry.c_str ())) |
|
2449 found = atry; |
|
2450 } |
|
2451 } |
4391
|
2452 |
4390
|
2453 /* If we have a real file, add it to the list, maybe done. */ |
|
2454 if (! found.empty ()) |
|
2455 { |
|
2456 str_list_add (ret, found); |
|
2457 if (! (all || found.empty ())) |
|
2458 done = true; |
|
2459 } |
|
2460 } |
|
2461 } |
4378
|
2462 } |
4391
|
2463 |
4378
|
2464 /* If we had to break up NAME, free the temporary PATH_ELT. */ |
|
2465 if (path_elt != orig_path_elt) |
|
2466 free (path_elt); |
|
2467 |
|
2468 return ret; |
|
2469 } |
|
2470 |
|
2471 /* kdefault.c: Expand extra colons. */ |
|
2472 |
|
2473 /* Check for leading colon first, then trailing, then doubled, since |
|
2474 that is fastest. Usually it will be leading or trailing. */ |
|
2475 |
|
2476 char * |
|
2477 kpse_expand_default (const char *path, const char *fallback) |
|
2478 { |
|
2479 unsigned path_length; |
|
2480 char *expansion; |
4391
|
2481 |
4378
|
2482 /* The default path better not be null. */ |
|
2483 assert (fallback); |
4391
|
2484 |
4378
|
2485 if (path == NULL) |
|
2486 expansion = xstrdup (fallback); |
|
2487 |
|
2488 /* Solitary or leading :? */ |
|
2489 else if (IS_ENV_SEP (*path)) |
|
2490 { |
|
2491 expansion = path[1] == 0 ? xstrdup (fallback) : concat (fallback, path); |
|
2492 } |
|
2493 |
|
2494 /* Sorry about the assignment in the middle of the expression, but |
|
2495 conventions were made to be flouted and all that. I don't see the |
|
2496 point of calling strlen twice or complicating the logic just to |
|
2497 avoid the assignment (especially now that I've pointed it out at |
|
2498 such great length). */ |
|
2499 else if (path[(path_length = strlen (path)) - 1] == ENV_SEP) |
|
2500 expansion = concat (path, fallback); |
|
2501 |
|
2502 /* OK, not leading or trailing. Check for doubled. */ |
|
2503 else |
|
2504 { |
|
2505 const char *loc; |
|
2506 |
|
2507 /* What we'll return if we find none. */ |
|
2508 expansion = xstrdup (path); |
|
2509 |
|
2510 for (loc = path; *loc; loc++) |
|
2511 { |
|
2512 if (IS_ENV_SEP (loc[0]) && IS_ENV_SEP (loc[1])) |
|
2513 { /* We have a doubled colon. */ |
|
2514 expansion = (char *) xmalloc (path_length + strlen (fallback) + 1); |
4391
|
2515 |
4378
|
2516 /* Copy stuff up to and including the first colon. */ |
|
2517 strncpy (expansion, path, loc - path + 1); |
|
2518 expansion[loc - path + 1] = 0; |
4391
|
2519 |
4378
|
2520 /* Copy in FALLBACK, and then the rest of PATH. */ |
|
2521 strcat (expansion, fallback); |
|
2522 strcat (expansion, loc + 1); |
|
2523 |
|
2524 break; |
|
2525 } |
|
2526 } |
|
2527 } |
4391
|
2528 |
4378
|
2529 return expansion; |
|
2530 } |
|
2531 |
|
2532 /* elt-dirs.c: Translate a path element to its corresponding |
|
2533 director{y,ies}. */ |
|
2534 |
|
2535 /* To avoid giving prototypes for all the routines and then their real |
|
2536 definitions, we give all the subroutines first. The entry point is |
|
2537 the last routine in the file. */ |
|
2538 |
|
2539 /* Make a copy of DIR (unless it's null) and save it in L. Ensure that |
|
2540 DIR ends with a DIR_SEP for the benefit of later searches. */ |
|
2541 |
|
2542 static void |
4390
|
2543 dir_list_add (str_llist_type *l, const std::string& dir) |
4378
|
2544 { |
4390
|
2545 char last_char = dir[dir.length () - 1]; |
|
2546 std::string saved_dir = dir; |
|
2547 if (IS_DIR_SEP (last_char) || IS_DEVICE_SEP (last_char)) |
|
2548 saved_dir += DIR_SEP_STRING; |
4391
|
2549 |
4378
|
2550 str_llist_add (l, saved_dir); |
|
2551 } |
|
2552 |
4390
|
2553 /* Return true if FN is a directory or a symlink to a directory, |
|
2554 false if not. */ |
|
2555 |
|
2556 static bool |
|
2557 dir_p (const std::string& fn) |
|
2558 { |
|
2559 #ifdef WIN32 |
4391
|
2560 unsigned int fa = GetFileAttributes (fn.c_str ()); |
4390
|
2561 return (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY)); |
|
2562 #else |
|
2563 struct stat stats; |
|
2564 return stat (fn.c_str (), &stats) == 0 && S_ISDIR (stats.st_mode); |
|
2565 #endif |
|
2566 } |
4391
|
2567 |
4378
|
2568 /* If DIR is a directory, add it to the list L. */ |
|
2569 |
|
2570 static void |
4390
|
2571 checked_dir_list_add (str_llist_type *l, const std::string& dir) |
4378
|
2572 { |
|
2573 if (dir_p (dir)) |
|
2574 dir_list_add (l, dir); |
|
2575 } |
|
2576 |
|
2577 /* The cache. Typically, several paths have the same element; for |
|
2578 example, /usr/local/lib/texmf/fonts//. We don't want to compute the |
|
2579 expansion of such a thing more than once. Even though we also cache |
|
2580 the dir_links call, that's not enough -- without this path element |
|
2581 caching as well, the execution time doubles. */ |
|
2582 |
|
2583 typedef struct |
|
2584 { |
|
2585 const char *key; |
|
2586 str_llist_type *value; |
|
2587 } cache_entry; |
|
2588 |
|
2589 static cache_entry *the_cache = NULL; |
|
2590 static unsigned cache_length = 0; |
|
2591 |
|
2592 /* Associate KEY with VALUE. We implement the cache as a simple linear |
|
2593 list, since it's unlikely to ever be more than a dozen or so elements |
|
2594 long. We don't bother to check here if PATH has already been saved; |
|
2595 we always add it to our list. We copy KEY but not VALUE; not sure |
|
2596 that's right, but it seems to be all that's needed. */ |
|
2597 |
|
2598 static void |
|
2599 cache (const char *key, str_llist_type *value) |
|
2600 { |
|
2601 cache_length++; |
|
2602 XRETALLOC (the_cache, cache_length, cache_entry); |
|
2603 the_cache[cache_length - 1].key = xstrdup (key); |
|
2604 the_cache[cache_length - 1].value = value; |
|
2605 } |
|
2606 |
|
2607 |
|
2608 /* To retrieve, just check the list in order. */ |
|
2609 |
|
2610 static str_llist_type * |
|
2611 cached (const char *key) |
|
2612 { |
|
2613 unsigned p; |
4391
|
2614 |
4378
|
2615 for (p = 0; p < cache_length; p++) |
|
2616 { |
|
2617 if (FILESTRCASEEQ (the_cache[p].key, key)) |
|
2618 return the_cache[p].value; |
|
2619 } |
4391
|
2620 |
4378
|
2621 return NULL; |
|
2622 } |
|
2623 |
|
2624 /* Handle the magic path constructs. */ |
|
2625 |
|
2626 /* Declare recursively called routine. */ |
|
2627 static void expand_elt (str_llist_type *, const char *, unsigned); |
|
2628 |
|
2629 |
|
2630 /* POST is a pointer into the original element (which may no longer be |
|
2631 ELT) to just after the doubled DIR_SEP, perhaps to the null. Append |
|
2632 subdirectories of ELT (up to ELT_LENGTH, which must be a /) to |
|
2633 STR_LIST_PTR. */ |
|
2634 |
|
2635 #ifdef WIN32 |
|
2636 /* Shared across recursive calls, it acts like a stack. */ |
|
2637 static char dirname[MAX_PATH]; |
|
2638 #endif |
|
2639 |
|
2640 static void |
|
2641 do_subdir (str_llist_type *str_list_ptr, const char *elt, |
|
2642 unsigned elt_length, const char *post) |
|
2643 { |
|
2644 #ifdef WIN32 |
|
2645 WIN32_FIND_DATA find_file_data; |
|
2646 HANDLE hnd; |
|
2647 int proceed; |
|
2648 #else |
|
2649 DIR *dir; |
|
2650 struct dirent *e; |
|
2651 #endif /* not WIN32 */ |
4389
|
2652 |
|
2653 std::string name (elt, elt_length); |
|
2654 |
4378
|
2655 assert (IS_DIR_SEP (elt[elt_length - 1]) |
|
2656 || IS_DEVICE_SEP (elt[elt_length - 1])); |
4391
|
2657 |
4378
|
2658 #if defined (WIN32) |
4391
|
2659 strcpy (dirname, name.c_str ()); |
|
2660 strcat (dirname, "/*.*"); /* "*.*" or "*" -- seems equivalent. */ |
|
2661 hnd = FindFirstFile (dirname, &find_file_data); |
4378
|
2662 |
4389
|
2663 if (hnd == INVALID_HANDLE_VALUE) |
4378
|
2664 return; |
|
2665 |
|
2666 /* Include top level before subdirectories, if nothing to match. */ |
|
2667 if (*post == 0) |
4390
|
2668 dir_list_add (str_list_ptr, name); |
4378
|
2669 else { |
|
2670 /* If we do have something to match, see if it exists. For |
|
2671 example, POST might be `pk/ljfour', and they might have a |
|
2672 directory `$TEXMF/fonts/pk/ljfour' that we should find. */ |
4389
|
2673 name += post; |
|
2674 expand_elt (str_list_ptr, name.c_str (), elt_length); |
|
2675 name.resize (elt_length); |
4378
|
2676 } |
|
2677 proceed = 1; |
4391
|
2678 while (proceed) |
|
2679 { |
|
2680 if (find_file_data.cFileName[0] != '.') |
|
2681 { |
|
2682 /* Construct the potential subdirectory name. */ |
|
2683 name += find_file_data.cFileName; |
|
2684 if (find_file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
|
2685 { |
|
2686 /* It's a directory, so append the separator. */ |
|
2687 name += DIR_SEP_STRING; |
|
2688 unsigned potential_len = name.length (); |
|
2689 do_subdir (str_list_ptr, name.c_str (), |
|
2690 potential_len, post); |
|
2691 } |
|
2692 name.resize (elt_length); |
|
2693 } |
|
2694 proceed = FindNextFile (hnd, &find_file_data); |
4378
|
2695 } |
4391
|
2696 FindClose (hnd); |
4378
|
2697 |
|
2698 #else /* not WIN32 */ |
|
2699 |
|
2700 /* If we can't open it, quit. */ |
4389
|
2701 dir = opendir (name.c_str ()); |
4378
|
2702 if (dir == NULL) |
4389
|
2703 return; |
4391
|
2704 |
4378
|
2705 /* Include top level before subdirectories, if nothing to match. */ |
|
2706 if (*post == 0) |
4390
|
2707 dir_list_add (str_list_ptr, name); |
4378
|
2708 else |
|
2709 { /* If we do have something to match, see if it exists. For |
|
2710 example, POST might be `pk/ljfour', and they might have a |
|
2711 directory `$TEXMF/fonts/pk/ljfour' that we should find. */ |
4389
|
2712 name += post; |
|
2713 expand_elt (str_list_ptr, name.c_str (), elt_length); |
|
2714 name.resize (elt_length); |
4378
|
2715 } |
|
2716 |
|
2717 while ((e = readdir (dir)) != NULL) |
|
2718 { /* If it begins with a `.', never mind. (This allows ``hidden'' |
|
2719 directories that the algorithm won't find.) */ |
|
2720 if (e->d_name[0] != '.') |
|
2721 { |
|
2722 int links; |
4391
|
2723 |
4378
|
2724 /* Construct the potential subdirectory name. */ |
4389
|
2725 name += e->d_name; |
4391
|
2726 |
4378
|
2727 /* If we can't stat it, or if it isn't a directory, continue. */ |
4389
|
2728 links = dir_links (name.c_str ()); |
4378
|
2729 |
|
2730 if (links >= 0) |
4391
|
2731 { |
4378
|
2732 /* It's a directory, so append the separator. */ |
4389
|
2733 name += DIR_SEP_STRING; |
|
2734 unsigned potential_len = name.length (); |
4391
|
2735 |
4378
|
2736 /* Should we recurse? To see if the subdirectory is a |
|
2737 leaf, check if it has two links (one for . and one for |
|
2738 ..). This means that symbolic links to directories do |
|
2739 not affect the leaf-ness. This is arguably wrong, but |
|
2740 the only alternative I know of is to stat every entry |
|
2741 in the directory, and that is unacceptably slow. |
4391
|
2742 |
4378
|
2743 The #ifdef here makes all this configurable at |
|
2744 compile-time, so that if we're using VMS directories or |
|
2745 some such, we can still find subdirectories, even if it |
|
2746 is much slower. */ |
|
2747 #ifdef ST_NLINK_TRICK |
|
2748 #ifdef AMIGA |
|
2749 /* With SAS/C++ 6.55 on the Amiga, `stat' sets the `st_nlink' |
|
2750 field to -1 for a file, or to 1 for a directory. */ |
|
2751 if (links == 1) |
|
2752 #else |
|
2753 if (links > 2) |
|
2754 #endif /* not AMIGA */ |
|
2755 #endif /* not ST_NLINK_TRICK */ |
|
2756 /* All criteria are met; find subdirectories. */ |
4389
|
2757 do_subdir (str_list_ptr, name.c_str (), |
4378
|
2758 potential_len, post); |
|
2759 #ifdef ST_NLINK_TRICK |
|
2760 else if (*post == 0) |
|
2761 /* Nothing to match, no recursive subdirectories to |
|
2762 look for: we're done with this branch. Add it. */ |
4390
|
2763 dir_list_add (str_list_ptr, name); |
4378
|
2764 #endif |
|
2765 } |
|
2766 |
|
2767 /* Remove the directory entry we just checked from `name'. */ |
4389
|
2768 name.resize (elt_length); |
4378
|
2769 } |
|
2770 } |
4391
|
2771 |
4378
|
2772 xclosedir (dir); |
|
2773 #endif /* not WIN32 */ |
|
2774 } |
|
2775 |
|
2776 |
|
2777 /* Assume ELT is non-empty and non-NULL. Return list of corresponding |
|
2778 directories (with no terminating NULL entry) in STR_LIST_PTR. Start |
|
2779 looking for magic constructs at START. */ |
|
2780 |
|
2781 static void |
|
2782 expand_elt (str_llist_type *str_list_ptr, const char *elt, unsigned start) |
|
2783 { |
|
2784 const char *dir = elt + start; |
|
2785 const char *post; |
4391
|
2786 |
4378
|
2787 while (*dir != 0) |
|
2788 { |
|
2789 if (IS_DIR_SEP (*dir)) |
|
2790 { |
|
2791 /* If two or more consecutive /'s, find subdirectories. */ |
|
2792 if (IS_DIR_SEP (dir[1])) |
|
2793 { |
|
2794 for (post = dir + 1; IS_DIR_SEP (*post); post++) ; |
|
2795 do_subdir (str_list_ptr, elt, dir - elt + 1, post); |
|
2796 return; |
|
2797 } |
|
2798 |
|
2799 /* No special stuff at this slash. Keep going. */ |
|
2800 } |
4391
|
2801 |
4378
|
2802 dir++; |
|
2803 } |
4391
|
2804 |
4378
|
2805 /* When we reach the end of ELT, it will be a normal filename. */ |
|
2806 checked_dir_list_add (str_list_ptr, elt); |
|
2807 } |
|
2808 |
|
2809 /* Here is the entry point. Returns directory list for ELT. */ |
|
2810 |
|
2811 str_llist_type * |
|
2812 kpse_element_dirs (const char *elt) |
|
2813 { |
|
2814 str_llist_type *ret; |
|
2815 |
|
2816 /* If given nothing, return nothing. */ |
4391
|
2817 if (! elt || !*elt) |
4378
|
2818 return NULL; |
|
2819 |
|
2820 /* If we've already cached the answer for ELT, return it. */ |
|
2821 ret = cached (elt); |
|
2822 if (ret) |
|
2823 return ret; |
|
2824 |
|
2825 /* We're going to have a real directory list to return. */ |
4390
|
2826 ret = new str_llist_type; |
4378
|
2827 *ret = NULL; |
|
2828 |
|
2829 /* We handle the hard case in a subroutine. */ |
|
2830 expand_elt (ret, elt, 0); |
|
2831 |
|
2832 /* Remember the directory list we just found, in case future calls are |
|
2833 made with the same ELT. */ |
|
2834 cache (elt, ret); |
|
2835 |
|
2836 #ifdef KPSE_DEBUG |
|
2837 if (KPSE_DEBUG_P (KPSE_DEBUG_EXPAND)) |
|
2838 { |
|
2839 DEBUGF1 ("path element %s =>", elt); |
|
2840 if (ret) |
|
2841 { |
|
2842 str_llist_elt_type *e; |
|
2843 for (e = *ret; e; e = STR_LLIST_NEXT (*e)) |
4390
|
2844 fprintf (stderr, " %s", (STR_LLIST (*e)).c_str ()); |
4378
|
2845 } |
|
2846 putc ('\n', stderr); |
|
2847 fflush (stderr); |
|
2848 } |
|
2849 #endif /* KPSE_DEBUG */ |
|
2850 |
|
2851 return ret; |
|
2852 } |
|
2853 |
|
2854 /* path-elt.c: Return the stuff between colons. */ |
|
2855 |
|
2856 /* The static (but dynamically allocated) area we return the answer in, |
|
2857 and how much we've currently allocated for it. */ |
|
2858 static char *elt = NULL; |
|
2859 static unsigned elt_alloc = 0; |
|
2860 |
|
2861 /* The path we're currently working on. */ |
|
2862 static const char *path = NULL; |
|
2863 |
|
2864 /* Upon entry, the static `path' is at the first (and perhaps last) |
|
2865 character of the return value, or else NULL if we're at the end (or |
|
2866 haven't been called). I make no provision for caching the results; |
|
2867 thus, we parse the same path over and over, on every lookup. If that |
|
2868 turns out to be a significant lose, it can be fixed, but I'm guessing |
|
2869 disk accesses overwhelm everything else. If ENV_P is true, use |
|
2870 IS_ENV_SEP; else use IS_DIR_SEP. */ |
|
2871 |
|
2872 static char * |
|
2873 element (const char *passed_path, bool env_p) |
|
2874 { |
|
2875 const char *p; |
|
2876 char *ret; |
|
2877 int brace_level; |
|
2878 unsigned len; |
4391
|
2879 |
4378
|
2880 if (passed_path) |
|
2881 path = passed_path; |
|
2882 /* Check if called with NULL, and no previous path (perhaps we reached |
|
2883 the end). */ |
4391
|
2884 else if (! path) |
4378
|
2885 return NULL; |
4391
|
2886 |
4378
|
2887 /* OK, we have a non-null `path' if we get here. */ |
|
2888 assert (path); |
|
2889 p = path; |
4391
|
2890 |
4378
|
2891 /* Find the next colon not enclosed by braces (or the end of the path). */ |
|
2892 brace_level = 0; |
|
2893 while (*p != 0 && !(brace_level == 0 |
4391
|
2894 && (env_p ? IS_ENV_SEP (*p) : IS_DIR_SEP (*p)))) |
|
2895 { |
|
2896 if (*p == '{') |
|
2897 ++brace_level; |
|
2898 else if (*p == '}') |
|
2899 --brace_level; |
|
2900 |
|
2901 ++p; |
|
2902 } |
|
2903 |
4378
|
2904 /* Return the substring starting at `path'. */ |
|
2905 len = p - path; |
|
2906 |
|
2907 /* Make sure we have enough space (including the null byte). */ |
|
2908 if (len + 1 > elt_alloc) |
|
2909 { |
|
2910 elt_alloc = len + 1; |
|
2911 elt = (char *) xrealloc (elt, elt_alloc); |
|
2912 } |
|
2913 |
|
2914 strncpy (elt, path, len); |
|
2915 elt[len] = 0; |
|
2916 ret = elt; |
|
2917 |
|
2918 /* If we are at the end, return NULL next time. */ |
|
2919 if (path[len] == 0) |
|
2920 path = NULL; |
|
2921 else |
|
2922 path += len + 1; |
|
2923 |
|
2924 return ret; |
|
2925 } |
|
2926 |
|
2927 char * |
|
2928 kpse_path_element (const char *p) |
|
2929 { |
|
2930 return element (p, true); |
|
2931 } |
|
2932 |
|
2933 char * |
|
2934 kpse_filename_component (const char *p) |
|
2935 { |
|
2936 return element (p, false); |
|
2937 } |
|
2938 |
4385
|
2939 /* xfopen.c: fopen and fclose with error checking. */ |
|
2940 |
|
2941 /* These routines just check the return status from standard library |
|
2942 routines and abort if an error happens. */ |
|
2943 |
|
2944 FILE * |
|
2945 xfopen (const char *filename, const char *mode) |
|
2946 { |
|
2947 FILE *f; |
4391
|
2948 |
4385
|
2949 assert (filename && mode); |
4391
|
2950 |
4385
|
2951 f = fopen (filename, mode); |
|
2952 if (f == NULL) |
|
2953 FATAL_PERROR (filename); |
|
2954 |
|
2955 return f; |
|
2956 } |
|
2957 |
|
2958 void |
|
2959 xfclose (FILE *f, const char *filename) |
|
2960 { |
|
2961 assert (f); |
4391
|
2962 |
4385
|
2963 if (fclose (f) == EOF) |
|
2964 FATAL_PERROR (filename); |
|
2965 } |
|
2966 |
4386
|
2967 #ifndef WIN32 |
4385
|
2968 void |
|
2969 xclosedir (DIR *d) |
|
2970 { |
|
2971 #ifdef CLOSEDIR_VOID |
|
2972 closedir (d); |
|
2973 #else |
|
2974 int ret = closedir (d); |
4391
|
2975 |
4385
|
2976 if (ret != 0) |
|
2977 FATAL ("closedir failed"); |
|
2978 #endif |
|
2979 } |
4386
|
2980 #endif |
4385
|
2981 |
|
2982 /* xmalloc.c: malloc with error checking. */ |
|
2983 |
|
2984 void * |
|
2985 xmalloc (unsigned size) |
|
2986 { |
|
2987 void *new_mem = (void *) malloc (size); |
|
2988 |
|
2989 if (new_mem == NULL) |
|
2990 { |
|
2991 fprintf (stderr, "fatal: memory exhausted (xmalloc of %u bytes).\n", |
|
2992 size); |
|
2993 /* 1 means success on VMS, so pick a random number (ASCII `K'). */ |
|
2994 exit (75); |
|
2995 } |
|
2996 |
|
2997 return new_mem; |
|
2998 } |
|
2999 |
|
3000 /* xrealloc.c: realloc with error checking. */ |
|
3001 |
|
3002 extern void *xmalloc (unsigned); |
|
3003 |
|
3004 void * |
|
3005 xrealloc (void *old_ptr, unsigned size) |
|
3006 { |
|
3007 void *new_mem; |
|
3008 |
|
3009 if (old_ptr == NULL) |
|
3010 new_mem = xmalloc (size); |
|
3011 else |
|
3012 { |
|
3013 new_mem = (void *) realloc (old_ptr, size); |
|
3014 if (new_mem == NULL) |
|
3015 { |
|
3016 /* We used to print OLD_PTR here using %x, and casting its |
|
3017 value to unsigned, but that lost on the Alpha, where |
|
3018 pointers and unsigned had different sizes. Since the info |
|
3019 is of little or no value anyway, just don't print it. */ |
|
3020 fprintf (stderr, "fatal: memory exhausted (realloc of %u bytes).\n", |
|
3021 size); |
|
3022 /* 1 means success on VMS, so pick a random number (ASCII `B'). */ |
|
3023 exit (66); |
|
3024 } |
|
3025 } |
|
3026 |
|
3027 return new_mem; |
|
3028 } |
|
3029 |
|
3030 /* xstrdup.c: strdup with error checking. */ |
|
3031 |
|
3032 /* dir.c: directory operations. */ |
|
3033 |
|
3034 #ifndef WIN32 |
|
3035 |
|
3036 /* Return -1 if FN isn't a directory, else its number of links. |
|
3037 Duplicate the call to stat; no need to incur overhead of a function |
|
3038 call for that little bit of cleanliness. */ |
|
3039 |
|
3040 int |
|
3041 dir_links (const char *fn) |
|
3042 { |
4390
|
3043 std::map<std::string, long> link_table; |
|
3044 |
4385
|
3045 long ret; |
4391
|
3046 |
4390
|
3047 if (link_table.find (fn) != link_table.end ()) |
|
3048 ret = link_table[fn]; |
4385
|
3049 else |
|
3050 { |
|
3051 struct stat stats; |
4390
|
3052 |
4385
|
3053 ret = stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode) |
|
3054 ? stats.st_nlink : (unsigned) -1; |
|
3055 |
4390
|
3056 link_table[fn] = ret; |
4391
|
3057 |
4385
|
3058 #ifdef KPSE_DEBUG |
|
3059 if (KPSE_DEBUG_P (KPSE_DEBUG_STAT)) |
4391
|
3060 DEBUGF2 ("dir_links (%s) => %ld\n", fn, ret); |
4385
|
3061 #endif |
|
3062 } |
|
3063 |
|
3064 return ret; |
|
3065 } |
|
3066 |
|
3067 #endif /* !WIN32 */ |
|
3068 |
|
3069 /* hash.c: hash table operations. */ |
|
3070 |
|
3071 /* The hash function. We go for simplicity here. */ |
|
3072 |
|
3073 /* All our hash tables are related to filenames. */ |
|
3074 #ifdef MONOCASE_FILENAMES |
|
3075 #define TRANSFORM(x) toupper (x) |
|
3076 #else |
|
3077 #define TRANSFORM(x) (x) |
|
3078 #endif |
|
3079 |
|
3080 static unsigned |
4390
|
3081 hash (hash_table_type table, const std::string& key) |
4385
|
3082 { |
|
3083 unsigned n = 0; |
4391
|
3084 |
4385
|
3085 /* Our keys aren't often anagrams of each other, so no point in |
|
3086 weighting the characters. */ |
4390
|
3087 size_t len = key.length (); |
|
3088 for (size_t i = 0; i < len; i++) |
|
3089 n = (n + n + TRANSFORM (key[i])) % table.size; |
4391
|
3090 |
4385
|
3091 return n; |
|
3092 } |
|
3093 |
|
3094 hash_table_type |
4391
|
3095 hash_create (unsigned size) |
4385
|
3096 { |
|
3097 /* hash_table_type ret; changed into "static ..." to work around gcc |
|
3098 optimizer bug for Alpha. */ |
|
3099 static hash_table_type ret; |
|
3100 unsigned b; |
4390
|
3101 ret.buckets = new hash_element_type * [size]; |
4385
|
3102 ret.size = size; |
4391
|
3103 |
4385
|
3104 /* calloc's zeroes aren't necessarily NULL, so be safe. */ |
|
3105 for (b = 0; b <ret.size; b++) |
|
3106 ret.buckets[b] = NULL; |
4391
|
3107 |
4385
|
3108 return ret; |
|
3109 } |
|
3110 |
|
3111 /* Whether or not KEY is already in MAP, insert it and VALUE. Do not |
|
3112 duplicate the strings, in case they're being purposefully shared. */ |
|
3113 |
|
3114 void |
4390
|
3115 hash_insert (hash_table_type *table, const std::string& key, |
|
3116 const std::string& value) |
4385
|
3117 { |
|
3118 unsigned n = hash (*table, key); |
4390
|
3119 hash_element_type *new_elt = new hash_element_type; |
4385
|
3120 |
|
3121 new_elt->key = key; |
|
3122 new_elt->value = value; |
|
3123 new_elt->next = NULL; |
4391
|
3124 |
4385
|
3125 /* Insert the new element at the end of the list. */ |
4391
|
3126 if (! table->buckets[n]) |
4385
|
3127 /* first element in bucket is a special case. */ |
|
3128 table->buckets[n] = new_elt; |
|
3129 else |
|
3130 { |
|
3131 hash_element_type *loc = table->buckets[n]; |
|
3132 while (loc->next) /* Find the last element. */ |
|
3133 loc = loc->next; |
|
3134 loc->next = new_elt; /* Insert the new one after. */ |
|
3135 } |
|
3136 } |
|
3137 |
|
3138 /* Look up STR in MAP. Return a (dynamically-allocated) list of the |
4391
|
3139 corresponding strings or NULL if no match. */ |
4385
|
3140 |
4390
|
3141 static string_vector |
|
3142 hash_lookup (hash_table_type table, const std::string& key) |
4385
|
3143 { |
|
3144 hash_element_type *p; |
4390
|
3145 string_vector ret; |
4385
|
3146 unsigned n = hash (table, key); |
4391
|
3147 |
4385
|
3148 /* Look at everything in this bucket. */ |
|
3149 for (p = table.buckets[n]; p != NULL; p = p->next) |
4390
|
3150 if (FILESTRCASEEQ (key.c_str (), p->key.c_str ())) |
|
3151 /* Cast because the general string_vector shouldn't force const data. */ |
|
3152 str_list_add (ret, p->value); |
4391
|
3153 |
4385
|
3154 #ifdef KPSE_DEBUG |
|
3155 if (KPSE_DEBUG_P (KPSE_DEBUG_HASH)) |
|
3156 { |
4391
|
3157 DEBUGF1 ("hash_lookup (%s) =>", key.c_str ()); |
4390
|
3158 if (ret.empty ()) |
4385
|
3159 fputs (" (nil)\n", stderr); |
|
3160 else |
|
3161 { |
4390
|
3162 int len = ret.length (); |
|
3163 for (int i = 0; i < len; i++) |
4385
|
3164 { |
|
3165 putc (' ', stderr); |
4390
|
3166 fputs (ret[i].c_str (), stderr); |
4385
|
3167 } |
|
3168 putc ('\n', stderr); |
|
3169 } |
|
3170 fflush (stderr); |
|
3171 } |
|
3172 #endif |
|
3173 |
4390
|
3174 return ret; |
4385
|
3175 } |
|
3176 |
|
3177 /* We only print nonempty buckets, to decrease output volume. */ |
|
3178 |
|
3179 void |
|
3180 hash_print (hash_table_type table, int summary_only) |
|
3181 { |
|
3182 unsigned b; |
|
3183 unsigned total_elements = 0, total_buckets = 0; |
4391
|
3184 |
|
3185 for (b = 0; b < table.size; b++) |
|
3186 { |
|
3187 hash_element_type *bucket = table.buckets[b]; |
|
3188 |
|
3189 if (bucket) |
|
3190 { |
|
3191 unsigned len = 1; |
|
3192 hash_element_type *tb; |
|
3193 |
|
3194 total_buckets++; |
|
3195 if (! summary_only) |
|
3196 fprintf (stderr, "%4d ", b); |
|
3197 |
|
3198 for (tb = bucket->next; tb != NULL; tb = tb->next) |
|
3199 len++; |
|
3200 |
|
3201 if (! summary_only) |
|
3202 fprintf (stderr, ":%-5d", len); |
|
3203 |
|
3204 total_elements += len; |
|
3205 |
|
3206 if (! summary_only) |
|
3207 { |
|
3208 for (tb = bucket; tb != NULL; tb = tb->next) |
|
3209 fprintf (stderr, " %s=>%s", tb->key.c_str (), |
|
3210 tb->value.c_str ()); |
|
3211 |
|
3212 putc ('\n', stderr); |
|
3213 } |
|
3214 } |
4385
|
3215 } |
4391
|
3216 |
4385
|
3217 fprintf (stderr, |
4391
|
3218 "%u buckets, %u nonempty (%u%%); %u entries, average chain %.1f.\n", |
|
3219 table.size, |
|
3220 total_buckets, |
|
3221 100 * total_buckets / table.size, |
|
3222 total_elements, |
|
3223 total_buckets ? total_elements / (double) total_buckets : 0.0); |
4385
|
3224 } |
|
3225 |
|
3226 /* concat.c: dynamic string concatenation. */ |
|
3227 |
|
3228 /* Return the concatenation of S1 and S2. See `concatn.c' for a |
|
3229 `concatn', which takes a variable number of arguments. */ |
|
3230 |
4391
|
3231 static char * |
4385
|
3232 concat (const char *s1, const char *s2) |
|
3233 { |
|
3234 char *answer = (char *) xmalloc (strlen (s1) + strlen (s2) + 1); |
|
3235 strcpy (answer, s1); |
|
3236 strcat (answer, s2); |
|
3237 |
|
3238 return answer; |
|
3239 } |
|
3240 |
|
3241 /* concat3.c: concatenate three strings. */ |
|
3242 |
4391
|
3243 static char * |
4385
|
3244 concat3 (const char *s1, const char *s2, const char *s3) |
|
3245 { |
|
3246 char *answer |
|
3247 = (char *) xmalloc (strlen (s1) + strlen (s2) + strlen (s3) + 1); |
|
3248 strcpy (answer, s1); |
|
3249 strcat (answer, s2); |
|
3250 strcat (answer, s3); |
|
3251 |
|
3252 return answer; |
|
3253 } |
|
3254 |
|
3255 /* debug.c: Help the user discover what's going on. */ |
|
3256 |
|
3257 #ifdef KPSE_DEBUG |
|
3258 |
|
3259 unsigned int kpathsea_debug = 0; |
|
3260 |
|
3261 /* If the real definitions of fopen or fclose are macros, we lose -- the |
|
3262 #undef won't restore them. */ |
|
3263 |
|
3264 FILE * |
|
3265 fopen (const char *filename, const char *mode) |
|
3266 { |
|
3267 #undef fopen |
|
3268 FILE *ret = fopen (filename, mode); |
|
3269 |
|
3270 if (KPSE_DEBUG_P (KPSE_DEBUG_FOPEN)) |
4391
|
3271 DEBUGF3 ("fopen (%s, %s) => 0x%lx\n", filename, mode, (unsigned long) ret); |
4385
|
3272 |
|
3273 return ret; |
|
3274 } |
|
3275 |
|
3276 int |
|
3277 fclose (FILE *f) |
|
3278 { |
|
3279 #undef fclose |
|
3280 int ret = fclose (f); |
4391
|
3281 |
4385
|
3282 if (KPSE_DEBUG_P (KPSE_DEBUG_FOPEN)) |
4391
|
3283 DEBUGF2 ("fclose (0x%lx) => %d\n", (unsigned long) f, ret); |
4385
|
3284 |
|
3285 return ret; |
|
3286 } |
|
3287 |
|
3288 #endif |
|
3289 |
|
3290 /* str-list.c: define routines for string lists. */ |
|
3291 |
|
3292 /* See the lib.h file for comments. */ |
|
3293 |
4391
|
3294 static void |
4390
|
3295 str_list_add (string_vector& l, const std::string& s) |
4385
|
3296 { |
4390
|
3297 int len = l.length (); |
|
3298 l.resize (len + 1); |
|
3299 l[len] = s; |
4385
|
3300 } |
|
3301 |
|
3302 /* May as well save some reallocations and do everything in a chunk |
|
3303 instead of calling str_list_add on each element. */ |
4391
|
3304 |
4385
|
3305 void |
4390
|
3306 str_list_concat (string_vector& target, const string_vector& more) |
4385
|
3307 { |
4390
|
3308 int e; |
|
3309 int prev_len = target.length (); |
|
3310 int new_len = prev_len + more.length (); |
|
3311 |
|
3312 target.resize (new_len); |
4391
|
3313 |
4390
|
3314 for (e = 0; e < more.length (); e++) |
|
3315 target[prev_len + e] = more[e]; |
4385
|
3316 } |
|
3317 |
|
3318 /* str-llist.c: Implementation of a linked list of strings. */ |
|
3319 |
|
3320 /* Add the new string STR to the end of the list L. */ |
|
3321 |
|
3322 void |
4390
|
3323 str_llist_add (str_llist_type *l, const std::string& str) |
4385
|
3324 { |
|
3325 str_llist_elt_type *e; |
4390
|
3326 str_llist_elt_type *new_elt = new str_llist_elt_type; |
4391
|
3327 |
4385
|
3328 /* The new element will be at the end of the list. */ |
|
3329 STR_LLIST (*new_elt) = str; |
|
3330 STR_LLIST_MOVED (*new_elt) = 0; |
|
3331 STR_LLIST_NEXT (*new_elt) = NULL; |
4391
|
3332 |
4385
|
3333 /* Find the current end of the list. */ |
|
3334 for (e = *l; e && STR_LLIST_NEXT (*e); e = STR_LLIST_NEXT (*e)) |
|
3335 ; |
4391
|
3336 |
|
3337 if (! e) |
4385
|
3338 *l = new_elt; |
|
3339 else |
|
3340 STR_LLIST_NEXT (*e) = new_elt; |
|
3341 } |
|
3342 |
|
3343 /* Move an element towards the top. The idea is that when a file is |
|
3344 found in a given directory, later files will likely be in that same |
|
3345 directory, and looking for the file in all the directories in between |
|
3346 is thus a waste. */ |
|
3347 |
|
3348 void |
|
3349 str_llist_float (str_llist_type *l, str_llist_elt_type *mover) |
|
3350 { |
|
3351 str_llist_elt_type *last_moved, *unmoved; |
4391
|
3352 |
4385
|
3353 /* If we've already moved this element, never mind. */ |
|
3354 if (STR_LLIST_MOVED (*mover)) |
|
3355 return; |
4391
|
3356 |
4385
|
3357 /* Find the first unmoved element (to insert before). We're |
|
3358 guaranteed this will terminate, since MOVER itself is currently |
|
3359 unmoved, and it must be in L (by hypothesis). */ |
|
3360 for (last_moved = NULL, unmoved = *l; STR_LLIST_MOVED (*unmoved); |
|
3361 last_moved = unmoved, unmoved = STR_LLIST_NEXT (*unmoved)) |
|
3362 ; |
|
3363 |
|
3364 /* If we are the first unmoved element, nothing to relink. */ |
|
3365 if (unmoved != mover) |
|
3366 { /* Remember `mover's current successor, so we can relink `mover's |
|
3367 predecessor to it. */ |
|
3368 str_llist_elt_type *before_mover; |
|
3369 str_llist_elt_type *after_mover = STR_LLIST_NEXT (*mover); |
4391
|
3370 |
4385
|
3371 /* Find `mover's predecessor. */ |
|
3372 for (before_mover = unmoved; STR_LLIST_NEXT (*before_mover) != mover; |
|
3373 before_mover = STR_LLIST_NEXT (*before_mover)) |
|
3374 ; |
4391
|
3375 |
4385
|
3376 /* `before_mover' now links to `after_mover'. */ |
|
3377 STR_LLIST_NEXT (*before_mover) = after_mover; |
|
3378 |
|
3379 /* Insert `mover' before `unmoved' and after `last_moved' (or at |
|
3380 the head of the list). */ |
|
3381 STR_LLIST_NEXT (*mover) = unmoved; |
4391
|
3382 if (! last_moved) |
4385
|
3383 *l = mover; |
|
3384 else |
|
3385 STR_LLIST_NEXT (*last_moved) = mover; |
|
3386 } |
|
3387 |
|
3388 /* We've moved it. */ |
|
3389 STR_LLIST_MOVED (*mover) = 1; |
|
3390 } |
|
3391 |
|
3392 /* variable.c: variable expansion. */ |
|
3393 |
|
3394 |
|
3395 /* We have to keep track of variables being expanded, otherwise |
|
3396 constructs like TEXINPUTS = $TEXINPUTS result in an infinite loop. |
|
3397 (Or indirectly recursive variables, etc.) Our simple solution is to |
|
3398 add to a list each time an expansion is started, and check the list |
|
3399 before expanding. */ |
|
3400 |
4391
|
3401 static std::map <std::string, bool> expansions; |
4385
|
3402 |
|
3403 static void |
4391
|
3404 expanding (const std::string& var, bool xp) |
4385
|
3405 { |
4391
|
3406 expansions[var] = xp; |
4385
|
3407 } |
|
3408 |
|
3409 /* Return whether VAR is currently being expanding. */ |
|
3410 |
4391
|
3411 static bool |
|
3412 expanding_p (const std::string& var) |
4385
|
3413 { |
4391
|
3414 return (expansions.find (var) != expansions.end ()) |
|
3415 ? expansions[var] : false; |
4385
|
3416 } |
|
3417 |
|
3418 /* Append the result of value of `var' to EXPANSION, where `var' begins |
|
3419 at START and ends at END. If `var' is not set, do not complain. |
|
3420 This is a subroutine for the more complicated expansion function. */ |
|
3421 |
|
3422 static void |
4391
|
3423 expand (std::string &expansion, const std::string& var) |
4385
|
3424 { |
4391
|
3425 if (expanding_p (var)) |
|
3426 { |
|
3427 WARNING1 ("kpathsea: variable `%s' references itself (eventually)", |
|
3428 var.c_str ()); |
4385
|
3429 } |
4391
|
3430 else |
|
3431 { |
|
3432 /* Check for an environment variable. */ |
|
3433 std::string value = octave_env::getenv (var); |
|
3434 |
|
3435 if (! value.empty ()) |
|
3436 { |
|
3437 expanding (var, true); |
|
3438 std::string tmp = kpse_var_expand (value); |
|
3439 expanding (var, false); |
|
3440 expansion += tmp; |
|
3441 } |
|
3442 } |
4385
|
3443 } |
|
3444 |
|
3445 /* Can't think of when it would be useful to change these (and the |
|
3446 diagnostic messages assume them), but ... */ |
|
3447 #ifndef IS_VAR_START /* starts all variable references */ |
|
3448 #define IS_VAR_START(c) ((c) == '$') |
|
3449 #endif |
|
3450 #ifndef IS_VAR_CHAR /* variable name constituent */ |
|
3451 #define IS_VAR_CHAR(c) (isalnum (c) || (c) == '_') |
|
3452 #endif |
|
3453 #ifndef IS_VAR_BEGIN_DELIMITER /* start delimited variable name (after $) */ |
|
3454 #define IS_VAR_BEGIN_DELIMITER(c) ((c) == '{') |
|
3455 #endif |
|
3456 #ifndef IS_VAR_END_DELIMITER |
|
3457 #define IS_VAR_END_DELIMITER(c) ((c) == '}') |
|
3458 #endif |
|
3459 |
|
3460 |
|
3461 /* Maybe we should support some or all of the various shell ${...} |
|
3462 constructs, especially ${var-value}. */ |
|
3463 |
4389
|
3464 std::string |
4391
|
3465 kpse_var_expand (const std::string& src) |
4385
|
3466 { |
4389
|
3467 std::string expansion; |
4391
|
3468 |
|
3469 size_t src_len = src.length (); |
|
3470 |
4385
|
3471 /* Copy everything but variable constructs. */ |
4391
|
3472 for (size_t i = 0; i < src_len; i++) |
|
3473 { |
|
3474 if (IS_VAR_START (src[i])) |
|
3475 { |
|
3476 i++; |
|
3477 |
|
3478 /* Three cases: `$VAR', `${VAR}', `$<anything-else>'. */ |
|
3479 if (IS_VAR_CHAR (src[i])) |
|
3480 { |
|
3481 /* $V: collect name constituents, then expand. */ |
|
3482 size_t var_end = i; |
|
3483 |
|
3484 do |
|
3485 { |
|
3486 var_end++; |
|
3487 } |
|
3488 while (IS_VAR_CHAR (src[var_end])); |
|
3489 |
|
3490 var_end--; /* had to go one past */ |
|
3491 expand (expansion, src.substr (i, var_end - i + 1)); |
|
3492 i = var_end; |
|
3493 |
|
3494 } |
|
3495 else if (IS_VAR_BEGIN_DELIMITER (src[i])) |
|
3496 { |
|
3497 /* ${: scan ahead for matching delimiter, then expand. */ |
|
3498 size_t var_end = ++i; |
|
3499 |
|
3500 while (var_end < src_len && !IS_VAR_END_DELIMITER (src[var_end])) |
|
3501 var_end++; |
|
3502 |
|
3503 if (var_end == src_len) |
|
3504 { |
|
3505 WARNING1 ("%s: No matching } for ${", src.c_str ()); |
|
3506 i = var_end - 1; /* will incr to eos at top of loop */ |
|
3507 } |
|
3508 else |
|
3509 { |
|
3510 expand (expansion, src.substr (i, var_end - i)); |
|
3511 i = var_end; /* will incr past } at top of loop*/ |
|
3512 } |
|
3513 } |
|
3514 else |
|
3515 { |
|
3516 /* $<something-else>: error. */ |
|
3517 WARNING2 ("%s: Unrecognized variable construct `$%c'", |
|
3518 src.c_str (), src[i]); |
|
3519 /* Just ignore those chars and keep going. */ |
|
3520 } |
|
3521 } |
|
3522 else |
|
3523 expansion += src[i]; |
|
3524 } |
4389
|
3525 |
|
3526 return expansion; |
4385
|
3527 } |