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 *name, enum autoload al)
46 struct uci_package *p = NULL;
47 struct uci_element *e;
49 uci_foreach_element(&ctx->root, e) {
50 if (strcmp(e->name, name) != 0)
53 p = uci_to_package(e);
57 if (al == AUTOLOAD_FORCE)
58 uci_load(ctx, name, &p);
61 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
62 lua_getfield(L, -1, "autoload");
63 if (!lua_isboolean(L, -1))
66 if (!lua_toboolean(L, -1))
69 uci_load(ctx, name, &p);
78 static void uci_lua_perror(lua_State *L, char *name)
80 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
81 lua_getfield(L, -1, "warn");
82 if (!lua_isboolean(L, -1))
84 if (lua_toboolean(L, -1) != 1)
86 uci_perror(ctx, name);
91 static void uci_push_section(lua_State *L, struct uci_section *s)
93 struct uci_element *e;
96 lua_pushstring(L, s->type);
97 lua_setfield(L, -2, ".type");
98 lua_pushstring(L, s->e.name);
99 lua_setfield(L, -2, ".name");
101 uci_foreach_element(&s->options, e) {
102 struct uci_option *o = uci_to_option(e);
104 case UCI_TYPE_STRING:
105 lua_pushstring(L, o->v.string);
106 lua_setfield(L, -2, o->e.name);
109 /* nothing to do yet */
115 static void uci_push_package(lua_State *L, struct uci_package *p)
117 struct uci_element *e;
121 uci_foreach_element(&p->sections, e) {
123 uci_push_section(L, uci_to_section(e));
124 lua_setfield(L, -2, e->name);
129 uci_lua_unload(lua_State *L)
131 struct uci_package *p;
134 luaL_checkstring(L, 1);
135 s = lua_tostring(L, -1);
136 p = find_package(L, s, AUTOLOAD_OFF);
139 lua_pushboolean(L, 1);
141 lua_pushboolean(L, 0);
147 uci_lua_load(lua_State *L)
149 struct uci_package *p = NULL;
153 lua_pop(L, 1); /* bool ret value of unload */
154 s = lua_tostring(L, -1);
156 if (uci_load(ctx, s, &p)) {
157 uci_lua_perror(L, "uci.load");
158 lua_pushboolean(L, 0);
160 lua_pushboolean(L, 1);
168 uci_lua_foreach(lua_State *L)
170 struct uci_package *p;
171 struct uci_element *e;
172 const char *package, *type;
175 package = luaL_checkstring(L, 1);
180 type = luaL_checkstring(L, 2);
182 if (!lua_isfunction(L, 3) || !package)
183 luaL_error(L, "Invalid argument");
185 p = find_package(L, package, AUTOLOAD_ON);
189 uci_foreach_element(&p->sections, e) {
190 struct uci_section *s = uci_to_section(e);
192 if (type && (strcmp(s->type, type) != 0))
195 lua_pushvalue(L, 3); /* iterator function */
196 uci_push_section(L, s);
197 if (lua_pcall(L, 1, 0, 0) == 0)
202 lua_pushboolean(L, ret);
207 uci_lua_get_any(lua_State *L, bool all)
209 struct uci_element *e = NULL;
210 struct uci_package *p = NULL;
211 struct uci_option *o = NULL;
212 const char *package = NULL;
213 const char *section = NULL;
214 const char *option = NULL;
216 int err = UCI_ERR_MEM;
221 luaL_checkstring(L, 1);
222 s = strdup(lua_tostring(L, 1));
227 package = luaL_checkstring(L, 1);
228 section = luaL_checkstring(L, 2);
230 option = luaL_checkstring(L, 3);
232 if ((err = uci_parse_tuple(ctx, s, (char **) &package, (char **) §ion, (char **) &option, NULL)))
236 if (!all && (section == NULL)) {
241 p = find_package(L, package, AUTOLOAD_ON);
243 err = UCI_ERR_NOTFOUND;
248 if ((err = uci_lookup(ctx, &e, p, section, option)))
255 case UCI_TYPE_PACKAGE:
256 uci_push_package(L, p);
258 case UCI_TYPE_SECTION:
260 uci_push_section(L, uci_to_section(e));
262 lua_pushstring(L, uci_to_section(e)->type);
264 case UCI_TYPE_OPTION:
265 o = uci_to_option(e);
267 case UCI_TYPE_STRING:
268 lua_pushstring(L, o->v.string);
271 /* nothing to do yet */
286 uci_lua_perror(L, "uci.get");
288 case UCI_ERR_NOTFOUND:
297 uci_lua_get(lua_State *L)
299 return uci_lua_get_any(L, false);
303 uci_lua_get_all(lua_State *L)
305 return uci_lua_get_any(L, true);
309 uci_lua_add(lua_State *L)
311 struct uci_section *s = NULL;
312 struct uci_package *p;
315 const char *name = NULL;
318 package = luaL_checkstring(L, 1);
319 type = luaL_checkstring(L, 2);
320 p = find_package(L, package, AUTOLOAD_ON);
324 if (uci_add_section(ctx, p, type, &s) || !s)
330 lua_pushstring(L, name);
335 uci_lua_delete(lua_State *L)
337 const char *package = NULL;
338 const char *section = NULL;
339 const char *option = NULL;
340 struct uci_package *p;
342 int err = UCI_ERR_MEM;
345 nargs = lua_gettop(L);
346 s = luaL_checkstring(L, 1);
349 /* Format: uci.delete("p.s[.o]") */
354 if ((err = uci_parse_tuple(ctx, (char *) s, (char **) &package, (char **) §ion, (char **) &option, NULL)))
358 /* Format: uci.delete("p", "s", "o") */
359 option = luaL_checkstring(L, 3);
362 /* Format: uci.delete("p", "s") */
363 section = luaL_checkstring(L, 2);
371 p = find_package(L, package, AUTOLOAD_ON);
373 err = UCI_ERR_NOTFOUND;
376 err = uci_delete(ctx, p, section, option);
380 uci_lua_perror(L, "uci.set");
381 lua_pushboolean(L, (err == 0));
386 uci_lua_set(lua_State *L)
388 struct uci_package *p;
389 const char *package = NULL;
390 const char *section = NULL;
391 const char *option = NULL;
392 const char *value = NULL;
394 int err = UCI_ERR_MEM;
397 nargs = lua_gettop(L);
399 s = luaL_checkstring(L, 1);
402 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
407 if ((err = uci_parse_tuple(ctx, (char *) s, (char **) &package, (char **) §ion, (char **) &option, (char **) &value)))
411 /* Format: uci.set("p", "s", "o", "v") */
412 option = luaL_checkstring(L, 3);
415 /* Format: uci.set("p", "s", "v") */
417 section = luaL_checkstring(L, 2);
418 value = luaL_checkstring(L, nargs);
425 if ((section == NULL) || (value == NULL)) {
430 p = find_package(L, package, AUTOLOAD_ON);
432 err = UCI_ERR_NOTFOUND;
435 err = uci_set(ctx, p, section, option, value, NULL);
439 uci_lua_perror(L, "uci.set");
440 lua_pushboolean(L, (err == 0));
451 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
453 struct uci_element *e, *tmp;
454 const char *s = NULL;
455 const char *section = NULL;
456 const char *option = NULL;
460 nargs = lua_gettop(L);
463 if (cmd != CMD_REVERT)
465 luaL_checkstring(L, 1);
466 option = lua_tostring(L, -1);
470 if (cmd != CMD_REVERT)
472 luaL_checkstring(L, 1);
473 section = lua_tostring(L, -1);
477 luaL_checkstring(L, 1);
478 s = lua_tostring(L, -1);
485 luaL_error(L, "Invalid argument count");
489 uci_foreach_element_safe(&ctx->root, tmp, e) {
490 struct uci_package *p = uci_to_package(e);
491 int ret = UCI_ERR_INVAL;
493 if (s && (strcmp(s, e->name) != 0))
498 ret = uci_commit(ctx, &p, false);
501 ret = uci_save(ctx, p);
504 ret = uci_revert(ctx, &p, section, option);
512 lua_pushboolean(L, !failed);
517 uci_lua_save(lua_State *L)
519 return uci_lua_package_cmd(L, CMD_SAVE);
523 uci_lua_commit(lua_State *L)
525 return uci_lua_package_cmd(L, CMD_COMMIT);
529 uci_lua_revert(lua_State *L)
531 return uci_lua_package_cmd(L, CMD_REVERT);
535 uci_lua_add_change(lua_State *L, struct uci_element *e)
537 struct uci_history *h;
540 h = uci_to_history(e);
544 lua_getfield(L, -1, h->section);
545 if (lua_isnil(L, -1)) {
548 lua_pushvalue(L, -1); /* copy for setfield */
549 lua_setfield(L, -3, h->section);
552 name = (h->e.name ? h->e.name : ".type");
554 lua_pushstring(L, h->value);
556 lua_pushstring(L, "");
557 lua_setfield(L, -2, name);
562 uci_lua_changes_pkg(lua_State *L, const char *package)
564 struct uci_package *p = NULL;
565 struct uci_element *e;
566 bool autoload = false;
568 p = find_package(L, package, AUTOLOAD_OFF);
571 p = find_package(L, package, AUTOLOAD_FORCE);
576 if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history))
580 uci_foreach_element(&p->saved_history, e) {
581 uci_lua_add_change(L, e);
583 uci_foreach_element(&p->history, e) {
584 uci_lua_add_change(L, e);
586 lua_setfield(L, -2, p->e.name);
594 uci_lua_changes(lua_State *L)
596 const char *package = NULL;
597 char **config = NULL;
601 nargs = lua_gettop(L);
604 package = luaL_checkstring(L, 1);
608 luaL_error(L, "invalid argument count");
613 uci_lua_changes_pkg(L, package);
615 if (uci_list_configs(ctx, &config) != 0)
618 for(i = 0; config[i] != NULL; i++) {
619 uci_lua_changes_pkg(L, config[i]);
628 uci_lua_set_confdir(lua_State *L)
632 luaL_checkstring(L, 1);
633 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
634 lua_pushboolean(L, (ret == 0));
639 uci_lua_set_savedir(lua_State *L)
643 luaL_checkstring(L, 1);
644 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
645 lua_pushboolean(L, (ret == 0));
650 static const luaL_Reg uci[] = {
651 { "load", uci_lua_load },
652 { "unload", uci_lua_unload },
653 { "get", uci_lua_get },
654 { "get_all", uci_lua_get_all },
655 { "add", uci_lua_add },
656 { "set", uci_lua_set },
657 { "save", uci_lua_save },
658 { "delete", uci_lua_delete },
659 { "commit", uci_lua_commit },
660 { "revert", uci_lua_revert },
661 { "changes", uci_lua_changes },
662 { "foreach", uci_lua_foreach },
663 { "set_confdir", uci_lua_set_confdir },
664 { "set_savedir", uci_lua_set_savedir },
670 luaopen_uci(lua_State *L)
672 ctx = uci_alloc_context();
674 luaL_error(L, "Cannot allocate UCI context\n");
675 luaL_register(L, MODNAME, uci);
677 /* enable autoload by default */
678 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
679 lua_pushboolean(L, 1);
680 lua_setfield(L, -2, "autoload");