some cleanups still no working
[svn42.git] / dart / ttyread.c
index cd057f7..65f0203 100644 (file)
@@ -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;
     }
   }