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> |
a0599745 MD |
68 | #include "libguile/_scm.h" |
69 | #include "libguile/dynl.h" | |
70 | #include "libguile/smob.h" | |
71 | #include "libguile/keywords.h" | |
72 | #include "libguile/ports.h" | |
73 | #include "libguile/strings.h" | |
80bc7890 | 74 | |
a0599745 | 75 | #include "libguile/validate.h" |
408ea28a | 76 | |
1edae076 MV |
77 | /* Converting a list of SCM strings into a argv-style array. You must |
78 | have ints disabled for the whole lifetime of the created argv (from | |
79 | before MAKE_ARGV_FROM_STRINGLIST until after | |
80 | MUST_FREE_ARGV). Atleast this is was the documentation for | |
81 | MAKARGVFROMSTRS says, it isn't really used that way. | |
82 | ||
83 | This code probably belongs into strings.c */ | |
84 | ||
1edae076 | 85 | static char ** |
1bbd0b84 | 86 | scm_make_argv_from_stringlist (SCM args,int *argcp,const char *subr,int argn) |
1edae076 | 87 | { |
7cf1a27e MD |
88 | char **argv; |
89 | int argc, i; | |
90 | ||
91 | argc = scm_ilength (args); | |
92 | argv = (char **) scm_must_malloc ((1L + argc) * sizeof (char *), subr); | |
93 | for (i = 0; SCM_NNULLP (args); args = SCM_CDR (args), i++) { | |
94 | size_t len; | |
95 | char *dst, *src; | |
96 | SCM str = SCM_CAR (args); | |
97 | ||
98 | SCM_ASSERT (SCM_ROSTRINGP (str), str, argn, subr); | |
99 | len = 1 + SCM_ROLENGTH (str); | |
100 | dst = (char *) scm_must_malloc ((long) len, subr); | |
101 | src = SCM_ROCHARS (str); | |
102 | while (len--) | |
103 | dst[len] = src[len]; | |
104 | argv[i] = dst; | |
105 | } | |
106 | ||
107 | if (argcp) | |
108 | *argcp = argc; | |
109 | argv[argc] = 0; | |
110 | return argv; | |
1edae076 MV |
111 | } |
112 | ||
1edae076 | 113 | static void |
1bbd0b84 | 114 | scm_must_free_argv(char **argv) |
1edae076 | 115 | { |
7cf1a27e MD |
116 | char **av = argv; |
117 | while (*av) | |
118 | free (*(av++)); | |
119 | free (argv); | |
1edae076 MV |
120 | } |
121 | ||
122 | /* Coerce an arbitrary readonly-string into a zero-terminated string. | |
123 | */ | |
124 | ||
1edae076 | 125 | static SCM |
1bbd0b84 | 126 | scm_coerce_rostring (SCM rostr,const char *subr,int argn) |
1edae076 | 127 | { |
7cf1a27e MD |
128 | SCM_ASSERT (SCM_ROSTRINGP (rostr), rostr, argn, subr); |
129 | if (SCM_SUBSTRP (rostr)) | |
130 | rostr = scm_makfromstr (SCM_ROCHARS (rostr), SCM_ROLENGTH (rostr), 0); | |
131 | return rostr; | |
1edae076 MV |
132 | } |
133 | ||
80bc7890 MV |
134 | /* Module registry |
135 | */ | |
136 | ||
137 | /* We can't use SCM objects here. One should be able to call | |
138 | SCM_REGISTER_MODULE from a C++ constructor for a static | |
139 | object. This happens before main and thus before libguile is | |
140 | initialized. */ | |
141 | ||
142 | struct moddata { | |
7cf1a27e MD |
143 | struct moddata *link; |
144 | char *module_name; | |
145 | void *init_func; | |
80bc7890 MV |
146 | }; |
147 | ||
148 | static struct moddata *registered_mods = NULL; | |
149 | ||
150 | void | |
6e8d25a6 | 151 | scm_register_module_xxx (char *module_name, void *init_func) |
80bc7890 | 152 | { |
7cf1a27e | 153 | struct moddata *md; |
80bc7890 | 154 | |
7cf1a27e | 155 | /* XXX - should we (and can we) DEFER_INTS here? */ |
80bc7890 | 156 | |
7cf1a27e MD |
157 | for (md = registered_mods; md; md = md->link) |
158 | if (!strcmp (md->module_name, module_name)) | |
159 | { | |
160 | md->init_func = init_func; | |
80bc7890 | 161 | return; |
7cf1a27e MD |
162 | } |
163 | ||
164 | md = (struct moddata *) malloc (sizeof (struct moddata)); | |
165 | if (md == NULL) | |
166 | { | |
167 | fprintf (stderr, | |
168 | "guile: can't register module (%s): not enough memory", | |
169 | module_name); | |
170 | return; | |
96599e6a | 171 | } |
80bc7890 | 172 | |
7cf1a27e MD |
173 | md->module_name = module_name; |
174 | md->init_func = init_func; | |
175 | md->link = registered_mods; | |
176 | registered_mods = md; | |
80bc7890 MV |
177 | } |
178 | ||
a1ec6916 | 179 | SCM_DEFINE (scm_registered_modules, "c-registered-modules", 0, 0, 0, |
1bbd0b84 | 180 | (), |
b380b885 MD |
181 | "Return a list of the object code modules that have been imported into\n" |
182 | "the current Guile process. Each element of the list is a pair whose\n" | |
f15a06bf MV |
183 | "car is the name of the module, and whose cdr is the function handle\n" |
184 | "for that module's initializer function. The name is the string that\n" | |
185 | "has been passed to scm_register_module_xxx.") | |
1bbd0b84 | 186 | #define FUNC_NAME s_scm_registered_modules |
80bc7890 | 187 | { |
7cf1a27e MD |
188 | SCM res; |
189 | struct moddata *md; | |
190 | ||
191 | res = SCM_EOL; | |
192 | for (md = registered_mods; md; md = md->link) | |
193 | res = scm_cons (scm_cons (scm_makfrom0str (md->module_name), | |
194 | scm_ulong2num ((unsigned long) md->init_func)), | |
195 | res); | |
196 | return res; | |
80bc7890 | 197 | } |
1bbd0b84 | 198 | #undef FUNC_NAME |
80bc7890 | 199 | |
a1ec6916 | 200 | SCM_DEFINE (scm_clear_registered_modules, "c-clear-registered-modules", 0, 0, 0, |
1bbd0b84 | 201 | (), |
b380b885 MD |
202 | "Destroy the list of modules registered with the current Guile process.\n" |
203 | "The return value is unspecified. @strong{Warning:} this function does\n" | |
204 | "not actually unlink or deallocate these modules, but only destroys the\n" | |
205 | "records of which modules have been loaded. It should therefore be used\n" | |
206 | "only by module bookkeeping operations.") | |
1bbd0b84 | 207 | #define FUNC_NAME s_scm_clear_registered_modules |
80bc7890 | 208 | { |
7cf1a27e | 209 | struct moddata *md1, *md2; |
80bc7890 | 210 | |
7cf1a27e | 211 | SCM_DEFER_INTS; |
80bc7890 | 212 | |
7cf1a27e MD |
213 | for (md1 = registered_mods; md1; md1 = md2) |
214 | { | |
215 | md2 = md1->link; | |
216 | free (md1); | |
80bc7890 | 217 | } |
7cf1a27e | 218 | registered_mods = NULL; |
80bc7890 | 219 | |
7cf1a27e MD |
220 | SCM_ALLOW_INTS; |
221 | return SCM_UNSPECIFIED; | |
80bc7890 | 222 | } |
1bbd0b84 | 223 | #undef FUNC_NAME |
80bc7890 | 224 | |
1edae076 | 225 | /* Dispatch to the system dependent files |
80bc7890 | 226 | * |
419e9e11 MV |
227 | * They define some static functions. These functions are called with |
228 | * deferred interrupts. When they want to throw errors, they are | |
229 | * expected to insert a SCM_ALLOW_INTS before doing the throw. It | |
230 | * might work to throw an error while interrupts are deferred (because | |
231 | * they will be unconditionally allowed the next time a SCM_ALLOW_INTS | |
232 | * is executed, SCM_DEFER_INTS and SCM_ALLOW_INTS do not nest). | |
1edae076 MV |
233 | */ |
234 | ||
4feb69af MV |
235 | #ifdef DYNAMIC_LINKING |
236 | ||
07286af9 | 237 | #include "libltdl/ltdl.h" |
4feb69af MV |
238 | |
239 | static void * | |
af45e3b0 | 240 | sysdep_dynl_link (const char *fname, const char *subr) |
4feb69af | 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 * | |
af45e3b0 | 299 | sysdep_dynl_link (const char *filename, const char *subr) |
96599e6a | 300 | { |
7cf1a27e MD |
301 | no_dynl_error (subr); |
302 | return NULL; | |
96599e6a MV |
303 | } |
304 | ||
305 | static void | |
a80a90e9 JB |
306 | sysdep_dynl_unlink (void *handle, |
307 | const char *subr) | |
96599e6a | 308 | { |
7cf1a27e | 309 | no_dynl_error (subr); |
96599e6a MV |
310 | } |
311 | ||
312 | static void * | |
a80a90e9 JB |
313 | sysdep_dynl_func (const char *symbol, |
314 | void *handle, | |
315 | const char *subr) | |
96599e6a | 316 | { |
7cf1a27e MD |
317 | no_dynl_error (subr); |
318 | return NULL; | |
96599e6a MV |
319 | } |
320 | ||
80bc7890 MV |
321 | #endif |
322 | ||
323 | int scm_tc16_dynamic_obj; | |
324 | ||
b82c6ce0 DH |
325 | #define DYNL_FILENAME(x) (SCM_CELL_OBJECT_1 (x)) |
326 | #define DYNL_HANDLE(x) ((void *) SCM_CELL_WORD_2 (x)) | |
327 | #define SET_DYNL_HANDLE(x, v) (SCM_SET_CELL_WORD_2 ((x), (v))) | |
7cf1a27e | 328 | |
7cf1a27e | 329 | |
80bc7890 | 330 | static SCM |
1bbd0b84 | 331 | mark_dynl_obj (SCM ptr) |
80bc7890 | 332 | { |
7cf1a27e | 333 | return DYNL_FILENAME (ptr); |
c487ad44 MV |
334 | } |
335 | ||
80bc7890 | 336 | static int |
1bbd0b84 | 337 | print_dynl_obj (SCM exp,SCM port,scm_print_state *pstate) |
80bc7890 | 338 | { |
7cf1a27e MD |
339 | scm_puts ("#<dynamic-object ", port); |
340 | scm_iprin1 (DYNL_FILENAME (exp), port, pstate); | |
341 | if (DYNL_HANDLE (exp) == NULL) | |
342 | scm_puts (" (unlinked)", port); | |
343 | scm_putc ('>', port); | |
344 | return 1; | |
80bc7890 MV |
345 | } |
346 | ||
8e3ab003 | 347 | |
af45e3b0 DH |
348 | SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 0, |
349 | (SCM fname), | |
b380b885 MD |
350 | "Open the dynamic library @var{library-file}. A library handle\n" |
351 | "representing the opened library is returned; this handle should be used\n" | |
352 | "as the @var{lib} argument to the following functions.") | |
1bbd0b84 | 353 | #define FUNC_NAME s_scm_dynamic_link |
80bc7890 | 354 | { |
7cf1a27e | 355 | void *handle; |
80bc7890 | 356 | |
7cf1a27e | 357 | SCM_COERCE_ROSTRING (1, fname); |
af45e3b0 | 358 | handle = sysdep_dynl_link (SCM_CHARS (fname), FUNC_NAME); |
cffcab30 | 359 | SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, SCM_UNPACK (fname), handle); |
80bc7890 | 360 | } |
1bbd0b84 | 361 | #undef FUNC_NAME |
80bc7890 | 362 | |
80bc7890 | 363 | |
a1ec6916 | 364 | SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0, |
1bbd0b84 | 365 | (SCM obj), |
b380b885 MD |
366 | "Return @code{#t} if @var{obj} is a dynamic library handle, or @code{#f}\n" |
367 | "otherwise.") | |
1bbd0b84 | 368 | #define FUNC_NAME s_scm_dynamic_object_p |
80bc7890 | 369 | { |
b82c6ce0 | 370 | return SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_dynamic_obj, obj)); |
80bc7890 | 371 | } |
1bbd0b84 | 372 | #undef FUNC_NAME |
80bc7890 | 373 | |
b82c6ce0 | 374 | |
a1ec6916 | 375 | SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0, |
1bbd0b84 | 376 | (SCM dobj), |
b82c6ce0 DH |
377 | "Unlink the library represented by @var{library-handle},\n" |
378 | "and remove any imported symbols from the address space.\n" | |
b380b885 | 379 | "GJB:FIXME:DOC: 2nd version below:\n" |
b82c6ce0 DH |
380 | "Unlink the indicated object file from the application. The\n" |
381 | "argument @var{dynobj} must have been obtained by a call to\n" | |
382 | "@code{dynamic-link}. After @code{dynamic-unlink} has been\n" | |
383 | "called on @var{dynobj}, its content is no longer accessible.\n") | |
1bbd0b84 | 384 | #define FUNC_NAME s_scm_dynamic_unlink |
80bc7890 | 385 | { |
7cf1a27e | 386 | /*fixme* GC-problem */ |
b82c6ce0 DH |
387 | SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj); |
388 | if (DYNL_HANDLE (dobj) == NULL) { | |
389 | SCM_MISC_ERROR ("Already unlinked: ~S", dobj); | |
390 | } else { | |
391 | SCM_DEFER_INTS; | |
392 | sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME); | |
393 | SET_DYNL_HANDLE (dobj, NULL); | |
394 | SCM_ALLOW_INTS; | |
395 | return SCM_UNSPECIFIED; | |
396 | } | |
80bc7890 | 397 | } |
1bbd0b84 | 398 | #undef FUNC_NAME |
80bc7890 | 399 | |
b82c6ce0 | 400 | |
a1ec6916 | 401 | SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0, |
1bbd0b84 | 402 | (SCM symb, SCM dobj), |
b380b885 MD |
403 | "Import the symbol @var{func} from @var{lib} (a dynamic library handle).\n" |
404 | "A @dfn{function handle} representing the imported function is returned.\n" | |
405 | "GJB:FIXME:DOC: 2nd version below\n" | |
406 | "Search the C function indicated by @var{function} (a string or symbol)\n" | |
407 | "in @var{dynobj} and return some Scheme object that can later be used\n" | |
408 | "with @code{dynamic-call} to actually call this function. Right now,\n" | |
409 | "these Scheme objects are formed by casting the address of the function\n" | |
410 | "to @code{long} and converting this number to its Scheme representation.\n\n" | |
411 | "Regardless whether your C compiler prepends an underscore @samp{_} to\n" | |
412 | "the global names in a program, you should @strong{not} include this\n" | |
413 | "underscore in @var{function}. Guile knows whether the underscore is\n" | |
414 | "needed or not and will add it when necessary.\n\n" | |
415 | "") | |
1bbd0b84 | 416 | #define FUNC_NAME s_scm_dynamic_func |
80bc7890 | 417 | { |
7cf1a27e | 418 | void (*func) (); |
80bc7890 | 419 | |
7cf1a27e MD |
420 | SCM_COERCE_ROSTRING (1, symb); |
421 | /*fixme* GC-problem */ | |
b82c6ce0 DH |
422 | SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj); |
423 | if (DYNL_HANDLE (dobj) == NULL) { | |
424 | SCM_MISC_ERROR ("Already unlinked: ~S", dobj); | |
425 | } else { | |
426 | SCM_DEFER_INTS; | |
427 | func = (void (*) ()) sysdep_dynl_func (SCM_CHARS (symb), | |
428 | DYNL_HANDLE (dobj), | |
429 | FUNC_NAME); | |
430 | SCM_ALLOW_INTS; | |
431 | return scm_ulong2num ((unsigned long) func); | |
432 | } | |
80bc7890 | 433 | } |
1bbd0b84 | 434 | #undef FUNC_NAME |
80bc7890 | 435 | |
b82c6ce0 | 436 | |
a1ec6916 | 437 | SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0, |
1bbd0b84 | 438 | (SCM func, SCM dobj), |
b380b885 MD |
439 | "Call @var{lib-thunk}, a procedure of no arguments. If @var{lib-thunk}\n" |
440 | "is a string, it is assumed to be a symbol found in the dynamic library\n" | |
441 | "@var{lib} and is fetched with @code{dynamic-func}. Otherwise, it should\n" | |
442 | "be a function handle returned by a previous call to @code{dynamic-func}.\n" | |
443 | "The return value is unspecified.\n" | |
444 | "GJB:FIXME:DOC 2nd version below\n" | |
445 | "Call the C function indicated by @var{function} and @var{dynobj}. The\n" | |
446 | "function is passed no arguments and its return value is ignored. When\n" | |
447 | "@var{function} is something returned by @code{dynamic-func}, call that\n" | |
448 | "function and ignore @var{dynobj}. When @var{function} is a string (or\n" | |
449 | "symbol, etc.), look it up in @var{dynobj}; this is equivalent to\n\n" | |
450 | "@smallexample\n" | |
451 | "(dynamic-call (dynamic-func @var{function} @var{dynobj} #f))\n" | |
452 | "@end smallexample\n\n" | |
453 | "Interrupts are deferred while the C function is executing (with\n" | |
454 | "@code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).\n" | |
455 | "") | |
1bbd0b84 | 456 | #define FUNC_NAME s_scm_dynamic_call |
80bc7890 | 457 | { |
7cf1a27e MD |
458 | void (*fptr) (); |
459 | ||
460 | if (SCM_ROSTRINGP (func)) | |
461 | func = scm_dynamic_func (func, dobj); | |
462 | fptr = (void (*) ()) SCM_NUM2ULONG (1, func); | |
463 | SCM_DEFER_INTS; | |
464 | fptr (); | |
465 | SCM_ALLOW_INTS; | |
466 | return SCM_UNSPECIFIED; | |
80bc7890 | 467 | } |
1bbd0b84 | 468 | #undef FUNC_NAME |
80bc7890 | 469 | |
a1ec6916 | 470 | SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0, |
1bbd0b84 | 471 | (SCM func, SCM dobj, SCM args), |
b380b885 MD |
472 | "Call @var{proc}, a dynamically loaded function, passing it the argument\n" |
473 | "list @var{args} (a list of strings). As with @code{dynamic-call},\n" | |
474 | "@var{proc} should be either a function handle or a string, in which case\n" | |
475 | "it is first fetched from @var{lib} with @code{dynamic-func}.\n\n" | |
476 | "@var{proc} is assumed to return an integer, which is used as the return\n" | |
477 | "value from @code{dynamic-args-call}.\n\n" | |
478 | "GJB:FIXME:DOC 2nd version below\n" | |
479 | "Call the C function indicated by @var{function} and @var{dynobj}, just\n" | |
480 | "like @code{dynamic-call}, but pass it some arguments and return its\n" | |
481 | "return value. The C function is expected to take two arguments and\n" | |
482 | "return an @code{int}, just like @code{main}:\n\n" | |
483 | "@smallexample\n" | |
484 | "int c_func (int argc, char **argv);\n" | |
485 | "@end smallexample\n\n" | |
486 | "The parameter @var{args} must be a list of strings and is converted into\n" | |
487 | "an array of @code{char *}. The array is passed in @var{argv} and its\n" | |
488 | "size in @var{argc}. The return value is converted to a Scheme number\n" | |
489 | "and returned from the call to @code{dynamic-args-call}.\n\n\n" | |
490 | "") | |
1bbd0b84 | 491 | #define FUNC_NAME s_scm_dynamic_args_call |
80bc7890 | 492 | { |
7cf1a27e MD |
493 | int (*fptr) (int argc, char **argv); |
494 | int result, argc; | |
495 | char **argv; | |
496 | ||
497 | if (SCM_ROSTRINGP (func)) | |
498 | func = scm_dynamic_func (func, dobj); | |
499 | ||
500 | fptr = (int (*) (int, char **)) SCM_NUM2ULONG (1, func); | |
501 | SCM_DEFER_INTS; | |
502 | argv = scm_make_argv_from_stringlist (args, &argc, FUNC_NAME, SCM_ARG3); | |
503 | result = (*fptr) (argc, argv); | |
504 | scm_must_free_argv (argv); | |
505 | SCM_ALLOW_INTS; | |
506 | ||
507 | return SCM_MAKINUM (0L + result); | |
80bc7890 | 508 | } |
1bbd0b84 | 509 | #undef FUNC_NAME |
80bc7890 | 510 | |
1edae076 MV |
511 | void |
512 | scm_init_dynamic_linking () | |
513 | { | |
7cf1a27e MD |
514 | scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0); |
515 | scm_set_smob_mark (scm_tc16_dynamic_obj, mark_dynl_obj); | |
516 | scm_set_smob_print (scm_tc16_dynamic_obj, print_dynl_obj); | |
517 | sysdep_dynl_init (); | |
a0599745 | 518 | #include "libguile/dynl.x" |
1edae076 | 519 | } |
89e00824 ML |
520 | |
521 | /* | |
522 | Local Variables: | |
523 | c-file-style: "gnu" | |
524 | End: | |
525 | */ |