1 /* $OpenBSD: terminal.c,v 1.3 1998/03/12 04:57:45 art 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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include "telnet_locl.h" 38 39 Ring ttyoring, ttyiring; 40 unsigned char ttyobuf[2*BUFSIZ], ttyibuf[BUFSIZ]; 41 42 int termdata; /* Debugging flag */ 43 44 #ifdef USE_TERMIO 45 # ifndef VDISCARD 46 cc_t termFlushChar; 47 # endif 48 # ifndef VLNEXT 49 cc_t termLiteralNextChar; 50 # endif 51 # ifndef VSUSP 52 cc_t termSuspChar; 53 # endif 54 # ifndef VWERASE 55 cc_t termWerasChar; 56 # endif 57 # ifndef VREPRINT 58 cc_t termRprntChar; 59 # endif 60 # ifndef VSTART 61 cc_t termStartChar; 62 # endif 63 # ifndef VSTOP 64 cc_t termStopChar; 65 # endif 66 # ifndef VEOL 67 cc_t termForw1Char; 68 # endif 69 # ifndef VEOL2 70 cc_t termForw2Char; 71 # endif 72 # ifndef VSTATUS 73 cc_t termAytChar; 74 # endif 75 #else 76 cc_t termForw2Char; 77 cc_t termAytChar; 78 #endif 79 80 /* 81 * initialize the terminal data structures. 82 */ 83 84 void 85 init_terminal() 86 { 87 if (ring_init(&ttyoring, ttyobuf, sizeof ttyobuf) != 1) { 88 exit(1); 89 } 90 if (ring_init(&ttyiring, ttyibuf, sizeof ttyibuf) != 1) { 91 exit(1); 92 } 93 autoflush = TerminalAutoFlush(); 94 } 95 96 97 /* 98 * Send as much data as possible to the terminal. 99 * 100 * Return value: 101 * -1: No useful work done, data waiting to go out. 102 * 0: No data was waiting, so nothing was done. 103 * 1: All waiting data was written out. 104 * n: All data - n was written out. 105 */ 106 107 108 int 109 ttyflush(drop) 110 int drop; 111 { 112 register int n, n0, n1; 113 114 n0 = ring_full_count(&ttyoring); 115 if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) { 116 if (drop) { 117 TerminalFlushOutput(); 118 /* we leave 'n' alone! */ 119 } else { 120 n = TerminalWrite((char *)ttyoring.consume, n); 121 } 122 } 123 if (n > 0) { 124 if (termdata && n) { 125 Dump('>', ttyoring.consume, n); 126 } 127 /* 128 * If we wrote everything, and the full count is 129 * larger than what we wrote, then write the 130 * rest of the buffer. 131 */ 132 if (n1 == n && n0 > n) { 133 n1 = n0 - n; 134 if (!drop) 135 n1 = TerminalWrite(ttyoring.bottom, n1); 136 if (n1 > 0) 137 n += n1; 138 } 139 ring_consumed(&ttyoring, n); 140 } 141 if (n < 0) 142 return -1; 143 if (n == n0) { 144 if (n0) 145 return -1; 146 return 0; 147 } 148 return n0 - n + 1; 149 } 150 151 152 /* 153 * These routines decides on what the mode should be (based on the values 154 * of various global variables). 155 */ 156 157 158 int 159 getconnmode() 160 { 161 extern int linemode; 162 int mode = 0; 163 #ifdef KLUDGELINEMODE 164 extern int kludgelinemode; 165 #endif 166 167 if (In3270) 168 return(MODE_FLOW); 169 170 if (my_want_state_is_dont(TELOPT_ECHO)) 171 mode |= MODE_ECHO; 172 173 if (localflow) 174 mode |= MODE_FLOW; 175 176 if ((eight & 1) || my_want_state_is_will(TELOPT_BINARY)) 177 mode |= MODE_INBIN; 178 179 if (eight & 2) 180 mode |= MODE_OUT8; 181 if (his_want_state_is_will(TELOPT_BINARY)) 182 mode |= MODE_OUTBIN; 183 184 #ifdef KLUDGELINEMODE 185 if (kludgelinemode) { 186 if (my_want_state_is_dont(TELOPT_SGA)) { 187 mode |= (MODE_TRAPSIG|MODE_EDIT); 188 if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) { 189 mode &= ~MODE_ECHO; 190 } 191 } 192 return(mode); 193 } 194 #endif 195 if (my_want_state_is_will(TELOPT_LINEMODE)) 196 mode |= linemode; 197 return(mode); 198 } 199 200 void 201 setconnmode(force) 202 int force; 203 { 204 register int newmode; 205 #ifdef ENCRYPTION 206 static int enc_passwd = 0; 207 #endif 208 209 newmode = getconnmode()|(force?MODE_FORCE:0); 210 211 TerminalNewMode(newmode); 212 213 #ifdef ENCRYPTION 214 if ((newmode & (MODE_ECHO|MODE_EDIT)) == MODE_EDIT) { 215 if (my_want_state_is_will(TELOPT_ENCRYPT) 216 && (enc_passwd == 0) && !encrypt_output) { 217 encrypt_request_start(0, 0); 218 enc_passwd = 1; 219 } 220 } else { 221 if (enc_passwd) { 222 encrypt_request_end(); 223 enc_passwd = 0; 224 } 225 } 226 #endif 227 228 } 229 230 231 void 232 setcommandmode() 233 { 234 TerminalNewMode(-1); 235 } 236