#include "stream.h" #include #include #include #include #include #include #include /// Creates and pushes new Stream into the Lua stack. /// \param L Lua state to push to /// \param fd File descriptor used by the Stream /// \return TODO int stream_push_new(lua_State * L, const int fd) { struct stream * s = lua_newuserdata(L, sizeof(struct stream)); memset(s, 0, sizeof(struct stream)); s->fd = fd; if (1 == luaL_newmetatable(L, "stream")) { lua_pushliteral(L, "__gc"); lua_pushcfunction(L, stream_gc); lua_rawset(L, -3); lua_pushliteral(L, "__index"); lua_createtable(L, 0, 1); lua_pushliteral(L, "read"); lua_pushcfunction(L, stream_read); lua_rawset(L, -3); lua_rawset(L, -3); } lua_setmetatable(L, -2); return LUA_OK; } /// Metamethod to handle garbage collection of Stream userdata. /// \param L Lua state in which Stream resides /// \return Number of the results pushed to the stack; always zero in this case int stream_gc(lua_State * L) { int n = lua_gettop(L); if (1 != n) { lua_pushliteral(L, "Invalid number of arguments received"); return lua_error(L); } struct stream * s = lua_touserdata(L, -1); if (NULL == s) { lua_pushliteral(L, "Missing stream argument"); return lua_error(L); } if (NULL != s->in.data) { free(s->in.data); } lua_pop(L, 1); return 0; } /// Starts reading operation from a stream. /// \param L Lua state in which Stream resides /// \return Number of the results pushed to the stack int stream_read(lua_State * L) { int n = lua_gettop(L); if (2 > n) { lua_pushliteral(L, "At least two arguments expected"); return lua_error(L); } struct stream * s = lua_touserdata(L, 1); if (NULL == s) { // TODO: Improve error handling and raising in steam_* lua_CFunctions lua_pushliteral(L, "Missing stream argument"); return lua_error(L); } if (NULL == s->in.data) { void * buffer = malloc(1024); if (NULL == buffer) { lua_pushliteral(L, "Could not allocate buffer for stream"); return lua_error(L); } s->in.data = buffer; s->in.allocated = 1024; s->in.length = 0; s->in.offset = 0; } return stream_readk(L, LUA_OK, 2); // Intentionally do not remove arguments from the stack. } static int grow(lua_State *, struct buffer *); static int prepare_at_least(lua_State *, struct stream *, const int, lua_KContext); static int read_more(lua_State *, struct stream *, const int, lua_KContext); static int until(struct buffer *, const char *, const int); static int grow(lua_State * L, struct buffer * b) { int allocated = b->allocated + 1024; if (8192 < allocated) { lua_pushliteral(L, "Too large buffer"); return lua_error(L); } void * buffer = realloc(b->data, allocated); if (NULL == buffer) { lua_pushliteral(L, "Could not grow buffer"); return lua_error(L); } b->data = buffer; b->allocated = allocated; return 0; } static int read_more(lua_State * L, struct stream * s, int minimum_length, lua_KContext ctx) { const int free_space = s->in.allocated + s->in.offset - s->in.length - 1; while (free_space < minimum_length) { grow(L, &s->in); } if (0 < s->in.offset) { memmove(s->in.data, s->in.data + s->in.offset, s->in.length - s->in.offset); s->in.offset = 0; s->in.length -= s->in.offset; } int length = read(s->fd, s->in.data + s->in.length, free_space); if (-1 == length) { if (EWOULDBLOCK == errno || EAGAIN == errno) { return lua_yieldk(L, 0, ctx, stream_readk); } else { lua_pushstring(L, strerror(errno)); return lua_error(L); } } s->in.length += length; return length; } static int prepare_at_least(lua_State * L, struct stream * s, int minimum_length, lua_KContext ctx) { const int remaining_bytes = s->in.length - s->in.offset; if (remaining_bytes < minimum_length) { return read_more(L, s, minimum_length, ctx); } return 0; } static int until(struct buffer * b, const char * pattern, int pattern_length) { int offset = b->offset; while (offset + pattern_length <= b->length) { if (0 == strncmp(&b->data[offset], pattern, pattern_length)) { return offset; } else { offset++; } } return -1; } /// Continuation function and core implementation of the reading operation from a stream. /// \param L Lua state running reading operation /// \param status Unused /// \param ctx Address of the Stream that is being read /// \param Number of the results pushed to the stack int stream_readk(lua_State * L, int status, lua_KContext ctx) { struct stream * s = lua_touserdata(L, 1); (void) status; for (; ctx <= lua_gettop(L); ++ctx) { size_t pattern_length; const char * pattern = lua_tolstring(L, ctx, &pattern_length); int offset; do { prepare_at_least(L, s, (int) pattern_length, ctx); offset = until(&s->in, pattern, (int) pattern_length); } while (-1 == offset); lua_pushlstring(L, &s->in.data[s->in.offset], offset - s->in.offset); s->in.offset = offset + 1; lua_remove(L, ctx); lua_insert(L, ctx); } lua_remove(L, 1); return lua_gettop(L); }