// License along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <assert.h>
+#include <errno.h>
+#include <getopt.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include <yac/dec.h>
#include <yac/err.h>
#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)
}
}
-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)
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,
items->offsets[idx],
NoColour ? "" : ColourReset);
}
+ struct YACItem *item = &(items->list[idx]);
if (item->atom.typ == YACItemEOC) {
indent--;
assert(indent >= 0);
}
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,
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;
}
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;
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;