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