ucimap: free memory allocated for fixups
[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, 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;
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(&p->sections, 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         }
306
307 done:
308         lua_pushboolean(L, ret);
309         return 1;
310 }
311
312 static int
313 uci_lua_get_any(lua_State *L, bool all)
314 {
315         struct uci_context *ctx;
316         struct uci_element *e = NULL;
317         struct uci_ptr ptr;
318         int offset = 0;
319         char *s = NULL;
320         int err = UCI_ERR_NOTFOUND;
321
322         ctx = find_context(L, &offset);
323
324         if (lookup_args(L, ctx, offset, &ptr, &s))
325                 goto error;
326
327         uci_lookup_ptr(ctx, &ptr, NULL, false);
328         if (!all && !ptr.s) {
329                 err = UCI_ERR_INVAL;
330                 goto error;
331         }
332         if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
333                 err = UCI_ERR_NOTFOUND;
334                 goto error;
335         }
336
337         err = UCI_OK;
338         e = ptr.last;
339         switch(e->type) {
340                 case UCI_TYPE_PACKAGE:
341                         uci_push_package(L, ptr.p);
342                         break;
343                 case UCI_TYPE_SECTION:
344                         if (all)
345                                 uci_push_section(L, ptr.s, -1);
346                         else
347                                 lua_pushstring(L, ptr.s->type);
348                         break;
349                 case UCI_TYPE_OPTION:
350                         uci_push_option(L, ptr.o);
351                         break;
352                 default:
353                         err = UCI_ERR_INVAL;
354                         goto error;
355         }
356         if (!err)
357                 return 1;
358
359 error:
360         if (s)
361                 free(s);
362
363         lua_pushnil(L);
364         return uci_push_status(L, ctx, true);
365 }
366
367 static int
368 uci_lua_get(lua_State *L)
369 {
370         return uci_lua_get_any(L, false);
371 }
372
373 static int
374 uci_lua_get_all(lua_State *L)
375 {
376         return uci_lua_get_any(L, true);
377 }
378
379 static int
380 uci_lua_add(lua_State *L)
381 {
382         struct uci_context *ctx;
383         struct uci_section *s = NULL;
384         struct uci_package *p;
385         const char *package;
386         const char *type;
387         const char *name = NULL;
388         int offset = 0;
389
390         ctx = find_context(L, &offset);
391         package = luaL_checkstring(L, 1 + offset);
392         type = luaL_checkstring(L, 2 + offset);
393         p = find_package(L, ctx, package, true);
394         if (!p)
395                 goto fail;
396
397         if (uci_add_section(ctx, p, type, &s) || !s)
398                 goto fail;
399
400         name = s->e.name;
401         lua_pushstring(L, name);
402         return 1;
403
404 fail:
405         lua_pushnil(L);
406         return uci_push_status(L, ctx, true);
407 }
408
409 static int
410 uci_lua_delete(lua_State *L)
411 {
412         struct uci_context *ctx;
413         struct uci_ptr ptr;
414         int offset = 0;
415         char *s = NULL;
416         int err = UCI_ERR_NOTFOUND;
417
418         ctx = find_context(L, &offset);
419
420         if (lookup_args(L, ctx, offset, &ptr, &s))
421                 goto error;
422
423         err = uci_delete(ctx, &ptr);
424
425 error:
426         if (s)
427                 free(s);
428         return uci_push_status(L, ctx, false);
429 }
430
431 static int
432 uci_lua_rename(lua_State *L)
433 {
434         struct uci_context *ctx;
435         struct uci_ptr ptr;
436         int err = UCI_ERR_MEM;
437         char *s = NULL;
438         int nargs, offset = 0;
439
440         ctx = find_context(L, &offset);
441         nargs = lua_gettop(L);
442         if (lookup_args(L, ctx, offset, &ptr, &s))
443                 goto error;
444
445         switch(nargs - offset) {
446         case 1:
447                 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
448                 break;
449         case 4:
450                 /* Format: uci.set("p", "s", "o", "v") */
451                 ptr.value = luaL_checkstring(L, nargs);
452                 break;
453         case 3:
454                 /* Format: uci.set("p", "s", "v") */
455                 ptr.value = ptr.option;
456                 ptr.option = NULL;
457                 break;
458         default:
459                 err = UCI_ERR_INVAL;
460                 goto error;
461         }
462
463         err = uci_lookup_ptr(ctx, &ptr, NULL, false);
464         if (err)
465                 goto error;
466
467         if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
468                 err = UCI_ERR_INVAL;
469                 goto error;
470         }
471
472         err = uci_rename(ctx, &ptr);
473         if (err)
474                 goto error;
475
476 error:
477         return uci_push_status(L, ctx, false);
478 }
479
480 static int
481 uci_lua_reorder(lua_State *L)
482 {
483         struct uci_context *ctx;
484         struct uci_ptr ptr;
485         int err = UCI_ERR_MEM;
486         char *s = NULL;
487         int nargs, offset = 0;
488
489         ctx = find_context(L, &offset);
490         nargs = lua_gettop(L);
491         if (lookup_args(L, ctx, offset, &ptr, &s))
492                 goto error;
493
494         switch(nargs - offset) {
495         case 1:
496                 /* Format: uci.set("p.s=v") or uci.set("p.s=v") */
497                 if (ptr.option) {
498                         err = UCI_ERR_INVAL;
499                         goto error;
500                 }
501                 break;
502         case 3:
503                 /* Format: uci.set("p", "s", "v") */
504                 ptr.value = ptr.option;
505                 ptr.option = NULL;
506                 break;
507         default:
508                 err = UCI_ERR_INVAL;
509                 goto error;
510         }
511
512         err = uci_lookup_ptr(ctx, &ptr, NULL, false);
513         if (err)
514                 goto error;
515
516         if ((ptr.s == NULL) || (ptr.value == NULL)) {
517                 err = UCI_ERR_INVAL;
518                 goto error;
519         }
520
521         err = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
522         if (err)
523                 goto error;
524
525 error:
526         return uci_push_status(L, ctx, false);
527 }
528
529
530 static int
531 uci_lua_set(lua_State *L)
532 {
533         struct uci_context *ctx;
534         struct uci_ptr ptr;
535         bool istable = false;
536         int err = UCI_ERR_MEM;
537         char *s = NULL;
538         int i, nargs, offset = 0;
539
540         ctx = find_context(L, &offset);
541         nargs = lua_gettop(L);
542         if (lookup_args(L, ctx, offset, &ptr, &s))
543                 goto error;
544
545         switch(nargs - offset) {
546         case 1:
547                 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
548                 break;
549         case 4:
550                 /* Format: uci.set("p", "s", "o", "v") */
551                 if (lua_istable(L, nargs)) {
552                         if (lua_objlen(L, nargs) < 1)
553                                 luaL_error(L, "Cannot set an uci option to an empty table value");
554                         lua_rawgeti(L, nargs, 1);
555                         ptr.value = luaL_checkstring(L, -1);
556                         lua_pop(L, 1);
557                         istable = true;
558                 } else {
559                         ptr.value = luaL_checkstring(L, nargs);
560                 }
561                 break;
562         case 3:
563                 /* Format: uci.set("p", "s", "v") */
564                 ptr.value = ptr.option;
565                 ptr.option = NULL;
566                 break;
567         default:
568                 err = UCI_ERR_INVAL;
569                 goto error;
570         }
571
572         err = uci_lookup_ptr(ctx, &ptr, NULL, false);
573         if (err)
574                 goto error;
575
576         if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
577                 err = UCI_ERR_INVAL;
578                 goto error;
579         }
580
581         if (istable) {
582                 if (lua_objlen(L, nargs) == 1) {
583                         i = 1;
584                         if (ptr.o)
585                                 err = uci_delete(ctx, &ptr);
586                 } else {
587                         i = 2;
588                         err = uci_set(ctx, &ptr);
589                         if (err)
590                                 goto error;
591                 }
592
593                 for (; i <= lua_objlen(L, nargs); i++) {
594                         lua_rawgeti(L, nargs, i);
595                         ptr.value = luaL_checkstring(L, -1);
596                         err = uci_add_list(ctx, &ptr);
597                         lua_pop(L, 1);
598                         if (err)
599                                 goto error;
600                 }
601         } else {
602                 err = uci_set(ctx, &ptr);
603                 if (err)
604                         goto error;
605         }
606
607
608 error:
609         return uci_push_status(L, ctx, false);
610 }
611
612 enum pkg_cmd {
613         CMD_SAVE,
614         CMD_COMMIT,
615         CMD_REVERT
616 };
617
618 static int
619 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
620 {
621         struct uci_context *ctx;
622         struct uci_element *e, *tmp;
623         struct uci_ptr ptr;
624         char *s = NULL;
625         int failed = 0;
626         int nargs, offset = 0;
627
628         ctx = find_context(L, &offset);
629         nargs = lua_gettop(L);
630         if ((cmd != CMD_REVERT) && (nargs - offset > 1))
631                 goto err;
632
633         if (lookup_args(L, ctx, offset, &ptr, &s))
634                 goto err;
635
636         uci_lookup_ptr(ctx, &ptr, NULL, false);
637
638         uci_foreach_element_safe(&ctx->root, tmp, e) {
639                 struct uci_package *p = uci_to_package(e);
640                 int ret = UCI_ERR_INVAL;
641
642                 if (ptr.p && (ptr.p != p))
643                         continue;
644
645                 ptr.p = p;
646                 switch(cmd) {
647                 case CMD_COMMIT:
648                         ret = uci_commit(ctx, &p, false);
649                         break;
650                 case CMD_SAVE:
651                         ret = uci_save(ctx, p);
652                         break;
653                 case CMD_REVERT:
654                         ret = uci_revert(ctx, &ptr);
655                         break;
656                 }
657
658                 if (ret != 0)
659                         failed = 1;
660         }
661
662 err:
663         return uci_push_status(L, ctx, false);
664 }
665
666 static int
667 uci_lua_save(lua_State *L)
668 {
669         return uci_lua_package_cmd(L, CMD_SAVE);
670 }
671
672 static int
673 uci_lua_commit(lua_State *L)
674 {
675         return uci_lua_package_cmd(L, CMD_COMMIT);
676 }
677
678 static int
679 uci_lua_revert(lua_State *L)
680 {
681         return uci_lua_package_cmd(L, CMD_REVERT);
682 }
683
684 static void
685 uci_lua_add_change(lua_State *L, struct uci_element *e)
686 {
687         struct uci_history *h;
688         const char *name;
689
690         h = uci_to_history(e);
691         if (!h->section)
692                 return;
693
694         lua_getfield(L, -1, h->section);
695         if (lua_isnil(L, -1)) {
696                 lua_pop(L, 1);
697                 lua_newtable(L);
698                 lua_pushvalue(L, -1); /* copy for setfield */
699                 lua_setfield(L, -3, h->section);
700         }
701
702         name = (h->e.name ? h->e.name : ".type");
703         if (h->value)
704                 lua_pushstring(L, h->value);
705         else
706                 lua_pushstring(L, "");
707         lua_setfield(L, -2, name);
708         lua_pop(L, 1);
709 }
710
711 static void
712 uci_lua_changes_pkg(lua_State *L, struct uci_context *ctx, const char *package)
713 {
714         struct uci_package *p = NULL;
715         struct uci_element *e;
716         bool autoload = false;
717
718         p = find_package(L, ctx, package, false);
719         if (!p) {
720                 autoload = true;
721                 p = find_package(L, ctx, package, true);
722                 if (!p)
723                         return;
724         }
725
726         if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history))
727                 goto done;
728
729         lua_newtable(L);
730         uci_foreach_element(&p->saved_history, e) {
731                 uci_lua_add_change(L, e);
732         }
733         uci_foreach_element(&p->history, e) {
734                 uci_lua_add_change(L, e);
735         }
736         lua_setfield(L, -2, p->e.name);
737
738 done:
739         if (autoload)
740                 uci_unload(ctx, p);
741 }
742
743 static int
744 uci_lua_changes(lua_State *L)
745 {
746         struct uci_context *ctx;
747         const char *package = NULL;
748         char **config = NULL;
749         int nargs;
750         int i, offset = 0;
751
752         ctx = find_context(L, &offset);
753         nargs = lua_gettop(L);
754         switch(nargs - offset) {
755         case 1:
756                 package = luaL_checkstring(L, 1 + offset);
757         case 0:
758                 break;
759         default:
760                 luaL_error(L, "invalid argument count");
761         }
762
763         lua_newtable(L);
764         if (package) {
765                 uci_lua_changes_pkg(L, ctx, package);
766         } else {
767                 if (uci_list_configs(ctx, &config) != 0)
768                         goto done;
769
770                 for(i = 0; config[i] != NULL; i++) {
771                         uci_lua_changes_pkg(L, ctx, config[i]);
772                 }
773         }
774
775 done:
776         return 1;
777 }
778
779 static int
780 uci_lua_get_confdir(lua_State *L)
781 {
782         struct uci_context *ctx = find_context(L, NULL);
783         lua_pushstring(L, ctx->confdir);
784         return 1;
785 }
786
787 static int
788 uci_lua_set_confdir(lua_State *L)
789 {
790         struct uci_context *ctx;
791         int ret, offset = 0;
792
793         ctx = find_context(L, &offset);
794         luaL_checkstring(L, 1 + offset);
795         ret = uci_set_confdir(ctx, lua_tostring(L, -1));
796         return uci_push_status(L, ctx, false);
797 }
798
799 static int
800 uci_lua_get_savedir(lua_State *L)
801 {
802         struct uci_context *ctx = find_context(L, NULL);
803         lua_pushstring(L, ctx->savedir);
804         return 1;
805 }
806
807 static int
808 uci_lua_add_history(lua_State *L)
809 {
810         struct uci_context *ctx;
811         int ret, offset = 0;
812
813         ctx = find_context(L, &offset);
814         luaL_checkstring(L, 1 + offset);
815         ret = uci_add_history_path(ctx, lua_tostring(L, -1));
816         return uci_push_status(L, ctx, false);
817 }
818
819 static int
820 uci_lua_load_plugins(lua_State *L)
821 {
822         struct uci_context *ctx;
823         int ret, offset = 0;
824         const char *str = NULL;
825
826         ctx = find_context(L, &offset);
827         if (lua_isstring(L, -1))
828                 str = lua_tostring(L, -1);
829         ret = uci_load_plugins(ctx, str);
830         return uci_push_status(L, ctx, false);
831 }
832
833 static int
834 uci_lua_set_savedir(lua_State *L)
835 {
836         struct uci_context *ctx;
837         int ret, offset = 0;
838
839         ctx = find_context(L, &offset);
840         luaL_checkstring(L, 1 + offset);
841         ret = uci_set_savedir(ctx, lua_tostring(L, -1));
842         return uci_push_status(L, ctx, false);
843 }
844
845 static int
846 uci_lua_gc(lua_State *L)
847 {
848         struct uci_context *ctx = find_context(L, NULL);
849         uci_free_context(ctx);
850         return 0;
851 }
852
853 static int
854 uci_lua_cursor(lua_State *L)
855 {
856         struct uci_context **u;
857         int argc = lua_gettop(L);
858
859         u = lua_newuserdata(L, sizeof(struct uci_context *));
860         luaL_getmetatable(L, METANAME);
861         lua_setmetatable(L, -2);
862
863         *u = uci_alloc_context();
864         if (!*u)
865                 luaL_error(L, "Cannot allocate UCI context");
866         switch (argc) {
867                 case 2:
868                         if (lua_isstring(L, 2) &&
869                                 (uci_set_savedir(*u, luaL_checkstring(L, 2)) != UCI_OK))
870                                 luaL_error(L, "Unable to set savedir");
871                         /* fall through */
872                 case 1:
873                         if (lua_isstring(L, 1) &&
874                                 (uci_set_confdir(*u, luaL_checkstring(L, 1)) != UCI_OK))
875                                 luaL_error(L, "Unable to set savedir");
876                         break;
877                 default:
878                         break;
879         }
880         return 1;
881 }
882
883 static const luaL_Reg uci[] = {
884         { "__gc", uci_lua_gc },
885         { "cursor", uci_lua_cursor },
886         { "load", uci_lua_load },
887         { "unload", uci_lua_unload },
888         { "get", uci_lua_get },
889         { "get_all", uci_lua_get_all },
890         { "add", uci_lua_add },
891         { "set", uci_lua_set },
892         { "rename", uci_lua_rename },
893         { "save", uci_lua_save },
894         { "delete", uci_lua_delete },
895         { "commit", uci_lua_commit },
896         { "revert", uci_lua_revert },
897         { "reorder", uci_lua_reorder },
898         { "changes", uci_lua_changes },
899         { "foreach", uci_lua_foreach },
900         { "add_history", uci_lua_add_history },
901         { "load_plugins", uci_lua_load_plugins },
902         { "get_confdir", uci_lua_get_confdir },
903         { "set_confdir", uci_lua_set_confdir },
904         { "get_savedir", uci_lua_get_savedir },
905         { "set_savedir", uci_lua_set_savedir },
906         { NULL, NULL },
907 };
908
909
910 int
911 luaopen_uci(lua_State *L)
912 {
913         /* create metatable */
914         luaL_newmetatable(L, METANAME);
915
916         /* metatable.__index = metatable */
917         lua_pushvalue(L, -1);
918         lua_setfield(L, -2, "__index");
919
920         /* fill metatable */
921         luaL_register(L, NULL, uci);
922         lua_pop(L, 1);
923
924         /* create module */
925         luaL_register(L, MODNAME, uci);
926
927         return 0;
928 }