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))
123 ptr->option = luaL_checkstring(L, 3);
126 ptr->section = luaL_checkstring(L, 2);
127 ptr->package = luaL_checkstring(L, 1);
128 if (uci_lookup_ptr(ctx, ptr, NULL, false) != UCI_OK)
132 if (uci_lookup_ptr(ctx, ptr, s, false) != UCI_OK)
148 static void uci_push_option(lua_State *L, struct uci_option *o)
150 struct uci_element *e;
154 case UCI_TYPE_STRING:
155 lua_pushstring(L, o->v.string);
159 uci_foreach_element(&o->v.list, e) {
161 lua_pushstring(L, e->name);
162 lua_rawseti(L, -2, i);
171 static void uci_push_section(lua_State *L, struct uci_section *s)
173 struct uci_element *e;
176 lua_pushstring(L, s->type);
177 lua_setfield(L, -2, ".type");
178 lua_pushstring(L, s->e.name);
179 lua_setfield(L, -2, ".name");
181 uci_foreach_element(&s->options, e) {
182 struct uci_option *o = uci_to_option(e);
183 uci_push_option(L, o);
184 lua_setfield(L, -2, o->e.name);
188 static void uci_push_package(lua_State *L, struct uci_package *p)
190 struct uci_element *e;
194 uci_foreach_element(&p->sections, e) {
196 uci_push_section(L, uci_to_section(e));
197 lua_setfield(L, -2, e->name);
202 uci_lua_unload(lua_State *L)
204 struct uci_package *p;
207 luaL_checkstring(L, 1);
208 s = lua_tostring(L, -1);
209 p = find_package(L, s, AUTOLOAD_OFF);
212 lua_pushboolean(L, 1);
214 lua_pushboolean(L, 0);
220 uci_lua_load(lua_State *L)
222 struct uci_package *p = NULL;
226 lua_pop(L, 1); /* bool ret value of unload */
227 s = lua_tostring(L, -1);
229 if (uci_load(ctx, s, &p)) {
230 uci_lua_perror(L, "uci.load");
231 lua_pushboolean(L, 0);
233 lua_pushboolean(L, 1);
241 uci_lua_foreach(lua_State *L)
243 struct uci_package *p;
244 struct uci_element *e;
245 const char *package, *type;
248 package = luaL_checkstring(L, 1);
253 type = luaL_checkstring(L, 2);
255 if (!lua_isfunction(L, 3) || !package)
256 luaL_error(L, "Invalid argument");
258 p = find_package(L, package, AUTOLOAD_ON);
262 uci_foreach_element(&p->sections, e) {
263 struct uci_section *s = uci_to_section(e);
265 if (type && (strcmp(s->type, type) != 0))
268 lua_pushvalue(L, 3); /* iterator function */
269 uci_push_section(L, s);
270 if (lua_pcall(L, 1, 0, 0) == 0)
275 lua_pushboolean(L, ret);
280 uci_lua_get_any(lua_State *L, bool all)
282 struct uci_element *e = NULL;
285 int err = UCI_ERR_NOTFOUND;
287 if (lookup_args(L, &ptr, &s))
290 uci_lookup_ptr(ctx, &ptr, NULL, false);
291 if (!all && !ptr.s) {
299 case UCI_TYPE_PACKAGE:
300 uci_push_package(L, ptr.p);
302 case UCI_TYPE_SECTION:
304 uci_push_section(L, ptr.s);
306 lua_pushstring(L, ptr.s->type);
308 case UCI_TYPE_OPTION:
309 uci_push_option(L, ptr.o);
322 uci_lua_perror(L, "uci.get");
324 case UCI_ERR_NOTFOUND:
333 uci_lua_get(lua_State *L)
335 return uci_lua_get_any(L, false);
339 uci_lua_get_all(lua_State *L)
341 return uci_lua_get_any(L, true);
345 uci_lua_add(lua_State *L)
347 struct uci_section *s = NULL;
348 struct uci_package *p;
351 const char *name = NULL;
354 package = luaL_checkstring(L, 1);
355 type = luaL_checkstring(L, 2);
356 p = find_package(L, package, AUTOLOAD_ON);
360 if (uci_add_section(ctx, p, type, &s) || !s)
366 lua_pushstring(L, name);
371 uci_lua_delete(lua_State *L)
375 int err = UCI_ERR_NOTFOUND;
377 if (lookup_args(L, &ptr, &s))
380 err = uci_delete(ctx, &ptr);
386 uci_lua_perror(L, "uci.delete");
387 lua_pushboolean(L, (err == 0));
392 uci_lua_set(lua_State *L)
394 bool istable = false;
396 int err = UCI_ERR_MEM;
400 nargs = lua_gettop(L);
401 if (lookup_args(L, &ptr, &s))
406 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
409 /* Format: uci.set("p", "s", "o", "v") */
410 if (lua_istable(L, nargs)) {
411 if (lua_objlen(L, nargs) < 1)
412 luaL_error(L, "Cannot set an uci option to an empty table value");
413 lua_rawgeti(L, nargs, 1);
414 ptr.value = luaL_checkstring(L, -1);
418 ptr.value = luaL_checkstring(L, nargs);
422 /* Format: uci.set("p", "s", "v") */
423 ptr.value = ptr.option;
431 err = uci_lookup_ptr(ctx, &ptr, NULL, false);
435 if ((ptr.s == NULL) || (ptr.value == NULL)) {
440 err = uci_set(ctx, &ptr);
445 for (i = 2; i <= lua_objlen(L, nargs); i++) {
446 lua_rawgeti(L, nargs, i);
447 ptr.value = luaL_checkstring(L, -1);
448 err = uci_add_list(ctx, &ptr);
457 uci_lua_perror(L, "uci.set");
458 lua_pushboolean(L, (err == 0));
469 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
471 struct uci_element *e, *tmp;
477 nargs = lua_gettop(L);
478 if ((cmd != CMD_REVERT) && (nargs > 1))
481 if (lookup_args(L, &ptr, &s))
484 uci_lookup_ptr(ctx, &ptr, NULL, false);
486 uci_foreach_element_safe(&ctx->root, tmp, e) {
487 struct uci_package *p = uci_to_package(e);
488 int ret = UCI_ERR_INVAL;
490 if (ptr.p && (ptr.p != p))
496 ret = uci_commit(ctx, &p, false);
499 ret = uci_save(ctx, p);
502 ret = uci_revert(ctx, &ptr);
511 lua_pushboolean(L, !failed);
516 uci_lua_save(lua_State *L)
518 return uci_lua_package_cmd(L, CMD_SAVE);
522 uci_lua_commit(lua_State *L)
524 return uci_lua_package_cmd(L, CMD_COMMIT);
528 uci_lua_revert(lua_State *L)
530 return uci_lua_package_cmd(L, CMD_REVERT);
534 uci_lua_add_change(lua_State *L, struct uci_element *e)
536 struct uci_history *h;
539 h = uci_to_history(e);
543 lua_getfield(L, -1, h->section);
544 if (lua_isnil(L, -1)) {
547 lua_pushvalue(L, -1); /* copy for setfield */
548 lua_setfield(L, -3, h->section);
551 name = (h->e.name ? h->e.name : ".type");
553 lua_pushstring(L, h->value);
555 lua_pushstring(L, "");
556 lua_setfield(L, -2, name);
561 uci_lua_changes_pkg(lua_State *L, const char *package)
563 struct uci_package *p = NULL;
564 struct uci_element *e;
565 bool autoload = false;
567 p = find_package(L, package, AUTOLOAD_OFF);
570 p = find_package(L, package, AUTOLOAD_FORCE);
575 if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history))
579 uci_foreach_element(&p->saved_history, e) {
580 uci_lua_add_change(L, e);
582 uci_foreach_element(&p->history, e) {
583 uci_lua_add_change(L, e);
585 lua_setfield(L, -2, p->e.name);
593 uci_lua_changes(lua_State *L)
595 const char *package = NULL;
596 char **config = NULL;
600 nargs = lua_gettop(L);
603 package = luaL_checkstring(L, 1);
607 luaL_error(L, "invalid argument count");
612 uci_lua_changes_pkg(L, package);
614 if (uci_list_configs(ctx, &config) != 0)
617 for(i = 0; config[i] != NULL; i++) {
618 uci_lua_changes_pkg(L, config[i]);
627 uci_lua_set_confdir(lua_State *L)
631 luaL_checkstring(L, 1);
632 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
633 lua_pushboolean(L, (ret == 0));
638 uci_lua_set_savedir(lua_State *L)
642 luaL_checkstring(L, 1);
643 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
644 lua_pushboolean(L, (ret == 0));
649 static const luaL_Reg uci[] = {
650 { "load", uci_lua_load },
651 { "unload", uci_lua_unload },
652 { "get", uci_lua_get },
653 { "get_all", uci_lua_get_all },
654 { "add", uci_lua_add },
655 { "set", uci_lua_set },
656 { "save", uci_lua_save },
657 { "delete", uci_lua_delete },
658 { "commit", uci_lua_commit },
659 { "revert", uci_lua_revert },
660 { "changes", uci_lua_changes },
661 { "foreach", uci_lua_foreach },
662 { "set_confdir", uci_lua_set_confdir },
663 { "set_savedir", uci_lua_set_savedir },
669 luaopen_uci(lua_State *L)
671 ctx = uci_alloc_context();
673 luaL_error(L, "Cannot allocate UCI context\n");
674 luaL_register(L, MODNAME, uci);
676 /* enable autoload by default */
677 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
678 lua_pushboolean(L, 1);
679 lua_setfield(L, -2, "autoload");