blob: fa434169261adcccb5ddced0023cf637719bd619 (plain)
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
/*
 * Output filters, the hook that lets a site pass page fragments through an
 * external program or an embedded Lua script on their way to the browser. A
 * filter takes over stdout between an open and a close call, so at most one
 * can be active at a time. The exec and Lua backends both hide behind the
 * struct cgit_filter call table, so callers never learn which is in use.
 */

#ifndef CGIT_FILTER_H
#define CGIT_FILTER_H

#include <stdarg.h>
#include <stdio.h>

typedef enum {
	ABOUT, COMMIT, SOURCE, EMAIL, TRAILER, AUTH
} filter_type;

struct cgit_filter {
	int (*open)(struct cgit_filter *, va_list ap);
	int (*close)(struct cgit_filter *);
	void (*fprintfp)(struct cgit_filter *, FILE *, const char *prefix);
	void (*cleanup)(struct cgit_filter *);
	int argument_count;
};

struct cgit_exec_filter {
	struct cgit_filter base;
	char *cmd;
	char **argv;
	int old_stdout;
	int pid;
};

extern struct cgit_filter *cgit_new_filter(const char *cmd,
					   filter_type filtertype);
extern void cgit_exec_filter_init(struct cgit_exec_filter *filter, char *cmd,
				  char **argv);

/*
 * Redirect stdout into the filter and pass it the variadic arguments its type
 * expects, then hand stdout back and return the filter's exit status.
 */
extern int cgit_open_filter(struct cgit_filter *filter, ...);
extern int cgit_close_filter(struct cgit_filter *filter);

// Describe the filter the way cgitrc would have configured it.
extern void cgit_fprintf_filter(struct cgit_filter *filter, FILE *f,
				const char *prefix);

extern void cgit_init_filters(void);
extern void cgit_cleanup_filters(void);

#endif // CGIT_FILTER_H