xref: /csrg-svn/usr.bin/telnet/terminal.c (revision 38208)
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
634898Sbostic  * provided that the above copyright notice and this paragraph are
734898Sbostic  * duplicated in all such forms and that any documentation,
834898Sbostic  * advertising materials, and other materials related to such
934898Sbostic  * distribution and use acknowledge that the software was developed
1034898Sbostic  * by the University of California, Berkeley.  The name of the
1134898Sbostic  * University may not be used to endorse or promote products derived
1234898Sbostic  * from this software without specific prior written permission.
1334898Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434898Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534898Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1633686Sbostic  */
1733686Sbostic 
1833686Sbostic #ifndef lint
19*38208Sminshall static char sccsid[] = "@(#)terminal.c	1.15 (Berkeley) 05/30/89";
2033686Sbostic #endif /* not lint */
2133686Sbostic 
2232148Sminshall #include <arpa/telnet.h>
2332381Sminshall #include <sys/types.h>
2432148Sminshall 
2532381Sminshall #include "ring.h"
2632381Sminshall 
2732148Sminshall #include "externs.h"
2832148Sminshall #include "types.h"
2932148Sminshall 
3032531Sminshall Ring	ttyoring, ttyiring;
3132531Sminshall char	ttyobuf[2*BUFSIZ], ttyibuf[BUFSIZ];
3232148Sminshall 
33*38208Sminshall int termdata;			/* Debugging flag */
34*38208Sminshall 
3532148Sminshall char
3632148Sminshall     termEofChar,
3732148Sminshall     termEraseChar,
3832148Sminshall     termFlushChar,
3932148Sminshall     termIntChar,
4032148Sminshall     termKillChar,
4134848Sminshall #if	defined(MSDOS)
4232148Sminshall     termLiteralNextChar,
4334848Sminshall #endif	/* defined(MSDOS) */
4432148Sminshall     termQuitChar;
4532148Sminshall 
4632148Sminshall /*
4732148Sminshall  * initialize the terminal data structures.
4832148Sminshall  */
4932148Sminshall 
5032148Sminshall init_terminal()
5132148Sminshall {
5234848Sminshall     if (ring_init(&ttyoring, ttyobuf, sizeof ttyobuf) != 1) {
5334848Sminshall 	exit(1);
5434848Sminshall     }
5534848Sminshall     if (ring_init(&ttyiring, ttyibuf, sizeof ttyibuf) != 1) {
5634848Sminshall 	exit(1);
5734848Sminshall     }
5832148Sminshall     autoflush = TerminalAutoFlush();
5932148Sminshall }
6032148Sminshall 
6132148Sminshall 
6232148Sminshall /*
6332148Sminshall  *		Send as much data as possible to the terminal.
6432148Sminshall  *
6532148Sminshall  *		The return value indicates whether we did any
6632148Sminshall  *	useful work.
6732148Sminshall  */
6832148Sminshall 
6932148Sminshall 
7032148Sminshall int
7132257Sminshall ttyflush(drop)
7232257Sminshall int drop;
7332148Sminshall {
7432667Sminshall     register int n, n0, n1;
7532148Sminshall 
7632667Sminshall     n0 = ring_full_count(&ttyoring);
7732667Sminshall     if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) {
7832257Sminshall 	if (drop) {
7932148Sminshall 	    TerminalFlushOutput();
8032148Sminshall 	    /* we leave 'n' alone! */
8132257Sminshall 	} else {
8233286Sminshall 	    n = TerminalWrite(ttyoring.consume, n);
8332148Sminshall 	}
8432148Sminshall     }
8532667Sminshall     if (n > 0) {
86*38208Sminshall 	if (termdata && n) {
87*38208Sminshall 	    Dump('>', ttyoring.consume, n);
88*38208Sminshall 	}
8932667Sminshall 	/*
9032667Sminshall 	 * If we wrote everything, and the full count is
9132667Sminshall 	 * larger than what we wrote, then write the
9232667Sminshall 	 * rest of the buffer.
9332667Sminshall 	 */
9432667Sminshall 	if (n1 == n && n0 > n) {
9532667Sminshall 		n1 = n0 - n;
9632667Sminshall 		if (!drop)
9733286Sminshall 			n1 = TerminalWrite(ttyoring.bottom, n1);
9832667Sminshall 		n += n1;
9932667Sminshall 	}
10032528Sminshall 	ring_consumed(&ttyoring, n);
10132148Sminshall     }
10232148Sminshall     return n > 0;
10332148Sminshall }
10432148Sminshall 
10532148Sminshall 
10632148Sminshall /*
10732148Sminshall  * These routines decides on what the mode should be (based on the values
10832148Sminshall  * of various global variables).
10932148Sminshall  */
11032148Sminshall 
11132148Sminshall 
11232148Sminshall int
11332148Sminshall getconnmode()
11432148Sminshall {
11532148Sminshall     static char newmode[16] =
11632148Sminshall 			{ 4, 5, 3, 3, 2, 2, 1, 1, 6, 6, 6, 6, 6, 6, 6, 6 };
11732148Sminshall     int modeindex = 0;
11832148Sminshall 
11932148Sminshall     if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) {
12032148Sminshall 	modeindex += 1;
12132148Sminshall     }
12237226Sminshall     if (should_he(TELOPT_ECHO)) {
12332148Sminshall 	modeindex += 2;
12432148Sminshall     }
12537226Sminshall     if (should_he(TELOPT_SGA)) {
12632148Sminshall 	modeindex += 4;
12732148Sminshall     }
12832148Sminshall     if (In3270) {
12932148Sminshall 	modeindex += 8;
13032148Sminshall     }
13132148Sminshall     return newmode[modeindex];
13232148Sminshall }
13332148Sminshall 
13432148Sminshall void
13532148Sminshall setconnmode()
13632148Sminshall {
13733286Sminshall     TerminalNewMode(getconnmode());
13832148Sminshall }
13932148Sminshall 
14032148Sminshall 
14132148Sminshall void
14232148Sminshall setcommandmode()
14332148Sminshall {
14433286Sminshall     TerminalNewMode(0);
14532148Sminshall }
146