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