15 * ldisc.c: PuTTY line disciplines
18 static void c_write (char *buf, int len) {
19 from_backend(0, buf, len);
22 static void c_write1 (char c) {
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) {
30 if ((c >= 32 && c <= 126) ||
34 return 2; /* ^x for some x */
36 return 4; /* <XY> for hex XY */
39 static void pwrite(unsigned char c) {
40 if ((c >= 32 && c <= 126) ||
45 cc[1] = (c == 127 ? '?' : c + 0x40);
50 sprintf(cc, "<%02X>", c);
55 static void bsb(int n) {
57 c_write("\010 \010", 3);
60 #define CTRL(x) (x^'@')
62 static void term_send(char *buf, int len) {
66 switch (term_quotenext ? ' ' : c) {
68 * ^h/^?: delete one char and output one BSB
69 * ^w: delete, and output BSBs, to return to last space/nonspace
71 * ^u: delete, and output BSBs, to return to BOL
72 * ^c: Do a ^u then send a telnet IP
73 * ^z: Do a ^u then send a telnet SUSP
74 * ^\: Do a ^u then send a telnet ABORT
75 * ^r: echo "^R\n" and redraw line
77 * ^d: if at BOL, end of file and close connection, else send line
79 * ^m: send line-plus-\r\n and reset to BOL
81 case CTRL('H'): case CTRL('?'): /* backspace/delete */
82 if (term_buflen > 0) {
83 bsb(plen(term_buf[term_buflen-1]));
87 case CTRL('W'): /* delete word */
88 while (term_buflen > 0) {
89 bsb(plen(term_buf[term_buflen-1]));
91 if (term_buflen > 0 &&
92 isspace(term_buf[term_buflen-1]) &&
93 !isspace(term_buf[term_buflen]))
97 case CTRL('U'): /* delete line */
98 case CTRL('C'): /* Send IP */
99 case CTRL('\\'): /* Quit */
100 case CTRL('Z'): /* Suspend */
101 while (term_buflen > 0) {
102 bsb(plen(term_buf[term_buflen-1]));
105 back->special (TS_EL);
106 if( c == CTRL('C') ) back->special (TS_IP);
107 if( c == CTRL('Z') ) back->special (TS_SUSP);
108 if( c == CTRL('\\') ) back->special (TS_ABORT);
110 case CTRL('R'): /* redraw line */
111 c_write("^R\r\n", 4);
114 for (i = 0; i < term_buflen; i++)
118 case CTRL('V'): /* quote next char */
119 term_quotenext = TRUE;
121 case CTRL('D'): /* logout or send */
122 if (term_buflen == 0) {
123 back->special (TS_EOF);
125 back->send(term_buf, term_buflen);
129 case CTRL('M'): /* send with newline */
131 back->send(term_buf, term_buflen);
132 if (cfg.protocol == PROT_RAW)
133 back->send("\r\n", 2);
139 default: /* get to this label from ^V handler */
140 if (term_buflen >= term_bufsiz) {
141 term_bufsiz = term_buflen + 256;
142 term_buf = saferealloc(term_buf, term_bufsiz);
144 term_buf[term_buflen++] = c;
146 term_quotenext = FALSE;
152 static void simple_send(char *buf, int len) {
153 if( term_buflen != 0 )
155 back->send(term_buf, term_buflen);
156 while (term_buflen > 0) {
157 bsb(plen(term_buf[term_buflen-1]));
162 back->send(buf, len);
165 Ldisc ldisc_term = { term_send };
166 Ldisc ldisc_simple = { simple_send };