loadlib.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. ** $Id: loadlib.c,v 1.1 2003/03/17 13:01:48 roberto Exp roberto $
  3. ** Bare-bones 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 bare-bones loadlib function is supposed to be used as a foundation
  22. * for more sophisticated dynamic library loaders, possibly still called
  23. * loadlib and probably written in Lua, that will be smart enough to try to
  24. * find the library in different directories and also perhaps guess the name
  25. * of the init function.
  26. *
  27. * This module contains an implementation of loadlib for Unix systems that
  28. * have dlfcn, an implementation for Windows, and a stub for other systems.
  29. * See the list at the end of this file for some links to available
  30. * implementations of dlfcn and interfaces to other native dynamic loaders
  31. * on top of which loadlib could be implemented.
  32. *
  33. */
  34. #include "lua.h"
  35. #include "lauxlib.h"
  36. #include "lualib.h"
  37. #ifndef USE_LOADLIB
  38. #define USE_LOADLIB 1
  39. #endif
  40. #ifndef USE_DLOPEN
  41. #define USE_DLOPEN 0
  42. #endif
  43. #if USE_LOADLIB
  44. #if defined(linux) || defined(sun) || defined(sgi) || defined(BSD) || USE_DLOPEN
  45. #define LOADLIB
  46. /*
  47. * This is an implementation of loadlib based on the dlfcn interface.
  48. * The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
  49. * NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
  50. * as an emulation layer on top of native functions.
  51. */
  52. #include <dlfcn.h>
  53. static int loadlib(lua_State *L)
  54. {
  55. const char *path=luaL_checkstring(L,1);
  56. const char *init=luaL_checkstring(L,2);
  57. void *lib=dlopen(path,RTLD_NOW);
  58. if (lib!=NULL)
  59. {
  60. lua_CFunction f=(lua_CFunction) dlsym(lib,init);
  61. if (f!=NULL)
  62. {
  63. lua_pushlightuserdata(L,lib);
  64. lua_pushcclosure(L,f,1);
  65. return 1;
  66. }
  67. }
  68. lua_pushnil(L);
  69. lua_pushstring(L,dlerror());
  70. lua_pushstring(L,(lib!=NULL) ? "init" : "open");
  71. if (lib!=NULL) dlclose(lib);
  72. return 3;
  73. }
  74. #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
  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. #else
  112. /*
  113. * write an implementation for your system here and send it to us, together
  114. * with preprocessing symbols that identify your system.
  115. */
  116. #endif
  117. #endif
  118. #ifndef LOADLIB
  119. /* Fallback for other systems */
  120. static int loadlib(lua_State *L)
  121. {
  122. lua_pushnil(L);
  123. lua_pushliteral(L,"`loadlib' not supported");
  124. lua_pushliteral(L,"absent");
  125. return 3;
  126. }
  127. #endif
  128. LUALIB_API int luaopen_loadlib (lua_State *L)
  129. {
  130. lua_register(L,"loadlib",loadlib);
  131. return 0;
  132. }
  133. /*
  134. * Here are some links por to available implementations of dlfcn and
  135. * interfaces to other native dynamic loaders on top of which loadlib could
  136. * be implemented. Please send contributions and corrections to us.
  137. *
  138. * AIX
  139. * Starting with AIX 4.2, dlfcn is included in the base OS.
  140. * There is also an emulation package available.
  141. * http://www.faqs.org/faqs/aix-faq/part4/section-21.html
  142. *
  143. * HPUX
  144. * HPUX 11 has dlfcn. For HPUX 10 use shl_*.
  145. * http://www.geda.seul.org/mailinglist/geda-dev37/msg00094.html
  146. * http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html
  147. *
  148. * Macintosh, Windows
  149. * http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html
  150. *
  151. * Mac OS X/Darwin
  152. * http://www.opendarwin.org/projects/dlcompat/
  153. *
  154. * GLIB has wrapper code for BeOS, OS2, Unix and Windows
  155. * http://cvs.gnome.org/lxr/source/glib/gmodule/
  156. *
  157. */