add uci.get_all to the lua binding, which converts a section or package to a lua...
[project/uci.git] / lua / uci.c
1 /*
2  * libuci plugin for Lua
3  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4  *
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
8  *
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.
13  */
14
15 #include <sys/types.h>
16 #include <sys/time.h>
17 #include <stdbool.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <errno.h>
23
24 #include <lauxlib.h>
25 #include <uci.h>
26
27 #define MODNAME        "uci"
28 //#define DEBUG 1
29
30 #ifdef DEBUG
31 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
32 #else
33 #define DPRINTF(...) do {} while (0)
34 #endif
35
36 static struct uci_context *ctx = NULL;
37
38 static struct uci_package *
39 find_package(const char *name)
40 {
41         struct uci_package *p = NULL;
42         struct uci_element *e;
43         uci_foreach_element(&ctx->root, e) {
44                 if (strcmp(e->name, name) != 0)
45                         continue;
46
47                 p = uci_to_package(e);
48                 break;
49         }
50         return p;
51 }
52
53 static void uci_lua_perror(lua_State *L, char *name)
54 {
55         lua_getfield(L, LUA_GLOBALSINDEX, "uci");
56         lua_getfield(L, -1, "warn");
57         if (!lua_isboolean(L, -1))
58                 goto done;
59         if (lua_toboolean(L, -1) != 1)
60                 goto done;
61         uci_perror(ctx, name);
62 done:
63         lua_pop(L, 2);
64 }
65
66 static void uci_push_section(lua_State *L, struct uci_section *s)
67 {
68         struct uci_element *e;
69
70         lua_newtable(L);
71         lua_pushstring(L, s->type);
72         lua_setfield(L, -2, "type");
73         lua_pushstring(L, s->e.name);
74         lua_setfield(L, -2, "name");
75
76         lua_newtable(L);
77         lua_pushvalue(L, -1);
78         lua_setfield(L, -3, "options");
79
80         uci_foreach_element(&s->options, e) {
81                 struct uci_option *o = uci_to_option(e);
82                 lua_pushstring(L, o->value);
83                 lua_setfield(L, -2, o->e.name);
84         }
85         lua_pop(L, 1);
86 }
87
88 static void uci_push_package(lua_State *L, struct uci_package *p)
89 {
90         struct uci_element *e;
91         int i = 0;
92
93         lua_newtable(L);
94         uci_foreach_element(&p->sections, e) {
95                 i++;
96                 luaL_setn(L, -1, i);
97                 uci_push_section(L, uci_to_section(e));
98                 lua_rawseti(L, -2, i);
99         }
100 }
101
102 static int
103 uci_lua_unload(lua_State *L)
104 {
105         struct uci_package *p;
106         const char *s;
107
108         luaL_checkstring(L, 1);
109         s = lua_tostring(L, -1);
110         p = find_package(s);
111         if (p) {
112                 uci_unload(ctx, p);
113                 lua_pushboolean(L, 1);
114         } else {
115                 lua_pushboolean(L, 0);
116         }
117         return 1;
118 }
119
120 static int
121 uci_lua_load(lua_State *L)
122 {
123         struct uci_package *p = NULL;
124         const char *s;
125
126         uci_lua_unload(L);
127         lua_pop(L, 1); /* bool ret value of unload */
128         s = lua_tostring(L, -1);
129
130         if (uci_load(ctx, s, &p)) {
131                 uci_lua_perror(L, "uci.load");
132                 lua_pushboolean(L, 0);
133         } else {
134                 lua_pushboolean(L, 1);
135         }
136
137         return 1;
138 }
139
140 static int
141 uci_lua_get_any(lua_State *L, bool all)
142 {
143         struct uci_element *e = NULL;
144         struct uci_package *p = NULL;
145         char *package = NULL;
146         char *section = NULL;
147         char *option = NULL;
148         char *s;
149         int err = UCI_ERR_MEM;
150
151         luaL_checkstring(L, 1);
152         s = strdup(lua_tostring(L, -1));
153         if (!s)
154                 goto error;
155
156         if ((err = uci_parse_tuple(ctx, s, &package, &section, &option, NULL)))
157                 goto error;
158
159         if (!all && (section == NULL)) {
160                 err = UCI_ERR_INVAL;
161                 goto error;
162         }
163
164         p = find_package(package);
165         if (!p) {
166                 err = UCI_ERR_NOTFOUND;
167                 goto error;
168         }
169
170         if (section) {
171                 if ((err = uci_lookup(ctx, &e, p, section, option)))
172                         goto error;
173         } else {
174                 e = &p->e;
175         }
176
177         switch(e->type) {
178                 case UCI_TYPE_PACKAGE:
179                         uci_push_package(L, p);
180                         break;
181                 case UCI_TYPE_SECTION:
182                         if (all)
183                                 uci_push_section(L, uci_to_section(e));
184                         else
185                                 lua_pushstring(L, uci_to_section(e)->type);
186                         break;
187                 case UCI_TYPE_OPTION:
188                         lua_pushstring(L, uci_to_option(e)->value);
189                         break;
190                 default:
191                         err = UCI_ERR_INVAL;
192                         goto error;
193         }
194 error:
195         if (s)
196                 free(s);
197
198         switch(err) {
199         default:
200                 ctx->err = err;
201                 uci_lua_perror(L, "uci.get");
202                 /* fall through */
203         case UCI_ERR_NOTFOUND:
204                 lua_pushnil(L);
205                 /* fall through */
206         case 0:
207                 return 1;
208         }
209 }
210
211 static int
212 uci_lua_get(lua_State *L)
213 {
214         return uci_lua_get_any(L, false);
215 }
216
217 static int
218 uci_lua_get_all(lua_State *L)
219 {
220         return uci_lua_get_any(L, true);
221 }
222
223 static int
224 uci_lua_set(lua_State *L)
225 {
226         struct uci_package *p;
227         char *package = NULL;
228         char *section = NULL;
229         char *option = NULL;
230         char *value = NULL;
231         char *s;
232         int err = UCI_ERR_MEM;
233
234         luaL_checkstring(L, 1);
235         s = strdup(lua_tostring(L, -1));
236         if (!s)
237                 goto error;
238
239         if ((err = uci_parse_tuple(ctx, s, &package, &section, &option, &value)))
240                 goto error;
241
242         if ((section == NULL) || (value == NULL)) {
243                 err = UCI_ERR_INVAL;
244                 goto error;
245         }
246
247         p = find_package(package);
248         if (!p) {
249                 err = UCI_ERR_NOTFOUND;
250                 goto error;
251         }
252         err = uci_set(ctx, p, section, option, value, NULL);
253
254 error:
255         if (err)
256                 uci_lua_perror(L, "uci.set");
257         lua_pushboolean(L, (err == 0));
258         return 1;
259 }
260
261 enum pkg_cmd {
262         CMD_SAVE,
263         CMD_COMMIT,
264         CMD_REVERT
265 };
266
267 static int
268 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
269 {
270         struct uci_element *e, *tmp;
271         const char *s = NULL;
272         const char *section = NULL;
273         const char *option = NULL;
274         int failed = 0;
275         int nargs;
276
277         nargs = lua_gettop(L);
278         switch(nargs) {
279         case 3:
280                 if (cmd != CMD_REVERT)
281                         goto err;
282                 luaL_checkstring(L, 1);
283                 option = lua_tostring(L, -1);
284                 lua_pop(L, 1);
285                 /* fall through */
286         case 2:
287                 if (cmd != CMD_REVERT)
288                         goto err;
289                 luaL_checkstring(L, 1);
290                 section = lua_tostring(L, -1);
291                 lua_pop(L, 1);
292                 /* fall through */
293         case 1:
294                 luaL_checkstring(L, 1);
295                 s = lua_tostring(L, -1);
296                 lua_pop(L, 1);
297                 break;
298         default:
299                 err:
300                 luaL_error(L, "Invalid argument count");
301                 break;
302         }
303
304         uci_foreach_element_safe(&ctx->root, tmp, e) {
305                 struct uci_package *p = uci_to_package(e);
306                 int ret = UCI_ERR_INVAL;
307
308                 if (s && (strcmp(s, e->name) != 0))
309                         continue;
310
311                 switch(cmd) {
312                 case CMD_COMMIT:
313                         ret = uci_commit(ctx, &p, false);
314                         break;
315                 case CMD_SAVE:
316                         ret = uci_save(ctx, p);
317                         break;
318                 case CMD_REVERT:
319                         ret = uci_revert(ctx, &p, section, option);
320                         break;
321                 }
322
323                 if (ret != 0)
324                         failed = 1;
325         }
326
327         lua_pushboolean(L, !failed);
328         return 1;
329 }
330
331 static int
332 uci_lua_save(lua_State *L)
333 {
334         return uci_lua_package_cmd(L, CMD_SAVE);
335 }
336
337 static int
338 uci_lua_commit(lua_State *L)
339 {
340         return uci_lua_package_cmd(L, CMD_COMMIT);
341 }
342
343 static int
344 uci_lua_revert(lua_State *L)
345 {
346         return uci_lua_package_cmd(L, CMD_REVERT);
347 }
348
349 static int
350 uci_lua_set_confdir(lua_State *L)
351 {
352         int ret;
353
354         luaL_checkstring(L, 1);
355         ret = uci_set_confdir(ctx, lua_tostring(L, -1));
356         lua_pushboolean(L, (ret == 0));
357         return 1;
358 }
359
360 static int
361 uci_lua_set_savedir(lua_State *L)
362 {
363         int ret;
364
365         luaL_checkstring(L, 1);
366         ret = uci_set_savedir(ctx, lua_tostring(L, -1));
367         lua_pushboolean(L, (ret == 0));
368
369         return 1;
370 }
371
372 static const luaL_Reg uci[] = {
373         { "load", uci_lua_load },
374         { "unload", uci_lua_unload },
375         { "get", uci_lua_get },
376         { "get_all", uci_lua_get_all },
377         { "set", uci_lua_set },
378         { "save", uci_lua_save },
379         { "commit", uci_lua_commit },
380         { "revert", uci_lua_revert },
381         { "set_confdir", uci_lua_set_confdir },
382         { "set_savedir", uci_lua_set_savedir },
383         { NULL, NULL },
384 };
385
386
387 int
388 luaopen_uci(lua_State *L)
389 {
390         ctx = uci_alloc_context();
391         if (!ctx)
392                 luaL_error(L, "Cannot allocate UCI context\n");
393         luaL_register(L, MODNAME, uci);
394         return 0;
395 }