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