readline fix
authorChristian Thaeter <ct@pipapo.org>
Sat, 6 Oct 2007 16:44:32 +0000 (18:44 +0200)
committerChristian Thaeter <ct@pipapo.org>
Sat, 6 Oct 2007 16:44:32 +0000 (18:44 +0200)
rxpd.c

diff --git a/rxpd.c b/rxpd.c
index e7a00fb..bff71bc 100644 (file)
--- a/rxpd.c
+++ b/rxpd.c
@@ -337,34 +337,38 @@ rxpd_buffer_readline (struct rxpd_buffer* self, int again)
 {
   int fd = self->conn->fd;
 
-  if (self->eol != self->buffer)
+  if (self->eol < self->eob)
     {
-      //there was a line pending, discard it now
+      //there was a line pending, shift buffer left
       memmove (self->buffer, self->eol+1, self->eob - self->eol - 1);
       self->eob = (char*)(self->eob - (self->eol - self->buffer + 1));
       self->eol = self->buffer;
       // TODO handle \r's
     }
 
+
   if (!again && self->state == RXPD_OK)   // we only read when again is 0, first iteration
     {
       ssize_t r = 0;
       do
         {
-          r = read(fd, self->eob, 4096 - (self->eob - self->buffer));
+          r = read(fd, self->eob, 4095 - (self->eob - self->buffer));
         }
       while (r == -1 && errno == EINTR);
 
-      if (r == -1)
-        self->state = RXPD_ERROR;
-
-      if (r == 0)
+      if (r != -1)
         {
-          shutdown (fd, SHUT_RD);
-          self->state = RXPD_EOF;
-        }
 
-      self->eob += r;
+          if (r == 0)
+            {
+              shutdown (fd, SHUT_RD);
+              self->state = RXPD_EOF;
+            }
+
+          self->eob += r;
+        }
+      else
+        self->state = RXPD_ERROR;
     }
 
   // find next newline, terminate string there
@@ -380,7 +384,7 @@ rxpd_buffer_readline (struct rxpd_buffer* self, int again)
 
   // TODO handle buffer overfulls
 
-  return self->eol == self->buffer ? NULL : self->buffer;
+  return (self->eob == self->buffer) ? NULL : self->buffer;
 }
 
 /*
@@ -490,7 +494,7 @@ rxpd_connection_parse_cmd (int fd, short event, void* ptr)
   struct rxpd_connection* self = (struct rxpd_connection*) ptr;
 
   char* line;
-  while (!(line = rxpd_buffer_readline (&self->in, 0)) && (rxpd_buffer_state (&self->in) == RXPD_OK));
+  line = rxpd_buffer_readline (&self->in, 0);
 
   if (!line)
     {