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>
28 #define METANAME MODNAME ".meta"
32 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
34 #define DPRINTF(...) do {} while (0)
37 static struct uci_context
*global_ctx
= NULL
;
39 static struct uci_context
*
40 find_context(lua_State
*L
, int *offset
)
42 struct uci_context
**ctx
;
43 if (!lua_isuserdata(L
, 1)) {
45 global_ctx
= uci_alloc_context();
47 luaL_error(L
, "failed to allocate UCI context");
55 ctx
= luaL_checkudata(L
, 1, METANAME
);
57 luaL_error(L
, "failed to get UCI context");
62 static struct uci_package
*
63 find_package(lua_State
*L
, struct uci_context
*ctx
, const char *str
, bool al
)
65 struct uci_package
*p
= NULL
;
66 struct uci_element
*e
;
70 sep
= strchr(str
, '.');
72 name
= malloc(1 + sep
- str
);
74 luaL_error(L
, "out of memory");
75 strncpy(name
, str
, sep
- str
);
80 uci_foreach_element(&ctx
->root
, e
) {
81 if (strcmp(e
->name
, name
) != 0)
84 p
= uci_to_package(e
);
89 uci_load(ctx
, name
, &p
);
91 uci_load(ctx
, name
, &p
);
101 lookup_args(lua_State
*L
, struct uci_context
*ctx
, int offset
, struct uci_ptr
*ptr
, char **buf
)
107 luaL_checkstring(L
, 1 + offset
);
108 s
= strdup(lua_tostring(L
, 1 + offset
));
112 memset(ptr
, 0, sizeof(struct uci_ptr
));
113 if (!find_package(L
, ctx
, s
, true))
116 switch (n
- offset
) {
119 ptr
->option
= luaL_checkstring(L
, 3 + offset
);
122 ptr
->section
= luaL_checkstring(L
, 2 + offset
);
123 ptr
->package
= luaL_checkstring(L
, 1 + offset
);
124 if (uci_lookup_ptr(ctx
, ptr
, NULL
, true) != UCI_OK
)
128 if (uci_lookup_ptr(ctx
, ptr
, s
, true) != UCI_OK
)
132 luaL_error(L
, "invalid argument count");
146 uci_push_status(lua_State
*L
, struct uci_context
*ctx
, bool hasarg
)
151 lua_pushboolean(L
, (ctx
->err
== UCI_OK
));
153 uci_get_errorstr(ctx
, &str
, MODNAME
);
155 lua_pushstring(L
, str
);
164 uci_push_option(lua_State
*L
, struct uci_option
*o
)
166 struct uci_element
*e
;
170 case UCI_TYPE_STRING
:
171 lua_pushstring(L
, o
->v
.string
);
175 uci_foreach_element(&o
->v
.list
, e
) {
177 lua_pushstring(L
, e
->name
);
178 lua_rawseti(L
, -2, i
);
188 uci_push_section(lua_State
*L
, struct uci_section
*s
, int index
)
190 struct uci_element
*e
;
193 lua_pushboolean(L
, s
->anonymous
);
194 lua_setfield(L
, -2, ".anonymous");
195 lua_pushstring(L
, s
->type
);
196 lua_setfield(L
, -2, ".type");
197 lua_pushstring(L
, s
->e
.name
);
198 lua_setfield(L
, -2, ".name");
200 lua_pushinteger(L
, index
);
201 lua_setfield(L
, -2, ".index");
204 uci_foreach_element(&s
->options
, e
) {
205 struct uci_option
*o
= uci_to_option(e
);
206 uci_push_option(L
, o
);
207 lua_setfield(L
, -2, o
->e
.name
);
212 uci_push_package(lua_State
*L
, struct uci_package
*p
)
214 struct uci_element
*e
;
218 uci_foreach_element(&p
->sections
, e
) {
219 uci_push_section(L
, uci_to_section(e
), i
);
220 lua_setfield(L
, -2, e
->name
);
226 uci_lua_unload(lua_State
*L
)
228 struct uci_context
*ctx
;
229 struct uci_package
*p
;
233 ctx
= find_context(L
, &offset
);
234 luaL_checkstring(L
, 1 + offset
);
235 s
= lua_tostring(L
, 1 + offset
);
236 p
= find_package(L
, ctx
, s
, false);
239 return uci_push_status(L
, ctx
, false);
241 lua_pushboolean(L
, 0);
247 uci_lua_load(lua_State
*L
)
249 struct uci_context
*ctx
;
250 struct uci_package
*p
= NULL
;
254 ctx
= find_context(L
, &offset
);
256 lua_pop(L
, 1); /* bool ret value of unload */
257 s
= lua_tostring(L
, -1);
259 uci_load(ctx
, s
, &p
);
260 return uci_push_status(L
, ctx
, false);
265 uci_lua_foreach(lua_State
*L
)
267 struct uci_context
*ctx
;
268 struct uci_package
*p
;
269 struct uci_element
*e
, *tmp
;
270 const char *package
, *type
;
275 ctx
= find_context(L
, &offset
);
276 package
= luaL_checkstring(L
, 1 + offset
);
281 type
= luaL_checkstring(L
, 2 + offset
);
283 if (!lua_isfunction(L
, 3 + offset
) || !package
)
284 luaL_error(L
, "Invalid argument");
286 p
= find_package(L
, ctx
, package
, true);
290 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
291 struct uci_section
*s
= uci_to_section(e
);
295 if (type
&& (strcmp(s
->type
, type
) != 0))
298 lua_pushvalue(L
, 3 + offset
); /* iterator function */
299 uci_push_section(L
, s
, i
- 1);
300 if (lua_pcall(L
, 1, 1, 0) == 0) {
302 if (lua_isboolean(L
, -1) && !lua_toboolean(L
, -1))
313 lua_pushboolean(L
, ret
);
318 uci_lua_get_any(lua_State
*L
, bool all
)
320 struct uci_context
*ctx
;
321 struct uci_element
*e
= NULL
;
325 int err
= UCI_ERR_NOTFOUND
;
327 ctx
= find_context(L
, &offset
);
329 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
332 uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
333 if (!all
&& !ptr
.s
) {
337 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
338 err
= UCI_ERR_NOTFOUND
;
345 case UCI_TYPE_PACKAGE
:
346 uci_push_package(L
, ptr
.p
);
348 case UCI_TYPE_SECTION
:
350 uci_push_section(L
, ptr
.s
, -1);
352 lua_pushstring(L
, ptr
.s
->type
);
354 case UCI_TYPE_OPTION
:
355 uci_push_option(L
, ptr
.o
);
369 return uci_push_status(L
, ctx
, true);
373 uci_lua_get(lua_State
*L
)
375 return uci_lua_get_any(L
, false);
379 uci_lua_get_all(lua_State
*L
)
381 return uci_lua_get_any(L
, true);
385 uci_lua_add(lua_State
*L
)
387 struct uci_context
*ctx
;
388 struct uci_section
*s
= NULL
;
389 struct uci_package
*p
;
392 const char *name
= NULL
;
395 ctx
= find_context(L
, &offset
);
396 package
= luaL_checkstring(L
, 1 + offset
);
397 type
= luaL_checkstring(L
, 2 + offset
);
398 p
= find_package(L
, ctx
, package
, true);
402 if (uci_add_section(ctx
, p
, type
, &s
) || !s
)
406 lua_pushstring(L
, name
);
411 return uci_push_status(L
, ctx
, true);
415 uci_lua_delete(lua_State
*L
)
417 struct uci_context
*ctx
;
422 ctx
= find_context(L
, &offset
);
424 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
427 uci_delete(ctx
, &ptr
);
432 return uci_push_status(L
, ctx
, false);
436 uci_lua_rename(lua_State
*L
)
438 struct uci_context
*ctx
;
440 int err
= UCI_ERR_MEM
;
442 int nargs
, offset
= 0;
444 ctx
= find_context(L
, &offset
);
445 nargs
= lua_gettop(L
);
446 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
449 switch(nargs
- offset
) {
451 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
454 /* Format: uci.set("p", "s", "o", "v") */
455 ptr
.value
= luaL_checkstring(L
, nargs
);
458 /* Format: uci.set("p", "s", "v") */
459 ptr
.value
= ptr
.option
;
467 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
471 if (((ptr
.s
== NULL
) && (ptr
.option
!= NULL
)) || (ptr
.value
== NULL
)) {
476 err
= uci_rename(ctx
, &ptr
);
481 return uci_push_status(L
, ctx
, false);
485 uci_lua_reorder(lua_State
*L
)
487 struct uci_context
*ctx
;
489 int err
= UCI_ERR_MEM
;
491 int nargs
, offset
= 0;
493 ctx
= find_context(L
, &offset
);
494 nargs
= lua_gettop(L
);
495 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
498 switch(nargs
- offset
) {
500 /* Format: uci.set("p.s=v") or uci.set("p.s=v") */
507 /* Format: uci.set("p", "s", "v") */
508 ptr
.value
= ptr
.option
;
516 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
520 if ((ptr
.s
== NULL
) || (ptr
.value
== NULL
)) {
525 err
= uci_reorder_section(ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
530 return uci_push_status(L
, ctx
, false);
535 uci_lua_set(lua_State
*L
)
537 struct uci_context
*ctx
;
539 bool istable
= false;
540 int err
= UCI_ERR_MEM
;
542 int i
, nargs
, offset
= 0;
544 ctx
= find_context(L
, &offset
);
545 nargs
= lua_gettop(L
);
546 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
549 switch(nargs
- offset
) {
551 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
554 /* Format: uci.set("p", "s", "o", "v") */
555 if (lua_istable(L
, nargs
)) {
556 if (lua_objlen(L
, nargs
) < 1)
557 luaL_error(L
, "Cannot set an uci option to an empty table value");
558 lua_rawgeti(L
, nargs
, 1);
559 ptr
.value
= luaL_checkstring(L
, -1);
563 ptr
.value
= luaL_checkstring(L
, nargs
);
567 /* Format: uci.set("p", "s", "v") */
568 ptr
.value
= ptr
.option
;
576 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
580 if (((ptr
.s
== NULL
) && (ptr
.option
!= NULL
)) || (ptr
.value
== NULL
)) {
586 if (lua_objlen(L
, nargs
) == 1) {
589 err
= uci_delete(ctx
, &ptr
);
592 err
= uci_set(ctx
, &ptr
);
597 for (; i
<= lua_objlen(L
, nargs
); i
++) {
598 lua_rawgeti(L
, nargs
, i
);
599 ptr
.value
= luaL_checkstring(L
, -1);
600 err
= uci_add_list(ctx
, &ptr
);
606 err
= uci_set(ctx
, &ptr
);
613 return uci_push_status(L
, ctx
, false);
623 uci_lua_package_cmd(lua_State
*L
, enum pkg_cmd cmd
)
625 struct uci_context
*ctx
;
626 struct uci_element
*e
, *tmp
;
629 int nargs
, offset
= 0;
631 ctx
= find_context(L
, &offset
);
632 nargs
= lua_gettop(L
);
633 if ((cmd
!= CMD_REVERT
) && (nargs
- offset
> 1))
636 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
639 uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
641 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
642 struct uci_package
*p
= uci_to_package(e
);
643 int ret
= UCI_ERR_INVAL
;
645 if (ptr
.p
&& (ptr
.p
!= p
))
651 ret
= uci_commit(ctx
, &p
, false);
654 ret
= uci_save(ctx
, p
);
657 ret
= uci_revert(ctx
, &ptr
);
663 return uci_push_status(L
, ctx
, false);
667 uci_lua_save(lua_State
*L
)
669 return uci_lua_package_cmd(L
, CMD_SAVE
);
673 uci_lua_commit(lua_State
*L
)
675 return uci_lua_package_cmd(L
, CMD_COMMIT
);
679 uci_lua_revert(lua_State
*L
)
681 return uci_lua_package_cmd(L
, CMD_REVERT
);
685 uci_lua_add_change(lua_State
*L
, struct uci_element
*e
)
694 lua_getfield(L
, -1, h
->section
);
695 if (lua_isnil(L
, -1)) {
698 lua_pushvalue(L
, -1); /* copy for setfield */
699 lua_setfield(L
, -3, h
->section
);
702 name
= (h
->e
.name
? h
->e
.name
: ".type");
704 lua_pushstring(L
, h
->value
);
706 lua_pushstring(L
, "");
707 lua_setfield(L
, -2, name
);
712 uci_lua_changes_pkg(lua_State
*L
, struct uci_context
*ctx
, const char *package
)
714 struct uci_package
*p
= NULL
;
715 struct uci_element
*e
;
716 bool autoload
= false;
718 p
= find_package(L
, ctx
, package
, false);
721 p
= find_package(L
, ctx
, package
, true);
726 if (uci_list_empty(&p
->delta
) && uci_list_empty(&p
->saved_delta
))
730 uci_foreach_element(&p
->saved_delta
, e
) {
731 uci_lua_add_change(L
, e
);
733 uci_foreach_element(&p
->delta
, e
) {
734 uci_lua_add_change(L
, e
);
736 lua_setfield(L
, -2, p
->e
.name
);
744 uci_lua_changes(lua_State
*L
)
746 struct uci_context
*ctx
;
747 const char *package
= NULL
;
748 char **config
= NULL
;
752 ctx
= find_context(L
, &offset
);
753 nargs
= lua_gettop(L
);
754 switch(nargs
- offset
) {
756 package
= luaL_checkstring(L
, 1 + offset
);
760 luaL_error(L
, "invalid argument count");
765 uci_lua_changes_pkg(L
, ctx
, package
);
767 if (uci_list_configs(ctx
, &config
) != 0)
770 for(i
= 0; config
[i
] != NULL
; i
++) {
771 uci_lua_changes_pkg(L
, ctx
, config
[i
]);
780 uci_lua_get_confdir(lua_State
*L
)
782 struct uci_context
*ctx
= find_context(L
, NULL
);
783 lua_pushstring(L
, ctx
->confdir
);
788 uci_lua_set_confdir(lua_State
*L
)
790 struct uci_context
*ctx
;
793 ctx
= find_context(L
, &offset
);
794 luaL_checkstring(L
, 1 + offset
);
795 uci_set_confdir(ctx
, lua_tostring(L
, -1));
796 return uci_push_status(L
, ctx
, false);
800 uci_lua_get_savedir(lua_State
*L
)
802 struct uci_context
*ctx
= find_context(L
, NULL
);
803 lua_pushstring(L
, ctx
->savedir
);
808 uci_lua_add_delta(lua_State
*L
)
810 struct uci_context
*ctx
;
813 ctx
= find_context(L
, &offset
);
814 luaL_checkstring(L
, 1 + offset
);
815 uci_add_delta_path(ctx
, lua_tostring(L
, -1));
816 return uci_push_status(L
, ctx
, false);
820 uci_lua_load_plugins(lua_State
*L
)
822 struct uci_context
*ctx
;
824 const char *str
= NULL
;
826 ctx
= find_context(L
, &offset
);
827 if (lua_isstring(L
, -1))
828 str
= lua_tostring(L
, -1);
829 uci_load_plugins(ctx
, str
);
830 return uci_push_status(L
, ctx
, false);
834 uci_lua_set_savedir(lua_State
*L
)
836 struct uci_context
*ctx
;
839 ctx
= find_context(L
, &offset
);
840 luaL_checkstring(L
, 1 + offset
);
841 uci_set_savedir(ctx
, lua_tostring(L
, -1));
842 return uci_push_status(L
, ctx
, false);
846 uci_lua_gc(lua_State
*L
)
848 struct uci_context
*ctx
= find_context(L
, NULL
);
849 uci_free_context(ctx
);
854 uci_lua_cursor(lua_State
*L
)
856 struct uci_context
**u
;
857 int argc
= lua_gettop(L
);
859 u
= lua_newuserdata(L
, sizeof(struct uci_context
*));
860 luaL_getmetatable(L
, METANAME
);
861 lua_setmetatable(L
, -2);
863 *u
= uci_alloc_context();
865 luaL_error(L
, "Cannot allocate UCI context");
868 if (lua_isstring(L
, 2) &&
869 (uci_set_savedir(*u
, luaL_checkstring(L
, 2)) != UCI_OK
))
870 luaL_error(L
, "Unable to set savedir");
873 if (lua_isstring(L
, 1) &&
874 (uci_set_confdir(*u
, luaL_checkstring(L
, 1)) != UCI_OK
))
875 luaL_error(L
, "Unable to set savedir");
883 static const luaL_Reg uci
[] = {
884 { "__gc", uci_lua_gc
},
885 { "cursor", uci_lua_cursor
},
886 { "load", uci_lua_load
},
887 { "unload", uci_lua_unload
},
888 { "get", uci_lua_get
},
889 { "get_all", uci_lua_get_all
},
890 { "add", uci_lua_add
},
891 { "set", uci_lua_set
},
892 { "rename", uci_lua_rename
},
893 { "save", uci_lua_save
},
894 { "delete", uci_lua_delete
},
895 { "commit", uci_lua_commit
},
896 { "revert", uci_lua_revert
},
897 { "reorder", uci_lua_reorder
},
898 { "changes", uci_lua_changes
},
899 { "foreach", uci_lua_foreach
},
900 { "add_history", uci_lua_add_delta
},
901 { "add_delta", uci_lua_add_delta
},
902 { "load_plugins", uci_lua_load_plugins
},
903 { "get_confdir", uci_lua_get_confdir
},
904 { "set_confdir", uci_lua_set_confdir
},
905 { "get_savedir", uci_lua_get_savedir
},
906 { "set_savedir", uci_lua_set_savedir
},
912 luaopen_uci(lua_State
*L
)
914 /* create metatable */
915 luaL_newmetatable(L
, METANAME
);
917 /* metatable.__index = metatable */
918 lua_pushvalue(L
, -1);
919 lua_setfield(L
, -2, "__index");
922 luaL_register(L
, NULL
, uci
);
926 luaL_register(L
, MODNAME
, uci
);