]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - ldisc.c
Silly mistake - restore-cursor-pos doesn't make sure the cursor
[PuTTY.git] / 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             back->special (TS_EL);
102             if( c == CTRL('C') )  back->special (TS_IP);
103             if( c == CTRL('Z') )  back->special (TS_SUSP);
104             if( c == CTRL('\\') ) back->special (TS_ABORT);
105             break;
106           case CTRL('R'):              /* redraw line */
107             c_write("^R\r\n", 4);
108             {
109                 int i;
110                 for (i = 0; i < term_buflen; i++)
111                     pwrite(term_buf[i]);
112             }
113             break;
114           case CTRL('V'):              /* quote next char */
115             term_quotenext = TRUE;
116             break;
117           case CTRL('D'):              /* logout or send */
118             if (term_buflen == 0) {
119                 back->special (TS_EOF);
120             } else {
121                 back->send(term_buf, term_buflen);
122                 term_buflen = 0;
123             }
124             break;
125           case CTRL('M'):              /* send with newline */
126             if (term_buflen > 0)
127                 back->send(term_buf, term_buflen);
128             if (cfg.protocol == PROT_RAW)
129                 back->send("\r\n", 2);
130             else
131                 back->send("\r", 1);
132             c_write("\r\n", 2);
133             term_buflen = 0;
134             break;
135           default:                     /* get to this label from ^V handler */
136             if (term_buflen >= term_bufsiz) {
137                 term_bufsiz = term_buflen + 256;
138                 term_buf = saferealloc(term_buf, term_bufsiz);
139             }
140             term_buf[term_buflen++] = c;
141             pwrite(c);
142             term_quotenext = FALSE;
143             break;
144         }
145     }
146 }
147
148 static void simple_send(char *buf, int len) {
149     if( term_buflen != 0 )
150     {
151         back->send(term_buf, term_buflen);
152         while (term_buflen > 0) {
153             bsb(plen(term_buf[term_buflen-1]));
154             term_buflen--;
155         }
156     }
157     if (len > 0)
158         back->send(buf, len);
159 }
160
161 Ldisc ldisc_term = { term_send };
162 Ldisc ldisc_simple = { simple_send };