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