push out uci error messages to lua as well
[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 error:
347         if (s)
348                 free(s);
349
350         lua_pushnil(L);
351         return uci_push_status(L, ctx, true);
352 }
353
354 static int
355 uci_lua_get(lua_State *L)
356 {
357         return uci_lua_get_any(L, false);
358 }
359
360 static int
361 uci_lua_get_all(lua_State *L)
362 {
363         return uci_lua_get_any(L, true);
364 }
365
366 static int
367 uci_lua_add(lua_State *L)
368 {
369         struct uci_context *ctx;
370         struct uci_section *s = NULL;
371         struct uci_package *p;
372         const char *package;
373         const char *type;
374         const char *name = NULL;
375         int offset = 0;
376
377         ctx = find_context(L, &offset);
378         package = luaL_checkstring(L, 1 + offset);
379         type = luaL_checkstring(L, 2 + offset);
380         p = find_package(L, ctx, package, true);
381         if (!p)
382                 goto fail;
383
384         if (uci_add_section(ctx, p, type, &s) || !s)
385                 goto fail;
386
387         name = s->e.name;
388         lua_pushstring(L, name);
389         return 1;
390
391 fail:
392         lua_pushnil(L);
393         return uci_push_status(L, ctx, true);
394 }
395
396 static int
397 uci_lua_delete(lua_State *L)
398 {
399         struct uci_context *ctx;
400         struct uci_ptr ptr;
401         int offset = 0;
402         char *s = NULL;
403         int err = UCI_ERR_NOTFOUND;
404
405         ctx = find_context(L, &offset);
406
407         if (lookup_args(L, ctx, offset, &ptr, &s))
408                 goto error;
409
410         err = uci_delete(ctx, &ptr);
411
412 error:
413         if (s)
414                 free(s);
415         return uci_push_status(L, ctx, false);
416 }
417
418 static int
419 uci_lua_set(lua_State *L)
420 {
421         struct uci_context *ctx;
422         struct uci_ptr ptr;
423         bool istable = false;
424         int err = UCI_ERR_MEM;
425         char *s = NULL;
426         int i, nargs, offset = 0;
427
428         ctx = find_context(L, &offset);
429         nargs = lua_gettop(L);
430         if (lookup_args(L, ctx, offset, &ptr, &s))
431                 goto error;
432
433         switch(nargs - offset) {
434         case 1:
435                 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
436                 break;
437         case 4:
438                 /* Format: uci.set("p", "s", "o", "v") */
439                 if (lua_istable(L, nargs)) {
440                         if (lua_objlen(L, nargs) < 1)
441                                 luaL_error(L, "Cannot set an uci option to an empty table value");
442                         lua_rawgeti(L, nargs, 1);
443                         ptr.value = luaL_checkstring(L, -1);
444                         lua_pop(L, 1);
445                         istable = true;
446                 } else {
447                         ptr.value = luaL_checkstring(L, nargs);
448                 }
449                 break;
450         case 3:
451                 /* Format: uci.set("p", "s", "v") */
452                 ptr.value = ptr.option;
453                 ptr.option = NULL;
454                 break;
455         default:
456                 err = UCI_ERR_INVAL;
457                 goto error;
458         }
459
460         err = uci_lookup_ptr(ctx, &ptr, NULL, false);
461         if (err)
462                 goto error;
463
464         if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
465                 err = UCI_ERR_INVAL;
466                 goto error;
467         }
468
469         err = uci_set(ctx, &ptr);
470         if (err)
471                 goto error;
472
473         if (istable) {
474                 for (i = 2; i <= lua_objlen(L, nargs); i++) {
475                         lua_rawgeti(L, nargs, i);
476                         ptr.value = luaL_checkstring(L, -1);
477                         err = uci_add_list(ctx, &ptr);
478                         lua_pop(L, 1);
479                         if (err)
480                                 goto error;
481                 }
482         }
483
484 error:
485         return uci_push_status(L, ctx, false);
486 }
487
488 enum pkg_cmd {
489         CMD_SAVE,
490         CMD_COMMIT,
491         CMD_REVERT
492 };
493
494 static int
495 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
496 {
497         struct uci_context *ctx;
498         struct uci_element *e, *tmp;
499         struct uci_ptr ptr;
500         char *s = NULL;
501         int failed = 0;
502         int nargs, offset = 0;
503
504         ctx = find_context(L, &offset);
505         nargs = lua_gettop(L);
506         if ((cmd != CMD_REVERT) && (nargs > 1))
507                 goto err;
508
509         if (lookup_args(L, ctx, offset, &ptr, &s))
510                 goto err;
511
512         uci_lookup_ptr(ctx, &ptr, NULL, false);
513
514         uci_foreach_element_safe(&ctx->root, tmp, e) {
515                 struct uci_package *p = uci_to_package(e);
516                 int ret = UCI_ERR_INVAL;
517
518                 if (ptr.p && (ptr.p != p))
519                         continue;
520
521                 ptr.p = p;
522                 switch(cmd) {
523                 case CMD_COMMIT:
524                         ret = uci_commit(ctx, &p, false);
525                         break;
526                 case CMD_SAVE:
527                         ret = uci_save(ctx, p);
528                         break;
529                 case CMD_REVERT:
530                         ret = uci_revert(ctx, &ptr);
531                         break;
532                 }
533
534                 if (ret != 0)
535                         failed = 1;
536         }
537
538 err:
539         return uci_push_status(L, ctx, false);
540 }
541
542 static int
543 uci_lua_save(lua_State *L)
544 {
545         return uci_lua_package_cmd(L, CMD_SAVE);
546 }
547
548 static int
549 uci_lua_commit(lua_State *L)
550 {
551         return uci_lua_package_cmd(L, CMD_COMMIT);
552 }
553
554 static int
555 uci_lua_revert(lua_State *L)
556 {
557         return uci_lua_package_cmd(L, CMD_REVERT);
558 }
559
560 static void
561 uci_lua_add_change(lua_State *L, struct uci_element *e)
562 {
563         struct uci_history *h;
564         const char *name;
565
566         h = uci_to_history(e);
567         if (!h->section)
568                 return;
569
570         lua_getfield(L, -1, h->section);
571         if (lua_isnil(L, -1)) {
572                 lua_pop(L, 1);
573                 lua_newtable(L);
574                 lua_pushvalue(L, -1); /* copy for setfield */
575                 lua_setfield(L, -3, h->section);
576         }
577
578         name = (h->e.name ? h->e.name : ".type");
579         if (h->value)
580                 lua_pushstring(L, h->value);
581         else
582                 lua_pushstring(L, "");
583         lua_setfield(L, -2, name);
584         lua_pop(L, 1);
585 }
586
587 static void
588 uci_lua_changes_pkg(lua_State *L, struct uci_context *ctx, const char *package)
589 {
590         struct uci_package *p = NULL;
591         struct uci_element *e;
592         bool autoload = false;
593
594         p = find_package(L, ctx, package, false);
595         if (!p) {
596                 autoload = true;
597                 p = find_package(L, ctx, package, true);
598                 if (!p)
599                         return;
600         }
601
602         if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history))
603                 goto done;
604
605         lua_newtable(L);
606         uci_foreach_element(&p->saved_history, e) {
607                 uci_lua_add_change(L, e);
608         }
609         uci_foreach_element(&p->history, e) {
610                 uci_lua_add_change(L, e);
611         }
612         lua_setfield(L, -2, p->e.name);
613
614 done:
615         if (autoload)
616                 uci_unload(ctx, p);
617 }
618
619 static int
620 uci_lua_changes(lua_State *L)
621 {
622         struct uci_context *ctx;
623         const char *package = NULL;
624         char **config = NULL;
625         int nargs;
626         int i, offset = 0;
627
628         ctx = find_context(L, &offset);
629         nargs = lua_gettop(L);
630         switch(nargs - offset) {
631         case 1:
632                 package = luaL_checkstring(L, 1 + offset);
633         case 0:
634                 break;
635         default:
636                 luaL_error(L, "invalid argument count");
637         }
638
639         lua_newtable(L);
640         if (package) {
641                 uci_lua_changes_pkg(L, ctx, package);
642         } else {
643                 if (uci_list_configs(ctx, &config) != 0)
644                         goto done;
645
646                 for(i = 0; config[i] != NULL; i++) {
647                         uci_lua_changes_pkg(L, ctx, config[i]);
648                 }
649         }
650
651 done:
652         return 1;
653 }
654
655 static int
656 uci_lua_get_confdir(lua_State *L)
657 {
658         struct uci_context *ctx = find_context(L, NULL);
659         lua_pushstring(L, ctx->confdir);
660         return 1;
661 }
662
663 static int
664 uci_lua_set_confdir(lua_State *L)
665 {
666         struct uci_context *ctx;
667         int ret, offset = 0;
668
669         ctx = find_context(L, &offset);
670         luaL_checkstring(L, 1 + offset);
671         ret = uci_set_confdir(ctx, lua_tostring(L, -1));
672         return uci_push_status(L, ctx, false);
673 }
674
675 static int
676 uci_lua_get_savedir(lua_State *L)
677 {
678         struct uci_context *ctx = find_context(L, NULL);
679         lua_pushstring(L, ctx->savedir);
680         return 1;
681 }
682
683 static int
684 uci_lua_set_savedir(lua_State *L)
685 {
686         struct uci_context *ctx;
687         int ret, offset = 0;
688
689         ctx = find_context(L, &offset);
690         luaL_checkstring(L, 1 + offset);
691         ret = uci_set_savedir(ctx, lua_tostring(L, -1));
692         return uci_push_status(L, ctx, false);
693 }
694
695 static int
696 uci_lua_gc(lua_State *L)
697 {
698         struct uci_context *ctx = find_context(L, NULL);
699         uci_free_context(ctx);
700         return 0;
701 }
702
703 static int
704 uci_lua_cursor(lua_State *L)
705 {
706         struct uci_context **u;
707         int argc = lua_gettop(L);
708
709         u = lua_newuserdata(L, sizeof(struct uci_context *));
710         luaL_getmetatable(L, METANAME);
711         lua_setmetatable(L, -2);
712
713         *u = uci_alloc_context();
714         if (!*u)
715                 luaL_error(L, "Cannot allocate UCI context");
716         switch (argc) {
717                 case 2:
718                         if (lua_isstring(L, 2) &&
719                                 (uci_set_savedir(*u, luaL_checkstring(L, 2)) != UCI_OK))
720                                 luaL_error(L, "Unable to set savedir");
721                         /* fall through */
722                 case 1:
723                         if (lua_isstring(L, 1) &&
724                                 (uci_set_confdir(*u, luaL_checkstring(L, 1)) != UCI_OK))
725                                 luaL_error(L, "Unable to set savedir");
726                         break;
727                 default:
728                         break;
729         }
730         return 1;
731 }
732
733 static const luaL_Reg uci[] = {
734         { "__gc", uci_lua_gc },
735         { "cursor", uci_lua_cursor },
736         { "load", uci_lua_load },
737         { "unload", uci_lua_unload },
738         { "get", uci_lua_get },
739         { "get_all", uci_lua_get_all },
740         { "add", uci_lua_add },
741         { "set", uci_lua_set },
742         { "save", uci_lua_save },
743         { "delete", uci_lua_delete },
744         { "commit", uci_lua_commit },
745         { "revert", uci_lua_revert },
746         { "changes", uci_lua_changes },
747         { "foreach", uci_lua_foreach },
748         { "get_confdir", uci_lua_get_confdir },
749         { "set_confdir", uci_lua_set_confdir },
750         { "get_savedir", uci_lua_get_savedir },
751         { "set_savedir", uci_lua_set_savedir },
752         { NULL, NULL },
753 };
754
755
756 int
757 luaopen_uci(lua_State *L)
758 {
759         /* create metatable */
760         luaL_newmetatable(L, METANAME);
761
762         /* metatable.__index = metatable */
763         lua_pushvalue(L, -1);
764         lua_setfield(L, -2, "__index");
765
766         /* fill metatable */
767         luaL_register(L, NULL, uci);
768         lua_pop(L, 1);
769
770         /* create module */
771         luaL_register(L, MODNAME, uci);
772
773         return 0;
774 }