works
[smouse] / src / unix / serial.c
index fe8aeb3..22978e4 100644 (file)
@@ -22,11 +22,10 @@ int ser_open(const char *port, int baud, unsigned int mode)
                return -1;
        }
 
-       if((fd = open(port, O_RDWR | O_NONBLOCK | O_NOCTTY)) == -1) {
+       if((fd = open(port, O_RDWR | O_NOCTTY)) == -1) {
                return -1;
        }
 
-       /*memset(&term, 0, sizeof term);*/
        tcgetattr(fd, &term);
 
        term.c_oflag = 0;
@@ -34,10 +33,13 @@ int ser_open(const char *port, int baud, unsigned int mode)
        term.c_cc[VMIN] = 0;
        term.c_cc[VTIME] = 0;
 
-       term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL | CRTSCTS;
+       term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL;
        if(mode & SER_8N2) {
                term.c_cflag |= CSTOPB;
        }
+       if(mode & SER_HWFLOW) {
+               term.c_cflag |= CRTSCTS;
+       }
 
        term.c_iflag = IGNBRK | IGNPAR;
 
@@ -50,7 +52,9 @@ int ser_open(const char *port, int baud, unsigned int mode)
                return -1;
        }
 
-       if(mode & SER_HWFLOW) {
+#ifdef TIOCM_RTS
+       /* assert DTR/RTS lines */
+       {
                int st;
                if(ioctl(fd, TIOCMGET, &st) == -1) {
                        perror("ser_open: failed to get modem status");
@@ -64,6 +68,7 @@ int ser_open(const char *port, int baud, unsigned int mode)
                        return -1;
                }
        }
+#endif
 
        return fd;
 }
@@ -73,6 +78,16 @@ void ser_close(int fd)
        close(fd);
 }
 
+int ser_block(int fd)
+{
+       return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
+}
+
+int ser_nonblock(int fd)
+{
+       return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
+}
+
 int ser_pending(int fd)
 {
        static struct timeval tv_zero;
@@ -136,8 +151,7 @@ char *ser_getline(int fd, char *buf, int bsz)
 {
        static char linebuf[512];
        static int widx;
-       int i, rd, size;
-       char *ptr;
+       int i, rd, size, offs;
 
        size = sizeof linebuf - widx;
        while(size && (rd = read(fd, linebuf + widx, size)) > 0) {
@@ -145,22 +159,18 @@ char *ser_getline(int fd, char *buf, int bsz)
                size -= rd;
        }
 
-       ptr = linebuf;
-       for(i=0; i<widx; i++) {
-               if(*ptr == '\r' || *ptr == '\n') {
-                       *ptr++ = '\n';
-                       if(i < widx - 1 && ptr[1] == '\n') {
-                               *ptr++ = 0;
-                       }
+       linebuf[widx] = 0;
 
-                       size = widx >= bsz ? bsz - 1 : widx;
+       for(i=0; i<widx; i++) {
+               if(linebuf[i] == '\r' || linebuf[i] == '\n') {
+                       size = i >= bsz ? bsz - 1 : i;
                        memcpy(buf, linebuf, size);
                        buf[size] = 0;
 
-                       memmove(linebuf, linebuf + widx, sizeof linebuf - widx);
+                       offs = i + 1;
+                       memmove(linebuf, linebuf + offs, widx - offs);
+                       widx -= offs;
                        return buf;
-               } else {
-                       ++ptr;
                }
        }
        return 0;