2 * LuCI Core - Utility library
3 * Copyright (C) 2008 Steven Barth <steven@midlink.org>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #define LUCI_MODNAME "luci.cutil"
21 #define LUCI_MODDESC "LuCI Core Utility Library"
22 #define LUCI_MODCOPY "2008 Steven Barth"
25 /* Pythonic overloaded MOD operator */
26 static int luci__string_mod(lua_State *L) {
29 luaL_checkstring(L, 1);
32 /* Discard further arguments */
35 /* Get format and push it to the bottom of the stack */
36 lua_getfield(L, 1, "format");
39 /* If second argument is a table, unpack it */
40 if (lua_istable(L, 3)) {
43 luaL_checkstack(L, n, "too many results to unpack");
44 for (i=1; i<=n; i++) {
57 /* Instantiate a class */
58 static int luci__instantiate(lua_State *L) {
59 luaL_checktype(L, 1, LUA_TTABLE);
61 /* Create the object */
64 /* Create the metatable */
65 lua_createtable(L, 0, 1);
67 lua_setfield(L, -2, "__index");
68 lua_setmetatable(L, -2);
70 /* Move instance at the bottom of the stack */
73 /* Invoke constructor if it exists */
74 lua_getfield(L, 1, "__init__");
75 if (lua_isfunction(L, -1)) {
76 /* Put instance at the bottom for the 2nd time */
80 /* Call constructor */
82 lua_call(L, lua_gettop(L)-2, 0);
90 /* luci.cutil.class(baseclass) */
91 static int luci_class(lua_State *L) {
95 /* Create metatable and register parent class if any */
96 if (lua_gettop(L) > 1 && lua_istable(L, 1)) {
97 lua_createtable(L, 0, 2);
99 lua_setfield(L, -2, "__index");
101 lua_createtable(L, 0, 1);
104 /* Set instantiator */
105 lua_pushcfunction(L, luci__instantiate);
106 lua_setfield(L, -2, "__call");
108 lua_setmetatable(L, -2);
112 /* luci.cutil.instanceof(object, class) */
113 static int luci_instanceof(lua_State *L) {
119 if (lua_getmetatable(L, 1)) {
120 /* get parent class */
121 lua_getfield(L, -1, "__index");
122 while (lua_istable(L, -1)) {
123 /* parent class == class */
124 if (lua_equal(L, -1, 2)) {
129 /* remove last metatable */
132 /* get metatable of parent class */
133 if (lua_getmetatable(L, -1)) {
134 /* remove last parent class */
137 /* get next parent class */
138 lua_getfield(L, -1, "__index");
145 lua_pushboolean(L, stat);
150 /* luci.cutil.pcdata(obj) */
151 static int luci_pcdata(lua_State *L) {
152 if (lua_isnone(L, 1)) {
156 luaL_checkstring(L, 1);
158 /* Discard anything else */
162 lua_pushvalue(L, lua_upvalueindex(1));
165 lua_pushvalue(L, lua_upvalueindex(2));
167 /* get gsub function */
168 lua_getfield(L, 1, "gsub");
171 /* tostring(obj):gsub(pattern, repl) */
176 /* luci.cutil.trim(str) */
177 static int luci_trim(lua_State *L) {
178 luaL_checkstring(L, 1);
181 /* pattern and repl */
182 lua_pushliteral(L, "^%s*(.-)%s*$");
183 lua_pushliteral(L, "%1");
186 lua_getfield(L, 1, "gsub");
189 /* str.gsub(str, pattern, repl) */
195 /* Registration helper for luci.cutil.pcdata */
196 static void luci__register_pcdata(lua_State *L) {
198 lua_pushliteral(L, "[&\"'<>]");
201 lua_createtable(L, 0, 5);
203 lua_pushliteral(L, "&");
204 lua_setfield(L, -2, "&");
205 lua_pushliteral(L, """);
206 lua_setfield(L, -2, "\"");
207 lua_pushliteral(L, "'");
208 lua_setfield(L, -2, "'");
209 lua_pushliteral(L, "<");
210 lua_setfield(L, -2, "<");
211 lua_pushliteral(L, ">");
212 lua_setfield(L, -2, ">");
214 /* register function */
215 lua_pushcclosure(L, luci_pcdata, 2);
216 lua_setfield(L, -2, "pcdata");
220 static const luaL_Reg registry[] = {
221 {"class", luci_class},
222 {"instanceof", luci_instanceof},
228 LUALIB_API int luaopen_luci_cutil(lua_State *L) {
229 luaL_register(L, LUCI_MODNAME, registry);
231 lua_pushliteral(L, LUCI_MODDESC);
232 lua_setfield(L, -2, "_DESCRIPTION");
234 lua_pushliteral(L, LUCI_MODCOPY);
235 lua_setfield(L, -2, "_COPYRIGHT");
237 /* Additional registrations */
238 luci__register_pcdata(L);
241 /* Register pythonic printf string operator */
242 lua_pushliteral(L, "");
243 lua_getmetatable(L, -1);
244 lua_pushcfunction(L, luci__string_mod);
245 lua_setfield(L, -2, "__mod");