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