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");
98 lua_pushstring(L
, s
->e
.name
);
99 lua_setfield(L
, -2, ".name");
101 uci_foreach_element(&s
->options
, e
) {
102 struct uci_option
*o
= uci_to_option(e
);
103 lua_pushstring(L
, o
->value
);
104 lua_setfield(L
, -2, o
->e
.name
);
108 static void uci_push_package(lua_State
*L
, struct uci_package
*p
)
110 struct uci_element
*e
;
114 uci_foreach_element(&p
->sections
, e
) {
116 uci_push_section(L
, uci_to_section(e
));
117 lua_setfield(L
, -2, e
->name
);
122 uci_lua_unload(lua_State
*L
)
124 struct uci_package
*p
;
127 luaL_checkstring(L
, 1);
128 s
= lua_tostring(L
, -1);
129 p
= find_package(L
, s
, AUTOLOAD_OFF
);
132 lua_pushboolean(L
, 1);
134 lua_pushboolean(L
, 0);
140 uci_lua_load(lua_State
*L
)
142 struct uci_package
*p
= NULL
;
146 lua_pop(L
, 1); /* bool ret value of unload */
147 s
= lua_tostring(L
, -1);
149 if (uci_load(ctx
, s
, &p
)) {
150 uci_lua_perror(L
, "uci.load");
151 lua_pushboolean(L
, 0);
153 lua_pushboolean(L
, 1);
161 uci_lua_foreach(lua_State
*L
)
163 struct uci_package
*p
;
164 struct uci_element
*e
;
165 const char *package
, *type
;
168 package
= luaL_checkstring(L
, 1);
173 type
= luaL_checkstring(L
, 2);
175 if (!lua_isfunction(L
, 3) || !package
)
176 luaL_error(L
, "Invalid argument");
178 p
= find_package(L
, package
, AUTOLOAD_ON
);
182 uci_foreach_element(&p
->sections
, e
) {
183 struct uci_section
*s
= uci_to_section(e
);
185 if (type
&& (strcmp(s
->type
, type
) != 0))
188 lua_pushvalue(L
, 3); /* iterator function */
189 uci_push_section(L
, s
);
190 if (lua_pcall(L
, 1, 0, 0) == 0)
195 lua_pushboolean(L
, ret
);
200 uci_lua_get_any(lua_State
*L
, bool all
)
202 struct uci_element
*e
= NULL
;
203 struct uci_package
*p
= NULL
;
204 const char *package
= NULL
;
205 const char *section
= NULL
;
206 const char *option
= NULL
;
208 int err
= UCI_ERR_MEM
;
213 luaL_checkstring(L
, 1);
214 s
= strdup(lua_tostring(L
, 1));
219 package
= luaL_checkstring(L
, 1);
220 section
= luaL_checkstring(L
, 2);
222 option
= luaL_checkstring(L
, 3);
224 if ((err
= uci_parse_tuple(ctx
, s
, (char **) &package
, (char **) §ion
, (char **) &option
, NULL
)))
228 if (!all
&& (section
== NULL
)) {
233 p
= find_package(L
, package
, AUTOLOAD_ON
);
235 err
= UCI_ERR_NOTFOUND
;
240 if ((err
= uci_lookup(ctx
, &e
, p
, section
, option
)))
247 case UCI_TYPE_PACKAGE
:
248 uci_push_package(L
, p
);
250 case UCI_TYPE_SECTION
:
252 uci_push_section(L
, uci_to_section(e
));
254 lua_pushstring(L
, uci_to_section(e
)->type
);
256 case UCI_TYPE_OPTION
:
257 lua_pushstring(L
, uci_to_option(e
)->value
);
270 uci_lua_perror(L
, "uci.get");
272 case UCI_ERR_NOTFOUND
:
281 uci_lua_get(lua_State
*L
)
283 return uci_lua_get_any(L
, false);
287 uci_lua_get_all(lua_State
*L
)
289 return uci_lua_get_any(L
, true);
293 uci_lua_add(lua_State
*L
)
295 struct uci_section
*s
= NULL
;
296 struct uci_package
*p
;
299 const char *name
= NULL
;
302 package
= luaL_checkstring(L
, 1);
303 type
= luaL_checkstring(L
, 2);
304 p
= find_package(L
, package
, AUTOLOAD_ON
);
308 if (uci_add_section(ctx
, p
, type
, &s
) || !s
)
314 lua_pushstring(L
, name
);
319 uci_lua_delete(lua_State
*L
)
321 const char *package
= NULL
;
322 const char *section
= NULL
;
323 const char *option
= NULL
;
324 struct uci_package
*p
;
326 int err
= UCI_ERR_MEM
;
329 nargs
= lua_gettop(L
);
330 s
= luaL_checkstring(L
, 1);
333 /* Format: uci.delete("p.s[.o]") */
338 if ((err
= uci_parse_tuple(ctx
, (char *) s
, (char **) &package
, (char **) §ion
, (char **) &option
, NULL
)))
342 /* Format: uci.delete("p", "s", "o") */
343 option
= luaL_checkstring(L
, 3);
346 /* Format: uci.delete("p", "s") */
347 section
= luaL_checkstring(L
, 2);
355 p
= find_package(L
, package
, AUTOLOAD_ON
);
357 err
= UCI_ERR_NOTFOUND
;
360 err
= uci_delete(ctx
, p
, section
, option
);
364 uci_lua_perror(L
, "uci.set");
365 lua_pushboolean(L
, (err
== 0));
370 uci_lua_set(lua_State
*L
)
372 struct uci_package
*p
;
373 const char *package
= NULL
;
374 const char *section
= NULL
;
375 const char *option
= NULL
;
376 const char *value
= NULL
;
378 int err
= UCI_ERR_MEM
;
381 nargs
= lua_gettop(L
);
383 s
= luaL_checkstring(L
, 1);
386 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
391 if ((err
= uci_parse_tuple(ctx
, (char *) s
, (char **) &package
, (char **) §ion
, (char **) &option
, (char **) &value
)))
395 /* Format: uci.set("p", "s", "o", "v") */
396 option
= luaL_checkstring(L
, 3);
399 /* Format: uci.set("p", "s", "v") */
401 section
= luaL_checkstring(L
, 2);
402 value
= luaL_checkstring(L
, nargs
);
409 if ((section
== NULL
) || (value
== NULL
)) {
414 p
= find_package(L
, package
, AUTOLOAD_ON
);
416 err
= UCI_ERR_NOTFOUND
;
419 err
= uci_set(ctx
, p
, section
, option
, value
, NULL
);
423 uci_lua_perror(L
, "uci.set");
424 lua_pushboolean(L
, (err
== 0));
435 uci_lua_package_cmd(lua_State
*L
, enum pkg_cmd cmd
)
437 struct uci_element
*e
, *tmp
;
438 const char *s
= NULL
;
439 const char *section
= NULL
;
440 const char *option
= NULL
;
444 nargs
= lua_gettop(L
);
447 if (cmd
!= CMD_REVERT
)
449 luaL_checkstring(L
, 1);
450 option
= lua_tostring(L
, -1);
454 if (cmd
!= CMD_REVERT
)
456 luaL_checkstring(L
, 1);
457 section
= lua_tostring(L
, -1);
461 luaL_checkstring(L
, 1);
462 s
= lua_tostring(L
, -1);
469 luaL_error(L
, "Invalid argument count");
473 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
474 struct uci_package
*p
= uci_to_package(e
);
475 int ret
= UCI_ERR_INVAL
;
477 if (s
&& (strcmp(s
, e
->name
) != 0))
482 ret
= uci_commit(ctx
, &p
, false);
485 ret
= uci_save(ctx
, p
);
488 ret
= uci_revert(ctx
, &p
, section
, option
);
496 lua_pushboolean(L
, !failed
);
501 uci_lua_save(lua_State
*L
)
503 return uci_lua_package_cmd(L
, CMD_SAVE
);
507 uci_lua_commit(lua_State
*L
)
509 return uci_lua_package_cmd(L
, CMD_COMMIT
);
513 uci_lua_revert(lua_State
*L
)
515 return uci_lua_package_cmd(L
, CMD_REVERT
);
519 uci_lua_add_change(lua_State
*L
, struct uci_element
*e
)
521 struct uci_history
*h
;
524 h
= uci_to_history(e
);
528 lua_getfield(L
, -1, h
->section
);
529 if (lua_isnil(L
, -1)) {
532 lua_pushvalue(L
, -1); /* copy for setfield */
533 lua_setfield(L
, -3, h
->section
);
536 name
= (h
->e
.name
? h
->e
.name
: ".type");
538 lua_pushstring(L
, h
->value
);
540 lua_pushstring(L
, "");
541 lua_setfield(L
, -2, name
);
546 uci_lua_changes_pkg(lua_State
*L
, const char *package
)
548 struct uci_package
*p
= NULL
;
549 struct uci_element
*e
;
550 bool autoload
= false;
552 p
= find_package(L
, package
, AUTOLOAD_OFF
);
555 p
= find_package(L
, package
, AUTOLOAD_FORCE
);
560 if (uci_list_empty(&p
->history
) && uci_list_empty(&p
->saved_history
))
564 uci_foreach_element(&p
->saved_history
, e
) {
565 uci_lua_add_change(L
, e
);
567 uci_foreach_element(&p
->history
, e
) {
568 uci_lua_add_change(L
, e
);
570 lua_setfield(L
, -2, p
->e
.name
);
578 uci_lua_changes(lua_State
*L
)
580 const char *package
= NULL
;
581 char **config
= NULL
;
585 nargs
= lua_gettop(L
);
588 package
= luaL_checkstring(L
, 1);
592 luaL_error(L
, "invalid argument count");
597 uci_lua_changes_pkg(L
, package
);
599 if (uci_list_configs(ctx
, &config
) != 0)
602 for(i
= 0; config
[i
] != NULL
; i
++) {
603 uci_lua_changes_pkg(L
, config
[i
]);
612 uci_lua_set_confdir(lua_State
*L
)
616 luaL_checkstring(L
, 1);
617 ret
= uci_set_confdir(ctx
, lua_tostring(L
, -1));
618 lua_pushboolean(L
, (ret
== 0));
623 uci_lua_set_savedir(lua_State
*L
)
627 luaL_checkstring(L
, 1);
628 ret
= uci_set_savedir(ctx
, lua_tostring(L
, -1));
629 lua_pushboolean(L
, (ret
== 0));
634 static const luaL_Reg uci
[] = {
635 { "load", uci_lua_load
},
636 { "unload", uci_lua_unload
},
637 { "get", uci_lua_get
},
638 { "get_all", uci_lua_get_all
},
639 { "add", uci_lua_add
},
640 { "set", uci_lua_set
},
641 { "save", uci_lua_save
},
642 { "delete", uci_lua_delete
},
643 { "commit", uci_lua_commit
},
644 { "revert", uci_lua_revert
},
645 { "changes", uci_lua_changes
},
646 { "foreach", uci_lua_foreach
},
647 { "set_confdir", uci_lua_set_confdir
},
648 { "set_savedir", uci_lua_set_savedir
},
654 luaopen_uci(lua_State
*L
)
656 ctx
= uci_alloc_context();
658 luaL_error(L
, "Cannot allocate UCI context\n");
659 luaL_register(L
, MODNAME
, uci
);
661 /* enable autoload by default */
662 lua_getfield(L
, LUA_GLOBALSINDEX
, "uci");
663 lua_pushboolean(L
, 1);
664 lua_setfield(L
, -2, "autoload");