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 lookup_args(lua_State *L, struct uci_context *ctx, int offset, struct uci_ptr *ptr, char **buf)
107 luaL_checkstring(L, 1 + offset);
108 s = strdup(lua_tostring(L, 1 + offset));
112 memset(ptr, 0, sizeof(struct uci_ptr));
113 if (!find_package(L, ctx, s, true))
116 switch (n - offset) {
119 ptr->option = luaL_checkstring(L, 3 + offset);
122 ptr->section = luaL_checkstring(L, 2 + offset);
123 ptr->package = luaL_checkstring(L, 1 + offset);
124 if (uci_lookup_ptr(ctx, ptr, NULL, false) != UCI_OK)
128 if (uci_lookup_ptr(ctx, ptr, s, false) != UCI_OK)
132 luaL_error(L, "invalid argument count");
146 uci_push_status(lua_State *L, struct uci_context *ctx, bool hasarg)
151 lua_pushboolean(L, (ctx->err == UCI_OK));
153 uci_get_errorstr(ctx, &str, MODNAME);
155 lua_pushstring(L, str);
164 uci_push_option(lua_State *L, struct uci_option *o)
166 struct uci_element *e;
170 case UCI_TYPE_STRING:
171 lua_pushstring(L, o->v.string);
175 uci_foreach_element(&o->v.list, e) {
177 lua_pushstring(L, e->name);
178 lua_rawseti(L, -2, i);
188 uci_push_section(lua_State *L, struct uci_section *s)
190 struct uci_element *e;
193 lua_pushboolean(L, s->anonymous);
194 lua_setfield(L, -2, ".anonymous");
195 lua_pushstring(L, s->type);
196 lua_setfield(L, -2, ".type");
197 lua_pushstring(L, s->e.name);
198 lua_setfield(L, -2, ".name");
200 uci_foreach_element(&s->options, e) {
201 struct uci_option *o = uci_to_option(e);
202 uci_push_option(L, o);
203 lua_setfield(L, -2, o->e.name);
208 uci_push_package(lua_State *L, struct uci_package *p)
210 struct uci_element *e;
214 uci_foreach_element(&p->sections, e) {
216 uci_push_section(L, uci_to_section(e));
217 lua_setfield(L, -2, e->name);
222 uci_lua_unload(lua_State *L)
224 struct uci_context *ctx;
225 struct uci_package *p;
229 ctx = find_context(L, &offset);
230 luaL_checkstring(L, 1 + offset);
231 s = lua_tostring(L, 1 + offset);
232 p = find_package(L, ctx, s, false);
235 return uci_push_status(L, ctx, false);
237 lua_pushboolean(L, 0);
243 uci_lua_load(lua_State *L)
245 struct uci_context *ctx;
246 struct uci_package *p = NULL;
250 ctx = find_context(L, &offset);
252 lua_pop(L, 1); /* bool ret value of unload */
253 s = lua_tostring(L, -1);
255 uci_load(ctx, s, &p);
256 return uci_push_status(L, ctx, false);
261 uci_lua_foreach(lua_State *L)
263 struct uci_context *ctx;
264 struct uci_package *p;
265 struct uci_element *e;
266 const char *package, *type;
270 ctx = find_context(L, &offset);
271 package = luaL_checkstring(L, 1 + offset);
276 type = luaL_checkstring(L, 2 + offset);
278 if (!lua_isfunction(L, 3 + offset) || !package)
279 luaL_error(L, "Invalid argument");
281 p = find_package(L, ctx, package, true);
285 uci_foreach_element(&p->sections, e) {
286 struct uci_section *s = uci_to_section(e);
288 if (type && (strcmp(s->type, type) != 0))
291 lua_pushvalue(L, 3 + offset); /* iterator function */
292 uci_push_section(L, s);
293 if (lua_pcall(L, 1, 0, 0) == 0)
298 lua_pushboolean(L, ret);
303 uci_lua_get_any(lua_State *L, bool all)
305 struct uci_context *ctx;
306 struct uci_element *e = NULL;
310 int err = UCI_ERR_NOTFOUND;
312 ctx = find_context(L, &offset);
314 if (lookup_args(L, ctx, offset, &ptr, &s))
317 uci_lookup_ptr(ctx, &ptr, NULL, false);
318 if (!all && !ptr.s) {
322 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
323 err = UCI_ERR_NOTFOUND;
330 case UCI_TYPE_PACKAGE:
331 uci_push_package(L, ptr.p);
333 case UCI_TYPE_SECTION:
335 uci_push_section(L, ptr.s);
337 lua_pushstring(L, ptr.s->type);
339 case UCI_TYPE_OPTION:
340 uci_push_option(L, ptr.o);
351 return uci_push_status(L, ctx, true);
355 uci_lua_get(lua_State *L)
357 return uci_lua_get_any(L, false);
361 uci_lua_get_all(lua_State *L)
363 return uci_lua_get_any(L, true);
367 uci_lua_add(lua_State *L)
369 struct uci_context *ctx;
370 struct uci_section *s = NULL;
371 struct uci_package *p;
374 const char *name = NULL;
377 ctx = find_context(L, &offset);
378 package = luaL_checkstring(L, 1 + offset);
379 type = luaL_checkstring(L, 2 + offset);
380 p = find_package(L, ctx, package, true);
384 if (uci_add_section(ctx, p, type, &s) || !s)
388 lua_pushstring(L, name);
393 return uci_push_status(L, ctx, true);
397 uci_lua_delete(lua_State *L)
399 struct uci_context *ctx;
403 int err = UCI_ERR_NOTFOUND;
405 ctx = find_context(L, &offset);
407 if (lookup_args(L, ctx, offset, &ptr, &s))
410 err = uci_delete(ctx, &ptr);
415 return uci_push_status(L, ctx, false);
419 uci_lua_set(lua_State *L)
421 struct uci_context *ctx;
423 bool istable = false;
424 int err = UCI_ERR_MEM;
426 int i, nargs, offset = 0;
428 ctx = find_context(L, &offset);
429 nargs = lua_gettop(L);
430 if (lookup_args(L, ctx, offset, &ptr, &s))
433 switch(nargs - offset) {
435 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
438 /* Format: uci.set("p", "s", "o", "v") */
439 if (lua_istable(L, nargs)) {
440 if (lua_objlen(L, nargs) < 1)
441 luaL_error(L, "Cannot set an uci option to an empty table value");
442 lua_rawgeti(L, nargs, 1);
443 ptr.value = luaL_checkstring(L, -1);
447 ptr.value = luaL_checkstring(L, nargs);
451 /* Format: uci.set("p", "s", "v") */
452 ptr.value = ptr.option;
460 err = uci_lookup_ptr(ctx, &ptr, NULL, false);
464 if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
469 err = uci_set(ctx, &ptr);
474 for (i = 2; i <= lua_objlen(L, nargs); i++) {
475 lua_rawgeti(L, nargs, i);
476 ptr.value = luaL_checkstring(L, -1);
477 err = uci_add_list(ctx, &ptr);
485 return uci_push_status(L, ctx, false);
495 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
497 struct uci_context *ctx;
498 struct uci_element *e, *tmp;
502 int nargs, offset = 0;
504 ctx = find_context(L, &offset);
505 nargs = lua_gettop(L);
506 if ((cmd != CMD_REVERT) && (nargs > 1))
509 if (lookup_args(L, ctx, offset, &ptr, &s))
512 uci_lookup_ptr(ctx, &ptr, NULL, false);
514 uci_foreach_element_safe(&ctx->root, tmp, e) {
515 struct uci_package *p = uci_to_package(e);
516 int ret = UCI_ERR_INVAL;
518 if (ptr.p && (ptr.p != p))
524 ret = uci_commit(ctx, &p, false);
527 ret = uci_save(ctx, p);
530 ret = uci_revert(ctx, &ptr);
539 return uci_push_status(L, ctx, false);
543 uci_lua_save(lua_State *L)
545 return uci_lua_package_cmd(L, CMD_SAVE);
549 uci_lua_commit(lua_State *L)
551 return uci_lua_package_cmd(L, CMD_COMMIT);
555 uci_lua_revert(lua_State *L)
557 return uci_lua_package_cmd(L, CMD_REVERT);
561 uci_lua_add_change(lua_State *L, struct uci_element *e)
563 struct uci_history *h;
566 h = uci_to_history(e);
570 lua_getfield(L, -1, h->section);
571 if (lua_isnil(L, -1)) {
574 lua_pushvalue(L, -1); /* copy for setfield */
575 lua_setfield(L, -3, h->section);
578 name = (h->e.name ? h->e.name : ".type");
580 lua_pushstring(L, h->value);
582 lua_pushstring(L, "");
583 lua_setfield(L, -2, name);
588 uci_lua_changes_pkg(lua_State *L, struct uci_context *ctx, const char *package)
590 struct uci_package *p = NULL;
591 struct uci_element *e;
592 bool autoload = false;
594 p = find_package(L, ctx, package, false);
597 p = find_package(L, ctx, package, true);
602 if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history))
606 uci_foreach_element(&p->saved_history, e) {
607 uci_lua_add_change(L, e);
609 uci_foreach_element(&p->history, e) {
610 uci_lua_add_change(L, e);
612 lua_setfield(L, -2, p->e.name);
620 uci_lua_changes(lua_State *L)
622 struct uci_context *ctx;
623 const char *package = NULL;
624 char **config = NULL;
628 ctx = find_context(L, &offset);
629 nargs = lua_gettop(L);
630 switch(nargs - offset) {
632 package = luaL_checkstring(L, 1 + offset);
636 luaL_error(L, "invalid argument count");
641 uci_lua_changes_pkg(L, ctx, package);
643 if (uci_list_configs(ctx, &config) != 0)
646 for(i = 0; config[i] != NULL; i++) {
647 uci_lua_changes_pkg(L, ctx, config[i]);
656 uci_lua_get_confdir(lua_State *L)
658 struct uci_context *ctx = find_context(L, NULL);
659 lua_pushstring(L, ctx->confdir);
664 uci_lua_set_confdir(lua_State *L)
666 struct uci_context *ctx;
669 ctx = find_context(L, &offset);
670 luaL_checkstring(L, 1 + offset);
671 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
672 return uci_push_status(L, ctx, false);
676 uci_lua_get_savedir(lua_State *L)
678 struct uci_context *ctx = find_context(L, NULL);
679 lua_pushstring(L, ctx->savedir);
684 uci_lua_set_savedir(lua_State *L)
686 struct uci_context *ctx;
689 ctx = find_context(L, &offset);
690 luaL_checkstring(L, 1 + offset);
691 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
692 return uci_push_status(L, ctx, false);
696 uci_lua_gc(lua_State *L)
698 struct uci_context *ctx = find_context(L, NULL);
699 uci_free_context(ctx);
704 uci_lua_cursor(lua_State *L)
706 struct uci_context **u;
707 int argc = lua_gettop(L);
709 u = lua_newuserdata(L, sizeof(struct uci_context *));
710 luaL_getmetatable(L, METANAME);
711 lua_setmetatable(L, -2);
713 *u = uci_alloc_context();
715 luaL_error(L, "Cannot allocate UCI context");
718 if (lua_isstring(L, 2) &&
719 (uci_set_savedir(*u, luaL_checkstring(L, 2)) != UCI_OK))
720 luaL_error(L, "Unable to set savedir");
723 if (lua_isstring(L, 1) &&
724 (uci_set_confdir(*u, luaL_checkstring(L, 1)) != UCI_OK))
725 luaL_error(L, "Unable to set savedir");
733 static const luaL_Reg uci[] = {
734 { "__gc", uci_lua_gc },
735 { "cursor", uci_lua_cursor },
736 { "load", uci_lua_load },
737 { "unload", uci_lua_unload },
738 { "get", uci_lua_get },
739 { "get_all", uci_lua_get_all },
740 { "add", uci_lua_add },
741 { "set", uci_lua_set },
742 { "save", uci_lua_save },
743 { "delete", uci_lua_delete },
744 { "commit", uci_lua_commit },
745 { "revert", uci_lua_revert },
746 { "changes", uci_lua_changes },
747 { "foreach", uci_lua_foreach },
748 { "get_confdir", uci_lua_get_confdir },
749 { "set_confdir", uci_lua_set_confdir },
750 { "get_savedir", uci_lua_get_savedir },
751 { "set_savedir", uci_lua_set_savedir },
757 luaopen_uci(lua_State *L)
759 /* create metatable */
760 luaL_newmetatable(L, METANAME);
762 /* metatable.__index = metatable */
763 lua_pushvalue(L, -1);
764 lua_setfield(L, -2, "__index");
767 luaL_register(L, NULL, uci);
771 luaL_register(L, MODNAME, uci);