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