template_parser.c 7.4 KB

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