implement autoload for uci lua binding (optional, enabled by default)
[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(lua_State *L, const char *name, bool autoload)
40 {
41 struct uci_package *p = NULL;
42 struct uci_element *e;
43
44 uci_foreach_element(&ctx->root, e) {
45 if (strcmp(e->name, name) != 0)
46 continue;
47
48 p = uci_to_package(e);
49 goto done;
50 }
51
52 if (autoload) {
53 do {
54 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
55 lua_getfield(L, -1, "autoload");
56 if (!lua_isboolean(L, -1))
57 break;
58
59 if (!lua_toboolean(L, -1))
60 break;
61
62 uci_load(ctx, name, &p);
63 } while (0);
64 lua_pop(L, 2);
65 }
66
67 done:
68 return p;
69 }
70
71 static void uci_lua_perror(lua_State *L, char *name)
72 {
73 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
74 lua_getfield(L, -1, "warn");
75 if (!lua_isboolean(L, -1))
76 goto done;
77 if (lua_toboolean(L, -1) != 1)
78 goto done;
79 uci_perror(ctx, name);
80 done:
81 lua_pop(L, 2);
82 }
83
84 static void uci_push_section(lua_State *L, struct uci_section *s)
85 {
86 struct uci_element *e;
87
88 lua_newtable(L);
89 lua_pushstring(L, s->type);
90 lua_setfield(L, -2, "type");
91 lua_pushstring(L, s->e.name);
92 lua_setfield(L, -2, "name");
93
94 lua_newtable(L);
95 lua_pushvalue(L, -1);
96 lua_setfield(L, -3, "options");
97
98 uci_foreach_element(&s->options, e) {
99 struct uci_option *o = uci_to_option(e);
100 lua_pushstring(L, o->value);
101 lua_setfield(L, -2, o->e.name);
102 }
103 lua_pop(L, 1);
104 }
105
106 static void uci_push_package(lua_State *L, struct uci_package *p)
107 {
108 struct uci_element *e;
109 int i = 0;
110
111 lua_newtable(L);
112 uci_foreach_element(&p->sections, e) {
113 i++;
114 luaL_setn(L, -1, i);
115 uci_push_section(L, uci_to_section(e));
116 lua_rawseti(L, -2, i);
117 }
118 }
119
120 static int
121 uci_lua_unload(lua_State *L)
122 {
123 struct uci_package *p;
124 const char *s;
125
126 luaL_checkstring(L, 1);
127 s = lua_tostring(L, -1);
128 p = find_package(L, s, false);
129 if (p) {
130 uci_unload(ctx, p);
131 lua_pushboolean(L, 1);
132 } else {
133 lua_pushboolean(L, 0);
134 }
135 return 1;
136 }
137
138 static int
139 uci_lua_load(lua_State *L)
140 {
141 struct uci_package *p = NULL;
142 const char *s;
143
144 uci_lua_unload(L);
145 lua_pop(L, 1); /* bool ret value of unload */
146 s = lua_tostring(L, -1);
147
148 if (uci_load(ctx, s, &p)) {
149 uci_lua_perror(L, "uci.load");
150 lua_pushboolean(L, 0);
151 } else {
152 lua_pushboolean(L, 1);
153 }
154
155 return 1;
156 }
157
158
159 static int
160 uci_lua_foreach(lua_State *L)
161 {
162 struct uci_package *p;
163 struct uci_element *e;
164 const char *package, *type;
165 bool ret = false;
166
167 package = luaL_checkstring(L, 1);
168
169 if (lua_isnil(L, 2))
170 type = NULL;
171 else
172 type = luaL_checkstring(L, 2);
173
174 if (!lua_isfunction(L, 3) || !package)
175 luaL_error(L, "Invalid argument");
176
177 p = find_package(L, package, true);
178 if (!p)
179 goto done;
180
181 uci_foreach_element(&p->sections, e) {
182 struct uci_section *s = uci_to_section(e);
183
184 if (type && (strcmp(s->type, type) != 0))
185 continue;
186
187 lua_pushvalue(L, 3); /* iterator function */
188 uci_push_section(L, s);
189 if (lua_pcall(L, 1, 0, 0) == 0)
190 ret = true;
191 }
192
193 done:
194 lua_pushboolean(L, ret);
195 return 1;
196 }
197
198 static int
199 uci_lua_get_any(lua_State *L, bool all)
200 {
201 struct uci_element *e = NULL;
202 struct uci_package *p = NULL;
203 const char *package = NULL;
204 const char *section = NULL;
205 const char *option = NULL;
206 char *s;
207 int err = UCI_ERR_MEM;
208 int n;
209
210 n = lua_gettop(L);
211
212 luaL_checkstring(L, 1);
213 s = strdup(lua_tostring(L, 1));
214 if (!s)
215 goto error;
216
217 if (n > 1) {
218 package = luaL_checkstring(L, 1);
219 section = luaL_checkstring(L, 2);
220 if (n > 2)
221 option = luaL_checkstring(L, 3);
222 } else {
223 if ((err = uci_parse_tuple(ctx, s, (char **) &package, (char **) &section, (char **) &option, NULL)))
224 goto error;
225 }
226
227 if (!all && (section == NULL)) {
228 err = UCI_ERR_INVAL;
229 goto error;
230 }
231
232 p = find_package(L, package, true);
233 if (!p) {
234 err = UCI_ERR_NOTFOUND;
235 goto error;
236 }
237
238 if (section) {
239 if ((err = uci_lookup(ctx, &e, p, section, option)))
240 goto error;
241 } else {
242 e = &p->e;
243 }
244
245 switch(e->type) {
246 case UCI_TYPE_PACKAGE:
247 uci_push_package(L, p);
248 break;
249 case UCI_TYPE_SECTION:
250 if (all)
251 uci_push_section(L, uci_to_section(e));
252 else
253 lua_pushstring(L, uci_to_section(e)->type);
254 break;
255 case UCI_TYPE_OPTION:
256 lua_pushstring(L, uci_to_option(e)->value);
257 break;
258 default:
259 err = UCI_ERR_INVAL;
260 goto error;
261 }
262 error:
263 if (s)
264 free(s);
265
266 switch(err) {
267 default:
268 ctx->err = err;
269 uci_lua_perror(L, "uci.get");
270 /* fall through */
271 case UCI_ERR_NOTFOUND:
272 lua_pushnil(L);
273 /* fall through */
274 case 0:
275 return 1;
276 }
277 }
278
279 static int
280 uci_lua_get(lua_State *L)
281 {
282 return uci_lua_get_any(L, false);
283 }
284
285 static int
286 uci_lua_get_all(lua_State *L)
287 {
288 return uci_lua_get_any(L, true);
289 }
290
291 static int
292 uci_lua_add(lua_State *L)
293 {
294 struct uci_section *s = NULL;
295 struct uci_package *p;
296 const char *package;
297 const char *type;
298 const char *name = NULL;
299
300 do {
301 package = luaL_checkstring(L, 1);
302 type = luaL_checkstring(L, 2);
303 p = find_package(L, package, true);
304 if (!p)
305 break;
306
307 if (uci_add_section(ctx, p, type, &s) || !s)
308 break;
309
310 name = s->e.name;
311 } while (0);
312
313 lua_pushstring(L, name);
314 return 1;
315 }
316
317 static int
318 uci_lua_set(lua_State *L)
319 {
320 struct uci_package *p;
321 const char *package = NULL;
322 const char *section = NULL;
323 const char *option = NULL;
324 const char *value = NULL;
325 const char *s;
326 int err = UCI_ERR_MEM;
327 int nargs;
328
329 nargs = lua_gettop(L);
330
331 s = luaL_checkstring(L, 1);
332 switch(nargs) {
333 case 1:
334 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
335 s = strdup(s);
336 if (!s)
337 goto error;
338
339 if ((err = uci_parse_tuple(ctx, (char *) s, (char **) &package, (char **) &section, (char **) &option, (char **) &value)))
340 goto error;
341 break;
342 case 4:
343 /* Format: uci.set("p", "s", "o", "v") */
344 option = luaL_checkstring(L, 3);
345 /* fall through */
346 case 3:
347 /* Format: uci.set("p", "s", "v") */
348 package = s;
349 section = luaL_checkstring(L, 2);
350 value = luaL_checkstring(L, nargs);
351 break;
352 default:
353 err = UCI_ERR_INVAL;
354 goto error;
355 }
356
357 if ((section == NULL) || (value == NULL)) {
358 err = UCI_ERR_INVAL;
359 goto error;
360 }
361
362 p = find_package(L, package, true);
363 if (!p) {
364 err = UCI_ERR_NOTFOUND;
365 goto error;
366 }
367 err = uci_set(ctx, p, section, option, value, NULL);
368
369 error:
370 if (err)
371 uci_lua_perror(L, "uci.set");
372 lua_pushboolean(L, (err == 0));
373 return 1;
374 }
375
376 enum pkg_cmd {
377 CMD_SAVE,
378 CMD_COMMIT,
379 CMD_REVERT
380 };
381
382 static int
383 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
384 {
385 struct uci_element *e, *tmp;
386 const char *s = NULL;
387 const char *section = NULL;
388 const char *option = NULL;
389 int failed = 0;
390 int nargs;
391
392 nargs = lua_gettop(L);
393 switch(nargs) {
394 case 3:
395 if (cmd != CMD_REVERT)
396 goto err;
397 luaL_checkstring(L, 1);
398 option = lua_tostring(L, -1);
399 lua_pop(L, 1);
400 /* fall through */
401 case 2:
402 if (cmd != CMD_REVERT)
403 goto err;
404 luaL_checkstring(L, 1);
405 section = lua_tostring(L, -1);
406 lua_pop(L, 1);
407 /* fall through */
408 case 1:
409 luaL_checkstring(L, 1);
410 s = lua_tostring(L, -1);
411 lua_pop(L, 1);
412 break;
413 case 0:
414 break;
415 default:
416 err:
417 luaL_error(L, "Invalid argument count");
418 break;
419 }
420
421 uci_foreach_element_safe(&ctx->root, tmp, e) {
422 struct uci_package *p = uci_to_package(e);
423 int ret = UCI_ERR_INVAL;
424
425 if (s && (strcmp(s, e->name) != 0))
426 continue;
427
428 switch(cmd) {
429 case CMD_COMMIT:
430 ret = uci_commit(ctx, &p, false);
431 break;
432 case CMD_SAVE:
433 ret = uci_save(ctx, p);
434 break;
435 case CMD_REVERT:
436 ret = uci_revert(ctx, &p, section, option);
437 break;
438 }
439
440 if (ret != 0)
441 failed = 1;
442 }
443
444 lua_pushboolean(L, !failed);
445 return 1;
446 }
447
448 static int
449 uci_lua_save(lua_State *L)
450 {
451 return uci_lua_package_cmd(L, CMD_SAVE);
452 }
453
454 static int
455 uci_lua_commit(lua_State *L)
456 {
457 return uci_lua_package_cmd(L, CMD_COMMIT);
458 }
459
460 static int
461 uci_lua_revert(lua_State *L)
462 {
463 return uci_lua_package_cmd(L, CMD_REVERT);
464 }
465
466 static int
467 uci_lua_set_confdir(lua_State *L)
468 {
469 int ret;
470
471 luaL_checkstring(L, 1);
472 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
473 lua_pushboolean(L, (ret == 0));
474 return 1;
475 }
476
477 static int
478 uci_lua_set_savedir(lua_State *L)
479 {
480 int ret;
481
482 luaL_checkstring(L, 1);
483 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
484 lua_pushboolean(L, (ret == 0));
485
486 return 1;
487 }
488
489 static const luaL_Reg uci[] = {
490 { "load", uci_lua_load },
491 { "unload", uci_lua_unload },
492 { "get", uci_lua_get },
493 { "get_all", uci_lua_get_all },
494 { "add", uci_lua_add },
495 { "set", uci_lua_set },
496 { "save", uci_lua_save },
497 { "commit", uci_lua_commit },
498 { "revert", uci_lua_revert },
499 { "foreach", uci_lua_foreach },
500 { "set_confdir", uci_lua_set_confdir },
501 { "set_savedir", uci_lua_set_savedir },
502 { NULL, NULL },
503 };
504
505
506 int
507 luaopen_uci(lua_State *L)
508 {
509 ctx = uci_alloc_context();
510 if (!ctx)
511 luaL_error(L, "Cannot allocate UCI context\n");
512 luaL_register(L, MODNAME, uci);
513
514 /* enable autoload by default */
515 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
516 lua_pushboolean(L, 1);
517 lua_setfield(L, -2, "autoload");
518 lua_pop(L, 1);
519
520 return 0;
521 }