fix 4-arg uci.set()
[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 DEBUG 1
29
30 #ifdef DEBUG
31 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
32 #else
33 #define DPRINTF(...) do {} while (0)
34 #endif
35
36 static struct uci_context *ctx = NULL;
37 enum autoload {
38         AUTOLOAD_OFF = 0,
39         AUTOLOAD_ON = 1,
40         AUTOLOAD_FORCE = 2
41 };
42
43 static struct uci_package *
44 find_package(lua_State *L, const char *str, enum autoload al)
45 {
46         struct uci_package *p = NULL;
47         struct uci_element *e;
48         char *sep;
49         char *name;
50
51         sep = strchr(str, '.');
52         if (sep) {
53                 name = malloc(1 + sep - str);
54                 if (!name)
55                         luaL_error(L, "out of memory");
56                 strncpy(name, str, sep - str);
57                 name[sep - str] = 0;
58         } else
59                 name = (char *) str;
60
61         uci_foreach_element(&ctx->root, e) {
62                 if (strcmp(e->name, name) != 0)
63                         continue;
64
65                 p = uci_to_package(e);
66                 goto done;
67         }
68
69         if (al == AUTOLOAD_FORCE)
70                 uci_load(ctx, name, &p);
71         else if (al) {
72                 do {
73                         lua_getfield(L, LUA_GLOBALSINDEX, "uci");
74                         lua_getfield(L, -1, "autoload");
75                         if (!lua_isboolean(L, -1))
76                                 break;
77
78                         if (!lua_toboolean(L, -1))
79                                 break;
80
81                         uci_load(ctx, name, &p);
82                 } while (0);
83                 lua_pop(L, 2);
84         }
85
86 done:
87         if (name != str)
88                 free(name);
89         return p;
90 }
91
92 static void uci_lua_perror(lua_State *L, char *name)
93 {
94         lua_getfield(L, LUA_GLOBALSINDEX, "uci");
95         lua_getfield(L, -1, "warn");
96         if (!lua_isboolean(L, -1))
97                 goto done;
98         if (lua_toboolean(L, -1) != 1)
99                 goto done;
100         uci_perror(ctx, name);
101 done:
102         lua_pop(L, 2);
103 }
104
105 static int
106 lookup_args(lua_State *L, struct uci_ptr *ptr, char **buf)
107 {
108         char *s = NULL;
109         int n;
110
111         n = lua_gettop(L);
112         luaL_checkstring(L, 1);
113         s = strdup(lua_tostring(L, 1));
114         if (!s)
115                 goto error;
116
117         memset(ptr, 0, sizeof(struct uci_ptr));
118         if (!find_package(L, s, AUTOLOAD_ON))
119                 goto error;
120
121         switch (n) {
122         case 4:
123         case 3:
124                 ptr->option = luaL_checkstring(L, 3);
125                 /* fall through */
126         case 2:
127                 ptr->section = luaL_checkstring(L, 2);
128                 ptr->package = luaL_checkstring(L, 1);
129                 if (uci_lookup_ptr(ctx, ptr, NULL, false) != UCI_OK)
130                         goto error;
131                 break;
132         case 1:
133                 if (uci_lookup_ptr(ctx, ptr, s, false) != UCI_OK)
134                         goto error;
135                 break;
136         default:
137                 luaL_error(L, "invalid argument count");
138                 goto error;
139         }
140
141         *buf = s;
142         return 0;
143
144 error:
145         if (s)
146                 free(s);
147         return 1;
148 }
149
150 static void uci_push_option(lua_State *L, struct uci_option *o)
151 {
152         struct uci_element *e;
153         int i = 0;
154
155         switch(o->type) {
156         case UCI_TYPE_STRING:
157                 lua_pushstring(L, o->v.string);
158                 break;
159         case UCI_TYPE_LIST:
160                 lua_newtable(L);
161                 uci_foreach_element(&o->v.list, e) {
162                         i++;
163                         lua_pushstring(L, e->name);
164                         lua_rawseti(L, -2, i);
165                 }
166                 break;
167         default:
168                 lua_pushnil(L);
169                 break;
170         }
171 }
172
173 static void uci_push_section(lua_State *L, struct uci_section *s)
174 {
175         struct uci_element *e;
176
177         lua_newtable(L);
178         lua_pushstring(L, s->type);
179         lua_setfield(L, -2, ".type");
180         lua_pushstring(L, s->e.name);
181         lua_setfield(L, -2, ".name");
182
183         uci_foreach_element(&s->options, e) {
184                 struct uci_option *o = uci_to_option(e);
185                 uci_push_option(L, o);
186                 lua_setfield(L, -2, o->e.name);
187         }
188 }
189
190 static void uci_push_package(lua_State *L, struct uci_package *p)
191 {
192         struct uci_element *e;
193         int i = 0;
194
195         lua_newtable(L);
196         uci_foreach_element(&p->sections, e) {
197                 i++;
198                 uci_push_section(L, uci_to_section(e));
199                 lua_setfield(L, -2, e->name);
200         }
201 }
202
203 static int
204 uci_lua_unload(lua_State *L)
205 {
206         struct uci_package *p;
207         const char *s;
208
209         luaL_checkstring(L, 1);
210         s = lua_tostring(L, -1);
211         p = find_package(L, s, AUTOLOAD_OFF);
212         if (p) {
213                 uci_unload(ctx, p);
214                 lua_pushboolean(L, 1);
215         } else {
216                 lua_pushboolean(L, 0);
217         }
218         return 1;
219 }
220
221 static int
222 uci_lua_load(lua_State *L)
223 {
224         struct uci_package *p = NULL;
225         const char *s;
226
227         uci_lua_unload(L);
228         lua_pop(L, 1); /* bool ret value of unload */
229         s = lua_tostring(L, -1);
230
231         if (uci_load(ctx, s, &p)) {
232                 uci_lua_perror(L, "uci.load");
233                 lua_pushboolean(L, 0);
234         } else {
235                 lua_pushboolean(L, 1);
236         }
237
238         return 1;
239 }
240
241
242 static int
243 uci_lua_foreach(lua_State *L)
244 {
245         struct uci_package *p;
246         struct uci_element *e;
247         const char *package, *type;
248         bool ret = false;
249
250         package = luaL_checkstring(L, 1);
251
252         if (lua_isnil(L, 2))
253                 type = NULL;
254         else
255                 type = luaL_checkstring(L, 2);
256
257         if (!lua_isfunction(L, 3) || !package)
258                 luaL_error(L, "Invalid argument");
259
260         p = find_package(L, package, AUTOLOAD_ON);
261         if (!p)
262                 goto done;
263
264         uci_foreach_element(&p->sections, e) {
265                 struct uci_section *s = uci_to_section(e);
266
267                 if (type && (strcmp(s->type, type) != 0))
268                         continue;
269
270                 lua_pushvalue(L, 3); /* iterator function */
271                 uci_push_section(L, s);
272                 if (lua_pcall(L, 1, 0, 0) == 0)
273                         ret = true;
274         }
275
276 done:
277         lua_pushboolean(L, ret);
278         return 1;
279 }
280
281 static int
282 uci_lua_get_any(lua_State *L, bool all)
283 {
284         struct uci_element *e = NULL;
285         struct uci_ptr ptr;
286         char *s = NULL;
287         int err = UCI_ERR_NOTFOUND;
288
289         if (lookup_args(L, &ptr, &s))
290                 goto error;
291
292         uci_lookup_ptr(ctx, &ptr, NULL, false);
293         if (!all && !ptr.s) {
294                 err = UCI_ERR_INVAL;
295                 goto error;
296         }
297
298         err = UCI_OK;
299         e = ptr.last;
300         switch(e->type) {
301                 case UCI_TYPE_PACKAGE:
302                         uci_push_package(L, ptr.p);
303                         break;
304                 case UCI_TYPE_SECTION:
305                         if (all)
306                                 uci_push_section(L, ptr.s);
307                         else
308                                 lua_pushstring(L, ptr.s->type);
309                         break;
310                 case UCI_TYPE_OPTION:
311                         uci_push_option(L, ptr.o);
312                         break;
313                 default:
314                         err = UCI_ERR_INVAL;
315                         goto error;
316         }
317 error:
318         if (s)
319                 free(s);
320
321         switch(err) {
322         default:
323                 ctx->err = err;
324                 uci_lua_perror(L, "uci.get");
325                 /* fall through */
326         case UCI_ERR_NOTFOUND:
327                 lua_pushnil(L);
328                 /* fall through */
329         case 0:
330                 return 1;
331         }
332 }
333
334 static int
335 uci_lua_get(lua_State *L)
336 {
337         return uci_lua_get_any(L, false);
338 }
339
340 static int
341 uci_lua_get_all(lua_State *L)
342 {
343         return uci_lua_get_any(L, true);
344 }
345
346 static int
347 uci_lua_add(lua_State *L)
348 {
349         struct uci_section *s = NULL;
350         struct uci_package *p;
351         const char *package;
352         const char *type;
353         const char *name = NULL;
354
355         do {
356                 package = luaL_checkstring(L, 1);
357                 type = luaL_checkstring(L, 2);
358                 p = find_package(L, package, AUTOLOAD_ON);
359                 if (!p)
360                         break;
361
362                 if (uci_add_section(ctx, p, type, &s) || !s)
363                         break;
364
365                 name = s->e.name;
366         } while (0);
367
368         lua_pushstring(L, name);
369         return 1;
370 }
371
372 static int
373 uci_lua_delete(lua_State *L)
374 {
375         struct uci_ptr ptr;
376         char *s = NULL;
377         int err = UCI_ERR_NOTFOUND;
378
379         if (lookup_args(L, &ptr, &s))
380                 goto error;
381
382         err = uci_delete(ctx, &ptr);
383
384 error:
385         if (s)
386                 free(s);
387         if (err)
388                 uci_lua_perror(L, "uci.delete");
389         lua_pushboolean(L, (err == 0));
390         return 1;
391 }
392
393 static int
394 uci_lua_set(lua_State *L)
395 {
396         bool istable = false;
397         struct uci_ptr ptr;
398         int err = UCI_ERR_MEM;
399         char *s = NULL;
400         int i, nargs;
401
402         nargs = lua_gettop(L);
403         if (lookup_args(L, &ptr, &s))
404                 goto error;
405
406         switch(nargs) {
407         case 1:
408                 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
409                 break;
410         case 4:
411                 /* Format: uci.set("p", "s", "o", "v") */
412                 if (lua_istable(L, nargs)) {
413                         if (lua_objlen(L, nargs) < 1)
414                                 luaL_error(L, "Cannot set an uci option to an empty table value");
415                         lua_rawgeti(L, nargs, 1);
416                         ptr.value = luaL_checkstring(L, -1);
417                         lua_pop(L, 1);
418                         istable = true;
419                 } else {
420                         ptr.value = luaL_checkstring(L, nargs);
421                 }
422                 break;
423         case 3:
424                 /* Format: uci.set("p", "s", "v") */
425                 ptr.value = ptr.option;
426                 ptr.option = NULL;
427                 break;
428         default:
429                 err = UCI_ERR_INVAL;
430                 goto error;
431         }
432
433         err = uci_lookup_ptr(ctx, &ptr, NULL, false);
434         if (err)
435                 goto error;
436
437         if ((ptr.s == NULL) || (ptr.value == NULL)) {
438                 err = UCI_ERR_INVAL;
439                 goto error;
440         }
441
442         err = uci_set(ctx, &ptr);
443         if (err)
444                 goto error;
445
446         if (istable) {
447                 for (i = 2; i <= lua_objlen(L, nargs); i++) {
448                         lua_rawgeti(L, nargs, i);
449                         ptr.value = luaL_checkstring(L, -1);
450                         err = uci_add_list(ctx, &ptr);
451                         lua_pop(L, 1);
452                         if (err)
453                                 goto error;
454                 }
455         }
456
457 error:
458         if (err)
459                 uci_lua_perror(L, "uci.set");
460         lua_pushboolean(L, (err == 0));
461         return 1;
462 }
463
464 enum pkg_cmd {
465         CMD_SAVE,
466         CMD_COMMIT,
467         CMD_REVERT
468 };
469
470 static int
471 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
472 {
473         struct uci_element *e, *tmp;
474         struct uci_ptr ptr;
475         char *s = NULL;
476         int failed = 0;
477         int nargs;
478
479         nargs = lua_gettop(L);
480         if ((cmd != CMD_REVERT) && (nargs > 1))
481                 goto err;
482
483         if (lookup_args(L, &ptr, &s))
484                 goto err;
485
486         uci_lookup_ptr(ctx, &ptr, NULL, false);
487
488         uci_foreach_element_safe(&ctx->root, tmp, e) {
489                 struct uci_package *p = uci_to_package(e);
490                 int ret = UCI_ERR_INVAL;
491
492                 if (ptr.p && (ptr.p != p))
493                         continue;
494
495                 ptr.p = p;
496                 switch(cmd) {
497                 case CMD_COMMIT:
498                         ret = uci_commit(ctx, &p, false);
499                         break;
500                 case CMD_SAVE:
501                         ret = uci_save(ctx, p);
502                         break;
503                 case CMD_REVERT:
504                         ret = uci_revert(ctx, &ptr);
505                         break;
506                 }
507
508                 if (ret != 0)
509                         failed = 1;
510         }
511
512 err:
513         lua_pushboolean(L, !failed);
514         return 1;
515 }
516
517 static int
518 uci_lua_save(lua_State *L)
519 {
520         return uci_lua_package_cmd(L, CMD_SAVE);
521 }
522
523 static int
524 uci_lua_commit(lua_State *L)
525 {
526         return uci_lua_package_cmd(L, CMD_COMMIT);
527 }
528
529 static int
530 uci_lua_revert(lua_State *L)
531 {
532         return uci_lua_package_cmd(L, CMD_REVERT);
533 }
534
535 static void
536 uci_lua_add_change(lua_State *L, struct uci_element *e)
537 {
538         struct uci_history *h;
539         const char *name;
540
541         h = uci_to_history(e);
542         if (!h->section)
543                 return;
544
545         lua_getfield(L, -1, h->section);
546         if (lua_isnil(L, -1)) {
547                 lua_pop(L, 1);
548                 lua_newtable(L);
549                 lua_pushvalue(L, -1); /* copy for setfield */
550                 lua_setfield(L, -3, h->section);
551         }
552
553         name = (h->e.name ? h->e.name : ".type");
554         if (h->value)
555                 lua_pushstring(L, h->value);
556         else
557                 lua_pushstring(L, "");
558         lua_setfield(L, -2, name);
559         lua_pop(L, 1);
560 }
561
562 static void
563 uci_lua_changes_pkg(lua_State *L, const char *package)
564 {
565         struct uci_package *p = NULL;
566         struct uci_element *e;
567         bool autoload = false;
568
569         p = find_package(L, package, AUTOLOAD_OFF);
570         if (!p) {
571                 autoload = true;
572                 p = find_package(L, package, AUTOLOAD_FORCE);
573                 if (!p)
574                         return;
575         }
576
577         if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history))
578                 goto done;
579
580         lua_newtable(L);
581         uci_foreach_element(&p->saved_history, e) {
582                 uci_lua_add_change(L, e);
583         }
584         uci_foreach_element(&p->history, e) {
585                 uci_lua_add_change(L, e);
586         }
587         lua_setfield(L, -2, p->e.name);
588
589 done:
590         if (autoload)
591                 uci_unload(ctx, p);
592 }
593
594 static int
595 uci_lua_changes(lua_State *L)
596 {
597         const char *package = NULL;
598         char **config = NULL;
599         int nargs;
600         int i;
601
602         nargs = lua_gettop(L);
603         switch(nargs) {
604         case 1:
605                 package = luaL_checkstring(L, 1);
606         case 0:
607                 break;
608         default:
609                 luaL_error(L, "invalid argument count");
610         }
611
612         lua_newtable(L);
613         if (package) {
614                 uci_lua_changes_pkg(L, package);
615         } else {
616                 if (uci_list_configs(ctx, &config) != 0)
617                         goto done;
618
619                 for(i = 0; config[i] != NULL; i++) {
620                         uci_lua_changes_pkg(L, config[i]);
621                 }
622         }
623
624 done:
625         return 1;
626 }
627
628 static int
629 uci_lua_set_confdir(lua_State *L)
630 {
631         int ret;
632
633         luaL_checkstring(L, 1);
634         ret = uci_set_confdir(ctx, lua_tostring(L, -1));
635         lua_pushboolean(L, (ret == 0));
636         return 1;
637 }
638
639 static int
640 uci_lua_set_savedir(lua_State *L)
641 {
642         int ret;
643
644         luaL_checkstring(L, 1);
645         ret = uci_set_savedir(ctx, lua_tostring(L, -1));
646         lua_pushboolean(L, (ret == 0));
647
648         return 1;
649 }
650
651 static const luaL_Reg uci[] = {
652         { "load", uci_lua_load },
653         { "unload", uci_lua_unload },
654         { "get", uci_lua_get },
655         { "get_all", uci_lua_get_all },
656         { "add", uci_lua_add },
657         { "set", uci_lua_set },
658         { "save", uci_lua_save },
659         { "delete", uci_lua_delete },
660         { "commit", uci_lua_commit },
661         { "revert", uci_lua_revert },
662         { "changes", uci_lua_changes },
663         { "foreach", uci_lua_foreach },
664         { "set_confdir", uci_lua_set_confdir },
665         { "set_savedir", uci_lua_set_savedir },
666         { NULL, NULL },
667 };
668
669
670 int
671 luaopen_uci(lua_State *L)
672 {
673         ctx = uci_alloc_context();
674         if (!ctx)
675                 luaL_error(L, "Cannot allocate UCI context\n");
676         luaL_register(L, MODNAME, uci);
677
678         /* enable autoload by default */
679         lua_getfield(L, LUA_GLOBALSINDEX, "uci");
680         lua_pushboolean(L, 1);
681         lua_setfield(L, -2, "autoload");
682         lua_pop(L, 1);
683
684         return 0;
685 }