f70ca503b54b70fc179b6ed54e127937ad128679
[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         char *s = NULL;
379         int err = UCI_ERR_NOTFOUND;
380
381         ctx = find_context(L, &offset);
382
383         if (lookup_args(L, ctx, offset, &ptr, &s))
384                 goto error;
385
386         lookup_ptr(ctx, &ptr, NULL, true);
387         if (!all && !ptr.s) {
388                 err = UCI_ERR_INVAL;
389                 goto error;
390         }
391         if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
392                 err = UCI_ERR_NOTFOUND;
393                 goto error;
394         }
395
396         err = UCI_OK;
397         e = ptr.last;
398         switch(e->type) {
399                 case UCI_TYPE_PACKAGE:
400                         uci_push_package(L, ptr.p);
401                         break;
402                 case UCI_TYPE_SECTION:
403                         if (all)
404                                 uci_push_section(L, ptr.s, -1);
405                         else
406                                 lua_pushstring(L, ptr.s->type);
407                         break;
408                 case UCI_TYPE_OPTION:
409                         uci_push_option(L, ptr.o);
410                         break;
411                 default:
412                         err = UCI_ERR_INVAL;
413                         goto error;
414         }
415         if (s)
416                 free(s);
417         if (!err)
418                 return 1;
419
420 error:
421         if (s)
422                 free(s);
423
424         lua_pushnil(L);
425         return uci_push_status(L, ctx, true);
426 }
427
428 static int
429 uci_lua_get(lua_State *L)
430 {
431         return uci_lua_get_any(L, false);
432 }
433
434 static int
435 uci_lua_get_all(lua_State *L)
436 {
437         return uci_lua_get_any(L, true);
438 }
439
440 static int
441 uci_lua_add(lua_State *L)
442 {
443         struct uci_context *ctx;
444         struct uci_section *s = NULL;
445         struct uci_package *p;
446         const char *package;
447         const char *type;
448         const char *name = NULL;
449         int offset = 0;
450
451         ctx = find_context(L, &offset);
452         package = luaL_checkstring(L, 1 + offset);
453         type = luaL_checkstring(L, 2 + offset);
454         p = find_package(L, ctx, package, true);
455         if (!p)
456                 goto fail;
457
458         if (uci_add_section(ctx, p, type, &s) || !s)
459                 goto fail;
460
461         name = s->e.name;
462         lua_pushstring(L, name);
463         return 1;
464
465 fail:
466         lua_pushnil(L);
467         return uci_push_status(L, ctx, true);
468 }
469
470 static int
471 uci_lua_delete(lua_State *L)
472 {
473         struct uci_context *ctx;
474         struct uci_ptr ptr;
475         int offset = 0;
476         char *s = NULL;
477
478         ctx = find_context(L, &offset);
479
480         if (lookup_args(L, ctx, offset, &ptr, &s))
481                 goto error;
482
483         uci_delete(ctx, &ptr);
484
485 error:
486         if (s)
487                 free(s);
488         return uci_push_status(L, ctx, false);
489 }
490
491 static int
492 uci_lua_rename(lua_State *L)
493 {
494         struct uci_context *ctx;
495         struct uci_ptr ptr;
496         int err = UCI_ERR_MEM;
497         char *s = NULL;
498         int nargs, offset = 0;
499
500         ctx = find_context(L, &offset);
501         nargs = lua_gettop(L);
502         if (lookup_args(L, ctx, offset, &ptr, &s))
503                 goto error;
504
505         switch(nargs - offset) {
506         case 1:
507                 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
508                 break;
509         case 4:
510                 /* Format: uci.set("p", "s", "o", "v") */
511                 ptr.value = luaL_checkstring(L, nargs);
512                 break;
513         case 3:
514                 /* Format: uci.set("p", "s", "v") */
515                 ptr.value = ptr.option;
516                 ptr.option = NULL;
517                 break;
518         default:
519                 err = UCI_ERR_INVAL;
520                 goto error;
521         }
522
523         err = lookup_ptr(ctx, &ptr, NULL, true);
524         if (err)
525                 goto error;
526
527         if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
528                 err = UCI_ERR_INVAL;
529                 goto error;
530         }
531
532         err = uci_rename(ctx, &ptr);
533         if (err)
534                 goto error;
535
536 error:
537         if (s)
538                 free(s);
539         return uci_push_status(L, ctx, false);
540 }
541
542 static int
543 uci_lua_reorder(lua_State *L)
544 {
545         struct uci_context *ctx;
546         struct uci_ptr ptr;
547         int err = UCI_ERR_MEM;
548         char *s = NULL;
549         int nargs, offset = 0;
550
551         ctx = find_context(L, &offset);
552         nargs = lua_gettop(L);
553         if (lookup_args(L, ctx, offset, &ptr, &s))
554                 goto error;
555
556         switch(nargs - offset) {
557         case 1:
558                 /* Format: uci.set("p.s=v") or uci.set("p.s=v") */
559                 if (ptr.option) {
560                         err = UCI_ERR_INVAL;
561                         goto error;
562                 }
563                 break;
564         case 3:
565                 /* Format: uci.set("p", "s", "v") */
566                 ptr.value = ptr.option;
567                 ptr.option = NULL;
568                 break;
569         default:
570                 err = UCI_ERR_INVAL;
571                 goto error;
572         }
573
574         err = lookup_ptr(ctx, &ptr, NULL, true);
575         if (err)
576                 goto error;
577
578         if ((ptr.s == NULL) || (ptr.value == NULL)) {
579                 err = UCI_ERR_INVAL;
580                 goto error;
581         }
582
583         err = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
584         if (err)
585                 goto error;
586
587 error:
588         if (s)
589                 free(s);
590         return uci_push_status(L, ctx, false);
591 }
592
593
594 static int
595 uci_lua_set(lua_State *L)
596 {
597         struct uci_context *ctx;
598         struct uci_ptr ptr;
599         bool istable = false;
600         int err = UCI_ERR_MEM;
601         char *s = NULL;
602         const char *v;
603         int i, nargs, offset = 0;
604
605         ctx = find_context(L, &offset);
606         nargs = lua_gettop(L);
607         if (lookup_args(L, ctx, offset, &ptr, &s))
608                 goto error;
609
610         switch(nargs - offset) {
611         case 1:
612                 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
613                 break;
614         case 4:
615                 /* Format: uci.set("p", "s", "o", "v") */
616                 if (lua_istable(L, nargs)) {
617                         if (lua_rawlen(L, nargs) < 1)
618                                 return luaL_error(L, "Cannot set an uci option to an empty table value");
619                         lua_rawgeti(L, nargs, 1);
620                         ptr.value = luaL_checkstring(L, -1);
621                         lua_pop(L, 1);
622                         istable = true;
623                 } else {
624                         ptr.value = luaL_checkstring(L, nargs);
625                 }
626                 break;
627         case 3:
628                 /* Format: uci.set("p", "s", "v") */
629                 ptr.value = ptr.option;
630                 ptr.option = NULL;
631                 break;
632         default:
633                 err = UCI_ERR_INVAL;
634                 goto error;
635         }
636
637         err = lookup_ptr(ctx, &ptr, NULL, true);
638         if (err)
639                 goto error;
640
641         if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
642                 err = UCI_ERR_INVAL;
643                 goto error;
644         }
645
646         if (istable) {
647                 if (lua_rawlen(L, nargs) == 1) {
648                         i = 1;
649                         if (ptr.o) {
650                                 v = ptr.value;
651                                 ptr.value = NULL;
652                                 err = uci_delete(ctx, &ptr);
653                                 if (err)
654                                         goto error;
655                                 ptr.value = v;
656                         }
657                 } else {
658                         i = 2;
659                         err = uci_set(ctx, &ptr);
660                         if (err)
661                                 goto error;
662                 }
663
664                 for (; i <= lua_rawlen(L, nargs); i++) {
665                         lua_rawgeti(L, nargs, i);
666                         ptr.value = luaL_checkstring(L, -1);
667                         err = uci_add_list(ctx, &ptr);
668                         lua_pop(L, 1);
669                         if (err)
670                                 goto error;
671                 }
672         } else {
673                 err = uci_set(ctx, &ptr);
674                 if (err)
675                         goto error;
676         }
677
678
679 error:
680         if (s)
681                 free(s);
682         return uci_push_status(L, ctx, false);
683 }
684
685 enum pkg_cmd {
686         CMD_SAVE,
687         CMD_COMMIT,
688         CMD_REVERT
689 };
690
691 static int
692 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
693 {
694         struct uci_context *ctx;
695         struct uci_element *e, *tmp;
696         struct uci_ptr ptr;
697         char *s = NULL;
698         int nargs, offset = 0;
699
700         ctx = find_context(L, &offset);
701         nargs = lua_gettop(L);
702         if ((cmd != CMD_REVERT) && (nargs - offset > 1))
703                 goto err;
704
705         if (lookup_args(L, ctx, offset, &ptr, &s))
706                 goto err;
707
708         lookup_ptr(ctx, &ptr, NULL, true);
709
710         uci_foreach_element_safe(&ctx->root, tmp, e) {
711                 struct uci_package *p = uci_to_package(e);
712
713                 if (ptr.p && (ptr.p != p))
714                         continue;
715
716                 ptr.p = p;
717                 switch(cmd) {
718                 case CMD_COMMIT:
719                         uci_commit(ctx, &p, false);
720                         break;
721                 case CMD_SAVE:
722                         uci_save(ctx, p);
723                         break;
724                 case CMD_REVERT:
725                         uci_revert(ctx, &ptr);
726                         break;
727                 }
728         }
729
730 err:
731         if (s)
732                 free(s);
733         return uci_push_status(L, ctx, false);
734 }
735
736 static int
737 uci_lua_save(lua_State *L)
738 {
739         return uci_lua_package_cmd(L, CMD_SAVE);
740 }
741
742 static int
743 uci_lua_commit(lua_State *L)
744 {
745         return uci_lua_package_cmd(L, CMD_COMMIT);
746 }
747
748 static int
749 uci_lua_revert(lua_State *L)
750 {
751         return uci_lua_package_cmd(L, CMD_REVERT);
752 }
753
754 static void
755 uci_lua_add_change(lua_State *L, struct uci_element *e)
756 {
757         struct uci_delta *h;
758         const char *name;
759         const char *value;
760
761         h = uci_to_delta(e);
762         if (!h->section)
763                 return;
764
765         lua_getfield(L, -1, h->section);
766         if (lua_isnil(L, -1)) {
767                 lua_pop(L, 1);
768                 lua_newtable(L);
769                 lua_pushvalue(L, -1); /* copy for setfield */
770                 lua_setfield(L, -3, h->section);
771         }
772
773         name = h->e.name;
774         value = h->value ? h->value : "";
775
776         if (name) {
777                 lua_getfield(L, -1, name);
778
779                 /* this delta is a list add operation */
780                 if (h->cmd == UCI_CMD_LIST_ADD) {
781                         /* there seems to be no table yet */
782                         if (!lua_istable(L, -1)) {
783                                 lua_newtable(L);
784
785                                 /* if there is a value on the stack already, add */
786                                 if (!lua_isnil(L, -2)) {
787                                         lua_pushvalue(L, -2);
788                                         lua_rawseti(L, -2, 1);
789                                         lua_pushstring(L, value);
790                                         lua_rawseti(L, -2, 2);
791
792                                 /* this is the first table item */
793                                 } else {
794                                         lua_pushstring(L, value);
795                                         lua_rawseti(L, -2, 1);
796                                 }
797
798                                 lua_setfield(L, -3, name);
799
800                         /* a table is on the top of the stack and this is a subsequent,
801                          * list_add, append this value to table */
802                         } else {
803                                 lua_pushstring(L, value);
804                                 lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
805                         }
806
807                 /* non-list change, simply set/replace field */
808                 } else {
809                         lua_pushstring(L, value);
810                         lua_setfield(L, -3, name);
811                 }
812
813                 lua_pop(L, 1);
814         } else {
815                 lua_pushstring(L, value);
816                 lua_setfield(L, -2, ".type");
817         }
818
819         lua_pop(L, 1);
820 }
821
822 static void
823 uci_lua_changes_pkg(lua_State *L, struct uci_context *ctx, const char *package)
824 {
825         struct uci_package *p = NULL;
826         struct uci_element *e;
827         bool autoload = false;
828
829         p = find_package(L, ctx, package, false);
830         if (!p) {
831                 autoload = true;
832                 p = find_package(L, ctx, package, true);
833                 if (!p)
834                         return;
835         }
836
837         if (uci_list_empty(&p->delta) && uci_list_empty(&p->saved_delta))
838                 goto done;
839
840         lua_newtable(L);
841         uci_foreach_element(&p->saved_delta, e) {
842                 uci_lua_add_change(L, e);
843         }
844         uci_foreach_element(&p->delta, e) {
845                 uci_lua_add_change(L, e);
846         }
847         lua_setfield(L, -2, p->e.name);
848
849 done:
850         if (autoload)
851                 uci_unload(ctx, p);
852 }
853
854 static int
855 uci_lua_changes(lua_State *L)
856 {
857         struct uci_context *ctx;
858         const char *package = NULL;
859         char **config = NULL;
860         int nargs;
861         int i, offset = 0;
862
863         ctx = find_context(L, &offset);
864         nargs = lua_gettop(L);
865         switch(nargs - offset) {
866         case 1:
867                 package = luaL_checkstring(L, 1 + offset);
868         case 0:
869                 break;
870         default:
871                 return luaL_error(L, "invalid argument count");
872         }
873
874         lua_newtable(L);
875         if (package) {
876                 uci_lua_changes_pkg(L, ctx, package);
877         } else {
878                 if (uci_list_configs(ctx, &config) != 0)
879                         goto done;
880
881                 for(i = 0; config[i] != NULL; i++) {
882                         uci_lua_changes_pkg(L, ctx, config[i]);
883                 }
884         }
885
886 done:
887         return 1;
888 }
889
890 static int
891 uci_lua_get_confdir(lua_State *L)
892 {
893         struct uci_context *ctx = find_context(L, NULL);
894         lua_pushstring(L, ctx->confdir);
895         return 1;
896 }
897
898 static int
899 uci_lua_set_confdir(lua_State *L)
900 {
901         struct uci_context *ctx;
902         int offset = 0;
903
904         ctx = find_context(L, &offset);
905         luaL_checkstring(L, 1 + offset);
906         uci_set_confdir(ctx, lua_tostring(L, -1));
907         return uci_push_status(L, ctx, false);
908 }
909
910 static int
911 uci_lua_get_savedir(lua_State *L)
912 {
913         struct uci_context *ctx = find_context(L, NULL);
914         lua_pushstring(L, ctx->savedir);
915         return 1;
916 }
917
918 static int
919 uci_lua_add_delta(lua_State *L)
920 {
921         struct uci_context *ctx;
922         int offset = 0;
923
924         ctx = find_context(L, &offset);
925         luaL_checkstring(L, 1 + offset);
926         uci_add_delta_path(ctx, lua_tostring(L, -1));
927         return uci_push_status(L, ctx, false);
928 }
929
930 static int
931 uci_lua_set_savedir(lua_State *L)
932 {
933         struct uci_context *ctx;
934         int offset = 0;
935
936         ctx = find_context(L, &offset);
937         luaL_checkstring(L, 1 + offset);
938         uci_set_savedir(ctx, lua_tostring(L, -1));
939         return uci_push_status(L, ctx, false);
940 }
941
942 static int
943 uci_lua_list_configs(lua_State *L)
944 {
945         struct uci_context *ctx;
946         char **configs = NULL;
947         char **ptr;
948         int i = 1;
949
950         ctx = find_context(L, NULL);
951         if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs)
952                 return uci_push_status(L, ctx, false);
953         lua_newtable(L);
954         for (ptr = configs; *ptr; ptr++) {
955                 lua_pushstring(L, *ptr);
956                 lua_rawseti(L, -2, i++);
957         }
958         free(configs);
959         return 1;
960 }
961
962 static int
963 uci_lua_gc(lua_State *L)
964 {
965         struct uci_context **ctx;
966
967         if (!lua_isuserdata(L, 1)) {
968                 if (!global_ctx)
969                         return 0;
970                 ctx = &global_ctx;
971         } else {
972                 ctx = luaL_checkudata(L, 1, METANAME);
973                 if (!*ctx)
974                         return 0;
975         }
976         uci_free_context(*ctx);
977         *ctx = NULL;
978         return 0;
979 }
980
981 static int
982 uci_lua_cursor(lua_State *L)
983 {
984         struct uci_context **u;
985         int argc = lua_gettop(L);
986
987         u = lua_newuserdata(L, sizeof(struct uci_context *));
988         luaL_getmetatable(L, METANAME);
989         lua_setmetatable(L, -2);
990
991         *u = uci_alloc_context();
992         if (!*u)
993                 return luaL_error(L, "Cannot allocate UCI context");
994         switch (argc) {
995                 case 2:
996                         if (lua_isstring(L, 2) &&
997                                 (uci_set_savedir(*u, luaL_checkstring(L, 2)) != UCI_OK))
998                                 return luaL_error(L, "Unable to set savedir");
999                         /* fall through */
1000                 case 1:
1001                         if (lua_isstring(L, 1) &&
1002                                 (uci_set_confdir(*u, luaL_checkstring(L, 1)) != UCI_OK))
1003                                 return luaL_error(L, "Unable to set savedir");
1004                         break;
1005                 default:
1006                         break;
1007         }
1008         return 1;
1009 }
1010
1011 static const luaL_Reg uci[] = {
1012         { "__gc", uci_lua_gc },
1013         { "close", uci_lua_gc },
1014         { "cursor", uci_lua_cursor },
1015         { "load", uci_lua_load },
1016         { "unload", uci_lua_unload },
1017         { "get", uci_lua_get },
1018         { "get_all", uci_lua_get_all },
1019         { "add", uci_lua_add },
1020         { "set", uci_lua_set },
1021         { "rename", uci_lua_rename },
1022         { "save", uci_lua_save },
1023         { "delete", uci_lua_delete },
1024         { "commit", uci_lua_commit },
1025         { "revert", uci_lua_revert },
1026         { "reorder", uci_lua_reorder },
1027         { "changes", uci_lua_changes },
1028         { "foreach", uci_lua_foreach },
1029         { "add_history", uci_lua_add_delta },
1030         { "add_delta", uci_lua_add_delta },
1031         { "get_confdir", uci_lua_get_confdir },
1032         { "set_confdir", uci_lua_set_confdir },
1033         { "get_savedir", uci_lua_get_savedir },
1034         { "set_savedir", uci_lua_set_savedir },
1035         { "list_configs", uci_lua_list_configs },
1036         { NULL, NULL },
1037 };
1038
1039
1040 int
1041 luaopen_uci(lua_State *L)
1042 {
1043         /* create metatable */
1044         luaL_newmetatable(L, METANAME);
1045
1046         /* metatable.__index = metatable */
1047         lua_pushvalue(L, -1);
1048         lua_setfield(L, -2, "__index");
1049
1050         /* fill metatable */
1051         luaL_setfuncs(L, uci, 0);
1052         lua_pop(L, 1);
1053
1054         /* create module */
1055         lua_newtable(L);
1056         lua_pushvalue(L, -1);
1057         luaL_setfuncs(L, uci, 0);
1058         lua_setglobal(L, MODNAME);
1059
1060         return 1;
1061 }