From ec1c94c5dd76df278b9d99e583c325c6baec989d Mon Sep 17 00:00:00 2001 From: scw Date: Tue, 29 Jun 2004 07:37:22 +0000 Subject: Parse a little TELNET control string. Support big screen on telnet connection. git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@2098 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- mbbsd/io.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- mbbsd/mbbsd.c | 28 ++++++++++++++++++++-------- mbbsd/term.c | 60 ++++++++++++++++++++++++++++++++++++++++------------------- 3 files changed, 116 insertions(+), 29 deletions(-) (limited to 'mbbsd') diff --git a/mbbsd/io.c b/mbbsd/io.c index 584faa29..3282b041 100644 --- a/mbbsd/io.c +++ b/mbbsd/io.c @@ -10,7 +10,7 @@ #define IBUFSIZE 256 #endif -static char outbuf[OBUFSIZE], inbuf[IBUFSIZE]; +static char outbuf[OBUFSIZE]; static int obufsize = 0, ibufsize = 0; static int icurrchar = 0; @@ -47,12 +47,15 @@ oflush() } } +#if 0 void init_buf() { memset(inbuf, 0, IBUFSIZE); } +#endif + void output(char *s, int len) { @@ -121,6 +124,7 @@ dogetch() { int len; static time_t lastact; + static unsigned char inbuf[IBUFSIZE]; if (ibufsize <= icurrchar) { if (flushf) @@ -177,7 +181,7 @@ dogetch() } #ifdef SKIP_TELNET_CONTROL_SIGNAL - } while( inbuf[0] == -1 ); + } while( inbuf[0] == IAC ); #endif ibufsize = len; icurrchar = 0; @@ -223,7 +227,56 @@ igetch() else // here is switch for default keys switch (ch) { case IAC: +#ifndef SKIP_TELNET_CONTROL_SIGNAL + { + unsigned char cmd[16]; + ch = dogetch(); + switch (ch) { + case IAC: + return IAC; /* escaped IAC */ + + case DO: + cmd[0] = IAC; + cmd[1] = WONT; /* this always work according to rfc 854 */ + cmd[2] = dogetch(); + if(cmd[1] != TELOPT_TTYPE && cmd[1] != TELOPT_NAWS && + cmd[1] != TELOPT_ECHO && cmd[1] != TELOPT_SGA) + write(1, cmd, 3); + break; + + case DONT: + cmd[0] = IAC; + cmd[1] = WONT; + cmd[2] = dogetch(); + write(1, cmd, 3); + break; + + case WILL: case WONT: + cmd[2] = dogetch(); + break; + + case SB: + { + int i; + ch = 0; /* use as mode */ + for (i = 2; i < 16; ++i) { + cmd[i] = dogetch(); + if (cmd[i] == IAC) + ch ^= 1; + else if (ch && cmd[i] == SE) + break; + else + mode = 0; + } + } + if (cmd[2] == TELOPT_NAWS) + telnet_parse_size(cmd); + + } /* switch first char after IAC */ + } +#endif /* ! defined SKIP_TELNET_CONTROL_SIGNAL */ continue; + #ifdef DEBUG case Ctrl('Q'):{ struct rusage ru; diff --git a/mbbsd/mbbsd.c b/mbbsd/mbbsd.c index c202e786..8c5ac52d 100644 --- a/mbbsd/mbbsd.c +++ b/mbbsd/mbbsd.c @@ -1076,29 +1076,41 @@ start_client() static void telnet_init() { - const static char svr[] = { + const static unsigned char svr[] = { IAC, DO, TELOPT_TTYPE, IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE, IAC, WILL, TELOPT_ECHO, - IAC, WILL, TELOPT_SGA + IAC, WILL, TELOPT_SGA, +#ifndef SKIP_TELNET_CONTROL_SIGNAL + IAC, DO, TELOPT_NAWS, +#endif + 0 }; - const char *cmd; + const unsigned char *cmd; int n, len; struct timeval to; - char buf[64]; + unsigned char buf[64]; fd_set ReadSet, r; + int recvlen; FD_ZERO(&ReadSet); FD_SET(0, &ReadSet); - for (n = 0, cmd = svr; n < 4; n++) { - len = (n == 1 ? 6 : 3); + for (n = 0, cmd = svr; *cmd == IAC; n++) { + len = (cmd[1] == SB ? 6 : 3); write(0, cmd, len); cmd += len; to.tv_sec = 3; to.tv_usec = 0; r = ReadSet; - if (select(1, &r, NULL, NULL, &to) > 0) - recv(0, buf, sizeof(buf), 0); + if (select(1, &r, NULL, NULL, &to) > 0){ + recvlen = recv(0, buf, sizeof(buf), 0); +#ifndef SKIP_TELNET_CONTROL_SIGNAL + if (recvlen && len == 3 && *(cmd - 1) == TELOPT_NAWS){ + if (buf[0] == IAC && buf[1] == WILL && buf[2] == TELOPT_NAWS) + telnet_parse_size(buf + 3); + } +#endif + } } } diff --git a/mbbsd/term.c b/mbbsd/term.c index 555840d1..f58bbfb8 100644 --- a/mbbsd/term.c +++ b/mbbsd/term.c @@ -53,21 +53,10 @@ outcf(int ch) #endif static void -term_resize(int sig) -{ - struct winsize newsize; +term_resize(int row, int col){ screenline_t *new_picture; - - signal(SIGWINCH, SIG_IGN); /* Don't bother me! */ - ioctl(0, TIOCGWINSZ, &newsize); - - /* make sure reasonable size */ - newsize.ws_row = MAX(24, MIN(100, newsize.ws_row)); - newsize.ws_col = MAX(80, MIN(200, newsize.ws_col)); - - if (newsize.ws_row > t_lines) { - new_picture = (screenline_t *) calloc(newsize.ws_row, - sizeof(screenline_t)); + if (big_picture != NULL && row > t_lines) { + new_picture = (screenline_t *) calloc(row, sizeof(screenline_t)); if (new_picture == NULL) { syslog(LOG_ERR, "calloc(): %m"); return; @@ -76,19 +65,52 @@ term_resize(int sig) free(big_picture); big_picture = new_picture; } - t_lines = newsize.ws_row; - t_columns = newsize.ws_col; - scr_lns = t_lines; /* XXX: scr_lns 跟 t_lines 有什麼不同, 為何分成兩個 */ + t_lines = row; + t_columns = col; b_lines = t_lines - 1; p_lines = t_lines - 4; +} - signal(SIGWINCH, term_resize); +static void +term_resize_catch(int sig) +{ + struct winsize newsize; + + signal(SIGWINCH, SIG_IGN); /* Don't bother me! */ + ioctl(0, TIOCGWINSZ, &newsize); + + /* make sure reasonable size */ + newsize.ws_row = MAX(24, MIN(100, newsize.ws_row)); + newsize.ws_col = MAX(80, MIN(200, newsize.ws_col)); + + term_resize(newsize.ws_row, newsize.ws_col); + + signal(SIGWINCH, term_resize_catch); +} + +#ifndef SKIP_TELNET_CONTROL_SIGNAL +void +telnet_parse_size(const unsigned char* msg){ + /* msg[0] == IAC, msg[1] == SB, msg[2] = TELOPT_NAWS */ + int i = 4, row, col; + + col = msg[3] << 8; + if(msg[i] == 0xff) + ++i; /* avoid escaped 0xff */ + col |= msg[i++]; + + row = msg[i++] << 8; + if(msg[i] == 0xff) + ++i; + row |= msg[i]; + term_resize(row, col); } +#endif int term_init() { - signal(SIGWINCH, term_resize); + signal(SIGWINCH, term_resize_catch); return YEA; } -- cgit v1.2.3