add get functions for savedir and confdir
[project/uci.git] / lua / uci.c
index b64c91f..3f8f3a5 100644 (file)
--- a/lua/uci.c
+++ b/lua/uci.c
@@ -119,6 +119,7 @@ lookup_args(lua_State *L, struct uci_ptr *ptr, char **buf)
                goto error;
 
        switch (n) {
+       case 4:
        case 3:
                ptr->option = luaL_checkstring(L, 3);
                /* fall through */
@@ -133,6 +134,7 @@ lookup_args(lua_State *L, struct uci_ptr *ptr, char **buf)
                        goto error;
                break;
        default:
+               luaL_error(L, "invalid argument count");
                goto error;
        }
 
@@ -173,6 +175,8 @@ static void uci_push_section(lua_State *L, struct uci_section *s)
        struct uci_element *e;
 
        lua_newtable(L);
+       lua_pushboolean(L, s->anonymous);
+       lua_setfield(L, -2, ".anonymous");
        lua_pushstring(L, s->type);
        lua_setfield(L, -2, ".type");
        lua_pushstring(L, s->e.name);
@@ -292,6 +296,10 @@ uci_lua_get_any(lua_State *L, bool all)
                err = UCI_ERR_INVAL;
                goto error;
        }
+       if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
+               err = UCI_ERR_NOTFOUND;
+               goto error;
+       }
 
        err = UCI_OK;
        e = ptr.last;
@@ -432,7 +440,7 @@ uci_lua_set(lua_State *L)
        if (err)
                goto error;
 
-       if ((ptr.s == NULL) || (ptr.value == NULL)) {
+       if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
                err = UCI_ERR_INVAL;
                goto error;
        }
@@ -624,6 +632,13 @@ done:
 }
 
 static int
+uci_lua_get_confdir(lua_State *L)
+{
+       lua_pushstring(L, ctx->confdir);
+       return 1;
+}
+
+static int
 uci_lua_set_confdir(lua_State *L)
 {
        int ret;
@@ -635,6 +650,13 @@ uci_lua_set_confdir(lua_State *L)
 }
 
 static int
+uci_lua_get_savedir(lua_State *L)
+{
+       lua_pushstring(L, ctx->savedir);
+       return 1;
+}
+
+static int
 uci_lua_set_savedir(lua_State *L)
 {
        int ret;
@@ -659,7 +681,9 @@ static const luaL_Reg uci[] = {
        { "revert", uci_lua_revert },
        { "changes", uci_lua_changes },
        { "foreach", uci_lua_foreach },
+       { "get_confdir", uci_lua_get_confdir },
        { "set_confdir", uci_lua_set_confdir },
+       { "get_savedir", uci_lua_get_savedir },
        { "set_savedir", uci_lua_set_savedir },
        { NULL, NULL },
 };