template_lualib.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * gluon-web Template - Lua binding
  3. *
  4. * Copyright (C) 2009 Jo-Philipp Wich <jow@openwrt.org>
  5. * Copyright (C) 2018 Matthias Schiffer <mschiffer@universe-factory.net>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "template_parser.h"
  20. #include "template_utils.h"
  21. #include "template_lmo.h"
  22. #include <lualib.h>
  23. #include <lauxlib.h>
  24. #include <errno.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #define TEMPLATE_LUALIB_META "gluon.web.template.parser"
  28. static int template_L_do_parse(lua_State *L, struct template_parser *parser, const char *chunkname)
  29. {
  30. int lua_status, rv;
  31. if (!parser)
  32. {
  33. lua_pushnil(L);
  34. lua_pushinteger(L, errno);
  35. lua_pushstring(L, strerror(errno));
  36. return 3;
  37. }
  38. lua_status = lua_load(L, template_reader, parser, chunkname);
  39. if (lua_status == 0)
  40. rv = 1;
  41. else
  42. rv = template_error(L, parser);
  43. template_close(parser);
  44. return rv;
  45. }
  46. static int template_L_parse(lua_State *L)
  47. {
  48. const char *file = luaL_checkstring(L, 1);
  49. struct template_parser *parser = template_open(file);
  50. return template_L_do_parse(L, parser, file);
  51. }
  52. static int template_L_parse_string(lua_State *L)
  53. {
  54. size_t len;
  55. const char *str = luaL_checklstring(L, 1, &len);
  56. struct template_parser *parser = template_string(str, len);
  57. return template_L_do_parse(L, parser, "[string]");
  58. }
  59. static int template_L_pcdata(lua_State *L)
  60. {
  61. size_t len = 0, outlen;
  62. const char *str = luaL_checklstring(L, 1, &len);
  63. char *res = pcdata(str, len, &outlen);
  64. if (res != NULL)
  65. {
  66. lua_pushlstring(L, res, outlen);
  67. free(res);
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. static int template_L_load_catalog(lua_State *L) {
  73. const char *lang = luaL_optstring(L, 1, "en");
  74. const char *dir = luaL_optstring(L, 2, NULL);
  75. lua_pushboolean(L, !lmo_load_catalog(lang, dir));
  76. return 1;
  77. }
  78. static int template_L_translate(lua_State *L) {
  79. size_t len;
  80. char *tr;
  81. size_t trlen;
  82. const char *key = luaL_checklstring(L, 1, &len);
  83. switch (lmo_translate(key, len, &tr, &trlen))
  84. {
  85. case 0:
  86. lua_pushlstring(L, tr, trlen);
  87. return 1;
  88. case -1:
  89. return 0;
  90. }
  91. lua_pushnil(L);
  92. lua_pushstring(L, "no catalog loaded");
  93. return 2;
  94. }
  95. /* module table */
  96. static const luaL_reg R[] = {
  97. { "parse", template_L_parse },
  98. { "parse_string", template_L_parse_string },
  99. { "pcdata", template_L_pcdata },
  100. { "load_catalog", template_L_load_catalog },
  101. { "translate", template_L_translate },
  102. {}
  103. };
  104. LUALIB_API int luaopen_gluon_web_template_parser(lua_State *L) {
  105. luaL_register(L, TEMPLATE_LUALIB_META, R);
  106. return 1;
  107. }