Robert de Bath's patch: ^C ^Z and ^D send Telnet specials in terminal line
[u/mdw/putty] / ldisc.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <ctype.h>
4
5 #include "putty.h"
6
7 /*
8 * ldisc.c: PuTTY line disciplines
9 */
10
11 static void c_write (char *buf, int len) {
12 while (len--) {
13 int new_head = (inbuf_head + 1) & INBUF_MASK;
14 if (new_head != inbuf_reap) {
15 inbuf[inbuf_head] = *buf++;
16 inbuf_head = new_head;
17 }
18 }
19 }
20
21 static char *term_buf = NULL;
22 static int term_buflen = 0, term_bufsiz = 0, term_quotenext = 0;
23
24 static int plen(unsigned char c) {
25 if ((c >= 32 && c <= 126) ||
26 (c >= 160))
27 return 1;
28 else if (c < 128)
29 return 2; /* ^x for some x */
30 else
31 return 4; /* <XY> for hex XY */
32 }
33
34 static void pwrite(unsigned char c) {
35 if ((c >= 32 && c <= 126) ||
36 (c >= 160)) {
37 char cc = (char)c;
38 c_write(&cc, 1);
39 } else if (c < 128) {
40 char cc[2];
41 cc[1] = (c == 127 ? '?' : c + 0x40);
42 cc[0] = '^';
43 c_write(cc, 2);
44 } else {
45 char cc[5];
46 sprintf(cc, "<%02X>", c);
47 c_write(cc, 4);
48 }
49 }
50
51 static void bsb(int n) {
52 while (n--)
53 c_write("\010 \010", 3);
54 }
55
56 #define CTRL(x) (x^'@')
57
58 static void term_send(char *buf, int len) {
59 while (len--) {
60 char c;
61 c = *buf++;
62 switch (term_quotenext ? ' ' : c) {
63 /*
64 * ^h/^?: delete one char and output one BSB
65 * ^w: delete, and output BSBs, to return to last space/nonspace
66 * boundary
67 * ^u: delete, and output BSBs, to return to BOL
68 * ^c: Do a ^u then send a telnet IP
69 * ^z: Do a ^u then send a telnet SUSP
70 * ^\: Do a ^u then send a telnet ABORT
71 * ^r: echo "^R\n" and redraw line
72 * ^v: quote next char
73 * ^d: if at BOL, end of file and close connection, else send line
74 * and reset to BOL
75 * ^m: send line-plus-\r\n and reset to BOL
76 */
77 case CTRL('H'): case CTRL('?'): /* backspace/delete */
78 if (term_buflen > 0) {
79 bsb(plen(term_buf[term_buflen-1]));
80 term_buflen--;
81 }
82 break;
83 case CTRL('W'): /* delete word */
84 while (term_buflen > 0) {
85 bsb(plen(term_buf[term_buflen-1]));
86 term_buflen--;
87 if (term_buflen > 0 &&
88 isspace(term_buf[term_buflen-1]) &&
89 !isspace(term_buf[term_buflen]))
90 break;
91 }
92 break;
93 case CTRL('U'): /* delete line */
94 case CTRL('C'): /* Send IP */
95 case CTRL('\\'): /* Quit */
96 case CTRL('Z'): /* Suspend */
97 while (term_buflen > 0) {
98 bsb(plen(term_buf[term_buflen-1]));
99 term_buflen--;
100 }
101 if( c == CTRL('C') ) back->special (TS_IP);
102 if( c == CTRL('Z') ) back->special (TS_SUSP);
103 if( c == CTRL('\\') ) back->special (TS_ABORT);
104 break;
105 case CTRL('R'): /* redraw line */
106 c_write("^R\r\n", 4);
107 {
108 int i;
109 for (i = 0; i < term_buflen; i++)
110 pwrite(term_buf[i]);
111 }
112 break;
113 case CTRL('V'): /* quote next char */
114 term_quotenext = TRUE;
115 break;
116 case CTRL('D'): /* logout or send */
117 if (term_buflen == 0) {
118 back->special (TS_EOF);
119 } else {
120 back->send(term_buf, term_buflen);
121 term_buflen = 0;
122 }
123 break;
124 case CTRL('M'): /* send with newline */
125 back->send(term_buf, term_buflen);
126 back->send("\r\n", 2);
127 c_write("\r\n", 2);
128 term_buflen = 0;
129 break;
130 default: /* get to this label from ^V handler */
131 if (term_buflen >= term_bufsiz) {
132 term_bufsiz = term_buflen + 256;
133 term_buf = saferealloc(term_buf, term_bufsiz);
134 }
135 term_buf[term_buflen++] = c;
136 pwrite(c);
137 term_quotenext = FALSE;
138 break;
139 }
140 }
141 }
142
143 static void simple_send(char *buf, int len) {
144 if( term_buflen != 0 )
145 {
146 back->send(term_buf, term_buflen);
147 while (term_buflen > 0) {
148 bsb(plen(term_buf[term_buflen-1]));
149 term_buflen--;
150 }
151 }
152 back->send(buf, len);
153 }
154
155 Ldisc ldisc_term = { term_send };
156 Ldisc ldisc_simple = { simple_send };