From 686f0a1794863adc1fe4cb27e920081e9b20e94e Mon Sep 17 00:00:00 2001 From: "martin.holst@gmail.com" Date: Sat, 28 Sep 2013 20:07:28 +0000 Subject: [PATCH] Moved script libraries into lualibs/folder, and added a patch to include the folder in the LUA_PATH (a.k.a package.path). See http://proxmark.org/forum/viewtopic.php?id=1750 for more details --- client/{ => lualibs}/commands.lua | 0 client/{ => lualibs}/getopt.lua | 0 client/{ => lualibs}/mf_default_keys.lua | 0 client/{ => lualibs}/read14a.lua | 0 client/scripting.c | 30 ++++++++++++++++++++++++ 5 files changed, 30 insertions(+) rename client/{ => lualibs}/commands.lua (100%) rename client/{ => lualibs}/getopt.lua (100%) rename client/{ => lualibs}/mf_default_keys.lua (100%) rename client/{ => lualibs}/read14a.lua (100%) diff --git a/client/commands.lua b/client/lualibs/commands.lua similarity index 100% rename from client/commands.lua rename to client/lualibs/commands.lua diff --git a/client/getopt.lua b/client/lualibs/getopt.lua similarity index 100% rename from client/getopt.lua rename to client/lualibs/getopt.lua diff --git a/client/mf_default_keys.lua b/client/lualibs/mf_default_keys.lua similarity index 100% rename from client/mf_default_keys.lua rename to client/lualibs/mf_default_keys.lua diff --git a/client/read14a.lua b/client/lualibs/read14a.lua similarity index 100% rename from client/read14a.lua rename to client/lualibs/read14a.lua diff --git a/client/scripting.c b/client/scripting.c index 82d6ffc9..ae11d7ca 100644 --- a/client/scripting.c +++ b/client/scripting.c @@ -137,8 +137,33 @@ static int l_ukbhit(lua_State *L) return 1; } +/** + * @brief Sets the lua path to include "./lualibs/?.lua", in order for a script to be + * able to do "require('foobar')" if foobar.lua is within lualibs folder. + * Taken from http://stackoverflow.com/questions/4125971/setting-the-global-lua-path-variable-from-c-c + * @param L + * @param path + * @return + */ +int setLuaPath( lua_State* L, const char* path ) +{ + lua_getglobal( L, "package" ); + lua_getfield( L, -1, "path" ); // get field "path" from table at top of stack (-1) + const char* cur_path = lua_tostring( L, -1 ); // grab path string from top of stack + int requiredLength = strlen(cur_path)+ strlen(path)+10; //A few bytes too many, whatever we can afford it + char * buf = malloc(requiredLength); + snprintf(buf, requiredLength, "%s;%s", cur_path, path); + lua_pop( L, 1 ); // get rid of the string on the stack we just pushed on line 5 + lua_pushstring( L, buf ); // push the new one + lua_setfield( L, -2, "path" ); // set the field "path" in table at -2 with value at top of stack + lua_pop( L, 1 ); // get rid of package table from top of stack + return 0; // all done! +} + + int set_pm3_libraries(lua_State *L) { + static const luaL_Reg libs[] = { {"SendCommand", l_SendCommand}, {"WaitForResponseTimeout", l_WaitForResponseTimeout}, @@ -165,5 +190,10 @@ int set_pm3_libraries(lua_State *L) //-- remove the global environment table from the stack lua_pop(L, 1); + + //-- Last but not least, add to the LUA_PATH (package.path in lua) + // so we can load libraries from the ./lualib/ - directory + setLuaPath(L,"./lualibs/?.lua"); + return 1; } -- 2.39.5