extensions.c 5.7 KB

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