Merge pull request #2051 from cesanta/fmt

Split mg_*printf* functions into src/printf.c/h
This commit is contained in:
Sergey Lyubka 2023-02-10 16:52:34 +00:00 committed by GitHub
commit c0f4bde5fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 262 additions and 235 deletions

View File

@ -25,7 +25,7 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: { fetch-depth: 2 } with: { fetch-depth: 2 }
- id: check - 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 - if: steps.check.outputs.MATCH == 1
run: ./test/setup_ga_network.sh run: ./test/setup_ga_network.sh
- if: steps.check.outputs.MATCH == 1 - if: steps.check.outputs.MATCH == 1
@ -38,7 +38,7 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: { fetch-depth: 2 } with: { fetch-depth: 2 }
- id: check - 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 - if: steps.check.outputs.MATCH == 1
run: sudo apt-get update && sudo apt-get install qemu binfmt-support qemu-user-static run: sudo apt-get update && sudo apt-get install qemu binfmt-support qemu-user-static
- if: steps.check.outputs.MATCH == 1 - if: steps.check.outputs.MATCH == 1
@ -51,7 +51,7 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: { fetch-depth: 2 } with: { fetch-depth: 2 }
- id: check - 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 - if: steps.check.outputs.MATCH == 1
run: sudo apt-get update && sudo apt-get install qemu binfmt-support qemu-user-static run: sudo apt-get update && sudo apt-get install qemu binfmt-support qemu-user-static
- if: steps.check.outputs.MATCH == 1 - if: steps.check.outputs.MATCH == 1
@ -64,7 +64,7 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: { fetch-depth: 2 } with: { fetch-depth: 2 }
- id: check - 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 - if: steps.check.outputs.MATCH == 1
run: sudo apt-get update ; sudo apt-get install libmbedtls-dev valgrind run: sudo apt-get update ; sudo apt-get install libmbedtls-dev valgrind
- if: steps.check.outputs.MATCH == 1 - if: steps.check.outputs.MATCH == 1
@ -86,7 +86,7 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: { fetch-depth: 2 } with: { fetch-depth: 2 }
- id: check - 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 - if: steps.check.outputs.MATCH == 1
run: HOMEBREW_NO_AUTO_UPDATE=1 brew install jq mbedtls openssl run: HOMEBREW_NO_AUTO_UPDATE=1 brew install jq mbedtls openssl
- if: steps.check.outputs.MATCH == 1 - if: steps.check.outputs.MATCH == 1
@ -102,7 +102,7 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: { fetch-depth: 2 } with: { fetch-depth: 2 }
- id: check - 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 - if: steps.check.outputs.MATCH == 1
run: make vc98 vc17 vc22 mingw mingw++ run: make vc98 vc17 vc22 mingw mingw++
arduino-xiao: arduino-xiao:
@ -111,7 +111,7 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: { fetch-depth: 2 } with: { fetch-depth: 2 }
- id: check - 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 - if: steps.check.outputs.MATCH == 1
run: make arduino-xiao run: make arduino-xiao
arm: arm:
@ -193,7 +193,7 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: { fetch-depth: 2 } with: { fetch-depth: 2 }
- id: check - 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 - if: steps.check.outputs.MATCH == 1
run: make -C examples/zephyr init run: make -C examples/zephyr init
- name: minify manifest - name: minify manifest

View File

