]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - ldisc.c
first pass
[PuTTY.git] / 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 #include <assert.h>
11
12 #include "putty.h"
13 #include "terminal.h"
14 #include "ldisc.h"
15
16 #define ECHOING (ldisc->localecho == FORCE_ON || \
17                  (ldisc->localecho == AUTO && \
18                       (ldisc->back->ldisc(ldisc->backhandle, LD_ECHO) || \
19                            term_ldisc(ldisc->term, LD_ECHO))))
20 #define EDITING (ldisc->localedit == FORCE_ON || \
21                  (ldisc->localedit == AUTO && \
22                       (ldisc->back->ldisc(ldisc->backhandle, LD_EDIT) || \
23                            term_ldisc(ldisc->term, LD_EDIT))))
24
25 static void c_write(Ldisc ldisc, const char *buf, int len)
26 {
27     from_backend(ldisc->frontend, 0, buf, len);
28 }
29
30 static int plen(Ldisc ldisc, unsigned char c)
31 {
32     if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf(ldisc->term)))
33         return 1;
34     else if (c < 128)
35         return 2;                      /* ^x for some x */
36     else if (in_utf(ldisc->term) && c >= 0xC0)
37         return 1;                      /* UTF-8 introducer character
38                                         * (FIXME: combining / wide chars) */
39     else if (in_utf(ldisc->term) && c >= 0x80 && c < 0xC0)
40         return 0;                      /* UTF-8 followup character */
41     else
42         return 4;                      /* <XY> hex representation */
43 }
44
45 static void pwrite(Ldisc ldisc, unsigned char c)
46 {
47     if ((c >= 32 && c <= 126) ||
48         (!in_utf(ldisc->term) && c >= 0xA0) ||
49         (in_utf(ldisc->term) && c >= 0x80)) {
50         c_write(ldisc, (char *)&c, 1);
51     } else if (c < 128) {
52         char cc[2];
53         cc[1] = (c == 127 ? '?' : c + 0x40);
54         cc[0] = '^';
55         c_write(ldisc, cc, 2);
56     } else {
57         char cc[5];
58         sprintf(cc, "<%02X>", c);
59         c_write(ldisc, cc, 4);
60     }
61 }
62
63 static int char_start(Ldisc ldisc, unsigned char c)
64 {
65     if (in_utf(ldisc->term))
66         return (c < 0x80 || c >= 0xC0);
67     else
68         return 1;
69 }
70
71 static void bsb(Ldisc ldisc, int n)
72 {
73     while (n--)
74         c_write(ldisc, "\010 \010", 3);
75 }
76
77 #define CTRL(x) (x^'@')
78 #define KCTRL(x) ((x^'@') | 0x100)
79
80 void *ldisc_create(Conf *conf, Terminal *term,
81                    Backend *back, void *backhandle,
82                    void *frontend)
83 {
84     Ldisc ldisc = snew(struct ldisc_tag);
85
86     ldisc->buf = NULL;
87     ldisc->buflen = 0;
88     ldisc->bufsiz = 0;
89     ldisc->quotenext = 0;
90
91     ldisc->back = back;
92     ldisc->backhandle = backhandle;
93     ldisc->term = term;
94     ldisc->frontend = frontend;
95
96     ldisc_configure(ldisc, conf);
97
98     /* Link ourselves into the backend and the terminal */
99     if (term)
100         term->ldisc = ldisc;
101     if (back)
102         back->provide_ldisc(backhandle, ldisc);
103
104     return ldisc;
105 }
106
107 void ldisc_configure(void *handle, Conf *conf)
108 {
109     Ldisc ldisc = (Ldisc) handle;
110
111     ldisc->telnet_keyboard = conf_get_int(conf, CONF_telnet_keyboard);
112     ldisc->telnet_newline = conf_get_int(conf, CONF_telnet_newline);
113     ldisc->protocol = conf_get_int(conf, CONF_protocol);
114     ldisc->localecho = conf_get_int(conf, CONF_localecho);
115     ldisc->localedit = conf_get_int(conf, CONF_localedit);
116 }
117
118 void ldisc_free(void *handle)
119 {
120     Ldisc ldisc = (Ldisc) handle;
121
122     if (ldisc->term)
123         ldisc->term->ldisc = NULL;
124     if (ldisc->back)
125         ldisc->back->provide_ldisc(ldisc->backhandle, NULL);
126     if (ldisc->buf)
127         sfree(ldisc->buf);
128     sfree(ldisc);
129 }
130
131 void ldisc_echoedit_update(void *handle)
132 {
133     Ldisc ldisc = (Ldisc) handle;
134     frontend_echoedit_update(ldisc->frontend, ECHOING, EDITING);
135 }
136
137 void ldisc_send(void *handle, const char *buf, int len, int interactive)
138 {
139     Ldisc ldisc = (Ldisc) handle;
140     int keyflag = 0;
141
142     assert(ldisc->term);
143     assert(len);
144
145     /*
146      * Notify the front end that something was pressed, in case
147      * it's depending on finding out (e.g. keypress termination for
148      * Close On Exit). 
149      */
150     frontend_keypress(ldisc->frontend);
151
152     if (interactive) {
153         /*
154          * Interrupt a paste from the clipboard, if one was in
155          * progress when the user pressed a key. This is easier than
156          * buffering the current piece of data and saving it until the
157          * terminal has finished pasting, and has the potential side
158          * benefit of permitting a user to cancel an accidental huge
159          * paste.
160          */
161         term_nopaste(ldisc->term);
162     }
163
164     /*
165      * Less than zero means null terminated special string.
166      */
167     if (len < 0) {
168         len = strlen(buf);
169         keyflag = KCTRL('@');
170     }
171     /*
172      * Either perform local editing, or just send characters.
173      */
174     if (EDITING) {
175         while (len--) {
176             int c;
177             c = (unsigned char)(*buf++) + keyflag;
178             if (!interactive && c == '\r')
179                 c += KCTRL('@');
180             switch (ldisc->quotenext ? ' ' : c) {
181                 /*
182                  * ^h/^?: delete, and output BSBs, to return to
183                  * last character boundary (in UTF-8 mode this may
184                  * be more than one byte)
185                  * ^w: delete, and output BSBs, to return to last
186                  * space/nonspace boundary
187                  * ^u: delete, and output BSBs, to return to BOL
188                  * ^c: Do a ^u then send a telnet IP
189                  * ^z: Do a ^u then send a telnet SUSP
190                  * ^\: Do a ^u then send a telnet ABORT
191                  * ^r: echo "^R\n" and redraw line
192                  * ^v: quote next char
193                  * ^d: if at BOL, end of file and close connection,
194                  * else send line and reset to BOL
195                  * ^m: send line-plus-\r\n and reset to BOL
196                  */
197               case KCTRL('H'):
198               case KCTRL('?'):         /* backspace/delete */
199                 if (ldisc->buflen > 0) {
200                     do {
201                         if (ECHOING)
202                             bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
203                         ldisc->buflen--;
204                     } while (!char_start(ldisc, ldisc->buf[ldisc->buflen]));
205                 }
206                 break;
207               case CTRL('W'):          /* delete word */
208                 while (ldisc->buflen > 0) {
209                     if (ECHOING)
210                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
211                     ldisc->buflen--;
212                     if (ldisc->buflen > 0 &&
213                         isspace((unsigned char)ldisc->buf[ldisc->buflen-1]) &&
214                         !isspace((unsigned char)ldisc->buf[ldisc->buflen]))
215                         break;
216                 }
217                 break;
218               case CTRL('U'):          /* delete line */
219               case CTRL('C'):          /* Send IP */
220               case CTRL('\\'):         /* Quit */
221               case CTRL('Z'):          /* Suspend */
222                 while (ldisc->buflen > 0) {
223                     if (ECHOING)
224                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
225                     ldisc->buflen--;
226                 }
227                 ldisc->back->special(ldisc->backhandle, TS_EL);
228                 /*
229                  * We don't send IP, SUSP or ABORT if the user has
230                  * configured telnet specials off! This breaks
231                  * talkers otherwise.
232                  */
233                 if (!ldisc->telnet_keyboard)
234                     goto default_case;
235                 if (c == CTRL('C'))
236                     ldisc->back->special(ldisc->backhandle, TS_IP);
237                 if (c == CTRL('Z'))
238                     ldisc->back->special(ldisc->backhandle, TS_SUSP);
239                 if (c == CTRL('\\'))
240                     ldisc->back->special(ldisc->backhandle, TS_ABORT);
241                 break;
242               case CTRL('R'):          /* redraw line */
243                 if (ECHOING) {
244                     int i;
245                     c_write(ldisc, "^R\r\n", 4);
246                     for (i = 0; i < ldisc->buflen; i++)
247                         pwrite(ldisc, ldisc->buf[i]);
248                 }
249                 break;
250               case CTRL('V'):          /* quote next char */
251                 ldisc->quotenext = TRUE;
252                 break;
253               case CTRL('D'):          /* logout or send */
254                 if (ldisc->buflen == 0) {
255                     ldisc->back->special(ldisc->backhandle, TS_EOF);
256                 } else {
257                     ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
258                     ldisc->buflen = 0;
259                 }
260                 break;
261                 /*
262                  * This particularly hideous bit of code from RDB
263                  * allows ordinary ^M^J to do the same thing as
264                  * magic-^M when in Raw protocol. The line `case
265                  * KCTRL('M'):' is _inside_ the if block. Thus:
266                  * 
267                  *  - receiving regular ^M goes straight to the
268                  *    default clause and inserts as a literal ^M.
269                  *  - receiving regular ^J _not_ directly after a
270                  *    literal ^M (or not in Raw protocol) fails the
271                  *    if condition, leaps to the bottom of the if,
272                  *    and falls through into the default clause
273                  *    again.
274                  *  - receiving regular ^J just after a literal ^M
275                  *    in Raw protocol passes the if condition,
276                  *    deletes the literal ^M, and falls through
277                  *    into the magic-^M code
278                  *  - receiving a magic-^M empties the line buffer,
279                  *    signals end-of-line in one of the various
280                  *    entertaining ways, and _doesn't_ fall out of
281                  *    the bottom of the if and through to the
282                  *    default clause because of the break.
283                  */
284               case CTRL('J'):
285                 if (ldisc->protocol == PROT_RAW &&
286                     ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
287                     if (ECHOING)
288                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
289                     ldisc->buflen--;
290                     /* FALLTHROUGH */
291               case KCTRL('M'):         /* send with newline */
292                     if (ldisc->buflen > 0)
293                         ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
294                     if (ldisc->protocol == PROT_RAW)
295                         ldisc->back->send(ldisc->backhandle, "\r\n", 2);
296                     else if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline)
297                         ldisc->back->special(ldisc->backhandle, TS_EOL);
298                     else
299                         ldisc->back->send(ldisc->backhandle, "\r", 1);
300                     if (ECHOING)
301                         c_write(ldisc, "\r\n", 2);
302                     ldisc->buflen = 0;
303                     break;
304                 }
305                 /* FALLTHROUGH */
306               default:                 /* get to this label from ^V handler */
307                 default_case:
308                 if (ldisc->buflen >= ldisc->bufsiz) {
309                     ldisc->bufsiz = ldisc->buflen + 256;
310                     ldisc->buf = sresize(ldisc->buf, ldisc->bufsiz, char);
311                 }
312                 ldisc->buf[ldisc->buflen++] = c;
313                 if (ECHOING)
314                     pwrite(ldisc, (unsigned char) c);
315                 ldisc->quotenext = FALSE;
316                 break;
317             }
318         }
319     } else {
320         if (ldisc->buflen != 0) {
321             ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
322             while (ldisc->buflen > 0) {
323                 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
324                 ldisc->buflen--;
325             }
326         }
327         if (len > 0) {
328             if (ECHOING)
329                 c_write(ldisc, buf, len);
330             if (keyflag && ldisc->protocol == PROT_TELNET && len == 1) {
331                 switch (buf[0]) {
332                   case CTRL('M'):
333                     if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline)
334                         ldisc->back->special(ldisc->backhandle, TS_EOL);
335                     else
336                         ldisc->back->send(ldisc->backhandle, "\r", 1);
337                     break;
338                   case CTRL('?'):
339                   case CTRL('H'):
340                     if (ldisc->telnet_keyboard) {
341                         ldisc->back->special(ldisc->backhandle, TS_EC);
342                         break;
343                     }
344                   case CTRL('C'):
345                     if (ldisc->telnet_keyboard) {
346                         ldisc->back->special(ldisc->backhandle, TS_IP);
347                         break;
348                     }
349                   case CTRL('Z'):
350                     if (ldisc->telnet_keyboard) {
351                         ldisc->back->special(ldisc->backhandle, TS_SUSP);
352                         break;
353                     }
354
355                   default:
356                     ldisc->back->send(ldisc->backhandle, buf, len);
357                     break;
358                 }
359             } else
360                 ldisc->back->send(ldisc->backhandle, buf, len);
361         }
362     }
363 }