/* demonstrate the use of terminal setting */ #include #include #include #include /* global variable */ struct termio savetty; /* function prototypes */ void sigcatch(); void change_tty(struct termio *); struct termio * settty_param(struct termio); int main(int argc, char* argv[]) { struct termio * newtty; int nrd; int i; char buf[32]; signal(SIGINT,sigcatch); /* catch the 'interrupt' signal, i.e., Ctrl-C*/ /* retrieve and save the current terminal setting into 'savetty' */ if (ioctl(0,TCGETA,&savetty) == -1) { fprintf(stderr, "ioctl failed: not a tty\n"); exit(1); } /* the newtty sets a new collection of tty values */ newtty = settty_param(savetty); change_tty(newtty); for (;;) { nrd = read(0,buf,sizeof(buf)); buf[nrd] = 0; printf("read %d ascii %d chars '%s'\n",nrd,buf[0],buf); for (i = 0; i < nrd; i ++) printf("read '%c' print '%c'\n",buf[i],buf[i]+1); if (buf[0] == 'c') { /* we use char 'c' to signal change terminal */ printf("change tty\n"); change_tty(newtty); } } } /* signal handling */ void sigcatch() { ioctl(0,TCSETAF,&savetty); /* exit(); */ printf(" you typed control - C\n"); } /* set new tty parameters */ struct termio * settty_param(struct termio orig_tty) { struct termio * newtty; newtty = (struct termio *)malloc(sizeof(struct termio)); (*newtty) = orig_tty; newtty->c_lflag &= ~ICANON; /* turn off canonical mode */ newtty->c_lflag &= ~ECHO; /* turn off echoing */ newtty->c_cc[VMIN] = 1; /* minimum 1 chars */ newtty->c_cc[VTIME] = 100; /* 10 sec interval */ return newtty; } /* acutally change tty setting with the given pamaraters */ void change_tty(struct termio * tty_param) { /* actually setting the terminal config with the newtty */ if (ioctl(0,TCSETAF,tty_param) == -1) { printf("cannot put tty into raw mode \n"); exit(1); } }