From c9fcb6debe082259160c5b641f7df7cb952cce09 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Sun, 15 Oct 2023 19:54:14 +0200 Subject: mico-dump: add filters and -t to list contents --- mico-dump.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 6 deletions(-) diff --git a/mico-dump.c b/mico-dump.c index 467db45..aea7138 100644 --- a/mico-dump.c +++ b/mico-dump.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include #include @@ -84,17 +86,44 @@ get1(struct bitreader *input) return v; } +char * +mystrccpy(char *dst, char *src, int c) +{ + while ((*dst = *src)) { + if (*dst == c) { + *dst = 0; + src++; + return src; + } + dst++; + src++; + } + + return 0; +} + #define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) +static int tflag; + int main(int argc, char *argv[]) { - if (argc < 2) { - fprintf(stderr, "usage: %s input.zip\n", argv[0]); - exit(-1); + int c; + while ((c = getopt(argc, argv, "t")) != -1) { + switch (c) { + case 't': tflag = 1; break; + case '?': + usage: + fprintf(stderr, "usage: %s [-t] input.zip [filter...]\n", argv[0]); + exit(-1); + } } - zip = zip_open(argv[1], ZIP_RDONLY, 0); + if (argc - optind < 1) + goto usage; + + zip = zip_open(argv[optind], ZIP_RDONLY, 0); int entries = zip_get_num_entries(zip, 0); @@ -113,6 +142,46 @@ main(int argc, char *argv[]) *s++ = '}'; *s = 0; + int keep = 1; + for (int j = optind + 1; j < argc; j++) { + if (strchr(argv[j], '=')) { // filter on label + char label[255]; + char *s = strchr(name, '{'); + if (!s) { + keep = 0; + break; + } + s++; + + int any = 0; + do { + s = mystrccpy(label, s, ','); + if (fnmatch(argv[j], label, 0) == 0) { + any = 1; + break; + } + } while(s); + if (!any) + keep = 0; + } else { // filter on name + char series[255]; + mystrccpy(series, name, '{'); + + if (fnmatch(argv[j], series, 0) == FNM_NOMATCH) { + keep = 0; + break; + } + } + } + + if (!keep) + continue; + + if (tflag) { + printf("%s\n", name); + continue; + } + char header[8]; uint64_t len, lenv; int ret = zip_fread(ts.input, &header, sizeof header); @@ -149,8 +218,6 @@ main(int argc, char *argv[]) lenv = ntohll(lenv); - printf("len=%ld lenv=%d\n", len, lenv); - if (len != lenv) { fprintf(stderr, "time and value length don't agree\n"); exit(-1); -- cgit 1.4.1