2 * ldisc.c: PuTTY line discipline. Sits between the input coming
3 * from keypresses in the window, and the output channel leading to
4 * the back end. Implements echo and/or local line editing,
5 * depending on what's currently configured.
14 #define ECHOING (cfg.localecho == LD_YES || \
15 (cfg.localecho == LD_BACKEND && \
16 (back->ldisc(LD_ECHO) || term_ldisc(LD_ECHO))))
17 #define EDITING (cfg.localedit == LD_YES || \
18 (cfg.localedit == LD_BACKEND && \
19 (back->ldisc(LD_EDIT) || term_ldisc(LD_EDIT))))
21 static void c_write (char *buf
, int len
) {
22 from_backend(0, buf
, len
);
25 static char *term_buf
= NULL
;
26 static int term_buflen
= 0, term_bufsiz
= 0, term_quotenext
= 0;
28 static int plen(unsigned char c
) {
29 if ((c
>= 32 && c
<= 126) ||
33 return 2; /* ^x for some x */
35 return 4; /* <XY> for hex XY */
38 static void pwrite(unsigned char c
) {
39 if ((c
>= 32 && c
<= 126) ||
44 cc
[1] = (c
== 127 ?
'?' : c
+ 0x40);
49 sprintf(cc
, "<%02X>", c
);
54 static void bsb(int n
) {
56 c_write("\010 \010", 3);
59 #define CTRL(x) (x^'@')
61 void ldisc_send(char *buf
, int len
) {
63 * Called with len=0 when the options change. We must inform
64 * the front end in case it needs to know.
67 void ldisc_update(int echo
, int edit
);
68 ldisc_update(ECHOING
, EDITING
);
71 * Either perform local editing, or just send characters.
77 switch (term_quotenext ?
' ' : c
) {
79 * ^h/^?: delete one char and output one BSB
80 * ^w: delete, and output BSBs, to return to last
81 * space/nonspace boundary
82 * ^u: delete, and output BSBs, to return to BOL
83 * ^c: Do a ^u then send a telnet IP
84 * ^z: Do a ^u then send a telnet SUSP
85 * ^\: Do a ^u then send a telnet ABORT
86 * ^r: echo "^R\n" and redraw line
88 * ^d: if at BOL, end of file and close connection,
89 * else send line and reset to BOL
90 * ^m: send line-plus-\r\n and reset to BOL
92 case CTRL('H'): case CTRL('?'): /* backspace/delete */
93 if (term_buflen
> 0) {
95 bsb(plen(term_buf
[term_buflen
-1]));
99 case CTRL('W'): /* delete word */
100 while (term_buflen
> 0) {
102 bsb(plen(term_buf
[term_buflen
-1]));
104 if (term_buflen
> 0 &&
105 isspace(term_buf
[term_buflen
-1]) &&
106 !isspace(term_buf
[term_buflen
]))
110 case CTRL('U'): /* delete line */
111 case CTRL('C'): /* Send IP */
112 case CTRL('\\'): /* Quit */
113 case CTRL('Z'): /* Suspend */
114 while (term_buflen
> 0) {
116 bsb(plen(term_buf
[term_buflen
-1]));
119 back
->special (TS_EL
);
120 if( c
== CTRL('C') ) back
->special (TS_IP
);
121 if( c
== CTRL('Z') ) back
->special (TS_SUSP
);
122 if( c
== CTRL('\\') ) back
->special (TS_ABORT
);
124 case CTRL('R'): /* redraw line */
127 c_write("^R\r\n", 4);
128 for (i
= 0; i
< term_buflen
; i
++)
132 case CTRL('V'): /* quote next char */
133 term_quotenext
= TRUE
;
135 case CTRL('D'): /* logout or send */
136 if (term_buflen
== 0) {
137 back
->special (TS_EOF
);
139 back
->send(term_buf
, term_buflen
);
143 case CTRL('M'): /* send with newline */
145 back
->send(term_buf
, term_buflen
);
146 if (cfg
.protocol
== PROT_RAW
)
147 back
->send("\r\n", 2);
154 default: /* get to this label from ^V handler */
155 if (term_buflen
>= term_bufsiz
) {
156 term_bufsiz
= term_buflen
+ 256;
157 term_buf
= saferealloc(term_buf
, term_bufsiz
);
159 term_buf
[term_buflen
++] = c
;
162 term_quotenext
= FALSE
;
167 if( term_buflen
!= 0 )
169 back
->send(term_buf
, term_buflen
);
170 while (term_buflen
> 0) {
171 bsb(plen(term_buf
[term_buflen
-1]));
178 back
->send(buf
, len
);