add rudimentary lua binding for uci (can only do "load" and "get" at the moment and...
[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_load(lua_State *L)
67 {
68         struct uci_package *p;
69         const char *s;
70
71         luaL_checkany(L, 1);
72         s = lua_tostring(L, -1);
73         p = find_package(s);
74         if (p) {
75                 uci_unload(ctx, p);
76                 p = NULL;
77         }
78         if (uci_load(ctx, s, &p)) {
79                 uci_lua_perror(L, "uci.load");
80                 lua_pushboolean(L, 0);
81         } else {
82                 lua_pushboolean(L, 1);
83         }
84
85         return 1;
86 }
87
88 static int
89 uci_lua_get(lua_State *L)
90 {
91         struct uci_lua_context *f;
92         struct uci_element *e = NULL;
93         struct uci_package *p = NULL;
94         char *package = NULL;
95         char *section = NULL;
96         char *option = NULL;
97         char *s;
98         int err = UCI_ERR_MEM;
99
100         luaL_checkany(L, 1);
101         s = strdup(lua_tostring(L, -1));
102         if (!s)
103                 goto error;
104
105         if ((err = uci_parse_tuple(ctx, s, &package, &section, &option, NULL)))
106                 goto error;
107
108         if (section == NULL) {
109                 err = UCI_ERR_INVAL;
110                 goto error;
111         }
112
113         p = find_package(package);
114         if (!p) {
115                 err = UCI_ERR_NOTFOUND;
116                 goto error;
117         }
118
119         if ((err = uci_lookup(ctx, &e, p, section, option)))
120                 goto error;
121
122         switch(e->type) {
123                 case UCI_TYPE_SECTION:
124                         lua_pushstring(L, uci_to_section(e)->type);
125                         break;
126                 case UCI_TYPE_OPTION:
127                         lua_pushstring(L, uci_to_option(e)->value);
128                         break;
129                 default:
130                         err = UCI_ERR_INVAL;
131                         goto error;
132         }
133 error:
134         if (s)
135                 free(s);
136
137         switch(err) {
138         default:
139                 ctx->err = err;
140                 uci_lua_perror(L, "uci.get");
141                 /* fall through */
142         case UCI_ERR_NOTFOUND:
143                 lua_pushnil(L);
144                 /* fall through */
145         case 0:
146                 return 1;
147         }
148 }
149
150
151 static const luaL_Reg uci[] = {
152         { "load", uci_lua_load },
153         { "get", uci_lua_get },
154         { NULL, NULL },
155 };
156
157
158 int
159 luaopen_uci(lua_State *L)
160 {
161         ctx = uci_alloc_context();
162         if (!ctx)
163                 luaL_error(L, "Cannot allocate UCI context\n");
164         luaL_register(L, MODNAME, uci);
165         return 0;
166 }