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