diff options
author | victor <victor@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2004-01-03 17:53:35 +0800 |
---|---|---|
committer | victor <victor@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2004-01-03 17:53:35 +0800 |
commit | 38262b22bfe8cfe1f7de6f272e18a3aae340f83a (patch) | |
tree | 819e75228a3d78d922eaaa9a4e81df89219457da /mbbsd | |
parent | 63bc2dad6f3d3fdb6b3495a4eda10a14a5647c6a (diff) | |
download | pttbbs-38262b22bfe8cfe1f7de6f272e18a3aae340f83a.tar pttbbs-38262b22bfe8cfe1f7de6f272e18a3aae340f83a.tar.gz pttbbs-38262b22bfe8cfe1f7de6f272e18a3aae340f83a.tar.bz2 pttbbs-38262b22bfe8cfe1f7de6f272e18a3aae340f83a.tar.lz pttbbs-38262b22bfe8cfe1f7de6f272e18a3aae340f83a.tar.xz pttbbs-38262b22bfe8cfe1f7de6f272e18a3aae340f83a.tar.zst pttbbs-38262b22bfe8cfe1f7de6f272e18a3aae340f83a.zip |
support utf8 convertion, but still buggy
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@1456 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'mbbsd')
-rw-r--r-- | mbbsd/convert.c | 94 | ||||
-rw-r--r-- | mbbsd/io.c | 39 | ||||
-rw-r--r-- | mbbsd/mbbsd.c | 18 |
3 files changed, 60 insertions, 91 deletions
diff --git a/mbbsd/convert.c b/mbbsd/convert.c index 141548d1..75480ab1 100644 --- a/mbbsd/convert.c +++ b/mbbsd/convert.c @@ -4,74 +4,58 @@ * The following code is copied and modified from "autoconvert" with GPL. */ -#ifdef GB_CONVERT +#ifdef CONVERT -#include "convert.h" +extern read_write_type write_type; +extern read_write_type read_type; -char *hzconvert(char *, int *, char *, void (*dbcvrt)()); - -extern const unsigned char GtoB[], BtoG[]; - -#define c1 (unsigned char)(s[0]) -#define c2 (unsigned char)(s[1]) - -static void g2b(char *s) +static int gb_read(int fd, void *buf, size_t count) { - unsigned int i; - - if ((c1 >= 0x81) && (c1 <= 0xfe) && (c2 >= 0x40) && (c2 <= 0xfe) && (c2 != 0x7f)) { - /* - * c1c2 in the table - */ - if (c2 < 0x7f) - i = ((c1 - 0x81) * 190 + (c2 - 0x40)) * 2; - else - i = ((c1 - 0x81) * 190 + (c2 - 0x41)) * 2; - s[0] = GtoB[i++]; - s[1] = GtoB[i]; - } else { /* c1c2 not in the table */ - if ((char) s[1] >= 0) /* half HANZI-character */ - s[0] = '?'; - else { /* invalid gbk-character */ - s[0] = GtoB_bad1; - s[1] = GtoB_bad2; - } - } + int len = read(fd, buf, count); + if (len > 0) + gb2big(buf, len); + return len; } -static void b2g(char *s) +static int gb_write(int fd, void *buf, size_t count) { - unsigned int i; + big2gb(buf, count); + return write(fd, buf, count); +} - if ((c1 >= 0x81) && (c1 <= 0xfe) && (c2 >= 0x40) && (c2 <= 0xfe) && (c2 != 0x7f)) { - /* - * c1c2 in the table - */ - if (c2 < 0x7f) - i = ((c1 - 0x81) * 190 + (c2 - 0x40)) * 2; - else - i = ((c1 - 0x81) * 190 + (c2 - 0x41)) * 2; - s[0] = BtoG[i++]; - s[1] = BtoG[i]; - } else { /* c1c2 not in the table */ - if ((char) s[1] >= 0) /* half HANZI-character */ - s[0] = '?'; - else { /* invalid big5-character */ - s[0] = BtoG_bad1; - s[1] = BtoG_bad2; - } +static int utf8_read(int fd, void *buf, size_t count) +{ + count = read(fd, buf, count); + if (count > 0) { + strcpy(buf, utf8_uni(buf, &count, 0)); + uni2big(buf, &count, 0); + ((char *)buf)[count] = 0; } + return count; } -unsigned char *gb2big(unsigned char *s, int plen) +static int utf8_write(int fd, void *buf, size_t count) { - unsigned char c = 0; - return hzconvert(s, &plen, &c, g2b); + strcpy(buf, big2uni(buf, &count, 0)); + uni_utf8(buf, &count, 0); + ((char *)buf)[count] = 0; + return write(fd, buf, count); } -unsigned char *big2gb(unsigned char *s, int plen) +void set_converting_type(int which) { - unsigned char c = 0; - return hzconvert(s, &plen, &c, b2g); + if (which == CONV_NORMAL) { + read_type = read; + write_type = (read_write_type)write; + } + else if (which == CONV_GB) { + read_type = gb_read; + write_type = gb_write; + } + else if (which == CONV_UTF8) { + read_type = utf8_read; + write_type = utf8_write; + } } + #endif @@ -17,37 +17,10 @@ static int icurrchar = 0; /* ----------------------------------------------------- */ /* convert routines */ /* ----------------------------------------------------- */ -#ifdef GB_CONVERT +#ifdef CONVERT -typedef int (* read_write_type)(int, void *, size_t); -static read_write_type write_type = (read_write_type)write; -static read_write_type read_type = read; - -int converting_read(int fd, void *buf, size_t count) -{ - int len = read(fd, buf, count); - if (len >= 0) - gb2big(buf, len); - return len; -} - -int converting_write(int fd, void *buf, size_t count) -{ - big2gb(buf, count); - return write(fd, buf, count); -} - -void set_converting_type(int which) -{ - if (which == 0) { - read_type = read; - write_type = (read_write_type)write; - } - else if (which == 1) { - read_type = converting_read; - write_type = converting_write; - } -} +read_write_type write_type = (read_write_type)write; +read_write_type read_type = read; inline static int read_wrapper(int fd, void *buf, size_t count) { return (*read_type)(fd, buf, count); @@ -65,7 +38,7 @@ void oflush() { if (obufsize) { -#ifdef GB_CONVERT +#ifdef CONVERT write_wrapper(1, outbuf, obufsize); #else write(1, outbuf, obufsize); @@ -87,7 +60,7 @@ output(char *s, int len) assert(len<OBUFSIZE); if (obufsize + len > OBUFSIZE) { -#ifdef GB_CONVERT +#ifdef CONVERT write_wrapper(1, outbuf, obufsize); #else write(1, outbuf, obufsize); @@ -193,7 +166,7 @@ dogetch() do{ #endif -#ifdef GB_CONVERT +#ifdef CONVERT while ((len = read_wrapper(0, inbuf, IBUFSIZE)) <= 0) { #else while ((len = read(0, inbuf, IBUFSIZE)) <= 0) { diff --git a/mbbsd/mbbsd.c b/mbbsd/mbbsd.c index 425406f1..5bd1185b 100644 --- a/mbbsd/mbbsd.c +++ b/mbbsd/mbbsd.c @@ -512,7 +512,7 @@ inline static void mkuserdir(char *userid) static void login_query() { -#ifdef GB_CONVERT +#ifdef CONVERT /* uid 加一位, for gb login */ char uid[IDLEN + 2], passbuf[PASSLEN]; int attempts, len; @@ -547,11 +547,15 @@ login_query() getdata(20, 0, "請輸入代號,或以[guest]參觀,以[new]註冊:", uid, sizeof(uid), DOECHO); -#ifdef GB_CONVERT +#ifdef CONVERT /* switch to gb mode if uid end with '.' */ len = strlen(uid); if (uid[0] && uid[len - 1] == '.') { - set_converting_type(1); + set_converting_type(CONV_GB); + uid[len - 1] = 0; + } + else if (uid[0] && uid[len - 1] == ',') { + set_converting_type(CONV_UTF8); uid[len - 1] = 0; } else if (len >= IDLEN + 1) @@ -1337,6 +1341,14 @@ daemon_login(int argc, char *argv[], char *envp[]) setuid(BBSUID); chdir(BBSHOME); + /* It's better to do something before fork */ +#ifdef CONVERT + big2gb_init(); + gb2big_init(); + big2uni_init(); + uni2big_init(); +#endif + #ifndef NO_FORK #ifdef PRE_FORK if( listen_port == 23 ){ // only pre-fork in port 23 |