c464941cf1c946bb2adcebe99050f61b131cda4d
[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-26 12:31:34.000000000 +0200
4 +++ uci.git/lua/uci.c   2008-08-27 10:27:29.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 @@ -297,6 +346,11 @@
250                 goto error;
251         }
252  
253 +       if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
254 +               err = UCI_ERR_NOTFOUND;
255 +               goto error;
256 +       }
257 +
258         err = UCI_OK;
259         e = ptr.last;
260         switch(e->type) {
261 @@ -323,7 +377,7 @@
262         switch(err) {
263         default:
264                 ctx->err = err;
265 -               uci_lua_perror(L, "uci.get");
266 +               uci_lua_perror(L, ctx, "uci.get");
267                 /* fall through */
268         case UCI_ERR_NOTFOUND:
269                 lua_pushnil(L);
270 @@ -348,6 +402,7 @@
271  static int
272  uci_lua_add(lua_State *L)
273  {
274 +       struct uci_context *ctx = *uci_lua_context(L, 1);
275         struct uci_section *s = NULL;
276         struct uci_package *p;
277         const char *package;
278 @@ -355,9 +410,9 @@
279         const char *name = NULL;
280  
281         do {
282 -               package = luaL_checkstring(L, 1);
283 -               type = luaL_checkstring(L, 2);
284 -               p = find_package(L, package, AUTOLOAD_ON);
285 +               package = luaL_checkstring(L, 2);
286 +               type = luaL_checkstring(L, 3);
287 +               p = find_package(L, ctx, package, AUTOLOAD_ON);
288                 if (!p)
289                         break;
290  
291 @@ -374,11 +429,11 @@
292  static int
293  uci_lua_delete(lua_State *L)
294  {
295 +       struct uci_context *ctx = *uci_lua_context(L, 1);
296         struct uci_ptr ptr;
297         char *s = NULL;
298         int err = UCI_ERR_NOTFOUND;
299 -
300 -       if (lookup_args(L, &ptr, &s))
301 +       if (lookup_args(L, ctx, &ptr, &s))
302                 goto error;
303  
304         err = uci_delete(ctx, &ptr);
305 @@ -387,7 +442,7 @@
306         if (s)
307                 free(s);
308         if (err)
309 -               uci_lua_perror(L, "uci.delete");
310 +               uci_lua_perror(L, ctx, "uci.delete");
311         lua_pushboolean(L, (err == 0));
312         return 1;
313  }
314 @@ -395,6 +450,7 @@
315  static int
316  uci_lua_set(lua_State *L)
317  {
318 +       struct uci_context *ctx = *uci_lua_context(L, 1);
319         bool istable = false;
320         struct uci_ptr ptr;
321         int err = UCI_ERR_MEM;
322 @@ -402,14 +458,14 @@
323         int i, nargs;
324  
325         nargs = lua_gettop(L);
326 -       if (lookup_args(L, &ptr, &s))
327 +       if (lookup_args(L, ctx, &ptr, &s))
328                 goto error;
329  
330         switch(nargs) {
331 -       case 1:
332 +       case 2:
333                 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
334                 break;
335 -       case 4:
336 +       case 5:
337                 /* Format: uci.set("p", "s", "o", "v") */
338                 if (lua_istable(L, nargs)) {
339                         if (lua_objlen(L, nargs) < 1)
340 @@ -422,7 +478,7 @@
341                         ptr.value = luaL_checkstring(L, nargs);
342                 }
343                 break;
344 -       case 3:
345 +       case 4:
346                 /* Format: uci.set("p", "s", "v") */
347                 ptr.value = ptr.option;
348                 ptr.option = NULL;
349 @@ -433,17 +489,23 @@
350         }
351  
352         err = uci_lookup_ptr(ctx, &ptr, NULL, false);
353 -       if (err)
354 +       if (err) {
355                 goto error;
356 +       }
357  
358 -       if ((ptr.s == NULL) || (ptr.value == NULL)) {
359 +       /* TODO: IMPROVE CHECK
360 +        * unable to create named section with original check
361 +        * therefore temporarily added: && (nargs != 4)
362 +        */
363 +       if (((ptr.s == NULL) && (nargs != 4)) || (ptr.value == NULL)) {
364                 err = UCI_ERR_INVAL;
365                 goto error;
366         }
367  
368         err = uci_set(ctx, &ptr);
369 -       if (err)
370 +       if (err) {
371                 goto error;
372 +       }
373  
374         if (istable) {
375                 for (i = 2; i <= lua_objlen(L, nargs); i++) {
376 @@ -458,7 +520,7 @@
377  
378  error:
379         if (err)
380 -               uci_lua_perror(L, "uci.set");
381 +               uci_lua_perror(L, ctx, "uci.set");
382         lua_pushboolean(L, (err == 0));
383         return 1;
384  }
385 @@ -472,6 +534,7 @@
386  static int
387  uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
388  {
389 +       struct uci_context *ctx = *uci_lua_context(L, 1);
390         struct uci_element *e, *tmp;
391         struct uci_ptr ptr;
392         char *s = NULL;
393 @@ -479,10 +542,10 @@
394         int nargs;
395  
396         nargs = lua_gettop(L);
397 -       if ((cmd != CMD_REVERT) && (nargs > 1))
398 +       if ((cmd != CMD_REVERT) && (nargs > 2))
399                 goto err;
400  
401 -       if (lookup_args(L, &ptr, &s))
402 +       if (lookup_args(L, ctx, &ptr, &s))
403                 goto err;
404  
405         uci_lookup_ptr(ctx, &ptr, NULL, false);
406 @@ -562,16 +625,16 @@
407  }
408  
409  static void
410 -uci_lua_changes_pkg(lua_State *L, const char *package)
411 +uci_lua_changes_pkg(lua_State *L, struct uci_context *ctx, const char *package)
412  {
413         struct uci_package *p = NULL;
414         struct uci_element *e;
415         bool autoload = false;
416  
417 -       p = find_package(L, package, AUTOLOAD_OFF);
418 +       p = find_package(L, ctx, package, AUTOLOAD_OFF);
419         if (!p) {
420                 autoload = true;
421 -               p = find_package(L, package, AUTOLOAD_FORCE);
422 +               p = find_package(L, ctx, package, AUTOLOAD_FORCE);
423                 if (!p)
424                         return;
425         }
426 @@ -596,6 +659,7 @@
427  static int
428  uci_lua_changes(lua_State *L)
429  {
430 +       struct uci_context *ctx = *uci_lua_context(L, 1);
431         const char *package = NULL;
432         char **config = NULL;
433         int nargs;
434 @@ -603,9 +667,9 @@
435  
436         nargs = lua_gettop(L);
437         switch(nargs) {
438 +       case 2:
439 +               package = luaL_checkstring(L, 2);
440         case 1:
441 -               package = luaL_checkstring(L, 1);
442 -       case 0:
443                 break;
444         default:
445                 luaL_error(L, "invalid argument count");
446 @@ -613,13 +677,13 @@
447  
448         lua_newtable(L);
449         if (package) {
450 -               uci_lua_changes_pkg(L, package);
451 +               uci_lua_changes_pkg(L, ctx, package);
452         } else {
453                 if (uci_list_configs(ctx, &config) != 0)
454                         goto done;
455  
456                 for(i = 0; config[i] != NULL; i++) {
457 -                       uci_lua_changes_pkg(L, config[i]);
458 +                       uci_lua_changes_pkg(L, ctx, config[i]);
459                 }
460         }
461  
462 @@ -628,29 +692,53 @@
463  }
464  
465  static int
466 +uci_lua_get_confdir(lua_State *L)
467 +{
468 +       struct uci_context *ctx = *uci_lua_context(L, 1);
469 +       lua_pushstring(L, ctx->confdir);
470 +       return 1;
471 +}
472 +
473 +static int
474  uci_lua_set_confdir(lua_State *L)
475  {
476 +       struct uci_context *ctx = *uci_lua_context(L, 1);
477         int ret;
478  
479 -       luaL_checkstring(L, 1);
480 +       luaL_checkstring(L, 2);
481         ret = uci_set_confdir(ctx, lua_tostring(L, -1));
482         lua_pushboolean(L, (ret == 0));
483         return 1;
484  }
485  
486  static int
487 +uci_lua_get_savedir(lua_State *L)
488 +{
489 +       struct uci_context *ctx = *uci_lua_context(L, 1);
490 +       lua_pushstring(L, ctx->savedir);
491 +       return 1;
492 +}
493 +
494 +static int
495  uci_lua_set_savedir(lua_State *L)
496  {
497 +       struct uci_context *ctx = *uci_lua_context(L, 1);
498         int ret;
499  
500 -       luaL_checkstring(L, 1);
501 +       luaL_checkstring(L, 2);
502         ret = uci_set_savedir(ctx, lua_tostring(L, -1));
503         lua_pushboolean(L, (ret == 0));
504  
505         return 1;
506  }
507  
508 -static const luaL_Reg uci[] = {
509 +static const luaL_Reg uci_module[] = {
510 +       { "cursor", uci_lua_cursor },
511 +       { NULL, NULL },
512 +};
513 +
514 +static const luaL_Reg uci_cursor[] = {
515 +       { "__gc", uci_lua_gc },
516         { "load", uci_lua_load },
517         { "unload", uci_lua_unload },
518         { "get", uci_lua_get },
519 @@ -663,25 +751,33 @@
520         { "revert", uci_lua_revert },
521         { "changes", uci_lua_changes },
522         { "foreach", uci_lua_foreach },
523 +       { "get_confdir", uci_lua_get_confdir },
524 +       { "get_savedir", uci_lua_get_savedir },
525         { "set_confdir", uci_lua_set_confdir },
526         { "set_savedir", uci_lua_set_savedir },
527         { NULL, NULL },
528  };
529  
530 -
531  int
532 -luaopen_uci(lua_State *L)
533 +luaopen_uci2(lua_State *L)
534  {
535 -       ctx = uci_alloc_context();
536 -       if (!ctx)
537 -               luaL_error(L, "Cannot allocate UCI context\n");
538 -       luaL_register(L, MODNAME, uci);
539 -
540 -       /* enable autoload by default */
541 -       lua_getfield(L, LUA_GLOBALSINDEX, "uci");
542 -       lua_pushboolean(L, 1);
543 -       lua_setfield(L, -2, "autoload");
544 -       lua_pop(L, 1);
545 +       /* Create metatable */
546 +       luaL_newmetatable(L, CURSOR_META);
547  
548 -       return 0;
549 +       /* metatable.__index = metatable */
550 +    lua_pushstring(L, "__index");
551 +    lua_pushvalue(L, -2);
552 +    lua_settable(L, -3);
553 +
554 +    /* fill and drop metatable */
555 +    luaL_register(L, NULL, uci_cursor);
556 +    lua_pop(L, 1);
557 +
558 +    /* register module table */
559 +       luaL_register(L, MODNAME, uci_module);
560 +       lua_pushliteral(L, "APIVERSION");
561 +       lua_pushinteger(L, 2);
562 +       lua_settable(L, -3);
563 +
564 +       return 1;
565  }