minor cleanup
[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 enum autoload {
38         AUTOLOAD_OFF = 0,
39         AUTOLOAD_ON = 1,
40         AUTOLOAD_FORCE = 2
41 };
42
43 static struct uci_package *
44 find_package(lua_State *L, const char *name, enum autoload al)
45 {
46         struct uci_package *p = NULL;
47         struct uci_element *e;
48
49         uci_foreach_element(&ctx->root, e) {
50                 if (strcmp(e->name, name) != 0)
51                         continue;
52
53                 p = uci_to_package(e);
54                 goto done;
55         }
56
57         if (al == AUTOLOAD_FORCE)
58                 uci_load(ctx, name, &p);
59         else if (al) {
60                 do {
61                         lua_getfield(L, LUA_GLOBALSINDEX, "uci");
62                         lua_getfield(L, -1, "autoload");
63                         if (!lua_isboolean(L, -1))
64                                 break;
65
66                         if (!lua_toboolean(L, -1))
67                                 break;
68
69                         uci_load(ctx, name, &p);
70                 } while (0);
71                 lua_pop(L, 2);
72         }
73
74 done:
75         return p;
76 }
77
78 static void uci_lua_perror(lua_State *L, char *name)
79 {
80         lua_getfield(L, LUA_GLOBALSINDEX, "uci");
81         lua_getfield(L, -1, "warn");
82         if (!lua_isboolean(L, -1))
83                 goto done;
84         if (lua_toboolean(L, -1) != 1)
85                 goto done;
86         uci_perror(ctx, name);
87 done:
88         lua_pop(L, 2);
89 }
90
91 static void uci_push_section(lua_State *L, struct uci_section *s)
92 {
93         struct uci_element *e;
94
95         lua_newtable(L);
96         lua_pushstring(L, s->type);
97         lua_setfield(L, -2, ".TYPE");
98
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);
103         }
104 }
105
106 static void uci_push_package(lua_State *L, struct uci_package *p)
107 {
108         struct uci_element *e;
109         int i = 0;
110
111         lua_newtable(L);
112         uci_foreach_element(&p->sections, e) {
113                 i++;
114                 uci_push_section(L, uci_to_section(e));
115                 lua_setfield(L, -2, e->name);
116         }
117 }
118
119 static int
120 uci_lua_unload(lua_State *L)
121 {
122         struct uci_package *p;
123         const char *s;
124
125         luaL_checkstring(L, 1);
126         s = lua_tostring(L, -1);
127         p = find_package(L, s, AUTOLOAD_OFF);
128         if (p) {
129                 uci_unload(ctx, p);
130                 lua_pushboolean(L, 1);
131         } else {
132                 lua_pushboolean(L, 0);
133         }
134         return 1;
135 }
136
137 static int
138 uci_lua_load(lua_State *L)
139 {
140         struct uci_package *p = NULL;
141         const char *s;
142
143         uci_lua_unload(L);
144         lua_pop(L, 1); /* bool ret value of unload */
145         s = lua_tostring(L, -1);
146
147         if (uci_load(ctx, s, &p)) {
148                 uci_lua_perror(L, "uci.load");
149                 lua_pushboolean(L, 0);
150         } else {
151                 lua_pushboolean(L, 1);
152         }
153
154         return 1;
155 }
156
157
158 static int
159 uci_lua_foreach(lua_State *L)
160 {
161         struct uci_package *p;
162         struct uci_element *e;
163         const char *package, *type;
164         bool ret = false;
165
166         package = luaL_checkstring(L, 1);
167
168         if (lua_isnil(L, 2))
169                 type = NULL;
170         else
171                 type = luaL_checkstring(L, 2);
172
173         if (!lua_isfunction(L, 3) || !package)
174                 luaL_error(L, "Invalid argument");
175
176         p = find_package(L, package, AUTOLOAD_ON);
177         if (!p)
178                 goto done;
179
180         uci_foreach_element(&p->sections, e) {
181                 struct uci_section *s = uci_to_section(e);
182
183                 if (type && (strcmp(s->type, type) != 0))
184                         continue;
185
186                 lua_pushvalue(L, 3); /* iterator function */
187                 uci_push_section(L, s);
188                 if (lua_pcall(L, 1, 0, 0) == 0)
189                         ret = true;
190         }
191
192 done:
193         lua_pushboolean(L, ret);
194         return 1;
195 }
196
197 static int
198 uci_lua_get_any(lua_State *L, bool all)
199 {
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;
205         char *s;
206         int err = UCI_ERR_MEM;
207         int n;
208
209         n = lua_gettop(L);
210
211         luaL_checkstring(L, 1);
212         s = strdup(lua_tostring(L, 1));
213         if (!s)
214                 goto error;
215
216         if (n > 1) {
217                 package = luaL_checkstring(L, 1);
218                 section = luaL_checkstring(L, 2);
219                 if (n > 2)
220                         option = luaL_checkstring(L, 3);
221         } else {
222                 if ((err = uci_parse_tuple(ctx, s, (char **) &package, (char **) &section, (char **) &option, NULL)))
223                         goto error;
224         }
225
226         if (!all && (section == NULL)) {
227                 err = UCI_ERR_INVAL;
228                 goto error;
229         }
230
231         p = find_package(L, package, AUTOLOAD_ON);
232         if (!p) {
233                 err = UCI_ERR_NOTFOUND;
234                 goto error;
235         }
236
237         if (section) {
238                 if ((err = uci_lookup(ctx, &e, p, section, option)))
239                         goto error;
240         } else {
241                 e = &p->e;
242         }
243
244         switch(e->type) {
245                 case UCI_TYPE_PACKAGE:
246                         uci_push_package(L, p);
247                         break;
248                 case UCI_TYPE_SECTION:
249                         if (all)
250                                 uci_push_section(L, uci_to_section(e));
251                         else
252                                 lua_pushstring(L, uci_to_section(e)->type);
253                         break;
254                 case UCI_TYPE_OPTION:
255                         lua_pushstring(L, uci_to_option(e)->value);
256                         break;
257                 default:
258                         err = UCI_ERR_INVAL;
259                         goto error;
260         }
261 error:
262         if (s)
263                 free(s);
264
265         switch(err) {
266         default:
267                 ctx->err = err;
268                 uci_lua_perror(L, "uci.get");
269                 /* fall through */
270         case UCI_ERR_NOTFOUND:
271                 lua_pushnil(L);
272                 /* fall through */
273         case 0:
274                 return 1;
275         }
276 }
277
278 static int
279 uci_lua_get(lua_State *L)
280 {
281         return uci_lua_get_any(L, false);
282 }
283
284 static int
285 uci_lua_get_all(lua_State *L)
286 {
287         return uci_lua_get_any(L, true);
288 }
289
290 static int
291 uci_lua_add(lua_State *L)
292 {
293         struct uci_section *s = NULL;
294         struct uci_package *p;
295         const char *package;
296         const char *type;
297         const char *name = NULL;
298
299         do {
300                 package = luaL_checkstring(L, 1);
301                 type = luaL_checkstring(L, 2);
302                 p = find_package(L, package, AUTOLOAD_ON);
303                 if (!p)
304                         break;
305
306                 if (uci_add_section(ctx, p, type, &s) || !s)
307                         break;
308
309                 name = s->e.name;
310         } while (0);
311
312         lua_pushstring(L, name);
313         return 1;
314 }
315
316 static int
317 uci_lua_delete(lua_State *L)
318 {
319         const char *package = NULL;
320         const char *section = NULL;
321         const char *option = NULL;
322         struct uci_package *p;
323         const char *s;
324         int err = UCI_ERR_MEM;
325         int nargs;
326
327         nargs = lua_gettop(L);
328         s = luaL_checkstring(L, 1);
329         switch(nargs) {
330         case 1:
331                 /* Format: uci.delete("p.s[.o]") */
332                 s = strdup(s);
333                 if (!s)
334                         goto error;
335
336                 if ((err = uci_parse_tuple(ctx, (char *) s, (char **) &package, (char **) &section, (char **) &option, NULL)))
337                         goto error;
338                 break;
339         case 3:
340                 /* Format: uci.delete("p", "s", "o") */
341                 option = luaL_checkstring(L, 3);
342                 /* fall through */
343         case 2:
344                 /* Format: uci.delete("p", "s") */
345                 section = luaL_checkstring(L, 2);
346                 package = s;
347                 break;
348         default:
349                 err = UCI_ERR_INVAL;
350                 goto error;
351         }
352
353         p = find_package(L, package, AUTOLOAD_ON);
354         if (!p) {
355                 err = UCI_ERR_NOTFOUND;
356                 goto error;
357         }
358         err = uci_delete(ctx, p, section, option);
359
360 error:
361         if (err)
362                 uci_lua_perror(L, "uci.set");
363         lua_pushboolean(L, (err == 0));
364         return 1;
365 }
366
367 static int
368 uci_lua_set(lua_State *L)
369 {
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;
375         const char *s;
376         int err = UCI_ERR_MEM;
377         int nargs;
378
379         nargs = lua_gettop(L);
380
381         s = luaL_checkstring(L, 1);
382         switch(nargs) {
383         case 1:
384                 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
385                 s = strdup(s);
386                 if (!s)
387                         goto error;
388
389                 if ((err = uci_parse_tuple(ctx, (char *) s, (char **) &package, (char **) &section, (char **) &option, (char **) &value)))
390                         goto error;
391                 break;
392         case 4:
393                 /* Format: uci.set("p", "s", "o", "v") */
394                 option = luaL_checkstring(L, 3);
395                 /* fall through */
396         case 3:
397                 /* Format: uci.set("p", "s", "v") */
398                 package = s;
399                 section = luaL_checkstring(L, 2);
400                 value = luaL_checkstring(L, nargs);
401                 break;
402         default:
403                 err = UCI_ERR_INVAL;
404                 goto error;
405         }
406
407         if ((section == NULL) || (value == NULL)) {
408                 err = UCI_ERR_INVAL;
409                 goto error;
410         }
411
412         p = find_package(L, package, AUTOLOAD_ON);
413         if (!p) {
414                 err = UCI_ERR_NOTFOUND;
415                 goto error;
416         }
417         err = uci_set(ctx, p, section, option, value, NULL);
418
419 error:
420         if (err)
421                 uci_lua_perror(L, "uci.set");
422         lua_pushboolean(L, (err == 0));
423         return 1;
424 }
425
426 enum pkg_cmd {
427         CMD_SAVE,
428         CMD_COMMIT,
429         CMD_REVERT
430 };
431
432 static int
433 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
434 {
435         struct uci_element *e, *tmp;
436         const char *s = NULL;
437         const char *section = NULL;
438         const char *option = NULL;
439         int failed = 0;
440         int nargs;
441
442         nargs = lua_gettop(L);
443         switch(nargs) {
444         case 3:
445                 if (cmd != CMD_REVERT)
446                         goto err;
447                 luaL_checkstring(L, 1);
448                 option = lua_tostring(L, -1);
449                 lua_pop(L, 1);
450                 /* fall through */
451         case 2:
452                 if (cmd != CMD_REVERT)
453                         goto err;
454                 luaL_checkstring(L, 1);
455                 section = lua_tostring(L, -1);
456                 lua_pop(L, 1);
457                 /* fall through */
458         case 1:
459                 luaL_checkstring(L, 1);
460                 s = lua_tostring(L, -1);
461                 lua_pop(L, 1);
462                 break;
463         case 0:
464                 break;
465         default:
466                 err:
467                 luaL_error(L, "Invalid argument count");
468                 break;
469         }
470
471         uci_foreach_element_safe(&ctx->root, tmp, e) {
472                 struct uci_package *p = uci_to_package(e);
473                 int ret = UCI_ERR_INVAL;
474
475                 if (s && (strcmp(s, e->name) != 0))
476                         continue;
477
478                 switch(cmd) {
479                 case CMD_COMMIT:
480                         ret = uci_commit(ctx, &p, false);
481                         break;
482                 case CMD_SAVE:
483                         ret = uci_save(ctx, p);
484                         break;
485                 case CMD_REVERT:
486                         ret = uci_revert(ctx, &p, section, option);
487                         break;
488                 }
489
490                 if (ret != 0)
491                         failed = 1;
492         }
493
494         lua_pushboolean(L, !failed);
495         return 1;
496 }
497
498 static int
499 uci_lua_save(lua_State *L)
500 {
501         return uci_lua_package_cmd(L, CMD_SAVE);
502 }
503
504 static int
505 uci_lua_commit(lua_State *L)
506 {
507         return uci_lua_package_cmd(L, CMD_COMMIT);
508 }
509
510 static int
511 uci_lua_revert(lua_State *L)
512 {
513         return uci_lua_package_cmd(L, CMD_REVERT);
514 }
515
516 static void
517 uci_lua_add_change(lua_State *L, struct uci_element *e)
518 {
519         struct uci_history *h;
520         const char *name;
521
522         h = uci_to_history(e);
523         if (!h->section)
524                 return;
525
526         lua_getfield(L, -1, h->section);
527         if (lua_isnil(L, -1)) {
528                 lua_pop(L, 1);
529                 lua_newtable(L);
530                 lua_pushvalue(L, -1); /* copy for setfield */
531                 lua_setfield(L, -3, h->section);
532         }
533
534         name = (h->e.name ? h->e.name : ".TYPE");
535         if (h->value)
536                 lua_pushstring(L, h->value);
537         else
538                 lua_pushstring(L, "");
539         lua_setfield(L, -2, name);
540         lua_pop(L, 1);
541 }
542
543 static void
544 uci_lua_changes_pkg(lua_State *L, const char *package)
545 {
546         struct uci_package *p = NULL;
547         struct uci_element *e;
548         bool autoload = false;
549
550         p = find_package(L, package, AUTOLOAD_OFF);
551         if (!p) {
552                 autoload = true;
553                 p = find_package(L, package, AUTOLOAD_FORCE);
554                 if (!p)
555                         return;
556         }
557
558         if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history))
559                 goto done;
560
561         lua_newtable(L);
562         uci_foreach_element(&p->saved_history, e) {
563                 uci_lua_add_change(L, e);
564         }
565         uci_foreach_element(&p->history, e) {
566                 uci_lua_add_change(L, e);
567         }
568         lua_setfield(L, -2, p->e.name);
569
570 done:
571         if (autoload)
572                 uci_unload(ctx, p);
573 }
574
575 static int
576 uci_lua_changes(lua_State *L)
577 {
578         const char *package = NULL;
579         char **config = NULL;
580         int nargs;
581         int i;
582
583         nargs = lua_gettop(L);
584         switch(nargs) {
585         case 1:
586                 package = luaL_checkstring(L, 1);
587         case 0:
588                 break;
589         default:
590                 luaL_error(L, "invalid argument count");
591         }
592
593         lua_newtable(L);
594         if (package) {
595                 uci_lua_changes_pkg(L, package);
596         } else {
597                 if (uci_list_configs(ctx, &config) != 0)
598                         goto done;
599
600                 for(i = 0; config[i] != NULL; i++) {
601                         uci_lua_changes_pkg(L, config[i]);
602                 }
603         }
604
605 done:
606         return 1;
607 }
608
609 static int
610 uci_lua_set_confdir(lua_State *L)
611 {
612         int ret;
613
614         luaL_checkstring(L, 1);
615         ret = uci_set_confdir(ctx, lua_tostring(L, -1));
616         lua_pushboolean(L, (ret == 0));
617         return 1;
618 }
619
620 static int
621 uci_lua_set_savedir(lua_State *L)
622 {
623         int ret;
624
625         luaL_checkstring(L, 1);
626         ret = uci_set_savedir(ctx, lua_tostring(L, -1));
627         lua_pushboolean(L, (ret == 0));
628
629         return 1;
630 }
631
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 },
647         { NULL, NULL },
648 };
649
650
651 int
652 luaopen_uci(lua_State *L)
653 {
654         ctx = uci_alloc_context();
655         if (!ctx)
656                 luaL_error(L, "Cannot allocate UCI context\n");
657         luaL_register(L, MODNAME, uci);
658
659         /* enable autoload by default */
660         lua_getfield(L, LUA_GLOBALSINDEX, "uci");
661         lua_pushboolean(L, 1);
662         lua_setfield(L, -2, "autoload");
663         lua_pop(L, 1);
664
665         return 0;
666 }