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
, false) != UCI_OK
)
128 if (uci_lookup_ptr(ctx
, ptr
, s
, false) != 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
;
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(&p
->sections
, 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, 0, 0) == 0)
305 lua_pushboolean(L
, ret
);
310 uci_lua_get_any(lua_State
*L
, bool all
)
312 struct uci_context
*ctx
;
313 struct uci_element
*e
= NULL
;
317 int err
= UCI_ERR_NOTFOUND
;
319 ctx
= find_context(L
, &offset
);
321 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
324 uci_lookup_ptr(ctx
, &ptr
, NULL
, false);
325 if (!all
&& !ptr
.s
) {
329 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
330 err
= UCI_ERR_NOTFOUND
;
337 case UCI_TYPE_PACKAGE
:
338 uci_push_package(L
, ptr
.p
);
340 case UCI_TYPE_SECTION
:
342 uci_push_section(L
, ptr
.s
, -1);
344 lua_pushstring(L
, ptr
.s
->type
);
346 case UCI_TYPE_OPTION
:
347 uci_push_option(L
, ptr
.o
);
361 return uci_push_status(L
, ctx
, true);
365 uci_lua_get(lua_State
*L
)
367 return uci_lua_get_any(L
, false);
371 uci_lua_get_all(lua_State
*L
)
373 return uci_lua_get_any(L
, true);
377 uci_lua_add(lua_State
*L
)
379 struct uci_context
*ctx
;
380 struct uci_section
*s
= NULL
;
381 struct uci_package
*p
;
384 const char *name
= NULL
;
387 ctx
= find_context(L
, &offset
);
388 package
= luaL_checkstring(L
, 1 + offset
);
389 type
= luaL_checkstring(L
, 2 + offset
);
390 p
= find_package(L
, ctx
, package
, true);
394 if (uci_add_section(ctx
, p
, type
, &s
) || !s
)
398 lua_pushstring(L
, name
);
403 return uci_push_status(L
, ctx
, true);
407 uci_lua_delete(lua_State
*L
)
409 struct uci_context
*ctx
;
413 int err
= UCI_ERR_NOTFOUND
;
415 ctx
= find_context(L
, &offset
);
417 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
420 err
= uci_delete(ctx
, &ptr
);
425 return uci_push_status(L
, ctx
, false);
429 uci_lua_rename(lua_State
*L
)
431 struct uci_context
*ctx
;
433 int err
= UCI_ERR_MEM
;
435 int nargs
, offset
= 0;
437 ctx
= find_context(L
, &offset
);
438 nargs
= lua_gettop(L
);
439 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
442 switch(nargs
- offset
) {
444 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
447 /* Format: uci.set("p", "s", "o", "v") */
448 ptr
.value
= luaL_checkstring(L
, nargs
);
451 /* Format: uci.set("p", "s", "v") */
452 ptr
.value
= ptr
.option
;
460 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, false);
464 if (((ptr
.s
== NULL
) && (ptr
.option
!= NULL
)) || (ptr
.value
== NULL
)) {
469 err
= uci_rename(ctx
, &ptr
);
474 return uci_push_status(L
, ctx
, false);
478 uci_lua_reorder(lua_State
*L
)
480 struct uci_context
*ctx
;
482 int err
= UCI_ERR_MEM
;
484 int nargs
, offset
= 0;
486 ctx
= find_context(L
, &offset
);
487 nargs
= lua_gettop(L
);
488 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
491 switch(nargs
- offset
) {
493 /* Format: uci.set("p.s=v") or uci.set("p.s=v") */
500 /* Format: uci.set("p", "s", "v") */
501 ptr
.value
= ptr
.option
;
509 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, false);
513 if ((ptr
.s
== NULL
) || (ptr
.value
== NULL
)) {
518 err
= uci_reorder_section(ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
523 return uci_push_status(L
, ctx
, false);
528 uci_lua_set(lua_State
*L
)
530 struct uci_context
*ctx
;
532 bool istable
= false;
533 int err
= UCI_ERR_MEM
;
535 int i
, nargs
, offset
= 0;
537 ctx
= find_context(L
, &offset
);
538 nargs
= lua_gettop(L
);
539 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
542 switch(nargs
- offset
) {
544 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
547 /* Format: uci.set("p", "s", "o", "v") */
548 if (lua_istable(L
, nargs
)) {
549 if (lua_objlen(L
, nargs
) < 1)
550 luaL_error(L
, "Cannot set an uci option to an empty table value");
551 lua_rawgeti(L
, nargs
, 1);
552 ptr
.value
= luaL_checkstring(L
, -1);
556 ptr
.value
= luaL_checkstring(L
, nargs
);
560 /* Format: uci.set("p", "s", "v") */
561 ptr
.value
= ptr
.option
;
569 err
= uci_lookup_ptr(ctx
, &ptr
, NULL
, false);
573 if (((ptr
.s
== NULL
) && (ptr
.option
!= NULL
)) || (ptr
.value
== NULL
)) {
579 if (lua_objlen(L
, nargs
) == 1) {
582 err
= uci_delete(ctx
, &ptr
);
585 err
= uci_set(ctx
, &ptr
);
590 for (; i
<= lua_objlen(L
, nargs
); i
++) {
591 lua_rawgeti(L
, nargs
, i
);
592 ptr
.value
= luaL_checkstring(L
, -1);
593 err
= uci_add_list(ctx
, &ptr
);
599 err
= uci_set(ctx
, &ptr
);
606 return uci_push_status(L
, ctx
, false);
616 uci_lua_package_cmd(lua_State
*L
, enum pkg_cmd cmd
)
618 struct uci_context
*ctx
;
619 struct uci_element
*e
, *tmp
;
623 int nargs
, offset
= 0;
625 ctx
= find_context(L
, &offset
);
626 nargs
= lua_gettop(L
);
627 if ((cmd
!= CMD_REVERT
) && (nargs
- offset
> 1))
630 if (lookup_args(L
, ctx
, offset
, &ptr
, &s
))
633 uci_lookup_ptr(ctx
, &ptr
, NULL
, false);
635 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
636 struct uci_package
*p
= uci_to_package(e
);
637 int ret
= UCI_ERR_INVAL
;
639 if (ptr
.p
&& (ptr
.p
!= p
))
645 ret
= uci_commit(ctx
, &p
, false);
648 ret
= uci_save(ctx
, p
);
651 ret
= uci_revert(ctx
, &ptr
);
660 return uci_push_status(L
, ctx
, false);
664 uci_lua_save(lua_State
*L
)
666 return uci_lua_package_cmd(L
, CMD_SAVE
);
670 uci_lua_commit(lua_State
*L
)
672 return uci_lua_package_cmd(L
, CMD_COMMIT
);
676 uci_lua_revert(lua_State
*L
)
678 return uci_lua_package_cmd(L
, CMD_REVERT
);
682 uci_lua_add_change(lua_State
*L
, struct uci_element
*e
)
684 struct uci_history
*h
;
687 h
= uci_to_history(e
);
691 lua_getfield(L
, -1, h
->section
);
692 if (lua_isnil(L
, -1)) {
695 lua_pushvalue(L
, -1); /* copy for setfield */
696 lua_setfield(L
, -3, h
->section
);
699 name
= (h
->e
.name
? h
->e
.name
: ".type");
701 lua_pushstring(L
, h
->value
);
703 lua_pushstring(L
, "");
704 lua_setfield(L
, -2, name
);
709 uci_lua_changes_pkg(lua_State
*L
, struct uci_context
*ctx
, const char *package
)
711 struct uci_package
*p
= NULL
;
712 struct uci_element
*e
;
713 bool autoload
= false;
715 p
= find_package(L
, ctx
, package
, false);
718 p
= find_package(L
, ctx
, package
, true);
723 if (uci_list_empty(&p
->history
) && uci_list_empty(&p
->saved_history
))
727 uci_foreach_element(&p
->saved_history
, e
) {
728 uci_lua_add_change(L
, e
);
730 uci_foreach_element(&p
->history
, e
) {
731 uci_lua_add_change(L
, e
);
733 lua_setfield(L
, -2, p
->e
.name
);
741 uci_lua_changes(lua_State
*L
)
743 struct uci_context
*ctx
;
744 const char *package
= NULL
;
745 char **config
= NULL
;
749 ctx
= find_context(L
, &offset
);
750 nargs
= lua_gettop(L
);
751 switch(nargs
- offset
) {
753 package
= luaL_checkstring(L
, 1 + offset
);
757 luaL_error(L
, "invalid argument count");
762 uci_lua_changes_pkg(L
, ctx
, package
);
764 if (uci_list_configs(ctx
, &config
) != 0)
767 for(i
= 0; config
[i
] != NULL
; i
++) {
768 uci_lua_changes_pkg(L
, ctx
, config
[i
]);
777 uci_lua_get_confdir(lua_State
*L
)
779 struct uci_context
*ctx
= find_context(L
, NULL
);
780 lua_pushstring(L
, ctx
->confdir
);
785 uci_lua_set_confdir(lua_State
*L
)
787 struct uci_context
*ctx
;
790 ctx
= find_context(L
, &offset
);
791 luaL_checkstring(L
, 1 + offset
);
792 ret
= uci_set_confdir(ctx
, lua_tostring(L
, -1));
793 return uci_push_status(L
, ctx
, false);
797 uci_lua_get_savedir(lua_State
*L
)
799 struct uci_context
*ctx
= find_context(L
, NULL
);
800 lua_pushstring(L
, ctx
->savedir
);
805 uci_lua_add_history(lua_State
*L
)
807 struct uci_context
*ctx
;
810 ctx
= find_context(L
, &offset
);
811 luaL_checkstring(L
, 1 + offset
);
812 ret
= uci_add_history_path(ctx
, lua_tostring(L
, -1));
813 return uci_push_status(L
, ctx
, false);
817 uci_lua_load_plugins(lua_State
*L
)
819 struct uci_context
*ctx
;
821 const char *str
= NULL
;
823 ctx
= find_context(L
, &offset
);
824 if (lua_isstring(L
, -1))
825 str
= lua_tostring(L
, -1);
826 ret
= uci_load_plugins(ctx
, str
);
827 return uci_push_status(L
, ctx
, false);
831 uci_lua_set_savedir(lua_State
*L
)
833 struct uci_context
*ctx
;
836 ctx
= find_context(L
, &offset
);
837 luaL_checkstring(L
, 1 + offset
);
838 ret
= uci_set_savedir(ctx
, lua_tostring(L
, -1));
839 return uci_push_status(L
, ctx
, false);
843 uci_lua_gc(lua_State
*L
)
845 struct uci_context
*ctx
= find_context(L
, NULL
);
846 uci_free_context(ctx
);
851 uci_lua_cursor(lua_State
*L
)
853 struct uci_context
**u
;
854 int argc
= lua_gettop(L
);
856 u
= lua_newuserdata(L
, sizeof(struct uci_context
*));
857 luaL_getmetatable(L
, METANAME
);
858 lua_setmetatable(L
, -2);
860 *u
= uci_alloc_context();
862 luaL_error(L
, "Cannot allocate UCI context");
865 if (lua_isstring(L
, 2) &&
866 (uci_set_savedir(*u
, luaL_checkstring(L
, 2)) != UCI_OK
))
867 luaL_error(L
, "Unable to set savedir");
870 if (lua_isstring(L
, 1) &&
871 (uci_set_confdir(*u
, luaL_checkstring(L
, 1)) != UCI_OK
))
872 luaL_error(L
, "Unable to set savedir");
880 static const luaL_Reg uci
[] = {
881 { "__gc", uci_lua_gc
},
882 { "cursor", uci_lua_cursor
},
883 { "load", uci_lua_load
},
884 { "unload", uci_lua_unload
},
885 { "get", uci_lua_get
},
886 { "get_all", uci_lua_get_all
},
887 { "add", uci_lua_add
},
888 { "set", uci_lua_set
},
889 { "rename", uci_lua_rename
},
890 { "save", uci_lua_save
},
891 { "delete", uci_lua_delete
},
892 { "commit", uci_lua_commit
},
893 { "revert", uci_lua_revert
},
894 { "reorder", uci_lua_reorder
},
895 { "changes", uci_lua_changes
},
896 { "foreach", uci_lua_foreach
},
897 { "add_history", uci_lua_add_history
},
898 { "load_plugins", uci_lua_load_plugins
},
899 { "get_confdir", uci_lua_get_confdir
},
900 { "set_confdir", uci_lua_set_confdir
},
901 { "get_savedir", uci_lua_get_savedir
},
902 { "set_savedir", uci_lua_set_savedir
},
908 luaopen_uci(lua_State
*L
)
910 /* create metatable */
911 luaL_newmetatable(L
, METANAME
);
913 /* metatable.__index = metatable */
914 lua_pushvalue(L
, -1);
915 lua_setfield(L
, -2, "__index");
918 luaL_register(L
, NULL
, uci
);
922 luaL_register(L
, MODNAME
, uci
);