2 * libuci plugin for Lua
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
15 #include <sys/types.h>
31 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
33 #define DPRINTF(...) do {} while (0)
36 static struct uci_context *ctx = NULL;
43 static struct uci_package *
44 find_package(lua_State *L, const char *str, enum autoload al)
46 struct uci_package *p = NULL;
47 struct uci_element *e;
51 sep = strchr(str, '.');
53 name = malloc(1 + sep - str);
55 luaL_error(L, "out of memory");
56 strncpy(name, str, sep - str);
61 uci_foreach_element(&ctx->root, e) {
62 if (strcmp(e->name, name) != 0)
65 p = uci_to_package(e);
69 if (al == AUTOLOAD_FORCE)
70 uci_load(ctx, name, &p);
73 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
74 lua_getfield(L, -1, "autoload");
75 if (!lua_isboolean(L, -1))
78 if (!lua_toboolean(L, -1))
81 uci_load(ctx, name, &p);
92 static void uci_lua_perror(lua_State *L, char *name)
94 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
95 lua_getfield(L, -1, "warn");
96 if (!lua_isboolean(L, -1))
98 if (lua_toboolean(L, -1) != 1)
100 uci_perror(ctx, name);
106 lookup_args(lua_State *L, struct uci_ptr *ptr, char **buf)
112 luaL_checkstring(L, 1);
113 s = strdup(lua_tostring(L, 1));
117 memset(ptr, 0, sizeof(struct uci_ptr));
118 if (!find_package(L, s, AUTOLOAD_ON))
124 ptr->option = luaL_checkstring(L, 3);
127 ptr->section = luaL_checkstring(L, 2);
128 ptr->package = luaL_checkstring(L, 1);
129 if (uci_lookup_ptr(ctx, ptr, NULL, false) != UCI_OK)
133 if (uci_lookup_ptr(ctx, ptr, s, false) != UCI_OK)
137 luaL_error(L, "invalid argument count");
150 static void uci_push_option(lua_State *L, struct uci_option *o)
152 struct uci_element *e;
156 case UCI_TYPE_STRING:
157 lua_pushstring(L, o->v.string);
161 uci_foreach_element(&o->v.list, e) {
163 lua_pushstring(L, e->name);
164 lua_rawseti(L, -2, i);
173 static void uci_push_section(lua_State *L, struct uci_section *s)
175 struct uci_element *e;
178 lua_pushstring(L, s->type);
179 lua_setfield(L, -2, ".type");
180 lua_pushstring(L, s->e.name);
181 lua_setfield(L, -2, ".name");
183 uci_foreach_element(&s->options, e) {
184 struct uci_option *o = uci_to_option(e);
185 uci_push_option(L, o);
186 lua_setfield(L, -2, o->e.name);
190 static void uci_push_package(lua_State *L, struct uci_package *p)
192 struct uci_element *e;
196 uci_foreach_element(&p->sections, e) {
198 uci_push_section(L, uci_to_section(e));
199 lua_setfield(L, -2, e->name);
204 uci_lua_unload(lua_State *L)
206 struct uci_package *p;
209 luaL_checkstring(L, 1);
210 s = lua_tostring(L, -1);
211 p = find_package(L, s, AUTOLOAD_OFF);
214 lua_pushboolean(L, 1);
216 lua_pushboolean(L, 0);
222 uci_lua_load(lua_State *L)
224 struct uci_package *p = NULL;
228 lua_pop(L, 1); /* bool ret value of unload */
229 s = lua_tostring(L, -1);
231 if (uci_load(ctx, s, &p)) {
232 uci_lua_perror(L, "uci.load");
233 lua_pushboolean(L, 0);
235 lua_pushboolean(L, 1);
243 uci_lua_foreach(lua_State *L)
245 struct uci_package *p;
246 struct uci_element *e;
247 const char *package, *type;
250 package = luaL_checkstring(L, 1);
255 type = luaL_checkstring(L, 2);
257 if (!lua_isfunction(L, 3) || !package)
258 luaL_error(L, "Invalid argument");
260 p = find_package(L, package, AUTOLOAD_ON);
264 uci_foreach_element(&p->sections, e) {
265 struct uci_section *s = uci_to_section(e);
267 if (type && (strcmp(s->type, type) != 0))
270 lua_pushvalue(L, 3); /* iterator function */
271 uci_push_section(L, s);
272 if (lua_pcall(L, 1, 0, 0) == 0)
277 lua_pushboolean(L, ret);
282 uci_lua_get_any(lua_State *L, bool all)
284 struct uci_element *e = NULL;
287 int err = UCI_ERR_NOTFOUND;
289 if (lookup_args(L, &ptr, &s))
292 uci_lookup_ptr(ctx, &ptr, NULL, false);
293 if (!all && !ptr.s) {
301 case UCI_TYPE_PACKAGE:
302 uci_push_package(L, ptr.p);
304 case UCI_TYPE_SECTION:
306 uci_push_section(L, ptr.s);
308 lua_pushstring(L, ptr.s->type);
310 case UCI_TYPE_OPTION:
311 uci_push_option(L, ptr.o);
324 uci_lua_perror(L, "uci.get");
326 case UCI_ERR_NOTFOUND:
335 uci_lua_get(lua_State *L)
337 return uci_lua_get_any(L, false);
341 uci_lua_get_all(lua_State *L)
343 return uci_lua_get_any(L, true);
347 uci_lua_add(lua_State *L)
349 struct uci_section *s = NULL;
350 struct uci_package *p;
353 const char *name = NULL;
356 package = luaL_checkstring(L, 1);
357 type = luaL_checkstring(L, 2);
358 p = find_package(L, package, AUTOLOAD_ON);
362 if (uci_add_section(ctx, p, type, &s) || !s)
368 lua_pushstring(L, name);
373 uci_lua_delete(lua_State *L)
377 int err = UCI_ERR_NOTFOUND;
379 if (lookup_args(L, &ptr, &s))
382 err = uci_delete(ctx, &ptr);
388 uci_lua_perror(L, "uci.delete");
389 lua_pushboolean(L, (err == 0));
394 uci_lua_set(lua_State *L)
396 bool istable = false;
398 int err = UCI_ERR_MEM;
402 nargs = lua_gettop(L);
403 if (lookup_args(L, &ptr, &s))
408 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
411 /* Format: uci.set("p", "s", "o", "v") */
412 if (lua_istable(L, nargs)) {
413 if (lua_objlen(L, nargs) < 1)
414 luaL_error(L, "Cannot set an uci option to an empty table value");
415 lua_rawgeti(L, nargs, 1);
416 ptr.value = luaL_checkstring(L, -1);
420 ptr.value = luaL_checkstring(L, nargs);
424 /* Format: uci.set("p", "s", "v") */
425 ptr.value = ptr.option;
433 err = uci_lookup_ptr(ctx, &ptr, NULL, false);
437 if ((ptr.s == NULL) || (ptr.value == NULL)) {
442 err = uci_set(ctx, &ptr);
447 for (i = 2; i <= lua_objlen(L, nargs); i++) {
448 lua_rawgeti(L, nargs, i);
449 ptr.value = luaL_checkstring(L, -1);
450 err = uci_add_list(ctx, &ptr);
459 uci_lua_perror(L, "uci.set");
460 lua_pushboolean(L, (err == 0));
471 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
473 struct uci_element *e, *tmp;
479 nargs = lua_gettop(L);
480 if ((cmd != CMD_REVERT) && (nargs > 1))
483 if (lookup_args(L, &ptr, &s))
486 uci_lookup_ptr(ctx, &ptr, NULL, false);
488 uci_foreach_element_safe(&ctx->root, tmp, e) {
489 struct uci_package *p = uci_to_package(e);
490 int ret = UCI_ERR_INVAL;
492 if (ptr.p && (ptr.p != p))
498 ret = uci_commit(ctx, &p, false);
501 ret = uci_save(ctx, p);
504 ret = uci_revert(ctx, &ptr);
513 lua_pushboolean(L, !failed);
518 uci_lua_save(lua_State *L)
520 return uci_lua_package_cmd(L, CMD_SAVE);
524 uci_lua_commit(lua_State *L)
526 return uci_lua_package_cmd(L, CMD_COMMIT);
530 uci_lua_revert(lua_State *L)
532 return uci_lua_package_cmd(L, CMD_REVERT);
536 uci_lua_add_change(lua_State *L, struct uci_element *e)
538 struct uci_history *h;
541 h = uci_to_history(e);
545 lua_getfield(L, -1, h->section);
546 if (lua_isnil(L, -1)) {
549 lua_pushvalue(L, -1); /* copy for setfield */
550 lua_setfield(L, -3, h->section);
553 name = (h->e.name ? h->e.name : ".type");
555 lua_pushstring(L, h->value);
557 lua_pushstring(L, "");
558 lua_setfield(L, -2, name);
563 uci_lua_changes_pkg(lua_State *L, const char *package)
565 struct uci_package *p = NULL;
566 struct uci_element *e;
567 bool autoload = false;
569 p = find_package(L, package, AUTOLOAD_OFF);
572 p = find_package(L, package, AUTOLOAD_FORCE);
577 if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history))
581 uci_foreach_element(&p->saved_history, e) {
582 uci_lua_add_change(L, e);
584 uci_foreach_element(&p->history, e) {
585 uci_lua_add_change(L, e);
587 lua_setfield(L, -2, p->e.name);
595 uci_lua_changes(lua_State *L)
597 const char *package = NULL;
598 char **config = NULL;
602 nargs = lua_gettop(L);
605 package = luaL_checkstring(L, 1);
609 luaL_error(L, "invalid argument count");
614 uci_lua_changes_pkg(L, package);
616 if (uci_list_configs(ctx, &config) != 0)
619 for(i = 0; config[i] != NULL; i++) {
620 uci_lua_changes_pkg(L, config[i]);
629 uci_lua_set_confdir(lua_State *L)
633 luaL_checkstring(L, 1);
634 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
635 lua_pushboolean(L, (ret == 0));
640 uci_lua_set_savedir(lua_State *L)
644 luaL_checkstring(L, 1);
645 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
646 lua_pushboolean(L, (ret == 0));
651 static const luaL_Reg uci[] = {
652 { "load", uci_lua_load },
653 { "unload", uci_lua_unload },
654 { "get", uci_lua_get },
655 { "get_all", uci_lua_get_all },
656 { "add", uci_lua_add },
657 { "set", uci_lua_set },
658 { "save", uci_lua_save },
659 { "delete", uci_lua_delete },
660 { "commit", uci_lua_commit },
661 { "revert", uci_lua_revert },
662 { "changes", uci_lua_changes },
663 { "foreach", uci_lua_foreach },
664 { "set_confdir", uci_lua_set_confdir },
665 { "set_savedir", uci_lua_set_savedir },
671 luaopen_uci(lua_State *L)
673 ctx = uci_alloc_context();
675 luaL_error(L, "Cannot allocate UCI context\n");
676 luaL_register(L, MODNAME, uci);
678 /* enable autoload by default */
679 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
680 lua_pushboolean(L, 1);
681 lua_setfield(L, -2, "autoload");