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