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.11 2001/03/10 15:02:05 stevesk Exp $");
25 #define TTY_OP_ISPEED 192 /* int follows */
26 #define TTY_OP_OSPEED 193 /* int follows */
29 * Converts POSIX speed_t to a baud rate. The values of the
30 * constants for speed_t are not themselves portable.
33 speed_to_baud(speed_t speed)
119 * Converts a numeric baud rate to a POSIX speed_t.
122 baud_to_speed(int baud)
208 * Encodes terminal modes for the terminal referenced by fd
209 * in a portable manner, and appends the modes to a packet
213 tty_make_modes(int fd)
218 if (tcgetattr(fd, &tio) < 0) {
219 packet_put_char(TTY_OP_END);
220 log("tcgetattr: %.100s", strerror(errno));
223 /* Store input and output baud rates. */
224 baud = speed_to_baud(cfgetospeed(&tio));
225 packet_put_char(TTY_OP_OSPEED);
226 packet_put_int(baud);
227 baud = speed_to_baud(cfgetispeed(&tio));
228 packet_put_char(TTY_OP_ISPEED);
229 packet_put_int(baud);
231 /* Store values of mode flags. */
232 #define TTYCHAR(NAME, OP) \
233 packet_put_char(OP); packet_put_char(tio.c_cc[NAME]);
234 #define TTYMODE(NAME, FIELD, OP) \
235 packet_put_char(OP); packet_put_char((tio.FIELD & NAME) != 0);
237 #include "ttymodes.h"
242 /* Mark end of mode data. */
243 packet_put_char(TTY_OP_END);
247 * Decodes terminal modes for the terminal referenced by fd in a portable
248 * manner from a packet being read.
251 tty_parse_modes(int fd, int *n_bytes_ptr)
259 * Get old attributes for the terminal. We will modify these
260 * flags. I am hoping that if there are any machine-specific
261 * modes, they will initially have reasonable values.
263 if (tcgetattr(fd, &tio) < 0)
268 opcode = packet_get_char();
275 baud = packet_get_int();
276 if (failure != -1 && cfsetispeed(&tio, baud_to_speed(baud)) < 0)
277 error("cfsetispeed failed for %d", baud);
282 baud = packet_get_int();
283 if (failure != -1 && cfsetospeed(&tio, baud_to_speed(baud)) < 0)
284 error("cfsetospeed failed for %d", baud);
287 #define TTYCHAR(NAME, OP) \
290 tio.c_cc[NAME] = packet_get_char(); \
292 #define TTYMODE(NAME, FIELD, OP) \
295 if (packet_get_char()) \
298 tio.FIELD &= ~NAME; \
301 #include "ttymodes.h"
307 debug("Ignoring unsupported tty mode opcode %d (0x%x)",
310 * Opcodes 0 to 127 are defined to have
311 * a one-byte argument.
313 if (opcode >= 0 && opcode < 128) {
315 (void) packet_get_char();
319 * Opcodes 128 to 159 are defined to have
320 * an integer argument.
322 if (opcode >= 128 && opcode < 160) {
324 (void) packet_get_int();
329 * It is a truly undefined opcode (160 to 255).
330 * We have no idea about its arguments. So we
331 * must stop parsing. Note that some data may be
332 * left in the packet; hopefully there is nothing
333 * more coming after the mode data.
335 log("parse_tty_modes: unknown opcode %d", opcode);
336 packet_integrity_check(0, 1, SSH_CMSG_REQUEST_PTY);
342 if (*n_bytes_ptr != n_bytes) {
343 *n_bytes_ptr = n_bytes;
344 return; /* Don't process bytes passed */
347 return; /* Packet parsed ok but tty stuff failed */
349 /* Set the new modes for the terminal. */
350 if (tcsetattr(fd, TCSANOW, &tio) < 0)
351 log("Setting tty modes failed: %.100s", strerror(errno));