aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2018-06-20 18:16:11 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2018-06-27 17:43:12 +0200
commit1ff524bc55b3ff10b7ab2a5f2cafbb41fce22949 (patch)
treeb713e86688158b2c633162c6bfef31e4fb3fddfd
parent738ae54a57f1df7191e075e9eb467d1fc76e4ad5 (diff)
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 <romain.forlot@iot.bzh>
-rw-r--r--ctl-lib/ctl-lua.c33
1 files changed, 33 insertions, 0 deletions
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();