template_parser.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * gluon-web Template - Parser implementation
  3. *
  4. * Copyright (C) 2009-2012 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 <sys/stat.h>
  25. #include <sys/mman.h>
  26. #include <ctype.h>
  27. #include <fcntl.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. /* code types */
  33. #define T_TYPE_INIT 0
  34. #define T_TYPE_TEXT 1
  35. #define T_TYPE_COMMENT 2
  36. #define T_TYPE_EXPR 3
  37. #define T_TYPE_INCLUDE 4
  38. #define T_TYPE_I18N 5
  39. #define T_TYPE_I18N_RAW 6
  40. #define T_TYPE_CODE 7
  41. #define T_TYPE_EOF 8
  42. struct template_chunk {
  43. const char *s;
  44. const char *e;
  45. int type;
  46. int line;
  47. };
  48. /* parser state */
  49. struct template_parser {
  50. size_t size;
  51. char *data;
  52. char *off;
  53. char *lua_chunk;
  54. int line;
  55. int in_expr;
  56. bool strip_before;
  57. bool strip_after;
  58. struct template_chunk prv_chunk;
  59. struct template_chunk cur_chunk;
  60. const char *file;
  61. };
  62. /* leading and trailing code for different types */
  63. static const char *const gen_code[][2] = {
  64. [T_TYPE_INIT] = {NULL, NULL},
  65. [T_TYPE_TEXT] = {"write(\"", "\")"},
  66. [T_TYPE_COMMENT] = {NULL, NULL},
  67. [T_TYPE_EXPR] = {"write(tostring(", " or \"\"))"},
  68. [T_TYPE_INCLUDE] = {"include(\"", "\")"},
  69. [T_TYPE_I18N] = {"write(\"", "\")"},
  70. [T_TYPE_I18N_RAW] = {"write(\"", "\")"},
  71. [T_TYPE_CODE] = {NULL, " "},
  72. [T_TYPE_EOF] = {NULL, NULL},
  73. };
  74. static struct template_parser * template_init(struct template_parser *parser)
  75. {
  76. parser->off = parser->data;
  77. parser->cur_chunk.type = T_TYPE_INIT;
  78. parser->cur_chunk.s = parser->data;
  79. parser->cur_chunk.e = parser->data;
  80. return parser;
  81. }
  82. struct template_parser * template_open(const char *file)
  83. {
  84. int fd = -1;
  85. struct stat s;
  86. struct template_parser *parser;
  87. if (!(parser = calloc(1, sizeof(*parser))))
  88. goto err;
  89. parser->file = file;
  90. fd = open(file, O_RDONLY|O_CLOEXEC);
  91. if (fd < 0)
  92. goto err;
  93. if (fstat(fd, &s))
  94. goto err;
  95. parser->size = s.st_size;
  96. parser->data = mmap(NULL, parser->size, PROT_READ, MAP_PRIVATE,
  97. fd, 0);
  98. close(fd);
  99. fd = -1;
  100. if (parser->data == MAP_FAILED)
  101. goto err;
  102. return template_init(parser);
  103. err:
  104. if (fd >= 0)
  105. close(fd);
  106. template_close(parser);
  107. return NULL;
  108. }
  109. struct template_parser * template_string(const char *str, size_t len)
  110. {
  111. struct template_parser *parser;
  112. if (!(parser = calloc(1, sizeof(*parser))))
  113. goto err;
  114. parser->size = len;
  115. parser->data = (char *)str;
  116. return template_init(parser);
  117. err:
  118. template_close(parser);
  119. return NULL;
  120. }
  121. void template_close(struct template_parser *parser)
  122. {
  123. if (!parser)
  124. return;
  125. free(parser->lua_chunk);
  126. /* if file is not set, we were parsing a string */
  127. if (parser->file) {
  128. if ((parser->data != NULL) && (parser->data != MAP_FAILED))
  129. munmap(parser->data, parser->size);
  130. }
  131. free(parser);
  132. }
  133. static void template_text(struct template_parser *parser, const char *e)
  134. {
  135. const char *s = parser->off;
  136. if (s < (parser->data + parser->size)) {
  137. if (parser->strip_after) {
  138. while ((s < e) && isspace(s[0]))
  139. s++;
  140. }
  141. parser->cur_chunk.type = T_TYPE_TEXT;
  142. } else {
  143. parser->cur_chunk.type = T_TYPE_EOF;
  144. }
  145. parser->cur_chunk.line = parser->line;
  146. parser->cur_chunk.s = s;
  147. parser->cur_chunk.e = e;
  148. }
  149. static void template_code(struct template_parser *parser, const char *e)
  150. {
  151. const char *s = parser->off;
  152. parser->strip_before = false;
  153. parser->strip_after = false;
  154. if (s < e && s[0] == '-') {
  155. parser->strip_before = true;
  156. s++;
  157. }
  158. if (s < e && e[-1] == '-') {
  159. parser->strip_after = true;
  160. e--;
  161. }
  162. switch (*s) {
  163. /* comment */
  164. case '#':
  165. s++;
  166. parser->cur_chunk.type = T_TYPE_COMMENT;
  167. break;
  168. /* include */
  169. case '+':
  170. s++;
  171. parser->cur_chunk.type = T_TYPE_INCLUDE;
  172. break;
  173. /* translate */
  174. case ':':
  175. s++;
  176. parser->cur_chunk.type = T_TYPE_I18N;
  177. break;
  178. /* translate raw */
  179. case '_':
  180. s++;
  181. parser->cur_chunk.type = T_TYPE_I18N_RAW;
  182. break;
  183. /* expr */
  184. case '=':
  185. s++;
  186. parser->cur_chunk.type = T_TYPE_EXPR;
  187. break;
  188. /* code */
  189. default:
  190. parser->cur_chunk.type = T_TYPE_CODE;
  191. }
  192. parser->cur_chunk.line = parser->line;
  193. parser->cur_chunk.s = s;
  194. parser->cur_chunk.e = e;
  195. }
  196. static struct template_buffer * template_format_chunk(struct template_parser *parser)
  197. {
  198. const char *p;
  199. const char *head, *tail;
  200. struct template_chunk *c = &parser->prv_chunk;
  201. if (parser->strip_before && c->type == T_TYPE_TEXT) {
  202. while ((c->e > c->s) && isspace(c->e[-1]))
  203. c->e--;
  204. }
  205. /* empty chunk */
  206. if (c->type == T_TYPE_EOF)
  207. return NULL;
  208. struct template_buffer *buf = buf_init(c->e - c->s);
  209. if (!buf)
  210. return NULL;
  211. if (c->e > c->s) {
  212. if ((head = gen_code[c->type][0]) != NULL)
  213. buf_append(buf, head, strlen(head));
  214. switch (c->type) {
  215. case T_TYPE_TEXT:
  216. luastr_escape(buf, c->s, c->e - c->s, false);
  217. break;
  218. case T_TYPE_EXPR:
  219. buf_append(buf, c->s, c->e - c->s);
  220. for (p = c->s; p < c->e; p++)
  221. parser->line += (*p == '\n');
  222. break;
  223. case T_TYPE_INCLUDE:
  224. luastr_escape(buf, c->s, c->e - c->s, false);
  225. break;
  226. case T_TYPE_I18N:
  227. luastr_translate(buf, c->s, c->e - c->s, true);
  228. break;
  229. case T_TYPE_I18N_RAW:
  230. luastr_translate(buf, c->s, c->e - c->s, false);
  231. break;
  232. case T_TYPE_CODE:
  233. buf_append(buf, c->s, c->e - c->s);
  234. for (p = c->s; p < c->e; p++)
  235. parser->line += (*p == '\n');
  236. break;
  237. }
  238. if ((tail = gen_code[c->type][1]) != NULL)
  239. buf_append(buf, tail, strlen(tail));
  240. }
  241. return buf;
  242. }
  243. const char * template_reader(lua_State *L __attribute__((unused)), void *ud, size_t *sz)
  244. {
  245. struct template_parser *parser = ud;
  246. /* free previous chunk */
  247. free(parser->lua_chunk);
  248. parser->lua_chunk = NULL;
  249. while (true) {
  250. int rem = parser->size - (parser->off - parser->data);
  251. char *tag;
  252. parser->prv_chunk = parser->cur_chunk;
  253. /* before tag */
  254. if (!parser->in_expr) {
  255. if ((tag = memmem(parser->off, rem, "<%", 2)) != NULL) {
  256. template_text(parser, tag);
  257. parser->off = tag + 2;
  258. parser->in_expr = 1;
  259. } else {
  260. template_text(parser, parser->data + parser->size);
  261. parser->off = parser->data + parser->size;
  262. }
  263. }
  264. /* inside tag */
  265. else {
  266. if ((tag = memmem(parser->off, rem, "%>", 2)) != NULL) {
  267. template_code(parser, tag);
  268. parser->off = tag + 2;
  269. parser->in_expr = 0;
  270. } else {
  271. /* unexpected EOF */
  272. template_code(parser, parser->data + parser->size);
  273. *sz = 1;
  274. return "\033";
  275. }
  276. }
  277. struct template_buffer *buf = template_format_chunk(parser);
  278. if (!buf)
  279. return NULL;
  280. *sz = buf_length(buf);
  281. if (*sz) {
  282. parser->lua_chunk = buf_destroy(buf);
  283. return parser->lua_chunk;
  284. }
  285. }
  286. }
  287. int template_error(lua_State *L, struct template_parser *parser)
  288. {
  289. const char *err = luaL_checkstring(L, -1);
  290. const char *off = parser->prv_chunk.s;
  291. const char *ptr;
  292. char msg[1024];
  293. int line = 0;
  294. int chunkline = 0;
  295. if ((ptr = memmem(err, strlen(err), "]:", 2)) != NULL) {
  296. chunkline = atoi(ptr + 2) - parser->prv_chunk.line;
  297. while (*ptr) {
  298. if (*ptr++ == ' ') {
  299. err = ptr;
  300. break;
  301. }
  302. }
  303. }
  304. if (memmem(err, strlen(err), "'char(27)'", 10) != NULL) {
  305. off = parser->data + parser->size;
  306. err = "'%>' expected before end of file";
  307. chunkline = 0;
  308. }
  309. for (ptr = parser->data; ptr < off; ptr++) {
  310. if (*ptr == '\n')
  311. line++;
  312. }
  313. snprintf(msg, sizeof(msg), "Syntax error in %s:%d: %s",
  314. parser->file ?: "[string]", line + chunkline, err ?: "(unknown error)");
  315. lua_pushnil(L);
  316. lua_pushinteger(L, line + chunkline);
  317. lua_pushstring(L, msg);
  318. return 3;
  319. }