From a4430dd49da981e327381a2f40befc18abe7f886 Mon Sep 17 00:00:00 2001 From: cpq Date: Fri, 10 Feb 2023 15:29:25 +0000 Subject: [PATCH] Split mg_*printf* functions into src/printf.c/h --- .github/workflows/test.yml | 16 +-- mongoose.c | 214 +++++++++++++++++++------------------ mongoose.h | 25 +++-- src/fmt.c | 68 ++---------- src/fmt.h | 17 ++- src/log.c | 9 +- src/printf.c | 96 +++++++++++++++++ src/util.c | 38 ------- src/util.h | 6 -- test/check.sh | 8 +- 10 files changed, 262 insertions(+), 235 deletions(-) create mode 100644 src/printf.c diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 288bc968..c2a5a6ad 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,7 +25,7 @@ jobs: - uses: actions/checkout@v3 with: { fetch-depth: 2 } - id: check - run: /bin/bash test/check.sh '^test|^src/*.[ch]' + run: /bin/bash test/check.sh '^test|^src/.*.[ch]' - if: steps.check.outputs.MATCH == 1 run: ./test/setup_ga_network.sh - if: steps.check.outputs.MATCH == 1 @@ -38,7 +38,7 @@ jobs: - uses: actions/checkout@v3 with: { fetch-depth: 2 } - id: check - run: /bin/bash test/check.sh '^test|^src/*.[ch]' + run: /bin/bash test/check.sh '^test|^src/.*.[ch]' - if: steps.check.outputs.MATCH == 1 run: sudo apt-get update && sudo apt-get install qemu binfmt-support qemu-user-static - if: steps.check.outputs.MATCH == 1 @@ -51,7 +51,7 @@ jobs: - uses: actions/checkout@v3 with: { fetch-depth: 2 } - id: check - run: /bin/bash test/check.sh '^test|^src/*.[ch]' + run: /bin/bash test/check.sh '^test|^src/.*.[ch]' - if: steps.check.outputs.MATCH == 1 run: sudo apt-get update && sudo apt-get install qemu binfmt-support qemu-user-static - if: steps.check.outputs.MATCH == 1 @@ -64,7 +64,7 @@ jobs: - uses: actions/checkout@v3 with: { fetch-depth: 2 } - id: check - run: /bin/bash test/check.sh '^test|^src/*.[ch]' + run: /bin/bash test/check.sh '^test|^src/.*.[ch]' - if: steps.check.outputs.MATCH == 1 run: sudo apt-get update ; sudo apt-get install libmbedtls-dev valgrind - if: steps.check.outputs.MATCH == 1 @@ -86,7 +86,7 @@ jobs: - uses: actions/checkout@v3 with: { fetch-depth: 2 } - id: check - run: /bin/bash test/check.sh '^test|^src/*.[ch]' + run: /bin/bash test/check.sh '^test|^src/.*.[ch]' - if: steps.check.outputs.MATCH == 1 run: HOMEBREW_NO_AUTO_UPDATE=1 brew install jq mbedtls openssl - if: steps.check.outputs.MATCH == 1 @@ -102,7 +102,7 @@ jobs: - uses: actions/checkout@v3 with: { fetch-depth: 2 } - id: check - run: /bin/bash test/check.sh '^test|^src/*.[ch]' + run: /bin/bash test/check.sh '^test|^src/.*.[ch]' - if: steps.check.outputs.MATCH == 1 run: make vc98 vc17 vc22 mingw mingw++ arduino-xiao: @@ -111,7 +111,7 @@ jobs: - uses: actions/checkout@v3 with: { fetch-depth: 2 } - id: check - run: /bin/bash test/check.sh '^examples/arduino|^src/*.[ch]' + run: /bin/bash test/check.sh '^examples/arduino|^src/.*.[ch]' - if: steps.check.outputs.MATCH == 1 run: make arduino-xiao arm: @@ -193,7 +193,7 @@ jobs: - uses: actions/checkout@v3 with: { fetch-depth: 2 } - id: check - run: /bin/bash test/check.sh '^examples/zephyr|^src/*.[ch]' + run: /bin/bash test/check.sh '^examples/zephyr|^src/.*.[ch]' - if: steps.check.outputs.MATCH == 1 run: make -C examples/zephyr init - name: minify manifest diff --git a/mongoose.c b/mongoose.c index 3ed21ec5..45891923 100644 --- a/mongoose.c +++ b/mongoose.c @@ -411,65 +411,6 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) { -static void mg_pfn_iobuf_private(char ch, void *param, bool expand) { - struct mg_iobuf *io = (struct mg_iobuf *) param; - if (expand && io->len + 2 > io->size) mg_iobuf_resize(io, io->len + 2); - if (io->len + 2 <= io->size) { - io->buf[io->len++] = (uint8_t) ch; - io->buf[io->len] = 0; - } else if (io->len < io->size) { - io->buf[io->len++] = 0; // Guarantee to 0-terminate - } -} - -static void mg_putchar_iobuf_static(char ch, void *param) { - mg_pfn_iobuf_private(ch, param, false); -} - -void mg_pfn_iobuf(char ch, void *param) { - mg_pfn_iobuf_private(ch, param, true); -} - -size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) { - struct mg_iobuf io = {(uint8_t *) buf, len, 0, 0}; - size_t n = mg_vxprintf(mg_putchar_iobuf_static, &io, fmt, ap); - if (n < len) buf[n] = '\0'; - return n; -} - -size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...) { - va_list ap; - size_t n; - va_start(ap, fmt); - n = mg_vsnprintf(buf, len, fmt, &ap); - va_end(ap); - return n; -} - -char *mg_vmprintf(const char *fmt, va_list *ap) { - struct mg_iobuf io = {0, 0, 0, 256}; - mg_vxprintf(mg_pfn_iobuf, &io, fmt, ap); - return (char *) io.buf; -} - -char *mg_mprintf(const char *fmt, ...) { - char *s; - va_list ap; - va_start(ap, fmt); - s = mg_vmprintf(fmt, &ap); - va_end(ap); - return s; -} - -size_t mg_xprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) { - size_t len = 0; - va_list ap; - va_start(ap, fmt); - len = mg_vxprintf(out, ptr, fmt, &ap); - va_end(ap); - return len; -} - static bool is_digit(int c) { return c >= '0' && c <= '9'; } @@ -646,6 +587,15 @@ static size_t bcpy(void (*out)(char, void *), void *ptr, uint8_t *buf, return n; } +size_t mg_xprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) { + size_t len = 0; + va_list ap; + va_start(ap, fmt); + len = mg_vxprintf(out, ptr, fmt, &ap); + va_end(ap); + return len; +} + size_t mg_vxprintf(void (*out)(char, void *), void *param, const char *fmt, va_list *ap) { size_t i = 0, n = 0; @@ -2748,13 +2698,8 @@ long mg_json_get_long(struct mg_str json, const char *path, long dflt) { -static void default_logger(char c, void *param) { - putchar(c); - (void) c, (void) param; -} - static int s_level = MG_LL_INFO; -static mg_pfn_t s_log_func = default_logger; +static mg_pfn_t s_log_func = mg_pfn_stdout; static void *s_log_func_param = NULL; void mg_log_set_fn(mg_pfn_t fn, void *param) { @@ -3537,6 +3482,107 @@ void mg_mgr_init(struct mg_mgr *mgr) { mgr->dns6.url = "udp://[2001:4860:4860::8888]:53"; } +#ifdef MG_ENABLE_LINES +#line 1 "src/printf.c" +#endif + + + + + +static void mg_pfn_iobuf_private(char ch, void *param, bool expand) { + struct mg_iobuf *io = (struct mg_iobuf *) param; + if (expand && io->len + 2 > io->size) mg_iobuf_resize(io, io->len + 2); + if (io->len + 2 <= io->size) { + io->buf[io->len++] = (uint8_t) ch; + io->buf[io->len] = 0; + } else if (io->len < io->size) { + io->buf[io->len++] = 0; // Guarantee to 0-terminate + } +} + +static void mg_putchar_iobuf_static(char ch, void *param) { + mg_pfn_iobuf_private(ch, param, false); +} + +void mg_pfn_iobuf(char ch, void *param) { + mg_pfn_iobuf_private(ch, param, true); +} + +size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) { + struct mg_iobuf io = {(uint8_t *) buf, len, 0, 0}; + size_t n = mg_vxprintf(mg_putchar_iobuf_static, &io, fmt, ap); + if (n < len) buf[n] = '\0'; + return n; +} + +size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...) { + va_list ap; + size_t n; + va_start(ap, fmt); + n = mg_vsnprintf(buf, len, fmt, &ap); + va_end(ap); + return n; +} + +char *mg_vmprintf(const char *fmt, va_list *ap) { + struct mg_iobuf io = {0, 0, 0, 256}; + mg_vxprintf(mg_pfn_iobuf, &io, fmt, ap); + return (char *) io.buf; +} + +char *mg_mprintf(const char *fmt, ...) { + char *s; + va_list ap; + va_start(ap, fmt); + s = mg_vmprintf(fmt, &ap); + va_end(ap); + return s; +} + +void mg_pfn_stdout(char c, void *param) { + putchar(c); + (void) param; +} + +static size_t print_ip4(void (*out)(char, void *), void *arg, uint8_t *p) { + return mg_xprintf(out, arg, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); +} + +static size_t print_ip6(void (*out)(char, void *), void *arg, uint16_t *p) { + return mg_xprintf(out, arg, "[%x:%x:%x:%x:%x:%x:%x:%x]", mg_ntohs(p[0]), + mg_ntohs(p[1]), mg_ntohs(p[2]), mg_ntohs(p[3]), + mg_ntohs(p[4]), mg_ntohs(p[5]), mg_ntohs(p[6]), + mg_ntohs(p[7])); +} + +size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap) { + uint8_t *p = va_arg(*ap, uint8_t *); + return print_ip4(out, arg, p); +} + +size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap) { + uint16_t *p = va_arg(*ap, uint16_t *); + return print_ip6(out, arg, p); +} + +size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap) { + struct mg_addr *addr = va_arg(*ap, struct mg_addr *); + if (addr->is_ip6) return print_ip6(out, arg, (uint16_t *) addr->ip6); + return print_ip4(out, arg, (uint8_t *) &addr->ip); +} + +size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap) { + struct mg_addr *a = va_arg(*ap, struct mg_addr *); + return mg_xprintf(out, arg, "%M:%hu", mg_print_ip, a, mg_ntohs(a->port)); +} + +size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap) { + uint8_t *p = va_arg(*ap, uint8_t *); + return mg_xprintf(out, arg, "%02x:%02x:%02x:%02x:%02x:%02x", p[0], p[1], p[2], + p[3], p[4], p[5]); +} + #ifdef MG_ENABLE_LINES #line 1 "src/rpc.c" #endif @@ -5552,44 +5598,6 @@ int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) { return allowed == '+'; } -static size_t print_ip4(void (*out)(char, void *), void *arg, uint8_t *p) { - return mg_xprintf(out, arg, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); -} - -static size_t print_ip6(void (*out)(char, void *), void *arg, uint16_t *p) { - return mg_xprintf(out, arg, "[%x:%x:%x:%x:%x:%x:%x:%x]", mg_ntohs(p[0]), - mg_ntohs(p[1]), mg_ntohs(p[2]), mg_ntohs(p[3]), - mg_ntohs(p[4]), mg_ntohs(p[5]), mg_ntohs(p[6]), - mg_ntohs(p[7])); -} - -size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap) { - uint8_t *p = va_arg(*ap, uint8_t *); - return print_ip4(out, arg, p); -} - -size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap) { - uint16_t *p = va_arg(*ap, uint16_t *); - return print_ip6(out, arg, p); -} - -size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap) { - struct mg_addr *addr = va_arg(*ap, struct mg_addr *); - if (addr->is_ip6) return print_ip6(out, arg, (uint16_t *) addr->ip6); - return print_ip4(out, arg, (uint8_t *) &addr->ip); -} - -size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap) { - struct mg_addr *a = va_arg(*ap, struct mg_addr *); - return mg_xprintf(out, arg, "%M:%hu", mg_print_ip, a, mg_ntohs(a->port)); -} - -size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap) { - uint8_t *p = va_arg(*ap, uint8_t *); - return mg_xprintf(out, arg, "%02x:%02x:%02x:%02x:%02x:%02x", p[0], p[1], p[2], - p[3], p[4], p[5]); -} - #if MG_ENABLE_CUSTOM_MILLIS #else uint64_t mg_millis(void) { diff --git a/mongoose.h b/mongoose.h index ed9af8dc..4ea28615 100644 --- a/mongoose.h +++ b/mongoose.h @@ -787,17 +787,32 @@ char *mg_remove_double_dots(char *s); -typedef void (*mg_pfn_t)(char, void *); // Custom putchar +typedef void (*mg_pfn_t)(char, void *); // Output function typedef size_t (*mg_pm_t)(mg_pfn_t, void *, va_list *); // %M printer -void mg_pfn_iobuf(char ch, void *param); // iobuf printer size_t mg_vxprintf(void (*)(char, void *), void *, const char *fmt, va_list *); size_t mg_xprintf(void (*fn)(char, void *), void *, const char *fmt, ...); + + + +// Convenience wrappers around mg_xprintf size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap); size_t mg_snprintf(char *, size_t, const char *fmt, ...); char *mg_vmprintf(const char *fmt, va_list *ap); char *mg_mprintf(const char *fmt, ...); +// %M print helper functions +size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap); +size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap); +size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap); +size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap); +size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap); + +// Various output functions +void mg_pfn_iobuf(char ch, void *param); // param: struct mg_iobuf * +void mg_pfn_queue(char ch, void *param); // param: struct mg_queue * +void mg_pfn_stdout(char c, void *param); // param: ignored + @@ -918,12 +933,6 @@ uint64_t mg_millis(void); #define MG_IPADDR_PARTS(ADDR) \ MG_U8P(ADDR)[0], MG_U8P(ADDR)[1], MG_U8P(ADDR)[2], MG_U8P(ADDR)[3] -size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap); -size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap); -size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap); -size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap); -size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap); - // Linked list management macros #define LIST_ADD_HEAD(type_, head_, elem_) \ do { \ diff --git a/src/fmt.c b/src/fmt.c index a66231e1..c14c21f2 100644 --- a/src/fmt.c +++ b/src/fmt.c @@ -2,65 +2,6 @@ #include "iobuf.h" #include "util.h" -static void mg_pfn_iobuf_private(char ch, void *param, bool expand) { - struct mg_iobuf *io = (struct mg_iobuf *) param; - if (expand && io->len + 2 > io->size) mg_iobuf_resize(io, io->len + 2); - if (io->len + 2 <= io->size) { - io->buf[io->len++] = (uint8_t) ch; - io->buf[io->len] = 0; - } else if (io->len < io->size) { - io->buf[io->len++] = 0; // Guarantee to 0-terminate - } -} - -static void mg_putchar_iobuf_static(char ch, void *param) { - mg_pfn_iobuf_private(ch, param, false); -} - -void mg_pfn_iobuf(char ch, void *param) { - mg_pfn_iobuf_private(ch, param, true); -} - -size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) { - struct mg_iobuf io = {(uint8_t *) buf, len, 0, 0}; - size_t n = mg_vxprintf(mg_putchar_iobuf_static, &io, fmt, ap); - if (n < len) buf[n] = '\0'; - return n; -} - -size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...) { - va_list ap; - size_t n; - va_start(ap, fmt); - n = mg_vsnprintf(buf, len, fmt, &ap); - va_end(ap); - return n; -} - -char *mg_vmprintf(const char *fmt, va_list *ap) { - struct mg_iobuf io = {0, 0, 0, 256}; - mg_vxprintf(mg_pfn_iobuf, &io, fmt, ap); - return (char *) io.buf; -} - -char *mg_mprintf(const char *fmt, ...) { - char *s; - va_list ap; - va_start(ap, fmt); - s = mg_vmprintf(fmt, &ap); - va_end(ap); - return s; -} - -size_t mg_xprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) { - size_t len = 0; - va_list ap; - va_start(ap, fmt); - len = mg_vxprintf(out, ptr, fmt, &ap); - va_end(ap); - return len; -} - static bool is_digit(int c) { return c >= '0' && c <= '9'; } @@ -237,6 +178,15 @@ static size_t bcpy(void (*out)(char, void *), void *ptr, uint8_t *buf, return n; } +size_t mg_xprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) { + size_t len = 0; + va_list ap; + va_start(ap, fmt); + len = mg_vxprintf(out, ptr, fmt, &ap); + va_end(ap); + return len; +} + size_t mg_vxprintf(void (*out)(char, void *), void *param, const char *fmt, va_list *ap) { size_t i = 0, n = 0; diff --git a/src/fmt.h b/src/fmt.h index db3b6fa1..7a217e8b 100644 --- a/src/fmt.h +++ b/src/fmt.h @@ -3,13 +3,26 @@ #include "arch.h" #include "iobuf.h" -typedef void (*mg_pfn_t)(char, void *); // Custom putchar +typedef void (*mg_pfn_t)(char, void *); // Output function typedef size_t (*mg_pm_t)(mg_pfn_t, void *, va_list *); // %M printer -void mg_pfn_iobuf(char ch, void *param); // iobuf printer +// The lowest level size_t mg_vxprintf(void (*)(char, void *), void *, const char *fmt, va_list *); size_t mg_xprintf(void (*fn)(char, void *), void *, const char *fmt, ...); + +// Convenience wrappers around mg_xprintf size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap); size_t mg_snprintf(char *, size_t, const char *fmt, ...); char *mg_vmprintf(const char *fmt, va_list *ap); char *mg_mprintf(const char *fmt, ...); + +// %M print helper functions +size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap); +size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap); +size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap); +size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap); +size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap); + +// Various output functions +void mg_pfn_iobuf(char ch, void *param); // param: struct mg_iobuf * +void mg_pfn_stdout(char c, void *param); // param: ignored diff --git a/src/log.c b/src/log.c index 72f32bad..79a0c2c0 100644 --- a/src/log.c +++ b/src/log.c @@ -1,15 +1,10 @@ #include "log.h" -#include "fmt.h" +#include "printf.h" #include "str.h" #include "util.h" -static void default_logger(char c, void *param) { - putchar(c); - (void) c, (void) param; -} - static int s_level = MG_LL_INFO; -static mg_pfn_t s_log_func = default_logger; +static mg_pfn_t s_log_func = mg_pfn_stdout; static void *s_log_func_param = NULL; void mg_log_set_fn(mg_pfn_t fn, void *param) { diff --git a/src/printf.c b/src/printf.c new file mode 100644 index 00000000..a4de5153 --- /dev/null +++ b/src/printf.c @@ -0,0 +1,96 @@ +#include "fmt.h" +#include "iobuf.h" +#include "util.h" + +static void mg_pfn_iobuf_private(char ch, void *param, bool expand) { + struct mg_iobuf *io = (struct mg_iobuf *) param; + if (expand && io->len + 2 > io->size) mg_iobuf_resize(io, io->len + 2); + if (io->len + 2 <= io->size) { + io->buf[io->len++] = (uint8_t) ch; + io->buf[io->len] = 0; + } else if (io->len < io->size) { + io->buf[io->len++] = 0; // Guarantee to 0-terminate + } +} + +static void mg_putchar_iobuf_static(char ch, void *param) { + mg_pfn_iobuf_private(ch, param, false); +} + +void mg_pfn_iobuf(char ch, void *param) { + mg_pfn_iobuf_private(ch, param, true); +} + +size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) { + struct mg_iobuf io = {(uint8_t *) buf, len, 0, 0}; + size_t n = mg_vxprintf(mg_putchar_iobuf_static, &io, fmt, ap); + if (n < len) buf[n] = '\0'; + return n; +} + +size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...) { + va_list ap; + size_t n; + va_start(ap, fmt); + n = mg_vsnprintf(buf, len, fmt, &ap); + va_end(ap); + return n; +} + +char *mg_vmprintf(const char *fmt, va_list *ap) { + struct mg_iobuf io = {0, 0, 0, 256}; + mg_vxprintf(mg_pfn_iobuf, &io, fmt, ap); + return (char *) io.buf; +} + +char *mg_mprintf(const char *fmt, ...) { + char *s; + va_list ap; + va_start(ap, fmt); + s = mg_vmprintf(fmt, &ap); + va_end(ap); + return s; +} + +void mg_pfn_stdout(char c, void *param) { + putchar(c); + (void) param; +} + +static size_t print_ip4(void (*out)(char, void *), void *arg, uint8_t *p) { + return mg_xprintf(out, arg, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); +} + +static size_t print_ip6(void (*out)(char, void *), void *arg, uint16_t *p) { + return mg_xprintf(out, arg, "[%x:%x:%x:%x:%x:%x:%x:%x]", mg_ntohs(p[0]), + mg_ntohs(p[1]), mg_ntohs(p[2]), mg_ntohs(p[3]), + mg_ntohs(p[4]), mg_ntohs(p[5]), mg_ntohs(p[6]), + mg_ntohs(p[7])); +} + +size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap) { + uint8_t *p = va_arg(*ap, uint8_t *); + return print_ip4(out, arg, p); +} + +size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap) { + uint16_t *p = va_arg(*ap, uint16_t *); + return print_ip6(out, arg, p); +} + +size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap) { + struct mg_addr *addr = va_arg(*ap, struct mg_addr *); + if (addr->is_ip6) return print_ip6(out, arg, (uint16_t *) addr->ip6); + return print_ip4(out, arg, (uint8_t *) &addr->ip); +} + +size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap) { + struct mg_addr *a = va_arg(*ap, struct mg_addr *); + return mg_xprintf(out, arg, "%M:%hu", mg_print_ip, a, mg_ntohs(a->port)); +} + +size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap) { + uint8_t *p = va_arg(*ap, uint8_t *); + return mg_xprintf(out, arg, "%02x:%02x:%02x:%02x:%02x:%02x", p[0], p[1], p[2], + p[3], p[4], p[5]); +} diff --git a/src/util.c b/src/util.c index 9b7940a3..6f7f5c0c 100644 --- a/src/util.c +++ b/src/util.c @@ -87,44 +87,6 @@ int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) { return allowed == '+'; } -static size_t print_ip4(void (*out)(char, void *), void *arg, uint8_t *p) { - return mg_xprintf(out, arg, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); -} - -static size_t print_ip6(void (*out)(char, void *), void *arg, uint16_t *p) { - return mg_xprintf(out, arg, "[%x:%x:%x:%x:%x:%x:%x:%x]", mg_ntohs(p[0]), - mg_ntohs(p[1]), mg_ntohs(p[2]), mg_ntohs(p[3]), - mg_ntohs(p[4]), mg_ntohs(p[5]), mg_ntohs(p[6]), - mg_ntohs(p[7])); -} - -size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap) { - uint8_t *p = va_arg(*ap, uint8_t *); - return print_ip4(out, arg, p); -} - -size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap) { - uint16_t *p = va_arg(*ap, uint16_t *); - return print_ip6(out, arg, p); -} - -size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap) { - struct mg_addr *addr = va_arg(*ap, struct mg_addr *); - if (addr->is_ip6) return print_ip6(out, arg, (uint16_t *) addr->ip6); - return print_ip4(out, arg, (uint8_t *) &addr->ip); -} - -size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap) { - struct mg_addr *a = va_arg(*ap, struct mg_addr *); - return mg_xprintf(out, arg, "%M:%hu", mg_print_ip, a, mg_ntohs(a->port)); -} - -size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap) { - uint8_t *p = va_arg(*ap, uint8_t *); - return mg_xprintf(out, arg, "%02x:%02x:%02x:%02x:%02x:%02x", p[0], p[1], p[2], - p[3], p[4], p[5]); -} - #if MG_ENABLE_CUSTOM_MILLIS #else uint64_t mg_millis(void) { diff --git a/src/util.h b/src/util.h index 9c0f7539..4b5cc696 100644 --- a/src/util.h +++ b/src/util.h @@ -25,12 +25,6 @@ uint64_t mg_millis(void); #define MG_IPADDR_PARTS(ADDR) \ MG_U8P(ADDR)[0], MG_U8P(ADDR)[1], MG_U8P(ADDR)[2], MG_U8P(ADDR)[3] -size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap); -size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap); -size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap); -size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap); -size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap); - // Linked list management macros #define LIST_ADD_HEAD(type_, head_, elem_) \ do { \ diff --git a/test/check.sh b/test/check.sh index 43d58aa8..a46bd0b7 100644 --- a/test/check.sh +++ b/test/check.sh @@ -20,11 +20,11 @@ # MATCH=1. If no files match, write MATCH=0. for FILE in $(git --no-pager diff --name-only HEAD~1 HEAD) ; do if [[ "$FILE" =~ $1 ]] ; then - echo FILE "$FILE" matches $1 # Log for debugging - echo MATCH=1 >> $GITHUB_OUTPUT - exit 0 # And exit early + echo FILE "$FILE" matches "$1" # Log for debugging + echo MATCH=1 >> $GITHUB_OUTPUT # Set output + exit 0 # And exit early else - echo FILE "$FILE" DOES NOT match $1 # Log for debugging + echo FILE "$FILE" DOES NOT match "$1" # Log for debugging fi done echo MATCH=0 >> $GITHUB_OUTPUT