2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Encoding and decoding of terminal modes in a portable way.
6 * Much of the format is defined in ttymodes.h; it is included multiple times
7 * into this file with the appropriate macro definitions to generate the
10 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
18 RCSID("$OpenBSD: ttymodes.c,v 1.8 2000/09/07 20:27:55 deraadt Exp $");
24 #define TTY_OP_ISPEED 192 /* int follows */
25 #define TTY_OP_OSPEED 193 /* int follows */
28 * Converts POSIX speed_t to a baud rate. The values of the
29 * constants for speed_t are not themselves portable.
32 speed_to_baud(speed_t speed)
118 * Converts a numeric baud rate to a POSIX speed_t.
121 baud_to_speed(int baud)
207 * Encodes terminal modes for the terminal referenced by fd
208 * in a portable manner, and appends the modes to a packet
212 tty_make_modes(int fd)
217 if (tcgetattr(fd, &tio) < 0) {
218 packet_put_char(TTY_OP_END);
219 log("tcgetattr: %.100s", strerror(errno));
222 /* Store input and output baud rates. */
223 baud = speed_to_baud(cfgetospeed(&tio));
224 packet_put_char(TTY_OP_OSPEED);
225 packet_put_int(baud);
226 baud = speed_to_baud(cfgetispeed(&tio));
227 packet_put_char(TTY_OP_ISPEED);
228 packet_put_int(baud);
230 /* Store values of mode flags. */
231 #define TTYCHAR(NAME, OP) \
232 packet_put_char(OP); packet_put_char(tio.c_cc[NAME]);
233 #define TTYMODE(NAME, FIELD, OP) \
234 packet_put_char(OP); packet_put_char((tio.FIELD & NAME) != 0);
235 #define SGTTYCHAR(NAME, OP)
236 #define SGTTYMODE(NAME, FIELD, OP)
237 #define SGTTYMODEN(NAME, FIELD, OP)
239 #include "ttymodes.h"
247 /* Mark end of mode data. */
248 packet_put_char(TTY_OP_END);
252 * Decodes terminal modes for the terminal referenced by fd in a portable
253 * manner from a packet being read.
256 tty_parse_modes(int fd, int *n_bytes_ptr)
264 * Get old attributes for the terminal. We will modify these
265 * flags. I am hoping that if there are any machine-specific
266 * modes, they will initially have reasonable values.
268 if (tcgetattr(fd, &tio) < 0)
273 opcode = packet_get_char();
280 baud = packet_get_int();
281 if (failure != -1 && cfsetispeed(&tio, baud_to_speed(baud)) < 0)
282 error("cfsetispeed failed for %d", baud);
287 baud = packet_get_int();
288 if (failure != -1 && cfsetospeed(&tio, baud_to_speed(baud)) < 0)
289 error("cfsetospeed failed for %d", baud);
292 #define TTYCHAR(NAME, OP) \
295 tio.c_cc[NAME] = packet_get_char(); \
297 #define TTYMODE(NAME, FIELD, OP) \
300 if (packet_get_char()) \
303 tio.FIELD &= ~NAME; \
305 #define SGTTYCHAR(NAME, OP)
306 #define SGTTYMODE(NAME, FIELD, OP)
307 #define SGTTYMODEN(NAME, FIELD, OP)
309 #include "ttymodes.h"
318 debug("Ignoring unsupported tty mode opcode %d (0x%x)",
321 * Opcodes 0 to 127 are defined to have
322 * a one-byte argument.
324 if (opcode >= 0 && opcode < 128) {
326 (void) packet_get_char();
330 * Opcodes 128 to 159 are defined to have
331 * an integer argument.
333 if (opcode >= 128 && opcode < 160) {
335 (void) packet_get_int();
340 * It is a truly undefined opcode (160 to 255).
341 * We have no idea about its arguments. So we
342 * must stop parsing. Note that some data may be
343 * left in the packet; hopefully there is nothing
344 * more coming after the mode data.
346 log("parse_tty_modes: unknown opcode %d", opcode);
347 packet_integrity_check(0, 1, SSH_CMSG_REQUEST_PTY);
353 if (*n_bytes_ptr != n_bytes) {
354 *n_bytes_ptr = n_bytes;
355 return; /* Don't process bytes passed */
358 return; /* Packet parsed ok but tty stuff failed */
360 /* Set the new modes for the terminal. */
361 if (tcsetattr(fd, TCSANOW, &tio) < 0)
362 log("Setting tty modes failed: %.100s", strerror(errno));