aboutsummaryrefslogtreecommitdiff
path: root/loadlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2007-12-12 12:36:12 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2007-12-12 12:36:12 -0200
commitbc82b4d78acd281f68f2f6fcd79395bbee32ee19 (patch)
treedb8be1efd0016b3e00e5d80d2b2e48de6b4b7979 /loadlib.c
parentdae850e0eecb3e3a4d3c08201876ae475936e285 (diff)
downloadlua-bc82b4d78acd281f68f2f6fcd79395bbee32ee19.tar.gz
lua-bc82b4d78acd281f68f2f6fcd79395bbee32ee19.tar.bz2
lua-bc82b4d78acd281f68f2f6fcd79395bbee32ee19.zip
new function 'package.searchpath'
Diffstat (limited to 'loadlib.c')
-rw-r--r--loadlib.c40
1 files changed, 29 insertions, 11 deletions
diff --git a/loadlib.c b/loadlib.c
index 16d5c4f4..c06c56f4 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -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
349static const char *findfile (lua_State *L, const char *name, 349static 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
366static 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
377static 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
372static void loaderror (lua_State *L, const char *filename) { 389static 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
605static const luaL_Reg pk_funcs[] = { 622static 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};