15 * ldisc.c: PuTTY line disciplines
18 static void c_write (char *buf, int len) {
23 static char *term_buf = NULL;
24 static int term_buflen = 0, term_bufsiz = 0, term_quotenext = 0;
26 static int plen(unsigned char c) {
27 if ((c >= 32 && c <= 126) ||
31 return 2; /* ^x for some x */
33 return 4; /* <XY> for hex XY */
36 static void pwrite(unsigned char c) {
37 if ((c >= 32 && c <= 126) ||
42 cc[1] = (c == 127 ? '?' : c + 0x40);
47 sprintf(cc, "<%02X>", c);
52 static void bsb(int n) {
54 c_write("\010 \010", 3);
57 #define CTRL(x) (x^'@')
59 static void term_send(char *buf, int len) {
63 switch (term_quotenext ? ' ' : c) {
65 * ^h/^?: delete one char and output one BSB
66 * ^w: delete, and output BSBs, to return to last space/nonspace
68 * ^u: delete, and output BSBs, to return to BOL
69 * ^c: Do a ^u then send a telnet IP
70 * ^z: Do a ^u then send a telnet SUSP
71 * ^\: Do a ^u then send a telnet ABORT
72 * ^r: echo "^R\n" and redraw line
74 * ^d: if at BOL, end of file and close connection, else send line
76 * ^m: send line-plus-\r\n and reset to BOL
78 case CTRL('H'): case CTRL('?'): /* backspace/delete */
79 if (term_buflen > 0) {
80 bsb(plen(term_buf[term_buflen-1]));
84 case CTRL('W'): /* delete word */
85 while (term_buflen > 0) {
86 bsb(plen(term_buf[term_buflen-1]));
88 if (term_buflen > 0 &&
89 isspace(term_buf[term_buflen-1]) &&
90 !isspace(term_buf[term_buflen]))
94 case CTRL('U'): /* delete line */
95 case CTRL('C'): /* Send IP */
96 case CTRL('\\'): /* Quit */
97 case CTRL('Z'): /* Suspend */
98 while (term_buflen > 0) {
99 bsb(plen(term_buf[term_buflen-1]));
102 back->special (TS_EL);
103 if( c == CTRL('C') ) back->special (TS_IP);
104 if( c == CTRL('Z') ) back->special (TS_SUSP);
105 if( c == CTRL('\\') ) back->special (TS_ABORT);
107 case CTRL('R'): /* redraw line */
108 c_write("^R\r\n", 4);
111 for (i = 0; i < term_buflen; i++)
115 case CTRL('V'): /* quote next char */
116 term_quotenext = TRUE;
118 case CTRL('D'): /* logout or send */
119 if (term_buflen == 0) {
120 back->special (TS_EOF);
122 back->send(term_buf, term_buflen);
126 case CTRL('M'): /* send with newline */
128 back->send(term_buf, term_buflen);
129 if (cfg.protocol == PROT_RAW)
130 back->send("\r\n", 2);
136 default: /* get to this label from ^V handler */
137 if (term_buflen >= term_bufsiz) {
138 term_bufsiz = term_buflen + 256;
139 term_buf = saferealloc(term_buf, term_bufsiz);
141 term_buf[term_buflen++] = c;
143 term_quotenext = FALSE;
149 static void simple_send(char *buf, int len) {
150 if( term_buflen != 0 )
152 back->send(term_buf, term_buflen);
153 while (term_buflen > 0) {
154 bsb(plen(term_buf[term_buflen-1]));
159 back->send(buf, len);
162 Ldisc ldisc_term = { term_send };
163 Ldisc ldisc_simple = { simple_send };