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
;
27 if( (parser
.fd
= open(file
, O_RDONLY
)) > 0 )
31 parser
.state
= T_STATE_TEXT_NEXT
;
33 lua_status
= lua_load(L
, template_reader
, &parser
, file
);
35 (void) close(parser
.fd
);
45 lua_pushinteger(L
, lua_status
);
46 lua_pushlstring(L
, parser
.out
, parser
.outsize
);
52 lua_pushinteger(L
, 255);
53 lua_pushstring(L
, "No such file or directory");
57 int template_L_sanitize_utf8(lua_State
*L
)
60 const char *str
= luaL_checklstring(L
, 1, &len
);
61 char *res
= sanitize_utf8(str
, len
);
65 lua_pushstring(L
, res
);
74 int template_L_sanitize_pcdata(lua_State
*L
)
77 const char *str
= luaL_checklstring(L
, 1, &len
);
78 char *res
= sanitize_pcdata(str
, len
);
82 lua_pushstring(L
, res
);
93 static const luaL_reg R
[] = {
94 { "parse", template_L_parse
},
95 { "sanitize_utf8", template_L_sanitize_utf8
},
96 { "sanitize_pcdata", template_L_sanitize_pcdata
},
100 LUALIB_API
int luaopen_luci_template_parser(lua_State
*L
) {
101 luaL_register(L
, TEMPLATE_LUALIB_META
, R
);