From d22104c2a1beab2077848aea3c6a7aa63dbd1cd4 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Fri, 18 May 2018 00:46:39 +0200 Subject: str Change-Id: I6f240bf971b7b4d45dc6cab6a56ef6157d00fe8f --- ctl-lib/ctl-lua.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'ctl-lib/ctl-lua.c') diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c index 3abf330..449ae09 100644 --- a/ctl-lib/ctl-lua.c +++ b/ctl-lib/ctl-lua.c @@ -738,14 +738,13 @@ int LuaLoadScript(const char *luaScriptPath) { } static int LuaDoScript(json_object *queryJ, CtlSourceT *source) { - const char *uid = NULL, *func = NULL; - char luaScriptPath[CONTROL_MAXPATH_LEN]; - char *filename, *fullpath; + const char *uid = NULL, *func = NULL, *filename = NULL, *fullpath = NULL; + char *luaScriptPath = NULL; int index, err = 0; + size_t p_length = 0; json_object *argsJ = NULL; static json_object *luaScriptPathJ = NULL; - luaScriptPath[CONTROL_MAXPATH_LEN - 1] = '\0'; if (!queryJ) { return -1; @@ -758,15 +757,20 @@ static int LuaDoScript(json_object *queryJ, CtlSourceT *source) { "args", &argsJ); if (err) { + AFB_ApiError(source->api, "LUA-DOSCRIPT-SCAN: Miss something in JSON object uid|[spath]|action|[args]: %s", json_object_get_string(queryJ)); return -1; } - // search for filename=script in CONTROL_LUA_PATH + // search for filename=uid*.lua or fallback on bindername*.lua in current directory if (!luaScriptPathJ) { - strncpy(luaScriptPath, CONTROL_DOSCRIPT_PRE, CONTROL_MAXPATH_LEN - 1); - strncat(luaScriptPath, "-", CONTROL_MAXPATH_LEN - strlen(luaScriptPath) - 1); - strncat(luaScriptPath, uid, CONTROL_MAXPATH_LEN - strlen(luaScriptPath) - 1); - luaScriptPathJ = ScanForConfig(luaScriptPath, CTL_SCAN_RECURSIVE, luaScriptPath, ".lua"); + luaScriptPathJ = ScanForConfig(".", CTL_SCAN_RECURSIVE, uid, ".lua"); + if (!luaScriptPathJ) + luaScriptPathJ = ScanForConfig(".", CTL_SCAN_RECURSIVE, GetBinderName(), ".lua"); + } + + if(!luaScriptPathJ) { + AFB_ApiError(source->api, "LUA-DOSCRIPT-SCAN: No script found"); + return -1; } for (index = 0; index < json_object_array_length(luaScriptPathJ); index++) { @@ -775,11 +779,14 @@ static int LuaDoScript(json_object *queryJ, CtlSourceT *source) { err = wrap_json_unpack(entryJ, "{s:s, s:s !}", "fullpath", &fullpath, "filename", &filename); if (err) { AFB_ApiError(source->api, "LUA-DOSCRIPT-SCAN:HOOPs invalid config file path = %s", json_object_get_string(entryJ)); - return -2; + return -1; } // Ignoring other found script. Only take the first one. if (!index) { + p_length = strlen(fullpath) + 1 + strlen(filename); + luaScriptPath = malloc(p_length + 1); + strncpy(luaScriptPath, fullpath, CONTROL_MAXPATH_LEN - 1); strncat(luaScriptPath, "/", CONTROL_MAXPATH_LEN - strlen(luaScriptPath) - 1); strncat(luaScriptPath, filename, CONTROL_MAXPATH_LEN - strlen(luaScriptPath) - 1); @@ -800,7 +807,7 @@ static int LuaDoScript(json_object *queryJ, CtlSourceT *source) { } if (!func) { AFB_ApiError(source->api, "LUA-DOSCRIPT:FAIL to deduct funcname from %s", filename); - return -5; + return -1; } // load function (should exist in CONTROL_PATH_LUA @@ -809,7 +816,7 @@ static int LuaDoScript(json_object *queryJ, CtlSourceT *source) { // Push AFB client context on the stack LuaAfbSourceT *afbSource = LuaSourcePush(luaState, source); if (!afbSource) - return -6; + return -1; return 0; } -- cgit 1.2.3-korg