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>
30 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
32 #define DPRINTF(...) do {} while (0)
35 static struct uci_context *ctx = NULL;
37 static struct uci_package *
38 find_package(const char *name)
40 struct uci_package *p = NULL;
41 struct uci_element *e;
42 uci_foreach_element(&ctx->root, e) {
43 if (strcmp(e->name, name) != 0)
46 p = uci_to_package(e);
52 static void uci_lua_perror(lua_State *L, char *name)
54 lua_getfield(L, LUA_GLOBALSINDEX, "uci");
55 lua_getfield(L, -1, "warn");
56 if (!lua_isboolean(L, -1))
58 if (lua_toboolean(L, -1) != 1)
60 uci_perror(ctx, name);
66 uci_lua_unload(lua_State *L)
68 struct uci_package *p;
71 luaL_checkstring(L, 1);
72 s = lua_tostring(L, -1);
76 lua_pushboolean(L, 1);
78 lua_pushboolean(L, 0);
84 uci_lua_load(lua_State *L)
86 struct uci_package *p = NULL;
90 lua_pop(L, 1); /* bool ret value of unload */
91 s = lua_tostring(L, -1);
93 if (uci_load(ctx, s, &p)) {
94 uci_lua_perror(L, "uci.load");
95 lua_pushboolean(L, 0);
97 lua_pushboolean(L, 1);
104 uci_lua_get(lua_State *L)
106 struct uci_element *e = NULL;
107 struct uci_package *p = NULL;
108 char *package = NULL;
109 char *section = NULL;
112 int err = UCI_ERR_MEM;
114 luaL_checkstring(L, 1);
115 s = strdup(lua_tostring(L, -1));
119 if ((err = uci_parse_tuple(ctx, s, &package, §ion, &option, NULL)))
122 if (section == NULL) {
127 p = find_package(package);
129 err = UCI_ERR_NOTFOUND;
133 if ((err = uci_lookup(ctx, &e, p, section, option)))
137 case UCI_TYPE_SECTION:
138 lua_pushstring(L, uci_to_section(e)->type);
140 case UCI_TYPE_OPTION:
141 lua_pushstring(L, uci_to_option(e)->value);
154 uci_lua_perror(L, "uci.get");
156 case UCI_ERR_NOTFOUND:
166 uci_lua_set(lua_State *L)
168 struct uci_package *p;
169 char *package = NULL;
170 char *section = NULL;
174 int err = UCI_ERR_MEM;
176 luaL_checkstring(L, 1);
177 s = strdup(lua_tostring(L, -1));
181 if ((err = uci_parse_tuple(ctx, s, &package, §ion, &option, &value)))
184 if ((section == NULL) || (value == NULL)) {
189 p = find_package(package);
191 err = UCI_ERR_NOTFOUND;
194 err = uci_set(ctx, p, section, option, value, NULL);
198 uci_lua_perror(L, "uci.set");
199 lua_pushboolean(L, (err == 0));
210 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
212 struct uci_element *e, *tmp;
213 const char *s = NULL;
214 const char *section = NULL;
215 const char *option = NULL;
219 nargs = lua_gettop(L);
222 if (cmd != CMD_REVERT)
224 luaL_checkstring(L, 1);
225 option = lua_tostring(L, -1);
229 if (cmd != CMD_REVERT)
231 luaL_checkstring(L, 1);
232 section = lua_tostring(L, -1);
236 luaL_checkstring(L, 1);
237 s = lua_tostring(L, -1);
242 luaL_error(L, "Invalid argument count");
246 uci_foreach_element_safe(&ctx->root, tmp, e) {
247 struct uci_package *p = uci_to_package(e);
248 int ret = UCI_ERR_INVAL;
250 if (s && (strcmp(s, e->name) != 0))
255 ret = uci_commit(ctx, &p, false);
258 ret = uci_save(ctx, p);
261 ret = uci_revert(ctx, &p, section, option);
269 lua_pushboolean(L, !failed);
274 uci_lua_save(lua_State *L)
276 return uci_lua_package_cmd(L, CMD_SAVE);
280 uci_lua_commit(lua_State *L)
282 return uci_lua_package_cmd(L, CMD_COMMIT);
286 uci_lua_revert(lua_State *L)
288 return uci_lua_package_cmd(L, CMD_REVERT);
292 uci_lua_set_confdir(lua_State *L)
296 luaL_checkstring(L, 1);
297 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
298 lua_pushboolean(L, (ret == 0));
303 uci_lua_set_savedir(lua_State *L)
307 luaL_checkstring(L, 1);
308 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
309 lua_pushboolean(L, (ret == 0));
314 static const luaL_Reg uci[] = {
315 { "load", uci_lua_load },
316 { "unload", uci_lua_unload },
317 { "get", uci_lua_get },
318 { "set", uci_lua_set },
319 { "save", uci_lua_save },
320 { "commit", uci_lua_commit },
321 { "revert", uci_lua_revert },
322 { "set_confdir", uci_lua_set_confdir },
323 { "set_savedir", uci_lua_set_savedir },
329 luaopen_uci(lua_State *L)
331 ctx = uci_alloc_context();
333 luaL_error(L, "Cannot allocate UCI context\n");
334 luaL_register(L, MODNAME, uci);