988c9b9792942072fa225c1099cb7c0cf4fbd6c7
[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
141 static int
142 uci_lua_foreach(lua_State *L)
143 {
144         struct uci_package *p;
145         struct uci_element *e;
146         const char *package, *type;
147         bool ret = false;
148
149         package = luaL_checkstring(L, 1);
150
151         if (lua_isnil(L, 2))
152                 type = NULL;
153         else
154                 type = luaL_checkstring(L, 2);
155
156         if (!lua_isfunction(L, 3) || !package)
157                 luaL_error(L, "Invalid argument");
158
159         p = find_package(package);
160         if (!p)
161                 goto done;
162
163         uci_foreach_element(&p->sections, e) {
164                 struct uci_section *s = uci_to_section(e);
165
166                 if (type && (strcmp(s->type, type) != 0))
167                         continue;
168
169                 lua_pushvalue(L, 3); /* iterator function */
170                 uci_push_section(L, s);
171                 if (lua_pcall(L, 1, 0, 0) == 0)
172                         ret = true;
173         }
174
175 done:
176         lua_pushboolean(L, ret);
177         return 1;
178 }
179
180 static int
181 uci_lua_get_any(lua_State *L, bool all)
182 {
183         struct uci_element *e = NULL;
184         struct uci_package *p = NULL;
185         const char *package = NULL;
186         const char *section = NULL;
187         const char *option = NULL;
188         char *s;
189         int err = UCI_ERR_MEM;
190         int n;
191
192         n = lua_gettop(L);
193
194         luaL_checkstring(L, 1);
195         s = strdup(lua_tostring(L, 1));
196         if (!s)
197                 goto error;
198
199         if (n > 1) {
200                 package = luaL_checkstring(L, 1);
201                 section = luaL_checkstring(L, 2);
202                 if (n > 2)
203                         option = luaL_checkstring(L, 3);
204         } else {
205                 if ((err = uci_parse_tuple(ctx, s, (char **) &package, (char **) &section, (char **) &option, NULL)))
206                         goto error;
207         }
208
209         if (!all && (section == NULL)) {
210                 err = UCI_ERR_INVAL;
211                 goto error;
212         }
213
214         p = find_package(package);
215         if (!p) {
216                 err = UCI_ERR_NOTFOUND;
217                 goto error;
218         }
219
220         if (section) {
221                 if ((err = uci_lookup(ctx, &e, p, section, option)))
222                         goto error;
223         } else {
224                 e = &p->e;
225         }
226
227         switch(e->type) {
228                 case UCI_TYPE_PACKAGE:
229                         uci_push_package(L, p);
230                         break;
231                 case UCI_TYPE_SECTION:
232                         if (all)
233                                 uci_push_section(L, uci_to_section(e));
234                         else
235                                 lua_pushstring(L, uci_to_section(e)->type);
236                         break;
237                 case UCI_TYPE_OPTION:
238                         lua_pushstring(L, uci_to_option(e)->value);
239                         break;
240                 default:
241                         err = UCI_ERR_INVAL;
242                         goto error;
243         }
244 error:
245         if (s)
246                 free(s);
247
248         switch(err) {
249         default:
250                 ctx->err = err;
251                 uci_lua_perror(L, "uci.get");
252                 /* fall through */
253         case UCI_ERR_NOTFOUND:
254                 lua_pushnil(L);
255                 /* fall through */
256         case 0:
257                 return 1;
258         }
259 }
260
261 static int
262 uci_lua_get(lua_State *L)
263 {
264         return uci_lua_get_any(L, false);
265 }
266
267 static int
268 uci_lua_get_all(lua_State *L)
269 {
270         return uci_lua_get_any(L, true);
271 }
272
273 static int
274 uci_lua_add(lua_State *L)
275 {
276         struct uci_section *s = NULL;
277         struct uci_package *p;
278         const char *package;
279         const char *type;
280         const char *name = NULL;
281
282         do {
283                 package = luaL_checkstring(L, 1);
284                 type = luaL_checkstring(L, 2);
285                 p = find_package(package);
286                 if (!p)
287                         break;
288
289                 if (uci_add_section(ctx, p, type, &s) || !s)
290                         break;
291
292                 name = s->e.name;
293         } while (0);
294
295         lua_pushstring(L, name);
296         return 1;
297 }
298
299 static int
300 uci_lua_set(lua_State *L)
301 {
302         struct uci_package *p;
303         const char *package = NULL;
304         const char *section = NULL;
305         const char *option = NULL;
306         const char *value = NULL;
307         const char *s;
308         int err = UCI_ERR_MEM;
309         int nargs;
310
311         nargs = lua_gettop(L);
312
313         s = luaL_checkstring(L, 1);
314         switch(nargs) {
315         case 1:
316                 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
317                 s = strdup(s);
318                 if (!s)
319                         goto error;
320
321                 if ((err = uci_parse_tuple(ctx, (char *) s, (char **) &package, (char **) &section, (char **) &option, (char **) &value)))
322                         goto error;
323                 break;
324         case 4:
325                 /* Format: uci.set("p", "s", "o", "v") */
326                 option = luaL_checkstring(L, 3);
327                 /* fall through */
328         case 3:
329                 /* Format: uci.set("p", "s", "v") */
330                 package = s;
331                 section = luaL_checkstring(L, 2);
332                 value = luaL_checkstring(L, nargs);
333                 break;
334         default:
335                 err = UCI_ERR_INVAL;
336                 goto error;
337         }
338
339         if ((section == NULL) || (value == NULL)) {
340                 err = UCI_ERR_INVAL;
341                 goto error;
342         }
343
344         p = find_package(package);
345         if (!p) {
346                 err = UCI_ERR_NOTFOUND;
347                 goto error;
348         }
349         err = uci_set(ctx, p, section, option, value, NULL);
350
351 error:
352         if (err)
353                 uci_lua_perror(L, "uci.set");
354         lua_pushboolean(L, (err == 0));
355         return 1;
356 }
357
358 enum pkg_cmd {
359         CMD_SAVE,
360         CMD_COMMIT,
361         CMD_REVERT
362 };
363
364 static int
365 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
366 {
367         struct uci_element *e, *tmp;
368         const char *s = NULL;
369         const char *section = NULL;
370         const char *option = NULL;
371         int failed = 0;
372         int nargs;
373
374         nargs = lua_gettop(L);
375         switch(nargs) {
376         case 3:
377                 if (cmd != CMD_REVERT)
378                         goto err;
379                 luaL_checkstring(L, 1);
380                 option = lua_tostring(L, -1);
381                 lua_pop(L, 1);
382                 /* fall through */
383         case 2:
384                 if (cmd != CMD_REVERT)
385                         goto err;
386                 luaL_checkstring(L, 1);
387                 section = lua_tostring(L, -1);
388                 lua_pop(L, 1);
389                 /* fall through */
390         case 1:
391                 luaL_checkstring(L, 1);
392                 s = lua_tostring(L, -1);
393                 lua_pop(L, 1);
394                 break;
395         case 0:
396                 break;
397         default:
398                 err:
399                 luaL_error(L, "Invalid argument count");
400                 break;
401         }
402
403         uci_foreach_element_safe(&ctx->root, tmp, e) {
404                 struct uci_package *p = uci_to_package(e);
405                 int ret = UCI_ERR_INVAL;
406
407                 if (s && (strcmp(s, e->name) != 0))
408                         continue;
409
410                 switch(cmd) {
411                 case CMD_COMMIT:
412                         ret = uci_commit(ctx, &p, false);
413                         break;
414                 case CMD_SAVE:
415                         ret = uci_save(ctx, p);
416                         break;
417                 case CMD_REVERT:
418                         ret = uci_revert(ctx, &p, section, option);
419                         break;
420                 }
421
422                 if (ret != 0)
423                         failed = 1;
424         }
425
426         lua_pushboolean(L, !failed);
427         return 1;
428 }
429
430 static int
431 uci_lua_save(lua_State *L)
432 {
433         return uci_lua_package_cmd(L, CMD_SAVE);
434 }
435
436 static int
437 uci_lua_commit(lua_State *L)
438 {
439         return uci_lua_package_cmd(L, CMD_COMMIT);
440 }
441
442 static int
443 uci_lua_revert(lua_State *L)
444 {
445         return uci_lua_package_cmd(L, CMD_REVERT);
446 }
447
448 static int
449 uci_lua_set_confdir(lua_State *L)
450 {
451         int ret;
452
453         luaL_checkstring(L, 1);
454         ret = uci_set_confdir(ctx, lua_tostring(L, -1));
455         lua_pushboolean(L, (ret == 0));
456         return 1;
457 }
458
459 static int
460 uci_lua_set_savedir(lua_State *L)
461 {
462         int ret;
463
464         luaL_checkstring(L, 1);
465         ret = uci_set_savedir(ctx, lua_tostring(L, -1));
466         lua_pushboolean(L, (ret == 0));
467
468         return 1;
469 }
470
471 static const luaL_Reg uci[] = {
472         { "load", uci_lua_load },
473         { "unload", uci_lua_unload },
474         { "get", uci_lua_get },
475         { "get_all", uci_lua_get_all },
476         { "add", uci_lua_add },
477         { "set", uci_lua_set },
478         { "save", uci_lua_save },
479         { "commit", uci_lua_commit },
480         { "revert", uci_lua_revert },
481         { "foreach", uci_lua_foreach },
482         { "set_confdir", uci_lua_set_confdir },
483         { "set_savedir", uci_lua_set_savedir },
484         { NULL, NULL },
485 };
486
487
488 int
489 luaopen_uci(lua_State *L)
490 {
491         ctx = uci_alloc_context();
492         if (!ctx)
493                 luaL_error(L, "Cannot allocate UCI context\n");
494         luaL_register(L, MODNAME, uci);
495         return 0;
496 }