add uci.rename()
[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 }
49 if (offset)
50 *offset = 0;
51 return global_ctx;
52 }
53 if (offset)
54 *offset = 1;
55 ctx = luaL_checkudata(L, 1, METANAME);
56 if (!ctx || !*ctx)
57 luaL_error(L, "failed to get UCI context");
58
59 return *ctx;
60 }
61
62 static struct uci_package *
63 find_package(lua_State *L, struct uci_context *ctx, const char *str, bool al)
64 {
65 struct uci_package *p = NULL;
66 struct uci_element *e;
67 char *sep;
68 char *name;
69
70 sep = strchr(str, '.');
71 if (sep) {
72 name = malloc(1 + sep - str);
73 if (!name)
74 luaL_error(L, "out of memory");
75 strncpy(name, str, sep - str);
76 name[sep - str] = 0;
77 } else
78 name = (char *) str;
79
80 uci_foreach_element(&ctx->root, e) {
81 if (strcmp(e->name, name) != 0)
82 continue;
83
84 p = uci_to_package(e);
85 goto done;
86 }
87
88 if (al == true)
89 uci_load(ctx, name, &p);
90 else if (al) {
91 uci_load(ctx, name, &p);
92 }
93
94 done:
95 if (name != str)
96 free(name);
97 return p;
98 }
99
100 static int
101 lookup_args(lua_State *L, struct uci_context *ctx, int offset, struct uci_ptr *ptr, char **buf)
102 {
103 char *s = NULL;
104 int n;
105
106 n = lua_gettop(L);
107 luaL_checkstring(L, 1 + offset);
108 s = strdup(lua_tostring(L, 1 + offset));
109 if (!s)
110 goto error;
111
112 memset(ptr, 0, sizeof(struct uci_ptr));
113 if (!find_package(L, ctx, s, true))
114 goto error;
115
116 switch (n - offset) {
117 case 4:
118 case 3:
119 ptr->option = luaL_checkstring(L, 3 + offset);
120 /* fall through */
121 case 2:
122 ptr->section = luaL_checkstring(L, 2 + offset);
123 ptr->package = luaL_checkstring(L, 1 + offset);
124 if (uci_lookup_ptr(ctx, ptr, NULL, false) != UCI_OK)
125 goto error;
126 break;
127 case 1:
128 if (uci_lookup_ptr(ctx, ptr, s, false) != UCI_OK)
129 goto error;
130 break;
131 default:
132 luaL_error(L, "invalid argument count");
133 goto error;
134 }
135
136 *buf = s;
137 return 0;
138
139 error:
140 if (s)
141 free(s);
142 return 1;
143 }
144
145 static int
146 uci_push_status(lua_State *L, struct uci_context *ctx, bool hasarg)
147 {
148 char *str = NULL;
149
150 if (!hasarg)
151 lua_pushboolean(L, (ctx->err == UCI_OK));
152 if (ctx->err) {
153 uci_get_errorstr(ctx, &str, MODNAME);
154 if (str) {
155 lua_pushstring(L, str);
156 free(str);
157 return 2;
158 }
159 }
160 return 1;
161 }
162
163 static void
164 uci_push_option(lua_State *L, struct uci_option *o)
165 {
166 struct uci_element *e;
167 int i = 0;
168
169 switch(o->type) {
170 case UCI_TYPE_STRING:
171 lua_pushstring(L, o->v.string);
172 break;
173 case UCI_TYPE_LIST:
174 lua_newtable(L);
175 uci_foreach_element(&o->v.list, e) {
176 i++;
177 lua_pushstring(L, e->name);
178 lua_rawseti(L, -2, i);
179 }
180 break;
181 default:
182 lua_pushnil(L);
183 break;
184 }
185 }
186
187 static void
188 uci_push_section(lua_State *L, struct uci_section *s, int index)
189 {
190 struct uci_element *e;
191
192 lua_newtable(L);
193 lua_pushboolean(L, s->anonymous);
194 lua_setfield(L, -2, ".anonymous");
195 lua_pushstring(L, s->type);
196 lua_setfield(L, -2, ".type");
197 lua_pushstring(L, s->e.name);
198 lua_setfield(L, -2, ".name");
199 if (index >= 0) {
200 lua_pushinteger(L, index);
201 lua_setfield(L, -2, ".index");
202 }
203
204 uci_foreach_element(&s->options, e) {
205 struct uci_option *o = uci_to_option(e);
206 uci_push_option(L, o);
207 lua_setfield(L, -2, o->e.name);
208 }
209 }
210
211 static void
212 uci_push_package(lua_State *L, struct uci_package *p)
213 {
214 struct uci_element *e;
215 int i = 0;
216
217 lua_newtable(L);
218 uci_foreach_element(&p->sections, e) {
219 uci_push_section(L, uci_to_section(e), i);
220 lua_setfield(L, -2, e->name);
221 i++;
222 }
223 }
224
225 static int
226 uci_lua_unload(lua_State *L)
227 {
228 struct uci_context *ctx;
229 struct uci_package *p;
230 const char *s;
231 int offset = 0;
232
233 ctx = find_context(L, &offset);
234 luaL_checkstring(L, 1 + offset);
235 s = lua_tostring(L, 1 + offset);
236 p = find_package(L, ctx, s, false);
237 if (p) {
238 uci_unload(ctx, p);
239 return uci_push_status(L, ctx, false);
240 } else {
241 lua_pushboolean(L, 0);
242 }
243 return 1;
244 }
245
246 static int
247 uci_lua_load(lua_State *L)
248 {
249 struct uci_context *ctx;
250 struct uci_package *p = NULL;
251 const char *s;
252 int offset = 0;
253
254 ctx = find_context(L, &offset);
255 uci_lua_unload(L);
256 lua_pop(L, 1); /* bool ret value of unload */
257 s = lua_tostring(L, -1);
258
259 uci_load(ctx, s, &p);
260 return uci_push_status(L, ctx, false);
261 }
262
263
264 static int
265 uci_lua_foreach(lua_State *L)
266 {
267 struct uci_context *ctx;
268 struct uci_package *p;
269 struct uci_element *e;
270 const char *package, *type;
271 bool ret = false;
272 int offset = 0;
273 int i = 0;
274
275 ctx = find_context(L, &offset);
276 package = luaL_checkstring(L, 1 + offset);
277
278 if (lua_isnil(L, 2))
279 type = NULL;
280 else
281 type = luaL_checkstring(L, 2 + offset);
282
283 if (!lua_isfunction(L, 3 + offset) || !package)
284 luaL_error(L, "Invalid argument");
285
286 p = find_package(L, ctx, package, true);
287 if (!p)
288 goto done;
289
290 uci_foreach_element(&p->sections, e) {
291 struct uci_section *s = uci_to_section(e);
292
293 i++;
294
295 if (type && (strcmp(s->type, type) != 0))
296 continue;
297
298 lua_pushvalue(L, 3 + offset); /* iterator function */
299 uci_push_section(L, s, i - 1);
300 if (lua_pcall(L, 1, 0, 0) == 0)
301 ret = true;
302 }
303
304 done:
305 lua_pushboolean(L, ret);
306 return 1;
307 }
308
309 static int
310 uci_lua_get_any(lua_State *L, bool all)
311 {
312 struct uci_context *ctx;
313 struct uci_element *e = NULL;
314 struct uci_ptr ptr;
315 int offset = 0;
316 char *s = NULL;
317 int err = UCI_ERR_NOTFOUND;
318
319 ctx = find_context(L, &offset);
320
321 if (lookup_args(L, ctx, offset, &ptr, &s))
322 goto error;
323
324 uci_lookup_ptr(ctx, &ptr, NULL, false);
325 if (!all && !ptr.s) {
326 err = UCI_ERR_INVAL;
327 goto error;
328 }
329 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
330 err = UCI_ERR_NOTFOUND;
331 goto error;
332 }
333
334 err = UCI_OK;
335 e = ptr.last;
336 switch(e->type) {
337 case UCI_TYPE_PACKAGE:
338 uci_push_package(L, ptr.p);
339 break;
340 case UCI_TYPE_SECTION:
341 if (all)
342 uci_push_section(L, ptr.s, -1);
343 else
344 lua_pushstring(L, ptr.s->type);
345 break;
346 case UCI_TYPE_OPTION:
347 uci_push_option(L, ptr.o);
348 break;
349 default:
350 err = UCI_ERR_INVAL;
351 goto error;
352 }
353 if (!err)
354 return 1;
355
356 error:
357 if (s)
358 free(s);
359
360 lua_pushnil(L);
361 return uci_push_status(L, ctx, true);
362 }
363
364 static int
365 uci_lua_get(lua_State *L)
366 {
367 return uci_lua_get_any(L, false);
368 }
369
370 static int
371 uci_lua_get_all(lua_State *L)
372 {
373 return uci_lua_get_any(L, true);
374 }
375
376 static int
377 uci_lua_add(lua_State *L)
378 {
379 struct uci_context *ctx;
380 struct uci_section *s = NULL;
381 struct uci_package *p;
382 const char *package;
383 const char *type;
384 const char *name = NULL;
385 int offset = 0;
386
387 ctx = find_context(L, &offset);
388 package = luaL_checkstring(L, 1 + offset);
389 type = luaL_checkstring(L, 2 + offset);
390 p = find_package(L, ctx, package, true);
391 if (!p)
392 goto fail;
393
394 if (uci_add_section(ctx, p, type, &s) || !s)
395 goto fail;
396
397 name = s->e.name;
398 lua_pushstring(L, name);
399 return 1;
400
401 fail:
402 lua_pushnil(L);
403 return uci_push_status(L, ctx, true);
404 }
405
406 static int
407 uci_lua_delete(lua_State *L)
408 {
409 struct uci_context *ctx;
410 struct uci_ptr ptr;
411 int offset = 0;
412 char *s = NULL;
413 int err = UCI_ERR_NOTFOUND;
414
415 ctx = find_context(L, &offset);
416
417 if (lookup_args(L, ctx, offset, &ptr, &s))
418 goto error;
419
420 err = uci_delete(ctx, &ptr);
421
422 error:
423 if (s)
424 free(s);
425 return uci_push_status(L, ctx, false);
426 }
427
428 static int
429 uci_lua_rename(lua_State *L)
430 {
431 struct uci_context *ctx;
432 struct uci_ptr ptr;
433 int err = UCI_ERR_MEM;
434 char *s = NULL;
435 int nargs, offset = 0;
436
437 ctx = find_context(L, &offset);
438 nargs = lua_gettop(L);
439 if (lookup_args(L, ctx, offset, &ptr, &s))
440 goto error;
441
442 switch(nargs - offset) {
443 case 1:
444 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
445 break;
446 case 4:
447 /* Format: uci.set("p", "s", "o", "v") */
448 ptr.value = luaL_checkstring(L, nargs);
449 break;
450 case 3:
451 /* Format: uci.set("p", "s", "v") */
452 ptr.value = ptr.option;
453 ptr.option = NULL;
454 break;
455 default:
456 err = UCI_ERR_INVAL;
457 goto error;
458 }
459
460 err = uci_lookup_ptr(ctx, &ptr, NULL, false);
461 if (err)
462 goto error;
463
464 if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
465 err = UCI_ERR_INVAL;
466 goto error;
467 }
468
469 err = uci_rename(ctx, &ptr);
470 if (err)
471 goto error;
472
473 error:
474 return uci_push_status(L, ctx, false);
475 }
476
477
478 static int
479 uci_lua_set(lua_State *L)
480 {
481 struct uci_context *ctx;
482 struct uci_ptr ptr;
483 bool istable = false;
484 int err = UCI_ERR_MEM;
485 char *s = NULL;
486 int i, nargs, offset = 0;
487
488 ctx = find_context(L, &offset);
489 nargs = lua_gettop(L);
490 if (lookup_args(L, ctx, offset, &ptr, &s))
491 goto error;
492
493 switch(nargs - offset) {
494 case 1:
495 /* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
496 break;
497 case 4:
498 /* Format: uci.set("p", "s", "o", "v") */
499 if (lua_istable(L, nargs)) {
500 if (lua_objlen(L, nargs) < 1)
501 luaL_error(L, "Cannot set an uci option to an empty table value");
502 lua_rawgeti(L, nargs, 1);
503 ptr.value = luaL_checkstring(L, -1);
504 lua_pop(L, 1);
505 istable = true;
506 } else {
507 ptr.value = luaL_checkstring(L, nargs);
508 }
509 break;
510 case 3:
511 /* Format: uci.set("p", "s", "v") */
512 ptr.value = ptr.option;
513 ptr.option = NULL;
514 break;
515 default:
516 err = UCI_ERR_INVAL;
517 goto error;
518 }
519
520 err = uci_lookup_ptr(ctx, &ptr, NULL, false);
521 if (err)
522 goto error;
523
524 if (((ptr.s == NULL) && (ptr.option != NULL)) || (ptr.value == NULL)) {
525 err = UCI_ERR_INVAL;
526 goto error;
527 }
528
529 if (istable) {
530 for (i = 1; i <= lua_objlen(L, nargs); i++) {
531 lua_rawgeti(L, nargs, i);
532 ptr.value = luaL_checkstring(L, -1);
533 err = uci_add_list(ctx, &ptr);
534 lua_pop(L, 1);
535 if (err)
536 goto error;
537 }
538 } else {
539 err = uci_set(ctx, &ptr);
540 if (err)
541 goto error;
542 }
543
544
545 error:
546 return uci_push_status(L, ctx, false);
547 }
548
549 enum pkg_cmd {
550 CMD_SAVE,
551 CMD_COMMIT,
552 CMD_REVERT
553 };
554
555 static int
556 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
557 {
558 struct uci_context *ctx;
559 struct uci_element *e, *tmp;
560 struct uci_ptr ptr;
561 char *s = NULL;
562 int failed = 0;
563 int nargs, offset = 0;
564
565 ctx = find_context(L, &offset);
566 nargs = lua_gettop(L);
567 if ((cmd != CMD_REVERT) && (nargs - offset > 1))
568 goto err;
569
570 if (lookup_args(L, ctx, offset, &ptr, &s))
571 goto err;
572
573 uci_lookup_ptr(ctx, &ptr, NULL, false);
574
575 uci_foreach_element_safe(&ctx->root, tmp, e) {
576 struct uci_package *p = uci_to_package(e);
577 int ret = UCI_ERR_INVAL;
578
579 if (ptr.p && (ptr.p != p))
580 continue;
581
582 ptr.p = p;
583 switch(cmd) {
584 case CMD_COMMIT:
585 ret = uci_commit(ctx, &p, false);
586 break;
587 case CMD_SAVE:
588 ret = uci_save(ctx, p);
589 break;
590 case CMD_REVERT:
591 ret = uci_revert(ctx, &ptr);
592 break;
593 }
594
595 if (ret != 0)
596 failed = 1;
597 }
598
599 err:
600 return uci_push_status(L, ctx, false);
601 }
602
603 static int
604 uci_lua_save(lua_State *L)
605 {
606 return uci_lua_package_cmd(L, CMD_SAVE);
607 }
608
609 static int
610 uci_lua_commit(lua_State *L)
611 {
612 return uci_lua_package_cmd(L, CMD_COMMIT);
613 }
614
615 static int
616 uci_lua_revert(lua_State *L)
617 {
618 return uci_lua_package_cmd(L, CMD_REVERT);
619 }
620
621 static void
622 uci_lua_add_change(lua_State *L, struct uci_element *e)
623 {
624 struct uci_history *h;
625 const char *name;
626
627 h = uci_to_history(e);
628 if (!h->section)
629 return;
630
631 lua_getfield(L, -1, h->section);
632 if (lua_isnil(L, -1)) {
633 lua_pop(L, 1);
634 lua_newtable(L);
635 lua_pushvalue(L, -1); /* copy for setfield */
636 lua_setfield(L, -3, h->section);
637 }
638
639 name = (h->e.name ? h->e.name : ".type");
640 if (h->value)
641 lua_pushstring(L, h->value);
642 else
643 lua_pushstring(L, "");
644 lua_setfield(L, -2, name);
645 lua_pop(L, 1);
646 }
647
648 static void
649 uci_lua_changes_pkg(lua_State *L, struct uci_context *ctx, const char *package)
650 {
651 struct uci_package *p = NULL;
652 struct uci_element *e;
653 bool autoload = false;
654
655 p = find_package(L, ctx, package, false);
656 if (!p) {
657 autoload = true;
658 p = find_package(L, ctx, package, true);
659 if (!p)
660 return;
661 }
662
663 if (uci_list_empty(&p->history) && uci_list_empty(&p->saved_history))
664 goto done;
665
666 lua_newtable(L);
667 uci_foreach_element(&p->saved_history, e) {
668 uci_lua_add_change(L, e);
669 }
670 uci_foreach_element(&p->history, e) {
671 uci_lua_add_change(L, e);
672 }
673 lua_setfield(L, -2, p->e.name);
674
675 done:
676 if (autoload)
677 uci_unload(ctx, p);
678 }
679
680 static int
681 uci_lua_changes(lua_State *L)
682 {
683 struct uci_context *ctx;
684 const char *package = NULL;
685 char **config = NULL;
686 int nargs;
687 int i, offset = 0;
688
689 ctx = find_context(L, &offset);
690 nargs = lua_gettop(L);
691 switch(nargs - offset) {
692 case 1:
693 package = luaL_checkstring(L, 1 + offset);
694 case 0:
695 break;
696 default:
697 luaL_error(L, "invalid argument count");
698 }
699
700 lua_newtable(L);
701 if (package) {
702 uci_lua_changes_pkg(L, ctx, package);
703 } else {
704 if (uci_list_configs(ctx, &config) != 0)
705 goto done;
706
707 for(i = 0; config[i] != NULL; i++) {
708 uci_lua_changes_pkg(L, ctx, config[i]);
709 }
710 }
711
712 done:
713 return 1;
714 }
715
716 static int
717 uci_lua_get_confdir(lua_State *L)
718 {
719 struct uci_context *ctx = find_context(L, NULL);
720 lua_pushstring(L, ctx->confdir);
721 return 1;
722 }
723
724 static int
725 uci_lua_set_confdir(lua_State *L)
726 {
727 struct uci_context *ctx;
728 int ret, offset = 0;
729
730 ctx = find_context(L, &offset);
731 luaL_checkstring(L, 1 + offset);
732 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
733 return uci_push_status(L, ctx, false);
734 }
735
736 static int
737 uci_lua_get_savedir(lua_State *L)
738 {
739 struct uci_context *ctx = find_context(L, NULL);
740 lua_pushstring(L, ctx->savedir);
741 return 1;
742 }
743
744 static int
745 uci_lua_add_history(lua_State *L)
746 {
747 struct uci_context *ctx;
748 int ret, offset = 0;
749
750 ctx = find_context(L, &offset);
751 luaL_checkstring(L, 1 + offset);
752 ret = uci_add_history_path(ctx, lua_tostring(L, -1));
753 return uci_push_status(L, ctx, false);
754 }
755
756 static int
757 uci_lua_set_savedir(lua_State *L)
758 {
759 struct uci_context *ctx;
760 int ret, offset = 0;
761
762 ctx = find_context(L, &offset);
763 luaL_checkstring(L, 1 + offset);
764 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
765 return uci_push_status(L, ctx, false);
766 }
767
768 static int
769 uci_lua_gc(lua_State *L)
770 {
771 struct uci_context *ctx = find_context(L, NULL);
772 uci_free_context(ctx);
773 return 0;
774 }
775
776 static int
777 uci_lua_cursor(lua_State *L)
778 {
779 struct uci_context **u;
780 int argc = lua_gettop(L);
781
782 u = lua_newuserdata(L, sizeof(struct uci_context *));
783 luaL_getmetatable(L, METANAME);
784 lua_setmetatable(L, -2);
785
786 *u = uci_alloc_context();
787 if (!*u)
788 luaL_error(L, "Cannot allocate UCI context");
789 switch (argc) {
790 case 2:
791 if (lua_isstring(L, 2) &&
792 (uci_set_savedir(*u, luaL_checkstring(L, 2)) != UCI_OK))
793 luaL_error(L, "Unable to set savedir");
794 /* fall through */
795 case 1:
796 if (lua_isstring(L, 1) &&
797 (uci_set_confdir(*u, luaL_checkstring(L, 1)) != UCI_OK))
798 luaL_error(L, "Unable to set savedir");
799 break;
800 default:
801 break;
802 }
803 return 1;
804 }
805
806 static const luaL_Reg uci[] = {
807 { "__gc", uci_lua_gc },
808 { "cursor", uci_lua_cursor },
809 { "load", uci_lua_load },
810 { "unload", uci_lua_unload },
811 { "get", uci_lua_get },
812 { "get_all", uci_lua_get_all },
813 { "add", uci_lua_add },
814 { "set", uci_lua_set },
815 { "rename", uci_lua_rename },
816 { "save", uci_lua_save },
817 { "delete", uci_lua_delete },
818 { "commit", uci_lua_commit },
819 { "revert", uci_lua_revert },
820 { "changes", uci_lua_changes },
821 { "foreach", uci_lua_foreach },
822 { "add_history", uci_lua_add_history },
823 { "get_confdir", uci_lua_get_confdir },
824 { "set_confdir", uci_lua_set_confdir },
825 { "get_savedir", uci_lua_get_savedir },
826 { "set_savedir", uci_lua_set_savedir },
827 { NULL, NULL },
828 };
829
830
831 int
832 luaopen_uci(lua_State *L)
833 {
834 /* create metatable */
835 luaL_newmetatable(L, METANAME);
836
837 /* metatable.__index = metatable */
838 lua_pushvalue(L, -1);
839 lua_setfield(L, -2, "__index");
840
841 /* fill metatable */
842 luaL_register(L, NULL, uci);
843 lua_pop(L, 1);
844
845 /* create module */
846 luaL_register(L, MODNAME, uci);
847
848 return 0;
849 }