From 16702c9a599b106ae08b2e99cfb453464256ef60 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Wed, 20 Jun 2018 18:16:11 +0200 Subject: Change LUA package path at LUA interpreter load Set package.path lua variable use the CONTROL_PLUGIN_PATH as it could have to find external lua packages in those directories. Change-Id: Iebea763782cfc024c5fef1fdbefe8ed80dbc4dbb Signed-off-by: Romain Forlot --- ctl-lib/ctl-lua.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c index 78eb4ba..02487b5 100644 --- a/ctl-lib/ctl-lua.c +++ b/ctl-lib/ctl-lua.c @@ -34,6 +34,8 @@ #define LUA_FIST_ARG 2 // when using luaL_newlib callback receive libtable as 1st arg #define LUA_MSG_MAX_LENGTH 2048 #define JSON_ERROR (json_object*)-1 +#define LUA_PATH_VALUE "package.path = package.path .. ';?.lua;" +#define LUA_GLOB_PATTERN "/?.lua;" static lua_State* luaState; CtlPluginT *ctlPlugins = NULL; @@ -1273,6 +1275,7 @@ int LuaConfigExec(AFB_ApiT apiHandle) { int LuaConfigLoad(AFB_ApiT apiHandle) { static int luaLoaded = 0; + int i; //int err = 0; // Lua loads only once @@ -1294,6 +1297,36 @@ int LuaConfigLoad(AFB_ApiT apiHandle) { luaL_newlib(luaState, afbFunction); lua_setglobal(luaState, "AFB"); + // set package.path lua variable use the CONTROL_PLUGIN_PATH as it could + // have to find external lua packages in those directories + size_t base_len = strlen(LUA_PATH_VALUE); + size_t spath_len = strlen(CONTROL_PLUGIN_PATH); + + int token_nb = spath_len ? 1:0; + char *spath = strdup(CONTROL_PLUGIN_PATH); + char *sep = spath; + while((sep = strchr(sep, ':')) != NULL) { + token_nb++; + sep++; + } + + // token + the lua glob pattern which is 7 char length + size_t total_len = base_len + spath_len + token_nb * 7 + 1; + char *lua_str = malloc(total_len + 1); + strncpy(lua_str, LUA_PATH_VALUE, total_len); + for (i = 0; i < token_nb; i++) { + sep = strsep(&spath, ":"); + strncat(lua_str, sep, total_len - strlen(lua_str)); + strncat(lua_str, LUA_GLOB_PATTERN, total_len - strlen(lua_str)); + } + strncat(lua_str, "'", 2); + + if(luaL_dostring(luaState, lua_str)) + printf("Fail change package.path error=%s", lua_tostring(luaState, -1)); + + free(spath); + free(lua_str); + // initialise static magic for context #ifndef CTX_MAGIC CTX_MAGIC = CtlConfigMagicNew(); -- cgit 1.2.3-korg