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