uci: expose add_list changes in lua api (backport of ef5f4ae3252cb067db9d73afca3f5f72...
authorJo-Philipp Wich <jow@openwrt.org>
Mon, 14 Nov 2011 13:33:25 +0000 (13:33 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Mon, 14 Nov 2011 13:33:25 +0000 (13:33 +0000)
SVN-Revision: 29113

package/uci/patches/150-lua_expose_add_list_changes.patch [new file with mode: 0644]

diff --git a/package/uci/patches/150-lua_expose_add_list_changes.patch b/package/uci/patches/150-lua_expose_add_list_changes.patch
new file mode 100644 (file)
index 0000000..9cd32d3
--- /dev/null
@@ -0,0 +1,55 @@
+--- a/lua/uci.c
++++ b/lua/uci.c
+@@ -691,6 +691,7 @@ uci_lua_add_change(lua_State *L, struct 
+ {
+       struct uci_history *h;
+       const char *name;
++      const char *value;
+       h = uci_to_history(e);
+       if (!h->section)
+@@ -704,12 +705,38 @@ uci_lua_add_change(lua_State *L, struct 
+               lua_setfield(L, -3, h->section);
+       }
+-      name = (h->e.name ? h->e.name : ".type");
+-      if (h->value)
+-              lua_pushstring(L, h->value);
+-      else
+-              lua_pushstring(L, "");
+-      lua_setfield(L, -2, name);
++      name = h->e.name;
++      value = h->value ? h->value : "";
++
++      if (name) {
++              lua_getfield(L, -1, name);
++
++              /* there seems to be no value yet */
++              if (lua_isnil(L, -1)) {
++                      /* this delta is a list add operation, initialize table */
++                      if (h->cmd == UCI_CMD_LIST_ADD) {
++                              lua_newtable(L);
++                              lua_pushstring(L, value);
++                              lua_rawseti(L, -2, 1);
++                              lua_setfield(L, -3, name);
++                      } else {
++                              lua_pushstring(L, value);
++                              lua_setfield(L, -3, name);
++                      }
++
++              /* a table is on the top of the stack so this is a subsequent,
++               * list_add, append this value to table */
++              } else if (lua_istable(L, -1)) {
++                      lua_pushstring(L, value);
++                      lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
++              }
++
++              lua_pop(L, 1);
++      } else {
++              lua_pushstring(L, value);
++              lua_setfield(L, -2, ".type");
++      }
++
+       lua_pop(L, 1);
+ }