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)
212 if (tcgetattr(fd, &tio) < 0) {
213 packet_put_char(TTY_OP_END);
214 log("tcgetattr: %.100s", strerror(errno));
217 /* Store input and output baud rates. */
218 baud = speed_to_baud(cfgetospeed(&tio));
219 packet_put_char(TTY_OP_OSPEED);
220 packet_put_int(baud);
221 baud = speed_to_baud(cfgetispeed(&tio));
222 packet_put_char(TTY_OP_ISPEED);
223 packet_put_int(baud);
225 /* Store values of mode flags. */
226 #define TTYCHAR(NAME, OP) \
227 packet_put_char(OP); packet_put_char(tio.c_cc[NAME]);
228 #define TTYMODE(NAME, FIELD, OP) \
229 packet_put_char(OP); packet_put_char((tio.FIELD & NAME) != 0);
230 #define SGTTYCHAR(NAME, OP)
231 #define SGTTYMODE(NAME, FIELD, OP)
232 #define SGTTYMODEN(NAME, FIELD, OP)
234 #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; \
300 #define SGTTYCHAR(NAME, OP)
301 #define SGTTYMODE(NAME, FIELD, OP)
302 #define SGTTYMODEN(NAME, FIELD, OP)
304 #include "ttymodes.h"
313 debug("Ignoring unsupported tty mode opcode %d (0x%x)",
316 * Opcodes 0 to 127 are defined to have
317 * a one-byte argument.
319 if (opcode >= 0 && opcode < 128) {
321 (void) packet_get_char();
325 * Opcodes 128 to 159 are defined to have
326 * an integer argument.
328 if (opcode >= 128 && opcode < 160) {
330 (void) packet_get_int();
335 * It is a truly undefined opcode (160 to 255).
336 * We have no idea about its arguments. So we
337 * must stop parsing. Note that some data may be
338 * left in the packet; hopefully there is nothing
339 * more coming after the mode data.
341 log("parse_tty_modes: unknown opcode %d", opcode);
342 packet_integrity_check(0, 1, SSH_CMSG_REQUEST_PTY);
348 if (*n_bytes_ptr != n_bytes) {
349 *n_bytes_ptr = n_bytes;
350 return; /* Don't process bytes passed */
353 return; /* Packet parsed ok but tty stuff failed */
355 /* Set the new modes for the terminal. */
356 if (tcsetattr(fd, TCSANOW, &tio) < 0)
357 log("Setting tty modes failed: %.100s", strerror(errno));