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 typedef struct ldisc_tag {
21 int buflen, bufsiz, quotenext;
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))))
33 static void c_write(Ldisc ldisc, char *buf, int len)
35 from_backend(ldisc->term, 0, buf, len);
38 static int plen(Ldisc ldisc, unsigned char c)
40 if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf(ldisc->term)))
43 return 2; /* ^x for some x */
45 return 4; /* <XY> for hex XY */
48 static void pwrite(Ldisc ldisc, unsigned char c)
50 if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf(ldisc->term))) {
51 c_write(ldisc, &c, 1);
54 cc[1] = (c == 127 ? '?' : c + 0x40);
56 c_write(ldisc, cc, 2);
59 sprintf(cc, "<%02X>", c);
60 c_write(ldisc, cc, 4);
64 static void bsb(Ldisc ldisc, int n)
67 c_write(ldisc, "\010 \010", 3);
70 #define CTRL(x) (x^'@')
71 #define KCTRL(x) ((x^'@') | 0x100)
73 void *ldisc_create(Terminal *term,
74 Backend *back, void *backhandle,
77 Ldisc ldisc = smalloc(sizeof(*ldisc));
85 ldisc->backhandle = backhandle;
87 ldisc->frontend = frontend;
89 /* Link ourselves into the backend and the terminal */
93 back->provide_ldisc(backhandle, ldisc);
98 void ldisc_send(void *handle, char *buf, int len, int interactive)
100 Ldisc ldisc = (Ldisc) handle;
103 * Called with len=0 when the options change. We must inform
104 * the front end in case it needs to know.
107 ldisc_update(ldisc->frontend, ECHOING, EDITING);
111 * Notify the front end that something was pressed, in case
112 * it's depending on finding out (e.g. keypress termination for
115 frontend_keypress(ldisc->frontend);
118 * Less than zero means null terminated special string.
122 keyflag = KCTRL('@');
125 * Either perform local editing, or just send characters.
130 c = *buf++ + keyflag;
131 if (!interactive && c == '\r')
133 switch (ldisc->quotenext ? ' ' : c) {
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
149 case KCTRL('?'): /* backspace/delete */
150 if (ldisc->buflen > 0) {
152 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
156 case CTRL('W'): /* delete word */
157 while (ldisc->buflen > 0) {
159 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
161 if (ldisc->buflen > 0 &&
162 isspace(ldisc->buf[ldisc->buflen - 1]) &&
163 !isspace(ldisc->buf[ldisc->buflen]))
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) {
173 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
176 ldisc->back->special(ldisc->backhandle, TS_EL);
178 * We don't send IP, SUSP or ABORT if the user has
179 * configured telnet specials off! This breaks
182 if (!cfg.telnet_keyboard)
185 ldisc->back->special(ldisc->backhandle, TS_IP);
187 ldisc->back->special(ldisc->backhandle, TS_SUSP);
189 ldisc->back->special(ldisc->backhandle, TS_ABORT);
191 case CTRL('R'): /* redraw line */
194 c_write(ldisc, "^R\r\n", 4);
195 for (i = 0; i < ldisc->buflen; i++)
196 pwrite(ldisc, ldisc->buf[i]);
199 case CTRL('V'): /* quote next char */
200 ldisc->quotenext = TRUE;
202 case CTRL('D'): /* logout or send */
203 if (ldisc->buflen == 0) {
204 ldisc->back->special(ldisc->backhandle, TS_EOF);
206 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
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:
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
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.
234 if (cfg.protocol == PROT_RAW &&
235 ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
237 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
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);
248 ldisc->back->send(ldisc->backhandle, "\r", 1);
250 c_write(ldisc, "\r\n", 2);
255 default: /* get to this label from ^V handler */
257 if (ldisc->buflen >= ldisc->bufsiz) {
258 ldisc->bufsiz = ldisc->buflen + 256;
259 ldisc->buf = srealloc(ldisc->buf, ldisc->bufsiz);
261 ldisc->buf[ldisc->buflen++] = c;
263 pwrite(ldisc, (unsigned char) c);
264 ldisc->quotenext = FALSE;
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]));
278 c_write(ldisc, buf, len);
279 if (keyflag && cfg.protocol == PROT_TELNET && len == 1) {
282 if (cfg.protocol == PROT_TELNET && cfg.telnet_newline)
283 ldisc->back->special(ldisc->backhandle, TS_EOL);
285 ldisc->back->send(ldisc->backhandle, "\r", 1);
289 if (cfg.telnet_keyboard) {
290 ldisc->back->special(ldisc->backhandle, TS_EC);
294 if (cfg.telnet_keyboard) {
295 ldisc->back->special(ldisc->backhandle, TS_IP);
299 if (cfg.telnet_keyboard) {
300 ldisc->back->special(ldisc->backhandle, TS_SUSP);
305 ldisc->back->send(ldisc->backhandle, buf, len);
309 ldisc->back->send(ldisc->backhandle, buf, len);