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 #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
40 * ** Adapted from Lua 5.2.0
42 static void luaL_setfuncs (lua_State
*L
, const luaL_Reg
*l
, int nup
) {
43 luaL_checkstack(L
, nup
+1, "too many upvalues");
44 for (; l
->name
!= NULL
; l
++) { /* fill the table with given functions */
46 lua_pushstring(L
, l
->name
);
47 for (i
= 0; i
< nup
; i
++) /* copy upvalues to the top */
48 lua_pushvalue(L
, -(nup
+1));
49 lua_pushcclosure(L
, l
->func
, nup
); /* closure with those upvalues */
50 lua_settable(L
, -(nup
+ 3));
52 lua_pop(L
, nup
); /* remove upvalues */
55 #define lua_rawlen(L, i) lua_objlen(L, i)
59 static struct uci_context
*global_ctx
= NULL
;
61 static struct uci_context
*
62 find_context(lua_State
*L
, int *offset
)
64 struct uci_context
**ctx
;
65 if (!lua_isuserdata(L
, 1)) {
67 global_ctx
= uci_alloc_context();
69 luaL_error(L
, "failed to allocate UCI context");
79 ctx
= luaL_checkudata(L
, 1, METANAME
);
81 luaL_error(L
, "failed to get UCI context");
88 static struct uci_package
*
89 find_package(lua_State
*L
, struct uci_context
*ctx
, const char *str
, bool al
)
91 struct uci_package
*p
= NULL
;
92 struct uci_element
*e
;
96 sep
= strchr(str
, '.');
98 name
= malloc(1 + sep
- str
);
100 luaL_error(L
, "out of memory");
103 strncpy(name
, str
, sep
- str
);
108 uci_foreach_element(&ctx
->root
, e
) {
109 if (strcmp(e
->name
, name
) != 0)
112 p
= uci_to_package(e
);
117 uci_load(ctx
, name
, &p
);
126 lookup_args(lua_State
*L
, struct uci_context
*ctx
, int offset
, struct uci_ptr
*ptr
, char **buf
)
132 luaL_checkstring(L
, 1 + offset
);
133 s
= strdup(lua_tostring(L
, 1 + offset
));
137 memset(ptr
, 0, sizeof(struct uci_ptr
));
138 if (!find_package(L
, ctx
, s
, true))
141 switch (n
- offset
) {
144 ptr
->option
= luaL_checkstring(L
, 3 + offset
);
147 ptr
->section
= luaL_checkstring(L
, 2 + offset
);
148 ptr
->package
= luaL_checkstring(L
, 1 + offset
);
149 if (uci_lookup_ptr(ctx
, ptr
, NULL
, true) != UCI_OK
)
153 if (uci_lookup_ptr(ctx
, ptr
, s
, true) != UCI_OK
)
157 luaL_error(L
, "invalid argument count");
171 uci_push_status(lua_State
*L
, struct uci_context
*ctx
, bool hasarg
)
176 lua_pushboolean(L
, (ctx
->err
== UCI_OK
));
178 uci_get_errorstr(ctx
, &str
, MODNAME
);
180 lua_pushstring(L
, str
);
189 uci_push_option(lua_State
*L
, struct uci_option
*o
)
191 struct uci_element
*e
;
195 case UCI_TYPE_STRING
:
196 lua_pushstring(L
, o
->v
.string
);
200 uci_foreach_element(&o
->v
.list
, e
) {
202 lua_pushstring(L
, e
->name
);
203 lua_rawseti(L
, -2, i
);
213 uci_push_section(lua_State
*L
, struct uci_section
*s
, int index
)
215 struct uci_element
*e
;
218 lua_pushboolean(L
, s
->anonymous
);
219 lua_setfield(L
, -2, ".anonymous");
220 lua_pushstring(L
, s
->type
);
221 lua_setfield(L
, -2, ".type");
222 lua_pushstring(L
, s
->e
.name
);
223 lua_setfield(L
, -2, ".name");
225 lua_pushinteger(L
, index
);
226 lua_setfield(L
, -2, ".index");
229 uci_foreach_element(&s
->options
, e
) {
230 struct uci_option
*o
= uci_to_option(e
);
231 uci_push_option(L
, o
);
232 lua_setfield(L
, -2, o
->e
.name
);
237 uci_push_package(lua_State
*L
, struct uci_package
*p
)
239 struct uci_element
*e
;
243 uci_foreach_element(&p
->sections
, e
) {
244 uci_push_section(L
, uci_to_section(e
), i
);
245 lua_setfield(L
, -2, e
->name
);
251 uci_lua_unload(lua_State
*L
)
253 struct uci_context
*ctx
;
254 struct uci_package
*p
;
258 ctx
= find_context(L
, &offset
);
259 luaL_checkstring(L
, 1 + offset
);
260 s
= lua_tostring(L
, 1 + offset
);
261 p
= find_package(L
, ctx
, s
, false);
264 return uci_push_status(L
, ctx
, false);
266 lua_pushboolean(L
, 0);
272 uci_lua_load(lua_State
*L
)
274 struct uci_context
*ctx
;
275 struct uci_package
*p
= NULL
;
279 ctx
= find_context(L
, &offset
);
281 lua_pop(L
, 1); /* bool ret value of unload */
282 s
= lua_tostring(L
, -1);
284 uci_load(ctx
, s
, &p
);
285 return uci_push_status(L
, ctx
, false);
290 uci_lua_foreach(lua_State
*L
)
292 struct uci_context
*ctx
;
293 struct uci_package
*p
;
294 struct uci_element
*e
, *tmp
;
295 const char *package
, *type
;
300 ctx
= find_context(L
, &offset
);
301 package
= luaL_checkstring(L
, 1 + offset
);
303 if (lua_isnil(L
, 2 + offset
))
306 type
= luaL_checkstring(L
, 2 + offset
);
308 if (!lua_isfunction(L
, 3 + offset
) || !package
)
309 return luaL_error(L
, "Invalid argument");
311 p
= find_package(L
, ctx
, package
, true);
315 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
316 struct uci_section
*s
= uci_to_section(e
);
320 if (type
&& (strcmp(s
->type
, type
) != 0))
323 lua_pushvalue(L
, 3 + offset
); /* iterator function */
324 uci_push_section(L
, s
, i
- 1);
325 if (lua_pcall(L
, 1, 1, 0) == 0) {
327 if (lua_isboolean(L
, -1) && !lua_toboolean(L
, -1))
338 lua_pushboolean(L
, ret
);
343 uci_lua_get_any(lua_State
*L
, bool all
)
345 struct uci_context
*ctx
;
346 struct uci_element
*e
= NULL
;
350 int err
= UCI_ERR_NOTFOUND
;
352 ctx
= find_context(L
, &offset
);
354 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
357 uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
358 if (!all
&& !ptr
.s
) {
362 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
363 err
= UCI_ERR_NOTFOUND
;
370 case UCI_TYPE_PACKAGE
:
371 uci_push_package(L
, ptr
.p
);
373 case UCI_TYPE_SECTION
:
375 uci_push_section(L
, ptr
.s
, -1);
377 lua_pushstring(L
, ptr
.s
->type
);
379 case UCI_TYPE_OPTION
:
380 uci_push_option(L
, ptr
.o
);
396 return uci_push_status(L
, ctx
, true);
400 uci_lua_get(lua_State
*L
)
402 return uci_lua_get_any(L
, false);
406 uci_lua_get_all(lua_State
*L
)
408 return uci_lua_get_any(L
, true);
412 uci_lua_add(lua_State
*L
)
414 struct uci_context
*ctx
;
415 struct uci_section
*s
= NULL
;
416 struct uci_package
*p
;
419 const char *name
= NULL
;
422 ctx
= find_context(L
, &offset
);
423 package
= luaL_checkstring(L
, 1 + offset
);
424 type
= luaL_checkstring(L
, 2 + offset
);
425 p
= find_package(L
, ctx
, package
, true);
429 if (uci_add_section(ctx
, p
, type
, &s
) || !s
)
433 lua_pushstring(L
, name
);
438 return uci_push_status(L
, ctx
, true);
442 uci_lua_delete(lua_State
*L
)
444 struct uci_context
*ctx
;
449 ctx
= find_context(L
, &offset
);
451 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
454 uci_delete(ctx
, &ptr
);
459 return uci_push_status(L
, ctx
, false);
463 uci_lua_rename(lua_State
*L
)
465 struct uci_context
*ctx
;
467 int err
= UCI_ERR_MEM
;
469 int nargs
, offset
= 0;
471 ctx
= find_context(L
, &offset
);
472 nargs
= lua_gettop(L
);
473 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
476 switch(nargs
- offset
) {
478 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
481 /* Format: uci.set("p", "s", "o", "v") */
482 ptr
.value
= luaL_checkstring(L
, nargs
);
485 /* Format: uci.set("p", "s", "v") */
486 ptr
.value
= ptr
.option
;
494 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
498 if (((ptr
.s
== NULL
) && (ptr
.option
!= NULL
)) || (ptr
.value
== NULL
)) {
503 err
= uci_rename(ctx
, &ptr
);
510 return uci_push_status(L
, ctx
, false);
514 uci_lua_reorder(lua_State
*L
)
516 struct uci_context
*ctx
;
518 int err
= UCI_ERR_MEM
;
520 int nargs
, offset
= 0;
522 ctx
= find_context(L
, &offset
);
523 nargs
= lua_gettop(L
);
524 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
527 switch(nargs
- offset
) {
529 /* Format: uci.set("p.s=v") or uci.set("p.s=v") */
536 /* Format: uci.set("p", "s", "v") */
537 ptr
.value
= ptr
.option
;
545 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
549 if ((ptr
.s
== NULL
) || (ptr
.value
== NULL
)) {
554 err
= uci_reorder_section(ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
561 return uci_push_status(L
, ctx
, false);
566 uci_lua_set(lua_State
*L
)
568 struct uci_context
*ctx
;
570 bool istable
= false;
571 int err
= UCI_ERR_MEM
;
574 int i
, nargs
, offset
= 0;
576 ctx
= find_context(L
, &offset
);
577 nargs
= lua_gettop(L
);
578 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
581 switch(nargs
- offset
) {
583 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
586 /* Format: uci.set("p", "s", "o", "v") */
587 if (lua_istable(L
, nargs
)) {
588 if (lua_rawlen(L
, nargs
) < 1)
589 return luaL_error(L
, "Cannot set an uci option to an empty table value");
590 lua_rawgeti(L
, nargs
, 1);
591 ptr
.value
= luaL_checkstring(L
, -1);
595 ptr
.value
= luaL_checkstring(L
, nargs
);
599 /* Format: uci.set("p", "s", "v") */
600 ptr
.value
= ptr
.option
;
608 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
612 if (((ptr
.s
== NULL
) && (ptr
.option
!= NULL
)) || (ptr
.value
== NULL
)) {
618 if (lua_rawlen(L
, nargs
) == 1) {
623 err
= uci_delete(ctx
, &ptr
);
630 err
= uci_set(ctx
, &ptr
);
635 for (; i
<= lua_rawlen(L
, nargs
); i
++) {
636 lua_rawgeti(L
, nargs
, i
);
637 ptr
.value
= luaL_checkstring(L
, -1);
638 err
= uci_add_list(ctx
, &ptr
);
644 err
= uci_set(ctx
, &ptr
);
653 return uci_push_status(L
, ctx
, false);
663 uci_lua_package_cmd(lua_State
*L
, enum pkg_cmd cmd
)
665 struct uci_context
*ctx
;
666 struct uci_element
*e
, *tmp
;
669 int nargs
, offset
= 0;
671 ctx
= find_context(L
, &offset
);
672 nargs
= lua_gettop(L
);
673 if ((cmd
!= CMD_REVERT
) && (nargs
- offset
> 1))
676 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
679 uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
681 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
682 struct uci_package
*p
= uci_to_package(e
);
684 if (ptr
.p
&& (ptr
.p
!= p
))
690 uci_commit(ctx
, &p
, false);
696 uci_revert(ctx
, &ptr
);
704 return uci_push_status(L
, ctx
, false);
708 uci_lua_save(lua_State
*L
)
710 return uci_lua_package_cmd(L
, CMD_SAVE
);
714 uci_lua_commit(lua_State
*L
)
716 return uci_lua_package_cmd(L
, CMD_COMMIT
);
720 uci_lua_revert(lua_State
*L
)
722 return uci_lua_package_cmd(L
, CMD_REVERT
);
726 uci_lua_add_change(lua_State
*L
, struct uci_element
*e
)
736 lua_getfield(L
, -1, h
->section
);
737 if (lua_isnil(L
, -1)) {
740 lua_pushvalue(L
, -1); /* copy for setfield */
741 lua_setfield(L
, -3, h
->section
);
745 value
= h
->value
? h
->value
: "";
748 lua_getfield(L
, -1, name
);
750 /* this delta is a list add operation */
751 if (h
->cmd
== UCI_CMD_LIST_ADD
) {
752 /* there seems to be no table yet */
753 if (!lua_istable(L
, -1)) {
756 /* if there is a value on the stack already, add */
757 if (!lua_isnil(L
, -2)) {
758 lua_pushvalue(L
, -2);
759 lua_rawseti(L
, -2, 1);
760 lua_pushstring(L
, value
);
761 lua_rawseti(L
, -2, 2);
763 /* this is the first table item */
765 lua_pushstring(L
, value
);
766 lua_rawseti(L
, -2, 1);
769 lua_setfield(L
, -3, name
);
771 /* a table is on the top of the stack and this is a subsequent,
772 * list_add, append this value to table */
774 lua_pushstring(L
, value
);
775 lua_rawseti(L
, -2, lua_rawlen(L
, -2) + 1);
778 /* non-list change, simply set/replace field */
780 lua_pushstring(L
, value
);
781 lua_setfield(L
, -3, name
);
786 lua_pushstring(L
, value
);
787 lua_setfield(L
, -2, ".type");
794 uci_lua_changes_pkg(lua_State
*L
, struct uci_context
*ctx
, const char *package
)
796 struct uci_package
*p
= NULL
;
797 struct uci_element
*e
;
798 bool autoload
= false;
800 p
= find_package(L
, ctx
, package
, false);
803 p
= find_package(L
, ctx
, package
, true);
808 if (uci_list_empty(&p
->delta
) && uci_list_empty(&p
->saved_delta
))
812 uci_foreach_element(&p
->saved_delta
, e
) {
813 uci_lua_add_change(L
, e
);
815 uci_foreach_element(&p
->delta
, e
) {
816 uci_lua_add_change(L
, e
);
818 lua_setfield(L
, -2, p
->e
.name
);
826 uci_lua_changes(lua_State
*L
)
828 struct uci_context
*ctx
;
829 const char *package
= NULL
;
830 char **config
= NULL
;
834 ctx
= find_context(L
, &offset
);
835 nargs
= lua_gettop(L
);
836 switch(nargs
- offset
) {
838 package
= luaL_checkstring(L
, 1 + offset
);
842 return luaL_error(L
, "invalid argument count");
847 uci_lua_changes_pkg(L
, ctx
, package
);
849 if (uci_list_configs(ctx
, &config
) != 0)
852 for(i
= 0; config
[i
] != NULL
; i
++) {
853 uci_lua_changes_pkg(L
, ctx
, config
[i
]);
862 uci_lua_get_confdir(lua_State
*L
)
864 struct uci_context
*ctx
= find_context(L
, NULL
);
865 lua_pushstring(L
, ctx
->confdir
);
870 uci_lua_set_confdir(lua_State
*L
)
872 struct uci_context
*ctx
;
875 ctx
= find_context(L
, &offset
);
876 luaL_checkstring(L
, 1 + offset
);
877 uci_set_confdir(ctx
, lua_tostring(L
, -1));
878 return uci_push_status(L
, ctx
, false);
882 uci_lua_get_savedir(lua_State
*L
)
884 struct uci_context
*ctx
= find_context(L
, NULL
);
885 lua_pushstring(L
, ctx
->savedir
);
890 uci_lua_add_delta(lua_State
*L
)
892 struct uci_context
*ctx
;
895 ctx
= find_context(L
, &offset
);
896 luaL_checkstring(L
, 1 + offset
);
897 uci_add_delta_path(ctx
, lua_tostring(L
, -1));
898 return uci_push_status(L
, ctx
, false);
902 uci_lua_set_savedir(lua_State
*L
)
904 struct uci_context
*ctx
;
907 ctx
= find_context(L
, &offset
);
908 luaL_checkstring(L
, 1 + offset
);
909 uci_set_savedir(ctx
, lua_tostring(L
, -1));
910 return uci_push_status(L
, ctx
, false);
914 uci_lua_gc(lua_State
*L
)
916 struct uci_context
*ctx
= find_context(L
, NULL
);
917 uci_free_context(ctx
);
922 uci_lua_cursor(lua_State
*L
)
924 struct uci_context
**u
;
925 int argc
= lua_gettop(L
);
927 u
= lua_newuserdata(L
, sizeof(struct uci_context
*));
928 luaL_getmetatable(L
, METANAME
);
929 lua_setmetatable(L
, -2);
931 *u
= uci_alloc_context();
933 return luaL_error(L
, "Cannot allocate UCI context");
936 if (lua_isstring(L
, 2) &&
937 (uci_set_savedir(*u
, luaL_checkstring(L
, 2)) != UCI_OK
))
938 return luaL_error(L
, "Unable to set savedir");
941 if (lua_isstring(L
, 1) &&
942 (uci_set_confdir(*u
, luaL_checkstring(L
, 1)) != UCI_OK
))
943 return luaL_error(L
, "Unable to set savedir");
951 static const luaL_Reg uci
[] = {
952 { "__gc", uci_lua_gc
},
953 { "cursor", uci_lua_cursor
},
954 { "load", uci_lua_load
},
955 { "unload", uci_lua_unload
},
956 { "get", uci_lua_get
},
957 { "get_all", uci_lua_get_all
},
958 { "add", uci_lua_add
},
959 { "set", uci_lua_set
},
960 { "rename", uci_lua_rename
},
961 { "save", uci_lua_save
},
962 { "delete", uci_lua_delete
},
963 { "commit", uci_lua_commit
},
964 { "revert", uci_lua_revert
},
965 { "reorder", uci_lua_reorder
},
966 { "changes", uci_lua_changes
},
967 { "foreach", uci_lua_foreach
},
968 { "add_history", uci_lua_add_delta
},
969 { "add_delta", uci_lua_add_delta
},
970 { "get_confdir", uci_lua_get_confdir
},
971 { "set_confdir", uci_lua_set_confdir
},
972 { "get_savedir", uci_lua_get_savedir
},
973 { "set_savedir", uci_lua_set_savedir
},
979 luaopen_uci(lua_State
*L
)
981 /* create metatable */
982 luaL_newmetatable(L
, METANAME
);
984 /* metatable.__index = metatable */
985 lua_pushvalue(L
, -1);
986 lua_setfield(L
, -2, "__index");
989 luaL_setfuncs(L
, uci
, 0);
994 lua_pushvalue(L
, -1);
995 luaL_setfuncs(L
, uci
, 0);
996 lua_setglobal(L
, MODNAME
);