extensions.c 4.8 KB

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