Commit | Line | Data |
---|---|---|
1edae076 MV |
1 | /* dynl.c - dynamic linking |
2 | * | |
7cf1a27e | 3 | * Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc. |
1edae076 MV |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
17 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
18 | * Boston, MA 02111-1307 USA | |
1edae076 MV |
19 | * |
20 | * As a special exception, the Free Software Foundation gives permission | |
21 | * for additional uses of the text contained in its release of GUILE. | |
22 | * | |
23 | * The exception is that, if you link the GUILE library with other files | |
24 | * to produce an executable, this does not by itself cause the | |
25 | * resulting executable to be covered by the GNU General Public License. | |
26 | * Your use of that executable is in no way restricted on account of | |
27 | * linking the GUILE library code into it. | |
28 | * | |
29 | * This exception does not however invalidate any other reasons why | |
30 | * the executable file might be covered by the GNU General Public License. | |
31 | * | |
32 | * This exception applies only to the code released by the | |
33 | * Free Software Foundation under the name GUILE. If you copy | |
34 | * code from other Free Software Foundation releases into a copy of | |
35 | * GUILE, as the General Public License permits, the exception does | |
36 | * not apply to the code that you add in this way. To avoid misleading | |
37 | * anyone as to the status of such modified files, you must delete | |
38 | * this exception notice from them. | |
39 | * | |
40 | * If you write modifications of your own for GUILE, it is your choice | |
41 | * whether to permit this exception to apply to your modifications. | |
82892bed | 42 | * If you do not wish that, delete this exception notice. */ |
1edae076 | 43 | |
1bbd0b84 GB |
44 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, |
45 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
46 | ||
47 | ||
1edae076 MV |
48 | /* "dynl.c" dynamically link&load object files. |
49 | Author: Aubrey Jaffer | |
50 | Modified for libguile by Marius Vollmer */ | |
51 | ||
104d4533 | 52 | #if 0 /* Disabled until we know for sure that it isn't needed */ |
80bc7890 MV |
53 | /* XXX - This is only here to drag in a definition of __eprintf. This |
54 | is needed for proper operation of dynamic linking. The real | |
55 | solution would probably be a shared libgcc. */ | |
56 | ||
57 | #undef NDEBUG | |
58 | #include <assert.h> | |
59 | ||
60 | static void | |
61 | maybe_drag_in_eprintf () | |
62 | { | |
63 | assert (!maybe_drag_in_eprintf); | |
64 | } | |
104d4533 | 65 | #endif |
80bc7890 | 66 | |
96599e6a | 67 | #include <stdio.h> |
1edae076 | 68 | #include "_scm.h" |
80bc7890 | 69 | #include "dynl.h" |
80bc7890 | 70 | #include "smob.h" |
8e3ab003 | 71 | #include "keywords.h" |
80bc7890 | 72 | |
b6791b2e | 73 | #include "validate.h" |
408ea28a | 74 | |
1edae076 MV |
75 | /* Converting a list of SCM strings into a argv-style array. You must |
76 | have ints disabled for the whole lifetime of the created argv (from | |
77 | before MAKE_ARGV_FROM_STRINGLIST until after | |
78 | MUST_FREE_ARGV). Atleast this is was the documentation for | |
79 | MAKARGVFROMSTRS says, it isn't really used that way. | |
80 | ||
81 | This code probably belongs into strings.c */ | |
82 | ||
1edae076 | 83 | static char ** |
1bbd0b84 | 84 | scm_make_argv_from_stringlist (SCM args,int *argcp,const char *subr,int argn) |
1edae076 | 85 | { |
7cf1a27e MD |
86 | char **argv; |
87 | int argc, i; | |
88 | ||
89 | argc = scm_ilength (args); | |
90 | argv = (char **) scm_must_malloc ((1L + argc) * sizeof (char *), subr); | |
91 | for (i = 0; SCM_NNULLP (args); args = SCM_CDR (args), i++) { | |
92 | size_t len; | |
93 | char *dst, *src; | |
94 | SCM str = SCM_CAR (args); | |
95 | ||
96 | SCM_ASSERT (SCM_ROSTRINGP (str), str, argn, subr); | |
97 | len = 1 + SCM_ROLENGTH (str); | |
98 | dst = (char *) scm_must_malloc ((long) len, subr); | |
99 | src = SCM_ROCHARS (str); | |
100 | while (len--) | |
101 | dst[len] = src[len]; | |
102 | argv[i] = dst; | |
103 | } | |
104 | ||
105 | if (argcp) | |
106 | *argcp = argc; | |
107 | argv[argc] = 0; | |
108 | return argv; | |
1edae076 MV |
109 | } |
110 | ||
1edae076 | 111 | static void |
1bbd0b84 | 112 | scm_must_free_argv(char **argv) |
1edae076 | 113 | { |
7cf1a27e MD |
114 | char **av = argv; |
115 | while (*av) | |
116 | free (*(av++)); | |
117 | free (argv); | |
1edae076 MV |
118 | } |
119 | ||
120 | /* Coerce an arbitrary readonly-string into a zero-terminated string. | |
121 | */ | |
122 | ||
1edae076 | 123 | static SCM |
1bbd0b84 | 124 | scm_coerce_rostring (SCM rostr,const char *subr,int argn) |
1edae076 | 125 | { |
7cf1a27e MD |
126 | SCM_ASSERT (SCM_ROSTRINGP (rostr), rostr, argn, subr); |
127 | if (SCM_SUBSTRP (rostr)) | |
128 | rostr = scm_makfromstr (SCM_ROCHARS (rostr), SCM_ROLENGTH (rostr), 0); | |
129 | return rostr; | |
1edae076 MV |
130 | } |
131 | ||
80bc7890 MV |
132 | /* Module registry |
133 | */ | |
134 | ||
135 | /* We can't use SCM objects here. One should be able to call | |
136 | SCM_REGISTER_MODULE from a C++ constructor for a static | |
137 | object. This happens before main and thus before libguile is | |
138 | initialized. */ | |
139 | ||
140 | struct moddata { | |
7cf1a27e MD |
141 | struct moddata *link; |
142 | char *module_name; | |
143 | void *init_func; | |
80bc7890 MV |
144 | }; |
145 | ||
146 | static struct moddata *registered_mods = NULL; | |
147 | ||
148 | void | |
6e8d25a6 | 149 | scm_register_module_xxx (char *module_name, void *init_func) |
80bc7890 | 150 | { |
7cf1a27e | 151 | struct moddata *md; |
80bc7890 | 152 | |
7cf1a27e | 153 | /* XXX - should we (and can we) DEFER_INTS here? */ |
80bc7890 | 154 | |
7cf1a27e MD |
155 | for (md = registered_mods; md; md = md->link) |
156 | if (!strcmp (md->module_name, module_name)) | |
157 | { | |
158 | md->init_func = init_func; | |
80bc7890 | 159 | return; |
7cf1a27e MD |
160 | } |
161 | ||
162 | md = (struct moddata *) malloc (sizeof (struct moddata)); | |
163 | if (md == NULL) | |
164 | { | |
165 | fprintf (stderr, | |
166 | "guile: can't register module (%s): not enough memory", | |
167 | module_name); | |
168 | return; | |
96599e6a | 169 | } |
80bc7890 | 170 | |
7cf1a27e MD |
171 | md->module_name = module_name; |
172 | md->init_func = init_func; | |
173 | md->link = registered_mods; | |
174 | registered_mods = md; | |
80bc7890 MV |
175 | } |
176 | ||
a1ec6916 | 177 | SCM_DEFINE (scm_registered_modules, "c-registered-modules", 0, 0, 0, |
1bbd0b84 | 178 | (), |
b380b885 MD |
179 | "Return a list of the object code modules that have been imported into\n" |
180 | "the current Guile process. Each element of the list is a pair whose\n" | |
f15a06bf MV |
181 | "car is the name of the module, and whose cdr is the function handle\n" |
182 | "for that module's initializer function. The name is the string that\n" | |
183 | "has been passed to scm_register_module_xxx.") | |
1bbd0b84 | 184 | #define FUNC_NAME s_scm_registered_modules |
80bc7890 | 185 | { |
7cf1a27e MD |
186 | SCM res; |
187 | struct moddata *md; | |
188 | ||
189 | res = SCM_EOL; | |
190 | for (md = registered_mods; md; md = md->link) | |
191 | res = scm_cons (scm_cons (scm_makfrom0str (md->module_name), | |
192 | scm_ulong2num ((unsigned long) md->init_func)), | |
193 | res); | |
194 | return res; | |
80bc7890 | 195 | } |
1bbd0b84 | 196 | #undef FUNC_NAME |
80bc7890 | 197 | |
a1ec6916 | 198 | SCM_DEFINE (scm_clear_registered_modules, "c-clear-registered-modules", 0, 0, 0, |
1bbd0b84 | 199 | (), |
b380b885 MD |
200 | "Destroy the list of modules registered with the current Guile process.\n" |
201 | "The return value is unspecified. @strong{Warning:} this function does\n" | |
202 | "not actually unlink or deallocate these modules, but only destroys the\n" | |
203 | "records of which modules have been loaded. It should therefore be used\n" | |
204 | "only by module bookkeeping operations.") | |
1bbd0b84 | 205 | #define FUNC_NAME s_scm_clear_registered_modules |
80bc7890 | 206 | { |
7cf1a27e | 207 | struct moddata *md1, *md2; |
80bc7890 | 208 | |
7cf1a27e | 209 | SCM_DEFER_INTS; |
80bc7890 | 210 | |
7cf1a27e MD |
211 | for (md1 = registered_mods; md1; md1 = md2) |
212 | { | |
213 | md2 = md1->link; | |
214 | free (md1); | |
80bc7890 | 215 | } |
7cf1a27e | 216 | registered_mods = NULL; |
80bc7890 | 217 | |
7cf1a27e MD |
218 | SCM_ALLOW_INTS; |
219 | return SCM_UNSPECIFIED; | |
80bc7890 | 220 | } |
1bbd0b84 | 221 | #undef FUNC_NAME |
80bc7890 | 222 | |
1edae076 | 223 | /* Dispatch to the system dependent files |
80bc7890 | 224 | * |
419e9e11 MV |
225 | * They define some static functions. These functions are called with |
226 | * deferred interrupts. When they want to throw errors, they are | |
227 | * expected to insert a SCM_ALLOW_INTS before doing the throw. It | |
228 | * might work to throw an error while interrupts are deferred (because | |
229 | * they will be unconditionally allowed the next time a SCM_ALLOW_INTS | |
230 | * is executed, SCM_DEFER_INTS and SCM_ALLOW_INTS do not nest). | |
1edae076 MV |
231 | */ |
232 | ||
8e3ab003 MV |
233 | #define DYNL_GLOBAL 0x0001 |
234 | ||
4feb69af MV |
235 | #ifdef DYNAMIC_LINKING |
236 | ||
237 | #include <ltdl.h> | |
238 | ||
239 | static void * | |
240 | sysdep_dynl_link (const char *fname, int flags, const char *subr) | |
241 | { | |
7cf1a27e MD |
242 | lt_dlhandle handle; |
243 | handle = lt_dlopenext (fname); | |
4feb69af MV |
244 | if (NULL == handle) |
245 | { | |
246 | SCM_ALLOW_INTS; | |
7cf1a27e | 247 | scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL); |
4feb69af MV |
248 | } |
249 | return (void *) handle; | |
250 | } | |
251 | ||
252 | static void | |
253 | sysdep_dynl_unlink (void *handle, const char *subr) | |
254 | { | |
255 | if (lt_dlclose ((lt_dlhandle) handle)) | |
256 | { | |
257 | SCM_ALLOW_INTS; | |
7cf1a27e | 258 | scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL); |
4feb69af MV |
259 | } |
260 | } | |
261 | ||
262 | static void * | |
263 | sysdep_dynl_func (const char *symb, void *handle, const char *subr) | |
264 | { | |
265 | void *fptr; | |
266 | ||
267 | fptr = lt_dlsym ((lt_dlhandle) handle, symb); | |
268 | if (!fptr) | |
269 | { | |
270 | SCM_ALLOW_INTS; | |
7cf1a27e | 271 | scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL); |
4feb69af MV |
272 | } |
273 | return fptr; | |
274 | } | |
275 | ||
276 | static void | |
277 | sysdep_dynl_init () | |
278 | { | |
279 | lt_dlinit (); | |
280 | } | |
281 | ||
1edae076 | 282 | #else |
96599e6a MV |
283 | |
284 | /* no dynamic linking available, throw errors. */ | |
285 | ||
286 | static void | |
1bbd0b84 | 287 | sysdep_dynl_init (void) |
96599e6a MV |
288 | { |
289 | } | |
290 | ||
291 | static void | |
a80a90e9 | 292 | no_dynl_error (const char *subr) |
96599e6a | 293 | { |
419e9e11 MV |
294 | SCM_ALLOW_INTS; |
295 | scm_misc_error (subr, "dynamic linking not available", SCM_EOL); | |
96599e6a MV |
296 | } |
297 | ||
298 | static void * | |
8e3ab003 MV |
299 | sysdep_dynl_link (const char *filename, |
300 | int flags, | |
a80a90e9 | 301 | const char *subr) |
96599e6a | 302 | { |
7cf1a27e MD |
303 | no_dynl_error (subr); |
304 | return NULL; | |
96599e6a MV |
305 | } |
306 | ||
307 | static void | |
a80a90e9 JB |
308 | sysdep_dynl_unlink (void *handle, |
309 | const char *subr) | |
96599e6a | 310 | { |
7cf1a27e | 311 | no_dynl_error (subr); |
96599e6a MV |
312 | } |
313 | ||
314 | static void * | |
a80a90e9 JB |
315 | sysdep_dynl_func (const char *symbol, |
316 | void *handle, | |
317 | const char *subr) | |
96599e6a | 318 | { |
7cf1a27e MD |
319 | no_dynl_error (subr); |
320 | return NULL; | |
96599e6a MV |
321 | } |
322 | ||
80bc7890 MV |
323 | #endif |
324 | ||
325 | int scm_tc16_dynamic_obj; | |
326 | ||
327 | struct dynl_obj { | |
7cf1a27e MD |
328 | SCM filename; |
329 | void *handle; | |
80bc7890 MV |
330 | }; |
331 | ||
7cf1a27e MD |
332 | #define DYNL_OBJ(x) ((struct dynl_obj *) &SCM_CDR (x)) |
333 | ||
334 | #define DYNL_FILENAME(x) (DYNL_OBJ (x)->filename) | |
335 | #define DYNL_HANDLE(x) (DYNL_OBJ (x)->handle) | |
336 | ||
80bc7890 | 337 | static SCM |
1bbd0b84 | 338 | mark_dynl_obj (SCM ptr) |
80bc7890 | 339 | { |
7cf1a27e | 340 | return DYNL_FILENAME (ptr); |
c487ad44 MV |
341 | } |
342 | ||
80bc7890 | 343 | static int |
1bbd0b84 | 344 | print_dynl_obj (SCM exp,SCM port,scm_print_state *pstate) |
80bc7890 | 345 | { |
7cf1a27e MD |
346 | scm_puts ("#<dynamic-object ", port); |
347 | scm_iprin1 (DYNL_FILENAME (exp), port, pstate); | |
348 | if (DYNL_HANDLE (exp) == NULL) | |
349 | scm_puts (" (unlinked)", port); | |
350 | scm_putc ('>', port); | |
351 | return 1; | |
80bc7890 MV |
352 | } |
353 | ||
8e3ab003 MV |
354 | static SCM kw_global; |
355 | SCM_SYMBOL (sym_global, "-global"); | |
356 | ||
a1ec6916 | 357 | SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 1, |
1bbd0b84 | 358 | (SCM fname, SCM rest), |
b380b885 MD |
359 | "Open the dynamic library @var{library-file}. A library handle\n" |
360 | "representing the opened library is returned; this handle should be used\n" | |
361 | "as the @var{lib} argument to the following functions.") | |
1bbd0b84 | 362 | #define FUNC_NAME s_scm_dynamic_link |
80bc7890 | 363 | { |
7cf1a27e MD |
364 | void *handle; |
365 | int flags = DYNL_GLOBAL; | |
80bc7890 | 366 | |
7cf1a27e | 367 | SCM_COERCE_ROSTRING (1, fname); |
c487ad44 | 368 | |
7cf1a27e MD |
369 | /* collect flags */ |
370 | while (SCM_CONSP (rest)) | |
371 | { | |
372 | SCM kw, val; | |
8e3ab003 | 373 | |
7cf1a27e MD |
374 | kw = SCM_CAR (rest); |
375 | rest = SCM_CDR (rest); | |
376 | ||
377 | if (!SCM_CONSP (rest)) | |
378 | SCM_MISC_ERROR ("keyword without value", SCM_EOL); | |
8e3ab003 | 379 | |
7cf1a27e MD |
380 | val = SCM_CAR (rest); |
381 | rest = SCM_CDR (rest); | |
8e3ab003 | 382 | |
7cf1a27e MD |
383 | if (kw == kw_global) |
384 | { | |
385 | if (SCM_FALSEP (val)) | |
386 | flags &= ~DYNL_GLOBAL; | |
387 | } | |
388 | else | |
389 | SCM_MISC_ERROR ("unknown keyword argument: ~A", | |
390 | scm_cons (kw, SCM_EOL)); | |
391 | } | |
80bc7890 | 392 | |
7cf1a27e MD |
393 | handle = sysdep_dynl_link (SCM_CHARS (fname), flags, FUNC_NAME); |
394 | ||
395 | SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, fname, handle); | |
80bc7890 | 396 | } |
1bbd0b84 | 397 | #undef FUNC_NAME |
80bc7890 | 398 | |
80bc7890 | 399 | static struct dynl_obj * |
7cf1a27e | 400 | get_dynl_obj (SCM dobj, const char *subr, int argn) |
80bc7890 | 401 | { |
7cf1a27e MD |
402 | struct dynl_obj *d; |
403 | SCM_ASSERT (SCM_NIMP (dobj) && SCM_UNPACK_CAR (dobj) == scm_tc16_dynamic_obj, | |
404 | dobj, argn, subr); | |
405 | d = DYNL_OBJ (dobj); | |
406 | SCM_ASSERT (d->handle != NULL, dobj, argn, subr); | |
407 | return d; | |
80bc7890 MV |
408 | } |
409 | ||
a1ec6916 | 410 | SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0, |
1bbd0b84 | 411 | (SCM obj), |
b380b885 MD |
412 | "Return @code{#t} if @var{obj} is a dynamic library handle, or @code{#f}\n" |
413 | "otherwise.") | |
1bbd0b84 | 414 | #define FUNC_NAME s_scm_dynamic_object_p |
80bc7890 | 415 | { |
7cf1a27e MD |
416 | return SCM_BOOL (SCM_NIMP (obj) |
417 | && SCM_UNPACK_CAR (obj) == scm_tc16_dynamic_obj); | |
80bc7890 | 418 | } |
1bbd0b84 | 419 | #undef FUNC_NAME |
80bc7890 | 420 | |
a1ec6916 | 421 | SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0, |
1bbd0b84 | 422 | (SCM dobj), |
b380b885 MD |
423 | "Unlink the library represented by @var{library-handle}, and remove any\n" |
424 | "imported symbols from the address space.\n" | |
425 | "GJB:FIXME:DOC: 2nd version below:\n" | |
426 | "Unlink the indicated object file from the application. The argument\n" | |
427 | "@var{dynobj} should be one of the values returned by\n" | |
428 | "@code{dynamic-link}. When @code{dynamic-unlink} has been called on\n" | |
429 | "@var{dynobj}, it is no longer usable as an argument to the functions\n" | |
430 | "below and you will get type mismatch errors when you try to.\n" | |
431 | "") | |
1bbd0b84 | 432 | #define FUNC_NAME s_scm_dynamic_unlink |
80bc7890 | 433 | { |
7cf1a27e MD |
434 | /*fixme* GC-problem */ |
435 | struct dynl_obj *d = get_dynl_obj (dobj, FUNC_NAME, SCM_ARG1); | |
436 | SCM_DEFER_INTS; | |
437 | sysdep_dynl_unlink (d->handle, FUNC_NAME); | |
438 | d->handle = NULL; | |
439 | SCM_ALLOW_INTS; | |
440 | return SCM_UNSPECIFIED; | |
80bc7890 | 441 | } |
1bbd0b84 | 442 | #undef FUNC_NAME |
80bc7890 | 443 | |
a1ec6916 | 444 | SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0, |
1bbd0b84 | 445 | (SCM symb, SCM dobj), |
b380b885 MD |
446 | "Import the symbol @var{func} from @var{lib} (a dynamic library handle).\n" |
447 | "A @dfn{function handle} representing the imported function is returned.\n" | |
448 | "GJB:FIXME:DOC: 2nd version below\n" | |
449 | "Search the C function indicated by @var{function} (a string or symbol)\n" | |
450 | "in @var{dynobj} and return some Scheme object that can later be used\n" | |
451 | "with @code{dynamic-call} to actually call this function. Right now,\n" | |
452 | "these Scheme objects are formed by casting the address of the function\n" | |
453 | "to @code{long} and converting this number to its Scheme representation.\n\n" | |
454 | "Regardless whether your C compiler prepends an underscore @samp{_} to\n" | |
455 | "the global names in a program, you should @strong{not} include this\n" | |
456 | "underscore in @var{function}. Guile knows whether the underscore is\n" | |
457 | "needed or not and will add it when necessary.\n\n" | |
458 | "") | |
1bbd0b84 | 459 | #define FUNC_NAME s_scm_dynamic_func |
80bc7890 | 460 | { |
7cf1a27e MD |
461 | struct dynl_obj *d; |
462 | void (*func) (); | |
80bc7890 | 463 | |
7cf1a27e MD |
464 | SCM_COERCE_ROSTRING (1, symb); |
465 | /*fixme* GC-problem */ | |
466 | d = get_dynl_obj (dobj, FUNC_NAME, SCM_ARG2); | |
80bc7890 | 467 | |
7cf1a27e MD |
468 | SCM_DEFER_INTS; |
469 | func = (void (*) ()) sysdep_dynl_func (SCM_CHARS (symb), | |
470 | d->handle, | |
471 | FUNC_NAME); | |
472 | SCM_ALLOW_INTS; | |
419e9e11 | 473 | |
7cf1a27e | 474 | return scm_ulong2num ((unsigned long) func); |
80bc7890 | 475 | } |
1bbd0b84 | 476 | #undef FUNC_NAME |
80bc7890 | 477 | |
a1ec6916 | 478 | SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0, |
1bbd0b84 | 479 | (SCM func, SCM dobj), |
b380b885 MD |
480 | "Call @var{lib-thunk}, a procedure of no arguments. If @var{lib-thunk}\n" |
481 | "is a string, it is assumed to be a symbol found in the dynamic library\n" | |
482 | "@var{lib} and is fetched with @code{dynamic-func}. Otherwise, it should\n" | |
483 | "be a function handle returned by a previous call to @code{dynamic-func}.\n" | |
484 | "The return value is unspecified.\n" | |
485 | "GJB:FIXME:DOC 2nd version below\n" | |
486 | "Call the C function indicated by @var{function} and @var{dynobj}. The\n" | |
487 | "function is passed no arguments and its return value is ignored. When\n" | |
488 | "@var{function} is something returned by @code{dynamic-func}, call that\n" | |
489 | "function and ignore @var{dynobj}. When @var{function} is a string (or\n" | |
490 | "symbol, etc.), look it up in @var{dynobj}; this is equivalent to\n\n" | |
491 | "@smallexample\n" | |
492 | "(dynamic-call (dynamic-func @var{function} @var{dynobj} #f))\n" | |
493 | "@end smallexample\n\n" | |
494 | "Interrupts are deferred while the C function is executing (with\n" | |
495 | "@code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).\n" | |
496 | "") | |
1bbd0b84 | 497 | #define FUNC_NAME s_scm_dynamic_call |
80bc7890 | 498 | { |
7cf1a27e MD |
499 | void (*fptr) (); |
500 | ||
501 | if (SCM_ROSTRINGP (func)) | |
502 | func = scm_dynamic_func (func, dobj); | |
503 | fptr = (void (*) ()) SCM_NUM2ULONG (1, func); | |
504 | SCM_DEFER_INTS; | |
505 | fptr (); | |
506 | SCM_ALLOW_INTS; | |
507 | return SCM_UNSPECIFIED; | |
80bc7890 | 508 | } |
1bbd0b84 | 509 | #undef FUNC_NAME |
80bc7890 | 510 | |
a1ec6916 | 511 | SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0, |
1bbd0b84 | 512 | (SCM func, SCM dobj, SCM args), |
b380b885 MD |
513 | "Call @var{proc}, a dynamically loaded function, passing it the argument\n" |
514 | "list @var{args} (a list of strings). As with @code{dynamic-call},\n" | |
515 | "@var{proc} should be either a function handle or a string, in which case\n" | |
516 | "it is first fetched from @var{lib} with @code{dynamic-func}.\n\n" | |
517 | "@var{proc} is assumed to return an integer, which is used as the return\n" | |
518 | "value from @code{dynamic-args-call}.\n\n" | |
519 | "GJB:FIXME:DOC 2nd version below\n" | |
520 | "Call the C function indicated by @var{function} and @var{dynobj}, just\n" | |
521 | "like @code{dynamic-call}, but pass it some arguments and return its\n" | |
522 | "return value. The C function is expected to take two arguments and\n" | |
523 | "return an @code{int}, just like @code{main}:\n\n" | |
524 | "@smallexample\n" | |
525 | "int c_func (int argc, char **argv);\n" | |
526 | "@end smallexample\n\n" | |
527 | "The parameter @var{args} must be a list of strings and is converted into\n" | |
528 | "an array of @code{char *}. The array is passed in @var{argv} and its\n" | |
529 | "size in @var{argc}. The return value is converted to a Scheme number\n" | |
530 | "and returned from the call to @code{dynamic-args-call}.\n\n\n" | |
531 | "") | |
1bbd0b84 | 532 | #define FUNC_NAME s_scm_dynamic_args_call |
80bc7890 | 533 | { |
7cf1a27e MD |
534 | int (*fptr) (int argc, char **argv); |
535 | int result, argc; | |
536 | char **argv; | |
537 | ||
538 | if (SCM_ROSTRINGP (func)) | |
539 | func = scm_dynamic_func (func, dobj); | |
540 | ||
541 | fptr = (int (*) (int, char **)) SCM_NUM2ULONG (1, func); | |
542 | SCM_DEFER_INTS; | |
543 | argv = scm_make_argv_from_stringlist (args, &argc, FUNC_NAME, SCM_ARG3); | |
544 | result = (*fptr) (argc, argv); | |
545 | scm_must_free_argv (argv); | |
546 | SCM_ALLOW_INTS; | |
547 | ||
548 | return SCM_MAKINUM (0L + result); | |
80bc7890 | 549 | } |
1bbd0b84 | 550 | #undef FUNC_NAME |
80bc7890 | 551 | |
1edae076 MV |
552 | void |
553 | scm_init_dynamic_linking () | |
554 | { | |
7cf1a27e MD |
555 | scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0); |
556 | scm_set_smob_mark (scm_tc16_dynamic_obj, mark_dynl_obj); | |
557 | scm_set_smob_print (scm_tc16_dynamic_obj, print_dynl_obj); | |
558 | sysdep_dynl_init (); | |
80bc7890 | 559 | #include "dynl.x" |
7cf1a27e | 560 | kw_global = scm_make_keyword_from_dash_symbol (sym_global); |
1edae076 | 561 | } |