]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - ldisc.c
Move MODULE files out of individual project directories into a
[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 <windows.h>
9 #include <stdio.h>
10 #include <ctype.h>
11
12 #include "putty.h"
13
14 #define ECHOING (cfg.localecho == LD_YES || \
15                  (cfg.localecho == LD_BACKEND && \
16                       (back->ldisc(LD_ECHO) || term_ldisc(LD_ECHO))))
17 #define EDITING (cfg.localedit == LD_YES || \
18                  (cfg.localedit == LD_BACKEND && \
19                       (back->ldisc(LD_EDIT) || term_ldisc(LD_EDIT))))
20
21 static void c_write(char *buf, int len)
22 {
23     from_backend(0, buf, len);
24 }
25
26 static char *term_buf = NULL;
27 static int term_buflen = 0, term_bufsiz = 0, term_quotenext = 0;
28
29 static int plen(unsigned char c)
30 {
31     if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf))
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(unsigned char c)
40 {
41     if ((c >= 32 && c <= 126) || (c >= 160 && !in_utf)) {
42         c_write(&c, 1);
43     } else if (c < 128) {
44         char cc[2];
45         cc[1] = (c == 127 ? '?' : c + 0x40);
46         cc[0] = '^';
47         c_write(cc, 2);
48     } else {
49         char cc[5];
50         sprintf(cc, "<%02X>", c);
51         c_write(cc, 4);
52     }
53 }
54
55 static void bsb(int n)
56 {
57     while (n--)
58         c_write("\010 \010", 3);
59 }
60
61 #define CTRL(x) (x^'@')
62 #define KCTRL(x) ((x^'@') | 0x100)
63
64 void ldisc_send(char *buf, int len, int interactive)
65 {
66     int keyflag = 0;
67     /*
68      * Called with len=0 when the options change. We must inform
69      * the front end in case it needs to know.
70      */
71     if (len == 0) {
72         void ldisc_update(int echo, int edit);
73         ldisc_update(ECHOING, EDITING);
74     }
75     /*
76      * Less than zero means null terminated special string.
77      */
78     if (len < 0) {
79         len = strlen(buf);
80         keyflag = KCTRL('@');
81     }
82     /*
83      * Either perform local editing, or just send characters.
84      */
85     if (EDITING) {
86         while (len--) {
87             int c;
88             c = *buf++ + keyflag;
89             if (!interactive && c == '\r')
90                 c += KCTRL('@');
91             switch (term_quotenext ? ' ' : c) {
92                 /*
93                  * ^h/^?: delete one char and output one BSB
94                  * ^w: delete, and output BSBs, to return to last
95                  * space/nonspace boundary
96                  * ^u: delete, and output BSBs, to return to BOL
97                  * ^c: Do a ^u then send a telnet IP
98                  * ^z: Do a ^u then send a telnet SUSP
99                  * ^\: Do a ^u then send a telnet ABORT
100                  * ^r: echo "^R\n" and redraw line
101                  * ^v: quote next char
102                  * ^d: if at BOL, end of file and close connection,
103                  * else send line and reset to BOL
104                  * ^m: send line-plus-\r\n and reset to BOL
105                  */
106               case KCTRL('H'):
107               case KCTRL('?'):         /* backspace/delete */
108                 if (term_buflen > 0) {
109                     if (ECHOING)
110                         bsb(plen(term_buf[term_buflen - 1]));
111                     term_buflen--;
112                 }
113                 break;
114               case CTRL('W'):          /* delete word */
115                 while (term_buflen > 0) {
116                     if (ECHOING)
117                         bsb(plen(term_buf[term_buflen - 1]));
118                     term_buflen--;
119                     if (term_buflen > 0 &&
120                         isspace(term_buf[term_buflen - 1]) &&
121                         !isspace(term_buf[term_buflen]))
122                         break;
123                 }
124                 break;
125               case CTRL('U'):          /* delete line */
126               case CTRL('C'):          /* Send IP */
127               case CTRL('\\'):         /* Quit */
128               case CTRL('Z'):          /* Suspend */
129                 while (term_buflen > 0) {
130                     if (ECHOING)
131                         bsb(plen(term_buf[term_buflen - 1]));
132                     term_buflen--;
133                 }
134                 back->special(TS_EL);
135                 /*
136                  * We don't send IP, SUSP or ABORT if the user has
137                  * configured telnet specials off! This breaks
138                  * talkers otherwise.
139                  */
140                 if (!cfg.telnet_keyboard)
141                     goto default_case;
142                 if (c == CTRL('C'))
143                     back->special(TS_IP);
144                 if (c == CTRL('Z'))
145                     back->special(TS_SUSP);
146                 if (c == CTRL('\\'))
147                     back->special(TS_ABORT);
148                 break;
149               case CTRL('R'):          /* redraw line */
150                 if (ECHOING) {
151                     int i;
152                     c_write("^R\r\n", 4);
153                     for (i = 0; i < term_buflen; i++)
154                         pwrite(term_buf[i]);
155                 }
156                 break;
157               case CTRL('V'):          /* quote next char */
158                 term_quotenext = TRUE;
159                 break;
160               case CTRL('D'):          /* logout or send */
161                 if (term_buflen == 0) {
162                     back->special(TS_EOF);
163                 } else {
164                     back->send(term_buf, term_buflen);
165                     term_buflen = 0;
166                 }
167                 break;
168                 /*
169                  * This particularly hideous bit of code from RDB
170                  * allows ordinary ^M^J to do the same thing as
171                  * magic-^M when in Raw protocol. The line `case
172                  * KCTRL('M'):' is _inside_ the if block. Thus:
173                  * 
174                  *  - receiving regular ^M goes straight to the
175                  *    default clause and inserts as a literal ^M.
176                  *  - receiving regular ^J _not_ directly after a
177                  *    literal ^M (or not in Raw protocol) fails the
178                  *    if condition, leaps to the bottom of the if,
179                  *    and falls through into the default clause
180                  *    again.
181                  *  - receiving regular ^J just after a literal ^M
182                  *    in Raw protocol passes the if condition,
183                  *    deletes the literal ^M, and falls through
184                  *    into the magic-^M code
185                  *  - receiving a magic-^M empties the line buffer,
186                  *    signals end-of-line in one of the various
187                  *    entertaining ways, and _doesn't_ fall out of
188                  *    the bottom of the if and through to the
189                  *    default clause because of the break.
190                  */
191               case CTRL('J'):
192                 if (cfg.protocol == PROT_RAW &&
193                     term_buflen > 0 && term_buf[term_buflen - 1] == '\r') {
194                     if (ECHOING)
195                         bsb(plen(term_buf[term_buflen - 1]));
196                     term_buflen--;
197                     /* FALLTHROUGH */
198               case KCTRL('M'):         /* send with newline */
199                     if (term_buflen > 0)
200                         back->send(term_buf, term_buflen);
201                     if (cfg.protocol == PROT_RAW)
202                         back->send("\r\n", 2);
203                     else if (cfg.protocol == PROT_TELNET && cfg.telnet_newline)
204                         back->special(TS_EOL);
205                     else
206                         back->send("\r", 1);
207                     if (ECHOING)
208                         c_write("\r\n", 2);
209                     term_buflen = 0;
210                     break;
211                 }
212                 /* FALLTHROUGH */
213               default:                 /* get to this label from ^V handler */
214                 default_case:
215                 if (term_buflen >= term_bufsiz) {
216                     term_bufsiz = term_buflen + 256;
217                     term_buf = saferealloc(term_buf, term_bufsiz);
218                 }
219                 term_buf[term_buflen++] = c;
220                 if (ECHOING)
221                     pwrite((unsigned char) c);
222                 term_quotenext = FALSE;
223                 break;
224             }
225         }
226     } else {
227         if (term_buflen != 0) {
228             back->send(term_buf, term_buflen);
229             while (term_buflen > 0) {
230                 bsb(plen(term_buf[term_buflen - 1]));
231                 term_buflen--;
232             }
233         }
234         if (len > 0) {
235             if (ECHOING)
236                 c_write(buf, len);
237             if (keyflag && cfg.protocol == PROT_TELNET && len == 1) {
238                 switch (buf[0]) {
239                   case CTRL('M'):
240                     if (cfg.protocol == PROT_TELNET && cfg.telnet_newline)
241                         back->special(TS_EOL);
242                     else
243                         back->send("\r", 1);
244                     break;
245                   case CTRL('?'):
246                   case CTRL('H'):
247                     if (cfg.telnet_keyboard) {
248                         back->special(TS_EC);
249                         break;
250                     }
251                   case CTRL('C'):
252                     if (cfg.telnet_keyboard) {
253                         back->special(TS_IP);
254                         break;
255                     }
256                   case CTRL('Z'):
257                     if (cfg.telnet_keyboard) {
258                         back->special(TS_SUSP);
259                         break;
260                     }
261
262                   default:
263                     back->send(buf, len);
264                     break;
265                 }
266             } else
267                 back->send(buf, len);
268         }
269     }
270 }