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