From 5f072cb7f02f992bbb318d0344a40bd534d8b8b3 Mon Sep 17 00:00:00 2001 From: enami Date: Fri, 16 Jul 2004 23:28:20 +0000 Subject: [PATCH] Print flags symbolically. --- regress/lib/libc/gen/humanize_number/hntest.c | 130 +++++++++++++++++- 1 file changed, 123 insertions(+), 7 deletions(-) diff --git a/regress/lib/libc/gen/humanize_number/hntest.c b/regress/lib/libc/gen/humanize_number/hntest.c index 88a0283df9a3..7c151f789a65 100644 --- a/regress/lib/libc/gen/humanize_number/hntest.c +++ b/regress/lib/libc/gen/humanize_number/hntest.c @@ -1,7 +1,8 @@ -/* $NetBSD: hntest.c,v 1.1 2004/07/14 22:47:31 enami Exp $ */ +/* $NetBSD: hntest.c,v 1.2 2004/07/16 23:28:20 enami Exp $ */ #include #include +#include #include #include #include @@ -31,6 +32,13 @@ const struct hnopts { */ { 5, 1, "", 0, HN_NOSPACE, 1, "1" }, + { 5, 1, "", 0, 0, 2, "1 " }, /* just for reference */ + { 5, 1, "", 0, HN_B, 3, "1 B" }, /* and more ... */ + { 5, 1, "", 0, HN_DECIMAL, 2, "1 " }, + { 5, 1, "", 0, HN_NOSPACE | HN_B, 2, "1B" }, + { 5, 1, "", 0, HN_B | HN_DECIMAL, 3, "1 B" }, + { 5, 1, "", 0, HN_NOSPACE | HN_B | HN_DECIMAL, 2, "1B" }, + /* * Space and HN_B. Rev. 1.7 produces "1B". */ @@ -44,9 +52,110 @@ const struct hnopts { { 6, 1000, "A", HN_AUTOSCALE, HN_DECIMAL, -1, "" }, }; +struct hnflags { + int hf_flags; + const char *hf_name; +}; + +const struct hnflags scale_flags[] = { + { HN_GETSCALE, "HN_GETSCALE" }, + { HN_AUTOSCALE, "HN_AUTOSCALE" }, +}; +const struct hnflags normal_flags[] = { + { HN_DECIMAL, "HN_DECIMAL" }, + { HN_NOSPACE, "HN_NOSPACE" }, + { HN_B, "HN_B" }, + { HN_DIVISOR_1000, "HN_DIVISOR_1000" }, +}; + +const char * + formatflags(char *, size_t, const struct hnflags *, size_t, int); +void newline(void); +void wprintf(const char *, ...); +int main(int, char *[]); + +const char * +formatflags(char *buf, size_t buflen, const struct hnflags *hfs, + size_t hfslen, int flags) +{ + const struct hnflags *hf; + char *p = buf; + size_t len = buflen; + int i, found, n; + + if (flags == 0) { + snprintf(buf, buflen, "0"); + return (buf); + } + for (i = found = 0; i < hfslen && flags & ~found; i++) { + hf = &hfs[i]; + if (flags & hf->hf_flags) { + found |= hf->hf_flags; + n = snprintf(p, len, "|%s", hf->hf_name); + if (n >= len) { + p = buf; + len = buflen; + /* Print `flags' as number */ + goto bad; + } + p += n; + len -= n; + } + } + flags &= ~found; + if (flags) +bad: + snprintf(p, len, "|0x%x", flags); + return (*buf == '|' ? buf + 1 : buf); +} + +static int col, bol = 1; +void +newline(void) +{ + + fprintf(stderr, "\n"); + col = 0; + bol = 1; +} + +void +wprintf(const char *fmt, ...) +{ + char buf[80]; + va_list ap; + int n; + + va_start(ap, fmt); + if (col >= 0) { + n = vsnprintf(buf, sizeof(buf), fmt, ap); + if (n >= sizeof(buf)) { + col = -1; + goto overflow; + } else if (n == 0) + goto out; + + if (!bol) { + if (col + n > 75) + fprintf(stderr, "\n "), col = 4; + else + fprintf(stderr, " "), col++; + } + fprintf(stderr, "%s", buf); + col += n; + bol = 0; + } else { +overflow: + vfprintf(stderr, fmt, ap); + } +out: + va_end(ap); +} + int main(int argc, char *argv[]) { + char fbuf[128]; const struct hnopts *ho; char *buf = NULL; size_t buflen = 0; @@ -68,12 +177,19 @@ main(int argc, char *argv[]) (rv == -1 || strcmp(buf, ho->ho_retstr) == 0)) continue; - fprintf(stderr, - "humanize_number(\"%s\", %d, %" PRId64 - ", \"%s\", 0x%x, 0x%x) = %d, but got %d/[%s]\n", - ho->ho_retstr, ho->ho_len, ho->ho_num, - ho->ho_suffix, ho->ho_scale, ho->ho_flags, - ho->ho_retval, rv, rv == -1 ? "" : buf); + wprintf("humanize_number(\"%s\", %d, %" PRId64 ",", + ho->ho_retstr, ho->ho_len, ho->ho_num); + wprintf("\"%s\",", ho->ho_suffix); + wprintf("%s,", formatflags(fbuf, sizeof(fbuf), scale_flags, + sizeof(scale_flags) / sizeof(scale_flags[0]), + ho->ho_scale)); + wprintf("%s)", formatflags(fbuf, sizeof(fbuf), normal_flags, + sizeof(normal_flags) / sizeof(normal_flags[0]), + ho->ho_flags)); + wprintf("= %d,", ho->ho_retval); + wprintf("but got"); + wprintf("%d/[%s]", rv, rv == -1 ? "" : buf); + newline(); error = 1; } exit(error ? EXIT_FAILURE : EXIT_SUCCESS);