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 (cfg.localecho == LD_YES || \
16 (cfg.localecho == LD_BACKEND && \
17 (ldisc->back->ldisc(ldisc->backhandle, LD_ECHO) || \
18 term_ldisc(ldisc->term, LD_ECHO))))
19 #define EDITING (cfg.localedit == LD_YES || \
20 (cfg.localedit == LD_BACKEND && \
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, &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(Terminal *term,
65 Backend *back, void *backhandle,
68 Ldisc ldisc = smalloc(sizeof(*ldisc));
76 ldisc->backhandle = backhandle;
78 ldisc->frontend = frontend;
80 /* Link ourselves into the backend and the terminal */
84 back->provide_ldisc(backhandle, ldisc);
89 void ldisc_send(void *handle, char *buf, int len, int interactive)
91 Ldisc ldisc = (Ldisc) handle;
94 * Called with len=0 when the options change. We must inform
95 * the front end in case it needs to know.
98 ldisc_update(ldisc->frontend, ECHOING, EDITING);
102 * Notify the front end that something was pressed, in case
103 * it's depending on finding out (e.g. keypress termination for
106 frontend_keypress(ldisc->frontend);
109 * Less than zero means null terminated special string.
113 keyflag = KCTRL('@');
116 * Either perform local editing, or just send characters.
121 c = *buf++ + keyflag;
122 if (!interactive && c == '\r')
124 switch (ldisc->quotenext ? ' ' : c) {
126 * ^h/^?: delete one char and output one BSB
127 * ^w: delete, and output BSBs, to return to last
128 * space/nonspace boundary
129 * ^u: delete, and output BSBs, to return to BOL
130 * ^c: Do a ^u then send a telnet IP
131 * ^z: Do a ^u then send a telnet SUSP
132 * ^\: Do a ^u then send a telnet ABORT
133 * ^r: echo "^R\n" and redraw line
134 * ^v: quote next char
135 * ^d: if at BOL, end of file and close connection,
136 * else send line and reset to BOL
137 * ^m: send line-plus-\r\n and reset to BOL
140 case KCTRL('?'): /* backspace/delete */
141 if (ldisc->buflen > 0) {
143 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
147 case CTRL('W'): /* delete word */
148 while (ldisc->buflen > 0) {
150 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
152 if (ldisc->buflen > 0 &&
153 isspace(ldisc->buf[ldisc->buflen - 1]) &&
154 !isspace(ldisc->buf[ldisc->buflen]))
158 case CTRL('U'): /* delete line */
159 case CTRL('C'): /* Send IP */
160 case CTRL('\\'): /* Quit */
161 case CTRL('Z'): /* Suspend */
162 while (ldisc->buflen > 0) {
164 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
167 ldisc->back->special(ldisc->backhandle, TS_EL);
169 * We don't send IP, SUSP or ABORT if the user has
170 * configured telnet specials off! This breaks
173 if (!cfg.telnet_keyboard)
176 ldisc->back->special(ldisc->backhandle, TS_IP);
178 ldisc->back->special(ldisc->backhandle, TS_SUSP);
180 ldisc->back->special(ldisc->backhandle, TS_ABORT);
182 case CTRL('R'): /* redraw line */
185 c_write(ldisc, "^R\r\n", 4);
186 for (i = 0; i < ldisc->buflen; i++)
187 pwrite(ldisc, ldisc->buf[i]);
190 case CTRL('V'): /* quote next char */
191 ldisc->quotenext = TRUE;
193 case CTRL('D'): /* logout or send */
194 if (ldisc->buflen == 0) {
195 ldisc->back->special(ldisc->backhandle, TS_EOF);
197 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
202 * This particularly hideous bit of code from RDB
203 * allows ordinary ^M^J to do the same thing as
204 * magic-^M when in Raw protocol. The line `case
205 * KCTRL('M'):' is _inside_ the if block. Thus:
207 * - receiving regular ^M goes straight to the
208 * default clause and inserts as a literal ^M.
209 * - receiving regular ^J _not_ directly after a
210 * literal ^M (or not in Raw protocol) fails the
211 * if condition, leaps to the bottom of the if,
212 * and falls through into the default clause
214 * - receiving regular ^J just after a literal ^M
215 * in Raw protocol passes the if condition,
216 * deletes the literal ^M, and falls through
217 * into the magic-^M code
218 * - receiving a magic-^M empties the line buffer,
219 * signals end-of-line in one of the various
220 * entertaining ways, and _doesn't_ fall out of
221 * the bottom of the if and through to the
222 * default clause because of the break.
225 if (cfg.protocol == PROT_RAW &&
226 ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
228 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
231 case KCTRL('M'): /* send with newline */
232 if (ldisc->buflen > 0)
233 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
234 if (cfg.protocol == PROT_RAW)
235 ldisc->back->send(ldisc->backhandle, "\r\n", 2);
236 else if (cfg.protocol == PROT_TELNET && cfg.telnet_newline)
237 ldisc->back->special(ldisc->backhandle, TS_EOL);
239 ldisc->back->send(ldisc->backhandle, "\r", 1);
241 c_write(ldisc, "\r\n", 2);
246 default: /* get to this label from ^V handler */
248 if (ldisc->buflen >= ldisc->bufsiz) {
249 ldisc->bufsiz = ldisc->buflen + 256;
250 ldisc->buf = srealloc(ldisc->buf, ldisc->bufsiz);
252 ldisc->buf[ldisc->buflen++] = c;
254 pwrite(ldisc, (unsigned char) c);
255 ldisc->quotenext = FALSE;
260 if (ldisc->buflen != 0) {
261 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
262 while (ldisc->buflen > 0) {
263 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
269 c_write(ldisc, buf, len);
270 if (keyflag && cfg.protocol == PROT_TELNET && len == 1) {
273 if (cfg.protocol == PROT_TELNET && cfg.telnet_newline)
274 ldisc->back->special(ldisc->backhandle, TS_EOL);
276 ldisc->back->send(ldisc->backhandle, "\r", 1);
280 if (cfg.telnet_keyboard) {
281 ldisc->back->special(ldisc->backhandle, TS_EC);
285 if (cfg.telnet_keyboard) {
286 ldisc->back->special(ldisc->backhandle, TS_IP);
290 if (cfg.telnet_keyboard) {
291 ldisc->back->special(ldisc->backhandle, TS_SUSP);
296 ldisc->back->send(ldisc->backhandle, buf, len);
300 ldisc->back->send(ldisc->backhandle, buf, len);