template_lualib.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_lualib.h"
  19. static int template_L_do_parse(lua_State *L, struct template_parser *parser, const char *chunkname)
  20. {
  21. int lua_status, rv;
  22. if (!parser)
  23. {
  24. lua_pushnil(L);
  25. lua_pushinteger(L, errno);
  26. lua_pushstring(L, strerror(errno));
  27. return 3;
  28. }
  29. lua_status = lua_load(L, template_reader, parser, chunkname);
  30. if (lua_status == 0)
  31. rv = 1;
  32. else
  33. rv = template_error(L, parser);
  34. template_close(parser);
  35. return rv;
  36. }
  37. int template_L_parse(lua_State *L)
  38. {
  39. const char *file = luaL_checkstring(L, 1);
  40. struct template_parser *parser = template_open(file);
  41. return template_L_do_parse(L, parser, file);
  42. }
  43. int template_L_parse_string(lua_State *L)
  44. {
  45. size_t len;
  46. const char *str = luaL_checklstring(L, 1, &len);
  47. struct template_parser *parser = template_string(str, len);
  48. return template_L_do_parse(L, parser, "[string]");
  49. }
  50. int template_L_utf8(lua_State *L)
  51. {
  52. size_t len = 0;
  53. const char *str = luaL_checklstring(L, 1, &len);
  54. char *res = utf8(str, len);
  55. if (res != NULL)
  56. {
  57. lua_pushstring(L, res);
  58. free(res);
  59. return 1;
  60. }
  61. return 0;
  62. }
  63. int template_L_pcdata(lua_State *L)
  64. {
  65. size_t len = 0;
  66. const char *str = luaL_checklstring(L, 1, &len);
  67. char *res = pcdata(str, len);
  68. if (res != NULL)
  69. {
  70. lua_pushstring(L, res);
  71. free(res);
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. int template_L_striptags(lua_State *L)
  77. {
  78. size_t len = 0;
  79. const char *str = luaL_checklstring(L, 1, &len);
  80. char *res = striptags(str, len);
  81. if (res != NULL)
  82. {
  83. lua_pushstring(L, res);
  84. free(res);
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. static int template_L_load_catalog(lua_State *L) {
  90. const char *lang = luaL_optstring(L, 1, "en");
  91. const char *dir = luaL_optstring(L, 2, NULL);
  92. lua_pushboolean(L, !lmo_load_catalog(lang, dir));
  93. return 1;
  94. }
  95. static int template_L_close_catalog(lua_State *L) {
  96. const char *lang = luaL_optstring(L, 1, "en");
  97. lmo_close_catalog(lang);
  98. return 0;
  99. }
  100. static int template_L_change_catalog(lua_State *L) {
  101. const char *lang = luaL_optstring(L, 1, "en");
  102. lua_pushboolean(L, !lmo_change_catalog(lang));
  103. return 1;
  104. }
  105. static int template_L_translate(lua_State *L) {
  106. size_t len;
  107. char *tr;
  108. int trlen;
  109. const char *key = luaL_checklstring(L, 1, &len);
  110. switch (lmo_translate(key, len, &tr, &trlen))
  111. {
  112. case 0:
  113. lua_pushlstring(L, tr, trlen);
  114. return 1;
  115. case -1:
  116. return 0;
  117. }
  118. lua_pushnil(L);
  119. lua_pushstring(L, "no catalog loaded");
  120. return 2;
  121. }
  122. static int template_L_hash(lua_State *L) {
  123. size_t len;
  124. const char *key = luaL_checklstring(L, 1, &len);
  125. lua_pushinteger(L, sfh_hash(key, len));
  126. return 1;
  127. }
  128. /* module table */
  129. static const luaL_reg R[] = {
  130. { "parse", template_L_parse },
  131. { "parse_string", template_L_parse_string },
  132. { "utf8", template_L_utf8 },
  133. { "pcdata", template_L_pcdata },
  134. { "striptags", template_L_striptags },
  135. { "load_catalog", template_L_load_catalog },
  136. { "close_catalog", template_L_close_catalog },
  137. { "change_catalog", template_L_change_catalog },
  138. { "translate", template_L_translate },
  139. { "hash", template_L_hash },
  140. { NULL, NULL }
  141. };
  142. LUALIB_API int luaopen_luci_template_parser(lua_State *L) {
  143. luaL_register(L, TEMPLATE_LUALIB_META, R);
  144. return 1;
  145. }