1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 1994-2002 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * usr/src/cmd/cmd-inet/usr.bin/telnet/terminal.c
8*7c478bd9Sstevel@tonic-gate  */
9*7c478bd9Sstevel@tonic-gate 
10*7c478bd9Sstevel@tonic-gate /*
11*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1990, 1993
12*7c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
15*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
16*7c478bd9Sstevel@tonic-gate  * are met:
17*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
18*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
19*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
20*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
21*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
22*7c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
23*7c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
24*7c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
25*7c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
26*7c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
27*7c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
28*7c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
29*7c478bd9Sstevel@tonic-gate  *
30*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31*7c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33*7c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34*7c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35*7c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36*7c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37*7c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38*7c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39*7c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40*7c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
41*7c478bd9Sstevel@tonic-gate  */
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #ifndef lint
44*7c478bd9Sstevel@tonic-gate static char sccsid[] = "@(#)terminal.c	8.1 (Berkeley) 6/6/93";
45*7c478bd9Sstevel@tonic-gate #endif /* not lint */
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #include <arpa/telnet.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
49*7c478bd9Sstevel@tonic-gate #include <errno.h>
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #include "ring.h"
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #include "externs.h"
54*7c478bd9Sstevel@tonic-gate #include "types.h"
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate Ring	ttyoring;
57*7c478bd9Sstevel@tonic-gate Ring	ttyiring;
58*7c478bd9Sstevel@tonic-gate static unsigned char ttyobuf[2*BUFSIZ];
59*7c478bd9Sstevel@tonic-gate static unsigned char ttyibuf[BUFSIZ];
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate int termdata;			/* Debugging flag */
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate #ifdef	USE_TERMIO
64*7c478bd9Sstevel@tonic-gate cc_t termAytChar;
65*7c478bd9Sstevel@tonic-gate #else
66*7c478bd9Sstevel@tonic-gate cc_t termForw2Char;
67*7c478bd9Sstevel@tonic-gate cc_t termAytChar;
68*7c478bd9Sstevel@tonic-gate #endif
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate /*
71*7c478bd9Sstevel@tonic-gate  * initialize the terminal data structures.
72*7c478bd9Sstevel@tonic-gate  */
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate     void
init_terminal()75*7c478bd9Sstevel@tonic-gate init_terminal()
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	if (ring_init(&ttyoring, ttyobuf, sizeof (ttyobuf)) != 1) {
78*7c478bd9Sstevel@tonic-gate 		exit(1);
79*7c478bd9Sstevel@tonic-gate 	}
80*7c478bd9Sstevel@tonic-gate 	if (ring_init(&ttyiring, ttyibuf, sizeof (ttyibuf)) != 1) {
81*7c478bd9Sstevel@tonic-gate 		exit(1);
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 	autoflush = 1;
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate /*
88*7c478bd9Sstevel@tonic-gate  *		Send as much data as possible to the terminal.
89*7c478bd9Sstevel@tonic-gate  *
90*7c478bd9Sstevel@tonic-gate  *		Return value:
91*7c478bd9Sstevel@tonic-gate  *			-2: Error occurred other than EWOULDBLOCK; see
92*7c478bd9Sstevel@tonic-gate  *			    'errno' for specifics
93*7c478bd9Sstevel@tonic-gate  *			-1: No useful work done, although data was waiting.
94*7c478bd9Sstevel@tonic-gate  *			    This may be due to EWOULDBLOCK error.
95*7c478bd9Sstevel@tonic-gate  *			 0: No data was waiting, so nothing was done.
96*7c478bd9Sstevel@tonic-gate  *			 1: All waiting data was written out.
97*7c478bd9Sstevel@tonic-gate  *			 n: Part of data was written.  'n' is the number
98*7c478bd9Sstevel@tonic-gate  *			    of bytes remaining which were not written.
99*7c478bd9Sstevel@tonic-gate  */
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate int
ttyflush(drop)102*7c478bd9Sstevel@tonic-gate ttyflush(drop)
103*7c478bd9Sstevel@tonic-gate 	int drop;
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	register int n, n0, n1;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	n0 = ring_full_count(&ttyoring);
108*7c478bd9Sstevel@tonic-gate 	if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) {
109*7c478bd9Sstevel@tonic-gate 		if (drop) {
110*7c478bd9Sstevel@tonic-gate 			TerminalFlushOutput();
111*7c478bd9Sstevel@tonic-gate 			/* we leave 'n' alone! */
112*7c478bd9Sstevel@tonic-gate 		} else {
113*7c478bd9Sstevel@tonic-gate 			n = TerminalWrite((char *)ttyoring.consume, n);
114*7c478bd9Sstevel@tonic-gate 			if (n == -1 && errno != EWOULDBLOCK)
115*7c478bd9Sstevel@tonic-gate 				return (-2);
116*7c478bd9Sstevel@tonic-gate 		}
117*7c478bd9Sstevel@tonic-gate 	}
118*7c478bd9Sstevel@tonic-gate 	if (n > 0) {
119*7c478bd9Sstevel@tonic-gate 		if (termdata && n) {
120*7c478bd9Sstevel@tonic-gate 			Dump('>', ttyoring.consume, n);
121*7c478bd9Sstevel@tonic-gate 		}
122*7c478bd9Sstevel@tonic-gate 		/*
123*7c478bd9Sstevel@tonic-gate 		 * If we wrote everything, and the full count is
124*7c478bd9Sstevel@tonic-gate 		 * larger than what we wrote, then write the
125*7c478bd9Sstevel@tonic-gate 		 * rest of the buffer.
126*7c478bd9Sstevel@tonic-gate 		 */
127*7c478bd9Sstevel@tonic-gate 		if (n1 == n && n0 > n) {
128*7c478bd9Sstevel@tonic-gate 			n1 = n0 - n;
129*7c478bd9Sstevel@tonic-gate 			if (!drop) {
130*7c478bd9Sstevel@tonic-gate 				n1 = TerminalWrite((char *)ttyoring.bottom, n1);
131*7c478bd9Sstevel@tonic-gate 				if (n1 == -1) {
132*7c478bd9Sstevel@tonic-gate 					if (errno != EWOULDBLOCK)
133*7c478bd9Sstevel@tonic-gate 						return (-2);
134*7c478bd9Sstevel@tonic-gate 					n1 = 0;
135*7c478bd9Sstevel@tonic-gate 				}
136*7c478bd9Sstevel@tonic-gate 			}
137*7c478bd9Sstevel@tonic-gate 			n += n1;
138*7c478bd9Sstevel@tonic-gate 		}
139*7c478bd9Sstevel@tonic-gate 		ring_consumed(&ttyoring, n);
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 	if (n < 0)
142*7c478bd9Sstevel@tonic-gate 		return (-1);
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	if (n == n0) {
145*7c478bd9Sstevel@tonic-gate 		if (n0)
146*7c478bd9Sstevel@tonic-gate 			return (-1);
147*7c478bd9Sstevel@tonic-gate 		return (0);
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 	return (n0 - n + 1);
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate /*
154*7c478bd9Sstevel@tonic-gate  * These routines decides on what the mode should be (based on the values
155*7c478bd9Sstevel@tonic-gate  * of various global variables).
156*7c478bd9Sstevel@tonic-gate  */
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate int
getconnmode()160*7c478bd9Sstevel@tonic-gate getconnmode()
161*7c478bd9Sstevel@tonic-gate {
162*7c478bd9Sstevel@tonic-gate 	extern int linemode;
163*7c478bd9Sstevel@tonic-gate 	int mode = 0;
164*7c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
165*7c478bd9Sstevel@tonic-gate 	extern int kludgelinemode;
166*7c478bd9Sstevel@tonic-gate #endif
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	if (my_want_state_is_dont(TELOPT_ECHO))
169*7c478bd9Sstevel@tonic-gate 		mode |= MODE_ECHO;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	if (localflow)
172*7c478bd9Sstevel@tonic-gate 		mode |= MODE_FLOW;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	if (my_want_state_is_will(TELOPT_BINARY))
175*7c478bd9Sstevel@tonic-gate 		mode |= MODE_INBIN;
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	if (my_want_state_is_do(TELOPT_BINARY))
178*7c478bd9Sstevel@tonic-gate 		mode |= MODE_OUTBIN;
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
181*7c478bd9Sstevel@tonic-gate 	if (kludgelinemode) {
182*7c478bd9Sstevel@tonic-gate 		if (my_want_state_is_dont(TELOPT_SGA)) {
183*7c478bd9Sstevel@tonic-gate 			mode |= (MODE_TRAPSIG|MODE_EDIT);
184*7c478bd9Sstevel@tonic-gate 			if (dontlecho &&
185*7c478bd9Sstevel@tonic-gate 			    (clocks.echotoggle > clocks.modenegotiated)) {
186*7c478bd9Sstevel@tonic-gate 				mode &= ~MODE_ECHO;
187*7c478bd9Sstevel@tonic-gate 			}
188*7c478bd9Sstevel@tonic-gate 		}
189*7c478bd9Sstevel@tonic-gate 		return (mode);
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate #endif
192*7c478bd9Sstevel@tonic-gate 	if (my_want_state_is_will(TELOPT_LINEMODE))
193*7c478bd9Sstevel@tonic-gate 		mode |= linemode;
194*7c478bd9Sstevel@tonic-gate 	return (mode);
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate void
setconnmode(force)198*7c478bd9Sstevel@tonic-gate setconnmode(force)
199*7c478bd9Sstevel@tonic-gate 	int force;
200*7c478bd9Sstevel@tonic-gate {
201*7c478bd9Sstevel@tonic-gate 	static int enc_passwd = 0;
202*7c478bd9Sstevel@tonic-gate 	register int newmode;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	newmode = getconnmode()|(force?MODE_FORCE:0);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	TerminalNewMode(newmode);
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	if ((newmode & (MODE_ECHO|MODE_EDIT)) == MODE_EDIT) {
209*7c478bd9Sstevel@tonic-gate 		if (my_want_state_is_will(TELOPT_ENCRYPT) &&
210*7c478bd9Sstevel@tonic-gate 		    (enc_passwd == 0) && !encrypt_output) {
211*7c478bd9Sstevel@tonic-gate 			encrypt_request_start(0, 0);
212*7c478bd9Sstevel@tonic-gate 			enc_passwd = 1;
213*7c478bd9Sstevel@tonic-gate 		}
214*7c478bd9Sstevel@tonic-gate 	} else {
215*7c478bd9Sstevel@tonic-gate 		if (enc_passwd) {
216*7c478bd9Sstevel@tonic-gate 			encrypt_request_end();
217*7c478bd9Sstevel@tonic-gate 			enc_passwd = 0;
218*7c478bd9Sstevel@tonic-gate 		}
219*7c478bd9Sstevel@tonic-gate 	}
220*7c478bd9Sstevel@tonic-gate }
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate     void
setcommandmode()224*7c478bd9Sstevel@tonic-gate setcommandmode()
225*7c478bd9Sstevel@tonic-gate {
226*7c478bd9Sstevel@tonic-gate 	TerminalNewMode(-1);
227*7c478bd9Sstevel@tonic-gate }
228