From a4cb97a6c1d8718be40a16c1c7fc0b2738d17947 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Fri, 19 Feb 2021 00:08:11 +0100 Subject: lib: add `%*pHX` + `%*pHS` hexdump in printfrr (I'll get to `zlog_hexdump()` in a separate pass.) Signed-off-by: David Lamparter --- lib/printfrr.h | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'lib/printfrr.h') diff --git a/lib/printfrr.h b/lib/printfrr.h index 8ea8fd69a..7083e8b58 100644 --- a/lib/printfrr.h +++ b/lib/printfrr.h @@ -227,7 +227,11 @@ void printfrr_ext_reg(const struct printfrr_ext *); } \ /* end */ -/* fbuf helper functions */ +/* fbuf helper functions - note all 3 of these return the length that would + * be written regardless of how much space was available in the buffer, as + * needed for implementing printfrr extensions. (They also accept NULL buf + * for that.) + */ static inline ssize_t bputs(struct fbuf *buf, const char *str) { @@ -251,6 +255,17 @@ static inline ssize_t bputch(struct fbuf *buf, char ch) return 1; } +static inline ssize_t bputhex(struct fbuf *buf, uint8_t val) +{ + static const char hexch[] = "0123456789abcdef"; + + if (buf && buf->pos < buf->buf + buf->len) + *buf->pos++ = hexch[(val >> 4) & 0xf]; + if (buf && buf->pos < buf->buf + buf->len) + *buf->pos++ = hexch[val & 0xf]; + return 2; +} + /* %pVA extension, equivalent to Linux kernel %pV */ struct va_format { @@ -261,6 +276,13 @@ struct va_format { #ifdef _FRR_ATTRIBUTE_PRINTFRR #pragma FRR printfrr_ext "%pFB" (struct fbuf *) #pragma FRR printfrr_ext "%pVA" (struct va_format *) + +#pragma FRR printfrr_ext "%pHX" (signed char *) +#pragma FRR printfrr_ext "%pHX" (unsigned char *) +#pragma FRR printfrr_ext "%pHX" (void *) +#pragma FRR printfrr_ext "%pHS" (signed char *) +#pragma FRR printfrr_ext "%pHS" (unsigned char *) +#pragma FRR printfrr_ext "%pHS" (void *) #endif /* when using non-ISO-C compatible extension specifiers... */ -- cgit v1.2.3