loadlib.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. ** $Id: loadlib.c,v 1.3 2003/04/02 13:09:14 roberto Exp roberto $
  3. ** Dynamic library loader for Lua
  4. ** See Copyright Notice in lua.h
  5. *
  6. * This Lua library exports a single function, called loadlib, which is
  7. * called from Lua as loadlib(lib,init), where lib is the full name of the
  8. * library to be loaded (including the complete path) and init is the name
  9. * of a function to be called after the library is loaded. Typically, this
  10. * function will register other functions, thus making the complete library
  11. * available to Lua. The init function is *not* automatically called by
  12. * loadlib. Instead, loadlib returns the init function as a Lua function
  13. * that the client can call when it thinks is appropriate. In the case of
  14. * errors, loadlib returns nil and two strings describing the error.
  15. * The first string is supplied by the operating system; it should be
  16. * informative and useful for error messages. The second string is "open",
  17. * "init", or "absent" to identify the error and is meant to be used for
  18. * making decisions without having to look into the first string (whose
  19. * format is system-dependent).
  20. *
  21. * This module contains an implementation of loadlib for Unix systems that
  22. * have dlfcn, an implementation for Windows, and a stub for other systems.
  23. * See the list at the end of this file for some links to available
  24. * implementations of dlfcn and interfaces to other native dynamic loaders
  25. * on top of which loadlib could be implemented.
  26. *
  27. */
  28. #include "lua.h"
  29. #include "lauxlib.h"
  30. #include "lualib.h"
  31. #undef LOADLIB
  32. #ifdef USE_DLOPEN
  33. #define LOADLIB
  34. /*
  35. * This is an implementation of loadlib based on the dlfcn interface.
  36. * The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
  37. * NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
  38. * as an emulation layer on top of native functions.
  39. */
  40. #include <dlfcn.h>
  41. static int loadlib(lua_State *L)
  42. {
  43. const char *path=luaL_checkstring(L,1);
  44. const char *init=luaL_checkstring(L,2);
  45. void *lib=dlopen(path,RTLD_NOW);
  46. if (lib!=NULL)
  47. {
  48. lua_CFunction f=(lua_CFunction) dlsym(lib,init);
  49. if (f!=NULL)
  50. {
  51. lua_pushlightuserdata(L,lib);
  52. lua_pushcclosure(L,f,1);
  53. return 1;
  54. }
  55. }
  56. /* else return appropriate error messages */
  57. lua_pushnil(L);
  58. lua_pushstring(L,dlerror());
  59. lua_pushstring(L,(lib!=NULL) ? "init" : "open");
  60. if (lib!=NULL) dlclose(lib);
  61. return 3;
  62. }
  63. #endif
  64. /*
  65. ** In Windows, default is to use dll; otherwise, default is not to use dll
  66. */
  67. #ifndef USE_DLL
  68. #ifdef _WIN32
  69. #define USE_DLL 1
  70. #else
  71. #define USE_DLL 0
  72. #endif
  73. #endif
  74. #if USE_DLL
  75. #define LOADLIB
  76. /*
  77. * This is an implementation of loadlib for Windows using native functions.
  78. */
  79. #include <windows.h>
  80. static void pusherror(lua_State *L)
  81. {
  82. int error=GetLastError();
  83. char buffer[128];
  84. if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  85. 0, error, 0, buffer, sizeof(buffer), 0))
  86. lua_pushstring(L,buffer);
  87. else
  88. lua_pushfstring(L,"system error %d\n",error);
  89. }
  90. static int loadlib(lua_State *L)
  91. {
  92. const char *path=luaL_checkstring(L,1);
  93. const char *init=luaL_checkstring(L,2);
  94. HINSTANCE lib=LoadLibrary(path);
  95. if (lib!=NULL)
  96. {
  97. lua_CFunction f=(lua_CFunction) GetProcAddress(lib,init);
  98. if (f!=NULL)
  99. {
  100. lua_pushlightuserdata(L,lib);
  101. lua_pushcclosure(L,f,1);
  102. return 1;
  103. }
  104. }
  105. lua_pushnil(L);
  106. pusherror(L);
  107. lua_pushstring(L,(lib!=NULL) ? "init" : "open");
  108. if (lib!=NULL) FreeLibrary(lib);
  109. return 3;
  110. }
  111. #endif
  112. #ifndef LOADLIB
  113. /* Fallback for other systems */
  114. /*
  115. ** Those systems support dlopen, so they should have defined USE_DLOPEN.
  116. ** The default (no)implementation gives them a special error message.
  117. */
  118. #ifdef linux
  119. #define LOADLIB
  120. #endif
  121. #ifdef sun
  122. #define LOADLIB
  123. #endif
  124. #ifdef sgi
  125. #define LOADLIB
  126. #endif
  127. #ifdef BSD
  128. #define LOADLIB
  129. #endif
  130. #ifdef _WIN32
  131. #define LOADLIB
  132. #endif
  133. #ifdef LOADLIB
  134. #undef LOADLIB
  135. #define LOADLIB "`loadlib' not installed (check your Lua configuration)"
  136. #else
  137. #define LOADLIB "`loadlib' not supported"
  138. #endif
  139. static int loadlib(lua_State *L)
  140. {
  141. lua_pushnil(L);
  142. lua_pushliteral(L,LOADLIB);
  143. lua_pushliteral(L,"absent");
  144. return 3;
  145. }
  146. #endif
  147. LUALIB_API int luaopen_loadlib (lua_State *L)
  148. {
  149. lua_register(L,"loadlib",loadlib);
  150. return 0;
  151. }
  152. /*
  153. * Here are some links to available implementations of dlfcn and
  154. * interfaces to other native dynamic loaders on top of which loadlib
  155. * could be implemented. Please send contributions and corrections to us.
  156. *
  157. * AIX
  158. * Starting with AIX 4.2, dlfcn is included in the base OS.
  159. * There is also an emulation package available.
  160. * http://www.faqs.org/faqs/aix-faq/part4/section-21.html
  161. *
  162. * HPUX
  163. * HPUX 11 has dlfcn. For HPUX 10 use shl_*.
  164. * http://www.geda.seul.org/mailinglist/geda-dev37/msg00094.html
  165. * http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html
  166. *
  167. * Macintosh, Windows
  168. * http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html
  169. *
  170. * Mac OS X/Darwin
  171. * http://www.opendarwin.org/projects/dlcompat/
  172. *
  173. * GLIB has wrapper code for BeOS, OS2, Unix and Windows
  174. * http://cvs.gnome.org/lxr/source/glib/gmodule/
  175. *
  176. */