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