template_lualib.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * LuCI Template - Lua binding
  3. *
  4. * Copyright (C) 2009 Jo-Philipp Wich <jow@openwrt.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. #include "template_parser.h"
  19. #include "template_utils.h"
  20. #include "template_lmo.h"
  21. #define TEMPLATE_LUALIB_META "gluon.web.template.parser"
  22. static int template_L_do_parse(lua_State *L, struct template_parser *parser, const char *chunkname)
  23. {
  24. int lua_status, rv;
  25. if (!parser)
  26. {
  27. lua_pushnil(L);
  28. lua_pushinteger(L, errno);
  29. lua_pushstring(L, strerror(errno));
  30. return 3;
  31. }
  32. lua_status = lua_load(L, template_reader, parser, chunkname);
  33. if (lua_status == 0)
  34. rv = 1;
  35. else
  36. rv = template_error(L, parser);
  37. template_close(parser);
  38. return rv;
  39. }
  40. static int template_L_parse(lua_State *L)
  41. {
  42. const char *file = luaL_checkstring(L, 1);
  43. struct template_parser *parser = template_open(file);
  44. return template_L_do_parse(L, parser, file);
  45. }
  46. static int template_L_parse_string(lua_State *L)
  47. {
  48. size_t len;
  49. const char *str = luaL_checklstring(L, 1, &len);
  50. struct template_parser *parser = template_string(str, len);
  51. return template_L_do_parse(L, parser, "[string]");
  52. }
  53. static int template_L_pcdata(lua_State *L)
  54. {
  55. size_t len = 0;
  56. const char *str = luaL_checklstring(L, 1, &len);
  57. char *res = pcdata(str, len);
  58. if (res != NULL)
  59. {
  60. lua_pushstring(L, res);
  61. free(res);
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. static int template_L_load_catalog(lua_State *L) {
  67. const char *lang = luaL_optstring(L, 1, "en");
  68. const char *dir = luaL_optstring(L, 2, NULL);
  69. lua_pushboolean(L, !lmo_load_catalog(lang, dir));
  70. return 1;
  71. }
  72. static int template_L_translate(lua_State *L) {
  73. size_t len;
  74. char *tr;
  75. int trlen;
  76. const char *key = luaL_checklstring(L, 1, &len);
  77. switch (lmo_translate(key, len, &tr, &trlen))
  78. {
  79. case 0:
  80. lua_pushlstring(L, tr, trlen);
  81. return 1;
  82. case -1:
  83. return 0;
  84. }
  85. lua_pushnil(L);
  86. lua_pushstring(L, "no catalog loaded");
  87. return 2;
  88. }
  89. /* module table */
  90. static const luaL_reg R[] = {
  91. { "parse", template_L_parse },
  92. { "parse_string", template_L_parse_string },
  93. { "pcdata", template_L_pcdata },
  94. { "load_catalog", template_L_load_catalog },
  95. { "translate", template_L_translate },
  96. {}
  97. };
  98. LUALIB_API int luaopen_gluon_web_template_parser(lua_State *L) {
  99. luaL_register(L, TEMPLATE_LUALIB_META, R);
  100. return 1;
  101. }