`realhost', passed back from all the backend init functions, was
[u/mdw/putty] / ldisc.c
CommitLineData
0965bee0 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
5bc238bb 8#include <windows.h>
9#include <stdio.h>
1d470ad2 10#include <ctype.h>
5bc238bb 11
12#include "putty.h"
13
0965bee0 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))))
5bc238bb 20
32874aea 21static void c_write(char *buf, int len)
22{
fe50e814 23 from_backend(0, buf, len);
24}
25
5bc238bb 26static char *term_buf = NULL;
27static int term_buflen = 0, term_bufsiz = 0, term_quotenext = 0;
28
32874aea 29static int plen(unsigned char c)
30{
31 if ((c >= 32 && c <= 126) || (c >= 160))
32 return 1;
5bc238bb 33 else if (c < 128)
32874aea 34 return 2; /* ^x for some x */
5bc238bb 35 else
32874aea 36 return 4; /* <XY> for hex XY */
5bc238bb 37}
38
32874aea 39static void pwrite(unsigned char c)
40{
41 if ((c >= 32 && c <= 126) || (c >= 160)) {
42 c_write(&c, 1);
5bc238bb 43 } else if (c < 128) {
32874aea 44 char cc[2];
45 cc[1] = (c == 127 ? '?' : c + 0x40);
46 cc[0] = '^';
47 c_write(cc, 2);
5bc238bb 48 } else {
32874aea 49 char cc[5];
50 sprintf(cc, "<%02X>", c);
51 c_write(cc, 4);
5bc238bb 52 }
53}
54
32874aea 55static void bsb(int n)
56{
5bc238bb 57 while (n--)
58 c_write("\010 \010", 3);
59}
60
5d1d4279 61#define CTRL(x) (x^'@')
62
32874aea 63void ldisc_send(char *buf, int len)
64{
0965bee0 65 /*
66 * Called with len=0 when the options change. We must inform
67 * the front end in case it needs to know.
68 */
69 if (len == 0) {
32874aea 70 void ldisc_update(int echo, int edit);
71 ldisc_update(ECHOING, EDITING);
5bc238bb 72 }
0965bee0 73 /*
74 * Either perform local editing, or just send characters.
75 */
76 if (EDITING) {
32874aea 77 while (len--) {
78 char c;
79 c = *buf++;
80 switch (term_quotenext ? ' ' : c) {
81 /*
82 * ^h/^?: delete one char and output one BSB
83 * ^w: delete, and output BSBs, to return to last
84 * space/nonspace boundary
85 * ^u: delete, and output BSBs, to return to BOL
86 * ^c: Do a ^u then send a telnet IP
87 * ^z: Do a ^u then send a telnet SUSP
88 * ^\: Do a ^u then send a telnet ABORT
89 * ^r: echo "^R\n" and redraw line
90 * ^v: quote next char
91 * ^d: if at BOL, end of file and close connection,
92 * else send line and reset to BOL
93 * ^m: send line-plus-\r\n and reset to BOL
94 */
95 case CTRL('H'):
96 case CTRL('?'): /* backspace/delete */
97 if (term_buflen > 0) {
98 if (ECHOING)
99 bsb(plen(term_buf[term_buflen - 1]));
100 term_buflen--;
101 }
102 break;
103 case CTRL('W'): /* delete word */
104 while (term_buflen > 0) {
105 if (ECHOING)
106 bsb(plen(term_buf[term_buflen - 1]));
107 term_buflen--;
108 if (term_buflen > 0 &&
109 isspace(term_buf[term_buflen - 1]) &&
110 !isspace(term_buf[term_buflen]))
111 break;
112 }
113 break;
114 case CTRL('U'): /* delete line */
115 case CTRL('C'): /* Send IP */
116 case CTRL('\\'): /* Quit */
117 case CTRL('Z'): /* Suspend */
118 while (term_buflen > 0) {
119 if (ECHOING)
120 bsb(plen(term_buf[term_buflen - 1]));
121 term_buflen--;
122 }
123 back->special(TS_EL);
124 if (c == CTRL('C'))
125 back->special(TS_IP);
126 if (c == CTRL('Z'))
127 back->special(TS_SUSP);
128 if (c == CTRL('\\'))
129 back->special(TS_ABORT);
130 break;
131 case CTRL('R'): /* redraw line */
132 if (ECHOING) {
133 int i;
134 c_write("^R\r\n", 4);
135 for (i = 0; i < term_buflen; i++)
136 pwrite(term_buf[i]);
137 }
138 break;
139 case CTRL('V'): /* quote next char */
140 term_quotenext = TRUE;
141 break;
142 case CTRL('D'): /* logout or send */
143 if (term_buflen == 0) {
144 back->special(TS_EOF);
145 } else {
146 back->send(term_buf, term_buflen);
147 term_buflen = 0;
148 }
149 break;
150 case CTRL('M'): /* send with newline */
151 if (term_buflen > 0)
152 back->send(term_buf, term_buflen);
153 if (cfg.protocol == PROT_RAW)
154 back->send("\r\n", 2);
155 else
156 back->send("\r", 1);
157 if (ECHOING)
158 c_write("\r\n", 2);
159 term_buflen = 0;
160 break;
161 default: /* get to this label from ^V handler */
162 if (term_buflen >= term_bufsiz) {
163 term_bufsiz = term_buflen + 256;
164 term_buf = saferealloc(term_buf, term_bufsiz);
165 }
166 term_buf[term_buflen++] = c;
167 if (ECHOING)
168 pwrite(c);
169 term_quotenext = FALSE;
170 break;
171 }
172 }
0965bee0 173 } else {
32874aea 174 if (term_buflen != 0) {
175 back->send(term_buf, term_buflen);
176 while (term_buflen > 0) {
177 bsb(plen(term_buf[term_buflen - 1]));
178 term_buflen--;
179 }
180 }
181 if (len > 0) {
182 if (ECHOING)
183 c_write(buf, len);
184 back->send(buf, len);
185 }
5d1d4279 186 }
5bc238bb 187}