From: Sergey Matveev Date: Thu, 21 Nov 2024 12:38:11 +0000 (+0300) Subject: Human-convenient arguments X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=88ab2dd8c8fe7a71c609b08d91098958dfc4683c63fe74d8e1041bc14d951685;p=keks.git Human-convenient arguments --- diff --git a/cyac/cmd/print-items/print-items.c b/cyac/cmd/print-items/print-items.c index 2cd9e9a..c1cc0d1 100644 --- a/cyac/cmd/print-items/print-items.c +++ b/cyac/cmd/print-items/print-items.c @@ -14,11 +14,14 @@ // License along with this program. If not, see . #include +#include +#include #include #include #include #include #include +#include #include #include @@ -29,32 +32,20 @@ #include "../lib/printai.h" #include "../lib/uuid.h" -static const size_t maxStrLen = 40; +static const char *ColourRed = "\x1b[31m"; +static const char *ColourGreen = "\x1b[32m"; +static const char *ColourYellow = "\x1b[33m"; +static const char *ColourBlue = "\x1b[34m"; +static const char *ColourMagenta = "\x1b[35m"; +static const char *ColourCyan = "\x1b[36m"; +static const char *ColourWhite = "\x1b[37m"; +static const char *ColourReset = "\x1b[0m"; -static const char - *ColourRed = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - "\x1b[31m"; -static const char - *ColourGreen = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - "\x1b[32m"; -static const char - *ColourYellow = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - "\x1b[33m"; -static const char - *ColourBlue = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - "\x1b[34m"; -static const char - *ColourMagenta = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - "\x1b[35m"; -static const char - *ColourCyan = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - "\x1b[36m"; -static const char - *ColourWhite = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - "\x1b[37m"; -static const char - *ColourReset = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - "\x1b[0m"; +static size_t MaxStrLen = 40; +static bool NoColour = false; +static bool NoOffsets = false; +static int OffDigits = 0; +static char OffFmt[16] = {0}; static void printIndent(const size_t indent) @@ -64,14 +55,19 @@ printIndent(const size_t indent) } } -static bool NoColour = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - false; -static bool NoOffsets = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - false; -static int OffDigits = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - 0; -static char OffFmt[16] = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - {0}; +static void +usage(void) +{ + fputs( + "Usage: print-items [OPTIONS] FILE\n" + " --max-str-len X -- limit output of strings to X (40 by default)\n" + " --do-encode -- encode the data after decoding, just to test\n" + " --no-offsets -- do not print offsets\n" + " --no-totals -- do noit print memory consumption totals\n" + "", + stderr); + exit(EXIT_FAILURE); +} static enum YACErr printer( // NOLINT(misc-no-recursion) @@ -82,16 +78,15 @@ printer( // NOLINT(misc-no-recursion) size_t listIdx, const char *mapKey) { - fprintf( - stdout, - "%s%zd%s ", - NoColour ? "" : ColourBlue, - indent, - NoColour ? "" : ColourReset); - struct YACItem *item = &(items->list[idx]); if (NoOffsets) { fputs(" ", stdout); } else { + fprintf( + stdout, + "%s%zd%s ", + NoColour ? "" : ColourBlue, + indent, + NoColour ? "" : ColourReset); fprintf( stdout, OffFmt, @@ -99,6 +94,7 @@ printer( // NOLINT(misc-no-recursion) items->offsets[idx], NoColour ? "" : ColourReset); } + struct YACItem *item = &(items->list[idx]); if (item->atom.typ == YACItemEOC) { indent--; assert(indent >= 0); @@ -235,7 +231,7 @@ printer( // NOLINT(misc-no-recursion) } case YACItemBin: { const size_t l = - (item->atom.v.str.len > maxStrLen) ? maxStrLen : item->atom.v.str.len; + (item->atom.v.str.len > MaxStrLen) ? MaxStrLen : item->atom.v.str.len; str = HexEnc(item->atom.v.str.ptr, l); fprintf( stdout, @@ -244,16 +240,16 @@ printer( // NOLINT(misc-no-recursion) item->atom.v.str.len, NoColour ? "" : ColourReset, str, - (item->atom.v.str.len > maxStrLen) ? "..." : ""); + (item->atom.v.str.len > MaxStrLen) ? "..." : ""); free(str); break; } case YACItemStr: { const size_t l = - (item->atom.v.str.len > maxStrLen) ? maxStrLen : item->atom.v.str.len; + (item->atom.v.str.len > MaxStrLen) ? MaxStrLen : item->atom.v.str.len; str = strndup((const char *)item->atom.v.str.ptr, l); fprintf( - stdout, "\"%s%s\"\n", str, (item->atom.v.str.len > maxStrLen) ? "..." : ""); + stdout, "\"%s%s\"\n", str, (item->atom.v.str.len > MaxStrLen) ? "..." : ""); free(str); break; } @@ -274,18 +270,62 @@ printer( // NOLINT(misc-no-recursion) int main(int argc, char **argv) { - if (argc < 2) { - fprintf(stderr, "Usage: %s FILE\n", argv[0]); - return EXIT_FAILURE; + bool doEncode = false; + bool noTotals = false; + NoColour = getenv("NO_COLOR") != NULL; + struct option longopts[] = { + {"max-str-len", required_argument, NULL, 'a'}, + {"do-encode", no_argument, NULL, 'b'}, + {"no-offsets", no_argument, NULL, 'c'}, + {"no-totals", no_argument, NULL, 'd'}, + {NULL, 0, NULL, 0}}; + int ch = 0; + for (;;) { + ch = getopt_long(argc, argv, "", longopts, NULL); + if (ch == -1) { + break; + } + switch (ch) { + case 'a': { // max-str-len + errno = 0; + long tmp = strtol(optarg, NULL, 10); + if (errno != 0) { + fprintf(stderr, "--max-str-len: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + if (tmp < 0) { + fputs("--max-str-len: is negative\n", stderr); + exit(EXIT_FAILURE); + } + MaxStrLen = (size_t)tmp; + break; + } + case 'b': // do-encode + doEncode = true; + break; + case 'c': // no-offsets + NoOffsets = true; + break; + case 'd': // no-totals + noTotals = true; + break; + case '?': + default: + usage(); + } + } + argc -= optind; + argv += optind; + if (argc < 1) { + usage(); } + size_t len = 0; unsigned char *buf = NULL; - if (!Mmap(&buf, &len, argv[1])) { - exit(EXIT_FAILURE); // NOLINT(concurrency-mt-unsafe) + if (!Mmap(&buf, &len, argv[0])) { + exit(EXIT_FAILURE); } - NoColour = getenv("NO_COLOR") != NULL; // NOLINT(concurrency-mt-unsafe) - NoOffsets = getenv("NO_OFFSETS") != NULL; // NOLINT(concurrency-mt-unsafe) - OffDigits = (int)(1 + floor(log10((double)len))); + OffDigits = NoOffsets ? -1 : (int)(1 + floor(log10((double)len))); snprintf(OffFmt, sizeof OffFmt, "%%s%%0%dzd%%s ", OffDigits); struct YACItems items; @@ -309,11 +349,13 @@ main(int argc, char **argv) fprintf(stderr, "err: %s\n", YACErr2Str(err)); return EXIT_FAILURE; } - printf( - "items: %zu size: %zu\n", - items.len, - items.len * (sizeof(struct YACItem) + (NoOffsets ? 0 : sizeof(size_t)))); - if (getenv("DO_ENCODE") != NULL) { // NOLINT(concurrency-mt-unsafe) + if (!noTotals) { + printf( + "items: %zu size: %zu\n", + items.len, + items.len * (sizeof(struct YACItem) + (NoOffsets ? 0 : sizeof(size_t)))); + } + if (doEncode) { unsigned char *dst = malloc(len); assert(dst != NULL); off = 0;