summaryrefslogtreecommitdiffhomepage
path: root/stream.c
diff options
context:
space:
mode:
Diffstat (limited to 'stream.c')
-rw-r--r--stream.c33
1 files changed, 22 insertions, 11 deletions
diff --git a/stream.c b/stream.c
index 017c031..1033b23 100644
--- a/stream.c
+++ b/stream.c
@@ -352,11 +352,32 @@ int stream_flush(lua_State * L)
return lua_error(L);
}
+ return stream_flushk(L, LUA_OK, (lua_KContext) s);
+}
+
+/// Continuation of the flush operation.
+/// \param L Lua state in which Stream resides
+/// \param status Unused
+/// \param ctx Address of the stream context in memory
+/// \return Number of the results pushed to the stack
+int stream_flushk(lua_State * L, const int status, lua_KContext ctx)
+{
+ struct stream * s = (struct stream *) ctx;
+ (void) status;
+
const int bytes_written = write(s->fd, s->out.data, s->out.length);
if (-1 == bytes_written)
{
- // TODO: Errors and yield
+ if (EAGAIN == errno || EWOULDBLOCK == errno)
+ {
+ return lua_yieldk(L, 0, ctx, stream_flushk);
+ }
+ else
+ {
+ lua_pushstring(L, strerror(errno));
+ return lua_error(L);
+ }
}
else
{
@@ -367,16 +388,6 @@ int stream_flush(lua_State * L)
return 0;
}
-/// Continuation of the flush operation.
-/// \param L Lua state in which Stream resides
-/// \param status Unused
-/// \param ctx TODO
-/// \return Number of the results pushed to the stack
-int stream_flushk(lua_State * L, const int status, lua_KContext ctx)
-{
- return 0;
-}
-
/// Discards the contents of output buffer without writing it anywhere.
/// \param L Lua state in which Stream resides
/// \return Number of the results pushed to the stack