diff options
author | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2009-09-29 01:59:20 +0800 |
---|---|---|
committer | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2009-09-29 01:59:20 +0800 |
commit | 53f0e4b56edede152eeb45ebbf58332ea9d7a2e1 (patch) | |
tree | 0ef63de86ba91a443ec1f14251c5f127cf4ee60f | |
parent | 85b3ca2ccd6363ff3bfd8b5f075eb2e690dc47fb (diff) | |
download | pttbbs-53f0e4b56edede152eeb45ebbf58332ea9d7a2e1.tar pttbbs-53f0e4b56edede152eeb45ebbf58332ea9d7a2e1.tar.gz pttbbs-53f0e4b56edede152eeb45ebbf58332ea9d7a2e1.tar.bz2 pttbbs-53f0e4b56edede152eeb45ebbf58332ea9d7a2e1.tar.lz pttbbs-53f0e4b56edede152eeb45ebbf58332ea9d7a2e1.tar.xz pttbbs-53f0e4b56edede152eeb45ebbf58332ea9d7a2e1.tar.zst pttbbs-53f0e4b56edede152eeb45ebbf58332ea9d7a2e1.zip |
* refine kbd stack and move to common/bbs
* rename visio to vtuikit
git-svn-id: http://opensvn.csie.org/pttbbs/trunk@4888 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | pttbbs/common/sys/Makefile | 3 | ||||
-rw-r--r-- | pttbbs/common/sys/vtkbd.c (renamed from pttbbs/mbbsd/vtkbd.c) | 68 | ||||
-rw-r--r-- | pttbbs/include/bbs.h | 2 | ||||
-rw-r--r-- | pttbbs/include/common.h | 1 | ||||
-rw-r--r-- | pttbbs/include/vtkbd.h | 8 | ||||
-rw-r--r-- | pttbbs/include/vtuikit.h (renamed from pttbbs/include/visio.h) | 7 | ||||
-rw-r--r-- | pttbbs/mbbsd/Makefile | 4 | ||||
-rw-r--r-- | pttbbs/mbbsd/io.c | 61 | ||||
-rw-r--r-- | pttbbs/mbbsd/telnet.c | 71 | ||||
-rw-r--r-- | pttbbs/mbbsd/vtuikit.c (renamed from pttbbs/mbbsd/visio.c) | 14 |
10 files changed, 120 insertions, 119 deletions
diff --git a/pttbbs/common/sys/Makefile b/pttbbs/common/sys/Makefile index 717ccb0f..4de3a4cf 100644 --- a/pttbbs/common/sys/Makefile +++ b/pttbbs/common/sys/Makefile @@ -3,7 +3,8 @@ SRCROOT= ../.. .include "$(SRCROOT)/pttbbs.mk" -SRCS:= daemon.c file.c lock.c log.c net.c sort.c string.c time.c crypt.c record.c vector.c telnet.c +SRCS:= daemon.c file.c lock.c log.c net.c sort.c string.c time.c \ + crypt.c record.c vector.c telnet.c vtkbd.c LIB:= cmsys all: .depend diff --git a/pttbbs/mbbsd/vtkbd.c b/pttbbs/common/sys/vtkbd.c index 8a00671f..a5f4cf9e 100644 --- a/pttbbs/mbbsd/vtkbd.c +++ b/pttbbs/common/sys/vtkbd.c @@ -38,9 +38,9 @@ * --------------------------------------------------------------------------- * * BS/DEL Rules * The BackSpace, Erase(<X]), and Delete keys are special due to history... - * - on vt220, BS=0x7F, Delete=ESC[3~ (screen/xterm/PuTTY) + * - on vt220, BS=0x7F, Delete=ESC[3~ (screen/xterm/PuTTY/PCMan) * - on vt100, BS=0x7F - * - on vt100/xterm, BS=0x08, Delete=0x7F (Windows/VMX/DOS telnet) + * - on vt100/xterm, BS=0x08, Delete=0x7F (VMX/Windows/DOS telnet/KKMan) * So we define * KEY_BS = BACKSPACE/ERASE = 0x08, 0x7F * KEY_DEL = DELETE = ESC[3~ @@ -74,7 +74,9 @@ * Num pad is also always converted to digits. */ -#include "bbs.h" +#include <assert.h> +#include <string.h> +#include "vtkbd.h" /* VtkbdCtx.state */ typedef enum { @@ -88,6 +90,9 @@ typedef enum { VKSTATE_TLIDE, // <Esc> [ * (wait ~, return esc_arg) } VKSTATES; +#define VKRAW_BS 0x08 // \b = Ctrl('H') +#define VKRAW_ERASE 0x7F // <X] + /* the processor API */ int vtkbd_process(int c, VtkbdCtx *ctx) @@ -104,8 +109,8 @@ vtkbd_process(int c, VtkbdCtx *ctx) // simple mappings switch (c) { // BS/ERASE/DEL Rules - case 0x7F: - case 0x08: + case VKRAW_BS: + case VKRAW_ERASE: return KEY_BS; } return c; @@ -276,4 +281,57 @@ vtkbd_process(int c, VtkbdCtx *ctx) return KEY_UNKNOWN; } +ssize_t +vtkbd_ignore_dbcs_evil_repeats(const unsigned char *buf, ssize_t len) +{ + // determine DBCS repeats by evil clients + // NOTE: this is usually invoked before vtkbd_process, + // so we have to deal with the raw sequence. + if (len == 2) + { + // XXX len==2 is dangerous. hope we are not in telnet IAC state... + if (buf[0] != buf[1]) + return len; + + // targest here: + // - VKRAW_BS + // - VKRAW_ERASE + // - Ctrl('D') (KKMan3 also treats Ctrl('D') as DBCS DEL) + if (buf[0] == VKRAW_BS || + buf[0] == VKRAW_ERASE || + buf[0] == Ctrl('D')) + return len/2; + } + else if (len == 6) + { + // RIGHT: KEY_ESC "OC" or KEY_ESC "[C" + // LEFT: KEY_ESC "OD" or KEY_ESC "[D" + if (buf[2] != 'C' && buf[2] != 'D') + return len; + + if ( buf[0] == KEY_ESC && + (buf[1] == '[' || buf[1] == 'O') && + buf[0] == buf[3] && + buf[1] == buf[4] && + buf[2] == buf[5]) + return len/2; + } + else if (len == 8) + { + // DEL: ESC_STR "[3~" // vt220 + if (buf[0] != KEY_ESC || + buf[2] != '3' || + buf[1] != '[' || + buf[3] != '~') + return len; + + if( buf[4] == buf[0] && + buf[5] == buf[1] && + buf[6] == buf[2] && + buf[7] == buf[3]) + return len/2; + } + return len; +} + // vim:sw=4:sw=4:et diff --git a/pttbbs/include/bbs.h b/pttbbs/include/bbs.h index d16d5b02..7892f646 100644 --- a/pttbbs/include/bbs.h +++ b/pttbbs/include/bbs.h @@ -44,7 +44,7 @@ extern "C" { #include "ansi.h" #include "vtkbd.h" -#include "visio.h" +#include "vtuikit.h" #include "statistic.h" #include "uflags.h" #include "pttstruct.h" diff --git a/pttbbs/include/common.h b/pttbbs/include/common.h index a2829047..e584cd54 100644 --- a/pttbbs/include/common.h +++ b/pttbbs/include/common.h @@ -155,7 +155,6 @@ #define ST_REJECT (IRH | HRM) #define QCAST int (*)(const void *, const void *) -#define Ctrl(c) (c & 037) #define chartoupper(c) ((c >= 'a' && c <= 'z') ? c+'A'-'a' : c) #define LEN_AUTHOR1 5 diff --git a/pttbbs/include/vtkbd.h b/pttbbs/include/vtkbd.h index f1e998c8..609787d4 100644 --- a/pttbbs/include/vtkbd.h +++ b/pttbbs/include/vtkbd.h @@ -25,6 +25,8 @@ #ifndef _VTKBD_H #define _VTKBD_H +#include <sys/types.h> + /* context definition */ typedef struct { int state; @@ -32,7 +34,11 @@ typedef struct { } VtkbdCtx; /* vtkbd API */ -int vtkbd_process(int c, VtkbdCtx *ctx); +int vtkbd_process(int c, VtkbdCtx *ctx); +ssize_t vtkbd_ignore_dbcs_evil_repeats(const unsigned char *buf, ssize_t len); + +/* key code macro */ +#define Ctrl(c) (c & 0x1F) /* common ASCII compatible keys definition */ #define KEY_TAB 9 diff --git a/pttbbs/include/visio.h b/pttbbs/include/vtuikit.h index f2879ce6..09257345 100644 --- a/pttbbs/include/visio.h +++ b/pttbbs/include/vtuikit.h @@ -3,14 +3,15 @@ #define _VISIO_H /* - * visio.h - * piaip's new implementation of visio + * vtuikit.h + * piaip's new implementation of vtuikit * - * see visio.c for license, usage, and introduction. + * see vtuikit.c for license, usage, and introduction. */ #include "bbs.h" #include "ansi.h" // we need it. +#include "vtkbd.h" // we usually use it #include <limits.h> // THEME DEFINITION ---------------------------------------------------- diff --git a/pttbbs/mbbsd/Makefile b/pttbbs/mbbsd/Makefile index 98335987..fbaeb72c 100644 --- a/pttbbs/mbbsd/Makefile +++ b/pttbbs/mbbsd/Makefile @@ -10,14 +10,14 @@ SRCROOT= .. PROG= mbbsd COREOBJS = bbs.o announce.o read.o board.o cache.o cal.o brc.o mail.o record.o fav.o ACCOBJS = user.o register.o passwd.o emaildb.o -NETOBJS = mbbsd.o io.o term.o telnet.o vtkbd.o +NETOBJS = mbbsd.o io.o term.o telnet.o TALKOBJS = talk.o chat.o friend.o UTILOBJS = stuff.o kaede.o convert.o name.o syspost.o PAGEROBJS= more.o pmore.o PLUGOBJS = calendar.o ordersong.o gamble.o vice.o angel.o CHESSOBJS= chess.o chc.o chc_tab.o ch_go.o ch_gomo.o ch_dark.o ch_reversi.o GAMEOBJS = card.o chicken.o -OBJS:= admin.o assess.o edit.o menu.o xyz.o var.o visio.o \ +OBJS:= admin.o assess.o edit.o menu.o xyz.o var.o vtuikit.o \ vote.o voteboard.o \ $(COREOBJS) $(ACCOBJS) $(NETOBJS) $(TALKOBJS) $(UTILOBJS) \ $(PAGEROBJS) $(PLUGOBJS) \ diff --git a/pttbbs/mbbsd/io.c b/pttbbs/mbbsd/io.c index e0abbe31..3003b2af 100644 --- a/pttbbs/mbbsd/io.c +++ b/pttbbs/mbbsd/io.c @@ -158,7 +158,7 @@ static int i_newfd = 0; static struct timeval i_to, *i_top = NULL; static int (*flushf) () = NULL; -void +inline void add_io(int fd, int timeout) { i_newfd = fd; @@ -171,7 +171,7 @@ add_io(int fd, int timeout) i_top = NULL; } -int +inline int num_in_buf(void) { if (ibufsize <= icurrchar) @@ -179,12 +179,42 @@ num_in_buf(void) return ibufsize - icurrchar; } -int +inline int input_isfull(void) { return ibufsize >= IBUFSIZE; } +inline static ssize_t +wrapped_tty_read(unsigned char *buf, size_t max) +{ + /* tty_read will handle abort_bbs. + * len <= 0: read more */ + ssize_t len = tty_read(buf, max); + if (len <= 0) + return len; + + // apply additional converts +#ifdef DBCSAWARE + if (ISDBCSAWARE() && HasUserFlag(UF_DBCS_DROP_REPEAT)) + len = vtkbd_ignore_dbcs_evil_repeats(buf, len); +#endif +#ifdef CONVERT + len = input_wrapper(inbuf, len); +#endif +#ifdef DBG_OUTRPT + { + static char xbuf[128]; + sprintf(xbuf, ESC_STR "[s" ESC_STR "[2;1H [%ld] " + ESC_STR "[u", len); + write(1, xbuf, strlen(xbuf)); + fsync(1); + } +#endif // DBG_OUTRPT + return len; +} + + /* * dogetch() is not reentrant-safe. SIGUSR[12] might happen at any time, and * dogetch() might be called again, and then ibufsize/icurrchar/inbuf might @@ -246,24 +276,7 @@ dogetch(void) STATINC(STAT_SYSREADSOCKET); do { - len = tty_read(inbuf, IBUFSIZE); - /* tty_read will handle abort_bbs. - * len <= 0: read more */ -#ifdef CONVERT - if(len > 0) - len = input_wrapper(inbuf, len); -#endif -#ifdef DBG_OUTRPT - // if (0) - { - static char xbuf[128]; - sprintf(xbuf, ESC_STR "[s" ESC_STR "[2;1H [%ld] " - ESC_STR "[u", len); - write(1, xbuf, strlen(xbuf)); - fsync(1); - } -#endif // DBG_OUTRPT - + len = wrapped_tty_read(inbuf, IBUFSIZE); } while (len <= 0); ibufsize = len; @@ -677,11 +690,7 @@ peek_input(float f, int c) if (wait_input(f, 1) && (IBUFSIZE > ibufsize)) { - int len = tty_read(inbuf + ibufsize, IBUFSIZE - ibufsize); -#ifdef CONVERT - if(len > 0) - len = input_wrapper(inbuf+ibufsize, len); -#endif + int len = wrapped_tty_read(inbuf + ibufsize, IBUFSIZE - ibufsize); if (len > 0) ibufsize += len; } diff --git a/pttbbs/mbbsd/telnet.c b/pttbbs/mbbsd/telnet.c index 4e7daf53..7cf1fee5 100644 --- a/pttbbs/mbbsd/telnet.c +++ b/pttbbs/mbbsd/telnet.c @@ -43,72 +43,6 @@ telnet_init(int do_init_cmd) telnet_ctx_send_init_cmds(ctx); } -#if defined(DBCSAWARE) -ssize_t -dbcs_detect_evil_repeats(unsigned char *buf, ssize_t l) -{ - // determine DBCS repeats by evil clients (ref: io.c) - if (l == 2) - { - // XXX l=2 is dangerous. hope we are not in telnet IAC state... - // BS: \b - // BS2: \x7f - // DEL2: Ctrl('D') (KKMan3 also treats Ctrl('D') as DBCS DEL) - if (buf[0] != buf[1]) - return l; - - // Note: BS/DEL behavior on different clients: - // screen/telnet:BS=0x7F, DEL=^[3~ - // PCMan2004: BS=0x7F, DEL=^[3~ - // KKMan3: BS=0x1b, DEL=0x7F - // WinXP telnet: BS=0x1b, DEL=0x7F - if (buf[0] == '\b' || - buf[0] == '\x7f' || - buf[0] == Ctrl('D')) - return l-1; - } - else if (l == 6) - { - // RIGHT: ESC_CHR "OC" or ESC_CHR "[C" - // LEFT: ESC_CHR "OD" or ESC_CHR "[D" - if (buf[2] != 'C' && buf[2] != 'D') - return l; - - if ( buf[0] == ESC_CHR && - (buf[1] == '[' || buf[1] == 'O') && - buf[0] == buf[3] && - buf[1] == buf[4] && - buf[2] == buf[5]) - return l-3; - } - else if (l == 8) - { - // RIGHT: ESC_CHR "[OC" - // LEFT: ESC_CHR "[OD" - // DEL: ESC_STR "[3~" // vt220 - if (buf[2] != '3' && buf[2] != 'O') - return l; - - if (buf[0] != ESC_CHR || - buf[1] != '[' || - buf[4] != buf[0] || - buf[5] != buf[1] || - buf[6] != buf[2] || - buf[7] != buf[3]) - return l; - - if (buf[2] == '3' && - buf[3] == '~') - return l-4; - - if ( buf[2] == 'O' && - (buf[3] == 'C' || buf[3] == 'D') ) - return l-4; - } - return l; -} -#endif - /* tty_read * read from tty, process telnet commands if raw connection. * return: >0 = length, <=0 means read more, abort/eof is automatically processed. @@ -122,11 +56,6 @@ tty_read(unsigned char *buf, size_t max) if(l == 0 || (l < 0 && !(errno == EINTR || errno == EAGAIN))) abort_bbs(0); -#if defined(DBCSAWARE) - if (ISDBCSAWARE() && HasUserFlag(UF_DBCS_DROP_REPEAT)) - l = dbcs_detect_evil_repeats(buf, l); -#endif - if(!raw_connection || l <= 0) return l; diff --git a/pttbbs/mbbsd/visio.c b/pttbbs/mbbsd/vtuikit.c index 38efcbc9..9dbcdf55 100644 --- a/pttbbs/mbbsd/visio.c +++ b/pttbbs/mbbsd/vtuikit.c @@ -1,10 +1,10 @@ /* $Id$ */ +#include "vtkbd.h" #include "bbs.h" /* - * visio.c - * piaip's new implementation of visio - * (visio: virtual screen input output, the name from Maple3) + * vtuikit.c (was: visio.c) + * piaip's new implementation of virtual terminal user interface toolkits * * This is not the original visio.c from Maple3. * We just borrowed its file name and few API names/prototypes @@ -14,13 +14,11 @@ * but won't stick to it. * Maybe at the end only 'vmsg' and 'vmsgf' will still be compatible.... * - * m3 visio = (ptt) visio+screen/term. + * m3 visio = (ptt) vtuikit+vtkbd+screen/term. * * Author: Hung-Te Lin (piaip), April 2008. * - * Copyright (c) 2008 Hung-Te Lin <piaip@csie.ntu.edu.tw> - * All rights reserved. - * + * Copyright (c) 2008-2009 Hung-Te Lin <piaip@csie.ntu.edu.tw> * Distributed under BSD license (GPL compatible). * * Redistribution and use in source and binary forms, with or without @@ -36,7 +34,7 @@ * (1) name the API in prefix of 'v'. * (2) use only screen.c APIs. * (3) take care of wide screen and DBCS. - * (4) utilize the colos in visio.h, and name asa VCLR_* (visio color) + * (4) utilize the colos in vtuikit.h, and name asa VCLR_* (vtuikit color) */ // ---- DEFINITION --------------------------------------------------- |