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
10 +#define MODNAME "uci2"
11 +#define CURSOR_META "uci2.cursor.meta"
16 #define DPRINTF(...) do {} while (0)
19 -static struct uci_context *ctx = NULL;
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)
30 struct uci_package *p = NULL;
31 struct uci_element *e;
33 uci_load(ctx, name, &p);
36 - lua_getfield(L, LUA_GLOBALSINDEX, "uci");
37 - lua_getfield(L, -1, "autoload");
38 - if (!lua_isboolean(L, -1))
41 - if (!lua_toboolean(L, -1))
44 uci_load(ctx, name, &p);
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)
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))
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)
73 - luaL_checkstring(L, 1);
74 - s = strdup(lua_tostring(L, 1));
75 + luaL_checkstring(L, 2);
76 + s = strdup(lua_tostring(L, 2));
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))
89 - ptr->option = luaL_checkstring(L, 3);
90 + ptr->option = luaL_checkstring(L, 4);
93 - ptr->section = luaL_checkstring(L, 2);
94 - ptr->package = luaL_checkstring(L, 1);
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)
103 if (uci_lookup_ptr(ctx, ptr, s, false) != UCI_OK)
106 @@ -202,15 +193,70 @@
110 +static struct uci_context**
111 +uci_lua_context(lua_State *L, int index)
113 + struct uci_context **u = (struct uci_context **)luaL_checkudata(L, index, CURSOR_META);
114 + luaL_argcheck(L, *u, index, "UCI cursor expected");
120 +uci_lua_cursor(lua_State *L)
122 + int argc = lua_gettop(L);
124 + /* create userdata object */
125 + struct uci_context **u = (struct uci_context **)lua_newuserdata(L, sizeof(struct uci_context *));
127 + /* set metatable for userdata */
128 + luaL_getmetatable(L, CURSOR_META);
129 + lua_setmetatable(L, -2);
131 + /* initialize context */
132 + *u = uci_alloc_context();
134 + luaL_error(L, "Cannot allocate UCI context");
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");
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");
153 +uci_lua_gc (lua_State *L) {
154 + struct uci_context **u = (struct uci_context **)luaL_checkudata(L, 1, CURSOR_META);
157 + uci_free_context(*u);
165 uci_lua_unload(lua_State *L)
167 + struct uci_context *ctx = *uci_lua_context(L, 1);
168 struct uci_package *p;
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);
178 lua_pushboolean(L, 1);
181 uci_lua_load(lua_State *L)
183 + struct uci_context *ctx = *uci_lua_context(L, 1);
184 struct uci_package *p = NULL;
188 s = lua_tostring(L, -1);
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);
195 lua_pushboolean(L, 1);
196 @@ -244,22 +291,23 @@
198 uci_lua_foreach(lua_State *L)
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;
206 - package = luaL_checkstring(L, 1);
207 + package = luaL_checkstring(L, 2);
209 - if (lua_isnil(L, 2))
210 + if (lua_isnil(L, 3))
213 - type = luaL_checkstring(L, 2);
214 + type = luaL_checkstring(L, 3);
216 - if (!lua_isfunction(L, 3) || !package)
217 + if (!lua_isfunction(L, 4) || !package)
218 luaL_error(L, "Invalid argument");
220 - p = find_package(L, package, AUTOLOAD_ON);
221 + p = find_package(L, ctx, package, AUTOLOAD_ON);
226 if (type && (strcmp(s->type, type) != 0))
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)
234 @@ -283,12 +331,13 @@
236 uci_lua_get_any(lua_State *L, bool all)
238 + struct uci_context *ctx = *uci_lua_context(L, 1);
239 struct uci_element *e = NULL;
242 int err = UCI_ERR_NOTFOUND;
244 - if (lookup_args(L, &ptr, &s))
245 + if (lookup_args(L, ctx, &ptr, &s))
248 uci_lookup_ptr(ctx, &ptr, NULL, false);
253 - uci_lua_perror(L, "uci.get");
254 + uci_lua_perror(L, ctx, "uci.get");
256 case UCI_ERR_NOTFOUND:
260 uci_lua_add(lua_State *L)
262 + struct uci_context *ctx = *uci_lua_context(L, 1);
263 struct uci_section *s = NULL;
264 struct uci_package *p;
267 const char *name = NULL;
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);
279 @@ -378,11 +428,11 @@
281 uci_lua_delete(lua_State *L)
283 + struct uci_context *ctx = *uci_lua_context(L, 1);
286 int err = UCI_ERR_NOTFOUND;
288 - if (lookup_args(L, &ptr, &s))
289 + if (lookup_args(L, ctx, &ptr, &s))
292 err = uci_delete(ctx, &ptr);
297 - uci_lua_perror(L, "uci.delete");
298 + uci_lua_perror(L, ctx, "uci.delete");
299 lua_pushboolean(L, (err == 0));
304 uci_lua_set(lua_State *L)
306 + struct uci_context *ctx = *uci_lua_context(L, 1);
307 bool istable = false;
309 int err = UCI_ERR_MEM;
310 @@ -406,14 +457,14 @@
313 nargs = lua_gettop(L);
314 - if (lookup_args(L, &ptr, &s))
315 + if (lookup_args(L, ctx, &ptr, &s))
321 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
325 /* Format: uci.set("p", "s", "o", "v") */
326 if (lua_istable(L, nargs)) {
327 if (lua_objlen(L, nargs) < 1)
329 ptr.value = luaL_checkstring(L, nargs);
334 /* Format: uci.set("p", "s", "v") */
335 ptr.value = ptr.option;
341 - uci_lua_perror(L, "uci.set");
342 + uci_lua_perror(L, ctx, "uci.set");
343 lua_pushboolean(L, (err == 0));
348 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
350 + struct uci_context *ctx = *uci_lua_context(L, 1);
351 struct uci_element *e, *tmp;
354 @@ -483,10 +535,10 @@
357 nargs = lua_gettop(L);
358 - if ((cmd != CMD_REVERT) && (nargs > 1))
359 + if ((cmd != CMD_REVERT) && (nargs > 2))
362 - if (lookup_args(L, &ptr, &s))
363 + if (lookup_args(L, ctx, &ptr, &s))
366 uci_lookup_ptr(ctx, &ptr, NULL, false);
367 @@ -566,16 +618,16 @@
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)
374 struct uci_package *p = NULL;
375 struct uci_element *e;
376 bool autoload = false;
378 - p = find_package(L, package, AUTOLOAD_OFF);
379 + p = find_package(L, ctx, package, AUTOLOAD_OFF);
382 - p = find_package(L, package, AUTOLOAD_FORCE);
383 + p = find_package(L, ctx, package, AUTOLOAD_FORCE);
389 uci_lua_changes(lua_State *L)
391 + struct uci_context *ctx = *uci_lua_context(L, 1);
392 const char *package = NULL;
393 char **config = NULL;
397 nargs = lua_gettop(L);
400 + package = luaL_checkstring(L, 2);
402 - package = luaL_checkstring(L, 1);
406 luaL_error(L, "invalid argument count");
407 @@ -617,13 +670,13 @@
411 - uci_lua_changes_pkg(L, package);
412 + uci_lua_changes_pkg(L, ctx, package);
414 if (uci_list_configs(ctx, &config) != 0)
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]);
423 @@ -632,29 +685,53 @@
427 +uci_lua_get_confdir(lua_State *L)
429 + struct uci_context *ctx = *uci_lua_context(L, 1);
430 + lua_pushstring(L, ctx->confdir);
435 uci_lua_set_confdir(lua_State *L)
437 + struct uci_context *ctx = *uci_lua_context(L, 1);
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));
448 +uci_lua_get_savedir(lua_State *L)
450 + struct uci_context *ctx = *uci_lua_context(L, 1);
451 + lua_pushstring(L, ctx->savedir);
456 uci_lua_set_savedir(lua_State *L)
458 + struct uci_context *ctx = *uci_lua_context(L, 1);
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));
469 -static const luaL_Reg uci[] = {
470 +static const luaL_Reg uci_module[] = {
471 + { "cursor", uci_lua_cursor },
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 },
493 -luaopen_uci(lua_State *L)
494 +luaopen_uci2(lua_State *L)
496 - ctx = uci_alloc_context();
498 - luaL_error(L, "Cannot allocate UCI context\n");
499 - luaL_register(L, MODNAME, uci);
501 - /* enable autoload by default */
502 - lua_getfield(L, LUA_GLOBALSINDEX, "uci");
503 - lua_pushboolean(L, 1);
504 - lua_setfield(L, -2, "autoload");
506 + /* Create metatable */
507 + luaL_newmetatable(L, CURSOR_META);
510 + /* metatable.__index = metatable */
511 + lua_pushstring(L, "__index");
512 + lua_pushvalue(L, -2);
513 + lua_settable(L, -3);
515 + /* fill and drop metatable */
516 + luaL_register(L, NULL, uci_cursor);
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);