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
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 + if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
254 + err = UCI_ERR_NOTFOUND;
265 - uci_lua_perror(L, "uci.get");
266 + uci_lua_perror(L, ctx, "uci.get");
268 case UCI_ERR_NOTFOUND:
272 uci_lua_add(lua_State *L)
274 + struct uci_context *ctx = *uci_lua_context(L, 1);
275 struct uci_section *s = NULL;
276 struct uci_package *p;
279 const char *name = NULL;
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);
291 @@ -374,11 +429,11 @@
293 uci_lua_delete(lua_State *L)
295 + struct uci_context *ctx = *uci_lua_context(L, 1);
298 int err = UCI_ERR_NOTFOUND;
300 - if (lookup_args(L, &ptr, &s))
301 + if (lookup_args(L, ctx, &ptr, &s))
304 err = uci_delete(ctx, &ptr);
309 - uci_lua_perror(L, "uci.delete");
310 + uci_lua_perror(L, ctx, "uci.delete");
311 lua_pushboolean(L, (err == 0));
316 uci_lua_set(lua_State *L)
318 + struct uci_context *ctx = *uci_lua_context(L, 1);
319 bool istable = false;
321 int err = UCI_ERR_MEM;
322 @@ -402,14 +458,14 @@
325 nargs = lua_gettop(L);
326 - if (lookup_args(L, &ptr, &s))
327 + if (lookup_args(L, ctx, &ptr, &s))
333 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
337 /* Format: uci.set("p", "s", "o", "v") */
338 if (lua_istable(L, nargs)) {
339 if (lua_objlen(L, nargs) < 1)
341 ptr.value = luaL_checkstring(L, nargs);
346 /* Format: uci.set("p", "s", "v") */
347 ptr.value = ptr.option;
349 @@ -433,17 +489,23 @@
352 err = uci_lookup_ptr(ctx, &ptr, NULL, false);
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)
363 + if (((ptr.s == NULL) && (nargs != 4)) || (ptr.value == NULL)) {
368 err = uci_set(ctx, &ptr);
375 for (i = 2; i <= lua_objlen(L, nargs); i++) {
380 - uci_lua_perror(L, "uci.set");
381 + uci_lua_perror(L, ctx, "uci.set");
382 lua_pushboolean(L, (err == 0));
387 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
389 + struct uci_context *ctx = *uci_lua_context(L, 1);
390 struct uci_element *e, *tmp;
393 @@ -479,10 +542,10 @@
396 nargs = lua_gettop(L);
397 - if ((cmd != CMD_REVERT) && (nargs > 1))
398 + if ((cmd != CMD_REVERT) && (nargs > 2))
401 - if (lookup_args(L, &ptr, &s))
402 + if (lookup_args(L, ctx, &ptr, &s))
405 uci_lookup_ptr(ctx, &ptr, NULL, false);
406 @@ -562,16 +625,16 @@
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)
413 struct uci_package *p = NULL;
414 struct uci_element *e;
415 bool autoload = false;
417 - p = find_package(L, package, AUTOLOAD_OFF);
418 + p = find_package(L, ctx, package, AUTOLOAD_OFF);
421 - p = find_package(L, package, AUTOLOAD_FORCE);
422 + p = find_package(L, ctx, package, AUTOLOAD_FORCE);
428 uci_lua_changes(lua_State *L)
430 + struct uci_context *ctx = *uci_lua_context(L, 1);
431 const char *package = NULL;
432 char **config = NULL;
436 nargs = lua_gettop(L);
439 + package = luaL_checkstring(L, 2);
441 - package = luaL_checkstring(L, 1);
445 luaL_error(L, "invalid argument count");
446 @@ -613,13 +677,13 @@
450 - uci_lua_changes_pkg(L, package);
451 + uci_lua_changes_pkg(L, ctx, package);
453 if (uci_list_configs(ctx, &config) != 0)
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]);
462 @@ -628,29 +692,53 @@
466 +uci_lua_get_confdir(lua_State *L)
468 + struct uci_context *ctx = *uci_lua_context(L, 1);
469 + lua_pushstring(L, ctx->confdir);
474 uci_lua_set_confdir(lua_State *L)
476 + struct uci_context *ctx = *uci_lua_context(L, 1);
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));
487 +uci_lua_get_savedir(lua_State *L)
489 + struct uci_context *ctx = *uci_lua_context(L, 1);
490 + lua_pushstring(L, ctx->savedir);
495 uci_lua_set_savedir(lua_State *L)
497 + struct uci_context *ctx = *uci_lua_context(L, 1);
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));
508 -static const luaL_Reg uci[] = {
509 +static const luaL_Reg uci_module[] = {
510 + { "cursor", uci_lua_cursor },
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 },
532 -luaopen_uci(lua_State *L)
533 +luaopen_uci2(lua_State *L)
535 - ctx = uci_alloc_context();
537 - luaL_error(L, "Cannot allocate UCI context\n");
538 - luaL_register(L, MODNAME, uci);
540 - /* enable autoload by default */
541 - lua_getfield(L, LUA_GLOBALSINDEX, "uci");
542 - lua_pushboolean(L, 1);
543 - lua_setfield(L, -2, "autoload");
545 + /* Create metatable */
546 + luaL_newmetatable(L, CURSOR_META);
549 + /* metatable.__index = metatable */
550 + lua_pushstring(L, "__index");
551 + lua_pushvalue(L, -2);
552 + lua_settable(L, -3);
554 + /* fill and drop metatable */
555 + luaL_register(L, NULL, uci_cursor);
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);