1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <stdio.h>
#include <termios.h>
#include <stdarg.h>
#define TTY_STORE 16
static struct termios orig_tty_state[TTY_STORE];
static int sttyfds[TTY_STORE];
/* init the stty store array */
void stty_initstore (void)
{
int i;
for (i = 0; i < TTY_STORE; i++)
sttyfds[i] = -1;
}
/* set tty on fd into raw mode */
int stty_raw (int fd)
{
struct termios tty_state;
int i;
if (tcgetattr(fd, &tty_state) < 0)
return (-1);
for (i = 0; i < TTY_STORE; i++)
if (sttyfds[i] == -1)
{
orig_tty_state[i] = tty_state;
sttyfds[i] = fd;
break;
}
tty_state.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty_state.c_oflag &= ~OPOST;
tty_state.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty_state.c_cflag &= ~(CSIZE | PARENB);
tty_state.c_cflag |= CS8;
tty_state.c_cc[VMIN] = 1;
tty_state.c_cc[VTIME] = 0;
if (tcsetattr(fd, TCSAFLUSH, &tty_state) < 0)
return (-1);
return (0);
}
int stty_clocal (int fd, int clocal)
{
struct termios tty_state;
if (tcgetattr(fd, &tty_state) < 0)
return (-1);
if (clocal)
tty_state.c_cflag |= CLOCAL;
else
tty_state.c_cflag &= ~CLOCAL;
if (tcsetattr(fd, TCSAFLUSH, &tty_state) < 0)
return (-1);
return (0);
}
/* restore all altered ttys to their original state */
void stty_orig (void)
{
int i;
for (i = 0; i < TTY_STORE; i++)
if (sttyfds[i] != -1)
{
tcsetattr (sttyfds[i], TCSAFLUSH, &orig_tty_state[i]);
sttyfds[i] = -1;
}
}
|