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