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