summary refs log tree commit diff
path: root/mhdr.c
blob: ffb49d94fd2c28bcf5ff3ec0ed9d10bcdbb52497 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <sys/stat.h>
#include <sys/types.h>

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "blaze822.h"

static size_t l;
static char *hdr;

void
header(char *file)
{
	struct message *msg;

	msg = blaze822(file);
	if (!msg)
		return;

	char *v = blaze822_hdr_(msg, hdr, l);
	if (v)
		printf("%s\n", v);
}

static void
printhdr(char *hdr)
{
	int uc = 1;

	while (*hdr && *hdr != ':') {
		putc(uc ? toupper(*hdr) : *hdr, stdout);
		uc = (*hdr == '-');
		hdr++;
	}
	fputs(hdr, stdout);
	fputc('\n', stdout);
}

void
headerall(char *file)
{
	struct message *msg;

	msg = blaze822(file);
	if (!msg)
		return;

	char *h = 0;
	while ((h = blaze822_next_header(msg, h))) {
		char d[4096];
		blaze822_decode_rfc2047(d, h, sizeof d, "UTF-8");

		printhdr(d);
	}
}

int
main(int argc, char *argv[])
{
	void (*cb)(char *) = headerall;

	if (argc >= 2 && argv[1][0] == '-') {
		l = strlen(argv[1])+1;
		hdr = malloc(l);
		hdr[0] = 0;
		char *s = hdr+1;
		char *t = argv[1]+1;
		while (*t)
			*s++ = tolower(*t++);
		*s = ':';

		cb = header;
		argc--;
		argv++;
	}

	if (argc == 1 && isatty(0)) {
		char *cur[] = { "." };
		blaze822_loop(1, cur, cb);
	} else {
		blaze822_loop(argc-1, argv+1, cb);
	}
	
	return 0;
}