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->cfg->localecho == FORCE_ON || \
16 (ldisc->cfg->localecho == AUTO && \
17 (ldisc->back->ldisc(ldisc->backhandle, LD_ECHO) || \
18 term_ldisc(ldisc->term, LD_ECHO))))
19 #define EDITING (ldisc->cfg->localedit == FORCE_ON || \
20 (ldisc->cfg->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(Config
*mycfg
, Terminal
*term
,
80 Backend
*back
, void *backhandle
,
83 Ldisc ldisc
= snew(struct ldisc_tag
);
92 ldisc
->backhandle
= backhandle
;
94 ldisc
->frontend
= frontend
;
96 /* Link ourselves into the backend and the terminal */
100 back
->provide_ldisc(backhandle
, ldisc
);
105 void ldisc_free(void *handle
)
107 Ldisc ldisc
= (Ldisc
) handle
;
110 ldisc
->term
->ldisc
= NULL
;
112 ldisc
->back
->provide_ldisc(ldisc
->backhandle
, NULL
);
118 void ldisc_send(void *handle
, char *buf
, int len
, int interactive
)
120 Ldisc ldisc
= (Ldisc
) handle
;
123 * Called with len=0 when the options change. We must inform
124 * the front end in case it needs to know.
127 ldisc_update(ldisc
->frontend
, ECHOING
, EDITING
);
131 * Notify the front end that something was pressed, in case
132 * it's depending on finding out (e.g. keypress termination for
135 frontend_keypress(ldisc
->frontend
);
138 * Less than zero means null terminated special string.
142 keyflag
= KCTRL('@');
145 * Either perform local editing, or just send characters.
150 c
= *buf
++ + keyflag
;
151 if (!interactive
&& c
== '\r')
153 switch (ldisc
->quotenext
? ' ' : c
) {
155 * ^h/^?: delete, and output BSBs, to return to
156 * last character boundary (in UTF-8 mode this may
157 * be more than one byte)
158 * ^w: delete, and output BSBs, to return to last
159 * space/nonspace boundary
160 * ^u: delete, and output BSBs, to return to BOL
161 * ^c: Do a ^u then send a telnet IP
162 * ^z: Do a ^u then send a telnet SUSP
163 * ^\: Do a ^u then send a telnet ABORT
164 * ^r: echo "^R\n" and redraw line
165 * ^v: quote next char
166 * ^d: if at BOL, end of file and close connection,
167 * else send line and reset to BOL
168 * ^m: send line-plus-\r\n and reset to BOL
171 case KCTRL('?'): /* backspace/delete */
172 if (ldisc
->buflen
> 0) {
175 bsb(ldisc
, plen(ldisc
, ldisc
->buf
[ldisc
->buflen
- 1]));
177 } while (!char_start(ldisc
, ldisc
->buf
[ldisc
->buflen
]));
180 case CTRL('W'): /* delete word */
181 while (ldisc
->buflen
> 0) {
183 bsb(ldisc
, plen(ldisc
, ldisc
->buf
[ldisc
->buflen
- 1]));
185 if (ldisc
->buflen
> 0 &&
186 isspace((unsigned char)ldisc
->buf
[ldisc
->buflen
-1]) &&
187 !isspace((unsigned char)ldisc
->buf
[ldisc
->buflen
]))
191 case CTRL('U'): /* delete line */
192 case CTRL('C'): /* Send IP */
193 case CTRL('\\'): /* Quit */
194 case CTRL('Z'): /* Suspend */
195 while (ldisc
->buflen
> 0) {
197 bsb(ldisc
, plen(ldisc
, ldisc
->buf
[ldisc
->buflen
- 1]));
200 ldisc
->back
->special(ldisc
->backhandle
, TS_EL
);
202 * We don't send IP, SUSP or ABORT if the user has
203 * configured telnet specials off! This breaks
206 if (!ldisc
->cfg
->telnet_keyboard
)
209 ldisc
->back
->special(ldisc
->backhandle
, TS_IP
);
211 ldisc
->back
->special(ldisc
->backhandle
, TS_SUSP
);
213 ldisc
->back
->special(ldisc
->backhandle
, TS_ABORT
);
215 case CTRL('R'): /* redraw line */
218 c_write(ldisc
, "^R\r\n", 4);
219 for (i
= 0; i
< ldisc
->buflen
; i
++)
220 pwrite(ldisc
, ldisc
->buf
[i
]);
223 case CTRL('V'): /* quote next char */
224 ldisc
->quotenext
= TRUE
;
226 case CTRL('D'): /* logout or send */
227 if (ldisc
->buflen
== 0) {
228 ldisc
->back
->special(ldisc
->backhandle
, TS_EOF
);
230 ldisc
->back
->send(ldisc
->backhandle
, ldisc
->buf
, ldisc
->buflen
);
235 * This particularly hideous bit of code from RDB
236 * allows ordinary ^M^J to do the same thing as
237 * magic-^M when in Raw protocol. The line `case
238 * KCTRL('M'):' is _inside_ the if block. Thus:
240 * - receiving regular ^M goes straight to the
241 * default clause and inserts as a literal ^M.
242 * - receiving regular ^J _not_ directly after a
243 * literal ^M (or not in Raw protocol) fails the
244 * if condition, leaps to the bottom of the if,
245 * and falls through into the default clause
247 * - receiving regular ^J just after a literal ^M
248 * in Raw protocol passes the if condition,
249 * deletes the literal ^M, and falls through
250 * into the magic-^M code
251 * - receiving a magic-^M empties the line buffer,
252 * signals end-of-line in one of the various
253 * entertaining ways, and _doesn't_ fall out of
254 * the bottom of the if and through to the
255 * default clause because of the break.
260 bsb(ldisc
, plen(ldisc
, ldisc
->buf
[ldisc
->buflen
- 1]));
263 case KCTRL('M'): /* send with newline */
264 if (ldisc
->buflen
> 0)
265 ldisc
->back
->send(ldisc
->backhandle
, ldisc
->buf
, ldisc
->buflen
);
267 ldisc
->back
->send(ldisc
->backhandle
, "\r", 1);
269 c_write(ldisc
, "\r\n", 2);
274 default: /* get to this label from ^V handler */
276 if (ldisc
->buflen
>= ldisc
->bufsiz
) {
277 ldisc
->bufsiz
= ldisc
->buflen
+ 256;
278 ldisc
->buf
= sresize(ldisc
->buf
, ldisc
->bufsiz
, char);
280 ldisc
->buf
[ldisc
->buflen
++] = c
;
282 pwrite(ldisc
, (unsigned char) c
);
283 ldisc
->quotenext
= FALSE
;
288 if (ldisc
->buflen
!= 0) {
289 ldisc
->back
->send(ldisc
->backhandle
, ldisc
->buf
, ldisc
->buflen
);
290 while (ldisc
->buflen
> 0) {
291 bsb(ldisc
, plen(ldisc
, ldisc
->buf
[ldisc
->buflen
- 1]));
297 c_write(ldisc
, buf
, len
);
298 ldisc
->back
->send(ldisc
->backhandle
, buf
, len
);