xref: /original-bsd/usr.bin/telnet/terminal.c (revision b7bd495c)
1 #include <arpa/telnet.h>
2 #include <sys/types.h>
3 
4 #include "ring.h"
5 
6 #include "externs.h"
7 #include "types.h"
8 
9 Ring	ttyoring, ttyiring;
10 char	ttyobuf[2*BUFSIZ], ttyibuf[BUFSIZ];
11 
12 char
13     termEofChar,
14     termEraseChar,
15     termFlushChar,
16     termIntChar,
17     termKillChar,
18     termLiteralNextChar,
19     termQuitChar;
20 
21 /*
22  * initialize the terminal data structures.
23  */
24 
25 init_terminal()
26 {
27     ring_init(&ttyoring, ttyobuf, sizeof ttyobuf);
28     ring_init(&ttyiring, ttyibuf, sizeof ttyibuf);
29     autoflush = TerminalAutoFlush();
30 }
31 
32 
33 /*
34  *		Send as much data as possible to the terminal.
35  *
36  *		The return value indicates whether we did any
37  *	useful work.
38  */
39 
40 
41 int
42 ttyflush(drop)
43 int drop;
44 {
45     register int n, n0, n1;
46 
47     n0 = ring_full_count(&ttyoring);
48     if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) {
49 	if (drop) {
50 	    TerminalFlushOutput();
51 	    /* we leave 'n' alone! */
52 	} else {
53 	    n = TerminalWrite(tout, ttyoring.consume, n);
54 	}
55     }
56     if (n > 0) {
57 	/*
58 	 * If we wrote everything, and the full count is
59 	 * larger than what we wrote, then write the
60 	 * rest of the buffer.
61 	 */
62 	if (n1 == n && n0 > n) {
63 		n1 = n0 - n;
64 		if (!drop)
65 			n1 = TerminalWrite(tout, ttyoring.bottom, n1);
66 		n += n1;
67 	}
68 	ring_consumed(&ttyoring, n);
69     }
70     return n > 0;
71 }
72 
73 
74 /*
75  * These routines decides on what the mode should be (based on the values
76  * of various global variables).
77  */
78 
79 
80 int
81 getconnmode()
82 {
83     static char newmode[16] =
84 			{ 4, 5, 3, 3, 2, 2, 1, 1, 6, 6, 6, 6, 6, 6, 6, 6 };
85     int modeindex = 0;
86 
87     if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) {
88 	modeindex += 1;
89     }
90     if (hisopts[TELOPT_ECHO]) {
91 	modeindex += 2;
92     }
93     if (hisopts[TELOPT_SGA]) {
94 	modeindex += 4;
95     }
96     if (In3270) {
97 	modeindex += 8;
98     }
99     return newmode[modeindex];
100 }
101 
102 void
103 setconnmode()
104 {
105     TerminalNewMode(tin, tout, getconnmode());
106 }
107 
108 
109 void
110 setcommandmode()
111 {
112     TerminalNewMode(tin, tout, 0);
113 }
114