summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--blaze822.c28
-rw-r--r--blaze822.h2
-rw-r--r--hdr.c30
-rw-r--r--scan.c22
-rw-r--r--thread.c21
5 files changed, 22 insertions, 81 deletions
diff --git a/blaze822.c b/blaze822.c
index 45165be..f55c00d 100644
--- a/blaze822.c
+++ b/blaze822.c
@@ -354,33 +354,25 @@ blaze822_hdr_(struct message *mesg, char *hdr, size_t hdrlen)
 	return v;
 }
 
-#if 0
 int
-main(int argc, char *argv[])
+blaze822_loop(int argc, char *argv[], void (*cb)(char *))
 {
-	char *s;
-	
 	char *line = 0;
 	size_t linelen = 0;
-	int read;
-
+	ssize_t rd;
 	int i = 0;
 
-	if (argc == 1 || (argc == 2 && strcmp(argv[1], "-") == 0)) {
-		while ((read = getdelim(&line, &linelen, '\n', stdin)) != -1) {
-			if (line[read-1] == '\n') line[read-1] = 0;
-			oneline(file);
+	if (argc == 0 || (argc == 1 && strcmp(argv[0], "-") == 0)) {
+		while ((rd = getdelim(&line, &linelen, '\n', stdin)) != -1) {
+			if (line[rd-1] == '\n')
+				line[rd-1] = 0;
+			cb(line);
 			i++;
 		}
 	} else {
-		for (i = 1; i < argc; i++) {
-			oneline(file);
-		}
-		i--;
+		for (i = 0; i < argc; i++)
+			cb(argv[i]);
 	}
 
-	printf("%d mails scanned\n", i);
-
-	return 0;
+	return i;
 }
-#endif
diff --git a/blaze822.h b/blaze822.h
index feff554..15de6f5 100644
--- a/blaze822.h
+++ b/blaze822.h
@@ -7,6 +7,8 @@ void blaze822_free(struct message *mesg);
 char *blaze822_hdr_(struct message *mesg, const char *hdr, size_t len);
 #define blaze822_hdr(mesg, hdr) blaze822_hdr_(mesg, "\0" hdr ":", 2+strlen((hdr)))
 
+int blaze822_loop(int, char **, void (*)(char *));
+
 time_t blaze822_date(char *);
 char *blaze822_addr(char *, char **, char **);
 int decode_rfc2047 (char *, char *, size_t);
diff --git a/hdr.c b/hdr.c
index 63485b4..03ac985 100644
--- a/hdr.c
+++ b/hdr.c
@@ -13,8 +13,11 @@
 
 #include "blaze822.h"
 
+static size_t l;
+static char *hdr;
+
 void
-header(char *hdr, size_t l, char *file)
+header(char *file)
 {
 	struct message *msg;
 
@@ -30,14 +33,8 @@ header(char *hdr, size_t l, char *file)
 int
 main(int argc, char *argv[])
 {
-	char *line = 0;
-	size_t linelen = 0;
-	int read;
-
-	int i = 0;
-
-	size_t l = strlen(argv[1])+2;
-        char *hdr = malloc(l);
+	l = strlen(argv[1])+2;
+        hdr = malloc(l);
 	hdr[0] = 0;
 	char *s = hdr+1;
 	char *t = argv[1];
@@ -45,19 +42,8 @@ main(int argc, char *argv[])
 		*s++ = tolower(*t++);
 	*s = ':';
 
-	if (argc == 2 || (argc == 3 && strcmp(argv[1], "-") == 0)) {
-		while ((read = getdelim(&line, &linelen, '\n', stdin)) != -1) {
-			if (line[read-1] == '\n') line[read-1] = 0;
-			header(hdr, l, line);
-			i++;
-		}
-	} else {
-		for (i = 2; i < argc; i++) {
-			header(hdr, l, argv[i]);
-		}
-		i--;
-	}
-
+	int i = blaze822_loop(argc-2, argv+2, header);
+	
 	printf("%d mails scanned\n", i);
 	
 	return 0;
diff --git a/scan.c b/scan.c
index ec811c6..74e0b14 100644
--- a/scan.c
+++ b/scan.c
@@ -120,27 +120,7 @@ oneline(char *file)
 int
 main(int argc, char *argv[])
 {
-	char *s;
-	
-	char *line = 0;
-	size_t linelen = 0;
-	int read;
-
-	int i = 0;
-
-	if (argc == 1 || (argc == 2 && strcmp(argv[1], "-") == 0)) {
-		while ((read = getdelim(&line, &linelen, '\n', stdin)) != -1) {
-			if (line[read-1] == '\n') line[read-1] = 0;
-//			printf("%s\n", line);
-			oneline(line);
-			i++;
-		}
-	} else {
-		for (i = 1; i < argc; i++) {
-			oneline(argv[i]);
-		}
-		i--;
-	}
+	int i = blaze822_loop(argc-1, argv+1, oneline);
 
 	printf("%d mails scanned\n", i);
 
diff --git a/thread.c b/thread.c
index 8eec9dc..6384a1b 100644
--- a/thread.c
+++ b/thread.c
@@ -272,26 +272,7 @@ print_tree(struct container *c, int depth)
 int
 main(int argc, char *argv[])
 {
-	char *s;
-	
-	char *line = 0;
-	size_t linelen = 0;
-	int read;
-
-	int i = 0;
-
-	if (argc == 1 || (argc == 2 && strcmp(argv[1], "-") == 0)) {
-		while ((read = getdelim(&line, &linelen, '\n', stdin)) != -1) {
-			if (line[read-1] == '\n') line[read-1] = 0;
-			thread(line);
-			i++;
-		}
-	} else {
-		for (i = 1; i < argc; i++) {
-			thread(argv[i]);
-		}
-		i--;
-	}
+	int i = blaze822_loop(argc-1, argv+1, thread);
 
 	printf("%d mails scanned\n", i);