diff options
Diffstat (limited to 'ctl-lib')
-rw-r--r-- | ctl-lib/ctl-action.c | 23 | ||||
-rw-r--r-- | ctl-lib/ctl-config.c | 45 | ||||
-rw-r--r-- | ctl-lib/ctl-config.h | 38 | ||||
-rw-r--r-- | ctl-lib/ctl-control.c | 8 | ||||
-rw-r--r-- | ctl-lib/ctl-event.c | 12 | ||||
-rw-r--r-- | ctl-lib/ctl-lua.c | 324 | ||||
-rw-r--r-- | ctl-lib/ctl-lua.h | 20 | ||||
-rw-r--r-- | ctl-lib/ctl-onload.c | 10 | ||||
-rw-r--r-- | ctl-lib/ctl-plugin.c | 67 | ||||
-rw-r--r-- | ctl-lib/ctl-plugin.h | 8 | ||||
-rw-r--r-- | ctl-lib/ctl-timer.c | 17 | ||||
-rw-r--r-- | ctl-lib/ctl-timer.h | 6 |
12 files changed, 259 insertions, 319 deletions
diff --git a/ctl-lib/ctl-action.c b/ctl-lib/ctl-action.c index 8459449..ba86f37 100644 --- a/ctl-lib/ctl-action.c +++ b/ctl-lib/ctl-action.c @@ -24,14 +24,14 @@ #include "ctl-config.h" -PUBLIC int ActionLabelToIndex(CtlActionT*actions, const char* actionLabel) { +int ActionLabelToIndex(CtlActionT*actions, const char* actionLabel) { for (int idx = 0; actions[idx].uid; idx++) { if (!strcasecmp(actionLabel, actions[idx].uid)) return idx; } return -1; } -PUBLIC void ActionExecUID(AFB_ReqT request, CtlConfigT *ctlConfig, const char *uid, json_object *queryJ) +void ActionExecUID(AFB_ReqT request, CtlConfigT *ctlConfig, const char *uid, json_object *queryJ) { for(int i=0; ctlConfig->sections[i].key != NULL; i++) { @@ -53,7 +53,7 @@ PUBLIC void ActionExecUID(AFB_ReqT request, CtlConfigT *ctlConfig, const char *u } } -PUBLIC void ActionExecOne(CtlSourceT *source, CtlActionT* action, json_object *queryJ) { +void ActionExecOne(CtlSourceT *source, CtlActionT* action, json_object *queryJ) { int err = 0; switch (action->type) { @@ -120,7 +120,7 @@ PUBLIC void ActionExecOne(CtlSourceT *source, CtlActionT* action, json_object *q // Direct Request Call in APIV3 #ifdef AFB_BINDING_PREV3 -STATIC void ActionDynRequest (AFB_ReqT request) { +static void ActionDynRequest (AFB_ReqT request) { // retrieve action handle from request and execute the request json_object *queryJ = afb_request_json(request); @@ -249,7 +249,7 @@ static int BuildOneAction(AFB_ApiT apiHandle, CtlActionT *action, const char *ur } // unpack individual action object -PUBLIC int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *actionJ, int exportApi) { +int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *actionJ, int exportApi) { int err = 0; const char *uri = NULL, *function = NULL; @@ -264,7 +264,7 @@ PUBLIC int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *ac err = afb_dynapi_add_verb(apiHandle, action->uid, action->info, ActionDynRequest, action, NULL, 0); if (err) { AFB_ApiError(apiHandle,"ACTION-LOAD-ONE fail to register API verb=%s", action->uid); - goto OnErrorExit; + return NULL; } action->api = apiHandle; } @@ -294,7 +294,7 @@ PUBLIC int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *ac return err; } -PUBLIC CtlActionT *ActionConfig(AFB_ApiT apiHandle, json_object *actionsJ, int exportApi) { +CtlActionT *ActionConfig(AFB_ApiT apiHandle, json_object *actionsJ, int exportApi) { int err; CtlActionT *actions; @@ -307,17 +307,16 @@ PUBLIC CtlActionT *ActionConfig(AFB_ApiT apiHandle, json_object *actionsJ, int e json_object *actionJ = json_object_array_get_idx(actionsJ, idx); err = ActionLoadOne(apiHandle, &actions[idx], actionJ, exportApi); - if (err) goto OnErrorExit; + if (err) + return NULL; } } else { actions = calloc(2, sizeof (CtlActionT)); err = ActionLoadOne(apiHandle, &actions[0], actionsJ, exportApi); - if (err) goto OnErrorExit; + if (err) + return NULL; } return actions; - -OnErrorExit: - return NULL; } diff --git a/ctl-lib/ctl-config.c b/ctl-lib/ctl-config.c index b811754..8dc06fa 100644 --- a/ctl-lib/ctl-config.c +++ b/ctl-lib/ctl-config.c @@ -23,14 +23,13 @@ #include <string.h> #include <sys/time.h> - #include "filescan-utils.h" #include "ctl-config.h" // Load control config file -PUBLIC int CtlConfigMagicNew() { +int CtlConfigMagicNew() { static int InitRandomDone=0; if (!InitRandomDone) { @@ -43,7 +42,7 @@ PUBLIC int CtlConfigMagicNew() { return ((long)rand()); } -PUBLIC json_object* CtlConfigScan(const char *dirList, const char *prefix) { + json_object* CtlConfigScan(const char *dirList, const char *prefix) { char controlFile [CONTROL_MAXPATH_LEN]; strncpy(controlFile, prefix, strlen(prefix)+1); strncat(controlFile, GetBinderName(), strlen(GetBinderName())); @@ -54,7 +53,7 @@ PUBLIC json_object* CtlConfigScan(const char *dirList, const char *prefix) { return responseJ; } -PUBLIC char* ConfigSearch(AFB_ApiT apiHandle, json_object *responseJ) { +char* ConfigSearch(AFB_ApiT apiHandle, json_object *responseJ) { // We load 1st file others are just warnings char filepath[CONTROL_MAXPATH_LEN]; for (int index = 0; index < json_object_array_length(responseJ); index++) { @@ -78,7 +77,7 @@ PUBLIC char* ConfigSearch(AFB_ApiT apiHandle, json_object *responseJ) { return strndup(filepath, sizeof(filepath)); } -PUBLIC char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char *prefix) { +char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char *prefix) { // search for default dispatch config file json_object* responseJ = CtlConfigScan (dirList, prefix); @@ -87,7 +86,7 @@ PUBLIC char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char return NULL; } -PUBLIC int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) { +int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) { // best effort to initialise everything before starting if (ctlConfig->requireJ) { @@ -118,13 +117,11 @@ PUBLIC int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) { else errcount += ctlConfig->sections[idx].loadCB(apiHandle, &ctlConfig->sections[idx], NULL); } - return errcount; -OnErrorExit: - return 1; + return errcount; } -PUBLIC CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle, const char* filepath) { +CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle, const char* filepath) { json_object *ctlConfigJ; CtlConfigT *ctlHandle=NULL; int err; @@ -133,7 +130,7 @@ PUBLIC CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle, const char* filepath) { ctlConfigJ = json_object_from_file(filepath); if (!ctlConfigJ) { AFB_ApiError(apiHandle, "CTL-LOAD-CONFIG Not invalid JSON %s ", filepath); - goto OnErrorExit; + return NULL; } AFB_ApiInfo(apiHandle, "CTL-LOAD-CONFIG: loading config filepath=%s", filepath); @@ -142,20 +139,21 @@ PUBLIC CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle, const char* filepath) { int done = json_object_object_get_ex(ctlConfigJ, "metadata", &metadataJ); if (done) { ctlHandle = calloc(1, sizeof (CtlConfigT)); - err = wrap_json_unpack(metadataJ, "{ss,ss,ss,s?s,s?o !}", "uid", &ctlHandle->uid, "version", &ctlHandle->version - , "api", &ctlHandle->api, "info", &ctlHandle->info, "require", &ctlHandle->requireJ); + err = wrap_json_unpack(metadataJ, "{ss,ss,ss,s?s,s?o !}", + "uid", &ctlHandle->uid, + "version", &ctlHandle->version, + "api", &ctlHandle->api, + "info", &ctlHandle->info, + "require", &ctlHandle->requireJ); if (err) { AFB_ApiError(apiHandle, "CTL-LOAD-CONFIG:METADATA Missing something uid|api|version|[info]|[require] in:\n-- %s", json_object_get_string(metadataJ)); - goto OnErrorExit; + free(ctlHandle); + return NULL; } } ctlHandle->configJ = ctlConfigJ; return ctlHandle; - -OnErrorExit: - free(ctlHandle); - return NULL; } void wrap_json_array_add(void* array, json_object *val) { @@ -255,12 +253,13 @@ json_object* LoadAdditionalsFiles(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, con return sectionJ; } -PUBLIC int CtlLoadSections(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, CtlSectionT *sections) { +int CtlLoadSections(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, CtlSectionT *sections) { int err; #ifdef CONTROL_SUPPORT_LUA err= LuaConfigLoad(apiHandle); - if (err) goto OnErrorExit; + if (err) + return 1; #endif err = 0; @@ -273,10 +272,8 @@ PUBLIC int CtlLoadSections(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, CtlSection err += sections[idx].loadCB(apiHandle, §ions[idx], updatedSectionJ); } } - if (err) goto OnErrorExit; + if (err) + return 1; return 0; - -OnErrorExit: - return 1; } diff --git a/ctl-lib/ctl-config.h b/ctl-lib/ctl-config.h index 8a7f2a3..8ae1888 100644 --- a/ctl-lib/ctl-config.h +++ b/ctl-lib/ctl-config.h @@ -85,38 +85,38 @@ typedef enum { } SectionEnumT; // ctl-action.c -PUBLIC CtlActionT *ActionConfig(AFB_ApiT apiHandle, json_object *actionsJ, int exportApi); -PUBLIC void ActionExecUID(AFB_ReqT request, CtlConfigT *ctlConfig, const char *uid, json_object *queryJ); -PUBLIC void ActionExecOne( CtlSourceT *source, CtlActionT* action, json_object *queryJ); -PUBLIC int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *, int exportApi); -PUBLIC int ActionLabelToIndex(CtlActionT* actions, const char* actionLabel); +CtlActionT *ActionConfig(AFB_ApiT apiHandle, json_object *actionsJ, int exportApi); +void ActionExecUID(AFB_ReqT request, CtlConfigT *ctlConfig, const char *uid, json_object *queryJ); +void ActionExecOne( CtlSourceT *source, CtlActionT* action, json_object *queryJ); +int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *, int exportApi); +int ActionLabelToIndex(CtlActionT* actions, const char* actionLabel); // ctl-config.c -PUBLIC int CtlConfigMagicNew(); -PUBLIC json_object* CtlConfigScan(const char *dirList, const char *prefix) ; -PUBLIC char* ConfigSearch(AFB_ApiT apiHandle, json_object *responseJ); -PUBLIC char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char *prefix) ; -PUBLIC int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) ; -PUBLIC CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle,const char* filepath) ; -PUBLIC int CtlLoadSections(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, CtlSectionT *sections); +int CtlConfigMagicNew(); +json_object* CtlConfigScan(const char *dirList, const char *prefix) ; +char* ConfigSearch(AFB_ApiT apiHandle, json_object *responseJ); +char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char *prefix) ; +int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) ; +CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle,const char* filepath) ; +int CtlLoadSections(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, CtlSectionT *sections); // ctl-event.c -PUBLIC int EventConfig(AFB_ApiT apihandle, CtlSectionT *section, json_object *actionsJ); +int EventConfig(AFB_ApiT apihandle, CtlSectionT *section, json_object *actionsJ); #ifdef AFB_BINDING_PREV3 -PUBLIC void CtrlDispatchApiEvent (AFB_ApiT apiHandle, const char *evtLabel, struct json_object *eventJ); +void CtrlDispatchApiEvent (AFB_ApiT apiHandle, const char *evtLabel, struct json_object *eventJ); #else -PUBLIC void CtrlDispatchV2Event(const char *evtLabel, json_object *eventJ); +void CtrlDispatchV2Event(const char *evtLabel, json_object *eventJ); #endif // ctl-control.c -PUBLIC int ControlConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ); +int ControlConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ); // ctl-onload.c -PUBLIC int OnloadConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ); +int OnloadConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ); // ctl-plugin.c -PUBLIC int PluginConfig(AFB_ApiT UNUSED_ARG(apiHandle), CtlSectionT *section, json_object *pluginsJ); -PUBLIC int PluginGetCB (AFB_ApiT apiHandle, CtlActionT *action , json_object *callbackJ); +int PluginConfig(AFB_ApiT UNUSED_ARG(apiHandle), CtlSectionT *section, json_object *pluginsJ); +int PluginGetCB (AFB_ApiT apiHandle, CtlActionT *action , json_object *callbackJ); #ifdef __cplusplus } diff --git a/ctl-lib/ctl-control.c b/ctl-lib/ctl-control.c index 914b51f..0605d6b 100644 --- a/ctl-lib/ctl-control.c +++ b/ctl-lib/ctl-control.c @@ -23,7 +23,7 @@ #include "ctl-config.h" // onload section receive one action or an array of actions -PUBLIC int ControlConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ) { +int ControlConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ) { // Load time parse actions in config file if (actionsJ != NULL) { @@ -31,12 +31,8 @@ PUBLIC int ControlConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object * if (!section->actions) { AFB_ApiError (apiHandle, "ControlLoad config fail processing onload actions"); - goto OnErrorExit; + return 1; } } return 0; - -OnErrorExit: - return 1; - } diff --git a/ctl-lib/ctl-event.c b/ctl-lib/ctl-event.c index 5be8b24..08cc04f 100644 --- a/ctl-lib/ctl-event.c +++ b/ctl-lib/ctl-event.c @@ -25,7 +25,7 @@ // Event dynamic API-V3 mode #ifdef AFB_BINDING_PREV3 -PUBLIC void CtrlDispatchApiEvent (AFB_ApiT apiHandle, const char *evtLabel, struct json_object *eventJ) { +void CtrlDispatchApiEvent (AFB_ApiT apiHandle, const char *evtLabel, struct json_object *eventJ) { AFB_ApiNotice (apiHandle, "Received event=%s, query=%s", evtLabel, json_object_get_string(eventJ)); // retrieve section config from api handle @@ -54,7 +54,7 @@ PUBLIC void CtrlDispatchApiEvent (AFB_ApiT apiHandle, const char *evtLabel, stru extern CtlConfigT *ctrlConfig; // call action attached to even name if any -PUBLIC void CtrlDispatchV2Event(const char *evtLabel, json_object *eventJ) { +void CtrlDispatchV2Event(const char *evtLabel, json_object *eventJ) { CtlActionT* actions = ctrlConfig->sections[CTL_SECTION_EVENT].actions; int index= ActionLabelToIndex(actions, evtLabel); @@ -74,7 +74,7 @@ PUBLIC void CtrlDispatchV2Event(const char *evtLabel, json_object *eventJ) { #endif // onload section receive one action or an array of actions -PUBLIC int EventConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ) { +int EventConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ) { // Load time parse actions in config file if (actionsJ != NULL) { @@ -82,13 +82,9 @@ PUBLIC int EventConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *ac if (!section->actions) { AFB_ApiError (apiHandle, "EventLoad config fail processing onload actions"); - goto OnErrorExit; + return 1; } } return 0; - -OnErrorExit: - return 1; - } diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c index 89c10b9..6bbfe58 100644 --- a/ctl-lib/ctl-lua.c +++ b/ctl-lib/ctl-lua.c @@ -69,7 +69,7 @@ typedef struct { * handle while waiting for someone smarter than me to find a better solution * https://stackoverflow.com/questions/45596493/lua-using-lua-newuserdata-from-lua-pcall */ -STATIC CtlSourceT *LuaSourcePop (lua_State *luaState, int index) { +static CtlSourceT *LuaSourcePop (lua_State *luaState, int index) { LuaAfbSourceT *afbSource; luaL_checktype(luaState, index, LUA_TLIGHTUSERDATA); afbSource = (LuaAfbSourceT *) lua_touserdata(luaState, index); @@ -80,7 +80,7 @@ STATIC CtlSourceT *LuaSourcePop (lua_State *luaState, int index) { return afbSource->source; } -STATIC LuaAfbSourceT *LuaSourcePush (lua_State *luaState, CtlSourceT *source) { +static LuaAfbSourceT *LuaSourcePush (lua_State *luaState, CtlSourceT *source) { LuaAfbSourceT *afbSource = (LuaAfbSourceT *)calloc(1, sizeof(LuaAfbSourceT)); if (!afbSource) { AFB_ApiError(source->api, "LuaSourcePush fail to allocate user data context"); @@ -94,7 +94,7 @@ STATIC LuaAfbSourceT *LuaSourcePush (lua_State *luaState, CtlSourceT *source) { } // Push a json structure on the stack as a LUA table -STATIC int LuaPushArgument (CtlSourceT *source, json_object *argsJ) { +static int LuaPushArgument (CtlSourceT *source, json_object *argsJ) { //AFB_NOTICE("LuaPushArgument argsJ=%s", json_object_get_string(argsJ)); @@ -144,12 +144,12 @@ STATIC int LuaPushArgument (CtlSourceT *source, json_object *argsJ) { return 1; } -STATIC json_object *LuaPopOneArg (CtlSourceT *source, lua_State* luaState, int idx); +static json_object *LuaPopOneArg (CtlSourceT *source, lua_State* luaState, int idx); // Move a table from Internal Lua representation to Json one // Numeric table are transformed in json array, string one in object // Mix numeric/string key are not supported -STATIC json_object *LuaTableToJson (CtlSourceT *source, lua_State* luaState, int index) { +static json_object *LuaTableToJson (CtlSourceT *source, lua_State* luaState, int index) { #define LUA_KEY_INDEX -2 #define LUA_VALUE_INDEX -1 @@ -201,7 +201,7 @@ STATIC json_object *LuaTableToJson (CtlSourceT *source, lua_State* luaState, int return tableJ; } -STATIC json_object *LuaPopOneArg (CtlSourceT *source, lua_State* luaState, int idx) { +static json_object *LuaPopOneArg (CtlSourceT *source, lua_State* luaState, int idx) { json_object *value=NULL; int luaType = lua_type(luaState, idx); @@ -254,25 +254,23 @@ static json_object *LuaPopArgs (CtlSourceT *source, lua_State* luaState, int sta responseJ= json_object_new_array(); for (int idx=start; idx <= stop; idx++) { json_object *argJ=LuaPopOneArg (source, luaState, idx); - if (!argJ) goto OnErrorExit; + if (!argJ) + return NULL; json_object_array_add(responseJ, argJ); } } return responseJ; - - OnErrorExit: - return NULL; } -STATIC int LuaFormatMessage(lua_State* luaState, int verbosity, int level) { +static int LuaFormatMessage(lua_State* luaState, int verbosity, int level) { char *message; CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); - if (!source) goto OnErrorExit; -\ + if (!source) + return 1; // if log level low then silently ignore message #ifdef AFB_BINDING_PREV3 @@ -285,7 +283,7 @@ STATIC int LuaFormatMessage(lua_State* luaState, int verbosity, int level) { if (!responseJ) { luaL_error(luaState,"LuaFormatMessage empty message"); - goto OnErrorExit; + return 1; } // if we have only on argument just return the value. @@ -353,41 +351,40 @@ PrintMessage: AFB_ApiVerbose(source->api, level,__FILE__,__LINE__,source->uid, "%s", message); json_object_put(responseJ); return 0; // nothing return to lua - - OnErrorExit: // on argument to return (the error message) - json_object_put(responseJ); - return 1; } -STATIC int LuaPrintInfo(lua_State* luaState) { +static int LuaPrintInfo(lua_State* luaState) { int err=LuaFormatMessage (luaState, AFB_VERBOSITY_LEVEL_INFO, _AFB_SYSLOG_LEVEL_INFO_); return err; } -STATIC int LuaPrintError(lua_State* luaState) { +static int LuaPrintError(lua_State* luaState) { int err=LuaFormatMessage (luaState, AFB_VERBOSITY_LEVEL_ERROR, _AFB_SYSLOG_LEVEL_ERROR_); return err; // no value return } -STATIC int LuaPrintWarning(lua_State* luaState) { +static int LuaPrintWarning(lua_State* luaState) { int err=LuaFormatMessage (luaState, AFB_VERBOSITY_LEVEL_WARNING, _AFB_SYSLOG_LEVEL_WARNING_); return err; } -STATIC int LuaPrintNotice(lua_State* luaState) { +static int LuaPrintNotice(lua_State* luaState) { int err=LuaFormatMessage (luaState, AFB_VERBOSITY_LEVEL_NOTICE, _AFB_SYSLOG_LEVEL_NOTICE_); return err; } -STATIC int LuaPrintDebug(lua_State* luaState) { +static int LuaPrintDebug(lua_State* luaState) { int err=LuaFormatMessage (luaState, AFB_VERBOSITY_LEVEL_DEBUG, _AFB_SYSLOG_LEVEL_DEBUG_); return err; } -STATIC int LuaAfbSuccess(lua_State* luaState) { +static int LuaAfbSuccess(lua_State* luaState) { CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); - if (!source) goto OnErrorExit; + if (!source) { + lua_error(luaState); + return 1; + } // ignore context argument json_object *responseJ= LuaPopArgs(source, luaState, LUA_FIST_ARG+1); @@ -397,15 +394,14 @@ STATIC int LuaAfbSuccess(lua_State* luaState) { json_object_put(responseJ); return 0; - - OnErrorExit: - lua_error(luaState); - return 1; } -STATIC int LuaAfbFail(lua_State* luaState) { +static int LuaAfbFail(lua_State* luaState) { CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); - if (!source) goto OnErrorExit; + if (!source) { + lua_error(luaState); + return 1; + } json_object *responseJ= LuaPopArgs(source, luaState, LUA_FIST_ARG+1); if (responseJ == JSON_ERROR) return 1; @@ -414,16 +410,12 @@ STATIC int LuaAfbFail(lua_State* luaState) { json_object_put(responseJ); return 0; - - OnErrorExit: - lua_error(luaState); - return 1; } #ifdef AFB_BINDING_PREV3 -STATIC void LuaAfbServiceCB(void *handle, int iserror, struct json_object *responseJ, AFB_ApiT apiHandle) { +static void LuaAfbServiceCB(void *handle, int iserror, struct json_object *responseJ, AFB_ApiT apiHandle) { #else -STATIC void LuaAfbServiceCB(void *handle, int iserror, struct json_object *responseJ) { +static void LuaAfbServiceCB(void *handle, int iserror, struct json_object *responseJ) { #endif LuaCbHandleT *handleCb= (LuaCbHandleT*)handle; int count=1; @@ -449,19 +441,21 @@ STATIC void LuaAfbServiceCB(void *handle, int iserror, struct json_object *respo } -STATIC int LuaAfbService(lua_State* luaState) { +static int LuaAfbService(lua_State* luaState) { int count = lua_gettop(luaState); CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); if (!source) { lua_pushliteral (luaState, "LuaAfbService-Fail Invalid request handle"); - goto OnErrorExit; + lua_error(luaState); + return 1; } // note: argument start at 2 because of AFB: table if (count <6 || !lua_isstring(luaState, 3) || !lua_isstring(luaState, 4) || !lua_isstring(luaState, 6)) { lua_pushliteral (luaState, "ERROR: syntax AFB:service(source, api, verb, {[Lua Table]})"); - goto OnErrorExit; + lua_error(luaState); + return 1; } // get api/verb+query @@ -481,26 +475,24 @@ STATIC int LuaAfbService(lua_State* luaState) { AFB_ServiceCall(source->api, api, verb, queryJ, LuaAfbServiceCB, handleCb); return 0; // no value return - - OnErrorExit: - lua_error(luaState); - return 1; } -STATIC int LuaAfbServiceSync(lua_State* luaState) { +static int LuaAfbServiceSync(lua_State* luaState) { int count = lua_gettop(luaState); json_object *responseJ; CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); if (!source) { lua_pushliteral (luaState, "LuaAfbServiceSync-Fail Invalid request handle"); - goto OnErrorExit; + lua_error(luaState); + return 1; } // note: argument start at 2 because of AFB: table if (count != 5 || !lua_isstring(luaState, LUA_FIST_ARG+1) || !lua_isstring(luaState, LUA_FIST_ARG+2) || !lua_istable(luaState, LUA_FIST_ARG+3)) { lua_pushliteral (luaState, "ERROR: syntax AFB:servsync(api, verb, {[Lua Table]})"); - goto OnErrorExit; + lua_error(luaState); + return 1; } @@ -516,103 +508,102 @@ STATIC int LuaAfbServiceSync(lua_State* luaState) { count+= LuaPushArgument(source, responseJ); return count; // return count values - - OnErrorExit: - lua_error(luaState); - return 1; } -STATIC int LuaAfbEventPush(lua_State* luaState) { +static int LuaAfbEventPush(lua_State* luaState) { LuaAfbEvent *afbevt; CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); if (!source) { lua_pushliteral (luaState, "LuaAfbEventSubscribe-Fail Invalid request handle"); - goto OnErrorExit; + lua_error(luaState); + return 1; } // if no private event handle then use default binding event if (!lua_islightuserdata(luaState, LUA_FIST_ARG+1)) { lua_pushliteral (luaState, "LuaAfbMakePush-Fail missing event handle"); - goto OnErrorExit; + lua_error(luaState); + return 1; } afbevt = (LuaAfbEvent*) lua_touserdata(luaState, LUA_FIST_ARG+1); if (!AFB_EventIsValid(afbevt->event)) { lua_pushliteral (luaState, "LuaAfbMakePush-Fail invalid event"); - goto OnErrorExit; + lua_error(luaState); + return 1; } json_object *ctlEventJ= LuaTableToJson(source, luaState, LUA_FIST_ARG+2); if (!ctlEventJ) { lua_pushliteral (luaState, "LuaAfbEventPush-Syntax is AFB:signal ([evtHandle], {lua table})"); - goto OnErrorExit; + lua_error(luaState); + return 1; } int done = AFB_EventPush(afbevt->event, ctlEventJ); if (!done) { lua_pushliteral (luaState, "LuaAfbEventPush-Fail No Subscriber to event"); AFB_ApiError(source->api, "LuaAfbEventPush-Fail name subscriber event=%s count=%d", afbevt->name, afbevt->count); - goto OnErrorExit; + lua_error(luaState); + return 1; } afbevt->count++; return 0; - -OnErrorExit: - lua_error(luaState); - return 1; } -STATIC int LuaAfbEventSubscribe(lua_State* luaState) { +static int LuaAfbEventSubscribe(lua_State* luaState) { LuaAfbEvent *afbevt; CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); if (!source) { lua_pushliteral (luaState, "LuaAfbEventSubscribe-Fail Invalid request handle"); - goto OnErrorExit; + lua_error(luaState); + return 1; } // if no private event handle then use default binding event if (!lua_islightuserdata(luaState, LUA_FIST_ARG+1)) { lua_pushliteral (luaState, "LuaAfbMakePush-Fail missing event handle"); - goto OnErrorExit; + lua_error(luaState); + return 1; } afbevt = (LuaAfbEvent*) lua_touserdata(luaState, LUA_FIST_ARG+1); if (!AFB_EventIsValid(afbevt->event)) { lua_pushliteral (luaState, "LuaAfbMakePush-Fail invalid event handle"); - goto OnErrorExit; + lua_error(luaState); + return 1; } int err = AFB_ReqSubscribe(source->request, afbevt->event); if (err) { lua_pushliteral (luaState, "LuaAfbEventSubscribe-Fail No Subscriber to event"); AFB_ApiError(source->api, "LuaAfbEventPush-Fail name subscriber event=%s count=%d", afbevt->name, afbevt->count); - goto OnErrorExit; + lua_error(luaState); + return 1; } afbevt->count++; return 0; - - OnErrorExit: - lua_error(luaState); - return 1; } -STATIC int LuaAfbEventMake(lua_State* luaState) { +static int LuaAfbEventMake(lua_State* luaState) { int count = lua_gettop(luaState); LuaAfbEvent *afbevt=calloc(1,sizeof(LuaAfbEvent)); CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); if (!source) { lua_pushliteral (luaState, "LuaAfbEventMake-Fail Invalid request handle"); - goto OnErrorExit; + lua_error(luaState); + return 1; } if (count != LUA_FIST_ARG+1 || !lua_isstring(luaState, LUA_FIST_ARG+1)) { lua_pushliteral (luaState, "LuaAfbEventMake-Syntax is evtHandle= AFB:event ('myEventName')"); - goto OnErrorExit; + lua_error(luaState); + return 1; } // event name should be the only argument @@ -623,63 +614,51 @@ STATIC int LuaAfbEventMake(lua_State* luaState) { if (!AFB_EventIsValid(afbevt->event)) { AFB_ApiError (source->api,"Fail to CreateEvent evtname=%s", afbevt->name); lua_pushliteral (luaState, "LuaAfbEventMake-Fail to Create Binder event"); - goto OnErrorExit; + lua_error(luaState); + return 1; } // push event handler as a LUA opaque handle lua_pushlightuserdata(luaState, afbevt); return 1; - - OnErrorExit: - lua_error(luaState); - return 1; } -STATIC int LuaAfbGetUid (lua_State* luaState) { +static int LuaAfbGetUid (lua_State* luaState) { CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); if (!source) { lua_pushliteral (luaState, "LuaAfbEventSubscribe-Fail Invalid request handle"); - goto OnErrorExit; + return 0; } // extract and return afbSource from timer handle lua_pushstring(luaState, source->uid); return 1; // return argument - -OnErrorExit: - return 0; } -STATIC int LuaAfbGetStatus (lua_State* luaState) { +static int LuaAfbGetStatus (lua_State* luaState) { CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); if (!source) { lua_pushliteral (luaState, "LuaAfbEventSubscribe-Fail Invalid request handle"); - goto OnErrorExit; + return 0; } // extract and return afbSource from timer handle lua_pushinteger(luaState, source->status); return 1; // return argument - -OnErrorExit: - return 0; } // Function call from LUA when lua2c plugin L2C is used -// Rfor: Not using LUA_FIRST_ARG here because we didn't use -// luaL_newlib function, so first args is on stack at first -// position. -PUBLIC int Lua2cWrapper(void* luaHandle, char *funcname, Lua2cFunctionT callback) { +int Lua2cWrapper(void* luaHandle, char *funcname, Lua2cFunctionT callback) { lua_State* luaState = (lua_State*)luaHandle; json_object *responseJ=NULL; - CtlSourceT *source= LuaSourcePop(luaState, 1); + CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); - json_object *argsJ= LuaPopArgs(source, luaState, 2); + json_object *argsJ= LuaPopArgs(source, luaState, LUA_FIST_ARG+1); int err= (*callback) (source, argsJ, &responseJ); // push error code and eventual response to LUA @@ -691,9 +670,9 @@ PUBLIC int Lua2cWrapper(void* luaHandle, char *funcname, Lua2cFunctionT callback } // Call a Lua function from a control action -PUBLIC int LuaCallFunc (CtlSourceT *source, CtlActionT *action, json_object *queryJ) { +int LuaCallFunc (CtlSourceT *source, CtlActionT *action, json_object *queryJ) { - int err, count; + int err = 0, count = 1; json_object* argsJ = action->argsJ; const char* func = action->exec.lua.funcname; @@ -702,16 +681,17 @@ PUBLIC int LuaCallFunc (CtlSourceT *source, CtlActionT *action, json_object *que lua_getglobal(luaState, func); // push source on the stack - count=1; // Push AFB client context on the stack - LuaAfbSourceT *afbSource= LuaSourcePush(luaState, source); - if (!afbSource) goto OnErrorExit; + LuaAfbSourceT *afbSource = LuaSourcePush(luaState, source); + if (!afbSource) + return -1; // push argsJ on the stack if (!argsJ) { - lua_pushnil(luaState); count++; + lua_pushnil(luaState); + count++; } else { - count+= LuaPushArgument (source, argsJ); + count += LuaPushArgument(source, argsJ); } // push queryJ on the stack @@ -719,25 +699,22 @@ PUBLIC int LuaCallFunc (CtlSourceT *source, CtlActionT *action, json_object *que lua_pushnil(luaState); count++; } else { - count+= LuaPushArgument (source, queryJ); + count += LuaPushArgument(source, queryJ); } // effectively exec LUA script code - err=lua_pcall(luaState, count, 1, 0); + err = lua_pcall(luaState, count, 1, 0); if (err) { AFB_ApiError(source->api, "LuaCallFunc Fail calling %s error=%s", func, lua_tostring(luaState,-1)); - goto OnErrorExit; + return -1; } // return LUA script value int rc= (int)lua_tointeger(luaState, -1); return rc; - - OnErrorExit: - return -1; } -PUBLIC int luaLoadScript(const char *luaScriptPath) +int LuaLoadScript(const char *luaScriptPath) { int err = 0; @@ -754,7 +731,7 @@ PUBLIC int luaLoadScript(const char *luaScriptPath) return err; } -STATIC int LuaDoScript(json_object *queryJ, CtlSourceT *source) +static int LuaDoScript(json_object *queryJ, CtlSourceT *source) { const char *uid = NULL, *func = NULL; char luaScriptPath[CONTROL_MAXPATH_LEN]; @@ -830,7 +807,7 @@ STATIC int LuaDoScript(json_object *queryJ, CtlSourceT *source) return 0; } -STATIC int LuaDoCall(json_object *queryJ, CtlSourceT *source) +static int LuaDoCall(json_object *queryJ, CtlSourceT *source) { int err = 0; int count = 0; @@ -863,7 +840,7 @@ STATIC int LuaDoCall(json_object *queryJ, CtlSourceT *source) return count; } -STATIC int LuaDoString(const char *script, CtlSourceT *source) +static int LuaDoString(const char *script, CtlSourceT *source) { int err = 0; @@ -882,7 +859,7 @@ STATIC int LuaDoString(const char *script, CtlSourceT *source) } // Execute LUA code from received API request -STATIC void LuaDoAction (LuaDoActionT action, AFB_ReqT request) { +static void LuaDoAction (LuaDoActionT action, AFB_ReqT request) { int err, count=0; CtlSourceT *source = alloca(sizeof(CtlSourceT)); source->request = request; @@ -915,36 +892,34 @@ STATIC void LuaDoAction (LuaDoActionT action, AFB_ReqT request) { default: AFB_ApiError(source->api, "LUA-DOSCRIPT-ACTION unknown query=%s", json_object_get_string(queryJ)); - goto OnErrorExit; + AFB_ReqFail(request,"LUA:ERROR", lua_tostring(luaState,-1)); + return; } if(count >= 0) err=lua_pcall(luaState, count+1, 0, 0); if (err) { AFB_ApiError(source->api, "LUA-DO-EXEC:FAIL query=%s err=%s", json_object_get_string(queryJ), lua_tostring(luaState,-1)); - goto OnErrorExit; + AFB_ReqFail(request,"LUA:ERROR", lua_tostring(luaState,-1)); + return; } return; - - OnErrorExit: - AFB_ReqFail(request,"LUA:ERROR", lua_tostring(luaState,-1)); - return; } -PUBLIC void ctlapi_execlua (AFB_ReqT request) { +void ctlapi_execlua (AFB_ReqT request) { LuaDoAction (LUA_DOSTRING, request); } -PUBLIC void ctlapi_request (AFB_ReqT request) { +void ctlapi_request (AFB_ReqT request) { LuaDoAction (LUA_DOCALL, request); } -PUBLIC void ctlapi_debuglua (AFB_ReqT request) { +void ctlapi_debuglua (AFB_ReqT request) { LuaDoAction (LUA_DOSCRIPT, request); } -STATIC TimerHandleT *LuaTimerPop (lua_State *luaState, int index) { +static TimerHandleT *LuaTimerPop (lua_State *luaState, int index) { TimerHandleT *timerHandle; luaL_checktype(luaState, index, LUA_TLIGHTUSERDATA); @@ -958,11 +933,12 @@ STATIC TimerHandleT *LuaTimerPop (lua_State *luaState, int index) { return timerHandle; } -STATIC int LuaTimerClear (lua_State* luaState) { +static int LuaTimerClear (lua_State* luaState) { // retrieve useful information opaque handle TimerHandleT *timerHandle = LuaTimerPop(luaState, LUA_FIST_ARG); - if (!timerHandle) goto OnErrorExit; + if (!timerHandle) + return 1; #ifdef AFB_BINDING_PREV3 // API handle does not exit in API-V2 @@ -972,15 +948,13 @@ STATIC int LuaTimerClear (lua_State* luaState) { TimerEvtStop(timerHandle); return 0; //happy end - -OnErrorExit: - return 1; } -STATIC int LuaTimerGet (lua_State* luaState) { +static int LuaTimerGet (lua_State* luaState) { // retrieve useful information opaque handle TimerHandleT *timerHandle = LuaTimerPop(luaState, LUA_FIST_ARG); - if (!timerHandle) goto OnErrorExit; + if (!timerHandle) + return 0; LuaCbHandleT *luaCbHandle = (LuaCbHandleT*) timerHandle->context; // create response as a JSON object @@ -996,15 +970,12 @@ STATIC int LuaTimerGet (lua_State* luaState) { json_object_put(responseJ); return count; // return argument - -OnErrorExit: - return 0; } // Timer Callback // Set timer -STATIC int LuaTimerSetCB (TimerHandleT *timer) { +static int LuaTimerSetCB (TimerHandleT *timer) { LuaCbHandleT *LuaCbHandle = (LuaCbHandleT*) timer->context; int count; @@ -1014,12 +985,14 @@ STATIC int LuaTimerSetCB (TimerHandleT *timer) { // Push AFB client context on the stack count=1; LuaAfbSourceT *afbSource= LuaSourcePush(luaState, LuaCbHandle->source); - if (!afbSource) goto OnErrorExit; + if (!afbSource) + return 1; // Push AFB client context on the stack count ++; lua_pushlightuserdata(luaState, timer); - if (!afbSource) goto OnErrorExit; + if (!afbSource) + return 1; // Push user Context count+= LuaPushArgument(LuaCbHandle->source, LuaCbHandle->context); @@ -1027,7 +1000,7 @@ STATIC int LuaTimerSetCB (TimerHandleT *timer) { int err=lua_pcall(luaState, count, LUA_MULTRET, 0); if (err) { AFB_ApiError (LuaCbHandle->source->api,"LUA-TIMER-CB:FAIL response=%s err=%s", json_object_get_string(LuaCbHandle->context), lua_tostring(luaState,-1)); - goto OnErrorExit; + return 1; } // get return parameter @@ -1036,13 +1009,10 @@ STATIC int LuaTimerSetCB (TimerHandleT *timer) { } return 0; // By default we are happy - - OnErrorExit: - return 1; // stop timer } // Free Timer context handle -STATIC int LuaTimerCtxFree(void *handle) { +static int LuaTimerCtxFree(void *handle) { LuaCbHandleT *LuaCbHandle = (LuaCbHandleT*) handle; free (LuaCbHandle->source); @@ -1051,13 +1021,16 @@ STATIC int LuaTimerCtxFree(void *handle) { return 0; } -STATIC int LuaTimerSet(lua_State* luaState) { +static int LuaTimerSet(lua_State* luaState) { const char *uid=NULL, *info=NULL; int delay=0, count=0; // Get source handle CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); - if (!source) goto OnErrorExit; + if (!source) { + lua_error(luaState); + return 1; // return error code + } json_object *timerJ = LuaPopOneArg(source, luaState, LUA_FIST_ARG+1); const char *callback = lua_tostring(luaState, LUA_FIST_ARG + 2); @@ -1065,7 +1038,8 @@ STATIC int LuaTimerSet(lua_State* luaState) { if (lua_gettop(luaState) != LUA_FIST_ARG+3 || !timerJ || !callback || !contextJ) { lua_pushliteral(luaState, "LuaTimerSet: Syntax timerset (source, timerT, 'callback', contextT)"); - goto OnErrorExit; + lua_error(luaState); + return 1; // return error code } int err = wrap_json_unpack(timerJ, "{ss, s?s si, si !}", @@ -1076,7 +1050,8 @@ STATIC int LuaTimerSet(lua_State* luaState) { if (err) { lua_pushliteral(luaState, "LuaTimerSet: Syntax timerT={uid:xxx delay:ms, count:xx}"); - goto OnErrorExit; + lua_error(luaState); + return 1; // return error code } // Allocate handle to store context and callback @@ -1103,10 +1078,6 @@ STATIC int LuaTimerSet(lua_State* luaState) { lua_pushnil(luaState); lua_pushlightuserdata(luaState, timerHandle); return 2; - -OnErrorExit: - lua_error(luaState); - return 1; // return error code } typedef struct { @@ -1116,7 +1087,7 @@ typedef struct { } LuaClientCtxT; -STATIC void *LuaClientCtxNew (void * handle) { +static void *LuaClientCtxNew (void * handle) { LuaClientCtxT *clientCtx = (LuaClientCtxT*) handle; int count=1; @@ -1129,7 +1100,8 @@ STATIC void *LuaClientCtxNew (void * handle) { // Push AFB client context on the stack LuaAfbSourceT *afbSource= LuaSourcePush(luaState, clientCtx->source); - if (!afbSource) goto OnErrorExit; + if (!afbSource) + return NULL; // Push user Context count+= LuaPushArgument(clientCtx->source, clientCtx->clientCtxJ); @@ -1137,23 +1109,19 @@ STATIC void *LuaClientCtxNew (void * handle) { int err=lua_pcall(luaState, count, 1, 0); if (err) { AFB_ApiError (clientCtx->source->api,"LuaClientCtxNew:FAIL response=%s err=%s", json_object_get_string(clientCtx->clientCtxJ), lua_tostring(luaState,-1)); - goto OnErrorExit; + return NULL; } // If function return an error status then free client context if (lua_tointeger(luaState, -1)) { free (clientCtx); - goto OnErrorExit; + return NULL; } return handle; // By default we are happy - -OnErrorExit: - return NULL; - } -STATIC void LuaClientCtxFree (void * handle) { +static void LuaClientCtxFree (void * handle) { LuaClientCtxT *clientCtx = (LuaClientCtxT*) handle; int count=1; @@ -1168,7 +1136,8 @@ STATIC void LuaClientCtxFree (void * handle) { // Push AFB client context on the stack LuaAfbSourceT *afbSource= LuaSourcePush(luaState, clientCtx->source); - if (!afbSource) goto OnErrorExit; + if (!afbSource) + return; // Push user Context count+= LuaPushArgument(clientCtx->source, clientCtx->clientCtxJ); @@ -1176,38 +1145,38 @@ STATIC void LuaClientCtxFree (void * handle) { int err=lua_pcall(luaState, count, LUA_MULTRET, 0); if (err) { AFB_ApiError (clientCtx->source->api,"LuaClientCtxFree:FAIL response=%s err=%s", json_object_get_string(clientCtx->clientCtxJ), lua_tostring(luaState,-1)); - goto OnErrorExit; + return; } // If function return an error status then free client context if (lua_toboolean(luaState, -1)) { free (clientCtx); - goto OnErrorExit; + return; } return; // No return status - -OnErrorExit: - return; - } // set a context that will be free when WS is closed -STATIC int LuaClientCtx(lua_State* luaState) { - +static int LuaClientCtx(lua_State* luaState) { // Get source handle CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG); - if (!source) goto OnErrorExit; + if (!source) { + lua_error(luaState); + return 1; // return error code + } if (!AFB_ReqIsValid (source->request)) { lua_pushliteral(luaState, "LuaSessionSet-Syntax should be called within client request context"); - goto OnErrorExit; + lua_error(luaState); + return 1; // return error code } // in only one arg then we should free the clientCtx if (lua_gettop(luaState) == LUA_FIST_ARG) { AFB_ClientCtxClear(source->request); - goto OnSuccessExit; + lua_pushnil(luaState); + return 1; } const char *callback = lua_tostring(luaState, LUA_FIST_ARG + 1); @@ -1215,7 +1184,8 @@ STATIC int LuaClientCtx(lua_State* luaState) { if (lua_gettop(luaState) != LUA_FIST_ARG+2 || !clientCtxJ || !callback) { lua_pushliteral(luaState, "LuaClientCtx-Syntax clientCtx (source, callback, clientCtx)"); - goto OnErrorExit; + lua_error(luaState); + return 1; // return error code } // Allocate handle to store clientCtx and callback @@ -1229,16 +1199,11 @@ STATIC int LuaClientCtx(lua_State* luaState) { void *done = AFB_ClientCtxSet (source->request, 1, LuaClientCtxNew, LuaClientCtxFree, clientCtx); if (!done) { lua_pushliteral(luaState, "LuaClientCtx-Fail to allocate client context)"); - goto OnErrorExit; + lua_error(luaState); + return 1; // return error code } -OnSuccessExit: - lua_pushnil(luaState); return 1; - -OnErrorExit: - lua_error(luaState); - return 1; // return error code } @@ -1275,7 +1240,7 @@ static const luaL_Reg afbFunction[] = { }; // Load Lua Interpreter -PUBLIC int LuaConfigLoad (AFB_ApiT apiHandle) { +int LuaConfigLoad (AFB_ApiT apiHandle) { static int luaLoaded=0, err = 0; //int err = 0; @@ -1287,7 +1252,8 @@ PUBLIC int LuaConfigLoad (AFB_ApiT apiHandle) { luaState = luaL_newstate(); if (!luaState) { AFB_ApiError(apiHandle, "LUA_INIT: Fail to open lua interpretor"); - goto OnErrorExit; + free(luaState); + return 1; } // load auxiliary libraries diff --git a/ctl-lib/ctl-lua.h b/ctl-lib/ctl-lua.h index deff9ee..aa03709 100644 --- a/ctl-lib/ctl-lua.h +++ b/ctl-lib/ctl-lua.h @@ -48,8 +48,7 @@ extern "C" { #include "ctl-timer.h" -PUBLIC int LuaLibInit (); - +int LuaLibInit (); typedef enum { LUA_DOCALL, @@ -58,15 +57,14 @@ typedef enum { } LuaDoActionT; extern const char *lua_utils; -PUBLIC int luaLoadScript(const char *luaScriptPath); -PUBLIC int LuaConfigLoad (AFB_ApiT apiHandle); -PUBLIC int LuaConfigExec(AFB_ApiT apiHandle, const char * prefix); -PUBLIC void LuaL2cNewLib(luaL_Reg *l2cFunc, int count, const char *prefix); -PUBLIC int Lua2cWrapper(void* luaHandle, char *funcname, Lua2cFunctionT callback); -PUBLIC int LuaCallFunc (CtlSourceT *source, CtlActionT *action, json_object *queryJ) ; -PUBLIC void ctlapi_lua_docall (afb_req request); -PUBLIC void ctlapi_lua_dostring (afb_req request); -PUBLIC void ctlapi_lua_doscript (afb_req request); +int LuaLoadScript(const char *luaScriptPath); +int LuaConfigLoad (AFB_ApiT apiHandle); +void LuaL2cNewLib(luaL_Reg *l2cFunc, int count, const char *prefix); +int Lua2cWrapper(void* luaHandle, char *funcname, Lua2cFunctionT callback); +int LuaCallFunc (CtlSourceT *source, CtlActionT *action, json_object *queryJ) ; +void ctlapi_lua_docall (afb_req request); +void ctlapi_lua_dostring (afb_req request); +void ctlapi_lua_doscript (afb_req request); #ifdef __cplusplus } diff --git a/ctl-lib/ctl-onload.c b/ctl-lib/ctl-onload.c index 5455c8a..f319103 100644 --- a/ctl-lib/ctl-onload.c +++ b/ctl-lib/ctl-onload.c @@ -23,7 +23,7 @@ #include "ctl-config.h" // onload section receive one action or an array of actions -PUBLIC int OnloadConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ) { +int OnloadConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ) { // Load time parse actions in control file if (actionsJ != NULL) { @@ -31,14 +31,14 @@ PUBLIC int OnloadConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *a if (!section->actions) { AFB_ApiError (apiHandle, "OnloadConfig control fail processing onload actions"); - goto OnErrorExit; + return 1; } } else { // Exec time process onload action now if (!section->actions) { AFB_ApiError (apiHandle, "OnloadConfig Cannot Exec Non Existing Onload Action"); - goto OnErrorExit; + return 1; } for (int idx=0; section->actions[idx].uid != NULL; idx ++) { @@ -52,8 +52,4 @@ PUBLIC int OnloadConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *a } return 0; - -OnErrorExit: - return 1; - } diff --git a/ctl-lib/ctl-plugin.c b/ctl-lib/ctl-plugin.c index ab9e171..70d8817 100644 --- a/ctl-lib/ctl-plugin.c +++ b/ctl-lib/ctl-plugin.c @@ -24,14 +24,14 @@ #include "ctl-config.h" -PUBLIC int PluginGetCB (AFB_ApiT apiHandle, CtlActionT *action , json_object *callbackJ) { +int PluginGetCB (AFB_ApiT apiHandle, CtlActionT *action , json_object *callbackJ) { const char *plugin=NULL, *function=NULL; json_object *argsJ; int idx; if (!ctlPlugins) { AFB_ApiError(apiHandle, "PluginGetCB plugin section missing cannot call '%s'", json_object_get_string(callbackJ)); - goto OnErrorExit; + return 1; } int err = wrap_json_unpack(callbackJ, "{ss,ss,s?o!}", @@ -40,7 +40,7 @@ PUBLIC int PluginGetCB (AFB_ApiT apiHandle, CtlActionT *action , json_object *ca "args", &argsJ); if (err) { AFB_ApiError(apiHandle, "PluginGet missing plugin|function|[args] in %s", json_object_get_string(callbackJ)); - goto OnErrorExit; + return 1; } for (idx=0; ctlPlugins[idx].uid != NULL; idx++) { @@ -49,7 +49,7 @@ PUBLIC int PluginGetCB (AFB_ApiT apiHandle, CtlActionT *action , json_object *ca if (!ctlPlugins[idx].uid) { AFB_ApiError(apiHandle, "PluginGetCB no plugin with uid=%s", plugin); - goto OnErrorExit; + return 1; } action->exec.cb.funcname = function; @@ -58,17 +58,13 @@ PUBLIC int PluginGetCB (AFB_ApiT apiHandle, CtlActionT *action , json_object *ca if (!action->exec.cb.callback) { AFB_ApiError(apiHandle, "PluginGetCB no plugin=%s no function=%s", plugin, function); - goto OnErrorExit; + return 1; } return 0; - -OnErrorExit: - return 1; - } // Wrapper to Lua2c plugin command add context and delegate to LuaWrapper -STATIC int DispatchOneL2c(void* luaState, char *funcname, Lua2cFunctionT callback) { +static int DispatchOneL2c(void* luaState, char *funcname, Lua2cFunctionT callback) { #ifndef CONTROL_SUPPORT_LUA fprintf(stderr, "CTL-ONE-L2C: LUA support not selected (cf:CONTROL_SUPPORT_LUA) in config.cmake"); return 1; @@ -78,7 +74,7 @@ STATIC int DispatchOneL2c(void* luaState, char *funcname, Lua2cFunctionT callbac #endif } -STATIC int PluginLoadCOne(AFB_ApiT apiHandle, const char *pluginpath, json_object *lua2csJ, void * handle, CtlPluginT *ctlPlugin) +static int PluginLoadCOne(AFB_ApiT apiHandle, const char *pluginpath, json_object *lua2csJ, const char *lua2c_prefix, void * handle, CtlPluginT *ctlPlugin) { void *dlHandle = dlopen(pluginpath, RTLD_NOW); @@ -188,7 +184,7 @@ STATIC int PluginLoadCOne(AFB_ApiT apiHandle, const char *pluginpath, json_objec return 0; } -STATIC int LoadFoundPlugins(AFB_ApiT apiHandle, json_object *scanResult, json_object *lua2csJ, void *handle, CtlPluginT *ctlPlugin) +static int LoadFoundPlugins(AFB_ApiT apiHandle, json_object *scanResult, json_object *lua2csJ, const char *lua2c_prefix, void *handle, CtlPluginT *ctlPlugin) { char pluginpath[CONTROL_MAXPATH_LEN]; char *filename; @@ -237,7 +233,7 @@ STATIC int LoadFoundPlugins(AFB_ApiT apiHandle, json_object *scanResult, json_ob return 0; } -STATIC char *GetDefaultSearchPath() +static char *GetDefaultSearchPath() { char *searchPath; const char *bPath; @@ -269,7 +265,7 @@ STATIC char *GetDefaultSearchPath() return searchPath; } -STATIC int FindPlugins(AFB_ApiT apiHandle, const char *searchPath, const char *file, json_object **pluginPathJ) +static int FindPlugins(AFB_ApiT apiHandle, const char *searchPath, const char *file, json_object **pluginPathJ) { *pluginPathJ = ScanForConfig(searchPath, CTL_SCAN_RECURSIVE, file, NULL); if (!*pluginPathJ || json_object_array_length(*pluginPathJ) == 0) { @@ -280,7 +276,7 @@ STATIC int FindPlugins(AFB_ApiT apiHandle, const char *searchPath, const char *f return 0; } -STATIC int PluginLoad (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object *pluginJ, void *handle) +static int PluginLoad (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object *pluginJ, void *handle) { int err = 0, i = 0; char *searchPath; @@ -299,7 +295,7 @@ STATIC int PluginLoad (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object *p "lua2c_prefix", &lua2c_prefix); if (err) { AFB_ApiError(apiHandle, "CTL-PLUGIN-LOADONE Plugin missing uid|[info]|file|[ldpath]|[lua2c] in:\n-- %s", json_object_get_string(pluginJ)); - goto OnErrorExit; + return 1; } // if search path not in Json config file, then try default @@ -310,43 +306,48 @@ STATIC int PluginLoad (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object *p // default file equal uid if (!fileJ) { file = ctlPlugin->uid; - if(FindPlugins(apiHandle, searchPath, file, &pluginPathJ)) - goto OnErrorExit; - LoadFoundPlugins(apiHandle, pluginPathJ, lua2csJ, handle, ctlPlugin); + if(FindPlugins(apiHandle, searchPath, file, &pluginPathJ)) { + free(searchPath); + if(pluginPathJ) + json_object_put(pluginPathJ); // No more needs for that json_object. + return 1; + } LoadFoundPlugins(apiHandle, pluginPathJ, lua2csJ, lua2c_prefix, handle, ctlPlugin); } else if(json_object_is_type(fileJ, json_type_string)) { file = json_object_get_string(fileJ); - if(FindPlugins(apiHandle, searchPath, file, &pluginPathJ)) - goto OnErrorExit; - LoadFoundPlugins(apiHandle, pluginPathJ, lua2csJ, handle, ctlPlugin); + if(FindPlugins(apiHandle, searchPath, file, &pluginPathJ)) { + free(searchPath); + json_object_put(pluginPathJ); // No more needs for that json_object. + return 1; + } LoadFoundPlugins(apiHandle, pluginPathJ, lua2csJ, lua2c_prefix, handle, ctlPlugin); } else if(json_object_is_type(fileJ, json_type_array)) { for(i = 0; i < json_object_array_length(fileJ);++i) { file = json_object_get_string(json_object_array_get_idx(fileJ, i)); - if(FindPlugins(apiHandle, searchPath, file, &pluginPathJ)) - goto OnErrorExit; - LoadFoundPlugins(apiHandle, pluginPathJ, lua2csJ, handle, ctlPlugin); + if(FindPlugins(apiHandle, searchPath, file, &pluginPathJ)) { + free(searchPath); + json_object_put(pluginPathJ); // No more needs for that json_object. + return 1; + } LoadFoundPlugins(apiHandle, pluginPathJ, lua2c_prefix, lua2csJ, handle, ctlPlugin); } } - if(err) - goto OnErrorExit; + if(err) { + free(searchPath); + json_object_put(pluginPathJ); // No more needs for that json_object. + return 1; + } free(searchPath); json_object_put(pluginPathJ); // No more needs for that json_object. return 0; - -OnErrorExit: - free(searchPath); - json_object_put(pluginPathJ); // No more needs for that json_object. - return 1; } -PUBLIC int PluginConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *pluginsJ) { +int PluginConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *pluginsJ) { int err = 0; int idx = 0; size_t length = 0; diff --git a/ctl-lib/ctl-plugin.h b/ctl-lib/ctl-plugin.h index 85d9788..a2bd071 100644 --- a/ctl-lib/ctl-plugin.h +++ b/ctl-lib/ctl-plugin.h @@ -149,14 +149,6 @@ extern "C" { #define CTL_PLUGIN_MAGIC 852369147 #endif -#ifndef PUBLIC - #define PUBLIC -#endif - -#ifndef STATIC - #define STATIC static -#endif - #ifndef UNUSED_ARG #define UNUSED_ARG(x) UNUSED_ ## x __attribute__((__unused__)) #define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x diff --git a/ctl-lib/ctl-timer.c b/ctl-lib/ctl-timer.c index e1ced46..2139c75 100644 --- a/ctl-lib/ctl-timer.c +++ b/ctl-lib/ctl-timer.c @@ -31,13 +31,16 @@ typedef struct { } AutoTestCtxT; -STATIC int TimerNext (sd_event_source* source, uint64_t timer, void* handle) { +static int TimerNext (sd_event_source* source, uint64_t timer, void* handle) { TimerHandleT *timerHandle = (TimerHandleT*) handle; int done; uint64_t usec; done= timerHandle->callback(timerHandle); - if (!done) goto OnErrorExit; + if (!done) { + AFB_ApiWarning(timerHandle->api, "TimerNext Callback Fail Tag=%s", timerHandle->uid); + return -1; + } // Rearm timer if needed timerHandle->count --; @@ -55,20 +58,16 @@ STATIC int TimerNext (sd_event_source* source, uint64_t timer, void* handle) { } return 0; - -OnErrorExit: - AFB_ApiWarning(timerHandle->api, "TimerNext Callback Fail Tag=%s", timerHandle->uid); - return -1; } -PUBLIC void TimerEvtStop(TimerHandleT *timerHandle) { +void TimerEvtStop(TimerHandleT *timerHandle) { sd_event_source_unref(timerHandle->evtSource); free (timerHandle); } -PUBLIC void TimerEvtStart(AFB_ApiT apiHandle, TimerHandleT *timerHandle, timerCallbackT callback, void *context) { +void TimerEvtStart(AFB_ApiT apiHandle, TimerHandleT *timerHandle, timerCallbackT callback, void *context) { uint64_t usec; // populate CB handle @@ -83,7 +82,7 @@ PUBLIC void TimerEvtStart(AFB_ApiT apiHandle, TimerHandleT *timerHandle, timerCa // Create Binding Event at Init -PUBLIC int TimerEvtInit (AFB_ApiT apiHandle) { +int TimerEvtInit (AFB_ApiT apiHandle) { AFB_ApiDebug (apiHandle, "Timer-Init Done"); return 0; diff --git a/ctl-lib/ctl-timer.h b/ctl-lib/ctl-timer.h index 4b06bdf..389d518 100644 --- a/ctl-lib/ctl-timer.h +++ b/ctl-lib/ctl-timer.h @@ -42,9 +42,9 @@ typedef struct TimerHandleS { typedef int (*timerCallbackT)(TimerHandleT *context); -PUBLIC int TimerEvtInit (AFB_ApiT apiHandle); -PUBLIC void TimerEvtStart(AFB_ApiT apiHandle, TimerHandleT *timerHandle, timerCallbackT callback, void *context); -PUBLIC void TimerEvtStop(TimerHandleT *timerHandle); +int TimerEvtInit (AFB_ApiT apiHandle); +void TimerEvtStart(AFB_ApiT apiHandle, TimerHandleT *timerHandle, timerCallbackT callback, void *context); +void TimerEvtStop(TimerHandleT *timerHandle); #ifdef __cplusplus } |