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_pushboolean(L, s->anonymous);
179 lua_setfield(L, -2, ".anonymous");
180 lua_pushstring(L, s->type);
181 lua_setfield(L, -2, ".type");
182 lua_pushstring(L, s->e.name);
183 lua_setfield(L, -2, ".name");
185 uci_foreach_element(&s->options, e) {
186 struct uci_option *o = uci_to_option(e);
187 uci_push_option(L, o);
188 lua_setfield(L, -2, o->e.name);
192 static void uci_push_package(lua_State *L, struct uci_package *p)
194 struct uci_element *e;
198 uci_foreach_element(&p->sections, e) {
200 uci_push_section(L, uci_to_section(e));
201 lua_setfield(L, -2, e->name);
206 uci_lua_unload(lua_State *L)
208 struct uci_package *p;
211 luaL_checkstring(L, 1);
212 s = lua_tostring(L, -1);
213 p = find_package(L, s, AUTOLOAD_OFF);
216 lua_pushboolean(L, 1);
218 lua_pushboolean(L, 0);
224 uci_lua_load(lua_State *L)
226 struct uci_package *p = NULL;
230 lua_pop(L, 1); /* bool ret value of unload */
231 s = lua_tostring(L, -1);
233 if (uci_load(ctx, s, &p)) {
234 uci_lua_perror(L, "uci.load");
235 lua_pushboolean(L, 0);
237 lua_pushboolean(L, 1);
245 uci_lua_foreach(lua_State *L)
247 struct uci_package *p;
248 struct uci_element *e;
249 const char *package, *type;
252 package = luaL_checkstring(L, 1);
257 type = luaL_checkstring(L, 2);
259 if (!lua_isfunction(L, 3) || !package)
260 luaL_error(L, "Invalid argument");
262 p = find_package(L, package, AUTOLOAD_ON);
266 uci_foreach_element(&p->sections, e) {
267 struct uci_section *s = uci_to_section(e);
269 if (type && (strcmp(s->type, type) != 0))
272 lua_pushvalue(L, 3); /* iterator function */
273 uci_push_section(L, s);
274 if (lua_pcall(L, 1, 0, 0) == 0)
279 lua_pushboolean(L, ret);
284 uci_lua_get_any(lua_State *L, bool all)
286 struct uci_element *e = NULL;
289 int err = UCI_ERR_NOTFOUND;
291 if (lookup_args(L, &ptr, &s))
294 uci_lookup_ptr(ctx, &ptr, NULL, false);
295 if (!all && !ptr.s) {
303 case UCI_TYPE_PACKAGE:
304 uci_push_package(L, ptr.p);
306 case UCI_TYPE_SECTION:
308 uci_push_section(L, ptr.s);
310 lua_pushstring(L, ptr.s->type);
312 case UCI_TYPE_OPTION:
313 uci_push_option(L, ptr.o);
326 uci_lua_perror(L, "uci.get");
328 case UCI_ERR_NOTFOUND:
337 uci_lua_get(lua_State *L)
339 return uci_lua_get_any(L, false);
343 uci_lua_get_all(lua_State *L)
345 return uci_lua_get_any(L, true);
349 uci_lua_add(lua_State *L)
351 struct uci_section *s = NULL;
352 struct uci_package *p;
355 const char *name = NULL;
358 package = luaL_checkstring(L, 1);
359 type = luaL_checkstring(L, 2);
360 p = find_package(L, package, AUTOLOAD_ON);
364 if (uci_add_section(ctx, p, type, &s) || !s)
370 lua_pushstring(L, name);
375 uci_lua_delete(lua_State *L)
379 int err = UCI_ERR_NOTFOUND;
381 if (lookup_args(L, &ptr, &s))
384 err = uci_delete(ctx, &ptr);
390 uci_lua_perror(L, "uci.delete");
391 lua_pushboolean(L, (err == 0));
396 uci_lua_set(lua_State *L)
398 bool istable = false;
400 int err = UCI_ERR_MEM;
404 nargs = lua_gettop(L);
405 if (lookup_args(L, &ptr, &s))
410 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
413 /* Format: uci.set("p", "s", "o", "v") */
414 if (lua_istable(L, nargs)) {
415 if (lua_objlen(L, nargs) < 1)
416 luaL_error(L, "Cannot set an uci option to an empty table value");
417 lua_rawgeti(L, nargs, 1);
418 ptr.value = luaL_checkstring(L, -1);
422 ptr.value = luaL_checkstring(L, nargs);
426 /* Format: uci.set("p", "s", "v") */
427 ptr.value = ptr.option;
435 err = uci_lookup_ptr(ctx, &ptr, NULL, false);
439 if ((ptr.s == NULL) || (ptr.value == NULL)) {
444 err = uci_set(ctx, &ptr);
449 for (i = 2; i <= lua_objlen(L, nargs); i++) {
450 lua_rawgeti(L, nargs, i);
451 ptr.value = luaL_checkstring(L, -1);
452 err = uci_add_list(ctx, &ptr);
461 uci_lua_perror(L, "uci.set");
462 lua_pushboolean(L, (err == 0));
473 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
475 struct uci_element *e, *tmp;
481 nargs = lua_gettop(L);
482 if ((cmd != CMD_REVERT) && (nargs > 1))
485 if (lookup_args(L, &ptr, &s))
488 uci_lookup_ptr(ctx, &ptr, NULL, false);
490 uci_foreach_element_safe(&ctx->root, tmp, e) {
491 struct uci_package *p = uci_to_package(e);
492 int ret = UCI_ERR_INVAL;
494 if (ptr.p && (ptr.p != p))
500 ret = uci_commit(ctx, &p, false);
503 ret = uci_save(ctx, p);
506 ret = uci_revert(ctx, &ptr);
515 lua_pushboolean(L, !failed);
520 uci_lua_save(lua_State *L)
522 return uci_lua_package_cmd(L, CMD_SAVE);
526 uci_lua_commit(lua_State *L)
528 return uci_lua_package_cmd(L, CMD_COMMIT);
532 uci_lua_revert(lua_State *L)
534 return uci_lua_package_cmd(L, CMD_REVERT);
538 uci_lua_add_change(lua_State *L, struct uci_element *e)
540 struct uci_history *h;
543 h = uci_to_history(e);
547 lua_getfield(L, -1, h->section);
548 if (lua_isnil(L, -1)) {
551 lua_pushvalue(L, -1); /* copy for setfield */
552 lua_setfield(L, -3, h->section);
555 name = (h->e.name ? h->e.name : ".type");
557 lua_pushstring(L, h->value);
559 lua_pushstring(L, "");
560 lua_setfield(L, -2, name);
565 uci_lua_changes_pkg(lua_State *L, const char *package)
567 struct uci_package *p = NULL;
568 struct uci_element *e;
569 bool autoload = false;
571 p = find_package(L, package, AUTOLOAD_OFF);
574 p = find_package(L, package, AUTOLOAD_FORCE);
579 if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history))
583 uci_foreach_element(&p->saved_history, e) {
584 uci_lua_add_change(L, e);
586 uci_foreach_element(&p->history, e) {
587 uci_lua_add_change(L, e);
589 lua_setfield(L, -2, p->e.name);
597 uci_lua_changes(lua_State *L)
599 const char *package = NULL;
600 char **config = NULL;
604 nargs = lua_gettop(L);
607 package = luaL_checkstring(L, 1);
611 luaL_error(L, "invalid argument count");
616 uci_lua_changes_pkg(L, package);
618 if (uci_list_configs(ctx, &config) != 0)
621 for(i = 0; config[i] != NULL; i++) {
622 uci_lua_changes_pkg(L, config[i]);
631 uci_lua_set_confdir(lua_State *L)
635 luaL_checkstring(L, 1);
636 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
637 lua_pushboolean(L, (ret == 0));
642 uci_lua_set_savedir(lua_State *L)
646 luaL_checkstring(L, 1);
647 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
648 lua_pushboolean(L, (ret == 0));
653 static const luaL_Reg uci[] = {
654 { "load", uci_lua_load },
655 { "unload", uci_lua_unload },
656 { "get", uci_lua_get },
657 { "get_all", uci_lua_get_all },
658 { "add", uci_lua_add },
659 { "set", uci_lua_set },
660 { "save", uci_lua_save },
661 { "delete", uci_lua_delete },
662 { "commit", uci_lua_commit },
663 { "revert", uci_lua_revert },
664 { "changes", uci_lua_changes },
665 { "foreach", uci_lua_foreach },
666 { "set_confdir", uci_lua_set_confdir },
667 { "set_savedir", uci_lua_set_savedir },
673 luaopen_uci(lua_State *L)
675 ctx = uci_alloc_context();
677 luaL_error(L, "Cannot allocate UCI context\n");
678 luaL_register(L, MODNAME, uci);
680 /* enable autoload by default */
681 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
682 lua_pushboolean(L, 1);
683 lua_setfield(L, -2, "autoload");