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