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);
126 lookup_extended(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
129 struct uci_ptr lookup;
131 /* use a copy of the passed ptr since failing lookups will
132 * clobber the state */
134 lookup.flags |= UCI_LOOKUP_EXTENDED;
136 rv = uci_lookup_ptr(ctx, &lookup, str, extended);
138 /* copy to passed ptr on success */
146 lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
148 if (ptr && !ptr->s && ptr->section && *ptr->section == '@')
149 return lookup_extended(ctx, ptr, str, extended);
151 return uci_lookup_ptr(ctx, ptr, str, extended);
155 lookup_args(lua_State *L, struct uci_context *ctx, int offset, struct uci_ptr *ptr, char **buf)
161 luaL_checkstring(L, 1 + offset);
162 s = strdup(lua_tostring(L, 1 + offset));
166 memset(ptr, 0, sizeof(struct uci_ptr));
167 if (!find_package(L, ctx, s, true))
170 switch (n - offset) {
173 ptr->option = luaL_checkstring(L, 3 + offset);
176 ptr->section = luaL_checkstring(L, 2 + offset);
177 ptr->package = luaL_checkstring(L, 1 + offset);
178 if (lookup_ptr(ctx, ptr, NULL, true) != UCI_OK)
182 if (lookup_ptr(ctx, ptr, s, true) != UCI_OK)
186 luaL_error(L, "invalid argument count");
200 uci_push_status(lua_State *L, struct uci_context *ctx, bool hasarg)
205 lua_pushboolean(L, (ctx->err == UCI_OK));
207 uci_get_errorstr(ctx, &str, MODNAME);
209 lua_pushstring(L, str);
218 uci_push_option(lua_State *L, struct uci_option *o)
220 struct uci_element *e;
224 case UCI_TYPE_STRING:
225 lua_pushstring(L, o->v.string);
229 uci_foreach_element(&o->v.list, e) {
231 lua_pushstring(L, e->name);
232 lua_rawseti(L, -2, i);
242 uci_push_section(lua_State *L, struct uci_section *s, int index)
244 struct uci_element *e;
247 lua_pushboolean(L, s->anonymous);
248 lua_setfield(L, -2, ".anonymous");
249 lua_pushstring(L, s->type);
250 lua_setfield(L, -2, ".type");
251 lua_pushstring(L, s->e.name);
252 lua_setfield(L, -2, ".name");
254 lua_pushinteger(L, index);
255 lua_setfield(L, -2, ".index");
258 uci_foreach_element(&s->options, e) {
259 struct uci_option *o = uci_to_option(e);
260 uci_push_option(L, o);
261 lua_setfield(L, -2, o->e.name);
266 uci_push_package(lua_State *L, struct uci_package *p)
268 struct uci_element *e;
272 uci_foreach_element(&p->sections, e) {
273 uci_push_section(L, uci_to_section(e), i);
274 lua_setfield(L, -2, e->name);
280 uci_lua_unload(lua_State *L)
282 struct uci_context *ctx;
283 struct uci_package *p;
287 ctx = find_context(L, &offset);
288 luaL_checkstring(L, 1 + offset);
289 s = lua_tostring(L, 1 + offset);
290 p = find_package(L, ctx, s, false);
293 return uci_push_status(L, ctx, false);
295 lua_pushboolean(L, 0);
301 uci_lua_load(lua_State *L)
303 struct uci_context *ctx;
304 struct uci_package *p = NULL;
308 ctx = find_context(L, &offset);
310 lua_pop(L, 1); /* bool ret value of unload */
311 s = lua_tostring(L, -1);
313 uci_load(ctx, s, &p);
314 return uci_push_status(L, ctx, false);
319 uci_lua_foreach(lua_State *L)
321 struct uci_context *ctx;
322 struct uci_package *p;
323 struct uci_element *e, *tmp;
324 const char *package, *type;
329 ctx = find_context(L, &offset);
330 package = luaL_checkstring(L, 1 + offset);
332 if (lua_isnil(L, 2 + offset))
335 type = luaL_checkstring(L, 2 + offset);
337 if (!lua_isfunction(L, 3 + offset) || !package)
338 return luaL_error(L, "Invalid argument");
340 p = find_package(L, ctx, package, true);
344 uci_foreach_element_safe(&p->sections, tmp, e) {
345 struct uci_section *s = uci_to_section(e);
349 if (type && (strcmp(s->type, type) != 0))
352 lua_pushvalue(L, 3 + offset); /* iterator function */
353 uci_push_section(L, s, i - 1);
354 if (lua_pcall(L, 1, 1, 0) == 0) {
356 if (lua_isboolean(L, -1) && !lua_toboolean(L, -1))
367 lua_pushboolean(L, ret);
372 uci_lua_get_any(lua_State *L, bool all)
374 struct uci_context *ctx;
375 struct uci_element *e = NULL;
379 int err = UCI_ERR_NOTFOUND;
381 ctx = find_context(L, &offset);
383 if (lookup_args(L, ctx, offset, &ptr, &s))
386 lookup_ptr(ctx, &ptr, NULL, true);
387 if (!all && !ptr.s) {
391 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
392 err = UCI_ERR_NOTFOUND;
399 case UCI_TYPE_PACKAGE:
400 uci_push_package(L, ptr.p);
402 case UCI_TYPE_SECTION:
404 uci_push_section(L, ptr.s, -1);
406 lua_pushstring(L, ptr.s->type);
408 case UCI_TYPE_OPTION:
409 uci_push_option(L, ptr.o);
425 return uci_push_status(L, ctx, true);
429 uci_lua_get(lua_State *L)
431 return uci_lua_get_any(L, false);
435 uci_lua_get_all(lua_State *L)
437 return uci_lua_get_any(L, true);
441 uci_lua_add(lua_State *L)
443 struct uci_context *ctx;
444 struct uci_section *s = NULL;
445 struct uci_package *p;
448 const char *name = NULL;
451 ctx = find_context(L, &offset);
452 package = luaL_checkstring(L, 1 + offset);
453 type = luaL_checkstring(L, 2 + offset);
454 p = find_package(L, ctx, package, true);
458 if (uci_add_section(ctx, p, type, &s) || !s)
462 lua_pushstring(L, name);
467 return uci_push_status(L, ctx, true);
471 uci_lua_delete(lua_State *L)
473 struct uci_context *ctx;
478 ctx = find_context(L, &offset);
480 if (lookup_args(L, ctx, offset, &ptr, &s))
483 uci_delete(ctx, &ptr);
488 return uci_push_status(L, ctx, false);
492 uci_lua_rename(lua_State *L)
494 struct uci_context *ctx;
496 int err = UCI_ERR_MEM;
498 int nargs, offset = 0;
500 ctx = find_context(L, &offset);
501 nargs = lua_gettop(L);
502 if (lookup_args(L, ctx, offset, &ptr, &s))
505 switch(nargs - offset) {
507 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
510 /* Format: uci.set("p", "s", "o", "v") */
511 ptr.value = luaL_checkstring(L, nargs);
514 /* Format: uci.set("p", "s", "v") */
515 ptr.value = ptr.option;
523 err = lookup_ptr(ctx, &ptr, NULL, true);
527 if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
532 err = uci_rename(ctx, &ptr);
539 return uci_push_status(L, ctx, false);
543 uci_lua_reorder(lua_State *L)
545 struct uci_context *ctx;
547 int err = UCI_ERR_MEM;
549 int nargs, offset = 0;
551 ctx = find_context(L, &offset);
552 nargs = lua_gettop(L);
553 if (lookup_args(L, ctx, offset, &ptr, &s))
556 switch(nargs - offset) {
558 /* Format: uci.set("p.s=v") or uci.set("p.s=v") */
565 /* Format: uci.set("p", "s", "v") */
566 ptr.value = ptr.option;
574 err = lookup_ptr(ctx, &ptr, NULL, true);
578 if ((ptr.s == NULL) || (ptr.value == NULL)) {
583 err = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
590 return uci_push_status(L, ctx, false);
595 uci_lua_set(lua_State *L)
597 struct uci_context *ctx;
599 bool istable = false;
600 int err = UCI_ERR_MEM;
603 int i, nargs, offset = 0;
605 ctx = find_context(L, &offset);
606 nargs = lua_gettop(L);
607 if (lookup_args(L, ctx, offset, &ptr, &s))
610 switch(nargs - offset) {
612 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
615 /* Format: uci.set("p", "s", "o", "v") */
616 if (lua_istable(L, nargs)) {
617 if (lua_rawlen(L, nargs) < 1)
618 return luaL_error(L, "Cannot set an uci option to an empty table value");
619 lua_rawgeti(L, nargs, 1);
620 ptr.value = luaL_checkstring(L, -1);
624 ptr.value = luaL_checkstring(L, nargs);
628 /* Format: uci.set("p", "s", "v") */
629 ptr.value = ptr.option;
637 err = lookup_ptr(ctx, &ptr, NULL, true);
641 if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
647 if (lua_rawlen(L, nargs) == 1) {
652 err = uci_delete(ctx, &ptr);
659 err = uci_set(ctx, &ptr);
664 for (; i <= lua_rawlen(L, nargs); i++) {
665 lua_rawgeti(L, nargs, i);
666 ptr.value = luaL_checkstring(L, -1);
667 err = uci_add_list(ctx, &ptr);
673 err = uci_set(ctx, &ptr);
682 return uci_push_status(L, ctx, false);
692 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
694 struct uci_context *ctx;
695 struct uci_element *e, *tmp;
698 int nargs, offset = 0;
700 ctx = find_context(L, &offset);
701 nargs = lua_gettop(L);
702 if ((cmd != CMD_REVERT) && (nargs - offset > 1))
705 if (lookup_args(L, ctx, offset, &ptr, &s))
708 lookup_ptr(ctx, &ptr, NULL, true);
710 uci_foreach_element_safe(&ctx->root, tmp, e) {
711 struct uci_package *p = uci_to_package(e);
713 if (ptr.p && (ptr.p != p))
719 uci_commit(ctx, &p, false);
725 uci_revert(ctx, &ptr);
733 return uci_push_status(L, ctx, false);
737 uci_lua_save(lua_State *L)
739 return uci_lua_package_cmd(L, CMD_SAVE);
743 uci_lua_commit(lua_State *L)
745 return uci_lua_package_cmd(L, CMD_COMMIT);
749 uci_lua_revert(lua_State *L)
751 return uci_lua_package_cmd(L, CMD_REVERT);
755 uci_lua_add_change(lua_State *L, struct uci_element *e)
765 lua_getfield(L, -1, h->section);
766 if (lua_isnil(L, -1)) {
769 lua_pushvalue(L, -1); /* copy for setfield */
770 lua_setfield(L, -3, h->section);
774 value = h->value ? h->value : "";
777 lua_getfield(L, -1, name);
779 /* this delta is a list add operation */
780 if (h->cmd == UCI_CMD_LIST_ADD) {
781 /* there seems to be no table yet */
782 if (!lua_istable(L, -1)) {
785 /* if there is a value on the stack already, add */
786 if (!lua_isnil(L, -2)) {
787 lua_pushvalue(L, -2);
788 lua_rawseti(L, -2, 1);
789 lua_pushstring(L, value);
790 lua_rawseti(L, -2, 2);
792 /* this is the first table item */
794 lua_pushstring(L, value);
795 lua_rawseti(L, -2, 1);
798 lua_setfield(L, -3, name);
800 /* a table is on the top of the stack and this is a subsequent,
801 * list_add, append this value to table */
803 lua_pushstring(L, value);
804 lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
807 /* non-list change, simply set/replace field */
809 lua_pushstring(L, value);
810 lua_setfield(L, -3, name);
815 lua_pushstring(L, value);
816 lua_setfield(L, -2, ".type");
823 uci_lua_changes_pkg(lua_State *L, struct uci_context *ctx, const char *package)
825 struct uci_package *p = NULL;
826 struct uci_element *e;
827 bool autoload = false;
829 p = find_package(L, ctx, package, false);
832 p = find_package(L, ctx, package, true);
837 if (uci_list_empty(&p->delta) && uci_list_empty(&p->saved_delta))
841 uci_foreach_element(&p->saved_delta, e) {
842 uci_lua_add_change(L, e);
844 uci_foreach_element(&p->delta, e) {
845 uci_lua_add_change(L, e);
847 lua_setfield(L, -2, p->e.name);
855 uci_lua_changes(lua_State *L)
857 struct uci_context *ctx;
858 const char *package = NULL;
859 char **config = NULL;
863 ctx = find_context(L, &offset);
864 nargs = lua_gettop(L);
865 switch(nargs - offset) {
867 package = luaL_checkstring(L, 1 + offset);
871 return luaL_error(L, "invalid argument count");
876 uci_lua_changes_pkg(L, ctx, package);
878 if (uci_list_configs(ctx, &config) != 0)
881 for(i = 0; config[i] != NULL; i++) {
882 uci_lua_changes_pkg(L, ctx, config[i]);
891 uci_lua_get_confdir(lua_State *L)
893 struct uci_context *ctx = find_context(L, NULL);
894 lua_pushstring(L, ctx->confdir);
899 uci_lua_set_confdir(lua_State *L)
901 struct uci_context *ctx;
904 ctx = find_context(L, &offset);
905 luaL_checkstring(L, 1 + offset);
906 uci_set_confdir(ctx, lua_tostring(L, -1));
907 return uci_push_status(L, ctx, false);
911 uci_lua_get_savedir(lua_State *L)
913 struct uci_context *ctx = find_context(L, NULL);
914 lua_pushstring(L, ctx->savedir);
919 uci_lua_add_delta(lua_State *L)
921 struct uci_context *ctx;
924 ctx = find_context(L, &offset);
925 luaL_checkstring(L, 1 + offset);
926 uci_add_delta_path(ctx, lua_tostring(L, -1));
927 return uci_push_status(L, ctx, false);
931 uci_lua_set_savedir(lua_State *L)
933 struct uci_context *ctx;
936 ctx = find_context(L, &offset);
937 luaL_checkstring(L, 1 + offset);
938 uci_set_savedir(ctx, lua_tostring(L, -1));
939 return uci_push_status(L, ctx, false);
943 uci_lua_list_configs(lua_State *L)
945 struct uci_context *ctx;
946 char **configs = NULL;
950 ctx = find_context(L, NULL);
951 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs)
952 return uci_push_status(L, ctx, false);
954 for (ptr = configs; *ptr; ptr++) {
955 lua_pushstring(L, *ptr);
956 lua_rawseti(L, -2, i++);
963 uci_lua_gc(lua_State *L)
965 struct uci_context **ctx;
967 if (!lua_isuserdata(L, 1)) {
972 ctx = luaL_checkudata(L, 1, METANAME);
976 uci_free_context(*ctx);
982 uci_lua_cursor(lua_State *L)
984 struct uci_context **u;
985 int argc = lua_gettop(L);
987 u = lua_newuserdata(L, sizeof(struct uci_context *));
988 luaL_getmetatable(L, METANAME);
989 lua_setmetatable(L, -2);
991 *u = uci_alloc_context();
993 return luaL_error(L, "Cannot allocate UCI context");
996 if (lua_isstring(L, 2) &&
997 (uci_set_savedir(*u, luaL_checkstring(L, 2)) != UCI_OK))
998 return luaL_error(L, "Unable to set savedir");
1001 if (lua_isstring(L, 1) &&
1002 (uci_set_confdir(*u, luaL_checkstring(L, 1)) != UCI_OK))
1003 return luaL_error(L, "Unable to set savedir");
1011 static const luaL_Reg uci[] = {
1012 { "__gc", uci_lua_gc },
1013 { "close", uci_lua_gc },
1014 { "cursor", uci_lua_cursor },
1015 { "load", uci_lua_load },
1016 { "unload", uci_lua_unload },
1017 { "get", uci_lua_get },
1018 { "get_all", uci_lua_get_all },
1019 { "add", uci_lua_add },
1020 { "set", uci_lua_set },
1021 { "rename", uci_lua_rename },
1022 { "save", uci_lua_save },
1023 { "delete", uci_lua_delete },
1024 { "commit", uci_lua_commit },
1025 { "revert", uci_lua_revert },
1026 { "reorder", uci_lua_reorder },
1027 { "changes", uci_lua_changes },
1028 { "foreach", uci_lua_foreach },
1029 { "add_history", uci_lua_add_delta },
1030 { "add_delta", uci_lua_add_delta },
1031 { "get_confdir", uci_lua_get_confdir },
1032 { "set_confdir", uci_lua_set_confdir },
1033 { "get_savedir", uci_lua_get_savedir },
1034 { "set_savedir", uci_lua_set_savedir },
1035 { "list_configs", uci_lua_list_configs },
1041 luaopen_uci(lua_State *L)
1043 /* create metatable */
1044 luaL_newmetatable(L, METANAME);
1046 /* metatable.__index = metatable */
1047 lua_pushvalue(L, -1);
1048 lua_setfield(L, -2, "__index");
1050 /* fill metatable */
1051 luaL_setfuncs(L, uci, 0);
1056 lua_pushvalue(L, -1);
1057 luaL_setfuncs(L, uci, 0);
1058 lua_setglobal(L, MODNAME);