xref: /PHP-8.3/Zend/zend_highlight.c (revision f907a009)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 through the world-wide-web at the following url:           |
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@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    +----------------------------------------------------------------------+
18 */
19 
20 #include "zend.h"
21 #include <zend_language_parser.h>
22 #include "zend_compile.h"
23 #include "zend_highlight.h"
24 #include "zend_ptr_stack.h"
25 #include "zend_globals.h"
26 #include "zend_exceptions.h"
27 
zend_html_putc(char c)28 ZEND_API void zend_html_putc(char c)
29 {
30 	switch (c) {
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 '\t':
41 			ZEND_PUTS("    ");
42 			break;
43 		default:
44 			ZEND_PUTC(c);
45 			break;
46 	}
47 }
48 
49 
zend_html_puts(const char * s,size_t len)50 ZEND_API void zend_html_puts(const char *s, size_t len)
51 {
52 	const unsigned char *ptr = (const unsigned char*)s, *end = ptr + len;
53 	unsigned char *filtered = NULL;
54 	size_t filtered_len;
55 
56 	if (LANG_SCNG(output_filter)) {
57 		LANG_SCNG(output_filter)(&filtered, &filtered_len, ptr, len);
58 		ptr = filtered;
59 		end = filtered + filtered_len;
60 	}
61 
62 	while (ptr<end) {
63 		if (*ptr==' ') {
64 			do {
65 				zend_html_putc(*ptr);
66 			} while ((++ptr < end) && (*ptr==' '));
67 		} else {
68 			zend_html_putc(*ptr++);
69 		}
70 	}
71 
72 	if (LANG_SCNG(output_filter)) {
73 		efree(filtered);
74 	}
75 }
76 
77 
zend_highlight(zend_syntax_highlighter_ini * syntax_highlighter_ini)78 ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini)
79 {
80 	zval token;
81 	int token_type;
82 	char *last_color = syntax_highlighter_ini->highlight_html;
83 	char *next_color;
84 
85 	zend_printf("<pre><code style=\"color: %s\">", last_color);
86 	/* highlight stuff coming back from zendlex() */
87 	while ((token_type=lex_scan(&token, NULL))) {
88 		switch (token_type) {
89 			case T_INLINE_HTML:
90 				next_color = syntax_highlighter_ini->highlight_html;
91 				break;
92 			case T_COMMENT:
93 			case T_DOC_COMMENT:
94 				next_color = syntax_highlighter_ini->highlight_comment;
95 				break;
96 			case T_OPEN_TAG:
97 			case T_OPEN_TAG_WITH_ECHO:
98 			case T_CLOSE_TAG:
99 			case T_LINE:
100 			case T_FILE:
101 			case T_DIR:
102 			case T_TRAIT_C:
103 			case T_METHOD_C:
104 			case T_FUNC_C:
105 			case T_NS_C:
106 			case T_CLASS_C:
107 				next_color = syntax_highlighter_ini->highlight_default;
108 				break;
109 			case '"':
110 			case T_ENCAPSED_AND_WHITESPACE:
111 			case T_CONSTANT_ENCAPSED_STRING:
112 				next_color = syntax_highlighter_ini->highlight_string;
113 				break;
114 			case T_WHITESPACE:
115 				zend_html_puts((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));  /* no color needed */
116 				ZVAL_UNDEF(&token);
117 				continue;
118 				break;
119 			default:
120 				if (Z_TYPE(token) == IS_UNDEF) {
121 					next_color = syntax_highlighter_ini->highlight_keyword;
122 				} else {
123 					next_color = syntax_highlighter_ini->highlight_default;
124 				}
125 				break;
126 		}
127 
128 		if (last_color != next_color) {
129 			if (last_color != syntax_highlighter_ini->highlight_html) {
130 				zend_printf("</span>");
131 			}
132 			last_color = next_color;
133 			if (last_color != syntax_highlighter_ini->highlight_html) {
134 				zend_printf("<span style=\"color: %s\">", last_color);
135 			}
136 		}
137 
138 		zend_html_puts((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
139 
140 		if (Z_TYPE(token) == IS_STRING) {
141 			switch (token_type) {
142 				case T_OPEN_TAG:
143 				case T_OPEN_TAG_WITH_ECHO:
144 				case T_CLOSE_TAG:
145 				case T_WHITESPACE:
146 				case T_COMMENT:
147 				case T_DOC_COMMENT:
148 					break;
149 				default:
150 					zval_ptr_dtor_str(&token);
151 					break;
152 			}
153 		}
154 		ZVAL_UNDEF(&token);
155 	}
156 
157 	if (last_color != syntax_highlighter_ini->highlight_html) {
158 		zend_printf("</span>");
159 	}
160 	zend_printf("</code></pre>");
161 
162 	/* Discard parse errors thrown during tokenization */
163 	zend_clear_exception();
164 }
165 
zend_strip(void)166 ZEND_API void zend_strip(void)
167 {
168 	zval token;
169 	int token_type;
170 	int prev_space = 0;
171 
172 	while ((token_type=lex_scan(&token, NULL))) {
173 		switch (token_type) {
174 			case T_WHITESPACE:
175 				if (!prev_space) {
176 					zend_write(" ", sizeof(" ") - 1);
177 					prev_space = 1;
178 				}
179 				ZEND_FALLTHROUGH;
180 			case T_COMMENT:
181 			case T_DOC_COMMENT:
182 				ZVAL_UNDEF(&token);
183 				continue;
184 
185 			case T_END_HEREDOC:
186 				zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
187 				/* read the following character, either newline or ; */
188 				if (lex_scan(&token, NULL) != T_WHITESPACE) {
189 					zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
190 				}
191 				zend_write("\n", sizeof("\n") - 1);
192 				prev_space = 1;
193 				ZVAL_UNDEF(&token);
194 				continue;
195 
196 			default:
197 				zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
198 				break;
199 		}
200 
201 		if (Z_TYPE(token) == IS_STRING) {
202 			switch (token_type) {
203 				case T_OPEN_TAG:
204 				case T_OPEN_TAG_WITH_ECHO:
205 				case T_CLOSE_TAG:
206 				case T_WHITESPACE:
207 				case T_COMMENT:
208 				case T_DOC_COMMENT:
209 					break;
210 
211 				default:
212 					zval_ptr_dtor_str(&token);
213 					break;
214 			}
215 		}
216 		prev_space = 0;
217 		ZVAL_UNDEF(&token);
218 	}
219 
220 	/* Discard parse errors thrown during tokenization */
221 	zend_clear_exception();
222 }
223