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> |
13070bd3 DH |
68 | #include <string.h> |
69 | ||
a0599745 MD |
70 | #include "libguile/_scm.h" |
71 | #include "libguile/dynl.h" | |
72 | #include "libguile/smob.h" | |
73 | #include "libguile/keywords.h" | |
74 | #include "libguile/ports.h" | |
75 | #include "libguile/strings.h" | |
80bc7890 | 76 | |
a0599745 | 77 | #include "libguile/validate.h" |
408ea28a | 78 | |
a6d9e5ab DH |
79 | /* Create a new C argv array from a scheme list of strings. */ |
80 | /* Dirk:FIXME:: A quite similar function is implemented in posix.c */ | |
81 | /* Dirk:FIXME:: In case of assertion errors, we get memory leaks */ | |
82 | ||
1edae076 MV |
83 | /* Converting a list of SCM strings into a argv-style array. You must |
84 | have ints disabled for the whole lifetime of the created argv (from | |
85 | before MAKE_ARGV_FROM_STRINGLIST until after | |
86 | MUST_FREE_ARGV). Atleast this is was the documentation for | |
87 | MAKARGVFROMSTRS says, it isn't really used that way. | |
88 | ||
a6d9e5ab DH |
89 | This code probably belongs into strings.c |
90 | (Dirk: IMO strings.c is not the right place.) */ | |
1edae076 | 91 | |
1edae076 | 92 | static char ** |
1bbd0b84 | 93 | scm_make_argv_from_stringlist (SCM args,int *argcp,const char *subr,int argn) |
1edae076 | 94 | { |
7cf1a27e | 95 | char **argv; |
a6d9e5ab DH |
96 | int argc; |
97 | int i; | |
7cf1a27e MD |
98 | |
99 | argc = scm_ilength (args); | |
a6d9e5ab DH |
100 | SCM_ASSERT (argc >= 0, args, argn, subr); |
101 | argv = (char **) scm_must_malloc ((argc + 1) * sizeof (char *), subr); | |
102 | for (i = 0; !SCM_NULLP (args); args = SCM_CDR (args), ++i) { | |
103 | SCM arg = SCM_CAR (args); | |
1be6b49c | 104 | size_t len; |
a6d9e5ab DH |
105 | char *dst; |
106 | char *src; | |
107 | ||
108 | SCM_ASSERT (SCM_STRINGP (arg), args, argn, subr); | |
109 | len = SCM_STRING_LENGTH (arg); | |
34f0f2b8 | 110 | src = SCM_STRING_CHARS (arg); |
a6d9e5ab DH |
111 | dst = (char *) scm_must_malloc (len + 1, subr); |
112 | memcpy (dst, src, len); | |
113 | dst[len] = 0; | |
7cf1a27e MD |
114 | argv[i] = dst; |
115 | } | |
116 | ||
117 | if (argcp) | |
118 | *argcp = argc; | |
119 | argv[argc] = 0; | |
120 | return argv; | |
1edae076 MV |
121 | } |
122 | ||
1edae076 | 123 | static void |
1bbd0b84 | 124 | scm_must_free_argv(char **argv) |
1edae076 | 125 | { |
7cf1a27e MD |
126 | char **av = argv; |
127 | while (*av) | |
128 | free (*(av++)); | |
129 | free (argv); | |
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 | ||
4feb69af MV |
233 | #ifdef DYNAMIC_LINKING |
234 | ||
07286af9 | 235 | #include "libltdl/ltdl.h" |
4feb69af MV |
236 | |
237 | static void * | |
af45e3b0 | 238 | sysdep_dynl_link (const char *fname, const char *subr) |
4feb69af | 239 | { |
7cf1a27e MD |
240 | lt_dlhandle handle; |
241 | handle = lt_dlopenext (fname); | |
4feb69af MV |
242 | if (NULL == handle) |
243 | { | |
01449aa5 DH |
244 | SCM fn; |
245 | SCM msg; | |
246 | ||
4feb69af | 247 | SCM_ALLOW_INTS; |
01449aa5 DH |
248 | fn = scm_makfrom0str (fname); |
249 | msg = scm_makfrom0str (lt_dlerror ()); | |
250 | scm_misc_error (subr, "file: ~S, message: ~S", SCM_LIST2 (fn, msg)); | |
4feb69af MV |
251 | } |
252 | return (void *) handle; | |
253 | } | |
254 | ||
255 | static void | |
256 | sysdep_dynl_unlink (void *handle, const char *subr) | |
257 | { | |
258 | if (lt_dlclose ((lt_dlhandle) handle)) | |
259 | { | |
260 | SCM_ALLOW_INTS; | |
7cf1a27e | 261 | scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL); |
4feb69af MV |
262 | } |
263 | } | |
264 | ||
265 | static void * | |
266 | sysdep_dynl_func (const char *symb, void *handle, const char *subr) | |
267 | { | |
268 | void *fptr; | |
269 | ||
270 | fptr = lt_dlsym ((lt_dlhandle) handle, symb); | |
271 | if (!fptr) | |
272 | { | |
273 | SCM_ALLOW_INTS; | |
7cf1a27e | 274 | scm_misc_error (subr, (char *) lt_dlerror (), SCM_EOL); |
4feb69af MV |
275 | } |
276 | return fptr; | |
277 | } | |
278 | ||
279 | static void | |
280 | sysdep_dynl_init () | |
281 | { | |
282 | lt_dlinit (); | |
283 | } | |
284 | ||
1edae076 | 285 | #else |
96599e6a MV |
286 | |
287 | /* no dynamic linking available, throw errors. */ | |
288 | ||
289 | static void | |
1bbd0b84 | 290 | sysdep_dynl_init (void) |
96599e6a MV |
291 | { |
292 | } | |
293 | ||
294 | static void | |
a80a90e9 | 295 | no_dynl_error (const char *subr) |
96599e6a | 296 | { |
419e9e11 MV |
297 | SCM_ALLOW_INTS; |
298 | scm_misc_error (subr, "dynamic linking not available", SCM_EOL); | |
96599e6a MV |
299 | } |
300 | ||
301 | static void * | |
af45e3b0 | 302 | sysdep_dynl_link (const char *filename, const char *subr) |
96599e6a | 303 | { |
7cf1a27e MD |
304 | no_dynl_error (subr); |
305 | return NULL; | |
96599e6a MV |
306 | } |
307 | ||
308 | static void | |
a80a90e9 JB |
309 | sysdep_dynl_unlink (void *handle, |
310 | const char *subr) | |
96599e6a | 311 | { |
7cf1a27e | 312 | no_dynl_error (subr); |
96599e6a MV |
313 | } |
314 | ||
315 | static void * | |
a80a90e9 JB |
316 | sysdep_dynl_func (const char *symbol, |
317 | void *handle, | |
318 | const char *subr) | |
96599e6a | 319 | { |
7cf1a27e MD |
320 | no_dynl_error (subr); |
321 | return NULL; | |
96599e6a MV |
322 | } |
323 | ||
80bc7890 MV |
324 | #endif |
325 | ||
e841c3e0 | 326 | scm_bits_t scm_tc16_dynamic_obj; |
80bc7890 | 327 | |
b82c6ce0 DH |
328 | #define DYNL_FILENAME(x) (SCM_CELL_OBJECT_1 (x)) |
329 | #define DYNL_HANDLE(x) ((void *) SCM_CELL_WORD_2 (x)) | |
330 | #define SET_DYNL_HANDLE(x, v) (SCM_SET_CELL_WORD_2 ((x), (v))) | |
7cf1a27e | 331 | |
7cf1a27e | 332 | |
80bc7890 | 333 | static SCM |
e841c3e0 | 334 | dynl_obj_mark (SCM ptr) |
80bc7890 | 335 | { |
7cf1a27e | 336 | return DYNL_FILENAME (ptr); |
c487ad44 MV |
337 | } |
338 | ||
e841c3e0 | 339 | |
80bc7890 | 340 | static int |
e841c3e0 | 341 | dynl_obj_print (SCM exp, SCM port, scm_print_state *pstate) |
80bc7890 | 342 | { |
7cf1a27e MD |
343 | scm_puts ("#<dynamic-object ", port); |
344 | scm_iprin1 (DYNL_FILENAME (exp), port, pstate); | |
345 | if (DYNL_HANDLE (exp) == NULL) | |
346 | scm_puts (" (unlinked)", port); | |
347 | scm_putc ('>', port); | |
348 | return 1; | |
80bc7890 MV |
349 | } |
350 | ||
8e3ab003 | 351 | |
af45e3b0 | 352 | SCM_DEFINE (scm_dynamic_link, "dynamic-link", 1, 0, 0, |
1e6808ea MG |
353 | (SCM filename), |
354 | "Open the dynamic library called @var{filename}. A library\n" | |
355 | "handle representing the opened library is returned; this handle\n" | |
356 | "should be used as the @var{dobj} argument to the following\n" | |
357 | "functions.") | |
1bbd0b84 | 358 | #define FUNC_NAME s_scm_dynamic_link |
80bc7890 | 359 | { |
7cf1a27e | 360 | void *handle; |
80bc7890 | 361 | |
1e6808ea MG |
362 | SCM_VALIDATE_STRING (1, filename); |
363 | SCM_STRING_COERCE_0TERMINATION_X (filename); | |
364 | handle = sysdep_dynl_link (SCM_STRING_CHARS (filename), FUNC_NAME); | |
365 | SCM_RETURN_NEWSMOB2 (scm_tc16_dynamic_obj, SCM_UNPACK (filename), handle); | |
80bc7890 | 366 | } |
1bbd0b84 | 367 | #undef FUNC_NAME |
80bc7890 | 368 | |
80bc7890 | 369 | |
a1ec6916 | 370 | SCM_DEFINE (scm_dynamic_object_p, "dynamic-object?", 1, 0, 0, |
1bbd0b84 | 371 | (SCM obj), |
b380b885 MD |
372 | "Return @code{#t} if @var{obj} is a dynamic library handle, or @code{#f}\n" |
373 | "otherwise.") | |
1bbd0b84 | 374 | #define FUNC_NAME s_scm_dynamic_object_p |
80bc7890 | 375 | { |
e841c3e0 | 376 | return SCM_BOOL (SCM_TYP16_PREDICATE (scm_tc16_dynamic_obj, obj)); |
80bc7890 | 377 | } |
1bbd0b84 | 378 | #undef FUNC_NAME |
80bc7890 | 379 | |
b82c6ce0 | 380 | |
a1ec6916 | 381 | SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0, |
1bbd0b84 | 382 | (SCM dobj), |
b82c6ce0 | 383 | "Unlink the indicated object file from the application. The\n" |
1e6808ea | 384 | "argument @var{dobj} must have been obtained by a call to\n" |
b82c6ce0 | 385 | "@code{dynamic-link}. After @code{dynamic-unlink} has been\n" |
1e6808ea | 386 | "called on @var{dobj}, its content is no longer accessible.") |
1bbd0b84 | 387 | #define FUNC_NAME s_scm_dynamic_unlink |
80bc7890 | 388 | { |
7cf1a27e | 389 | /*fixme* GC-problem */ |
b82c6ce0 DH |
390 | SCM_VALIDATE_SMOB (SCM_ARG1, dobj, dynamic_obj); |
391 | if (DYNL_HANDLE (dobj) == NULL) { | |
392 | SCM_MISC_ERROR ("Already unlinked: ~S", dobj); | |
393 | } else { | |
394 | SCM_DEFER_INTS; | |
395 | sysdep_dynl_unlink (DYNL_HANDLE (dobj), FUNC_NAME); | |
396 | SET_DYNL_HANDLE (dobj, NULL); | |
397 | SCM_ALLOW_INTS; | |
398 | return SCM_UNSPECIFIED; | |
399 | } | |
80bc7890 | 400 | } |
1bbd0b84 | 401 | #undef FUNC_NAME |
80bc7890 | 402 | |
b82c6ce0 | 403 | |
a1ec6916 | 404 | SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0, |
a6d9e5ab DH |
405 | (SCM name, SCM dobj), |
406 | "Search the dynamic object @var{dobj} for the C function\n" | |
407 | "indicated by the string @var{name} and return some Scheme\n" | |
408 | "handle that can later be used with @code{dynamic-call} to\n" | |
409 | "actually call the function.\n\n" | |
b380b885 MD |
410 | "Regardless whether your C compiler prepends an underscore @samp{_} to\n" |
411 | "the global names in a program, you should @strong{not} include this\n" | |
412 | "underscore in @var{function}. Guile knows whether the underscore is\n" | |
872e0c72 | 413 | "needed or not and will add it when necessary.") |
1bbd0b84 | 414 | #define FUNC_NAME s_scm_dynamic_func |
80bc7890 | 415 | { |
a6d9e5ab DH |
416 | /* The returned handle is formed by casting the address of the function to a |
417 | * long value and converting this to a scheme number | |
418 | */ | |
419 | ||
7cf1a27e | 420 | void (*func) (); |
80bc7890 | 421 | |
a6d9e5ab | 422 | SCM_VALIDATE_STRING (1, name); |
7cf1a27e | 423 | /*fixme* GC-problem */ |
b82c6ce0 DH |
424 | SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj); |
425 | if (DYNL_HANDLE (dobj) == NULL) { | |
426 | SCM_MISC_ERROR ("Already unlinked: ~S", dobj); | |
427 | } else { | |
a002f1a2 DH |
428 | char *chars; |
429 | ||
b82c6ce0 | 430 | SCM_DEFER_INTS; |
a6d9e5ab DH |
431 | SCM_STRING_COERCE_0TERMINATION_X (name); |
432 | chars = SCM_STRING_CHARS (name); | |
a002f1a2 | 433 | func = (void (*) ()) sysdep_dynl_func (chars, DYNL_HANDLE (dobj), FUNC_NAME); |
b82c6ce0 DH |
434 | SCM_ALLOW_INTS; |
435 | return scm_ulong2num ((unsigned long) func); | |
436 | } | |
80bc7890 | 437 | } |
1bbd0b84 | 438 | #undef FUNC_NAME |
80bc7890 | 439 | |
b82c6ce0 | 440 | |
a1ec6916 | 441 | SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0, |
1bbd0b84 | 442 | (SCM func, SCM dobj), |
1e6808ea MG |
443 | "Call the C function indicated by @var{func} and @var{dobj}.\n" |
444 | "The function is passed no arguments and its return value is\n" | |
445 | "ignored. When @var{function} is something returned by\n" | |
446 | "@code{dynamic-func}, call that function and ignore @var{dobj}.\n" | |
447 | "When @var{func} is a string , look it up in @var{dynobj}; this\n" | |
448 | "is equivalent to\n" | |
b380b885 | 449 | "@smallexample\n" |
1e6808ea | 450 | "(dynamic-call (dynamic-func @var{func} @var{dobj} #f))\n" |
b380b885 MD |
451 | "@end smallexample\n\n" |
452 | "Interrupts are deferred while the C function is executing (with\n" | |
872e0c72 | 453 | "@code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).") |
1bbd0b84 | 454 | #define FUNC_NAME s_scm_dynamic_call |
80bc7890 | 455 | { |
7cf1a27e MD |
456 | void (*fptr) (); |
457 | ||
a6d9e5ab | 458 | if (SCM_STRINGP (func)) |
7cf1a27e MD |
459 | func = scm_dynamic_func (func, dobj); |
460 | fptr = (void (*) ()) SCM_NUM2ULONG (1, func); | |
461 | SCM_DEFER_INTS; | |
462 | fptr (); | |
463 | SCM_ALLOW_INTS; | |
464 | return SCM_UNSPECIFIED; | |
80bc7890 | 465 | } |
1bbd0b84 | 466 | #undef FUNC_NAME |
80bc7890 | 467 | |
a1ec6916 | 468 | SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0, |
1bbd0b84 | 469 | (SCM func, SCM dobj, SCM args), |
1e6808ea MG |
470 | "Call the C function indicated by @var{func} and @var{dobj},\n" |
471 | "just like @code{dynamic-call}, but pass it some arguments and\n" | |
472 | "return its return value. The C function is expected to take\n" | |
473 | "two arguments and return an @code{int}, just like @code{main}:\n" | |
b380b885 MD |
474 | "@smallexample\n" |
475 | "int c_func (int argc, char **argv);\n" | |
476 | "@end smallexample\n\n" | |
1e6808ea MG |
477 | "The parameter @var{args} must be a list of strings and is\n" |
478 | "converted into an array of @code{char *}. The array is passed\n" | |
479 | "in @var{argv} and its size in @var{argc}. The return value is\n" | |
480 | "converted to a Scheme number and returned from the call to\n" | |
481 | "@code{dynamic-args-call}.") | |
1bbd0b84 | 482 | #define FUNC_NAME s_scm_dynamic_args_call |
80bc7890 | 483 | { |
7cf1a27e MD |
484 | int (*fptr) (int argc, char **argv); |
485 | int result, argc; | |
486 | char **argv; | |
487 | ||
a6d9e5ab | 488 | if (SCM_STRINGP (func)) |
7cf1a27e MD |
489 | func = scm_dynamic_func (func, dobj); |
490 | ||
491 | fptr = (int (*) (int, char **)) SCM_NUM2ULONG (1, func); | |
492 | SCM_DEFER_INTS; | |
493 | argv = scm_make_argv_from_stringlist (args, &argc, FUNC_NAME, SCM_ARG3); | |
494 | result = (*fptr) (argc, argv); | |
495 | scm_must_free_argv (argv); | |
496 | SCM_ALLOW_INTS; | |
497 | ||
498 | return SCM_MAKINUM (0L + result); | |
80bc7890 | 499 | } |
1bbd0b84 | 500 | #undef FUNC_NAME |
80bc7890 | 501 | |
1edae076 MV |
502 | void |
503 | scm_init_dynamic_linking () | |
504 | { | |
7cf1a27e | 505 | scm_tc16_dynamic_obj = scm_make_smob_type ("dynamic-object", 0); |
e841c3e0 KN |
506 | scm_set_smob_mark (scm_tc16_dynamic_obj, dynl_obj_mark); |
507 | scm_set_smob_print (scm_tc16_dynamic_obj, dynl_obj_print); | |
7cf1a27e | 508 | sysdep_dynl_init (); |
8dc9439f | 509 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 510 | #include "libguile/dynl.x" |
8dc9439f | 511 | #endif |
1edae076 | 512 | } |
89e00824 ML |
513 | |
514 | /* | |
515 | Local Variables: | |
516 | c-file-style: "gnu" | |
517 | End: | |
518 | */ |