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.
16 #define ECHOING (ldisc->localecho == FORCE_ON || \
17 (ldisc->localecho == AUTO && \
18 (ldisc->back->ldisc(ldisc->backhandle, LD_ECHO) || \
19 term_ldisc(ldisc->term, LD_ECHO))))
20 #define EDITING (ldisc->localedit == FORCE_ON || \
21 (ldisc->localedit == AUTO && \
22 (ldisc->back->ldisc(ldisc->backhandle, LD_EDIT) || \
23 term_ldisc(ldisc->term, LD_EDIT))))
25 static void c_write(Ldisc ldisc, const char *buf, int len)
27 from_backend(ldisc->frontend, 0, buf, len);
30 static int plen(Ldisc ldisc, unsigned char c)
32 if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf(ldisc->term)))
35 return 2; /* ^x for some x */
36 else if (in_utf(ldisc->term) && c >= 0xC0)
37 return 1; /* UTF-8 introducer character
38 * (FIXME: combining / wide chars) */
39 else if (in_utf(ldisc->term) && c >= 0x80 && c < 0xC0)
40 return 0; /* UTF-8 followup character */
42 return 4; /* <XY> hex representation */
45 static void pwrite(Ldisc ldisc, unsigned char c)
47 if ((c >= 32 && c <= 126) ||
48 (!in_utf(ldisc->term) && c >= 0xA0) ||
49 (in_utf(ldisc->term) && c >= 0x80)) {
50 c_write(ldisc, (char *)&c, 1);
53 cc[1] = (c == 127 ? '?' : c + 0x40);
55 c_write(ldisc, cc, 2);
58 sprintf(cc, "<%02X>", c);
59 c_write(ldisc, cc, 4);
63 static int char_start(Ldisc ldisc, unsigned char c)
65 if (in_utf(ldisc->term))
66 return (c < 0x80 || c >= 0xC0);
71 static void bsb(Ldisc ldisc, int n)
74 c_write(ldisc, "\010 \010", 3);
77 #define CTRL(x) (x^'@')
78 #define KCTRL(x) ((x^'@') | 0x100)
80 void *ldisc_create(Conf *conf, Terminal *term,
81 Backend *back, void *backhandle,
84 Ldisc ldisc = snew(struct ldisc_tag);
92 ldisc->backhandle = backhandle;
94 ldisc->frontend = frontend;
96 ldisc_configure(ldisc, conf);
98 /* Link ourselves into the backend and the terminal */
102 back->provide_ldisc(backhandle, ldisc);
107 void ldisc_configure(void *handle, Conf *conf)
109 Ldisc ldisc = (Ldisc) handle;
111 ldisc->telnet_keyboard = conf_get_int(conf, CONF_telnet_keyboard);
112 ldisc->telnet_newline = conf_get_int(conf, CONF_telnet_newline);
113 ldisc->protocol = conf_get_int(conf, CONF_protocol);
114 ldisc->localecho = conf_get_int(conf, CONF_localecho);
115 ldisc->localedit = conf_get_int(conf, CONF_localedit);
118 void ldisc_free(void *handle)
120 Ldisc ldisc = (Ldisc) handle;
123 ldisc->term->ldisc = NULL;
125 ldisc->back->provide_ldisc(ldisc->backhandle, NULL);
131 void ldisc_echoedit_update(void *handle)
133 Ldisc ldisc = (Ldisc) handle;
134 frontend_echoedit_update(ldisc->frontend, ECHOING, EDITING);
137 void ldisc_send(void *handle, const char *buf, int len, int interactive)
139 Ldisc ldisc = (Ldisc) handle;
146 * Notify the front end that something was pressed, in case
147 * it's depending on finding out (e.g. keypress termination for
150 frontend_keypress(ldisc->frontend);
154 * Interrupt a paste from the clipboard, if one was in
155 * progress when the user pressed a key. This is easier than
156 * buffering the current piece of data and saving it until the
157 * terminal has finished pasting, and has the potential side
158 * benefit of permitting a user to cancel an accidental huge
161 term_nopaste(ldisc->term);
165 * Less than zero means null terminated special string.
169 keyflag = KCTRL('@');
172 * Either perform local editing, or just send characters.
177 c = (unsigned char)(*buf++) + keyflag;
178 if (!interactive && c == '\r')
180 switch (ldisc->quotenext ? ' ' : c) {
182 * ^h/^?: delete, and output BSBs, to return to
183 * last character boundary (in UTF-8 mode this may
184 * be more than one byte)
185 * ^w: delete, and output BSBs, to return to last
186 * space/nonspace boundary
187 * ^u: delete, and output BSBs, to return to BOL
188 * ^c: Do a ^u then send a telnet IP
189 * ^z: Do a ^u then send a telnet SUSP
190 * ^\: Do a ^u then send a telnet ABORT
191 * ^r: echo "^R\n" and redraw line
192 * ^v: quote next char
193 * ^d: if at BOL, end of file and close connection,
194 * else send line and reset to BOL
195 * ^m: send line-plus-\r\n and reset to BOL
198 case KCTRL('?'): /* backspace/delete */
199 if (ldisc->buflen > 0) {
202 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
204 } while (!char_start(ldisc, ldisc->buf[ldisc->buflen]));
207 case CTRL('W'): /* delete word */
208 while (ldisc->buflen > 0) {
210 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
212 if (ldisc->buflen > 0 &&
213 isspace((unsigned char)ldisc->buf[ldisc->buflen-1]) &&
214 !isspace((unsigned char)ldisc->buf[ldisc->buflen]))
218 case CTRL('U'): /* delete line */
219 case CTRL('C'): /* Send IP */
220 case CTRL('\\'): /* Quit */
221 case CTRL('Z'): /* Suspend */
222 while (ldisc->buflen > 0) {
224 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
227 ldisc->back->special(ldisc->backhandle, TS_EL);
229 * We don't send IP, SUSP or ABORT if the user has
230 * configured telnet specials off! This breaks
233 if (!ldisc->telnet_keyboard)
236 ldisc->back->special(ldisc->backhandle, TS_IP);
238 ldisc->back->special(ldisc->backhandle, TS_SUSP);
240 ldisc->back->special(ldisc->backhandle, TS_ABORT);
242 case CTRL('R'): /* redraw line */
245 c_write(ldisc, "^R\r\n", 4);
246 for (i = 0; i < ldisc->buflen; i++)
247 pwrite(ldisc, ldisc->buf[i]);
250 case CTRL('V'): /* quote next char */
251 ldisc->quotenext = TRUE;
253 case CTRL('D'): /* logout or send */
254 if (ldisc->buflen == 0) {
255 ldisc->back->special(ldisc->backhandle, TS_EOF);
257 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
262 * This particularly hideous bit of code from RDB
263 * allows ordinary ^M^J to do the same thing as
264 * magic-^M when in Raw protocol. The line `case
265 * KCTRL('M'):' is _inside_ the if block. Thus:
267 * - receiving regular ^M goes straight to the
268 * default clause and inserts as a literal ^M.
269 * - receiving regular ^J _not_ directly after a
270 * literal ^M (or not in Raw protocol) fails the
271 * if condition, leaps to the bottom of the if,
272 * and falls through into the default clause
274 * - receiving regular ^J just after a literal ^M
275 * in Raw protocol passes the if condition,
276 * deletes the literal ^M, and falls through
277 * into the magic-^M code
278 * - receiving a magic-^M empties the line buffer,
279 * signals end-of-line in one of the various
280 * entertaining ways, and _doesn't_ fall out of
281 * the bottom of the if and through to the
282 * default clause because of the break.
285 if (ldisc->protocol == PROT_RAW &&
286 ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
288 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
291 case KCTRL('M'): /* send with newline */
292 if (ldisc->buflen > 0)
293 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
294 if (ldisc->protocol == PROT_RAW)
295 ldisc->back->send(ldisc->backhandle, "\r\n", 2);
296 else if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline)
297 ldisc->back->special(ldisc->backhandle, TS_EOL);
299 ldisc->back->send(ldisc->backhandle, "\r", 1);
301 c_write(ldisc, "\r\n", 2);
306 default: /* get to this label from ^V handler */
308 if (ldisc->buflen >= ldisc->bufsiz) {
309 ldisc->bufsiz = ldisc->buflen + 256;
310 ldisc->buf = sresize(ldisc->buf, ldisc->bufsiz, char);
312 ldisc->buf[ldisc->buflen++] = c;
314 pwrite(ldisc, (unsigned char) c);
315 ldisc->quotenext = FALSE;
320 if (ldisc->buflen != 0) {
321 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
322 while (ldisc->buflen > 0) {
323 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
329 c_write(ldisc, buf, len);
330 if (keyflag && ldisc->protocol == PROT_TELNET && len == 1) {
333 if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline)
334 ldisc->back->special(ldisc->backhandle, TS_EOL);
336 ldisc->back->send(ldisc->backhandle, "\r", 1);
340 if (ldisc->telnet_keyboard) {
341 ldisc->back->special(ldisc->backhandle, TS_EC);
345 if (ldisc->telnet_keyboard) {
346 ldisc->back->special(ldisc->backhandle, TS_IP);
350 if (ldisc->telnet_keyboard) {
351 ldisc->back->special(ldisc->backhandle, TS_SUSP);
356 ldisc->back->send(ldisc->backhandle, buf, len);
360 ldisc->back->send(ldisc->backhandle, buf, len);