0667fc24a7b70ee35036d53ac84f1fccfcf7d802
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");
57 ctx
= luaL_checkudata(L
, 1, METANAME
);
59 luaL_error(L
, "failed to get UCI context");
66 static struct uci_package
*
67 find_package(lua_State
*L
, struct uci_context
*ctx
, const char *str
, bool al
)
69 struct uci_package
*p
= NULL
;
70 struct uci_element
*e
;
74 sep
= strchr(str
, '.');
76 name
= malloc(1 + sep
- str
);
78 luaL_error(L
, "out of memory");
81 strncpy(name
, str
, sep
- str
);
86 uci_foreach_element(&ctx
->root
, e
) {
87 if (strcmp(e
->name
, name
) != 0)
90 p
= uci_to_package(e
);
95 uci_load(ctx
, name
, &p
);
97 uci_load(ctx
, name
, &p
);
107 lookup_args(lua_State
*L
, struct uci_context
*ctx
, int offset
, struct uci_ptr
*ptr
, char **buf
)
113 luaL_checkstring(L
, 1 + offset
);
114 s
= strdup(lua_tostring(L
, 1 + offset
));
118 memset(ptr
, 0, sizeof(struct uci_ptr
));
119 if (!find_package(L
, ctx
, s
, true))
122 switch (n
- offset
) {
125 ptr
->option
= luaL_checkstring(L
, 3 + offset
);
128 ptr
->section
= luaL_checkstring(L
, 2 + offset
);
129 ptr
->package
= luaL_checkstring(L
, 1 + offset
);
130 if (uci_lookup_ptr(ctx
, ptr
, NULL
, true) != UCI_OK
)
134 if (uci_lookup_ptr(ctx
, ptr
, s
, true) != UCI_OK
)
138 luaL_error(L
, "invalid argument count");
152 uci_push_status(lua_State
*L
, struct uci_context
*ctx
, bool hasarg
)
157 lua_pushboolean(L
, (ctx
->err
== UCI_OK
));
159 uci_get_errorstr(ctx
, &str
, MODNAME
);
161 lua_pushstring(L
, str
);
170 uci_push_option(lua_State
*L
, struct uci_option
*o
)
172 struct uci_element
*e
;
176 case UCI_TYPE_STRING
:
177 lua_pushstring(L
, o
->v
.string
);
181 uci_foreach_element(&o
->v
.list
, e
) {
183 lua_pushstring(L
, e
->name
);
184 lua_rawseti(L
, -2, i
);
194 uci_push_section(lua_State
*L
, struct uci_section
*s
, int index
)
196 struct uci_element
*e
;
199 lua_pushboolean(L
, s
->anonymous
);
200 lua_setfield(L
, -2, ".anonymous");
201 lua_pushstring(L
, s
->type
);
202 lua_setfield(L
, -2, ".type");
203 lua_pushstring(L
, s
->e
.name
);
204 lua_setfield(L
, -2, ".name");
206 lua_pushinteger(L
, index
);
207 lua_setfield(L
, -2, ".index");
210 uci_foreach_element(&s
->options
, e
) {
211 struct uci_option
*o
= uci_to_option(e
);
212 uci_push_option(L
, o
);
213 lua_setfield(L
, -2, o
->e
.name
);
218 uci_push_package(lua_State
*L
, struct uci_package
*p
)
220 struct uci_element
*e
;
224 uci_foreach_element(&p
->sections
, e
) {
225 uci_push_section(L
, uci_to_section(e
), i
);
226 lua_setfield(L
, -2, e
->name
);
232 uci_lua_unload(lua_State
*L
)
234 struct uci_context
*ctx
;
235 struct uci_package
*p
;
239 ctx
= find_context(L
, &offset
);
240 luaL_checkstring(L
, 1 + offset
);
241 s
= lua_tostring(L
, 1 + offset
);
242 p
= find_package(L
, ctx
, s
, false);
245 return uci_push_status(L
, ctx
, false);
247 lua_pushboolean(L
, 0);
253 uci_lua_load(lua_State
*L
)
255 struct uci_context
*ctx
;
256 struct uci_package
*p
= NULL
;
260 ctx
= find_context(L
, &offset
);
262 lua_pop(L
, 1); /* bool ret value of unload */
263 s
= lua_tostring(L
, -1);
265 uci_load(ctx
, s
, &p
);
266 return uci_push_status(L
, ctx
, false);
271 uci_lua_foreach(lua_State
*L
)
273 struct uci_context
*ctx
;
274 struct uci_package
*p
;
275 struct uci_element
*e
, *tmp
;
276 const char *package
, *type
;
281 ctx
= find_context(L
, &offset
);
282 package
= luaL_checkstring(L
, 1 + offset
);
287 type
= luaL_checkstring(L
, 2 + offset
);
289 if (!lua_isfunction(L
, 3 + offset
) || !package
)
290 return luaL_error(L
, "Invalid argument");
292 p
= find_package(L
, ctx
, package
, true);
296 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
297 struct uci_section
*s
= uci_to_section(e
);
301 if (type
&& (strcmp(s
->type
, type
) != 0))
304 lua_pushvalue(L
, 3 + offset
); /* iterator function */
305 uci_push_section(L
, s
, i
- 1);
306 if (lua_pcall(L
, 1, 1, 0) == 0) {
308 if (lua_isboolean(L
, -1) && !lua_toboolean(L
, -1))
319 lua_pushboolean(L
, ret
);
324 uci_lua_get_any(lua_State
*L
, bool all
)
326 struct uci_context
*ctx
;
327 struct uci_element
*e
= NULL
;
331 int err
= UCI_ERR_NOTFOUND
;
333 ctx
= find_context(L
, &offset
);
335 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
338 uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
339 if (!all
&& !ptr
.s
) {
343 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
344 err
= UCI_ERR_NOTFOUND
;
351 case UCI_TYPE_PACKAGE
:
352 uci_push_package(L
, ptr
.p
);
354 case UCI_TYPE_SECTION
:
356 uci_push_section(L
, ptr
.s
, -1);
358 lua_pushstring(L
, ptr
.s
->type
);
360 case UCI_TYPE_OPTION
:
361 uci_push_option(L
, ptr
.o
);
375 return uci_push_status(L
, ctx
, true);
379 uci_lua_get(lua_State
*L
)
381 return uci_lua_get_any(L
, false);
385 uci_lua_get_all(lua_State
*L
)
387 return uci_lua_get_any(L
, true);
391 uci_lua_add(lua_State
*L
)
393 struct uci_context
*ctx
;
394 struct uci_section
*s
= NULL
;
395 struct uci_package
*p
;
398 const char *name
= NULL
;
401 ctx
= find_context(L
, &offset
);
402 package
= luaL_checkstring(L
, 1 + offset
);
403 type
= luaL_checkstring(L
, 2 + offset
);
404 p
= find_package(L
, ctx
, package
, true);
408 if (uci_add_section(ctx
, p
, type
, &s
) || !s
)
412 lua_pushstring(L
, name
);
417 return uci_push_status(L
, ctx
, true);
421 uci_lua_delete(lua_State
*L
)
423 struct uci_context
*ctx
;
428 ctx
= find_context(L
, &offset
);
430 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
433 uci_delete(ctx
, &ptr
);
438 return uci_push_status(L
, ctx
, false);
442 uci_lua_rename(lua_State
*L
)
444 struct uci_context
*ctx
;
446 int err
= UCI_ERR_MEM
;
448 int nargs
, offset
= 0;
450 ctx
= find_context(L
, &offset
);
451 nargs
= lua_gettop(L
);
452 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
455 switch(nargs
- offset
) {
457 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
460 /* Format: uci.set("p", "s", "o", "v") */
461 ptr
.value
= luaL_checkstring(L
, nargs
);
464 /* Format: uci.set("p", "s", "v") */
465 ptr
.value
= ptr
.option
;
473 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
477 if (((ptr
.s
== NULL
) && (ptr
.option
!= NULL
)) || (ptr
.value
== NULL
)) {
482 err
= uci_rename(ctx
, &ptr
);
487 return uci_push_status(L
, ctx
, false);
491 uci_lua_reorder(lua_State
*L
)
493 struct uci_context
*ctx
;
495 int err
= UCI_ERR_MEM
;
497 int nargs
, offset
= 0;
499 ctx
= find_context(L
, &offset
);
500 nargs
= lua_gettop(L
);
501 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
504 switch(nargs
- offset
) {
506 /* Format: uci.set("p.s=v") or uci.set("p.s=v") */
513 /* Format: uci.set("p", "s", "v") */
514 ptr
.value
= ptr
.option
;
522 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
526 if ((ptr
.s
== NULL
) || (ptr
.value
== NULL
)) {
531 err
= uci_reorder_section(ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
536 return uci_push_status(L
, ctx
, false);
541 uci_lua_set(lua_State
*L
)
543 struct uci_context
*ctx
;
545 bool istable
= false;
546 int err
= UCI_ERR_MEM
;
548 int i
, nargs
, offset
= 0;
550 ctx
= find_context(L
, &offset
);
551 nargs
= lua_gettop(L
);
552 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
555 switch(nargs
- offset
) {
557 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
560 /* Format: uci.set("p", "s", "o", "v") */
561 if (lua_istable(L
, nargs
)) {
562 if (lua_objlen(L
, nargs
) < 1)
563 return luaL_error(L
, "Cannot set an uci option to an empty table value");
564 lua_rawgeti(L
, nargs
, 1);
565 ptr
.value
= luaL_checkstring(L
, -1);
569 ptr
.value
= luaL_checkstring(L
, nargs
);
573 /* Format: uci.set("p", "s", "v") */
574 ptr
.value
= ptr
.option
;
582 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
586 if (((ptr
.s
== NULL
) && (ptr
.option
!= NULL
)) || (ptr
.value
== NULL
)) {
592 if (lua_objlen(L
, nargs
) == 1) {
595 err
= uci_delete(ctx
, &ptr
);
598 err
= uci_set(ctx
, &ptr
);
603 for (; i
<= lua_objlen(L
, nargs
); i
++) {
604 lua_rawgeti(L
, nargs
, i
);
605 ptr
.value
= luaL_checkstring(L
, -1);
606 err
= uci_add_list(ctx
, &ptr
);
612 err
= uci_set(ctx
, &ptr
);
619 return uci_push_status(L
, ctx
, false);
629 uci_lua_package_cmd(lua_State
*L
, enum pkg_cmd cmd
)
631 struct uci_context
*ctx
;
632 struct uci_element
*e
, *tmp
;
635 int nargs
, offset
= 0;
637 ctx
= find_context(L
, &offset
);
638 nargs
= lua_gettop(L
);
639 if ((cmd
!= CMD_REVERT
) && (nargs
- offset
> 1))
642 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
645 uci_lookup_ptr(ctx
, &ptr
, NULL
, true);
647 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
648 struct uci_package
*p
= uci_to_package(e
);
650 if (ptr
.p
&& (ptr
.p
!= p
))
656 uci_commit(ctx
, &p
, false);
662 uci_revert(ctx
, &ptr
);
668 return uci_push_status(L
, ctx
, false);
672 uci_lua_save(lua_State
*L
)
674 return uci_lua_package_cmd(L
, CMD_SAVE
);
678 uci_lua_commit(lua_State
*L
)
680 return uci_lua_package_cmd(L
, CMD_COMMIT
);
684 uci_lua_revert(lua_State
*L
)
686 return uci_lua_package_cmd(L
, CMD_REVERT
);
690 uci_lua_add_change(lua_State
*L
, struct uci_element
*e
)
700 lua_getfield(L
, -1, h
->section
);
701 if (lua_isnil(L
, -1)) {
704 lua_pushvalue(L
, -1); /* copy for setfield */
705 lua_setfield(L
, -3, h
->section
);
709 value
= h
->value
? h
->value
: "";
712 lua_getfield(L
, -1, name
);
714 /* this delta is a list add operation */
715 if (h
->cmd
== UCI_CMD_LIST_ADD
) {
716 /* there seems to be no table yet */
717 if (!lua_istable(L
, -1)) {
720 /* if there is a value on the stack already, add */
721 if (!lua_isnil(L
, -2)) {
722 lua_pushvalue(L
, -2);
723 lua_rawseti(L
, -2, 1);
724 lua_pushstring(L
, value
);
725 lua_rawseti(L
, -2, 2);
727 /* this is the first table item */
729 lua_pushstring(L
, value
);
730 lua_rawseti(L
, -2, 1);
733 lua_setfield(L
, -3, name
);
735 /* a table is on the top of the stack and this is a subsequent,
736 * list_add, append this value to table */
738 lua_pushstring(L
, value
);
739 lua_rawseti(L
, -2, lua_objlen(L
, -2) + 1);
742 /* non-list change, simply set/replace field */
744 lua_pushstring(L
, value
);
745 lua_setfield(L
, -3, name
);
750 lua_pushstring(L
, value
);
751 lua_setfield(L
, -2, ".type");
758 uci_lua_changes_pkg(lua_State
*L
, struct uci_context
*ctx
, const char *package
)
760 struct uci_package
*p
= NULL
;
761 struct uci_element
*e
;
762 bool autoload
= false;
764 p
= find_package(L
, ctx
, package
, false);
767 p
= find_package(L
, ctx
, package
, true);
772 if (uci_list_empty(&p
->delta
) && uci_list_empty(&p
->saved_delta
))
776 uci_foreach_element(&p
->saved_delta
, e
) {
777 uci_lua_add_change(L
, e
);
779 uci_foreach_element(&p
->delta
, e
) {
780 uci_lua_add_change(L
, e
);
782 lua_setfield(L
, -2, p
->e
.name
);
790 uci_lua_changes(lua_State
*L
)
792 struct uci_context
*ctx
;
793 const char *package
= NULL
;
794 char **config
= NULL
;
798 ctx
= find_context(L
, &offset
);
799 nargs
= lua_gettop(L
);
800 switch(nargs
- offset
) {
802 package
= luaL_checkstring(L
, 1 + offset
);
806 return luaL_error(L
, "invalid argument count");
811 uci_lua_changes_pkg(L
, ctx
, package
);
813 if (uci_list_configs(ctx
, &config
) != 0)
816 for(i
= 0; config
[i
] != NULL
; i
++) {
817 uci_lua_changes_pkg(L
, ctx
, config
[i
]);
826 uci_lua_get_confdir(lua_State
*L
)
828 struct uci_context
*ctx
= find_context(L
, NULL
);
829 lua_pushstring(L
, ctx
->confdir
);
834 uci_lua_set_confdir(lua_State
*L
)
836 struct uci_context
*ctx
;
839 ctx
= find_context(L
, &offset
);
840 luaL_checkstring(L
, 1 + offset
);
841 uci_set_confdir(ctx
, lua_tostring(L
, -1));
842 return uci_push_status(L
, ctx
, false);
846 uci_lua_get_savedir(lua_State
*L
)
848 struct uci_context
*ctx
= find_context(L
, NULL
);
849 lua_pushstring(L
, ctx
->savedir
);
854 uci_lua_add_delta(lua_State
*L
)
856 struct uci_context
*ctx
;
859 ctx
= find_context(L
, &offset
);
860 luaL_checkstring(L
, 1 + offset
);
861 uci_add_delta_path(ctx
, lua_tostring(L
, -1));
862 return uci_push_status(L
, ctx
, false);
866 uci_lua_load_plugins(lua_State
*L
)
868 struct uci_context
*ctx
;
870 const char *str
= NULL
;
872 ctx
= find_context(L
, &offset
);
873 if (lua_isstring(L
, -1))
874 str
= lua_tostring(L
, -1);
875 uci_load_plugins(ctx
, str
);
876 return uci_push_status(L
, ctx
, false);
880 uci_lua_set_savedir(lua_State
*L
)
882 struct uci_context
*ctx
;
885 ctx
= find_context(L
, &offset
);
886 luaL_checkstring(L
, 1 + offset
);
887 uci_set_savedir(ctx
, lua_tostring(L
, -1));
888 return uci_push_status(L
, ctx
, false);
892 uci_lua_gc(lua_State
*L
)
894 struct uci_context
*ctx
= find_context(L
, NULL
);
895 uci_free_context(ctx
);
900 uci_lua_cursor(lua_State
*L
)
902 struct uci_context
**u
;
903 int argc
= lua_gettop(L
);
905 u
= lua_newuserdata(L
, sizeof(struct uci_context
*));
906 luaL_getmetatable(L
, METANAME
);
907 lua_setmetatable(L
, -2);
909 *u
= uci_alloc_context();
911 return luaL_error(L
, "Cannot allocate UCI context");
914 if (lua_isstring(L
, 2) &&
915 (uci_set_savedir(*u
, luaL_checkstring(L
, 2)) != UCI_OK
))
916 return luaL_error(L
, "Unable to set savedir");
919 if (lua_isstring(L
, 1) &&
920 (uci_set_confdir(*u
, luaL_checkstring(L
, 1)) != UCI_OK
))
921 return luaL_error(L
, "Unable to set savedir");
929 static const luaL_Reg uci
[] = {
930 { "__gc", uci_lua_gc
},
931 { "cursor", uci_lua_cursor
},
932 { "load", uci_lua_load
},
933 { "unload", uci_lua_unload
},
934 { "get", uci_lua_get
},
935 { "get_all", uci_lua_get_all
},
936 { "add", uci_lua_add
},
937 { "set", uci_lua_set
},
938 { "rename", uci_lua_rename
},
939 { "save", uci_lua_save
},
940 { "delete", uci_lua_delete
},
941 { "commit", uci_lua_commit
},
942 { "revert", uci_lua_revert
},
943 { "reorder", uci_lua_reorder
},
944 { "changes", uci_lua_changes
},
945 { "foreach", uci_lua_foreach
},
946 { "add_history", uci_lua_add_delta
},
947 { "add_delta", uci_lua_add_delta
},
948 { "load_plugins", uci_lua_load_plugins
},
949 { "get_confdir", uci_lua_get_confdir
},
950 { "set_confdir", uci_lua_set_confdir
},
951 { "get_savedir", uci_lua_get_savedir
},
952 { "set_savedir", uci_lua_set_savedir
},
958 luaopen_uci(lua_State
*L
)
960 /* create metatable */
961 luaL_newmetatable(L
, METANAME
);
963 /* metatable.__index = metatable */
964 lua_pushvalue(L
, -1);
965 lua_setfield(L
, -2, "__index");
968 luaL_register(L
, NULL
, uci
);
972 luaL_register(L
, MODNAME
, uci
);