2 +++ b/trigger/uci_trigger.c
4 +#include <sys/types.h>
15 +static int refcount = 0;
16 +static lua_State *gL = NULL;
17 +static bool prepared = false;
19 +struct trigger_set_op {
20 + struct uci_package *p;
21 + struct uci_delta *h;
24 +static int trigger_set(lua_State *L)
26 + struct trigger_set_op *so =
27 + (struct trigger_set_op *)lua_touserdata(L, 1);
28 + struct uci_package *p = so->p;
29 + struct uci_delta *h = so->h;
30 + struct uci_context *ctx = p->ctx;
32 + /* ignore non-standard savedirs/configdirs
33 + * in order to not trigger events on uci state changes */
34 + if (strcmp(ctx->savedir, UCI_SAVEDIR) ||
35 + strcmp(ctx->confdir, UCI_CONFDIR))
39 + lua_getglobal(L, "require");
40 + lua_pushstring(L, "uci.trigger");
43 + lua_getglobal(L, "uci");
44 + lua_getfield(L, -1, "trigger");
45 + lua_getfield(L, -1, "load_modules");
49 + lua_getglobal(L, "uci");
50 + lua_getfield(L, -1, "trigger");
53 + lua_getfield(L, -1, "set");
54 + lua_createtable(L, 4, 0);
56 + lua_pushstring(L, p->e.name);
57 + lua_rawseti(L, -2, 1);
59 + lua_pushstring(L, h->section);
60 + lua_rawseti(L, -2, 2);
63 + lua_pushstring(L, h->e.name);
64 + lua_rawseti(L, -2, 3);
67 + lua_pushstring(L, h->value);
68 + lua_rawseti(L, -2, 4);
77 +static int report (lua_State *L, int status) {
78 + if (status && !lua_isnil(L, -1)) {
79 + const char *msg = lua_tostring(L, -1);
80 + if (msg == NULL) msg = "(error object is not a string)";
81 + fprintf(stderr, "ERROR: %s\n", msg);
89 +static inline int report(lua_State *L, int status) {
95 +static void trigger_set_hook(const struct uci_hook_ops *ops, struct uci_package *p, struct uci_delta *h)
97 + struct trigger_set_op so;
101 + report(gL, lua_cpcall(gL, &trigger_set, &so));
104 +static struct uci_hook_ops hook = {
105 + .set = trigger_set_hook,
108 +static int trigger_attach(struct uci_context *ctx)
111 + gL = luaL_newstate();
118 + uci_add_hook(ctx, &hook);
122 +static void trigger_detach(struct uci_context *ctx)
124 + if (gL && (--refcount <= 0)) {
132 +struct uci_plugin_ops uci_plugin = {
133 + .attach = trigger_attach,
134 + .detach = trigger_detach,
138 @@ -35,6 +35,7 @@ ADD_EXECUTABLE(ucimap-example ucimap-exa
139 TARGET_LINK_LIBRARIES(ucimap-example uci-static ucimap dl)
141 ADD_SUBDIRECTORY(lua)
142 +ADD_SUBDIRECTORY(trigger)
144 INSTALL(FILES uci.h uci_config.h ucimap.h
145 DESTINATION include/libubox
147 +++ b/trigger/CMakeLists.txt
149 +cmake_minimum_required(VERSION 2.6)
153 +SET(CMAKE_INSTALL_PREFIX /)
155 +ADD_DEFINITIONS(-Os -Wall -Werror --std=gnu99 -g3 -I..)
158 + SET(CMAKE_SHARED_MODULE_CREATE_C_FLAGS "${CMAKE_SHARED_MODULE_CREATE_C_FLAGS} -undefined dynamic_lookup")
161 +ADD_LIBRARY(uci_trigger MODULE uci_trigger.c)
162 +SET_TARGET_PROPERTIES(uci_trigger PROPERTIES
163 + OUTPUT_NAME uci_trigger
166 +TARGET_LINK_LIBRARIES(uci_trigger uci)