sas Fri Feb 16 11:01:23 2001 EDT Added files: /php4/ext/ircg ircg_scanner.c ircg_scanner.re Modified files: /php4/ext/ircg Makefile.in ircg.c Log: The new scanner adds HTML anchors automatically to a certain subset of RFC 2396 URIs. The scanner also handles mIRC color codes.
Index: php4/ext/ircg/Makefile.in diff -u php4/ext/ircg/Makefile.in:1.1 php4/ext/ircg/Makefile.in:1.2 --- php4/ext/ircg/Makefile.in:1.1 Mon Nov 13 06:51:09 2000 +++ php4/ext/ircg/Makefile.in Fri Feb 16 11:01:22 2001 @@ -1,8 +1,11 @@ -# $Id: Makefile.in,v 1.1 2000/11/13 14:51:09 sas Exp $ +# $Id: Makefile.in,v 1.2 2001/02/16 19:01:22 sas Exp $ LTLIBRARY_NAME = libircg.la -LTLIBRARY_SOURCES = ircg.c +LTLIBRARY_SOURCES = ircg.c ircg_scanner.c LTLIBRARY_SHARED_NAME = ircg.la LTLIBRARY_SHARED_LIBADD = $(IRCG_SHARED_LIBADD) include $(top_srcdir)/build/dynlib.mk + +$(srcdir)/ircg_scanner.c: $(srcdir)/ircg_scanner.re + re2c -b $(srcdir)/ircg_scanner.re > $@ Index: php4/ext/ircg/ircg.c diff -u php4/ext/ircg/ircg.c:1.22 php4/ext/ircg/ircg.c:1.23 --- php4/ext/ircg/ircg.c:1.22 Mon Jan 15 22:53:30 2001 +++ php4/ext/ircg/ircg.c Fri Feb 16 11:01:22 2001 @@ -16,6 +16,8 @@ +----------------------------------------------------------------------+ */ +/* $Id: ircg.c,v 1.23 2001/02/16 19:01:22 sas Exp $ */ + #include "php.h" #include "php_ini.h" #include "php_ircg.h" @@ -155,92 +157,8 @@ zend_llist_destroy(&conn->buffer); free(conn); } - -static const char *color_list[] = { - "white", - "black", - "blue", - "green", - "red", - "brown", - "purple", - "orange", - "yellow", - "lightgreen", - "teal", - "lightcyan", - "lightblue", - "pink", - "gray", - "lightgrey" -}; -static void mirc_color(const char *input, smart_str *result) -{ - int mode = 0; - const char *p; - char c; - int fg = -1, bg = -1; - int font_open = 0; - - for (p = input; (c = *p); p++) { - switch (mode) { - case 0: -evaluate_normal: - if (c == '') { - mode = 1; - bg = fg = -1; - } else { - if (font_open) { - smart_str_appends_ex(result, "</font>", 1); - font_open = 0; - } - if (fg >= 0 && fg <= 15) { - smart_str_appends_ex(result, "<font color=\"", 1); - smart_str_appends_ex(result, color_list[fg], 1); - smart_str_appends_ex(result, "\">", 1); - font_open = 1; - } - smart_str_appendc_ex(result, c, 1); - } - break; - case 1: - if (isdigit(c)) { - if (fg == -1) fg = 0; - fg = fg * 10 + c - '0'; - if (fg > 10) - mode = 2; - } else { - mode = 0; - goto evaluate_normal; - } - break; - case 2: - if (c == ',') { - mode = 3; - } else { - mode = 0; - goto evaluate_normal; - } - break; - case 3: - if (isdigit(c)) { - if (bg == -1) bg = 0; - bg = bg * 10 + c - '0'; - if (bg > 10) - mode = 0; - } else { - mode = 0; - goto evaluate_normal; - } - break; - } - } - if (font_open) { - smart_str_appends_ex(result, "</font>", 1); - font_open = 0; - } -} +void ircg_mirc_color(const char *, smart_str *); static void format_msg(const char *fmt, const char *channel, const char *to, const char *from, const char *msg, smart_str *result) { @@ -257,7 +175,7 @@ if (msg) { new_msg.len = 0; - mirc_color(msg, &new_msg); + ircg_mirc_color(msg, &new_msg); encoded_msg.c = php_escape_html_entities(new_msg.c, new_msg.len, &encoded_msg.len, 1, 1); } Index: php4/ext/ircg/ircg_scanner.c +++ php4/ext/ircg/ircg_scanner.c /* Generated by re2c 0.5 on Fri Feb 16 19:55:49 2001 */ #line 1 "/home/sas/src/php4/ext/ircg/ircg_scanner.re" /* +----------------------------------------------------------------------+ | PHP version 4.0 | +----------------------------------------------------------------------+ | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.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 | | [EMAIL PROTECTED] so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Sascha Schumann <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ /* $Id: ircg_scanner.c,v 1.1 2001/02/16 19:01:22 sas 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", "pink", "gray", "lightgrey" }; enum { STATE_PLAIN, STATE_URL, STATE_COLOR_FG, STATE_COLOR_COMMA, STATE_COLOR_BG }; typedef struct { int bg_code; int fg_code; int font_tag_open; smart_str scheme; smart_str *result; } ircg_msg_scanner; #line 71 #define YYFILL(n) { } #define YYCTYPE unsigned char #define YYCURSOR xp #define YYLIMIT end #define YYMARKER q #define STATE mode #define STD_PARA ircg_msg_scanner *ctx, const char *start, const char *YYCURSOR #define STD_ARGS ctx, start, YYCURSOR static void handle_scheme(STD_PARA) { ctx->scheme.len = 0; smart_str_appendl(&ctx->scheme, start, YYCURSOR - start); smart_str_0(&ctx->scheme); } static void handle_url(STD_PARA) { smart_str_appends(ctx->result, "<a target=blank href=\""); smart_str_append(ctx->result, &ctx->scheme); smart_str_appendl(ctx->result, start, YYCURSOR - start); smart_str_appends(ctx->result, "\">"); smart_str_append(ctx->result, &ctx->scheme); smart_str_appendl(ctx->result, start, YYCURSOR - start); smart_str_appends(ctx->result, "</a>"); } 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; } } #define IS_VALID_CODE(n) (n >= 0 && n <= 15) static void finish_color_stuff(STD_PARA) { if (ctx->font_tag_open) { smart_str_appends(ctx->result, "</font>"); ctx->font_tag_open = 0; } } static void commit_color_stuff(STD_PARA) { finish_color_stuff(STD_ARGS); if (IS_VALID_CODE(ctx->fg_code)) { smart_str_appends(ctx->result, "<font color=\""); smart_str_appends(ctx->result, color_list[ctx->fg_code]); smart_str_appends(ctx->result, "\">"); ctx->font_tag_open = 1; } } static void passthru(STD_PARA) { smart_str_appendl(ctx->result, start, YYCURSOR - start); } void ircg_mirc_color(const char *msg, smart_str *result) { int mode = STATE_PLAIN; const char *end, *xp, *q, *start; size_t msg_len; ircg_msg_scanner mctx, *ctx = &mctx; mctx.result = result; mctx.scheme.c = NULL; mctx.font_tag_open = 0; msg_len = strlen(msg); end = msg + msg_len; xp = msg; while (1) { start = YYCURSOR; switch (STATE) { case STATE_PLAIN: { YYCTYPE yych; unsigned int yyaccept; static unsigned char yybm[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; goto yy0; yy1: ++YYCURSOR; yy0: if((YYLIMIT - YYCURSOR) < 4) YYFILL(4); yych = *YYCURSOR; if(yych <= '@'){ if(yych <= '\000') goto yy7; if(yych == '\003') goto yy4; goto yy6; } else { if(yych <= 'Z') goto yy2; if(yych <= '`') goto yy6; if(yych >= '{') goto yy6; } yy2: yyaccept = 0; yych = *(YYMARKER = ++YYCURSOR); if(yych <= '@'){ if(yych <= '/') goto yy3; if(yych <= ':') goto yy10; } else { if(yych <= 'Z') goto yy10; if(yych <= '`') goto yy3; if(yych <= 'z') goto yy10; } yy3: #line 172 { passthru(STD_ARGS); continue; } yy4: yych = *++YYCURSOR; yy5: #line 171 { mctx.fg_code = mctx.bg_code = -1; STATE = STATE_COLOR_FG; continue; } yy6: yych = *++YYCURSOR; goto yy3; yy7: yych = *++YYCURSOR; yy8: #line 173 { goto stop; } yy9: ++YYCURSOR; if(YYLIMIT == YYCURSOR) YYFILL(1); yych = *YYCURSOR; yy10: if(yybm[0+yych] & 128) goto yy9; if(yych == ':') goto yy12; yy11: YYCURSOR = YYMARKER; switch(yyaccept){ case 0: goto yy3; } yy12: yych = *++YYCURSOR; if(yych != '/') goto yy11; yy13: yych = *++YYCURSOR; if(yych != '/') goto yy11; yy14: yych = *++YYCURSOR; yy15: #line 170 { handle_scheme(STD_ARGS); STATE = STATE_URL; continue; } } #line 174 break; case STATE_URL: { YYCTYPE yych; unsigned int yyaccept; static unsigned char yybm[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 128, 0, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 0, 0, 0, 128, 0, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; goto yy16; yy17: ++YYCURSOR; yy16: if((YYLIMIT - YYCURSOR) < 2) YYFILL(2); yych = *YYCURSOR; if(yych <= '>'){ if(yych <= '"'){ if(yych != '!') goto yy20; } else { if(yych == '<') goto yy20; if(yych >= '>') goto yy20; } } else { if(yych <= '`'){ if(yych <= 'Z') goto yy18; if(yych != '_') goto yy20; } else { if(yych <= 'z') goto yy18; if(yych != '~') goto yy20; } } yy18: yych = *++YYCURSOR; goto yy23; yy19: #line 182 { handle_url(STD_ARGS); STATE = STATE_PLAIN; continue; } yy20: yych = *++YYCURSOR; yy21: #line 183 { passthru(STD_ARGS); STATE = STATE_PLAIN; continue; } yy22: ++YYCURSOR; if(YYLIMIT == YYCURSOR) YYFILL(1); yych = *YYCURSOR; yy23: if(yybm[0+yych] & 128) goto yy22; goto yy19; } #line 184 break; case STATE_COLOR_FG: { YYCTYPE yych; unsigned int yyaccept; goto yy24; yy25: ++YYCURSOR; yy24: if((YYLIMIT - YYCURSOR) < 2) YYFILL(2); yych = *YYCURSOR; if(yych <= '/') goto yy28; if(yych >= ':') goto yy28; yy26: yych = *++YYCURSOR; if(yych <= '/') goto yy27; if(yych <= '9') goto yy30; yy27: #line 191 { handle_color_digit(STD_ARGS, 0); STATE = STATE_COLOR_COMMA; continue; } yy28: yych = *++YYCURSOR; yy29: #line 192 { finish_color_stuff(STD_ARGS); passthru(STD_ARGS); STATE = STATE_PLAIN; continue; } yy30: yych = *++YYCURSOR; goto yy27; } #line 193 break; case STATE_COLOR_COMMA: { YYCTYPE yych; unsigned int yyaccept; goto yy31; yy32: ++YYCURSOR; yy31: if(YYLIMIT == YYCURSOR) YYFILL(1); yych = *YYCURSOR; if(yych != ',') goto yy35; yy33: yych = *++YYCURSOR; yy34: #line 200 { STATE = STATE_COLOR_BG; continue; } yy35: yych = *++YYCURSOR; yy36: #line 201 { YYCURSOR--; commit_color_stuff(STD_ARGS); STATE = STATE_PLAIN; continue; } } #line 202 break; case STATE_COLOR_BG: { YYCTYPE yych; unsigned int yyaccept; goto yy37; yy38: ++YYCURSOR; yy37: if((YYLIMIT - YYCURSOR) < 2) YYFILL(2); yych = *YYCURSOR; if(yych <= '/') goto yy41; if(yych >= ':') goto yy41; yy39: yych = *++YYCURSOR; if(yych <= '/') goto yy40; if(yych <= '9') goto yy43; yy40: #line 209 { handle_color_digit(STD_ARGS, 1); commit_color_stuff(STD_ARGS); STATE = STATE_PLAIN; continue; } yy41: yych = *++YYCURSOR; yy42: #line 210 { commit_color_stuff(STD_ARGS); STATE = STATE_PLAIN; continue; } yy43: yych = *++YYCURSOR; goto yy40; } #line 211 break; } } stop: smart_str_free(&ctx->scheme); finish_color_stuff(STD_ARGS); } Index: php4/ext/ircg/ircg_scanner.re +++ php4/ext/ircg/ircg_scanner.re /* +----------------------------------------------------------------------+ | PHP version 4.0 | +----------------------------------------------------------------------+ | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.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 | | [EMAIL PROTECTED] so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Sascha Schumann <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ /* $Id: ircg_scanner.re,v 1.1 2001/02/16 19:01:22 sas 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", "pink", "gray", "lightgrey" }; enum { STATE_PLAIN, STATE_URL, STATE_COLOR_FG, STATE_COLOR_COMMA, STATE_COLOR_BG }; typedef struct { int bg_code; int fg_code; int font_tag_open; 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 = ""; */ #define YYFILL(n) { } #define YYCTYPE unsigned char #define YYCURSOR xp #define YYLIMIT end #define YYMARKER q #define STATE mode #define STD_PARA ircg_msg_scanner *ctx, const char *start, const char *YYCURSOR #define STD_ARGS ctx, start, YYCURSOR static void handle_scheme(STD_PARA) { ctx->scheme.len = 0; smart_str_appendl(&ctx->scheme, start, YYCURSOR - start); smart_str_0(&ctx->scheme); } static void handle_url(STD_PARA) { smart_str_appends(ctx->result, "<a target=blank href=\""); smart_str_append(ctx->result, &ctx->scheme); smart_str_appendl(ctx->result, start, YYCURSOR - start); smart_str_appends(ctx->result, "\">"); smart_str_append(ctx->result, &ctx->scheme); smart_str_appendl(ctx->result, start, YYCURSOR - start); smart_str_appends(ctx->result, "</a>"); } 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; } } #define IS_VALID_CODE(n) (n >= 0 && n <= 15) static void finish_color_stuff(STD_PARA) { if (ctx->font_tag_open) { smart_str_appends(ctx->result, "</font>"); ctx->font_tag_open = 0; } } static void commit_color_stuff(STD_PARA) { finish_color_stuff(STD_ARGS); if (IS_VALID_CODE(ctx->fg_code)) { smart_str_appends(ctx->result, "<font color=\""); smart_str_appends(ctx->result, color_list[ctx->fg_code]); smart_str_appends(ctx->result, "\">"); ctx->font_tag_open = 1; } } static void passthru(STD_PARA) { smart_str_appendl(ctx->result, start, YYCURSOR - start); } void ircg_mirc_color(const char *msg, smart_str *result) { int mode = STATE_PLAIN; const char *end, *xp, *q, *start; size_t msg_len; ircg_msg_scanner mctx, *ctx = &mctx; mctx.result = result; mctx.scheme.c = NULL; mctx.font_tag_open = 0; msg_len = strlen(msg); end = msg + msg_len; xp = msg; while (1) { start = YYCURSOR; switch (STATE) { case STATE_PLAIN: /*!re2c scheme "://" { handle_scheme(STD_ARGS); STATE = STATE_URL; continue; } coloresc { mctx.fg_code = mctx.bg_code = -1; STATE = STATE_COLOR_FG; continue; } anynoneof { passthru(STD_ARGS); continue; } eof { goto stop; } */ break; case STATE_URL: /*!re2c [a-zA-Z0-9\./\-~_?=@&+#:;!*'()%,$]+ { handle_url(STD_ARGS); STATE = STATE_PLAIN; continue; } any { passthru(STD_ARGS); STATE = STATE_PLAIN; continue; } */ break; case STATE_COLOR_FG: /*!re2c digit digit? { handle_color_digit(STD_ARGS, 0); STATE = STATE_COLOR_COMMA; continue; } any { finish_color_stuff(STD_ARGS); passthru(STD_ARGS); STATE = STATE_PLAIN; continue; } */ break; case STATE_COLOR_COMMA: /*!re2c "," { STATE = STATE_COLOR_BG; continue; } any { YYCURSOR--; commit_color_stuff(STD_ARGS); STATE = STATE_PLAIN; continue; } */ break; case STATE_COLOR_BG: /*!re2c digit digit? { handle_color_digit(STD_ARGS, 1); commit_color_stuff(STD_ARGS); STATE = STATE_PLAIN; continue; } any { commit_color_stuff(STD_ARGS); STATE = STATE_PLAIN; continue; } */ break; } } stop: smart_str_free(&ctx->scheme); finish_color_stuff(STD_ARGS); }
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]