From 73297c8e6522a6399616e24d0e791162d2121c1d Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Wed, 29 Jun 2022 17:42:55 +0100 Subject: [PATCH] Use mg_putchar_iobuf in mg_vprintf --- Makefile | 4 ++-- mongoose.c | 23 ++++++++++++++++------- mongoose.h | 1 + src/fmt.c | 15 +++++++++++++-- src/net.c | 8 +++----- src/str.h | 1 + 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 4833f74b..0c4a3170 100644 --- a/Makefile +++ b/Makefile @@ -109,16 +109,16 @@ riscv: DEFS += -DMG_ENABLE_FILE=0 -DMG_ENABLE_MIP=1 -DMG_ARCH=MG_ARCH_NEWLIB riscv: mongoose.h $(SRCS) $(DOCKER) mdashnet/riscv riscv-none-elf-gcc -march=rv32imc -mabi=ilp32 $(SRCS) $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -o unit_test -#vc98: VCFLAGS += -DMG_ENABLE_IPV6=1 vc98: Makefile mongoose.h $(SRCS) $(DOCKER) mdashnet/vc98 wine cl $(SRCS) $(VCFLAGS) ws2_32.lib /Fe$@.exe $(DOCKER) mdashnet/vc98 wine $@.exe -#vc2017: VCFLAGS += -DMG_ENABLE_IPV6=1 +# vc2017: DEFS += -DMG_ENABLE_IPV6=1 vc2017: Makefile mongoose.h $(SRCS) $(DOCKER) mdashnet/vc2017 wine64 cl $(SRCS) $(VCFLAGS) ws2_32.lib /Fe$@.exe $(DOCKER) mdashnet/vc2017 wine64 $@.exe +# vc22: DEFS += -DMG_ENABLE_IPV6=$(IPV6) vc22: Makefile mongoose.h $(SRCS) $(DOCKER) mdashnet/vc22 wine64 cl $(SRCS) $(VCFLAGS) ws2_32.lib /Fe$@.exe $(DOCKER) mdashnet/vc22 wine64 $@.exe diff --git a/mongoose.c b/mongoose.c index ac13edb5..dce2a13d 100644 --- a/mongoose.c +++ b/mongoose.c @@ -598,7 +598,6 @@ char *mg_mprintf(const char *fmt, ...) { return s; } - size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) { size_t len = 0; va_list ap; @@ -610,7 +609,19 @@ size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) { static void mg_putchar_iobuf_static(char ch, void *param) { struct mg_iobuf *io = (struct mg_iobuf *) param; - if (io->len < io->size) io->buf[io->len++] = (uint8_t) ch; + if (io->len + 2 <= io->size) { + io->buf[io->len++] = (uint8_t) ch; + io->buf[io->len] = 0; + } +} + +static void mg_putchar_iobuf(char ch, void *param) { + struct mg_iobuf *io = (struct mg_iobuf *) param; + if (io->len + 2 > io->size) mg_iobuf_resize(io, io->size + 64); + if (io->len + 2 <= io->size) { + io->buf[io->len++] = (uint8_t) ch; + io->buf[io->len] = 0; + } } // We don't use realloc() in mongoose, so resort to inefficient calloc @@ -3290,11 +3301,9 @@ struct mg_connection *mg_mqtt_listen(struct mg_mgr *mgr, const char *url, size_t mg_vprintf(struct mg_connection *c, const char *fmt, va_list ap) { - char mem[256], *buf = mem; - size_t len = mg_vasprintf(&buf, sizeof(mem), fmt, ap); - len = mg_send(c, buf, len); - if (buf != mem) free(buf); - return len; + size_t old = c->send.len; + mg_vrprintf(mg_putchar_iobuf, &c->send, fmt, &ap); + return c->send.len - old; } size_t mg_printf(struct mg_connection *c, const char *fmt, ...) { diff --git a/mongoose.h b/mongoose.h index 72b5c8e0..72bb82f6 100644 --- a/mongoose.h +++ b/mongoose.h @@ -728,6 +728,7 @@ size_t mg_dtoa(char *buf, size_t len, double d, int width); typedef void (*mg_pc_t)(char, void *); // Custom putchar typedef size_t (*mg_pm_t)(mg_pc_t, void *, va_list *); // %M printer void mg_putchar_realloc(char ch, void *param); // Print to malloced str +void mg_putchar_iobuf(char ch, void *param); // Print to iobuf size_t mg_vrprintf(void (*)(char, void *), void *, const char *fmt, va_list *); size_t mg_rprintf(void (*fn)(char, void *), void *, const char *fmt, ...); diff --git a/src/fmt.c b/src/fmt.c index 362391dd..71e2f68c 100644 --- a/src/fmt.c +++ b/src/fmt.c @@ -47,7 +47,6 @@ char *mg_mprintf(const char *fmt, ...) { return s; } - size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) { size_t len = 0; va_list ap; @@ -59,7 +58,19 @@ size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) { static void mg_putchar_iobuf_static(char ch, void *param) { struct mg_iobuf *io = (struct mg_iobuf *) param; - if (io->len < io->size) io->buf[io->len++] = (uint8_t) ch; + if (io->len + 2 <= io->size) { + io->buf[io->len++] = (uint8_t) ch; + io->buf[io->len] = 0; + } +} + +static void mg_putchar_iobuf(char ch, void *param) { + struct mg_iobuf *io = (struct mg_iobuf *) param; + if (io->len + 2 > io->size) mg_iobuf_resize(io, io->size + 64); + if (io->len + 2 <= io->size) { + io->buf[io->len++] = (uint8_t) ch; + io->buf[io->len] = 0; + } } // We don't use realloc() in mongoose, so resort to inefficient calloc diff --git a/src/net.c b/src/net.c index 7cefd42e..00215504 100644 --- a/src/net.c +++ b/src/net.c @@ -6,11 +6,9 @@ #include "util.h" size_t mg_vprintf(struct mg_connection *c, const char *fmt, va_list ap) { - char mem[256], *buf = mem; - size_t len = mg_vasprintf(&buf, sizeof(mem), fmt, ap); - len = mg_send(c, buf, len); - if (buf != mem) free(buf); - return len; + size_t old = c->send.len; + mg_vrprintf(mg_putchar_iobuf, &c->send, fmt, &ap); + return c->send.len - old; } size_t mg_printf(struct mg_connection *c, const char *fmt, ...) { diff --git a/src/str.h b/src/str.h index 4aac6960..71b89343 100644 --- a/src/str.h +++ b/src/str.h @@ -44,6 +44,7 @@ size_t mg_dtoa(char *buf, size_t len, double d, int width); typedef void (*mg_pc_t)(char, void *); // Custom putchar typedef size_t (*mg_pm_t)(mg_pc_t, void *, va_list *); // %M printer void mg_putchar_realloc(char ch, void *param); // Print to malloced str +void mg_putchar_iobuf(char ch, void *param); // Print to iobuf size_t mg_vrprintf(void (*)(char, void *), void *, const char *fmt, va_list *); size_t mg_rprintf(void (*fn)(char, void *), void *, const char *fmt, ...);