]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - ldisc.c
Add a Config * argument to ldisc_create(), and use it in place of the global
[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 (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))))
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, (char *)&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(Config *mycfg, 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->cfg = mycfg;
76     ldisc->back = back;
77     ldisc->backhandle = backhandle;
78     ldisc->term = term;
79     ldisc->frontend = frontend;
80
81     /* Link ourselves into the backend and the terminal */
82     if (term)
83         term->ldisc = ldisc;
84     if (back)
85         back->provide_ldisc(backhandle, ldisc);
86
87     return ldisc;
88 }
89
90 void ldisc_send(void *handle, char *buf, int len, int interactive)
91 {
92     Ldisc ldisc = (Ldisc) handle;
93     int keyflag = 0;
94     /*
95      * Called with len=0 when the options change. We must inform
96      * the front end in case it needs to know.
97      */
98     if (len == 0) {
99         ldisc_update(ldisc->frontend, ECHOING, EDITING);
100         return;
101     }
102     /*
103      * Notify the front end that something was pressed, in case
104      * it's depending on finding out (e.g. keypress termination for
105      * Close On Exit). 
106      */
107     frontend_keypress(ldisc->frontend);
108
109     /*
110      * Less than zero means null terminated special string.
111      */
112     if (len < 0) {
113         len = strlen(buf);
114         keyflag = KCTRL('@');
115     }
116     /*
117      * Either perform local editing, or just send characters.
118      */
119     if (EDITING) {
120         while (len--) {
121             int c;
122             c = *buf++ + keyflag;
123             if (!interactive && c == '\r')
124                 c += KCTRL('@');
125             switch (ldisc->quotenext ? ' ' : c) {
126                 /*
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
139                  */
140               case KCTRL('H'):
141               case KCTRL('?'):         /* backspace/delete */
142                 if (ldisc->buflen > 0) {
143                     if (ECHOING)
144                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
145                     ldisc->buflen--;
146                 }
147                 break;
148               case CTRL('W'):          /* delete word */
149                 while (ldisc->buflen > 0) {
150                     if (ECHOING)
151                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
152                     ldisc->buflen--;
153                     if (ldisc->buflen > 0 &&
154                         isspace(ldisc->buf[ldisc->buflen - 1]) &&
155                         !isspace(ldisc->buf[ldisc->buflen]))
156                         break;
157                 }
158                 break;
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) {
164                     if (ECHOING)
165                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
166                     ldisc->buflen--;
167                 }
168                 ldisc->back->special(ldisc->backhandle, TS_EL);
169                 /*
170                  * We don't send IP, SUSP or ABORT if the user has
171                  * configured telnet specials off! This breaks
172                  * talkers otherwise.
173                  */
174                 if (!ldisc->cfg->telnet_keyboard)
175                     goto default_case;
176                 if (c == CTRL('C'))
177                     ldisc->back->special(ldisc->backhandle, TS_IP);
178                 if (c == CTRL('Z'))
179                     ldisc->back->special(ldisc->backhandle, TS_SUSP);
180                 if (c == CTRL('\\'))
181                     ldisc->back->special(ldisc->backhandle, TS_ABORT);
182                 break;
183               case CTRL('R'):          /* redraw line */
184                 if (ECHOING) {
185                     int i;
186                     c_write(ldisc, "^R\r\n", 4);
187                     for (i = 0; i < ldisc->buflen; i++)
188                         pwrite(ldisc, ldisc->buf[i]);
189                 }
190                 break;
191               case CTRL('V'):          /* quote next char */
192                 ldisc->quotenext = TRUE;
193                 break;
194               case CTRL('D'):          /* logout or send */
195                 if (ldisc->buflen == 0) {
196                     ldisc->back->special(ldisc->backhandle, TS_EOF);
197                 } else {
198                     ldisc->back->send(ldisc->backhandle, ldisc->buf, ldisc->buflen);
199                     ldisc->buflen = 0;
200                 }
201                 break;
202                 /*
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:
207                  * 
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
214                  *    again.
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.
224                  */
225               case CTRL('J'):
226                 if (ldisc->cfg->protocol == PROT_RAW &&
227                     ldisc->buflen > 0 && ldisc->buf[ldisc->buflen - 1] == '\r') {
228                     if (ECHOING)
229                         bsb(ldisc, plen(ldisc, ldisc->buf[ldisc->buflen - 1]));
230                     ldisc->buflen--;
231                     /* FALLTHROUGH */
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);
239                     else
240                         ldisc->back->send(ldisc->backhandle, "\r", 1);
241                     if (ECHOING)
242                         c_write(ldisc, "\r\n", 2);
243                     ldisc->buflen = 0;
244                     break;
245                 }
246                 /* FALLTHROUGH */
247               default:                 /* get to this label from ^V handler */
248                 default_case:
249                 if (ldisc->buflen >= ldisc->bufsiz) {
250                     ldisc->bufsiz = ldisc->buflen + 256;
251                     ldisc->buf = srealloc(ldisc->buf, ldisc->bufsiz);
252                 }
253                 ldisc->buf[ldisc->buflen++] = c;
254                 if (ECHOING)
255                     pwrite(ldisc, (unsigned char) c);
256                 ldisc->quotenext = FALSE;
257                 break;
258             }
259         }
260     } else {
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]));
265                 ldisc->buflen--;
266             }
267         }
268         if (len > 0) {
269             if (ECHOING)
270                 c_write(ldisc, buf, len);
271             if (keyflag && ldisc->cfg->protocol == PROT_TELNET && len == 1) {
272                 switch (buf[0]) {
273                   case CTRL('M'):
274                     if (ldisc->cfg->protocol == PROT_TELNET && ldisc->cfg->telnet_newline)
275                         ldisc->back->special(ldisc->backhandle, TS_EOL);
276                     else
277                         ldisc->back->send(ldisc->backhandle, "\r", 1);
278                     break;
279                   case CTRL('?'):
280                   case CTRL('H'):
281                     if (ldisc->cfg->telnet_keyboard) {
282                         ldisc->back->special(ldisc->backhandle, TS_EC);
283                         break;
284                     }
285                   case CTRL('C'):
286                     if (ldisc->cfg->telnet_keyboard) {
287                         ldisc->back->special(ldisc->backhandle, TS_IP);
288                         break;
289                     }
290                   case CTRL('Z'):
291                     if (ldisc->cfg->telnet_keyboard) {
292                         ldisc->back->special(ldisc->backhandle, TS_SUSP);
293                         break;
294                     }
295
296                   default:
297                     ldisc->back->send(ldisc->backhandle, buf, len);
298                     break;
299                 }
300             } else
301                 ldisc->back->send(ldisc->backhandle, buf, len);
302         }
303     }
304 }