From: Francois Fleuret Date: Sun, 15 Mar 2009 17:30:34 +0000 (+0100) Subject: Simplified the inject_into_tty_buffer routine. Mostly, directly use X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=7bc54403d507b18c42fc870b4b533e27b6f2a244;p=selector.git Simplified the inject_into_tty_buffer routine. Mostly, directly use STDIN_FILENO instead of first looking at the name of the tty attached to it, to find then what is its filehandler. --- diff --git a/selector.cc b/selector.cc index 53b65bb..ff94e1a 100644 --- a/selector.cc +++ b/selector.cc @@ -70,7 +70,6 @@ int remove_duplicates = 0; // if(hashed[code]) { // } else { - // } // } @@ -79,29 +78,17 @@ int remove_duplicates = 0; // This looks severely Linux-only ... void inject_into_tty_buffer(char *line) { - char *tty = ttyname(STDIN_FILENO); - int fd = open(tty, O_RDWR); - struct termios oldtio, newtio; - - if (fd >= 0) { - // Save current port settings - tcgetattr(fd,&oldtio); - memset(&newtio, 0, sizeof(newtio)); - // Set input mode (non-canonical, *no echo*,...) - tcflush(fd, TCIFLUSH); - tcsetattr(fd,TCSANOW, &newtio); - // Put the selected line in the tty input buffer - for(char *k = line; *k; k++) { - ioctl(fd, TIOCSTI, k); - } - // Restore the old settings - tcsetattr(fd,TCSANOW, &oldtio); - close(fd); - } else { - cerr << "Can not open " << tty << "." << endl; - exit(1); + tcgetattr(STDIN_FILENO,&oldtio); + memset(&newtio, 0, sizeof(newtio)); + // Set input mode (non-canonical, *no echo*,...) + tcsetattr(STDIN_FILENO,TCSANOW, &newtio); + // Put the selected line in the tty input buffer + for(char *k = line; *k; k++) { + ioctl(STDIN_FILENO, TIOCSTI, k); } + // Restore the old settings + tcsetattr(STDIN_FILENO,TCSANOW, &oldtio); } //////////////////////////////////////////////////////////////////////