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