commit d6ff5d9f46be3db99dd3fec0c12504b7ba1854e1
parent a05190fa3b5c3685b0772f0235f136e05587fa9c
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Wed, 13 Jul 2005 16:02:20 -0300
searchpath is "private affair" of loadlib
Diffstat:
M | lauxlib.c | | | 35 | +---------------------------------- |
M | lauxlib.h | | | 5 | +---- |
M | loadlib.c | | | 62 | ++++++++++++++++++++++++++++++++++++++++++++++---------------- |
3 files changed, 48 insertions(+), 54 deletions(-)
diff --git a/lauxlib.c b/lauxlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lauxlib.c,v 1.139 2005/07/11 16:41:51 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.140 2005/07/12 14:32:48 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@@ -325,16 +325,6 @@ LUALIB_API int luaL_getn (lua_State *L, int t) {
/* }====================================================== */
-static const char *pushnexttemplate (lua_State *L, const char *path) {
- const char *l;
- while (*path == *LUA_PATHSEP) path++; /* skip separators */
- if (*path == '\0') return NULL; /* no more templates */
- l = strchr(path, *LUA_PATHSEP); /* find next separator */
- if (l == NULL) l = path+strlen(path);
- lua_pushlstring(L, path, l - path); /* template */
- return l;
-}
-
LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
const char *r) {
@@ -353,29 +343,6 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
}
-static int readable (const char *fname) {
- FILE *f = fopen(fname, "r"); /* try to open file */
- if (f == NULL) return 0; /* open failed */
- fclose(f);
- return 1;
-}
-
-
-LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
- const char *path) {
- const char *p = path;
- for (;;) {
- const char *fname;
- if ((p = pushnexttemplate(L, p)) == NULL)
- return NULL;
- fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
- lua_remove(L, -2); /* remove path template */
- if (readable(fname)) /* does file exist and is readable? */
- return fname; /* return that file name */
- lua_pop(L, 1); /* remove file name */
- }
-}
-
LUALIB_API const char *luaL_getfield (lua_State *L, int idx,
const char *fname) {
diff --git a/lauxlib.h b/lauxlib.h
@@ -1,5 +1,5 @@
/*
-** $Id: lauxlib.h,v 1.78 2005/05/31 14:25:18 roberto Exp roberto $
+** $Id: lauxlib.h,v 1.79 2005/05/31 14:34:02 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@@ -62,9 +62,6 @@ LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,
const char *const lst[]);
-LUALIB_API const char *(luaL_searchpath) (lua_State *L, const char *name,
- const char *path);
-
LUALIB_API int (luaL_ref) (lua_State *L, int t);
LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
diff --git a/loadlib.c b/loadlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: loadlib.c,v 1.32 2005/07/11 16:41:57 roberto Exp roberto $
+** $Id: loadlib.c,v 1.33 2005/07/12 21:17:46 roberto Exp roberto $
** Dynamic library loader for Lua
** See Copyright Notice in lua.h
**
@@ -329,15 +329,41 @@ static int ll_loadlib (lua_State *L) {
*/
+static int readable (const char *fname) {
+ FILE *f = fopen(fname, "r"); /* try to open file */
+ if (f == NULL) return 0; /* open failed */
+ fclose(f);
+ return 1;
+}
+
+
+static const char *pushnexttemplate (lua_State *L, const char *path) {
+ const char *l;
+ while (*path == *LUA_PATHSEP) path++; /* skip separators */
+ if (*path == '\0') return NULL; /* no more templates */
+ l = strchr(path, *LUA_PATHSEP); /* find next separator */
+ if (l == NULL) l = path + strlen(path);
+ lua_pushlstring(L, path, l - path); /* template */
+ return l;
+}
+
+
static const char *findfile (lua_State *L, const char *pname) {
- const char *name = luaL_checkstring(L, 1);
- const char *fname = luaL_gsub(L, name, ".", LUA_DIRSEP);
const char *path;
+ const char *name = luaL_checkstring(L, 1);
+ name = luaL_gsub(L, name, ".", LUA_DIRSEP);
lua_getfield(L, LUA_ENVIRONINDEX, pname);
path = lua_tostring(L, -1);
if (path == NULL)
luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
- return luaL_searchpath(L, fname, path);
+ while ((path = pushnexttemplate(L, path)) != NULL) {
+ const char *fname;
+ fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
+ if (readable(fname)) /* does file exist and is readable? */
+ return fname; /* return that file name */
+ lua_pop(L, 2); /* remove path template and file name */
+ }
+ return NULL; /* not found */
}
@@ -414,16 +440,21 @@ static int require_aux (lua_State *L, const char *name) {
}
-static void ll_error (lua_State *L, const char *name) {
- const char *msg;
- lua_settop(L, 1);
- lua_getfield(L, LUA_ENVIRONINDEX, "path");
- lua_getfield(L, LUA_ENVIRONINDEX, "cpath");
- msg = lua_pushfstring(L, "package " LUA_QS " not found in following paths:\n"
- " Lua path: %s\n C path: %s\n", name,
- lua_tostring(L, -2), lua_tostring(L, -1));
- msg = luaL_gsub(L, msg, LUA_PATHSEP, "\n ");
- luaL_error(L, msg);
+static void require_check (lua_State *L, const char *name) {
+ if (!require_aux(L, name)) { /* error? */
+ /* build and show error message */
+ const char *msg;
+ lua_settop(L, 1);
+ lua_getfield(L, LUA_ENVIRONINDEX, "path");
+ lua_getfield(L, LUA_ENVIRONINDEX, "cpath");
+ msg = lua_pushfstring(L,
+ "package " LUA_QS " not found in following paths:\n"
+ " Lua path: %s\n"
+ " C path: %s\n", name,
+ lua_tostring(L, -2), lua_tostring(L, -1));
+ msg = luaL_gsub(L, msg, LUA_PATHSEP, "\n ");
+ luaL_error(L, msg);
+ }
}
@@ -436,8 +467,7 @@ static int ll_require (lua_State *L) {
lua_pushlstring(L, name, pt - name);
require_aux(L, lua_tostring(L, -1));
}
- if (!require_aux(L, name)) /* load module itself */
- ll_error(L, name);
+ require_check(L, name); /* load module itself */
return 1;
}