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 6*34898Sbostic * provided that the above copyright notice and this paragraph are 7*34898Sbostic * duplicated in all such forms and that any documentation, 8*34898Sbostic * advertising materials, and other materials related to such 9*34898Sbostic * distribution and use acknowledge that the software was developed 10*34898Sbostic * by the University of California, Berkeley. The name of the 11*34898Sbostic * University may not be used to endorse or promote products derived 12*34898Sbostic * from this software without specific prior written permission. 13*34898Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34898Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34898Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1633686Sbostic */ 1733686Sbostic 1833686Sbostic #ifndef lint 19*34898Sbostic static char sccsid[] = "@(#)terminal.c 1.13 (Berkeley) 06/29/88"; 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 3332148Sminshall char 3432148Sminshall termEofChar, 3532148Sminshall termEraseChar, 3632148Sminshall termFlushChar, 3732148Sminshall termIntChar, 3832148Sminshall termKillChar, 3934848Sminshall #if defined(MSDOS) 4032148Sminshall termLiteralNextChar, 4134848Sminshall #endif /* defined(MSDOS) */ 4232148Sminshall termQuitChar; 4332148Sminshall 4432148Sminshall /* 4532148Sminshall * initialize the terminal data structures. 4632148Sminshall */ 4732148Sminshall 4832148Sminshall init_terminal() 4932148Sminshall { 5034848Sminshall if (ring_init(&ttyoring, ttyobuf, sizeof ttyobuf) != 1) { 5134848Sminshall exit(1); 5234848Sminshall } 5334848Sminshall if (ring_init(&ttyiring, ttyibuf, sizeof ttyibuf) != 1) { 5434848Sminshall exit(1); 5534848Sminshall } 5632148Sminshall autoflush = TerminalAutoFlush(); 5732148Sminshall } 5832148Sminshall 5932148Sminshall 6032148Sminshall /* 6132148Sminshall * Send as much data as possible to the terminal. 6232148Sminshall * 6332148Sminshall * The return value indicates whether we did any 6432148Sminshall * useful work. 6532148Sminshall */ 6632148Sminshall 6732148Sminshall 6832148Sminshall int 6932257Sminshall ttyflush(drop) 7032257Sminshall int drop; 7132148Sminshall { 7232667Sminshall register int n, n0, n1; 7332148Sminshall 7432667Sminshall n0 = ring_full_count(&ttyoring); 7532667Sminshall if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) { 7632257Sminshall if (drop) { 7732148Sminshall TerminalFlushOutput(); 7832148Sminshall /* we leave 'n' alone! */ 7932257Sminshall } else { 8033286Sminshall n = TerminalWrite(ttyoring.consume, n); 8132148Sminshall } 8232148Sminshall } 8332667Sminshall if (n > 0) { 8432667Sminshall /* 8532667Sminshall * If we wrote everything, and the full count is 8632667Sminshall * larger than what we wrote, then write the 8732667Sminshall * rest of the buffer. 8832667Sminshall */ 8932667Sminshall if (n1 == n && n0 > n) { 9032667Sminshall n1 = n0 - n; 9132667Sminshall if (!drop) 9233286Sminshall n1 = TerminalWrite(ttyoring.bottom, n1); 9332667Sminshall n += n1; 9432667Sminshall } 9532528Sminshall ring_consumed(&ttyoring, n); 9632148Sminshall } 9732148Sminshall return n > 0; 9832148Sminshall } 9932148Sminshall 10032148Sminshall 10132148Sminshall /* 10232148Sminshall * These routines decides on what the mode should be (based on the values 10332148Sminshall * of various global variables). 10432148Sminshall */ 10532148Sminshall 10632148Sminshall 10732148Sminshall int 10832148Sminshall getconnmode() 10932148Sminshall { 11032148Sminshall static char newmode[16] = 11132148Sminshall { 4, 5, 3, 3, 2, 2, 1, 1, 6, 6, 6, 6, 6, 6, 6, 6 }; 11232148Sminshall int modeindex = 0; 11332148Sminshall 11432148Sminshall if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) { 11532148Sminshall modeindex += 1; 11632148Sminshall } 11732148Sminshall if (hisopts[TELOPT_ECHO]) { 11832148Sminshall modeindex += 2; 11932148Sminshall } 12032148Sminshall if (hisopts[TELOPT_SGA]) { 12132148Sminshall modeindex += 4; 12232148Sminshall } 12332148Sminshall if (In3270) { 12432148Sminshall modeindex += 8; 12532148Sminshall } 12632148Sminshall return newmode[modeindex]; 12732148Sminshall } 12832148Sminshall 12932148Sminshall void 13032148Sminshall setconnmode() 13132148Sminshall { 13233286Sminshall TerminalNewMode(getconnmode()); 13332148Sminshall } 13432148Sminshall 13532148Sminshall 13632148Sminshall void 13732148Sminshall setcommandmode() 13832148Sminshall { 13933286Sminshall TerminalNewMode(0); 14032148Sminshall } 141