Commit | Line | Data |
---|---|---|
1edae076 MV |
1 | /* dynl.c - dynamic linking |
2 | * | |
a80a90e9 | 3 | * Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999 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 MV |
69 | #include "dynl.h" |
70 | #include "genio.h" | |
71 | #include "smob.h" | |
8e3ab003 | 72 | #include "keywords.h" |
80bc7890 | 73 | |
b6791b2e | 74 | #include "validate.h" |
408ea28a | 75 | |
1edae076 MV |
76 | /* Converting a list of SCM strings into a argv-style array. You must |
77 | have ints disabled for the whole lifetime of the created argv (from | |
78 | before MAKE_ARGV_FROM_STRINGLIST until after | |
79 | MUST_FREE_ARGV). Atleast this is was the documentation for | |
80 | MAKARGVFROMSTRS says, it isn't really used that way. | |
81 | ||
82 | This code probably belongs into strings.c */ | |
83 | ||
1edae076 | 84 | static char ** |
1bbd0b84 | 85 | scm_make_argv_from_stringlist (SCM args,int *argcp,const char *subr,int argn) |
1edae076 MV |
86 | { |
87 | char **argv; | |
88 | int argc, i; | |
89 | ||
90 | argc = scm_ilength(args); | |
91 | argv = (char **) scm_must_malloc ((1L+argc)*sizeof(char *), subr); | |
92 | for(i = 0; SCM_NNULLP (args); args = SCM_CDR (args), i++) { | |
93 | size_t len; | |
94 | char *dst, *src; | |
95 | SCM str = SCM_CAR (args); | |
96 | ||
0c95b57d | 97 | SCM_ASSERT (SCM_ROSTRINGP (str), str, argn, subr); |
1edae076 MV |
98 | len = 1 + SCM_ROLENGTH (str); |
99 | dst = (char *) scm_must_malloc ((long)len, subr); | |
100 | src = SCM_ROCHARS (str); | |
101 | while (len--) | |
102 | dst[len] = src[len]; | |
103 | argv[i] = dst; | |
104 | } | |
105 | ||
106 | if (argcp) | |
107 | *argcp = argc; | |
108 | argv[argc] = 0; | |
109 | return argv; | |
110 | } | |
111 | ||
1edae076 | 112 | static void |
1bbd0b84 | 113 | scm_must_free_argv(char **argv) |
1edae076 MV |
114 | { |
115 | char **av = argv; | |
c3e09ef9 MD |
116 | while (*av) |
117 | free(*(av++)); | |
1edae076 MV |
118 | free(argv); |
119 | } | |
120 | ||
121 | /* Coerce an arbitrary readonly-string into a zero-terminated string. | |
122 | */ | |
123 | ||
1edae076 | 124 | static SCM |
1bbd0b84 | 125 | scm_coerce_rostring (SCM rostr,const char *subr,int argn) |
1edae076 | 126 | { |
0c95b57d | 127 | SCM_ASSERT (SCM_ROSTRINGP (rostr), rostr, argn, subr); |
1edae076 MV |
128 | if (SCM_SUBSTRP (rostr)) |
129 | rostr = scm_makfromstr (SCM_ROCHARS (rostr), SCM_ROLENGTH (rostr), 0); | |
130 | return rostr; | |
131 | } | |
132 | ||
80bc7890 MV |
133 | /* Module registry |
134 | */ | |
135 | ||
136 | /* We can't use SCM objects here. One should be able to call | |
137 | SCM_REGISTER_MODULE from a C++ constructor for a static | |
138 | object. This happens before main and thus before libguile is | |
139 | initialized. */ | |
140 | ||
141 | struct moddata { | |
142 | struct moddata *link; | |
143 | char *module_name; | |
144 | void *init_func; | |
145 | }; | |
146 | ||
147 | static struct moddata *registered_mods = NULL; | |
148 | ||
149 | void | |
6e8d25a6 | 150 | scm_register_module_xxx (char *module_name, void *init_func) |
80bc7890 MV |
151 | { |
152 | struct moddata *md; | |
153 | ||
154 | /* XXX - should we (and can we) DEFER_INTS here? */ | |
155 | ||
156 | for (md = registered_mods; md; md = md->link) | |
157 | if (!strcmp (md->module_name, module_name)) { | |
158 | md->init_func = init_func; | |
159 | return; | |
160 | } | |
161 | ||
162 | md = (struct moddata *)malloc (sizeof (struct moddata)); | |
96599e6a MV |
163 | if (md == NULL) { |
164 | fprintf (stderr, | |
165 | "guile: can't register module (%s): not enough memory", | |
166 | module_name); | |
80bc7890 | 167 | return; |
96599e6a | 168 | } |
80bc7890 MV |
169 | |
170 | md->module_name = module_name; | |
171 | md->init_func = init_func; | |
172 | md->link = registered_mods; | |
173 | registered_mods = md; | |
174 | } | |
175 | ||
a1ec6916 | 176 | SCM_DEFINE (scm_registered_modules, "c-registered-modules", 0, 0, 0, |
1bbd0b84 | 177 | (), |
b380b885 MD |
178 | "Return a list of the object code modules that have been imported into\n" |
179 | "the current Guile process. Each element of the list is a pair whose\n" | |
180 | "car is the name of the module (as it might be used by\n" | |
181 | "@code{use-modules}, for instance), and whose cdr is the function handle\n" | |
182 | "for that module's initializer function.") | |
1bbd0b84 | 183 | #define FUNC_NAME s_scm_registered_modules |
80bc7890 MV |
184 | { |
185 | SCM res; | |
186 | struct moddata *md; | |
187 | ||
188 | res = SCM_EOL; | |
189 | for (md = registered_mods; md; md = md->link) | |
190 | res = scm_cons (scm_cons (scm_makfrom0str (md->module_name), | |
191 | scm_ulong2num ((unsigned long) md->init_func)), | |
192 | res); | |
193 | return res; | |
194 | } | |
1bbd0b84 | 195 | #undef FUNC_NAME |
80bc7890 | 196 | |
a1ec6916 | 197 | SCM_DEFINE (scm_clear_registered_modules, "c-clear-registered-modules", 0, 0, 0, |
1bbd0b84 | 198 | (), |
b380b885 MD |
199 | "Destroy the list of modules registered with the current Guile process.\n" |
200 | "The return value is unspecified. @strong{Warning:} this function does\n" | |
201 | "not actually unlink or deallocate these modules, but only destroys the\n" | |
202 | "records of which modules have been loaded. It should therefore be used\n" | |
203 | "only by module bookkeeping operations.") | |
1bbd0b84 | 204 | #define FUNC_NAME s_scm_clear_registered_modules |
80bc7890 MV |
205 | { |
206 | struct moddata *md1, *md2; | |
207 | ||
208 | SCM_DEFER_INTS; | |
209 | ||
210 | for (md1 = registered_mods; md1; md1 = md2) { | |
211 | md2 = md1->link; | |
212 | free (md1); | |
213 | } | |
214 | registered_mods = NULL; | |
215 | ||
216 | SCM_ALLOW_INTS; | |
217 | return SCM_UNSPECIFIED; | |
218 | } | |
1bbd0b84 | 219 | #undef FUNC_NAME |
80bc7890 | 220 | |
1edae076 | 221 | /* Dispatch to the system dependent files |
80bc7890 | 222 | * |
419e9e11 MV |
223 | * They define some static functions. These functions are called with |
224 | * deferred interrupts. When they want to throw errors, they are | |
225 | * expected to insert a SCM_ALLOW_INTS before doing the throw. It | |
226 | * might work to throw an error while interrupts are deferred (because | |
227 | * they will be unconditionally allowed the next time a SCM_ALLOW_INTS | |
228 | * is executed, SCM_DEFER_INTS and SCM_ALLOW_INTS do not nest). | |
1edae076 MV |
229 | */ |
230 | ||
8e3ab003 MV |
231 | #define DYNL_GLOBAL 0x0001 |
232 | ||
4feb69af MV |
233 | #ifdef DYNAMIC_LINKING |
234 | ||
235 | #include <ltdl.h> | |
236 | ||
237 | static void * | |
238 | sysdep_dynl_link (const char *fname, int flags, const char *subr) | |
239 | { | |
240 | lt_dlhandle handle = lt_dlopenext (fname); | |
241 | if (NULL == handle) | |
242 | { | |
243 | SCM_ALLOW_INTS; | |
244 | scm_misc_error (subr, (char *)lt_dlerror (), SCM_EOL); | |
245 | } | |
246 | return (void *) handle; | |
247 | } | |
248 | ||
249 | static void | |
250 | sysdep_dynl_unlink (void *handle, const char *subr) | |
251 | { | |
252 | if (lt_dlclose ((lt_dlhandle) handle)) | |
253 | { | |
254 | SCM_ALLOW_INTS; | |
255 | scm_misc_error (subr, (char *)lt_dlerror (), SCM_EOL); | |
256 | } | |
257 | } | |
258 | ||
259 | static void * | |
260 | sysdep_dynl_func (const char *symb, void *handle, const char *subr) | |
261 | { | |
262 | void *fptr; | |
263 | ||
264 | fptr = lt_dlsym ((lt_dlhandle) handle, symb); | |
265 | if (!fptr) | |
266 | { | |
267 | SCM_ALLOW_INTS; | |
268 | scm_misc_error (subr, (char *)lt_dlerror (), SCM_EOL); | |
269 | } | |
270 | return fptr; | |
271 | } | |
272 | ||
273 | static void | |
274 | sysdep_dynl_init () | |
275 | { | |
276 | lt_dlinit (); | |
277 | } | |
278 | ||
1edae076 | 279 | #else |
96599e6a MV |
280 | |
281 | /* no dynamic linking available, throw errors. */ | |
282 | ||
283 | static void | |
1bbd0b84 | 284 | sysdep_dynl_init (void) |
96599e6a MV |
285 | { |
286 | } | |
287 | ||
288 | static void | |
a80a90e9 | 289 | no_dynl_error (const char *subr) |
96599e6a | 290 | { |
419e9e11 MV |
291 | SCM_ALLOW_INTS; |
292 | scm_misc_error (subr, "dynamic linking not available", SCM_EOL); | |
96599e6a MV |
293 | } |
294 | ||
295 | static void * | |
8e3ab003 MV |
296 | sysdep_dynl_link (const char *filename, |
297 | int flags, | |
a80a90e9 | 298 | const char *subr) |
96599e6a MV |
299 | { |
300 | no_dynl_error (subr); | |
301 | return NULL; | |
302 | } | |
303 | ||
304 | static void | |
a80a90e9 JB |
305 | sysdep_dynl_unlink (void *handle, |
306 | const char *subr) | |
96599e6a MV |
307 | { |
308 | no_dynl_error (subr); | |
309 | } | |
310 | ||
311 | static void * | |
a80a90e9 JB |
312 | sysdep_dynl_func (const char *symbol, |
313 | void *handle, | |
314 | const char *subr) | |
96599e6a MV |
315 | { |
316 | no_dynl_error (subr); | |
317 | return NULL; | |
318 | } | |
319 | ||
80bc7890 MV |
320 | #endif |
321 | ||
322 | int scm_tc16_dynamic_obj; | |
323 | ||
324 | struct dynl_obj { | |
325 | SCM filename; | |
326 | void *handle; | |
327 | }; | |
328 | ||
80bc7890 | 329 | static SCM |
1bbd0b84 | 330 | mark_dynl_obj (SCM ptr) |
80bc7890 MV |
331 | { |
332 | struct dynl_obj *d = (struct dynl_obj *)SCM_CDR (ptr); | |
80bc7890 MV |
333 | return d->filename; |
334 | } | |
335 | ||
c487ad44 | 336 | static scm_sizet |
1bbd0b84 | 337 | free_dynl_obj (SCM ptr) |
c487ad44 MV |
338 | { |
339 | scm_must_free ((char *)SCM_CDR (ptr)); | |
340 | return sizeof (struct dynl_obj); | |
341 | } | |
342 | ||
80bc7890 | 343 | static int |
1bbd0b84 | 344 | print_dynl_obj (SCM exp,SCM port,scm_print_state *pstate) |
80bc7890 MV |
345 | { |
346 | struct dynl_obj *d = (struct dynl_obj *)SCM_CDR (exp); | |
b7f3516f | 347 | scm_puts ("#<dynamic-object ", port); |
80bc7890 | 348 | scm_iprin1 (d->filename, port, pstate); |
1fe1799f | 349 | if (d->handle == NULL) |
b7f3516f TT |
350 | scm_puts (" (unlinked)", port); |
351 | scm_putc ('>', port); | |
80bc7890 MV |
352 | return 1; |
353 | } | |
354 | ||
8e3ab003 MV |
355 | static SCM kw_global; |
356 | SCM_SYMBOL (sym_global, "-global"); | |
357 | ||
a1ec6916 | 358 | SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 1, |
1bbd0b84 | 359 | (SCM fname, SCM rest), |
b380b885 MD |
360 | "Open the dynamic library @var{library-file}. A library handle\n" |
361 | "representing the opened library is returned; this handle should be used\n" | |
362 | "as the @var{lib} argument to the following functions.") | |
1bbd0b84 | 363 | #define FUNC_NAME s_scm_dynamic_link |
80bc7890 MV |
364 | { |
365 | SCM z; | |
c487ad44 | 366 | void *handle; |
80bc7890 | 367 | struct dynl_obj *d; |
8e3ab003 | 368 | int flags = DYNL_GLOBAL; |
80bc7890 | 369 | |
c1bfcf60 | 370 | SCM_COERCE_ROSTRING (1, fname); |
c487ad44 | 371 | |
8e3ab003 | 372 | /* collect flags */ |
0c95b57d | 373 | while (SCM_CONSP (rest)) |
8e3ab003 MV |
374 | { |
375 | SCM kw, val; | |
376 | ||
377 | kw = SCM_CAR (rest); | |
378 | rest = SCM_CDR (rest); | |
379 | ||
0c95b57d | 380 | if (!SCM_CONSP (rest)) |
5d2d2ffc | 381 | SCM_MISC_ERROR ("keyword without value", SCM_EOL); |
8e3ab003 MV |
382 | |
383 | val = SCM_CAR (rest); | |
384 | rest = SCM_CDR (rest); | |
385 | ||
386 | if (kw == kw_global) | |
387 | { | |
388 | if (SCM_FALSEP (val)) | |
389 | flags &= ~DYNL_GLOBAL; | |
390 | } | |
391 | else | |
5d2d2ffc | 392 | SCM_MISC_ERROR ("unknown keyword argument: ~A", |
8e3ab003 MV |
393 | scm_cons (kw, SCM_EOL)); |
394 | } | |
395 | ||
c487ad44 | 396 | SCM_DEFER_INTS; |
1bbd0b84 | 397 | handle = sysdep_dynl_link (SCM_CHARS (fname), flags, FUNC_NAME); |
c487ad44 | 398 | |
80bc7890 | 399 | d = (struct dynl_obj *)scm_must_malloc (sizeof (struct dynl_obj), |
1bbd0b84 | 400 | FUNC_NAME); |
80bc7890 | 401 | d->filename = fname; |
c487ad44 | 402 | d->handle = handle; |
80bc7890 | 403 | |
80bc7890 MV |
404 | SCM_NEWCELL (z); |
405 | SCM_SETCHARS (z, d); | |
406 | SCM_SETCAR (z, scm_tc16_dynamic_obj); | |
407 | SCM_ALLOW_INTS; | |
408 | ||
409 | return z; | |
410 | } | |
1bbd0b84 | 411 | #undef FUNC_NAME |
80bc7890 | 412 | |
80bc7890 | 413 | static struct dynl_obj * |
1bbd0b84 | 414 | get_dynl_obj (SCM dobj,const char *subr,int argn) |
80bc7890 MV |
415 | { |
416 | struct dynl_obj *d; | |
c209c88e | 417 | SCM_ASSERT (SCM_NIMP (dobj) && SCM_CARW (dobj) == scm_tc16_dynamic_obj, |
80bc7890 MV |
418 | dobj, argn, subr); |
419 | d = (struct dynl_obj *)SCM_CDR (dobj); | |
420 | SCM_ASSERT (d->handle != NULL, dobj, argn, subr); | |
421 | return d; | |
422 | } | |
423 | ||
a1ec6916 | 424 | SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0, |
1bbd0b84 | 425 | (SCM obj), |
b380b885 MD |
426 | "Return @code{#t} if @var{obj} is a dynamic library handle, or @code{#f}\n" |
427 | "otherwise.") | |
1bbd0b84 | 428 | #define FUNC_NAME s_scm_dynamic_object_p |
80bc7890 | 429 | { |
c209c88e | 430 | return SCM_BOOL(SCM_NIMP (obj) && SCM_CARW (obj) == scm_tc16_dynamic_obj); |
80bc7890 | 431 | } |
1bbd0b84 | 432 | #undef FUNC_NAME |
80bc7890 | 433 | |
a1ec6916 | 434 | SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0, |
1bbd0b84 | 435 | (SCM dobj), |
b380b885 MD |
436 | "Unlink the library represented by @var{library-handle}, and remove any\n" |
437 | "imported symbols from the address space.\n" | |
438 | "GJB:FIXME:DOC: 2nd version below:\n" | |
439 | "Unlink the indicated object file from the application. The argument\n" | |
440 | "@var{dynobj} should be one of the values returned by\n" | |
441 | "@code{dynamic-link}. When @code{dynamic-unlink} has been called on\n" | |
442 | "@var{dynobj}, it is no longer usable as an argument to the functions\n" | |
443 | "below and you will get type mismatch errors when you try to.\n" | |
444 | "") | |
1bbd0b84 | 445 | #define FUNC_NAME s_scm_dynamic_unlink |
80bc7890 | 446 | { |
1bbd0b84 | 447 | struct dynl_obj *d = get_dynl_obj (dobj, FUNC_NAME, SCM_ARG1); |
419e9e11 | 448 | SCM_DEFER_INTS; |
1bbd0b84 | 449 | sysdep_dynl_unlink (d->handle, FUNC_NAME); |
80bc7890 | 450 | d->handle = NULL; |
419e9e11 MV |
451 | SCM_ALLOW_INTS; |
452 | return SCM_UNSPECIFIED; | |
80bc7890 | 453 | } |
1bbd0b84 | 454 | #undef FUNC_NAME |
80bc7890 | 455 | |
a1ec6916 | 456 | SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0, |
1bbd0b84 | 457 | (SCM symb, SCM dobj), |
b380b885 MD |
458 | "Import the symbol @var{func} from @var{lib} (a dynamic library handle).\n" |
459 | "A @dfn{function handle} representing the imported function is returned.\n" | |
460 | "GJB:FIXME:DOC: 2nd version below\n" | |
461 | "Search the C function indicated by @var{function} (a string or symbol)\n" | |
462 | "in @var{dynobj} and return some Scheme object that can later be used\n" | |
463 | "with @code{dynamic-call} to actually call this function. Right now,\n" | |
464 | "these Scheme objects are formed by casting the address of the function\n" | |
465 | "to @code{long} and converting this number to its Scheme representation.\n\n" | |
466 | "Regardless whether your C compiler prepends an underscore @samp{_} to\n" | |
467 | "the global names in a program, you should @strong{not} include this\n" | |
468 | "underscore in @var{function}. Guile knows whether the underscore is\n" | |
469 | "needed or not and will add it when necessary.\n\n" | |
470 | "") | |
1bbd0b84 | 471 | #define FUNC_NAME s_scm_dynamic_func |
80bc7890 MV |
472 | { |
473 | struct dynl_obj *d; | |
474 | void (*func) (); | |
475 | ||
c1bfcf60 | 476 | SCM_COERCE_ROSTRING (1, symb); |
1bbd0b84 | 477 | d = get_dynl_obj (dobj, FUNC_NAME, SCM_ARG2); |
80bc7890 | 478 | |
419e9e11 | 479 | SCM_DEFER_INTS; |
cdbadcac | 480 | func = (void (*) ()) sysdep_dynl_func (SCM_CHARS (symb), d->handle, |
1bbd0b84 | 481 | FUNC_NAME); |
419e9e11 MV |
482 | SCM_ALLOW_INTS; |
483 | ||
80bc7890 MV |
484 | return scm_ulong2num ((unsigned long)func); |
485 | } | |
1bbd0b84 | 486 | #undef FUNC_NAME |
80bc7890 | 487 | |
a1ec6916 | 488 | SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0, |
1bbd0b84 | 489 | (SCM func, SCM dobj), |
b380b885 MD |
490 | "Call @var{lib-thunk}, a procedure of no arguments. If @var{lib-thunk}\n" |
491 | "is a string, it is assumed to be a symbol found in the dynamic library\n" | |
492 | "@var{lib} and is fetched with @code{dynamic-func}. Otherwise, it should\n" | |
493 | "be a function handle returned by a previous call to @code{dynamic-func}.\n" | |
494 | "The return value is unspecified.\n" | |
495 | "GJB:FIXME:DOC 2nd version below\n" | |
496 | "Call the C function indicated by @var{function} and @var{dynobj}. The\n" | |
497 | "function is passed no arguments and its return value is ignored. When\n" | |
498 | "@var{function} is something returned by @code{dynamic-func}, call that\n" | |
499 | "function and ignore @var{dynobj}. When @var{function} is a string (or\n" | |
500 | "symbol, etc.), look it up in @var{dynobj}; this is equivalent to\n\n" | |
501 | "@smallexample\n" | |
502 | "(dynamic-call (dynamic-func @var{function} @var{dynobj} #f))\n" | |
503 | "@end smallexample\n\n" | |
504 | "Interrupts are deferred while the C function is executing (with\n" | |
505 | "@code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).\n" | |
506 | "") | |
1bbd0b84 | 507 | #define FUNC_NAME s_scm_dynamic_call |
80bc7890 MV |
508 | { |
509 | void (*fptr)(); | |
510 | ||
0c95b57d | 511 | if (SCM_ROSTRINGP (func)) |
80bc7890 | 512 | func = scm_dynamic_func (func, dobj); |
4638e087 | 513 | fptr = (void (*)()) SCM_NUM2ULONG (1, func); |
419e9e11 | 514 | SCM_DEFER_INTS; |
80bc7890 | 515 | fptr (); |
419e9e11 MV |
516 | SCM_ALLOW_INTS; |
517 | return SCM_UNSPECIFIED; | |
80bc7890 | 518 | } |
1bbd0b84 | 519 | #undef FUNC_NAME |
80bc7890 | 520 | |
a1ec6916 | 521 | SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0, |
1bbd0b84 | 522 | (SCM func, SCM dobj, SCM args), |
b380b885 MD |
523 | "Call @var{proc}, a dynamically loaded function, passing it the argument\n" |
524 | "list @var{args} (a list of strings). As with @code{dynamic-call},\n" | |
525 | "@var{proc} should be either a function handle or a string, in which case\n" | |
526 | "it is first fetched from @var{lib} with @code{dynamic-func}.\n\n" | |
527 | "@var{proc} is assumed to return an integer, which is used as the return\n" | |
528 | "value from @code{dynamic-args-call}.\n\n" | |
529 | "GJB:FIXME:DOC 2nd version below\n" | |
530 | "Call the C function indicated by @var{function} and @var{dynobj}, just\n" | |
531 | "like @code{dynamic-call}, but pass it some arguments and return its\n" | |
532 | "return value. The C function is expected to take two arguments and\n" | |
533 | "return an @code{int}, just like @code{main}:\n\n" | |
534 | "@smallexample\n" | |
535 | "int c_func (int argc, char **argv);\n" | |
536 | "@end smallexample\n\n" | |
537 | "The parameter @var{args} must be a list of strings and is converted into\n" | |
538 | "an array of @code{char *}. The array is passed in @var{argv} and its\n" | |
539 | "size in @var{argc}. The return value is converted to a Scheme number\n" | |
540 | "and returned from the call to @code{dynamic-args-call}.\n\n\n" | |
541 | "") | |
1bbd0b84 | 542 | #define FUNC_NAME s_scm_dynamic_args_call |
80bc7890 MV |
543 | { |
544 | int (*fptr) (int argc, char **argv); | |
545 | int result, argc; | |
546 | char **argv; | |
547 | ||
0c95b57d | 548 | if (SCM_ROSTRINGP (func)) |
80bc7890 MV |
549 | func = scm_dynamic_func (func, dobj); |
550 | ||
4638e087 | 551 | fptr = (int (*)(int, char **)) SCM_NUM2ULONG (1,func); |
419e9e11 | 552 | SCM_DEFER_INTS; |
1bbd0b84 | 553 | argv = scm_make_argv_from_stringlist (args, &argc, FUNC_NAME, |
80bc7890 | 554 | SCM_ARG3); |
80bc7890 | 555 | result = (*fptr) (argc, argv); |
80bc7890 | 556 | scm_must_free_argv (argv); |
419e9e11 MV |
557 | SCM_ALLOW_INTS; |
558 | ||
80bc7890 MV |
559 | return SCM_MAKINUM(0L+result); |
560 | } | |
1bbd0b84 | 561 | #undef FUNC_NAME |
80bc7890 | 562 | |
1edae076 MV |
563 | void |
564 | scm_init_dynamic_linking () | |
565 | { | |
23a62151 MD |
566 | scm_tc16_dynamic_obj = scm_make_smob_type_mfpe ("dynamic-object", sizeof (struct dynl_obj), |
567 | mark_dynl_obj, free_dynl_obj, | |
568 | print_dynl_obj, NULL); | |
80bc7890 MV |
569 | sysdep_dynl_init (); |
570 | #include "dynl.x" | |
8e3ab003 | 571 | kw_global = scm_make_keyword_from_dash_symbol (sym_global); |
1edae076 | 572 | } |