2011-11-10 10:01:59 +00:00
|
|
|
/*
|
|
|
|
* Unix test code that puts the terminal into raw mode, then echos typed
|
|
|
|
* characters to stdout. Derived from sample code in the Stevens book, posted
|
|
|
|
* online at http://www.lafn.org/~dave/linux/terminalIO.html.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "FormatChar.h"
|
|
|
|
|
|
|
|
static struct termios save_termios;
|
|
|
|
static int term_saved;
|
|
|
|
|
|
|
|
/* RAW! mode */
|
|
|
|
int tty_raw(int fd)
|
|
|
|
{
|
2012-04-01 09:03:12 +00:00
|
|
|
struct termios buf;
|
2011-11-10 10:01:59 +00:00
|
|
|
|
2012-04-01 09:03:12 +00:00
|
|
|
if (tcgetattr(fd, &save_termios) < 0) /* get the original state */
|
|
|
|
return -1;
|
2011-11-10 10:01:59 +00:00
|
|
|
|
2012-04-01 09:03:12 +00:00
|
|
|
buf = save_termios;
|
2011-11-10 10:01:59 +00:00
|
|
|
|
2012-04-01 09:03:12 +00:00
|
|
|
/* echo off, canonical mode off, extended input
|
|
|
|
processing off, signal chars off */
|
|
|
|
buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
|
2011-11-10 10:01:59 +00:00
|
|
|
|
2012-04-01 09:03:12 +00:00
|
|
|
/* no SIGINT on BREAK, CR-to-NL off, input parity
|
|
|
|
check off, don't strip the 8th bit on input,
|
|
|
|
ouput flow control off */
|
|
|
|
buf.c_iflag &= ~(BRKINT | ICRNL | ISTRIP | IXON);
|
2011-11-10 10:01:59 +00:00
|
|
|
|
2012-04-01 09:03:12 +00:00
|
|
|
/* clear size bits, parity checking off */
|
|
|
|
buf.c_cflag &= ~(CSIZE | PARENB);
|
2011-11-10 10:01:59 +00:00
|
|
|
|
2012-04-01 09:03:12 +00:00
|
|
|
/* set 8 bits/char */
|
|
|
|
buf.c_cflag |= CS8;
|
2011-11-10 10:01:59 +00:00
|
|
|
|
2012-04-01 09:03:12 +00:00
|
|
|
/* output processing off */
|
|
|
|
buf.c_oflag &= ~(OPOST);
|
2011-11-10 10:01:59 +00:00
|
|
|
|
2012-04-01 09:03:12 +00:00
|
|
|
buf.c_cc[VMIN] = 1; /* 1 byte at a time */
|
|
|
|
buf.c_cc[VTIME] = 0; /* no timer on input */
|
2011-11-10 10:01:59 +00:00
|
|
|
|
2012-04-01 09:03:12 +00:00
|
|
|
if (tcsetattr(fd, TCSAFLUSH, &buf) < 0)
|
|
|
|
return -1;
|
2011-11-10 10:01:59 +00:00
|
|
|
|
2012-04-01 09:03:12 +00:00
|
|
|
term_saved = 1;
|
2011-11-10 10:01:59 +00:00
|
|
|
|
2012-04-01 09:03:12 +00:00
|
|
|
return 0;
|
2011-11-10 10:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* set it to normal! */
|
|
|
|
int tty_reset(int fd)
|
|
|
|
{
|
2012-04-01 09:03:12 +00:00
|
|
|
if (term_saved)
|
|
|
|
if (tcsetattr(fd, TCSAFLUSH, &save_termios) < 0)
|
|
|
|
return -1;
|
2011-11-10 10:01:59 +00:00
|
|
|
|
2012-04-01 09:03:12 +00:00
|
|
|
return 0;
|
2011-11-10 10:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2012-04-01 09:03:12 +00:00
|
|
|
tty_raw(0);
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
while (true) {
|
|
|
|
char ch;
|
|
|
|
char buf[16];
|
|
|
|
int actual = read(0, &ch, 1);
|
|
|
|
if (actual != 1) {
|
|
|
|
perror("read error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
formatChar(buf, ch);
|
|
|
|
fputs(buf, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
if (ch == 3) // Ctrl-C
|
|
|
|
break;
|
2011-11-10 10:01:59 +00:00
|
|
|
}
|
2012-04-01 09:03:12 +00:00
|
|
|
|
|
|
|
tty_reset(0);
|
|
|
|
return 0;
|
2011-11-10 10:01:59 +00:00
|
|
|
}
|