xref: /netbsd-src/usr.bin/telnet/terminal.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: terminal.c,v 1.5 1996/02/28 21:04:17 thorpej 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)terminal.c	8.2 (Berkeley) 2/16/95";
39 #else
40 static char rcsid[] = "$NetBSD: terminal.c,v 1.5 1996/02/28 21:04:17 thorpej Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <arpa/telnet.h>
45 #include <sys/types.h>
46 
47 #include "ring.h"
48 
49 #include "externs.h"
50 #include "types.h"
51 
52 Ring		ttyoring, ttyiring;
53 unsigned char	ttyobuf[2*BUFSIZ], ttyibuf[BUFSIZ];
54 
55 int termdata;			/* Debugging flag */
56 
57 #ifdef	USE_TERMIO
58 # ifndef VDISCARD
59 cc_t termFlushChar;
60 # endif
61 # ifndef VLNEXT
62 cc_t termLiteralNextChar;
63 # endif
64 # ifndef VSUSP
65 cc_t termSuspChar;
66 # endif
67 # ifndef VWERASE
68 cc_t termWerasChar;
69 # endif
70 # ifndef VREPRINT
71 cc_t termRprntChar;
72 # endif
73 # ifndef VSTART
74 cc_t termStartChar;
75 # endif
76 # ifndef VSTOP
77 cc_t termStopChar;
78 # endif
79 # ifndef VEOL
80 cc_t termForw1Char;
81 # endif
82 # ifndef VEOL2
83 cc_t termForw2Char;
84 # endif
85 # ifndef VSTATUS
86 cc_t termAytChar;
87 # endif
88 #else
89 cc_t termForw2Char;
90 cc_t termAytChar;
91 #endif
92 
93 /*
94  * initialize the terminal data structures.
95  */
96 
97     void
98 init_terminal()
99 {
100     if (ring_init(&ttyoring, ttyobuf, sizeof ttyobuf) != 1) {
101 	exit(1);
102     }
103     if (ring_init(&ttyiring, ttyibuf, sizeof ttyibuf) != 1) {
104 	exit(1);
105     }
106     autoflush = TerminalAutoFlush();
107 }
108 
109 
110 /*
111  *		Send as much data as possible to the terminal.
112  *
113  *		Return value:
114  *			-1: No useful work done, data waiting to go out.
115  *			 0: No data was waiting, so nothing was done.
116  *			 1: All waiting data was written out.
117  *			 n: All data - n was written out.
118  */
119 
120 
121     int
122 ttyflush(drop)
123     int drop;
124 {
125     register int n, n0, n1;
126 
127     n0 = ring_full_count(&ttyoring);
128     if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) {
129 	if (drop) {
130 	    TerminalFlushOutput();
131 	    /* we leave 'n' alone! */
132 	} else {
133 	    n = TerminalWrite(ttyoring.consume, n);
134 	}
135     }
136     if (n > 0) {
137 	if (termdata && n) {
138 	    Dump('>', ttyoring.consume, n);
139 	}
140 	/*
141 	 * If we wrote everything, and the full count is
142 	 * larger than what we wrote, then write the
143 	 * rest of the buffer.
144 	 */
145 	if (n1 == n && n0 > n) {
146 		n1 = n0 - n;
147 		if (!drop)
148 			n1 = TerminalWrite(ttyoring.bottom, n1);
149 		if (n1 > 0)
150 			n += n1;
151 	}
152 	ring_consumed(&ttyoring, n);
153     }
154     if (n < 0)
155 	return -1;
156     if (n == n0) {
157 	if (n0)
158 	    return -1;
159 	return 0;
160     }
161     return n0 - n + 1;
162 }
163 
164 
165 /*
166  * These routines decides on what the mode should be (based on the values
167  * of various global variables).
168  */
169 
170 
171     int
172 getconnmode()
173 {
174     extern int linemode;
175     int mode = 0;
176 #ifdef	KLUDGELINEMODE
177     extern int kludgelinemode;
178 #endif
179 
180     if (In3270)
181 	return(MODE_FLOW);
182 
183     if (my_want_state_is_dont(TELOPT_ECHO))
184 	mode |= MODE_ECHO;
185 
186     if (localflow)
187 	mode |= MODE_FLOW;
188 
189     if (my_want_state_is_will(TELOPT_BINARY))
190 	mode |= MODE_INBIN;
191 
192     if (his_want_state_is_will(TELOPT_BINARY))
193 	mode |= MODE_OUTBIN;
194 
195 #ifdef	KLUDGELINEMODE
196     if (kludgelinemode) {
197 	if (my_want_state_is_dont(TELOPT_SGA)) {
198 	    mode |= (MODE_TRAPSIG|MODE_EDIT);
199 	    if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) {
200 		mode &= ~MODE_ECHO;
201 	    }
202 	}
203 	return(mode);
204     }
205 #endif
206     if (my_want_state_is_will(TELOPT_LINEMODE))
207 	mode |= linemode;
208     return(mode);
209 }
210 
211     void
212 setconnmode(force)
213     int force;
214 {
215     register int newmode;
216 
217     newmode = getconnmode()|(force?MODE_FORCE:0);
218 
219     TerminalNewMode(newmode);
220 
221 
222 }
223 
224 
225     void
226 setcommandmode()
227 {
228     TerminalNewMode(-1);
229 }
230