]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - ldisc.c
Robustness fixes for KEXINIT handling and others. In particular, I've
[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
11 #include "putty.h"
12 #include "terminal.h"
13 #include "ldisc.h"
14
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))))
23
24 static void c_write(Ldisc ldisc, char *buf, int len)
25 {
26     from_backend(ldisc->term, 0, buf, len);
27 }
28
29 static int plen(Ldisc ldisc, unsigned char c)
30 {
31     if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf(ldisc->term)))
32         return 1;
33     else if (c < 128)
34         return 2;                      /* ^x for some x */
35     else
36         return 4;                      /* <XY> for hex XY */
37 }
38
39 static void pwrite(Ldisc ldisc, unsigned char c)
40 {
41     if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf(ldisc->term))) {
42         c_write(ldisc, &c, 1);
43     } else if (c < 128) {
44         char cc[2];
45         cc[1] = (c == 127 ? '?' : c + 0x40);
46         cc[0] = '^';
47         c_write(ldisc, cc, 2);
48     } else {
49         char cc[5];
50         sprintf(cc, "<%02X>", c);
51         c_write(ldisc, cc, 4);
52     }
53 }
54
55 static void bsb(Ldisc ldisc, int n)
56 {
57     while (n--)
58         c_write(ldisc, "\010 \010", 3);
59 }
60
61 #define CTRL(x) (x^'@')
62 #define KCTRL(x) ((x^'@') | 0x100)
63
64 void *ldisc_create(Terminal *term,
65                    Backend *back, void *backhandle,
66                    void *frontend)
67 {
68     Ldisc ldisc = smalloc(sizeof(*ldisc));
69
70     ldisc->buf = NULL;
71     ldisc->buflen = 0;
72     ldisc->bufsiz = 0;
73     ldisc->quotenext = 0;
74
75     ldisc->back = back;
76     ldisc->backhandle = backhandle;
77     ldisc->term = term;
78     ldisc->frontend = frontend;
79
80     /* Link ourselves into the backend and the terminal */
81     if (term)
82         term->ldisc = ldisc;
83     if (back)
84         back->provide_ldisc(backhandle, ldisc);
85
86     return ldisc;
87 }
88
89 void ldisc_send(void *handle, char *buf, int len, int interactive)
90 {
91     Ldisc ldisc = (Ldisc) handle;
92     int keyflag = 0;
93     /*
94      * Called with len=0 when the options change. We must inform
95      * the front end in case it needs to know.
96      */
97     if (len == 0) {
98         ldisc_update(ldisc->frontend, ECHOING, EDITING);
99         return;
100     }
101     /*
102      * Notify the front end that something was pressed, in case
103      * it's depending on finding out (e.g. keypress termination for
104      * Close On Exit). 
105      */
106     frontend_keypress(ldisc->frontend);
107
108     /*
109      * Less than zero means null terminated special string.
110      */
111     if (len < 0) {
112         len = strlen(buf);
113         keyflag = KCTRL('@');
114     }
115     /*
116      * Either perform local editing, or just send characters.
117      */
118     if (EDITING) {
119         while (len--) {
120             int c;
121             c = *buf++ + keyflag;
122             if (!interactive && c == '\r')
123                 c += KCTRL('@');
124             switch (ldisc->quotenext ? ' ' : c) {
125                 /*
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
138                  */
139               case KCTRL('H'):
140               case KCTRL('?'):         /* backspace/delete */
141                 if (ldisc->buflen > 0) {
142                     if (ECHOING)
143                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
144                     ldisc->buflen--;
145                 }
146                 break;
147               case CTRL('W'):          /* delete word */
148                 while (ldisc->buflen > 0) {
149                     if (ECHOING)
150                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
151                     ldisc->buflen--;
152                     if (ldisc->buflen > 0 &&
153                         isspace(ldisc->buf[ldisc->buflen - 1]) &&
154                         !isspace(ldisc->buf[ldisc->buflen]))
155                         break;
156                 }
157                 break;
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) {
163                     if (ECHOING)
164                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
165                     ldisc->buflen--;
166                 }
167                 ldisc->back->special(ldisc->backhandle, TS_EL);
168                 /*
169                  * We don't send IP, SUSP or ABORT if the user has
170                  * configured telnet specials off! This breaks
171                  * talkers otherwise.
172                  */
173                 if (!cfg.telnet_keyboard)
174                     goto default_case;
175                 if (c == CTRL('C'))
176                     ldisc->back->special(ldisc->backhandle, TS_IP);
177                 if (c == CTRL('Z'))
178                     ldisc->back->special(ldisc->backhandle, TS_SUSP);
179                 if (c == CTRL('\\'))
180                     ldisc->back->special(ldisc->backhandle, TS_ABORT);
181                 break;
182               case CTRL('R'):          /* redraw line */
183                 if (ECHOING) {
184                     int i;
185                     c_write(ldisc, "^R\r\n", 4);
186                     for (i = 0; i < ldisc->buflen; i++)
187                         pwrite(ldisc, ldisc->buf[i]);
188                 }
189                 break;
190               case CTRL('V'):          /* quote next char */
191                 ldisc->quotenext = TRUE;
192                 break;
193               case CTRL('D'):          /* logout or send */
194                 if (ldisc->buflen == 0) {
195                     ldisc->back->special(ldisc->backhandle, TS_EOF);
196                 } else {
197                     ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
198                     ldisc->buflen = 0;
199                 }
200                 break;
201                 /*
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:
206                  * 
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
213                  *    again.
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.
223                  */
224               case CTRL('J'):
225                 if (cfg.protocol == PROT_RAW &&
226                     ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
227                     if (ECHOING)
228                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
229                     ldisc->buflen--;
230                     /* FALLTHROUGH */
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);
238                     else
239                         ldisc->back->send(ldisc->backhandle, "\r", 1);
240                     if (ECHOING)
241                         c_write(ldisc, "\r\n", 2);
242                     ldisc->buflen = 0;
243                     break;
244                 }
245                 /* FALLTHROUGH */
246               default:                 /* get to this label from ^V handler */
247                 default_case:
248                 if (ldisc->buflen >= ldisc->bufsiz) {
249                     ldisc->bufsiz = ldisc->buflen + 256;
250                     ldisc->buf = srealloc(ldisc->buf, ldisc->bufsiz);
251                 }
252                 ldisc->buf[ldisc->buflen++] = c;
253                 if (ECHOING)
254                     pwrite(ldisc, (unsigned char) c);
255                 ldisc->quotenext = FALSE;
256                 break;
257             }
258         }
259     } else {
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]));
264                 ldisc->buflen--;
265             }
266         }
267         if (len > 0) {
268             if (ECHOING)
269                 c_write(ldisc, buf, len);
270             if (keyflag && cfg.protocol == PROT_TELNET && len == 1) {
271                 switch (buf[0]) {
272                   case CTRL('M'):
273                     if (cfg.protocol == PROT_TELNET && cfg.telnet_newline)
274                         ldisc->back->special(ldisc->backhandle, TS_EOL);
275                     else
276                         ldisc->back->send(ldisc->backhandle, "\r", 1);
277                     break;
278                   case CTRL('?'):
279                   case CTRL('H'):
280                     if (cfg.telnet_keyboard) {
281                         ldisc->back->special(ldisc->backhandle, TS_EC);
282                         break;
283                     }
284                   case CTRL('C'):
285                     if (cfg.telnet_keyboard) {
286                         ldisc->back->special(ldisc->backhandle, TS_IP);
287                         break;
288                     }
289                   case CTRL('Z'):
290                     if (cfg.telnet_keyboard) {
291                         ldisc->back->special(ldisc->backhandle, TS_SUSP);
292                         break;
293                     }
294
295                   default:
296                     ldisc->back->send(ldisc->backhandle, buf, len);
297                     break;
298                 }
299             } else
300                 ldisc->back->send(ldisc->backhandle, buf, len);
301         }
302     }
303 }