Mercurial > octave
annotate liboctave/util/kpse.cc @ 27379:3db033e86376
use m_ prefix for data members in most liboctave/util classes
Files affected: cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h,
glob-match.cc, glob-match.h, kpse.cc, kpse.h, lo-array-errwarn.cc,
lo-array-errwarn.h, lo-regexp.cc, lo-regexp.h, oct-inttypes.h,
oct-mutex.cc, oct-mutex.h, oct-shlib.cc, oct-shlib.h, oct-sort.cc,
oct-sort.h, octave-preserve-stream-state.h, singleton-cleanup.cc, and
singleton-cleanup.h.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 09 Sep 2019 16:15:40 -0400 |
parents | 00f796120a6d |
children | 12f28f9368ec |
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 |
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
26175
diff
changeset
|
6 Copyright (C) 2003-2019 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 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24199
diff
changeset
|
13 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
14 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24199
diff
changeset
|
15 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
16 (at your option) any later version. |
16768 | 17 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
18 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
19 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
21 GNU General Public License for more details. |
16768 | 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 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24199
diff
changeset
|
25 <https://www.gnu.org/licenses/>. |
16768 | 26 |
27 */ | |
4378 | 28 |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
29 #if defined (HAVE_CONFIG_H) |
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
30 # include "config.h" |
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
31 #endif |
4378 | 32 |
21874 | 33 #include <cctype> |
34 #include <cerrno> | |
35 #include <cstdlib> | |
36 | |
4390 | 37 #include <map> |
21868
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
38 #include <fstream> |
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
39 #include <iostream> |
4389 | 40 #include <string> |
41 | |
21926 | 42 #include "dir-ops.h" |
21882
d8104206e8a9
additional cleanups for kpse.cc
John W. Eaton <jwe@octave.org>
parents:
21874
diff
changeset
|
43 #include "file-ops.h" |
21865 | 44 #include "file-stat.h" |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
45 #include "kpse.h" |
4391 | 46 #include "oct-env.h" |
47 #include "oct-passwd.h" | |
22061
737b1d9d7ee3
use octave::sys::time instead of C library time function
John W. Eaton <jwe@octave.org>
parents:
21979
diff
changeset
|
48 #include "oct-time.h" |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
49 #include "pathsearch.h" |
21910
4d723ba06b4a
provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents:
21887
diff
changeset
|
50 #include "unistd-wrappers.h" |
4385 | 51 |
21979
d04da18a407a
use OCTAVE_USE_WINDOWS_API more consistently
John W. Eaton <jwe@octave.org>
parents:
21926
diff
changeset
|
52 #if defined (OCTAVE_USE_WINDOWS_API) |
21874 | 53 # define WIN32_LEAN_AND_MEAN 1 |
54 # include <windows.h> | |
55 #endif | |
4385 | 56 |
21874 | 57 // Define the characters which separate components of filenames and |
58 // environment variable paths. | |
4385 | 59 |
21882
d8104206e8a9
additional cleanups for kpse.cc
John W. Eaton <jwe@octave.org>
parents:
21874
diff
changeset
|
60 #define IS_DEVICE_SEP(ch) octave::sys::file_ops::is_dev_sep (ch) |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22115
diff
changeset
|
61 #define NAME_BEGINS_WITH_DEVICE(name) \ |
21882
d8104206e8a9
additional cleanups for kpse.cc
John W. Eaton <jwe@octave.org>
parents:
21874
diff
changeset
|
62 (name.length () > 0 && IS_DEVICE_SEP ((name)[1])) |
d8104206e8a9
additional cleanups for kpse.cc
John W. Eaton <jwe@octave.org>
parents:
21874
diff
changeset
|
63 |
d8104206e8a9
additional cleanups for kpse.cc
John W. Eaton <jwe@octave.org>
parents:
21874
diff
changeset
|
64 #define DIR_SEP_STRING octave::sys::file_ops::dir_sep_str () |
d8104206e8a9
additional cleanups for kpse.cc
John W. Eaton <jwe@octave.org>
parents:
21874
diff
changeset
|
65 #define IS_DIR_SEP(ch) octave::sys::file_ops::is_dir_sep (ch) |
21863
a25110491607
eliminate obsolete sysdir.h header file
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
66 |
21882
d8104206e8a9
additional cleanups for kpse.cc
John W. Eaton <jwe@octave.org>
parents:
21874
diff
changeset
|
67 #define ENV_SEP octave::directory_path::path_sep_char () |
d8104206e8a9
additional cleanups for kpse.cc
John W. Eaton <jwe@octave.org>
parents:
21874
diff
changeset
|
68 #define ENV_SEP_STRING octave::directory_path::path_sep_str () |
d8104206e8a9
additional cleanups for kpse.cc
John W. Eaton <jwe@octave.org>
parents:
21874
diff
changeset
|
69 #define IS_ENV_SEP(ch) octave::directory_path::is_path_sep (ch) |
21874 | 70 |
71 // 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
|
72 #if ! defined (NO_DEBUG) |
4385 | 73 |
21874 | 74 // OK, we'll have tracing support. |
75 # define KPSE_DEBUG | |
4385 | 76 |
21874 | 77 // Test if a bit is on. |
21882
d8104206e8a9
additional cleanups for kpse.cc
John W. Eaton <jwe@octave.org>
parents:
21874
diff
changeset
|
78 # define KPSE_DEBUG_P(bit) (kpse_debug & (1 << (bit))) |
4385 | 79 |
21874 | 80 # define KPSE_DEBUG_STAT 0 // stat calls |
81 # define KPSE_DEBUG_EXPAND 1 // path element expansion | |
82 # define KPSE_DEBUG_SEARCH 2 // searches | |
83 # define KPSE_DEBUG_VARS 3 // variable values | |
84 # define KPSE_LAST_DEBUG KPSE_DEBUG_VARS | |
4385 | 85 |
21874 | 86 #endif |
4385 | 87 |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
88 unsigned int kpse_debug = 0; |
4399 | 89 |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
90 void |
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
91 kpse_path_iterator::set_end (void) |
4394 | 92 { |
27379
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
93 m_e = m_b + 1; |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
94 |
27379
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
95 if (m_e == m_len) |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
96 ; // OK, we have found the last element. |
27379
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
97 else if (m_e > m_len) |
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
98 m_b = m_e = std::string::npos; |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
99 else |
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
100 { |
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
101 // Find the next colon not enclosed by braces (or the end of the |
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
102 // path). |
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
103 |
27379
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
104 while (m_e < m_len && ! octave::directory_path::is_path_sep (m_path[m_e])) |
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
105 m_e++; |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
106 } |
4394 | 107 } |
108 | |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
109 void |
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
110 kpse_path_iterator::next (void) |
4399 | 111 { |
27379
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
112 m_b = m_e + 1; |
4399 | 113 |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
114 // Skip any consecutive colons. |
27379
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
115 while (m_b < m_len && octave::directory_path::is_path_sep (m_path[m_b])) |
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
116 m_b++; |
4399 | 117 |
27379
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
118 if (m_b >= m_len) |
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
119 m_b = m_e = std::string::npos; |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
120 else |
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
121 set_end (); |
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
122 } |
4399 | 123 |
4391 | 124 /* Truncate any too-long components in NAME, returning the result. It's |
125 too bad this is necessary. See comments in readable.c for why. */ | |
126 | |
4393 | 127 static std::string |
128 kpse_truncate_filename (const std::string& name) | |
4391 | 129 { |
130 unsigned c_len = 0; /* Length of current component. */ | |
131 unsigned ret_len = 0; /* Length of constructed result. */ | |
132 | |
4393 | 133 std::string ret = name; |
134 | |
27379
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
135 size_t m_len = name.length (); |
4393 | 136 |
27379
3db033e86376
use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
137 for (size_t i = 0; i < m_len; i++) |
4391 | 138 { |
4393 | 139 if (IS_DIR_SEP (name[i]) || IS_DEVICE_SEP (name[i])) |
4391 | 140 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
141 /* At a directory delimiter, reset component length. */ |
4391 | 142 c_len = 0; |
143 } | |
22115
b017351e92e1
* kpse.cc (kpse_truncate_filename): Use namespace qualifier for static dir_entry function.
John W. Eaton <jwe@octave.org>
parents:
22061
diff
changeset
|
144 else if (c_len > octave::sys::dir_entry::max_name_length ()) |
4391 | 145 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
146 /* If past the max for a component, ignore this character. */ |
4391 | 147 continue; |
148 } | |
149 | |
150 /* Copy this character. */ | |
4393 | 151 ret[ret_len++] = name[i]; |
4391 | 152 c_len++; |
153 } | |
154 | |
4393 | 155 ret.resize (ret_len); |
4391 | 156 |
157 return ret; | |
158 } | |
159 | |
160 /* If access can read FN, run stat (assigning to stat buffer ST) and | |
161 check that fn is not a directory. Don't check for just being a | |
162 regular file, as it is potentially useful to read fifo's or some | |
163 kinds of devices. */ | |
164 | |
21979
d04da18a407a
use OCTAVE_USE_WINDOWS_API more consistently
John W. Eaton <jwe@octave.org>
parents:
21926
diff
changeset
|
165 #if defined (OCTAVE_USE_WINDOWS_API) |
4391 | 166 static inline bool |
21865 | 167 READABLE (const std::string& fn) |
4391 | 168 { |
4393 | 169 const char *t = fn.c_str (); |
170 return (GetFileAttributes (t) != 0xFFFFFFFF | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
171 && ! (GetFileAttributes (t) & FILE_ATTRIBUTE_DIRECTORY)); |
4391 | 172 } |
173 #else | |
174 static inline bool | |
21865 | 175 READABLE (const std::string& fn) |
4391 | 176 { |
21865 | 177 bool retval = false; |
178 | |
4393 | 179 const char *t = fn.c_str (); |
21865 | 180 |
21910
4d723ba06b4a
provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents:
21887
diff
changeset
|
181 if (octave_access_wrapper (t, octave_access_r_ok ()) == 0) |
21865 | 182 { |
183 octave::sys::file_stat fs (fn); | |
184 | |
185 retval = fs && ! fs.is_dir (); | |
186 } | |
187 | |
188 return retval; | |
4391 | 189 } |
190 #endif | |
191 | |
192 /* POSIX invented the brain-damage of not necessarily truncating | |
193 filename components; the system's behavior is defined by the value of | |
194 the symbol _POSIX_NO_TRUNC, but you can't change it dynamically! | |
195 | |
196 Generic const return warning. See extend-fname.c. */ | |
197 | |
4393 | 198 static std::string |
199 kpse_readable_file (const std::string& name) | |
4391 | 200 { |
4393 | 201 std::string ret; |
4391 | 202 |
21865 | 203 if (READABLE (name)) |
4391 | 204 { |
4393 | 205 ret = name; |
4391 | 206 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21690
diff
changeset
|
207 #if defined (ENAMETOOLONG) |
4391 | 208 } |
209 else if (errno == ENAMETOOLONG) | |
210 { | |
211 ret = kpse_truncate_filename (name); | |
212 | |
213 /* 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
|
214 so let's call access again. */ |
4391 | 215 |
21865 | 216 if (! READABLE (ret)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
217 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
218 /* Failed. */ |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20955
diff
changeset
|
219 ret = ""; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
220 } |
4391 | 221 #endif /* ENAMETOOLONG */ |
222 | |
223 } | |
224 else | |
225 { | |
226 /* Some other error. */ | |
227 if (errno == EACCES) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
228 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
229 /* Maybe warn them if permissions are bad. */ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
230 perror (name.c_str ()); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
231 } |
4393 | 232 |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20955
diff
changeset
|
233 ret = ""; |
4391 | 234 } |
235 | |
236 return ret; | |
237 } | |
238 | |
239 static bool | |
240 kpse_absolute_p (const std::string& filename, int relative_ok) | |
241 { | |
21865 | 242 return (octave::sys::env::absolute_pathname (filename) |
243 || (relative_ok | |
244 && octave::sys::env::rooted_relative_pathname (filename))); | |
4391 | 245 } |
246 | |
4378 | 247 /* The very first search is for texmf.cnf, called when someone tries to |
248 initialize the TFM path or whatever. init_path calls kpse_cnf_get | |
249 which calls kpse_all_path_search to find all the texmf.cnf's. We | |
250 need to do various special things in this case, since we obviously | |
251 don't yet have the configuration files when we're searching for the | |
252 configuration files. */ | |
253 static bool first_search = true; | |
254 | |
21868
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
255 /* This function is called after every search. */ |
4378 | 256 |
257 static void | |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
258 log_search (const std::list<std::string>& filenames) |
4378 | 259 { |
21868
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
260 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
4391 | 261 { |
23433
c9fab0bc983e
maint: Use convention 'int& x' for naming references.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
262 for (const auto& filename : filenames) |
22061
737b1d9d7ee3
use octave::sys::time instead of C library time function
John W. Eaton <jwe@octave.org>
parents:
21979
diff
changeset
|
263 { |
737b1d9d7ee3
use octave::sys::time instead of C library time function
John W. Eaton <jwe@octave.org>
parents:
21979
diff
changeset
|
264 octave::sys::time now; |
23807
336f89b6208b
Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
265 std::cerr << now.unix_time () << ' ' << filename << std::endl; |
22061
737b1d9d7ee3
use octave::sys::time instead of C library time function
John W. Eaton <jwe@octave.org>
parents:
21979
diff
changeset
|
266 } |
4378 | 267 } |
268 } | |
4392 | 269 |
4378 | 270 /* Concatenate each element in DIRS with NAME (assume each ends with a |
271 /, to save time). If SEARCH_ALL is false, return the first readable | |
272 regular file. Else continue to search for more. In any case, if | |
273 none, return a list containing just NULL. | |
274 | |
275 We keep a single buffer for the potential filenames and reallocate | |
276 only when necessary. I'm not sure it's noticeably faster, but it | |
277 does seem cleaner. (We do waste a bit of space in the return | |
278 value, though, since we don't shrink it to the final size returned.) */ | |
279 | |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
280 static std::list<std::string> |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
281 dir_search (const std::string& dir, const std::string& name, |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
282 bool search_all) |
4378 | 283 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
284 std::list<std::string> ret; |
4378 | 285 |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
286 std::string potential = dir + name; |
4393 | 287 |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
288 std::string tmp = kpse_readable_file (potential); |
4393 | 289 |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
290 if (! tmp.empty ()) |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
291 { |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
292 ret.push_back (potential); |
4391 | 293 |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
294 if (! search_all) |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
295 return ret; |
4378 | 296 } |
4391 | 297 |
4378 | 298 return ret; |
299 } | |
4392 | 300 |
4378 | 301 /* This is called when NAME is absolute or explicitly relative; if it's |
302 readable, return (a list containing) it; otherwise, return NULL. */ | |
303 | |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
304 static std::list<std::string> |
4393 | 305 absolute_search (const std::string& name) |
4378 | 306 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
307 std::list<std::string> ret_list; |
4393 | 308 std::string found = kpse_readable_file (name); |
4391 | 309 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14155
diff
changeset
|
310 /* Add 'found' to the return list even if it's null; that tells |
4378 | 311 the caller we didn't find anything. */ |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
312 ret_list.push_back (found); |
4391 | 313 |
4378 | 314 return ret_list; |
315 } | |
4392 | 316 |
4378 | 317 /* This is the hard case -- look for NAME in PATH. If ALL is false, |
318 return the first file found. Otherwise, search all elements of PATH. */ | |
319 | |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
320 static std::list<std::string> |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
321 path_search (const std::string& path, const std::string& name, bool all) |
4378 | 322 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
323 std::list<std::string> ret_list; |
4378 | 324 bool done = false; |
4390 | 325 |
8021 | 326 for (kpse_path_iterator pi (path); ! done && pi != std::string::npos; pi++) |
4390 | 327 { |
4394 | 328 std::string elt = *pi; |
329 | |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
330 std::list<std::string> found; |
4390 | 331 |
332 /* Do not touch the device if present */ | |
333 if (NAME_BEGINS_WITH_DEVICE (elt)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
334 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
335 while (elt.length () > 3 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
336 && 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
|
337 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
338 elt[2] = elt[1]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
339 elt[1] = elt[0]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
340 elt = elt.substr (1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
341 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
342 } |
4390 | 343 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
344 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
345 /* We never want to search the whole disk. */ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
346 while (elt.length () > 1 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
347 && 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
|
348 elt = elt.substr (1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
349 } |
4391 | 350 |
21872 | 351 /* Our caller (search), also tests first_search, and does |
352 the resetting. */ | |
21865 | 353 if (first_search) |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
354 found = std::list<std::string> (); |
21865 | 355 |
356 /* Search the filesystem. */ | |
357 | |
358 if (found.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
359 { |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
360 std::string dir = kpse_element_dir (elt); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
361 |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
362 if (! dir.empty ()) |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
363 found = dir_search (dir, name, all); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
364 } |
4390 | 365 |
366 /* Did we find anything anywhere? */ | |
367 if (! found.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
368 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
369 if (all) |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
370 ret_list.splice (ret_list.end (), found); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
371 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
372 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
373 ret_list.push_back (found.front ()); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
374 done = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
375 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
376 } |
4378 | 377 } |
378 | |
379 return ret_list; | |
4390 | 380 } |
4392 | 381 |
26175
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
382 /* If NAME has a leading ~ or ~user, Unix-style, expand it to the user's |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
383 home directory, and return a new malloced string. If no ~, or no |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
384 <pwd.h>, just return NAME. */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
385 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
386 static std::string |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
387 kpse_tilde_expand (const std::string& name) |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
388 { |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
389 std::string expansion; |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
390 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
391 /* If no leading tilde, do nothing. */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
392 if (name.empty () || name[0] != '~') |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
393 { |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
394 expansion = name; |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
395 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
396 /* If a bare tilde, return the home directory or '.'. (Very |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
397 unlikely that the directory name will do anyone any good, but |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
398 ... */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
399 } |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
400 else if (name.length () == 1) |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
401 { |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
402 expansion = octave::sys::env::get_home_directory (); |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
403 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
404 if (expansion.empty ()) |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
405 expansion = "."; |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
406 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
407 /* If '~/', remove any trailing / or replace leading // in $HOME. |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
408 Should really check for doubled intermediate slashes, too. */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
409 } |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
410 else if (IS_DIR_SEP (name[1])) |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
411 { |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
412 unsigned c = 1; |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
413 std::string home = octave::sys::env::get_home_directory (); |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
414 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
415 if (home.empty ()) |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
416 home = "."; |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
417 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
418 size_t home_len = home.length (); |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
419 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
420 /* handle leading // */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
421 if (home_len > 1 && IS_DIR_SEP (home[0]) && IS_DIR_SEP (home[1])) |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
422 home = home.substr (1); |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
423 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
424 /* omit / after ~ */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
425 if (IS_DIR_SEP (home[home_len - 1])) |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
426 c++; |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
427 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
428 expansion = home + name.substr (c); |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
429 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
430 /* If '~user' or '~user/', look up user in the passwd database (but |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
431 OS/2 doesn't have this concept. */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
432 } |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
433 else |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
434 #if defined (HAVE_PWD_H) |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
435 { |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
436 unsigned c = 2; |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
437 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
438 /* find user name */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
439 while (name.length () > c && ! IS_DIR_SEP (name[c])) |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
440 c++; |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
441 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
442 std::string user = name.substr (1, c-1); |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
443 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
444 /* We only need the cast here for (deficient) systems |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
445 which do not declare 'getpwnam' in <pwd.h>. */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
446 octave::sys::password p = octave::sys::password::getpwnam (user); |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
447 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
448 /* If no such user, just use '.'. */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
449 std::string home = (p ? p.dir () : "."); |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
450 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
451 if (home.empty ()) |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
452 home = "."; |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
453 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
454 /* handle leading // */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
455 if (home.length () > 1 && IS_DIR_SEP (home[0]) && IS_DIR_SEP (home[1])) |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
456 home = home.substr (1); |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
457 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
458 /* If HOME ends in /, omit the / after ~user. */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
459 if (name.length () > c && IS_DIR_SEP (home.back ())) |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
460 c++; |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
461 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
462 expansion = (name.length () > c ? home : home + name.substr (c)); |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
463 } |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
464 #else /* not HAVE_PWD_H */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
465 expansion = name; |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
466 #endif /* not HAVE_PWD_H */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
467 |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
468 return expansion; |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
469 } |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
470 |
4378 | 471 /* Search PATH for ORIGINAL_NAME. If ALL is false, or ORIGINAL_NAME is |
472 absolute_p, check ORIGINAL_NAME itself. Otherwise, look at each | |
473 element of PATH for the first readable ORIGINAL_NAME. | |
4391 | 474 |
4378 | 475 Always return a list; if no files are found, the list will |
476 contain just NULL. If ALL is true, the list will be | |
477 terminated with NULL. */ | |
478 | |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
479 static std::list<std::string> |
4390 | 480 search (const std::string& path, const std::string& original_name, |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
481 bool all) |
4378 | 482 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
483 std::list<std::string> ret_list; |
4378 | 484 bool absolute_p; |
485 | |
26175
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
486 /* Make a leading ~ count as an absolute filename. */ |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
487 std::string name = kpse_tilde_expand (original_name); |
4391 | 488 |
4378 | 489 /* If the first name is absolute or explicitly relative, no need to |
490 consider PATH at all. */ | |
491 absolute_p = kpse_absolute_p (name, true); | |
4391 | 492 |
4378 | 493 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) |
21868
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
494 std::cerr << "kdebug: start search (file=" << name |
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
495 << ", find_all=" << all << ", path=" << path << ")." |
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
496 << std::endl; |
4378 | 497 |
498 /* Find the file(s). */ | |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
499 ret_list = (absolute_p |
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
500 ? absolute_search (name) |
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
501 : path_search (path, name, all)); |
4391 | 502 |
4378 | 503 /* The very first search is for texmf.cnf. We can't log that, since |
504 we want to allow setting TEXMFLOG in texmf.cnf. */ | |
4391 | 505 if (first_search) |
506 { | |
507 first_search = false; | |
508 } | |
509 else | |
510 { | |
511 /* 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
|
512 debugging line if we're doing that. */ |
4391 | 513 |
514 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) | |
21868
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
515 std::cerr << "kdebug: search (" << original_name << ") =>"; |
4391 | 516 |
517 log_search (ret_list); | |
518 | |
519 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) | |
21868
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
520 std::cerr << std::endl; |
4391 | 521 } |
4378 | 522 |
4390 | 523 return ret_list; |
4378 | 524 } |
4392 | 525 |
4378 | 526 /* Search PATH for the first NAME. */ |
527 | |
26175
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
528 /* Perform tilde expansion on NAME. If the result is an absolute or |
4399 | 529 explicitly relative filename, check whether it is a readable |
530 (regular) file. | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11501
diff
changeset
|
531 |
4399 | 532 Otherwise, look in each of the directories specified in PATH (also do |
21865 | 533 tilde and variable expansion on elements in PATH). |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11501
diff
changeset
|
534 |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21732
diff
changeset
|
535 The caller must expand PATH. This is because it makes more sense to |
4399 | 536 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
|
537 |
4399 | 538 In any case, return the complete filename if found, otherwise NULL. */ |
539 | |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
540 std::string |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
541 kpse_path_search (const std::string& path, const std::string& name) |
4378 | 542 { |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
543 std::list<std::string> ret_list = search (path, name, false); |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
544 |
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
545 return ret_list.empty () ? "" : ret_list.front (); |
4378 | 546 } |
547 | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14155
diff
changeset
|
548 /* Like 'kpse_path_search' with MUST_EXIST true, but return a list of |
4399 | 549 all the filenames (or NULL if none), instead of taking the first. */ |
550 | |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
551 std::list<std::string> |
4390 | 552 kpse_all_path_search (const std::string& path, const std::string& name) |
4378 | 553 { |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
554 return search (path, name, true); |
4378 | 555 } |
4392 | 556 |
4378 | 557 /* This is the hard case -- look in each element of PATH for each |
558 element of NAMES. If ALL is false, return the first file found. | |
559 Otherwise, search all elements of PATH. */ | |
560 | |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
561 std::list<std::string> |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
562 path_find_first_of (const std::string& path, |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
563 const std::list<std::string>& names, bool all) |
4378 | 564 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
565 std::list<std::string> ret_list; |
4378 | 566 bool done = false; |
4390 | 567 |
8021 | 568 for (kpse_path_iterator pi (path); ! done && pi != std::string::npos; pi++) |
4378 | 569 { |
4394 | 570 std::string elt = *pi; |
571 | |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
572 std::string dir; |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
573 std::list<std::string> found; |
4378 | 574 |
575 /* Do not touch the device if present */ | |
576 | |
577 if (NAME_BEGINS_WITH_DEVICE (elt)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
578 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
579 while (elt.length () > 3 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
580 && 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
|
581 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
582 elt[2] = elt[1]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
583 elt[1] = elt[0]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
584 elt = elt.substr (1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
585 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
586 } |
4378 | 587 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
588 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
589 /* We never want to search the whole disk. */ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
590 while (elt.length () > 1 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
591 && 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
|
592 elt = elt.substr (1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
593 } |
4378 | 594 |
595 /* We have to search one directory at a time. */ | |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
596 dir = kpse_element_dir (elt); |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
597 |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
598 if (! dir.empty ()) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
599 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
600 for (auto it = names.cbegin (); it != names.cend () && ! done; it++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
601 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
602 std::string name = *it; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
603 |
21872 | 604 /* Our caller (find_first_of), also tests first_search, |
605 and does the resetting. */ | |
21865 | 606 if (first_search) |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
607 found = std::list<std::string> (); |
21865 | 608 |
609 /* Search the filesystem. */ | |
610 | |
611 if (found.empty ()) | |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
612 found = dir_search (dir, name, all); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
613 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
614 /* Did we find anything anywhere? */ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
615 if (! found.empty ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
616 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
617 if (all) |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
618 ret_list.splice (ret_list.end (), found); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
619 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
620 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
621 ret_list.push_back (found.front ()); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
622 done = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
623 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
624 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
625 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
626 } |
4378 | 627 } |
628 | |
629 return ret_list; | |
4391 | 630 } |
4378 | 631 |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
632 static std::list<std::string> |
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
633 find_first_of (const std::string& path, const std::list<std::string>& names, |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
634 bool all) |
4378 | 635 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
636 std::list<std::string> ret_list; |
4378 | 637 |
638 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) | |
639 { | |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
640 std::cerr << "kdebug: start find_first_of ("; |
4391 | 641 |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
642 for (auto p = names.cbegin (); p != names.cend (); p++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
643 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
644 if (p == names.cbegin ()) |
21868
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
645 std::cerr << *p; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
646 else |
21868
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
647 std::cerr << ", " << *p; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
648 } |
4391 | 649 |
23807
336f89b6208b
Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
650 std::cerr << "), path=" << path << '.' << std::endl; |
4378 | 651 } |
652 | |
23433
c9fab0bc983e
maint: Use convention 'int& x' for naming references.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
653 for (const auto& name : names) |
4409 | 654 { |
655 if (kpse_absolute_p (name, true)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
656 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
657 /* 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
|
658 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
|
659 are done. */ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
660 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
661 ret_list = absolute_search (name); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
662 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
663 if (! ret_list.empty ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
664 return ret_list; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
665 } |
4409 | 666 } |
667 | |
4378 | 668 /* Find the file. */ |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
669 ret_list = path_find_first_of (path, names, all); |
4378 | 670 |
671 /* The very first search is for texmf.cnf. We can't log that, since | |
672 we want to allow setting TEXMFLOG in texmf.cnf. */ | |
4391 | 673 if (first_search) |
674 { | |
675 first_search = false; | |
676 } | |
677 else | |
678 { | |
679 /* 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
|
680 debugging line if we're doing that. */ |
4391 | 681 |
682 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
683 { |
21868
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
684 std::cerr << "kdebug: find_first_of ("; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
685 |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
686 for (auto p = names.cbegin (); p != names.cend (); p++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
687 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
688 if (p == names.cbegin ()) |
24199
2ac103974d00
Remove extra space between scope operator '::' and function.
Rik <rik@octave.org>
parents:
23829
diff
changeset
|
689 std::cerr << *p; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
690 else |
21868
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
691 std::cerr << ", " << *p; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
692 } |
10411 | 693 |
21868
e2796ea8feac
* kpse.cc: Use iostream for debug messages.
John W. Eaton <jwe@octave.org>
parents:
21867
diff
changeset
|
694 std::cerr << ") =>"; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
695 } |
4391 | 696 |
697 log_search (ret_list); | |
698 | |
699 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH)) | |
21887
525b46f1d48f
* kpse.cc (find_first_of): Use iostream instead of stdio for debug message.
John W. Eaton <jwe@octave.org>
parents:
21884
diff
changeset
|
700 std::cerr << std::endl; |
4391 | 701 } |
4378 | 702 |
4390 | 703 return ret_list; |
4378 | 704 } |
705 | |
706 /* Search each element of PATH for each element of NAMES. Return the | |
707 first one found. */ | |
708 | |
4399 | 709 /* Search each element of PATH for each element in the list of NAMES. |
710 Return the first one found. */ | |
711 | |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
712 std::string |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
713 kpse_path_find_first_of (const std::string& path, |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
714 const std::list<std::string>& names) |
4378 | 715 { |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
716 std::list<std::string> ret_list = find_first_of (path, names, false); |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
717 |
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
718 return ret_list.empty () ? "" : ret_list.front (); |
4378 | 719 } |
720 | |
721 /* Search each element of PATH for each element of NAMES and return a | |
722 list containing everything found, in the order found. */ | |
723 | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14155
diff
changeset
|
724 /* Like 'kpse_path_find_first_of' with MUST_EXIST true, but return a |
4399 | 725 list of all the filenames (or NULL if none), instead of taking the |
726 first. */ | |
727 | |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
728 std::list<std::string> |
4390 | 729 kpse_all_path_find_first_of (const std::string& path, |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
730 const std::list<std::string>& names) |
4378 | 731 { |
21884
e8a8cb1a7258
eliminate unused parameter in kpse functions
John W. Eaton <jwe@octave.org>
parents:
21883
diff
changeset
|
732 return find_first_of (path, names, true); |
4378 | 733 } |
734 | |
26175
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
735 /* Perform tilde expansion on each element of the path, and include |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
736 canonical directory names for only the the actually existing |
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
737 directories in the result. */ |
4399 | 738 |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
739 std::string |
4397 | 740 kpse_path_expand (const std::string& path) |
4378 | 741 { |
4392 | 742 std::string ret; |
26175
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
743 unsigned len = 0; |
4392 | 744 |
4378 | 745 /* Now expand each of the path elements, printing the results */ |
26175
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
746 for (kpse_path_iterator pi (path); pi != std::string::npos; pi++) |
4391 | 747 { |
26175
6e1a800dd365
eliminate brace and variable expansion in PATH search code
John W. Eaton <jwe@octave.org>
parents:
26174
diff
changeset
|
748 std::string elt = kpse_tilde_expand (*pi); |
4394 | 749 |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
750 std::string dir; |
4391 | 751 |
752 /* Do not touch the device if present */ | |
753 if (NAME_BEGINS_WITH_DEVICE (elt)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
754 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
755 while (elt.length () > 3 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
756 && 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
|
757 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
758 elt[2] = elt[1]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
759 elt[1] = elt[0]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
760 elt = elt.substr (1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
761 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
762 } |
4391 | 763 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
764 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
765 /* We never want to search the whole disk. */ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
766 while (elt.length () > 1 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
767 && 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
|
768 elt = elt.substr (1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
769 } |
4378 | 770 |
4394 | 771 /* 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
|
772 Be faster to check the database, but this is more reliable. */ |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
773 dir = kpse_element_dir (elt); |
4394 | 774 |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
775 size_t dirlen = dir.length (); |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
776 |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
777 if (dirlen > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
778 { |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
779 ret += dir; |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
780 len += dirlen; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
781 |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
782 /* Retain trailing slash if that's the root directory. */ |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
783 if (dirlen == 1 |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
784 || (dirlen == 3 && NAME_BEGINS_WITH_DEVICE (dir) |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
785 && IS_DIR_SEP (dir[2]))) |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
786 { |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
787 ret += ENV_SEP_STRING; |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
788 len++; |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
789 } |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
790 |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
791 ret[len-1] = ENV_SEP; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
792 } |
4378 | 793 } |
4391 | 794 |
23812
057a894914df
Use C++11 string fcns back() and pop_back() to simplify code.
Rik <rik@octave.org>
parents:
23807
diff
changeset
|
795 if (! ret.empty ()) |
057a894914df
Use C++11 string fcns back() and pop_back() to simplify code.
Rik <rik@octave.org>
parents:
23807
diff
changeset
|
796 ret.pop_back (); |
4391 | 797 |
4378 | 798 return ret; |
799 } | |
4392 | 800 |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21732
diff
changeset
|
801 /* braces.c -- code for doing word expansion in curly braces. Taken from |
16768 | 802 bash 1.14.5. [And subsequently modified for kpatshea.] |
803 | |
804 Copyright (C) 1987,1991 Free Software Foundation, Inc. */ | |
4378 | 805 |
4391 | 806 #define brace_whitespace(c) (! (c) || (c) == ' ' || (c) == '\t' || (c) == '\n') |
4378 | 807 |
808 /* Basic idea: | |
809 | |
810 Segregate the text into 3 sections: preamble (stuff before an open brace), | |
811 postamble (stuff after the matching close brace) and amble (stuff after | |
812 preamble, and before postamble). Expand amble, and then tack on the | |
813 expansions to preamble. Expand postamble, and tack on the expansions to | |
4391 | 814 the result so far. */ |
4378 | 815 |
4397 | 816 /* Return a new array of strings which is the result of appending each |
817 string in ARR2 to each string in ARR1. The resultant array is | |
818 len (arr1) * len (arr2) long. For convenience, ARR1 (and its contents) | |
819 are free ()'ed. ARR1 can be NULL, in that case, a new version of ARR2 | |
820 is returned. */ | |
821 | |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
822 static std::list<std::string> |
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
823 array_concat (const std::list<std::string>& arr1, |
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
824 const std::list<std::string>& arr2) |
4378 | 825 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
826 std::list<std::string> result; |
4397 | 827 |
828 if (arr1.empty ()) | |
829 result = arr2; | |
830 else if (arr2.empty ()) | |
831 result = arr1; | |
832 else | |
833 { | |
23433
c9fab0bc983e
maint: Use convention 'int& x' for naming references.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
834 for (const auto& elt_2 : arr2) |
c9fab0bc983e
maint: Use convention 'int& x' for naming references.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
835 for (const auto& elt_1 : arr1) |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
836 result.push_back (elt_1 + elt_2); |
4397 | 837 } |
838 | |
839 return result; | |
4378 | 840 } |
841 | |
4397 | 842 static int brace_gobbler (const std::string&, int&, int); |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
843 static std::list<std::string> expand_amble (const std::string&); |
4378 | 844 |
845 /* Return an array of strings; the brace expansion of TEXT. */ | |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
846 static std::list<std::string> |
4397 | 847 brace_expand (const std::string& text) |
4378 | 848 { |
849 /* Find the text of the preamble. */ | |
4397 | 850 int i = 0; |
851 int c = brace_gobbler (text, i, '{'); | |
852 | |
853 std::string preamble = text.substr (0, i); | |
854 | |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
855 std::list<std::string> result (1, preamble); |
4397 | 856 |
857 if (c == '{') | |
4378 | 858 { |
4397 | 859 /* Find the amble. This is the stuff inside this set of braces. */ |
860 int start = ++i; | |
861 c = brace_gobbler (text, i, '}'); | |
862 | |
863 /* What if there isn't a matching close brace? */ | |
864 if (! c) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
865 { |
19410
95c533ed464b
use warning IDs for all warnings in liboctave
John W. Eaton <jwe@octave.org>
parents:
18084
diff
changeset
|
866 (*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
|
867 ("Octave:pathsearch-syntax", |
95c533ed464b
use warning IDs for all warnings in liboctave
John W. Eaton <jwe@octave.org>
parents:
18084
diff
changeset
|
868 "%s: Unmatched {", text.c_str ()); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
869 |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
870 result = std::list<std::string> (1, text); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
871 } |
4397 | 872 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
873 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
874 std::string amble = text.substr (start, i-start); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
875 result = array_concat (result, expand_amble (amble)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
876 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
877 std::string postamble = text.substr (i+1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
878 result = array_concat (result, brace_expand (postamble)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
879 } |
4378 | 880 } |
881 | |
4397 | 882 return result; |
4378 | 883 } |
884 | |
4397 | 885 /* The character which is used to separate arguments. */ |
886 static int brace_arg_separator = ','; | |
887 | |
4378 | 888 /* Expand the text found inside of braces. We simply try to split the |
889 text at BRACE_ARG_SEPARATORs into separate strings. We then brace | |
890 expand each slot which needs it, until there are no more slots which | |
891 need it. */ | |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
892 static std::list<std::string> |
4397 | 893 expand_amble (const std::string& text) |
4378 | 894 { |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
895 std::list<std::string> result; |
4397 | 896 |
897 size_t text_len = text.length (); | |
898 size_t start; | |
899 int i, c; | |
900 | |
901 for (start = 0, i = 0, c = 1; c && start < text_len; start = ++i) | |
4378 | 902 { |
4397 | 903 int i0 = i; |
904 int c0 = brace_gobbler (text, i0, brace_arg_separator); | |
905 int i1 = i; | |
906 int c1 = brace_gobbler (text, i1, ENV_SEP); | |
4378 | 907 c = c0 | c1; |
908 i = (i0 < i1 ? i0 : i1); | |
909 | |
4397 | 910 std::string tem = text.substr (start, i-start); |
911 | |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
912 std::list<std::string> partial = brace_expand (tem); |
4397 | 913 |
914 if (result.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
915 result = partial; |
4378 | 916 else |
21867
0cdfd6d230e6
use std::list<std::string> instead of string_vector in pathsearch functions
John W. Eaton <jwe@octave.org>
parents:
21865
diff
changeset
|
917 result.splice (result.end (), partial); |
4378 | 918 } |
4397 | 919 |
920 return result; | |
4378 | 921 } |
922 | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21732
diff
changeset
|
923 /* Start at INDEX, and skip characters in TEXT. Set INDEX to the |
4378 | 924 index of the character matching SATISFY. This understands about |
925 quoting. Return the character that caused us to stop searching; | |
926 this is either the same as SATISFY, or 0. */ | |
927 static int | |
4397 | 928 brace_gobbler (const std::string& text, int& indx, int satisfy) |
4378 | 929 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
930 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
|
931 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
|
932 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
|
933 int pass_next = 0; |
4397 | 934 |
935 size_t text_len = text.length (); | |
936 | |
937 size_t i = indx; | |
938 | |
939 for (; i < text_len; i++) | |
4378 | 940 { |
4397 | 941 c = text[i]; |
942 | |
4378 | 943 if (pass_next) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
944 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
945 pass_next = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
946 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
947 } |
4378 | 948 |
949 /* 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
|
950 escape the quote character in a double-quoted string. */ |
4378 | 951 if (c == '\\' && (quoted == 0 || quoted == '"' || quoted == '`')) |
952 { | |
953 pass_next = 1; | |
954 continue; | |
955 } | |
956 | |
957 if (quoted) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
958 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
959 if (c == quoted) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
960 quoted = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
961 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
962 } |
4378 | 963 |
964 if (c == '"' || c == '\'' || c == '`') | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
965 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
966 quoted = c; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
967 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
968 } |
4391 | 969 |
20955
77f5591878bf
maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents:
20791
diff
changeset
|
970 if (c == satisfy && ! level && ! quoted) |
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 /* 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
|
973 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
|
974 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
|
975 if (c == '{' |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
976 && ((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
|
977 && (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
|
978 && (brace_whitespace (text[i+1]) || text[i+1] == '}')))) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
979 continue; |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14155
diff
changeset
|
980 /* 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
|
981 in a '${ }' construct */ |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
982 if ((c != '{') || i == 0 || (text[i-1] != '$')) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
983 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
984 } |
4378 | 985 |
986 if (c == '{') | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
987 level++; |
4378 | 988 else if (c == '}' && level) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
989 level--; |
4378 | 990 } |
991 | |
4397 | 992 indx = i; |
26084
8eae32b6bce0
Don't segfault for unmatched brace in PATH (bug #55013).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
993 c = (c == satisfy) ? c : 0; |
4397 | 994 return c; |
4378 | 995 } |
996 | |
4390 | 997 /* Return true if FN is a directory or a symlink to a directory, |
998 false if not. */ | |
999 | |
1000 static bool | |
1001 dir_p (const std::string& fn) | |
1002 { | |
21865 | 1003 octave::sys::file_stat fs (fn); |
1004 | |
1005 return (fs && fs.is_dir ()); | |
4390 | 1006 } |
4391 | 1007 |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
1008 /* Given a path element ELT, return a the element with a trailing slash |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
1009 or an empty string if the element is not a directory. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11501
diff
changeset
|
1010 |
4399 | 1011 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
|
1012 most likely only useful to be called from 'kpse_path_search', which |
4399 | 1013 has already assumed expansion has been done. */ |
1014 | |
21883
02add2f597a1
compile kpse.cc separately from pathsearch.cc
John W. Eaton <jwe@octave.org>
parents:
21882
diff
changeset
|
1015 std::string |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
1016 kpse_element_dir (const std::string& elt) |
4378 | 1017 { |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
1018 std::string ret; |
4378 | 1019 |
1020 /* If given nothing, return nothing. */ | |
4398 | 1021 if (elt.empty ()) |
21871
cab605836305
use std::list instead of custom list type in pathsearch code
John W. Eaton <jwe@octave.org>
parents:
21870
diff
changeset
|
1022 return ret; |
4378 | 1023 |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
1024 if (dir_p (elt)) |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
1025 { |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
1026 ret = elt; |
4378 | 1027 |
23812
057a894914df
Use C++11 string fcns back() and pop_back() to simplify code.
Rik <rik@octave.org>
parents:
23807
diff
changeset
|
1028 char last_char = ret.back (); |
21873
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
1029 |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
1030 if (! (IS_DIR_SEP (last_char) || IS_DEVICE_SEP (last_char))) |
40195d04b17c
still more simplification of pathsearch
John W. Eaton <jwe@octave.org>
parents:
21872
diff
changeset
|
1031 ret += DIR_SEP_STRING; |
4378 | 1032 } |
1033 | |
1034 return ret; | |
1035 } |