template_parser.c 7.3 KB

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