extensions.c 5.3 KB

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