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