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