X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=lua%2Fuci.c;h=70d02054062ecde669f864329cf07bcfd6a16346;hp=bdac3f6f5c13dd9892f5315bf9d7659fc23dd9c0;hb=cd9311c8d6b7432f507e9eed12587c971875a9de;hpb=740ed93a529b0a8d9dc8a21f7d6fbac65295f4a3 diff --git a/lua/uci.c b/lua/uci.c index bdac3f6..70d0205 100644 --- a/lua/uci.c +++ b/lua/uci.c @@ -426,6 +426,56 @@ error: } static int +uci_lua_rename(lua_State *L) +{ + struct uci_context *ctx; + struct uci_ptr ptr; + int err = UCI_ERR_MEM; + char *s = NULL; + int nargs, offset = 0; + + ctx = find_context(L, &offset); + nargs = lua_gettop(L); + if (lookup_args(L, ctx, offset, &ptr, &s)) + goto error; + + switch(nargs - offset) { + case 1: + /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */ + break; + case 4: + /* Format: uci.set("p", "s", "o", "v") */ + ptr.value = luaL_checkstring(L, nargs); + break; + case 3: + /* Format: uci.set("p", "s", "v") */ + ptr.value = ptr.option; + ptr.option = NULL; + break; + default: + err = UCI_ERR_INVAL; + goto error; + } + + err = uci_lookup_ptr(ctx, &ptr, NULL, false); + if (err) + goto error; + + if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) { + err = UCI_ERR_INVAL; + goto error; + } + + err = uci_rename(ctx, &ptr); + if (err) + goto error; + +error: + return uci_push_status(L, ctx, false); +} + + +static int uci_lua_set(lua_State *L) { struct uci_context *ctx; @@ -477,7 +527,18 @@ uci_lua_set(lua_State *L) } if (istable) { - for (i = 1; i <= lua_objlen(L, nargs); i++) { + if (lua_objlen(L, nargs) == 1) { + i = 1; + if (ptr.o) + err = uci_delete(ctx, &ptr); + } else { + i = 2; + err = uci_set(ctx, &ptr); + if (err) + goto error; + } + + for (; i <= lua_objlen(L, nargs); i++) { lua_rawgeti(L, nargs, i); ptr.value = luaL_checkstring(L, -1); err = uci_add_list(ctx, &ptr); @@ -762,6 +823,7 @@ static const luaL_Reg uci[] = { { "get_all", uci_lua_get_all }, { "add", uci_lua_add }, { "set", uci_lua_set }, + { "rename", uci_lua_rename }, { "save", uci_lua_save }, { "delete", uci_lua_delete }, { "commit", uci_lua_commit },