xref: /csrg-svn/usr.bin/telnet/terminal.c (revision 34848)
133686Sbostic /*
233686Sbostic  * Copyright (c) 1988 Regents of the University of California.
333686Sbostic  * All rights reserved.
433686Sbostic  *
533686Sbostic  * Redistribution and use in source and binary forms are permitted
633686Sbostic  * provided that this notice is preserved and that due credit is given
733686Sbostic  * to the University of California at Berkeley. The name of the University
833686Sbostic  * may not be used to endorse or promote products derived from this
933686Sbostic  * software without specific prior written permission. This software
1033686Sbostic  * is provided ``as is'' without express or implied warranty.
1133686Sbostic  */
1233686Sbostic 
1333686Sbostic #ifndef lint
14*34848Sminshall static char sccsid[] = "@(#)terminal.c	1.12 (Berkeley) 06/27/88";
1533686Sbostic #endif /* not lint */
1633686Sbostic 
1732148Sminshall #include <arpa/telnet.h>
1832381Sminshall #include <sys/types.h>
1932148Sminshall 
2032381Sminshall #include "ring.h"
2132381Sminshall 
2232148Sminshall #include "externs.h"
2332148Sminshall #include "types.h"
2432148Sminshall 
2532531Sminshall Ring	ttyoring, ttyiring;
2632531Sminshall char	ttyobuf[2*BUFSIZ], ttyibuf[BUFSIZ];
2732148Sminshall 
2832148Sminshall char
2932148Sminshall     termEofChar,
3032148Sminshall     termEraseChar,
3132148Sminshall     termFlushChar,
3232148Sminshall     termIntChar,
3332148Sminshall     termKillChar,
34*34848Sminshall #if	defined(MSDOS)
3532148Sminshall     termLiteralNextChar,
36*34848Sminshall #endif	/* defined(MSDOS) */
3732148Sminshall     termQuitChar;
3832148Sminshall 
3932148Sminshall /*
4032148Sminshall  * initialize the terminal data structures.
4132148Sminshall  */
4232148Sminshall 
4332148Sminshall init_terminal()
4432148Sminshall {
45*34848Sminshall     if (ring_init(&ttyoring, ttyobuf, sizeof ttyobuf) != 1) {
46*34848Sminshall 	exit(1);
47*34848Sminshall     }
48*34848Sminshall     if (ring_init(&ttyiring, ttyibuf, sizeof ttyibuf) != 1) {
49*34848Sminshall 	exit(1);
50*34848Sminshall     }
5132148Sminshall     autoflush = TerminalAutoFlush();
5232148Sminshall }
5332148Sminshall 
5432148Sminshall 
5532148Sminshall /*
5632148Sminshall  *		Send as much data as possible to the terminal.
5732148Sminshall  *
5832148Sminshall  *		The return value indicates whether we did any
5932148Sminshall  *	useful work.
6032148Sminshall  */
6132148Sminshall 
6232148Sminshall 
6332148Sminshall int
6432257Sminshall ttyflush(drop)
6532257Sminshall int drop;
6632148Sminshall {
6732667Sminshall     register int n, n0, n1;
6832148Sminshall 
6932667Sminshall     n0 = ring_full_count(&ttyoring);
7032667Sminshall     if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) {
7132257Sminshall 	if (drop) {
7232148Sminshall 	    TerminalFlushOutput();
7332148Sminshall 	    /* we leave 'n' alone! */
7432257Sminshall 	} else {
7533286Sminshall 	    n = TerminalWrite(ttyoring.consume, n);
7632148Sminshall 	}
7732148Sminshall     }
7832667Sminshall     if (n > 0) {
7932667Sminshall 	/*
8032667Sminshall 	 * If we wrote everything, and the full count is
8132667Sminshall 	 * larger than what we wrote, then write the
8232667Sminshall 	 * rest of the buffer.
8332667Sminshall 	 */
8432667Sminshall 	if (n1 == n && n0 > n) {
8532667Sminshall 		n1 = n0 - n;
8632667Sminshall 		if (!drop)
8733286Sminshall 			n1 = TerminalWrite(ttyoring.bottom, n1);
8832667Sminshall 		n += n1;
8932667Sminshall 	}
9032528Sminshall 	ring_consumed(&ttyoring, n);
9132148Sminshall     }
9232148Sminshall     return n > 0;
9332148Sminshall }
9432148Sminshall 
9532148Sminshall 
9632148Sminshall /*
9732148Sminshall  * These routines decides on what the mode should be (based on the values
9832148Sminshall  * of various global variables).
9932148Sminshall  */
10032148Sminshall 
10132148Sminshall 
10232148Sminshall int
10332148Sminshall getconnmode()
10432148Sminshall {
10532148Sminshall     static char newmode[16] =
10632148Sminshall 			{ 4, 5, 3, 3, 2, 2, 1, 1, 6, 6, 6, 6, 6, 6, 6, 6 };
10732148Sminshall     int modeindex = 0;
10832148Sminshall 
10932148Sminshall     if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) {
11032148Sminshall 	modeindex += 1;
11132148Sminshall     }
11232148Sminshall     if (hisopts[TELOPT_ECHO]) {
11332148Sminshall 	modeindex += 2;
11432148Sminshall     }
11532148Sminshall     if (hisopts[TELOPT_SGA]) {
11632148Sminshall 	modeindex += 4;
11732148Sminshall     }
11832148Sminshall     if (In3270) {
11932148Sminshall 	modeindex += 8;
12032148Sminshall     }
12132148Sminshall     return newmode[modeindex];
12232148Sminshall }
12332148Sminshall 
12432148Sminshall void
12532148Sminshall setconnmode()
12632148Sminshall {
12733286Sminshall     TerminalNewMode(getconnmode());
12832148Sminshall }
12932148Sminshall 
13032148Sminshall 
13132148Sminshall void
13232148Sminshall setcommandmode()
13332148Sminshall {
13433286Sminshall     TerminalNewMode(0);
13532148Sminshall }
136