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 #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
40 * ** Adapted from Lua 5.2.0
42 static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
43 luaL_checkstack(L, nup+1, "too many upvalues");
44 for (; l->name != NULL; l++) { /* fill the table with given functions */
46 lua_pushstring(L, l->name);
47 for (i = 0; i < nup; i++) /* copy upvalues to the top */
48 lua_pushvalue(L, -(nup+1));
49 lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
50 lua_settable(L, -(nup + 3));
52 lua_pop(L, nup); /* remove upvalues */
55 #define lua_rawlen(L, i) lua_objlen(L, i)
59 static struct uci_context *global_ctx = NULL;
61 static struct uci_context *
62 find_context(lua_State *L, int *offset)
64 struct uci_context **ctx;
65 if (!lua_isuserdata(L, 1)) {
67 global_ctx = uci_alloc_context();
69 luaL_error(L, "failed to allocate UCI context");
79 ctx = luaL_checkudata(L, 1, METANAME);
81 luaL_error(L, "failed to get UCI context");
88 static struct uci_package *
89 find_package(lua_State *L, struct uci_context *ctx, const char *str, bool al)
91 struct uci_package *p = NULL;
92 struct uci_element *e;
96 sep = strchr(str, '.');
98 name = malloc(1 + sep - str);
100 luaL_error(L, "out of memory");
103 strncpy(name, str, sep - str);
108 uci_foreach_element(&ctx->root, e) {
109 if (strcmp(e->name, name) != 0)
112 p = uci_to_package(e);
117 uci_load(ctx, name, &p);
119 uci_load(ctx, name, &p);
129 lookup_args(lua_State *L, struct uci_context *ctx, int offset, struct uci_ptr *ptr, char **buf)
135 luaL_checkstring(L, 1 + offset);
136 s = strdup(lua_tostring(L, 1 + offset));
140 memset(ptr, 0, sizeof(struct uci_ptr));
141 if (!find_package(L, ctx, s, true))
144 switch (n - offset) {
147 ptr->option = luaL_checkstring(L, 3 + offset);
150 ptr->section = luaL_checkstring(L, 2 + offset);
151 ptr->package = luaL_checkstring(L, 1 + offset);
152 if (uci_lookup_ptr(ctx, ptr, NULL, true) != UCI_OK)
156 if (uci_lookup_ptr(ctx, ptr, s, true) != UCI_OK)
160 luaL_error(L, "invalid argument count");
174 uci_push_status(lua_State *L, struct uci_context *ctx, bool hasarg)
179 lua_pushboolean(L, (ctx->err == UCI_OK));
181 uci_get_errorstr(ctx, &str, MODNAME);
183 lua_pushstring(L, str);
192 uci_push_option(lua_State *L, struct uci_option *o)
194 struct uci_element *e;
198 case UCI_TYPE_STRING:
199 lua_pushstring(L, o->v.string);
203 uci_foreach_element(&o->v.list, e) {
205 lua_pushstring(L, e->name);
206 lua_rawseti(L, -2, i);
216 uci_push_section(lua_State *L, struct uci_section *s, int index)
218 struct uci_element *e;
221 lua_pushboolean(L, s->anonymous);
222 lua_setfield(L, -2, ".anonymous");
223 lua_pushstring(L, s->type);
224 lua_setfield(L, -2, ".type");
225 lua_pushstring(L, s->e.name);
226 lua_setfield(L, -2, ".name");
228 lua_pushinteger(L, index);
229 lua_setfield(L, -2, ".index");
232 uci_foreach_element(&s->options, e) {
233 struct uci_option *o = uci_to_option(e);
234 uci_push_option(L, o);
235 lua_setfield(L, -2, o->e.name);
240 uci_push_package(lua_State *L, struct uci_package *p)
242 struct uci_element *e;
246 uci_foreach_element(&p->sections, e) {
247 uci_push_section(L, uci_to_section(e), i);
248 lua_setfield(L, -2, e->name);
254 uci_lua_unload(lua_State *L)
256 struct uci_context *ctx;
257 struct uci_package *p;
261 ctx = find_context(L, &offset);
262 luaL_checkstring(L, 1 + offset);
263 s = lua_tostring(L, 1 + offset);
264 p = find_package(L, ctx, s, false);
267 return uci_push_status(L, ctx, false);
269 lua_pushboolean(L, 0);
275 uci_lua_load(lua_State *L)
277 struct uci_context *ctx;
278 struct uci_package *p = NULL;
282 ctx = find_context(L, &offset);
284 lua_pop(L, 1); /* bool ret value of unload */
285 s = lua_tostring(L, -1);
287 uci_load(ctx, s, &p);
288 return uci_push_status(L, ctx, false);
293 uci_lua_foreach(lua_State *L)
295 struct uci_context *ctx;
296 struct uci_package *p;
297 struct uci_element *e, *tmp;
298 const char *package, *type;
303 ctx = find_context(L, &offset);
304 package = luaL_checkstring(L, 1 + offset);
306 if (lua_isnil(L, 2 + offset))
309 type = luaL_checkstring(L, 2 + offset);
311 if (!lua_isfunction(L, 3 + offset) || !package)
312 return luaL_error(L, "Invalid argument");
314 p = find_package(L, ctx, package, true);
318 uci_foreach_element_safe(&p->sections, tmp, e) {
319 struct uci_section *s = uci_to_section(e);
323 if (type && (strcmp(s->type, type) != 0))
326 lua_pushvalue(L, 3 + offset); /* iterator function */
327 uci_push_section(L, s, i - 1);
328 if (lua_pcall(L, 1, 1, 0) == 0) {
330 if (lua_isboolean(L, -1) && !lua_toboolean(L, -1))
341 lua_pushboolean(L, ret);
346 uci_lua_get_any(lua_State *L, bool all)
348 struct uci_context *ctx;
349 struct uci_element *e = NULL;
353 int err = UCI_ERR_NOTFOUND;
355 ctx = find_context(L, &offset);
357 if (lookup_args(L, ctx, offset, &ptr, &s))
360 uci_lookup_ptr(ctx, &ptr, NULL, true);
361 if (!all && !ptr.s) {
365 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
366 err = UCI_ERR_NOTFOUND;
373 case UCI_TYPE_PACKAGE:
374 uci_push_package(L, ptr.p);
376 case UCI_TYPE_SECTION:
378 uci_push_section(L, ptr.s, -1);
380 lua_pushstring(L, ptr.s->type);
382 case UCI_TYPE_OPTION:
383 uci_push_option(L, ptr.o);
399 return uci_push_status(L, ctx, true);
403 uci_lua_get(lua_State *L)
405 return uci_lua_get_any(L, false);
409 uci_lua_get_all(lua_State *L)
411 return uci_lua_get_any(L, true);
415 uci_lua_add(lua_State *L)
417 struct uci_context *ctx;
418 struct uci_section *s = NULL;
419 struct uci_package *p;
422 const char *name = NULL;
425 ctx = find_context(L, &offset);
426 package = luaL_checkstring(L, 1 + offset);
427 type = luaL_checkstring(L, 2 + offset);
428 p = find_package(L, ctx, package, true);
432 if (uci_add_section(ctx, p, type, &s) || !s)
436 lua_pushstring(L, name);
441 return uci_push_status(L, ctx, true);
445 uci_lua_delete(lua_State *L)
447 struct uci_context *ctx;
452 ctx = find_context(L, &offset);
454 if (lookup_args(L, ctx, offset, &ptr, &s))
457 uci_delete(ctx, &ptr);
462 return uci_push_status(L, ctx, false);
466 uci_lua_rename(lua_State *L)
468 struct uci_context *ctx;
470 int err = UCI_ERR_MEM;
472 int nargs, offset = 0;
474 ctx = find_context(L, &offset);
475 nargs = lua_gettop(L);
476 if (lookup_args(L, ctx, offset, &ptr, &s))
479 switch(nargs - offset) {
481 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
484 /* Format: uci.set("p", "s", "o", "v") */
485 ptr.value = luaL_checkstring(L, nargs);
488 /* Format: uci.set("p", "s", "v") */
489 ptr.value = ptr.option;
497 err = uci_lookup_ptr(ctx, &ptr, NULL, true);
501 if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
506 err = uci_rename(ctx, &ptr);
513 return uci_push_status(L, ctx, false);
517 uci_lua_reorder(lua_State *L)
519 struct uci_context *ctx;
521 int err = UCI_ERR_MEM;
523 int nargs, offset = 0;
525 ctx = find_context(L, &offset);
526 nargs = lua_gettop(L);
527 if (lookup_args(L, ctx, offset, &ptr, &s))
530 switch(nargs - offset) {
532 /* Format: uci.set("p.s=v") or uci.set("p.s=v") */
539 /* Format: uci.set("p", "s", "v") */
540 ptr.value = ptr.option;
548 err = uci_lookup_ptr(ctx, &ptr, NULL, true);
552 if ((ptr.s == NULL) || (ptr.value == NULL)) {
557 err = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
564 return uci_push_status(L, ctx, false);
569 uci_lua_set(lua_State *L)
571 struct uci_context *ctx;
573 bool istable = false;
574 int err = UCI_ERR_MEM;
577 int i, nargs, offset = 0;
579 ctx = find_context(L, &offset);
580 nargs = lua_gettop(L);
581 if (lookup_args(L, ctx, offset, &ptr, &s))
584 switch(nargs - offset) {
586 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
589 /* Format: uci.set("p", "s", "o", "v") */
590 if (lua_istable(L, nargs)) {
591 if (lua_rawlen(L, nargs) < 1)
592 return luaL_error(L, "Cannot set an uci option to an empty table value");
593 lua_rawgeti(L, nargs, 1);
594 ptr.value = luaL_checkstring(L, -1);
598 ptr.value = luaL_checkstring(L, nargs);
602 /* Format: uci.set("p", "s", "v") */
603 ptr.value = ptr.option;
611 err = uci_lookup_ptr(ctx, &ptr, NULL, true);
615 if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
621 if (lua_rawlen(L, nargs) == 1) {
626 err = uci_delete(ctx, &ptr);
633 err = uci_set(ctx, &ptr);
638 for (; i <= lua_rawlen(L, nargs); i++) {
639 lua_rawgeti(L, nargs, i);
640 ptr.value = luaL_checkstring(L, -1);
641 err = uci_add_list(ctx, &ptr);
647 err = uci_set(ctx, &ptr);
656 return uci_push_status(L, ctx, false);
666 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
668 struct uci_context *ctx;
669 struct uci_element *e, *tmp;
672 int nargs, offset = 0;
674 ctx = find_context(L, &offset);
675 nargs = lua_gettop(L);
676 if ((cmd != CMD_REVERT) && (nargs - offset > 1))
679 if (lookup_args(L, ctx, offset, &ptr, &s))
682 uci_lookup_ptr(ctx, &ptr, NULL, true);
684 uci_foreach_element_safe(&ctx->root, tmp, e) {
685 struct uci_package *p = uci_to_package(e);
687 if (ptr.p && (ptr.p != p))
693 uci_commit(ctx, &p, false);
699 uci_revert(ctx, &ptr);
707 return uci_push_status(L, ctx, false);
711 uci_lua_save(lua_State *L)
713 return uci_lua_package_cmd(L, CMD_SAVE);
717 uci_lua_commit(lua_State *L)
719 return uci_lua_package_cmd(L, CMD_COMMIT);
723 uci_lua_revert(lua_State *L)
725 return uci_lua_package_cmd(L, CMD_REVERT);
729 uci_lua_add_change(lua_State *L, struct uci_element *e)
739 lua_getfield(L, -1, h->section);
740 if (lua_isnil(L, -1)) {
743 lua_pushvalue(L, -1); /* copy for setfield */
744 lua_setfield(L, -3, h->section);
748 value = h->value ? h->value : "";
751 lua_getfield(L, -1, name);
753 /* this delta is a list add operation */
754 if (h->cmd == UCI_CMD_LIST_ADD) {
755 /* there seems to be no table yet */
756 if (!lua_istable(L, -1)) {
759 /* if there is a value on the stack already, add */
760 if (!lua_isnil(L, -2)) {
761 lua_pushvalue(L, -2);
762 lua_rawseti(L, -2, 1);
763 lua_pushstring(L, value);
764 lua_rawseti(L, -2, 2);
766 /* this is the first table item */
768 lua_pushstring(L, value);
769 lua_rawseti(L, -2, 1);
772 lua_setfield(L, -3, name);
774 /* a table is on the top of the stack and this is a subsequent,
775 * list_add, append this value to table */
777 lua_pushstring(L, value);
778 lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
781 /* non-list change, simply set/replace field */
783 lua_pushstring(L, value);
784 lua_setfield(L, -3, name);
789 lua_pushstring(L, value);
790 lua_setfield(L, -2, ".type");
797 uci_lua_changes_pkg(lua_State *L, struct uci_context *ctx, const char *package)
799 struct uci_package *p = NULL;
800 struct uci_element *e;
801 bool autoload = false;
803 p = find_package(L, ctx, package, false);
806 p = find_package(L, ctx, package, true);
811 if (uci_list_empty(&p->delta) && uci_list_empty(&p->saved_delta))
815 uci_foreach_element(&p->saved_delta, e) {
816 uci_lua_add_change(L, e);
818 uci_foreach_element(&p->delta, e) {
819 uci_lua_add_change(L, e);
821 lua_setfield(L, -2, p->e.name);
829 uci_lua_changes(lua_State *L)
831 struct uci_context *ctx;
832 const char *package = NULL;
833 char **config = NULL;
837 ctx = find_context(L, &offset);
838 nargs = lua_gettop(L);
839 switch(nargs - offset) {
841 package = luaL_checkstring(L, 1 + offset);
845 return luaL_error(L, "invalid argument count");
850 uci_lua_changes_pkg(L, ctx, package);
852 if (uci_list_configs(ctx, &config) != 0)
855 for(i = 0; config[i] != NULL; i++) {
856 uci_lua_changes_pkg(L, ctx, config[i]);
865 uci_lua_get_confdir(lua_State *L)
867 struct uci_context *ctx = find_context(L, NULL);
868 lua_pushstring(L, ctx->confdir);
873 uci_lua_set_confdir(lua_State *L)
875 struct uci_context *ctx;
878 ctx = find_context(L, &offset);
879 luaL_checkstring(L, 1 + offset);
880 uci_set_confdir(ctx, lua_tostring(L, -1));
881 return uci_push_status(L, ctx, false);
885 uci_lua_get_savedir(lua_State *L)
887 struct uci_context *ctx = find_context(L, NULL);
888 lua_pushstring(L, ctx->savedir);
893 uci_lua_add_delta(lua_State *L)
895 struct uci_context *ctx;
898 ctx = find_context(L, &offset);
899 luaL_checkstring(L, 1 + offset);
900 uci_add_delta_path(ctx, lua_tostring(L, -1));
901 return uci_push_status(L, ctx, false);
905 uci_lua_set_savedir(lua_State *L)
907 struct uci_context *ctx;
910 ctx = find_context(L, &offset);
911 luaL_checkstring(L, 1 + offset);
912 uci_set_savedir(ctx, lua_tostring(L, -1));
913 return uci_push_status(L, ctx, false);
917 uci_lua_gc(lua_State *L)
919 struct uci_context *ctx = find_context(L, NULL);
920 uci_free_context(ctx);
925 uci_lua_cursor(lua_State *L)
927 struct uci_context **u;
928 int argc = lua_gettop(L);
930 u = lua_newuserdata(L, sizeof(struct uci_context *));
931 luaL_getmetatable(L, METANAME);
932 lua_setmetatable(L, -2);
934 *u = uci_alloc_context();
936 return luaL_error(L, "Cannot allocate UCI context");
939 if (lua_isstring(L, 2) &&
940 (uci_set_savedir(*u, luaL_checkstring(L, 2)) != UCI_OK))
941 return luaL_error(L, "Unable to set savedir");
944 if (lua_isstring(L, 1) &&
945 (uci_set_confdir(*u, luaL_checkstring(L, 1)) != UCI_OK))
946 return luaL_error(L, "Unable to set savedir");
954 static const luaL_Reg uci[] = {
955 { "__gc", uci_lua_gc },
956 { "cursor", uci_lua_cursor },
957 { "load", uci_lua_load },
958 { "unload", uci_lua_unload },
959 { "get", uci_lua_get },
960 { "get_all", uci_lua_get_all },
961 { "add", uci_lua_add },
962 { "set", uci_lua_set },
963 { "rename", uci_lua_rename },
964 { "save", uci_lua_save },
965 { "delete", uci_lua_delete },
966 { "commit", uci_lua_commit },
967 { "revert", uci_lua_revert },
968 { "reorder", uci_lua_reorder },
969 { "changes", uci_lua_changes },
970 { "foreach", uci_lua_foreach },
971 { "add_history", uci_lua_add_delta },
972 { "add_delta", uci_lua_add_delta },
973 { "get_confdir", uci_lua_get_confdir },
974 { "set_confdir", uci_lua_set_confdir },
975 { "get_savedir", uci_lua_get_savedir },
976 { "set_savedir", uci_lua_set_savedir },
982 luaopen_uci(lua_State *L)
984 /* create metatable */
985 luaL_newmetatable(L, METANAME);
987 /* metatable.__index = metatable */
988 lua_pushvalue(L, -1);
989 lua_setfield(L, -2, "__index");
992 luaL_setfuncs(L, uci, 0);
997 lua_pushvalue(L, -1);
998 luaL_setfuncs(L, uci, 0);
999 lua_setglobal(L, MODNAME);