diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2007-12-12 12:36:12 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2007-12-12 12:36:12 -0200 |
| commit | bc82b4d78acd281f68f2f6fcd79395bbee32ee19 (patch) | |
| tree | db8be1efd0016b3e00e5d80d2b2e48de6b4b7979 | |
| parent | dae850e0eecb3e3a4d3c08201876ae475936e285 (diff) | |
| download | lua-bc82b4d78acd281f68f2f6fcd79395bbee32ee19.tar.gz lua-bc82b4d78acd281f68f2f6fcd79395bbee32ee19.tar.bz2 lua-bc82b4d78acd281f68f2f6fcd79395bbee32ee19.zip | |
new function 'package.searchpath'
| -rw-r--r-- | loadlib.c | 40 |
1 files changed, 29 insertions, 11 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: loadlib.c,v 1.57 2007/03/26 15:57:35 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.58 2007/06/21 13:52:27 roberto Exp roberto $ |
| 3 | ** Dynamic library loader for Lua | 3 | ** Dynamic library loader for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | ** | 5 | ** |
| @@ -346,18 +346,12 @@ static const char *pushnexttemplate (lua_State *L, const char *path) { | |||
| 346 | } | 346 | } |
| 347 | 347 | ||
| 348 | 348 | ||
| 349 | static const char *findfile (lua_State *L, const char *name, | 349 | static const char *searchpath (lua_State *L, const char *name, |
| 350 | const char *pname) { | 350 | const char *path) { |
| 351 | const char *path; | ||
| 352 | name = luaL_gsub(L, name, ".", LUA_DIRSEP); | ||
| 353 | lua_getfield(L, LUA_ENVIRONINDEX, pname); | ||
| 354 | path = lua_tostring(L, -1); | ||
| 355 | if (path == NULL) | ||
| 356 | luaL_error(L, LUA_QL("package.%s") " must be a string", pname); | ||
| 357 | lua_pushliteral(L, ""); /* error accumulator */ | 351 | lua_pushliteral(L, ""); /* error accumulator */ |
| 358 | while ((path = pushnexttemplate(L, path)) != NULL) { | 352 | while ((path = pushnexttemplate(L, path)) != NULL) { |
| 359 | const char *filename; | 353 | const char *filename = luaL_gsub(L, lua_tostring(L, -1), |
| 360 | filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); | 354 | LUA_PATH_MARK, name); |
| 361 | lua_remove(L, -2); /* remove path template */ | 355 | lua_remove(L, -2); /* remove path template */ |
| 362 | if (readable(filename)) /* does file exist and is readable? */ | 356 | if (readable(filename)) /* does file exist and is readable? */ |
| 363 | return filename; /* return that file name */ | 357 | return filename; /* return that file name */ |
| @@ -369,6 +363,29 @@ static const char *findfile (lua_State *L, const char *name, | |||
| 369 | } | 363 | } |
| 370 | 364 | ||
| 371 | 365 | ||
| 366 | static int ll_searchpath (lua_State *L) { | ||
| 367 | const char *f = searchpath(L, luaL_checkstring(L, 1), luaL_checkstring(L, 2)); | ||
| 368 | if (f != NULL) return 1; | ||
| 369 | else { /* error message is on top of the stack */ | ||
| 370 | lua_pushnil(L); | ||
| 371 | lua_insert(L, -2); | ||
| 372 | return 2; /* return nil + error message */ | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | |||
| 377 | static const char *findfile (lua_State *L, const char *name, | ||
| 378 | const char *pname) { | ||
| 379 | const char *path; | ||
| 380 | name = luaL_gsub(L, name, ".", LUA_DIRSEP); | ||
| 381 | lua_getfield(L, LUA_ENVIRONINDEX, pname); | ||
| 382 | path = lua_tostring(L, -1); | ||
| 383 | if (path == NULL) | ||
| 384 | luaL_error(L, LUA_QL("package.%s") " must be a string", pname); | ||
| 385 | return searchpath(L, name, path); | ||
| 386 | } | ||
| 387 | |||
| 388 | |||
| 372 | static void loaderror (lua_State *L, const char *filename) { | 389 | static void loaderror (lua_State *L, const char *filename) { |
| 373 | luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", | 390 | luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", |
| 374 | lua_tostring(L, 1), filename, lua_tostring(L, -1)); | 391 | lua_tostring(L, 1), filename, lua_tostring(L, -1)); |
| @@ -604,6 +621,7 @@ static void setpath (lua_State *L, const char *fieldname, const char *envname, | |||
| 604 | 621 | ||
| 605 | static const luaL_Reg pk_funcs[] = { | 622 | static const luaL_Reg pk_funcs[] = { |
| 606 | {"loadlib", ll_loadlib}, | 623 | {"loadlib", ll_loadlib}, |
| 624 | {"searchpath", ll_searchpath}, | ||
| 607 | {"seeall", ll_seeall}, | 625 | {"seeall", ll_seeall}, |
| 608 | {NULL, NULL} | 626 | {NULL, NULL} |
| 609 | }; | 627 | }; |
