1786
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1786
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
2021
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
1786
|
27 #include <cstdlib> |
|
28 |
|
29 #include <string> |
|
30 |
3024
|
31 #include "lo-utils.h" |
3833
|
32 #include "oct-env.h" |
3505
|
33 #include "oct-kpse.h" |
1786
|
34 #include "pathsearch.h" |
|
35 #include "str-vec.h" |
3024
|
36 #include "str-vec.h" |
1786
|
37 |
3174
|
38 static bool octave_kpathsea_initialized = false; |
1786
|
39 |
|
40 string_vector |
|
41 dir_path::elements (void) |
|
42 { |
|
43 return initialized ? pv : string_vector (); |
|
44 } |
|
45 |
|
46 string_vector |
|
47 dir_path::all_directories (void) |
|
48 { |
|
49 int count = 0; |
|
50 string_vector retval; |
|
51 |
|
52 if (initialized) |
|
53 { |
|
54 int len = pv.length (); |
|
55 |
|
56 int nmax = len > 32 ? len : 32; |
|
57 |
|
58 retval.resize (len); |
|
59 |
|
60 for (int i = 0; i < len; i++) |
|
61 { |
3505
|
62 str_llist_type *elt_dirs |
|
63 = ::octave_kpse_element_dirs (pv[i].c_str ()); |
1786
|
64 |
3415
|
65 if (elt_dirs) |
1786
|
66 { |
3415
|
67 str_llist_elt_type *dir; |
1786
|
68 |
3415
|
69 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1786
|
70 { |
3415
|
71 char *elt_dir = STR_LLIST (*dir); |
1786
|
72 |
3415
|
73 if (elt_dir) |
|
74 { |
|
75 if (count == nmax) |
|
76 nmax *= 2; |
1786
|
77 |
3415
|
78 retval.resize (nmax); |
|
79 |
|
80 retval[count++] = elt_dir; |
|
81 } |
1786
|
82 } |
|
83 } |
|
84 } |
|
85 |
|
86 retval.resize (count); |
|
87 } |
|
88 |
|
89 return retval; |
|
90 } |
|
91 |
3504
|
92 std::string |
|
93 dir_path::find_first (const std::string& nm) |
1786
|
94 { |
3504
|
95 std::string retval; |
1786
|
96 |
|
97 if (initialized) |
|
98 { |
3505
|
99 char *tmp = ::octave_kpse_path_search (p.c_str (), nm.c_str (), true); |
4242
|
100 |
|
101 if (tmp) |
|
102 { |
|
103 retval = tmp; |
|
104 free (tmp); |
|
105 } |
|
106 } |
|
107 |
|
108 return retval; |
|
109 } |
|
110 |
|
111 static string_vector |
|
112 make_retval (char **tmp) |
|
113 { |
|
114 string_vector retval; |
|
115 |
|
116 if (tmp) |
|
117 { |
|
118 int count = 0; |
|
119 char **ptr = tmp; |
|
120 while (*ptr++) |
|
121 count++; |
|
122 |
|
123 retval.resize (count); |
|
124 |
|
125 for (int i = 0; i < count; i++) |
|
126 retval[i] = tmp[i]; |
|
127 } |
|
128 |
|
129 return retval; |
|
130 } |
|
131 |
|
132 static void |
|
133 free_c_array (char **tmp) |
|
134 { |
|
135 if (tmp) |
|
136 { |
|
137 char **ptr = tmp; |
|
138 |
|
139 while (char *elt = *ptr++) |
|
140 if (elt) |
|
141 free (elt); |
|
142 |
|
143 free (tmp); |
|
144 } |
|
145 } |
|
146 |
|
147 string_vector |
|
148 dir_path::find_all (const std::string& nm) |
|
149 { |
|
150 string_vector retval; |
|
151 |
|
152 if (initialized) |
|
153 { |
|
154 char **tmp = ::octave_kpse_all_path_search (p.c_str (), nm.c_str ()); |
|
155 |
|
156 retval = make_retval (tmp); |
|
157 |
|
158 free_c_array (tmp); |
|
159 } |
|
160 |
|
161 return retval; |
|
162 } |
|
163 |
|
164 static const char ** |
|
165 make_c_names (const string_vector& names) |
|
166 { |
|
167 int len = names.length (); |
|
168 |
|
169 const char **c_names = new const char *[len+1]; |
|
170 |
|
171 for (int i = 0; i < len; i++) |
|
172 c_names[i] = strsave (names[i].c_str ()); |
|
173 |
|
174 c_names[len] = 0; |
|
175 |
|
176 return c_names; |
|
177 } |
|
178 |
|
179 static void |
|
180 delete_c_names (const char **c_names) |
|
181 { |
|
182 const char **p = c_names; |
|
183 |
|
184 while (const char *elt = *p++) |
|
185 delete [] elt; |
|
186 |
|
187 delete [] c_names; |
|
188 } |
|
189 |
|
190 std::string |
|
191 dir_path::find_first_of (const string_vector& names) |
|
192 { |
|
193 std::string retval; |
|
194 |
|
195 if (initialized) |
|
196 { |
|
197 const char **c_names = make_c_names (names); |
|
198 |
|
199 char *tmp = ::octave_kpse_path_find_first_of (p.c_str (), c_names, true); |
|
200 |
|
201 delete_c_names (c_names); |
|
202 |
1786
|
203 if (tmp) |
|
204 { |
|
205 retval = tmp; |
|
206 free (tmp); |
|
207 } |
|
208 } |
|
209 |
|
210 return retval; |
|
211 } |
|
212 |
|
213 string_vector |
4242
|
214 dir_path::find_all_first_of (const string_vector& names) |
1786
|
215 { |
|
216 string_vector retval; |
|
217 |
3174
|
218 if (initialized) |
1786
|
219 { |
4242
|
220 const char **c_names = make_c_names (names); |
|
221 |
|
222 char **tmp = ::octave_kpse_all_path_find_first_of (p.c_str (), c_names); |
1786
|
223 |
4242
|
224 delete_c_names (c_names); |
1786
|
225 |
4242
|
226 retval = make_retval (tmp); |
3174
|
227 |
4242
|
228 free_c_array (tmp); |
1786
|
229 } |
|
230 |
|
231 return retval; |
|
232 } |
|
233 |
|
234 void |
3504
|
235 dir_path::set_program_name (const std::string& nm) |
3024
|
236 { |
3834
|
237 std::string selfautodir = octave_env::getenv ("SELFAUTODIR"); |
|
238 std::string selfautoloc = octave_env::getenv ("SELFAUTOLOC"); |
|
239 std::string selfautoparent = octave_env::getenv ("SELFAUTOPARENT"); |
|
240 |
3505
|
241 ::octave_kpse_set_progname (nm.c_str ()); |
3833
|
242 |
|
243 // Calling kpse_set_progname has the unfortunate side-effect of |
3834
|
244 // exporting the following variables. If they were empty when we |
|
245 // started, we make them empty again so that they will not interfere |
|
246 // with TeX if it is run as a subprocess of Octave (if they were set |
|
247 // before, we want to preserve their values). |
3833
|
248 // |
|
249 // XXX FIXME XXX -- is there a reasonable way to actually remove |
|
250 // them from the environment? |
|
251 |
3834
|
252 if (selfautodir.empty ()) |
|
253 octave_env::putenv ("SELFAUTODIR", ""); |
|
254 |
|
255 if (selfautoloc.empty ()) |
|
256 octave_env::putenv ("SELFAUTOLOC", ""); |
|
257 |
|
258 if (selfautoparent.empty ()) |
|
259 octave_env::putenv ("SELFAUTOPARENT", ""); |
3024
|
260 } |
|
261 |
|
262 void |
1786
|
263 dir_path::init (void) |
|
264 { |
3174
|
265 if (! octave_kpathsea_initialized) |
1786
|
266 { |
|
267 char *s = getenv ("KPATHSEA_DEBUG"); |
|
268 |
|
269 if (s) |
|
270 kpathsea_debug |= atoi (s); |
3174
|
271 |
|
272 octave_kpathsea_initialized = true; |
1786
|
273 } |
|
274 |
3196
|
275 char *t1 = 0; |
|
276 |
|
277 if (p_default.empty ()) |
3505
|
278 t1 = ::octave_kpse_path_expand (p_orig.c_str ()); |
3196
|
279 else |
3174
|
280 { |
3505
|
281 char *t2 |
|
282 = ::octave_kpse_expand_default (p_orig.c_str (), p_default.c_str ()); |
3196
|
283 |
3505
|
284 t1 = ::octave_kpse_path_expand (t2); |
3196
|
285 |
|
286 if (t2) |
|
287 free (t2); |
|
288 } |
|
289 |
|
290 if (t1) |
|
291 { |
|
292 p = t1; |
|
293 free (t1); |
3174
|
294 } |
|
295 else |
3504
|
296 p = std::string (); |
3174
|
297 |
1786
|
298 int count = 0; |
3505
|
299 char *path_elt = ::octave_kpse_path_element (p.c_str ()); |
1786
|
300 while (path_elt) |
|
301 { |
3505
|
302 path_elt = ::octave_kpse_path_element (0); |
1786
|
303 count++; |
|
304 } |
|
305 |
|
306 pv.resize (count); |
|
307 |
3505
|
308 path_elt = ::octave_kpse_path_element (p.c_str ()); |
1786
|
309 |
|
310 for (int i = 0; i < count; i++) |
|
311 { |
|
312 pv[i] = path_elt; |
3505
|
313 path_elt = ::octave_kpse_path_element (0); |
1786
|
314 } |
|
315 |
|
316 initialized = true; |
|
317 } |
|
318 |
|
319 /* |
|
320 ;;; Local Variables: *** |
|
321 ;;; mode: C++ *** |
|
322 ;;; End: *** |
|
323 */ |