X-Git-Url: https://git.archive.openwrt.org/?a=blobdiff_plain;f=lua%2Fuci.c;h=1c67742736df9871d9cb7ca7ae8745961b958feb;hb=b485972f3a40be1627fb2bdd9904889a155f12e0;hp=f2be5d5ca3bba253b29865e9045b3d91d79d8124;hpb=b809dbd44309be056a7ce96b18a418fb51641cce;p=project%2Fuci.git diff --git a/lua/uci.c b/lua/uci.c index f2be5d5..1c67742 100644 --- a/lua/uci.c +++ b/lua/uci.c @@ -88,26 +88,44 @@ 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"); - - lua_newtable(L); - lua_pushvalue(L, -1); - lua_setfield(L, -3, "options"); + 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); } - lua_pop(L, 1); } static void uci_push_package(lua_State *L, struct uci_package *p) @@ -118,9 +136,8 @@ static void uci_push_package(lua_State *L, struct uci_package *p) lua_newtable(L); uci_foreach_element(&p->sections, e) { i++; - luaL_setn(L, -1, i); uci_push_section(L, uci_to_section(e)); - lua_rawseti(L, -2, i); + lua_setfield(L, -2, e->name); } } @@ -207,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; @@ -260,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; @@ -382,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); @@ -405,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; @@ -423,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) @@ -534,12 +573,12 @@ uci_lua_add_change(lua_State *L, struct uci_element *e) lua_getfield(L, -1, h->section); if (lua_isnil(L, -1)) { lua_pop(L, 1); - lua_createtable(L, 0, 0); + lua_newtable(L); lua_pushvalue(L, -1); /* copy for setfield */ 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 @@ -566,7 +605,7 @@ uci_lua_changes_pkg(lua_State *L, const char *package) if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history)) goto done; - lua_createtable(L, 0, 0); + lua_newtable(L); uci_foreach_element(&p->saved_history, e) { uci_lua_add_change(L, e); } @@ -598,7 +637,7 @@ uci_lua_changes(lua_State *L) luaL_error(L, "invalid argument count"); } - lua_createtable(L, 0, 0); + lua_newtable(L); if (package) { uci_lua_changes_pkg(L, package); } else {