8 * ldisc.c: PuTTY line disciplines
11 static void c_write (char *buf
, int len
) {
16 static char *term_buf
= NULL
;
17 static int term_buflen
= 0, term_bufsiz
= 0, term_quotenext
= 0;
19 static int plen(unsigned char c
) {
20 if ((c
>= 32 && c
<= 126) ||
24 return 2; /* ^x for some x */
26 return 4; /* <XY> for hex XY */
29 static void pwrite(unsigned char c
) {
30 if ((c
>= 32 && c
<= 126) ||
35 cc
[1] = (c
== 127 ?
'?' : c
+ 0x40);
40 sprintf(cc
, "<%02X>", c
);
45 static void bsb(int n
) {
47 c_write("\010 \010", 3);
50 #define CTRL(x) (x^'@')
52 static void term_send(char *buf
, int len
) {
56 switch (term_quotenext ?
' ' : c
) {
58 * ^h/^?: delete one char and output one BSB
59 * ^w: delete, and output BSBs, to return to last space/nonspace
61 * ^u: delete, and output BSBs, to return to BOL
62 * ^c: Do a ^u then send a telnet IP
63 * ^z: Do a ^u then send a telnet SUSP
64 * ^\: Do a ^u then send a telnet ABORT
65 * ^r: echo "^R\n" and redraw line
67 * ^d: if at BOL, end of file and close connection, else send line
69 * ^m: send line-plus-\r\n and reset to BOL
71 case CTRL('H'): case CTRL('?'): /* backspace/delete */
72 if (term_buflen
> 0) {
73 bsb(plen(term_buf
[term_buflen
-1]));
77 case CTRL('W'): /* delete word */
78 while (term_buflen
> 0) {
79 bsb(plen(term_buf
[term_buflen
-1]));
81 if (term_buflen
> 0 &&
82 isspace(term_buf
[term_buflen
-1]) &&
83 !isspace(term_buf
[term_buflen
]))
87 case CTRL('U'): /* delete line */
88 case CTRL('C'): /* Send IP */
89 case CTRL('\\'): /* Quit */
90 case CTRL('Z'): /* Suspend */
91 while (term_buflen
> 0) {
92 bsb(plen(term_buf
[term_buflen
-1]));
95 back
->special (TS_EL
);
96 if( c
== CTRL('C') ) back
->special (TS_IP
);
97 if( c
== CTRL('Z') ) back
->special (TS_SUSP
);
98 if( c
== CTRL('\\') ) back
->special (TS_ABORT
);
100 case CTRL('R'): /* redraw line */
101 c_write("^R\r\n", 4);
104 for (i
= 0; i
< term_buflen
; i
++)
108 case CTRL('V'): /* quote next char */
109 term_quotenext
= TRUE
;
111 case CTRL('D'): /* logout or send */
112 if (term_buflen
== 0) {
113 back
->special (TS_EOF
);
115 back
->send(term_buf
, term_buflen
);
119 case CTRL('M'): /* send with newline */
121 back
->send(term_buf
, term_buflen
);
122 if (cfg
.protocol
== PROT_RAW
)
123 back
->send("\r\n", 2);
129 default: /* get to this label from ^V handler */
130 if (term_buflen
>= term_bufsiz
) {
131 term_bufsiz
= term_buflen
+ 256;
132 term_buf
= saferealloc(term_buf
, term_bufsiz
);
134 term_buf
[term_buflen
++] = c
;
136 term_quotenext
= FALSE
;
142 static void simple_send(char *buf
, int len
) {
143 if( term_buflen
!= 0 )
145 back
->send(term_buf
, term_buflen
);
146 while (term_buflen
> 0) {
147 bsb(plen(term_buf
[term_buflen
-1]));
152 back
->send(buf
, len
);
155 Ldisc ldisc_term
= { term_send
};
156 Ldisc ldisc_simple
= { simple_send
};