1999-11-09 15:05:34 +03:00
|
|
|
#include <windows.h>
|
2000-10-09 16:19:09 +04:00
|
|
|
#ifndef AUTO_WINSOCK
|
|
|
|
#ifdef WINSOCK_TWO
|
|
|
|
#include <winsock2.h>
|
|
|
|
#else
|
|
|
|
#include <winsock.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
1999-11-09 15:05:34 +03:00
|
|
|
#include <stdio.h>
|
1999-11-22 13:07:24 +03:00
|
|
|
#include <ctype.h>
|
1999-11-09 15:05:34 +03:00
|
|
|
|
|
|
|
#include "putty.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ldisc.c: PuTTY line disciplines
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void c_write (char *buf, int len) {
|
2000-07-26 16:13:51 +04:00
|
|
|
while (len--)
|
|
|
|
c_write1(*buf++);
|
1999-11-09 15:05:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *term_buf = NULL;
|
|
|
|
static int term_buflen = 0, term_bufsiz = 0, term_quotenext = 0;
|
|
|
|
|
|
|
|
static int plen(unsigned char c) {
|
|
|
|
if ((c >= 32 && c <= 126) ||
|
|
|
|
(c >= 160))
|
|
|
|
return 1;
|
|
|
|
else if (c < 128)
|
|
|
|
return 2; /* ^x for some x */
|
|
|
|
else
|
|
|
|
return 4; /* <XY> for hex XY */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pwrite(unsigned char c) {
|
|
|
|
if ((c >= 32 && c <= 126) ||
|
|
|
|
(c >= 160)) {
|
2000-07-26 16:13:51 +04:00
|
|
|
c_write1(c);
|
1999-11-09 15:05:34 +03:00
|
|
|
} else if (c < 128) {
|
|
|
|
char cc[2];
|
|
|
|
cc[1] = (c == 127 ? '?' : c + 0x40);
|
|
|
|
cc[0] = '^';
|
|
|
|
c_write(cc, 2);
|
|
|
|
} else {
|
|
|
|
char cc[5];
|
|
|
|
sprintf(cc, "<%02X>", c);
|
|
|
|
c_write(cc, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bsb(int n) {
|
|
|
|
while (n--)
|
|
|
|
c_write("\010 \010", 3);
|
|
|
|
}
|
|
|
|
|
2000-03-11 17:03:04 +03:00
|
|
|
#define CTRL(x) (x^'@')
|
|
|
|
|
1999-11-09 15:05:34 +03:00
|
|
|
static void term_send(char *buf, int len) {
|
|
|
|
while (len--) {
|
|
|
|
char c;
|
|
|
|
c = *buf++;
|
|
|
|
switch (term_quotenext ? ' ' : c) {
|
|
|
|
/*
|
|
|
|
* ^h/^?: delete one char and output one BSB
|
|
|
|
* ^w: delete, and output BSBs, to return to last space/nonspace
|
|
|
|
* boundary
|
|
|
|
* ^u: delete, and output BSBs, to return to BOL
|
2000-03-11 17:03:04 +03:00
|
|
|
* ^c: Do a ^u then send a telnet IP
|
|
|
|
* ^z: Do a ^u then send a telnet SUSP
|
|
|
|
* ^\: Do a ^u then send a telnet ABORT
|
1999-11-09 15:05:34 +03:00
|
|
|
* ^r: echo "^R\n" and redraw line
|
|
|
|
* ^v: quote next char
|
|
|
|
* ^d: if at BOL, end of file and close connection, else send line
|
|
|
|
* and reset to BOL
|
2000-03-11 17:03:04 +03:00
|
|
|
* ^m: send line-plus-\r\n and reset to BOL
|
1999-11-09 15:05:34 +03:00
|
|
|
*/
|
2000-03-11 17:03:04 +03:00
|
|
|
case CTRL('H'): case CTRL('?'): /* backspace/delete */
|
1999-11-09 15:05:34 +03:00
|
|
|
if (term_buflen > 0) {
|
|
|
|
bsb(plen(term_buf[term_buflen-1]));
|
|
|
|
term_buflen--;
|
|
|
|
}
|
|
|
|
break;
|
2000-03-11 17:03:04 +03:00
|
|
|
case CTRL('W'): /* delete word */
|
1999-11-09 15:05:34 +03:00
|
|
|
while (term_buflen > 0) {
|
|
|
|
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;
|
2000-03-11 17:03:04 +03:00
|
|
|
case CTRL('U'): /* delete line */
|
|
|
|
case CTRL('C'): /* Send IP */
|
|
|
|
case CTRL('\\'): /* Quit */
|
|
|
|
case CTRL('Z'): /* Suspend */
|
1999-11-09 15:05:34 +03:00
|
|
|
while (term_buflen > 0) {
|
|
|
|
bsb(plen(term_buf[term_buflen-1]));
|
|
|
|
term_buflen--;
|
|
|
|
}
|
2000-03-17 13:31:14 +03:00
|
|
|
back->special (TS_EL);
|
2000-03-11 17:03:04 +03:00
|
|
|
if( c == CTRL('C') ) back->special (TS_IP);
|
|
|
|
if( c == CTRL('Z') ) back->special (TS_SUSP);
|
|
|
|
if( c == CTRL('\\') ) back->special (TS_ABORT);
|
1999-11-09 15:05:34 +03:00
|
|
|
break;
|
2000-03-11 17:03:04 +03:00
|
|
|
case CTRL('R'): /* redraw line */
|
1999-11-09 15:05:34 +03:00
|
|
|
c_write("^R\r\n", 4);
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < term_buflen; i++)
|
|
|
|
pwrite(term_buf[i]);
|
|
|
|
}
|
|
|
|
break;
|
2000-03-11 17:03:04 +03:00
|
|
|
case CTRL('V'): /* quote next char */
|
1999-11-09 15:05:34 +03:00
|
|
|
term_quotenext = TRUE;
|
|
|
|
break;
|
2000-03-11 17:03:04 +03:00
|
|
|
case CTRL('D'): /* logout or send */
|
1999-11-09 15:05:34 +03:00
|
|
|
if (term_buflen == 0) {
|
2000-03-11 17:03:04 +03:00
|
|
|
back->special (TS_EOF);
|
1999-11-09 15:05:34 +03:00
|
|
|
} else {
|
|
|
|
back->send(term_buf, term_buflen);
|
|
|
|
term_buflen = 0;
|
|
|
|
}
|
|
|
|
break;
|
2000-03-11 17:03:04 +03:00
|
|
|
case CTRL('M'): /* send with newline */
|
2000-06-26 16:55:47 +04:00
|
|
|
if (term_buflen > 0)
|
|
|
|
back->send(term_buf, term_buflen);
|
2000-03-17 13:31:14 +03:00
|
|
|
if (cfg.protocol == PROT_RAW)
|
|
|
|
back->send("\r\n", 2);
|
|
|
|
else
|
|
|
|
back->send("\r", 1);
|
1999-11-09 15:05:34 +03:00
|
|
|
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;
|
|
|
|
pwrite(c);
|
|
|
|
term_quotenext = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void simple_send(char *buf, int len) {
|
2000-03-11 17:03:04 +03:00
|
|
|
if( term_buflen != 0 )
|
|
|
|
{
|
|
|
|
back->send(term_buf, term_buflen);
|
|
|
|
while (term_buflen > 0) {
|
|
|
|
bsb(plen(term_buf[term_buflen-1]));
|
|
|
|
term_buflen--;
|
|
|
|
}
|
|
|
|
}
|
2000-06-26 16:55:47 +04:00
|
|
|
if (len > 0)
|
|
|
|
back->send(buf, len);
|
1999-11-09 15:05:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Ldisc ldisc_term = { term_send };
|
|
|
|
Ldisc ldisc_simple = { simple_send };
|