2 * libuci plugin for Lua
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <sys/types.h>
31 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
33 #define DPRINTF(...) do {} while (0)
36 static struct uci_context
*ctx
= NULL
;
43 static struct uci_package
*
44 find_package(lua_State
*L
, const char *str
, enum autoload al
)
46 struct uci_package
*p
= NULL
;
47 struct uci_element
*e
;
51 sep
= strchr(str
, '.');
53 name
= malloc(1 + sep
- str
);
55 luaL_error(L
, "out of memory");
56 strncpy(name
, str
, sep
- str
);
61 uci_foreach_element(&ctx
->root
, e
) {
62 if (strcmp(e
->name
, name
) != 0)
65 p
= uci_to_package(e
);
69 if (al
== AUTOLOAD_FORCE
)
70 uci_load(ctx
, name
, &p
);
73 lua_getfield(L
, LUA_GLOBALSINDEX
, "uci");
74 lua_getfield(L
, -1, "autoload");
75 if (!lua_isboolean(L
, -1))
78 if (!lua_toboolean(L
, -1))
81 uci_load(ctx
, name
, &p
);
92 static void uci_lua_perror(lua_State
*L
, char *name
)
94 lua_getfield(L
, LUA_GLOBALSINDEX
, "uci");
95 lua_getfield(L
, -1, "warn");
96 if (!lua_isboolean(L
, -1))
98 if (lua_toboolean(L
, -1) != 1)
100 uci_perror(ctx
, name
);
106 lookup_args(lua_State
*L
, struct uci_ptr
*ptr
, char **buf
)
112 luaL_checkstring(L
, 1);
113 s
= strdup(lua_tostring(L
, 1));
117 memset(ptr
, 0, sizeof(struct uci_ptr
));
118 if (!find_package(L
, s
, AUTOLOAD_ON
))
124 ptr
->option
= luaL_checkstring(L
, 3);
127 ptr
->section
= luaL_checkstring(L
, 2);
128 ptr
->package
= luaL_checkstring(L
, 1);
129 if (uci_lookup_ptr(ctx
, ptr
, NULL
, false) != UCI_OK
)
133 if (uci_lookup_ptr(ctx
, ptr
, s
, false) != UCI_OK
)
137 luaL_error(L
, "invalid argument count");
150 static void uci_push_option(lua_State
*L
, struct uci_option
*o
)
152 struct uci_element
*e
;
156 case UCI_TYPE_STRING
:
157 lua_pushstring(L
, o
->v
.string
);
161 uci_foreach_element(&o
->v
.list
, e
) {
163 lua_pushstring(L
, e
->name
);
164 lua_rawseti(L
, -2, i
);
173 static void uci_push_section(lua_State
*L
, struct uci_section
*s
)
175 struct uci_element
*e
;
178 lua_pushboolean(L
, s
->anonymous
);
179 lua_setfield(L
, -2, ".anonymous");
180 lua_pushstring(L
, s
->type
);
181 lua_setfield(L
, -2, ".type");
182 lua_pushstring(L
, s
->e
.name
);
183 lua_setfield(L
, -2, ".name");
185 uci_foreach_element(&s
->options
, e
) {
186 struct uci_option
*o
= uci_to_option(e
);
187 uci_push_option(L
, o
);
188 lua_setfield(L
, -2, o
->e
.name
);
192 static void uci_push_package(lua_State
*L
, struct uci_package
*p
)
194 struct uci_element
*e
;
198 uci_foreach_element(&p
->sections
, e
) {
200 uci_push_section(L
, uci_to_section(e
));
201 lua_setfield(L
, -2, e
->name
);
206 uci_lua_unload(lua_State
*L
)
208 struct uci_package
*p
;
211 luaL_checkstring(L
, 1);
212 s
= lua_tostring(L
, -1);
213 p
= find_package(L
, s
, AUTOLOAD_OFF
);
216 lua_pushboolean(L
, 1);
218 lua_pushboolean(L
, 0);
224 uci_lua_load(lua_State
*L
)
226 struct uci_package
*p
= NULL
;
230 lua_pop(L
, 1); /* bool ret value of unload */
231 s
= lua_tostring(L
, -1);
233 if (uci_load(ctx
, s
, &p
)) {
234 uci_lua_perror(L
, "uci.load");
235 lua_pushboolean(L
, 0);
237 lua_pushboolean(L
, 1);
245 uci_lua_foreach(lua_State
*L
)
247 struct uci_package
*p
;
248 struct uci_element
*e
;
249 const char *package
, *type
;
252 package
= luaL_checkstring(L
, 1);
257 type
= luaL_checkstring(L
, 2);
259 if (!lua_isfunction(L
, 3) || !package
)
260 luaL_error(L
, "Invalid argument");
262 p
= find_package(L
, package
, AUTOLOAD_ON
);
266 uci_foreach_element(&p
->sections
, e
) {
267 struct uci_section
*s
= uci_to_section(e
);
269 if (type
&& (strcmp(s
->type
, type
) != 0))
272 lua_pushvalue(L
, 3); /* iterator function */
273 uci_push_section(L
, s
);
274 if (lua_pcall(L
, 1, 0, 0) == 0)
279 lua_pushboolean(L
, ret
);
284 uci_lua_get_any(lua_State
*L
, bool all
)
286 struct uci_element
*e
= NULL
;
289 int err
= UCI_ERR_NOTFOUND
;
291 if (lookup_args(L
, &ptr
, &s
))
294 uci_lookup_ptr(ctx
, &ptr
, NULL
, false);
295 if (!all
&& !ptr
.s
) {
299 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
300 err
= UCI_ERR_NOTFOUND
;
307 case UCI_TYPE_PACKAGE
:
308 uci_push_package(L
, ptr
.p
);
310 case UCI_TYPE_SECTION
:
312 uci_push_section(L
, ptr
.s
);
314 lua_pushstring(L
, ptr
.s
->type
);
316 case UCI_TYPE_OPTION
:
317 uci_push_option(L
, ptr
.o
);
330 uci_lua_perror(L
, "uci.get");
332 case UCI_ERR_NOTFOUND
:
341 uci_lua_get(lua_State
*L
)
343 return uci_lua_get_any(L
, false);
347 uci_lua_get_all(lua_State
*L
)
349 return uci_lua_get_any(L
, true);
353 uci_lua_add(lua_State
*L
)
355 struct uci_section
*s
= NULL
;
356 struct uci_package
*p
;
359 const char *name
= NULL
;
362 package
= luaL_checkstring(L
, 1);
363 type
= luaL_checkstring(L
, 2);
364 p
= find_package(L
, package
, AUTOLOAD_ON
);
368 if (uci_add_section(ctx
, p
, type
, &s
) || !s
)
374 lua_pushstring(L
, name
);
379 uci_lua_delete(lua_State
*L
)
383 int err
= UCI_ERR_NOTFOUND
;
385 if (lookup_args(L
, &ptr
, &s
))
388 err
= uci_delete(ctx
, &ptr
);
394 uci_lua_perror(L
, "uci.delete");
395 lua_pushboolean(L
, (err
== 0));
400 uci_lua_set(lua_State
*L
)
402 bool istable
= false;
404 int err
= UCI_ERR_MEM
;
408 nargs
= lua_gettop(L
);
409 if (lookup_args(L
, &ptr
, &s
))
414 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
417 /* Format: uci.set("p", "s", "o", "v") */
418 if (lua_istable(L
, nargs
)) {
419 if (lua_objlen(L
, nargs
) < 1)
420 luaL_error(L
, "Cannot set an uci option to an empty table value");
421 lua_rawgeti(L
, nargs
, 1);
422 ptr
.value
= luaL_checkstring(L
, -1);
426 ptr
.value
= luaL_checkstring(L
, nargs
);
430 /* Format: uci.set("p", "s", "v") */
431 ptr
.value
= ptr
.option
;
439 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, false);
443 if ((ptr
.s
== NULL
) || (ptr
.value
== NULL
)) {
448 err
= uci_set(ctx
, &ptr
);
453 for (i
= 2; i
<= lua_objlen(L
, nargs
); i
++) {
454 lua_rawgeti(L
, nargs
, i
);
455 ptr
.value
= luaL_checkstring(L
, -1);
456 err
= uci_add_list(ctx
, &ptr
);
465 uci_lua_perror(L
, "uci.set");
466 lua_pushboolean(L
, (err
== 0));
477 uci_lua_package_cmd(lua_State
*L
, enum pkg_cmd cmd
)
479 struct uci_element
*e
, *tmp
;
485 nargs
= lua_gettop(L
);
486 if ((cmd
!= CMD_REVERT
) && (nargs
> 1))
489 if (lookup_args(L
, &ptr
, &s
))
492 uci_lookup_ptr(ctx
, &ptr
, NULL
, false);
494 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
495 struct uci_package
*p
= uci_to_package(e
);
496 int ret
= UCI_ERR_INVAL
;
498 if (ptr
.p
&& (ptr
.p
!= p
))
504 ret
= uci_commit(ctx
, &p
, false);
507 ret
= uci_save(ctx
, p
);
510 ret
= uci_revert(ctx
, &ptr
);
519 lua_pushboolean(L
, !failed
);
524 uci_lua_save(lua_State
*L
)
526 return uci_lua_package_cmd(L
, CMD_SAVE
);
530 uci_lua_commit(lua_State
*L
)
532 return uci_lua_package_cmd(L
, CMD_COMMIT
);
536 uci_lua_revert(lua_State
*L
)
538 return uci_lua_package_cmd(L
, CMD_REVERT
);
542 uci_lua_add_change(lua_State
*L
, struct uci_element
*e
)
544 struct uci_history
*h
;
547 h
= uci_to_history(e
);
551 lua_getfield(L
, -1, h
->section
);
552 if (lua_isnil(L
, -1)) {
555 lua_pushvalue(L
, -1); /* copy for setfield */
556 lua_setfield(L
, -3, h
->section
);
559 name
= (h
->e
.name
? h
->e
.name
: ".type");
561 lua_pushstring(L
, h
->value
);
563 lua_pushstring(L
, "");
564 lua_setfield(L
, -2, name
);
569 uci_lua_changes_pkg(lua_State
*L
, const char *package
)
571 struct uci_package
*p
= NULL
;
572 struct uci_element
*e
;
573 bool autoload
= false;
575 p
= find_package(L
, package
, AUTOLOAD_OFF
);
578 p
= find_package(L
, package
, AUTOLOAD_FORCE
);
583 if (uci_list_empty(&p
->history
) && uci_list_empty(&p
->saved_history
))
587 uci_foreach_element(&p
->saved_history
, e
) {
588 uci_lua_add_change(L
, e
);
590 uci_foreach_element(&p
->history
, e
) {
591 uci_lua_add_change(L
, e
);
593 lua_setfield(L
, -2, p
->e
.name
);
601 uci_lua_changes(lua_State
*L
)
603 const char *package
= NULL
;
604 char **config
= NULL
;
608 nargs
= lua_gettop(L
);
611 package
= luaL_checkstring(L
, 1);
615 luaL_error(L
, "invalid argument count");
620 uci_lua_changes_pkg(L
, package
);
622 if (uci_list_configs(ctx
, &config
) != 0)
625 for(i
= 0; config
[i
] != NULL
; i
++) {
626 uci_lua_changes_pkg(L
, config
[i
]);
635 uci_lua_set_confdir(lua_State
*L
)
639 luaL_checkstring(L
, 1);
640 ret
= uci_set_confdir(ctx
, lua_tostring(L
, -1));
641 lua_pushboolean(L
, (ret
== 0));
646 uci_lua_set_savedir(lua_State
*L
)
650 luaL_checkstring(L
, 1);
651 ret
= uci_set_savedir(ctx
, lua_tostring(L
, -1));
652 lua_pushboolean(L
, (ret
== 0));
657 static const luaL_Reg uci
[] = {
658 { "load", uci_lua_load
},
659 { "unload", uci_lua_unload
},
660 { "get", uci_lua_get
},
661 { "get_all", uci_lua_get_all
},
662 { "add", uci_lua_add
},
663 { "set", uci_lua_set
},
664 { "save", uci_lua_save
},
665 { "delete", uci_lua_delete
},
666 { "commit", uci_lua_commit
},
667 { "revert", uci_lua_revert
},
668 { "changes", uci_lua_changes
},
669 { "foreach", uci_lua_foreach
},
670 { "set_confdir", uci_lua_set_confdir
},
671 { "set_savedir", uci_lua_set_savedir
},
677 luaopen_uci(lua_State
*L
)
679 ctx
= uci_alloc_context();
681 luaL_error(L
, "Cannot allocate UCI context\n");
682 luaL_register(L
, MODNAME
, uci
);
684 /* enable autoload by default */
685 lua_getfield(L
, LUA_GLOBALSINDEX
, "uci");
686 lua_pushboolean(L
, 1);
687 lua_setfield(L
, -2, "autoload");