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.
15 #define ECHOING (ldisc->localecho == FORCE_ON || \
16 (ldisc->localecho == AUTO && \
17 (ldisc->back->ldisc(ldisc->backhandle, LD_ECHO) || \
18 term_ldisc(ldisc->term, LD_ECHO))))
19 #define EDITING (ldisc->localedit == FORCE_ON || \
20 (ldisc->localedit == AUTO && \
21 (ldisc->back->ldisc(ldisc->backhandle, LD_EDIT) || \
22 term_ldisc(ldisc->term, LD_EDIT))))
24 static void c_write(Ldisc ldisc, char *buf, int len)
26 from_backend(ldisc->frontend, 0, buf, len);
29 static int plen(Ldisc ldisc, unsigned char c)
31 if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf(ldisc->term)))
34 return 2; /* ^x for some x */
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 */
41 return 4; /* <XY> hex representation */
44 static void pwrite(Ldisc ldisc, unsigned char c)
46 if ((c >= 32 && c <= 126) ||
47 (!in_utf(ldisc->term) && c >= 0xA0) ||
48 (in_utf(ldisc->term) && c >= 0x80)) {
49 c_write(ldisc, (char *)&c, 1);
52 cc[1] = (c == 127 ? '?' : c + 0x40);
54 c_write(ldisc, cc, 2);
57 sprintf(cc, "<%02X>", c);
58 c_write(ldisc, cc, 4);
62 static int char_start(Ldisc ldisc, unsigned char c)
64 if (in_utf(ldisc->term))
65 return (c < 0x80 || c >= 0xC0);
70 static void bsb(Ldisc ldisc, int n)
73 c_write(ldisc, "\010 \010", 3);
76 #define CTRL(x) (x^'@')
77 #define KCTRL(x) ((x^'@') | 0x100)
79 void *ldisc_create(Conf *conf, Terminal *term,
80 Backend *back, void *backhandle,
83 Ldisc ldisc = snew(struct ldisc_tag);
91 ldisc->backhandle = backhandle;
93 ldisc->frontend = frontend;
95 ldisc_configure(ldisc, conf);
97 /* Link ourselves into the backend and the terminal */
101 back->provide_ldisc(backhandle, ldisc);
106 void ldisc_configure(void *handle, Conf *conf)
108 Ldisc ldisc = (Ldisc) handle;
110 ldisc->telnet_keyboard = conf_get_int(conf, CONF_telnet_keyboard);
111 ldisc->telnet_newline = conf_get_int(conf, CONF_telnet_newline);
112 ldisc->protocol = conf_get_int(conf, CONF_protocol);
113 ldisc->localecho = conf_get_int(conf, CONF_localecho);
114 ldisc->localedit = conf_get_int(conf, CONF_localedit);
117 void ldisc_free(void *handle)
119 Ldisc ldisc = (Ldisc) handle;
122 ldisc->term->ldisc = NULL;
124 ldisc->back->provide_ldisc(ldisc->backhandle, NULL);
130 void ldisc_send(void *handle, char *buf, int len, int interactive)
132 Ldisc ldisc = (Ldisc) handle;
135 * Called with len=0 when the options change. We must inform
136 * the front end in case it needs to know.
139 ldisc_update(ldisc->frontend, ECHOING, EDITING);
143 * Notify the front end that something was pressed, in case
144 * it's depending on finding out (e.g. keypress termination for
147 frontend_keypress(ldisc->frontend);
150 * Less than zero means null terminated special string.
154 keyflag = KCTRL('@');
157 * Either perform local editing, or just send characters.
162 c = (unsigned char)(*buf++) + keyflag;
163 if (!interactive && c == '\r')
165 switch (ldisc->quotenext ? ' ' : c) {
167 * ^h/^?: delete, and output BSBs, to return to
168 * last character boundary (in UTF-8 mode this may
169 * be more than one byte)
170 * ^w: delete, and output BSBs, to return to last
171 * space/nonspace boundary
172 * ^u: delete, and output BSBs, to return to BOL
173 * ^c: Do a ^u then send a telnet IP
174 * ^z: Do a ^u then send a telnet SUSP
175 * ^\: Do a ^u then send a telnet ABORT
176 * ^r: echo "^R\n" and redraw line
177 * ^v: quote next char
178 * ^d: if at BOL, end of file and close connection,
179 * else send line and reset to BOL
180 * ^m: send line-plus-\r\n and reset to BOL
183 case KCTRL('?'): /* backspace/delete */
184 if (ldisc->buflen > 0) {
187 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
189 } while (!char_start(ldisc, ldisc->buf[ldisc->buflen]));
192 case CTRL('W'): /* delete word */
193 while (ldisc->buflen > 0) {
195 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
197 if (ldisc->buflen > 0 &&
198 isspace((unsigned char)ldisc->buf[ldisc->buflen-1]) &&
199 !isspace((unsigned char)ldisc->buf[ldisc->buflen]))
203 case CTRL('U'): /* delete line */
204 case CTRL('C'): /* Send IP */
205 case CTRL('\\'): /* Quit */
206 case CTRL('Z'): /* Suspend */
207 while (ldisc->buflen > 0) {
209 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
212 ldisc->back->special(ldisc->backhandle, TS_EL);
214 * We don't send IP, SUSP or ABORT if the user has
215 * configured telnet specials off! This breaks
218 if (!ldisc->telnet_keyboard)
221 ldisc->back->special(ldisc->backhandle, TS_IP);
223 ldisc->back->special(ldisc->backhandle, TS_SUSP);
225 ldisc->back->special(ldisc->backhandle, TS_ABORT);
227 case CTRL('R'): /* redraw line */
230 c_write(ldisc, "^R\r\n", 4);
231 for (i = 0; i < ldisc->buflen; i++)
232 pwrite(ldisc, ldisc->buf[i]);
235 case CTRL('V'): /* quote next char */
236 ldisc->quotenext = TRUE;
238 case CTRL('D'): /* logout or send */
239 if (ldisc->buflen == 0) {
240 ldisc->back->special(ldisc->backhandle, TS_EOF);
242 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
247 * This particularly hideous bit of code from RDB
248 * allows ordinary ^M^J to do the same thing as
249 * magic-^M when in Raw protocol. The line `case
250 * KCTRL('M'):' is _inside_ the if block. Thus:
252 * - receiving regular ^M goes straight to the
253 * default clause and inserts as a literal ^M.
254 * - receiving regular ^J _not_ directly after a
255 * literal ^M (or not in Raw protocol) fails the
256 * if condition, leaps to the bottom of the if,
257 * and falls through into the default clause
259 * - receiving regular ^J just after a literal ^M
260 * in Raw protocol passes the if condition,
261 * deletes the literal ^M, and falls through
262 * into the magic-^M code
263 * - receiving a magic-^M empties the line buffer,
264 * signals end-of-line in one of the various
265 * entertaining ways, and _doesn't_ fall out of
266 * the bottom of the if and through to the
267 * default clause because of the break.
270 if (ldisc->protocol == PROT_RAW &&
271 ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
273 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
276 case KCTRL('M'): /* send with newline */
277 if (ldisc->buflen > 0)
278 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
279 if (ldisc->protocol == PROT_RAW)
280 ldisc->back->send(ldisc->backhandle, "\r\n", 2);
281 else if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline)
282 ldisc->back->special(ldisc->backhandle, TS_EOL);
284 ldisc->back->send(ldisc->backhandle, "\r", 1);
286 c_write(ldisc, "\r\n", 2);
291 default: /* get to this label from ^V handler */
293 if (ldisc->buflen >= ldisc->bufsiz) {
294 ldisc->bufsiz = ldisc->buflen + 256;
295 ldisc->buf = sresize(ldisc->buf, ldisc->bufsiz, char);
297 ldisc->buf[ldisc->buflen++] = c;
299 pwrite(ldisc, (unsigned char) c);
300 ldisc->quotenext = FALSE;
305 if (ldisc->buflen != 0) {
306 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
307 while (ldisc->buflen > 0) {
308 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
314 c_write(ldisc, buf, len);
315 if (keyflag && ldisc->protocol == PROT_TELNET && len == 1) {
318 if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline)
319 ldisc->back->special(ldisc->backhandle, TS_EOL);
321 ldisc->back->send(ldisc->backhandle, "\r", 1);
325 if (ldisc->telnet_keyboard) {
326 ldisc->back->special(ldisc->backhandle, TS_EC);
330 if (ldisc->telnet_keyboard) {
331 ldisc->back->special(ldisc->backhandle, TS_IP);
335 if (ldisc->telnet_keyboard) {
336 ldisc->back->special(ldisc->backhandle, TS_SUSP);
341 ldisc->back->send(ldisc->backhandle, buf, len);
345 ldisc->back->send(ldisc->backhandle, buf, len);