summary refs log tree commit diff
path: root/www/git.causal.agency/filter.c
blob: 7c7e9320943274d62c41a5f705eefa3879c143c8 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <err.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

#define Q(...) #__VA_ARGS__

#define MANDOC_OPTIONS "fragment,man=%N.%S,includes=../tree/%I"

static int about(int argc, char *argv[]) {
	if (argc < 2) return 1;
	if (!fnmatch("README.[1-9]", argv[1], 0)) {
		execlp("mandoc", "mandoc", "-T", "html", "-O", MANDOC_OPTIONS, NULL);
		err(127, "mandoc");
	} else if (!fnmatch("*.[1-9]", argv[1], 0)) {
		execlp(
			"mandoc", "mandoc", "-T", "html", "-O", "toc," MANDOC_OPTIONS, NULL
		);
		err(127, "mandoc");
	} else {
		execlp("hilex", "hilex", "-l", "text", "-f", "html", "-o", "pre", NULL);
		err(127, "hilex");
	}
}

static int email(void) {
	size_t cap = 0;
	char *buf = NULL;
	if (getline(&buf, &cap, stdin) < 0) err(1, "getline");
	if (buf[0] == 'C' && !strncmp(&buf[strcspn(buf, " ")], " McEnroe", 8)) {
		printf("June%s", &buf[strcspn(buf, " ")]);
	} else {
		printf("%s", buf);
	}
	return 0;
}

static int owner(void) {
	printf(Q(<a href="https://liberapay.com/june/donate"><img alt="Donate using Liberapay" src="https://liberapay.com/assets/widgets/donate.svg"></a>));
	return 0;
}

#define CTAGS_PATTERN "*.[chlmy]"
#define TEMPLATE "/tmp/filter.XXXXXXXXXX"

static char tmp[PATH_MAX];
static char tags[] = TEMPLATE;
static void cleanup(void) {
	unlink(tmp);
	unlink(tags);
}

static int source(int argc, char *argv[]) {
	if (argc < 2) return 1;
	if (
		strcmp("Makefile", argv[1]) &&
		strcmp(".profile", argv[1]) &&
		strcmp(".shrc", argv[1]) &&
		fnmatch(CTAGS_PATTERN, argv[1], 0) &&
		fnmatch("*.mk", argv[1], 0) &&
		fnmatch("*.[1-9]", argv[1], 0) &&
		fnmatch("*.sh", argv[1], 0)
	) {
		execlp("hilex", "hilex", "-t", "-n", argv[1], "-f", "html", NULL);
		err(127, "hilex");
	}

	const char *ext = strrchr(argv[1], '.');
	if (!strcmp(argv[1], ".profile") || !strcmp(argv[1], ".shrc")) {
		ext = ".sh";
	} else if (!strcmp(argv[1], "Makefile")) {
		ext = ".mk";
	} else if (!ext) {
		ext = "";
	}

	snprintf(tmp, sizeof(tmp), TEMPLATE "%s", ext);
	int fd = mkstemps(tmp, strlen(ext));
	if (fd < 0) err(1, "%s", tmp);
	atexit(cleanup);

	char buf[4096];
	for (ssize_t len; 0 < (len = read(STDIN_FILENO, buf, sizeof(buf)));) {
		if (write(fd, buf, len) < 0) err(1, "%s", tmp);
	}
	if (close(fd) < 0) err(1, "%s", tmp);

	fd = mkstemp(tags);
	if (fd < 0) err(1, "%s", tags);
	close(fd);
	pid_t pid = fork();
	if (pid < 0) err(1, "fork");
	if (!pid) {
		if (!fnmatch(CTAGS_PATTERN, argv[1], 0)) {
			execlp("ctags", "ctags", "-w", "-f", tags, tmp, NULL);
			warn("ctags");
		} else {
			execlp("mtags", "mtags", "-f", tags, tmp, NULL);
			warn("mtags");
		}
		_exit(127);
	}
	int status;
	if (wait(&status) < 0) err(1, "wait");

	int rw[2];
	if (pipe(rw) < 0) err(1, "pipe");
	pid = fork();
	if (pid < 0) err(1, "fork");
	if (!pid) {
		dup2(rw[1], STDOUT_FILENO);
		close(rw[0]);
		close(rw[1]);
		execlp("hilex", "hilex", "-f", "html", tmp, NULL);
		warn("hilex");
		_exit(127);
	}
	pid = fork();
	if (pid < 0) err(1, "fork");
	if (!pid) {
		dup2(rw[0], STDIN_FILENO);
		close(rw[0]);
		close(rw[1]);
		execlp("htagml", "htagml", "-im", "-f", tags, tmp, NULL);
		warn("htagml");
		_exit(127);
	}
	close(rw[0]);
	close(rw[1]);

	if (wait(&status) < 0) err(1, "wait");
	if (wait(&status) < 0) err(1, "wait");
	return status;
}

int main(int argc, char *argv[]) {
#ifdef __OpenBSD__
	int error;
	switch (getprogname()[0]) {
		break; case 'a': error = pledge("stdio exec", NULL);
		break; case 's': error = pledge("stdio tmppath proc exec", NULL);
		break; default:  error = pledge("stdio", NULL);
	}
	if (error) err(1, "pledge");
#endif
	switch (getprogname()[0]) {
		case 'a': return about(argc, argv);
		case 'e': return email();
		case 'o': return owner();
		case 's': return source(argc, argv);
		default: return 1;
	}
}