X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;f=lua%2Fuci.c;h=ce5c31e1372d98586a1b4ebaaa7fec16640a4679;hb=66311debd9b145ae37087df7c7f3ed16a3852090;hp=026569f86684d3790581a633b34fc471a9c34da7;hpb=42c5188ed499746fc09917707c929bd96339be14;p=project%2Fuci.git diff --git a/lua/uci.c b/lua/uci.c index 026569f..ce5c31e 100644 --- a/lua/uci.c +++ b/lua/uci.c @@ -63,18 +63,33 @@ done: } static int -uci_lua_load(lua_State *L) +uci_lua_unload(lua_State *L) { struct uci_package *p; const char *s; - luaL_checkany(L, 1); + luaL_checkstring(L, 1); s = lua_tostring(L, -1); p = find_package(s); if (p) { uci_unload(ctx, p); - p = NULL; + lua_pushboolean(L, 1); + } else { + lua_pushboolean(L, 0); } + return 1; +} + +static int +uci_lua_load(lua_State *L) +{ + struct uci_package *p = NULL; + const char *s; + + uci_lua_unload(L); + lua_pop(L, 1); /* bool ret value of unload */ + s = lua_tostring(L, -1); + if (uci_load(ctx, s, &p)) { uci_lua_perror(L, "uci.load"); lua_pushboolean(L, 0); @@ -97,7 +112,7 @@ uci_lua_get(lua_State *L) char *s; int err = UCI_ERR_MEM; - luaL_checkany(L, 1); + luaL_checkstring(L, 1); s = strdup(lua_tostring(L, -1)); if (!s) goto error; @@ -148,9 +163,124 @@ error: } +static int +uci_lua_set(lua_State *L) +{ + struct uci_package *p; + char *package = NULL; + char *section = NULL; + char *option = NULL; + char *value = NULL; + char *s; + int err = UCI_ERR_MEM; + + luaL_checkstring(L, 1); + s = strdup(lua_tostring(L, -1)); + if (!s) + goto error; + + if ((err = uci_parse_tuple(ctx, s, &package, §ion, &option, &value))) + goto error; + + if ((section == NULL) || (value == NULL)) { + err = UCI_ERR_INVAL; + goto error; + } + + p = find_package(package); + if (!p) { + err = UCI_ERR_NOTFOUND; + goto error; + } + err = uci_set(ctx, p, section, option, value, NULL); + +error: + if (err) + uci_lua_perror(L, "uci.set"); + lua_pushboolean(L, (err == 0)); + return 1; +} + +static int +uci_lua_commit(lua_State *L) +{ + struct uci_element *e, *tmp; + const char *s = NULL; + int failed = 0; + + if (!lua_isnoneornil(L, -1)) { + luaL_checkstring(L, 1); + s = lua_tostring(L, -1); + } + + uci_foreach_element_safe(&ctx->root, tmp, e) { + struct uci_package *p = uci_to_package(e); + + if (s && (strcmp(s, e->name) != 0)) + continue; + + if (uci_commit(ctx, &p, false) != 0) + failed = 1; + } + lua_pushboolean(L, !failed); + return 1; +} + +static int +uci_lua_save(lua_State *L) +{ + struct uci_element *e; + const char *s = NULL; + int failed = 0; + + if (!lua_isnoneornil(L, -1)) { + luaL_checkstring(L, 1); + s = lua_tostring(L, -1); + } + + uci_foreach_element(&ctx->root, e) { + if (s && (strcmp(s, e->name) != 0)) + continue; + + if (uci_save(ctx, uci_to_package(e)) != 0) + failed = 1; + } + lua_pushboolean(L, !failed); + return 1; +} + +static int +uci_lua_set_confdir(lua_State *L) +{ + int ret; + + luaL_checkstring(L, 1); + ret = uci_set_confdir(ctx, lua_tostring(L, -1)); + lua_pushboolean(L, (ret == 0)); + return 1; +} + +static int +uci_lua_set_savedir(lua_State *L) +{ + int ret; + + luaL_checkstring(L, 1); + ret = uci_set_savedir(ctx, lua_tostring(L, -1)); + lua_pushboolean(L, (ret == 0)); + + return 1; +} + static const luaL_Reg uci[] = { { "load", uci_lua_load }, + { "unload", uci_lua_unload }, { "get", uci_lua_get }, + { "set", uci_lua_set }, + { "save", uci_lua_save }, + { "commit", uci_lua_commit }, + { "set_confdir", uci_lua_set_confdir }, + { "set_savedir", uci_lua_set_savedir }, { NULL, NULL }, };