From 38c611cff6d03383e111dd4c9c9f2c5f8dfc87d6 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 21 Aug 2021 16:44:02 +0200 Subject: Moved error handling and lua state out of buffer_prepare_at_least --- buffer.c | 16 ++-------------- buffer.h | 4 +--- stream.c | 12 +++++++++++- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/buffer.c b/buffer.c index a3f527f..ab39cb7 100644 --- a/buffer.c +++ b/buffer.c @@ -5,10 +5,6 @@ #include #include -#include - -#include "stream.h" - int buffer_grow(struct buffer * b) { int allocated = b->allocated + 1024; @@ -57,7 +53,7 @@ int buffer_read_more(int fd, struct buffer * in, int minimum_length) return length; } -int buffer_prepare_at_least(lua_State * L, int fd, struct buffer * in, int minimum_length, lua_KContext ctx) +int buffer_prepare_at_least(int fd, struct buffer * in, int minimum_length) { const int remaining_bytes = in->length - in->next; @@ -65,15 +61,7 @@ int buffer_prepare_at_least(lua_State * L, int fd, struct buffer * in, int minim { const int res = buffer_read_more(fd, in, minimum_length); if (-1 == res) - { - if (EWOULDBLOCK == errno || EAGAIN == errno) - return lua_yieldk(L, 0, ctx, stream_readk); - else - { - lua_pushstring(L, strerror(errno)); - return lua_error(L); - } - } + return res; } return remaining_bytes; diff --git a/buffer.h b/buffer.h index c051f2d..1a614ec 100644 --- a/buffer.h +++ b/buffer.h @@ -1,7 +1,5 @@ #pragma once -#include - struct buffer { char * data; @@ -13,5 +11,5 @@ struct buffer int buffer_grow(struct buffer *); int buffer_read_more(const int, struct buffer *, const int); -int buffer_prepare_at_least(lua_State *, const int, struct buffer *, const int, lua_KContext); +int buffer_prepare_at_least(const int, struct buffer *, const int); int buffer_until(struct buffer *, const char *, const int); diff --git a/stream.c b/stream.c index 9cf475e..eb1d0ea 100644 --- a/stream.c +++ b/stream.c @@ -155,7 +155,17 @@ int stream_readk(lua_State * L, int status, lua_KContext ctx) int remaining_bytes; do { - remaining_bytes = buffer_prepare_at_least(L, s->fd, &s->in, (int) pattern_length, ctx); + remaining_bytes = buffer_prepare_at_least(s->fd, &s->in, (int) pattern_length); + if (-1 == remaining_bytes) + { + if (EWOULDBLOCK == errno || EAGAIN == errno) + return lua_yieldk(L, 0, ctx, stream_readk); + else + { + lua_pushstring(L, strerror(errno)); + return lua_error(L); + } + } offset = buffer_until(&s->in, pattern, (int) pattern_length); } while (-1 == offset && 0 < remaining_bytes); -- cgit v1.1