template_parser.c 8.5 KB

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