0003-libs-web-add-support-for-string-templates-to-the-template-parser.patch 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. From: Matthias Schiffer <mschiffer@universe-factory.net>
  2. Date: Fri, 16 May 2014 11:37:21 +0200
  3. Subject: libs/web: add support for string templates to the template parser
  4. diff --git a/libs/web/src/template_lualib.c b/libs/web/src/template_lualib.c
  5. index 0d43641..1035611 100644
  6. --- a/libs/web/src/template_lualib.c
  7. +++ b/libs/web/src/template_lualib.c
  8. @@ -18,10 +18,8 @@
  9. #include "template_lualib.h"
  10. -int template_L_parse(lua_State *L)
  11. +static int template_L_do_parse(lua_State *L, struct template_parser *parser, const char *chunkname)
  12. {
  13. - const char *file = luaL_checkstring(L, 1);
  14. - struct template_parser *parser = template_open(file);
  15. int lua_status, rv;
  16. if (!parser)
  17. @@ -32,7 +30,7 @@ int template_L_parse(lua_State *L)
  18. return 3;
  19. }
  20. - lua_status = lua_load(L, template_reader, parser, file);
  21. + lua_status = lua_load(L, template_reader, parser, chunkname);
  22. if (lua_status == 0)
  23. rv = 1;
  24. @@ -44,6 +42,23 @@ int template_L_parse(lua_State *L)
  25. return rv;
  26. }
  27. +int template_L_parse(lua_State *L)
  28. +{
  29. + const char *file = luaL_checkstring(L, 1);
  30. + struct template_parser *parser = template_open(file);
  31. +
  32. + return template_L_do_parse(L, parser, file);
  33. +}
  34. +
  35. +int template_L_parse_string(lua_State *L)
  36. +{
  37. + size_t len;
  38. + const char *str = luaL_checklstring(L, 1, &len);
  39. + struct template_parser *parser = template_string(str, len);
  40. +
  41. + return template_L_do_parse(L, parser, "[string]");
  42. +}
  43. +
  44. int template_L_utf8(lua_State *L)
  45. {
  46. size_t len = 0;
  47. @@ -146,6 +161,7 @@ static int template_L_hash(lua_State *L) {
  48. /* module table */
  49. static const luaL_reg R[] = {
  50. { "parse", template_L_parse },
  51. + { "parse_string", template_L_parse_string },
  52. { "utf8", template_L_utf8 },
  53. { "pcdata", template_L_pcdata },
  54. { "striptags", template_L_striptags },
  55. diff --git a/libs/web/src/template_parser.c b/libs/web/src/template_parser.c
  56. index 1aa5131..c263fbf 100644
  57. --- a/libs/web/src/template_parser.c
  58. +++ b/libs/web/src/template_parser.c
  59. @@ -100,6 +100,36 @@ err:
  60. return NULL;
  61. }
  62. +struct template_parser * template_string(const char *str, uint32_t len)
  63. +{
  64. + struct template_parser *parser;
  65. +
  66. + if (!str) {
  67. + errno = EINVAL;
  68. + goto err;
  69. + }
  70. +
  71. + if (!(parser = malloc(sizeof(*parser))))
  72. + goto err;
  73. +
  74. + memset(parser, 0, sizeof(*parser));
  75. + parser->fd = -1;
  76. +
  77. + parser->size = len;
  78. + parser->data = (char*)str;
  79. +
  80. + parser->off = parser->data;
  81. + parser->cur_chunk.type = T_TYPE_INIT;
  82. + parser->cur_chunk.s = parser->data;
  83. + parser->cur_chunk.e = parser->data;
  84. +
  85. + return parser;
  86. +
  87. +err:
  88. + template_close(parser);
  89. + return NULL;
  90. +}
  91. +
  92. void template_close(struct template_parser *parser)
  93. {
  94. if (!parser)
  95. @@ -108,11 +138,14 @@ void template_close(struct template_parser *parser)
  96. if (parser->gc != NULL)
  97. free(parser->gc);
  98. - if ((parser->data != NULL) && (parser->data != MAP_FAILED))
  99. - munmap(parser->data, parser->size);
  100. + /* if file is not set, we were parsing a string */
  101. + if (parser->file) {
  102. + if ((parser->data != NULL) && (parser->data != MAP_FAILED))
  103. + munmap(parser->data, parser->size);
  104. - if (parser->fd >= 0)
  105. - close(parser->fd);
  106. + if (parser->fd >= 0)
  107. + close(parser->fd);
  108. + }
  109. free(parser);
  110. }
  111. @@ -376,7 +409,7 @@ int template_error(lua_State *L, struct template_parser *parser)
  112. line++;
  113. snprintf(msg, sizeof(msg), "Syntax error in %s:%d: %s",
  114. - parser->file, line + chunkline, err ? err : "(unknown error)");
  115. + parser->file ? parser->file : "[string]", line + chunkline, err ? err : "(unknown error)");
  116. lua_pushnil(L);
  117. lua_pushinteger(L, line + chunkline);
  118. diff --git a/libs/web/src/template_parser.h b/libs/web/src/template_parser.h
  119. index ad03cbc..a3200a2 100644
  120. --- a/libs/web/src/template_parser.h
  121. +++ b/libs/web/src/template_parser.h
  122. @@ -71,6 +71,7 @@ struct template_parser {
  123. };
  124. struct template_parser * template_open(const char *file);
  125. +struct template_parser * template_string(const char *str, uint32_t len);
  126. void template_close(struct template_parser *parser);
  127. const char *template_reader(lua_State *L, void *ud, size_t *sz);