aboutsummaryrefslogtreecommitdiffstats
path: root/ctl-lib/ctl-lua.c
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2018-04-30 12:09:41 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2018-05-11 11:33:25 +0200
commit641001d1d02790dbadea7db9e7d9e1638fe8c9ef (patch)
tree7093fea14c8fb8130de205e5d5c32b4117192c4b /ctl-lib/ctl-lua.c
parent2fa16f981f6e5d86ac5938821e0c533786b60fc7 (diff)
Improve way to loading an action
Split and cleaner function that load from JSON object an object CtlActionT that could be used by the controller. Also use a new syntax that reduce the number of JSON fields to parse based on a URI syntax. Change-Id: Iabc447368ca56e3a4279fb728ba2f546e11c961b Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'ctl-lib/ctl-lua.c')
-rw-r--r--ctl-lib/ctl-lua.c108
1 files changed, 14 insertions, 94 deletions
diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c
index d5c2975..89c10b9 100644
--- a/ctl-lib/ctl-lua.c
+++ b/ctl-lib/ctl-lua.c
@@ -35,9 +35,8 @@
#define LUA_MSG_MAX_LENGTH 512
#define JSON_ERROR (json_object*)-1
-
-extern CtlLua2cFuncT *ctlLua2cFunc;
static lua_State* luaState;
+CtlPluginT *ctlPlugins = NULL;
#ifndef CTX_MAGIC
static int CTX_MAGIC;
@@ -740,18 +739,17 @@ PUBLIC int LuaCallFunc (CtlSourceT *source, CtlActionT *action, json_object *que
PUBLIC int luaLoadScript(const char *luaScriptPath)
{
- int err = luaL_loadfile(luaState, luaScriptPath);
- if (err) {
- AFB_ApiError(source->api, "LUA-DOSCRIPT HOOPs Error in LUA loading scripts=%s err=%s", luaScriptPath, lua_tostring(luaState,-1));
+ int err = 0;
+
+ if(!luaScriptPath)
+ return -1;
+
+ err = luaL_loadfile(luaState, luaScriptPath);
+ if (err)
return err;
- }
// Script was loaded we need to parse to make it executable
err = lua_pcall(luaState, 0, 0, 0);
- if (err) {
- AFB_ApiError(source->api, "LUA-DOSCRIPT:FAIL to load %s", luaScriptPath);
- return err;
- }
return err;
}
@@ -804,9 +802,11 @@ STATIC int LuaDoScript(json_object *queryJ, CtlSourceT *source)
}
}
- err = luaLoadScript(luaScriptPath);
- if(err)
+ err = LuaLoadScript(luaScriptPath);
+ if(err) {
+ AFB_ApiError(source->api, "LUA-DOSCRIPT HOOPs Error in LUA loading scripts=%s err=%s", luaScriptPath, lua_tostring(luaState,-1));
return err;
+ }
// if no func name given try to deduct from filename
if (!func && (func=(char*)GetMidleName(filename))!=NULL) {
@@ -1243,12 +1243,12 @@ OnErrorExit:
// Register a new L2c list of LUA user plugin commands
-PUBLIC void LuaL2cNewLib(luaL_Reg *l2cFunc, int count) {
+void LuaL2cNewLib(luaL_Reg *l2cFunc, int count, const char *prefix) {
// luaL_newlib(luaState, l2cFunc); macro does not work with pointer :(
luaL_checkversion(luaState);
lua_createtable(luaState, 0, count+1);
luaL_setfuncs(luaState,l2cFunc,0);
- lua_setglobal(luaState, "L2C");
+ lua_setglobal(luaState, prefix);
}
static const luaL_Reg afbFunction[] = {
@@ -1314,84 +1314,4 @@ PUBLIC int LuaConfigLoad (AFB_ApiT apiHandle) {
}
return 0;
-
- OnErrorExit:
- free(luaState);
- return 1;
-}
-
-// Create Binding Event at Init Exec Time
-PUBLIC int LuaConfigExec (AFB_ApiT apiHandle, const char* prefix) {
-
- int err, index;
-
- // create L2C mapping before any LUA script is loaded
- if (ctlLua2cFunc && ctlLua2cFunc->l2cCount) {
- LuaL2cNewLib (ctlLua2cFunc->l2cFunc, ctlLua2cFunc->l2cCount);
- }
-
- // search for default policy config files
- char fullprefix[CONTROL_MAXPATH_LEN] = "";
- if(prefix)
- strncpy (fullprefix, prefix, strlen(prefix)+1);
- else
- strncat (fullprefix, GetBinderName(), strlen(GetBinderName()));
-
- strncat (fullprefix, "-", strlen("-"));
-
- const char *dirList= getenv("CONTROL_LUA_PATH");
- //if (!dirList) dirList=CONTROL_LUA_PATH;
-
- // special case for no lua even when avaliable
- if (!strcasecmp ("/dev/null", dirList)) {
- return 0;
- }
-
- json_object *luaScriptPathJ = ScanForConfig(dirList , CTL_SCAN_RECURSIVE, fullprefix, "lua");
-
- // load+exec any file found in LUA search path
- if(luaScriptPathJ) {
- for (index=0; index < json_object_array_length(luaScriptPathJ); index++) {
- json_object *entryJ=json_object_array_get_idx(luaScriptPathJ, index);
-
- char *filename; char*fullpath;
- err= wrap_json_unpack (entryJ, "{s:s, s:s !}", "fullpath", &fullpath,"filename", &filename);
- if (err) {
- AFB_ApiError(apiHandle, "LUA-INIT HOOPs invalid config file path = %s", json_object_get_string(entryJ));
- goto OnErrorExit;
- }
-
- char filepath[CONTROL_MAXPATH_LEN];
- strncpy(filepath, fullpath, strlen(fullpath)+1);
- strncat(filepath, "/", strlen("/"));
- strncat(filepath, filename, strlen(filename));
- err= luaL_loadfile(luaState, filepath);
- if (err) {
- AFB_ApiError(apiHandle, "LUA-LOAD HOOPs Error in LUA loading scripts=%s err=%s", filepath, lua_tostring(luaState,-1));
- goto OnErrorExit;
- }
-
- // exec/compil script
- err = lua_pcall(luaState, 0, 0, 0);
- if (err) {
- AFB_ApiError(apiHandle, "LUA-LOAD HOOPs Error in LUA exec scripts=%s err=%s", filepath, lua_tostring(luaState,-1));
- goto OnErrorExit;
- } else {
- AFB_ApiNotice(apiHandle, "LUA-LOAD '%s'", filepath);
- }
- }
-
- json_object_put(luaScriptPathJ);
- // no policy config found remove control API from binder
- if (index == 0) {
- AFB_ApiWarning (apiHandle, "POLICY-INIT:WARNING (setenv CONTROL_LUA_PATH) No LUA '%s*.lua' in '%s'", fullprefix, dirList);
- }
- }
- else AFB_ApiWarning (apiHandle, "POLICY-INIT:WARNING (setenv CONTROL_LUA_PATH) No LUA '%s*.lua' in '%s'", fullprefix, dirList);
-
- AFB_ApiDebug (apiHandle, "Control: LUA Init Done");
- return 0;
-
- OnErrorExit:
- return 1;
}