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