Commit | Line | Data |
---|---|---|
e2ab7927 MV |
1 | /* extensions.c - registering and loading extensions. |
2 | * | |
3 | * Copyright (C) 2001 Free Software Foundation, Inc. | |
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 | |
17 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
18 | * Boston, MA 02111-1307 USA | |
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. | |
42 | * If you do not wish that, delete this exception notice. */ | |
43 | ||
fbbdb121 GH |
44 | #include <string.h> |
45 | ||
e2ab7927 MV |
46 | #include "libguile/_scm.h" |
47 | #include "libguile/strings.h" | |
48 | #include "libguile/gc.h" | |
49 | #include "libguile/dynl.h" | |
50 | ||
51 | #include "libguile/extensions.h" | |
52 | ||
1be6b49c ML |
53 | typedef struct extension_t |
54 | { | |
55 | struct extension_t *next; | |
e2ab7927 MV |
56 | const char *lib; |
57 | const char *init; | |
58 | void (*func)(void *); | |
59 | void *data; | |
1be6b49c | 60 | } extension_t; |
e2ab7927 | 61 | |
1be6b49c | 62 | static extension_t *registered_extensions; |
e2ab7927 | 63 | |
bef38a17 MV |
64 | /* Register a LIB/INIT pair for use by `scm_load_extension'. LIB is |
65 | allowed to be NULL and then only INIT is used to identify the | |
66 | registered entry. This is useful when you don't know the library | |
67 | name (which isn't really relevant anyway in a completely linked | |
68 | program) and you are sure that INIT is unique (which it must be for | |
69 | static linking). Hmm, given this reasoning, what use is LIB | |
70 | anyway? | |
71 | */ | |
72 | ||
e2ab7927 MV |
73 | void |
74 | scm_c_register_extension (const char *lib, const char *init, | |
75 | void (*func) (void *), void *data) | |
76 | { | |
1be6b49c | 77 | extension_t *ext = scm_must_malloc (sizeof(extension_t), |
e2ab7927 | 78 | "scm_register_extension"); |
bef38a17 MV |
79 | if (lib) |
80 | ext->lib = scm_must_strdup (lib); | |
81 | else | |
82 | ext->lib = NULL; | |
e2ab7927 MV |
83 | ext->init = scm_must_strdup (init); |
84 | ext->func = func; | |
85 | ext->data = data; | |
86 | ||
87 | ext->next = registered_extensions; | |
88 | registered_extensions = ext; | |
89 | } | |
90 | ||
91 | static void | |
92 | load_extension (SCM lib, SCM init) | |
93 | { | |
94 | /* Search the registry. */ | |
95 | { | |
1be6b49c | 96 | extension_t *ext; |
e2ab7927 MV |
97 | |
98 | for (ext = registered_extensions; ext; ext = ext->next) | |
bef38a17 | 99 | if ((ext->lib == NULL || !strcmp (ext->lib, SCM_STRING_CHARS (lib))) |
e2ab7927 MV |
100 | && !strcmp (ext->init, SCM_STRING_CHARS (init))) |
101 | { | |
102 | ext->func (ext->data); | |
103 | return; | |
104 | } | |
105 | } | |
106 | ||
107 | /* Dynamically link the library. */ | |
108 | ||
109 | scm_dynamic_call (init, scm_dynamic_link (lib)); | |
110 | } | |
111 | ||
112 | void | |
113 | scm_c_load_extension (const char *lib, const char *init) | |
114 | { | |
115 | load_extension (scm_makfrom0str (lib), scm_makfrom0str (init)); | |
116 | } | |
117 | ||
118 | SCM_DEFINE (scm_load_extension, "load-extension", 2, 0, 0, | |
119 | (SCM lib, SCM init), | |
120 | "Load and initilize the extension designated by LIB and INIT." | |
121 | "When there is no pre-registered function for LIB/INIT, this is " | |
122 | "equivalent to " | |
123 | " " | |
124 | " (dynamic-call INIT (dynamic-link LIB)) " | |
125 | " " | |
126 | "When there is a pre-registered function, that function is called " | |
127 | "instead. " | |
128 | " " | |
129 | "Normally, there is no pre-registered function. This option exists " | |
130 | "only for situations where dynamic linking is unavailable or unwanted. " | |
131 | "In that case, you would statically link your program with the desired " | |
132 | "library, and register its init function right after Guile has been " | |
133 | "initialized. " | |
134 | " " | |
135 | "LIB should be a string denoting a shared library without any file type " | |
136 | "suffix such as \".so\". The suffix is provided automatically. It " | |
137 | "should also not contain any directory components. Libraries that " | |
138 | "implement Guile Extensions should be put into the normal locations for " | |
139 | "shared libraries. We recommend to use the naming convention " | |
140 | "libguile-bla-blum for a extension related to a module `(bla blum)'. " | |
141 | " " | |
142 | "The normal way for a extension to be used is to write a small Scheme " | |
143 | "file that defines a module, and to load the extension into this " | |
144 | "module. When the module is auto-loaded, the extension is loaded as " | |
145 | "well. For example, " | |
146 | " " | |
147 | " (define-module (bla blum)) " | |
148 | " " | |
149 | " (load-extension \"libguile-bla-blum\" \"bla_init_blum\")") | |
150 | #define FUNC_NAME s_scm_load_extension | |
151 | { | |
152 | SCM_VALIDATE_STRING (1, lib); | |
153 | SCM_VALIDATE_STRING (2, init); | |
154 | load_extension (lib, init); | |
155 | return SCM_UNSPECIFIED; | |
156 | } | |
157 | #undef FUNC_NAME | |
158 | ||
159 | void | |
160 | scm_init_extensions () | |
161 | { | |
162 | registered_extensions = NULL; | |
163 | #ifndef SCM_MAGIC_SNARFER | |
164 | #include "libguile/extensions.x" | |
165 | #endif | |
166 | } | |
167 | ||
168 | /* | |
169 | Local Variables: | |
170 | c-file-style: "gnu" | |
171 | End: | |
172 | */ |