Debian Codegraph @

telnet/terminal.c

raw
1 /*
2 Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1988, 1990, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include <stdlib.h>
51
52 #include <arpa/telnet.h>
53 #include <sys/types.h>
54
55 #include "ring.h"
56
57 #include "externs.h"
58 #include "types.h"
59
60 #ifdef ENCRYPTION
61 # include <libtelnet/encrypt.h>
62 #endif
63
64 Ring ttyoring, ttyiring;
65 unsigned char ttyobuf[2 * BUFSIZ], ttyibuf[BUFSIZ];
66
67 int termdata; /* Debugging flag */
68
69 #ifdef USE_TERMIO
70 # ifndef VDISCARD
71 cc_t termFlushChar;
72 # endif
73 # ifndef VLNEXT
74 cc_t termLiteralNextChar;
75 # endif
76 # ifndef VSUSP
77 cc_t termSuspChar;
78 # endif
79 # ifndef VWERASE
80 cc_t termWerasChar;
81 # endif
82 # ifndef VREPRINT
83 cc_t termRprntChar;
84 # endif
85 # ifndef VSTART
86 cc_t termStartChar;
87 # endif
88 # ifndef VSTOP
89 cc_t termStopChar;
90 # endif
91 # ifndef VEOL
92 cc_t termForw1Char;
93 # endif
94 # ifndef VEOL2
95 cc_t termForw2Char;
96 # endif
97 # ifndef VSTATUS
98 cc_t termAytChar;
99 # endif
100 #else
101 cc_t termForw2Char;
102 cc_t termAytChar;
103 #endif
104
105 /*
106 * initialize the terminal data structures.
107 */
108
109 void
110 init_terminal (void)
111 {
112 if (ring_init (&ttyoring, ttyobuf, sizeof ttyobuf) != 1)
113 {
114 exit (EXIT_FAILURE);
115 }
116 if (ring_init (&ttyiring, ttyibuf, sizeof ttyibuf) != 1)
117 {
118 exit (EXIT_FAILURE);
119 }
120 autoflush = TerminalAutoFlush ();
121 }
122
123
124 /*
125 * Send as much data as possible to the terminal.
126 *
127 * Return value:
128 * -1: No useful work done, data waiting to go out.
129 * 0: No data was waiting, so nothing was done.
130 * 1: All waiting data was written out.
131 * n: All data - n was written out.
132 */
133
134
135 int
136 ttyflush (int drop)
137 {
138 int n, n0, n1;
139
140 n0 = ring_full_count (&ttyoring);
141 if ((n1 = n = ring_full_consecutive (&ttyoring)) > 0)
142 {
143 if (drop)
144 {
145 TerminalFlushOutput ();
146 /* we leave 'n' alone! */
147 }
148 else
149 {
150 n = TerminalWrite ((char *) ttyoring.consume, n);
151 }
152 }
153 if (n > 0)
154 {
155 if (termdata && n)
156 {
157 Dump ('>', ttyoring.consume, n);
158 }
159 /*
160 * If we wrote everything, and the full count is
161 * larger than what we wrote, then write the
162 * rest of the buffer.
163 */
164 if (n1 == n && n0 > n)
165 {
166 n1 = n0 - n;
167 if (!drop)
168 n1 = TerminalWrite ((char *) ttyoring.bottom, n1);
169 if (n1 > 0)
170 n += n1;
171 }
172 ring_consumed (&ttyoring, n);
173 }
174 if (n < 0)
175 return -1;
176 if (n == n0)
177 {
178 if (n0)
179 return -1;
180 return 0;
181 }
182 return n0 - n + 1;
183 }
184
185
186 /*
187 * These routines decides on what the mode should be (based on the values
188 * of various global variables).
189 */
190
191
192 int
193 getconnmode (void)
194 {
195 extern int linemode;
196 int mode = 0;
197 #ifdef KLUDGELINEMODE
198 extern int kludgelinemode;
199 #endif
200
201 if (my_want_state_is_dont (TELOPT_ECHO))
202 mode |= MODE_ECHO;
203
204 if (localflow)
205 mode |= MODE_FLOW;
206
207 if (my_want_state_is_will (TELOPT_BINARY))
208 mode |= MODE_INBIN;
209
210 if (his_want_state_is_will (TELOPT_BINARY))
211 mode |= MODE_OUTBIN;
212
213 #ifdef KLUDGELINEMODE
214 if (kludgelinemode)
215 {
216 if (my_want_state_is_dont (TELOPT_SGA))
217 {
218 mode |= (MODE_TRAPSIG | MODE_EDIT);
219 if (dontlecho && (clocks.echotoggle > clocks.modenegotiated))
220 {
221 mode &= ~MODE_ECHO;
222 }
223 }
224 return (mode);
225 }
226 #endif
227 if (my_want_state_is_will (TELOPT_LINEMODE))
228 mode |= linemode;
229 return (mode);
230 }
231
232 void
233 setconnmode (int force)
234 {
235 #ifdef ENCRYPTION
236 static int enc_passwd = 0;
237 #endif /* ENCRYPTION */
238 int newmode;
239
240 newmode = getconnmode () | (force ? MODE_FORCE : 0);
241
242 TerminalNewMode (newmode);
243
244 #ifdef ENCRYPTION
245 if ((newmode & (MODE_ECHO | MODE_EDIT)) == MODE_EDIT)
246 {
247 if (my_want_state_is_will (TELOPT_ENCRYPT)
248 && (enc_passwd == 0) && !encrypt_output)
249 {
250 encrypt_request_start (0, 0);
251 enc_passwd = 1;
252 }
253 }
254 else
255 {
256 if (enc_passwd)
257 {
258 encrypt_request_end ();
259 enc_passwd = 0;
260 }
261 }
262 #endif /* ENCRYPTION */
263
264 }
265
266
267 void
268 setcommandmode (void)
269 {
270 TerminalNewMode (-1);
271 }