uci: update to latest version, uses cmake now
[openwrt/openwrt.git] / package / uci / patches / 120-uci_trigger.patch
1 --- /dev/null
2 +++ b/trigger/uci_trigger.c
3 @@ -0,0 +1,132 @@
4 +#include <sys/types.h>
5 +#include <sys/time.h>
6 +#include <stdbool.h>
7 +#include <string.h>
8 +#include <stdio.h>
9 +#include <lualib.h>
10 +#include <lauxlib.h>
11 +#include "uci.h"
12 +
13 +// #define DEBUG
14 +
15 +static int refcount = 0;
16 +static lua_State *gL = NULL;
17 +static bool prepared = false;
18 +
19 +struct trigger_set_op {
20 + struct uci_package *p;
21 + struct uci_delta *h;
22 +};
23 +
24 +static int trigger_set(lua_State *L)
25 +{
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;
31 +
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))
36 + return 0;
37 +
38 + if (!prepared) {
39 + lua_getglobal(L, "require");
40 + lua_pushstring(L, "uci.trigger");
41 + lua_call(L, 1, 0);
42 +
43 + lua_getglobal(L, "uci");
44 + lua_getfield(L, -1, "trigger");
45 + lua_getfield(L, -1, "load_modules");
46 + lua_call(L, 0, 0);
47 + prepared = true;
48 + } else {
49 + lua_getglobal(L, "uci");
50 + lua_getfield(L, -1, "trigger");
51 + }
52 +
53 + lua_getfield(L, -1, "set");
54 + lua_createtable(L, 4, 0);
55 +
56 + lua_pushstring(L, p->e.name);
57 + lua_rawseti(L, -2, 1);
58 + if (h->section) {
59 + lua_pushstring(L, h->section);
60 + lua_rawseti(L, -2, 2);
61 + }
62 + if (h->e.name) {
63 + lua_pushstring(L, h->e.name);
64 + lua_rawseti(L, -2, 3);
65 + }
66 + if (h->value) {
67 + lua_pushstring(L, h->value);
68 + lua_rawseti(L, -2, 4);
69 + }
70 + lua_call(L, 1, 0);
71 + lua_pop(L, 2);
72 + return 0;
73 +}
74 +
75 +#ifdef DEBUG
76 +
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);
82 + lua_pop(L, 1);
83 + }
84 + return status;
85 +}
86 +
87 +#else
88 +
89 +static inline int report(lua_State *L, int status) {
90 + return status;
91 +}
92 +
93 +#endif
94 +
95 +static void trigger_set_hook(const struct uci_hook_ops *ops, struct uci_package *p, struct uci_delta *h)
96 +{
97 + struct trigger_set_op so;
98 +
99 + so.p = p;
100 + so.h = h;
101 + report(gL, lua_cpcall(gL, &trigger_set, &so));
102 +}
103 +
104 +static struct uci_hook_ops hook = {
105 + .set = trigger_set_hook,
106 +};
107 +
108 +static int trigger_attach(struct uci_context *ctx)
109 +{
110 + if (!gL) {
111 + gL = luaL_newstate();
112 + if (!gL)
113 + return -1;
114 + luaL_openlibs(gL);
115 +
116 + refcount++;
117 + }
118 + uci_add_hook(ctx, &hook);
119 + return 0;
120 +}
121 +
122 +static void trigger_detach(struct uci_context *ctx)
123 +{
124 + if (gL && (--refcount <= 0)) {
125 + lua_close(gL);
126 + gL = NULL;
127 + refcount = 0;
128 + prepared = false;
129 + }
130 +}
131 +
132 +struct uci_plugin_ops uci_plugin = {
133 + .attach = trigger_attach,
134 + .detach = trigger_detach,
135 +};
136 --- a/CMakeLists.txt
137 +++ b/CMakeLists.txt
138 @@ -35,6 +35,7 @@ ADD_EXECUTABLE(ucimap-example ucimap-exa
139 TARGET_LINK_LIBRARIES(ucimap-example uci-static ucimap dl)
140
141 ADD_SUBDIRECTORY(lua)
142 +ADD_SUBDIRECTORY(trigger)
143
144 INSTALL(FILES uci.h uci_config.h ucimap.h
145 DESTINATION include/libubox
146 --- /dev/null
147 +++ b/trigger/CMakeLists.txt
148 @@ -0,0 +1,19 @@
149 +cmake_minimum_required(VERSION 2.6)
150 +
151 +PROJECT(uci C)
152 +
153 +SET(CMAKE_INSTALL_PREFIX /)
154 +
155 +ADD_DEFINITIONS(-Os -Wall -Werror --std=gnu99 -g3 -I..)
156 +
157 +IF(APPLE)
158 + SET(CMAKE_SHARED_MODULE_CREATE_C_FLAGS "${CMAKE_SHARED_MODULE_CREATE_C_FLAGS} -undefined dynamic_lookup")
159 +ENDIF(APPLE)
160 +
161 +ADD_LIBRARY(uci_trigger MODULE uci_trigger.c)
162 +SET_TARGET_PROPERTIES(uci_trigger PROPERTIES
163 + OUTPUT_NAME uci_trigger
164 + PREFIX ""
165 +)
166 +TARGET_LINK_LIBRARIES(uci_trigger uci)
167 +
168