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_section(lua_State
*L
, struct uci_section
*s
)
93 struct uci_element
*e
;
96 lua_pushstring(L
, s
->type
);
97 lua_setfield(L
, -2, ".type");
99 uci_foreach_element(&s
->options
, e
) {
100 struct uci_option
*o
= uci_to_option(e
);
101 lua_pushstring(L
, o
->value
);
102 lua_setfield(L
, -2, o
->e
.name
);
106 static void uci_push_package(lua_State
*L
, struct uci_package
*p
)
108 struct uci_element
*e
;
112 uci_foreach_element(&p
->sections
, e
) {
114 uci_push_section(L
, uci_to_section(e
));
115 lua_setfield(L
, -2, e
->name
);
120 uci_lua_unload(lua_State
*L
)
122 struct uci_package
*p
;
125 luaL_checkstring(L
, 1);
126 s
= lua_tostring(L
, -1);
127 p
= find_package(L
, s
, AUTOLOAD_OFF
);
130 lua_pushboolean(L
, 1);
132 lua_pushboolean(L
, 0);
138 uci_lua_load(lua_State
*L
)
140 struct uci_package
*p
= NULL
;
144 lua_pop(L
, 1); /* bool ret value of unload */
145 s
= lua_tostring(L
, -1);
147 if (uci_load(ctx
, s
, &p
)) {
148 uci_lua_perror(L
, "uci.load");
149 lua_pushboolean(L
, 0);
151 lua_pushboolean(L
, 1);
159 uci_lua_foreach(lua_State
*L
)
161 struct uci_package
*p
;
162 struct uci_element
*e
;
163 const char *package
, *type
;
166 package
= luaL_checkstring(L
, 1);
171 type
= luaL_checkstring(L
, 2);
173 if (!lua_isfunction(L
, 3) || !package
)
174 luaL_error(L
, "Invalid argument");
176 p
= find_package(L
, package
, AUTOLOAD_ON
);
180 uci_foreach_element(&p
->sections
, e
) {
181 struct uci_section
*s
= uci_to_section(e
);
183 if (type
&& (strcmp(s
->type
, type
) != 0))
186 lua_pushvalue(L
, 3); /* iterator function */
187 uci_push_section(L
, s
);
188 if (lua_pcall(L
, 1, 0, 0) == 0)
193 lua_pushboolean(L
, ret
);
198 uci_lua_get_any(lua_State
*L
, bool all
)
200 struct uci_element
*e
= NULL
;
201 struct uci_package
*p
= NULL
;
202 const char *package
= NULL
;
203 const char *section
= NULL
;
204 const char *option
= NULL
;
206 int err
= UCI_ERR_MEM
;
211 luaL_checkstring(L
, 1);
212 s
= strdup(lua_tostring(L
, 1));
217 package
= luaL_checkstring(L
, 1);
218 section
= luaL_checkstring(L
, 2);
220 option
= luaL_checkstring(L
, 3);
222 if ((err
= uci_parse_tuple(ctx
, s
, (char **) &package
, (char **) §ion
, (char **) &option
, NULL
)))
226 if (!all
&& (section
== NULL
)) {
231 p
= find_package(L
, package
, AUTOLOAD_ON
);
233 err
= UCI_ERR_NOTFOUND
;
238 if ((err
= uci_lookup(ctx
, &e
, p
, section
, option
)))
245 case UCI_TYPE_PACKAGE
:
246 uci_push_package(L
, p
);
248 case UCI_TYPE_SECTION
:
250 uci_push_section(L
, uci_to_section(e
));
252 lua_pushstring(L
, uci_to_section(e
)->type
);
254 case UCI_TYPE_OPTION
:
255 lua_pushstring(L
, uci_to_option(e
)->value
);
268 uci_lua_perror(L
, "uci.get");
270 case UCI_ERR_NOTFOUND
:
279 uci_lua_get(lua_State
*L
)
281 return uci_lua_get_any(L
, false);
285 uci_lua_get_all(lua_State
*L
)
287 return uci_lua_get_any(L
, true);
291 uci_lua_add(lua_State
*L
)
293 struct uci_section
*s
= NULL
;
294 struct uci_package
*p
;
297 const char *name
= NULL
;
300 package
= luaL_checkstring(L
, 1);
301 type
= luaL_checkstring(L
, 2);
302 p
= find_package(L
, package
, AUTOLOAD_ON
);
306 if (uci_add_section(ctx
, p
, type
, &s
) || !s
)
312 lua_pushstring(L
, name
);
317 uci_lua_delete(lua_State
*L
)
319 const char *package
= NULL
;
320 const char *section
= NULL
;
321 const char *option
= NULL
;
322 struct uci_package
*p
;
324 int err
= UCI_ERR_MEM
;
327 nargs
= lua_gettop(L
);
328 s
= luaL_checkstring(L
, 1);
331 /* Format: uci.delete("p.s[.o]") */
336 if ((err
= uci_parse_tuple(ctx
, (char *) s
, (char **) &package
, (char **) §ion
, (char **) &option
, NULL
)))
340 /* Format: uci.delete("p", "s", "o") */
341 option
= luaL_checkstring(L
, 3);
344 /* Format: uci.delete("p", "s") */
345 section
= luaL_checkstring(L
, 2);
353 p
= find_package(L
, package
, AUTOLOAD_ON
);
355 err
= UCI_ERR_NOTFOUND
;
358 err
= uci_delete(ctx
, p
, section
, option
);
362 uci_lua_perror(L
, "uci.set");
363 lua_pushboolean(L
, (err
== 0));
368 uci_lua_set(lua_State
*L
)
370 struct uci_package
*p
;
371 const char *package
= NULL
;
372 const char *section
= NULL
;
373 const char *option
= NULL
;
374 const char *value
= NULL
;
376 int err
= UCI_ERR_MEM
;
379 nargs
= lua_gettop(L
);
381 s
= luaL_checkstring(L
, 1);
384 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
389 if ((err
= uci_parse_tuple(ctx
, (char *) s
, (char **) &package
, (char **) §ion
, (char **) &option
, (char **) &value
)))
393 /* Format: uci.set("p", "s", "o", "v") */
394 option
= luaL_checkstring(L
, 3);
397 /* Format: uci.set("p", "s", "v") */
399 section
= luaL_checkstring(L
, 2);
400 value
= luaL_checkstring(L
, nargs
);
407 if ((section
== NULL
) || (value
== NULL
)) {
412 p
= find_package(L
, package
, AUTOLOAD_ON
);
414 err
= UCI_ERR_NOTFOUND
;
417 err
= uci_set(ctx
, p
, section
, option
, value
, NULL
);
421 uci_lua_perror(L
, "uci.set");
422 lua_pushboolean(L
, (err
== 0));
433 uci_lua_package_cmd(lua_State
*L
, enum pkg_cmd cmd
)
435 struct uci_element
*e
, *tmp
;
436 const char *s
= NULL
;
437 const char *section
= NULL
;
438 const char *option
= NULL
;
442 nargs
= lua_gettop(L
);
445 if (cmd
!= CMD_REVERT
)
447 luaL_checkstring(L
, 1);
448 option
= lua_tostring(L
, -1);
452 if (cmd
!= CMD_REVERT
)
454 luaL_checkstring(L
, 1);
455 section
= lua_tostring(L
, -1);
459 luaL_checkstring(L
, 1);
460 s
= lua_tostring(L
, -1);
467 luaL_error(L
, "Invalid argument count");
471 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
472 struct uci_package
*p
= uci_to_package(e
);
473 int ret
= UCI_ERR_INVAL
;
475 if (s
&& (strcmp(s
, e
->name
) != 0))
480 ret
= uci_commit(ctx
, &p
, false);
483 ret
= uci_save(ctx
, p
);
486 ret
= uci_revert(ctx
, &p
, section
, option
);
494 lua_pushboolean(L
, !failed
);
499 uci_lua_save(lua_State
*L
)
501 return uci_lua_package_cmd(L
, CMD_SAVE
);
505 uci_lua_commit(lua_State
*L
)
507 return uci_lua_package_cmd(L
, CMD_COMMIT
);
511 uci_lua_revert(lua_State
*L
)
513 return uci_lua_package_cmd(L
, CMD_REVERT
);
517 uci_lua_add_change(lua_State
*L
, struct uci_element
*e
)
519 struct uci_history
*h
;
522 h
= uci_to_history(e
);
526 lua_getfield(L
, -1, h
->section
);
527 if (lua_isnil(L
, -1)) {
530 lua_pushvalue(L
, -1); /* copy for setfield */
531 lua_setfield(L
, -3, h
->section
);
534 name
= (h
->e
.name
? h
->e
.name
: ".type");
536 lua_pushstring(L
, h
->value
);
538 lua_pushstring(L
, "");
539 lua_setfield(L
, -2, name
);
544 uci_lua_changes_pkg(lua_State
*L
, const char *package
)
546 struct uci_package
*p
= NULL
;
547 struct uci_element
*e
;
548 bool autoload
= false;
550 p
= find_package(L
, package
, AUTOLOAD_OFF
);
553 p
= find_package(L
, package
, AUTOLOAD_FORCE
);
558 if (uci_list_empty(&p
->history
) && uci_list_empty(&p
->saved_history
))
562 uci_foreach_element(&p
->saved_history
, e
) {
563 uci_lua_add_change(L
, e
);
565 uci_foreach_element(&p
->history
, e
) {
566 uci_lua_add_change(L
, e
);
568 lua_setfield(L
, -2, p
->e
.name
);
576 uci_lua_changes(lua_State
*L
)
578 const char *package
= NULL
;
579 char **config
= NULL
;
583 nargs
= lua_gettop(L
);
586 package
= luaL_checkstring(L
, 1);
590 luaL_error(L
, "invalid argument count");
595 uci_lua_changes_pkg(L
, package
);
597 if (uci_list_configs(ctx
, &config
) != 0)
600 for(i
= 0; config
[i
] != NULL
; i
++) {
601 uci_lua_changes_pkg(L
, config
[i
]);
610 uci_lua_set_confdir(lua_State
*L
)
614 luaL_checkstring(L
, 1);
615 ret
= uci_set_confdir(ctx
, lua_tostring(L
, -1));
616 lua_pushboolean(L
, (ret
== 0));
621 uci_lua_set_savedir(lua_State
*L
)
625 luaL_checkstring(L
, 1);
626 ret
= uci_set_savedir(ctx
, lua_tostring(L
, -1));
627 lua_pushboolean(L
, (ret
== 0));
632 static const luaL_Reg uci
[] = {
633 { "load", uci_lua_load
},
634 { "unload", uci_lua_unload
},
635 { "get", uci_lua_get
},
636 { "get_all", uci_lua_get_all
},
637 { "add", uci_lua_add
},
638 { "set", uci_lua_set
},
639 { "save", uci_lua_save
},
640 { "delete", uci_lua_delete
},
641 { "commit", uci_lua_commit
},
642 { "revert", uci_lua_revert
},
643 { "changes", uci_lua_changes
},
644 { "foreach", uci_lua_foreach
},
645 { "set_confdir", uci_lua_set_confdir
},
646 { "set_savedir", uci_lua_set_savedir
},
652 luaopen_uci(lua_State
*L
)
654 ctx
= uci_alloc_context();
656 luaL_error(L
, "Cannot allocate UCI context\n");
657 luaL_register(L
, MODNAME
, uci
);
659 /* enable autoload by default */
660 lua_getfield(L
, LUA_GLOBALSINDEX
, "uci");
661 lua_pushboolean(L
, 1);
662 lua_setfield(L
, -2, "autoload");