1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
Index: uci.git/lua/uci.c
===================================================================
--- uci.git.orig/lua/uci.c 2008-08-26 12:31:34.000000000 +0200
+++ uci.git/lua/uci.c 2008-08-26 21:09:10.000000000 +0200
@@ -25,6 +25,7 @@
#include <uci.h>
#define MODNAME "uci"
+#define CURSOR_META "uci.cursor.meta"
//#define DEBUG 1
#ifdef DEBUG
@@ -33,7 +34,6 @@
#define DPRINTF(...) do {} while (0)
#endif
-static struct uci_context *ctx = NULL;
enum autoload {
AUTOLOAD_OFF = 0,
AUTOLOAD_ON = 1,
@@ -41,7 +41,7 @@
};
static struct uci_package *
-find_package(lua_State *L, const char *str, enum autoload al)
+find_package(lua_State *L, struct uci_context *ctx, const char *str, enum autoload al)
{
struct uci_package *p = NULL;
struct uci_element *e;
@@ -70,17 +70,8 @@
uci_load(ctx, name, &p);
else if (al) {
do {
- lua_getfield(L, LUA_GLOBALSINDEX, "uci");
- lua_getfield(L, -1, "autoload");
- if (!lua_isboolean(L, -1))
- break;
-
- if (!lua_toboolean(L, -1))
- break;
-
uci_load(ctx, name, &p);
} while (0);
- lua_pop(L, 2);
}
done:
@@ -89,9 +80,9 @@
return p;
}
-static void uci_lua_perror(lua_State *L, char *name)
+static void uci_lua_perror(lua_State *L, struct uci_context *ctx, char *name)
{
- lua_getfield(L, LUA_GLOBALSINDEX, "uci");
+ lua_getfield(L, LUA_GLOBALSINDEX, MODNAME);
lua_getfield(L, -1, "warn");
if (!lua_isboolean(L, -1))
goto done;
@@ -103,33 +94,33 @@
}
static int
-lookup_args(lua_State *L, struct uci_ptr *ptr, char **buf)
+lookup_args(lua_State *L, struct uci_context *ctx, struct uci_ptr *ptr, char **buf)
{
char *s = NULL;
int n;
n = lua_gettop(L);
- luaL_checkstring(L, 1);
- s = strdup(lua_tostring(L, 1));
+ luaL_checkstring(L, 2);
+ s = strdup(lua_tostring(L, 2));
if (!s)
goto error;
memset(ptr, 0, sizeof(struct uci_ptr));
- if (!find_package(L, s, AUTOLOAD_ON))
+ if (!find_package(L, ctx, s, AUTOLOAD_ON))
goto error;
switch (n) {
+ case 5:
case 4:
- case 3:
- ptr->option = luaL_checkstring(L, 3);
+ ptr->option = luaL_checkstring(L, 4);
/* fall through */
- case 2:
- ptr->section = luaL_checkstring(L, 2);
- ptr->package = luaL_checkstring(L, 1);
+ case 3:
+ ptr->section = luaL_checkstring(L, 3);
+ ptr->package = luaL_checkstring(L, 2);
if (uci_lookup_ptr(ctx, ptr, NULL, false) != UCI_OK)
goto error;
break;
- case 1:
+ case 2:
if (uci_lookup_ptr(ctx, ptr, s, false) != UCI_OK)
goto error;
break;
@@ -202,15 +193,70 @@
}
}
+static struct uci_context**
+uci_lua_context(lua_State *L, int index)
+{
+ struct uci_context **u = (struct uci_context **)luaL_checkudata(L, index, CURSOR_META);
+ luaL_argcheck(L, *u, index, "UCI cursor expected");
+ return u;
+}
+
+
+static int
+uci_lua_cursor(lua_State *L)
+{
+ int argc = lua_gettop(L);
+
+ /* create userdata object */
+ struct uci_context **u = (struct uci_context **)lua_newuserdata(L, sizeof(struct uci_context *));
+
+ /* set metatable for userdata */
+ luaL_getmetatable(L, CURSOR_META);
+ lua_setmetatable(L, -2);
+
+ /* initialize context */
+ *u = uci_alloc_context();
+ if (*u == NULL) {
+ luaL_error(L, "Cannot allocate UCI context");
+ }
+
+ if (argc == 2 && lua_isnoneornil(L, 2) == 0) {
+ if (uci_set_savedir(*u, luaL_checkstring(L, 2)) != 0) {
+ luaL_error(L, "Unable to set savedir");
+ }
+ }
+
+ if (argc >= 1 && lua_isnoneornil(L, 1) == 0) {
+ if (uci_set_confdir(*u, luaL_checkstring(L, 1)) != 0) {
+ luaL_error(L, "Unable to set confdir");
+ }
+ }
+
+ return 1;
+}
+
+static int
+uci_lua_gc (lua_State *L) {
+ struct uci_context **u = (struct uci_context **)luaL_checkudata(L, 1, CURSOR_META);
+
+ if (*u) {
+ uci_free_context(*u);
+ *u = NULL;
+ }
+
+ return 0;
+}
+
static int
uci_lua_unload(lua_State *L)
{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
struct uci_package *p;
const char *s;
- luaL_checkstring(L, 1);
+ luaL_checkstring(L, 2);
s = lua_tostring(L, -1);
- p = find_package(L, s, AUTOLOAD_OFF);
+ p = find_package(L, ctx, s, AUTOLOAD_OFF);
if (p) {
uci_unload(ctx, p);
lua_pushboolean(L, 1);
@@ -223,6 +269,7 @@
static int
uci_lua_load(lua_State *L)
{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
struct uci_package *p = NULL;
const char *s;
@@ -231,7 +278,7 @@
s = lua_tostring(L, -1);
if (uci_load(ctx, s, &p)) {
- uci_lua_perror(L, "uci.load");
+ uci_lua_perror(L, ctx, "uci.load");
lua_pushboolean(L, 0);
} else {
lua_pushboolean(L, 1);
@@ -244,22 +291,23 @@
static int
uci_lua_foreach(lua_State *L)
{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
struct uci_package *p;
struct uci_element *e;
const char *package, *type;
bool ret = false;
- package = luaL_checkstring(L, 1);
+ package = luaL_checkstring(L, 2);
- if (lua_isnil(L, 2))
+ if (lua_isnil(L, 3))
type = NULL;
else
- type = luaL_checkstring(L, 2);
+ type = luaL_checkstring(L, 3);
- if (!lua_isfunction(L, 3) || !package)
+ if (!lua_isfunction(L, 4) || !package)
luaL_error(L, "Invalid argument");
- p = find_package(L, package, AUTOLOAD_ON);
+ p = find_package(L, ctx, package, AUTOLOAD_ON);
if (!p)
goto done;
@@ -269,7 +317,7 @@
if (type && (strcmp(s->type, type) != 0))
continue;
- lua_pushvalue(L, 3); /* iterator function */
+ lua_pushvalue(L, 4); /* iterator function */
uci_push_section(L, s);
if (lua_pcall(L, 1, 0, 0) == 0)
ret = true;
@@ -283,12 +331,13 @@
static int
uci_lua_get_any(lua_State *L, bool all)
{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
struct uci_element *e = NULL;
struct uci_ptr ptr;
char *s = NULL;
int err = UCI_ERR_NOTFOUND;
- if (lookup_args(L, &ptr, &s))
+ if (lookup_args(L, ctx, &ptr, &s))
goto error;
uci_lookup_ptr(ctx, &ptr, NULL, false);
@@ -323,7 +372,7 @@
switch(err) {
default:
ctx->err = err;
- uci_lua_perror(L, "uci.get");
+ uci_lua_perror(L, ctx, "uci.get");
/* fall through */
case UCI_ERR_NOTFOUND:
lua_pushnil(L);
@@ -348,6 +397,7 @@
static int
uci_lua_add(lua_State *L)
{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
struct uci_section *s = NULL;
struct uci_package *p;
const char *package;
@@ -355,9 +405,9 @@
const char *name = NULL;
do {
- package = luaL_checkstring(L, 1);
- type = luaL_checkstring(L, 2);
- p = find_package(L, package, AUTOLOAD_ON);
+ package = luaL_checkstring(L, 2);
+ type = luaL_checkstring(L, 3);
+ p = find_package(L, ctx, package, AUTOLOAD_ON);
if (!p)
break;
@@ -374,11 +424,12 @@
static int
uci_lua_delete(lua_State *L)
{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
struct uci_ptr ptr;
char *s = NULL;
int err = UCI_ERR_NOTFOUND;
- if (lookup_args(L, &ptr, &s))
+ if (lookup_args(L, ctx, &ptr, &s))
goto error;
err = uci_delete(ctx, &ptr);
@@ -387,7 +438,7 @@
if (s)
free(s);
if (err)
- uci_lua_perror(L, "uci.delete");
+ uci_lua_perror(L, ctx, "uci.delete");
lua_pushboolean(L, (err == 0));
return 1;
}
@@ -395,6 +446,7 @@
static int
uci_lua_set(lua_State *L)
{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
bool istable = false;
struct uci_ptr ptr;
int err = UCI_ERR_MEM;
@@ -402,14 +454,14 @@
int i, nargs;
nargs = lua_gettop(L);
- if (lookup_args(L, &ptr, &s))
+ if (lookup_args(L, ctx, &ptr, &s))
goto error;
switch(nargs) {
- case 1:
+ case 2:
/* Format: uci.set("p.s.o=v") or uci.set("p.s=v") */
break;
- case 4:
+ case 5:
/* Format: uci.set("p", "s", "o", "v") */
if (lua_istable(L, nargs)) {
if (lua_objlen(L, nargs) < 1)
@@ -422,7 +474,7 @@
ptr.value = luaL_checkstring(L, nargs);
}
break;
- case 3:
+ case 4:
/* Format: uci.set("p", "s", "v") */
ptr.value = ptr.option;
ptr.option = NULL;
@@ -433,17 +485,23 @@
}
err = uci_lookup_ptr(ctx, &ptr, NULL, false);
- if (err)
+ if (err) {
goto error;
+ }
- if ((ptr.s == NULL) || (ptr.value == NULL)) {
+ /* TODO: IMPROVE CHECK
+ * unable to create named section with original check
+ * therefore temporarily added: && (nargs != 4)
+ */
+ if (((ptr.s == NULL) && (nargs != 4)) || (ptr.value == NULL)) {
err = UCI_ERR_INVAL;
goto error;
}
err = uci_set(ctx, &ptr);
- if (err)
+ if (err) {
goto error;
+ }
if (istable) {
for (i = 2; i <= lua_objlen(L, nargs); i++) {
@@ -458,7 +516,7 @@
error:
if (err)
- uci_lua_perror(L, "uci.set");
+ uci_lua_perror(L, ctx, "uci.set");
lua_pushboolean(L, (err == 0));
return 1;
}
@@ -472,6 +530,7 @@
static int
uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
struct uci_element *e, *tmp;
struct uci_ptr ptr;
char *s = NULL;
@@ -479,10 +538,10 @@
int nargs;
nargs = lua_gettop(L);
- if ((cmd != CMD_REVERT) && (nargs > 1))
+ if ((cmd != CMD_REVERT) && (nargs > 2))
goto err;
- if (lookup_args(L, &ptr, &s))
+ if (lookup_args(L, ctx, &ptr, &s))
goto err;
uci_lookup_ptr(ctx, &ptr, NULL, false);
@@ -562,16 +621,16 @@
}
static void
-uci_lua_changes_pkg(lua_State *L, const char *package)
+uci_lua_changes_pkg(lua_State *L, struct uci_context *ctx, const char *package)
{
struct uci_package *p = NULL;
struct uci_element *e;
bool autoload = false;
- p = find_package(L, package, AUTOLOAD_OFF);
+ p = find_package(L, ctx, package, AUTOLOAD_OFF);
if (!p) {
autoload = true;
- p = find_package(L, package, AUTOLOAD_FORCE);
+ p = find_package(L, ctx, package, AUTOLOAD_FORCE);
if (!p)
return;
}
@@ -596,6 +655,7 @@
static int
uci_lua_changes(lua_State *L)
{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
const char *package = NULL;
char **config = NULL;
int nargs;
@@ -603,9 +663,9 @@
nargs = lua_gettop(L);
switch(nargs) {
+ case 2:
+ package = luaL_checkstring(L, 2);
case 1:
- package = luaL_checkstring(L, 1);
- case 0:
break;
default:
luaL_error(L, "invalid argument count");
@@ -613,13 +673,13 @@
lua_newtable(L);
if (package) {
- uci_lua_changes_pkg(L, package);
+ uci_lua_changes_pkg(L, ctx, package);
} else {
if (uci_list_configs(ctx, &config) != 0)
goto done;
for(i = 0; config[i] != NULL; i++) {
- uci_lua_changes_pkg(L, config[i]);
+ uci_lua_changes_pkg(L, ctx, config[i]);
}
}
@@ -628,29 +688,53 @@
}
static int
+uci_lua_get_confdir(lua_State *L)
+{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
+ lua_pushstring(L, ctx->confdir);
+ return 1;
+}
+
+static int
uci_lua_set_confdir(lua_State *L)
{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
int ret;
- luaL_checkstring(L, 1);
+ luaL_checkstring(L, 2);
ret = uci_set_confdir(ctx, lua_tostring(L, -1));
lua_pushboolean(L, (ret == 0));
return 1;
}
static int
+uci_lua_get_savedir(lua_State *L)
+{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
+ lua_pushstring(L, ctx->savedir);
+ return 1;
+}
+
+static int
uci_lua_set_savedir(lua_State *L)
{
+ struct uci_context *ctx = *uci_lua_context(L, 1);
int ret;
- luaL_checkstring(L, 1);
+ luaL_checkstring(L, 2);
ret = uci_set_savedir(ctx, lua_tostring(L, -1));
lua_pushboolean(L, (ret == 0));
return 1;
}
-static const luaL_Reg uci[] = {
+static const luaL_Reg uci_module[] = {
+ { "cursor", uci_lua_cursor },
+ { NULL, NULL },
+};
+
+static const luaL_Reg uci_cursor[] = {
+ { "__gc", uci_lua_gc },
{ "load", uci_lua_load },
{ "unload", uci_lua_unload },
{ "get", uci_lua_get },
@@ -663,25 +747,33 @@
{ "revert", uci_lua_revert },
{ "changes", uci_lua_changes },
{ "foreach", uci_lua_foreach },
+ { "get_confdir", uci_lua_get_confdir },
+ { "get_savedir", uci_lua_get_savedir },
{ "set_confdir", uci_lua_set_confdir },
{ "set_savedir", uci_lua_set_savedir },
{ NULL, NULL },
};
-
int
luaopen_uci(lua_State *L)
{
- ctx = uci_alloc_context();
- if (!ctx)
- luaL_error(L, "Cannot allocate UCI context\n");
- luaL_register(L, MODNAME, uci);
-
- /* enable autoload by default */
- lua_getfield(L, LUA_GLOBALSINDEX, "uci");
- lua_pushboolean(L, 1);
- lua_setfield(L, -2, "autoload");
- lua_pop(L, 1);
+ /* Create metatable */
+ luaL_newmetatable(L, CURSOR_META);
- return 0;
+ /* metatable.__index = metatable */
+ lua_pushstring(L, "__index");
+ lua_pushvalue(L, -2);
+ lua_settable(L, -3);
+
+ /* fill and drop metatable */
+ luaL_register(L, NULL, uci_cursor);
+ lua_pop(L, 1);
+
+ /* register module table */
+ luaL_register(L, MODNAME, uci_module);
+ lua_pushliteral(L, "APIVERSION");
+ lua_pushinteger(L, 2);
+ lua_settable(L, -3);
+
+ return 1;
}
|