commit 80ec81926c19ff638a65088bb3d5a55d20b55945
parent 96917ff42a7867e72629d6cd39c818df4c250b91
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Thu, 1 Jul 2004 11:26:06 -0300
`lua.c' also needs the default path, so auxlib should provide it
Diffstat:
3 files changed, 23 insertions(+), 29 deletions(-)
diff --git a/lauxlib.c b/lauxlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lauxlib.c,v 1.117 2004/06/21 20:05:29 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.118 2004/06/29 16:57:56 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@@ -341,6 +341,15 @@ LUALIB_API int luaL_getn (lua_State *L, int t) {
/* }====================================================== */
+static const char *getpath (lua_State *L) {
+ const char *path;
+ lua_getglobal(L, LUA_PATH); /* try global variable */
+ path = lua_tostring(L, -1);
+ if (path) return path;
+ path = getenv(LUA_PATH); /* else try environment variable */
+ if (path) return path;
+ return LUA_PATH_DEFAULT; /* else use default */
+}
static const char *pushnexttemplate (lua_State *L, const char *path) {
@@ -354,8 +363,8 @@ static const char *pushnexttemplate (lua_State *L, const char *path) {
}
-static const char *gsub (lua_State *L, const char *s, const char *p,
- const char *r) {
+static const char *luaL_gsub (lua_State *L, const char *s,
+ const char *p, const char *r) {
const char *wild;
int l = strlen(p);
luaL_Buffer b;
@@ -363,7 +372,7 @@ static const char *gsub (lua_State *L, const char *s, const char *p,
while ((wild = strstr(s, p)) != NULL) {
luaL_addlstring(&b, s, wild - s); /* push prefix */
luaL_addstring(&b, r); /* push replacement in place of pattern */
- s = wild + l; /* continue after p */
+ s = wild + l; /* continue after `p' */
}
luaL_addstring(&b, s); /* push last suffix (`n' already includes this) */
luaL_pushresult(&b);
@@ -375,17 +384,20 @@ LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
const char *path) {
FILE *f;
const char *p = path;
+ if (p == NULL) p = getpath(L);
+ else lua_pushnil(L); /* to balance item pushed by `getpath' */
for (;;) {
const char *fname;
if ((p = pushnexttemplate(L, p)) == NULL) {
lua_pushfstring(L, "no readable `%s' in path `%s'", name, path);
return NULL;
}
- fname = gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
+ fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
lua_remove(L, -2); /* remove path template */
f = fopen(fname, "r"); /* try to read it */
if (f) {
fclose(f);
+ lua_remove(L, -2); /* remove path */
return fname;
}
lua_pop(L, 1); /* remove file name */
diff --git a/lbaselib.c b/lbaselib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lbaselib.c,v 1.149 2004/06/21 20:05:29 roberto Exp roberto $
+** $Id: lbaselib.c,v 1.150 2004/06/29 16:58:17 roberto Exp roberto $
** Basic library
** See Copyright Notice in lua.h
*/
@@ -459,20 +459,8 @@ static int luaB_newproxy (lua_State *L) {
*/
-static const char *getpath (lua_State *L) {
- const char *path;
- lua_getglobal(L, LUA_PATH); /* try global variable */
- path = lua_tostring(L, -1);
- if (path) return path;
- path = getenv(LUA_PATH); /* else try environment variable */
- if (path) return path;
- return LUA_PATH_DEFAULT; /* else use default */
-}
-
-
static int luaB_require (lua_State *L) {
const char *name = luaL_checkstring(L, 1);
- const char *path = getpath(L);
const char *fname;
int loaded;
lua_getglobal(L, REQTAB);
@@ -485,7 +473,7 @@ static int luaB_require (lua_State *L) {
/* else must load it; first mark it as loaded */
lua_pushboolean(L, 1);
lua_setfield(L, loaded, name); /* _LOADED[name] = true */
- fname = luaL_searchpath(L, name, path);
+ fname = luaL_searchpath(L, name, NULL);
if (fname == NULL || luaL_loadfile(L, fname) != 0)
return luaL_error(L, "error loading package `%s' (%s)", name,
lua_tostring(L, -1));
diff --git a/lua.c b/lua.c
@@ -1,5 +1,5 @@
/*
-** $Id: lua.c,v 1.127 2004/06/16 20:22:43 roberto Exp roberto $
+** $Id: lua.c,v 1.128 2004/06/17 14:06:52 roberto Exp roberto $
** Lua stand-alone interpreter
** See Copyright Notice in lua.h
*/
@@ -143,15 +143,9 @@ static int dostring (const char *s, const char *name) {
static int load_file (const char *name) {
- lua_getglobal(L, "require");
- if (!lua_isfunction(L, -1)) { /* no `require' defined? */
- lua_pop(L, 1);
- return file_input(name);
- }
- else {
- lua_pushstring(L, name);
- return report(lcall(1, 1));
- }
+ name = luaL_searchpath(L, name, NULL);
+ if (name == NULL) return report(1);
+ else return file_input(name);
}