2 * Copyright 1994-2002 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 #pragma ident "%Z%%M% %I% %E% SMI"
9 * usr/src/cmd/cmd-inet/usr.bin/telnet/terminal.c
13 * Copyright (c) 1988, 1990, 1993
14 * The Regents of the University of California. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 static char sccsid
[] = "@(#)terminal.c 8.1 (Berkeley) 6/6/93";
47 #include <arpa/telnet.h>
48 #include <sys/types.h>
58 static unsigned char ttyobuf
[2*BUFSIZ
];
59 static unsigned char ttyibuf
[BUFSIZ
];
61 int termdata
; /* Debugging flag */
71 * initialize the terminal data structures.
77 if (ring_init(&ttyoring
, ttyobuf
, sizeof (ttyobuf
)) != 1) {
80 if (ring_init(&ttyiring
, ttyibuf
, sizeof (ttyibuf
)) != 1) {
88 * Send as much data as possible to the terminal.
91 * -2: Error occurred other than EWOULDBLOCK; see
92 * 'errno' for specifics
93 * -1: No useful work done, although data was waiting.
94 * This may be due to EWOULDBLOCK error.
95 * 0: No data was waiting, so nothing was done.
96 * 1: All waiting data was written out.
97 * n: Part of data was written. 'n' is the number
98 * of bytes remaining which were not written.
105 register int n
, n0
, n1
;
107 n0
= ring_full_count(&ttyoring
);
108 if ((n1
= n
= ring_full_consecutive(&ttyoring
)) > 0) {
110 TerminalFlushOutput();
111 /* we leave 'n' alone! */
113 n
= TerminalWrite((char *)ttyoring
.consume
, n
);
114 if (n
== -1 && errno
!= EWOULDBLOCK
)
120 Dump('>', ttyoring
.consume
, n
);
123 * If we wrote everything, and the full count is
124 * larger than what we wrote, then write the
125 * rest of the buffer.
127 if (n1
== n
&& n0
> n
) {
130 n1
= TerminalWrite((char *)ttyoring
.bottom
, n1
);
132 if (errno
!= EWOULDBLOCK
)
139 ring_consumed(&ttyoring
, n
);
154 * These routines decides on what the mode should be (based on the values
155 * of various global variables).
164 #ifdef KLUDGELINEMODE
165 extern int kludgelinemode
;
168 if (my_want_state_is_dont(TELOPT_ECHO
))
174 if (my_want_state_is_will(TELOPT_BINARY
))
177 if (my_want_state_is_do(TELOPT_BINARY
))
180 #ifdef KLUDGELINEMODE
181 if (kludgelinemode
) {
182 if (my_want_state_is_dont(TELOPT_SGA
)) {
183 mode
|= (MODE_TRAPSIG
|MODE_EDIT
);
185 (clocks
.echotoggle
> clocks
.modenegotiated
)) {
192 if (my_want_state_is_will(TELOPT_LINEMODE
))
201 static int enc_passwd
= 0;
202 register int newmode
;
204 newmode
= getconnmode()|(force
?MODE_FORCE
:0);
206 TerminalNewMode(newmode
);
208 if ((newmode
& (MODE_ECHO
|MODE_EDIT
)) == MODE_EDIT
) {
209 if (my_want_state_is_will(TELOPT_ENCRYPT
) &&
210 (enc_passwd
== 0) && !encrypt_output
) {
211 encrypt_request_start(0, 0);
216 encrypt_request_end();