diff options
author | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2009-10-29 15:35:42 +0800 |
---|---|---|
committer | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2009-10-29 15:35:42 +0800 |
commit | 1b533fe56c6884df5dffef5ee170ff36942f9327 (patch) | |
tree | dad4336831550ba7d6628c06766d8615bf872e6b | |
parent | 8b50c9f882456beb81cfe17ebb2cbc021bd11cb3 (diff) | |
download | pttbbs-1b533fe56c6884df5dffef5ee170ff36942f9327.tar pttbbs-1b533fe56c6884df5dffef5ee170ff36942f9327.tar.gz pttbbs-1b533fe56c6884df5dffef5ee170ff36942f9327.tar.bz2 pttbbs-1b533fe56c6884df5dffef5ee170ff36942f9327.tar.lz pttbbs-1b533fe56c6884df5dffef5ee170ff36942f9327.tar.xz pttbbs-1b533fe56c6884df5dffef5ee170ff36942f9327.tar.zst pttbbs-1b533fe56c6884df5dffef5ee170ff36942f9327.zip |
* change vkey_purge to the planned behavior
* isolate vkey layer
* use 'static inline' instead of 'inline static' to make it more clear...
* I'm not sure if any compiler will really care about the ordering here
git-svn-id: http://opensvn.csie.org/pttbbs/trunk@5011 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | pttbbs/mbbsd/ccw.c | 25 | ||||
-rw-r--r-- | pttbbs/mbbsd/io.c | 125 | ||||
-rw-r--r-- | pttbbs/mbbsd/register.c | 3 | ||||
-rw-r--r-- | pttbbs/mbbsd/stuff.c | 1 |
4 files changed, 90 insertions, 64 deletions
diff --git a/pttbbs/mbbsd/ccw.c b/pttbbs/mbbsd/ccw.c index 218601e6..d0b335c4 100644 --- a/pttbbs/mbbsd/ccw.c +++ b/pttbbs/mbbsd/ccw.c @@ -989,7 +989,7 @@ ccw_chat_recv(CCW_CTX *ctx) #ifdef EXP_ANTIFLOOD // prevent flooding */ -CCW_PROTO void +CCW_PROTO int ccw_chat_anti_flood(CCW_CTX *ctx) { ccw_chat_ext *ext = ccw_chat_get_ext(ctx); @@ -1000,26 +1000,26 @@ ccw_chat_anti_flood(CCW_CTX *ctx) // 3 秒內洗半面是不行的 ((25-5)/2) if( ++ext->flood > 10 ) { + const char *alert_msg = "請勿大量剪貼或造成洗板面的效果。"; + move(b_lines, 0); clrtoeol(); bell(); + outs(alert_msg); + doupdate(); // flush all input! - unsigned char garbage[STRLEN]; - vkey_purge(); - while (wait_input(1, 0)) - { - if (vkey_is_ready()) - vkey_purge(); - else - tty_read(garbage, sizeof(garbage)); - } vkey_purge(); - vmsg("請勿大量剪貼或造成洗板面的效果。"); + while (wait_input(1, 1)) + vkey_purge(); + vmsg(alert_msg); // log? sleep(2); + vkey_purge(); + return 1; } } else { ext->lasttime = now; ext->flood = 0; } + return 0; } #endif // EXP_ANTIFLOOD @@ -1028,7 +1028,8 @@ ccw_chat_peek_cmd(CCW_CTX *ctx, const char *buf, int local) { ccw_chat_check_newmail(ctx); #ifdef EXP_ANTIFLOOD - ccw_chat_anti_flood(ctx); + if (ccw_chat_anti_flood(ctx)) + return 1; #endif if (buf[0] != '/') return 0; diff --git a/pttbbs/mbbsd/io.c b/pttbbs/mbbsd/io.c index 8b0df55f..973518b7 100644 --- a/pttbbs/mbbsd/io.c +++ b/pttbbs/mbbsd/io.c @@ -52,7 +52,7 @@ static unsigned char fakeEscFilter(unsigned char c) /* ----------------------------------------------------- */ #ifdef CONVERT -inline static ssize_t input_wrapper(void *buf, ssize_t count) { +static inline ssize_t input_wrapper(void *buf, ssize_t count) { /* input_wrapper is a special case. * because we may do nothing, * a if-branch is better than a function-pointer call. @@ -63,11 +63,11 @@ inline static ssize_t input_wrapper(void *buf, ssize_t count) { return count; } -inline static int read_wrapper(int fd, void *buf, size_t count) { +static inline int read_wrapper(int fd, void *buf, size_t count) { return (*read_type)(fd, buf, count); } -inline static int write_wrapper(int fd, void *buf, size_t count) { +static inline int write_wrapper(int fd, void *buf, size_t count) { return (*write_type)(fd, buf, count); } #endif @@ -369,6 +369,8 @@ process_pager_keys(int ch) /* input routines */ /* ----------------------------------------------------- */ +// traditional implementation + static int i_newfd = 0; static struct timeval i_to, *i_top = NULL; @@ -393,25 +395,19 @@ num_in_buf(void) return ibufsize - icurrchar; } -inline int -vkey_is_ready(void) +static inline int +input_isfull(void) { - return num_in_buf() > 0; + return ibufsize >= IBUFSIZE; } -inline int -vkey_is_typeahead() +static inline void +drop_input(void) { - return num_in_buf() > 0; -} - -inline int -vkey_is_full(void) -{ - return ibufsize >= IBUFSIZE; + icurrchar = ibufsize = 0; } -inline static ssize_t +static inline ssize_t wrapped_tty_read(unsigned char *buf, size_t max) { /* tty_read will handle abort_bbs. @@ -544,8 +540,8 @@ dogetch(void) // virtual terminal keyboard context static VtkbdCtx vtkbd_ctx; -inline int -vkey(void) +static inline int +igetch(void) { register int ch; @@ -642,57 +638,88 @@ wait_input(float f, int bIgnoreBuf) return 1; } -inline int peek_input(float f, int c); // forward declaration +/* + * wait user input for f seconds. + * return 1 if control key c is available. + * (if c == EOF, simply read into buffer and return 0) + */ +inline int +peek_input(float f, int c) +{ + int i = 0; + assert (c == EOF || (c > 0 && c < ' ')); // only ^x keys are safe to be detected. + // other keys may fall into escape sequence. + + if (wait_input(f, 1) && (IBUFSIZE > ibufsize)) + { + int len = wrapped_tty_read(inbuf + ibufsize, IBUFSIZE - ibufsize); + if (len > 0) + ibufsize += len; + } + if (c == EOF) + return 0; + + // scan inbuf + for (i = icurrchar; i < ibufsize && inbuf[i] != c; i++) ; + return i < ibufsize ? 1 : 0; +} + + +/* vkey system emulation */ + +inline int +vkey_is_ready(void) +{ + return num_in_buf() > 0; +} + +inline int +vkey_is_typeahead() +{ + return num_in_buf() > 0; +} + +inline int +vkey_is_full(void) +{ + return input_isfull(); +} inline void vkey_purge(void) { - icurrchar = ibufsize = 0; - // XXX read more from fd and discard... - peek_input(0.1, Ctrl('C')); - icurrchar = ibufsize = 0; + int max_try = 64; + unsigned char garbage[4096]; + drop_input(); + + STATINC(STAT_SYSREADSOCKET); + while (wait_input(0.01, 1) && max_try-- > 0) { + wrapped_tty_read(garbage, sizeof(garbage)); + } } int -vkey_detach(void) +vkey_attach(int fd) { int r = i_newfd; - add_io(0, 0); + add_io(fd, 0); return r; } int -vkey_attach(int fd) +vkey_detach(void) { int r = i_newfd; - add_io(fd, 0); + add_io(0, 0); return r; } -/* - * wait user input for f seconds. - * return 1 if control key c is available. - */ -inline int -peek_input(float f, int c) +inline int +vkey(void) { - int i = 0; - assert (c > 0 && c < ' '); // only ^x keys are safe to be detected. - // other keys may fall into escape sequence. - - if (wait_input(f, 1) && (IBUFSIZE > ibufsize)) - { - int len = wrapped_tty_read(inbuf + ibufsize, IBUFSIZE - ibufsize); - if (len > 0) - ibufsize += len; - } - for (i = icurrchar; i < ibufsize; i++) - { - if (inbuf[i] == c) - return 1; - } - return 0; + return igetch(); } + /* vim:sw=4 */ diff --git a/pttbbs/mbbsd/register.c b/pttbbs/mbbsd/register.c index 5f7f84f4..5c2e9e77 100644 --- a/pttbbs/mbbsd/register.c +++ b/pttbbs/mbbsd/register.c @@ -453,9 +453,8 @@ static int verify_captcha() prints("請仔細檢查上面的圖形, %d 秒後即可輸入...", i); // flush out current input doupdate(); - peek_input(0.1f, Ctrl('C')); - vkey_purge(); sleep(1); + vkey_purge(); } // input captcha diff --git a/pttbbs/mbbsd/stuff.c b/pttbbs/mbbsd/stuff.c index ff40d6c4..1381c2e0 100644 --- a/pttbbs/mbbsd/stuff.c +++ b/pttbbs/mbbsd/stuff.c @@ -157,7 +157,6 @@ wait_penalty(int sec) if (sec < 0 || sec >= 5) sec = 5; sleep(sec); - peek_input(0.1, Ctrl('C')); vkey_purge(); } lastWait = now; |