Reify bytevector? in the correct module
[bpt/guile.git] / libguile / extensions.c
CommitLineData
e2ab7927
MV
1/* extensions.c - registering and loading extensions.
2 *
08969a24 3 * Copyright (C) 2001, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
e2ab7927 4 *
73be1d9e 5 * This library is free software; you can redistribute it and/or
53befeb7
NJ
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 3 of
8 * the License, or (at your option) any later version.
e2ab7927 9 *
53befeb7
NJ
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
e2ab7927 14 *
73be1d9e
MV
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
53befeb7
NJ
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301 USA
73be1d9e 19 */
e2ab7927 20
dbb605f5 21#ifdef HAVE_CONFIG_H
c0fc1089
RB
22# include <config.h>
23#endif
24
fbbdb121
GH
25#include <string.h>
26
e2ab7927
MV
27#include "libguile/_scm.h"
28#include "libguile/strings.h"
29#include "libguile/gc.h"
30#include "libguile/dynl.h"
4695c759 31#include "libguile/dynwind.h"
e2ab7927
MV
32
33#include "libguile/extensions.h"
34
1be6b49c
ML
35typedef struct extension_t
36{
37 struct extension_t *next;
e2ab7927
MV
38 const char *lib;
39 const char *init;
40 void (*func)(void *);
41 void *data;
1be6b49c 42} extension_t;
e2ab7927 43
f45eccff 44static extension_t *registered_extensions = NULL;
877514da 45static scm_i_pthread_mutex_t ext_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
e2ab7927 46
bef38a17
MV
47/* Register a LIB/INIT pair for use by `scm_load_extension'. LIB is
48 allowed to be NULL and then only INIT is used to identify the
49 registered entry. This is useful when you don't know the library
50 name (which isn't really relevant anyway in a completely linked
51 program) and you are sure that INIT is unique (which it must be for
52 static linking). Hmm, given this reasoning, what use is LIB
53 anyway?
54*/
55
e2ab7927
MV
56void
57scm_c_register_extension (const char *lib, const char *init,
58 void (*func) (void *), void *data)
59{
4c9419ac 60 extension_t *ext = scm_malloc (sizeof(extension_t));
bef38a17 61 if (lib)
4c9419ac 62 ext->lib = scm_strdup (lib);
bef38a17
MV
63 else
64 ext->lib = NULL;
4c9419ac 65 ext->init = scm_strdup (init);
e2ab7927
MV
66 ext->func = func;
67 ext->data = data;
68
877514da 69 scm_i_pthread_mutex_lock (&ext_lock);
e2ab7927
MV
70 ext->next = registered_extensions;
71 registered_extensions = ext;
877514da 72 scm_i_pthread_mutex_unlock (&ext_lock);
e2ab7927
MV
73}
74
75static void
76load_extension (SCM lib, SCM init)
77{
877514da
AW
78 extension_t *head;
79
80 scm_i_pthread_mutex_lock (&ext_lock);
81 head = registered_extensions;
82 scm_i_pthread_mutex_unlock (&ext_lock);
83
e2ab7927 84 /* Search the registry. */
877514da 85 if (head != NULL)
4695c759
MV
86 {
87 extension_t *ext;
88 char *clib, *cinit;
a5fc6570 89 int found = 0;
4695c759 90
661ae7ab 91 scm_dynwind_begin (0);
4695c759
MV
92
93 clib = scm_to_locale_string (lib);
661ae7ab 94 scm_dynwind_free (clib);
4695c759 95 cinit = scm_to_locale_string (init);
661ae7ab 96 scm_dynwind_free (cinit);
4695c759 97
877514da 98 for (ext = head; ext; ext = ext->next)
4695c759
MV
99 if ((ext->lib == NULL || !strcmp (ext->lib, clib))
100 && !strcmp (ext->init, cinit))
101 {
102 ext->func (ext->data);
a5fc6570 103 found = 1;
4695c759
MV
104 break;
105 }
106
661ae7ab 107 scm_dynwind_end ();
a5fc6570
AW
108
109 if (found)
110 return;
4695c759 111 }
e2ab7927
MV
112
113 /* Dynamically link the library. */
08969a24 114#if HAVE_MODULES
e2ab7927 115 scm_dynamic_call (init, scm_dynamic_link (lib));
08969a24
AW
116#else
117 scm_misc_error ("load-extension",
118 "extension ~S:~S not registered and dynamic-link disabled",
119 scm_list_2 (init, lib));
120#endif
e2ab7927
MV
121}
122
123void
124scm_c_load_extension (const char *lib, const char *init)
125{
4695c759 126 load_extension (scm_from_locale_string (lib), scm_from_locale_string (init));
e2ab7927
MV
127}
128
129SCM_DEFINE (scm_load_extension, "load-extension", 2, 0, 0,
130 (SCM lib, SCM init),
72dd0a03 131 "Load and initialize the extension designated by LIB and INIT.\n"
9401323e
NJ
132 "When there is no pre-registered function for LIB/INIT, this is\n"
133 "equivalent to\n"
134 "\n"
135 "@lisp\n"
136 "(dynamic-call INIT (dynamic-link LIB))\n"
137 "@end lisp\n"
138 "\n"
139 "When there is a pre-registered function, that function is called\n"
140 "instead.\n"
141 "\n"
142 "Normally, there is no pre-registered function. This option exists\n"
143 "only for situations where dynamic linking is unavailable or unwanted.\n"
144 "In that case, you would statically link your program with the desired\n"
145 "library, and register its init function right after Guile has been\n"
146 "initialized.\n"
147 "\n"
148 "LIB should be a string denoting a shared library without any file type\n"
149 "suffix such as \".so\". The suffix is provided automatically. It\n"
150 "should also not contain any directory components. Libraries that\n"
151 "implement Guile Extensions should be put into the normal locations for\n"
152 "shared libraries. We recommend to use the naming convention\n"
153 "libguile-bla-blum for a extension related to a module `(bla blum)'.\n"
154 "\n"
155 "The normal way for a extension to be used is to write a small Scheme\n"
156 "file that defines a module, and to load the extension into this\n"
157 "module. When the module is auto-loaded, the extension is loaded as\n"
158 "well. For example,\n"
159 "\n"
160 "@lisp\n"
161 "(define-module (bla blum))\n"
162 "\n"
163 "(load-extension \"libguile-bla-blum\" \"bla_init_blum\")\n"
164 "@end lisp")
e2ab7927
MV
165#define FUNC_NAME s_scm_load_extension
166{
e2ab7927
MV
167 load_extension (lib, init);
168 return SCM_UNSPECIFIED;
169}
170#undef FUNC_NAME
171
172void
173scm_init_extensions ()
174{
e2ab7927 175#include "libguile/extensions.x"
e2ab7927
MV
176}
177
178/*
179 Local Variables:
180 c-file-style: "gnu"
181 End:
182*/