File: ircg_scanner.re

package info (click to toggle)
php4 6%3A4.4.4-8%2Betch1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 34,976 kB
  • ctags: 39,148
  • sloc: ansic: 340,486; php: 34,786; cpp: 10,150; sh: 9,010; lex: 2,180; yacc: 1,712; xml: 1,335; makefile: 559; awk: 466; java: 455; perl: 154
file content (312 lines) | stat: -rw-r--r-- 8,181 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
   +----------------------------------------------------------------------+
   | PHP Version 4                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2006 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.01 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_01.txt                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author: Sascha Schumann <sascha@schumann.cx>                         |
   +----------------------------------------------------------------------+
 */

/* $Id: ircg_scanner.re,v 1.18.8.3.6.1 2006/01/01 13:46:54 sniper Exp $ */

#include <ext/standard/php_smart_str.h>
#include <stdio.h>
#include <string.h>

static const char *color_list[] = {
    "white",
    "black",
    "blue",
    "green",
    "red",
    "brown",
    "purple",
    "orange",
    "yellow",
    "lightgreen",
    "teal",
    "lightcyan",
    "lightblue",
    "#ff00ff",
    "#bebebe",
    "lightgrey"
};


typedef struct {
	int bg_code;
	int fg_code;
	int font_tag_open;
	int bold_tag_open;
	int underline_tag_open;
	int italic_tag_open;
	char fg_color[6];
	char bg_color[6];
	
	smart_str scheme;
	smart_str *result;
} ircg_msg_scanner;

/*!re2c
any = [\000-\377];
anynoneof = [\001-\377];
eof = [\000];
alpha = [a-zA-Z];
alnum = [a-zA-Z0-9];
digit = [0-9];
scheme = alpha alnum*;
coloresc = "\003";
colorhex = "\004";
bold = "\002";
underline = "\037";
italic = "\35";
ircnl = "\036";
winquotes = [\204\223\224];
hex = [a-fA-F0-9];
*/

#define YYFILL(n) do { } while (0)
#define YYCTYPE unsigned char
#define YYCURSOR xp
#define YYLIMIT end
#define YYMARKER q

#define STD_PARA ircg_msg_scanner *ctx, const char *start, const char *YYCURSOR
#define STD_ARGS ctx, start, YYCURSOR

#define passthru() do {									\
	size_t __len = xp - start;							\
	smart_str_appendl_ex(mctx.result, start, __len, 1); \
} while (0)

static inline void handle_scheme(STD_PARA)
{
	ctx->scheme.len = 0;
	smart_str_appendl_ex(&ctx->scheme, start, YYCURSOR - start, 1);
	smart_str_0(&ctx->scheme);
}

static inline void handle_url(STD_PARA)
{
	smart_str_appends_ex(ctx->result, "<a target=blank href=\"", 1);
	smart_str_append_ex(ctx->result, &ctx->scheme, 1);
	smart_str_appendl_ex(ctx->result, start, YYCURSOR - start, 1);
	smart_str_appends_ex(ctx->result, "\">", 1);
	smart_str_append_ex(ctx->result, &ctx->scheme, 1);
	smart_str_appendl_ex(ctx->result, start, YYCURSOR - start, 1);
	smart_str_appends_ex(ctx->result, "</a>", 1);
}

static void handle_color_digit(STD_PARA, int mode)
{
	int len;
	int nr;

	len = YYCURSOR - start;
	switch (len) {
		case 2:
			nr = (start[0] - '0') * 10 + (start[1] - '0');
			break;
		case 1:
			nr = start[0] - '0';
			break;
	}
	
	switch (mode) {
		case 0: ctx->fg_code = nr; break;
		case 1: ctx->bg_code = nr; break;
	}
}

static void handle_hex(STD_PARA, int mode)
{
	memcpy(mode == 0 ? ctx->fg_color : ctx->bg_color, start, 6);
}

#define IS_VALID_CODE(n) (n >= 0 && n <= 15)

static void finish_color_stuff(STD_PARA)
{
	if (ctx->font_tag_open) {
		smart_str_appends_ex(ctx->result, "</font>", 1);
		ctx->font_tag_open = 0;
	}
}

static void handle_bold(STD_PARA, int final)
{
	switch (ctx->bold_tag_open) {
	case 0:
		if (!final) smart_str_appends_ex(ctx->result, "<b>", 1);
		break;
	case 1:
		smart_str_appends_ex(ctx->result, "</b>", 1);
		break;
	}

	ctx->bold_tag_open = 1 - ctx->bold_tag_open;
}

static void handle_underline(STD_PARA, int final)
{
	switch (ctx->underline_tag_open) {
	case 0:
		if (!final) smart_str_appends_ex(ctx->result, "<u>", 1);
		break;
	case 1:
		smart_str_appends_ex(ctx->result, "</u>", 1);
		break;
	}

	ctx->underline_tag_open = 1 - ctx->underline_tag_open;
}

static void handle_italic(STD_PARA, int final)
{
	switch (ctx->italic_tag_open) {
	case 0:
		if (!final) smart_str_appends_ex(ctx->result, "<i>", 1);
		break;
	case 1:
		smart_str_appends_ex(ctx->result, "</i>", 1);
		break;
	}

	ctx->italic_tag_open = 1 - ctx->italic_tag_open;
}

