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.
14 #define ECHOING (cfg.localecho == LD_YES || \
15 (cfg.localecho == LD_BACKEND && \
16 (back->ldisc(backhandle, LD_ECHO) || \
17 term_ldisc(term, LD_ECHO))))
18 #define EDITING (cfg.localedit == LD_YES || \
19 (cfg.localedit == LD_BACKEND && \
20 (back->ldisc(backhandle, LD_EDIT) || \
21 term_ldisc(term, LD_EDIT))))
23 static void c_write(char *buf
, int len
)
25 from_backend(term
, 0, buf
, len
);
28 static char *term_buf
= NULL
;
29 static int term_buflen
= 0, term_bufsiz
= 0, term_quotenext
= 0;
31 static int plen(unsigned char c
)
33 if ((c
>= 32 && c
<= 126) || (c
>= 160 && !in_utf(term
)))
36 return 2; /* ^x for some x */
38 return 4; /* <XY> for hex XY */
41 static void pwrite(unsigned char c
)
43 if ((c
>= 32 && c
<= 126) || (c
>= 160 && !in_utf(term
))) {
47 cc
[1] = (c
== 127 ?
'?' : c
+ 0x40);
52 sprintf(cc
, "<%02X>", c
);
57 static void bsb(int n
)
60 c_write("\010 \010", 3);
63 #define CTRL(x) (x^'@')
64 #define KCTRL(x) ((x^'@') | 0x100)
66 void ldisc_send(char *buf
, int len
, int interactive
)
70 * Called with len=0 when the options change. We must inform
71 * the front end in case it needs to know.
74 void ldisc_update(int echo
, int edit
);
75 ldisc_update(ECHOING
, EDITING
);
79 * Notify the front end that something was pressed, in case
80 * it's depending on finding out (e.g. keypress termination for
85 * Less than zero means null terminated special string.
92 * Either perform local editing, or just send characters.
98 if (!interactive
&& c
== '\r')
100 switch (term_quotenext ?
' ' : c
) {
102 * ^h/^?: delete one char and output one BSB
103 * ^w: delete, and output BSBs, to return to last
104 * space/nonspace boundary
105 * ^u: delete, and output BSBs, to return to BOL
106 * ^c: Do a ^u then send a telnet IP
107 * ^z: Do a ^u then send a telnet SUSP
108 * ^\: Do a ^u then send a telnet ABORT
109 * ^r: echo "^R\n" and redraw line
110 * ^v: quote next char
111 * ^d: if at BOL, end of file and close connection,
112 * else send line and reset to BOL
113 * ^m: send line-plus-\r\n and reset to BOL
116 case KCTRL('?'): /* backspace/delete */
117 if (term_buflen
> 0) {
119 bsb(plen(term_buf
[term_buflen
- 1]));
123 case CTRL('W'): /* delete word */
124 while (term_buflen
> 0) {
126 bsb(plen(term_buf
[term_buflen
- 1]));
128 if (term_buflen
> 0 &&
129 isspace(term_buf
[term_buflen
- 1]) &&
130 !isspace(term_buf
[term_buflen
]))
134 case CTRL('U'): /* delete line */
135 case CTRL('C'): /* Send IP */
136 case CTRL('\\'): /* Quit */
137 case CTRL('Z'): /* Suspend */
138 while (term_buflen
> 0) {
140 bsb(plen(term_buf
[term_buflen
- 1]));
143 back
->special(backhandle
, TS_EL
);
145 * We don't send IP, SUSP or ABORT if the user has
146 * configured telnet specials off! This breaks
149 if (!cfg
.telnet_keyboard
)
152 back
->special(backhandle
, TS_IP
);
154 back
->special(backhandle
, TS_SUSP
);
156 back
->special(backhandle
, TS_ABORT
);
158 case CTRL('R'): /* redraw line */
161 c_write("^R\r\n", 4);
162 for (i
= 0; i
< term_buflen
; i
++)
166 case CTRL('V'): /* quote next char */
167 term_quotenext
= TRUE
;
169 case CTRL('D'): /* logout or send */
170 if (term_buflen
== 0) {
171 back
->special(backhandle
, TS_EOF
);
173 back
->send(backhandle
, term_buf
, term_buflen
);
178 * This particularly hideous bit of code from RDB
179 * allows ordinary ^M^J to do the same thing as
180 * magic-^M when in Raw protocol. The line `case
181 * KCTRL('M'):' is _inside_ the if block. Thus:
183 * - receiving regular ^M goes straight to the
184 * default clause and inserts as a literal ^M.
185 * - receiving regular ^J _not_ directly after a
186 * literal ^M (or not in Raw protocol) fails the
187 * if condition, leaps to the bottom of the if,
188 * and falls through into the default clause
190 * - receiving regular ^J just after a literal ^M
191 * in Raw protocol passes the if condition,
192 * deletes the literal ^M, and falls through
193 * into the magic-^M code
194 * - receiving a magic-^M empties the line buffer,
195 * signals end-of-line in one of the various
196 * entertaining ways, and _doesn't_ fall out of
197 * the bottom of the if and through to the
198 * default clause because of the break.
201 if (cfg
.protocol
== PROT_RAW
&&
202 term_buflen
> 0 && term_buf
[term_buflen
- 1] == '\r') {
204 bsb(plen(term_buf
[term_buflen
- 1]));
207 case KCTRL('M'): /* send with newline */
209 back
->send(backhandle
, term_buf
, term_buflen
);
210 if (cfg
.protocol
== PROT_RAW
)
211 back
->send(backhandle
, "\r\n", 2);
212 else if (cfg
.protocol
== PROT_TELNET
&& cfg
.telnet_newline
)
213 back
->special(backhandle
, TS_EOL
);
215 back
->send(backhandle
, "\r", 1);
222 default: /* get to this label from ^V handler */
224 if (term_buflen
>= term_bufsiz
) {
225 term_bufsiz
= term_buflen
+ 256;
226 term_buf
= saferealloc(term_buf
, term_bufsiz
);
228 term_buf
[term_buflen
++] = c
;
230 pwrite((unsigned char) c
);
231 term_quotenext
= FALSE
;
236 if (term_buflen
!= 0) {
237 back
->send(backhandle
, term_buf
, term_buflen
);
238 while (term_buflen
> 0) {
239 bsb(plen(term_buf
[term_buflen
- 1]));
246 if (keyflag
&& cfg
.protocol
== PROT_TELNET
&& len
== 1) {
249 if (cfg
.protocol
== PROT_TELNET
&& cfg
.telnet_newline
)
250 back
->special(backhandle
, TS_EOL
);
252 back
->send(backhandle
, "\r", 1);
256 if (cfg
.telnet_keyboard
) {
257 back
->special(backhandle
, TS_EC
);
261 if (cfg
.telnet_keyboard
) {
262 back
->special(backhandle
, TS_IP
);
266 if (cfg
.telnet_keyboard
) {
267 back
->special(backhandle
, TS_SUSP
);
272 back
->send(backhandle
, buf
, len
);
276 back
->send(backhandle
, buf
, len
);