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
9 +#define CURSOR_META "uci.cursor.meta"
14 #define DPRINTF(...) do {} while (0)
17 -static struct uci_context *ctx = NULL;
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)
28 struct uci_package *p = NULL;
29 struct uci_element *e;
31 uci_load(ctx, name, &p);
34 - lua_getfield(L, LUA_GLOBALSINDEX, "uci");
35 - lua_getfield(L, -1, "autoload");
36 - if (!lua_isboolean(L, -1))
39 - if (!lua_toboolean(L, -1))
42 uci_load(ctx, name, &p);
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)
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))
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)
71 - luaL_checkstring(L, 1);
72 - s = strdup(lua_tostring(L, 1));
73 + luaL_checkstring(L, 2);
74 + s = strdup(lua_tostring(L, 2));
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))
87 - ptr->option = luaL_checkstring(L, 3);
88 + ptr->option = luaL_checkstring(L, 4);
91 - ptr->section = luaL_checkstring(L, 2);
92 - ptr->package = luaL_checkstring(L, 1);
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)
101 if (uci_lookup_ptr(ctx, ptr, s, false) != UCI_OK)
104 @@ -202,15 +193,70 @@
108 +static struct uci_context**
109 +uci_lua_context(lua_State *L, int index)
111 + struct uci_context **u = (struct uci_context **)luaL_checkudata(L, index, CURSOR_META);
112 + luaL_argcheck(L, *u, index, "UCI cursor expected");
118 +uci_lua_cursor(lua_State *L)
120 + int argc = lua_gettop(L);
122 + /* create userdata object */
123 + struct uci_context **u = (struct uci_context **)lua_newuserdata(L, sizeof(struct uci_context *));
125 + /* set metatable for userdata */
126 + luaL_getmetatable(L, CURSOR_META);
127 + lua_setmetatable(L, -2);
129 + /* initialize context */
130 + *u = uci_alloc_context();
132 + luaL_error(L, "Cannot allocate UCI context");
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");
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");
151 +uci_lua_gc (lua_State *L) {
152 + struct uci_context **u = (struct uci_context **)luaL_checkudata(L, 1, CURSOR_META);
155 + uci_free_context(*u);
163 uci_lua_unload(lua_State *L)
165 + struct uci_context *ctx = *uci_lua_context(L, 1);
166 struct uci_package *p;
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);
176 lua_pushboolean(L, 1);
179 uci_lua_load(lua_State *L)
181 + struct uci_context *ctx = *uci_lua_context(L, 1);
182 struct uci_package *p = NULL;
186 s = lua_tostring(L, -1);
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);
193 lua_pushboolean(L, 1);
194 @@ -244,22 +291,23 @@
196 uci_lua_foreach(lua_State *L)
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;
204 - package = luaL_checkstring(L, 1);
205 + package = luaL_checkstring(L, 2);
207 - if (lua_isnil(L, 2))
208 + if (lua_isnil(L, 3))
211 - type = luaL_checkstring(L, 2);
212 + type = luaL_checkstring(L, 3);
214 - if (!lua_isfunction(L, 3) || !package)
215 + if (!lua_isfunction(L, 4) || !package)
216 luaL_error(L, "Invalid argument");
218 - p = find_package(L, package, AUTOLOAD_ON);
219 + p = find_package(L, ctx, package, AUTOLOAD_ON);
224 if (type && (strcmp(s->type, type) != 0))
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)
232 @@ -283,12 +331,13 @@
234 uci_lua_get_any(lua_State *L, bool all)
236 + struct uci_context *ctx = *uci_lua_context(L, 1);
237 struct uci_element *e = NULL;
240 int err = UCI_ERR_NOTFOUND;
242 - if (lookup_args(L, &ptr, &s))
243 + if (lookup_args(L, ctx, &ptr, &s))
246 uci_lookup_ptr(ctx, &ptr, NULL, false);
251 - uci_lua_perror(L, "uci.get");
252 + uci_lua_perror(L, ctx, "uci.get");
254 case UCI_ERR_NOTFOUND:
258 uci_lua_add(lua_State *L)
260 + struct uci_context *ctx = *uci_lua_context(L, 1);
261 struct uci_section *s = NULL;
262 struct uci_package *p;
265 const char *name = NULL;
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);
277 @@ -374,11 +424,12 @@
279 uci_lua_delete(lua_State *L)
281 + struct uci_context *ctx = *uci_lua_context(L, 1);
284 int err = UCI_ERR_NOTFOUND;
286 - if (lookup_args(L, &ptr, &s))
287 + if (lookup_args(L, ctx, &ptr, &s))
290 err = uci_delete(ctx, &ptr);
295 - uci_lua_perror(L, "uci.delete");
296 + uci_lua_perror(L, ctx, "uci.delete");
297 lua_pushboolean(L, (err == 0));
302 uci_lua_set(lua_State *L)
304 + struct uci_context *ctx = *uci_lua_context(L, 1);
305 bool istable = false;
307 int err = UCI_ERR_MEM;
308 @@ -402,14 +454,14 @@
311 nargs = lua_gettop(L);
312 - if (lookup_args(L, &ptr, &s))
313 + if (lookup_args(L, ctx, &ptr, &s))
319 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
323 /* Format: uci.set("p", "s", "o", "v") */
324 if (lua_istable(L, nargs)) {
325 if (lua_objlen(L, nargs) < 1)
327 ptr.value = luaL_checkstring(L, nargs);
332 /* Format: uci.set("p", "s", "v") */
333 ptr.value = ptr.option;
335 @@ -433,17 +485,23 @@
338 err = uci_lookup_ptr(ctx, &ptr, NULL, false);
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)
349 + if (((ptr.s == NULL) && (nargs != 4)) || (ptr.value == NULL)) {
354 err = uci_set(ctx, &ptr);
361 for (i = 2; i <= lua_objlen(L, nargs); i++) {
366 - uci_lua_perror(L, "uci.set");
367 + uci_lua_perror(L, ctx, "uci.set");
368 lua_pushboolean(L, (err == 0));
373 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
375 + struct uci_context *ctx = *uci_lua_context(L, 1);
376 struct uci_element *e, *tmp;
379 @@ -479,10 +538,10 @@
382 nargs = lua_gettop(L);
383 - if ((cmd != CMD_REVERT) && (nargs > 1))
384 + if ((cmd != CMD_REVERT) && (nargs > 2))
387 - if (lookup_args(L, &ptr, &s))
388 + if (lookup_args(L, ctx, &ptr, &s))
391 uci_lookup_ptr(ctx, &ptr, NULL, false);
392 @@ -562,16 +621,16 @@
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)
399 struct uci_package *p = NULL;
400 struct uci_element *e;
401 bool autoload = false;
403 - p = find_package(L, package, AUTOLOAD_OFF);
404 + p = find_package(L, ctx, package, AUTOLOAD_OFF);
407 - p = find_package(L, package, AUTOLOAD_FORCE);
408 + p = find_package(L, ctx, package, AUTOLOAD_FORCE);
414 uci_lua_changes(lua_State *L)
416 + struct uci_context *ctx = *uci_lua_context(L, 1);
417 const char *package = NULL;
418 char **config = NULL;
422 nargs = lua_gettop(L);
425 + package = luaL_checkstring(L, 2);
427 - package = luaL_checkstring(L, 1);
431 luaL_error(L, "invalid argument count");
432 @@ -613,13 +673,13 @@
436 - uci_lua_changes_pkg(L, package);
437 + uci_lua_changes_pkg(L, ctx, package);
439 if (uci_list_configs(ctx, &config) != 0)
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]);
448 @@ -628,29 +688,53 @@
452 +uci_lua_get_confdir(lua_State *L)
454 + struct uci_context *ctx = *uci_lua_context(L, 1);
455 + lua_pushstring(L, ctx->confdir);
460 uci_lua_set_confdir(lua_State *L)
462 + struct uci_context *ctx = *uci_lua_context(L, 1);
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));
473 +uci_lua_get_savedir(lua_State *L)
475 + struct uci_context *ctx = *uci_lua_context(L, 1);
476 + lua_pushstring(L, ctx->savedir);
481 uci_lua_set_savedir(lua_State *L)
483 + struct uci_context *ctx = *uci_lua_context(L, 1);
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));
494 -static const luaL_Reg uci[] = {
495 +static const luaL_Reg uci_module[] = {
496 + { "cursor", uci_lua_cursor },
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 },
518 luaopen_uci(lua_State *L)
520 - ctx = uci_alloc_context();
522 - luaL_error(L, "Cannot allocate UCI context\n");
523 - luaL_register(L, MODNAME, uci);
525 - /* enable autoload by default */
526 - lua_getfield(L, LUA_GLOBALSINDEX, "uci");
527 - lua_pushboolean(L, 1);
528 - lua_setfield(L, -2, "autoload");
530 + /* Create metatable */
531 + luaL_newmetatable(L, CURSOR_META);
534 + /* metatable.__index = metatable */
535 + lua_pushstring(L, "__index");
536 + lua_pushvalue(L, -2);
537 + lua_settable(L, -3);
539 + /* fill and drop metatable */
540 + luaL_register(L, NULL, uci_cursor);
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);