From b1a865dc160b720b935386e989e0a44c7218e708 Mon Sep 17 00:00:00 2001 From: Tobias Waldekranz Date: Tue, 2 Feb 2016 17:51:43 +0100 Subject: [PATCH] cleanup print output * Get rid of the Unicode bit pattern nonsense, it just adds noise. * Use emphasis instead of invert to mark enabled flags, easier on the eyes. Additionally, prefix flags with +/- to make the state explicit on terminals that do not handle ANSI escape sequences. * Align output to make it easier to read. --- phytool.h | 2 +- print_mv6.c | 32 ++++++++++++++++---------------- print_phy.c | 46 ++++++++++++++++++---------------------------- 3 files changed, 35 insertions(+), 45 deletions(-) diff --git a/phytool.h b/phytool.h index 3046b43..20de864 100644 --- a/phytool.h +++ b/phytool.h @@ -46,7 +46,7 @@ int phy_read (const struct loc *loc); int phy_write(const struct loc *loc, uint16_t val); uint32_t phy_id (const struct loc *loc); -void print_bit_array(uint16_t val, int indent); +void print_attr_name(const char *name, int indent); void print_bool(const char *name, int on); int print_phytool(const struct loc *loc, int indent); diff --git a/print_mv6.c b/print_mv6.c index b47a718..5ff89a8 100644 --- a/print_mv6.c +++ b/print_mv6.c @@ -91,7 +91,7 @@ static void mv6_port_ps(uint16_t val, int indent) printf("%*smv6: reg:PS(0x00) val:%#.4x\n", indent, "", val); - printf("%*sflags: ", indent + INDENT, ""); + print_attr_name("flags", indent + INDENT); print_bool("pause-en", val & 0x8000); putchar(' '); @@ -113,9 +113,10 @@ static void mv6_port_ps(uint16_t val, int indent) print_bool("flow-ctrl", val & 0x0010); putchar('\n'); - printf("%*sspeed: %d-%s\n", indent + INDENT, "", - speed, (val & 0x0400)? "full" : "half"); - printf("%*smode: %#x\n", indent + INDENT, "", val & 0xf); + print_attr_name("speed", indent + INDENT); + printf("%d-%s\n", speed, (val & 0x0400)? "full" : "half"); + print_attr_name("mode", indent + INDENT); + printf("%#x\n", val & 0xf); } static const char *mv6_egress_mode_str[] = { @@ -165,7 +166,7 @@ static void mv6_port_pc(uint16_t val, int indent) printf("%*smv6: reg:PC(0x04) val:%#.4x\n", indent, "", val); - printf("%*sflags: ", indent + INDENT, ""); + print_attr_name("flags", indent + INDENT); print_bool("router-header", val & 0x0800); putchar(' '); @@ -178,20 +179,20 @@ static void mv6_port_pc(uint16_t val, int indent) print_bool("tag-if-both", val & 0x0040); putchar('\n'); - printf("%*segress-mode: %s\n", indent + INDENT, "", - mv6_egress_mode_str[(val & 0x3000) >> 12]); + print_attr_name("egress-mode", indent + INDENT); + puts(mv6_egress_mode_str[(val & 0x3000) >> 12]); - printf("%*sframe-mode: %s\n", indent + INDENT, "", - mv6_frame_mode_str[(val & 0x0300) >> 8]); + print_attr_name("frame-mode", indent + INDENT); + puts(mv6_frame_mode_str[(val & 0x0300) >> 8]); - printf("%*sinitial-pri: %s\n", indent + INDENT, "", - mv6_initial_pri_str[(val & 0x0030) >> 4]); + print_attr_name("initial-pri", indent + INDENT); + puts(mv6_initial_pri_str[(val & 0x0030) >> 4]); - printf("%*segress-floods: %s\n", indent + INDENT, "", - mv6_egress_floods_str[(val & 0x000c) >> 2]); + print_attr_name("egress-floods", indent + INDENT); + puts(mv6_egress_floods_str[(val & 0x000c) >> 2]); - printf("%*sport-state: %s\n", indent + INDENT, "", - mv6_port_state_str[(val & 0x0003)]); + print_attr_name("port-state", indent + INDENT); + puts(mv6_port_state_str[(val & 0x0003)]); } struct mv6_port_desc { @@ -247,7 +248,6 @@ int mv6_port_one(const struct loc *loc, int indent, struct mv6_port_desc *pd) else pd->printer[loc->reg](val, indent); - print_bit_array(val, indent + INDENT); return 0; } diff --git a/print_phy.c b/print_phy.c index d2b3089..3afbc85 100644 --- a/print_phy.c +++ b/print_phy.c @@ -22,31 +22,12 @@ #include "phytool.h" -const char hi[] = { 0xe2, 0x97, 0x89, 0x00 }; -const char lo[] = { 0xe2, 0x97, 0x8b, 0x00 }; - -void print_bit_array(uint16_t val, int indent) -{ - int i; - - printf("%*s", indent, ""); - - for (i = 15; i >= 0; i--) { - fputs((val & (1 << i))? hi : lo, stdout); - - if (i) - fputs((i % 4 == 0)? " " : " ", stdout); - } - - printf("\n%*s %x %x %x %x\n", indent, "", - (val & 0xf000) >> 12, (val & 0x0f00) >> 8, - (val & 0x00f0) >> 4, (val & 0x000f)); -} - void print_bool(const char *name, int on) { if (on) - fputs("\e[7m", stdout); + fputs("\e[1m+", stdout); + else + fputs("-", stdout); fputs(name, stdout); @@ -54,6 +35,16 @@ void print_bool(const char *name, int on) fputs("\e[0m", stdout); } +void print_attr_name(const char *name, int indent) +{ + int start, end, len; + + printf("%*s%n%s:%n", indent, "", &start, name, &end); + + len = end - start; + printf("%*s", (len > 16) ? 0 : 16 - len, ""); +} + static void ieee_bmcr(uint16_t val, int indent) { int speed = 10; @@ -65,7 +56,7 @@ static void ieee_bmcr(uint16_t val, int indent) printf("%*sieee-phy: reg:BMCR(0x00) val:%#.4x\n", indent, "", val); - printf("%*sflags: ", indent + INDENT, ""); + print_attr_name("flags", indent + INDENT); print_bool("reset", val & BMCR_RESET); putchar(' '); @@ -87,15 +78,15 @@ static void ieee_bmcr(uint16_t val, int indent) print_bool("collision-test", val & BMCR_CTST); putchar('\n'); - printf("%*sspeed: %d-%s\n", indent + INDENT, "", speed, - (val & BMCR_FULLDPLX) ? "full" : "half"); + print_attr_name("speed", indent + INDENT); + printf("%d-%s\n", speed, (val & BMCR_FULLDPLX) ? "full" : "half"); } static void ieee_bmsr(uint16_t val, int indent) { printf("%*sieee-phy: reg:BMSR(0x01) val:%#.4x\n", indent, "", val); - printf("%*slink capabilities: ", indent + INDENT, ""); + print_attr_name("capabilities", indent + INDENT); print_bool("100-b4", val & BMSR_100BASE4); putchar(' '); @@ -117,7 +108,7 @@ static void ieee_bmsr(uint16_t val, int indent) print_bool("100-t2-h", val & BMSR_100HALF2); putchar('\n'); - printf("%*sflags: ", indent + INDENT, ""); + print_attr_name("flags", indent + INDENT); print_bool("ext-status", val & BMSR_ESTATEN); putchar(' '); @@ -158,7 +149,6 @@ static int ieee_one(const struct loc *loc, int indent) else ieee_reg_printers[loc->reg](val, indent); - print_bit_array(val, indent + INDENT); return 0; }