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
;
38 static struct uci_package
*
39 find_package(lua_State
*L
, const char *name
, bool autoload
)
41 struct uci_package
*p
= NULL
;
42 struct uci_element
*e
;
44 uci_foreach_element(&ctx
->root
, e
) {
45 if (strcmp(e
->name
, name
) != 0)
48 p
= uci_to_package(e
);
54 lua_getfield(L
, LUA_GLOBALSINDEX
, "uci");
55 lua_getfield(L
, -1, "autoload");
56 if (!lua_isboolean(L
, -1))
59 if (!lua_toboolean(L
, -1))
62 uci_load(ctx
, name
, &p
);
71 static void uci_lua_perror(lua_State
*L
, char *name
)
73 lua_getfield(L
, LUA_GLOBALSINDEX
, "uci");
74 lua_getfield(L
, -1, "warn");
75 if (!lua_isboolean(L
, -1))
77 if (lua_toboolean(L
, -1) != 1)
79 uci_perror(ctx
, name
);
84 static void uci_push_section(lua_State
*L
, struct uci_section
*s
)
86 struct uci_element
*e
;
89 lua_pushstring(L
, s
->type
);
90 lua_setfield(L
, -2, "type");
91 lua_pushstring(L
, s
->e
.name
);
92 lua_setfield(L
, -2, "name");
96 lua_setfield(L
, -3, "options");
98 uci_foreach_element(&s
->options
, e
) {
99 struct uci_option
*o
= uci_to_option(e
);
100 lua_pushstring(L
, o
->value
);
101 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
) {
115 uci_push_section(L
, uci_to_section(e
));
116 lua_rawseti(L
, -2, i
);
121 uci_lua_unload(lua_State
*L
)
123 struct uci_package
*p
;
126 luaL_checkstring(L
, 1);
127 s
= lua_tostring(L
, -1);
128 p
= find_package(L
, s
, false);
131 lua_pushboolean(L
, 1);
133 lua_pushboolean(L
, 0);
139 uci_lua_load(lua_State
*L
)
141 struct uci_package
*p
= NULL
;
145 lua_pop(L
, 1); /* bool ret value of unload */
146 s
= lua_tostring(L
, -1);
148 if (uci_load(ctx
, s
, &p
)) {
149 uci_lua_perror(L
, "uci.load");
150 lua_pushboolean(L
, 0);
152 lua_pushboolean(L
, 1);
160 uci_lua_foreach(lua_State
*L
)
162 struct uci_package
*p
;
163 struct uci_element
*e
;
164 const char *package
, *type
;
167 package
= luaL_checkstring(L
, 1);
172 type
= luaL_checkstring(L
, 2);
174 if (!lua_isfunction(L
, 3) || !package
)
175 luaL_error(L
, "Invalid argument");
177 p
= find_package(L
, package
, true);
181 uci_foreach_element(&p
->sections
, e
) {
182 struct uci_section
*s
= uci_to_section(e
);
184 if (type
&& (strcmp(s
->type
, type
) != 0))
187 lua_pushvalue(L
, 3); /* iterator function */
188 uci_push_section(L
, s
);
189 if (lua_pcall(L
, 1, 0, 0) == 0)
194 lua_pushboolean(L
, ret
);
199 uci_lua_get_any(lua_State
*L
, bool all
)
201 struct uci_element
*e
= NULL
;
202 struct uci_package
*p
= NULL
;
203 const char *package
= NULL
;
204 const char *section
= NULL
;
205 const char *option
= NULL
;
207 int err
= UCI_ERR_MEM
;
212 luaL_checkstring(L
, 1);
213 s
= strdup(lua_tostring(L
, 1));
218 package
= luaL_checkstring(L
, 1);
219 section
= luaL_checkstring(L
, 2);
221 option
= luaL_checkstring(L
, 3);
223 if ((err
= uci_parse_tuple(ctx
, s
, (char **) &package
, (char **) §ion
, (char **) &option
, NULL
)))
227 if (!all
&& (section
== NULL
)) {
232 p
= find_package(L
, package
, true);
234 err
= UCI_ERR_NOTFOUND
;
239 if ((err
= uci_lookup(ctx
, &e
, p
, section
, option
)))
246 case UCI_TYPE_PACKAGE
:
247 uci_push_package(L
, p
);
249 case UCI_TYPE_SECTION
:
251 uci_push_section(L
, uci_to_section(e
));
253 lua_pushstring(L
, uci_to_section(e
)->type
);
255 case UCI_TYPE_OPTION
:
256 lua_pushstring(L
, uci_to_option(e
)->value
);
269 uci_lua_perror(L
, "uci.get");
271 case UCI_ERR_NOTFOUND
:
280 uci_lua_get(lua_State
*L
)
282 return uci_lua_get_any(L
, false);
286 uci_lua_get_all(lua_State
*L
)
288 return uci_lua_get_any(L
, true);
292 uci_lua_add(lua_State
*L
)
294 struct uci_section
*s
= NULL
;
295 struct uci_package
*p
;
298 const char *name
= NULL
;
301 package
= luaL_checkstring(L
, 1);
302 type
= luaL_checkstring(L
, 2);
303 p
= find_package(L
, package
, true);
307 if (uci_add_section(ctx
, p
, type
, &s
) || !s
)
313 lua_pushstring(L
, name
);
318 uci_lua_delete(lua_State
*L
)
320 const char *package
= NULL
;
321 const char *section
= NULL
;
322 const char *option
= NULL
;
323 struct uci_package
*p
;
325 int err
= UCI_ERR_MEM
;
328 nargs
= lua_gettop(L
);
329 s
= luaL_checkstring(L
, 1);
332 /* Format: uci.delete("p.s[.o]") */
337 if ((err
= uci_parse_tuple(ctx
, (char *) s
, (char **) &package
, (char **) §ion
, (char **) &option
, NULL
)))
341 /* Format: uci.delete("p", "s", "o") */
342 option
= luaL_checkstring(L
, 3);
345 /* Format: uci.delete("p", "s") */
346 section
= luaL_checkstring(L
, 2);
354 p
= find_package(L
, package
, true);
356 err
= UCI_ERR_NOTFOUND
;
359 err
= uci_delete(ctx
, p
, section
, option
);
363 uci_lua_perror(L
, "uci.set");
364 lua_pushboolean(L
, (err
== 0));
369 uci_lua_set(lua_State
*L
)
371 struct uci_package
*p
;
372 const char *package
= NULL
;
373 const char *section
= NULL
;
374 const char *option
= NULL
;
375 const char *value
= NULL
;
377 int err
= UCI_ERR_MEM
;
380 nargs
= lua_gettop(L
);
382 s
= luaL_checkstring(L
, 1);
385 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
390 if ((err
= uci_parse_tuple(ctx
, (char *) s
, (char **) &package
, (char **) §ion
, (char **) &option
, (char **) &value
)))
394 /* Format: uci.set("p", "s", "o", "v") */
395 option
= luaL_checkstring(L
, 3);
398 /* Format: uci.set("p", "s", "v") */
400 section
= luaL_checkstring(L
, 2);
401 value
= luaL_checkstring(L
, nargs
);
408 if ((section
== NULL
) || (value
== NULL
)) {
413 p
= find_package(L
, package
, true);
415 err
= UCI_ERR_NOTFOUND
;
418 err
= uci_set(ctx
, p
, section
, option
, value
, NULL
);
422 uci_lua_perror(L
, "uci.set");
423 lua_pushboolean(L
, (err
== 0));
434 uci_lua_package_cmd(lua_State
*L
, enum pkg_cmd cmd
)
436 struct uci_element
*e
, *tmp
;
437 const char *s
= NULL
;
438 const char *section
= NULL
;
439 const char *option
= NULL
;
443 nargs
= lua_gettop(L
);
446 if (cmd
!= CMD_REVERT
)
448 luaL_checkstring(L
, 1);
449 option
= lua_tostring(L
, -1);
453 if (cmd
!= CMD_REVERT
)
455 luaL_checkstring(L
, 1);
456 section
= lua_tostring(L
, -1);
460 luaL_checkstring(L
, 1);
461 s
= lua_tostring(L
, -1);
468 luaL_error(L
, "Invalid argument count");
472 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
473 struct uci_package
*p
= uci_to_package(e
);
474 int ret
= UCI_ERR_INVAL
;
476 if (s
&& (strcmp(s
, e
->name
) != 0))
481 ret
= uci_commit(ctx
, &p
, false);
484 ret
= uci_save(ctx
, p
);
487 ret
= uci_revert(ctx
, &p
, section
, option
);
495 lua_pushboolean(L
, !failed
);
500 uci_lua_save(lua_State
*L
)
502 return uci_lua_package_cmd(L
, CMD_SAVE
);
506 uci_lua_commit(lua_State
*L
)
508 return uci_lua_package_cmd(L
, CMD_COMMIT
);
512 uci_lua_revert(lua_State
*L
)
514 return uci_lua_package_cmd(L
, CMD_REVERT
);
518 uci_lua_set_confdir(lua_State
*L
)
522 luaL_checkstring(L
, 1);
523 ret
= uci_set_confdir(ctx
, lua_tostring(L
, -1));
524 lua_pushboolean(L
, (ret
== 0));
529 uci_lua_set_savedir(lua_State
*L
)
533 luaL_checkstring(L
, 1);
534 ret
= uci_set_savedir(ctx
, lua_tostring(L
, -1));
535 lua_pushboolean(L
, (ret
== 0));
540 static const luaL_Reg uci
[] = {
541 { "load", uci_lua_load
},
542 { "unload", uci_lua_unload
},
543 { "get", uci_lua_get
},
544 { "get_all", uci_lua_get_all
},
545 { "add", uci_lua_add
},
546 { "set", uci_lua_set
},
547 { "save", uci_lua_save
},
548 { "delete", uci_lua_delete
},
549 { "commit", uci_lua_commit
},
550 { "revert", uci_lua_revert
},
551 { "foreach", uci_lua_foreach
},
552 { "set_confdir", uci_lua_set_confdir
},
553 { "set_savedir", uci_lua_set_savedir
},
559 luaopen_uci(lua_State
*L
)
561 ctx
= uci_alloc_context();
563 luaL_error(L
, "Cannot allocate UCI context\n");
564 luaL_register(L
, MODNAME
, uci
);
566 /* enable autoload by default */
567 lua_getfield(L
, LUA_GLOBALSINDEX
, "uci");
568 lua_pushboolean(L
, 1);
569 lua_setfield(L
, -2, "autoload");