8 * ldisc.c: PuTTY line disciplines
11 static void c_write (char *buf, int 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;
21 static char *term_buf = NULL;
22 static int term_buflen = 0, term_bufsiz = 0, term_quotenext = 0;
24 static int plen(unsigned char c) {
25 if ((c >= 32 && c <= 126) ||
29 return 2; /* ^x for some x */
31 return 4; /* <XY> for hex XY */
34 static void pwrite(unsigned char c) {
35 if ((c >= 32 && c <= 126) ||
41 cc[1] = (c == 127 ? '?' : c + 0x40);
46 sprintf(cc, "<%02X>", c);
51 static void bsb(int n) {
53 c_write("\010 \010", 3);
56 #define CTRL(x) (x^'@')
58 static void term_send(char *buf, int len) {
62 switch (term_quotenext ? ' ' : c) {
64 * ^h/^?: delete one char and output one BSB
65 * ^w: delete, and output BSBs, to return to last space/nonspace
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
73 * ^d: if at BOL, end of file and close connection, else send line
75 * ^m: send line-plus-\r\n and reset to BOL
77 case CTRL('H'): case CTRL('?'): /* backspace/delete */
78 if (term_buflen > 0) {
79 bsb(plen(term_buf[term_buflen-1]));
83 case CTRL('W'): /* delete word */
84 while (term_buflen > 0) {
85 bsb(plen(term_buf[term_buflen-1]));
87 if (term_buflen > 0 &&
88 isspace(term_buf[term_buflen-1]) &&
89 !isspace(term_buf[term_buflen]))
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]));
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);
106 case CTRL('R'): /* redraw line */
107 c_write("^R\r\n", 4);
110 for (i = 0; i < term_buflen; i++)
114 case CTRL('V'): /* quote next char */
115 term_quotenext = TRUE;
117 case CTRL('D'): /* logout or send */
118 if (term_buflen == 0) {
119 back->special (TS_EOF);
121 back->send(term_buf, term_buflen);
125 case CTRL('M'): /* send with newline */
127 back->send(term_buf, term_buflen);
128 if (cfg.protocol == PROT_RAW)
129 back->send("\r\n", 2);
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);
140 term_buf[term_buflen++] = c;
142 term_quotenext = FALSE;
148 static void simple_send(char *buf, int len) {
149 if( term_buflen != 0 )
151 back->send(term_buf, term_buflen);
152 while (term_buflen > 0) {
153 bsb(plen(term_buf[term_buflen-1]));
158 back->send(buf, len);
161 Ldisc ldisc_term = { term_send };
162 Ldisc ldisc_simple = { simple_send };