#include #include #include #include #include #include #include extern int errno; struct termios oldsettings; void reset_terminal() { tcsetattr(0, TCSANOW, & oldsettings); printf("\nterminal reset\n"); } void control_c(int x) { printf("\r\ncontrol-c\r\n"); exit(0); } void cprint_clearly(char c) { if (c<' ' || c>'~' || c=='[' || c==']' || c=='"') printf("[%d]", c); else printf("%c", c); if (c==4) exit(0); } void print_clearly(char * s) { int i; for (i=0; 1; i+=1) if (s[i] == 0) break; else cprint_clearly(s[i]); } int main() { tcgetattr(0, & oldsettings); signal(SIGINT, control_c); atexit(reset_terminal); struct termios info; tcgetattr(0, & info); info.c_lflag &= ~ ECHO; info.c_lflag &= ~ ICANON; info.c_cc[VMIN] = 1; info.c_cc[VTIME] = 0; tcsetattr(0, TCSAFLUSH, & info); while (1) { char line[1000]; int r = read(0, line, 999); if (r<0) { if (errno == EAGAIN) { usleep(10000); continue; } fprintf(stderr, "errno is %d\n", errno); perror("read"); } else if (r==0) printf("nothing!\n"); else { line[r] = 0; printf("got \""); print_clearly(line); printf("\"\n", line); } } }