code_mix_prep.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* Copyright (c) 2007-2008 Thomas Lavergne
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. *
  21. * Adapted by Domingo Alvarez Duarte
  22. */
  23. #include <stddef.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #ifndef MixInteger
  28. #define MixInteger int
  29. #endif
  30. #define mix_tempsize 4096
  31. static const char *err_badeof = "unexpected end of file";
  32. enum mix_token_e {
  33. tok_sh,
  34. tok_code_start, tok_code_end, tok_expr_start, tok_expr_end,
  35. tok_data, tok_code, tok_expr,
  36. tok_eof
  37. };
  38. typedef struct mix_state_s {
  39. size_t size;
  40. const char *buffer;
  41. const char *code_start;
  42. size_t code_startsize;
  43. const char *code_end;
  44. size_t code_endsize;
  45. const char *expr;
  46. size_t exprsize;
  47. char print_out[64];
  48. size_t print_outsize;
  49. size_t pos;
  50. int token;
  51. char temp[mix_tempsize];
  52. char sq_reader_buf[mix_tempsize];
  53. char *sq_reader_last_pos;
  54. char *sq_reader_pos;
  55. const char *error;
  56. MixInteger result_size, result_pos;
  57. const char *result;
  58. size_t new_lines;
  59. } mix_state_t;
  60. /* Read an open bracket of the form [=*[ and return the number of = found.
  61. * if there is no string like this at the current pos return -1.
  62. * after this call the cursor is set at the first char after the bracket.
  63. */
  64. static int mix_bracket_open(mix_state_t *S) {
  65. size_t pos, cnt;
  66. if (S->buffer[S->pos] != '[')
  67. return -1;
  68. pos = S->pos + 1;
  69. cnt = 0;
  70. while (pos < S->size && S->buffer[pos] == '=')
  71. ++pos, ++cnt;
  72. if (pos == S->size || S->buffer[pos] != '[')
  73. return -1;
  74. S->pos = pos + 1;
  75. return cnt;
  76. }
  77. /* Read a close bracket of the form ]=*] and return the number of = found.
  78. * if there is no string like this at the current pos return -1.
  79. * after this call the cursor is set at the first char after the bracket.
  80. */
  81. static int mix_bracket_close(mix_state_t *S) {
  82. size_t pos, cnt;
  83. if (S->buffer[S->pos] != ']')
  84. return -1;
  85. pos = S->pos + 1;
  86. cnt = 0;
  87. while (pos < S->size && S->buffer[pos] == '=')
  88. ++pos, ++cnt;
  89. if (pos == S->size || S->buffer[pos] != ']')
  90. return -1;
  91. S->pos = pos + 1;
  92. return cnt;
  93. }
  94. /* Skip a lua string enclosed by type (who can be either \" or \') taking
  95. * account of escape chars. after a call to this the cursor is set at the
  96. * first character after the string.
  97. * Beware that on enter the cursor must be on the first char of the string
  98. * not on the openning char.
  99. */
  100. static int mix_skip_string(mix_state_t *S, char type) {
  101. while (S->pos < S->size) {
  102. if (S->buffer[S->pos] == type) {
  103. ++S->pos;
  104. return 0;
  105. }
  106. if (S->buffer[S->pos] == '\\')
  107. ++S->pos;
  108. ++S->pos;
  109. }
  110. return -1;
  111. }
  112. /* Skip a lua long string wich is enclosed by square bracket and level = signs
  113. * after the call the cursor is set on the first character after the string.
  114. * Beware that on enter the cursor must be on the first char of the string
  115. * not on the openning bracket.
  116. */
  117. static int mix_skip_lstring(mix_state_t *S, int level) {
  118. while (S->pos < S->size) {
  119. int tmp = mix_bracket_close(S);
  120. if (tmp == -1)
  121. ++S->pos;
  122. else if (tmp == level)
  123. return 0;
  124. }
  125. return -1;
  126. }
  127. /* Skip a squirrel comment either one line or long. after the call the cursor is set
  128. * to the first character after the coment.
  129. * Beware that on enter the cursor must be set on the first char after the //
  130. * comment prefix.
  131. */
  132. static int mix_skip_comment(mix_state_t *S) {
  133. int level = mix_bracket_open(S);
  134. if (level != -1)
  135. return mix_skip_string(S, level);
  136. while (S->pos < S->size && S->buffer[S->pos] != '\n')
  137. ++S->pos;
  138. ++S->new_lines;
  139. return 0;
  140. }
  141. #define mix_iscode_start(s) (!strncmp((s)->code_start, (s)->buffer + (s)->pos, (s)->code_startsize))
  142. #define mix_iscode_end(s) (!strncmp((s)->code_end, (s)->buffer + (s)->pos, (s)->code_endsize))
  143. #define mix_isexpr(s) (!strncmp((s)->expr, (s)->buffer + (s)->pos, (s)->exprsize))
  144. #define result_literal(s) S->result_size = sizeof(s)-1, S->result = s;
  145. static const char * sq_mix_reader_str(void *ud) {
  146. mix_state_t *S = (mix_state_t *)ud;
  147. int retrying_code_start = 0;
  148. try_again:
  149. if (S->error != NULL)
  150. return 0;
  151. /* Data chunk are sent to lua by block cause we must escape some char
  152. * and we cannot modify the buffer itself. So we must use another
  153. * buffer sending escaped block one by one.
  154. * If there is no data to send in the chunk, change the state and go
  155. * directly to the tok_code_start part.
  156. */
  157. if (S->token == tok_data) {
  158. S->result_size = 0;
  159. while (S->pos < S->size) {
  160. char c = S->buffer[S->pos];
  161. if (c == S->code_start[0])
  162. if (mix_iscode_start(S))
  163. break;
  164. if (c == '\\' || c == '"')
  165. S->temp[S->result_size++] = '\\';
  166. if (c == '\r' || c == '\n'){
  167. S->temp[S->result_size++] = '\\';
  168. if(c == '\r') c = 'r';
  169. else {
  170. S->new_lines++;
  171. c = 'n';
  172. }
  173. }
  174. S->temp[S->result_size++] = c;
  175. ++S->pos;
  176. if (S->result_size >= mix_tempsize - 1) {
  177. if (S->pos == S->size)
  178. S->token = tok_code_start;
  179. return S->result = S->temp;
  180. }
  181. }
  182. if (S->pos < S->size)
  183. S->pos += S->code_startsize;
  184. S->token = tok_code_start;
  185. if (S->result_size != 0)
  186. return S->result = S->temp;
  187. }
  188. /* Send the termination of the string and the function call before
  189. * going in tok_code/tok_expr state or if at eof go to tok_eof.
  190. */
  191. if (S->token == tok_code_start) {
  192. if (S->pos == S->size) {
  193. S->token = tok_eof;
  194. } else if (mix_isexpr(S)) {
  195. S->token = tok_expr_start;
  196. S->pos += S->exprsize;
  197. } else {
  198. S->token = tok_code;
  199. }
  200. if(!retrying_code_start) return result_literal("\");");
  201. }
  202. /* Send the output function, but without the string start character, so
  203. * the result of the lua code will be send as parameters to the output
  204. * function.
  205. */
  206. if (S->token == tok_expr_start) {
  207. if(S->new_lines){
  208. --S->new_lines;
  209. return result_literal("\n");
  210. }
  211. S->token = tok_expr;
  212. S->result_size = S->print_outsize - 1;
  213. return S->result = S->print_out;
  214. }
  215. /* Send a lua chunk in one block to the lua engine. No escaping are
  216. * needed here so we can send the full block. Then switch to the
  217. * tok_code_end. (this correctly skip all form of lua string and comments)
  218. * If no lua code is found, go directly to the tok_code_end part.
  219. */
  220. if (S->token == tok_code || S->token == tok_expr) {
  221. if(S->new_lines){
  222. --S->new_lines;
  223. return result_literal("\n");
  224. }
  225. size_t old = S->pos;
  226. while (S->pos < S->size) {
  227. char c = S->buffer[S->pos];
  228. if (c == '\'') {
  229. ++S->pos;
  230. mix_skip_string(S, '\'');
  231. } else if (c == '"') {
  232. ++S->pos;
  233. mix_skip_string(S, '"');
  234. } else if (c == '[') {
  235. int level = mix_bracket_open(S);
  236. if (level != -1)
  237. mix_skip_lstring(S, level);
  238. else
  239. ++S->pos;
  240. } else if (c == '/') {
  241. ++S->pos;
  242. if (S->buffer[S->pos] == '/') {
  243. ++S->pos;
  244. mix_skip_comment(S);
  245. }
  246. } else if (c == S->code_end[0]) {
  247. if (mix_iscode_end(S))
  248. break;
  249. ++S->pos;
  250. } else {
  251. ++S->pos;
  252. }
  253. }
  254. S->result_size = S->pos - old;
  255. if (S->pos == S->size) {
  256. S->error = err_badeof;
  257. return NULL;
  258. }
  259. S->token = S->token == tok_code ? tok_code_end : tok_expr_end;
  260. S->pos += S->code_endsize;
  261. if (S->result_size != 0)
  262. return S->result = S->buffer + old;
  263. }
  264. /* Send the data output function name and (' that start a data block to
  265. * the lua engine and switch to the tok_data state.
  266. */
  267. if (S->token == tok_code_end) {
  268. if(S->new_lines){
  269. --S->new_lines;
  270. return result_literal("\n");
  271. }
  272. S->token = tok_data;
  273. if(mix_iscode_start(S))
  274. {
  275. //we have an empty str so let's skip it
  276. retrying_code_start = 1;
  277. goto try_again;
  278. }
  279. if ((S->pos+1) == S->size) return 0;
  280. S->result_size = S->print_outsize;
  281. return S->result = S->print_out;
  282. }
  283. /* Close the output function call on end of expression and go to the lua
  284. * to data transition.
  285. */
  286. if (S->token == tok_expr_end) {
  287. S->token = tok_code_end;
  288. return result_literal(");");
  289. }
  290. /* If we skipped a sh-bang line we must send an empty line to keep the
  291. * lua line counter correct.
  292. */
  293. if (S->token == tok_sh) {
  294. S->token = tok_code_end;
  295. ++S->new_lines;
  296. return result_literal("\n");
  297. }
  298. return NULL;
  299. }
  300. static MixInteger sq_mix_reader_char(void *ud) {
  301. mix_state_t *S = (mix_state_t *)ud;
  302. if (S->error != NULL) return 0;
  303. start:
  304. if (S->result_size){
  305. MixInteger c = S->result[S->result_pos++];
  306. if(S->result_pos >= S->result_size){
  307. S->result_pos = S->result_size = 0;
  308. }
  309. return c;
  310. }
  311. if(sq_mix_reader_str(ud)) goto start;
  312. return 0;
  313. }
  314. static void sq_mix_init(mix_state_t *S,
  315. char *buffer, int buffer_size,
  316. const char *print_out,
  317. const char *code_start,
  318. const char *code_end,
  319. const char *expr){
  320. memset(S, 0, sizeof(mix_state_t));
  321. snprintf(S->print_out, sizeof(S->print_out), "%s(\"", print_out ? print_out : "mix_write");
  322. S->print_outsize = strlen(S->print_out);
  323. S->code_start = code_start ? code_start : "{%";
  324. S->code_startsize = strlen(S->code_start);
  325. S->code_end = code_end ? code_end : "%}";
  326. S->code_endsize = strlen(S->code_end);
  327. S->expr = expr ? expr : "=";
  328. S->exprsize = strlen(S->expr);
  329. S->buffer = buffer;
  330. S->size = buffer_size;
  331. S->token = tok_code_end;
  332. }