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