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