@ -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) { static bool is_digit(int c) {
return c >= '0' && c <= '9'; return c >= '0' && c <= '9';
} }
@ -646,6 +587,15 @@ static size_t bcpy(void (*out)(char, void *), void *ptr, uint8_t *buf,
return n; 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, size_t mg_vxprintf(void (*out)(char, void *), void *param, const char *fmt,
va_list *ap) { va_list *ap) {
size_t i = 0, n = 0; 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 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; static void *s_log_func_param = NULL;
void mg_log_set_fn(mg_pfn_t fn, void *param) { 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"; 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 #ifdef MG_ENABLE_LINES
#line 1 "src/rpc.c" #line 1 "src/rpc.c"
#endif #endif
@ -5552,44 +5598,6 @@ int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) {
return allowed == '+'; 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 #if MG_ENABLE_CUSTOM_MILLIS
#else #else
uint64_t mg_millis(void) { uint64_t mg_millis(void) {

View File

@ -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 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_vxprintf(void (*)(char, void *), void *, const char *fmt, va_list *);
size_t mg_xprintf(void (*fn)(char, void *), void *, const char *fmt, ...); 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_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap);
size_t mg_snprintf(char *, size_t, const char *fmt, ...); size_t mg_snprintf(char *, size_t, const char *fmt, ...);
char *mg_vmprintf(const char *fmt, va_list *ap); char *mg_vmprintf(const char *fmt, va_list *ap);
char *mg_mprintf(const char *fmt, ...); 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) \ #define MG_IPADDR_PARTS(ADDR) \
MG_U8P(ADDR)[0], MG_U8P(ADDR)[1], MG_U8P(ADDR)[2], MG_U8P(ADDR)[3] 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 // Linked list management macros
#define LIST_ADD_HEAD(type_, head_, elem_) \ #define LIST_ADD_HEAD(type_, head_, elem_) \
do { \ do { \

View File

@ -2,65 +2,6 @@
#include "iobuf.h" #include "iobuf.h"
#include "util.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) { static bool is_digit(int c) {
return c >= '0' && c <= '9'; return c >= '0' && c <= '9';
} }
@ -237,6 +178,15 @@ static size_t bcpy(void (*out)(char, void *), void *ptr, uint8_t *buf,
return n; 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, size_t mg_vxprintf(void (*out)(char, void *), void *param, const char *fmt,
va_list *ap) { va_list *ap) {
size_t i = 0, n = 0; size_t i = 0, n = 0;

View File

@ -3,13 +3,26 @@
#include "arch.h" #include "arch.h"
#include "iobuf.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 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_vxprintf(void (*)(char, void *), void *, const char *fmt, va_list *);
size_t mg_xprintf(void (*fn)(char, void *), void *, const char *fmt, ...); 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_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap);
size_t mg_snprintf(char *, size_t, const char *fmt, ...); size_t mg_snprintf(char *, size_t, const char *fmt, ...);
char *mg_vmprintf(const char *fmt, va_list *ap); char *mg_vmprintf(const char *fmt, va_list *ap);
char *mg_mprintf(const char *fmt, ...); 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

View File

@ -1,15 +1,10 @@
#include "log.h" #include "log.h"
#include "fmt.h" #include "printf.h"
#include "str.h" #include "str.h"
#include "util.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 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; static void *s_log_func_param = NULL;
void mg_log_set_fn(mg_pfn_t fn, void *param) { void mg_log_set_fn(mg_pfn_t fn, void *param) {

96
src/printf.c Normal file
View File

@ -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]);
}

View File

@ -87,44 +87,6 @@ int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) {
return allowed == '+'; 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 #if MG_ENABLE_CUSTOM_MILLIS
#else #else
uint64_t mg_millis(void) { uint64_t mg_millis(void) {

View File

@ -25,12 +25,6 @@ uint64_t mg_millis(void);
#define MG_IPADDR_PARTS(ADDR) \ #define MG_IPADDR_PARTS(ADDR) \
MG_U8P(ADDR)[0], MG_U8P(ADDR)[1], MG_U8P(ADDR)[2], MG_U8P(ADDR)[3] 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 // Linked list management macros
#define LIST_ADD_HEAD(type_, head_, elem_) \ #define LIST_ADD_HEAD(type_, head_, elem_) \
do { \ do { \

View File

@ -20,11 +20,11 @@
# MATCH=1. If no files match, write MATCH=0. # MATCH=1. If no files match, write MATCH=0.
for FILE in $(git --no-pager diff --name-only HEAD~1 HEAD) ; do for FILE in $(git --no-pager diff --name-only HEAD~1 HEAD) ; do
if [[ "$FILE" =~ $1 ]] ; then if [[ "$FILE" =~ $1 ]] ; then
echo FILE "$FILE" matches $1 # Log for debugging echo FILE "$FILE" matches "$1" # Log for debugging
echo MATCH=1 >> $GITHUB_OUTPUT echo MATCH=1 >> $GITHUB_OUTPUT # Set output
exit 0 # And exit early exit 0 # And exit early
else else
echo FILE "$FILE" DOES NOT match $1 # Log for debugging echo FILE "$FILE" DOES NOT match "$1" # Log for debugging
fi fi
done done
echo MATCH=0 >> $GITHUB_OUTPUT echo MATCH=0 >> $GITHUB_OUTPUT