Annotation of src/usr.bin/telnet/terminal.c, Revision 1.8
1.8 ! guenther 1: /* $OpenBSD: terminal.c,v 1.7 2014/07/19 23:50:38 guenther Exp $ */
1.2 niklas 2: /* $NetBSD: terminal.c,v 1.5 1996/02/28 21:04:17 thorpej Exp $ */
3:
1.1 deraadt 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.
1.6 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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:
1.3 art 33: #include "telnet_locl.h"
1.1 deraadt 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:
1.8 ! guenther 75: void
1.1 deraadt 76: init_terminal()
77: {
1.8 ! guenther 78: ring_init(&ttyoring, ttyobuf, sizeof ttyobuf);
! 79: ring_init(&ttyiring, ttyibuf, sizeof ttyibuf);
! 80: autoflush = TerminalAutoFlush();
1.1 deraadt 81: }
82:
83:
84: /*
85: * Send as much data as possible to the terminal.
86: *
87: * Return value:
88: * -1: No useful work done, data waiting to go out.
89: * 0: No data was waiting, so nothing was done.
90: * 1: All waiting data was written out.
91: * n: All data - n was written out.
92: */
93:
94:
95: int
96: ttyflush(drop)
97: int drop;
98: {
1.4 mpech 99: int n, n0, n1;
1.1 deraadt 100:
101: n0 = ring_full_count(&ttyoring);
102: if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) {
103: if (drop) {
104: TerminalFlushOutput();
105: /* we leave 'n' alone! */
106: } else {
1.3 art 107: n = TerminalWrite((char *)ttyoring.consume, n);
1.1 deraadt 108: }
109: }
110: if (n > 0) {
111: if (termdata && n) {
112: Dump('>', ttyoring.consume, n);
113: }
114: /*
115: * If we wrote everything, and the full count is
116: * larger than what we wrote, then write the
117: * rest of the buffer.
118: */
119: if (n1 == n && n0 > n) {
120: n1 = n0 - n;
121: if (!drop)
122: n1 = TerminalWrite(ttyoring.bottom, n1);
1.2 niklas 123: if (n1 > 0)
124: n += n1;
1.1 deraadt 125: }
126: ring_consumed(&ttyoring, n);
127: }
1.5 millert 128: if (n < 0) {
129: if (errno == EPIPE)
130: kill(0, SIGQUIT);
1.1 deraadt 131: return -1;
1.5 millert 132: }
1.1 deraadt 133: if (n == n0) {
134: if (n0)
135: return -1;
136: return 0;
137: }
138: return n0 - n + 1;
139: }
140:
141:
142: /*
143: * These routines decides on what the mode should be (based on the values
144: * of various global variables).
145: */
146:
147:
148: int
149: getconnmode()
150: {
151: extern int linemode;
152: int mode = 0;
153: #ifdef KLUDGELINEMODE
154: extern int kludgelinemode;
155: #endif
156:
157: if (my_want_state_is_dont(TELOPT_ECHO))
158: mode |= MODE_ECHO;
159:
160: if (localflow)
161: mode |= MODE_FLOW;
162:
1.3 art 163: if ((eight & 1) || my_want_state_is_will(TELOPT_BINARY))
1.1 deraadt 164: mode |= MODE_INBIN;
165:
1.3 art 166: if (eight & 2)
167: mode |= MODE_OUT8;
1.1 deraadt 168: if (his_want_state_is_will(TELOPT_BINARY))
169: mode |= MODE_OUTBIN;
170:
171: #ifdef KLUDGELINEMODE
172: if (kludgelinemode) {
173: if (my_want_state_is_dont(TELOPT_SGA)) {
174: mode |= (MODE_TRAPSIG|MODE_EDIT);
175: if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) {
176: mode &= ~MODE_ECHO;
177: }
178: }
179: return(mode);
180: }
181: #endif
182: if (my_want_state_is_will(TELOPT_LINEMODE))
183: mode |= linemode;
184: return(mode);
185: }
186:
187: void
188: setconnmode(force)
189: int force;
190: {
1.4 mpech 191: int newmode;
1.1 deraadt 192:
193: newmode = getconnmode()|(force?MODE_FORCE:0);
194:
195: TerminalNewMode(newmode);
196: }
197:
198:
199: void
200: setcommandmode()
201: {
202: TerminalNewMode(-1);
203: }