Created
December 14, 2019 21:40
-
-
Save Jongy/3b114208c39e7eb4826ffa5a0421d056 to your computer and use it in GitHub Desktop.
Print input characters in raw terminal mode. Useful when developing shells/readline etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <ctype.h> | |
#include <stdio.h> | |
#include <termios.h> | |
#include <string.h> | |
int main(void) | |
{ | |
struct termios tty_attr; | |
tcgetattr(STDIN_FILENO, &tty_attr); | |
// flags taken from "man 3 termios" | |
tty_attr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | |
| INLCR | IGNCR | ICRNL | IXON); | |
tty_attr.c_oflag &= ~OPOST; | |
tty_attr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); | |
tty_attr.c_cflag &= ~(CSIZE | PARENB); | |
tty_attr.c_cflag |= CS8; | |
// read min 1 character at a time, no timeout | |
tty_attr.c_cc[VTIME] = 0; | |
tty_attr.c_cc[VMIN] = 1; | |
tcsetattr(STDIN_FILENO, TCSANOW, &tty_attr); | |
setvbuf(stdout, NULL, _IONBF, 0); | |
while (1) { | |
char c; | |
read(STDIN_FILENO, &c, 1); | |
if (isprint(c)) { | |
printf("%c", c); | |
} else { | |
printf("\\x%02x", c); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment