lua: allow uci.set() to create/update a list value
[project/uci.git] / lua / uci.c
index 1535f56..1c67742 100644 (file)
--- a/lua/uci.c
+++ b/lua/uci.c
@@ -88,6 +88,29 @@ done:
        lua_pop(L, 2);
 }
 
+static void uci_push_option(lua_State *L, struct uci_option *o)
+{
+       struct uci_element *e;
+       int i = 0;
+
+       switch(o->type) {
+       case UCI_TYPE_STRING:
+               lua_pushstring(L, o->v.string);
+               break;
+       case UCI_TYPE_LIST:
+               lua_newtable(L);
+               uci_foreach_element(&o->v.list, e) {
+                       i++;
+                       lua_pushstring(L, e->name);
+                       lua_rawseti(L, -2, i);
+               }
+               break;
+       default:
+               lua_pushnil(L);
+               break;
+       }
+}
+
 static void uci_push_section(lua_State *L, struct uci_section *s)
 {
        struct uci_element *e;
@@ -100,15 +123,8 @@ static void uci_push_section(lua_State *L, struct uci_section *s)
 
        uci_foreach_element(&s->options, e) {
                struct uci_option *o = uci_to_option(e);
-               switch(o->type) {
-               case UCI_TYPE_STRING:
-                       lua_pushstring(L, o->v.string);
-                       lua_setfield(L, -2, o->e.name);
-                       break;
-               default:
-                       /* nothing to do yet */
-                       break;
-               }
+               uci_push_option(L, o);
+               lua_setfield(L, -2, o->e.name);
        }
 }
 
@@ -263,14 +279,7 @@ uci_lua_get_any(lua_State *L, bool all)
                        break;
                case UCI_TYPE_OPTION:
                        o = uci_to_option(e);
-                       switch(o->type) {
-                       case UCI_TYPE_STRING:
-                               lua_pushstring(L, o->v.string);
-                               break;
-                       default:
-                               /* nothing to do yet */
-                               break;
-                       }
+                       uci_push_option(L, o);
                        break;
                default:
                        err = UCI_ERR_INVAL;
@@ -392,7 +401,8 @@ uci_lua_set(lua_State *L)
        const char *value = NULL;
        const char *s;
        int err = UCI_ERR_MEM;
-       int nargs;
+       int i, nargs;
+       bool istable = false;
 
        nargs = lua_gettop(L);
 
@@ -415,7 +425,16 @@ uci_lua_set(lua_State *L)
                /* Format: uci.set("p", "s", "v") */
                package = s;
                section = luaL_checkstring(L, 2);
-               value = luaL_checkstring(L, nargs);
+               if (lua_istable(L, nargs)) {
+                       if (lua_objlen(L, nargs) < 1)
+                               luaL_error(L, "Cannot set an uci option to an empty table value");
+                       lua_rawgeti(L, nargs, 1);
+                       value = luaL_checkstring(L, -1);
+                       lua_pop(L, 1);
+                       istable = true;
+               } else {
+                       value = luaL_checkstring(L, nargs);
+               }
                break;
        default:
                err = UCI_ERR_INVAL;
@@ -433,6 +452,16 @@ uci_lua_set(lua_State *L)
                goto error;
        }
        err = uci_set(ctx, p, section, option, value, NULL);
+       if (istable) {
+               for (i = 2; i <= lua_objlen(L, nargs); i++) {
+                       lua_rawgeti(L, nargs, i);
+                       value = luaL_checkstring(L, -1);
+                       err = uci_add_list(ctx, p, section, option, value, NULL);
+                       lua_pop(L, 1);
+                       if (err)
+                               goto error;
+               }
+       }
 
 error:
        if (err)