Skip to content

Commit

Permalink
cleanup print output
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
wkz committed Feb 2, 2016
1 parent ffde8b6 commit b1a865d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 45 deletions.
2 changes: 1 addition & 1 deletion phytool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 16 additions & 16 deletions print_mv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');

Expand All @@ -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[] = {
Expand Down Expand Up @@ -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(' ');

Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down
46 changes: 18 additions & 28 deletions print_phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,29 @@

#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);

if (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;
Expand All @@ -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(' ');

Expand All @@ -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(' ');

Expand All @@ -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(' ');

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit b1a865d

Please sign in to comment.