[backfire] uci: expose add_list changes in lua api (backport of ef5f4ae3252cb067db9d7...
[openwrt-10.03/.git] / package / uci / patches / 150-lua_expose_add_list_changes.patch
1 --- a/lua/uci.c
2 +++ b/lua/uci.c
3 @@ -691,6 +691,7 @@ uci_lua_add_change(lua_State *L, struct 
4  {
5         struct uci_history *h;
6         const char *name;
7 +       const char *value;
8  
9         h = uci_to_history(e);
10         if (!h->section)
11 @@ -704,12 +705,38 @@ uci_lua_add_change(lua_State *L, struct 
12                 lua_setfield(L, -3, h->section);
13         }
14  
15 -       name = (h->e.name ? h->e.name : ".type");
16 -       if (h->value)
17 -               lua_pushstring(L, h->value);
18 -       else
19 -               lua_pushstring(L, "");
20 -       lua_setfield(L, -2, name);
21 +       name = h->e.name;
22 +       value = h->value ? h->value : "";
23 +
24 +       if (name) {
25 +               lua_getfield(L, -1, name);
26 +
27 +               /* there seems to be no value yet */
28 +               if (lua_isnil(L, -1)) {
29 +                       /* this delta is a list add operation, initialize table */
30 +                       if (h->cmd == UCI_CMD_LIST_ADD) {
31 +                               lua_newtable(L);
32 +                               lua_pushstring(L, value);
33 +                               lua_rawseti(L, -2, 1);
34 +                               lua_setfield(L, -3, name);
35 +                       } else {
36 +                               lua_pushstring(L, value);
37 +                               lua_setfield(L, -3, name);
38 +                       }
39 +
40 +               /* a table is on the top of the stack so this is a subsequent,
41 +                * list_add, append this value to table */
42 +               } else if (lua_istable(L, -1)) {
43 +                       lua_pushstring(L, value);
44 +                       lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
45 +               }
46 +
47 +               lua_pop(L, 1);
48 +       } else {
49 +               lua_pushstring(L, value);
50 +               lua_setfield(L, -2, ".type");
51 +       }
52 +
53         lua_pop(L, 1);
54  }
55