template_parser.c 7.4 KB

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