add uci.rename()
[project/uci.git] / lua / uci.c
index 2d3b972..67e9418 100644 (file)
--- 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;
@@ -476,12 +526,8 @@ uci_lua_set(lua_State *L)
                goto error;
        }
 
-       err = uci_set(ctx, &ptr);
-       if (err)
-               goto error;
-
        if (istable) {
-               for (i = 2; i <= lua_objlen(L, nargs); i++) {
+               for (i = 1; i <= lua_objlen(L, nargs); i++) {
                        lua_rawgeti(L, nargs, i);
                        ptr.value = luaL_checkstring(L, -1);
                        err = uci_add_list(ctx, &ptr);
@@ -489,8 +535,13 @@ uci_lua_set(lua_State *L)
                        if (err)
                                goto error;
                }
+       } else {
+               err = uci_set(ctx, &ptr);
+               if (err)
+                       goto error;
        }
 
+
 error:
        return uci_push_status(L, ctx, false);
 }
@@ -691,6 +742,18 @@ uci_lua_get_savedir(lua_State *L)
 }
 
 static int
+uci_lua_add_history(lua_State *L)
+{
+       struct uci_context *ctx;
+       int ret, offset = 0;
+
+       ctx = find_context(L, &offset);
+       luaL_checkstring(L, 1 + offset);
+       ret = uci_add_history_path(ctx, lua_tostring(L, -1));
+       return uci_push_status(L, ctx, false);
+}
+
+static int
 uci_lua_set_savedir(lua_State *L)
 {
        struct uci_context *ctx;
@@ -749,12 +812,14 @@ 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 },
        { "revert", uci_lua_revert },
        { "changes", uci_lua_changes },
        { "foreach", uci_lua_foreach },
+       { "add_history", uci_lua_add_history },
        { "get_confdir", uci_lua_get_confdir },
        { "set_confdir", uci_lua_set_confdir },
        { "get_savedir", uci_lua_get_savedir },