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