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
;
406 nargs
= lua_gettop(L
);
408 s
= luaL_checkstring(L
, 1);
411 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
416 if ((err
= uci_parse_tuple(ctx
, (char *) s
, (char **) &package
, (char **) §ion
, (char **) &option
, (char **) &value
)))
420 /* Format: uci.set("p", "s", "o", "v") */
421 option
= luaL_checkstring(L
, 3);
424 /* Format: uci.set("p", "s", "v") */
426 section
= luaL_checkstring(L
, 2);
427 value
= luaL_checkstring(L
, nargs
);
434 if ((section
== NULL
) || (value
== NULL
)) {
439 p
= find_package(L
, package
, AUTOLOAD_ON
);
441 err
= UCI_ERR_NOTFOUND
;
444 err
= uci_set(ctx
, p
, section
, option
, value
, NULL
);
448 uci_lua_perror(L
, "uci.set");
449 lua_pushboolean(L
, (err
== 0));
460 uci_lua_package_cmd(lua_State
*L
, enum pkg_cmd cmd
)
462 struct uci_element
*e
, *tmp
;
463 const char *s
= NULL
;
464 const char *section
= NULL
;
465 const char *option
= NULL
;
469 nargs
= lua_gettop(L
);
472 if (cmd
!= CMD_REVERT
)
474 luaL_checkstring(L
, 1);
475 option
= lua_tostring(L
, -1);
479 if (cmd
!= CMD_REVERT
)
481 luaL_checkstring(L
, 1);
482 section
= lua_tostring(L
, -1);
486 luaL_checkstring(L
, 1);
487 s
= lua_tostring(L
, -1);
494 luaL_error(L
, "Invalid argument count");
498 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
499 struct uci_package
*p
= uci_to_package(e
);
500 int ret
= UCI_ERR_INVAL
;
502 if (s
&& (strcmp(s
, e
->name
) != 0))
507 ret
= uci_commit(ctx
, &p
, false);
510 ret
= uci_save(ctx
, p
);
513 ret
= uci_revert(ctx
, &p
, section
, option
);
521 lua_pushboolean(L
, !failed
);
526 uci_lua_save(lua_State
*L
)
528 return uci_lua_package_cmd(L
, CMD_SAVE
);
532 uci_lua_commit(lua_State
*L
)
534 return uci_lua_package_cmd(L
, CMD_COMMIT
);
538 uci_lua_revert(lua_State
*L
)
540 return uci_lua_package_cmd(L
, CMD_REVERT
);
544 uci_lua_add_change(lua_State
*L
, struct uci_element
*e
)
546 struct uci_history
*h
;
549 h
= uci_to_history(e
);
553 lua_getfield(L
, -1, h
->section
);
554 if (lua_isnil(L
, -1)) {
557 lua_pushvalue(L
, -1); /* copy for setfield */
558 lua_setfield(L
, -3, h
->section
);
561 name
= (h
->e
.name
? h
->e
.name
: ".type");
563 lua_pushstring(L
, h
->value
);
565 lua_pushstring(L
, "");
566 lua_setfield(L
, -2, name
);
571 uci_lua_changes_pkg(lua_State
*L
, const char *package
)
573 struct uci_package
*p
= NULL
;
574 struct uci_element
*e
;
575 bool autoload
= false;
577 p
= find_package(L
, package
, AUTOLOAD_OFF
);
580 p
= find_package(L
, package
, AUTOLOAD_FORCE
);
585 if (uci_list_empty(&p
->history
) && uci_list_empty(&p
->saved_history
))
589 uci_foreach_element(&p
->saved_history
, e
) {
590 uci_lua_add_change(L
, e
);
592 uci_foreach_element(&p
->history
, e
) {
593 uci_lua_add_change(L
, e
);
595 lua_setfield(L
, -2, p
->e
.name
);
603 uci_lua_changes(lua_State
*L
)
605 const char *package
= NULL
;
606 char **config
= NULL
;
610 nargs
= lua_gettop(L
);
613 package
= luaL_checkstring(L
, 1);
617 luaL_error(L
, "invalid argument count");
622 uci_lua_changes_pkg(L
, package
);
624 if (uci_list_configs(ctx
, &config
) != 0)
627 for(i
= 0; config
[i
] != NULL
; i
++) {
628 uci_lua_changes_pkg(L
, config
[i
]);
637 uci_lua_set_confdir(lua_State
*L
)
641 luaL_checkstring(L
, 1);
642 ret
= uci_set_confdir(ctx
, lua_tostring(L
, -1));
643 lua_pushboolean(L
, (ret
== 0));
648 uci_lua_set_savedir(lua_State
*L
)
652 luaL_checkstring(L
, 1);
653 ret
= uci_set_savedir(ctx
, lua_tostring(L
, -1));
654 lua_pushboolean(L
, (ret
== 0));
659 static const luaL_Reg uci
[] = {
660 { "load", uci_lua_load
},
661 { "unload", uci_lua_unload
},
662 { "get", uci_lua_get
},
663 { "get_all", uci_lua_get_all
},
664 { "add", uci_lua_add
},
665 { "set", uci_lua_set
},
666 { "save", uci_lua_save
},
667 { "delete", uci_lua_delete
},
668 { "commit", uci_lua_commit
},
669 { "revert", uci_lua_revert
},
670 { "changes", uci_lua_changes
},
671 { "foreach", uci_lua_foreach
},
672 { "set_confdir", uci_lua_set_confdir
},
673 { "set_savedir", uci_lua_set_savedir
},
679 luaopen_uci(lua_State
*L
)
681 ctx
= uci_alloc_context();
683 luaL_error(L
, "Cannot allocate UCI context\n");
684 luaL_register(L
, MODNAME
, uci
);
686 /* enable autoload by default */
687 lua_getfield(L
, LUA_GLOBALSINDEX
, "uci");
688 lua_pushboolean(L
, 1);
689 lua_setfield(L
, -2, "autoload");