]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - ldisc.c
Clarify when ldisc->term may be NULL.
[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, 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_send(void *handle, char *buf, int len, int interactive)
132 {
133     Ldisc ldisc = (Ldisc) handle;
134     int keyflag = 0;
135     /*
136      * Called with len=0 when the options change. We must inform
137      * the front end in case it needs to know.
138      */
139     if (len == 0) {
140         ldisc_update(ldisc->frontend, ECHOING, EDITING);
141         return;
142     }
143
144     /*
145      * If that wasn't true, then we expect ldisc->term to be non-NULL
146      * hereafter. (The only front ends which have an ldisc but no term
147      * are those which do networking but no terminal emulation, in
148      * which case they need the above if statement to handle
149      * ldisc_updates passed from the back ends, but should never send
150      * any actual input through this function.)
151      */
152     assert(ldisc->term);
153
154     /*
155      * Notify the front end that something was pressed, in case
156      * it's depending on finding out (e.g. keypress termination for
157      * Close On Exit). 
158      */
159     frontend_keypress(ldisc->frontend);
160
161     if (interactive) {
162         /*
163          * Interrupt a paste from the clipboard, if one was in
164          * progress when the user pressed a key. This is easier than
165          * buffering the current piece of data and saving it until the
166          * terminal has finished pasting, and has the potential side
167          * benefit of permitting a user to cancel an accidental huge
168          * paste.
169          */
170         term_nopaste(ldisc->term);
171     }
172
173     /*
174      * Less than zero means null terminated special string.
175      */
176     if (len < 0) {
177         len = strlen(buf);
178         keyflag = KCTRL('@');
179     }
180     /*
181      * Either perform local editing, or just send characters.
182      */
183     if (EDITING) {
184         while (len--) {
185             int c;
186             c = (unsigned char)(*buf++) + keyflag;
187             if (!interactive && c == '\r')
188                 c += KCTRL('@');
189             switch (ldisc->quotenext ? ' ' : c) {
190                 /*
191                  * ^h/^?: delete, and output BSBs, to return to
192                  * last character boundary (in UTF-8 mode this may
193                  * be more than one byte)
194                  * ^w: delete, and output BSBs, to return to last
195                  * space/nonspace boundary
196                  * ^u: delete, and output BSBs, to return to BOL
197                  * ^c: Do a ^u then send a telnet IP
198                  * ^z: Do a ^u then send a telnet SUSP
199                  * ^\: Do a ^u then send a telnet ABORT
200                  * ^r: echo "^R\n" and redraw line
201                  * ^v: quote next char
202                  * ^d: if at BOL, end of file and close connection,
203                  * else send line and reset to BOL
204                  * ^m: send line-plus-\r\n and reset to BOL
205                  */
206               case KCTRL('H'):
207               case KCTRL('?'):         /* backspace/delete */
208                 if (ldisc->buflen > 0) {
209                     do {
210                         if (ECHOING)
211                             bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
212                         ldisc->buflen--;
213                     } while (!char_start(ldisc, ldisc->buf[ldisc->buflen]));
214                 }
215                 break;
216               case CTRL('W'):          /* delete word */
217                 while (ldisc->buflen > 0) {
218                     if (ECHOING)
219                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
220                     ldisc->buflen--;
221                     if (ldisc->buflen > 0 &&
222                         isspace((unsigned char)ldisc->buf[ldisc->buflen-1]) &&
223                         !isspace((unsigned char)ldisc->buf[ldisc->buflen]))
224                         break;
225                 }
226                 break;
227               case CTRL('U'):          /* delete line */
228               case CTRL('C'):          /* Send IP */
229               case CTRL('\\'):         /* Quit */
230               case CTRL('Z'):          /* Suspend */
231                 while (ldisc->buflen > 0) {
232                     if (ECHOING)
233                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
234                     ldisc->buflen--;
235                 }
236                 ldisc->back->special(ldisc->backhandle, TS_EL);
237                 /*
238                  * We don't send IP, SUSP or ABORT if the user has
239                  * configured telnet specials off! This breaks
240                  * talkers otherwise.
241                  */
242                 if (!ldisc->telnet_keyboard)
243                     goto default_case;
244                 if (c == CTRL('C'))
245                     ldisc->back->special(ldisc->backhandle, TS_IP);
246                 if (c == CTRL('Z'))
247                     ldisc->back->special(ldisc->backhandle, TS_SUSP);
248                 if (c == CTRL('\\'))
249                     ldisc->back->special(ldisc->backhandle, TS_ABORT);
250                 break;
251               case CTRL('R'):          /* redraw line */
252                 if (ECHOING) {
253                     int i;
254                     c_write(ldisc, "^R\r\n", 4);
255                     for (i = 0; i < ldisc->buflen; i++)
256                         pwrite(ldisc, ldisc->buf[i]);
257                 }
258                 break;
259               case CTRL('V'):          /* quote next char */
260                 ldisc->quotenext = TRUE;
261                 break;
262               case CTRL('D'):          /* logout or send */
263                 if (ldisc->buflen == 0) {
264                     ldisc->back->special(ldisc->backhandle, TS_EOF);
265                 } else {
266                     ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
267                     ldisc->buflen = 0;
268                 }
269                 break;
270                 /*
271                  * This particularly hideous bit of code from RDB
272                  * allows ordinary ^M^J to do the same thing as
273                  * magic-^M when in Raw protocol. The line `case
274                  * KCTRL('M'):' is _inside_ the if block. Thus:
275                  * 
276                  *  - receiving regular ^M goes straight to the
277                  *    default clause and inserts as a literal ^M.
278                  *  - receiving regular ^J _not_ directly after a
279                  *    literal ^M (or not in Raw protocol) fails the
280                  *    if condition, leaps to the bottom of the if,
281                  *    and falls through into the default clause
282                  *    again.
283                  *  - receiving regular ^J just after a literal ^M
284                  *    in Raw protocol passes the if condition,
285                  *    deletes the literal ^M, and falls through
286                  *    into the magic-^M code
287                  *  - receiving a magic-^M empties the line buffer,
288                  *    signals end-of-line in one of the various
289                  *    entertaining ways, and _doesn't_ fall out of
290                  *    the bottom of the if and through to the
291                  *    default clause because of the break.
292                  */
293               case CTRL('J'):
294                 if (ldisc->protocol == PROT_RAW &&
295                     ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
296                     if (ECHOING)
297                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
298                     ldisc->buflen--;
299                     /* FALLTHROUGH */
300               case KCTRL('M'):         /* send with newline */
301                     if (ldisc->buflen > 0)
302                         ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
303                     if (ldisc->protocol == PROT_RAW)
304                         ldisc->back->send(ldisc->backhandle, "\r\n", 2);
305                     else if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline)
306                         ldisc->back->special(ldisc->backhandle, TS_EOL);
307                     else
308                         ldisc->back->send(ldisc->backhandle, "\r", 1);
309                     if (ECHOING)
310                         c_write(ldisc, "\r\n", 2);
311                     ldisc->buflen = 0;
312                     break;
313                 }
314                 /* FALLTHROUGH */
315               default:                 /* get to this label from ^V handler */
316                 default_case:
317                 if (ldisc->buflen >= ldisc->bufsiz) {
318                     ldisc->bufsiz = ldisc->buflen + 256;
319                     ldisc->buf = sresize(ldisc->buf, ldisc->bufsiz, char);
320                 }
321                 ldisc->buf[ldisc->buflen++] = c;
322                 if (ECHOING)
323                     pwrite(ldisc, (unsigned char) c);
324                 ldisc->quotenext = FALSE;
325                 break;
326             }
327         }
328     } else {
329         if (ldisc->buflen != 0) {
330             ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
331             while (ldisc->buflen > 0) {
332                 bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
333                 ldisc->buflen--;
334             }
335         }
336         if (len > 0) {
337             if (ECHOING)
338                 c_write(ldisc, buf, len);
339             if (keyflag && ldisc->protocol == PROT_TELNET && len == 1) {
340                 switch (buf[0]) {
341                   case CTRL('M'):
342                     if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline)
343                         ldisc->back->special(ldisc->backhandle, TS_EOL);
344                     else
345                         ldisc->back->send(ldisc->backhandle, "\r", 1);
346                     break;
347                   case CTRL('?'):
348                   case CTRL('H'):
349                     if (ldisc->telnet_keyboard) {
350                         ldisc->back->special(ldisc->backhandle, TS_EC);
351                         break;
352                     }
353                   case CTRL('C'):
354                     if (ldisc->telnet_keyboard) {
355                         ldisc->back->special(ldisc->backhandle, TS_IP);
356                         break;
357                     }
358                   case CTRL('Z'):
359                     if (ldisc->telnet_keyboard) {
360                         ldisc->back->special(ldisc->backhandle, TS_SUSP);
361                         break;
362                     }
363
364                   default:
365                     ldisc->back->send(ldisc->backhandle, buf, len);
366                     break;
367                 }
368             } else
369                 ldisc->back->send(ldisc->backhandle, buf, len);
370         }
371     }
372 }