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.10 2001/01/21 19:06:01 markus 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);
236 #define SGTTYCHAR(NAME, OP)
237 #define SGTTYMODE(NAME, FIELD, OP)
238 #define SGTTYMODEN(NAME, FIELD, OP)
240 #include "ttymodes.h"
248 /* Mark end of mode data. */
249 packet_put_char(TTY_OP_END);
253 * Decodes terminal modes for the terminal referenced by fd in a portable
254 * manner from a packet being read.
257 tty_parse_modes(int fd, int *n_bytes_ptr)
265 * Get old attributes for the terminal. We will modify these
266 * flags. I am hoping that if there are any machine-specific
267 * modes, they will initially have reasonable values.
269 if (tcgetattr(fd, &tio) < 0)
274 opcode = packet_get_char();
281 baud = packet_get_int();
282 if (failure != -1 && cfsetispeed(&tio, baud_to_speed(baud)) < 0)
283 error("cfsetispeed failed for %d", baud);
288 baud = packet_get_int();
289 if (failure != -1 && cfsetospeed(&tio, baud_to_speed(baud)) < 0)
290 error("cfsetospeed failed for %d", baud);
293 #define TTYCHAR(NAME, OP) \
296 tio.c_cc[NAME] = packet_get_char(); \
298 #define TTYMODE(NAME, FIELD, OP) \
301 if (packet_get_char()) \
304 tio.FIELD &= ~NAME; \
306 #define SGTTYCHAR(NAME, OP)
307 #define SGTTYMODE(NAME, FIELD, OP)
308 #define SGTTYMODEN(NAME, FIELD, OP)
310 #include "ttymodes.h"
319 debug("Ignoring unsupported tty mode opcode %d (0x%x)",
322 * Opcodes 0 to 127 are defined to have
323 * a one-byte argument.
325 if (opcode >= 0 && opcode < 128) {
327 (void) packet_get_char();
331 * Opcodes 128 to 159 are defined to have
332 * an integer argument.
334 if (opcode >= 128 && opcode < 160) {
336 (void) packet_get_int();
341 * It is a truly undefined opcode (160 to 255).
342 * We have no idea about its arguments. So we
343 * must stop parsing. Note that some data may be
344 * left in the packet; hopefully there is nothing
345 * more coming after the mode data.
347 log("parse_tty_modes: unknown opcode %d", opcode);
348 packet_integrity_check(0, 1, SSH_CMSG_REQUEST_PTY);
354 if (*n_bytes_ptr != n_bytes) {
355 *n_bytes_ptr = n_bytes;
356 return; /* Don't process bytes passed */
359 return; /* Packet parsed ok but tty stuff failed */
361 /* Set the new modes for the terminal. */
362 if (tcsetattr(fd, TCSANOW, &tio) < 0)
363 log("Setting tty modes failed: %.100s", strerror(errno));