xref: /original-bsd/usr.bin/telnet/terminal.c (revision f052b07a)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)terminal.c	1.13 (Berkeley) 06/29/88";
20 #endif /* not lint */
21 
22 #include <arpa/telnet.h>
23 #include <sys/types.h>
24 
25 #include "ring.h"
26 
27 #include "externs.h"
28 #include "types.h"
29 
30 Ring	ttyoring, ttyiring;
31 char	ttyobuf[2*BUFSIZ], ttyibuf[BUFSIZ];
32 
33 char
34     termEofChar,
35     termEraseChar,
36     termFlushChar,
37     termIntChar,
38     termKillChar,
39 #if	defined(MSDOS)
40     termLiteralNextChar,
41 #endif	/* defined(MSDOS) */
42     termQuitChar;
43 
44 /*
45  * initialize the terminal data structures.
46  */
47 
48 init_terminal()
49 {
50     if (ring_init(&ttyoring, ttyobuf, sizeof ttyobuf) != 1) {
51 	exit(1);
52     }
53     if (ring_init(&ttyiring, ttyibuf, sizeof ttyibuf) != 1) {
54 	exit(1);
55     }
56     autoflush = TerminalAutoFlush();
57 }
58 
59 
60 /*
61  *		Send as much data as possible to the terminal.
62  *
63  *		The return value indicates whether we did any
64  *	useful work.
65  */
66 
67 
68 int
69 ttyflush(drop)
70 int drop;
71 {
72     register int n, n0, n1;
73 
74     n0 = ring_full_count(&ttyoring);
75     if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) {
76 	if (drop) {
77 	    TerminalFlushOutput();
78 	    /* we leave 'n' alone! */
79 	} else {
80 	    n = TerminalWrite(ttyoring.consume, n);
81 	}
82     }
83     if (n > 0) {
84 	/*
85 	 * If we wrote everything, and the full count is
86 	 * larger than what we wrote, then write the
87 	 * rest of the buffer.
88 	 */
89 	if (n1 == n && n0 > n) {
90 		n1 = n0 - n;
91 		if (!drop)
92 			n1 = TerminalWrite(ttyoring.bottom, n1);
93 		n += n1;
94 	}
95 	ring_consumed(&ttyoring, n);
96     }
97     return n > 0;
98 }
99 
100 
101 /*
102  * These routines decides on what the mode should be (based on the values
103  * of various global variables).
104  */
105 
106 
107 int
108 getconnmode()
109 {
110     static char newmode[16] =
111 			{ 4, 5, 3, 3, 2, 2, 1, 1, 6, 6, 6, 6, 6, 6, 6, 6 };
112     int modeindex = 0;
113 
114     if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) {
115 	modeindex += 1;
116     }
117     if (hisopts[TELOPT_ECHO]) {
118 	modeindex += 2;
119     }
120     if (hisopts[TELOPT_SGA]) {
121 	modeindex += 4;
122     }
123     if (In3270) {
124 	modeindex += 8;
125     }
126     return newmode[modeindex];
127 }
128 
129 void
130 setconnmode()
131 {
132     TerminalNewMode(getconnmode());
133 }
134 
135 
136 void
137 setcommandmode()
138 {
139     TerminalNewMode(0);
140 }
141