diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/plugins/lua/loadlib.c | 4 | ||||
-rw-r--r-- | apps/plugins/lua/rockaux.c | 33 | ||||
-rw-r--r-- | apps/plugins/lua/rockconf.h | 2 | ||||
-rw-r--r-- | apps/plugins/lua/rocklib.c | 32 | ||||
-rw-r--r-- | 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 @@ | |||
20 | 20 | ||
21 | #include "lauxlib.h" | 21 | #include "lauxlib.h" |
22 | #include "lualib.h" | 22 | #include "lualib.h" |
23 | #include "rocklib.h" | ||
23 | 24 | ||
24 | 25 | ||
25 | #define setprogdir(L) ((void)0) | 26 | #define setprogdir(L) ((void)0) |
@@ -53,7 +54,7 @@ static const char *pushnexttemplate (lua_State *L, const char *path) { | |||
53 | 54 | ||
54 | static const char *findfile (lua_State *L, const char *name, | 55 | static const char *findfile (lua_State *L, const char *name, |
55 | const char *pname) { | 56 | const char *pname) { |
56 | const char *path; | 57 | const char *path, *current_path = get_current_path(L, 2); |
57 | name = luaL_gsub(L, name, ".", LUA_DIRSEP); | 58 | name = luaL_gsub(L, name, ".", LUA_DIRSEP); |
58 | lua_getfield(L, LUA_ENVIRONINDEX, pname); | 59 | lua_getfield(L, LUA_ENVIRONINDEX, pname); |
59 | path = lua_tostring(L, -1); | 60 | path = lua_tostring(L, -1); |
@@ -63,6 +64,7 @@ static const char *findfile (lua_State *L, const char *name, | |||
63 | while ((path = pushnexttemplate(L, path)) != NULL) { | 64 | while ((path = pushnexttemplate(L, path)) != NULL) { |
64 | const char *filename; | 65 | const char *filename; |
65 | filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); | 66 | filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); |
67 | if(current_path != NULL) filename = luaL_gsub(L, filename, "$", current_path); | ||
66 | lua_remove(L, -2); /* remove path template */ | 68 | lua_remove(L, -2); /* remove path template */ |
67 | if (readable(filename)) /* does file exist and is readable? */ | 69 | if (readable(filename)) /* does file exist and is readable? */ |
68 | return filename; /* return that file name */ | 70 | 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 @@ | |||
21 | ****************************************************************************/ | 21 | ****************************************************************************/ |
22 | 22 | ||
23 | #include "plugin.h" | 23 | #include "plugin.h" |
24 | #define _ROCKCONF_H_ /* Protect against unwanted include */ | ||
25 | #include "lua.h" | ||
24 | 26 | ||
25 | #if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__) | 27 | #if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__) |
26 | int errno = 0; | 28 | int errno = 0; |
@@ -59,3 +61,34 @@ int strcoll(const char * str1, const char * str2) | |||
59 | return rb->strcmp(str1, str2); | 61 | return rb->strcmp(str1, str2); |
60 | } | 62 | } |
61 | 63 | ||
64 | const char* get_current_path(lua_State *L, int level) | ||
65 | { | ||
66 | static char buffer[MAX_PATH]; | ||
67 | lua_Debug ar; | ||
68 | |||
69 | if(lua_getstack(L, level, &ar)) | ||
70 | { | ||
71 | /* Try determining the base path of the current Lua chunk | ||
72 | and write it to dest. */ | ||
73 | lua_getinfo(L, "S", &ar); | ||
74 | |||
75 | char* curfile = (char*) &ar.source[1]; | ||
76 | char* pos = rb->strrchr(curfile, '/'); | ||
77 | if(pos != NULL) | ||
78 | { | ||
79 | unsigned int len = (unsigned int)(pos - curfile); | ||
80 | len = len + 1 > sizeof(buffer) ? sizeof(buffer) - 1 : len; | ||
81 | |||
82 | if(len > 0) | ||
83 | memcpy(buffer, curfile, len); | ||
84 | |||
85 | buffer[len] = '/'; | ||
86 | buffer[len+1] = '\0'; | ||
87 | |||
88 | return buffer; | ||
89 | } | ||
90 | } | ||
91 | |||
92 | return NULL; | ||
93 | } | ||
94 | |||
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 @@ | |||
29 | #undef luai_jmpbuf | 29 | #undef luai_jmpbuf |
30 | 30 | ||
31 | #undef LUA_PATH_DEFAULT | 31 | #undef LUA_PATH_DEFAULT |
32 | #define LUA_PATH_DEFAULT "./?.lua;" VIEWERS_DIR"/?.lua;" | 32 | #define LUA_PATH_DEFAULT "$/?.lua;" VIEWERS_DIR"/?.lua;" |
33 | 33 | ||
34 | #ifndef SIMULATOR | 34 | #ifndef SIMULATOR |
35 | #include "../../codecs/lib/setjmp.h" | 35 | #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) | |||
782 | 782 | ||
783 | RB_WRAP(current_path) | 783 | RB_WRAP(current_path) |
784 | { | 784 | { |
785 | char buffer[MAX_PATH]; | 785 | const char *current_path = get_current_path(L, 1); |
786 | lua_Debug ar; | 786 | if(current_path != NULL) |
787 | |||
788 | if(lua_getstack(L, 1, &ar)) | ||
789 | { | 787 | { |
790 | /* Try determining the base path of the current Lua chunk | 788 | lua_pushstring(L, current_path); |
791 | and write it to dest. */ | 789 | return 1; |
792 | lua_getinfo(L, "S", &ar); | ||
793 | |||
794 | char* curfile = (char*) &ar.source[1]; | ||
795 | char* pos = rb->strrchr(curfile, '/'); | ||
796 | if(pos != NULL) | ||
797 | { | ||
798 | unsigned int len = (unsigned int)(pos - curfile); | ||
799 | len = len + 1 > sizeof(buffer) ? sizeof(buffer) - 1 : len; | ||
800 | |||
801 | if(len > 0) | ||
802 | memcpy(buffer, curfile, len); | ||
803 | |||
804 | buffer[len] = '/'; | ||
805 | buffer[len+1] = '\0'; | ||
806 | |||
807 | lua_pushstring(L, buffer); | ||
808 | return 1; | ||
809 | } | ||
810 | } | 790 | } |
811 | 791 | else | |
812 | return 0; | 792 | return 0; |
813 | } | 793 | } |
814 | 794 | ||
815 | #define R(NAME) {#NAME, rock_##NAME} | 795 | #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 @@ | |||
24 | 24 | ||
25 | #define LUA_ROCKLIBNAME "rb" | 25 | #define LUA_ROCKLIBNAME "rb" |
26 | LUALIB_API int (luaopen_rock) (lua_State *L); | 26 | LUALIB_API int (luaopen_rock) (lua_State *L); |
27 | bool get_cur_path(lua_State *L, char* dest, size_t dest_size); | 27 | const char* get_current_path(lua_State *L, int level); |
28 | 28 | ||
29 | #endif /* _ROCKLIB_H_ */ | 29 | #endif /* _ROCKLIB_H_ */ |
30 | 30 | ||