# HG changeset patch # User Tero Marttila # Date 1235840931 -7200 # Node ID ca16f3a8f3b70cd56a2091c93a911a9f239f9fa4 # Parent 4147fae232d9fcee2ca2f3ff528c447102aead9c add support for \n line endings as well diff -r 4147fae232d9 -r ca16f3a8f3b7 src/line_proto.c --- a/src/line_proto.c Sat Feb 28 18:48:10 2009 +0200 +++ b/src/line_proto.c Sat Feb 28 19:08:51 2009 +0200 @@ -122,21 +122,28 @@ * */ int _parse_line (char *buf, size_t len, size_t *hint) { - int i; + int i, next = 0; // empty buffer -> nothing if (len == 0) return 0; - // look for terminating '\r\n' sequence - for (i = *hint; i < len - 1; i++) { - // match this + next char - if (buf[i] == '\r' && buf[i + 1] == '\n') + // look for terminating '\r\n' or '\n' sequence + for (i = *hint; i < len; i++) { + // match this + next char? + if (i < len - 1 && buf[i] == '\r' && buf[i + 1] == '\n') { + next = i + 2; break; + + } else if (buf[i] == '\n') { + next = i + 1; + break; + } } - // incomplete? - if (i >= len - 1) { + // searched the whole buffer? + if (i >= len) { + // do continue one char back, to keep any \r *hint = len - 1; return 0; } @@ -144,8 +151,8 @@ // mangle the newline off buf[i] = '\0'; - // return offset to next line - return i + 2; + // return offset to next line, as set in loop based on delim + return next; } err_t line_proto_read (struct line_proto *lp, const char **line_ptr) diff -r 4147fae232d9 -r ca16f3a8f3b7 src/nexus.c --- a/src/nexus.c Sat Feb 28 18:48:10 2009 +0200 +++ b/src/nexus.c Sat Feb 28 19:08:51 2009 +0200 @@ -13,7 +13,7 @@ #include "line_proto.h" #define CONNECT_HOST "localhost" -#define CONNECT_SERV "5001" +#define CONNECT_SERV "5002" #define LINE_LENGTH 512 void on_line (const char *line, void *arg) { @@ -38,8 +38,8 @@ if (sock_tcp_connect(&sock, CONNECT_HOST, CONNECT_SERV, &_err)) errx(1, "sock_gnutls_connect: %s", error_msg(&_err)); - // line protocol - if (line_proto_create(&lp, sock, LINE_LENGTH, on_line, NULL, &_err)) + // line protocol, with safety margin for buffer + if (line_proto_create(&lp, sock, LINE_LENGTH * 2, on_line, NULL, &_err)) errx(1, "line_proto_create: %s", error_msg(&_err)); // run event loop