Line discipline module now uses dynamically allocated data. Also
[u/mdw/putty] / ldisc.c
1 /*
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.
6 */
7
8 #include <stdio.h>
9 #include <ctype.h>
10
11 #include "putty.h"
12 #include "terminal.h"
13
14 typedef struct ldisc_tag {
15 Terminal *term;
16 Backend *back;
17 void *backhandle;
18 void *frontend;
19
20 char *buf;
21 int buflen, bufsiz, quotenext;
22 } *Ldisc;
23
24 #define ECHOING (cfg.localecho == LD_YES || \
25 (cfg.localecho == LD_BACKEND && \
26 (ldisc->back->ldisc(ldisc->backhandle, LD_ECHO) || \
27 term_ldisc(ldisc->term, LD_ECHO))))
28 #define EDITING (cfg.localedit == LD_YES || \
29 (cfg.localedit == LD_BACKEND && \
30 (ldisc->back->ldisc(ldisc->backhandle, LD_EDIT) || \
31 term_ldisc(ldisc->term, LD_EDIT))))
32
33 static void c_write(Ldisc ldisc, char *buf, int len)
34 {
35 from_backend(ldisc->term, 0, buf, len);
36 }
37
38 static int plen(Ldisc ldisc, unsigned char c)
39 {
40 if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf(ldisc->term)))
41 return 1;
42 else if (c < 128)
43 return 2; /* ^x for some x */
44 else
45 return 4; /* <XY> for hex XY */
46 }
47
48 static void pwrite(Ldisc ldisc, unsigned char c)
49 {
50 if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf(ldisc->term))) {
51 c_write(ldisc, &c, 1);
52 } else if (c < 128) {
53 char cc[2];
54 cc[1] = (c == 127 ? '?' : c + 0x40);
55 cc[0] = '^';
56 c_write(ldisc, cc, 2);
57 } else {
58 char cc[5];
59 sprintf(cc, "<%02X>", c);
60 c_write(ldisc, cc, 4);
61 }
62 }
63
64 static void bsb(Ldisc ldisc, int n)
65 {
66 while (n--)
67 c_write(ldisc, "\010 \010", 3);
68 }
69
70 #define CTRL(x) (x^'@')
71 #define KCTRL(x) ((x^'@') | 0x100)
72
73 void *ldisc_create(Terminal *term,
74 Backend *back, void *backhandle,
75 void *frontend)
76 {
77 Ldisc ldisc = smalloc(sizeof(*ldisc));
78
79 ldisc->buf = NULL;
80 ldisc->buflen = 0;
81 ldisc->bufsiz = 0;
82 ldisc->quotenext = 0;
83
84 ldisc->back = back;
85 ldisc->backhandle = backhandle;
86 ldisc->term = term;
87 ldisc->frontend = frontend;
88
89 /* Link ourselves into the backend and the terminal */
90 if (term)
91 term->ldisc = ldisc;
92 if (back)
93 back->provide_ldisc(backhandle, ldisc);
94
95 return ldisc;
96 }
97
98 void ldisc_send(void *handle, char *buf, int len, int interactive)
99 {
100 Ldisc ldisc = (Ldisc) handle;
101 int keyflag = 0;
102 /*
103 * Called with len=0 when the options change. We must inform
104 * the front end in case it needs to know.
105 */
106 if (len == 0) {
107 ldisc_update(ldisc->frontend, ECHOING, EDITING);
108 return;
109 }
110 /*
111 * Notify the front end that something was pressed, in case
112 * it's depending on finding out (e.g. keypress termination for
113 * Close On Exit).
114 */
115 frontend_keypress(ldisc->frontend);
116
117 /*
118 * Less than zero means null terminated special string.
119 */
120 if (len < 0) {
121 len = strlen(buf);
122 keyflag = KCTRL('@');
123 }
124 /*
125 * Either perform local editing, or just send characters.
126 */
127 if (EDITING) {
128 while (len--) {
129 int c;
130 c = *buf++ + keyflag;
131 if (!interactive && c == '\r')
132 c += KCTRL('@');
133 switch (ldisc->quotenext ? ' ' : c) {
134 /*
135 * ^h/^?: delete one char and output one BSB
136 * ^w: delete, and output BSBs, to return to last
137 * space/nonspace boundary
138 * ^u: delete, and output BSBs, to return to BOL
139 * ^c: Do a ^u then send a telnet IP
140 * ^z: Do a ^u then send a telnet SUSP
141 * ^\: Do a ^u then send a telnet ABORT
142 * ^r: echo "^R\n" and redraw line
143 * ^v: quote next char
144 * ^d: if at BOL, end of file and close connection,
145 * else send line and reset to BOL
146 * ^m: send line-plus-\r\n and reset to BOL
147 */
148 case KCTRL('H'):
149 case KCTRL('?'): /* backspace/delete */
150 if (ldisc->buflen > 0) {
151 if (ECHOING)
152 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
153 ldisc->buflen--;
154 }
155 break;
156 case CTRL('W'): /* delete word */
157 while (ldisc->buflen > 0) {
158 if (ECHOING)
159 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
160 ldisc->buflen--;
161 if (ldisc->buflen > 0 &&
162 isspace(ldisc->buf[ldisc->buflen - 1]) &&
163 !isspace(ldisc->buf[ldisc->buflen]))
164 break;
165 }
166 break;
167 case CTRL('U'): /* delete line */
168 case CTRL('C'): /* Send IP */
169 case CTRL('\\'): /* Quit */
170 case CTRL('Z'): /* Suspend */
171 while (ldisc->buflen > 0) {
172 if (ECHOING)
173 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
174 ldisc->buflen--;
175 }
176 ldisc->back->special(ldisc->backhandle, TS_EL);
177 /*
178 * We don't send IP, SUSP or ABORT if the user has
179 * configured telnet specials off! This breaks
180 * talkers otherwise.
181 */
182 if (!cfg.telnet_keyboard)
183 goto default_case;
184 if (c == CTRL('C'))
185 ldisc->back->special(ldisc->backhandle, TS_IP);
186 if (c == CTRL('Z'))
187 ldisc->back->special(ldisc->backhandle, TS_SUSP);
188 if (c == CTRL('\\'))
189 ldisc->back->special(ldisc->backhandle, TS_ABORT);
190 break;
191 case CTRL('R'): /* redraw line */
192 if (ECHOING) {
193 int i;
194 c_write(ldisc, "^R\r\n", 4);
195 for (i = 0; i < ldisc->buflen; i++)
196 pwrite(ldisc, ldisc->buf[i]);
197 }
198 break;
199 case CTRL('V'): /* quote next char */
200 ldisc->quotenext = TRUE;
201 break;
202 case CTRL('D'): /* logout or send */
203 if (ldisc->buflen == 0) {
204 ldisc->back->special(ldisc->backhandle, TS_EOF);
205 } else {
206 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
207 ldisc->buflen = 0;
208 }
209 break;
210 /*
211 * This particularly hideous bit of code from RDB
212 * allows ordinary ^M^J to do the same thing as
213 * magic-^M when in Raw protocol. The line `case
214 * KCTRL('M'):' is _inside_ the if block. Thus:
215 *
216 * - receiving regular ^M goes straight to the
217 * default clause and inserts as a literal ^M.
218 * - receiving regular ^J _not_ directly after a
219 * literal ^M (or not in Raw protocol) fails the
220 * if condition, leaps to the bottom of the if,
221 * and falls through into the default clause
222 * again.
223 * - receiving regular ^J just after a literal ^M
224 * in Raw protocol passes the if condition,
225 * deletes the literal ^M, and falls through
226 * into the magic-^M code
227 * - receiving a magic-^M empties the line buffer,
228 * signals end-of-line in one of the various
229 * entertaining ways, and _doesn't_ fall out of
230 * the bottom of the if and through to the
231 * default clause because of the break.
232 */
233 case CTRL('J'):
234 if (cfg.protocol == PROT_RAW &&
235 ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
236 if (ECHOING)
237 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
238 ldisc->buflen--;
239 /* FALLTHROUGH */
240 case KCTRL('M'): /* send with newline */
241 if (ldisc->buflen > 0)
242 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
243 if (cfg.protocol == PROT_RAW)
244 ldisc->back->send(ldisc->backhandle, "\r\n", 2);
245 else if (cfg.protocol == PROT_TELNET && cfg.telnet_newline)
246 ldisc->back->special(ldisc->backhandle, TS_EOL);
247 else
248 ldisc->back->send(ldisc->backhandle, "\r", 1);
249 if (ECHOING)
250 c_write(ldisc, "\r\n", 2);
251 ldisc->buflen = 0;
252 break;
253 }
254 /* FALLTHROUGH */
255 default: /* get to this label from ^V handler */
256 default_case:
257 if (ldisc->buflen >= ldisc->bufsiz) {
258 ldisc->bufsiz = ldisc->buflen + 256;
259 ldisc->buf = srealloc(ldisc->buf, ldisc->bufsiz);
260 }
261 ldisc->buf[ldisc->buflen++] = c;
262 if (ECHOING)
263 pwrite(ldisc, (unsigned char) c);
264 ldisc->quotenext = FALSE;
265 break;
266 }
267 }
268 } else {
269 if (ldisc->buflen != 0) {
270 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
271 while (ldisc->buflen > 0) {
272 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
273 ldisc->buflen--;
274 }
275 }
276 if (len > 0) {
277 if (ECHOING)
278 c_write(ldisc, buf, len);
279 if (keyflag && cfg.protocol == PROT_TELNET && len == 1) {
280 switch (buf[0]) {
281 case CTRL('M'):
282 if (cfg.protocol == PROT_TELNET && cfg.telnet_newline)
283 ldisc->back->special(ldisc->backhandle, TS_EOL);
284 else
285 ldisc->back->send(ldisc->backhandle, "\r", 1);
286 break;
287 case CTRL('?'):
288 case CTRL('H'):
289 if (cfg.telnet_keyboard) {
290 ldisc->back->special(ldisc->backhandle, TS_EC);
291 break;
292 }
293 case CTRL('C'):
294 if (cfg.telnet_keyboard) {
295 ldisc->back->special(ldisc->backhandle, TS_IP);
296 break;
297 }
298 case CTRL('Z'):
299 if (cfg.telnet_keyboard) {
300 ldisc->back->special(ldisc->backhandle, TS_SUSP);
301 break;
302 }
303
304 default:
305 ldisc->back->send(ldisc->backhandle, buf, len);
306 break;
307 }
308 } else
309 ldisc->back->send(ldisc->backhandle, buf, len);
310 }
311 }
312 }