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(const char *name)
41 struct uci_package *p = NULL;
42 struct uci_element *e;
43 uci_foreach_element(&ctx->root, e) {
44 if (strcmp(e->name, name) != 0)
47 p = uci_to_package(e);
53 static void uci_lua_perror(lua_State *L, char *name)
55 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
56 lua_getfield(L, -1, "warn");
57 if (!lua_isboolean(L, -1))
59 if (lua_toboolean(L, -1) != 1)
61 uci_perror(ctx, name);
66 static void uci_push_section(lua_State *L, struct uci_section *s)
68 struct uci_element *e;
71 lua_pushstring(L, s->type);
72 lua_setfield(L, -2, "type");
73 lua_pushstring(L, s->e.name);
74 lua_setfield(L, -2, "name");
78 lua_setfield(L, -3, "options");
80 uci_foreach_element(&s->options, e) {
81 struct uci_option *o = uci_to_option(e);
82 lua_pushstring(L, o->value);
83 lua_setfield(L, -2, o->e.name);
88 static void uci_push_package(lua_State *L, struct uci_package *p)
90 struct uci_element *e;
94 uci_foreach_element(&p->sections, e) {
97 uci_push_section(L, uci_to_section(e));
98 lua_rawseti(L, -2, i);
103 uci_lua_unload(lua_State *L)
105 struct uci_package *p;
108 luaL_checkstring(L, 1);
109 s = lua_tostring(L, -1);
113 lua_pushboolean(L, 1);
115 lua_pushboolean(L, 0);
121 uci_lua_load(lua_State *L)
123 struct uci_package *p = NULL;
127 lua_pop(L, 1); /* bool ret value of unload */
128 s = lua_tostring(L, -1);
130 if (uci_load(ctx, s, &p)) {
131 uci_lua_perror(L, "uci.load");
132 lua_pushboolean(L, 0);
134 lua_pushboolean(L, 1);
141 uci_lua_get_any(lua_State *L, bool all)
143 struct uci_element *e = NULL;
144 struct uci_package *p = NULL;
145 char *package = NULL;
146 char *section = NULL;
149 int err = UCI_ERR_MEM;
151 luaL_checkstring(L, 1);
152 s = strdup(lua_tostring(L, -1));
156 if ((err = uci_parse_tuple(ctx, s, &package, §ion, &option, NULL)))
159 if (!all && (section == NULL)) {
164 p = find_package(package);
166 err = UCI_ERR_NOTFOUND;
171 if ((err = uci_lookup(ctx, &e, p, section, option)))
178 case UCI_TYPE_PACKAGE:
179 uci_push_package(L, p);
181 case UCI_TYPE_SECTION:
183 uci_push_section(L, uci_to_section(e));
185 lua_pushstring(L, uci_to_section(e)->type);
187 case UCI_TYPE_OPTION:
188 lua_pushstring(L, uci_to_option(e)->value);
201 uci_lua_perror(L, "uci.get");
203 case UCI_ERR_NOTFOUND:
212 uci_lua_get(lua_State *L)
214 return uci_lua_get_any(L, false);
218 uci_lua_get_all(lua_State *L)
220 return uci_lua_get_any(L, true);
224 uci_lua_set(lua_State *L)
226 struct uci_package *p;
227 char *package = NULL;
228 char *section = NULL;
232 int err = UCI_ERR_MEM;
234 luaL_checkstring(L, 1);
235 s = strdup(lua_tostring(L, -1));
239 if ((err = uci_parse_tuple(ctx, s, &package, §ion, &option, &value)))
242 if ((section == NULL) || (value == NULL)) {
247 p = find_package(package);
249 err = UCI_ERR_NOTFOUND;
252 err = uci_set(ctx, p, section, option, value, NULL);
256 uci_lua_perror(L, "uci.set");
257 lua_pushboolean(L, (err == 0));
268 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
270 struct uci_element *e, *tmp;
271 const char *s = NULL;
272 const char *section = NULL;
273 const char *option = NULL;
277 nargs = lua_gettop(L);
280 if (cmd != CMD_REVERT)
282 luaL_checkstring(L, 1);
283 option = lua_tostring(L, -1);
287 if (cmd != CMD_REVERT)
289 luaL_checkstring(L, 1);
290 section = lua_tostring(L, -1);
294 luaL_checkstring(L, 1);
295 s = lua_tostring(L, -1);
300 luaL_error(L, "Invalid argument count");
304 uci_foreach_element_safe(&ctx->root, tmp, e) {
305 struct uci_package *p = uci_to_package(e);
306 int ret = UCI_ERR_INVAL;
308 if (s && (strcmp(s, e->name) != 0))
313 ret = uci_commit(ctx, &p, false);
316 ret = uci_save(ctx, p);
319 ret = uci_revert(ctx, &p, section, option);
327 lua_pushboolean(L, !failed);
332 uci_lua_save(lua_State *L)
334 return uci_lua_package_cmd(L, CMD_SAVE);
338 uci_lua_commit(lua_State *L)
340 return uci_lua_package_cmd(L, CMD_COMMIT);
344 uci_lua_revert(lua_State *L)
346 return uci_lua_package_cmd(L, CMD_REVERT);
350 uci_lua_set_confdir(lua_State *L)
354 luaL_checkstring(L, 1);
355 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
356 lua_pushboolean(L, (ret == 0));
361 uci_lua_set_savedir(lua_State *L)
365 luaL_checkstring(L, 1);
366 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
367 lua_pushboolean(L, (ret == 0));
372 static const luaL_Reg uci[] = {
373 { "load", uci_lua_load },
374 { "unload", uci_lua_unload },
375 { "get", uci_lua_get },
376 { "get_all", uci_lua_get_all },
377 { "set", uci_lua_set },
378 { "save", uci_lua_save },
379 { "commit", uci_lua_commit },
380 { "revert", uci_lua_revert },
381 { "set_confdir", uci_lua_set_confdir },
382 { "set_savedir", uci_lua_set_savedir },
388 luaopen_uci(lua_State *L)
390 ctx = uci_alloc_context();
392 luaL_error(L, "Cannot allocate UCI context\n");
393 luaL_register(L, MODNAME, uci);