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
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 + if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
252 + err = UCI_ERR_NOTFOUND;
263 - uci_lua_perror(L, "uci.get");
264 + uci_lua_perror(L, ctx, "uci.get");
266 case UCI_ERR_NOTFOUND:
270 uci_lua_add(lua_State *L)
272 + struct uci_context *ctx = *uci_lua_context(L, 1);
273 struct uci_section *s = NULL;
274 struct uci_package *p;
277 const char *name = NULL;
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);
289 @@ -374,11 +429,12 @@
291 uci_lua_delete(lua_State *L)
293 + struct uci_context *ctx = *uci_lua_context(L, 1);
296 int err = UCI_ERR_NOTFOUND;
298 - if (lookup_args(L, &ptr, &s))
299 + if (lookup_args(L, ctx, &ptr, &s))
302 err = uci_delete(ctx, &ptr);
307 - uci_lua_perror(L, "uci.delete");
308 + uci_lua_perror(L, ctx, "uci.delete");
309 lua_pushboolean(L, (err == 0));
314 uci_lua_set(lua_State *L)
316 + struct uci_context *ctx = *uci_lua_context(L, 1);
317 bool istable = false;
319 int err = UCI_ERR_MEM;
320 @@ -402,14 +459,14 @@
323 nargs = lua_gettop(L);
324 - if (lookup_args(L, &ptr, &s))
325 + if (lookup_args(L, ctx, &ptr, &s))
331 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
335 /* Format: uci.set("p", "s", "o", "v") */
336 if (lua_istable(L, nargs)) {
337 if (lua_objlen(L, nargs) < 1)
339 ptr.value = luaL_checkstring(L, nargs);
344 /* Format: uci.set("p", "s", "v") */
345 ptr.value = ptr.option;
347 @@ -433,17 +490,23 @@
350 err = uci_lookup_ptr(ctx, &ptr, NULL, false);
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)
361 + if (((ptr.s == NULL) && (nargs != 4)) || (ptr.value == NULL)) {
366 err = uci_set(ctx, &ptr);
373 for (i = 2; i <= lua_objlen(L, nargs); i++) {
378 - uci_lua_perror(L, "uci.set");
379 + uci_lua_perror(L, ctx, "uci.set");
380 lua_pushboolean(L, (err == 0));
385 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
387 + struct uci_context *ctx = *uci_lua_context(L, 1);
388 struct uci_element *e, *tmp;
391 @@ -479,10 +543,10 @@
394 nargs = lua_gettop(L);
395 - if ((cmd != CMD_REVERT) && (nargs > 1))
396 + if ((cmd != CMD_REVERT) && (nargs > 2))
399 - if (lookup_args(L, &ptr, &s))
400 + if (lookup_args(L, ctx, &ptr, &s))
403 uci_lookup_ptr(ctx, &ptr, NULL, false);
404 @@ -562,16 +626,16 @@
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)
411 struct uci_package *p = NULL;
412 struct uci_element *e;
413 bool autoload = false;
415 - p = find_package(L, package, AUTOLOAD_OFF);
416 + p = find_package(L, ctx, package, AUTOLOAD_OFF);
419 - p = find_package(L, package, AUTOLOAD_FORCE);
420 + p = find_package(L, ctx, package, AUTOLOAD_FORCE);
426 uci_lua_changes(lua_State *L)
428 + struct uci_context *ctx = *uci_lua_context(L, 1);
429 const char *package = NULL;
430 char **config = NULL;
434 nargs = lua_gettop(L);
437 + package = luaL_checkstring(L, 2);
439 - package = luaL_checkstring(L, 1);
443 luaL_error(L, "invalid argument count");
444 @@ -613,13 +678,13 @@
448 - uci_lua_changes_pkg(L, package);
449 + uci_lua_changes_pkg(L, ctx, package);
451 if (uci_list_configs(ctx, &config) != 0)
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]);
460 @@ -628,29 +693,53 @@
464 +uci_lua_get_confdir(lua_State *L)
466 + struct uci_context *ctx = *uci_lua_context(L, 1);
467 + lua_pushstring(L, ctx->confdir);
472 uci_lua_set_confdir(lua_State *L)
474 + struct uci_context *ctx = *uci_lua_context(L, 1);
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));
485 +uci_lua_get_savedir(lua_State *L)
487 + struct uci_context *ctx = *uci_lua_context(L, 1);
488 + lua_pushstring(L, ctx->savedir);
493 uci_lua_set_savedir(lua_State *L)
495 + struct uci_context *ctx = *uci_lua_context(L, 1);
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));
506 -static const luaL_Reg uci[] = {
507 +static const luaL_Reg uci_module[] = {
508 + { "cursor", uci_lua_cursor },
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 },
530 luaopen_uci(lua_State *L)
532 - ctx = uci_alloc_context();
534 - luaL_error(L, "Cannot allocate UCI context\n");
535 - luaL_register(L, MODNAME, uci);
537 - /* enable autoload by default */
538 - lua_getfield(L, LUA_GLOBALSINDEX, "uci");
539 - lua_pushboolean(L, 1);
540 - lua_setfield(L, -2, "autoload");
542 + /* Create metatable */
543 + luaL_newmetatable(L, CURSOR_META);
546 + /* metatable.__index = metatable */
547 + lua_pushstring(L, "__index");
548 + lua_pushvalue(L, -2);
549 + lua_settable(L, -3);
551 + /* fill and drop metatable */
552 + luaL_register(L, NULL, uci_cursor);
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);