196a25bcf08a26e77c739c29b8ec6c66caf11d06
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
39 int luaopen_uci(lua_State
*L
);
42 * ** Adapted from Lua 5.2.0
44 static void luaL_setfuncs (lua_State
*L
, const luaL_Reg
*l
, int nup
) {
45 luaL_checkstack(L
, nup
+1, "too many upvalues");
46 for (; l
->name
!= NULL
; l
++) { /* fill the table with given functions */
48 lua_pushstring(L
, l
->name
);
49 for (i
= 0; i
< nup
; i
++) /* copy upvalues to the top */
50 lua_pushvalue(L
, -(nup
+1));
51 lua_pushcclosure(L
, l
->func
, nup
); /* closure with those upvalues */
52 lua_settable(L
, -(nup
+ 3));
54 lua_pop(L
, nup
); /* remove upvalues */
57 #define lua_rawlen(L, i) lua_objlen(L, i)
61 static struct uci_context
*global_ctx
= NULL
;
63 static struct uci_context
*
64 find_context(lua_State
*L
, int *offset
)
66 struct uci_context
**ctx
;
67 if (!lua_isuserdata(L
, 1)) {
69 global_ctx
= uci_alloc_context();
71 luaL_error(L
, "failed to allocate UCI context");
81 ctx
= luaL_checkudata(L
, 1, METANAME
);
83 luaL_error(L
, "failed to get UCI context");
90 static struct uci_package
*
91 find_package(lua_State
*L
, struct uci_context
*ctx
, const char *str
, bool al
)
93 struct uci_package
*p
= NULL
;
94 struct uci_element
*e
;
98 sep
= strchr(str
, '.');
100 name
= malloc(1 + sep
- str
);
102 luaL_error(L
, "out of memory");
105 strncpy(name
, str
, sep
- str
);
110 uci_foreach_element(&ctx
->root
, e
) {
111 if (strcmp(e
->name
, name
) != 0)
114 p
= uci_to_package(e
);
119 uci_load(ctx
, name
, &p
);
128 lookup_extended(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
, bool extended
)
131 struct uci_ptr lookup
;
133 /* use a copy of the passed ptr since failing lookups will
134 * clobber the state */
136 lookup
.flags
|= UCI_LOOKUP_EXTENDED
;
138 rv
= uci_lookup_ptr(ctx
, &lookup
, str
, extended
);
140 /* copy to passed ptr on success */
148 lookup_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
, bool extended
)
150 if (ptr
&& !ptr
->s
&& ptr
->section
&& *ptr
->section
== '@')
151 return lookup_extended(ctx
, ptr
, str
, extended
);
153 return uci_lookup_ptr(ctx
, ptr
, str
, extended
);
157 lookup_args(lua_State
*L
, struct uci_context
*ctx
, int offset
, struct uci_ptr
*ptr
, char **buf
)
163 luaL_checkstring(L
, 1 + offset
);
164 s
= strdup(lua_tostring(L
, 1 + offset
));
168 memset(ptr
, 0, sizeof(struct uci_ptr
));
169 if (!find_package(L
, ctx
, s
, true))
172 switch (n
- offset
) {
175 ptr
->option
= luaL_checkstring(L
, 3 + offset
);
178 ptr
->section
= luaL_checkstring(L
, 2 + offset
);
179 ptr
->package
= luaL_checkstring(L
, 1 + offset
);
180 if (lookup_ptr(ctx
, ptr
, NULL
, true) != UCI_OK
)
184 if (lookup_ptr(ctx
, ptr
, s
, true) != UCI_OK
)
188 luaL_error(L
, "invalid argument count");
202 uci_push_status(lua_State
*L
, struct uci_context
*ctx
, bool hasarg
)
207 lua_pushboolean(L
, (ctx
->err
== UCI_OK
));
209 uci_get_errorstr(ctx
, &str
, MODNAME
);
211 lua_pushstring(L
, str
);
220 uci_push_option(lua_State
*L
, struct uci_option
*o
)
222 struct uci_element
*e
;
226 case UCI_TYPE_STRING
:
227 lua_pushstring(L
, o
->v
.string
);
231 uci_foreach_element(&o
->v
.list
, e
) {
233 lua_pushstring(L
, e
->name
);
234 lua_rawseti(L
, -2, i
);
244 uci_push_section(lua_State
*L
, struct uci_section
*s
, int index
)
246 struct uci_element
*e
;
249 lua_pushboolean(L
, s
->anonymous
);
250 lua_setfield(L
, -2, ".anonymous");
251 lua_pushstring(L
, s
->type
);
252 lua_setfield(L
, -2, ".type");
253 lua_pushstring(L
, s
->e
.name
);
254 lua_setfield(L
, -2, ".name");
256 lua_pushinteger(L
, index
);
257 lua_setfield(L
, -2, ".index");
260 uci_foreach_element(&s
->options
, e
) {
261 struct uci_option
*o
= uci_to_option(e
);
262 uci_push_option(L
, o
);
263 lua_setfield(L
, -2, o
->e
.name
);
268 uci_push_package(lua_State
*L
, struct uci_package
*p
)
270 struct uci_element
*e
;
274 uci_foreach_element(&p
->sections
, e
) {
275 uci_push_section(L
, uci_to_section(e
), i
);
276 lua_setfield(L
, -2, e
->name
);
282 uci_lua_unload(lua_State
*L
)
284 struct uci_context
*ctx
;
285 struct uci_package
*p
;
289 ctx
= find_context(L
, &offset
);
290 luaL_checkstring(L
, 1 + offset
);
291 s
= lua_tostring(L
, 1 + offset
);
292 p
= find_package(L
, ctx
, s
, false);
295 return uci_push_status(L
, ctx
, false);
297 lua_pushboolean(L
, 0);
303 uci_lua_load(lua_State
*L
)
305 struct uci_context
*ctx
;
306 struct uci_package
*p
= NULL
;
310 ctx
= find_context(L
, &offset
);
312 lua_pop(L
, 1); /* bool ret value of unload */
313 s
= lua_tostring(L
, -1);
315 uci_load(ctx
, s
, &p
);
316 return uci_push_status(L
, ctx
, false);
321 uci_lua_foreach(lua_State
*L
)
323 struct uci_context
*ctx
;
324 struct uci_package
*p
;
325 struct uci_element
*e
, *tmp
;
326 const char *package
, *type
;
331 ctx
= find_context(L
, &offset
);
332 package
= luaL_checkstring(L
, 1 + offset
);
334 if (lua_isnil(L
, 2 + offset
))
337 type
= luaL_checkstring(L
, 2 + offset
);
339 if (!lua_isfunction(L
, 3 + offset
) || !package
)
340 return luaL_error(L
, "Invalid argument");
342 p
= find_package(L
, ctx
, package
, true);
346 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
347 struct uci_section
*s
= uci_to_section(e
);
351 if (type
&& (strcmp(s
->type
, type
) != 0))
354 lua_pushvalue(L
, 3 + offset
); /* iterator function */
355 uci_push_section(L
, s
, i
- 1);
356 if (lua_pcall(L
, 1, 1, 0) == 0) {
358 if (lua_isboolean(L
, -1) && !lua_toboolean(L
, -1))
369 lua_pushboolean(L
, ret
);
374 uci_lua_get_any(lua_State
*L
, bool all
)
376 struct uci_context
*ctx
;
377 struct uci_element
*e
= NULL
;
382 int err
= UCI_ERR_NOTFOUND
;
384 ctx
= find_context(L
, &offset
);
386 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
389 lookup_ptr(ctx
, &ptr
, NULL
, true);
390 if (!all
&& !ptr
.s
) {
391 ctx
->err
= UCI_ERR_INVAL
;
394 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
395 ctx
->err
= UCI_ERR_NOTFOUND
;
402 case UCI_TYPE_PACKAGE
:
403 uci_push_package(L
, ptr
.p
);
405 case UCI_TYPE_SECTION
:
407 uci_push_section(L
, ptr
.s
, -1);
410 lua_pushstring(L
, ptr
.s
->type
);
411 lua_pushstring(L
, ptr
.s
->e
.name
);
415 case UCI_TYPE_OPTION
:
416 uci_push_option(L
, ptr
.o
);
419 ctx
->err
= UCI_ERR_INVAL
;
432 return uci_push_status(L
, ctx
, true);
436 uci_lua_get(lua_State
*L
)
438 return uci_lua_get_any(L
, false);
442 uci_lua_get_all(lua_State
*L
)
444 return uci_lua_get_any(L
, true);
448 uci_lua_add(lua_State
*L
)
450 struct uci_context
*ctx
;
451 struct uci_section
*s
= NULL
;
452 struct uci_package
*p
;
455 const char *name
= NULL
;
458 ctx
= find_context(L
, &offset
);
459 package
= luaL_checkstring(L
, 1 + offset
);
460 type
= luaL_checkstring(L
, 2 + offset
);
461 p
= find_package(L
, ctx
, package
, true);
465 if (uci_add_section(ctx
, p
, type
, &s
) || !s
)
469 lua_pushstring(L
, name
);
474 return uci_push_status(L
, ctx
, true);
478 uci_lua_delete(lua_State
*L
)
480 struct uci_context
*ctx
;
485 ctx
= find_context(L
, &offset
);
487 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
490 uci_delete(ctx
, &ptr
);
495 return uci_push_status(L
, ctx
, false);
499 uci_lua_rename(lua_State
*L
)
501 struct uci_context
*ctx
;
503 int err
= UCI_ERR_MEM
;
505 int nargs
, offset
= 0;
507 ctx
= find_context(L
, &offset
);
508 nargs
= lua_gettop(L
);
509 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
512 switch(nargs
- offset
) {
514 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
517 /* Format: uci.set("p", "s", "o", "v") */
518 ptr
.value
= luaL_checkstring(L
, nargs
);
521 /* Format: uci.set("p", "s", "v") */
522 ptr
.value
= ptr
.option
;
526 ctx
->err
= UCI_ERR_INVAL
;
530 err
= lookup_ptr(ctx
, &ptr
, NULL
, true);
534 if (((ptr
.s
== NULL
) && (ptr
.option
!= NULL
)) || (ptr
.value
== NULL
)) {
535 ctx
->err
= UCI_ERR_INVAL
;
539 err
= uci_rename(ctx
, &ptr
);
546 return uci_push_status(L
, ctx
, false);
550 uci_lua_reorder(lua_State
*L
)
552 struct uci_context
*ctx
;
554 int err
= UCI_ERR_MEM
;
556 int nargs
, offset
= 0;
558 ctx
= find_context(L
, &offset
);
559 nargs
= lua_gettop(L
);
560 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
563 switch(nargs
- offset
) {
565 /* Format: uci.set("p.s=v") or uci.set("p.s=v") */
567 ctx
->err
= UCI_ERR_INVAL
;
572 /* Format: uci.set("p", "s", "v") */
573 ptr
.value
= ptr
.option
;
577 ctx
->err
= UCI_ERR_INVAL
;
581 err
= lookup_ptr(ctx
, &ptr
, NULL
, true);
585 if ((ptr
.s
== NULL
) || (ptr
.value
== NULL
)) {
586 ctx
->err
= UCI_ERR_INVAL
;
590 err
= uci_reorder_section(ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
597 return uci_push_status(L
, ctx
, false);
602 uci_lua_set(lua_State
*L
)
604 struct uci_context
*ctx
;
606 bool istable
= false;
607 int err
= UCI_ERR_MEM
;
611 int nargs
, offset
= 0;
613 ctx
= find_context(L
, &offset
);
614 nargs
= lua_gettop(L
);
615 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
618 switch(nargs
- offset
) {
620 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
623 /* Format: uci.set("p", "s", "o", "v") */
624 if (lua_istable(L
, nargs
)) {
625 if (lua_rawlen(L
, nargs
) < 1) {
627 return luaL_error(L
, "Cannot set an uci option to an empty table value");
629 lua_rawgeti(L
, nargs
, 1);
630 ptr
.value
= luaL_checkstring(L
, -1);
634 ptr
.value
= luaL_checkstring(L
, nargs
);
638 /* Format: uci.set("p", "s", "v") */
639 ptr
.value
= ptr
.option
;
643 ctx
->err
= UCI_ERR_INVAL
;
647 err
= lookup_ptr(ctx
, &ptr
, NULL
, true);
651 if (((ptr
.s
== NULL
) && (ptr
.option
!= NULL
)) || (ptr
.value
== NULL
)) {
652 ctx
->err
= UCI_ERR_INVAL
;
657 if (lua_rawlen(L
, nargs
) == 1) {
662 err
= uci_delete(ctx
, &ptr
);
669 err
= uci_set(ctx
, &ptr
);
674 for (; i
<= lua_rawlen(L
, nargs
); i
++) {
675 lua_rawgeti(L
, nargs
, i
);
676 ptr
.value
= luaL_checkstring(L
, -1);
677 err
= uci_add_list(ctx
, &ptr
);
683 err
= uci_set(ctx
, &ptr
);
692 return uci_push_status(L
, ctx
, false);
702 uci_lua_package_cmd(lua_State
*L
, enum pkg_cmd cmd
)
704 struct uci_context
*ctx
;
705 struct uci_element
*e
, *tmp
;
708 int nargs
, offset
= 0;
710 ctx
= find_context(L
, &offset
);
711 nargs
= lua_gettop(L
);
712 if ((cmd
!= CMD_REVERT
) && (nargs
- offset
> 1))
715 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
718 lookup_ptr(ctx
, &ptr
, NULL
, true);
720 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
721 struct uci_package
*p
= uci_to_package(e
);
723 if (ptr
.p
&& (ptr
.p
!= p
))
729 uci_commit(ctx
, &p
, false);
735 uci_revert(ctx
, &ptr
);
743 return uci_push_status(L
, ctx
, false);
747 uci_lua_save(lua_State
*L
)
749 return uci_lua_package_cmd(L
, CMD_SAVE
);
753 uci_lua_commit(lua_State
*L
)
755 return uci_lua_package_cmd(L
, CMD_COMMIT
);
759 uci_lua_revert(lua_State
*L
)
761 return uci_lua_package_cmd(L
, CMD_REVERT
);
765 uci_lua_add_change(lua_State
*L
, struct uci_element
*e
)
775 lua_getfield(L
, -1, h
->section
);
776 if (lua_isnil(L
, -1)) {
779 lua_pushvalue(L
, -1); /* copy for setfield */
780 lua_setfield(L
, -3, h
->section
);
784 value
= h
->value
? h
->value
: "";
787 lua_getfield(L
, -1, name
);
789 /* this delta is a list add operation */
790 if (h
->cmd
== UCI_CMD_LIST_ADD
) {
791 /* there seems to be no table yet */
792 if (!lua_istable(L
, -1)) {
795 /* if there is a value on the stack already, add */
796 if (!lua_isnil(L
, -2)) {
797 lua_pushvalue(L
, -2);
798 lua_rawseti(L
, -2, 1);
799 lua_pushstring(L
, value
);
800 lua_rawseti(L
, -2, 2);
802 /* this is the first table item */
804 lua_pushstring(L
, value
);
805 lua_rawseti(L
, -2, 1);
808 lua_setfield(L
, -3, name
);
810 /* a table is on the top of the stack and this is a subsequent,
811 * list_add, append this value to table */
813 lua_pushstring(L
, value
);
814 lua_rawseti(L
, -2, lua_rawlen(L
, -2) + 1);
817 /* non-list change, simply set/replace field */
819 lua_pushstring(L
, value
);
820 lua_setfield(L
, -3, name
);
825 lua_pushstring(L
, value
);
826 lua_setfield(L
, -2, ".type");
833 uci_lua_changes_pkg(lua_State
*L
, struct uci_context
*ctx
, const char *package
)
835 struct uci_package
*p
= NULL
;
836 struct uci_element
*e
;
837 bool autoload
= false;
839 p
= find_package(L
, ctx
, package
, false);
842 p
= find_package(L
, ctx
, package
, true);
847 if (uci_list_empty(&p
->delta
) && uci_list_empty(&p
->saved_delta
))
851 uci_foreach_element(&p
->saved_delta
, e
) {
852 uci_lua_add_change(L
, e
);
854 uci_foreach_element(&p
->delta
, e
) {
855 uci_lua_add_change(L
, e
);
857 lua_setfield(L
, -2, p
->e
.name
);
865 uci_lua_changes(lua_State
*L
)
867 struct uci_context
*ctx
;
868 const char *package
= NULL
;
869 char **config
= NULL
;
873 ctx
= find_context(L
, &offset
);
874 nargs
= lua_gettop(L
);
875 switch(nargs
- offset
) {
877 package
= luaL_checkstring(L
, 1 + offset
);
881 return luaL_error(L
, "invalid argument count");
886 uci_lua_changes_pkg(L
, ctx
, package
);
890 if ((uci_list_configs(ctx
, &config
) != UCI_OK
) || !config
)
893 for (i
= 0; config
[i
] != NULL
; i
++) {
894 uci_lua_changes_pkg(L
, ctx
, config
[i
]);
902 uci_lua_get_confdir(lua_State
*L
)
904 struct uci_context
*ctx
= find_context(L
, NULL
);
905 lua_pushstring(L
, ctx
->confdir
);
910 uci_lua_set_confdir(lua_State
*L
)
912 struct uci_context
*ctx
;
915 ctx
= find_context(L
, &offset
);
916 luaL_checkstring(L
, 1 + offset
);
917 uci_set_confdir(ctx
, lua_tostring(L
, -1));
918 return uci_push_status(L
, ctx
, false);
922 uci_lua_get_savedir(lua_State
*L
)
924 struct uci_context
*ctx
= find_context(L
, NULL
);
925 lua_pushstring(L
, ctx
->savedir
);
930 uci_lua_add_delta(lua_State
*L
)
932 struct uci_context
*ctx
;
935 ctx
= find_context(L
, &offset
);
936 luaL_checkstring(L
, 1 + offset
);
937 uci_add_delta_path(ctx
, lua_tostring(L
, -1));
938 return uci_push_status(L
, ctx
, false);
942 uci_lua_set_savedir(lua_State
*L
)
944 struct uci_context
*ctx
;
947 ctx
= find_context(L
, &offset
);
948 luaL_checkstring(L
, 1 + offset
);
949 uci_set_savedir(ctx
, lua_tostring(L
, -1));
950 return uci_push_status(L
, ctx
, false);
954 uci_lua_list_configs(lua_State
*L
)
956 struct uci_context
*ctx
;
957 char **configs
= NULL
;
961 ctx
= find_context(L
, NULL
);
962 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
)
963 return uci_push_status(L
, ctx
, false);
965 for (ptr
= configs
; *ptr
; ptr
++) {
966 lua_pushstring(L
, *ptr
);
967 lua_rawseti(L
, -2, i
++);
974 uci_lua_gc(lua_State
*L
)
976 struct uci_context
**ctx
;
978 if (!lua_isuserdata(L
, 1)) {
983 ctx
= luaL_checkudata(L
, 1, METANAME
);
987 uci_free_context(*ctx
);
993 uci_lua_cursor(lua_State
*L
)
995 struct uci_context
**u
;
996 int argc
= lua_gettop(L
);
998 u
= lua_newuserdata(L
, sizeof(struct uci_context
*));
999 luaL_getmetatable(L
, METANAME
);
1000 lua_setmetatable(L
, -2);
1002 *u
= uci_alloc_context();
1004 return luaL_error(L
, "Cannot allocate UCI context");
1007 if (lua_isstring(L
, 2) &&
1008 (uci_set_savedir(*u
, luaL_checkstring(L
, 2)) != UCI_OK
))
1009 return luaL_error(L
, "Unable to set savedir");
1012 if (lua_isstring(L
, 1) &&
1013 (uci_set_confdir(*u
, luaL_checkstring(L
, 1)) != UCI_OK
))
1014 return luaL_error(L
, "Unable to set confdir");
1022 static const luaL_Reg uci
[] = {
1023 { "__gc", uci_lua_gc
},
1024 { "close", uci_lua_gc
},
1025 { "cursor", uci_lua_cursor
},
1026 { "load", uci_lua_load
},
1027 { "unload", uci_lua_unload
},
1028 { "get", uci_lua_get
},
1029 { "get_all", uci_lua_get_all
},
1030 { "add", uci_lua_add
},
1031 { "set", uci_lua_set
},
1032 { "rename", uci_lua_rename
},
1033 { "save", uci_lua_save
},
1034 { "delete", uci_lua_delete
},
1035 { "commit", uci_lua_commit
},
1036 { "revert", uci_lua_revert
},
1037 { "reorder", uci_lua_reorder
},
1038 { "changes", uci_lua_changes
},
1039 { "foreach", uci_lua_foreach
},
1040 { "add_history", uci_lua_add_delta
},
1041 { "add_delta", uci_lua_add_delta
},
1042 { "get_confdir", uci_lua_get_confdir
},
1043 { "set_confdir", uci_lua_set_confdir
},
1044 { "get_savedir", uci_lua_get_savedir
},
1045 { "set_savedir", uci_lua_set_savedir
},
1046 { "list_configs", uci_lua_list_configs
},
1052 luaopen_uci(lua_State
*L
)
1054 /* create metatable */
1055 luaL_newmetatable(L
, METANAME
);
1057 /* metatable.__index = metatable */
1058 lua_pushvalue(L
, -1);
1059 lua_setfield(L
, -2, "__index");
1061 /* fill metatable */
1062 luaL_setfuncs(L
, uci
, 0);
1067 lua_pushvalue(L
, -1);
1068 luaL_setfuncs(L
, uci
, 0);
1069 lua_setglobal(L
, MODNAME
);