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");
102 lua_pushvalue(L, -1);
103 lua_setfield(L, -3, "options");
105 uci_foreach_element(&s->options, e) {
106 struct uci_option *o = uci_to_option(e);
107 lua_pushstring(L, o->value);
108 lua_setfield(L, -2, o->e.name);
113 static void uci_push_package(lua_State *L, struct uci_package *p)
115 struct uci_element *e;
119 uci_foreach_element(&p->sections, e) {
122 uci_push_section(L, uci_to_section(e));
123 lua_rawseti(L, -2, i);
128 uci_lua_unload(lua_State *L)
130 struct uci_package *p;
133 luaL_checkstring(L, 1);
134 s = lua_tostring(L, -1);
135 p = find_package(L, s, AUTOLOAD_OFF);
138 lua_pushboolean(L, 1);
140 lua_pushboolean(L, 0);
146 uci_lua_load(lua_State *L)
148 struct uci_package *p = NULL;
152 lua_pop(L, 1); /* bool ret value of unload */
153 s = lua_tostring(L, -1);
155 if (uci_load(ctx, s, &p)) {
156 uci_lua_perror(L, "uci.load");
157 lua_pushboolean(L, 0);
159 lua_pushboolean(L, 1);
167 uci_lua_foreach(lua_State *L)
169 struct uci_package *p;
170 struct uci_element *e;
171 const char *package, *type;
174 package = luaL_checkstring(L, 1);
179 type = luaL_checkstring(L, 2);
181 if (!lua_isfunction(L, 3) || !package)
182 luaL_error(L, "Invalid argument");
184 p = find_package(L, package, AUTOLOAD_ON);
188 uci_foreach_element(&p->sections, e) {
189 struct uci_section *s = uci_to_section(e);
191 if (type && (strcmp(s->type, type) != 0))
194 lua_pushvalue(L, 3); /* iterator function */
195 uci_push_section(L, s);
196 if (lua_pcall(L, 1, 0, 0) == 0)
201 lua_pushboolean(L, ret);
206 uci_lua_get_any(lua_State *L, bool all)
208 struct uci_element *e = NULL;
209 struct uci_package *p = NULL;
210 const char *package = NULL;
211 const char *section = NULL;
212 const char *option = NULL;
214 int err = UCI_ERR_MEM;
219 luaL_checkstring(L, 1);
220 s = strdup(lua_tostring(L, 1));
225 package = luaL_checkstring(L, 1);
226 section = luaL_checkstring(L, 2);
228 option = luaL_checkstring(L, 3);
230 if ((err = uci_parse_tuple(ctx, s, (char **) &package, (char **) §ion, (char **) &option, NULL)))
234 if (!all && (section == NULL)) {
239 p = find_package(L, package, AUTOLOAD_ON);
241 err = UCI_ERR_NOTFOUND;
246 if ((err = uci_lookup(ctx, &e, p, section, option)))
253 case UCI_TYPE_PACKAGE:
254 uci_push_package(L, p);
256 case UCI_TYPE_SECTION:
258 uci_push_section(L, uci_to_section(e));
260 lua_pushstring(L, uci_to_section(e)->type);
262 case UCI_TYPE_OPTION:
263 lua_pushstring(L, uci_to_option(e)->value);
276 uci_lua_perror(L, "uci.get");
278 case UCI_ERR_NOTFOUND:
287 uci_lua_get(lua_State *L)
289 return uci_lua_get_any(L, false);
293 uci_lua_get_all(lua_State *L)
295 return uci_lua_get_any(L, true);
299 uci_lua_add(lua_State *L)
301 struct uci_section *s = NULL;
302 struct uci_package *p;
305 const char *name = NULL;
308 package = luaL_checkstring(L, 1);
309 type = luaL_checkstring(L, 2);
310 p = find_package(L, package, AUTOLOAD_ON);
314 if (uci_add_section(ctx, p, type, &s) || !s)
320 lua_pushstring(L, name);
325 uci_lua_delete(lua_State *L)
327 const char *package = NULL;
328 const char *section = NULL;
329 const char *option = NULL;
330 struct uci_package *p;
332 int err = UCI_ERR_MEM;
335 nargs = lua_gettop(L);
336 s = luaL_checkstring(L, 1);
339 /* Format: uci.delete("p.s[.o]") */
344 if ((err = uci_parse_tuple(ctx, (char *) s, (char **) &package, (char **) §ion, (char **) &option, NULL)))
348 /* Format: uci.delete("p", "s", "o") */
349 option = luaL_checkstring(L, 3);
352 /* Format: uci.delete("p", "s") */
353 section = luaL_checkstring(L, 2);
361 p = find_package(L, package, AUTOLOAD_ON);
363 err = UCI_ERR_NOTFOUND;
366 err = uci_delete(ctx, p, section, option);
370 uci_lua_perror(L, "uci.set");
371 lua_pushboolean(L, (err == 0));
376 uci_lua_set(lua_State *L)
378 struct uci_package *p;
379 const char *package = NULL;
380 const char *section = NULL;
381 const char *option = NULL;
382 const char *value = NULL;
384 int err = UCI_ERR_MEM;
387 nargs = lua_gettop(L);
389 s = luaL_checkstring(L, 1);
392 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
397 if ((err = uci_parse_tuple(ctx, (char *) s, (char **) &package, (char **) §ion, (char **) &option, (char **) &value)))
401 /* Format: uci.set("p", "s", "o", "v") */
402 option = luaL_checkstring(L, 3);
405 /* Format: uci.set("p", "s", "v") */
407 section = luaL_checkstring(L, 2);
408 value = luaL_checkstring(L, nargs);
415 if ((section == NULL) || (value == NULL)) {
420 p = find_package(L, package, AUTOLOAD_ON);
422 err = UCI_ERR_NOTFOUND;
425 err = uci_set(ctx, p, section, option, value, NULL);
429 uci_lua_perror(L, "uci.set");
430 lua_pushboolean(L, (err == 0));
441 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
443 struct uci_element *e, *tmp;
444 const char *s = NULL;
445 const char *section = NULL;
446 const char *option = NULL;
450 nargs = lua_gettop(L);
453 if (cmd != CMD_REVERT)
455 luaL_checkstring(L, 1);
456 option = lua_tostring(L, -1);
460 if (cmd != CMD_REVERT)
462 luaL_checkstring(L, 1);
463 section = lua_tostring(L, -1);
467 luaL_checkstring(L, 1);
468 s = lua_tostring(L, -1);
475 luaL_error(L, "Invalid argument count");
479 uci_foreach_element_safe(&ctx->root, tmp, e) {
480 struct uci_package *p = uci_to_package(e);
481 int ret = UCI_ERR_INVAL;
483 if (s && (strcmp(s, e->name) != 0))
488 ret = uci_commit(ctx, &p, false);
491 ret = uci_save(ctx, p);
494 ret = uci_revert(ctx, &p, section, option);
502 lua_pushboolean(L, !failed);
507 uci_lua_save(lua_State *L)
509 return uci_lua_package_cmd(L, CMD_SAVE);
513 uci_lua_commit(lua_State *L)
515 return uci_lua_package_cmd(L, CMD_COMMIT);
519 uci_lua_revert(lua_State *L)
521 return uci_lua_package_cmd(L, CMD_REVERT);
525 uci_lua_add_change(lua_State *L, struct uci_element *e)
527 struct uci_history *h;
530 h = uci_to_history(e);
534 lua_getfield(L, -1, h->section);
535 if (lua_isnil(L, -1)) {
537 lua_createtable(L, 0, 0);
538 lua_pushvalue(L, -1); /* copy for setfield */
539 lua_setfield(L, -3, h->section);
542 name = (h->e.name ? h->e.name : ".TYPE");
544 lua_pushstring(L, h->value);
546 lua_pushstring(L, "");
547 lua_setfield(L, -2, name);
552 uci_lua_changes_pkg(lua_State *L, const char *package)
554 struct uci_package *p = NULL;
555 struct uci_element *e;
556 bool autoload = false;
558 p = find_package(L, package, AUTOLOAD_OFF);
561 p = find_package(L, package, AUTOLOAD_FORCE);
566 if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history))
569 lua_createtable(L, 0, 0);
570 uci_foreach_element(&p->saved_history, e) {
571 uci_lua_add_change(L, e);
573 uci_foreach_element(&p->history, e) {
574 uci_lua_add_change(L, e);
576 lua_setfield(L, -2, p->e.name);
584 uci_lua_changes(lua_State *L)
586 const char *package = NULL;
587 char **config = NULL;
591 nargs = lua_gettop(L);
594 package = luaL_checkstring(L, 1);
598 luaL_error(L, "invalid argument count");
601 lua_createtable(L, 0, 0);
603 uci_lua_changes_pkg(L, package);
605 if (uci_list_configs(ctx, &config) != 0)
608 for(i = 0; config[i] != NULL; i++) {
609 uci_lua_changes_pkg(L, config[i]);
618 uci_lua_set_confdir(lua_State *L)
622 luaL_checkstring(L, 1);
623 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
624 lua_pushboolean(L, (ret == 0));
629 uci_lua_set_savedir(lua_State *L)
633 luaL_checkstring(L, 1);
634 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
635 lua_pushboolean(L, (ret == 0));
640 static const luaL_Reg uci[] = {
641 { "load", uci_lua_load },
642 { "unload", uci_lua_unload },
643 { "get", uci_lua_get },
644 { "get_all", uci_lua_get_all },
645 { "add", uci_lua_add },
646 { "set", uci_lua_set },
647 { "save", uci_lua_save },
648 { "delete", uci_lua_delete },
649 { "commit", uci_lua_commit },
650 { "revert", uci_lua_revert },
651 { "changes", uci_lua_changes },
652 { "foreach", uci_lua_foreach },
653 { "set_confdir", uci_lua_set_confdir },
654 { "set_savedir", uci_lua_set_savedir },
660 luaopen_uci(lua_State *L)
662 ctx = uci_alloc_context();
664 luaL_error(L, "Cannot allocate UCI context\n");
665 luaL_register(L, MODNAME, uci);
667 /* enable autoload by default */
668 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
669 lua_pushboolean(L, 1);
670 lua_setfield(L, -2, "autoload");