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.
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))))
21 static void c_write(char *buf, int len)
23 from_backend(0, buf, len);
26 static char *term_buf = NULL;
27 static int term_buflen = 0, term_bufsiz = 0, term_quotenext = 0;
29 static int plen(unsigned char c)
31 if ((c >= 32 && c <= 126) || (c >= 160))
34 return 2; /* ^x for some x */
36 return 4; /* <XY> for hex XY */
39 static void pwrite(unsigned char c)
41 if ((c >= 32 && c <= 126) || (c >= 160)) {
45 cc[1] = (c == 127 ? '?' : c + 0x40);
50 sprintf(cc, "<%02X>", c);
55 static void bsb(int n)
58 c_write("\010 \010", 3);
61 #define CTRL(x) (x^'@')
63 void ldisc_send(char *buf, int len)
66 * Called with len=0 when the options change. We must inform
67 * the front end in case it needs to know.
70 void ldisc_update(int echo, int edit);
71 ldisc_update(ECHOING, EDITING);
74 * Either perform local editing, or just send characters.
80 switch (term_quotenext ? ' ' : c) {
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
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
96 case CTRL('?'): /* backspace/delete */
97 if (term_buflen > 0) {
99 bsb(plen(term_buf[term_buflen - 1]));
103 case CTRL('W'): /* delete word */
104 while (term_buflen > 0) {
106 bsb(plen(term_buf[term_buflen - 1]));
108 if (term_buflen > 0 &&
109 isspace(term_buf[term_buflen - 1]) &&
110 !isspace(term_buf[term_buflen]))
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) {
120 bsb(plen(term_buf[term_buflen - 1]));
123 back->special(TS_EL);
125 back->special(TS_IP);
127 back->special(TS_SUSP);
129 back->special(TS_ABORT);
131 case CTRL('R'): /* redraw line */
134 c_write("^R\r\n", 4);
135 for (i = 0; i < term_buflen; i++)
139 case CTRL('V'): /* quote next char */
140 term_quotenext = TRUE;
142 case CTRL('D'): /* logout or send */
143 if (term_buflen == 0) {
144 back->special(TS_EOF);
146 back->send(term_buf, term_buflen);
150 case CTRL('M'): /* send with newline */
152 back->send(term_buf, term_buflen);
153 if (cfg.protocol == PROT_RAW)
154 back->send("\r\n", 2);
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);
166 term_buf[term_buflen++] = c;
169 term_quotenext = FALSE;
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]));
184 back->send(buf, len);