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