add uci.foreach()
[project/uci.git] / lua / uci.c
1 /*
2 * libuci plugin for Lua
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <sys/types.h>
16 #include <sys/time.h>
17 #include <stdbool.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <errno.h>
23
24 #include <lauxlib.h>
25 #include <uci.h>
26
27 #define MODNAME "uci"
28 //#define DEBUG 1
29
30 #ifdef DEBUG
31 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
32 #else
33 #define DPRINTF(...) do {} while (0)
34 #endif
35
36 static struct uci_context *ctx = NULL;
37
38 static struct uci_package *
39 find_package(const char *name)
40 {
41 struct uci_package *p = NULL;
42 struct uci_element *e;
43 uci_foreach_element(&ctx->root, e) {
44 if (strcmp(e->name, name) != 0)
45 continue;
46
47 p = uci_to_package(e);
48 break;
49 }
50 return p;
51 }
52
53 static void uci_lua_perror(lua_State *L, char *name)
54 {
55 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
56 lua_getfield(L, -1, "warn");
57 if (!lua_isboolean(L, -1))
58 goto done;
59 if (lua_toboolean(L, -1) != 1)
60 goto done;
61 uci_perror(ctx, name);
62 done:
63 lua_pop(L, 2);
64 }
65
66 static void uci_push_section(lua_State *L, struct uci_section *s)
67 {
68 struct uci_element *e;
69
70 lua_newtable(L);
71 lua_pushstring(L, s->type);
72 lua_setfield(L, -2, "type");
73 lua_pushstring(L, s->e.name);
74 lua_setfield(L, -2, "name");
75
76 lua_newtable(L);
77 lua_pushvalue(L, -1);
78 lua_setfield(L, -3, "options");
79
80 uci_foreach_element(&s->options, e) {
81 struct uci_option *o = uci_to_option(e);
82 lua_pushstring(L, o->value);
83 lua_setfield(L, -2, o->e.name);
84 }
85 lua_pop(L, 1);
86 }
87
88 static void uci_push_package(lua_State *L, struct uci_package *p)
89 {
90 struct uci_element *e;
91 int i = 0;
92
93 lua_newtable(L);
94 uci_foreach_element(&p->sections, e) {
95 i++;
96 luaL_setn(L, -1, i);
97 uci_push_section(L, uci_to_section(e));
98 lua_rawseti(L, -2, i);
99 }
100 }
101
102 static int
103 uci_lua_unload(lua_State *L)
104 {
105 struct uci_package *p;
106 const char *s;
107
108 luaL_checkstring(L, 1);
109 s = lua_tostring(L, -1);
110 p = find_package(s);
111 if (p) {
112 uci_unload(ctx, p);
113 lua_pushboolean(L, 1);
114 } else {
115 lua_pushboolean(L, 0);
116 }
117 return 1;
118 }
119
120 static int
121 uci_lua_load(lua_State *L)
122 {
123 struct uci_package *p = NULL;
124 const char *s;
125
126 uci_lua_unload(L);
127 lua_pop(L, 1); /* bool ret value of unload */
128 s = lua_tostring(L, -1);
129
130 if (uci_load(ctx, s, &p)) {
131 uci_lua_perror(L, "uci.load");
132 lua_pushboolean(L, 0);
133 } else {
134 lua_pushboolean(L, 1);
135 }
136
137 return 1;
138 }
139
140
141 static int
142 uci_lua_foreach(lua_State *L)
143 {
144 struct uci_package *p;
145 struct uci_element *e;
146 const char *package, *type;
147 bool ret = false;
148
149 package = luaL_checkstring(L, 1);
150
151 if (lua_isnil(L, 2))
152 type = NULL;
153 else
154 type = luaL_checkstring(L, 2);
155
156 if (!lua_isfunction(L, 3) || !package)
157 luaL_error(L, "Invalid argument");
158
159 p = find_package(package);
160 if (!p)
161 goto done;
162
163 uci_foreach_element(&p->sections, e) {
164 struct uci_section *s = uci_to_section(e);
165
166 if (type && (strcmp(s->type, type) != 0))
167 continue;
168
169 lua_pushvalue(L, 3); /* iterator function */
170 uci_push_section(L, s);
171 if (lua_pcall(L, 1, 0, 0) == 0)
172 ret = true;
173 }
174
175 done:
176 lua_pushboolean(L, ret);
177 return 1;
178 }
179
180 static int
181 uci_lua_get_any(lua_State *L, bool all)
182 {
183 struct uci_element *e = NULL;
184 struct uci_package *p = NULL;
185 char *package = NULL;
186 char *section = NULL;
187 char *option = NULL;
188 char *s;
189 int err = UCI_ERR_MEM;
190
191 luaL_checkstring(L, 1);
192 s = strdup(lua_tostring(L, -1));
193 if (!s)
194 goto error;
195
196 if ((err = uci_parse_tuple(ctx, s, &package, &section, &option, NULL)))
197 goto error;
198
199 if (!all && (section == NULL)) {
200 err = UCI_ERR_INVAL;
201 goto error;
202 }
203
204 p = find_package(package);
205 if (!p) {
206 err = UCI_ERR_NOTFOUND;
207 goto error;
208 }
209
210 if (section) {
211 if ((err = uci_lookup(ctx, &e, p, section, option)))
212 goto error;
213 } else {
214 e = &p->e;
215 }
216
217 switch(e->type) {
218 case UCI_TYPE_PACKAGE:
219 uci_push_package(L, p);
220 break;
221 case UCI_TYPE_SECTION:
222 if (all)
223 uci_push_section(L, uci_to_section(e));
224 else
225 lua_pushstring(L, uci_to_section(e)->type);
226 break;
227 case UCI_TYPE_OPTION:
228 lua_pushstring(L, uci_to_option(e)->value);
229 break;
230 default:
231 err = UCI_ERR_INVAL;
232 goto error;
233 }
234 error:
235 if (s)
236 free(s);
237
238 switch(err) {
239 default:
240 ctx->err = err;
241 uci_lua_perror(L, "uci.get");
242 /* fall through */
243 case UCI_ERR_NOTFOUND:
244 lua_pushnil(L);
245 /* fall through */
246 case 0:
247 return 1;
248 }
249 }
250
251 static int
252 uci_lua_get(lua_State *L)
253 {
254 return uci_lua_get_any(L, false);
255 }
256
257 static int
258 uci_lua_get_all(lua_State *L)
259 {
260 return uci_lua_get_any(L, true);
261 }
262
263 static int
264 uci_lua_set(lua_State *L)
265 {
266 struct uci_package *p;
267 char *package = NULL;
268 char *section = NULL;
269 char *option = NULL;
270 char *value = NULL;
271 char *s;
272 int err = UCI_ERR_MEM;
273
274 luaL_checkstring(L, 1);
275 s = strdup(lua_tostring(L, -1));
276 if (!s)
277 goto error;
278
279 if ((err = uci_parse_tuple(ctx, s, &package, &section, &option, &value)))
280 goto error;
281
282 if ((section == NULL) || (value == NULL)) {
283 err = UCI_ERR_INVAL;
284 goto error;
285 }
286
287 p = find_package(package);
288 if (!p) {
289 err = UCI_ERR_NOTFOUND;
290 goto error;
291 }
292 err = uci_set(ctx, p, section, option, value, NULL);
293
294 error:
295 if (err)
296 uci_lua_perror(L, "uci.set");
297 lua_pushboolean(L, (err == 0));
298 return 1;
299 }
300
301 enum pkg_cmd {
302 CMD_SAVE,
303 CMD_COMMIT,
304 CMD_REVERT
305 };
306
307 static int
308 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
309 {
310 struct uci_element *e, *tmp;
311 const char *s = NULL;
312 const char *section = NULL;
313 const char *option = NULL;
314 int failed = 0;
315 int nargs;
316
317 nargs = lua_gettop(L);
318 switch(nargs) {
319 case 3:
320 if (cmd != CMD_REVERT)
321 goto err;
322 luaL_checkstring(L, 1);
323 option = lua_tostring(L, -1);
324 lua_pop(L, 1);
325 /* fall through */
326 case 2:
327 if (cmd != CMD_REVERT)
328 goto err;
329 luaL_checkstring(L, 1);
330 section = lua_tostring(L, -1);
331 lua_pop(L, 1);
332 /* fall through */
333 case 1:
334 luaL_checkstring(L, 1);
335 s = lua_tostring(L, -1);
336 lua_pop(L, 1);
337 break;
338 default:
339 err:
340 luaL_error(L, "Invalid argument count");
341 break;
342 }
343
344 uci_foreach_element_safe(&ctx->root, tmp, e) {
345 struct uci_package *p = uci_to_package(e);
346 int ret = UCI_ERR_INVAL;
347
348 if (s && (strcmp(s, e->name) != 0))
349 continue;
350
351 switch(cmd) {
352 case CMD_COMMIT:
353 ret = uci_commit(ctx, &p, false);
354 break;
355 case CMD_SAVE:
356 ret = uci_save(ctx, p);
357 break;
358 case CMD_REVERT:
359 ret = uci_revert(ctx, &p, section, option);
360 break;
361 }
362
363 if (ret != 0)
364 failed = 1;
365 }
366
367 lua_pushboolean(L, !failed);
368 return 1;
369 }
370
371 static int
372 uci_lua_save(lua_State *L)
373 {
374 return uci_lua_package_cmd(L, CMD_SAVE);
375 }
376
377 static int
378 uci_lua_commit(lua_State *L)
379 {
380 return uci_lua_package_cmd(L, CMD_COMMIT);
381 }
382
383 static int
384 uci_lua_revert(lua_State *L)
385 {
386 return uci_lua_package_cmd(L, CMD_REVERT);
387 }
388
389 static int
390 uci_lua_set_confdir(lua_State *L)
391 {
392 int ret;
393
394 luaL_checkstring(L, 1);
395 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
396 lua_pushboolean(L, (ret == 0));
397 return 1;
398 }
399
400 static int
401 uci_lua_set_savedir(lua_State *L)
402 {
403 int ret;
404
405 luaL_checkstring(L, 1);
406 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
407 lua_pushboolean(L, (ret == 0));
408
409 return 1;
410 }
411
412 static const luaL_Reg uci[] = {
413 { "load", uci_lua_load },
414 { "unload", uci_lua_unload },
415 { "get", uci_lua_get },
416 { "get_all", uci_lua_get_all },
417 { "set", uci_lua_set },
418 { "save", uci_lua_save },
419 { "commit", uci_lua_commit },
420 { "revert", uci_lua_revert },
421 { "foreach", uci_lua_foreach },
422 { "set_confdir", uci_lua_set_confdir },
423 { "set_savedir", uci_lua_set_savedir },
424 { NULL, NULL },
425 };
426
427
428 int
429 luaopen_uci(lua_State *L)
430 {
431 ctx = uci_alloc_context();
432 if (!ctx)
433 luaL_error(L, "Cannot allocate UCI context\n");
434 luaL_register(L, MODNAME, uci);
435 return 0;
436 }