5a101ad7c57303c04082f8a44b7b4c244a20d77c
[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, true) != UCI_OK)
125                         goto error;
126                 break;
127         case 1:
128                 if (uci_lookup_ptr(ctx, ptr, s, true) != 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, int index)
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         if (index >= 0) {
200                 lua_pushinteger(L, index);
201                 lua_setfield(L, -2, ".index");
202         }
203
204         uci_foreach_element(&s->options, e) {
205                 struct uci_option *o = uci_to_option(e);
206                 uci_push_option(L, o);
207                 lua_setfield(L, -2, o->e.name);
208         }
209 }
210
211 static void
212 uci_push_package(lua_State *L, struct uci_package *p)
213 {
214         struct uci_element *e;
215         int i = 0;
216
217         lua_newtable(L);
218         uci_foreach_element(&p->sections, e) {
219                 uci_push_section(L, uci_to_section(e), i);
220                 lua_setfield(L, -2, e->name);
221                 i++;
222         }
223 }
224
225 static int
226 uci_lua_unload(lua_State *L)
227 {
228         struct uci_context *ctx;
229         struct uci_package *p;
230         const char *s;
231         int offset = 0;
232
233         ctx = find_context(L, &offset);
234         luaL_checkstring(L, 1 + offset);
235         s = lua_tostring(L, 1 + offset);
236         p = find_package(L, ctx, s, false);
237         if (p) {
238                 uci_unload(ctx, p);
239                 return uci_push_status(L, ctx, false);
240         } else {
241                 lua_pushboolean(L, 0);
242         }
243         return 1;
244 }
245
246 static int
247 uci_lua_load(lua_State *L)
248 {
249         struct uci_context *ctx;
250         struct uci_package *p = NULL;
251         const char *s;
252         int offset = 0;
253
254         ctx = find_context(L, &offset);
255         uci_lua_unload(L);
256         lua_pop(L, 1); /* bool ret value of unload */
257         s = lua_tostring(L, -1);
258
259         uci_load(ctx, s, &p);
260         return uci_push_status(L, ctx, false);
261 }
262
263
264 static int
265 uci_lua_foreach(lua_State *L)
266 {
267         struct uci_context *ctx;
268         struct uci_package *p;
269         struct uci_element *e, *tmp;
270         const char *package, *type;
271         bool ret = false;
272         int offset = 0;
273         int i = 0;
274
275         ctx = find_context(L, &offset);
276         package = luaL_checkstring(L, 1 + offset);
277
278         if (lua_isnil(L, 2))
279                 type = NULL;
280         else
281                 type = luaL_checkstring(L, 2 + offset);
282
283         if (!lua_isfunction(L, 3 + offset) || !package)
284                 luaL_error(L, "Invalid argument");
285
286         p = find_package(L, ctx, package, true);
287         if (!p)
288                 goto done;
289
290         uci_foreach_element_safe(&p->sections, tmp, e) {
291                 struct uci_section *s = uci_to_section(e);
292
293                 i++;
294
295                 if (type && (strcmp(s->type, type) != 0))
296                         continue;
297
298                 lua_pushvalue(L, 3 + offset); /* iterator function */
299                 uci_push_section(L, s, i - 1);
300                 if (lua_pcall(L, 1, 1, 0) == 0) {
301                         ret = true;
302                         if (lua_isboolean(L, -1) && !lua_toboolean(L, -1))
303                                 break;
304                 }
305                 else
306                 {
307                         lua_error(L);
308                         break;
309                 }
310         }
311
312 done:
313         lua_pushboolean(L, ret);
314         return 1;
315 }
316
317 static int
318 uci_lua_get_any(lua_State *L, bool all)
319 {
320         struct uci_context *ctx;
321         struct uci_element *e = NULL;
322         struct uci_ptr ptr;
323         int offset = 0;
324         char *s = NULL;
325         int err = UCI_ERR_NOTFOUND;
326
327         ctx = find_context(L, &offset);
328
329         if (lookup_args(L, ctx, offset, &ptr, &s))
330                 goto error;
331
332         uci_lookup_ptr(ctx, &ptr, NULL, true);
333         if (!all && !ptr.s) {
334                 err = UCI_ERR_INVAL;
335                 goto error;
336         }
337         if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
338                 err = UCI_ERR_NOTFOUND;
339                 goto error;
340         }
341
342         err = UCI_OK;
343         e = ptr.last;
344         switch(e->type) {
345                 case UCI_TYPE_PACKAGE:
346                         uci_push_package(L, ptr.p);
347                         break;
348                 case UCI_TYPE_SECTION:
349                         if (all)
350                                 uci_push_section(L, ptr.s, -1);
351                         else
352                                 lua_pushstring(L, ptr.s->type);
353                         break;
354                 case UCI_TYPE_OPTION:
355                         uci_push_option(L, ptr.o);
356                         break;
357                 default:
358                         err = UCI_ERR_INVAL;
359                         goto error;
360         }
361         if (!err)
362                 return 1;
363
364 error:
365         if (s)
366                 free(s);
367
368         lua_pushnil(L);
369         return uci_push_status(L, ctx, true);
370 }
371
372 static int
373 uci_lua_get(lua_State *L)
374 {
375         return uci_lua_get_any(L, false);
376 }
377
378 static int
379 uci_lua_get_all(lua_State *L)
380 {
381         return uci_lua_get_any(L, true);
382 }
383
384 static int
385 uci_lua_add(lua_State *L)
386 {
387         struct uci_context *ctx;
388         struct uci_section *s = NULL;
389         struct uci_package *p;
390         const char *package;
391         const char *type;
392         const char *name = NULL;
393         int offset = 0;
394
395         ctx = find_context(L, &offset);
396         package = luaL_checkstring(L, 1 + offset);
397         type = luaL_checkstring(L, 2 + offset);
398         p = find_package(L, ctx, package, true);
399         if (!p)
400                 goto fail;
401
402         if (uci_add_section(ctx, p, type, &s) || !s)
403                 goto fail;
404
405         name = s->e.name;
406         lua_pushstring(L, name);
407         return 1;
408
409 fail:
410         lua_pushnil(L);
411         return uci_push_status(L, ctx, true);
412 }
413
414 static int
415 uci_lua_delete(lua_State *L)
416 {
417         struct uci_context *ctx;
418         struct uci_ptr ptr;
419         int offset = 0;
420         char *s = NULL;
421         int err = UCI_ERR_NOTFOUND;
422
423         ctx = find_context(L, &offset);
424
425         if (lookup_args(L, ctx, offset, &ptr, &s))
426                 goto error;
427
428         err = uci_delete(ctx, &ptr);
429
430 error:
431         if (s)
432                 free(s);
433         return uci_push_status(L, ctx, false);
434 }
435
436 static int
437 uci_lua_rename(lua_State *L)
438 {
439         struct uci_context *ctx;
440         struct uci_ptr ptr;
441         int err = UCI_ERR_MEM;
442         char *s = NULL;
443         int nargs, offset = 0;
444
445         ctx = find_context(L, &offset);
446         nargs = lua_gettop(L);
447         if (lookup_args(L, ctx, offset, &ptr, &s))
448                 goto error;
449
450         switch(nargs - offset) {
451         case 1:
452                 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
453                 break;
454         case 4:
455                 /* Format: uci.set("p", "s", "o", "v") */
456                 ptr.value = luaL_checkstring(L, nargs);
457                 break;
458         case 3:
459                 /* Format: uci.set("p", "s", "v") */
460                 ptr.value = ptr.option;
461                 ptr.option = NULL;
462                 break;
463         default:
464                 err = UCI_ERR_INVAL;
465                 goto error;
466         }
467
468         err = uci_lookup_ptr(ctx, &ptr, NULL, true);
469         if (err)
470                 goto error;
471
472         if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
473                 err = UCI_ERR_INVAL;
474                 goto error;
475         }
476
477         err = uci_rename(ctx, &ptr);
478         if (err)
479                 goto error;
480
481 error:
482         return uci_push_status(L, ctx, false);
483 }
484
485 static int
486 uci_lua_reorder(lua_State *L)
487 {
488         struct uci_context *ctx;
489         struct uci_ptr ptr;
490         int err = UCI_ERR_MEM;
491         char *s = NULL;
492         int nargs, offset = 0;
493
494         ctx = find_context(L, &offset);
495         nargs = lua_gettop(L);
496         if (lookup_args(L, ctx, offset, &ptr, &s))
497                 goto error;
498
499         switch(nargs - offset) {
500         case 1:
501                 /* Format: uci.set("p.s=v") or uci.set("p.s=v") */
502                 if (ptr.option) {
503                         err = UCI_ERR_INVAL;
504                         goto error;
505                 }
506                 break;
507         case 3:
508                 /* Format: uci.set("p", "s", "v") */
509                 ptr.value = ptr.option;
510                 ptr.option = NULL;
511                 break;
512         default:
513                 err = UCI_ERR_INVAL;
514                 goto error;
515         }
516
517         err = uci_lookup_ptr(ctx, &ptr, NULL, true);
518         if (err)
519                 goto error;
520
521         if ((ptr.s == NULL) || (ptr.value == NULL)) {
522                 err = UCI_ERR_INVAL;
523                 goto error;
524         }
525
526         err = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
527         if (err)
528                 goto error;
529
530 error:
531         return uci_push_status(L, ctx, false);
532 }
533
534
535 static int
536 uci_lua_set(lua_State *L)
537 {
538         struct uci_context *ctx;
539         struct uci_ptr ptr;
540         bool istable = false;
541         int err = UCI_ERR_MEM;
542         char *s = NULL;
543         int i, nargs, offset = 0;
544
545         ctx = find_context(L, &offset);
546         nargs = lua_gettop(L);
547         if (lookup_args(L, ctx, offset, &ptr, &s))
548                 goto error;
549
550         switch(nargs - offset) {
551         case 1:
552                 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
553                 break;
554         case 4:
555                 /* Format: uci.set("p", "s", "o", "v") */
556                 if (lua_istable(L, nargs)) {
557                         if (lua_objlen(L, nargs) < 1)
558                                 luaL_error(L, "Cannot set an uci option to an empty table value");
559                         lua_rawgeti(L, nargs, 1);
560                         ptr.value = luaL_checkstring(L, -1);
561                         lua_pop(L, 1);
562                         istable = true;
563                 } else {
564                         ptr.value = luaL_checkstring(L, nargs);
565                 }
566                 break;
567         case 3:
568                 /* Format: uci.set("p", "s", "v") */
569                 ptr.value = ptr.option;
570                 ptr.option = NULL;
571                 break;
572         default:
573                 err = UCI_ERR_INVAL;
574                 goto error;
575         }
576
577         err = uci_lookup_ptr(ctx, &ptr, NULL, true);
578         if (err)
579                 goto error;
580
581         if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
582                 err = UCI_ERR_INVAL;
583                 goto error;
584         }
585
586         if (istable) {
587                 if (lua_objlen(L, nargs) == 1) {
588                         i = 1;
589                         if (ptr.o)
590                                 err = uci_delete(ctx, &ptr);
591                 } else {
592                         i = 2;
593                         err = uci_set(ctx, &ptr);
594                         if (err)
595                                 goto error;
596                 }
597
598                 for (; i <= lua_objlen(L, nargs); i++) {
599                         lua_rawgeti(L, nargs, i);
600                         ptr.value = luaL_checkstring(L, -1);
601                         err = uci_add_list(ctx, &ptr);
602                         lua_pop(L, 1);
603                         if (err)
604                                 goto error;
605                 }
606         } else {
607                 err = uci_set(ctx, &ptr);
608                 if (err)
609                         goto error;
610         }
611
612
613 error:
614         return uci_push_status(L, ctx, false);
615 }
616
617 enum pkg_cmd {
618         CMD_SAVE,
619         CMD_COMMIT,
620         CMD_REVERT
621 };
622
623 static int
624 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
625 {
626         struct uci_context *ctx;
627         struct uci_element *e, *tmp;
628         struct uci_ptr ptr;
629         char *s = NULL;
630         int failed = 0;
631         int nargs, offset = 0;
632
633         ctx = find_context(L, &offset);
634         nargs = lua_gettop(L);
635         if ((cmd != CMD_REVERT) && (nargs - offset > 1))
636                 goto err;
637
638         if (lookup_args(L, ctx, offset, &ptr, &s))
639                 goto err;
640
641         uci_lookup_ptr(ctx, &ptr, NULL, true);
642
643         uci_foreach_element_safe(&ctx->root, tmp, e) {
644                 struct uci_package *p = uci_to_package(e);
645                 int ret = UCI_ERR_INVAL;
646
647                 if (ptr.p && (ptr.p != p))
648                         continue;
649
650                 ptr.p = p;
651                 switch(cmd) {
652                 case CMD_COMMIT:
653                         ret = uci_commit(ctx, &p, false);
654                         break;
655                 case CMD_SAVE:
656                         ret = uci_save(ctx, p);
657                         break;
658                 case CMD_REVERT:
659                         ret = uci_revert(ctx, &ptr);
660                         break;
661                 }
662
663                 if (ret != 0)
664                         failed = 1;
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                 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 ret, offset = 0;
797
798         ctx = find_context(L, &offset);
799         luaL_checkstring(L, 1 + offset);
800         ret = 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 ret, offset = 0;
817
818         ctx = find_context(L, &offset);
819         luaL_checkstring(L, 1 + offset);
820         ret = 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 ret, 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         ret = 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 ret, offset = 0;
843
844         ctx = find_context(L, &offset);
845         luaL_checkstring(L, 1 + offset);
846         ret = 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                 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                                 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                                 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 }