template_lualib.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_CATALOG "gluon.web.template.parser.catalog"
  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 inlen, outlen;
  62. char *out;
  63. const char *in = luaL_checklstring(L, 1, &inlen);
  64. if (!pcdata(in, inlen, &out, &outlen))
  65. return 0;
  66. lua_pushlstring(L, out, outlen);
  67. free(out);
  68. return 1;
  69. }
  70. static int template_L_load_catalog(lua_State *L)
  71. {
  72. const char *file = luaL_checkstring(L, 1);
  73. lmo_catalog_t *cat = lua_newuserdata(L, sizeof(*cat));
  74. if (!lmo_load(cat, file)) {
  75. lua_pop(L, 1);
  76. return 0;
  77. }
  78. luaL_getmetatable(L, TEMPLATE_CATALOG);
  79. lua_setmetatable(L, -2);
  80. return 1;
  81. }
  82. static int template_catalog_call(lua_State *L)
  83. {
  84. size_t inlen, outlen;
  85. lmo_catalog_t *cat = luaL_checkudata(L, 1, TEMPLATE_CATALOG);
  86. const char *in = luaL_checklstring(L, 2, &inlen), *out;
  87. if (!lmo_translate(cat, in, inlen, &out, &outlen))
  88. return 0;
  89. lua_pushlstring(L, out, outlen);
  90. return 1;
  91. }
  92. static int template_catalog_gc(lua_State *L)
  93. {
  94. lmo_catalog_t *cat = luaL_checkudata(L, 1, TEMPLATE_CATALOG);
  95. lmo_unload(cat);
  96. return 0;
  97. }
  98. static const luaL_reg R[] = {
  99. { "parse", template_L_parse },
  100. { "parse_string", template_L_parse_string },
  101. { "pcdata", template_L_pcdata },
  102. { "load_catalog", template_L_load_catalog },
  103. {}
  104. };
  105. static const luaL_reg template_catalog_methods[] = {
  106. { "__call", template_catalog_call },
  107. { "__gc", template_catalog_gc },
  108. {}
  109. };
  110. __attribute__ ((visibility("default")))
  111. LUALIB_API int luaopen_gluon_web_template_parser(lua_State *L) {
  112. luaL_register(L, "gluon.web.template.parser", R);
  113. luaL_newmetatable(L, TEMPLATE_CATALOG);
  114. luaL_register(L, NULL, template_catalog_methods);
  115. lua_pop(L, 1);
  116. return 1;
  117. }