static void commit_color_stuff(STD_PARA)
{
	finish_color_stuff(STD_ARGS);

	if (IS_VALID_CODE(ctx->fg_code)) {
		smart_str_appends_ex(ctx->result, "<font color=\"", 1);
		smart_str_appends_ex(ctx->result, color_list[ctx->fg_code], 1);
		smart_str_appends_ex(ctx->result, "\">", 1);
		ctx->font_tag_open = 1;
	}
}

static void commit_color_hex(STD_PARA)
{
	finish_color_stuff(STD_ARGS);

	if (ctx->fg_color[0] != 0) {
		smart_str_appends_ex(ctx->result, "<font color=\"", 1);
		smart_str_appendl_ex(ctx->result, ctx->fg_color, 6, 1);
		smart_str_appends_ex(ctx->result, "\">", 1);
		ctx->font_tag_open = 1;
	}
}

static void add_entity(STD_PARA, const char *entity)
{
	smart_str_appends_ex(ctx->result, entity, 1);
}

void ircg_mirc_color(const char *msg, smart_str *result, size_t msg_len, int auto_links, int gen_br) 
{
	const char *end, *xp, *q, *start;
	ircg_msg_scanner mctx, *ctx = &mctx;

	mctx.result = result;
	mctx.scheme.c = NULL;
	mctx.italic_tag_open = mctx.font_tag_open = mctx.bold_tag_open = mctx.underline_tag_open = 0;
	
	if (msg_len == -1)
		msg_len = strlen(msg);
	end = msg + msg_len;
	xp = msg;
	

state_plain:
	if (xp >= end) goto stop;
	start = YYCURSOR;
/*!re2c
	scheme "://"	{ if (auto_links) { handle_scheme(STD_ARGS); goto state_url; } else { passthru(); goto state_plain; } }
	coloresc 		{ mctx.fg_code = mctx.bg_code = -1; goto state_color_fg; }
	colorhex		{ mctx.fg_color[0] = mctx.bg_color[0] = 0; goto state_color_hex; }
	"<"				{ add_entity(STD_ARGS, "&lt;"); goto state_plain; }
	">"				{ add_entity(STD_ARGS, "&gt;"); goto state_plain; }
	"&"				{ add_entity(STD_ARGS, "&amp;"); goto state_plain; }
	winquotes		{ add_entity(STD_ARGS, "&quot;"); goto state_plain; }
	ircnl			{ if (gen_br) smart_str_appendl_ex(ctx->result, "<br>", 4, 1); goto state_plain; }
	bold			{ handle_bold(STD_ARGS, 0); goto state_plain; }
	underline		{ handle_underline(STD_ARGS, 0); goto state_plain; }
	italic			{ handle_italic(STD_ARGS, 0); goto state_plain; }
	anynoneof		{ passthru(); goto state_plain; }
*/

state_color_hex:
	start = YYCURSOR;
/*!re2c
  hex hex hex hex hex hex { handle_hex(STD_ARGS, 0); goto state_color_hex_bg; }
  ","					{ goto state_color_hex_bg; }
  any					{ finish_color_stuff(STD_ARGS); passthru(); goto state_plain; }
*/

	
state_color_hex_comma:	
	start = YYCURSOR;
/*!re2c
  ","					{ goto state_color_hex_bg; }
  any					{ YYCURSOR--; commit_color_hex(STD_ARGS); goto state_plain; }
*/


state_color_hex_bg:
	start = YYCURSOR;
/*!re2c
  hex hex hex hex hex hex	{ handle_hex(STD_ARGS, 1); commit_color_hex(STD_ARGS); goto state_plain; }
  any						{ commit_color_hex(STD_ARGS); passthru(); goto state_plain; }
*/

state_url:
	start = YYCURSOR;
/*!re2c
  	[-a-zA-Z0-9~_?=.@&+/#:;!*'()%,$]+		{ handle_url(STD_ARGS); goto state_plain; }
	any				{ passthru(); goto state_plain; }
*/


state_color_fg:		
	start = YYCURSOR;
/*!re2c
  	digit digit?		{ handle_color_digit(STD_ARGS, 0); goto state_color_comma; }
	","					{ goto state_color_bg; }
	any					{ finish_color_stuff(STD_ARGS); passthru(); goto state_plain; }
*/

	
state_color_comma:	
	start = YYCURSOR;
/*!re2c
  ","					{ goto state_color_bg; }
  any					{ YYCURSOR--; commit_color_stuff(STD_ARGS); goto state_plain; }
*/
	

state_color_bg:
	start = YYCURSOR;
/*!re2c
  	digit digit?		{ handle_color_digit(STD_ARGS, 1); commit_color_stuff(STD_ARGS); goto state_plain; }
	any					{ commit_color_stuff(STD_ARGS); passthru(); goto state_plain; }
*/

stop:
	smart_str_free_ex(&ctx->scheme, 1);

	finish_color_stuff(STD_ARGS);
	handle_bold(STD_ARGS, 1);
	handle_underline(STD_ARGS, 1);
	handle_italic(STD_ARGS, 1);
}