Commit | Line | Data |
---|---|---|
897ce52e | 1 | /* |
7a26b73d | 2 | * Copyright (c) 1988, 1990 Regents of the University of California. |
897ce52e KB |
3 | * All rights reserved. |
4 | * | |
cb956e54 | 5 | * %sccs.include.redist.c% |
897ce52e KB |
6 | */ |
7 | ||
8 | #ifndef lint | |
e2753f3d | 9 | static char sccsid[] = "@(#)terminal.c 5.5 (Berkeley) %G%"; |
897ce52e KB |
10 | #endif /* not lint */ |
11 | ||
674388c7 | 12 | #include <arpa/telnet.h> |
115a5494 GM |
13 | #include <sys/types.h> |
14 | ||
15 | #include "ring.h" | |
674388c7 GM |
16 | |
17 | #include "externs.h" | |
18 | #include "types.h" | |
19 | ||
15d31b7e DB |
20 | Ring ttyoring, ttyiring; |
21 | unsigned char ttyobuf[2*BUFSIZ], ttyibuf[BUFSIZ]; | |
674388c7 | 22 | |
40cc3fc2 GM |
23 | int termdata; /* Debugging flag */ |
24 | ||
6055a9f6 | 25 | #ifdef USE_TERMIO |
7a26b73d | 26 | # ifndef VDISCARD |
4a8a7128 | 27 | cc_t termFlushChar; |
7daa10bf PB |
28 | # endif |
29 | # ifndef VLNEXT | |
4a8a7128 | 30 | cc_t termLiteralNextChar; |
7daa10bf PB |
31 | # endif |
32 | # ifndef VSUSP | |
4a8a7128 | 33 | cc_t termSuspChar; |
7daa10bf PB |
34 | # endif |
35 | # ifndef VWERASE | |
4a8a7128 | 36 | cc_t termWerasChar; |
7daa10bf PB |
37 | # endif |
38 | # ifndef VREPRINT | |
4a8a7128 | 39 | cc_t termRprntChar; |
7daa10bf PB |
40 | # endif |
41 | # ifndef VSTART | |
4a8a7128 | 42 | cc_t termStartChar; |
7daa10bf PB |
43 | # endif |
44 | # ifndef VSTOP | |
4a8a7128 | 45 | cc_t termStopChar; |
7daa10bf | 46 | # endif |
87b60187 | 47 | # ifndef VEOL |
4a8a7128 | 48 | cc_t termForw1Char; |
87b60187 PB |
49 | # endif |
50 | # ifndef VEOL2 | |
4a8a7128 | 51 | cc_t termForw2Char; |
87b60187 | 52 | # endif |
7a26b73d PB |
53 | # ifndef VSTATUS |
54 | cc_t termAytChar; | |
55 | # endif | |
87b60187 | 56 | #else |
4a8a7128 | 57 | cc_t termForw2Char; |
7a26b73d | 58 | cc_t termAytChar; |
6055a9f6 | 59 | #endif |
674388c7 GM |
60 | |
61 | /* | |
62 | * initialize the terminal data structures. | |
63 | */ | |
64 | ||
15d31b7e | 65 | void |
674388c7 GM |
66 | init_terminal() |
67 | { | |
80a47e22 GM |
68 | if (ring_init(&ttyoring, ttyobuf, sizeof ttyobuf) != 1) { |
69 | exit(1); | |
70 | } | |
71 | if (ring_init(&ttyiring, ttyibuf, sizeof ttyibuf) != 1) { | |
72 | exit(1); | |
73 | } | |
674388c7 GM |
74 | autoflush = TerminalAutoFlush(); |
75 | } | |
76 | ||
77 | ||
78 | /* | |
79 | * Send as much data as possible to the terminal. | |
80 | * | |
6055a9f6 PB |
81 | * Return value: |
82 | * -1: No useful work done, data waiting to go out. | |
83 | * 0: No data was waiting, so nothing was done. | |
84 | * 1: All waiting data was written out. | |
85 | * n: All data - n was written out. | |
674388c7 GM |
86 | */ |
87 | ||
88 | ||
15d31b7e | 89 | int |
d732be39 | 90 | ttyflush(drop) |
15d31b7e | 91 | int drop; |
674388c7 | 92 | { |
ad1c581e | 93 | register int n, n0, n1; |
674388c7 | 94 | |
ad1c581e GM |
95 | n0 = ring_full_count(&ttyoring); |
96 | if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) { | |
d732be39 | 97 | if (drop) { |
674388c7 GM |
98 | TerminalFlushOutput(); |
99 | /* we leave 'n' alone! */ | |
d732be39 | 100 | } else { |
448f9c06 | 101 | n = TerminalWrite(ttyoring.consume, n); |
674388c7 GM |
102 | } |
103 | } | |
ad1c581e | 104 | if (n > 0) { |
40cc3fc2 GM |
105 | if (termdata && n) { |
106 | Dump('>', ttyoring.consume, n); | |
107 | } | |
ad1c581e GM |
108 | /* |
109 | * If we wrote everything, and the full count is | |
110 | * larger than what we wrote, then write the | |
111 | * rest of the buffer. | |
112 | */ | |
113 | if (n1 == n && n0 > n) { | |
114 | n1 = n0 - n; | |
115 | if (!drop) | |
448f9c06 | 116 | n1 = TerminalWrite(ttyoring.bottom, n1); |
ad1c581e GM |
117 | n += n1; |
118 | } | |
8b6750f5 | 119 | ring_consumed(&ttyoring, n); |
674388c7 | 120 | } |
6055a9f6 PB |
121 | if (n < 0) |
122 | return -1; | |
123 | if (n == n0) { | |
124 | if (n0) | |
125 | return -1; | |
126 | return 0; | |
127 | } | |
128 | return n0 - n + 1; | |
674388c7 GM |
129 | } |
130 | ||
674388c7 | 131 | \f |
674388c7 GM |
132 | /* |
133 | * These routines decides on what the mode should be (based on the values | |
134 | * of various global variables). | |
135 | */ | |
136 | ||
137 | ||
15d31b7e | 138 | int |
674388c7 GM |
139 | getconnmode() |
140 | { | |
6055a9f6 PB |
141 | extern int linemode; |
142 | int mode = 0; | |
143 | #ifdef KLUDGELINEMODE | |
144 | extern int kludgelinemode; | |
145 | #endif | |
146 | ||
147 | if (In3270) | |
148 | return(MODE_FLOW); | |
149 | ||
150 | if (my_want_state_is_dont(TELOPT_ECHO)) | |
151 | mode |= MODE_ECHO; | |
152 | ||
153 | if (localflow) | |
154 | mode |= MODE_FLOW; | |
155 | ||
7daa10bf PB |
156 | if (my_want_state_is_will(TELOPT_BINARY)) |
157 | mode |= MODE_INBIN; | |
158 | ||
159 | if (his_want_state_is_will(TELOPT_BINARY)) | |
160 | mode |= MODE_OUTBIN; | |
161 | ||
6055a9f6 PB |
162 | #ifdef KLUDGELINEMODE |
163 | if (kludgelinemode) { | |
164 | if (my_want_state_is_dont(TELOPT_SGA)) { | |
165 | mode |= (MODE_TRAPSIG|MODE_EDIT); | |
166 | if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) { | |
167 | mode &= ~MODE_ECHO; | |
168 | } | |
169 | } | |
170 | return(mode); | |
674388c7 | 171 | } |
6055a9f6 PB |
172 | #endif |
173 | if (my_want_state_is_will(TELOPT_LINEMODE)) | |
174 | mode |= linemode; | |
175 | return(mode); | |
674388c7 GM |
176 | } |
177 | ||
15d31b7e | 178 | void |
6055a9f6 | 179 | setconnmode(force) |
15d31b7e | 180 | int force; |
674388c7 | 181 | { |
8832c633 | 182 | #ifdef ENCRYPTION |
15d31b7e | 183 | static int enc_passwd = 0; |
84ee7b62 | 184 | #endif /* ENCRYPTION */ |
15d31b7e DB |
185 | register int newmode; |
186 | ||
187 | newmode = getconnmode()|(force?MODE_FORCE:0); | |
188 | ||
189 | TerminalNewMode(newmode); | |
190 | ||
8832c633 | 191 | #ifdef ENCRYPTION |
15d31b7e DB |
192 | if ((newmode & (MODE_ECHO|MODE_EDIT)) == MODE_EDIT) { |
193 | if (my_want_state_is_will(TELOPT_ENCRYPT) | |
194 | && (enc_passwd == 0) && !encrypt_output) { | |
b7c8f459 | 195 | encrypt_request_start(0, 0); |
15d31b7e DB |
196 | enc_passwd = 1; |
197 | } | |
198 | } else { | |
199 | if (enc_passwd) { | |
200 | encrypt_request_end(); | |
201 | enc_passwd = 0; | |
202 | } | |
203 | } | |
84ee7b62 | 204 | #endif /* ENCRYPTION */ |
15d31b7e | 205 | |
674388c7 GM |
206 | } |
207 | ||
208 | ||
15d31b7e | 209 | void |
674388c7 GM |
210 | setcommandmode() |
211 | { | |
6055a9f6 | 212 | TerminalNewMode(-1); |
674388c7 | 213 | } |