--- libaitio/src/pty.c 2011/09/19 23:09:16 1.1.2.3 +++ libaitio/src/pty.c 2011/09/21 12:55:07 1.1.2.5 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: pty.c,v 1.1.2.3 2011/09/19 23:09:16 misho Exp $ +* $Id: pty.c,v 1.1.2.5 2011/09/21 12:55:07 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -216,6 +216,70 @@ ioSetSidTTY(int *ttyfd, const char *ttyname) return -1; } else close(fd); + + /* redirect standart handles to tty */ + dup2(*ttyfd, STDIN_FILENO); + dup2(*ttyfd, STDOUT_FILENO); + dup2(*ttyfd, STDERR_FILENO); + if (*ttyfd > 2) + close(*ttyfd); + + /* NOW TTY IS READY! */ + return 0; +} + +/* + * ioSetRAWMode() Enter into RAW mode + * @fd = tty fd + * @otio = saved old termios for later restore if !=NULL + * return: -1 error or 0 ok + */ +inline int +ioSetRAWMode(int fd, struct termios *otio) +{ + struct termios tio; + + if (tcgetattr(fd, &tio) == -1) { + LOGERR; + return -1; + } + if (otio) + *otio = tio; + + tio.c_iflag |= IGNPAR; + tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF); +#ifdef IUCLC + tio.c_iflag &= ~IUCLC; +#endif + tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL); +#ifdef IEXTEN + tio.c_lflag &= ~IEXTEN; +#endif + tio.c_oflag &= ~OPOST; + tio.c_cc[VMIN] = 1; + tio.c_cc[VTIME] = 0; + + if (tcsetattr(fd, TCSADRAIN, &tio) == -1) { + LOGERR; + return -1; + } + + return 0; +} + +/* + * ioRestoreMode() Restore termios to tty fd + * @fd = tty fd + * @tio = termios structure for restore + * return: -1 error or 0 ok + */ +inline int +ioRestoreMode(int fd, struct termios tio) +{ + if (tcsetattr(fd, TCSADRAIN, &tio) == -1) { + LOGERR; + return -1; + } return 0; }