3326
|
1 /* |
|
2 |
|
3 Copyright (C) 1999 John W. Eaton |
|
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 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #if defined (WITH_SHL) |
|
28 #include <cerrno> |
|
29 #include <cstring> |
|
30 #endif |
|
31 |
|
32 extern "C" |
|
33 { |
|
34 #if defined (WITH_DL) |
|
35 #if defined (HAVE_DLFCN_H) |
|
36 #include <dlfcn.h> |
|
37 #else |
|
38 extern void *dlopen (const char *, int); |
|
39 extern const char *dlerror (void); |
|
40 extern void *dlsym (void *, const char *); |
|
41 extern int dlclose (void *); |
|
42 #endif |
|
43 #ifndef RTLD_LAZY |
|
44 #define RTLD_LAZY 1 |
|
45 #endif |
|
46 #elif defined (WITH_SHL) |
|
47 #include <dl.h> |
|
48 #endif |
|
49 } |
|
50 |
|
51 #include "file-stat.h" |
|
52 #include "lo-error.h" |
|
53 #include "oct-shlib.h" |
|
54 #include "str-vec.h" |
|
55 |
|
56 class |
|
57 octave_base_shlib : public octave_shlib |
|
58 { |
|
59 public: |
|
60 |
|
61 octave_base_shlib (void) |
|
62 : octave_shlib (octave_xshlib ()), file (), fcn_names (), |
|
63 tm_loaded (static_cast<time_t> (0)) |
|
64 { count = 1; } |
|
65 |
3504
|
66 octave_base_shlib (const std::string& f) |
3326
|
67 : octave_shlib (octave_xshlib ()), file (f), fcn_names (), |
|
68 tm_loaded (static_cast<time_t> (0)) |
|
69 { count = 1; } |
|
70 |
|
71 ~octave_base_shlib (void) { } |
|
72 |
3504
|
73 void open (const std::string&, bool = false) { } |
3326
|
74 |
3504
|
75 void *search (const std::string&, name_mangler = 0) { return 0; } |
3326
|
76 |
|
77 void close (octave_shlib::close_hook = 0) { } |
|
78 |
3504
|
79 bool remove (const std::string& fcn_name); |
3326
|
80 |
|
81 bool is_open (void) const { return false; } |
|
82 |
|
83 bool is_out_of_date (void) const; |
|
84 |
|
85 int number_of_functions_loaded (void) const { return fcn_names.length (); } |
|
86 |
3504
|
87 std::string file_name (void) const { return file; } |
3326
|
88 |
|
89 octave_time time_loaded (void) const { return tm_loaded; } |
|
90 |
|
91 protected: |
|
92 |
3504
|
93 std::string file; |
3326
|
94 |
|
95 string_vector fcn_names; |
|
96 |
|
97 octave_time tm_loaded; |
|
98 |
|
99 void stamp_time (bool warn_future = false); |
|
100 |
3504
|
101 void add_to_fcn_names (const std::string& name); |
3326
|
102 |
|
103 void do_close_hook (octave_shlib::close_hook = 0); |
|
104 |
|
105 void tabula_rasa (void); |
|
106 |
|
107 // No copying! |
|
108 |
|
109 octave_base_shlib (const octave_base_shlib&); |
|
110 |
|
111 octave_base_shlib& operator = (const octave_base_shlib&); |
|
112 }; |
|
113 |
|
114 bool |
3504
|
115 octave_base_shlib::remove (const std::string& fcn_name) |
3326
|
116 { |
|
117 bool retval = false; |
|
118 |
|
119 int n = number_of_functions_loaded (); |
|
120 |
|
121 string_vector new_fcn_names (n); |
|
122 |
|
123 int k = 0; |
|
124 |
|
125 for (int i = 0; i < n; i++) |
|
126 { |
|
127 if (fcn_names(i) == fcn_name) |
|
128 retval = true; |
|
129 else |
|
130 new_fcn_names(k++) = fcn_names(i); |
|
131 } |
|
132 |
|
133 new_fcn_names.resize (k); |
|
134 |
|
135 fcn_names = new_fcn_names; |
|
136 |
|
137 return retval; |
|
138 } |
|
139 |
|
140 bool |
|
141 octave_base_shlib::is_out_of_date (void) const |
|
142 { |
|
143 file_stat fs (file); |
|
144 |
|
145 return fs.is_newer (tm_loaded); |
|
146 } |
|
147 |
|
148 void |
|
149 octave_base_shlib::stamp_time (bool warn_future) |
|
150 { |
|
151 tm_loaded.stamp (); |
|
152 |
|
153 if (warn_future) |
|
154 { |
|
155 file_stat fs (file); |
|
156 |
|
157 if (fs.is_newer (tm_loaded)) |
|
158 (*current_liboctave_warning_handler) |
|
159 ("timestamp on file %s is in the future", file.c_str ()); |
|
160 } |
|
161 } |
|
162 |
|
163 void |
3504
|
164 octave_base_shlib::add_to_fcn_names (const std::string& name) |
3326
|
165 { |
|
166 int n = number_of_functions_loaded (); |
|
167 |
|
168 for (int i = 0; i < n; i++) |
|
169 if (fcn_names(i) == name) |
|
170 return; |
|
171 |
|
172 fcn_names.resize (n+1); |
|
173 |
|
174 fcn_names(n) = name; |
|
175 } |
|
176 |
|
177 void |
|
178 octave_base_shlib::do_close_hook (octave_shlib::close_hook cl_hook) |
|
179 { |
|
180 int n = number_of_functions_loaded (); |
|
181 |
|
182 for (int i = 0; i < n; i++) |
|
183 cl_hook (fcn_names(i)); |
|
184 } |
|
185 |
|
186 void |
|
187 octave_base_shlib::tabula_rasa (void) |
|
188 { |
|
189 file = ""; |
|
190 |
|
191 fcn_names.resize (0); |
|
192 |
|
193 tm_loaded = static_cast<time_t> (0); |
|
194 } |
|
195 |
|
196 #if defined (WITH_DL) |
|
197 |
|
198 class |
|
199 octave_dlopen_shlib : public octave_base_shlib |
|
200 { |
|
201 public: |
|
202 |
|
203 octave_dlopen_shlib (void); |
|
204 |
|
205 ~octave_dlopen_shlib (void); |
|
206 |
3504
|
207 void open (const std::string& f, bool warn_future = false); |
3326
|
208 |
3504
|
209 void *search (const std::string& name, name_mangler mangler = 0); |
3326
|
210 |
|
211 void close (octave_shlib::close_hook cl_hook = 0); |
|
212 |
|
213 bool is_open (void) const { return (library != 0); } |
|
214 |
|
215 private: |
|
216 |
|
217 // No copying! |
|
218 |
|
219 octave_dlopen_shlib (const octave_dlopen_shlib&); |
|
220 |
|
221 octave_dlopen_shlib& operator = (const octave_dlopen_shlib&); |
|
222 |
|
223 void *library; |
|
224 }; |
|
225 |
|
226 octave_dlopen_shlib::octave_dlopen_shlib (void) |
|
227 : octave_base_shlib (), library (0) |
|
228 { |
|
229 } |
|
230 |
|
231 octave_dlopen_shlib::~octave_dlopen_shlib (void) |
|
232 { |
|
233 close (); |
|
234 } |
|
235 |
|
236 void |
3504
|
237 octave_dlopen_shlib::open (const std::string& f, bool warn_future) |
3326
|
238 { |
|
239 if (! is_open ()) |
|
240 { |
|
241 file = f; |
|
242 |
|
243 library = dlopen (file.c_str (), RTLD_LAZY); |
|
244 |
|
245 if (library) |
|
246 stamp_time (warn_future); |
|
247 else |
|
248 { |
|
249 const char *msg = dlerror (); |
|
250 |
|
251 if (msg) |
|
252 (*current_liboctave_error_handler) ("%s", msg); |
|
253 } |
|
254 } |
|
255 else |
|
256 (*current_liboctave_error_handler) |
|
257 ("shared library %s is already open", file.c_str ()); |
|
258 } |
|
259 |
|
260 void * |
3504
|
261 octave_dlopen_shlib::search (const std::string& name, |
3326
|
262 octave_shlib::name_mangler mangler) |
|
263 { |
|
264 void *function = 0; |
|
265 |
|
266 if (is_open ()) |
|
267 { |
3504
|
268 std::string sym_name = name; |
3326
|
269 |
|
270 if (mangler) |
|
271 sym_name = mangler (name); |
|
272 |
|
273 function = dlsym (library, sym_name.c_str ()); |
|
274 |
|
275 if (function) |
|
276 add_to_fcn_names (name); |
|
277 } |
|
278 else |
|
279 (*current_liboctave_error_handler) |
|
280 ("shared library %s is not open", file.c_str ()); |
|
281 |
|
282 return function; |
|
283 } |
|
284 |
|
285 void |
|
286 octave_dlopen_shlib::close (octave_shlib::close_hook cl_hook) |
|
287 { |
|
288 if (is_open ()) |
|
289 { |
|
290 do_close_hook (cl_hook); |
|
291 |
|
292 dlclose (library); |
|
293 |
|
294 library = 0; |
|
295 |
|
296 tabula_rasa (); |
|
297 } |
|
298 } |
|
299 |
|
300 #elif defined (WITH_SHL) |
|
301 |
|
302 class |
|
303 octave_shl_load_shlib : public octave_base_shlib |
|
304 { |
|
305 public: |
|
306 |
|
307 octave_shl_load_shlib (void); |
|
308 |
|
309 ~octave_shl_load_shlib (void); |
|
310 |
3504
|
311 void open (const std::string& f, bool warn_future = false); |
3326
|
312 |
3504
|
313 void *search (const std::string& name, name_mangler mangler = 0); |
3326
|
314 |
|
315 void close (octave_shlib::close_hook cl_hook = 0); |
|
316 |
3609
|
317 bool is_open (void) const { return (library != 0); } |
3326
|
318 |
|
319 private: |
|
320 |
|
321 // No copying! |
|
322 |
|
323 octave_shl_load_shlib (const octave_shl_load_shlib&); |
|
324 |
|
325 octave_shl_load_shlib& operator = (const octave_shl_load_shlib&); |
|
326 |
|
327 shl_t library; |
|
328 }; |
|
329 |
|
330 octave_shl_load_shlib::octave_shl_load_shlib (void) |
|
331 : octave_base_shlib (), library (0) |
|
332 { |
|
333 } |
|
334 |
|
335 octave_shl_load_shlib::~octave_shl_load_shlib (void) |
|
336 { |
|
337 close (); |
|
338 } |
|
339 |
|
340 void |
3504
|
341 octave_shl_load_shlib::open (const std::string& f, bool warn_future) |
3326
|
342 { |
|
343 if (! is_open ()) |
|
344 { |
|
345 file = f; |
|
346 |
|
347 library = shl_load (file.c_str (), BIND_DEFERRED, 0L); |
|
348 |
|
349 if (library) |
|
350 stamp_time (warn_future); |
|
351 else |
3504
|
352 { |
|
353 using namespace std; |
|
354 (*current_liboctave_error_handler) ("%s", strerror (errno)); |
|
355 } |
3326
|
356 } |
|
357 else |
|
358 (*current_liboctave_error_handler) |
|
359 ("shared library %s is already open", file.c_str ()); |
|
360 } |
|
361 |
|
362 void * |
3504
|
363 octave_shl_load_shlib::search (const std::string& name, |
3326
|
364 octave_shlib::name_mangler mangler) |
|
365 { |
|
366 void *function = 0; |
|
367 |
|
368 if (is_open ()) |
|
369 { |
3609
|
370 std::string sym_name = name; |
3326
|
371 |
|
372 if (mangler) |
|
373 sym_name = mangler (name); |
|
374 |
|
375 int status = shl_findsym (&library, sym_name.c_str (), |
|
376 TYPE_UNDEFINED, &function); |
|
377 |
|
378 if (status == 0) |
|
379 add_to_fcn_names (name); |
|
380 } |
|
381 else |
|
382 (*current_liboctave_error_handler) |
|
383 ("shared library %s is not open", file.c_str ()); |
|
384 |
|
385 return function; |
|
386 } |
|
387 |
|
388 void |
|
389 octave_shl_load_shlib::close (octave_shlib::close_hook cl_hook) |
|
390 { |
|
391 if (is_open ()) |
|
392 { |
|
393 do_close_hook (cl_hook); |
|
394 |
|
395 shl_unload (library); |
|
396 |
|
397 library = 0; |
|
398 |
|
399 tabula_rasa (); |
|
400 } |
|
401 } |
|
402 |
|
403 #endif |
|
404 |
|
405 octave_shlib * |
|
406 octave_shlib::make_shlib (void) |
|
407 { |
|
408 #if defined (WITH_DL) |
|
409 return new octave_dlopen_shlib (); |
|
410 #elif defined (WITH_SHL) |
|
411 return new octave_shl_load_shlib (); |
|
412 #else |
|
413 return new octave_base_shlib (); |
|
414 #endif |
|
415 } |
|
416 |
|
417 /* |
|
418 ;;; Local Variables: *** |
|
419 ;;; mode: C++ *** |
|
420 ;;; End: *** |
|
421 */ |