add list support to the uci lua binding
[project/uci.git] / lua / uci.c
index 2e1e513..b3be0c9 100644 (file)
--- a/lua/uci.c
+++ b/lua/uci.c
@@ -88,17 +88,42 @@ 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;
 
        lua_newtable(L);
        lua_pushstring(L, s->type);
-       lua_setfield(L, -2, ".TYPE");
+       lua_setfield(L, -2, ".type");
+       lua_pushstring(L, s->e.name);
+       lua_setfield(L, -2, ".name");
 
        uci_foreach_element(&s->options, e) {
                struct uci_option *o = uci_to_option(e);
-               lua_pushstring(L, o->value);
+               uci_push_option(L, o);
                lua_setfield(L, -2, o->e.name);
        }
 }
@@ -199,6 +224,7 @@ uci_lua_get_any(lua_State *L, bool all)
 {
        struct uci_element *e = NULL;
        struct uci_package *p = NULL;
+       struct uci_option *o = NULL;
        const char *package = NULL;
        const char *section = NULL;
        const char *option = NULL;
@@ -252,7 +278,8 @@ uci_lua_get_any(lua_State *L, bool all)
                                lua_pushstring(L, uci_to_section(e)->type);
                        break;
                case UCI_TYPE_OPTION:
-                       lua_pushstring(L, uci_to_option(e)->value);
+                       o = uci_to_option(e);
+                       uci_push_option(L, o);
                        break;
                default:
                        err = UCI_ERR_INVAL;
@@ -531,7 +558,7 @@ uci_lua_add_change(lua_State *L, struct uci_element *e)
                lua_setfield(L, -3, h->section);
        }
 
-       name = (h->e.name ? h->e.name : ".TYPE");
+       name = (h->e.name ? h->e.name : ".type");
        if (h->value)
                lua_pushstring(L, h->value);
        else