1 /* extensions.c - registering and loading extensions.
3 * Copyright (C) 2001, 2006 Free Software Foundation, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libguile/_scm.h"
27 #include "libguile/strings.h"
28 #include "libguile/gc.h"
29 #include "libguile/dynl.h"
30 #include "libguile/dynwind.h"
32 #include "libguile/extensions.h"
34 typedef struct extension_t
36 struct extension_t *next;
43 static extension_t *registered_extensions;
45 /* Register a LIB/INIT pair for use by `scm_load_extension'. LIB is
46 allowed to be NULL and then only INIT is used to identify the
47 registered entry. This is useful when you don't know the library
48 name (which isn't really relevant anyway in a completely linked
49 program) and you are sure that INIT is unique (which it must be for
50 static linking). Hmm, given this reasoning, what use is LIB
55 scm_c_register_extension (const char *lib, const char *init,
56 void (*func) (void *), void *data)
58 extension_t *ext = scm_malloc (sizeof(extension_t));
60 ext->lib = scm_strdup (lib);
63 ext->init = scm_strdup (init);
67 ext->next = registered_extensions;
68 registered_extensions = ext;
72 load_extension (SCM lib, SCM init)
74 /* Search the registry. */
75 if (registered_extensions != NULL)
81 scm_dynwind_begin (0);
83 clib = scm_to_locale_string (lib);
84 scm_dynwind_free (clib);
85 cinit = scm_to_locale_string (init);
86 scm_dynwind_free (cinit);
88 for (ext = registered_extensions; ext; ext = ext->next)
89 if ((ext->lib == NULL || !strcmp (ext->lib, clib))
90 && !strcmp (ext->init, cinit))
92 ext->func (ext->data);
103 /* Dynamically link the library. */
104 scm_dynamic_call (init, scm_dynamic_link (lib));
108 scm_c_load_extension (const char *lib, const char *init)
110 load_extension (scm_from_locale_string (lib), scm_from_locale_string (init));
113 SCM_DEFINE (scm_load_extension, "load-extension", 2, 0, 0,
115 "Load and initialize the extension designated by LIB and INIT.\n"
116 "When there is no pre-registered function for LIB/INIT, this is\n"
120 "(dynamic-call INIT (dynamic-link LIB))\n"
123 "When there is a pre-registered function, that function is called\n"
126 "Normally, there is no pre-registered function. This option exists\n"
127 "only for situations where dynamic linking is unavailable or unwanted.\n"
128 "In that case, you would statically link your program with the desired\n"
129 "library, and register its init function right after Guile has been\n"
132 "LIB should be a string denoting a shared library without any file type\n"
133 "suffix such as \".so\". The suffix is provided automatically. It\n"
134 "should also not contain any directory components. Libraries that\n"
135 "implement Guile Extensions should be put into the normal locations for\n"
136 "shared libraries. We recommend to use the naming convention\n"
137 "libguile-bla-blum for a extension related to a module `(bla blum)'.\n"
139 "The normal way for a extension to be used is to write a small Scheme\n"
140 "file that defines a module, and to load the extension into this\n"
141 "module. When the module is auto-loaded, the extension is loaded as\n"
142 "well. For example,\n"
145 "(define-module (bla blum))\n"
147 "(load-extension \"libguile-bla-blum\" \"bla_init_blum\")\n"
149 #define FUNC_NAME s_scm_load_extension
151 load_extension (lib, init);
152 return SCM_UNSPECIFIED;
157 scm_init_extensions ()
159 registered_extensions = NULL;
160 #include "libguile/extensions.x"