2 * LuCI Template - Lua binding
4 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "template_lualib.h"
21 int template_L_parse(lua_State *L)
23 const char *file = luaL_checkstring(L, 1);
24 struct template_parser *parser = template_open(file);
30 lua_pushinteger(L, errno);
31 lua_pushstring(L, strerror(errno));
35 lua_status = lua_load(L, template_reader, parser, file);
40 rv = template_error(L, parser);
42 template_close(parser);
47 int template_L_sanitize_utf8(lua_State *L)
50 const char *str = luaL_checklstring(L, 1, &len);
51 char *res = sanitize_utf8(str, len);
55 lua_pushstring(L, res);
64 int template_L_sanitize_pcdata(lua_State *L)
67 const char *str = luaL_checklstring(L, 1, &len);
68 char *res = sanitize_pcdata(str, len);
72 lua_pushstring(L, res);
81 static int template_L_load_catalog(lua_State *L) {
82 const char *lang = luaL_optstring(L, 1, "en");
83 const char *dir = luaL_optstring(L, 2, NULL);
84 lua_pushboolean(L, !lmo_load_catalog(lang, dir));
88 static int template_L_close_catalog(lua_State *L) {
89 const char *lang = luaL_optstring(L, 1, "en");
90 lmo_close_catalog(lang);
94 static int template_L_change_catalog(lua_State *L) {
95 const char *lang = luaL_optstring(L, 1, "en");
96 lua_pushboolean(L, !lmo_change_catalog(lang));
100 static int template_L_translate(lua_State *L) {
104 const char *key = luaL_checklstring(L, 1, &len);
106 switch (lmo_translate(key, len, &tr, &trlen))
109 lua_pushlstring(L, tr, trlen);
117 lua_pushstring(L, "no catalog loaded");
121 static int template_L_hash(lua_State *L) {
123 const char *key = luaL_checklstring(L, 1, &len);
124 lua_pushinteger(L, sfh_hash(key, len));
130 static const luaL_reg R[] = {
131 { "parse", template_L_parse },
132 { "sanitize_utf8", template_L_sanitize_utf8 },
133 { "sanitize_pcdata", template_L_sanitize_pcdata },
134 { "load_catalog", template_L_load_catalog },
135 { "close_catalog", template_L_close_catalog },
136 { "change_catalog", template_L_change_catalog },
137 { "translate", template_L_translate },
138 { "hash", template_L_hash },
142 LUALIB_API int luaopen_luci_template_parser(lua_State *L) {
143 luaL_register(L, TEMPLATE_LUALIB_META, R);