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(LD_ECHO) || term_ldisc(term, LD_ECHO))))
17 #define EDITING (cfg.localedit == LD_YES || \
18 (cfg.localedit == LD_BACKEND && \
19 (back->ldisc(LD_EDIT) || term_ldisc(term, LD_EDIT))))
21 static void c_write(char *buf
, int len
)
23 from_backend(term
, 0, buf
, len
);
26 static char *term_buf
= NULL
;
27 static int term_buflen
= 0, term_bufsiz
= 0, term_quotenext
= 0;
29 static int plen(unsigned char c
)
31 if ((c
>= 32 && c
<= 126) || (c
>= 160 && !in_utf(term
)))
34 return 2; /* ^x for some x */
36 return 4; /* <XY> for hex XY */
39 static void pwrite(unsigned char c
)
41 if ((c
>= 32 && c
<= 126) || (c
>= 160 && !in_utf(term
))) {
45 cc
[1] = (c
== 127 ?
'?' : c
+ 0x40);
50 sprintf(cc
, "<%02X>", c
);
55 static void bsb(int n
)
58 c_write("\010 \010", 3);
61 #define CTRL(x) (x^'@')
62 #define KCTRL(x) ((x^'@') | 0x100)
64 void ldisc_send(char *buf
, int len
, int interactive
)
68 * Called with len=0 when the options change. We must inform
69 * the front end in case it needs to know.
72 void ldisc_update(int echo
, int edit
);
73 ldisc_update(ECHOING
, EDITING
);
77 * Notify the front end that something was pressed, in case
78 * it's depending on finding out (e.g. keypress termination for
83 * Less than zero means null terminated special string.
90 * Either perform local editing, or just send characters.
96 if (!interactive
&& c
== '\r')
98 switch (term_quotenext ?
' ' : c
) {
100 * ^h/^?: delete one char and output one BSB
101 * ^w: delete, and output BSBs, to return to last
102 * space/nonspace boundary
103 * ^u: delete, and output BSBs, to return to BOL
104 * ^c: Do a ^u then send a telnet IP
105 * ^z: Do a ^u then send a telnet SUSP
106 * ^\: Do a ^u then send a telnet ABORT
107 * ^r: echo "^R\n" and redraw line
108 * ^v: quote next char
109 * ^d: if at BOL, end of file and close connection,
110 * else send line and reset to BOL
111 * ^m: send line-plus-\r\n and reset to BOL
114 case KCTRL('?'): /* backspace/delete */
115 if (term_buflen
> 0) {
117 bsb(plen(term_buf
[term_buflen
- 1]));
121 case CTRL('W'): /* delete word */
122 while (term_buflen
> 0) {
124 bsb(plen(term_buf
[term_buflen
- 1]));
126 if (term_buflen
> 0 &&
127 isspace(term_buf
[term_buflen
- 1]) &&
128 !isspace(term_buf
[term_buflen
]))
132 case CTRL('U'): /* delete line */
133 case CTRL('C'): /* Send IP */
134 case CTRL('\\'): /* Quit */
135 case CTRL('Z'): /* Suspend */
136 while (term_buflen
> 0) {
138 bsb(plen(term_buf
[term_buflen
- 1]));
141 back
->special(TS_EL
);
143 * We don't send IP, SUSP or ABORT if the user has
144 * configured telnet specials off! This breaks
147 if (!cfg
.telnet_keyboard
)
150 back
->special(TS_IP
);
152 back
->special(TS_SUSP
);
154 back
->special(TS_ABORT
);
156 case CTRL('R'): /* redraw line */
159 c_write("^R\r\n", 4);
160 for (i
= 0; i
< term_buflen
; i
++)
164 case CTRL('V'): /* quote next char */
165 term_quotenext
= TRUE
;
167 case CTRL('D'): /* logout or send */
168 if (term_buflen
== 0) {
169 back
->special(TS_EOF
);
171 back
->send(term_buf
, term_buflen
);
176 * This particularly hideous bit of code from RDB
177 * allows ordinary ^M^J to do the same thing as
178 * magic-^M when in Raw protocol. The line `case
179 * KCTRL('M'):' is _inside_ the if block. Thus:
181 * - receiving regular ^M goes straight to the
182 * default clause and inserts as a literal ^M.
183 * - receiving regular ^J _not_ directly after a
184 * literal ^M (or not in Raw protocol) fails the
185 * if condition, leaps to the bottom of the if,
186 * and falls through into the default clause
188 * - receiving regular ^J just after a literal ^M
189 * in Raw protocol passes the if condition,
190 * deletes the literal ^M, and falls through
191 * into the magic-^M code
192 * - receiving a magic-^M empties the line buffer,
193 * signals end-of-line in one of the various
194 * entertaining ways, and _doesn't_ fall out of
195 * the bottom of the if and through to the
196 * default clause because of the break.
199 if (cfg
.protocol
== PROT_RAW
&&
200 term_buflen
> 0 && term_buf
[term_buflen
- 1] == '\r') {
202 bsb(plen(term_buf
[term_buflen
- 1]));
205 case KCTRL('M'): /* send with newline */
207 back
->send(term_buf
, term_buflen
);
208 if (cfg
.protocol
== PROT_RAW
)
209 back
->send("\r\n", 2);
210 else if (cfg
.protocol
== PROT_TELNET
&& cfg
.telnet_newline
)
211 back
->special(TS_EOL
);
220 default: /* get to this label from ^V handler */
222 if (term_buflen
>= term_bufsiz
) {
223 term_bufsiz
= term_buflen
+ 256;
224 term_buf
= saferealloc(term_buf
, term_bufsiz
);
226 term_buf
[term_buflen
++] = c
;
228 pwrite((unsigned char) c
);
229 term_quotenext
= FALSE
;
234 if (term_buflen
!= 0) {
235 back
->send(term_buf
, term_buflen
);
236 while (term_buflen
> 0) {
237 bsb(plen(term_buf
[term_buflen
- 1]));
244 if (keyflag
&& cfg
.protocol
== PROT_TELNET
&& len
== 1) {
247 if (cfg
.protocol
== PROT_TELNET
&& cfg
.telnet_newline
)
248 back
->special(TS_EOL
);
254 if (cfg
.telnet_keyboard
) {
255 back
->special(TS_EC
);
259 if (cfg
.telnet_keyboard
) {
260 back
->special(TS_IP
);
264 if (cfg
.telnet_keyboard
) {
265 back
->special(TS_SUSP
);
270 back
->send(buf
, len
);
274 back
->send(buf
, len
);