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