From a097f78066067f1a4a994fa18fd6b9bec70c4b81 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Wed, 4 Oct 2017 18:47:15 -0400 Subject: Rewrite xx.c --- bin/xx.c | 105 ++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 54 insertions(+), 51 deletions(-) (limited to 'bin') diff --git a/bin/xx.c b/bin/xx.c index 645162f1..cfc6cb9f 100644 --- a/bin/xx.c +++ b/bin/xx.c @@ -25,59 +25,68 @@ #include #include -static bool allZero(const uint8_t *buf, size_t len) { - for (size_t i = 0; i < len; ++i) +static bool zero(const uint8_t *buf, size_t len) { + for (size_t i = 0; i < len; ++i) { if (buf[i]) return false; + } return true; } -enum { - FLAG_ASCII = 1 << 0, - FLAG_OFFSET = 1 << 1, - FLAG_SKIP = 1 << 2, - FLAG_UNDUMP = 1 << 3, -}; - -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 (;;) { - offset += len; - len = fread(buf, 1, sizeof(buf), file); - if (!len) break; - - if ((flags & FLAG_SKIP) && len == sizeof(buf)) { - static bool skip; - if (allZero(buf, len)) { +static struct { + size_t cols; + size_t group; + bool ascii; + bool offset; + bool skip; +} options = { 16, 8, true, true, false }; + +static void dump(FILE *file) { + bool skip = false; + + uint8_t buf[options.cols]; + size_t offset = 0; + for (size_t len; (len = fread(buf, 1, sizeof(buf), file)); offset += len) { + + if (options.skip) { + if (zero(buf, len)) { if (!skip) printf("*\n"); skip = true; continue; + } else { + skip = false; } - skip = false; } - if (flags & FLAG_OFFSET) { + if (options.offset) { printf("%08zx: ", offset); } - for (size_t i = 0; i < len; ++i) { - if (group && i && !(i % group)) printf(" "); - printf("%02x ", buf[i]); + for (size_t i = 0; i < sizeof(buf); ++i) { + if (options.group) { + if (i && !(i % options.group)) { + printf(" "); + } + } + if (i < len) { + printf("%02hhx ", buf[i]); + } else { + printf(" "); + } } - if (flags & FLAG_ASCII) { - for (size_t i = len; i < cols; ++i) { - printf((group && !(i % group)) ? " " : " "); - } + if (options.ascii) { printf(" "); for (size_t i = 0; i < len; ++i) { - if (group && i && !(i % group)) printf(" "); + if (options.group) { + if (i && !(i % options.group)) { + printf(" "); + } + } printf("%c", isprint(buf[i]) ? buf[i] : '.'); } } printf("\n"); - if (len < sizeof(buf)) break; } } @@ -87,41 +96,35 @@ static void undump(FILE *file) { while (1 == (match = fscanf(file, " %hhx", &byte))) { printf("%c", byte); } - if (match == 0) errx(EX_DATAERR, "invalid input"); + if (!match) errx(EX_DATAERR, "invalid input"); } int main(int argc, char *argv[]) { - size_t cols = 16; - size_t group = 8; - uint8_t flags = FLAG_ASCII | FLAG_OFFSET; + bool reverse = false; char *path = NULL; int opt; - while (0 < (opt = getopt(argc, argv, "ac:fg:hku"))) { + while (0 < (opt = getopt(argc, argv, "ac:g:rsz"))) { switch (opt) { - case 'a': flags ^= FLAG_ASCII; 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"); - return (opt == 'h') ? EX_OK : EX_USAGE; + case 'a': options.ascii ^= true; break; + case 'c': options.cols = strtoul(optarg, NULL, 0); break; + case 'g': options.group = strtoul(optarg, NULL, 0); break; + case 'r': reverse = true; break; + case 's': options.offset ^= true; break; + case 'z': options.skip ^= true; break; + default: return EX_USAGE; } } - if (!cols) return EX_USAGE; - if (argc > optind) { - path = argv[optind]; - } + if (argc > optind) path = argv[optind]; + if (!options.cols) return EX_USAGE; FILE *file = path ? fopen(path, "r") : stdin; if (!file) err(EX_NOINPUT, "%s", path); - if (flags & FLAG_UNDUMP) { + if (reverse) { undump(file); } else { - dump(cols, group, flags, file); + dump(file); } if (ferror(file)) err(EX_IOERR, "%s", path); -- cgit 1.4.1 >Open youtube and twitch with mpvJune McEnroe 2021-02-09Add volume control bindings to cwmJune McEnroe 2021-02-09Use w3m to open other URLsJune McEnroe 2021-02-09Disable mouse mode in xtermJune McEnroe 2021-02-09Add pbcopy, pbpaste, open dispatch scriptsJune McEnroe 2021-02-09Use flags for pbd client behaviourJune McEnroe 2021-02-08Add macOS-like copy and paste to xtermJune McEnroe 2021-02-08Only update clock script every minuteJune McEnroe 2021-02-08Use 4M- for window resizing in cwmJune McEnroe 2021-02-08Tweak trackpad scaling, mouse accelerationJune McEnroe 2021-02-08Use xsel in up and add it do install.shJune McEnroe 2021-02-07Swap root window coloursJune McEnroe 2021-02-07Add -X flag to install X stuff on OpenBSDJune McEnroe 2021-02-07Adjust brightness by smaller incrementsJune McEnroe 2021-02-07Fix cwm window cycling, move big by defaultJune McEnroe 2021-02-07Use class names for Foreground, Background, BorderColorJune McEnroe 2021-02-07Add simple battery status and clock to xsessionJune McEnroe 2021-02-07Set cursor theme and sizeJune McEnroe 2021-02-07Use scrot for up -s if no screencaptureJune McEnroe 2021-02-07Enable mouse acceleration in XJune McEnroe 2021-02-07Set colours for Xt and cwmJune McEnroe 2021-02-07Set urgency on bell in xtermJune McEnroe 2021-02-07Add bindings for brightness controlJune McEnroe 2021-02-07Set X key repeat rateJune McEnroe 2021-02-07Bump font size to 12June McEnroe 2021-02-07Fully configure and rebind cwmJune McEnroe 2021-02-07Add BintiJune McEnroe 2021-02-07Finish configuring xtermJune McEnroe 2021-02-06Enable tapping, reverse scroll, set scaling in wsconsctlJune McEnroe 2021-02-06Set root window to black on purple snowJune McEnroe 2021-02-06Add xmodmap configurationJune McEnroe 2021-02-06Add initial OpenBSD X configurationJune McEnroe 2021-02-06Add xterm output to schemeJune McEnroe