set, save, commit for the lua plugin
[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_lua_context *f;
107 struct uci_element *e = NULL;
108 struct uci_package *p = NULL;
109 char *package = NULL;
110 char *section = NULL;
111 char *option = NULL;
112 char *s;
113 int err = UCI_ERR_MEM;
114
115 luaL_checkstring(L, 1);
116 s = strdup(lua_tostring(L, -1));
117 if (!s)
118 goto error;
119
120 if ((err = uci_parse_tuple(ctx, s, &package, &section, &option, NULL)))
121 goto error;
122
123 if (section == NULL) {
124 err = UCI_ERR_INVAL;
125 goto error;
126 }
127
128 p = find_package(package);
129 if (!p) {
130 err = UCI_ERR_NOTFOUND;
131 goto error;
132 }
133
134 if ((err = uci_lookup(ctx, &e, p, section, option)))
135 goto error;
136
137 switch(e->type) {
138 case UCI_TYPE_SECTION:
139 lua_pushstring(L, uci_to_section(e)->type);
140 break;
141 case UCI_TYPE_OPTION:
142 lua_pushstring(L, uci_to_option(e)->value);
143 break;
144 default:
145 err = UCI_ERR_INVAL;
146 goto error;
147 }
148 error:
149 if (s)
150 free(s);
151
152 switch(err) {
153 default:
154 ctx->err = err;
155 uci_lua_perror(L, "uci.get");
156 /* fall through */
157 case UCI_ERR_NOTFOUND:
158 lua_pushnil(L);
159 /* fall through */
160 case 0:
161 return 1;
162 }
163 }
164
165
166 static int
167 uci_lua_set(lua_State *L)
168 {
169 struct uci_package *p;
170 char *package = NULL;
171 char *section = NULL;
172 char *option = NULL;
173 char *value = NULL;
174 char *s;
175 int err = UCI_ERR_MEM;
176
177 luaL_checkstring(L, 1);
178 s = strdup(lua_tostring(L, -1));
179 if (!s)
180 goto error;
181
182 if ((err = uci_parse_tuple(ctx, s, &package, &section, &option, &value)))
183 goto error;
184
185 if ((section == NULL) || (value == NULL)) {
186 err = UCI_ERR_INVAL;
187 goto error;
188 }
189
190 p = find_package(package);
191 if (!p) {
192 err = UCI_ERR_NOTFOUND;
193 goto error;
194 }
195 err = uci_set(ctx, p, section, option, value, NULL);
196
197 error:
198 if (err)
199 uci_lua_perror(L, "uci.set");
200 lua_pushboolean(L, (err == 0));
201 return 1;
202 }
203
204 static int
205 uci_lua_commit(lua_State *L)
206 {
207 struct uci_element *e, *tmp;
208 const char *s = NULL;
209 int failed = 0;
210
211 if (!lua_isnoneornil(L, -1)) {
212 luaL_checkstring(L, 1);
213 s = lua_tostring(L, -1);
214 }
215
216 uci_foreach_element_safe(&ctx->root, tmp, e) {
217 struct uci_package *p = uci_to_package(e);
218
219 if (s && (strcmp(s, e->name) != 0))
220 continue;
221
222 if (uci_commit(ctx, &p, false) != 0)
223 failed = 1;
224 }
225 lua_pushboolean(L, !failed);
226 return 1;
227 }
228
229 static int
230 uci_lua_save(lua_State *L)
231 {
232 struct uci_element *e;
233 const char *s = NULL;
234 int failed = 0;
235
236 if (!lua_isnoneornil(L, -1)) {
237 luaL_checkstring(L, 1);
238 s = lua_tostring(L, -1);
239 }
240
241 uci_foreach_element(&ctx->root, e) {
242 if (s && (strcmp(s, e->name) != 0))
243 continue;
244
245 if (uci_save(ctx, uci_to_package(e)) != 0)
246 failed = 1;
247 }
248 lua_pushboolean(L, !failed);
249 return 1;
250 }
251
252 static int
253 uci_lua_set_confdir(lua_State *L)
254 {
255 int ret;
256
257 luaL_checkstring(L, 1);
258 ret = uci_set_confdir(ctx, lua_tostring(L, -1));
259 lua_pushboolean(L, (ret == 0));
260 return 1;
261 }
262
263 static int
264 uci_lua_set_savedir(lua_State *L)
265 {
266 int ret;
267
268 luaL_checkstring(L, 1);
269 ret = uci_set_savedir(ctx, lua_tostring(L, -1));
270 lua_pushboolean(L, (ret == 0));
271
272 return 1;
273 }
274
275 static const luaL_Reg uci[] = {
276 { "load", uci_lua_load },
277 { "unload", uci_lua_unload },
278 { "get", uci_lua_get },
279 { "set", uci_lua_set },
280 { "save", uci_lua_save },
281 { "commit", uci_lua_commit },
282 { "set_confdir", uci_lua_set_confdir },
283 { "set_savedir", uci_lua_set_savedir },
284 { NULL, NULL },
285 };
286
287
288 int
289 luaopen_uci(lua_State *L)
290 {
291 ctx = uci_alloc_context();
292 if (!ctx)
293 luaL_error(L, "Cannot allocate UCI context\n");
294 luaL_register(L, MODNAME, uci);
295 return 0;
296 }