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