From 9bff845b49e277af46d6b7a09bb111472f3d3f49 Mon Sep 17 00:00:00 2001 From: Maurus Cuelenaere Date: Wed, 1 Jul 2009 17:01:22 +0000 Subject: Lua: because Rockbox doesn't support any current working directory functionality, 'hack' loadlib so it replace '$' in LUA_PATH_DEFAULT with the directory wherein the current script is. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21595 a1c6a512-1295-4272-9138-f99709370657 --- apps/plugins/lua/loadlib.c | 4 +++- apps/plugins/lua/rockaux.c | 33 +++++++++++++++++++++++++++++++++ apps/plugins/lua/rockconf.h | 2 +- apps/plugins/lua/rocklib.c | 32 ++++++-------------------------- apps/plugins/lua/rocklib.h | 2 +- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/apps/plugins/lua/loadlib.c b/apps/plugins/lua/loadlib.c index 035116dc71..1cc7ebd7db 100644 --- a/apps/plugins/lua/loadlib.c +++ b/apps/plugins/lua/loadlib.c @@ -20,6 +20,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "rocklib.h" #define setprogdir(L) ((void)0) @@ -53,7 +54,7 @@ static const char *pushnexttemplate (lua_State *L, const char *path) { static const char *findfile (lua_State *L, const char *name, const char *pname) { - const char *path; + const char *path, *current_path = get_current_path(L, 2); name = luaL_gsub(L, name, ".", LUA_DIRSEP); lua_getfield(L, LUA_ENVIRONINDEX, pname); path = lua_tostring(L, -1); @@ -63,6 +64,7 @@ static const char *findfile (lua_State *L, const char *name, while ((path = pushnexttemplate(L, path)) != NULL) { const char *filename; filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); + if(current_path != NULL) filename = luaL_gsub(L, filename, "$", current_path); lua_remove(L, -2); /* remove path template */ if (readable(filename)) /* does file exist and is readable? */ return filename; /* return that file name */ diff --git a/apps/plugins/lua/rockaux.c b/apps/plugins/lua/rockaux.c index 11433f286f..95f2ab169b 100644 --- a/apps/plugins/lua/rockaux.c +++ b/apps/plugins/lua/rockaux.c @@ -21,6 +21,8 @@ ****************************************************************************/ #include "plugin.h" +#define _ROCKCONF_H_ /* Protect against unwanted include */ +#include "lua.h" #if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__) int errno = 0; @@ -59,3 +61,34 @@ int strcoll(const char * str1, const char * str2) return rb->strcmp(str1, str2); } +const char* get_current_path(lua_State *L, int level) +{ + static char buffer[MAX_PATH]; + lua_Debug ar; + + if(lua_getstack(L, level, &ar)) + { + /* Try determining the base path of the current Lua chunk + and write it to dest. */ + lua_getinfo(L, "S", &ar); + + char* curfile = (char*) &ar.source[1]; + char* pos = rb->strrchr(curfile, '/'); + if(pos != NULL) + { + unsigned int len = (unsigned int)(pos - curfile); + len = len + 1 > sizeof(buffer) ? sizeof(buffer) - 1 : len; + + if(len > 0) + memcpy(buffer, curfile, len); + + buffer[len] = '/'; + buffer[len+1] = '\0'; + + return buffer; + } + } + + return NULL; +} + diff --git a/apps/plugins/lua/rockconf.h b/apps/plugins/lua/rockconf.h index 40f7d74554..b72ebeacd0 100644 --- a/apps/plugins/lua/rockconf.h +++ b/apps/plugins/lua/rockconf.h @@ -29,7 +29,7 @@ #undef luai_jmpbuf #undef LUA_PATH_DEFAULT -#define LUA_PATH_DEFAULT "./?.lua;" VIEWERS_DIR"/?.lua;" +#define LUA_PATH_DEFAULT "$/?.lua;" VIEWERS_DIR"/?.lua;" #ifndef SIMULATOR #include "../../codecs/lib/setjmp.h" diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index 5109092570..410916b2f9 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c @@ -782,34 +782,14 @@ RB_WRAP(read_bmp_file) RB_WRAP(current_path) { - char buffer[MAX_PATH]; - lua_Debug ar; - - if(lua_getstack(L, 1, &ar)) + const char *current_path = get_current_path(L, 1); + if(current_path != NULL) { - /* Try determining the base path of the current Lua chunk - and write it to dest. */ - lua_getinfo(L, "S", &ar); - - char* curfile = (char*) &ar.source[1]; - char* pos = rb->strrchr(curfile, '/'); - if(pos != NULL) - { - unsigned int len = (unsigned int)(pos - curfile); - len = len + 1 > sizeof(buffer) ? sizeof(buffer) - 1 : len; - - if(len > 0) - memcpy(buffer, curfile, len); - - buffer[len] = '/'; - buffer[len+1] = '\0'; - - lua_pushstring(L, buffer); - return 1; - } + lua_pushstring(L, current_path); + return 1; } - - return 0; + else + return 0; } #define R(NAME) {#NAME, rock_##NAME} diff --git a/apps/plugins/lua/rocklib.h b/apps/plugins/lua/rocklib.h index 4a1e79cc8a..84b5fd2de0 100644 --- a/apps/plugins/lua/rocklib.h +++ b/apps/plugins/lua/rocklib.h @@ -24,7 +24,7 @@ #define LUA_ROCKLIBNAME "rb" LUALIB_API int (luaopen_rock) (lua_State *L); -bool get_cur_path(lua_State *L, char* dest, size_t dest_size); +const char* get_current_path(lua_State *L, int level); #endif /* _ROCKLIB_H_ */ -- cgit v1.2.3