2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Created: Tue Mar 21 15:59:15 1995 ylo
6 * Encoding and decoding of terminal modes in a portable way.
7 * Much of the format is defined in ttymodes.h; it is included multiple times
8 * into this file with the appropriate macro definitions to generate the
19 #define TTY_OP_ISPEED 192 /* int follows */
20 #define TTY_OP_OSPEED 193 /* int follows */
23 * Converts POSIX speed_t to a baud rate. The values of the
24 * constants for speed_t are not themselves portable.
27 speed_to_baud(speed_t speed)
113 * Converts a numeric baud rate to a POSIX speed_t.
116 baud_to_speed(int baud)
202 * Encodes terminal modes for the terminal referenced by fd
203 * in a portable manner, and appends the modes to a packet
207 tty_make_modes(int fd)
213 if (tcgetattr(fd, &tio) < 0) {
214 packet_put_char(TTY_OP_END);
215 log("tcgetattr: %.100s", strerror(errno));
218 /* Store input and output baud rates. */
219 baud = speed_to_baud(cfgetospeed(&tio));
220 packet_put_char(TTY_OP_OSPEED);
221 packet_put_int(baud);
222 baud = speed_to_baud(cfgetispeed(&tio));
223 packet_put_char(TTY_OP_ISPEED);
224 packet_put_int(baud);
226 /* Store values of mode flags. */
227 #define TTYCHAR(NAME, OP) \
228 packet_put_char(OP); packet_put_char(tio.c_cc[NAME]);
229 #define TTYMODE(NAME, FIELD, OP) \
230 packet_put_char(OP); packet_put_char((tio.FIELD & NAME) != 0);
231 #define SGTTYCHAR(NAME, OP)
232 #define SGTTYMODE(NAME, FIELD, OP)
233 #define SGTTYMODEN(NAME, FIELD, OP)
235 #include "ttymodes.h"
243 /* Mark end of mode data. */
244 packet_put_char(TTY_OP_END);
248 * Decodes terminal modes for the terminal referenced by fd in a portable
249 * manner from a packet being read.
252 tty_parse_modes(int fd, int *n_bytes_ptr)
260 * Get old attributes for the terminal. We will modify these
261 * flags. I am hoping that if there are any machine-specific
262 * modes, they will initially have reasonable values.
264 if (tcgetattr(fd, &tio) < 0)
269 opcode = packet_get_char();
276 baud = packet_get_int();
277 if (failure != -1 && cfsetispeed(&tio, baud_to_speed(baud)) < 0)
278 error("cfsetispeed failed for %d", baud);
283 baud = packet_get_int();
284 if (failure != -1 && cfsetospeed(&tio, baud_to_speed(baud)) < 0)
285 error("cfsetospeed failed for %d", baud);
288 #define TTYCHAR(NAME, OP) \
291 tio.c_cc[NAME] = packet_get_char(); \
293 #define TTYMODE(NAME, FIELD, OP) \
296 if (packet_get_char()) \
299 tio.FIELD &= ~NAME; \
301 #define SGTTYCHAR(NAME, OP)
302 #define SGTTYMODE(NAME, FIELD, OP)
303 #define SGTTYMODEN(NAME, FIELD, OP)
305 #include "ttymodes.h"
314 debug("Ignoring unsupported tty mode opcode %d (0x%x)",
317 * Opcodes 0 to 127 are defined to have
318 * a one-byte argument.
320 if (opcode >= 0 && opcode < 128) {
322 (void) packet_get_char();
326 * Opcodes 128 to 159 are defined to have
327 * an integer argument.
329 if (opcode >= 128 && opcode < 160) {
331 (void) packet_get_int();
336 * It is a truly undefined opcode (160 to 255).
337 * We have no idea about its arguments. So we
338 * must stop parsing. Note that some data may be
339 * left in the packet; hopefully there is nothing
340 * more coming after the mode data.
342 log("parse_tty_modes: unknown opcode %d", opcode);
343 packet_integrity_check(0, 1, SSH_CMSG_REQUEST_PTY);
349 if (*n_bytes_ptr != n_bytes) {
350 *n_bytes_ptr = n_bytes;
351 return; /* Don't process bytes passed */
354 return; /* Packet parsed ok but tty stuff failed */
356 /* Set the new modes for the terminal. */
357 if (tcsetattr(fd, TCSANOW, &tio) < 0)
358 log("Setting tty modes failed: %.100s", strerror(errno));