luci-0.11: merge outstanding trunk changes
[project/luci.git] / libs / web / src / template_lualib.c
1 /*
2 * LuCI Template - Lua binding
3 *
4 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 */
18
19 #include "template_lualib.h"
20
21 int template_L_parse(lua_State *L)
22 {
23 const char *file = luaL_checkstring(L, 1);
24 struct template_parser *parser = template_open(file);
25 int lua_status, rv;
26
27 if (!parser)
28 {
29 lua_pushnil(L);
30 lua_pushinteger(L, errno);
31 lua_pushstring(L, strerror(errno));
32 return 3;
33 }
34
35 lua_status = lua_load(L, template_reader, parser, file);
36
37 if (lua_status == 0)
38 rv = 1;
39 else
40 rv = template_error(L, parser);
41
42 template_close(parser);
43
44 return rv;
45 }
46
47 int template_L_utf8(lua_State *L)
48 {
49 size_t len = 0;
50 const char *str = luaL_checklstring(L, 1, &len);
51 char *res = utf8(str, len);
52
53 if (res != NULL)
54 {
55 lua_pushstring(L, res);
56 free(res);
57
58 return 1;
59 }
60
61 return 0;
62 }
63
64 int template_L_pcdata(lua_State *L)
65 {
66 size_t len = 0;
67 const char *str = luaL_checklstring(L, 1, &len);
68 char *res = pcdata(str, len);
69
70 if (res != NULL)
71 {
72 lua_pushstring(L, res);
73 free(res);
74
75 return 1;
76 }
77
78 return 0;
79 }
80
81 int template_L_striptags(lua_State *L)
82 {
83 size_t len = 0;
84 const char *str = luaL_checklstring(L, 1, &len);
85 char *res = striptags(str, len);
86
87 if (res != NULL)
88 {
89 lua_pushstring(L, res);
90 free(res);
91
92 return 1;
93 }
94
95 return 0;
96 }
97
98 static int template_L_load_catalog(lua_State *L) {
99 const char *lang = luaL_optstring(L, 1, "en");
100 const char *dir = luaL_optstring(L, 2, NULL);
101 lua_pushboolean(L, !lmo_load_catalog(lang, dir));
102 return 1;
103 }
104
105 static int template_L_close_catalog(lua_State *L) {
106 const char *lang = luaL_optstring(L, 1, "en");
107 lmo_close_catalog(lang);
108 return 0;
109 }
110
111 static int template_L_change_catalog(lua_State *L) {
112 const char *lang = luaL_optstring(L, 1, "en");
113 lua_pushboolean(L, !lmo_change_catalog(lang));
114 return 1;
115 }
116
117 static int template_L_translate(lua_State *L) {
118 size_t len;
119 char *tr;
120 int trlen;
121 const char *key = luaL_checklstring(L, 1, &len);
122
123 switch (lmo_translate(key, len, &tr, &trlen))
124 {
125 case 0:
126 lua_pushlstring(L, tr, trlen);
127 return 1;
128
129 case -1:
130 return 0;
131 }
132
133 lua_pushnil(L);
134 lua_pushstring(L, "no catalog loaded");
135 return 2;
136 }
137
138 static int template_L_hash(lua_State *L) {
139 size_t len;
140 const char *key = luaL_checklstring(L, 1, &len);
141 lua_pushinteger(L, sfh_hash(key, len));
142 return 1;
143 }
144
145
146 /* module table */
147 static const luaL_reg R[] = {
148 { "parse", template_L_parse },
149 { "utf8", template_L_utf8 },
150 { "pcdata", template_L_pcdata },
151 { "striptags", template_L_striptags },
152 { "load_catalog", template_L_load_catalog },
153 { "close_catalog", template_L_close_catalog },
154 { "change_catalog", template_L_change_catalog },
155 { "translate", template_L_translate },
156 { "hash", template_L_hash },
157 { NULL, NULL }
158 };
159
160 LUALIB_API int luaopen_luci_template_parser(lua_State *L) {
161 luaL_register(L, TEMPLATE_LUALIB_META, R);
162 return 1;
163 }