diff options
-rwxr-xr-x | curtis/.bin/xx.c | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/curtis/.bin/xx.c b/curtis/.bin/xx.c index 269c9d55..1c77fcd5 100755 --- a/curtis/.bin/xx.c +++ b/curtis/.bin/xx.c @@ -20,13 +20,18 @@ static bool allZero(const uint8_t *buf, size_t len) { } enum { - FLAG_ASCII = 1, - FLAG_OFFSET = 2, - FLAG_SKIP = 4, - FLAG_UNDUMP = 8, + FLAG_ASCII = 1 << 0, + FLAG_COLOR = 1 << 1, + FLAG_OFFSET = 1 << 2, + FLAG_SKIP = 1 << 3, + FLAG_UNDUMP = 1 << 4, }; -void dump(size_t cols, size_t group, uint8_t flags, FILE *file) { +static void color(size_t i) { + printf("\x1b[%c%cm", (i % 8) ? '3' : '9', (char)(i % 8 + '0')); +} + +static void dump(size_t cols, size_t group, uint8_t flags, FILE *file) { uint8_t buf[cols]; size_t offset = 0, len = 0; for (;;) { @@ -34,6 +39,8 @@ void dump(size_t cols, size_t group, uint8_t flags, FILE *file) { len = fread(buf, 1, sizeof(buf), file); if (!len) break; + if (flags & FLAG_COLOR) printf("\x1b[39m"); + if ((flags & FLAG_SKIP) && len == sizeof(buf)) { static bool skip; if (allZero(buf, len)) { @@ -50,6 +57,7 @@ void dump(size_t cols, size_t group, uint8_t flags, FILE *file) { for (size_t i = 0; i < len; ++i) { if (group && i && !(i % group)) printf(" "); + if (flags & FLAG_COLOR) color(i); printf("%02x ", buf[i]); } @@ -59,6 +67,7 @@ void dump(size_t cols, size_t group, uint8_t flags, FILE *file) { } printf(" "); for (size_t i = 0; i < len; ++i) { + if (flags & FLAG_COLOR) color(i); printf("%c", isprint(buf[i]) ? buf[i] : '.'); } } @@ -68,7 +77,7 @@ void dump(size_t cols, size_t group, uint8_t flags, FILE *file) { } } -void undump(FILE *file) { +static void undump(FILE *file) { uint8_t byte; int match; while (1 == (match = fscanf(file, " %hhx", &byte))) { @@ -84,16 +93,17 @@ int main(int argc, char *argv[]) { char *path = NULL; int opt; - while (0 < (opt = getopt(argc, argv, "ac:fg:hku"))) { + while (0 < (opt = getopt(argc, argv, "aCc:fg:hku"))) { switch (opt) { case 'a': flags ^= FLAG_ASCII; break; + case 'C': flags ^= FLAG_COLOR; break; case 'f': flags ^= FLAG_OFFSET; break; case 'k': flags ^= FLAG_SKIP; break; case 'u': flags ^= FLAG_UNDUMP; break; case 'c': cols = strtoul(optarg, NULL, 10); break; case 'g': group = strtoul(optarg, NULL, 10); break; default: - fprintf(stderr, "usage: xx [-afku] [-c cols] [-g group] [file]\n"); + fprintf(stderr, "usage: xx [-aCfku] [-c cols] [-g group] [file]\n"); return (opt == 'h') ? EX_OK : EX_USAGE; } } |