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