add revert, merge package handler functions
[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 <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <errno.h>
22
23 #include <lauxlib.h>
24 #include <uci.h>
25
26 #define MODNAME        "uci"
27 //#define DEBUG 1
28
29 #ifdef DEBUG
30 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
31 #else
32 #define DPRINTF(...) do {} while (0)
33 #endif
34
35 static struct uci_context *ctx = NULL;
36
37 static struct uci_package *
38 find_package(const char *name)
39 {
40         struct uci_package *p = NULL;
41         struct uci_element *e;
42         uci_foreach_element(&ctx->root, e) {
43                 if (strcmp(e->name, name) != 0)
44                         continue;
45
46                 p = uci_to_package(e);
47                 break;
48         }
49         return p;
50 }
51
52 static void uci_lua_perror(lua_State *L, char *name)
53 {
54         lua_getfield(L, LUA_GLOBALSINDEX, "uci");
55         lua_getfield(L, -1, "warn");
56         if (!lua_isboolean(L, -1))
57                 goto done;
58         if (lua_toboolean(L, -1) != 1)
59                 goto done;
60         uci_perror(ctx, name);
61 done:
62         lua_pop(L, 2);
63 }
64
65 static int
66 uci_lua_unload(lua_State *L)
67 {
68         struct uci_package *p;
69         const char *s;
70
71         luaL_checkstring(L, 1);
72         s = lua_tostring(L, -1);
73         p = find_package(s);
74         if (p) {
75                 uci_unload(ctx, p);
76                 lua_pushboolean(L, 1);
77         } else {
78                 lua_pushboolean(L, 0);
79         }
80         return 1;
81 }
82
83 static int
84 uci_lua_load(lua_State *L)
85 {
86         struct uci_package *p = NULL;
87         const char *s;
88
89         uci_lua_unload(L);
90         lua_pop(L, 1); /* bool ret value of unload */
91         s = lua_tostring(L, -1);
92
93         if (uci_load(ctx, s, &p)) {
94                 uci_lua_perror(L, "uci.load");
95                 lua_pushboolean(L, 0);
96         } else {
97                 lua_pushboolean(L, 1);
98         }
99
100         return 1;
101 }
102
103 static int
104 uci_lua_get(lua_State *L)
105 {
106         struct uci_element *e = NULL;
107         struct uci_package *p = NULL;
108         char *package = NULL;
109         char *section = NULL;
110         char *option = NULL;
111         char *s;
112         int err = UCI_ERR_MEM;
113
114         luaL_checkstring(L, 1);
115         s = strdup(lua_tostring(L, -1));
116         if (!s)
117                 goto error;
118
119         if ((err = uci_parse_tuple(ctx, s, &package, &section, &option, NULL)))
120                 goto error;
121
122         if (section == NULL) {
123                 err = UCI_ERR_INVAL;
124                 goto error;
125         }
126
127         p = find_package(package);
128         if (!p) {
129                 err = UCI_ERR_NOTFOUND;
130                 goto error;
131         }
132
133         if ((err = uci_lookup(ctx, &e, p, section, option)))
134                 goto error;
135
136         switch(e->type) {
137                 case UCI_TYPE_SECTION:
138                         lua_pushstring(L, uci_to_section(e)->type);
139                         break;
140                 case UCI_TYPE_OPTION:
141                         lua_pushstring(L, uci_to_option(e)->value);
142                         break;
143                 default:
144                         err = UCI_ERR_INVAL;
145                         goto error;
146         }
147 error:
148         if (s)
149                 free(s);
150
151         switch(err) {
152         default:
153                 ctx->err = err;
154                 uci_lua_perror(L, "uci.get");
155                 /* fall through */
156         case UCI_ERR_NOTFOUND:
157                 lua_pushnil(L);
158                 /* fall through */
159         case 0:
160                 return 1;
161         }
162 }
163
164
165 static int
166 uci_lua_set(lua_State *L)
167 {
168         struct uci_package *p;
169         char *package = NULL;
170         char *section = NULL;
171         char *option = NULL;
172         char *value = NULL;
173         char *s;
174         int err = UCI_ERR_MEM;
175
176         luaL_checkstring(L, 1);
177         s = strdup(lua_tostring(L, -1));
178         if (!s)
179                 goto error;
180
181         if ((err = uci_parse_tuple(ctx, s, &package, &section, &option, &value)))
182                 goto error;
183
184         if ((section == NULL) || (value == NULL)) {
185                 err = UCI_ERR_INVAL;
186                 goto error;
187         }
188
189         p = find_package(package);
190         if (!p) {
191                 err = UCI_ERR_NOTFOUND;
192                 goto error;
193         }
194         err = uci_set(ctx, p, section, option, value, NULL);
195
196 error:
197         if (err)
198                 uci_lua_perror(L, "uci.set");
199         lua_pushboolean(L, (err == 0));
200         return 1;
201 }
202
203 enum pkg_cmd {
204         CMD_SAVE,
205         CMD_COMMIT,
206         CMD_REVERT
207 };
208
209 static int
210 uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
211 {
212         struct uci_element *e, *tmp;
213         const char *s = NULL;
214         const char *section = NULL;
215         const char *option = NULL;
216         int failed = 0;
217         int nargs;
218
219         nargs = lua_gettop(L);
220         switch(nargs) {
221         case 3:
222                 if (cmd != CMD_REVERT)
223                         goto err;
224                 luaL_checkstring(L, 1);
225                 option = lua_tostring(L, -1);
226                 lua_pop(L, 1);
227                 /* fall through */
228         case 2:
229                 if (cmd != CMD_REVERT)
230                         goto err;
231                 luaL_checkstring(L, 1);
232                 section = lua_tostring(L, -1);
233                 lua_pop(L, 1);
234                 /* fall through */
235         case 1:
236                 luaL_checkstring(L, 1);
237                 s = lua_tostring(L, -1);
238                 lua_pop(L, 1);
239                 break;
240         default:
241                 err:
242                 luaL_error(L, "Invalid argument count");
243                 break;
244         }
245
246         uci_foreach_element_safe(&ctx->root, tmp, e) {
247                 struct uci_package *p = uci_to_package(e);
248                 int ret = UCI_ERR_INVAL;
249
250                 if (s && (strcmp(s, e->name) != 0))
251                         continue;
252
253                 switch(cmd) {
254                 case CMD_COMMIT:
255                         ret = uci_commit(ctx, &p, false);
256                         break;
257                 case CMD_SAVE:
258                         ret = uci_save(ctx, p);
259                         break;
260                 case CMD_REVERT:
261                         ret = uci_revert(ctx, &p, section, option);
262                         break;
263                 }
264
265                 if (ret != 0)
266                         failed = 1;
267         }
268
269         lua_pushboolean(L, !failed);
270         return 1;
271 }
272
273 static int
274 uci_lua_save(lua_State *L)
275 {
276         return uci_lua_package_cmd(L, CMD_SAVE);
277 }
278
279 static int
280 uci_lua_commit(lua_State *L)
281 {
282         return uci_lua_package_cmd(L, CMD_COMMIT);
283 }
284
285 static int
286 uci_lua_revert(lua_State *L)
287 {
288         return uci_lua_package_cmd(L, CMD_REVERT);
289 }
290
291 static int
292 uci_lua_set_confdir(lua_State *L)
293 {
294         int ret;
295
296         luaL_checkstring(L, 1);
297         ret = uci_set_confdir(ctx, lua_tostring(L, -1));
298         lua_pushboolean(L, (ret == 0));
299         return 1;
300 }
301
302 static int
303 uci_lua_set_savedir(lua_State *L)
304 {
305         int ret;
306
307         luaL_checkstring(L, 1);
308         ret = uci_set_savedir(ctx, lua_tostring(L, -1));
309         lua_pushboolean(L, (ret == 0));
310
311         return 1;
312 }
313
314 static const luaL_Reg uci[] = {
315         { "load", uci_lua_load },
316         { "unload", uci_lua_unload },
317         { "get", uci_lua_get },
318         { "set", uci_lua_set },
319         { "save", uci_lua_save },
320         { "commit", uci_lua_commit },
321         { "revert", uci_lua_revert },
322         { "set_confdir", uci_lua_set_confdir },
323         { "set_savedir", uci_lua_set_savedir },
324         { NULL, NULL },
325 };
326
327
328 int
329 luaopen_uci(lua_State *L)
330 {
331         ctx = uci_alloc_context();
332         if (!ctx)
333                 luaL_error(L, "Cannot allocate UCI context\n");
334         luaL_register(L, MODNAME, uci);
335         return 0;
336 }