You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
6.0 KiB

28 years ago
24 years ago
28 years ago
25 years ago
27 years ago
25 years ago
27 years ago
28 years ago
27 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
24 years ago
28 years ago
24 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2003 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@zend.com> |
  16. | Zeev Suraski <zeev@zend.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "zend.h"
  20. #include "zend_language_parser.h"
  21. #include "zend_compile.h"
  22. #include "zend_highlight.h"
  23. #include "zend_ptr_stack.h"
  24. #include "zend_globals.h"
  25. ZEND_API void zend_html_putc(char c)
  26. {
  27. switch (c) {
  28. case '\n':
  29. ZEND_PUTS("<br />");
  30. break;
  31. case '<':
  32. ZEND_PUTS("&lt;");
  33. break;
  34. case '>':
  35. ZEND_PUTS("&gt;");
  36. break;
  37. case '&':
  38. ZEND_PUTS("&amp;");
  39. break;
  40. case ' ':
  41. ZEND_PUTS("&nbsp;");
  42. break;
  43. case '\t':
  44. ZEND_PUTS("&nbsp;&nbsp;&nbsp;&nbsp;");
  45. break;
  46. default:
  47. ZEND_PUTC(c);
  48. break;
  49. }
  50. }
  51. ZEND_API void zend_html_puts(const char *s, uint len TSRMLS_DC)
  52. {
  53. const char *ptr=s, *end=s+len;
  54. while (ptr<end) {
  55. if (*ptr==' ') {
  56. /* Series of spaces should be displayed as &nbsp;'s
  57. * whereas single spaces should be displayed as a space
  58. */
  59. if ((ptr+1) < end && *(ptr+1)==' ') {
  60. do {
  61. zend_html_putc(*ptr);
  62. } while ((++ptr < end) && (*ptr==' '));
  63. } else {
  64. ZEND_PUTC(*ptr);
  65. ptr++;
  66. }
  67. } else {
  68. zend_html_putc(*ptr++);
  69. }
  70. }
  71. }
  72. ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini TSRMLS_DC)
  73. {
  74. zval token;
  75. int token_type;
  76. char *last_color = syntax_highlighter_ini->highlight_html;
  77. char *next_color;
  78. int in_string=0;
  79. zend_printf("<code>");
  80. zend_printf("<font color=\"%s\">\n", last_color);
  81. /* highlight stuff coming back from zendlex() */
  82. token.type = 0;
  83. while ((token_type=lex_scan(&token TSRMLS_CC))) {
  84. switch (token_type) {
  85. case T_INLINE_HTML:
  86. next_color = syntax_highlighter_ini->highlight_html;
  87. break;
  88. case T_COMMENT:
  89. next_color = syntax_highlighter_ini->highlight_comment;
  90. break;
  91. case T_OPEN_TAG:
  92. case T_OPEN_TAG_WITH_ECHO:
  93. next_color = syntax_highlighter_ini->highlight_default;
  94. break;
  95. case T_CLOSE_TAG:
  96. next_color = syntax_highlighter_ini->highlight_default;
  97. break;
  98. case T_CONSTANT_ENCAPSED_STRING:
  99. next_color = syntax_highlighter_ini->highlight_string;
  100. break;
  101. case '"':
  102. next_color = syntax_highlighter_ini->highlight_string;
  103. in_string = !in_string;
  104. break;
  105. case T_WHITESPACE:
  106. zend_html_puts(LANG_SCNG(yy_text), LANG_SCNG(yy_leng) TSRMLS_CC); /* no color needed */
  107. token.type = 0;
  108. continue;
  109. break;
  110. default:
  111. if (token.type==0) {
  112. next_color = syntax_highlighter_ini->highlight_keyword;
  113. } else {
  114. if (in_string) {
  115. next_color = syntax_highlighter_ini->highlight_string;
  116. } else {
  117. next_color = syntax_highlighter_ini->highlight_default;
  118. }
  119. }
  120. break;
  121. }
  122. if (last_color != next_color) {
  123. if (last_color != syntax_highlighter_ini->highlight_html) {
  124. zend_printf("</font>");
  125. }
  126. last_color = next_color;
  127. if (last_color != syntax_highlighter_ini->highlight_html) {
  128. zend_printf("<font color=\"%s\">", last_color);
  129. }
  130. }
  131. switch (token_type) {
  132. case T_END_HEREDOC:
  133. zend_html_puts(token.value.str.val, token.value.str.len TSRMLS_CC);
  134. break;
  135. default:
  136. zend_html_puts(LANG_SCNG(yy_text), LANG_SCNG(yy_leng) TSRMLS_CC);
  137. break;
  138. }
  139. if (token.type == IS_STRING) {
  140. switch (token_type) {
  141. case T_OPEN_TAG:
  142. case T_OPEN_TAG_WITH_ECHO:
  143. case T_CLOSE_TAG:
  144. case T_WHITESPACE:
  145. case T_COMMENT:
  146. break;
  147. default:
  148. efree(token.value.str.val);
  149. break;
  150. }
  151. } else if (token_type == T_END_HEREDOC) {
  152. zend_bool has_semicolon=(strchr(token.value.str.val, ';')?1:0);
  153. efree(token.value.str.val);
  154. if (has_semicolon) {
  155. /* the following semicolon was unput(), ignore it */
  156. lex_scan(&token TSRMLS_CC);
  157. }
  158. }
  159. token.type = 0;
  160. }
  161. if (last_color != syntax_highlighter_ini->highlight_html) {
  162. zend_printf("</font>\n");
  163. }
  164. zend_printf("</font>\n");
  165. zend_printf("</code>");
  166. }
  167. ZEND_API void zend_strip(TSRMLS_D)
  168. {
  169. zval token;
  170. int token_type;
  171. token.type = 0;
  172. while ((token_type=lex_scan(&token TSRMLS_CC))) {
  173. switch (token_type) {
  174. case T_COMMENT:
  175. token.type = 0;
  176. continue;
  177. case T_WHITESPACE:
  178. if (token.type) {
  179. putchar(' ');
  180. token.type = 0;
  181. }
  182. continue;
  183. }
  184. switch (token_type) {
  185. case 349:
  186. break;
  187. default: {
  188. char c, *ptr=LANG_SCNG(yy_text), *end=LANG_SCNG(yy_text)+LANG_SCNG(yy_leng);
  189. while (ptr<end) {
  190. c = *ptr++;
  191. putchar(c);
  192. }
  193. }
  194. break;
  195. }
  196. if (token.type == IS_STRING) {
  197. switch (token_type) {
  198. case T_OPEN_TAG:
  199. case T_OPEN_TAG_WITH_ECHO:
  200. case T_CLOSE_TAG:
  201. case T_WHITESPACE:
  202. case T_COMMENT:
  203. break;
  204. default:
  205. efree(token.value.str.val);
  206. break;
  207. }
  208. } else if (token_type == T_END_HEREDOC) {
  209. zend_bool has_semicolon=(strchr(token.value.str.val, ';')?1:0);
  210. efree(token.value.str.val);
  211. if (has_semicolon) {
  212. /* the following semicolon was unput(), ignore it */
  213. lex_scan(&token TSRMLS_CC);
  214. }
  215. }
  216. token.type = 0;
  217. }
  218. }
  219. /*
  220. * Local variables:
  221. * tab-width: 4
  222. * c-basic-offset: 4
  223. * End:
  224. */