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