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