template_parser.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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(pcdata(translate('", "')))"},
  70. [T_TYPE_I18N_RAW] = {"write(translate('", "'))"},
  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 void luastr_escape(struct template_buffer *out, const char *s, const char *e)
  197. {
  198. for (const char *ptr = s; ptr < e; ptr++) {
  199. switch (*ptr) {
  200. case '\\':
  201. buf_append(out, "\\\\", 2);
  202. break;
  203. case '\'':
  204. buf_append(out, "\\\'", 2);
  205. break;
  206. case '\n':
  207. buf_append(out, "\\n", 2);
  208. break;
  209. default:
  210. buf_putchar(out, *ptr);
  211. }
  212. }
  213. }
  214. static struct template_buffer * template_format_chunk(struct template_parser *parser)
  215. {
  216. const char *p;
  217. const char *head, *tail;
  218. struct template_chunk *c = &parser->prv_chunk;
  219. if (parser->strip_before && c->type == T_TYPE_TEXT) {
  220. while ((c->e > c->s) && isspace(c->e[-1]))
  221. c->e--;
  222. }
  223. /* empty chunk */
  224. if (c->type == T_TYPE_EOF)
  225. return NULL;
  226. struct template_buffer *buf = buf_init(c->e - c->s);
  227. if (!buf)
  228. return NULL;
  229. if (c->e > c->s) {
  230. if ((head = gen_code[c->type][0]) != NULL)
  231. buf_append(buf, head, strlen(head));
  232. switch (c->type) {
  233. case T_TYPE_TEXT:
  234. case T_TYPE_INCLUDE:
  235. case T_TYPE_I18N:
  236. case T_TYPE_I18N_RAW:
  237. luastr_escape(buf, c->s, c->e);
  238. break;
  239. case T_TYPE_EXPR:
  240. buf_append(buf, c->s, c->e - c->s);
  241. for (p = c->s; p < c->e; p++)
  242. parser->line += (*p == '\n');
  243. break;
  244. case T_TYPE_CODE:
  245. buf_append(buf, c->s, c->e - c->s);
  246. for (p = c->s; p < c->e; p++)
  247. parser->line += (*p == '\n');
  248. break;
  249. }
  250. if ((tail = gen_code[c->type][1]) != NULL)
  251. buf_append(buf, tail, strlen(tail));
  252. }
  253. return buf;
  254. }
  255. const char * template_reader(lua_State *L __attribute__((unused)), void *ud, size_t *sz)
  256. {
  257. struct template_parser *parser = ud;
  258. /* free previous chunk */
  259. free(parser->lua_chunk);
  260. parser->lua_chunk = NULL;
  261. while (true) {
  262. int rem = parser->size - (parser->off - parser->data);
  263. char *tag;
  264. parser->prv_chunk = parser->cur_chunk;
  265. /* before tag */
  266. if (!parser->in_expr) {
  267. if ((tag = memmem(parser->off, rem, "<%", 2)) != NULL) {
  268. template_text(parser, tag);
  269. parser->off = tag + 2;
  270. parser->in_expr = 1;
  271. } else {
  272. template_text(parser, parser->data + parser->size);
  273. parser->off = parser->data + parser->size;
  274. }
  275. }
  276. /* inside tag */
  277. else {
  278. if ((tag = memmem(parser->off, rem, "%>", 2)) != NULL) {
  279. template_code(parser, tag);
  280. parser->off = tag + 2;
  281. parser->in_expr = 0;
  282. } else {
  283. /* unexpected EOF */
  284. template_code(parser, parser->data + parser->size);
  285. *sz = 1;
  286. return "\033";
  287. }
  288. }
  289. struct template_buffer *buf = template_format_chunk(parser);
  290. if (!buf)
  291. return NULL;
  292. *sz = buf_length(buf);
  293. if (*sz) {
  294. parser->lua_chunk = buf_destroy(buf);
  295. return parser->lua_chunk;
  296. }
  297. }
  298. }
  299. int template_error(lua_State *L, struct template_parser *parser)
  300. {
  301. const char *err = luaL_checkstring(L, -1);
  302. const char *off = parser->prv_chunk.s;
  303. const char *ptr;
  304. char msg[1024];
  305. int line = 0;
  306. int chunkline = 0;
  307. if ((ptr = memmem(err, strlen(err), "]:", 2)) != NULL) {
  308. chunkline = atoi(ptr + 2) - parser->prv_chunk.line;
  309. while (*ptr) {
  310. if (*ptr++ == ' ') {
  311. err = ptr;
  312. break;
  313. }
  314. }
  315. }
  316. if (memmem(err, strlen(err), "'char(27)'", 10) != NULL) {
  317. off = parser->data + parser->size;
  318. err = "'%>' expected before end of file";
  319. chunkline = 0;
  320. }
  321. for (ptr = parser->data; ptr < off; ptr++) {
  322. if (*ptr == '\n')
  323. line++;
  324. }
  325. snprintf(msg, sizeof(msg), "Syntax error in %s:%d: %s",
  326. parser->file ?: "[string]", line + chunkline, err ?: "(unknown error)");
  327. lua_pushnil(L);
  328. lua_pushinteger(L, line + chunkline);
  329. lua_pushstring(L, msg);
  330. return 3;
  331. }