- case CTRL('H'): case CTRL('?'): /* backspace/delete */
- if (term_buflen > 0) {
- if (ECHOING)
- bsb(plen(term_buf[term_buflen-1]));
- term_buflen--;
- }
- break;
- case CTRL('W'): /* delete word */
- while (term_buflen > 0) {
- if (ECHOING)
- bsb(plen(term_buf[term_buflen-1]));
- term_buflen--;
- if (term_buflen > 0 &&
- isspace(term_buf[term_buflen-1]) &&
- !isspace(term_buf[term_buflen]))
- break;
- }
- break;
- case CTRL('U'): /* delete line */
- case CTRL('C'): /* Send IP */
- case CTRL('\\'): /* Quit */
- case CTRL('Z'): /* Suspend */
- while (term_buflen > 0) {
- if (ECHOING)
- bsb(plen(term_buf[term_buflen-1]));
- term_buflen--;
- }
- back->special (TS_EL);
- if( c == CTRL('C') ) back->special (TS_IP);
- if( c == CTRL('Z') ) back->special (TS_SUSP);
- if( c == CTRL('\\') ) back->special (TS_ABORT);
- break;
- case CTRL('R'): /* redraw line */
- if (ECHOING) {
- int i;
- c_write("^R\r\n", 4);
- for (i = 0; i < term_buflen; i++)
- pwrite(term_buf[i]);
- }
- break;
- case CTRL('V'): /* quote next char */
- term_quotenext = TRUE;
- break;
- case CTRL('D'): /* logout or send */
- if (term_buflen == 0) {
- back->special (TS_EOF);
- } else {
- back->send(term_buf, term_buflen);
- term_buflen = 0;
- }
- break;
- case CTRL('M'): /* send with newline */
- if (term_buflen > 0)
- back->send(term_buf, term_buflen);
- if (cfg.protocol == PROT_RAW)
- back->send("\r\n", 2);
- else
- back->send("\r", 1);
- if (ECHOING)
- c_write("\r\n", 2);
- term_buflen = 0;
- break;
- default: /* get to this label from ^V handler */
- if (term_buflen >= term_bufsiz) {
- term_bufsiz = term_buflen + 256;
- term_buf = saferealloc(term_buf, term_bufsiz);
- }
- term_buf[term_buflen++] = c;
- if (ECHOING)
- pwrite(c);
- term_quotenext = FALSE;
- break;
- }
- }
+ if (!ldisc->cfg->telnet_keyboard)
+ goto default_case;
+ if (c == CTRL('C'))
+ ldisc->back->special(ldisc->backhandle, TS_IP);
+ if (c == CTRL('Z'))
+ ldisc->back->special(ldisc->backhandle, TS_SUSP);
+ if (c == CTRL('\\'))
+ ldisc->back->special(ldisc->backhandle, TS_ABORT);
+ break;
+ case CTRL('R'): /* redraw line */
+ if (ECHOING) {
+ int i;
+ c_write(ldisc, "^R\r\n", 4);
+ for (i = 0; i < ldisc->buflen; i++)
+ pwrite(ldisc, ldisc->buf[i]);
+ }
+ break;
+ case CTRL('V'): /* quote next char */
+ ldisc->quotenext = TRUE;
+ break;
+ case CTRL('D'): /* logout or send */
+ if (ldisc->buflen == 0) {
+ ldisc->back->special(ldisc->backhandle, TS_EOF);
+ } else {
+ ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
+ ldisc->buflen = 0;
+ }
+ break;
+ /*
+ * This particularly hideous bit of code from RDB
+ * allows ordinary ^M^J to do the same thing as
+ * magic-^M when in Raw protocol. The line `case
+ * KCTRL('M'):' is _inside_ the if block. Thus:
+ *
+ * - receiving regular ^M goes straight to the
+ * default clause and inserts as a literal ^M.
+ * - receiving regular ^J _not_ directly after a
+ * literal ^M (or not in Raw protocol) fails the
+ * if condition, leaps to the bottom of the if,
+ * and falls through into the default clause
+ * again.
+ * - receiving regular ^J just after a literal ^M
+ * in Raw protocol passes the if condition,
+ * deletes the literal ^M, and falls through
+ * into the magic-^M code
+ * - receiving a magic-^M empties the line buffer,
+ * signals end-of-line in one of the various
+ * entertaining ways, and _doesn't_ fall out of
+ * the bottom of the if and through to the
+ * default clause because of the break.
+ */
+ case CTRL('J'):
+ if (ldisc->cfg->protocol == PROT_RAW &&
+ ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
+ if (ECHOING)
+ bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
+ ldisc->buflen--;
+ /* FALLTHROUGH */
+ case KCTRL('M'): /* send with newline */
+ if (ldisc->buflen > 0)
+ ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
+ if (ldisc->cfg->protocol == PROT_RAW)
+ ldisc->back->send(ldisc->backhandle, "\r\n", 2);
+ else if (ldisc->cfg->protocol == PROT_TELNET && ldisc->cfg->telnet_newline)
+ ldisc->back->special(ldisc->backhandle, TS_EOL);
+ else
+ ldisc->back->send(ldisc->backhandle, "\r", 1);
+ if (ECHOING)
+ c_write(ldisc, "\r\n", 2);
+ ldisc->buflen = 0;
+ break;
+ }
+ /* FALLTHROUGH */
+ default: /* get to this label from ^V handler */
+ default_case:
+ if (ldisc->buflen >= ldisc->bufsiz) {
+ ldisc->bufsiz = ldisc->buflen + 256;
+ ldisc->buf = sresize(ldisc->buf, ldisc->bufsiz, char);
+ }
+ ldisc->buf[ldisc->buflen++] = c;
+ if (ECHOING)
+ pwrite(ldisc, (unsigned char) c);
+ ldisc->quotenext = FALSE;
+ break;
+ }
+ }