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 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);
105 case CTRL('R'): /* redraw line */
106 c_write("^R\r\n", 4);
109 for (i = 0; i < term_buflen; i++)
113 case CTRL('V'): /* quote next char */
114 term_quotenext = TRUE;
116 case CTRL('D'): /* logout or send */
117 if (term_buflen == 0) {
118 back->special (TS_EOF);
120 back->send(term_buf, term_buflen);
124 case CTRL('M'): /* send with newline */
125 back->send(term_buf, term_buflen);
126 back->send("\r\n", 2);
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);
135 term_buf[term_buflen++] = c;
137 term_quotenext = FALSE;
143 static void simple_send(char *buf, int len) {
144 if( term_buflen != 0 )
146 back->send(term_buf, term_buflen);
147 while (term_buflen > 0) {
148 bsb(plen(term_buf[term_buflen-1]));
152 back->send(buf, len);
155 Ldisc ldisc_term = { term_send };
156 Ldisc ldisc_simple = { simple_send };