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;
38 static struct uci_package *
39 find_package(lua_State *L, const char *name, bool autoload)
41 struct uci_package *p = NULL;
42 struct uci_element *e;
44 uci_foreach_element(&ctx->root, e) {
45 if (strcmp(e->name, name) != 0)
48 p = uci_to_package(e);
54 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
55 lua_getfield(L, -1, "autoload");
56 if (!lua_isboolean(L, -1))
59 if (!lua_toboolean(L, -1))
62 uci_load(ctx, name, &p);
71 static void uci_lua_perror(lua_State *L, char *name)
73 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
74 lua_getfield(L, -1, "warn");
75 if (!lua_isboolean(L, -1))
77 if (lua_toboolean(L, -1) != 1)
79 uci_perror(ctx, name);
84 static void uci_push_section(lua_State *L, struct uci_section *s)
86 struct uci_element *e;
89 lua_pushstring(L, s->type);
90 lua_setfield(L, -2, "type");
91 lua_pushstring(L, s->e.name);
92 lua_setfield(L, -2, "name");
96 lua_setfield(L, -3, "options");
98 uci_foreach_element(&s->options, e) {
99 struct uci_option *o = uci_to_option(e);
100 lua_pushstring(L, o->value);
101 lua_setfield(L, -2, o->e.name);
106 static void uci_push_package(lua_State *L, struct uci_package *p)
108 struct uci_element *e;
112 uci_foreach_element(&p->sections, e) {
115 uci_push_section(L, uci_to_section(e));
116 lua_rawseti(L, -2, i);
121 uci_lua_unload(lua_State *L)
123 struct uci_package *p;
126 luaL_checkstring(L, 1);
127 s = lua_tostring(L, -1);
128 p = find_package(L, s, false);
131 lua_pushboolean(L, 1);
133 lua_pushboolean(L, 0);
139 uci_lua_load(lua_State *L)
141 struct uci_package *p = NULL;
145 lua_pop(L, 1); /* bool ret value of unload */
146 s = lua_tostring(L, -1);
148 if (uci_load(ctx, s, &p)) {
149 uci_lua_perror(L, "uci.load");
150 lua_pushboolean(L, 0);
152 lua_pushboolean(L, 1);
160 uci_lua_foreach(lua_State *L)
162 struct uci_package *p;
163 struct uci_element *e;
164 const char *package, *type;
167 package = luaL_checkstring(L, 1);
172 type = luaL_checkstring(L, 2);
174 if (!lua_isfunction(L, 3) || !package)
175 luaL_error(L, "Invalid argument");
177 p = find_package(L, package, true);
181 uci_foreach_element(&p->sections, e) {
182 struct uci_section *s = uci_to_section(e);
184 if (type && (strcmp(s->type, type) != 0))
187 lua_pushvalue(L, 3); /* iterator function */
188 uci_push_section(L, s);
189 if (lua_pcall(L, 1, 0, 0) == 0)
194 lua_pushboolean(L, ret);
199 uci_lua_get_any(lua_State *L, bool all)
201 struct uci_element *e = NULL;
202 struct uci_package *p = NULL;
203 const char *package = NULL;
204 const char *section = NULL;
205 const char *option = NULL;
207 int err = UCI_ERR_MEM;
212 luaL_checkstring(L, 1);
213 s = strdup(lua_tostring(L, 1));
218 package = luaL_checkstring(L, 1);
219 section = luaL_checkstring(L, 2);
221 option = luaL_checkstring(L, 3);
223 if ((err = uci_parse_tuple(ctx, s, (char **) &package, (char **) §ion, (char **) &option, NULL)))
227 if (!all && (section == NULL)) {
232 p = find_package(L, package, true);
234 err = UCI_ERR_NOTFOUND;
239 if ((err = uci_lookup(ctx, &e, p, section, option)))
246 case UCI_TYPE_PACKAGE:
247 uci_push_package(L, p);
249 case UCI_TYPE_SECTION:
251 uci_push_section(L, uci_to_section(e));
253 lua_pushstring(L, uci_to_section(e)->type);
255 case UCI_TYPE_OPTION:
256 lua_pushstring(L, uci_to_option(e)->value);
269 uci_lua_perror(L, "uci.get");
271 case UCI_ERR_NOTFOUND:
280 uci_lua_get(lua_State *L)
282 return uci_lua_get_any(L, false);
286 uci_lua_get_all(lua_State *L)
288 return uci_lua_get_any(L, true);
292 uci_lua_add(lua_State *L)
294 struct uci_section *s = NULL;
295 struct uci_package *p;
298 const char *name = NULL;
301 package = luaL_checkstring(L, 1);
302 type = luaL_checkstring(L, 2);
303 p = find_package(L, package, true);
307 if (uci_add_section(ctx, p, type, &s) || !s)
313 lua_pushstring(L, name);
318 uci_lua_set(lua_State *L)
320 struct uci_package *p;
321 const char *package = NULL;
322 const char *section = NULL;
323 const char *option = NULL;
324 const char *value = NULL;
326 int err = UCI_ERR_MEM;
329 nargs = lua_gettop(L);
331 s = luaL_checkstring(L, 1);
334 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
339 if ((err = uci_parse_tuple(ctx, (char *) s, (char **) &package, (char **) §ion, (char **) &option, (char **) &value)))
343 /* Format: uci.set("p", "s", "o", "v") */
344 option = luaL_checkstring(L, 3);
347 /* Format: uci.set("p", "s", "v") */
349 section = luaL_checkstring(L, 2);
350 value = luaL_checkstring(L, nargs);
357 if ((section == NULL) || (value == NULL)) {
362 p = find_package(L, package, true);
364 err = UCI_ERR_NOTFOUND;
367 err = uci_set(ctx, p, section, option, value, NULL);
371 uci_lua_perror(L, "uci.set");
372 lua_pushboolean(L, (err == 0));
383 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
385 struct uci_element *e, *tmp;
386 const char *s = NULL;
387 const char *section = NULL;
388 const char *option = NULL;
392 nargs = lua_gettop(L);
395 if (cmd != CMD_REVERT)
397 luaL_checkstring(L, 1);
398 option = lua_tostring(L, -1);
402 if (cmd != CMD_REVERT)
404 luaL_checkstring(L, 1);
405 section = lua_tostring(L, -1);
409 luaL_checkstring(L, 1);
410 s = lua_tostring(L, -1);
417 luaL_error(L, "Invalid argument count");
421 uci_foreach_element_safe(&ctx->root, tmp, e) {
422 struct uci_package *p = uci_to_package(e);
423 int ret = UCI_ERR_INVAL;
425 if (s && (strcmp(s, e->name) != 0))
430 ret = uci_commit(ctx, &p, false);
433 ret = uci_save(ctx, p);
436 ret = uci_revert(ctx, &p, section, option);
444 lua_pushboolean(L, !failed);
449 uci_lua_save(lua_State *L)
451 return uci_lua_package_cmd(L, CMD_SAVE);
455 uci_lua_commit(lua_State *L)
457 return uci_lua_package_cmd(L, CMD_COMMIT);
461 uci_lua_revert(lua_State *L)
463 return uci_lua_package_cmd(L, CMD_REVERT);
467 uci_lua_set_confdir(lua_State *L)
471 luaL_checkstring(L, 1);
472 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
473 lua_pushboolean(L, (ret == 0));
478 uci_lua_set_savedir(lua_State *L)
482 luaL_checkstring(L, 1);
483 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
484 lua_pushboolean(L, (ret == 0));
489 static const luaL_Reg uci[] = {
490 { "load", uci_lua_load },
491 { "unload", uci_lua_unload },
492 { "get", uci_lua_get },
493 { "get_all", uci_lua_get_all },
494 { "add", uci_lua_add },
495 { "set", uci_lua_set },
496 { "save", uci_lua_save },
497 { "commit", uci_lua_commit },
498 { "revert", uci_lua_revert },
499 { "foreach", uci_lua_foreach },
500 { "set_confdir", uci_lua_set_confdir },
501 { "set_savedir", uci_lua_set_savedir },
507 luaopen_uci(lua_State *L)
509 ctx = uci_alloc_context();
511 luaL_error(L, "Cannot allocate UCI context\n");
512 luaL_register(L, MODNAME, uci);
514 /* enable autoload by default */
515 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
516 lua_pushboolean(L, 1);
517 lua_setfield(L, -2, "autoload");