When we disconnect because we have no supported authentication
[u/mdw/putty] / ldisc.c
CommitLineData
0965bee0 1/*
2 * ldisc.c: PuTTY line discipline. Sits between the input coming
3 * from keypresses in the window, and the output channel leading to
4 * the back end. Implements echo and/or local line editing,
5 * depending on what's currently configured.
6 */
7
5bc238bb 8#include <stdio.h>
1d470ad2 9#include <ctype.h>
5bc238bb 10
11#include "putty.h"
887035a5 12#include "terminal.h"
5def7522 13#include "ldisc.h"
b9d7bcad 14
5ecd7ad0 15#define ECHOING (ldisc->cfg->localecho == FORCE_ON || \
16 (ldisc->cfg->localecho == AUTO && \
b9d7bcad 17 (ldisc->back->ldisc(ldisc->backhandle, LD_ECHO) || \
18 term_ldisc(ldisc->term, LD_ECHO))))
5ecd7ad0 19#define EDITING (ldisc->cfg->localedit == FORCE_ON || \
20 (ldisc->cfg->localedit == AUTO && \
b9d7bcad 21 (ldisc->back->ldisc(ldisc->backhandle, LD_EDIT) || \
22 term_ldisc(ldisc->term, LD_EDIT))))
5bc238bb 23
b9d7bcad 24static void c_write(Ldisc ldisc, char *buf, int len)
32874aea 25{
39d04c2b 26 from_backend(ldisc->frontend, 0, buf, len);
fe50e814 27}
28
b9d7bcad 29static int plen(Ldisc ldisc, unsigned char c)
32874aea 30{
b9d7bcad 31 if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf(ldisc->term)))
32874aea 32 return 1;
5bc238bb 33 else if (c < 128)
32874aea 34 return 2; /* ^x for some x */
f0fccd51 35 else if (in_utf(ldisc->term) && c >= 0xC0)
36 return 1; /* UTF-8 introducer character
37 * (FIXME: combining / wide chars) */
38 else if (in_utf(ldisc->term) && c >= 0x80 && c < 0xC0)
39 return 0; /* UTF-8 followup character */
5bc238bb 40 else
f0fccd51 41 return 4; /* <XY> hex representation */
5bc238bb 42}
43
b9d7bcad 44static void pwrite(Ldisc ldisc, unsigned char c)
32874aea 45{
f0fccd51 46 if ((c >= 32 && c <= 126) ||
47 (!in_utf(ldisc->term) && c >= 0xA0) ||
48 (in_utf(ldisc->term) && c >= 0x80)) {
fe5634f6 49 c_write(ldisc, (char *)&c, 1);
5bc238bb 50 } else if (c < 128) {
32874aea 51 char cc[2];
52 cc[1] = (c == 127 ? '?' : c + 0x40);
53 cc[0] = '^';
b9d7bcad 54 c_write(ldisc, cc, 2);
5bc238bb 55 } else {
32874aea 56 char cc[5];
57 sprintf(cc, "<%02X>", c);
b9d7bcad 58 c_write(ldisc, cc, 4);
5bc238bb 59 }
60}
61
f0fccd51 62static int char_start(Ldisc ldisc, unsigned char c)
63{
64 if (in_utf(ldisc->term))
65 return (c < 0x80 || c >= 0xC0);
66 else
67 return 1;
68}
69
b9d7bcad 70static void bsb(Ldisc ldisc, int n)
32874aea 71{
5bc238bb 72 while (n--)
b9d7bcad 73 c_write(ldisc, "\010 \010", 3);
5bc238bb 74}
75
5d1d4279 76#define CTRL(x) (x^'@')
a5f3e637 77#define KCTRL(x) ((x^'@') | 0x100)
5d1d4279 78
fe5634f6 79void *ldisc_create(Config *mycfg, Terminal *term,
b9d7bcad 80 Backend *back, void *backhandle,
81 void *frontend)
32874aea 82{
3d88e64d 83 Ldisc ldisc = snew(struct ldisc_tag);
b9d7bcad 84
85 ldisc->buf = NULL;
86 ldisc->buflen = 0;
87 ldisc->bufsiz = 0;
88 ldisc->quotenext = 0;
89
fe5634f6 90 ldisc->cfg = mycfg;
b9d7bcad 91 ldisc->back = back;
92 ldisc->backhandle = backhandle;
93 ldisc->term = term;
94 ldisc->frontend = frontend;
95
96 /* Link ourselves into the backend and the terminal */
97 if (term)
98 term->ldisc = ldisc;
99 if (back)
100 back->provide_ldisc(backhandle, ldisc);
101
102 return ldisc;
103}
104
fabd1805 105void ldisc_free(void *handle)
106{
107 Ldisc ldisc = (Ldisc) handle;
108
109 if (ldisc->term)
110 ldisc->term->ldisc = NULL;
111 if (ldisc->back)
112 ldisc->back->provide_ldisc(ldisc->backhandle, NULL);
113 if (ldisc->buf)
114 sfree(ldisc->buf);
115 sfree(ldisc);
116}
117
b9d7bcad 118void ldisc_send(void *handle, char *buf, int len, int interactive)
119{
120 Ldisc ldisc = (Ldisc) handle;
a5f3e637 121 int keyflag = 0;
0965bee0 122 /*
123 * Called with len=0 when the options change. We must inform
124 * the front end in case it needs to know.
125 */
126 if (len == 0) {
b9d7bcad 127 ldisc_update(ldisc->frontend, ECHOING, EDITING);
887035a5 128 return;
5bc238bb 129 }
0965bee0 130 /*
ab2c86b9 131 * Notify the front end that something was pressed, in case
132 * it's depending on finding out (e.g. keypress termination for
133 * Close On Exit).
134 */
b9d7bcad 135 frontend_keypress(ldisc->frontend);
136
ab2c86b9 137 /*
a5f3e637 138 * Less than zero means null terminated special string.
139 */
140 if (len < 0) {
141 len = strlen(buf);
142 keyflag = KCTRL('@');
143 }
144 /*
0965bee0 145 * Either perform local editing, or just send characters.
146 */
147 if (EDITING) {
32874aea 148 while (len--) {
a5f3e637 149 int c;
150 c = *buf++ + keyflag;
760e88b2 151 if (!interactive && c == '\r')
152 c += KCTRL('@');
b9d7bcad 153 switch (ldisc->quotenext ? ' ' : c) {
32874aea 154 /*
f0fccd51 155 * ^h/^?: delete, and output BSBs, to return to
156 * last character boundary (in UTF-8 mode this may
157 * be more than one byte)
32874aea 158 * ^w: delete, and output BSBs, to return to last
159 * space/nonspace boundary
160 * ^u: delete, and output BSBs, to return to BOL
161 * ^c: Do a ^u then send a telnet IP
162 * ^z: Do a ^u then send a telnet SUSP
163 * ^\: Do a ^u then send a telnet ABORT
164 * ^r: echo "^R\n" and redraw line
165 * ^v: quote next char
166 * ^d: if at BOL, end of file and close connection,
167 * else send line and reset to BOL
168 * ^m: send line-plus-\r\n and reset to BOL
169 */
a5f3e637 170 case KCTRL('H'):
171 case KCTRL('?'): /* backspace/delete */
b9d7bcad 172 if (ldisc->buflen > 0) {
f0fccd51 173 do {
174 if (ECHOING)
175 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
176 ldisc->buflen--;
177 } while (!char_start(ldisc, ldisc->buf[ldisc->buflen]));
32874aea 178 }
179 break;
180 case CTRL('W'): /* delete word */
b9d7bcad 181 while (ldisc->buflen > 0) {
32874aea 182 if (ECHOING)
b9d7bcad 183 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
184 ldisc->buflen--;
185 if (ldisc->buflen > 0 &&
e93ed432 186 isspace((unsigned char)ldisc->buf[ldisc->buflen-1]) &&
187 !isspace((unsigned char)ldisc->buf[ldisc->buflen]))
32874aea 188 break;
189 }
190 break;
191 case CTRL('U'): /* delete line */
192 case CTRL('C'): /* Send IP */
193 case CTRL('\\'): /* Quit */
194 case CTRL('Z'): /* Suspend */
b9d7bcad 195 while (ldisc->buflen > 0) {
32874aea 196 if (ECHOING)
b9d7bcad 197 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
198 ldisc->buflen--;
32874aea 199 }
b9d7bcad 200 ldisc->back->special(ldisc->backhandle, TS_EL);
2a8a449c 201 /*
202 * We don't send IP, SUSP or ABORT if the user has
203 * configured telnet specials off! This breaks
204 * talkers otherwise.
205 */
fe5634f6 206 if (!ldisc->cfg->telnet_keyboard)
2a8a449c 207 goto default_case;
32874aea 208 if (c == CTRL('C'))
b9d7bcad 209 ldisc->back->special(ldisc->backhandle, TS_IP);
32874aea 210 if (c == CTRL('Z'))
b9d7bcad 211 ldisc->back->special(ldisc->backhandle, TS_SUSP);
32874aea 212 if (c == CTRL('\\'))
b9d7bcad 213 ldisc->back->special(ldisc->backhandle, TS_ABORT);
32874aea 214 break;
215 case CTRL('R'): /* redraw line */
216 if (ECHOING) {
217 int i;
b9d7bcad 218 c_write(ldisc, "^R\r\n", 4);
219 for (i = 0; i < ldisc->buflen; i++)
220 pwrite(ldisc, ldisc->buf[i]);
32874aea 221 }
222 break;
223 case CTRL('V'): /* quote next char */
b9d7bcad 224 ldisc->quotenext = TRUE;
32874aea 225 break;
226 case CTRL('D'): /* logout or send */
b9d7bcad 227 if (ldisc->buflen == 0) {
228 ldisc->back->special(ldisc->backhandle, TS_EOF);
32874aea 229 } else {
b9d7bcad 230 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
231 ldisc->buflen = 0;
32874aea 232 }
233 break;
a5f3e637 234 /*
235 * This particularly hideous bit of code from RDB
236 * allows ordinary ^M^J to do the same thing as
237 * magic-^M when in Raw protocol. The line `case
238 * KCTRL('M'):' is _inside_ the if block. Thus:
239 *
240 * - receiving regular ^M goes straight to the
241 * default clause and inserts as a literal ^M.
242 * - receiving regular ^J _not_ directly after a
243 * literal ^M (or not in Raw protocol) fails the
244 * if condition, leaps to the bottom of the if,
245 * and falls through into the default clause
246 * again.
247 * - receiving regular ^J just after a literal ^M
248 * in Raw protocol passes the if condition,
249 * deletes the literal ^M, and falls through
250 * into the magic-^M code
251 * - receiving a magic-^M empties the line buffer,
252 * signals end-of-line in one of the various
253 * entertaining ways, and _doesn't_ fall out of
254 * the bottom of the if and through to the
255 * default clause because of the break.
256 */
257 case CTRL('J'):
fe5634f6 258 if (ldisc->cfg->protocol == PROT_RAW &&
b9d7bcad 259 ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
a5f3e637 260 if (ECHOING)
b9d7bcad 261 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
262 ldisc->buflen--;
a5f3e637 263 /* FALLTHROUGH */
264 case KCTRL('M'): /* send with newline */
b9d7bcad 265 if (ldisc->buflen > 0)
266 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
fe5634f6 267 if (ldisc->cfg->protocol == PROT_RAW)
b9d7bcad 268 ldisc->back->send(ldisc->backhandle, "\r\n", 2);
fe5634f6 269 else if (ldisc->cfg->protocol == PROT_TELNET && ldisc->cfg->telnet_newline)
b9d7bcad 270 ldisc->back->special(ldisc->backhandle, TS_EOL);
a5f3e637 271 else
b9d7bcad 272 ldisc->back->send(ldisc->backhandle, "\r", 1);
a5f3e637 273 if (ECHOING)
b9d7bcad 274 c_write(ldisc, "\r\n", 2);
275 ldisc->buflen = 0;
a5f3e637 276 break;
277 }
278 /* FALLTHROUGH */
32874aea 279 default: /* get to this label from ^V handler */
2a8a449c 280 default_case:
b9d7bcad 281 if (ldisc->buflen >= ldisc->bufsiz) {
282 ldisc->bufsiz = ldisc->buflen + 256;
3d88e64d 283 ldisc->buf = sresize(ldisc->buf, ldisc->bufsiz, char);
32874aea 284 }
b9d7bcad 285 ldisc->buf[ldisc->buflen++] = c;
32874aea 286 if (ECHOING)
b9d7bcad 287 pwrite(ldisc, (unsigned char) c);
288 ldisc->quotenext = FALSE;
32874aea 289 break;
290 }
291 }
0965bee0 292 } else {
b9d7bcad 293 if (ldisc->buflen != 0) {
294 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
295 while (ldisc->buflen > 0) {
296 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
297 ldisc->buflen--;
32874aea 298 }
299 }
300 if (len > 0) {
301 if (ECHOING)
b9d7bcad 302 c_write(ldisc, buf, len);
fe5634f6 303 if (keyflag && ldisc->cfg->protocol == PROT_TELNET && len == 1) {
a5f3e637 304 switch (buf[0]) {
305 case CTRL('M'):
fe5634f6 306 if (ldisc->cfg->protocol == PROT_TELNET && ldisc->cfg->telnet_newline)
b9d7bcad 307 ldisc->back->special(ldisc->backhandle, TS_EOL);
eee63b77 308 else
b9d7bcad 309 ldisc->back->send(ldisc->backhandle, "\r", 1);
a5f3e637 310 break;
311 case CTRL('?'):
312 case CTRL('H'):
fe5634f6 313 if (ldisc->cfg->telnet_keyboard) {
b9d7bcad 314 ldisc->back->special(ldisc->backhandle, TS_EC);
a5f3e637 315 break;
316 }
317 case CTRL('C'):
fe5634f6 318 if (ldisc->cfg->telnet_keyboard) {
b9d7bcad 319 ldisc->back->special(ldisc->backhandle, TS_IP);
a5f3e637 320 break;
321 }
322 case CTRL('Z'):
fe5634f6 323 if (ldisc->cfg->telnet_keyboard) {
b9d7bcad 324 ldisc->back->special(ldisc->backhandle, TS_SUSP);
a5f3e637 325 break;
326 }
327
328 default:
b9d7bcad 329 ldisc->back->send(ldisc->backhandle, buf, len);
a5f3e637 330 break;
331 }
332 } else
b9d7bcad 333 ldisc->back->send(ldisc->backhandle, buf, len);
32874aea 334 }
5d1d4279 335 }
5bc238bb 336}