8 * ldisc.c: PuTTY line disciplines
11 static void c_write (char *buf
, int len
) {
12 from_backend(0, buf
, len
);
15 static void c_write1 (char c
) {
19 static char *term_buf
= NULL
;
20 static int term_buflen
= 0, term_bufsiz
= 0, term_quotenext
= 0;
22 static int plen(unsigned char c
) {
23 if ((c
>= 32 && c
<= 126) ||
27 return 2; /* ^x for some x */
29 return 4; /* <XY> for hex XY */
32 static void pwrite(unsigned char c
) {
33 if ((c
>= 32 && c
<= 126) ||
38 cc
[1] = (c
== 127 ?
'?' : c
+ 0x40);
43 sprintf(cc
, "<%02X>", c
);
48 static void bsb(int n
) {
50 c_write("\010 \010", 3);
53 #define CTRL(x) (x^'@')
55 static void term_send(char *buf
, int len
) {
59 switch (term_quotenext ?
' ' : c
) {
61 * ^h/^?: delete one char and output one BSB
62 * ^w: delete, and output BSBs, to return to last space/nonspace
64 * ^u: delete, and output BSBs, to return to BOL
65 * ^c: Do a ^u then send a telnet IP
66 * ^z: Do a ^u then send a telnet SUSP
67 * ^\: Do a ^u then send a telnet ABORT
68 * ^r: echo "^R\n" and redraw line
70 * ^d: if at BOL, end of file and close connection, else send line
72 * ^m: send line-plus-\r\n and reset to BOL
74 case CTRL('H'): case CTRL('?'): /* backspace/delete */
75 if (term_buflen
> 0) {
76 bsb(plen(term_buf
[term_buflen
-1]));
80 case CTRL('W'): /* delete word */
81 while (term_buflen
> 0) {
82 bsb(plen(term_buf
[term_buflen
-1]));
84 if (term_buflen
> 0 &&
85 isspace(term_buf
[term_buflen
-1]) &&
86 !isspace(term_buf
[term_buflen
]))
90 case CTRL('U'): /* delete line */
91 case CTRL('C'): /* Send IP */
92 case CTRL('\\'): /* Quit */
93 case CTRL('Z'): /* Suspend */
94 while (term_buflen
> 0) {
95 bsb(plen(term_buf
[term_buflen
-1]));
98 back
->special (TS_EL
);
99 if( c
== CTRL('C') ) back
->special (TS_IP
);
100 if( c
== CTRL('Z') ) back
->special (TS_SUSP
);
101 if( c
== CTRL('\\') ) back
->special (TS_ABORT
);
103 case CTRL('R'): /* redraw line */
104 c_write("^R\r\n", 4);
107 for (i
= 0; i
< term_buflen
; i
++)
111 case CTRL('V'): /* quote next char */
112 term_quotenext
= TRUE
;
114 case CTRL('D'): /* logout or send */
115 if (term_buflen
== 0) {
116 back
->special (TS_EOF
);
118 back
->send(term_buf
, term_buflen
);
122 case CTRL('M'): /* send with newline */
124 back
->send(term_buf
, term_buflen
);
125 if (cfg
.protocol
== PROT_RAW
)
126 back
->send("\r\n", 2);
132 default: /* get to this label from ^V handler */
133 if (term_buflen
>= term_bufsiz
) {
134 term_bufsiz
= term_buflen
+ 256;
135 term_buf
= saferealloc(term_buf
, term_bufsiz
);
137 term_buf
[term_buflen
++] = c
;
139 term_quotenext
= FALSE
;
145 static void simple_send(char *buf
, int len
) {
146 if( term_buflen
!= 0 )
148 back
->send(term_buf
, term_buflen
);
149 while (term_buflen
> 0) {
150 bsb(plen(term_buf
[term_buflen
-1]));
155 back
->send(buf
, len
);
158 Ldisc ldisc_term
= { term_send
};
159 Ldisc ldisc_simple
= { simple_send
};