code_mix_prep.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. if (S->error != NULL)
  148. return 0;
  149. /* Data chunk are sent to lua by block cause we must escape some char
  150. * and we cannot modify the buffer itself. So we must use another
  151. * buffer sending escaped block one by one.
  152. * If there is no data to send in the chunk, change the state and go
  153. * directly to the tok_code_start part.
  154. */
  155. if (S->token == tok_data) {
  156. S->result_size = 0;
  157. while (S->pos < S->size) {
  158. char c = S->buffer[S->pos];
  159. if (c == S->code_start[0])
  160. if (mix_iscode_start(S))
  161. break;
  162. if (c == '\\' || c == '"')
  163. S->temp[S->result_size++] = '\\';
  164. if (c == '\r' || c == '\n'){
  165. S->temp[S->result_size++] = '\\';
  166. if(c == '\r') c = 'r';
  167. else {
  168. S->new_lines++;
  169. c = 'n';
  170. }
  171. }
  172. S->temp[S->result_size++] = c;
  173. ++S->pos;
  174. if (S->result_size >= mix_tempsize - 1) {
  175. if (S->pos == S->size)
  176. S->token = tok_code_start;
  177. return S->result = S->temp;
  178. }
  179. }
  180. if (S->pos < S->size)
  181. S->pos += S->code_startsize;
  182. S->token = tok_code_start;
  183. if (S->result_size != 0)
  184. return S->result = S->temp;
  185. }
  186. /* Send the termination of the string and the function call before
  187. * going in tok_code/tok_expr state or if at eof go to tok_eof.
  188. */
  189. if (S->token == tok_code_start) {
  190. if (S->pos == S->size) {
  191. S->token = tok_eof;
  192. } else if (mix_isexpr(S)) {
  193. S->token = tok_expr_start;
  194. S->pos += S->exprsize;
  195. } else {
  196. S->token = tok_code;
  197. }
  198. return result_literal("\");");
  199. }
  200. /* Send the output function, but without the string start character, so
  201. * the result of the lua code will be send as parameters to the output
  202. * function.
  203. */
  204. if (S->token == tok_expr_start) {
  205. if(S->new_lines){
  206. --S->new_lines;
  207. return result_literal("\n");
  208. }
  209. S->token = tok_expr;
  210. S->result_size = S->print_outsize - 1;
  211. return S->result = S->print_out;
  212. }
  213. /* Send a lua chunk in one block to the lua engine. No escaping are
  214. * needed here so we can send the full block. Then switch to the
  215. * tok_code_end. (this correctly skip all form of lua string and comments)
  216. * If no lua code is found, go directly to the tok_code_end part.
  217. */
  218. if (S->token == tok_code || S->token == tok_expr) {
  219. if(S->new_lines){
  220. --S->new_lines;
  221. return result_literal("\n");
  222. }
  223. size_t old = S->pos;
  224. while (S->pos < S->size) {
  225. char c = S->buffer[S->pos];
  226. if (c == '\'') {
  227. ++S->pos;
  228. mix_skip_string(S, '\'');
  229. } else if (c == '"') {
  230. ++S->pos;
  231. mix_skip_string(S, '"');
  232. } else if (c == '[') {
  233. int level = mix_bracket_open(S);
  234. if (level != -1)
  235. mix_skip_lstring(S, level);
  236. else
  237. ++S->pos;
  238. } else if (c == '/') {
  239. ++S->pos;
  240. if (S->buffer[S->pos] == '/') {
  241. ++S->pos;
  242. mix_skip_comment(S);
  243. }
  244. } else if (c == S->code_end[0]) {
  245. if (mix_iscode_end(S))
  246. break;
  247. ++S->pos;
  248. } else {
  249. ++S->pos;
  250. }
  251. }
  252. S->result_size = S->pos - old;
  253. if (S->pos == S->size) {
  254. S->error = err_badeof;
  255. return NULL;
  256. }
  257. S->token = S->token == tok_code ? tok_code_end : tok_expr_end;
  258. S->pos += S->code_endsize;
  259. if (S->result_size != 0)
  260. return S->result = S->buffer + old;
  261. }
  262. /* Send the data output function name and (' that start a data block to
  263. * the lua engine and switch to the tok_data state.
  264. */
  265. if (S->token == tok_code_end) {
  266. if(S->new_lines){
  267. --S->new_lines;
  268. return result_literal("\n");
  269. }
  270. S->token = tok_data;
  271. S->result_size = S->print_outsize;
  272. return S->result = S->print_out;
  273. }
  274. /* Close the output function call on end of expression and go to the lua
  275. * to data transition.
  276. */
  277. if (S->token == tok_expr_end) {
  278. S->token = tok_code_end;
  279. return result_literal(");");
  280. }
  281. /* If we skipped a sh-bang line we must send an empty line to keep the
  282. * lua line counter correct.
  283. */
  284. if (S->token == tok_sh) {
  285. S->token = tok_code_end;
  286. ++S->new_lines;
  287. return result_literal("\n");
  288. }
  289. return NULL;
  290. }
  291. static MixInteger sq_mix_reader_char(void *ud) {
  292. mix_state_t *S = (mix_state_t *)ud;
  293. if (S->error != NULL) return 0;
  294. start:
  295. if (S->result_size){
  296. MixInteger c = S->result[S->result_pos++];
  297. if(S->result_pos >= S->result_size){
  298. S->result_pos = S->result_size = 0;
  299. }
  300. return c;
  301. }
  302. if(sq_mix_reader_str(ud)) goto start;
  303. return 0;
  304. }
  305. static void sq_mix_init(mix_state_t *S,
  306. char *buffer, int buffer_size,
  307. const char *print_out,
  308. const char *code_start,
  309. const char *code_end,
  310. const char *expr){
  311. memset(S, 0, sizeof(mix_state_t));
  312. snprintf(S->print_out, sizeof(S->print_out), "%s(\"", print_out ? print_out : "mix_write");
  313. S->print_outsize = strlen(S->print_out);
  314. S->code_start = code_start ? code_start : "{%";
  315. S->code_startsize = strlen(S->code_start);
  316. S->code_end = code_end ? code_end : "%}";
  317. S->code_endsize = strlen(S->code_end);
  318. S->expr = expr ? expr : "=";
  319. S->exprsize = strlen(S->expr);
  320. S->buffer = buffer;
  321. S->size = buffer_size;
  322. S->token = tok_code_end;
  323. }