int sexe_exec_pget(sexe_t *S, char *name, shjson_t **arg_p) { shjson_t *json; lua_getglobal(S, name); json = sexe_table_get(S); if (!json) return (SHERR_INVAL); *arg_p = json; return (0); }
int update_sexe_userdata(sexe_t *S) { SHFL *fl; shjson_t *udata; shfs_t *fs; shbuf_t *buff; shkey_t *k; char path[PATH_MAX+1]; char *str; int err; k = &S->pname; if (shkey_cmp(k, ashkey_blank())) { fprintf(stderr, "DEBUG: update_sexe_userdata: no app key\n"); return (0); /* blank */ } sprintf(path, "/sys/data/sexe/%s", shkey_hex(k)); lua_getglobal(S, "userdata"); udata = sexe_table_get(S); if (!udata) { fprintf(stderr, "DEBUG: update_sexe_userdata: no global 'userdata' variable.\n"); return (SHERR_INVAL); } str = shjson_print(udata); if (!str) { fprintf(stderr, "DEBUG: update_sexe_userdata: error encoding JSON.\n"); return (SHERR_INVAL); } shjson_free(&udata); buff = shbuf_init(); shbuf_catstr(buff, str); free(str); fs = shfs_init(NULL); fl = shfs_file_find(fs, path); err = shfs_write(fl, buff); shbuf_free(&buff); shfs_free(&fs); return (err); }
static int _lfunc_trigger_event(lua_State *L) { shjson_t *json; int e_type = (int)luaL_checknumber(L, 1); int t_reg = 0; json = NULL; if (lua_istable(L, 2)) { lua_pushvalue(L, 2); json = sexe_table_get(L); } /* second optional arg; table of data. */ sexe_event_handle(L, e_type, json); if (json) shjson_free(&json); }
/** * The "os.trigger(<string>[,<table>])" function will trigger all callbacks for the specifid event name to be called with an optional object argument. */ int lfunc_trigger_event(sexe_t *L) { const char *e_name; shjson_t *json; int t_reg = 0; int ret_bool; int err; /* first argument: event name */ e_name = (int)luaL_checkstring(L, 1); /* second optional arg; table of data. */ json = NULL; if (lua_istable(L, 2)) { lua_pushvalue(L, 2); json = sexe_table_get(L); } ret_bool = 1; err = sexe_event_handle(L, e_name, json); if (err) ret_bool = 0; #if 0 /* iterate through registered functions for event */ { shkey_t *key = sexe_event_key(e_name); char *e_hex = shkey_hex(key); lua_getglobal(L, EVENT_ENV); if (lua_isnil(L, -1)) { return (0); /* error */ } lua_getfield(L, -1, e_hex); if (lua_isnil(L, -1)) { return (0); /* error */ } lua_pushnil(L); while (lua_next(L, -2)) { int t = lua_type(L, -1); if (t == LUA_TFUNCTION) { /* copy function call onto stack lua_pushvalue(L, 1); */ /* 1. user args */ if (json) sexe_table_set(L, json); else lua_pushnil(L); /* 2. event name */ lua_pushstring(L, e_name); /* exec */ lua_pcall(L, 2, 0, 0); } else { lua_pop(L, 1); /* value */ } //lua_pop(L, 1); /* key */ } } #endif if (json) shjson_free(&json); /* return single boolean */ lua_pushboolean(L, ret_bool); return (1); }