Fix: Skip whitespace in AT command line
authorDenis Kenzior <denkenz@gmail.com>
Mon, 1 Feb 2010 23:58:48 +0000 (17:58 -0600)
committerDenis Kenzior <denkenz@gmail.com>
Mon, 1 Feb 2010 23:59:29 +0000 (17:59 -0600)
gatchat/gatserver.c

index a1f2398..7d6ed74 100644 (file)
@@ -272,14 +272,21 @@ static char *extract_line(GAtServer *p)
        unsigned char *buf = ring_buffer_read_ptr(p->buf, pos);
        int strip_front = 0;
        int line_length = 0;
+       gboolean in_string = FALSE;
        char *line;
+       int i;
 
        while (pos < p->read_so_far) {
-               if (line_length == 0 && (*buf == ' ' || *buf == '\t'))
+               if (*buf == '"')
+                       in_string = !in_string;
+
+               if (in_string == FALSE && (*buf == ' ' || *buf == '\t')) {
+                       if (line_length == 0)
                                strip_front += 1;
-               else
+               else
                        line_length += 1;
 
+next:
                buf += 1;
                pos += 1;
 
@@ -299,11 +306,32 @@ static char *extract_line(GAtServer *p)
 
        /* Strip leading whitespace + AT */
        ring_buffer_drain(p->buf, strip_front + 2);
-       ring_buffer_read(p->buf, line, line_length);
+
+       pos = 0;
+       i = 0;
+       wrap = ring_buffer_len_no_wrap(p->buf);
+       buf = ring_buffer_read_ptr(p->buf, pos);
+
+       while (pos < (p->read_so_far - strip_front - 2)) {
+               if (*buf == '"')
+                       in_string = !in_string;
+
+               if ((*buf == ' ' || *buf == '\t') && in_string == FALSE)
+                       ; /* Skip */
+               else if (*buf != '\r')
+                       line[i++] = *buf;
+
+               buf += 1;
+               pos += 1;
+
+               if (pos == wrap)
+                       buf = ring_buffer_read_ptr(p->buf, pos);
+       }
+
        /* Strip \r */
-       ring_buffer_drain(p->buf, 1);
+       ring_buffer_drain(p->buf, p->read_so_far - strip_front - 2);
 
-       line[line_length] = '\0';
+       line[i] = '\0';
 
        return line;
 }