loadlib.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. ** $Id: loadlib.c,v 1.5 2003/05/14 21:01:53 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. #define loadlib_c
  29. #define LUA_LIB
  30. #include "lua.h"
  31. #include "lauxlib.h"
  32. #include "lualib.h"
  33. #undef LOADLIB
  34. #ifdef USE_DLOPEN
  35. #define LOADLIB
  36. /*
  37. * This is an implementation of loadlib based on the dlfcn interface.
  38. * The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
  39. * NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
  40. * as an emulation layer on top of native functions.
  41. */
  42. #include <dlfcn.h>
  43. static int loadlib(lua_State *L)
  44. {
  45. const char *path=luaL_checkstring(L,1);
  46. const char *init=luaL_checkstring(L,2);
  47. void *lib=dlopen(path,RTLD_NOW);
  48. if (lib!=NULL)
  49. {
  50. lua_CFunction f=(lua_CFunction) dlsym(lib,init);
  51. if (f!=NULL)
  52. {
  53. lua_pushlightuserdata(L,lib);
  54. lua_pushcclosure(L,f,1);
  55. return 1;
  56. }
  57. }
  58. /* else return appropriate error messages */
  59. lua_pushnil(L);
  60. lua_pushstring(L,dlerror());
  61. lua_pushstring(L,(lib!=NULL) ? "init" : "open");
  62. if (lib!=NULL) dlclose(lib);
  63. return 3;
  64. }
  65. #endif
  66. /*
  67. ** In Windows, default is to use dll; otherwise, default is not to use dll
  68. */
  69. #ifndef USE_DLL
  70. #ifdef _WIN32
  71. #define USE_DLL 1
  72. #else
  73. #define USE_DLL 0
  74. #endif
  75. #endif
  76. #if USE_DLL
  77. #define LOADLIB
  78. /*
  79. * This is an implementation of loadlib for Windows using native functions.
  80. */
  81. #include <windows.h>
  82. static void pusherror(lua_State *L)
  83. {
  84. int error=GetLastError();
  85. char buffer[128];
  86. if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  87. 0, error, 0, buffer, sizeof(buffer), 0))
  88. lua_pushstring(L,buffer);
  89. else
  90. lua_pushfstring(L,"system error %d\n",error);
  91. }
  92. static int loadlib(lua_State *L)
  93. {
  94. const char *path=luaL_checkstring(L,1);
  95. const char *init=luaL_checkstring(L,2);
  96. HINSTANCE lib=LoadLibrary(path);
  97. if (lib!=NULL)
  98. {
  99. lua_CFunction f=(lua_CFunction) GetProcAddress(lib,init);
  100. if (f!=NULL)
  101. {
  102. lua_pushlightuserdata(L,lib);
  103. lua_pushcclosure(L,f,1);
  104. return 1;
  105. }
  106. }
  107. lua_pushnil(L);
  108. pusherror(L);
  109. lua_pushstring(L,(lib!=NULL) ? "init" : "open");
  110. if (lib!=NULL) FreeLibrary(lib);
  111. return 3;
  112. }
  113. #endif
  114. #ifndef LOADLIB
  115. /* Fallback for other systems */
  116. /*
  117. ** Those systems support dlopen, so they should have defined USE_DLOPEN.
  118. ** The default (no)implementation gives them a special error message.
  119. */
  120. #if defined(linux) || defined(sun) || defined(sgi) || defined(BSD) || defined(_WIN32)
  121. #define LOADLIB "`loadlib' not installed (check your Lua configuration)"
  122. #else
  123. #define LOADLIB "`loadlib' not supported"
  124. #endif
  125. static int loadlib(lua_State *L)
  126. {
  127. lua_pushnil(L);
  128. lua_pushliteral(L,LOADLIB);
  129. lua_pushliteral(L,"absent");
  130. return 3;
  131. }
  132. #endif
  133. LUALIB_API int luaopen_loadlib (lua_State *L)
  134. {
  135. lua_register(L,"loadlib",loadlib);
  136. return 0;
  137. }
  138. /*
  139. * Here are some links to available implementations of dlfcn and
  140. * interfaces to other native dynamic loaders on top of which loadlib
  141. * could be implemented. Please send contributions and corrections to us.
  142. *
  143. * AIX
  144. * Starting with AIX 4.2, dlfcn is included in the base OS.
  145. * There is also an emulation package available.
  146. * http://www.faqs.org/faqs/aix-faq/part4/section-21.html
  147. *
  148. * HPUX
  149. * HPUX 11 has dlfcn. For HPUX 10 use shl_*.
  150. * http://www.geda.seul.org/mailinglist/geda-dev37/msg00094.html
  151. * http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html
  152. *
  153. * Macintosh, Windows
  154. * http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html
  155. *
  156. * Mac OS X/Darwin
  157. * http://www.opendarwin.org/projects/dlcompat/
  158. *
  159. * GLIB has wrapper code for BeOS, OS2, Unix and Windows
  160. * http://cvs.gnome.org/lxr/source/glib/gmodule/
  161. *
  162. */