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