Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/cqueues.tex
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,9 @@ \subsubsection{\routine{cqueues.COMMIT}}
\subsubsection[\fn{socket:fill}]{\fn{socket:fill(size[, timeout])}}
Fills the input buffer with `size' bytes. Returns true on success, false and an error code on failure.

\subsubsection[{\fn{socket:unget}}]{\fn{socket:unget(string)}}
\subsubsection[{\fn{socket:unget}}]{\fn{socket:unget(string [, mode])}}
Writes `string' to the head of the socket input buffer.
$mode$ should be in the format described at \method{socket:setmode}

\subsubsection[{\fn{socket:pending}}]{\fn{socket:pending()}}
Returns two numbers---the counts of buffered bytes in the input and output streams. This does not include the bytes in the kernel's buffer.
Expand Down
28 changes: 28 additions & 0 deletions regress/90-unget-textmode.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh
_=[[
. "${0%%/*}/regress.sh"
exec runlua "$0" "$@"
]]
require"regress".export".*"

local function test_with_mode(str, mode)
local c, s = socket.pair()
check(s:xwrite(str, "bn"))
local foo = c:xread(-99999, mode)
check(c:unget(foo, mode))
local bar = c:xread(-99999, mode)
check(foo == bar, "unget + read does not round trip")
end
local function test(str)
info("testing: %q", str)
test_with_mode(str, "t")
test_with_mode(str, "b")
end

test'test'
test'test\n'
test'hi\r\nthere\nworld\r'
test'\n\n'
test'foo\r\r\n'

say("OK")
26 changes: 22 additions & 4 deletions src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <stddef.h> /* NULL offsetof size_t */
#include <stdarg.h> /* va_list va_start va_arg va_end */
#include <stdlib.h> /* strtol(3) */
#include <string.h> /* memset(3) memchr(3) memcpy(3) memmem(3) */
#include <string.h> /* memset(3) memchr(3) memcpy(3) memmem(3) memrchr(3) */
#include <math.h> /* NAN */
#include <errno.h> /* EBADF ENOTSOCK EOPNOTSUPP EOVERFLOW EPIPE */

Expand Down Expand Up @@ -2137,12 +2137,30 @@ static lso_nargs_t lso_recv3(lua_State *L) {

static lso_nargs_t lso_unget2(lua_State *L) {
struct luasocket *S = lso_checkself(L, 1);
const void *src;
size_t len;
const char *src, *new_base;
size_t len, found;
struct iovec iov;
int error;
int mode, error;

src = luaL_checklstring(L, 2, &len);
mode = lso_imode(luaL_optstring(L, 3, ""), S->ibuf.mode);

if (mode & LSO_TEXT) {
/* insert \r before each \n */
while ((new_base = memrchr(src, '\n', len))) {
found = len - (new_base - src); /* number of bytes found including \n */

if ((error = fifo_grow(&S->ibuf.fifo, found+1)))
goto error;

fifo_rewind(&S->ibuf.fifo, found+1);
fifo_slice(&S->ibuf.fifo, &iov, 0, found+1);
memcpy(iov.iov_base+1, new_base, found);
((char*)iov.iov_base)[0] = '\r';

len = new_base - src;
}
}

if ((error = fifo_grow(&S->ibuf.fifo, len)))
goto error;
Expand Down