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