1
/* Generated by re2c 0.5 on Wed Jun 18 02:07:15 2003 */
2
#line 1 "ircg_scanner.re"
4
+----------------------------------------------------------------------+
6
+----------------------------------------------------------------------+
7
| Copyright (c) 1997-2004 The PHP Group |
8
+----------------------------------------------------------------------+
9
| This source file is subject to version 3.0 of the PHP license, |
10
| that is bundled with this package in the file LICENSE, and is |
11
| available through the world-wide-web at the following url: |
12
| http://www.php.net/license/3_0.txt. |
13
| If you did not receive a copy of the PHP license and are unable to |
14
| obtain it through the world-wide-web, please send a note to |
15
| license@php.net so we can mail you a copy immediately. |
16
+----------------------------------------------------------------------+
17
| Author: Sascha Schumann <sascha@schumann.cx> |
18
+----------------------------------------------------------------------+
21
/* $Id: ircg_scanner.c,v 1.31 2004/01/08 08:15:53 andi Exp $ */
27
#include "php_ircg_smart_str.h"
28
#include "php_ircg_alloc.h"
30
static const char *color_list[] = {
55
int underline_tag_open;
67
#define YYFILL(n) do { } while (0)
68
#define YYCTYPE unsigned char
73
#define STD_PARA ircg_msg_scanner *ctx, const char *start, const char *YYCURSOR
74
#define STD_ARGS ctx, start, YYCURSOR
76
#define PASSTHRU() do { \
77
smart_str_appendl_ex(mctx.result, start, xp-start, 1); \
80
static inline void handle_scheme(STD_PARA)
83
smart_str_appendl_ex(&ctx->scheme, start, YYCURSOR - start, 1);
84
smart_str_0(&ctx->scheme);
87
static inline void handle_url(STD_PARA)
89
smart_str_appends_ex(ctx->result, "<a target=blank href=\"", 1);
90
smart_str_append_ex(ctx->result, &ctx->scheme, 1);
91
smart_str_appendl_ex(ctx->result, start, YYCURSOR - start, 1);
92
smart_str_appends_ex(ctx->result, "\">", 1);
93
smart_str_append_ex(ctx->result, &ctx->scheme, 1);
94
smart_str_appendl_ex(ctx->result, start, YYCURSOR - start, 1);
95
smart_str_appends_ex(ctx->result, "</a>", 1);
98
static void handle_color_digit(STD_PARA, int mode)
103
len = YYCURSOR - start;
106
nr = (start[0] - '0') * 10 + (start[1] - '0');
114
case 0: ctx->fg_code = nr; break;
115
case 1: ctx->bg_code = nr; break;
119
static void handle_hex(STD_PARA, int mode)
121
memcpy(mode == 0 ? ctx->fg_color : ctx->bg_color, start, 6);
124
#define IS_VALID_CODE(n) (n >= 0 && n <= 15)
126
static void finish_color_stuff(STD_PARA)
128
if (ctx->font_tag_open) {
129
smart_str_appends_ex(ctx->result, "</font>", 1);
130
ctx->font_tag_open = 0;
134
static void handle_bold(STD_PARA, int final)
136
switch (ctx->bold_tag_open) {
138
if (!final) smart_str_appends_ex(ctx->result, "<b>", 1);
141
smart_str_appends_ex(ctx->result, "</b>", 1);
145
ctx->bold_tag_open = 1 - ctx->bold_tag_open;
148
static void handle_underline(STD_PARA, int final)
150
switch (ctx->underline_tag_open) {
152
if (!final) smart_str_appends_ex(ctx->result, "<u>", 1);
155
smart_str_appends_ex(ctx->result, "</u>", 1);
159
ctx->underline_tag_open = 1 - ctx->underline_tag_open;
162
static void handle_italic(STD_PARA, int final)
164
switch (ctx->italic_tag_open) {
166
if (!final) smart_str_appends_ex(ctx->result, "<i>", 1);
169
smart_str_appends_ex(ctx->result, "</i>", 1);
173
ctx->italic_tag_open = 1 - ctx->italic_tag_open;
176
static void commit_color_stuff(STD_PARA)
178
finish_color_stuff(STD_ARGS);
180
if (IS_VALID_CODE(ctx->fg_code)) {
181
smart_str_appends_ex(ctx->result, "<font color=\"", 1);
182
smart_str_appends_ex(ctx->result, color_list[ctx->fg_code], 1);
183
smart_str_appends_ex(ctx->result, "\">", 1);
184
ctx->font_tag_open = 1;
188
#define ADD_CONST(entity) do { \
189
smart_str_appends_ex(result, entity, 1); \
192
static void commit_color_hex(STD_PARA)
194
finish_color_stuff(STD_ARGS);
196
if (ctx->fg_color[0] != 0) {
197
smart_str_appends_ex(ctx->result, "<font color=\"", 1);
198
smart_str_appendl_ex(ctx->result, ctx->fg_color, 6, 1);
199
smart_str_appends_ex(ctx->result, "\">", 1);
200
ctx->font_tag_open = 1;
204
static void do_reset(STD_PARA)
206
finish_color_stuff(STD_ARGS);
207
handle_bold(STD_ARGS, 1);
208
handle_underline(STD_ARGS, 1);
209
handle_italic(STD_ARGS, 1);
212
void ircg_mirc_color(const char *msg, smart_str *result, size_t msg_len, int auto_links, int gen_br)
214
const char *end, *xp, *q, *start;
215
ircg_msg_scanner mctx, *ctx = &mctx;
217
mctx.result = result;
218
mctx.scheme.c = NULL;
219
mctx.italic_tag_open = mctx.font_tag_open = mctx.bold_tag_open = mctx.underline_tag_open = 0;
222
msg_len = strlen(msg);
228
if (xp >= end) goto stop;
232
unsigned int yyaccept;
236
if((YYLIMIT - YYCURSOR) < 4) YYFILL(4);
239
case '\000': goto yy2;
240
case '\002': goto yy19;
241
case '\003': goto yy5;
242
case '\004': goto yy17;
243
case '\017': goto yy25;
244
case '\035': goto yy23;
245
case '\036': goto yy15;
246
case '\037': goto yy21;
301
case '\204': case '\223':
302
case '\224': goto yy13;
305
yy2: YYCURSOR = YYMARKER;
310
yych = *(YYMARKER = ++YYCURSOR);
377
{ PASSTHRU(); goto state_plain; }
378
yy5: yych = *++YYCURSOR;
381
{ mctx.fg_code = mctx.bg_code = -1; goto state_color_fg; }
382
yy7: yych = *++YYCURSOR;
385
{ ADD_CONST("<"); goto state_plain; }
386
yy9: yych = *++YYCURSOR;
389
{ ADD_CONST(">"); goto state_plain; }
390
yy11: yych = *++YYCURSOR;
393
{ ADD_CONST("&"); goto state_plain; }
394
yy13: yych = *++YYCURSOR;
397
{ ADD_CONST("""); goto state_plain; }
398
yy15: yych = *++YYCURSOR;
401
{ if (gen_br) ADD_CONST("<br>"); goto state_plain; }
402
yy17: yych = *++YYCURSOR;
405
{ mctx.fg_color[0] = mctx.bg_color[0] = 0; goto state_color_hex; }
406
yy19: yych = *++YYCURSOR;
409
{ handle_bold(STD_ARGS, 0); goto state_plain; }
410
yy21: yych = *++YYCURSOR;
413
{ handle_underline(STD_ARGS, 0); goto state_plain; }
414
yy23: yych = *++YYCURSOR;
417
{ handle_italic(STD_ARGS, 0); goto state_plain; }
418
yy25: yych = *++YYCURSOR;
421
{ do_reset(STD_ARGS); goto state_plain; }
422
yy27: yych = *++YYCURSOR;
425
if(YYLIMIT == YYCURSOR) YYFILL(1);
491
yy30: yych = *++YYCURSOR;
496
yy31: yych = *++YYCURSOR;
501
yy32: yych = *++YYCURSOR;
504
{ if (auto_links) { handle_scheme(STD_ARGS); goto state_url; } else { PASSTHRU(); goto state_plain; } }
513
unsigned int yyaccept;
517
if((YYLIMIT - YYCURSOR) < 6) YYFILL(6);
544
yych = *(YYMARKER = ++YYCURSOR);
570
{ finish_color_stuff(STD_ARGS); PASSTHRU(); goto state_plain; }
571
yy38: yych = *++YYCURSOR;
574
{ goto state_color_hex_bg; }
575
yy40: yych = *++YYCURSOR;
577
yy41: yych = *++YYCURSOR;
601
yy42: YYCURSOR = YYMARKER;
605
yy43: yych = *++YYCURSOR;
629
yy44: yych = *++YYCURSOR;
653
yy45: yych = *++YYCURSOR;
677
yy46: yych = *++YYCURSOR;
680
{ handle_hex(STD_ARGS, 0); goto state_color_hex_bg; }
686
state_color_hex_comma:
690
unsigned int yyaccept;
694
if(YYLIMIT == YYCURSOR) YYFILL(1);
700
yy50: yych = *++YYCURSOR;
703
{ goto state_color_hex_bg; }
704
yy52: yych = *++YYCURSOR;
707
{ YYCURSOR--; commit_color_hex(STD_ARGS); goto state_plain; }
717
unsigned int yyaccept;
721
if((YYLIMIT - YYCURSOR) < 6) YYFILL(6);
747
yych = *(YYMARKER = ++YYCURSOR);
773
{ commit_color_hex(STD_ARGS); PASSTHRU(); goto state_plain; }
774
yy58: yych = *++YYCURSOR;
776
yy59: yych = *++YYCURSOR;
800
yy60: YYCURSOR = YYMARKER;
804
yy61: yych = *++YYCURSOR;
828
yy62: yych = *++YYCURSOR;
852
yy63: yych = *++YYCURSOR;
876
yy64: yych = *++YYCURSOR;
879
{ handle_hex(STD_ARGS, 1); commit_color_hex(STD_ARGS); goto state_plain; }
888
unsigned int yyaccept;
892
if((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
919
case ';': case '=': case '?':
946
case 'Z': case '_': case 'a':
971
case 'z': case '~': goto yy68;
974
yy68: yych = *++YYCURSOR;
978
{ handle_url(STD_ARGS); goto state_plain; }
979
yy70: yych = *++YYCURSOR;
982
{ PASSTHRU(); goto state_plain; }
984
if(YYLIMIT == YYCURSOR) YYFILL(1);
1011
case ';': case '=': case '?':
1038
case 'Z': case '_': case 'a':
1063
case 'z': case '~': goto yy72;
1075
unsigned int yyaccept;
1079
if((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
1082
case ',': goto yy78;
1092
case '9': goto yy76;
1095
yy76: yych = *++YYCURSOR;
1106
case '9': goto yy82;
1111
{ handle_color_digit(STD_ARGS, 0); goto state_color_comma; }
1112
yy78: yych = *++YYCURSOR;
1115
{ goto state_color_bg; }
1116
yy80: yych = *++YYCURSOR;
1119
{ finish_color_stuff(STD_ARGS); PASSTHRU(); goto state_plain; }
1120
yy82: yych = *++YYCURSOR;
1131
unsigned int yyaccept;
1135
if(YYLIMIT == YYCURSOR) YYFILL(1);
1138
case ',': goto yy85;
1141
yy85: yych = *++YYCURSOR;
1144
{ goto state_color_bg; }
1145
yy87: yych = *++YYCURSOR;
1148
{ YYCURSOR--; commit_color_stuff(STD_ARGS); goto state_plain; }
1158
unsigned int yyaccept;
1162
if((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
1174
case '9': goto yy91;
1177
yy91: yych = *++YYCURSOR;
1188
case '9': goto yy95;
1193
{ handle_color_digit(STD_ARGS, 1); commit_color_stuff(STD_ARGS); goto state_plain; }
1194
yy93: yych = *++YYCURSOR;
1197
{ commit_color_stuff(STD_ARGS); PASSTHRU(); goto state_plain; }
1198
yy95: yych = *++YYCURSOR;
1205
smart_str_free_ex(&mctx.scheme, 1);