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