configurable 'directory separator' in 'package.searchpath'

This commit is contained in:
Roberto Ierusalimschy
2011-11-09 13:18:04 -02:00
parent a716ae1f50
commit 30cea671a7

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: loadlib.c,v 1.100 2011/07/05 12:49:35 roberto Exp roberto $ ** $Id: loadlib.c,v 1.101 2011/11/06 13:59:12 roberto Exp roberto $
** Dynamic library loader for Lua ** Dynamic library loader for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
** **
@@ -9,6 +9,14 @@
*/ */
/*
** if needed, includes windows header before everything else
*/
#if defined(LUA_DL_DLL)
#include <windows.h>
#endif
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -62,6 +70,20 @@
#endif #endif
/*
** LUA_CSUBSEP is the character that replaces dots in submodule names
** when searching for a C loader.
** LUA_LSUBSEP is the character that replaces dots in submodule names
** when searching for a Lua loader.
*/
#if !defined(LUA_CSUBSEP)
#define LUA_CSUBSEP LUA_DIRSEP
#endif
#if !defined(LUA_LSUBSEP)
#define LUA_LSUBSEP LUA_DIRSEP
#endif
/* prefix for open functions in C libraries */ /* prefix for open functions in C libraries */
#define LUA_POF "luaopen_" #define LUA_POF "luaopen_"
@@ -133,8 +155,6 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
** ======================================================================= ** =======================================================================
*/ */
#include <windows.h>
#undef setprogdir #undef setprogdir
/* /*
@@ -322,9 +342,10 @@ static const char *pushnexttemplate (lua_State *L, const char *path) {
static const char *searchpath (lua_State *L, const char *name, static const char *searchpath (lua_State *L, const char *name,
const char *path, const char *path,
const char *sep) { const char *sep,
const char *dirsep) {
if (*sep != '\0') /* non-empty separator? */ if (*sep != '\0') /* non-empty separator? */
name = luaL_gsub(L, name, sep, LUA_DIRSEP); /* replace it by proper one */ name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
lua_pushliteral(L, ""); /* error accumulator */ lua_pushliteral(L, ""); /* error accumulator */
while ((path = pushnexttemplate(L, path)) != NULL) { while ((path = pushnexttemplate(L, path)) != NULL) {
const char *filename = luaL_gsub(L, lua_tostring(L, -1), const char *filename = luaL_gsub(L, lua_tostring(L, -1),
@@ -343,7 +364,8 @@ static const char *searchpath (lua_State *L, const char *name,
static int ll_searchpath (lua_State *L) { static int ll_searchpath (lua_State *L) {
const char *f = searchpath(L, luaL_checkstring(L, 1), const char *f = searchpath(L, luaL_checkstring(L, 1),
luaL_checkstring(L, 2), luaL_checkstring(L, 2),
luaL_optstring(L, 3, ".")); luaL_optstring(L, 3, "."),
luaL_optstring(L, 4, LUA_DIRSEP));
if (f != NULL) return 1; if (f != NULL) return 1;
else { /* error message is on top of the stack */ else { /* error message is on top of the stack */
lua_pushnil(L); lua_pushnil(L);
@@ -354,13 +376,14 @@ static int ll_searchpath (lua_State *L) {
static const char *findfile (lua_State *L, const char *name, static const char *findfile (lua_State *L, const char *name,
const char *pname) { const char *pname,
const char *dirsep) {
const char *path; const char *path;
lua_getfield(L, lua_upvalueindex(1), pname); lua_getfield(L, lua_upvalueindex(1), pname);
path = lua_tostring(L, -1); path = lua_tostring(L, -1);
if (path == NULL) if (path == NULL)
luaL_error(L, LUA_QL("package.%s") " must be a string", pname); luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
return searchpath(L, name, path, "."); return searchpath(L, name, path, ".", dirsep);
} }
@@ -379,7 +402,7 @@ static int checkload (lua_State *L, int stat, const char *filename) {
static int searcher_Lua (lua_State *L) { static int searcher_Lua (lua_State *L) {
const char *filename; const char *filename;
const char *name = luaL_checkstring(L, 1); const char *name = luaL_checkstring(L, 1);
filename = findfile(L, name, "path"); filename = findfile(L, name, "path", LUA_LSUBSEP);
if (filename == NULL) return 1; /* module not found in this path */ if (filename == NULL) return 1; /* module not found in this path */
return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
} }
@@ -405,7 +428,7 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) {
static int searcher_C (lua_State *L) { static int searcher_C (lua_State *L) {
const char *name = luaL_checkstring(L, 1); const char *name = luaL_checkstring(L, 1);
const char *filename = findfile(L, name, "cpath"); const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
if (filename == NULL) return 1; /* module not found in this path */ if (filename == NULL) return 1; /* module not found in this path */
return checkload(L, (loadfunc(L, filename, name) == 0), filename); return checkload(L, (loadfunc(L, filename, name) == 0), filename);
} }
@@ -418,7 +441,7 @@ static int searcher_Croot (lua_State *L) {
int stat; int stat;
if (p == NULL) return 0; /* is root */ if (p == NULL) return 0; /* is root */
lua_pushlstring(L, name, p - name); lua_pushlstring(L, name, p - name);
filename = findfile(L, lua_tostring(L, -1), "cpath"); filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
if (filename == NULL) return 1; /* root not found */ if (filename == NULL) return 1; /* root not found */
if ((stat = loadfunc(L, filename, name)) != 0) { if ((stat = loadfunc(L, filename, name)) != 0) {
if (stat != ERRFUNC) if (stat != ERRFUNC)