X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/putty/blobdiff_plain/b9d7bcadee831e9b59fb785f2464a5fc1897bd1a..3d5040f8f85f049cbb072a827a6184b4b4314b08:/ldisc.c diff --git a/ldisc.c b/ldisc.c index d9335c9c..efbb8714 100644 --- a/ldisc.c +++ b/ldisc.c @@ -10,29 +10,20 @@ #include "putty.h" #include "terminal.h" +#include "ldisc.h" -typedef struct ldisc_tag { - Terminal *term; - Backend *back; - void *backhandle; - void *frontend; - - char *buf; - int buflen, bufsiz, quotenext; -} *Ldisc; - -#define ECHOING (cfg.localecho == LD_YES || \ - (cfg.localecho == LD_BACKEND && \ +#define ECHOING (ldisc->localecho == FORCE_ON || \ + (ldisc->localecho == AUTO && \ (ldisc->back->ldisc(ldisc->backhandle, LD_ECHO) || \ term_ldisc(ldisc->term, LD_ECHO)))) -#define EDITING (cfg.localedit == LD_YES || \ - (cfg.localedit == LD_BACKEND && \ +#define EDITING (ldisc->localedit == FORCE_ON || \ + (ldisc->localedit == AUTO && \ (ldisc->back->ldisc(ldisc->backhandle, LD_EDIT) || \ term_ldisc(ldisc->term, LD_EDIT)))) static void c_write(Ldisc ldisc, char *buf, int len) { - from_backend(ldisc->term, 0, buf, len); + from_backend(ldisc->frontend, 0, buf, len); } static int plen(Ldisc ldisc, unsigned char c) @@ -41,14 +32,21 @@ static int plen(Ldisc ldisc, unsigned char c) return 1; else if (c < 128) return 2; /* ^x for some x */ + else if (in_utf(ldisc->term) && c >= 0xC0) + return 1; /* UTF-8 introducer character + * (FIXME: combining / wide chars) */ + else if (in_utf(ldisc->term) && c >= 0x80 && c < 0xC0) + return 0; /* UTF-8 followup character */ else - return 4; /* for hex XY */ + return 4; /* hex representation */ } static void pwrite(Ldisc ldisc, unsigned char c) { - if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf(ldisc->term))) { - c_write(ldisc, &c, 1); + if ((c >= 32 && c <= 126) || + (!in_utf(ldisc->term) && c >= 0xA0) || + (in_utf(ldisc->term) && c >= 0x80)) { + c_write(ldisc, (char *)&c, 1); } else if (c < 128) { char cc[2]; cc[1] = (c == 127 ? '?' : c + 0x40); @@ -61,6 +59,14 @@ static void pwrite(Ldisc ldisc, unsigned char c) } } +static int char_start(Ldisc ldisc, unsigned char c) +{ + if (in_utf(ldisc->term)) + return (c < 0x80 || c >= 0xC0); + else + return 1; +} + static void bsb(Ldisc ldisc, int n) { while (n--) @@ -70,11 +76,11 @@ static void bsb(Ldisc ldisc, int n) #define CTRL(x) (x^'@') #define KCTRL(x) ((x^'@') | 0x100) -void *ldisc_create(Terminal *term, +void *ldisc_create(Conf *conf, Terminal *term, Backend *back, void *backhandle, void *frontend) { - Ldisc ldisc = smalloc(sizeof(*ldisc)); + Ldisc ldisc = snew(struct ldisc_tag); ldisc->buf = NULL; ldisc->buflen = 0; @@ -86,6 +92,8 @@ void *ldisc_create(Terminal *term, ldisc->term = term; ldisc->frontend = frontend; + ldisc_configure(ldisc, conf); + /* Link ourselves into the backend and the terminal */ if (term) term->ldisc = ldisc; @@ -95,6 +103,30 @@ void *ldisc_create(Terminal *term, return ldisc; } +void ldisc_configure(void *handle, Conf *conf) +{ + Ldisc ldisc = (Ldisc) handle; + + ldisc->telnet_keyboard = conf_get_int(conf, CONF_telnet_keyboard); + ldisc->telnet_newline = conf_get_int(conf, CONF_telnet_newline); + ldisc->protocol = conf_get_int(conf, CONF_protocol); + ldisc->localecho = conf_get_int(conf, CONF_localecho); + ldisc->localedit = conf_get_int(conf, CONF_localedit); +} + +void ldisc_free(void *handle) +{ + Ldisc ldisc = (Ldisc) handle; + + if (ldisc->term) + ldisc->term->ldisc = NULL; + if (ldisc->back) + ldisc->back->provide_ldisc(ldisc->backhandle, NULL); + if (ldisc->buf) + sfree(ldisc->buf); + sfree(ldisc); +} + void ldisc_send(void *handle, char *buf, int len, int interactive) { Ldisc ldisc = (Ldisc) handle; @@ -127,12 +159,14 @@ void ldisc_send(void *handle, char *buf, int len, int interactive) if (EDITING) { while (len--) { int c; - c = *buf++ + keyflag; + c = (unsigned char)(*buf++) + keyflag; if (!interactive && c == '\r') c += KCTRL('@'); switch (ldisc->quotenext ? ' ' : c) { /* - * ^h/^?: delete one char and output one BSB + * ^h/^?: delete, and output BSBs, to return to + * last character boundary (in UTF-8 mode this may + * be more than one byte) * ^w: delete, and output BSBs, to return to last * space/nonspace boundary * ^u: delete, and output BSBs, to return to BOL @@ -148,9 +182,11 @@ void ldisc_send(void *handle, char *buf, int len, int interactive) case KCTRL('H'): case KCTRL('?'): /* backspace/delete */ if (ldisc->buflen > 0) { - if (ECHOING) - bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1])); - ldisc->buflen--; + do { + if (ECHOING) + bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1])); + ldisc->buflen--; + } while (!char_start(ldisc, ldisc->buf[ldisc->buflen])); } break; case CTRL('W'): /* delete word */ @@ -159,8 +195,8 @@ void ldisc_send(void *handle, char *buf, int len, int interactive) bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1])); ldisc->buflen--; if (ldisc->buflen > 0 && - isspace(ldisc->buf[ldisc->buflen - 1]) && - !isspace(ldisc->buf[ldisc->buflen])) + isspace((unsigned char)ldisc->buf[ldisc->buflen-1]) && + !isspace((unsigned char)ldisc->buf[ldisc->buflen])) break; } break; @@ -179,7 +215,7 @@ void ldisc_send(void *handle, char *buf, int len, int interactive) * configured telnet specials off! This breaks * talkers otherwise. */ - if (!cfg.telnet_keyboard) + if (!ldisc->telnet_keyboard) goto default_case; if (c == CTRL('C')) ldisc->back->special(ldisc->backhandle, TS_IP); @@ -231,7 +267,7 @@ void ldisc_send(void *handle, char *buf, int len, int interactive) * default clause because of the break. */ case CTRL('J'): - if (cfg.protocol == PROT_RAW && + if (ldisc->protocol == PROT_RAW && ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') { if (ECHOING) bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1])); @@ -240,9 +276,9 @@ void ldisc_send(void *handle, char *buf, int len, int interactive) case KCTRL('M'): /* send with newline */ if (ldisc->buflen > 0) ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen); - if (cfg.protocol == PROT_RAW) + if (ldisc->protocol == PROT_RAW) ldisc->back->send(ldisc->backhandle, "\r\n", 2); - else if (cfg.protocol == PROT_TELNET && cfg.telnet_newline) + else if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline) ldisc->back->special(ldisc->backhandle, TS_EOL); else ldisc->back->send(ldisc->backhandle, "\r", 1); @@ -256,7 +292,7 @@ void ldisc_send(void *handle, char *buf, int len, int interactive) default_case: if (ldisc->buflen >= ldisc->bufsiz) { ldisc->bufsiz = ldisc->buflen + 256; - ldisc->buf = srealloc(ldisc->buf, ldisc->bufsiz); + ldisc->buf = sresize(ldisc->buf, ldisc->bufsiz, char); } ldisc->buf[ldisc->buflen++] = c; if (ECHOING) @@ -276,27 +312,27 @@ void ldisc_send(void *handle, char *buf, int len, int interactive) if (len > 0) { if (ECHOING) c_write(ldisc, buf, len); - if (keyflag && cfg.protocol == PROT_TELNET && len == 1) { + if (keyflag && ldisc->protocol == PROT_TELNET && len == 1) { switch (buf[0]) { case CTRL('M'): - if (cfg.protocol == PROT_TELNET && cfg.telnet_newline) + if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline) ldisc->back->special(ldisc->backhandle, TS_EOL); else ldisc->back->send(ldisc->backhandle, "\r", 1); break; case CTRL('?'): case CTRL('H'): - if (cfg.telnet_keyboard) { + if (ldisc->telnet_keyboard) { ldisc->back->special(ldisc->backhandle, TS_EC); break; } case CTRL('C'): - if (cfg.telnet_keyboard) { + if (ldisc->telnet_keyboard) { ldisc->back->special(ldisc->backhandle, TS_IP); break; } case CTRL('Z'): - if (cfg.telnet_keyboard) { + if (ldisc->telnet_keyboard) { ldisc->back->special(ldisc->backhandle, TS_SUSP); break; }