lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I've been playing with IRC for a bit now and one thing that's been
bothering me is that the specs allow for a line to be terminated with
just CR. LuaSocket however discards CRs and treats only LF as line
break, which means any IRC client using it can't easily conform to the
spec.

I decided to fix this with a simple patch to recvline(); it now
accepts CR, LF, or CR LF as line breaks. I didn't test this
extensively, but everything still seems to be working, so here we go.

-- 
Sent from my toaster.
diff -rupN -x Makefile -x '*.orig' -x '*.o' -x '*.a' -x '*.rej' -x '*.patch' -x test.lua luasocket-2.0.2-acceptfd/src/buffer.c luasocket-2.0.2-acceptfd-linebreak/src/buffer.c
--- luasocket-2.0.2-acceptfd/src/buffer.c	2007-10-13 17:44:03.000000000 -0600
+++ luasocket-2.0.2-acceptfd-linebreak/src/buffer.c	2010-11-30 21:31:31.725722058 -0700
@@ -216,6 +216,9 @@ static int recvall(p_buffer buf, luaL_Bu
 /*-------------------------------------------------------------------------*\
 * Reads a line terminated by a CR LF pair or just by a LF. The CR and LF 
 * are not returned by the function and are discarded from the buffer
+* MODIFICATION BY RENA: Now accepts lines terminated by just CR as well; allows
+* lines terminated by CR, LF or CR LF. Intended for protocols like IRC that
+* allow CR alone as line break.
 \*-------------------------------------------------------------------------*/
 static int recvline(p_buffer buf, luaL_Buffer *b) {
     int err = IO_DONE;
@@ -223,12 +226,18 @@ static int recvline(p_buffer buf, luaL_B
         size_t count, pos; const char *data;
         err = buffer_get(buf, &data, &count);
         pos = 0;
-        while (pos < count && data[pos] != '\n') {
-            /* we ignore all \r's */
-            if (data[pos] != '\r') luaL_putchar(b, data[pos]);
+        while (pos < count) {
+			char chr = data[pos];
+			if(chr == '\n') break;
+			else if(chr == '\r') {
+				//check for \r\n
+				if(((pos+1) < count) && (data[pos+1] == '\n')) pos++;
+				break;
+			}
+			else luaL_putchar(b, chr);
             pos++;
         }
-        if (pos < count) { /* found '\n' */
+        if (pos < count) { /* found end */
             buffer_skip(buf, pos+1); /* skip '\n' too */
             break; /* we are done */
         } else /* reached the end of the buffer */
Binary files luasocket-2.0.2-acceptfd/src/socket.so.2.0.2 and luasocket-2.0.2-acceptfd-linebreak/src/socket.so.2.0.2 differ