summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2023-10-15 19:54:14 +0200
committerLeah Neukirchen <leah@vuxu.org>2023-10-15 19:54:14 +0200
commitc9fcb6debe082259160c5b641f7df7cb952cce09 (patch)
tree55c193ca54e91355e9719953033e1ceec7ac5833
parent0949ed4e42c107ae1f3ad51a52339878a4f61c18 (diff)
downloadmico-c9fcb6debe082259160c5b641f7df7cb952cce09.tar.gz
mico-c9fcb6debe082259160c5b641f7df7cb952cce09.tar.xz
mico-c9fcb6debe082259160c5b641f7df7cb952cce09.zip
mico-dump: add filters and -t to list contents
-rw-r--r--mico-dump.c79
1 files 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 <arpa/inet.h>
 #include <assert.h>
+#include <fnmatch.h>
+#include <getopt.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -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);