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