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