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
->term
, 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 */
36 return 4; /* <XY> for hex XY */
39 static void pwrite(Ldisc ldisc
, unsigned char c
)
41 if ((c
>= 32 && c
<= 126) || (c
>= 160 && !in_utf(ldisc
->term
))) {
42 c_write(ldisc
, (char *)&c
, 1);
45 cc
[1] = (c
== 127 ?
'?' : c
+ 0x40);
47 c_write(ldisc
, cc
, 2);
50 sprintf(cc
, "<%02X>", c
);
51 c_write(ldisc
, cc
, 4);
55 static void bsb(Ldisc ldisc
, int n
)
58 c_write(ldisc
, "\010 \010", 3);
61 #define CTRL(x) (x^'@')
62 #define KCTRL(x) ((x^'@') | 0x100)
64 void *ldisc_create(Config
*mycfg
, Terminal
*term
,
65 Backend
*back
, void *backhandle
,
68 Ldisc ldisc
= smalloc(sizeof(*ldisc
));
77 ldisc
->backhandle
= backhandle
;
79 ldisc
->frontend
= frontend
;
81 /* Link ourselves into the backend and the terminal */
85 back
->provide_ldisc(backhandle
, ldisc
);
90 void ldisc_free(void *handle
)
92 Ldisc ldisc
= (Ldisc
) handle
;
95 ldisc
->term
->ldisc
= NULL
;
97 ldisc
->back
->provide_ldisc(ldisc
->backhandle
, NULL
);
103 void ldisc_send(void *handle
, char *buf
, int len
, int interactive
)
105 Ldisc ldisc
= (Ldisc
) handle
;
108 * Called with len=0 when the options change. We must inform
109 * the front end in case it needs to know.
112 ldisc_update(ldisc
->frontend
, ECHOING
, EDITING
);
116 * Notify the front end that something was pressed, in case
117 * it's depending on finding out (e.g. keypress termination for
120 frontend_keypress(ldisc
->frontend
);
123 * Less than zero means null terminated special string.
127 keyflag
= KCTRL('@');
130 * Either perform local editing, or just send characters.
135 c
= *buf
++ + keyflag
;
136 if (!interactive
&& c
== '\r')
138 switch (ldisc
->quotenext ?
' ' : c
) {
140 * ^h/^?: delete one char and output one BSB
141 * ^w: delete, and output BSBs, to return to last
142 * space/nonspace boundary
143 * ^u: delete, and output BSBs, to return to BOL
144 * ^c: Do a ^u then send a telnet IP
145 * ^z: Do a ^u then send a telnet SUSP
146 * ^\: Do a ^u then send a telnet ABORT
147 * ^r: echo "^R\n" and redraw line
148 * ^v: quote next char
149 * ^d: if at BOL, end of file and close connection,
150 * else send line and reset to BOL
151 * ^m: send line-plus-\r\n and reset to BOL
154 case KCTRL('?'): /* backspace/delete */
155 if (ldisc
->buflen
> 0) {
157 bsb(ldisc
, plen(ldisc
, ldisc
->buf
[ldisc
->buflen
- 1]));
161 case CTRL('W'): /* delete word */
162 while (ldisc
->buflen
> 0) {
164 bsb(ldisc
, plen(ldisc
, ldisc
->buf
[ldisc
->buflen
- 1]));
166 if (ldisc
->buflen
> 0 &&
167 isspace((unsigned char)ldisc
->buf
[ldisc
->buflen
-1]) &&
168 !isspace((unsigned char)ldisc
->buf
[ldisc
->buflen
]))
172 case CTRL('U'): /* delete line */
173 case CTRL('C'): /* Send IP */
174 case CTRL('\\'): /* Quit */
175 case CTRL('Z'): /* Suspend */
176 while (ldisc
->buflen
> 0) {
178 bsb(ldisc
, plen(ldisc
, ldisc
->buf
[ldisc
->buflen
- 1]));
181 ldisc
->back
->special(ldisc
->backhandle
, TS_EL
);
183 * We don't send IP, SUSP or ABORT if the user has
184 * configured telnet specials off! This breaks
187 if (!ldisc
->cfg
->telnet_keyboard
)
190 ldisc
->back
->special(ldisc
->backhandle
, TS_IP
);
192 ldisc
->back
->special(ldisc
->backhandle
, TS_SUSP
);
194 ldisc
->back
->special(ldisc
->backhandle
, TS_ABORT
);
196 case CTRL('R'): /* redraw line */
199 c_write(ldisc
, "^R\r\n", 4);
200 for (i
= 0; i
< ldisc
->buflen
; i
++)
201 pwrite(ldisc
, ldisc
->buf
[i
]);
204 case CTRL('V'): /* quote next char */
205 ldisc
->quotenext
= TRUE
;
207 case CTRL('D'): /* logout or send */
208 if (ldisc
->buflen
== 0) {
209 ldisc
->back
->special(ldisc
->backhandle
, TS_EOF
);
211 ldisc
->back
->send(ldisc
->backhandle
, ldisc
->buf
, ldisc
->buflen
);
216 * This particularly hideous bit of code from RDB
217 * allows ordinary ^M^J to do the same thing as
218 * magic-^M when in Raw protocol. The line `case
219 * KCTRL('M'):' is _inside_ the if block. Thus:
221 * - receiving regular ^M goes straight to the
222 * default clause and inserts as a literal ^M.
223 * - receiving regular ^J _not_ directly after a
224 * literal ^M (or not in Raw protocol) fails the
225 * if condition, leaps to the bottom of the if,
226 * and falls through into the default clause
228 * - receiving regular ^J just after a literal ^M
229 * in Raw protocol passes the if condition,
230 * deletes the literal ^M, and falls through
231 * into the magic-^M code
232 * - receiving a magic-^M empties the line buffer,
233 * signals end-of-line in one of the various
234 * entertaining ways, and _doesn't_ fall out of
235 * the bottom of the if and through to the
236 * default clause because of the break.
239 if (ldisc
->cfg
->protocol
== PROT_RAW
&&
240 ldisc
->buflen
> 0 && ldisc
->buf
[ldisc
->buflen
- 1] == '\r') {
242 bsb(ldisc
, plen(ldisc
, ldisc
->buf
[ldisc
->buflen
- 1]));
245 case KCTRL('M'): /* send with newline */
246 if (ldisc
->buflen
> 0)
247 ldisc
->back
->send(ldisc
->backhandle
, ldisc
->buf
, ldisc
->buflen
);
248 if (ldisc
->cfg
->protocol
== PROT_RAW
)
249 ldisc
->back
->send(ldisc
->backhandle
, "\r\n", 2);
250 else if (ldisc
->cfg
->protocol
== PROT_TELNET
&& ldisc
->cfg
->telnet_newline
)
251 ldisc
->back
->special(ldisc
->backhandle
, TS_EOL
);
253 ldisc
->back
->send(ldisc
->backhandle
, "\r", 1);
255 c_write(ldisc
, "\r\n", 2);
260 default: /* get to this label from ^V handler */
262 if (ldisc
->buflen
>= ldisc
->bufsiz
) {
263 ldisc
->bufsiz
= ldisc
->buflen
+ 256;
264 ldisc
->buf
= srealloc(ldisc
->buf
, ldisc
->bufsiz
);
266 ldisc
->buf
[ldisc
->buflen
++] = c
;
268 pwrite(ldisc
, (unsigned char) c
);
269 ldisc
->quotenext
= FALSE
;
274 if (ldisc
->buflen
!= 0) {
275 ldisc
->back
->send(ldisc
->backhandle
, ldisc
->buf
, ldisc
->buflen
);
276 while (ldisc
->buflen
> 0) {
277 bsb(ldisc
, plen(ldisc
, ldisc
->buf
[ldisc
->buflen
- 1]));
283 c_write(ldisc
, buf
, len
);
284 if (keyflag
&& ldisc
->cfg
->protocol
== PROT_TELNET
&& len
== 1) {
287 if (ldisc
->cfg
->protocol
== PROT_TELNET
&& ldisc
->cfg
->telnet_newline
)
288 ldisc
->back
->special(ldisc
->backhandle
, TS_EOL
);
290 ldisc
->back
->send(ldisc
->backhandle
, "\r", 1);
294 if (ldisc
->cfg
->telnet_keyboard
) {
295 ldisc
->back
->special(ldisc
->backhandle
, TS_EC
);
299 if (ldisc
->cfg
->telnet_keyboard
) {
300 ldisc
->back
->special(ldisc
->backhandle
, TS_IP
);
304 if (ldisc
->cfg
->telnet_keyboard
) {
305 ldisc
->back
->special(ldisc
->backhandle
, TS_SUSP
);
310 ldisc
->back
->send(ldisc
->backhandle
, buf
, len
);
314 ldisc
->back
->send(ldisc
->backhandle
, buf
, len
);