xref: /PHP-8.4/Zend/zend_highlight.c (revision 780a8280)
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_PROPERTY_C:
106 			case T_NS_C:
107 			case T_CLASS_C:
108 				next_color = syntax_highlighter_ini->highlight_default;
109 				break;
110 			case '"':
111 			case T_ENCAPSED_AND_WHITESPACE:
112 			case T_CONSTANT_ENCAPSED_STRING:
113 				next_color = syntax_highlighter_ini->highlight_string;
114 				break;
115 			case T_WHITESPACE:
116 				zend_html_puts((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));  /* no color needed */
117 				ZVAL_UNDEF(&token);
118 				continue;
119 				break;
120 			default:
121 				if (Z_TYPE(token) == IS_UNDEF) {
122 					next_color = syntax_highlighter_ini->highlight_keyword;
123 				} else {
124 					next_color = syntax_highlighter_ini->highlight_default;
125 				}
126 				break;
127 		}
128 
129 		if (last_color != next_color) {
130 			if (last_color != syntax_highlighter_ini->highlight_html) {
131 				zend_printf("</span>");
132 			}
133 			last_color = next_color;
134 			if (last_color != syntax_highlighter_ini->highlight_html) {
135 				zend_printf("<span style=\"color: %s\">", last_color);
136 			}
137 		}
138 
139 		zend_html_puts((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
140 
141 		if (Z_TYPE(token) == IS_STRING) {
142 			switch (token_type) {
143 				case T_OPEN_TAG:
144 				case T_OPEN_TAG_WITH_ECHO:
145 				case T_CLOSE_TAG:
146 				case T_WHITESPACE:
147 				case T_COMMENT:
148 				case T_DOC_COMMENT:
149 					break;
150 				default:
151 					zval_ptr_dtor_str(&token);
152 					break;
153 			}
154 		}
155 		ZVAL_UNDEF(&token);
156 	}
157 
158 	if (last_color != syntax_highlighter_ini->highlight_html) {
159 		zend_printf("</span>");
160 	}
161 	zend_printf("</code></pre>");
162 
163 	/* Discard parse errors thrown during tokenization */
164 	zend_clear_exception();
165 }
166 
zend_strip(void)167 ZEND_API void zend_strip(void)
168 {
169 	zval token;
170 	int token_type;
171 	int prev_space = 0;
172 
173 	while ((token_type=lex_scan(&token, NULL))) {
174 		switch (token_type) {
175 			case T_WHITESPACE:
176 				if (!prev_space) {
177 					zend_write(" ", sizeof(" ") - 1);
178 					prev_space = 1;
179 				}
180 				ZEND_FALLTHROUGH;
181 			case T_COMMENT:
182 			case T_DOC_COMMENT:
183 				ZVAL_UNDEF(&token);
184 				continue;
185 
186 			case T_END_HEREDOC:
187 				zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
188 				/* read the following character, either newline or ; */
189 				if (lex_scan(&token, NULL) != T_WHITESPACE) {
190 					zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
191 				}
192 				zend_write("\n", sizeof("\n") - 1);
193 				prev_space = 1;
194 				ZVAL_UNDEF(&token);
195 				continue;
196 
197 			default:
198 				zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
199 				break;
200 		}
201 
202 		if (Z_TYPE(token) == IS_STRING) {
203 			switch (token_type) {
204 				case T_OPEN_TAG:
205 				case T_OPEN_TAG_WITH_ECHO:
206 				case T_CLOSE_TAG:
207 				case T_WHITESPACE:
208 				case T_COMMENT:
209 				case T_DOC_COMMENT:
210 					break;
211 
212 				default:
213 					zval_ptr_dtor_str(&token);
214 					break;
215 			}
216 		}
217 		prev_space = 0;
218 		ZVAL_UNDEF(&token);
219 	}
220 
221 	/* Discard parse errors thrown during tokenization */
222 	zend_clear_exception();
223 }
224