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