extensions.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* extensions.c - registering and loading extensions.
  2. *
  3. * Copyright (C) 2001, 2006 Free Software Foundation, Inc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  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.
  9. *
  10. * This library is distributed in the hope that it will be useful, but
  11. * 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.
  14. *
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include <string.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/strings.h"
  26. #include "libguile/gc.h"
  27. #include "libguile/dynl.h"
  28. #include "libguile/dynwind.h"
  29. #include "libguile/extensions.h"
  30. typedef struct extension_t
  31. {
  32. struct extension_t *next;
  33. const char *lib;
  34. const char *init;
  35. void (*func)(void *);
  36. void *data;
  37. } extension_t;
  38. static extension_t *registered_extensions;
  39. /* Register a LIB/INIT pair for use by `scm_load_extension'. LIB is
  40. allowed to be NULL and then only INIT is used to identify the
  41. registered entry. This is useful when you don't know the library
  42. name (which isn't really relevant anyway in a completely linked
  43. program) and you are sure that INIT is unique (which it must be for
  44. static linking). Hmm, given this reasoning, what use is LIB
  45. anyway?
  46. */
  47. void
  48. scm_c_register_extension (const char *lib, const char *init,
  49. void (*func) (void *), void *data)
  50. {
  51. extension_t *ext = scm_malloc (sizeof(extension_t));
  52. if (lib)
  53. ext->lib = scm_strdup (lib);
  54. else
  55. ext->lib = NULL;
  56. ext->init = scm_strdup (init);
  57. ext->func = func;
  58. ext->data = data;
  59. ext->next = registered_extensions;
  60. registered_extensions = ext;
  61. }
  62. static void
  63. load_extension (SCM lib, SCM init)
  64. {
  65. /* Search the registry. */
  66. if (registered_extensions != NULL)
  67. {
  68. extension_t *ext;
  69. char *clib, *cinit;
  70. int found = 0;
  71. scm_dynwind_begin (0);
  72. clib = scm_to_locale_string (lib);
  73. scm_dynwind_free (clib);
  74. cinit = scm_to_locale_string (init);
  75. scm_dynwind_free (cinit);
  76. for (ext = registered_extensions; ext; ext = ext->next)
  77. if ((ext->lib == NULL || !strcmp (ext->lib, clib))
  78. && !strcmp (ext->init, cinit))
  79. {
  80. ext->func (ext->data);
  81. found = 1;
  82. break;
  83. }
  84. scm_dynwind_end ();
  85. if (found)
  86. return;
  87. }
  88. /* Dynamically link the library. */
  89. scm_dynamic_call (init, scm_dynamic_link (lib));
  90. }
  91. void
  92. scm_c_load_extension (const char *lib, const char *init)
  93. {
  94. load_extension (scm_from_locale_string (lib), scm_from_locale_string (init));
  95. }
  96. SCM_DEFINE (scm_load_extension, "load-extension", 2, 0, 0,
  97. (SCM lib, SCM init),
  98. "Load and initialize the extension designated by LIB and INIT.\n"
  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")
  132. #define FUNC_NAME s_scm_load_extension
  133. {
  134. load_extension (lib, init);
  135. return SCM_UNSPECIFIED;
  136. }
  137. #undef FUNC_NAME
  138. void
  139. scm_init_extensions ()
  140. {
  141. registered_extensions = NULL;
  142. #include "libguile/extensions.x"
  143. }
  144. /*
  145. Local Variables:
  146. c-file-style: "gnu"
  147. End:
  148. */