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>
31 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
33 #define DPRINTF(...) do {} while (0)
36 static struct uci_context
*ctx
= NULL
;
43 static struct uci_package
*
44 find_package(lua_State
*L
, const char *name
, enum autoload al
)
46 struct uci_package
*p
= NULL
;
47 struct uci_element
*e
;
49 uci_foreach_element(&ctx
->root
, e
) {
50 if (strcmp(e
->name
, name
) != 0)
53 p
= uci_to_package(e
);
57 if (al
== AUTOLOAD_FORCE
)
58 uci_load(ctx
, name
, &p
);
61 lua_getfield(L
, LUA_GLOBALSINDEX
, "uci");
62 lua_getfield(L
, -1, "autoload");
63 if (!lua_isboolean(L
, -1))
66 if (!lua_toboolean(L
, -1))
69 uci_load(ctx
, name
, &p
);
78 static void uci_lua_perror(lua_State
*L
, char *name
)
80 lua_getfield(L
, LUA_GLOBALSINDEX
, "uci");
81 lua_getfield(L
, -1, "warn");
82 if (!lua_isboolean(L
, -1))
84 if (lua_toboolean(L
, -1) != 1)
86 uci_perror(ctx
, name
);
91 static void uci_push_option(lua_State
*L
, struct uci_option
*o
)
93 struct uci_element
*e
;
98 lua_pushstring(L
, o
->v
.string
);
102 uci_foreach_element(&o
->v
.list
, e
) {
104 lua_pushstring(L
, e
->name
);
105 lua_rawseti(L
, -2, i
);
114 static void uci_push_section(lua_State
*L
, struct uci_section
*s
)
116 struct uci_element
*e
;
119 lua_pushstring(L
, s
->type
);
120 lua_setfield(L
, -2, ".type");
121 lua_pushstring(L
, s
->e
.name
);
122 lua_setfield(L
, -2, ".name");
124 uci_foreach_element(&s
->options
, e
) {
125 struct uci_option
*o
= uci_to_option(e
);
126 uci_push_option(L
, o
);
127 lua_setfield(L
, -2, o
->e
.name
);
131 static void uci_push_package(lua_State
*L
, struct uci_package
*p
)
133 struct uci_element
*e
;
137 uci_foreach_element(&p
->sections
, e
) {
139 uci_push_section(L
, uci_to_section(e
));
140 lua_setfield(L
, -2, e
->name
);
145 uci_lua_unload(lua_State
*L
)
147 struct uci_package
*p
;
150 luaL_checkstring(L
, 1);
151 s
= lua_tostring(L
, -1);
152 p
= find_package(L
, s
, AUTOLOAD_OFF
);
155 lua_pushboolean(L
, 1);
157 lua_pushboolean(L
, 0);
163 uci_lua_load(lua_State
*L
)
165 struct uci_package
*p
= NULL
;
169 lua_pop(L
, 1); /* bool ret value of unload */
170 s
= lua_tostring(L
, -1);
172 if (uci_load(ctx
, s
, &p
)) {
173 uci_lua_perror(L
, "uci.load");
174 lua_pushboolean(L
, 0);
176 lua_pushboolean(L
, 1);
184 uci_lua_foreach(lua_State
*L
)
186 struct uci_package
*p
;
187 struct uci_element
*e
;
188 const char *package
, *type
;
191 package
= luaL_checkstring(L
, 1);
196 type
= luaL_checkstring(L
, 2);
198 if (!lua_isfunction(L
, 3) || !package
)
199 luaL_error(L
, "Invalid argument");
201 p
= find_package(L
, package
, AUTOLOAD_ON
);
205 uci_foreach_element(&p
->sections
, e
) {
206 struct uci_section
*s
= uci_to_section(e
);
208 if (type
&& (strcmp(s
->type
, type
) != 0))
211 lua_pushvalue(L
, 3); /* iterator function */
212 uci_push_section(L
, s
);
213 if (lua_pcall(L
, 1, 0, 0) == 0)
218 lua_pushboolean(L
, ret
);
223 uci_lua_get_any(lua_State
*L
, bool all
)
225 struct uci_element
*e
= NULL
;
226 struct uci_package
*p
= NULL
;
227 struct uci_option
*o
= NULL
;
228 const char *package
= NULL
;
229 const char *section
= NULL
;
230 const char *option
= NULL
;
232 int err
= UCI_ERR_MEM
;
237 luaL_checkstring(L
, 1);
238 s
= strdup(lua_tostring(L
, 1));
243 package
= luaL_checkstring(L
, 1);
244 section
= luaL_checkstring(L
, 2);
246 option
= luaL_checkstring(L
, 3);
248 if ((err
= uci_parse_tuple(ctx
, s
, (char **) &package
, (char **) §ion
, (char **) &option
, NULL
)))
252 if (!all
&& (section
== NULL
)) {
257 p
= find_package(L
, package
, AUTOLOAD_ON
);
259 err
= UCI_ERR_NOTFOUND
;
264 if ((err
= uci_lookup(ctx
, &e
, p
, section
, option
)))
271 case UCI_TYPE_PACKAGE
:
272 uci_push_package(L
, p
);
274 case UCI_TYPE_SECTION
:
276 uci_push_section(L
, uci_to_section(e
));
278 lua_pushstring(L
, uci_to_section(e
)->type
);
280 case UCI_TYPE_OPTION
:
281 o
= uci_to_option(e
);
282 uci_push_option(L
, o
);
295 uci_lua_perror(L
, "uci.get");
297 case UCI_ERR_NOTFOUND
:
306 uci_lua_get(lua_State
*L
)
308 return uci_lua_get_any(L
, false);
312 uci_lua_get_all(lua_State
*L
)
314 return uci_lua_get_any(L
, true);
318 uci_lua_add(lua_State
*L
)
320 struct uci_section
*s
= NULL
;
321 struct uci_package
*p
;
324 const char *name
= NULL
;
327 package
= luaL_checkstring(L
, 1);
328 type
= luaL_checkstring(L
, 2);
329 p
= find_package(L
, package
, AUTOLOAD_ON
);
333 if (uci_add_section(ctx
, p
, type
, &s
) || !s
)
339 lua_pushstring(L
, name
);
344 uci_lua_delete(lua_State
*L
)
346 const char *package
= NULL
;
347 const char *section
= NULL
;
348 const char *option
= NULL
;
349 struct uci_package
*p
;
351 int err
= UCI_ERR_MEM
;
354 nargs
= lua_gettop(L
);
355 s
= luaL_checkstring(L
, 1);
358 /* Format: uci.delete("p.s[.o]") */
363 if ((err
= uci_parse_tuple(ctx
, (char *) s
, (char **) &package
, (char **) §ion
, (char **) &option
, NULL
)))
367 /* Format: uci.delete("p", "s", "o") */
368 option
= luaL_checkstring(L
, 3);
371 /* Format: uci.delete("p", "s") */
372 section
= luaL_checkstring(L
, 2);
380 p
= find_package(L
, package
, AUTOLOAD_ON
);
382 err
= UCI_ERR_NOTFOUND
;
385 err
= uci_delete(ctx
, p
, section
, option
);
389 uci_lua_perror(L
, "uci.set");
390 lua_pushboolean(L
, (err
== 0));
395 uci_lua_set(lua_State
*L
)
397 struct uci_package
*p
;
398 const char *package
= NULL
;
399 const char *section
= NULL
;
400 const char *option
= NULL
;
401 const char *value
= NULL
;
403 int err
= UCI_ERR_MEM
;
405 bool istable
= false;
407 nargs
= lua_gettop(L
);
409 s
= luaL_checkstring(L
, 1);
412 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
417 if ((err
= uci_parse_tuple(ctx
, (char *) s
, (char **) &package
, (char **) §ion
, (char **) &option
, (char **) &value
)))
421 /* Format: uci.set("p", "s", "o", "v") */
422 option
= luaL_checkstring(L
, 3);
425 /* Format: uci.set("p", "s", "v") */
427 section
= luaL_checkstring(L
, 2);
428 if (lua_istable(L
, nargs
)) {
429 if (lua_objlen(L
, nargs
) < 1)
430 luaL_error(L
, "Cannot set an uci option to an empty table value");
431 lua_rawgeti(L
, nargs
, 1);
432 value
= luaL_checkstring(L
, -1);
436 value
= luaL_checkstring(L
, nargs
);
444 if ((section
== NULL
) || (value
== NULL
)) {
449 p
= find_package(L
, package
, AUTOLOAD_ON
);
451 err
= UCI_ERR_NOTFOUND
;
454 err
= uci_set(ctx
, p
, section
, option
, value
, NULL
);
456 for (i
= 2; i
<= lua_objlen(L
, nargs
); i
++) {
457 lua_rawgeti(L
, nargs
, i
);
458 value
= luaL_checkstring(L
, -1);
459 err
= uci_add_list(ctx
, p
, section
, option
, value
, NULL
);
468 uci_lua_perror(L
, "uci.set");
469 lua_pushboolean(L
, (err
== 0));
480 uci_lua_package_cmd(lua_State
*L
, enum pkg_cmd cmd
)
482 struct uci_element
*e
, *tmp
;
483 const char *s
= NULL
;
484 const char *section
= NULL
;
485 const char *option
= NULL
;
489 nargs
= lua_gettop(L
);
492 if (cmd
!= CMD_REVERT
)
494 luaL_checkstring(L
, 1);
495 option
= lua_tostring(L
, -1);
499 if (cmd
!= CMD_REVERT
)
501 luaL_checkstring(L
, 1);
502 section
= lua_tostring(L
, -1);
506 luaL_checkstring(L
, 1);
507 s
= lua_tostring(L
, -1);
514 luaL_error(L
, "Invalid argument count");
518 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
519 struct uci_package
*p
= uci_to_package(e
);
520 int ret
= UCI_ERR_INVAL
;
522 if (s
&& (strcmp(s
, e
->name
) != 0))
527 ret
= uci_commit(ctx
, &p
, false);
530 ret
= uci_save(ctx
, p
);
533 ret
= uci_revert(ctx
, &p
, section
, option
);
541 lua_pushboolean(L
, !failed
);
546 uci_lua_save(lua_State
*L
)
548 return uci_lua_package_cmd(L
, CMD_SAVE
);
552 uci_lua_commit(lua_State
*L
)
554 return uci_lua_package_cmd(L
, CMD_COMMIT
);
558 uci_lua_revert(lua_State
*L
)
560 return uci_lua_package_cmd(L
, CMD_REVERT
);
564 uci_lua_add_change(lua_State
*L
, struct uci_element
*e
)
566 struct uci_history
*h
;
569 h
= uci_to_history(e
);
573 lua_getfield(L
, -1, h
->section
);
574 if (lua_isnil(L
, -1)) {
577 lua_pushvalue(L
, -1); /* copy for setfield */
578 lua_setfield(L
, -3, h
->section
);
581 name
= (h
->e
.name
? h
->e
.name
: ".type");
583 lua_pushstring(L
, h
->value
);
585 lua_pushstring(L
, "");
586 lua_setfield(L
, -2, name
);
591 uci_lua_changes_pkg(lua_State
*L
, const char *package
)
593 struct uci_package
*p
= NULL
;
594 struct uci_element
*e
;
595 bool autoload
= false;
597 p
= find_package(L
, package
, AUTOLOAD_OFF
);
600 p
= find_package(L
, package
, AUTOLOAD_FORCE
);
605 if (uci_list_empty(&p
->history
) && uci_list_empty(&p
->saved_history
))
609 uci_foreach_element(&p
->saved_history
, e
) {
610 uci_lua_add_change(L
, e
);
612 uci_foreach_element(&p
->history
, e
) {
613 uci_lua_add_change(L
, e
);
615 lua_setfield(L
, -2, p
->e
.name
);
623 uci_lua_changes(lua_State
*L
)
625 const char *package
= NULL
;
626 char **config
= NULL
;
630 nargs
= lua_gettop(L
);
633 package
= luaL_checkstring(L
, 1);
637 luaL_error(L
, "invalid argument count");
642 uci_lua_changes_pkg(L
, package
);
644 if (uci_list_configs(ctx
, &config
) != 0)
647 for(i
= 0; config
[i
] != NULL
; i
++) {
648 uci_lua_changes_pkg(L
, config
[i
]);
657 uci_lua_set_confdir(lua_State
*L
)
661 luaL_checkstring(L
, 1);
662 ret
= uci_set_confdir(ctx
, lua_tostring(L
, -1));
663 lua_pushboolean(L
, (ret
== 0));
668 uci_lua_set_savedir(lua_State
*L
)
672 luaL_checkstring(L
, 1);
673 ret
= uci_set_savedir(ctx
, lua_tostring(L
, -1));
674 lua_pushboolean(L
, (ret
== 0));
679 static const luaL_Reg uci
[] = {
680 { "load", uci_lua_load
},
681 { "unload", uci_lua_unload
},
682 { "get", uci_lua_get
},
683 { "get_all", uci_lua_get_all
},
684 { "add", uci_lua_add
},
685 { "set", uci_lua_set
},
686 { "save", uci_lua_save
},
687 { "delete", uci_lua_delete
},
688 { "commit", uci_lua_commit
},
689 { "revert", uci_lua_revert
},
690 { "changes", uci_lua_changes
},
691 { "foreach", uci_lua_foreach
},
692 { "set_confdir", uci_lua_set_confdir
},
693 { "set_savedir", uci_lua_set_savedir
},
699 luaopen_uci(lua_State
*L
)
701 ctx
= uci_alloc_context();
703 luaL_error(L
, "Cannot allocate UCI context\n");
704 luaL_register(L
, MODNAME
, uci
);
706 /* enable autoload by default */
707 lua_getfield(L
, LUA_GLOBALSINDEX
, "uci");
708 lua_pushboolean(L
, 1);
709 lua_setfield(L
, -2, "autoload");