+++ /dev/null
-/*\r
-** $Id: loadlib.c,v 1.111.1.1 2013/04/12 18:48:47 roberto Exp $\r
-** Dynamic library loader for Lua\r
-** See Copyright Notice in lua.h\r
-**\r
-** This module contains an implementation of loadlib for Unix systems\r
-** that have dlfcn, an implementation for Windows, and a stub for other\r
-** systems.\r
-*/\r
-\r
-\r
-/*\r
-** if needed, includes windows header before everything else\r
-*/\r
-#if defined(_WIN32) && !defined(UEFI_C_SOURCE) && !defined(_DOS_WATCOM)\r
-#include <windows.h>\r
-#endif\r
-\r
-\r
-#include <stdlib.h>\r
-#include <string.h>\r
-\r
-\r
-#define loadlib_c\r
-#define LUA_LIB\r
-\r
-#include "lua.h"\r
-\r
-#include "lauxlib.h"\r
-#include "lualib.h"\r
-\r
-\r
-/*\r
-** LUA_PATH and LUA_CPATH are the names of the environment\r
-** variables that Lua check to set its paths.\r
-*/\r
-#if !defined(LUA_PATH)\r
-#define LUA_PATH "LUA_PATH"\r
-#endif\r
-\r
-#if !defined(LUA_CPATH)\r
-#define LUA_CPATH "LUA_CPATH"\r
-#endif\r
-\r
-#define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR\r
-\r
-#define LUA_PATHVERSION LUA_PATH LUA_PATHSUFFIX\r
-#define LUA_CPATHVERSION LUA_CPATH LUA_PATHSUFFIX\r
-\r
-/*\r
-** LUA_PATH_SEP is the character that separates templates in a path.\r
-** LUA_PATH_MARK is the string that marks the substitution points in a\r
-** template.\r
-** LUA_EXEC_DIR in a Windows path is replaced by the executable's\r
-** directory.\r
-** LUA_IGMARK is a mark to ignore all before it when building the\r
-** luaopen_ function name.\r
-*/\r
-#if !defined (LUA_PATH_SEP)\r
-#define LUA_PATH_SEP ";"\r
-#endif\r
-#if !defined (LUA_PATH_MARK)\r
-#define LUA_PATH_MARK "?"\r
-#endif\r
-#if !defined (LUA_EXEC_DIR)\r
-#define LUA_EXEC_DIR "!"\r
-#endif\r
-#if !defined (LUA_IGMARK)\r
-#define LUA_IGMARK "-"\r
-#endif\r
-\r
-\r
-/*\r
-** LUA_CSUBSEP is the character that replaces dots in submodule names\r
-** when searching for a C loader.\r
-** LUA_LSUBSEP is the character that replaces dots in submodule names\r
-** when searching for a Lua loader.\r
-*/\r
-#if !defined(LUA_CSUBSEP)\r
-#define LUA_CSUBSEP LUA_DIRSEP\r
-#endif\r
-\r
-#if !defined(LUA_LSUBSEP)\r
-#define LUA_LSUBSEP LUA_DIRSEP\r
-#endif\r
-\r
-\r
-/* prefix for open functions in C libraries */\r
-#define LUA_POF "luaopen_"\r
-\r
-/* separator for open functions in C libraries */\r
-#define LUA_OFSEP "_"\r
-\r
-\r
-/* table (in the registry) that keeps handles for all loaded C libraries */\r
-#define CLIBS "_CLIBS"\r
-\r
-#define LIB_FAIL "open"\r
-\r
-\r
-/* error codes for ll_loadfunc */\r
-#define ERRLIB 1\r
-#define ERRFUNC 2\r
-\r
-#define setprogdir(L) ((void)0)\r
-\r
-\r
-/*\r
-** system-dependent functions\r
-*/\r
-static void ll_unloadlib (void *lib);\r
-static void *ll_load (lua_State *L, const char *path, int seeglb);\r
-static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);\r
-\r
-\r
-\r
-#if defined(LUA_USE_DLOPEN)\r
-/*\r
-** {========================================================================\r
-** This is an implementation of loadlib based on the dlfcn interface.\r
-** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,\r
-** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least\r
-** as an emulation layer on top of native functions.\r
-** =========================================================================\r
-*/\r
-\r
-#include <dlfcn.h>\r
-\r
-static void ll_unloadlib (void *lib) {\r
- dlclose(lib);\r
-}\r
-\r
-\r
-static void *ll_load (lua_State *L, const char *path, int seeglb) {\r
- void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));\r
- if (lib == NULL) lua_pushstring(L, dlerror());\r
- return lib;\r
-}\r
-\r
-\r
-static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {\r
- lua_CFunction f = (lua_CFunction)dlsym(lib, sym);\r
- if (f == NULL) lua_pushstring(L, dlerror());\r
- return f;\r
-}\r
-\r
-/* }====================================================== */\r
-\r
-\r
-\r
-#elif defined(LUA_DL_DLL)\r
-/*\r
-** {======================================================================\r
-** This is an implementation of loadlib for Windows using native functions.\r
-** =======================================================================\r
-*/\r
-\r
-#undef setprogdir\r
-\r
-/*\r
-** optional flags for LoadLibraryEx\r
-*/\r
-#if !defined(LUA_LLE_FLAGS)\r
-#define LUA_LLE_FLAGS 0\r
-#endif\r
-\r
-\r
-static void setprogdir (lua_State *L) {\r
- char buff[MAX_PATH + 1];\r
- char *lb;\r
- DWORD nsize = sizeof(buff)/sizeof(char);\r
- DWORD n = GetModuleFileNameA(NULL, buff, nsize);\r
- if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)\r
- luaL_error(L, "unable to get ModuleFileName");\r
- else {\r
- *lb = '\0';\r
- luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff);\r
- lua_remove(L, -2); /* remove original string */\r
- }\r
-}\r
-\r
-\r
-static void pusherror (lua_State *L) {\r
- int error = GetLastError();\r
- char buffer[128];\r
- if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,\r
- NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL))\r
- lua_pushstring(L, buffer);\r
- else\r
- lua_pushfstring(L, "system error %d\n", error);\r
-}\r
-\r
-static void ll_unloadlib (void *lib) {\r
- FreeLibrary((HMODULE)lib);\r
-}\r
-\r
-\r
-static void *ll_load (lua_State *L, const char *path, int seeglb) {\r
- HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);\r
- (void)(seeglb); /* not used: symbols are 'global' by default */\r
- if (lib == NULL) pusherror(L);\r
- return lib;\r
-}\r
-\r
-\r
-static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {\r
- lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym);\r
- if (f == NULL) pusherror(L);\r
- return f;\r
-}\r
-\r
-/* }====================================================== */\r
-\r
-\r
-#else\r
-/*\r
-** {======================================================\r
-** Fallback for other systems\r
-** =======================================================\r
-*/\r
-\r
-#undef LIB_FAIL\r
-#define LIB_FAIL "absent"\r
-\r
-\r
-#define DLMSG "dynamic libraries not enabled; check your Lua installation"\r
-\r
-\r
-static void ll_unloadlib (void *lib) {\r
- (void)(lib); /* not used */\r
-}\r
-\r
-\r
-static void *ll_load (lua_State *L, const char *path, int seeglb) {\r
- (void)(path); (void)(seeglb); /* not used */\r
- lua_pushliteral(L, DLMSG);\r
- return NULL;\r
-}\r
-\r
-\r
-static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {\r
- (void)(lib); (void)(sym); /* not used */\r
- lua_pushliteral(L, DLMSG);\r
- return NULL;\r
-}\r
-\r
-/* }====================================================== */\r
-#endif\r
-\r
-\r
-static void *ll_checkclib (lua_State *L, const char *path) {\r
- void *plib;\r
- lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);\r
- lua_getfield(L, -1, path);\r
- plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */\r
- lua_pop(L, 2); /* pop CLIBS table and 'plib' */\r
- return plib;\r
-}\r
-\r
-\r
-static void ll_addtoclib (lua_State *L, const char *path, void *plib) {\r
- lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);\r
- lua_pushlightuserdata(L, plib);\r
- lua_pushvalue(L, -1);\r
- lua_setfield(L, -3, path); /* CLIBS[path] = plib */\r
- lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */\r
- lua_pop(L, 1); /* pop CLIBS table */\r
-}\r
-\r
-\r
-/*\r
-** __gc tag method for CLIBS table: calls 'll_unloadlib' for all lib\r
-** handles in list CLIBS\r
-*/\r
-static int gctm (lua_State *L) {\r
- int n = luaL_len(L, 1);\r
- for (; n >= 1; n--) { /* for each handle, in reverse order */\r
- lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */\r
- ll_unloadlib(lua_touserdata(L, -1));\r
- lua_pop(L, 1); /* pop handle */\r
- }\r
- return 0;\r
-}\r
-\r
-\r
-static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {\r
- void *reg = ll_checkclib(L, path); /* check loaded C libraries */\r
- if (reg == NULL) { /* must load library? */\r
- reg = ll_load(L, path, *sym == '*');\r
- if (reg == NULL) return ERRLIB; /* unable to load library */\r
- ll_addtoclib(L, path, reg);\r
- }\r
- if (*sym == '*') { /* loading only library (no function)? */\r
- lua_pushboolean(L, 1); /* return 'true' */\r
- return 0; /* no errors */\r
- }\r
- else {\r
- lua_CFunction f = ll_sym(L, reg, sym);\r
- if (f == NULL)\r
- return ERRFUNC; /* unable to find function */\r
- lua_pushcfunction(L, f); /* else create new function */\r
- return 0; /* no errors */\r
- }\r
-}\r
-\r
-\r
-static int ll_loadlib (lua_State *L) {\r
- const char *path = luaL_checkstring(L, 1);\r
- const char *init = luaL_checkstring(L, 2);\r
- int stat = ll_loadfunc(L, path, init);\r
- if (stat == 0) /* no errors? */\r
- return 1; /* return the loaded function */\r
- else { /* error; error message is on stack top */\r
- lua_pushnil(L);\r
- lua_insert(L, -2);\r
- lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");\r
- return 3; /* return nil, error message, and where */\r
- }\r
-}\r
-\r
-\r
-\r
-/*\r
-** {======================================================\r
-** 'require' function\r
-** =======================================================\r
-*/\r
-\r
-\r
-static int readable (const char *filename) {\r
- FILE *f = fopen(filename, "r"); /* try to open file */\r
- if (f == NULL) return 0; /* open failed */\r
- fclose(f);\r
- return 1;\r
-}\r
-\r
-\r
-static const char *pushnexttemplate (lua_State *L, const char *path) {\r
- const char *l;\r
- while (*path == *LUA_PATH_SEP) path++; /* skip separators */\r
- if (*path == '\0') return NULL; /* no more templates */\r
- l = strchr(path, *LUA_PATH_SEP); /* find next separator */\r
- if (l == NULL) l = path + strlen(path);\r
- lua_pushlstring(L, path, l - path); /* template */\r
- return l;\r
-}\r
-\r
-\r
-static const char *searchpath (lua_State *L, const char *name,\r
- const char *path,\r
- const char *sep,\r
- const char *dirsep) {\r
- luaL_Buffer msg; /* to build error message */\r
- luaL_buffinit(L, &msg);\r
- if (*sep != '\0') /* non-empty separator? */\r
- name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */\r
- while ((path = pushnexttemplate(L, path)) != NULL) {\r
- const char *filename = luaL_gsub(L, lua_tostring(L, -1),\r
- LUA_PATH_MARK, name);\r
- lua_remove(L, -2); /* remove path template */\r
- if (readable(filename)) /* does file exist and is readable? */\r
- return filename; /* return that file name */\r
- lua_pushfstring(L, "\n\tno file " LUA_QS, filename);\r
- lua_remove(L, -2); /* remove file name */\r
- luaL_addvalue(&msg); /* concatenate error msg. entry */\r
- }\r
- luaL_pushresult(&msg); /* create error message */\r
- return NULL; /* not found */\r
-}\r
-\r
-\r
-static int ll_searchpath (lua_State *L) {\r
- const char *f = searchpath(L, luaL_checkstring(L, 1),\r
- luaL_checkstring(L, 2),\r
- luaL_optstring(L, 3, "."),\r
- luaL_optstring(L, 4, LUA_DIRSEP));\r
- if (f != NULL) return 1;\r
- else { /* error message is on top of the stack */\r
- lua_pushnil(L);\r
- lua_insert(L, -2);\r
- return 2; /* return nil + error message */\r
- }\r
-}\r
-\r
-\r
-static const char *findfile (lua_State *L, const char *name,\r
- const char *pname,\r
- const char *dirsep) {\r
- const char *path;\r
- lua_getfield(L, lua_upvalueindex(1), pname);\r
- path = lua_tostring(L, -1);\r
- if (path == NULL)\r
- luaL_error(L, LUA_QL("package.%s") " must be a string", pname);\r
- return searchpath(L, name, path, ".", dirsep);\r
-}\r
-\r
-\r
-static int checkload (lua_State *L, int stat, const char *filename) {\r
- if (stat) { /* module loaded successfully? */\r
- lua_pushstring(L, filename); /* will be 2nd argument to module */\r
- return 2; /* return open function and file name */\r
- }\r
- else\r
- return luaL_error(L, "error loading module " LUA_QS\r
- " from file " LUA_QS ":\n\t%s",\r
- lua_tostring(L, 1), filename, lua_tostring(L, -1));\r
-}\r
-\r
-\r
-static int searcher_Lua (lua_State *L) {\r
- const char *filename;\r
- const char *name = luaL_checkstring(L, 1);\r
- filename = findfile(L, name, "path", LUA_LSUBSEP);\r
- if (filename == NULL) return 1; /* module not found in this path */\r
- return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);\r
-}\r
-\r
-\r
-static int loadfunc (lua_State *L, const char *filename, const char *modname) {\r
- const char *funcname;\r
- const char *mark;\r
- modname = luaL_gsub(L, modname, ".", LUA_OFSEP);\r
- mark = strchr(modname, *LUA_IGMARK);\r
- if (mark) {\r
- int stat;\r
- funcname = lua_pushlstring(L, modname, mark - modname);\r
- funcname = lua_pushfstring(L, LUA_POF"%s", funcname);\r
- stat = ll_loadfunc(L, filename, funcname);\r
- if (stat != ERRFUNC) return stat;\r
- modname = mark + 1; /* else go ahead and try old-style name */\r
- }\r
- funcname = lua_pushfstring(L, LUA_POF"%s", modname);\r
- return ll_loadfunc(L, filename, funcname);\r
-}\r
-\r
-\r
-static int searcher_C (lua_State *L) {\r
- const char *name = luaL_checkstring(L, 1);\r
- const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);\r
- if (filename == NULL) return 1; /* module not found in this path */\r
- return checkload(L, (loadfunc(L, filename, name) == 0), filename);\r
-}\r
-\r
-\r
-static int searcher_Croot (lua_State *L) {\r
- const char *filename;\r
- const char *name = luaL_checkstring(L, 1);\r
- const char *p = strchr(name, '.');\r
- int stat;\r
- if (p == NULL) return 0; /* is root */\r
- lua_pushlstring(L, name, p - name);\r
- filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);\r
- if (filename == NULL) return 1; /* root not found */\r
- if ((stat = loadfunc(L, filename, name)) != 0) {\r
- if (stat != ERRFUNC)\r
- return checkload(L, 0, filename); /* real error */\r
- else { /* open function not found */\r
- lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,\r
- name, filename);\r
- return 1;\r
- }\r
- }\r
- lua_pushstring(L, filename); /* will be 2nd argument to module */\r
- return 2;\r
-}\r
-\r
-\r
-static int searcher_preload (lua_State *L) {\r
- const char *name = luaL_checkstring(L, 1);\r
- lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");\r
- lua_getfield(L, -1, name);\r
- if (lua_isnil(L, -1)) /* not found? */\r
- lua_pushfstring(L, "\n\tno field package.preload['%s']", name);\r
- return 1;\r
-}\r
-\r
-\r
-static void findloader (lua_State *L, const char *name) {\r
- int i;\r
- luaL_Buffer msg; /* to build error message */\r
- luaL_buffinit(L, &msg);\r
- lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */\r
- if (!lua_istable(L, 3))\r
- luaL_error(L, LUA_QL("package.searchers") " must be a table");\r
- /* iterate over available searchers to find a loader */\r
- for (i = 1; ; i++) {\r
- lua_rawgeti(L, 3, i); /* get a searcher */\r
- if (lua_isnil(L, -1)) { /* no more searchers? */\r
- lua_pop(L, 1); /* remove nil */\r
- luaL_pushresult(&msg); /* create error message */\r
- luaL_error(L, "module " LUA_QS " not found:%s",\r
- name, lua_tostring(L, -1));\r
- }\r
- lua_pushstring(L, name);\r
- lua_call(L, 1, 2); /* call it */\r
- if (lua_isfunction(L, -2)) /* did it find a loader? */\r
- return; /* module loader found */\r
- else if (lua_isstring(L, -2)) { /* searcher returned error message? */\r
- lua_pop(L, 1); /* remove extra return */\r
- luaL_addvalue(&msg); /* concatenate error message */\r
- }\r
- else\r
- lua_pop(L, 2); /* remove both returns */\r
- }\r
-}\r
-\r
-\r
-static int ll_require (lua_State *L) {\r
- const char *name = luaL_checkstring(L, 1);\r
- lua_settop(L, 1); /* _LOADED table will be at index 2 */\r
- lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");\r
- lua_getfield(L, 2, name); /* _LOADED[name] */\r
- if (lua_toboolean(L, -1)) /* is it there? */\r
- return 1; /* package is already loaded */\r
- /* else must load package */\r
- lua_pop(L, 1); /* remove 'getfield' result */\r
- findloader(L, name);\r
- lua_pushstring(L, name); /* pass name as argument to module loader */\r
- lua_insert(L, -2); /* name is 1st argument (before search data) */\r
- lua_call(L, 2, 1); /* run loader to load module */\r
- if (!lua_isnil(L, -1)) /* non-nil return? */\r
- lua_setfield(L, 2, name); /* _LOADED[name] = returned value */\r
- lua_getfield(L, 2, name);\r
- if (lua_isnil(L, -1)) { /* module did not set a value? */\r
- lua_pushboolean(L, 1); /* use true as result */\r
- lua_pushvalue(L, -1); /* extra copy to be returned */\r
- lua_setfield(L, 2, name); /* _LOADED[name] = true */\r
- }\r
- return 1;\r
-}\r
-\r
-/* }====================================================== */\r
-\r
-\r
-\r
-/*\r
-** {======================================================\r
-** 'module' function\r
-** =======================================================\r
-*/\r
-#if defined(LUA_COMPAT_MODULE)\r
-\r
-/*\r
-** changes the environment variable of calling function\r
-*/\r
-static void set_env (lua_State *L) {\r
- lua_Debug ar;\r
- if (lua_getstack(L, 1, &ar) == 0 ||\r
- lua_getinfo(L, "f", &ar) == 0 || /* get calling function */\r
- lua_iscfunction(L, -1))\r
- luaL_error(L, LUA_QL("module") " not called from a Lua function");\r
- lua_pushvalue(L, -2); /* copy new environment table to top */\r
- lua_setupvalue(L, -2, 1);\r
- lua_pop(L, 1); /* remove function */\r
-}\r
-\r
-\r
-static void dooptions (lua_State *L, int n) {\r
- int i;\r
- for (i = 2; i <= n; i++) {\r
- if (lua_isfunction(L, i)) { /* avoid 'calling' extra info. */\r
- lua_pushvalue(L, i); /* get option (a function) */\r
- lua_pushvalue(L, -2); /* module */\r
- lua_call(L, 1, 0);\r
- }\r
- }\r
-}\r
-\r
-\r
-static void modinit (lua_State *L, const char *modname) {\r
- const char *dot;\r
- lua_pushvalue(L, -1);\r
- lua_setfield(L, -2, "_M"); /* module._M = module */\r
- lua_pushstring(L, modname);\r
- lua_setfield(L, -2, "_NAME");\r
- dot = strrchr(modname, '.'); /* look for last dot in module name */\r
- if (dot == NULL) dot = modname;\r
- else dot++;\r
- /* set _PACKAGE as package name (full module name minus last part) */\r
- lua_pushlstring(L, modname, dot - modname);\r
- lua_setfield(L, -2, "_PACKAGE");\r
-}\r
-\r
-\r
-static int ll_module (lua_State *L) {\r
- const char *modname = luaL_checkstring(L, 1);\r
- int lastarg = lua_gettop(L); /* last parameter */\r
- luaL_pushmodule(L, modname, 1); /* get/create module table */\r
- /* check whether table already has a _NAME field */\r
- lua_getfield(L, -1, "_NAME");\r
- if (!lua_isnil(L, -1)) /* is table an initialized module? */\r
- lua_pop(L, 1);\r
- else { /* no; initialize it */\r
- lua_pop(L, 1);\r
- modinit(L, modname);\r
- }\r
- lua_pushvalue(L, -1);\r
- set_env(L);\r
- dooptions(L, lastarg);\r
- return 1;\r
-}\r
-\r
-\r
-static int ll_seeall (lua_State *L) {\r
- luaL_checktype(L, 1, LUA_TTABLE);\r
- if (!lua_getmetatable(L, 1)) {\r
- lua_createtable(L, 0, 1); /* create new metatable */\r
- lua_pushvalue(L, -1);\r
- lua_setmetatable(L, 1);\r
- }\r
- lua_pushglobaltable(L);\r
- lua_setfield(L, -2, "__index"); /* mt.__index = _G */\r
- return 0;\r
-}\r
-\r
-#endif\r
-/* }====================================================== */\r
-\r
-\r
-\r
-/* auxiliary mark (for internal use) */\r
-#define AUXMARK "\1"\r
-\r
-\r
-/*\r
-** return registry.LUA_NOENV as a boolean\r
-*/\r
-static int noenv (lua_State *L) {\r
- int b;\r
- lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");\r
- b = lua_toboolean(L, -1);\r
- lua_pop(L, 1); /* remove value */\r
- return b;\r
-}\r
-\r
-\r
-static void setpath (lua_State *L, const char *fieldname, const char *envname1,\r
- const char *envname2, const char *def) {\r
- const char *path = getenv(envname1);\r
- if (path == NULL) /* no environment variable? */\r
- path = getenv(envname2); /* try alternative name */\r
- if (path == NULL || noenv(L)) /* no environment variable? */\r
- lua_pushstring(L, def); /* use default */\r
- else {\r
- /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */\r
- path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,\r
- LUA_PATH_SEP AUXMARK LUA_PATH_SEP);\r
- luaL_gsub(L, path, AUXMARK, def);\r
- lua_remove(L, -2);\r
- }\r
- setprogdir(L);\r
- lua_setfield(L, -2, fieldname);\r
-}\r
-\r
-\r
-static const luaL_Reg pk_funcs[] = {\r
- {"loadlib", ll_loadlib},\r
- {"searchpath", ll_searchpath},\r
-#if defined(LUA_COMPAT_MODULE)\r
- {"seeall", ll_seeall},\r
-#endif\r
- {NULL, NULL}\r
-};\r
-\r
-\r
-static const luaL_Reg ll_funcs[] = {\r
-#if defined(LUA_COMPAT_MODULE)\r
- {"module", ll_module},\r
-#endif\r
- {"require", ll_require},\r
- {NULL, NULL}\r
-};\r
-\r
-\r
-static void createsearcherstable (lua_State *L) {\r
- static const lua_CFunction searchers[] =\r
- {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};\r
- int i;\r
- /* create 'searchers' table */\r
- lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);\r
- /* fill it with pre-defined searchers */\r
- for (i=0; searchers[i] != NULL; i++) {\r
- lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */\r
- lua_pushcclosure(L, searchers[i], 1);\r
- lua_rawseti(L, -2, i+1);\r
- }\r
-}\r
-\r
-\r
-LUAMOD_API int luaopen_package (lua_State *L) {\r
- /* create table CLIBS to keep track of loaded C libraries */\r
- luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS);\r
- lua_createtable(L, 0, 1); /* metatable for CLIBS */\r
- lua_pushcfunction(L, gctm);\r
- lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */\r
- lua_setmetatable(L, -2);\r
- /* create `package' table */\r
- luaL_newlib(L, pk_funcs);\r
- createsearcherstable(L);\r
-#if defined(LUA_COMPAT_LOADERS)\r
- lua_pushvalue(L, -1); /* make a copy of 'searchers' table */\r
- lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */\r
-#endif\r
- lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */\r
- /* set field 'path' */\r
- setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT);\r
- /* set field 'cpath' */\r
- setpath(L, "cpath", LUA_CPATHVERSION, LUA_CPATH, LUA_CPATH_DEFAULT);\r
- /* store config information */\r
- lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"\r
- LUA_EXEC_DIR "\n" LUA_IGMARK "\n");\r
- lua_setfield(L, -2, "config");\r
- /* set field `loaded' */\r
- luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");\r
- lua_setfield(L, -2, "loaded");\r
- /* set field `preload' */\r
- luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");\r
- lua_setfield(L, -2, "preload");\r
- lua_pushglobaltable(L);\r
- lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */\r
- luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */\r
- lua_pop(L, 1); /* pop global table */\r
- return 1; /* return 'package' table */\r
-}\r
-\r