X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=dart%2Fttyread.c;h=65f0203ac9d008bf34783d3b6eeb17b60bbe471f;hb=050398149d26f27a0b19971cbcea990574d73a57;hp=cd057f7c3940d1d19356bb6872f0965b3cca2e50;hpb=cb3568f6daeeb35b6bf7effec71715780946b117;p=svn42.git diff --git a/dart/ttyread.c b/dart/ttyread.c index cd057f7..65f0203 100644 --- a/dart/ttyread.c +++ b/dart/ttyread.c @@ -31,7 +31,7 @@ int setup_tty(int fd) { struct termios tmio; - + int ret = tcgetattr(fd, &tmio); if(ret) { perror("tcgetattr()"); @@ -53,12 +53,18 @@ int setup_tty(int fd) tmio.c_lflag &= ~ECHO; tmio.c_lflag |= CLOCAL; + tmio.c_iflag &= ~ICRNL; + tmio.c_iflag &= ~IGNCR; + tmio.c_iflag |= IGNBRK | BRKINT; + + tmio.c_cflag |= CLOCAL; + ret = tcsetattr(fd, TCSANOW, &tmio); if(ret) { perror("tcsetattr()"); return ret; } - + ret = tcflush(fd, TCIFLUSH); if(ret) { perror("tcflush()"); @@ -91,7 +97,7 @@ int main(int argc, char* argv[]) return 1; } - int fd = open(argv[1], O_RDONLY); + int fd = open(argv[1], O_RDONLY | O_NONBLOCK | O_NOCTTY); if(fd < 0) { perror("open()"); return 2; @@ -99,15 +105,35 @@ int main(int argc, char* argv[]) if(setup_tty(fd)) return 3; + fd_set rfds, efds; char buf[100]; for(;;) { - int r = read(fd, buf, sizeof(buf)); - if(r <=0 ) return r; - - int i; + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + FD_ZERO(&efds); + FD_SET(1, &efds); + + int s = select(fd+1, &rfds, NULL, &efds, NULL); + if(s < 0) { + perror("select()"); + return s; + } + if(FD_ISSET(1, &efds)) return 0; + if(!FD_ISSET(fd, &rfds)) continue; + + ssize_t r = read(fd, buf, sizeof(buf)); + if(r <= 0) { + perror("read()"); + return r; + } + + ssize_t i; for(i=0; i < r;) { - int w = write(fd, &(buf[i]), r - i); - if(w < 0) return w; + ssize_t w = write(1, &(buf[i]), r - i); + if(w < 0) { + perror("write()"); + return w; + } i+=w; } }