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 (ldisc->cfg->localecho == LD_YES || \
16 (ldisc->cfg->localecho == LD_BACKEND && \
17 (ldisc->back->ldisc(ldisc->backhandle, LD_ECHO) || \
18 term_ldisc(ldisc->term, LD_ECHO))))
19 #define EDITING (ldisc->cfg->localedit == LD_YES || \
20 (ldisc->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, (char *)&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(Config *mycfg, Terminal *term,
65 Backend *back, void *backhandle,
68 Ldisc ldisc = smalloc(sizeof(*ldisc));
77 ldisc->backhandle = backhandle;
79 ldisc->frontend = frontend;
81 /* Link ourselves into the backend and the terminal */
85 back->provide_ldisc(backhandle, ldisc);
90 void ldisc_send(void *handle, char *buf, int len, int interactive)
92 Ldisc ldisc = (Ldisc) handle;
95 * Called with len=0 when the options change. We must inform
96 * the front end in case it needs to know.
99 ldisc_update(ldisc->frontend, ECHOING, EDITING);
103 * Notify the front end that something was pressed, in case
104 * it's depending on finding out (e.g. keypress termination for
107 frontend_keypress(ldisc->frontend);
110 * Less than zero means null terminated special string.
114 keyflag = KCTRL('@');
117 * Either perform local editing, or just send characters.
122 c = *buf++ + keyflag;
123 if (!interactive && c == '\r')
125 switch (ldisc->quotenext ? ' ' : c) {
127 * ^h/^?: delete one char and output one BSB
128 * ^w: delete, and output BSBs, to return to last
129 * space/nonspace boundary
130 * ^u: delete, and output BSBs, to return to BOL
131 * ^c: Do a ^u then send a telnet IP
132 * ^z: Do a ^u then send a telnet SUSP
133 * ^\: Do a ^u then send a telnet ABORT
134 * ^r: echo "^R\n" and redraw line
135 * ^v: quote next char
136 * ^d: if at BOL, end of file and close connection,
137 * else send line and reset to BOL
138 * ^m: send line-plus-\r\n and reset to BOL
141 case KCTRL('?'): /* backspace/delete */
142 if (ldisc->buflen > 0) {
144 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
148 case CTRL('W'): /* delete word */
149 while (ldisc->buflen > 0) {
151 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
153 if (ldisc->buflen > 0 &&
154 isspace(ldisc->buf[ldisc->buflen - 1]) &&
155 !isspace(ldisc->buf[ldisc->buflen]))
159 case CTRL('U'): /* delete line */
160 case CTRL('C'): /* Send IP */
161 case CTRL('\\'): /* Quit */
162 case CTRL('Z'): /* Suspend */
163 while (ldisc->buflen > 0) {
165 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
168 ldisc->back->special(ldisc->backhandle, TS_EL);
170 * We don't send IP, SUSP or ABORT if the user has
171 * configured telnet specials off! This breaks
174 if (!ldisc->cfg->telnet_keyboard)
177 ldisc->back->special(ldisc->backhandle, TS_IP);
179 ldisc->back->special(ldisc->backhandle, TS_SUSP);
181 ldisc->back->special(ldisc->backhandle, TS_ABORT);
183 case CTRL('R'): /* redraw line */
186 c_write(ldisc, "^R\r\n", 4);
187 for (i = 0; i < ldisc->buflen; i++)
188 pwrite(ldisc, ldisc->buf[i]);
191 case CTRL('V'): /* quote next char */
192 ldisc->quotenext = TRUE;
194 case CTRL('D'): /* logout or send */
195 if (ldisc->buflen == 0) {
196 ldisc->back->special(ldisc->backhandle, TS_EOF);
198 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
203 * This particularly hideous bit of code from RDB
204 * allows ordinary ^M^J to do the same thing as
205 * magic-^M when in Raw protocol. The line `case
206 * KCTRL('M'):' is _inside_ the if block. Thus:
208 * - receiving regular ^M goes straight to the
209 * default clause and inserts as a literal ^M.
210 * - receiving regular ^J _not_ directly after a
211 * literal ^M (or not in Raw protocol) fails the
212 * if condition, leaps to the bottom of the if,
213 * and falls through into the default clause
215 * - receiving regular ^J just after a literal ^M
216 * in Raw protocol passes the if condition,
217 * deletes the literal ^M, and falls through
218 * into the magic-^M code
219 * - receiving a magic-^M empties the line buffer,
220 * signals end-of-line in one of the various
221 * entertaining ways, and _doesn't_ fall out of
222 * the bottom of the if and through to the
223 * default clause because of the break.
226 if (ldisc->cfg->protocol == PROT_RAW &&
227 ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
229 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
232 case KCTRL('M'): /* send with newline */
233 if (ldisc->buflen > 0)
234 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
235 if (ldisc->cfg->protocol == PROT_RAW)
236 ldisc->back->send(ldisc->backhandle, "\r\n", 2);
237 else if (ldisc->cfg->protocol == PROT_TELNET && ldisc->cfg->telnet_newline)
238 ldisc->back->special(ldisc->backhandle, TS_EOL);
240 ldisc->back->send(ldisc->backhandle, "\r", 1);
242 c_write(ldisc, "\r\n", 2);
247 default: /* get to this label from ^V handler */
249 if (ldisc->buflen >= ldisc->bufsiz) {
250 ldisc->bufsiz = ldisc->buflen + 256;
251 ldisc->buf = srealloc(ldisc->buf, ldisc->bufsiz);
253 ldisc->buf[ldisc->buflen++] = c;
255 pwrite(ldisc, (unsigned char) c);
256 ldisc->quotenext = FALSE;
261 if (ldisc->buflen != 0) {
262 ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
263 while (ldisc->buflen > 0) {
264 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
270 c_write(ldisc, buf, len);
271 if (keyflag && ldisc->cfg->protocol == PROT_TELNET && len == 1) {
274 if (ldisc->cfg->protocol == PROT_TELNET && ldisc->cfg->telnet_newline)
275 ldisc->back->special(ldisc->backhandle, TS_EOL);
277 ldisc->back->send(ldisc->backhandle, "\r", 1);
281 if (ldisc->cfg->telnet_keyboard) {
282 ldisc->back->special(ldisc->backhandle, TS_EC);
286 if (ldisc->cfg->telnet_keyboard) {
287 ldisc->back->special(ldisc->backhandle, TS_IP);
291 if (ldisc->cfg->telnet_keyboard) {
292 ldisc->back->special(ldisc->backhandle, TS_SUSP);
297 ldisc->back->send(ldisc->backhandle, buf, len);
301 ldisc->back->send(ldisc->backhandle, buf, len);