diff options
Diffstat (limited to 'Build/source/texk/web2c/luatexdir/luasocket/src/buffer.c')
-rw-r--r-- | Build/source/texk/web2c/luatexdir/luasocket/src/buffer.c | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/Build/source/texk/web2c/luatexdir/luasocket/src/buffer.c b/Build/source/texk/web2c/luatexdir/luasocket/src/buffer.c index 73f4ffa85e6..4ef4e8eb5c4 100644 --- a/Build/source/texk/web2c/luatexdir/luasocket/src/buffer.c +++ b/Build/source/texk/web2c/luatexdir/luasocket/src/buffer.c @@ -1,8 +1,6 @@ /*=========================================================================*\ * Input/Output interface for Lua programs * LuaSocket toolkit -* -* RCS ID: $Id: buffer.c,v 1.28 2007/06/11 23:44:54 diego Exp $ \*=========================================================================*/ #include "lua.h" #include "lauxlib.h" @@ -42,7 +40,7 @@ int buffer_open(lua_State *L) { * Initializes C structure \*-------------------------------------------------------------------------*/ void buffer_init(p_buffer buf, p_io io, p_timeout tm) { - buf->first = buf->last = 0; + buf->first = buf->last = 0; buf->io = io; buf->tm = tm; buf->received = buf->sent = 0; @@ -53,8 +51,8 @@ void buffer_init(p_buffer buf, p_io io, p_timeout tm) { * object:getstats() interface \*-------------------------------------------------------------------------*/ int buffer_meth_getstats(lua_State *L, p_buffer buf) { - lua_pushnumber(L, buf->received); - lua_pushnumber(L, buf->sent); + lua_pushnumber(L, (lua_Number) buf->received); + lua_pushnumber(L, (lua_Number) buf->sent); lua_pushnumber(L, timeout_gettime() - buf->birthday); return 3; } @@ -63,8 +61,8 @@ int buffer_meth_getstats(lua_State *L, p_buffer buf) { * object:setstats() interface \*-------------------------------------------------------------------------*/ int buffer_meth_setstats(lua_State *L, p_buffer buf) { - buf->received = (long) luaL_optnumber(L, 2, buf->received); - buf->sent = (long) luaL_optnumber(L, 3, buf->sent); + buf->received = (long) luaL_optnumber(L, 2, (lua_Number) buf->received); + buf->sent = (long) luaL_optnumber(L, 3, (lua_Number) buf->sent); if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4); lua_pushnumber(L, 1); return 1; @@ -80,7 +78,9 @@ int buffer_meth_send(lua_State *L, p_buffer buf) { const char *data = luaL_checklstring(L, 2, &size); long start = (long) luaL_optnumber(L, 3, 1); long end = (long) luaL_optnumber(L, 4, -1); +#ifdef LUASOCKET_DEBUG p_timeout tm = timeout_markstart(buf->tm); +#endif if (start < 0) start = (long) (size+start+1); if (end < 0) end = (long) (size+end+1); if (start < 1) start = (long) 1; @@ -90,9 +90,9 @@ int buffer_meth_send(lua_State *L, p_buffer buf) { if (err != IO_DONE) { lua_pushnil(L); lua_pushstring(L, buf->io->error(buf->io->ctx, err)); - lua_pushnumber(L, sent+start-1); + lua_pushnumber(L, (lua_Number) (sent+start-1)); } else { - lua_pushnumber(L, sent+start-1); + lua_pushnumber(L, (lua_Number) (sent+start-1)); lua_pushnil(L); lua_pushnil(L); } @@ -111,7 +111,9 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) { luaL_Buffer b; size_t size; const char *part = luaL_optlstring(L, 3, "", &size); +#ifdef LUASOCKET_DEBUG p_timeout tm = timeout_markstart(buf->tm); +#endif /* initialize buffer with optional extra prefix * (useful for concatenating previous partial results) */ luaL_buffinit(L, &b); @@ -122,9 +124,15 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) { if (p[0] == '*' && p[1] == 'l') err = recvline(buf, &b); else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b); else luaL_argcheck(L, 0, 2, "invalid receive pattern"); - /* get a fixed number of bytes (minus what was already partially - * received) */ - } else err = recvraw(buf, (size_t) lua_tonumber(L, 2)-size, &b); + /* get a fixed number of bytes (minus what was already partially + * received) */ + } else { + double n = lua_tonumber(L, 2); + size_t wanted = (size_t) n; + luaL_argcheck(L, n >= 0, 2, "invalid receive pattern"); + if (size == 0 || wanted > size) + err = recvraw(buf, wanted-size, &b); + } /* check if there was an error */ if (err != IO_DONE) { /* we can't push anyting in the stack before pushing the @@ -166,7 +174,7 @@ static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent) { size_t total = 0; int err = IO_DONE; while (total < count && err == IO_DONE) { - size_t done; + size_t done = 0; size_t step = (count-total <= STEPSIZE)? count-total: STEPSIZE; err = io->send(io->ctx, data+total, step, &done, tm); total += done; @@ -225,7 +233,7 @@ static int recvline(p_buffer buf, luaL_Buffer *b) { pos = 0; while (pos < count && data[pos] != '\n') { /* we ignore all \r's */ - if (data[pos] != '\r') luaL_putchar(b, data[pos]); + if (data[pos] != '\r') luaL_addchar(b, data[pos]); pos++; } if (pos < count) { /* found '\n' */ |