* luci/applications: luci-fw: implement combined tcp+udp protocol option
[project/luci.git] / contrib / uci / patches / 200-revised-lua-api.patch
1 Index: uci.git/lua/uci.c
2 ===================================================================
3 --- uci.git.orig/lua/uci.c      2008-08-27 16:45:56.000000000 +0200
4 +++ uci.git/lua/uci.c   2008-08-27 16:55:30.000000000 +0200
5 @@ -24,7 +24,8 @@
6  #include <lauxlib.h>
7  #include <uci.h>
8  
9 -#define MODNAME        "uci"
10 +#define MODNAME        "uci2"
11 +#define CURSOR_META    "uci2.cursor.meta"
12  //#define DEBUG 1
13  
14  #ifdef DEBUG
15 @@ -33,7 +34,6 @@
16  #define DPRINTF(...) do {} while (0)
17  #endif
18  
19 -static struct uci_context *ctx = NULL;
20  enum autoload {
21         AUTOLOAD_OFF = 0,
22         AUTOLOAD_ON = 1,
23 @@ -41,7 +41,7 @@
24  };
25  
26  static struct uci_package *
27 -find_package(lua_State *L, const char *str, enum autoload al)
28 +find_package(lua_State *L, struct uci_context *ctx, const char *str, enum autoload al)
29  {
30         struct uci_package *p = NULL;
31         struct uci_element *e;
32 @@ -70,17 +70,8 @@
33                 uci_load(ctx, name, &p);
34         else if (al) {
35                 do {
36 -                       lua_getfield(L, LUA_GLOBALSINDEX, "uci");
37 -                       lua_getfield(L, -1, "autoload");
38 -                       if (!lua_isboolean(L, -1))
39 -                               break;
40 -
41 -                       if (!lua_toboolean(L, -1))
42 -                               break;
43 -
44                         uci_load(ctx, name, &p);
45                 } while (0);
46 -               lua_pop(L, 2);
47         }
48  
49  done:
50 @@ -89,9 +80,9 @@
51         return p;
52  }
53  
54 -static void uci_lua_perror(lua_State *L, char *name)
55 +static void uci_lua_perror(lua_State *L, struct uci_context *ctx, char *name)
56  {
57 -       lua_getfield(L, LUA_GLOBALSINDEX, "uci");
58 +       lua_getfield(L, LUA_GLOBALSINDEX, MODNAME);
59         lua_getfield(L, -1, "warn");
60         if (!lua_isboolean(L, -1))
61                 goto done;
62 @@ -103,33 +94,33 @@
63  }
64  
65  static int
66 -lookup_args(lua_State *L, struct uci_ptr *ptr, char **buf)
67 +lookup_args(lua_State *L, struct uci_context *ctx, struct uci_ptr *ptr, char **buf)
68  {
69         char *s = NULL;
70         int n;
71  
72         n = lua_gettop(L);
73 -       luaL_checkstring(L, 1);
74 -       s = strdup(lua_tostring(L, 1));
75 +       luaL_checkstring(L, 2);
76 +       s = strdup(lua_tostring(L, 2));
77         if (!s)
78                 goto error;
79  
80         memset(ptr, 0, sizeof(struct uci_ptr));
81 -       if (!find_package(L, s, AUTOLOAD_ON))
82 +       if (!find_package(L, ctx, s, AUTOLOAD_ON))
83                 goto error;
84  
85         switch (n) {
86 +       case 5:
87         case 4:
88 -       case 3:
89 -               ptr->option = luaL_checkstring(L, 3);
90 +               ptr->option = luaL_checkstring(L, 4);
91                 /* fall through */
92 -       case 2:
93 -               ptr->section = luaL_checkstring(L, 2);
94 -               ptr->package = luaL_checkstring(L, 1);
95 +       case 3:
96 +               ptr->section = luaL_checkstring(L, 3);
97 +               ptr->package = luaL_checkstring(L, 2);
98                 if (uci_lookup_ptr(ctx, ptr, NULL, false) != UCI_OK)
99                         goto error;
100                 break;
101 -       case 1:
102 +       case 2:
103                 if (uci_lookup_ptr(ctx, ptr, s, false) != UCI_OK)
104                         goto error;
105                 break;
106 @@ -202,15 +193,70 @@
107         }
108  }
109  
110 +static struct uci_context**
111 +uci_lua_context(lua_State *L, int index)
112 +{
113 +       struct uci_context **u = (struct uci_context **)luaL_checkudata(L, index, CURSOR_META);
114 +       luaL_argcheck(L, *u, index, "UCI cursor expected");
115 +       return u;
116 +}
117 +
118 +
119 +static int
120 +uci_lua_cursor(lua_State *L)
121 +{
122 +       int argc = lua_gettop(L);
123 +
124 +       /* create userdata object */
125 +       struct uci_context **u = (struct uci_context **)lua_newuserdata(L, sizeof(struct uci_context *));
126 +
127 +       /* set metatable for userdata */
128 +       luaL_getmetatable(L, CURSOR_META);
129 +       lua_setmetatable(L, -2);
130 +
131 +       /* initialize context */
132 +       *u = uci_alloc_context();
133 +       if (*u == NULL) {
134 +               luaL_error(L, "Cannot allocate UCI context");
135 +       }
136 +
137 +       if (argc == 2 && lua_isnoneornil(L, 2) == 0) {
138 +               if (uci_set_savedir(*u, luaL_checkstring(L, 2)) != 0) {
139 +                       luaL_error(L, "Unable to set savedir");
140 +               }
141 +       }
142 +
143 +       if (argc >= 1 && lua_isnoneornil(L, 1) == 0) {
144 +               if (uci_set_confdir(*u, luaL_checkstring(L, 1)) != 0) {
145 +                       luaL_error(L, "Unable to set confdir");
146 +               }
147 +       }
148 +
149 +       return 1;
150 +}
151 +
152 +static int
153 +uci_lua_gc (lua_State *L) {
154 +       struct uci_context **u = (struct uci_context **)luaL_checkudata(L, 1, CURSOR_META);
155 +
156 +       if (*u) {
157 +               uci_free_context(*u);
158 +               *u = NULL;
159 +       }
160 +
161 +       return 0;
162 +}
163 +
164  static int
165  uci_lua_unload(lua_State *L)
166  {
167 +       struct uci_context *ctx = *uci_lua_context(L, 1);
168         struct uci_package *p;
169         const char *s;
170  
171 -       luaL_checkstring(L, 1);
172 +       luaL_checkstring(L, 2);
173         s = lua_tostring(L, -1);
174 -       p = find_package(L, s, AUTOLOAD_OFF);
175 +       p = find_package(L, ctx, s, AUTOLOAD_OFF);
176         if (p) {
177                 uci_unload(ctx, p);
178                 lua_pushboolean(L, 1);
179 @@ -223,6 +269,7 @@
180  static int
181  uci_lua_load(lua_State *L)
182  {
183 +       struct uci_context *ctx = *uci_lua_context(L, 1);
184         struct uci_package *p = NULL;
185         const char *s;
186  
187 @@ -231,7 +278,7 @@
188         s = lua_tostring(L, -1);
189  
190         if (uci_load(ctx, s, &p)) {
191 -               uci_lua_perror(L, "uci.load");
192 +               uci_lua_perror(L, ctx, "uci.load");
193                 lua_pushboolean(L, 0);
194         } else {
195                 lua_pushboolean(L, 1);
196 @@ -244,22 +291,23 @@
197  static int
198  uci_lua_foreach(lua_State *L)
199  {
200 +       struct uci_context *ctx = *uci_lua_context(L, 1);
201         struct uci_package *p;
202         struct uci_element *e;
203         const char *package, *type;
204         bool ret = false;
205  
206 -       package = luaL_checkstring(L, 1);
207 +       package = luaL_checkstring(L, 2);
208  
209 -       if (lua_isnil(L, 2))
210 +       if (lua_isnil(L, 3))
211                 type = NULL;
212         else
213 -               type = luaL_checkstring(L, 2);
214 +               type = luaL_checkstring(L, 3);
215  
216 -       if (!lua_isfunction(L, 3) || !package)
217 +       if (!lua_isfunction(L, 4) || !package)
218                 luaL_error(L, "Invalid argument");
219  
220 -       p = find_package(L, package, AUTOLOAD_ON);
221 +       p = find_package(L, ctx, package, AUTOLOAD_ON);
222         if (!p)
223                 goto done;
224  
225 @@ -269,7 +317,7 @@
226                 if (type && (strcmp(s->type, type) != 0))
227                         continue;
228  
229 -               lua_pushvalue(L, 3); /* iterator function */
230 +               lua_pushvalue(L, 4); /* iterator function */
231                 uci_push_section(L, s);
232                 if (lua_pcall(L, 1, 0, 0) == 0)
233                         ret = true;
234 @@ -283,12 +331,13 @@
235  static int
236  uci_lua_get_any(lua_State *L, bool all)
237  {
238 +       struct uci_context *ctx = *uci_lua_context(L, 1);
239         struct uci_element *e = NULL;
240         struct uci_ptr ptr;
241         char *s = NULL;
242         int err = UCI_ERR_NOTFOUND;
243  
244 -       if (lookup_args(L, &ptr, &s))
245 +       if (lookup_args(L, ctx, &ptr, &s))
246                 goto error;
247  
248         uci_lookup_ptr(ctx, &ptr, NULL, false);
249 @@ -327,7 +376,7 @@
250         switch(err) {
251         default:
252                 ctx->err = err;
253 -               uci_lua_perror(L, "uci.get");
254 +               uci_lua_perror(L, ctx, "uci.get");
255                 /* fall through */
256         case UCI_ERR_NOTFOUND:
257                 lua_pushnil(L);
258 @@ -352,6 +401,7 @@
259  static int
260  uci_lua_add(lua_State *L)
261  {
262 +       struct uci_context *ctx = *uci_lua_context(L, 1);
263         struct uci_section *s = NULL;
264         struct uci_package *p;
265         const char *package;
266 @@ -359,9 +409,9 @@
267         const char *name = NULL;
268  
269         do {
270 -               package = luaL_checkstring(L, 1);
271 -               type = luaL_checkstring(L, 2);
272 -               p = find_package(L, package, AUTOLOAD_ON);
273 +               package = luaL_checkstring(L, 2);
274 +               type = luaL_checkstring(L, 3);
275 +               p = find_package(L, ctx, package, AUTOLOAD_ON);
276                 if (!p)
277                         break;
278  
279 @@ -378,11 +428,11 @@
280  static int
281  uci_lua_delete(lua_State *L)
282  {
283 +       struct uci_context *ctx = *uci_lua_context(L, 1);
284         struct uci_ptr ptr;
285         char *s = NULL;
286         int err = UCI_ERR_NOTFOUND;
287 -
288 -       if (lookup_args(L, &ptr, &s))
289 +       if (lookup_args(L, ctx, &ptr, &s))
290                 goto error;
291  
292         err = uci_delete(ctx, &ptr);
293 @@ -391,7 +441,7 @@
294         if (s)
295                 free(s);
296         if (err)
297 -               uci_lua_perror(L, "uci.delete");
298 +               uci_lua_perror(L, ctx, "uci.delete");
299         lua_pushboolean(L, (err == 0));
300         return 1;
301  }
302 @@ -399,6 +449,7 @@
303  static int
304  uci_lua_set(lua_State *L)
305  {
306 +       struct uci_context *ctx = *uci_lua_context(L, 1);
307         bool istable = false;
308         struct uci_ptr ptr;
309         int err = UCI_ERR_MEM;
310 @@ -406,14 +457,14 @@
311         int i, nargs;
312  
313         nargs = lua_gettop(L);
314 -       if (lookup_args(L, &ptr, &s))
315 +       if (lookup_args(L, ctx, &ptr, &s))
316                 goto error;
317  
318         switch(nargs) {
319 -       case 1:
320 +       case 2:
321                 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
322                 break;
323 -       case 4:
324 +       case 5:
325                 /* Format: uci.set("p", "s", "o", "v") */
326                 if (lua_istable(L, nargs)) {
327                         if (lua_objlen(L, nargs) < 1)
328 @@ -426,7 +477,7 @@
329                         ptr.value = luaL_checkstring(L, nargs);
330                 }
331                 break;
332 -       case 3:
333 +       case 4:
334                 /* Format: uci.set("p", "s", "v") */
335                 ptr.value = ptr.option;
336                 ptr.option = NULL;
337 @@ -462,7 +513,7 @@
338  
339  error:
340         if (err)
341 -               uci_lua_perror(L, "uci.set");
342 +               uci_lua_perror(L, ctx, "uci.set");
343         lua_pushboolean(L, (err == 0));
344         return 1;
345  }
346 @@ -476,6 +527,7 @@
347  static int
348  uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
349  {
350 +       struct uci_context *ctx = *uci_lua_context(L, 1);
351         struct uci_element *e, *tmp;
352         struct uci_ptr ptr;
353         char *s = NULL;
354 @@ -483,10 +535,10 @@
355         int nargs;
356  
357         nargs = lua_gettop(L);
358 -       if ((cmd != CMD_REVERT) && (nargs > 1))
359 +       if ((cmd != CMD_REVERT) && (nargs > 2))
360                 goto err;
361  
362 -       if (lookup_args(L, &ptr, &s))
363 +       if (lookup_args(L, ctx, &ptr, &s))
364                 goto err;
365  
366         uci_lookup_ptr(ctx, &ptr, NULL, false);
367 @@ -566,16 +618,16 @@
368  }
369  
370  static void
371 -uci_lua_changes_pkg(lua_State *L, const char *package)
372 +uci_lua_changes_pkg(lua_State *L, struct uci_context *ctx, const char *package)
373  {
374         struct uci_package *p = NULL;
375         struct uci_element *e;
376         bool autoload = false;
377  
378 -       p = find_package(L, package, AUTOLOAD_OFF);
379 +       p = find_package(L, ctx, package, AUTOLOAD_OFF);
380         if (!p) {
381                 autoload = true;
382 -               p = find_package(L, package, AUTOLOAD_FORCE);
383 +               p = find_package(L, ctx, package, AUTOLOAD_FORCE);
384                 if (!p)
385                         return;
386         }
387 @@ -600,6 +652,7 @@
388  static int
389  uci_lua_changes(lua_State *L)
390  {
391 +       struct uci_context *ctx = *uci_lua_context(L, 1);
392         const char *package = NULL;
393         char **config = NULL;
394         int nargs;
395 @@ -607,9 +660,9 @@
396  
397         nargs = lua_gettop(L);
398         switch(nargs) {
399 +       case 2:
400 +               package = luaL_checkstring(L, 2);
401         case 1:
402 -               package = luaL_checkstring(L, 1);
403 -       case 0:
404                 break;
405         default:
406                 luaL_error(L, "invalid argument count");
407 @@ -617,13 +670,13 @@
408  
409         lua_newtable(L);
410         if (package) {
411 -               uci_lua_changes_pkg(L, package);
412 +               uci_lua_changes_pkg(L, ctx, package);
413         } else {
414                 if (uci_list_configs(ctx, &config) != 0)
415                         goto done;
416  
417                 for(i = 0; config[i] != NULL; i++) {
418 -                       uci_lua_changes_pkg(L, config[i]);
419 +                       uci_lua_changes_pkg(L, ctx, config[i]);
420                 }
421         }
422  
423 @@ -632,29 +685,53 @@
424  }
425  
426  static int
427 +uci_lua_get_confdir(lua_State *L)
428 +{
429 +       struct uci_context *ctx = *uci_lua_context(L, 1);
430 +       lua_pushstring(L, ctx->confdir);
431 +       return 1;
432 +}
433 +
434 +static int
435  uci_lua_set_confdir(lua_State *L)
436  {
437 +       struct uci_context *ctx = *uci_lua_context(L, 1);
438         int ret;
439  
440 -       luaL_checkstring(L, 1);
441 +       luaL_checkstring(L, 2);
442         ret = uci_set_confdir(ctx, lua_tostring(L, -1));
443         lua_pushboolean(L, (ret == 0));
444         return 1;
445  }
446  
447  static int
448 +uci_lua_get_savedir(lua_State *L)
449 +{
450 +       struct uci_context *ctx = *uci_lua_context(L, 1);
451 +       lua_pushstring(L, ctx->savedir);
452 +       return 1;
453 +}
454 +
455 +static int
456  uci_lua_set_savedir(lua_State *L)
457  {
458 +       struct uci_context *ctx = *uci_lua_context(L, 1);
459         int ret;
460  
461 -       luaL_checkstring(L, 1);
462 +       luaL_checkstring(L, 2);
463         ret = uci_set_savedir(ctx, lua_tostring(L, -1));
464         lua_pushboolean(L, (ret == 0));
465  
466         return 1;
467  }
468  
469 -static const luaL_Reg uci[] = {
470 +static const luaL_Reg uci_module[] = {
471 +       { "cursor", uci_lua_cursor },
472 +       { NULL, NULL },
473 +};
474 +
475 +static const luaL_Reg uci_cursor[] = {
476 +       { "__gc", uci_lua_gc },
477         { "load", uci_lua_load },
478         { "unload", uci_lua_unload },
479         { "get", uci_lua_get },
480 @@ -667,25 +744,33 @@
481         { "revert", uci_lua_revert },
482         { "changes", uci_lua_changes },
483         { "foreach", uci_lua_foreach },
484 +       { "get_confdir", uci_lua_get_confdir },
485 +       { "get_savedir", uci_lua_get_savedir },
486         { "set_confdir", uci_lua_set_confdir },
487         { "set_savedir", uci_lua_set_savedir },
488         { NULL, NULL },
489  };
490  
491 -
492  int
493 -luaopen_uci(lua_State *L)
494 +luaopen_uci2(lua_State *L)
495  {
496 -       ctx = uci_alloc_context();
497 -       if (!ctx)
498 -               luaL_error(L, "Cannot allocate UCI context\n");
499 -       luaL_register(L, MODNAME, uci);
500 -
501 -       /* enable autoload by default */
502 -       lua_getfield(L, LUA_GLOBALSINDEX, "uci");
503 -       lua_pushboolean(L, 1);
504 -       lua_setfield(L, -2, "autoload");
505 -       lua_pop(L, 1);
506 +       /* Create metatable */
507 +       luaL_newmetatable(L, CURSOR_META);
508  
509 -       return 0;
510 +       /* metatable.__index = metatable */
511 +    lua_pushstring(L, "__index");
512 +    lua_pushvalue(L, -2);
513 +    lua_settable(L, -3);
514 +
515 +    /* fill and drop metatable */
516 +    luaL_register(L, NULL, uci_cursor);
517 +    lua_pop(L, 1);
518 +
519 +    /* register module table */
520 +       luaL_register(L, MODNAME, uci_module);
521 +       lua_pushliteral(L, "APIVERSION");
522 +       lua_pushinteger(L, 2);
523 +       lua_settable(L, -3);
524 +
525 +       return 1;
526  }