1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 1994-2002 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 7*0Sstevel@tonic-gate 8*0Sstevel@tonic-gate /* 9*0Sstevel@tonic-gate * usr/src/cmd/cmd-inet/usr.bin/telnet/terminal.c 10*0Sstevel@tonic-gate */ 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gate /* 13*0Sstevel@tonic-gate * Copyright (c) 1988, 1990, 1993 14*0Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 15*0Sstevel@tonic-gate * 16*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 17*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 18*0Sstevel@tonic-gate * are met: 19*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 20*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 21*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 22*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 23*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 24*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 25*0Sstevel@tonic-gate * must display the following acknowledgement: 26*0Sstevel@tonic-gate * This product includes software developed by the University of 27*0Sstevel@tonic-gate * California, Berkeley and its contributors. 28*0Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 29*0Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 30*0Sstevel@tonic-gate * without specific prior written permission. 31*0Sstevel@tonic-gate * 32*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42*0Sstevel@tonic-gate * SUCH DAMAGE. 43*0Sstevel@tonic-gate */ 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate #ifndef lint 46*0Sstevel@tonic-gate static char sccsid[] = "@(#)terminal.c 8.1 (Berkeley) 6/6/93"; 47*0Sstevel@tonic-gate #endif /* not lint */ 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate #include <arpa/telnet.h> 50*0Sstevel@tonic-gate #include <sys/types.h> 51*0Sstevel@tonic-gate #include <errno.h> 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate #include "ring.h" 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate #include "externs.h" 56*0Sstevel@tonic-gate #include "types.h" 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate Ring ttyoring; 59*0Sstevel@tonic-gate Ring ttyiring; 60*0Sstevel@tonic-gate static unsigned char ttyobuf[2*BUFSIZ]; 61*0Sstevel@tonic-gate static unsigned char ttyibuf[BUFSIZ]; 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate int termdata; /* Debugging flag */ 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate #ifdef USE_TERMIO 66*0Sstevel@tonic-gate cc_t termAytChar; 67*0Sstevel@tonic-gate #else 68*0Sstevel@tonic-gate cc_t termForw2Char; 69*0Sstevel@tonic-gate cc_t termAytChar; 70*0Sstevel@tonic-gate #endif 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate /* 73*0Sstevel@tonic-gate * initialize the terminal data structures. 74*0Sstevel@tonic-gate */ 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate void 77*0Sstevel@tonic-gate init_terminal() 78*0Sstevel@tonic-gate { 79*0Sstevel@tonic-gate if (ring_init(&ttyoring, ttyobuf, sizeof (ttyobuf)) != 1) { 80*0Sstevel@tonic-gate exit(1); 81*0Sstevel@tonic-gate } 82*0Sstevel@tonic-gate if (ring_init(&ttyiring, ttyibuf, sizeof (ttyibuf)) != 1) { 83*0Sstevel@tonic-gate exit(1); 84*0Sstevel@tonic-gate } 85*0Sstevel@tonic-gate autoflush = 1; 86*0Sstevel@tonic-gate } 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate /* 90*0Sstevel@tonic-gate * Send as much data as possible to the terminal. 91*0Sstevel@tonic-gate * 92*0Sstevel@tonic-gate * Return value: 93*0Sstevel@tonic-gate * -2: Error occurred other than EWOULDBLOCK; see 94*0Sstevel@tonic-gate * 'errno' for specifics 95*0Sstevel@tonic-gate * -1: No useful work done, although data was waiting. 96*0Sstevel@tonic-gate * This may be due to EWOULDBLOCK error. 97*0Sstevel@tonic-gate * 0: No data was waiting, so nothing was done. 98*0Sstevel@tonic-gate * 1: All waiting data was written out. 99*0Sstevel@tonic-gate * n: Part of data was written. 'n' is the number 100*0Sstevel@tonic-gate * of bytes remaining which were not written. 101*0Sstevel@tonic-gate */ 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate int 104*0Sstevel@tonic-gate ttyflush(drop) 105*0Sstevel@tonic-gate int drop; 106*0Sstevel@tonic-gate { 107*0Sstevel@tonic-gate register int n, n0, n1; 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate n0 = ring_full_count(&ttyoring); 110*0Sstevel@tonic-gate if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) { 111*0Sstevel@tonic-gate if (drop) { 112*0Sstevel@tonic-gate TerminalFlushOutput(); 113*0Sstevel@tonic-gate /* we leave 'n' alone! */ 114*0Sstevel@tonic-gate } else { 115*0Sstevel@tonic-gate n = TerminalWrite((char *)ttyoring.consume, n); 116*0Sstevel@tonic-gate if (n == -1 && errno != EWOULDBLOCK) 117*0Sstevel@tonic-gate return (-2); 118*0Sstevel@tonic-gate } 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate if (n > 0) { 121*0Sstevel@tonic-gate if (termdata && n) { 122*0Sstevel@tonic-gate Dump('>', ttyoring.consume, n); 123*0Sstevel@tonic-gate } 124*0Sstevel@tonic-gate /* 125*0Sstevel@tonic-gate * If we wrote everything, and the full count is 126*0Sstevel@tonic-gate * larger than what we wrote, then write the 127*0Sstevel@tonic-gate * rest of the buffer. 128*0Sstevel@tonic-gate */ 129*0Sstevel@tonic-gate if (n1 == n && n0 > n) { 130*0Sstevel@tonic-gate n1 = n0 - n; 131*0Sstevel@tonic-gate if (!drop) { 132*0Sstevel@tonic-gate n1 = TerminalWrite((char *)ttyoring.bottom, n1); 133*0Sstevel@tonic-gate if (n1 == -1) { 134*0Sstevel@tonic-gate if (errno != EWOULDBLOCK) 135*0Sstevel@tonic-gate return (-2); 136*0Sstevel@tonic-gate n1 = 0; 137*0Sstevel@tonic-gate } 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate n += n1; 140*0Sstevel@tonic-gate } 141*0Sstevel@tonic-gate ring_consumed(&ttyoring, n); 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate if (n < 0) 144*0Sstevel@tonic-gate return (-1); 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate if (n == n0) { 147*0Sstevel@tonic-gate if (n0) 148*0Sstevel@tonic-gate return (-1); 149*0Sstevel@tonic-gate return (0); 150*0Sstevel@tonic-gate } 151*0Sstevel@tonic-gate return (n0 - n + 1); 152*0Sstevel@tonic-gate } 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate /* 156*0Sstevel@tonic-gate * These routines decides on what the mode should be (based on the values 157*0Sstevel@tonic-gate * of various global variables). 158*0Sstevel@tonic-gate */ 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate int 162*0Sstevel@tonic-gate getconnmode() 163*0Sstevel@tonic-gate { 164*0Sstevel@tonic-gate extern int linemode; 165*0Sstevel@tonic-gate int mode = 0; 166*0Sstevel@tonic-gate #ifdef KLUDGELINEMODE 167*0Sstevel@tonic-gate extern int kludgelinemode; 168*0Sstevel@tonic-gate #endif 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate if (my_want_state_is_dont(TELOPT_ECHO)) 171*0Sstevel@tonic-gate mode |= MODE_ECHO; 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate if (localflow) 174*0Sstevel@tonic-gate mode |= MODE_FLOW; 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate if (my_want_state_is_will(TELOPT_BINARY)) 177*0Sstevel@tonic-gate mode |= MODE_INBIN; 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate if (my_want_state_is_do(TELOPT_BINARY)) 180*0Sstevel@tonic-gate mode |= MODE_OUTBIN; 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate #ifdef KLUDGELINEMODE 183*0Sstevel@tonic-gate if (kludgelinemode) { 184*0Sstevel@tonic-gate if (my_want_state_is_dont(TELOPT_SGA)) { 185*0Sstevel@tonic-gate mode |= (MODE_TRAPSIG|MODE_EDIT); 186*0Sstevel@tonic-gate if (dontlecho && 187*0Sstevel@tonic-gate (clocks.echotoggle > clocks.modenegotiated)) { 188*0Sstevel@tonic-gate mode &= ~MODE_ECHO; 189*0Sstevel@tonic-gate } 190*0Sstevel@tonic-gate } 191*0Sstevel@tonic-gate return (mode); 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate #endif 194*0Sstevel@tonic-gate if (my_want_state_is_will(TELOPT_LINEMODE)) 195*0Sstevel@tonic-gate mode |= linemode; 196*0Sstevel@tonic-gate return (mode); 197*0Sstevel@tonic-gate } 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate void 200*0Sstevel@tonic-gate setconnmode(force) 201*0Sstevel@tonic-gate int force; 202*0Sstevel@tonic-gate { 203*0Sstevel@tonic-gate static int enc_passwd = 0; 204*0Sstevel@tonic-gate register int newmode; 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate newmode = getconnmode()|(force?MODE_FORCE:0); 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate TerminalNewMode(newmode); 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate if ((newmode & (MODE_ECHO|MODE_EDIT)) == MODE_EDIT) { 211*0Sstevel@tonic-gate if (my_want_state_is_will(TELOPT_ENCRYPT) && 212*0Sstevel@tonic-gate (enc_passwd == 0) && !encrypt_output) { 213*0Sstevel@tonic-gate encrypt_request_start(0, 0); 214*0Sstevel@tonic-gate enc_passwd = 1; 215*0Sstevel@tonic-gate } 216*0Sstevel@tonic-gate } else { 217*0Sstevel@tonic-gate if (enc_passwd) { 218*0Sstevel@tonic-gate encrypt_request_end(); 219*0Sstevel@tonic-gate enc_passwd = 0; 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate } 222*0Sstevel@tonic-gate } 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate void 226*0Sstevel@tonic-gate setcommandmode() 227*0Sstevel@tonic-gate { 228*0Sstevel@tonic-gate TerminalNewMode(-1); 229*0Sstevel@tonic-gate } 230