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>
28 #define METANAME MODNAME ".meta"
32 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
34 #define DPRINTF(...) do {} while (0)
37 static struct uci_context *global_ctx = NULL;
39 static struct uci_context *
40 find_context(lua_State *L, int *offset)
42 struct uci_context **ctx;
43 if (!lua_isuserdata(L, 1)) {
45 global_ctx = uci_alloc_context();
47 luaL_error(L, "failed to allocate UCI context");
55 ctx = luaL_checkudata(L, 1, METANAME);
57 luaL_error(L, "failed to get UCI context");
62 static struct uci_package *
63 find_package(lua_State *L, struct uci_context *ctx, const char *str, bool al)
65 struct uci_package *p = NULL;
66 struct uci_element *e;
70 sep = strchr(str, '.');
72 name = malloc(1 + sep - str);
74 luaL_error(L, "out of memory");
75 strncpy(name, str, sep - str);
80 uci_foreach_element(&ctx->root, e) {
81 if (strcmp(e->name, name) != 0)
84 p = uci_to_package(e);
89 uci_load(ctx, name, &p);
91 uci_load(ctx, name, &p);
101 uci_lua_perror(lua_State *L, struct uci_context *ctx, char *name)
103 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
104 lua_getfield(L, -1, "warn");
105 if (!lua_isboolean(L, -1))
107 if (lua_toboolean(L, -1) != 1)
109 uci_perror(ctx, name);
115 lookup_args(lua_State *L, struct uci_context *ctx, int offset, struct uci_ptr *ptr, char **buf)
121 luaL_checkstring(L, 1 + offset);
122 s = strdup(lua_tostring(L, 1 + offset));
126 memset(ptr, 0, sizeof(struct uci_ptr));
127 if (!find_package(L, ctx, s, true))
130 switch (n - offset) {
133 ptr->option = luaL_checkstring(L, 3 + offset);
136 ptr->section = luaL_checkstring(L, 2 + offset);
137 ptr->package = luaL_checkstring(L, 1 + offset);
138 if (uci_lookup_ptr(ctx, ptr, NULL, false) != UCI_OK)
142 if (uci_lookup_ptr(ctx, ptr, s, false) != UCI_OK)
146 luaL_error(L, "invalid argument count");
160 uci_push_option(lua_State *L, struct uci_option *o)
162 struct uci_element *e;
166 case UCI_TYPE_STRING:
167 lua_pushstring(L, o->v.string);
171 uci_foreach_element(&o->v.list, e) {
173 lua_pushstring(L, e->name);
174 lua_rawseti(L, -2, i);
184 uci_push_section(lua_State *L, struct uci_section *s)
186 struct uci_element *e;
189 lua_pushboolean(L, s->anonymous);
190 lua_setfield(L, -2, ".anonymous");
191 lua_pushstring(L, s->type);
192 lua_setfield(L, -2, ".type");
193 lua_pushstring(L, s->e.name);
194 lua_setfield(L, -2, ".name");
196 uci_foreach_element(&s->options, e) {
197 struct uci_option *o = uci_to_option(e);
198 uci_push_option(L, o);
199 lua_setfield(L, -2, o->e.name);
204 uci_push_package(lua_State *L, struct uci_package *p)
206 struct uci_element *e;
210 uci_foreach_element(&p->sections, e) {
212 uci_push_section(L, uci_to_section(e));
213 lua_setfield(L, -2, e->name);
218 uci_lua_unload(lua_State *L)
220 struct uci_context *ctx;
221 struct uci_package *p;
225 ctx = find_context(L, &offset);
226 luaL_checkstring(L, 1 + offset);
227 s = lua_tostring(L, 1 + offset);
228 p = find_package(L, ctx, s, false);
231 lua_pushboolean(L, 1);
233 lua_pushboolean(L, 0);
239 uci_lua_load(lua_State *L)
241 struct uci_context *ctx;
242 struct uci_package *p = NULL;
246 ctx = find_context(L, &offset);
248 lua_pop(L, 1); /* bool ret value of unload */
249 s = lua_tostring(L, -1);
251 if (uci_load(ctx, s, &p)) {
252 uci_lua_perror(L, ctx, "uci.load");
253 lua_pushboolean(L, 0);
255 lua_pushboolean(L, 1);
263 uci_lua_foreach(lua_State *L)
265 struct uci_context *ctx;
266 struct uci_package *p;
267 struct uci_element *e;
268 const char *package, *type;
272 ctx = find_context(L, &offset);
273 package = luaL_checkstring(L, 1 + offset);
278 type = luaL_checkstring(L, 2 + offset);
280 if (!lua_isfunction(L, 3 + offset) || !package)
281 luaL_error(L, "Invalid argument");
283 p = find_package(L, ctx, package, true);
287 uci_foreach_element(&p->sections, e) {
288 struct uci_section *s = uci_to_section(e);
290 if (type && (strcmp(s->type, type) != 0))
293 lua_pushvalue(L, 3 + offset); /* iterator function */
294 uci_push_section(L, s);
295 if (lua_pcall(L, 1, 0, 0) == 0)
300 lua_pushboolean(L, ret);
305 uci_lua_get_any(lua_State *L, bool all)
307 struct uci_context *ctx;
308 struct uci_element *e = NULL;
312 int err = UCI_ERR_NOTFOUND;
314 ctx = find_context(L, &offset);
316 if (lookup_args(L, ctx, offset, &ptr, &s))
319 uci_lookup_ptr(ctx, &ptr, NULL, false);
320 if (!all && !ptr.s) {
324 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
325 err = UCI_ERR_NOTFOUND;
332 case UCI_TYPE_PACKAGE:
333 uci_push_package(L, ptr.p);
335 case UCI_TYPE_SECTION:
337 uci_push_section(L, ptr.s);
339 lua_pushstring(L, ptr.s->type);
341 case UCI_TYPE_OPTION:
342 uci_push_option(L, ptr.o);
355 uci_lua_perror(L, ctx, "uci.get");
357 case UCI_ERR_NOTFOUND:
366 uci_lua_get(lua_State *L)
368 return uci_lua_get_any(L, false);
372 uci_lua_get_all(lua_State *L)
374 return uci_lua_get_any(L, true);
378 uci_lua_add(lua_State *L)
380 struct uci_context *ctx;
381 struct uci_section *s = NULL;
382 struct uci_package *p;
385 const char *name = NULL;
388 ctx = find_context(L, &offset);
389 package = luaL_checkstring(L, 1 + offset);
390 type = luaL_checkstring(L, 2 + offset);
391 p = find_package(L, ctx, package, true);
395 if (uci_add_section(ctx, p, type, &s) || !s)
399 lua_pushstring(L, name);
408 uci_lua_delete(lua_State *L)
410 struct uci_context *ctx;
414 int err = UCI_ERR_NOTFOUND;
416 ctx = find_context(L, &offset);
418 if (lookup_args(L, ctx, offset, &ptr, &s))
421 err = uci_delete(ctx, &ptr);
427 uci_lua_perror(L, ctx, "uci.delete");
428 lua_pushboolean(L, (err == 0));
433 uci_lua_set(lua_State *L)
435 struct uci_context *ctx;
437 bool istable = false;
438 int err = UCI_ERR_MEM;
440 int i, nargs, offset = 0;
442 ctx = find_context(L, &offset);
443 nargs = lua_gettop(L);
444 if (lookup_args(L, ctx, offset, &ptr, &s))
447 switch(nargs - offset) {
449 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
452 /* Format: uci.set("p", "s", "o", "v") */
453 if (lua_istable(L, nargs)) {
454 if (lua_objlen(L, nargs) < 1)
455 luaL_error(L, "Cannot set an uci option to an empty table value");
456 lua_rawgeti(L, nargs, 1);
457 ptr.value = luaL_checkstring(L, -1);
461 ptr.value = luaL_checkstring(L, nargs);
465 /* Format: uci.set("p", "s", "v") */
466 ptr.value = ptr.option;
474 err = uci_lookup_ptr(ctx, &ptr, NULL, false);
478 if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
483 err = uci_set(ctx, &ptr);
488 for (i = 2; i <= lua_objlen(L, nargs); i++) {
489 lua_rawgeti(L, nargs, i);
490 ptr.value = luaL_checkstring(L, -1);
491 err = uci_add_list(ctx, &ptr);
500 uci_lua_perror(L, ctx, "uci.set");
501 lua_pushboolean(L, (err == 0));
512 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
514 struct uci_context *ctx;
515 struct uci_element *e, *tmp;
519 int nargs, offset = 0;
521 ctx = find_context(L, &offset);
522 nargs = lua_gettop(L);
523 if ((cmd != CMD_REVERT) && (nargs > 1))
526 if (lookup_args(L, ctx, offset, &ptr, &s))
529 uci_lookup_ptr(ctx, &ptr, NULL, false);
531 uci_foreach_element_safe(&ctx->root, tmp, e) {
532 struct uci_package *p = uci_to_package(e);
533 int ret = UCI_ERR_INVAL;
535 if (ptr.p && (ptr.p != p))
541 ret = uci_commit(ctx, &p, false);
544 ret = uci_save(ctx, p);
547 ret = uci_revert(ctx, &ptr);
556 lua_pushboolean(L, !failed);
561 uci_lua_save(lua_State *L)
563 return uci_lua_package_cmd(L, CMD_SAVE);
567 uci_lua_commit(lua_State *L)
569 return uci_lua_package_cmd(L, CMD_COMMIT);
573 uci_lua_revert(lua_State *L)
575 return uci_lua_package_cmd(L, CMD_REVERT);
579 uci_lua_add_change(lua_State *L, struct uci_element *e)
581 struct uci_history *h;
584 h = uci_to_history(e);
588 lua_getfield(L, -1, h->section);
589 if (lua_isnil(L, -1)) {
592 lua_pushvalue(L, -1); /* copy for setfield */
593 lua_setfield(L, -3, h->section);
596 name = (h->e.name ? h->e.name : ".type");
598 lua_pushstring(L, h->value);
600 lua_pushstring(L, "");
601 lua_setfield(L, -2, name);
606 uci_lua_changes_pkg(lua_State *L, struct uci_context *ctx, const char *package)
608 struct uci_package *p = NULL;
609 struct uci_element *e;
610 bool autoload = false;
612 p = find_package(L, ctx, package, false);
615 p = find_package(L, ctx, package, true);
620 if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history))
624 uci_foreach_element(&p->saved_history, e) {
625 uci_lua_add_change(L, e);
627 uci_foreach_element(&p->history, e) {
628 uci_lua_add_change(L, e);
630 lua_setfield(L, -2, p->e.name);
638 uci_lua_changes(lua_State *L)
640 struct uci_context *ctx;
641 const char *package = NULL;
642 char **config = NULL;
646 ctx = find_context(L, &offset);
647 nargs = lua_gettop(L);
648 switch(nargs - offset) {
650 package = luaL_checkstring(L, 1 + offset);
654 luaL_error(L, "invalid argument count");
659 uci_lua_changes_pkg(L, ctx, package);
661 if (uci_list_configs(ctx, &config) != 0)
664 for(i = 0; config[i] != NULL; i++) {
665 uci_lua_changes_pkg(L, ctx, config[i]);
674 uci_lua_get_confdir(lua_State *L)
676 struct uci_context *ctx = find_context(L, NULL);
677 lua_pushstring(L, ctx->confdir);
682 uci_lua_set_confdir(lua_State *L)
684 struct uci_context *ctx;
687 ctx = find_context(L, &offset);
688 luaL_checkstring(L, 1 + offset);
689 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
690 lua_pushboolean(L, (ret == 0));
695 uci_lua_get_savedir(lua_State *L)
697 struct uci_context *ctx = find_context(L, NULL);
698 lua_pushstring(L, ctx->savedir);
703 uci_lua_set_savedir(lua_State *L)
705 struct uci_context *ctx;
708 ctx = find_context(L, &offset);
709 luaL_checkstring(L, 1 + offset);
710 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
711 lua_pushboolean(L, (ret == 0));
717 uci_lua_gc(lua_State *L)
719 struct uci_context *ctx = find_context(L, NULL);
720 uci_free_context(ctx);
725 uci_lua_cursor(lua_State *L)
727 struct uci_context **u;
728 int argc = lua_gettop(L);
730 u = lua_newuserdata(L, sizeof(struct uci_context *));
731 luaL_getmetatable(L, METANAME);
732 lua_setmetatable(L, -2);
734 *u = uci_alloc_context();
736 luaL_error(L, "Cannot allocate UCI context");
739 if (lua_isstring(L, 2) &&
740 (uci_set_savedir(*u, luaL_checkstring(L, 2)) != UCI_OK))
741 luaL_error(L, "Unable to set savedir");
744 if (lua_isstring(L, 1) &&
745 (uci_set_confdir(*u, luaL_checkstring(L, 1)) != UCI_OK))
746 luaL_error(L, "Unable to set savedir");
754 static const luaL_Reg uci[] = {
755 { "__gc", uci_lua_gc },
756 { "cursor", uci_lua_cursor },
757 { "load", uci_lua_load },
758 { "unload", uci_lua_unload },
759 { "get", uci_lua_get },
760 { "get_all", uci_lua_get_all },
761 { "add", uci_lua_add },
762 { "set", uci_lua_set },
763 { "save", uci_lua_save },
764 { "delete", uci_lua_delete },
765 { "commit", uci_lua_commit },
766 { "revert", uci_lua_revert },
767 { "changes", uci_lua_changes },
768 { "foreach", uci_lua_foreach },
769 { "get_confdir", uci_lua_get_confdir },
770 { "set_confdir", uci_lua_set_confdir },
771 { "get_savedir", uci_lua_get_savedir },
772 { "set_savedir", uci_lua_set_savedir },
778 luaopen_uci(lua_State *L)
780 /* create metatable */
781 luaL_newmetatable(L, METANAME);
783 /* metatable.__index = metatable */
784 lua_pushvalue(L, -1);
785 lua_setfield(L, -2, "__index");
788 luaL_register(L, NULL, uci);
792 luaL_register(L, MODNAME, uci);